1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
<?php
/*
Copyright 2001 - 2006 Ampache.org
All Rights Reserved
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
created by RosenSama 2005
call: php quarantine_migration.php
*/
$no_session='1';
require_once('../lib/init.php');
usage();
// grab list of files from table
$sql = "SELECT id, file, user,action FROM upload WHERE action != 'quarantine'";
$db_results = mysql_query($sql, dbh());
$files = array();
$files['add'] = array();
$files['delete'] = array();
while ($results = mysql_fetch_assoc($db_results)) {
$action = $results['action'];
$files[$action][] = $results;
} // end while
/* Make sure we have write access to the upload dir */
$upload_dir = conf('upload_dir');
if (!@is_writeable($upload_dir)) {
echo "\n" . _('Error: Unable to write to') . " $upload_dir ". "\n";
exit;
}
$catalog_id = find_upload_catalog($upload_dir);
if (!$catalog_id) {
cli_out(_("Error: Upload directory not inside a catalog"));
exit;
} // we must have a valid upload dir
$catalog = new Catalog($catalog_id);
/* Itterate through the files we need to move */
foreach ($files['add'] as $data) {
/* Make sure that the target filename doesn't exist */
$target_file = $upload_dir . "/" . basename($data['file']);
if (file_exists($target_file)) {
echo 'Error: File Exists';
debug_event('file_exists','Error: $target_file already exist','1');
continue;
}
$results = copy($data['file'],$target_file);
if (!$results) { echo "Error: Unable to copy " . $data['file'] . " skipping...\n"; continue; }
/* Check the filesize to make sure it copied */
$new_sum = filesize($target_file);
$old_sum = filesize($data['file']);
if ($new_sum != $old_sum || !$new_sum) {
echo "Error: Size Inconsistency, not deleting " . $data['file'] . "\n";
continue;
}
$results = unlink($data['file']);
if (!$results) { echo "Error: Unable to delete " . $data['file'] . "\n";}
else {
echo _('Adding') . " " . $target_file . " " . _('to database') . "\n";
$catalog->add_file($target_file);
/* Remove it from the catalog */
$sql = "DELETE FROM upload WHERE id='" . $data['id'] . "'";
$db_results = mysql_query($sql, dbh());
}
} // end foreach
/* Itterate through the files we need to delete */
foreach ($files['delete'] as $data) {
$results = unlink($data['file']);
if (!$results) { echo "Error: Unable to Delete File\n"; }
else {
echo _('Deleted') . " " . $data['file'] . "\n";
$sql = "DELETE FROM upload WHERE id='" . $data['id'] . "'";
$db_results = mysql_query($sql, dbh());
}
} // end foreach
exit();
/*!
@function usage()
@discussion echo the help for this script
*/
function usage( ) {
$text = _( "
************* WARNING *************
This script will move, and
potentially delete uploaded files.
************* WARNING *************
All files marked for add will be moved to the upload directory. All files
marked for deletion will be deleted. This script must be run as a user with
sufficient rights to perform the above two functions.
\n" );
echo $text;
$text = _( "Continue? (Y/N):" );
echo $text;
// grab a character ignoring whitespace
do {
$input= fgetc( STDIN );
} while ( trim( $input ) == '' );
if(strcasecmp($input,"y") != 0 ) {
echo "\nExiting...\n";
exit;
}
} // usage()
/*!
@function cli_out()
@discussion util for error formatting
@param $text the message to be output
@param $mode to STDERR (0) or STDOUT (1, default)
*/
function cli_out( $text, $mode = 1 ) {
switch( $mode ) {
case 0:
$dest = STDERR;
$pre = _( "Error: " );
$post = _( "!\n" );
break;
case 1:
default:
$dest = STDOUT;
$pre = "";
$post = "\n";
}
fwrite( $dest, $pre . $text . $post );
} // error_out
?>
|