54 |
rodolico |
1 |
<?php
|
|
|
2 |
include_once("library.php");
|
|
|
3 |
include_once('reports.php');
|
|
|
4 |
include_once( './database.php' );
|
|
|
5 |
|
|
|
6 |
/*
|
|
|
7 |
* function will attempt to make a constant ($value) safe for SQL depending on the type.
|
|
|
8 |
*
|
|
|
9 |
* if $value is empty, $default is returned, as will happen if any of the
|
|
|
10 |
* conversions (date, datetime, etc...) fail.
|
|
|
11 |
*
|
|
|
12 |
* First, it will pass it through get_magic_quotes_gpc,
|
|
|
13 |
* then will run through mysql_real_escape_string
|
|
|
14 |
*
|
|
|
15 |
* For strings, will encapsulate in quotes
|
|
|
16 |
* Dates will attempt a conversion, then change to YYYY-MM-DD and encapsulate in quotes
|
|
|
17 |
* DateTime will perform the same, but change to YYYY-MM-DD HH:MM:SS
|
|
|
18 |
* Integer and Floats are passed through builtins intval and floatval
|
|
|
19 |
* Boolean only checks the first character, a '0', 'f' and 'n' denoting false
|
|
|
20 |
* all else denoting true. The result is converted based on the variable
|
|
|
21 |
* $falsetrue, with the first char denoting false and the second denoting true
|
|
|
22 |
*/
|
|
|
23 |
function makeSafeSQLConstant ( $value, $type='S', $default='null', $falsetrue='10' ) {
|
|
|
24 |
if (strlen($value) == 0) // simply set any empty values to null
|
|
|
25 |
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' ) {
|
|
|
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' ) {
|
|
|
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 |
function lPad( $string, $count, $padChar = '0' ) {
|
|
|
71 |
while ( strlen( $string) < $count ) {
|
|
|
72 |
$string = '0' . $string;
|
|
|
73 |
}
|
|
|
74 |
return $string;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
function makeFileName ( $name, $device_id, $client_id, $site_id ) {
|
|
|
78 |
|
|
|
79 |
// we are getting an index. There is a small chance we could end up with duplicates, but since the file
|
|
|
80 |
// name will not consist solely of the index, it should not cause a collision.
|
|
|
81 |
$thisIndex = getOneDBValue( "select theValue from _system where group_name= 'Documents' and key_name = 'last index'" );
|
|
|
82 |
|
|
|
83 |
$thisIndex++;
|
|
|
84 |
doSQL( "update _system set theValue = '$thisIndex' where group_name= 'Documents' and key_name = 'last index'" );
|
|
|
85 |
$thisIndex = lPad( $thisIndex, 6 );
|
|
|
86 |
$nameOnDisk = $thisIndex . '-';
|
|
|
87 |
if ( $device_id ) {
|
|
|
88 |
$nameOnDisk .= 'd-';
|
|
|
89 |
$nameOnDisk .= getOneDBValue( "select name from device where device_id = $device_id" );
|
|
|
90 |
} elseif ( $site_id ) {
|
|
|
91 |
$nameOnDisk .= 's-';
|
|
|
92 |
$nameOnDisk .= getOneDBValue( "select name from site where site_id = $site_id" );
|
|
|
93 |
} elseif ( $client_id ) {
|
|
|
94 |
$nameOnDisk .= 'c-';
|
|
|
95 |
$nameOnDisk .= getOneDBValue( "select name from client where client_id = $client_id" );
|
|
|
96 |
} else {
|
|
|
97 |
return array( 'valid' => false, 'message' => 'Error: not able to find client/site/device info' );
|
|
|
98 |
}
|
|
|
99 |
$nameOnDisk .= '.' . pathinfo($name, PATHINFO_EXTENSION);
|
|
|
100 |
$nameOnDisk = getRelativePath( $nameOnDisk ) . $nameOnDisk;
|
|
|
101 |
return array( 'valid' => true, 'filename' => $nameOnDisk );
|
|
|
102 |
} // function saveFile
|
|
|
103 |
|
|
|
104 |
function getRelativePath ( $filename ) {
|
|
|
105 |
// the first part of the file name is the index, which is used to
|
|
|
106 |
// calculate the path, so we'll throw away everything else.
|
|
|
107 |
$filename = explode( '-', $filename )[0];
|
|
|
108 |
$path = getOneDBValue( "select theValue from _system where group_name = 'Documents' and key_name = 'root path'") . '/';
|
|
|
109 |
while ( strlen( $filename ) > 2 ) {
|
|
|
110 |
$path .= substr( $filename, 0, 2 ) . '/';
|
|
|
111 |
$filename = substr( $filename, 2 );
|
|
|
112 |
}
|
|
|
113 |
return $path;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
function makePath ( $filename ) {
|
|
|
117 |
$path = getOneDBValue( "select theValue from _system where group_name = 'Documents' and key_name = 'root path'" );
|
|
|
118 |
$path .= '/' . $filename;
|
|
|
119 |
$path = pathinfo( $filename, PATHINFO_DIRNAME );
|
|
|
120 |
//return $path;
|
|
|
121 |
return is_dir( $path ) ? true : mkdir( $path, 0777, true );
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
function addDocument ( $nameOnDisk, $device_id, $client_id, $site_id, $mime_type, $name, $description ) {
|
|
|
125 |
if ( $device_id ) {
|
|
|
126 |
$owner_type = 'd';
|
|
|
127 |
$owner_id = $device_id;
|
|
|
128 |
} elseif ( $site_id ) {
|
|
|
129 |
$owner_type = 's';
|
|
|
130 |
$owner_id = $site_id;
|
|
|
131 |
} elseif ( $client_id ) {
|
|
|
132 |
$owner_type = 'c';
|
|
|
133 |
$owner_id = $client_id;
|
|
|
134 |
}
|
|
|
135 |
if ( ! isset( $mime_type ) ) {
|
|
|
136 |
$extension = pathinfo( $nameOnDisk, PATHINFO_EXTENSION);
|
|
|
137 |
$mime_type = getOneDBValue( "select mime_type from document_mime_type where extension = '$extension'" );
|
|
|
138 |
} // if we need to find the mime_type
|
|
|
139 |
$nameOnDisk = makeSafeSQLConstant( $nameOnDisk );
|
|
|
140 |
$description = makeSafeSQLConstant( $description );
|
|
|
141 |
$mime_type = makeSafeSQLConstant( $mime_type );
|
|
|
142 |
$owner_type = makeSafeSQLConstant( $owner_type );
|
|
|
143 |
|
|
|
144 |
$document_mime_type_id = getOneDBValue( "select document_mime_type_id from document_mime_type where mime_type = $mime_type" );
|
|
|
145 |
$query = "insert into document ( name,owner_type,owner_id,description, document_mime_type_id, name_on_disk, added_date )
|
|
|
146 |
values ( $name, $owner_type, $owner_id, $description, $document_mime_type_id, $nameOnDisk, now() )";
|
|
|
147 |
$result = queryDatabaseExtended( $query );
|
|
|
148 |
return isset( $result['insert_id'] ) ? $result['insert_id'] : null;
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
|
|
|
152 |
?>
|