Line 1... |
Line 1... |
1 |
<?php
|
1 |
<?php
|
2 |
include_once("library.php");
|
2 |
include_once("library.php");
|
3 |
include_once('reports.php');
|
3 |
include_once('reports.php');
|
4 |
include_once( './database.php' );
|
4 |
include_once( './database.php' );
|
5 |
|
5 |
|
- |
|
6 |
function logIt ( $message ) {
|
- |
|
7 |
//return;
|
- |
|
8 |
$LOGFILE = '/home/rodolico/www/web/computer_asset_manager_v1/logs/debug.log';
|
- |
|
9 |
$fp = fopen( $LOGFILE , 'a');
|
- |
|
10 |
fwrite($fp, "$message\n" );
|
- |
|
11 |
fclose($fp);
|
- |
|
12 |
}
|
6 |
|
13 |
|
7 |
/*
|
14 |
/*
|
8 |
* function designed to handle input from a form. If the input is
|
15 |
* function designed to handle input from a form. If the input is
|
9 |
* unset, will retrun the $default value.
|
16 |
* unset, will retrun the $default value.
|
10 |
* Otherwise, will filter the value based on $filter
|
17 |
* Otherwise, will filter the value based on $filter
|
Line 33... |
Line 40... |
33 |
* First, it will pass it through get_magic_quotes_gpc,
|
40 |
* First, it will pass it through get_magic_quotes_gpc,
|
34 |
* then will run through mysql_real_escape_string
|
41 |
* then will run through mysql_real_escape_string
|
35 |
*
|
42 |
*
|
36 |
* For strings, will encapsulate in quotes
|
43 |
* For strings, will encapsulate in quotes
|
37 |
* Dates will attempt a conversion, then change to YYYY-MM-DD and encapsulate in quotes
|
44 |
* Dates will attempt a conversion, then change to YYYY-MM-DD and encapsulate in quotes
|
- |
|
45 |
* if default is the constant now(), will pass that through to MySQL
|
38 |
* DateTime will perform the same, but change to YYYY-MM-DD HH:MM:SS
|
46 |
* DateTime will perform the same, but change to YYYY-MM-DD HH:MM:SS
|
39 |
* Integer and Floats are passed through builtins intval and floatval
|
47 |
* Integer and Floats are passed through builtins intval and floatval
|
40 |
* Boolean only checks the first character, a '0', 'f' and 'n' denoting false
|
48 |
* Boolean only checks the first character, a '0', 'f' and 'n' denoting false
|
41 |
* all else denoting true. The result is converted based on the variable
|
49 |
* all else denoting true. The result is converted based on the variable
|
42 |
* $falsetrue, with the first char denoting false and the second denoting true
|
50 |
* $falsetrue, with the first char denoting false and the second denoting true
|
Line 54... |
Line 62... |
54 |
$value = mysql_real_escape_string( $value );
|
62 |
$value = mysql_real_escape_string( $value );
|
55 |
$value = strlen( $value ) > 0 ? "'$value'" : $default;
|
63 |
$value = strlen( $value ) > 0 ? "'$value'" : $default;
|
56 |
break;
|
64 |
break;
|
57 |
case 'date' :
|
65 |
case 'date' :
|
58 |
case 'd' :
|
66 |
case 'd' :
|
59 |
if ( $value != 'null' ) {
|
67 |
if ( $value != 'null' && $value != 'now()' ) {
|
60 |
$result = strtotime( $value );
|
68 |
$result = strtotime( $value );
|
61 |
$value = ( $result === false ) ? $default : "'" . Date( 'Y-m-d', $result) . "'";
|
69 |
$value = ( $result === false ) ? $default : "'" . Date( 'Y-m-d', $result) . "'";
|
62 |
}
|
70 |
}
|
63 |
break;
|
71 |
break;
|
64 |
case 'datetime':
|
72 |
case 'datetime':
|
65 |
case 'timestamp':
|
73 |
case 'timestamp':
|
66 |
case 'dt':
|
74 |
case 'dt':
|
67 |
if ( $value != 'null' ) {
|
75 |
if ( $value != 'null' && $value != 'now()' ) {
|
68 |
$result = strtotime( $value );
|
76 |
$result = strtotime( $value );
|
69 |
$value = ( $result === false ) ? $default : "'" . Date( 'Y-m-d H:i:s', $result) . "'";
|
77 |
$value = ( $result === false ) ? $default : "'" . Date( 'Y-m-d H:i:s', $result) . "'";
|
70 |
}
|
78 |
}
|
71 |
break;
|
79 |
break;
|
72 |
case 'integer':
|
80 |
case 'integer':
|
Line 95... |
Line 103... |
95 |
}
|
103 |
}
|
96 |
return $string;
|
104 |
return $string;
|
97 |
}
|
105 |
}
|
98 |
|
106 |
|
99 |
function makeFileName ( $name, $device_id, $client_id, $site_id ) {
|
107 |
function makeFileName ( $name, $device_id, $client_id, $site_id ) {
|
100 |
|
- |
|
101 |
// we are getting an index. There is a small chance we could end up with duplicates, but since the file
|
108 |
// we are getting an index. There is a small chance we could end up with duplicates, but since the file
|
102 |
// name will not consist solely of the index, it should not cause a collision.
|
109 |
// name will not consist solely of the index, it should not cause a collision.
|
103 |
$thisIndex = getOneDBValue( "select theValue from _system where group_name= 'Files' and key_name = 'last index'" );
|
110 |
$thisIndex = getOneDBValue( "select theValue from _system where group_name= 'Files' and key_name = 'last index'" );
|
104 |
$thisIndex++;
|
111 |
$thisIndex++;
|
105 |
doSQL( "update _system set theValue = '$thisIndex' where group_name= 'Files' and key_name = 'last index'" );
|
112 |
doSQL( "update _system set theValue = '$thisIndex' where group_name= 'Files' and key_name = 'last index'" );
|
Line 116... |
Line 123... |
116 |
$nameOnDisk .= 'c-';
|
123 |
$nameOnDisk .= 'c-';
|
117 |
$nameOnDisk .= getOneDBValue( "select name from client where client_id = $client_id" );
|
124 |
$nameOnDisk .= getOneDBValue( "select name from client where client_id = $client_id" );
|
118 |
} else {
|
125 |
} else {
|
119 |
return array( 'valid' => false, 'message' => 'Error: not able to find client/site/device info' );
|
126 |
return array( 'valid' => false, 'message' => 'Error: not able to find client/site/device info' );
|
120 |
}
|
127 |
}
|
- |
|
128 |
logIt( "File name set to $nameOnDisk before processing" );
|
121 |
// add the extension
|
129 |
// add the extension
|
122 |
$nameOnDisk .= '.' . pathinfo($name, PATHINFO_EXTENSION);
|
130 |
$nameOnDisk .= '.' . pathinfo($name, PATHINFO_EXTENSION);
|
- |
|
131 |
logIt( "File name set to $nameOnDisk after adding extension" );
|
123 |
// and calculate the relative path to be added to the Files root path value
|
132 |
// and calculate the relative path to be added to the Files root path value
|
124 |
$nameOnDisk = getRelativePath( $nameOnDisk ) . $nameOnDisk;
|
133 |
$nameOnDisk = getRelativePath( $nameOnDisk ) . $nameOnDisk;
|
- |
|
134 |
logIt( "File name set to $nameOnDisk after adding rel path" );
|
125 |
return array( 'valid' => true, 'filename' => $nameOnDisk );
|
135 |
return array( 'valid' => true, 'filename' => $nameOnDisk );
|
126 |
} // function saveFile
|
136 |
} // function saveFile
|
127 |
|
137 |
|
128 |
|
138 |
|
129 |
/*
|
139 |
/*
|
Line 189... |
Line 199... |
189 |
return getOneDBValue( "select file_mime_type_id from file_mime_type where extension = $value" );
|
199 |
return getOneDBValue( "select file_mime_type_id from file_mime_type where extension = $value" );
|
190 |
}
|
200 |
}
|
191 |
return null;
|
201 |
return null;
|
192 |
}
|
202 |
}
|
193 |
|
203 |
|
- |
|
204 |
function addFile ( $fields ) {
|
194 |
function addFile ( $nameOnDisk, $device_id, $client_id, $site_id, $mime_type, $name, $description ) {
|
205 |
logIt( "entering addFile with fields set to\n" . print_r( $fields, true ) );
|
- |
|
206 |
|
- |
|
207 |
// file_id is not set in any query, so get rid of it
|
- |
|
208 |
$file_id = $fields['file_id'];
|
- |
|
209 |
unset( $fields['file_id'] );
|
- |
|
210 |
|
- |
|
211 |
// based on device, site or client, two fields are created
|
195 |
if ( $device_id ) {
|
212 |
if ( $fields['device_id'] ) {
|
196 |
$owner_type = 'd';
|
213 |
$fields['owner_type'] = 'd';
|
197 |
$owner_id = $device_id;
|
214 |
$fields['owner_id'] = $fields['device_id'];
|
198 |
} elseif ( $site_id ) {
|
215 |
} elseif ( $fields['site_id'] ) {
|
199 |
$owner_type = 's';
|
216 |
$fields['owner_type'] = 's';
|
200 |
$owner_id = $site_id;
|
217 |
$fields['owner_id'] = $fields['site_id'];
|
201 |
} elseif ( $client_id ) {
|
218 |
} elseif ( $fields['client_id'] ) {
|
202 |
$owner_type = 'c';
|
219 |
$fields['owner_type'] = 'c';
|
203 |
$owner_id = $client_id;
|
220 |
$fields['owner_id'] = $fields['client_id'];
|
204 |
}
|
221 |
}
|
- |
|
222 |
// these are not actually part of the database, so trash them
|
- |
|
223 |
unset( $fields['client_id'] );
|
- |
|
224 |
unset( $fields['site_id'] );
|
- |
|
225 |
unset( $fields['device_id'] );
|
- |
|
226 |
|
205 |
if ( ! isset( $mime_type ) ) {
|
227 |
if ( ! isset( $fields['mime_type'] ) ) {
|
206 |
$extension = pathinfo( $nameOnDisk, PATHINFO_EXTENSION);
|
228 |
$extension = pathinfo( $fields['name_on_disk'], PATHINFO_EXTENSION);
|
207 |
$mime_type = getOneDBValue( "select mime_type from file_mime_type where extension = '$extension'" );
|
229 |
$fields['mime_type'] = getOneDBValue( "select mime_type from file_mime_type where extension = '$extension'" );
|
208 |
} // if we need to find the mime_type
|
230 |
} // if we need to find the mime_type
|
209 |
$nameOnDisk = makeSafeSQLConstant( $nameOnDisk );
|
231 |
foreach ( $fields as $key => $value ) {
|
210 |
$description = makeSafeSQLConstant( $description );
|
232 |
if ( strpos( $key, 'date' ) ) { // field name has date in it
|
211 |
$mime_type = makeSafeSQLConstant( $mime_type );
|
233 |
$fields[$key] = makeSafeSQLConstant( $value, 'd' );
|
212 |
$owner_type = makeSafeSQLConstant( $owner_type );
|
234 |
} else {
|
213 |
$name = makeSafeSQLConstant( $name );
|
235 |
$fields[$key] = makeSafeSQLConstant( $value );
|
- |
|
236 |
}
|
214 |
|
237 |
}
|
215 |
$file_mime_type_id = getOneDBValue( "select file_mime_type_id from file_mime_type where mime_type = $mime_type" );
|
238 |
$fields['file_mime_type_id'] = getOneDBValue( "select file_mime_type_id from file_mime_type where mime_type = $fields[mime_type]" );
|
- |
|
239 |
unset( $fields['mime_type'] );
|
216 |
if ( ! $file_mime_type_id ) $file_mime_type_id = 'null';
|
240 |
if ( ! $fields['file_mime_type_id'] ) $fields['file_mime_type_id'] = 'null';
|
- |
|
241 |
if ( $file_id ) { // we are updating an existing record
|
- |
|
242 |
$query = array();
|
- |
|
243 |
foreach ( $fields as $key => $value ) {
|
- |
|
244 |
$query[] = "$key = $value";
|
- |
|
245 |
}
|
217 |
$query = "insert into file ( name,owner_type,owner_id,description, file_mime_type_id, name_on_disk, added_date )
|
246 |
$query = 'update file set ' . implode( ',', $query ) . " where file_id = $file_id";
|
- |
|
247 |
} else { // we are adding a new record
|
218 |
values ( $name, $owner_type, $owner_id, $description, $file_mime_type_id, $nameOnDisk, now() )";
|
248 |
$query = 'insert into file (' . implode( ',', array_keys( $fields ) ) . ') values (' . implode( ',', array_values( $fields ) ) . ')';
|
- |
|
249 |
}
|
- |
|
250 |
logIt( $query );
|
219 |
$result = queryDatabaseExtended( $query );
|
251 |
$result = queryDatabaseExtended( $query );
|
- |
|
252 |
if ( $result['insert_id'] )
|
220 |
return isset( $result['insert_id'] ) ? $result['insert_id'] : null;
|
253 |
return array( 'valid' => true, 'message' => "New Record $result[insert_id]" );
|
- |
|
254 |
else
|
- |
|
255 |
return array( 'valid' => false, 'message' => "Database Error" );
|
221 |
}
|
256 |
}
|
222 |
|
257 |
|
- |
|
258 |
function return_bytes($val) {
|
- |
|
259 |
$val = trim($val);
|
- |
|
260 |
$last = strtolower($val[strlen($val)-1]);
|
- |
|
261 |
switch($last)
|
- |
|
262 |
{
|
- |
|
263 |
case 'g':
|
- |
|
264 |
$val *= 1024;
|
- |
|
265 |
case 'm':
|
- |
|
266 |
$val *= 1024;
|
- |
|
267 |
case 'k':
|
- |
|
268 |
$val *= 1024;
|
- |
|
269 |
}
|
- |
|
270 |
return $val;
|
- |
|
271 |
} // return_bytes
|
223 |
|
272 |
|
- |
|
273 |
function prettyPrintBytes( $value ) {
|
- |
|
274 |
$sizes = array( '', 'kilo', 'mega', 'giga', 'tera' );
|
- |
|
275 |
while ( $value > 1024 ) {
|
- |
|
276 |
$value /= 1024;
|
- |
|
277 |
$index++;
|
- |
|
278 |
}
|
- |
|
279 |
return intval( $value ) . ' ' . $sizes[$index] . 'bytes';
|
- |
|
280 |
}
|
- |
|
281 |
|
- |
|
282 |
function maxUploadFileSize () {
|
- |
|
283 |
//select maximum upload size
|
- |
|
284 |
$max_upload = return_bytes(ini_get('upload_max_filesize'));
|
- |
|
285 |
//select post limit
|
- |
|
286 |
$max_post = return_bytes(ini_get('post_max_size'));
|
- |
|
287 |
//select memory limit
|
- |
|
288 |
$memory_limit = return_bytes(ini_get('memory_limit'));
|
- |
|
289 |
// return the smallest of them, this defines the real limit
|
- |
|
290 |
return prettyPrintBytes( min($max_upload, $max_post, $memory_limit) );
|
- |
|
291 |
} // maxUploadFileSize
|
- |
|
292 |
|
- |
|
293 |
function getFileUploadError( $error ) {
|
- |
|
294 |
$message = '';
|
- |
|
295 |
switch ( $error ) {
|
- |
|
296 |
case 0 : $message = 'There is no error, the file uploaded with success';
|
- |
|
297 |
break;
|
- |
|
298 |
case 1 : $message = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
|
- |
|
299 |
break;
|
- |
|
300 |
case 2 : $message = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
|
- |
|
301 |
break;
|
- |
|
302 |
case 3 : $message = 'The uploaded file was only partially uploaded';
|
- |
|
303 |
break;
|
- |
|
304 |
case 4 : $message = 'No file was uploaded';
|
- |
|
305 |
break;
|
- |
|
306 |
case 6 : $message = 'Missing a temporary folder';
|
- |
|
307 |
break;
|
- |
|
308 |
case 7 : $message = 'Failed to write file to disk.';
|
- |
|
309 |
break;
|
- |
|
310 |
case 8 : $message = 'A PHP extension stopped the file upload.';
|
- |
|
311 |
}
|
- |
|
312 |
return array( 'valid' => $error == 0, 'message' => $message );
|
- |
|
313 |
} // getFileUploadError
|
224 |
|
314 |
|
- |
|
315 |
function uploadFile ( $source, $nameOnDisk ) {
|
- |
|
316 |
$saveTo = getAbsolutePath( $nameOnDisk );
|
- |
|
317 |
if ( makePath( $saveTo ) ) {
|
- |
|
318 |
logIt( "Path Made - $saveTo" );
|
- |
|
319 |
logIt( "moving $source to $saveTo" );
|
- |
|
320 |
$result['valid'] = move_uploaded_file( $source, $saveTo );
|
- |
|
321 |
} else {
|
- |
|
322 |
$result = array( 'valid'=>false, 'message' => print_r(error_get_last(), true) );
|
- |
|
323 |
} // if move_uploaded_file .. else
|
- |
|
324 |
return $result;
|
- |
|
325 |
} // uploadFile
|
- |
|
326 |
|
225 |
?>
|
327 |
?>
|