Subversion Repositories computer_asset_manager_v1

Rev

Rev 58 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
54 rodolico 1
<?php
2
   include_once("library.php");
3
   include_once('reports.php');
4
   include_once( './database.php' );
5
 
55 rodolico 6
 
7
   /*
8
    * function designed to handle input from a form. If the input is
9
    * unset, will retrun the $default value.
10
    * Otherwise, will filter the value based on $filter
11
    * Some common filters are:
12
    *    FILTER_SANITIZE_SPECIAL_CHARS - clean up text so no HTML
13
    *    FILTER_SANITIZE_EMAIL         - email addresses
14
    *    FILTER_SANITIZE_NUMBER_FLOAT  - floating point numbers
15
    *    FILTER_SANITIZE_NUMBER_INT    - integers
16
    *    FILTER_SANITIZE_URL           - A URL
17
    * http://php.net/manual/en/filter.filters.sanitize.php
18
    */
19
   function cleanInput ( $value, $default = '', $filter = FILTER_DEFAULT ) {
20
      // unset or empty values just get the default
21
      if ( ! isset( $value ) || strlen( trim( $value ) ) == 0 ) return $default;
22
 
23
      return filter_var( trim( $value ), $filter );
24
   }
25
 
26
 
27
   /*
28
    * function will attempt to make a constant ($value) safe for SQL depending on the type.
29
    * 
30
    * if $value is empty, $default is returned, as will happen if any of the
31
    * conversions (date, datetime, etc...) fail.
32
    * 
33
    * First, it will pass it through get_magic_quotes_gpc, 
34
    * then will run through mysql_real_escape_string
35
    * 
36
    * For strings, will encapsulate in quotes
37
    * Dates will attempt a conversion, then change to YYYY-MM-DD and encapsulate in quotes
38
    * 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
40
    * 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
42
    *    $falsetrue, with the first char denoting false and the second denoting true
43
    */
44
   function makeSafeSQLConstant ( $value, $type='S', $default='null', $falsetrue='10' ) {
45
      // return $default on undefined or empty values
46
      if ( ! isset( $value ) ) return $default;
47
      if (strlen($value) == 0) return $default;
48
      // print "Processing $value as $type with default $default<br>\n";
49
      switch ( strtolower( $type ) ) {
50
         case 'string' :
51
         case 's' : 
52
                  if ( get_magic_quotes_gpc() ) 
53
                     $value = stripslashes($value);
54
                  $value = mysql_real_escape_string( $value );
55
                  $value = strlen( $value ) > 0 ? "'$value'" : $default;
54 rodolico 56
                  break;
55 rodolico 57
         case 'date' :
58
         case 'd' :
59
                  if ( $value != 'null' ) {
60
                     $result = strtotime( $value );
61
                     $value = ( $result === false ) ? $default : "'" . Date( 'Y-m-d', $result) . "'";
62
                  }
63
                  break;
64
         case 'datetime':
65
         case 'timestamp':
66
         case 'dt': 
67
                  if ( $value != 'null' ) {
68
                     $result = strtotime( $value );
69
                     $value = ( $result === false ) ? $default : "'" . Date( 'Y-m-d H:i:s', $result) . "'";
70
                  }
71
                  break;
72
         case 'integer':
73
         case 'i' :  
74
                  $value = intval( $value );
75
                  break;
76
         case 'float':
77
         case 'f' :  
78
                  $value = floatval( $value );
79
                  break;
80
         case 'bool':
81
         case 'boolean':
82
         case 'b' :  // note, because of the way strpos works, you can not
83
                     // simply set $value based on the output; you MUST do
84
                     // as below; specifically check for false, then set the result
85
                     $value =  strpos( '0fn', strtolower(substr( $value, 0, 1 )) ) === false ? 0 : 1;
86
                     $value = substr( $falsetrue, $value, 0, 1 );
87
                     break;
88
      } // switch
89
      return $value;
90
   }
54 rodolico 91
 
92
   function lPad( $string, $count, $padChar = '0' ) {
93
      while ( strlen( $string) < $count ) {
94
         $string = '0' . $string;
95
      }
96
      return $string;
97
   }
98
 
99
   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
102
      // name will not consist solely of the index, it should not cause a collision.
59 rodolico 103
      $thisIndex = getOneDBValue( "select theValue from _system where group_name= 'Files' and key_name = 'last index'" );
54 rodolico 104
      $thisIndex++;
59 rodolico 105
      doSQL( "update _system set theValue = '$thisIndex' where group_name= 'Files' and key_name = 'last index'" );
54 rodolico 106
      $thisIndex = lPad( $thisIndex, 6 );
55 rodolico 107
      // start building the file name
54 rodolico 108
      $nameOnDisk = $thisIndex . '-';
109
      if ( $device_id ) {
110
         $nameOnDisk .= 'd-';
111
         $nameOnDisk .= getOneDBValue( "select name from device where device_id = $device_id" );
112
      } elseif ( $site_id ) {
113
         $nameOnDisk .= 's-';
114
         $nameOnDisk .= getOneDBValue( "select name from site where site_id = $site_id" );
115
      } elseif ( $client_id ) {
116
         $nameOnDisk .= 'c-';
117
         $nameOnDisk .= getOneDBValue( "select name from client where client_id = $client_id" );
118
      } else {
119
         return array( 'valid' => false, 'message' => 'Error: not able to find client/site/device info' );
120
      }
55 rodolico 121
      // add the extension
54 rodolico 122
      $nameOnDisk .= '.' . pathinfo($name, PATHINFO_EXTENSION);
59 rodolico 123
      // and calculate the relative path to be added to the Files root path value
54 rodolico 124
      $nameOnDisk = getRelativePath( $nameOnDisk ) . $nameOnDisk;
125
      return array( 'valid' => true, 'filename' => $nameOnDisk );
126
   } // function saveFile
127
 
55 rodolico 128
 
129
   /*
130
    * take the first digits of a file name (which is the index) and turn
131
    * them into a path, by breaking them into two character directories.
132
    * Thus, 123456-someFileName.something becomes
133
    * 12/34/123456-someFileName.something
134
    * however, this only returns the path part, so it will return
135
    * 12/34/
136
    */
54 rodolico 137
   function getRelativePath ( $filename ) {
138
      // the first part of the file name is the index, which is used to
139
      // calculate the path, so we'll throw away everything else.
140
      $filename = explode( '-', $filename )[0];
141
      while ( strlen( $filename ) > 2 ) {
142
         $path .= substr( $filename, 0, 2 ) . '/';
143
         $filename = substr( $filename, 2 );
144
      }
145
      return $path;
146
   }
147
 
55 rodolico 148
   /*
149
    * prepends two directory entries stored in _system table to
150
    * $filename
151
    */
152
   function getAbsolutePath ( $filename ) {
153
      $path = $_SESSION['file system root']
59 rodolico 154
                . '/' . getOneDBValue( "select theValue from _system where group_name = 'Files' and key_name = 'root path'" )
55 rodolico 155
                . '/' . $filename;
156
      return $path;
157
   }
158
 
159
   /*
160
    * returns the URL path to a file
161
    */
162
   function getURLPath ( $filename ) {
163
      $path = $_SESSION['html root']
59 rodolico 164
                . '/' . getOneDBValue( "select theValue from _system where group_name = 'Files' and key_name = 'root path'" )
55 rodolico 165
                . '/' . $filename;
166
      return $path;
167
   }
168
 
169
 
54 rodolico 170
   function makePath ( $filename ) {
171
      $path =  pathinfo( $filename, PATHINFO_DIRNAME );
172
      return is_dir( $path ) ? true : mkdir( $path, 0777, true );
173
   }
174
 
55 rodolico 175
   /*
176
    * Tries to find $value as a mime type
177
    * if $value is non-zero numeric, looks it up in the table as mime_type_id
178
    * if a string, checks for mime_type
179
    * if still not found, looks for extension
59 rodolico 180
    * returns index into file_mime_type, or 0 if not found
55 rodolico 181
    */
182
   function check_mime_type ( $value ) {
183
      if ( is_string( $value ) ) {
184
         $value = makeSafeSQLConstant( $value );
185
         // check if they passed in a mime type
59 rodolico 186
         $id = getOneDBValue( "select file_mime_type_id from file_mime_type where mime_type = $value" );
55 rodolico 187
         if ( $id ) return $id;
188
         // well, no, so check if they passed in an extension
59 rodolico 189
         return getOneDBValue( "select file_mime_type_id from file_mime_type where extension = $value" );
55 rodolico 190
      }
191
      return null;
192
   }
193
 
59 rodolico 194
   function addFile ( $nameOnDisk, $device_id, $client_id, $site_id, $mime_type, $name, $description ) {
54 rodolico 195
      if ( $device_id ) {
196
         $owner_type = 'd';
197
         $owner_id = $device_id;
198
      } elseif ( $site_id ) {
199
         $owner_type = 's';
200
         $owner_id = $site_id;
201
      } elseif ( $client_id ) {
202
         $owner_type = 'c';
203
         $owner_id = $client_id;
204
      }
205
      if ( ! isset( $mime_type ) ) {
206
         $extension = pathinfo( $nameOnDisk, PATHINFO_EXTENSION);
59 rodolico 207
         $mime_type = getOneDBValue( "select mime_type from file_mime_type where extension = '$extension'" );
54 rodolico 208
      } // if we need to find the mime_type
209
      $nameOnDisk = makeSafeSQLConstant( $nameOnDisk );
210
      $description = makeSafeSQLConstant( $description );
211
      $mime_type = makeSafeSQLConstant( $mime_type );
212
      $owner_type = makeSafeSQLConstant( $owner_type );
55 rodolico 213
      $name = makeSafeSQLConstant( $name );
54 rodolico 214
 
59 rodolico 215
      $file_mime_type_id = getOneDBValue( "select file_mime_type_id from file_mime_type where mime_type = $mime_type" );
216
      if ( ! $file_mime_type_id ) $file_mime_type_id = 'null';
217
      $query = "insert into file ( name,owner_type,owner_id,description, file_mime_type_id, name_on_disk, added_date )
218
                              values ( $name, $owner_type, $owner_id, $description, $file_mime_type_id, $nameOnDisk, now() )";
54 rodolico 219
      $result = queryDatabaseExtended( $query );
220
      return isset( $result['insert_id'] ) ? $result['insert_id'] : null;
221
   }
55 rodolico 222
 
54 rodolico 223
 
55 rodolico 224
 
54 rodolico 225
?>