99 |
rodolico |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
|
|
|
4 |
/*
|
|
|
5 |
* function will attempt to make a constant ($value) safe for SQL depending on the type.
|
|
|
6 |
*
|
|
|
7 |
* if $value is empty, $default is returned, as will happen if any of the
|
|
|
8 |
* conversions (date, datetime, etc...) fail.
|
|
|
9 |
*
|
|
|
10 |
* First, it will pass it through get_magic_quotes_gpc,
|
|
|
11 |
* then will run through mysql_real_escape_string
|
|
|
12 |
*
|
|
|
13 |
* For strings, will encapsulate in quotes
|
|
|
14 |
* Dates will attempt a conversion, then change to YYYY-MM-DD and encapsulate in quotes
|
|
|
15 |
* if default is the constant now(), will pass that through to MySQL
|
|
|
16 |
* DateTime will perform the same, but change to YYYY-MM-DD HH:MM:SS
|
|
|
17 |
* Integer and Floats are passed through builtins intval and floatval
|
|
|
18 |
* Boolean only checks the first character, a '0', 'f' and 'n' denoting false
|
|
|
19 |
* all else denoting true. The result is converted based on the variable
|
|
|
20 |
* $falsetrue, with the first char denoting false and the second denoting true
|
|
|
21 |
*/
|
|
|
22 |
function makeSafeSQLConstant ( $value, $type='S', $default='null', $falsetrue='10' ) {
|
|
|
23 |
// return $default on undefined or empty values
|
|
|
24 |
if ( ! isset( $value ) ) return $default;
|
|
|
25 |
if (strlen($value) == 0) return $default;
|
|
|
26 |
// print "Processing $value as $type with default $default<br>\n";
|
|
|
27 |
switch ( strtolower( $type ) ) {
|
|
|
28 |
case 'string' :
|
|
|
29 |
case 's' :
|
|
|
30 |
if ( get_magic_quotes_gpc() )
|
|
|
31 |
$value = stripslashes($value);
|
|
|
32 |
$value = mysql_real_escape_string( $value );
|
|
|
33 |
$value = strlen( $value ) > 0 ? "'$value'" : $default;
|
|
|
34 |
break;
|
|
|
35 |
case 'date' :
|
|
|
36 |
case 'd' :
|
|
|
37 |
if ( $value != 'null' && $value != 'now()' ) {
|
|
|
38 |
$result = strtotime( $value );
|
|
|
39 |
$value = ( $result === false ) ? $default : "'" . Date( 'Y-m-d', $result) . "'";
|
|
|
40 |
}
|
|
|
41 |
break;
|
|
|
42 |
case 'datetime':
|
|
|
43 |
case 'timestamp':
|
|
|
44 |
case 'dt':
|
|
|
45 |
if ( $value != 'null' && $value != 'now()' ) {
|
|
|
46 |
$result = strtotime( $value );
|
|
|
47 |
$value = ( $result === false ) ? $default : "'" . Date( 'Y-m-d H:i:s', $result) . "'";
|
|
|
48 |
}
|
|
|
49 |
break;
|
|
|
50 |
case 'integer':
|
|
|
51 |
case 'i' :
|
|
|
52 |
$value = intval( $value );
|
|
|
53 |
break;
|
|
|
54 |
case 'float':
|
|
|
55 |
case 'f' :
|
|
|
56 |
$value = floatval( $value );
|
|
|
57 |
break;
|
|
|
58 |
case 'bool':
|
|
|
59 |
case 'boolean':
|
|
|
60 |
case 'b' : // note, because of the way strpos works, you can not
|
|
|
61 |
// simply set $value based on the output; you MUST do
|
|
|
62 |
// as below; specifically check for false, then set the result
|
|
|
63 |
$value = strpos( '0fn', strtolower(substr( $value, 0, 1 )) ) === false ? 0 : 1;
|
|
|
64 |
$value = substr( $falsetrue, $value, 0, 1 );
|
|
|
65 |
break;
|
|
|
66 |
} // switch
|
|
|
67 |
return $value;
|
|
|
68 |
}
|
|
|
69 |
/****************************************************************************************
|
|
|
70 |
* Functions to process an array the device_attrib table
|
|
|
71 |
***************************************************************************************/
|
|
|
72 |
|
|
|
73 |
/*
|
|
|
74 |
* takes a tab delimited array of lines and turns it into an array of array
|
|
|
75 |
* Assumes first line contains the headers
|
|
|
76 |
* turns the file
|
|
|
77 |
* header1\theader2\theader3
|
|
|
78 |
* value1\tvalue2\tvalue3
|
|
|
79 |
*
|
|
|
80 |
* into
|
|
|
81 |
* [0]
|
|
|
82 |
* header1=>value1
|
|
|
83 |
* header2=>value2
|
|
|
84 |
* header3=>value3
|
|
|
85 |
*
|
|
|
86 |
* $contents is an array of lines, with each line having multiple
|
|
|
87 |
* fields delimited by $delimiter
|
|
|
88 |
*
|
|
|
89 |
*/
|
|
|
90 |
function tabDelimToArray($contents, $delimiter = "\t" ) {
|
|
|
91 |
$rows = array();
|
|
|
92 |
if ( gettype($contents) != 'array' )
|
|
|
93 |
$contents = explode( "\n", $contents );
|
|
|
94 |
$headers = explode( $delimiter, array_shift( $contents ) );
|
|
|
95 |
for ( $line = 0; $line < count( $contents ); $line++ ) {
|
|
|
96 |
if ( $contents[$line] ) {
|
|
|
97 |
$thisLine = explode( $delimiter, $contents[$line] );
|
|
|
98 |
$thisLine = preg_replace( '/^\s+/', '', $thisLine );
|
|
|
99 |
$thisLine = preg_replace( '/\s+$/', '', $thisLine );
|
|
|
100 |
$columns = array();
|
|
|
101 |
for ( $i = 0; $i < count( $headers ); $i++ ) {
|
|
|
102 |
$columns[$headers[$i]] = $thisLine[$i];
|
|
|
103 |
} // for
|
|
|
104 |
$rows[] = $columns;
|
|
|
105 |
} // if
|
|
|
106 |
} // while
|
|
|
107 |
return $rows;
|
|
|
108 |
} // function
|
|
|
109 |
|
|
|
110 |
/*
|
|
|
111 |
* getDeviceID
|
|
|
112 |
*/
|
|
|
113 |
function getDeviceID( $device, $createIfNotFound = false, $site = '', $client = '', $site_id = 0, $client_id = 0 ) {
|
|
|
114 |
$device = makeSafeSQLConstant( $device, 's', '' );
|
|
|
115 |
if ( ! $device ) return 0;
|
|
|
116 |
$device_id = getOneDBValue( "select device_id from device where name = $device" );
|
|
|
117 |
if ( $device_id ) return $device_id;
|
|
|
118 |
if ( $createIfNotFound ) {
|
|
|
119 |
if ( ! $site_id ) {
|
|
|
120 |
$site_id = findSite( $site, $client, $site_id, $client_id );
|
|
|
121 |
}
|
|
|
122 |
if ( $site_id ) {
|
|
|
123 |
$site_id = makeSafeSQLConstant( $site_id,'i','' );
|
|
|
124 |
$result = queryDatabaseExtended( "insert into device (site_id,device_type_id,name,added_date,removed_date) values( $site_id,1,$device,now(),null )" );
|
|
|
125 |
if ( $result['insert_id'] )
|
|
|
126 |
return $result[insert_id];
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
return 0;
|
|
|
130 |
} // getDeviceID
|
|
|
131 |
|
|
|
132 |
|
|
|
133 |
function getAttributeID( $attribute, $createIfNotFound = false ) {
|
|
|
134 |
$attribute = makeSafeSQLConstant( $attribute, 's', '' );
|
|
|
135 |
if ( ! $attribute ) return 0;
|
|
|
136 |
$return = getOneDBValue( "select attrib_id from attrib where name = $attribute" );
|
|
|
137 |
if ( ! $return && $createIfNotFound ) {
|
|
|
138 |
$return = queryDatabaseExtended( "insert into attrib( name, added_date, removed_date ) values ( $attribute, now(), null )" );
|
102 |
rodolico |
139 |
return $return['insert_id'] ? $return['insert_id'] : 0;
|
99 |
rodolico |
140 |
}
|
|
|
141 |
return $return;
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
/*
|
|
|
145 |
* returns true if the value is empty or null
|
|
|
146 |
*/
|
|
|
147 |
function nullOrEmpty( $str ) {
|
|
|
148 |
return ( !isset( $str) || trim($str) === '');
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
/*
|
|
|
152 |
* Takes a two dimensional array and adds/updates the values in device_attrib
|
|
|
153 |
*
|
|
|
154 |
* $contents is an array of array, where they keys for each sub-array is the field name of the table to insert/update
|
|
|
155 |
* $unique if true will replace any existing key for the device in question
|
|
|
156 |
* $createDeviceIfNotFound, if true, will create any device name found if it doesn't exist
|
|
|
157 |
* $device_id is used for the insert/update unless $device_id is a key in any row in the array
|
|
|
158 |
* $site_id is used to A) uniquely identify a device or B) create the device if $createDeviceIfNotFound is true and device not foudn
|
|
|
159 |
* $client_id is used the same way as $site_id
|
|
|
160 |
*
|
|
|
161 |
* $contents is assumed to be
|
|
|
162 |
* [0] => {
|
|
|
163 |
* [value] => string to be set/added to device_attrib table
|
|
|
164 |
* [attrib_id] => key from attrib table
|
|
|
165 |
* [attribute] => string matching name from attrib table
|
|
|
166 |
* [device_id] => key from device table
|
|
|
167 |
* [device] => string matching name from device table
|
|
|
168 |
* [site_id] => key from site table
|
|
|
169 |
* [site] => string matching name from site table
|
|
|
170 |
* [client_id] => key from client table
|
|
|
171 |
* [client] => string matching name from client table
|
|
|
172 |
* }
|
|
|
173 |
* [1] => { another set of values the same as above }
|
|
|
174 |
*
|
|
|
175 |
* The only required values are attrib_id (or attribute) and value. If this is the case, it will be added to the device from
|
|
|
176 |
* parameter $device_id
|
|
|
177 |
*
|
|
|
178 |
* If [something_id] is found, that is used. If it is null (or doesn't exist), an attempt is made to determine the proper
|
|
|
179 |
* id from the database by looking for the string. If that is null, the parameter passed to this function is used. If all of
|
|
|
180 |
* that fails, the row is returned to the caller.
|
|
|
181 |
*
|
|
|
182 |
*/
|
|
|
183 |
function parseTabDelimFile ( $contents, $createAttributeIfNotFound = false, $createDeviceIfNotFound = false, $default_device_id='', $default_site_id='', $default_client_id='' ) {
|
|
|
184 |
$data = tabDelimToArray( $contents );
|
|
|
185 |
// we'll put our SQL into an array, then dump it.
|
|
|
186 |
$sql = array();
|
102 |
rodolico |
187 |
// and track any errors here
|
|
|
188 |
$errors = array();
|
100 |
rodolico |
189 |
/*
|
|
|
190 |
print "<pre>";
|
|
|
191 |
var_dump( $createAttributeIfNotFound, $createDeviceIfNotFound, $default_device_id, $default_site_id, $default_client_id );
|
|
|
192 |
print "</pre>";
|
|
|
193 |
return $sql;
|
|
|
194 |
*/
|
99 |
rodolico |
195 |
// the following two arrays will store attributes and devices as we find them
|
|
|
196 |
// we can then FIRST look them up here, in memory, and go to database only when we don't know them
|
|
|
197 |
$attributesFromDatabase = array(); // uniquely store our attributes here
|
|
|
198 |
$deviceFromDatabase = array();
|
|
|
199 |
for ( $i = 0; $i < count( $data ); $i++ ) { // go through each line and grab fields we need
|
|
|
200 |
// get device_id
|
|
|
201 |
if ( ! $data[$i]['device_id'] ) {
|
|
|
202 |
if ( $data[$i]['device'] ) {
|
|
|
203 |
if ( isset( $deviceFromDatabase[$data[$i]['device']] ) ) {
|
|
|
204 |
$data[$i]['device_id'] = $deviceFromDatabase[$data[$i]['device']];
|
|
|
205 |
} else {
|
|
|
206 |
$data[$i]['device_id'] = getDeviceID( $data[$i]['device'], $createDeviceIfNotFound, $default_site_id, $default_client_id );
|
|
|
207 |
}
|
|
|
208 |
if ( $data[$i]['device_id'] )
|
|
|
209 |
$deviceFromDatabase[$data[$i]['device']] = $data[$i]['device_id'];
|
|
|
210 |
}
|
|
|
211 |
if ( ! $data[$i]['device_id'] ) {
|
|
|
212 |
if ( $default_device_id ) {
|
|
|
213 |
$data[$i]['device_id'] = $default_device_id;
|
|
|
214 |
} else {
|
102 |
rodolico |
215 |
$errors[] = "Can not locate device [" . $data[$i]['device'] . "] in line $i";
|
99 |
rodolico |
216 |
continue;
|
|
|
217 |
}
|
|
|
218 |
}
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
// get attribute_id
|
|
|
222 |
if ( ! $data[$i]['attrib_id'] ) {
|
|
|
223 |
if ( $data[$i]['attribute'] ) {
|
|
|
224 |
if ( isset( $attributesFromDatabase[$data[$i]['attribute']] ) ) {
|
|
|
225 |
$data[$i]['attrib_id'] = $attributesFromDatabase[$data[$i]['attribute']];
|
|
|
226 |
} else {
|
|
|
227 |
$data[$i]['attrib_id'] = getAttributeID( $data[$i]['attribute'], $createAttributeIfNotFound );
|
|
|
228 |
}
|
|
|
229 |
if ( $data[$i]['attrib_id'] )
|
|
|
230 |
$attributesFromDatabase[$data[$i]['attribute']] = $data[$i]['attrib_id'];
|
|
|
231 |
}
|
|
|
232 |
if ( ! $data[$i]['attrib_id'] ) {
|
102 |
rodolico |
233 |
$errors[] = 'Can not locate attribute [' . $data[$i]['attribute'] . "] in line $i";
|
99 |
rodolico |
234 |
continue;
|
|
|
235 |
}
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
if ( ! $data[$i]['value'] ) {
|
102 |
rodolico |
239 |
$errors[] = "No Value given for line $i, skipped";
|
99 |
rodolico |
240 |
continue;
|
|
|
241 |
}
|
|
|
242 |
|
102 |
rodolico |
243 |
$sql = array_merge( $sql, makeSQL( $data[$i] ) );
|
99 |
rodolico |
244 |
}
|
102 |
rodolico |
245 |
return array( 'errors' => $errors, 'sql' => $sql );
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
/*
|
|
|
249 |
* makeSQL
|
|
|
250 |
* creates one or more SQL statements to upload attribute/value into the database.
|
|
|
251 |
* if unique is true, will not do an insert. Instead, it will only update an existing
|
|
|
252 |
* row if device_id and attrib_id pair already match.
|
|
|
253 |
* if unique is false, will only do an insert of a new (possibly duplicated) value
|
|
|
254 |
* WARNING: If you have multiple rows with device_id/attrib_id pair, this could delete all of them.
|
|
|
255 |
*/
|
|
|
256 |
|
|
|
257 |
function makeSQL ( $data, $unique = false ) {
|
|
|
258 |
$value = makeSafeSQLConstant( $data['value'] );
|
|
|
259 |
$attrib_id = makeSafeSQLConstant( $data['attrib_id'], 'i' );
|
|
|
260 |
$device_id = makeSafeSQLConstant( $data['device_id'],'i' );
|
|
|
261 |
$sql = array();
|
|
|
262 |
// standard SQL that does an insert if the value doesn't already exist.
|
|
|
263 |
// remove any existing records
|
|
|
264 |
$sql[] =
|
|
|
265 |
"update attrib_device
|
|
|
266 |
set removed_date = now()
|
|
|
267 |
where
|
|
|
268 |
device_id = $device_id
|
|
|
269 |
and attrib_id = $attrib_id
|
|
|
270 |
and removed_date is null
|
|
|
271 |
and attrib_id in (select attrib_id from attrib where multiples is null);";
|
|
|
272 |
$sql[] =
|
|
|
273 |
"update attrib_device
|
|
|
274 |
set value = $value
|
|
|
275 |
where
|
|
|
276 |
device_id = $device_id
|
|
|
277 |
and attrib_id = $attrib_id
|
|
|
278 |
and attrib_id in (select attrib_id from attrib where multiples is null)
|
|
|
279 |
and not exists (
|
|
|
280 |
select * from (select * from attrib_device ) as m2 where device_id = $device_id and attrib_id = $attrib_id and value = $value
|
|
|
281 |
);";
|
|
|
282 |
$sql[] =
|
|
|
283 |
"insert into attrib_device ( device_id, attrib_id,value, added_date )
|
|
|
284 |
select $device_id,$attrib_id, $value, now()
|
|
|
285 |
from dual
|
|
|
286 |
where
|
|
|
287 |
not exists (
|
|
|
288 |
select *
|
|
|
289 |
from
|
|
|
290 |
attrib_device
|
|
|
291 |
where
|
|
|
292 |
device_id = $device_id
|
|
|
293 |
and attrib_id = $attrib_id
|
|
|
294 |
and value = $value
|
|
|
295 |
and removed_date is null
|
|
|
296 |
);";
|
99 |
rodolico |
297 |
return $sql;
|
102 |
rodolico |
298 |
} // getSQL
|
|
|
299 |
|
|
|
300 |
function updateDatabase ( $queries ) {
|
|
|
301 |
$count = 0;
|
|
|
302 |
for( $i = 0; $i < count( $queries ); $i++ ) {
|
|
|
303 |
$result = doSQL( $queries[$i] );
|
|
|
304 |
$count += $result['affected_rows'];
|
|
|
305 |
}
|
|
|
306 |
return "$count entries added or updated";
|
99 |
rodolico |
307 |
}
|
|
|
308 |
|
|
|
309 |
|
|
|
310 |
/*
|
|
|
311 |
* following block of code is duplicated from the files module. It should instead be placed in root/include/library.php or something
|
|
|
312 |
*/
|
|
|
313 |
|
|
|
314 |
/*
|
|
|
315 |
* function designed to handle input from a form. If the input is
|
|
|
316 |
* unset, will retrun the $default value.
|
|
|
317 |
* Otherwise, will filter the value based on $filter
|
|
|
318 |
* Some common filters are:
|
|
|
319 |
* FILTER_SANITIZE_SPECIAL_CHARS - clean up text so no HTML
|
|
|
320 |
* FILTER_SANITIZE_EMAIL - email addresses
|
|
|
321 |
* FILTER_SANITIZE_NUMBER_FLOAT - floating point numbers
|
|
|
322 |
* FILTER_SANITIZE_NUMBER_INT - integers
|
|
|
323 |
* FILTER_SANITIZE_URL - A URL
|
|
|
324 |
* http://php.net/manual/en/filter.filters.sanitize.php
|
|
|
325 |
*/
|
|
|
326 |
function cleanInput ( $value, $default = '', $filter = FILTER_DEFAULT ) {
|
|
|
327 |
// unset or empty values just get the default
|
|
|
328 |
if ( ! isset( $value ) || strlen( trim( $value ) ) == 0 ) return $default;
|
|
|
329 |
|
|
|
330 |
return filter_var( trim( $value ), $filter );
|
|
|
331 |
}
|
|
|
332 |
|
|
|
333 |
|
|
|
334 |
function return_bytes($val) {
|
|
|
335 |
$val = trim($val);
|
|
|
336 |
$last = strtolower($val[strlen($val)-1]);
|
|
|
337 |
switch($last)
|
|
|
338 |
{
|
|
|
339 |
case 'g':
|
|
|
340 |
$val *= 1024;
|
|
|
341 |
case 'm':
|
|
|
342 |
$val *= 1024;
|
|
|
343 |
case 'k':
|
|
|
344 |
$val *= 1024;
|
|
|
345 |
}
|
|
|
346 |
return $val;
|
|
|
347 |
} // return_bytes
|
|
|
348 |
|
|
|
349 |
function prettyPrintBytes( $value ) {
|
|
|
350 |
$sizes = array( '', 'kilo', 'mega', 'giga', 'tera' );
|
|
|
351 |
while ( $value > 1024 ) {
|
|
|
352 |
$value /= 1024;
|
|
|
353 |
$index++;
|
|
|
354 |
}
|
|
|
355 |
return intval( $value ) . ' ' . $sizes[$index] . 'bytes';
|
|
|
356 |
}
|
|
|
357 |
|
|
|
358 |
function maxUploadFileSize () {
|
|
|
359 |
//select maximum upload size
|
|
|
360 |
$max_upload = return_bytes(ini_get('upload_max_filesize'));
|
|
|
361 |
//select post limit
|
|
|
362 |
$max_post = return_bytes(ini_get('post_max_size'));
|
|
|
363 |
//select memory limit
|
|
|
364 |
$memory_limit = return_bytes(ini_get('memory_limit'));
|
|
|
365 |
// return the smallest of them, this defines the real limit
|
|
|
366 |
return prettyPrintBytes( min($max_upload, $max_post, $memory_limit) );
|
|
|
367 |
} // maxUploadFileSize
|
|
|
368 |
|
|
|
369 |
function getFileUploadError( $error ) {
|
|
|
370 |
$message = '';
|
|
|
371 |
switch ( $error ) {
|
|
|
372 |
case 0 : $message = 'There is no error, the file uploaded with success';
|
|
|
373 |
break;
|
|
|
374 |
case 1 : $message = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
|
|
|
375 |
break;
|
|
|
376 |
case 2 : $message = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
|
|
|
377 |
break;
|
|
|
378 |
case 3 : $message = 'The uploaded file was only partially uploaded';
|
|
|
379 |
break;
|
|
|
380 |
case 4 : $message = 'No file was uploaded';
|
|
|
381 |
break;
|
|
|
382 |
case 6 : $message = 'Missing a temporary folder';
|
|
|
383 |
break;
|
|
|
384 |
case 7 : $message = 'Failed to write file to disk.';
|
|
|
385 |
break;
|
|
|
386 |
case 8 : $message = 'A PHP extension stopped the file upload.';
|
|
|
387 |
}
|
|
|
388 |
return array( 'valid' => $error == 0, 'message' => $message );
|
|
|
389 |
} // getFileUploadError
|
|
|
390 |
|
|
|
391 |
function uploadFile ( $source, $nameOnDisk ) {
|
|
|
392 |
$saveTo = getAbsolutePath( $nameOnDisk );
|
|
|
393 |
if ( makePath( $saveTo ) ) {
|
|
|
394 |
logIt( "Path Made - $saveTo" );
|
|
|
395 |
logIt( "moving $source to $saveTo" );
|
|
|
396 |
$result['valid'] = move_uploaded_file( $source, $saveTo );
|
|
|
397 |
} else {
|
|
|
398 |
$result = array( 'valid'=>false, 'message' => print_r(error_get_last(), true) );
|
|
|
399 |
} // if move_uploaded_file .. else
|
|
|
400 |
return $result;
|
|
|
401 |
} // uploadFile
|
|
|
402 |
|
|
|
403 |
|
|
|
404 |
?>
|
|
|
405 |
|
|
|
406 |
|
|
|
407 |
?>
|