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 |
/*
|
|
|
71 |
* Trims a value, then compares it to several 'null' possibilities
|
|
|
72 |
* if it is a null, returns empty string, otherwise returns value
|
|
|
73 |
*/
|
|
|
74 |
function cleanUpDMIValue( $value ) {
|
|
|
75 |
$value = trim( $value );
|
|
|
76 |
switch (strtolower( $value )) {
|
|
|
77 |
case 'unknown':
|
|
|
78 |
case 'unspecified' :
|
|
|
79 |
case 'not available':
|
|
|
80 |
case 'to be filled by o.e.m.':
|
|
|
81 |
case 'not specified': return '';
|
|
|
82 |
}
|
|
|
83 |
return $value;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/*
|
|
|
87 |
* Read value from array of hash, return array of "$outputKey\tvalue" for each entry found
|
|
|
88 |
* If nothing found, returns empty array
|
|
|
89 |
* if $unique is true, returns first non-empty value found
|
|
|
90 |
* if $outputKey is not defined, uses $key
|
|
|
91 |
*/
|
|
|
92 |
function getDMIInformation( $data, $type, $key, $outputKey = '', $unique = true ) {
|
|
|
93 |
$return = array();
|
|
|
94 |
foreach ( $data[$type] as $handle ) {
|
|
|
95 |
if ( isset( $handle[$key]) ) {
|
|
|
96 |
$a = cleanUpDMIValue( $handle[$key] );
|
|
|
97 |
if ( $a ) {
|
|
|
98 |
$return[] = ( $outputKey ? $outputKey : $key ) . "\t" . $a;
|
|
|
99 |
if ( $unique ) {
|
|
|
100 |
return $return;
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
return $return;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/*
|
|
|
109 |
* adds $value to array
|
|
|
110 |
* if $value is scalar, simply adds it
|
|
|
111 |
* if $value is array, adds every row
|
|
|
112 |
* returns modified array
|
|
|
113 |
*/
|
|
|
114 |
function mergeArrays( $array, $value ) {
|
|
|
115 |
if ( $value ) {
|
|
|
116 |
switch (gettype( $value ) ) {
|
|
|
117 |
case 'array' : foreach ( $value as $thisVal ) {
|
|
|
118 |
$array[] = $thisVal;
|
|
|
119 |
}
|
|
|
120 |
break;
|
|
|
121 |
case 'boolean' :
|
|
|
122 |
case 'integer' :
|
|
|
123 |
case 'double' :
|
|
|
124 |
case 'string' : $array[] = $value;
|
|
|
125 |
} // switch
|
|
|
126 |
}
|
|
|
127 |
return $array;
|
|
|
128 |
} //mergeArrays
|
|
|
129 |
|
|
|
130 |
/*
|
|
|
131 |
* Function will take an array of lines representing the output of a dmidecode file
|
|
|
132 |
* and pick and choose the values we want to put into CAMP attributes.
|
|
|
133 |
* It will return an array of key/value pairs to be added into the attributes table
|
|
|
134 |
*
|
|
|
135 |
* NOTE: this is a summary of values we decided to store. dmidecode files have a lot of
|
|
|
136 |
* additional information we chose not to include
|
|
|
137 |
*
|
|
|
138 |
*/
|
|
|
139 |
|
|
|
140 |
function dmidecode2array( $contents ) {
|
|
|
141 |
|
|
|
142 |
// allows us to group the dmi types by function. not implemented
|
|
|
143 |
$dmiGroups = array(
|
|
|
144 |
"bios" => "0,13",
|
|
|
145 |
"system" => "1,12,15,23,32",
|
|
|
146 |
"baseboard" => "2,10,41",
|
|
|
147 |
"chassis" => "3",
|
|
|
148 |
"processor" => "4",
|
|
|
149 |
"memory" => "5,6,16,17",
|
|
|
150 |
"cache" => "7",
|
|
|
151 |
"connector" => "8",
|
|
|
152 |
"slot" => "9"
|
|
|
153 |
);
|
|
|
154 |
|
|
|
155 |
// This contains the standard DMI types, ie no OEM stuff. For reference, not used in code
|
|
|
156 |
|
|
|
157 |
$standardDMITypes = array(
|
|
|
158 |
"0" => "BIOS",
|
|
|
159 |
"1" => "System",
|
|
|
160 |
"2" => "Baseboard",
|
|
|
161 |
"3" => "Chassis",
|
|
|
162 |
"4" => "Processor",
|
|
|
163 |
"5" => "Memory Controller",
|
|
|
164 |
"6" => "Memory Module",
|
|
|
165 |
"7" => "Cache",
|
|
|
166 |
"8" => "Port Connector",
|
|
|
167 |
"9" => "System Slots",
|
|
|
168 |
"10" => "On Board Devices",
|
|
|
169 |
"11" => "OEM Strings",
|
|
|
170 |
"12" => "System Configuration Options",
|
|
|
171 |
"13" => "BIOS Language",
|
|
|
172 |
"14" => "Group Associations",
|
|
|
173 |
"15" => "System Event Log",
|
|
|
174 |
"16" => "Physical Memory Array",
|
|
|
175 |
"17" => "Memory Device",
|
|
|
176 |
"18" => "32-bit Memory Error",
|
|
|
177 |
"19" => "Memory Array Mapped Address",
|
|
|
178 |
"20" => "Memory Device Mapped Address",
|
|
|
179 |
"21" => "Built-in Pointing Device",
|
|
|
180 |
"22" => "Portable Battery",
|
|
|
181 |
"23" => "System Reset",
|
|
|
182 |
"24" => "Hardware Security",
|
|
|
183 |
"25" => "System Power Controls",
|
|
|
184 |
"26" => "Voltage Probe",
|
|
|
185 |
"27" => "Cooling Device",
|
|
|
186 |
"28" => "Temperature Probe",
|
|
|
187 |
"29" => "Electrical Current Probe",
|
|
|
188 |
"30" => "Out-of-band Remote Access",
|
|
|
189 |
"31" => "Boot Integrity Services",
|
|
|
190 |
"32" => "System Boot",
|
|
|
191 |
"33" => "64-bit Memory Error",
|
|
|
192 |
"34" => "Management Device",
|
|
|
193 |
"35" => "Management Device Component",
|
|
|
194 |
"36" => "Management Device Threshold Data",
|
|
|
195 |
"37" => "Memory Channel",
|
|
|
196 |
"38" => "IPMI Device",
|
|
|
197 |
"39" => "Power Supply",
|
|
|
198 |
"40" => "Additional Information",
|
|
|
199 |
"41" => "Onboard Devices Extended Information",
|
|
|
200 |
"42" => "Management Controller Host Interface"
|
|
|
201 |
);
|
|
|
202 |
|
|
|
203 |
// verify this is something we can work with, First line should be the version of dmidecode found
|
|
|
204 |
if ( preg_match('/dmidecode ([0-9.]+)/', $contents[0], $results) ) {
|
|
|
205 |
$dmidecodeVersion = $results[1];
|
|
|
206 |
} else {
|
|
|
207 |
return "Not a valid dmidecode file";
|
|
|
208 |
}
|
|
|
209 |
// first, let's parse it into a hash
|
|
|
210 |
$currentHandle = '';
|
|
|
211 |
$currentType;
|
|
|
212 |
$data = array();
|
|
|
213 |
for ( $i = 0; $i < count($contents); $i++ ) {
|
|
|
214 |
if ( preg_match('/^Handle ([0-9a-zx]+).*DMI type[^0-9]*(\d+),/i', $contents[$i], $outputArray) ) {
|
|
|
215 |
//print "matched\n"; print_r($outputArray); die;
|
|
|
216 |
$currentType = $outputArray[2];
|
|
|
217 |
$currentHandle = $outputArray[1];
|
|
|
218 |
if ( isset( $standardDMITypes[$currentType] ) || array_key_exists( $currentType,$standardDMITypes ) ) {
|
|
|
219 |
$name = $contents[$i+1];
|
|
|
220 |
$data[$currentType][$currentHandle]['name'] = $name;
|
|
|
221 |
$i += 2; // skip the line with the name, and go to the next line
|
|
|
222 |
} else {
|
|
|
223 |
$currentType = $currentHandle = '';
|
|
|
224 |
}
|
|
|
225 |
}
|
|
|
226 |
if ( $currentHandle && preg_match('/^\s+(.*):\s+(.*)$/i', $contents[$i], $outputArray) ) {
|
|
|
227 |
$data[$currentType][$currentHandle][$outputArray[1]] = $outputArray[2];
|
|
|
228 |
}
|
|
|
229 |
}
|
|
|
230 |
# well, we have at least one entry, so let's go
|
|
|
231 |
$return = array();
|
|
|
232 |
// manufacturer info
|
|
|
233 |
$return = mergeArrays( $return, getDMIInformation( $data, '1','Manufacturer' ) );
|
|
|
234 |
$return = mergeArrays( $return, getDMIInformation( $data, '1','Product Name', 'Model' ) );
|
|
|
235 |
$return = mergeArrays( $return, getDMIInformation( $data, '1','Serial Number' ) );
|
|
|
236 |
$return = mergeArrays( $return, getDMIInformation( $data, '1','UUID' ) );
|
|
|
237 |
// physical machine
|
|
|
238 |
$return = mergeArrays( $return, getDMIInformation( $data, '3','Type', 'Enclosure Type' ) );
|
|
|
239 |
$return = mergeArrays( $return, getDMIInformation( $data, '3','Asset Tag', 'Enclosure Asset Tag' ) );
|
|
|
240 |
$return = mergeArrays( $return, getDMIInformation( $data, '3','Height', 'Enclosure Height' ) );
|
|
|
241 |
|
|
|
242 |
// firmware version
|
|
|
243 |
$return = mergeArrays( $return, getDMIInformation( $data, '0','Vendor', 'Firmware Vendor' ) );
|
|
|
244 |
$return = mergeArrays( $return, getDMIInformation( $data, '0','Release Date', 'Firmware Release' ) );
|
|
|
245 |
$return = mergeArrays( $return, getDMIInformation( $data, '0','Version', 'Firmware Version' ) );
|
|
|
246 |
$return = mergeArrays( $return, getDMIInformation( $data, '0','Firmware Revision', 'Firmware Revision' ) );
|
|
|
247 |
// processor information. NOTE: assumes all sockets populated and all processors the saem
|
|
|
248 |
if ( isset( $data['4'] ) ) // count the number of sockets
|
|
|
249 |
$return[] = "CPU Count\t" . count( $data['4'] );
|
|
|
250 |
$return = mergeArrays( $return, getDMIInformation( $data, '4','Family', 'CPU Family' ) );
|
|
|
251 |
$return = mergeArrays( $return, getDMIInformation( $data, '4','Manufacturer', 'CPU Manufacturer' ) );
|
|
|
252 |
$return = mergeArrays( $return, getDMIInformation( $data, '4','Version', 'CPU Version' ) );
|
|
|
253 |
$return = mergeArrays( $return, getDMIInformation( $data, '4','Current Speed', 'CPU Speed' ) );
|
|
|
254 |
$return = mergeArrays( $return, getDMIInformation( $data, '4','Core Count', 'CPU Cores (ea)' ) );
|
|
|
255 |
$return = mergeArrays( $return, getDMIInformation( $data, '4','Thread Count', 'CPU Threads (ea)' ) );
|
|
|
256 |
|
|
|
257 |
/*
|
|
|
258 |
* memory information. We want details on each slot for this, so we have to go through each entry, one at a time.
|
|
|
259 |
* For readability, we also want to take several values and turn them into one human
|
|
|
260 |
* readable string for each socket. So, we go through each entry in the memory socket, determine if it is populated
|
|
|
261 |
* then concat it together for a pretty output
|
|
|
262 |
*/
|
|
|
263 |
|
|
|
264 |
if ( isset( $data['17'] ) ) {
|
|
|
265 |
$return[] = "Memory Slots\t" . count( $data['17'] );
|
|
|
266 |
$totalRAM = 0; // we'll accumulate the total size as we find slots populated
|
|
|
267 |
foreach ( $data['17'] as $entry ) { // type 17 has one entry for every DIMM slot
|
|
|
268 |
$mem = array(); // fill this up with the values we want to turn into a string, then we'll implode it into the string
|
|
|
269 |
$mem[] = trim($entry['Locator'] ? $entry['Locator'] : ($entry['Bank Locator'] ? $entry['Bank Locator'] : '' ));
|
|
|
270 |
if ( $entry['Size'] == 'No Module Installed' ) {
|
|
|
271 |
$mem[] = 'Unpopulated';
|
|
|
272 |
} else {
|
|
|
273 |
$mem[] = trim($entry['Size']); // size can be kilo, meg, gig, tera, so convert it to mega
|
|
|
274 |
if ( preg_match('/(\d+)\s+(GB|MB|TB|kb)/i', $entry['Size'] , $outputArray) ) {
|
|
|
275 |
switch ( strtolower( $outputArray[2] ) ) {
|
|
|
276 |
case 'gb' :
|
|
|
277 |
$outputArray[1] *= 1024;
|
|
|
278 |
break;
|
|
|
279 |
case 'tb' :
|
|
|
280 |
$outputArray[1] *= 1024 * 1024;
|
|
|
281 |
break;
|
|
|
282 |
case 'kb' :
|
|
|
283 |
$outputArray[1] /= 1024;
|
|
|
284 |
break;
|
|
|
285 |
}
|
|
|
286 |
$totalRAM += $outputArray[1];
|
|
|
287 |
}
|
|
|
288 |
$mem[] = trim($entry['Form Factor']);
|
|
|
289 |
$mem[] = trim($entry['Type']);
|
|
|
290 |
$mem[] = trim($entry['Data Width']);
|
|
|
291 |
if ( isset( $entry['Configured Clock Speed']) && isset($entry['Speed']) && cleanUpDMIValue($entry['Configured Clock Speed']) && cleanUpDMIValue($entry['Speed']) )
|
|
|
292 |
$mem[] = cleanUpDMIValue($entry['Configured Clock Speed'] . '/' . cleanUpDMIValue($entry['Speed']) );
|
|
|
293 |
if ( isset($entry['Manufacturer']) && cleanUpDMIValue($entry['Manufacturer']) )
|
|
|
294 |
$mem[] = cleanUpDMIValue($entry['Manufacturer']);
|
|
|
295 |
if ( isset($entry['Part Number']) && cleanUpDMIValue( $entry['Part Number'] ) )
|
|
|
296 |
$mem[] = 'p/n ' . cleanUpDMIValue($entry['Part Number']);
|
|
|
297 |
if ( isset($entry['Serial Number']) && trim($entry['Serial Number']) != 'Not Specified' )
|
|
|
298 |
$mem[] = 's/n ' . cleanUpDMIValue($entry['Serial Number']);
|
|
|
299 |
}
|
|
|
300 |
$return[] = "Memory\t" . implode( ', ', $mem );
|
|
|
301 |
}
|
|
|
302 |
$return[] = "Memory Total\t$totalRAM MB";
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
/*
|
|
|
306 |
* power supplies. Most workstations will not even have an entry here, but for servers we will have details on each individual
|
|
|
307 |
* power supply, including the part number, serial number, etc..., so like memory, we want a detailed listing with a human
|
|
|
308 |
* readable string for each psu
|
|
|
309 |
*/
|
|
|
310 |
if ( isset( $data['39'] ) ) {
|
|
|
311 |
$return[] = "Power Supply Count\t" . count( $data['39'] );
|
|
|
312 |
foreach ( $data['39'] as $entry ) {
|
|
|
313 |
if ( preg_match('/^Present/i', $entry['Status'] ) ) {
|
|
|
314 |
$psu = array();
|
|
|
315 |
$psu[] = trim($entry['Name']);
|
|
|
316 |
$psu[] = trim($entry['Max Power Capacity']);
|
|
|
317 |
$psu[] = trim($entry['Manufacturer']);
|
|
|
318 |
$psu[] = 'p/n ' . trim($entry['Model Part Number']);
|
|
|
319 |
$psu[] = 's/n ' . trim($entry['Serial Number']);
|
|
|
320 |
$return[] = "Power Supply\t" . implode( ', ', $psu );
|
|
|
321 |
}
|
|
|
322 |
}
|
|
|
323 |
}
|
|
|
324 |
return array( $return );
|
|
|
325 |
} // dmidecode2array
|
|
|
326 |
|
|
|
327 |
/*
|
|
|
328 |
* Test code for dmidecode input
|
|
|
329 |
*/
|
|
|
330 |
/*
|
|
|
331 |
$filename = $argv[1];
|
|
|
332 |
if ( $filename ) {
|
|
|
333 |
$contents = file( $filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
|
|
|
334 |
$out = dmidecode2array( $contents );
|
|
|
335 |
print_r( $out );
|
|
|
336 |
} else {
|
|
|
337 |
print "You must pass the file as the first command line parameter\n";
|
|
|
338 |
}
|
|
|
339 |
*/
|
|
|
340 |
|
|
|
341 |
/****************************************************************************************
|
|
|
342 |
* Functions to process an array the device_attrib table
|
|
|
343 |
***************************************************************************************/
|
|
|
344 |
|
|
|
345 |
/*
|
|
|
346 |
* takes a tab delimited array of lines and turns it into an array of array
|
|
|
347 |
* Assumes first line contains the headers
|
|
|
348 |
* turns the file
|
|
|
349 |
* header1\theader2\theader3
|
|
|
350 |
* value1\tvalue2\tvalue3
|
|
|
351 |
*
|
|
|
352 |
* into
|
|
|
353 |
* [0]
|
|
|
354 |
* header1=>value1
|
|
|
355 |
* header2=>value2
|
|
|
356 |
* header3=>value3
|
|
|
357 |
*
|
|
|
358 |
* $contents is an array of lines, with each line having multiple
|
|
|
359 |
* fields delimited by $delimiter
|
|
|
360 |
*
|
|
|
361 |
*/
|
|
|
362 |
function tabDelimToArray($contents, $delimiter = "\t" ) {
|
|
|
363 |
$rows = array();
|
|
|
364 |
if ( gettype($contents) != 'array' )
|
|
|
365 |
$contents = explode( "\n", $contents );
|
|
|
366 |
$headers = explode( $delimiter, array_shift( $contents ) );
|
|
|
367 |
for ( $line = 0; $line < count( $contents ); $line++ ) {
|
|
|
368 |
if ( $contents[$line] ) {
|
|
|
369 |
$thisLine = explode( $delimiter, $contents[$line] );
|
|
|
370 |
$thisLine = preg_replace( '/^\s+/', '', $thisLine );
|
|
|
371 |
$thisLine = preg_replace( '/\s+$/', '', $thisLine );
|
|
|
372 |
$columns = array();
|
|
|
373 |
for ( $i = 0; $i < count( $headers ); $i++ ) {
|
|
|
374 |
$columns[$headers[$i]] = $thisLine[$i];
|
|
|
375 |
} // for
|
|
|
376 |
$rows[] = $columns;
|
|
|
377 |
} // if
|
|
|
378 |
} // while
|
|
|
379 |
return $rows;
|
|
|
380 |
} // function
|
|
|
381 |
|
|
|
382 |
/*
|
|
|
383 |
* getDeviceID
|
|
|
384 |
*/
|
|
|
385 |
function getDeviceID( $device, $createIfNotFound = false, $site = '', $client = '', $site_id = 0, $client_id = 0 ) {
|
|
|
386 |
$device = makeSafeSQLConstant( $device, 's', '' );
|
|
|
387 |
if ( ! $device ) return 0;
|
|
|
388 |
$device_id = getOneDBValue( "select device_id from device where name = $device" );
|
|
|
389 |
if ( $device_id ) return $device_id;
|
|
|
390 |
if ( $createIfNotFound ) {
|
|
|
391 |
if ( ! $site_id ) {
|
|
|
392 |
$site_id = findSite( $site, $client, $site_id, $client_id );
|
|
|
393 |
}
|
|
|
394 |
if ( $site_id ) {
|
|
|
395 |
$site_id = makeSafeSQLConstant( $site_id,'i','' );
|
|
|
396 |
$result = queryDatabaseExtended( "insert into device (site_id,device_type_id,name,added_date,removed_date) values( $site_id,1,$device,now(),null )" );
|
|
|
397 |
if ( $result['insert_id'] )
|
|
|
398 |
return $result[insert_id];
|
|
|
399 |
}
|
|
|
400 |
}
|
|
|
401 |
return 0;
|
|
|
402 |
} // getDeviceID
|
|
|
403 |
|
|
|
404 |
|
|
|
405 |
function getAttributeID( $attribute, $createIfNotFound = false ) {
|
|
|
406 |
$attribute = makeSafeSQLConstant( $attribute, 's', '' );
|
|
|
407 |
if ( ! $attribute ) return 0;
|
|
|
408 |
$return = getOneDBValue( "select attrib_id from attrib where name = $attribute" );
|
|
|
409 |
if ( ! $return && $createIfNotFound ) {
|
|
|
410 |
$return = queryDatabaseExtended( "insert into attrib( name, added_date, removed_date ) values ( $attribute, now(), null )" );
|
|
|
411 |
return $result['insert_id'] ? $result['insert_id'] : 0;
|
|
|
412 |
}
|
|
|
413 |
return $return;
|
|
|
414 |
}
|
|
|
415 |
|
|
|
416 |
/*
|
|
|
417 |
* returns true if the value is empty or null
|
|
|
418 |
*/
|
|
|
419 |
function nullOrEmpty( $str ) {
|
|
|
420 |
return ( !isset( $str) || trim($str) === '');
|
|
|
421 |
}
|
|
|
422 |
|
|
|
423 |
/*
|
|
|
424 |
* Takes a two dimensional array and adds/updates the values in device_attrib
|
|
|
425 |
*
|
|
|
426 |
* $contents is an array of array, where they keys for each sub-array is the field name of the table to insert/update
|
|
|
427 |
* $unique if true will replace any existing key for the device in question
|
|
|
428 |
* $createDeviceIfNotFound, if true, will create any device name found if it doesn't exist
|
|
|
429 |
* $device_id is used for the insert/update unless $device_id is a key in any row in the array
|
|
|
430 |
* $site_id is used to A) uniquely identify a device or B) create the device if $createDeviceIfNotFound is true and device not foudn
|
|
|
431 |
* $client_id is used the same way as $site_id
|
|
|
432 |
*
|
|
|
433 |
* $contents is assumed to be
|
|
|
434 |
* [0] => {
|
|
|
435 |
* [value] => string to be set/added to device_attrib table
|
|
|
436 |
* [attrib_id] => key from attrib table
|
|
|
437 |
* [attribute] => string matching name from attrib table
|
|
|
438 |
* [device_id] => key from device table
|
|
|
439 |
* [device] => string matching name from device table
|
|
|
440 |
* [site_id] => key from site table
|
|
|
441 |
* [site] => string matching name from site table
|
|
|
442 |
* [client_id] => key from client table
|
|
|
443 |
* [client] => string matching name from client table
|
|
|
444 |
* }
|
|
|
445 |
* [1] => { another set of values the same as above }
|
|
|
446 |
*
|
|
|
447 |
* The only required values are attrib_id (or attribute) and value. If this is the case, it will be added to the device from
|
|
|
448 |
* parameter $device_id
|
|
|
449 |
*
|
|
|
450 |
* If [something_id] is found, that is used. If it is null (or doesn't exist), an attempt is made to determine the proper
|
|
|
451 |
* id from the database by looking for the string. If that is null, the parameter passed to this function is used. If all of
|
|
|
452 |
* that fails, the row is returned to the caller.
|
|
|
453 |
*
|
|
|
454 |
*/
|
|
|
455 |
function parseTabDelimFile ( $contents, $createAttributeIfNotFound = false, $createDeviceIfNotFound = false, $default_device_id='', $default_site_id='', $default_client_id='' ) {
|
|
|
456 |
$data = tabDelimToArray( $contents );
|
|
|
457 |
// we'll put our SQL into an array, then dump it.
|
|
|
458 |
$sql = array();
|
|
|
459 |
// the following two arrays will store attributes and devices as we find them
|
|
|
460 |
// we can then FIRST look them up here, in memory, and go to database only when we don't know them
|
|
|
461 |
$attributesFromDatabase = array(); // uniquely store our attributes here
|
|
|
462 |
$deviceFromDatabase = array();
|
|
|
463 |
for ( $i = 0; $i < count( $data ); $i++ ) { // go through each line and grab fields we need
|
|
|
464 |
|
|
|
465 |
// get device_id
|
|
|
466 |
if ( ! $data[$i]['device_id'] ) {
|
|
|
467 |
if ( $data[$i]['device'] ) {
|
|
|
468 |
if ( isset( $deviceFromDatabase[$data[$i]['device']] ) ) {
|
|
|
469 |
$data[$i]['device_id'] = $deviceFromDatabase[$data[$i]['device']];
|
|
|
470 |
} else {
|
|
|
471 |
$data[$i]['device_id'] = getDeviceID( $data[$i]['device'], $createDeviceIfNotFound, $default_site_id, $default_client_id );
|
|
|
472 |
}
|
|
|
473 |
if ( $data[$i]['device_id'] )
|
|
|
474 |
$deviceFromDatabase[$data[$i]['device']] = $data[$i]['device_id'];
|
|
|
475 |
}
|
|
|
476 |
if ( ! $data[$i]['device_id'] ) {
|
|
|
477 |
if ( $default_device_id ) {
|
|
|
478 |
$data[$i]['device_id'] = $default_device_id;
|
|
|
479 |
} else {
|
|
|
480 |
$sql[] = "/* Can not locate device in line $i */";
|
|
|
481 |
continue;
|
|
|
482 |
}
|
|
|
483 |
}
|
|
|
484 |
}
|
|
|
485 |
|
|
|
486 |
// get attribute_id
|
|
|
487 |
if ( ! $data[$i]['attrib_id'] ) {
|
|
|
488 |
if ( $data[$i]['attribute'] ) {
|
|
|
489 |
if ( isset( $attributesFromDatabase[$data[$i]['attribute']] ) ) {
|
|
|
490 |
$data[$i]['attrib_id'] = $attributesFromDatabase[$data[$i]['attribute']];
|
|
|
491 |
} else {
|
|
|
492 |
$data[$i]['attrib_id'] = getAttributeID( $data[$i]['attribute'], $createAttributeIfNotFound );
|
|
|
493 |
}
|
|
|
494 |
if ( $data[$i]['attrib_id'] )
|
|
|
495 |
$attributesFromDatabase[$data[$i]['attribute']] = $data[$i]['attrib_id'];
|
|
|
496 |
}
|
|
|
497 |
if ( ! $data[$i]['attrib_id'] ) {
|
|
|
498 |
$sql[] = "/* Can not locate attribute in line $i */";
|
|
|
499 |
continue;
|
|
|
500 |
}
|
|
|
501 |
}
|
|
|
502 |
|
|
|
503 |
if ( ! $data[$i]['value'] ) {
|
|
|
504 |
$sql[] = "/* No Value given for line $i, skipped */";
|
|
|
505 |
continue;
|
|
|
506 |
}
|
|
|
507 |
|
|
|
508 |
$value = makeSafeSQLConstant( $data[$i]['value'] );
|
|
|
509 |
$attrib_id = makeSafeSQLConstant( $data[$i]['attrib_id'], 'i' );
|
|
|
510 |
$device_id = makeSafeSQLConstant( $data[$i]['device_id'],'i' );
|
|
|
511 |
// standard SQL that does an insert if the value doesn't already exist.
|
|
|
512 |
$sql[] =
|
|
|
513 |
"insert into attrib_device ( device_id, attrib_id,value, added_date )
|
|
|
514 |
select $device_id,$attrib_id, $value, now()
|
|
|
515 |
from dual
|
|
|
516 |
where
|
|
|
517 |
not exists (
|
|
|
518 |
select * from attrib_device join device using (device_id) join attrib using (attrib_id) where device_id = $device_id and attrib_id = $attrib_id
|
|
|
519 |
);";
|
|
|
520 |
}
|
|
|
521 |
return $sql;
|
|
|
522 |
}
|
|
|
523 |
|
|
|
524 |
|
|
|
525 |
/*
|
|
|
526 |
* following block of code is duplicated from the files module. It should instead be placed in root/include/library.php or something
|
|
|
527 |
*/
|
|
|
528 |
|
|
|
529 |
/*
|
|
|
530 |
* function designed to handle input from a form. If the input is
|
|
|
531 |
* unset, will retrun the $default value.
|
|
|
532 |
* Otherwise, will filter the value based on $filter
|
|
|
533 |
* Some common filters are:
|
|
|
534 |
* FILTER_SANITIZE_SPECIAL_CHARS - clean up text so no HTML
|
|
|
535 |
* FILTER_SANITIZE_EMAIL - email addresses
|
|
|
536 |
* FILTER_SANITIZE_NUMBER_FLOAT - floating point numbers
|
|
|
537 |
* FILTER_SANITIZE_NUMBER_INT - integers
|
|
|
538 |
* FILTER_SANITIZE_URL - A URL
|
|
|
539 |
* http://php.net/manual/en/filter.filters.sanitize.php
|
|
|
540 |
*/
|
|
|
541 |
function cleanInput ( $value, $default = '', $filter = FILTER_DEFAULT ) {
|
|
|
542 |
// unset or empty values just get the default
|
|
|
543 |
if ( ! isset( $value ) || strlen( trim( $value ) ) == 0 ) return $default;
|
|
|
544 |
|
|
|
545 |
return filter_var( trim( $value ), $filter );
|
|
|
546 |
}
|
|
|
547 |
|
|
|
548 |
|
|
|
549 |
function return_bytes($val) {
|
|
|
550 |
$val = trim($val);
|
|
|
551 |
$last = strtolower($val[strlen($val)-1]);
|
|
|
552 |
switch($last)
|
|
|
553 |
{
|
|
|
554 |
case 'g':
|
|
|
555 |
$val *= 1024;
|
|
|
556 |
case 'm':
|
|
|
557 |
$val *= 1024;
|
|
|
558 |
case 'k':
|
|
|
559 |
$val *= 1024;
|
|
|
560 |
}
|
|
|
561 |
return $val;
|
|
|
562 |
} // return_bytes
|
|
|
563 |
|
|
|
564 |
function prettyPrintBytes( $value ) {
|
|
|
565 |
$sizes = array( '', 'kilo', 'mega', 'giga', 'tera' );
|
|
|
566 |
while ( $value > 1024 ) {
|
|
|
567 |
$value /= 1024;
|
|
|
568 |
$index++;
|
|
|
569 |
}
|
|
|
570 |
return intval( $value ) . ' ' . $sizes[$index] . 'bytes';
|
|
|
571 |
}
|
|
|
572 |
|
|
|
573 |
function maxUploadFileSize () {
|
|
|
574 |
//select maximum upload size
|
|
|
575 |
$max_upload = return_bytes(ini_get('upload_max_filesize'));
|
|
|
576 |
//select post limit
|
|
|
577 |
$max_post = return_bytes(ini_get('post_max_size'));
|
|
|
578 |
//select memory limit
|
|
|
579 |
$memory_limit = return_bytes(ini_get('memory_limit'));
|
|
|
580 |
// return the smallest of them, this defines the real limit
|
|
|
581 |
return prettyPrintBytes( min($max_upload, $max_post, $memory_limit) );
|
|
|
582 |
} // maxUploadFileSize
|
|
|
583 |
|
|
|
584 |
function getFileUploadError( $error ) {
|
|
|
585 |
$message = '';
|
|
|
586 |
switch ( $error ) {
|
|
|
587 |
case 0 : $message = 'There is no error, the file uploaded with success';
|
|
|
588 |
break;
|
|
|
589 |
case 1 : $message = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
|
|
|
590 |
break;
|
|
|
591 |
case 2 : $message = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
|
|
|
592 |
break;
|
|
|
593 |
case 3 : $message = 'The uploaded file was only partially uploaded';
|
|
|
594 |
break;
|
|
|
595 |
case 4 : $message = 'No file was uploaded';
|
|
|
596 |
break;
|
|
|
597 |
case 6 : $message = 'Missing a temporary folder';
|
|
|
598 |
break;
|
|
|
599 |
case 7 : $message = 'Failed to write file to disk.';
|
|
|
600 |
break;
|
|
|
601 |
case 8 : $message = 'A PHP extension stopped the file upload.';
|
|
|
602 |
}
|
|
|
603 |
return array( 'valid' => $error == 0, 'message' => $message );
|
|
|
604 |
} // getFileUploadError
|
|
|
605 |
|
|
|
606 |
function uploadFile ( $source, $nameOnDisk ) {
|
|
|
607 |
$saveTo = getAbsolutePath( $nameOnDisk );
|
|
|
608 |
if ( makePath( $saveTo ) ) {
|
|
|
609 |
logIt( "Path Made - $saveTo" );
|
|
|
610 |
logIt( "moving $source to $saveTo" );
|
|
|
611 |
$result['valid'] = move_uploaded_file( $source, $saveTo );
|
|
|
612 |
} else {
|
|
|
613 |
$result = array( 'valid'=>false, 'message' => print_r(error_get_last(), true) );
|
|
|
614 |
} // if move_uploaded_file .. else
|
|
|
615 |
return $result;
|
|
|
616 |
} // uploadFile
|
|
|
617 |
|
|
|
618 |
|
|
|
619 |
?>
|
|
|
620 |
|
|
|
621 |
|
|
|
622 |
?>
|