Subversion Repositories computer_asset_manager_v1

Rev

Rev 58 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 58 Rev 59
Line 98... Line 98...
98
   
98
   
99
   function makeFileName ( $name, $device_id, $client_id, $site_id ) {
99
   function makeFileName ( $name, $device_id, $client_id, $site_id ) {
100
      
100
      
101
      // we are getting an index. There is a small chance we could end up with duplicates, but since the file
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.
102
      // name will not consist solely of the index, it should not cause a collision.
103
      $thisIndex = getOneDBValue( "select theValue from _system where group_name= 'Documents' and key_name = 'last index'" );
103
      $thisIndex = getOneDBValue( "select theValue from _system where group_name= 'Files' and key_name = 'last index'" );
104
      $thisIndex++;
104
      $thisIndex++;
105
      doSQL( "update _system set theValue = '$thisIndex' where group_name= 'Documents' and key_name = 'last index'" );
105
      doSQL( "update _system set theValue = '$thisIndex' where group_name= 'Files' and key_name = 'last index'" );
106
      $thisIndex = lPad( $thisIndex, 6 );
106
      $thisIndex = lPad( $thisIndex, 6 );
107
      // start building the file name
107
      // start building the file name
108
      $nameOnDisk = $thisIndex . '-';
108
      $nameOnDisk = $thisIndex . '-';
109
      if ( $device_id ) {
109
      if ( $device_id ) {
110
         $nameOnDisk .= 'd-';
110
         $nameOnDisk .= 'd-';
Line 118... Line 118...
118
      } else {
118
      } else {
119
         return array( 'valid' => false, 'message' => 'Error: not able to find client/site/device info' );
119
         return array( 'valid' => false, 'message' => 'Error: not able to find client/site/device info' );
120
      }
120
      }
121
      // add the extension
121
      // add the extension
122
      $nameOnDisk .= '.' . pathinfo($name, PATHINFO_EXTENSION);
122
      $nameOnDisk .= '.' . pathinfo($name, PATHINFO_EXTENSION);
123
      // and calculate the relative path to be added to the Documents root path value
123
      // and calculate the relative path to be added to the Files root path value
124
      $nameOnDisk = getRelativePath( $nameOnDisk ) . $nameOnDisk;
124
      $nameOnDisk = getRelativePath( $nameOnDisk ) . $nameOnDisk;
125
      return array( 'valid' => true, 'filename' => $nameOnDisk );
125
      return array( 'valid' => true, 'filename' => $nameOnDisk );
126
   } // function saveFile
126
   } // function saveFile
127
   
127
   
128
   
128
   
Line 149... Line 149...
149
    * prepends two directory entries stored in _system table to
149
    * prepends two directory entries stored in _system table to
150
    * $filename
150
    * $filename
151
    */
151
    */
152
   function getAbsolutePath ( $filename ) {
152
   function getAbsolutePath ( $filename ) {
153
      $path = $_SESSION['file system root']
153
      $path = $_SESSION['file system root']
154
                . '/' . getOneDBValue( "select theValue from _system where group_name = 'Documents' and key_name = 'root path'" )
154
                . '/' . getOneDBValue( "select theValue from _system where group_name = 'Files' and key_name = 'root path'" )
155
                . '/' . $filename;
155
                . '/' . $filename;
156
      return $path;
156
      return $path;
157
   }
157
   }
158
      
158
      
159
   /*
159
   /*
160
    * returns the URL path to a file
160
    * returns the URL path to a file
161
    */
161
    */
162
   function getURLPath ( $filename ) {
162
   function getURLPath ( $filename ) {
163
      $path = $_SESSION['html root']
163
      $path = $_SESSION['html root']
164
                . '/' . getOneDBValue( "select theValue from _system where group_name = 'Documents' and key_name = 'root path'" )
164
                . '/' . getOneDBValue( "select theValue from _system where group_name = 'Files' and key_name = 'root path'" )
165
                . '/' . $filename;
165
                . '/' . $filename;
166
      return $path;
166
      return $path;
167
   }
167
   }
168
   
168
   
169
   
169
   
Line 175... Line 175...
175
   /*
175
   /*
176
    * Tries to find $value as a mime type
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
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
178
    * if a string, checks for mime_type
179
    * if still not found, looks for extension
179
    * if still not found, looks for extension
180
    * returns index into document_mime_type, or 0 if not found
180
    * returns index into file_mime_type, or 0 if not found
181
    */
181
    */
182
   function check_mime_type ( $value ) {
182
   function check_mime_type ( $value ) {
183
      if ( is_string( $value ) ) {
183
      if ( is_string( $value ) ) {
184
         $value = makeSafeSQLConstant( $value );
184
         $value = makeSafeSQLConstant( $value );
185
         // check if they passed in a mime type
185
         // check if they passed in a mime type
186
         $id = getOneDBValue( "select document_mime_type_id from document_mime_type where mime_type = $value" );
186
         $id = getOneDBValue( "select file_mime_type_id from file_mime_type where mime_type = $value" );
187
         if ( $id ) return $id;
187
         if ( $id ) return $id;
188
         // well, no, so check if they passed in an extension
188
         // well, no, so check if they passed in an extension
189
         return getOneDBValue( "select document_mime_type_id from document_mime_type where extension = $value" );
189
         return getOneDBValue( "select file_mime_type_id from file_mime_type where extension = $value" );
190
      }
190
      }
191
      return null;
191
      return null;
192
   }
192
   }
193
 
193
 
194
   function addDocument ( $nameOnDisk, $device_id, $client_id, $site_id, $mime_type, $name, $description ) {
194
   function addFile ( $nameOnDisk, $device_id, $client_id, $site_id, $mime_type, $name, $description ) {
195
      if ( $device_id ) {
195
      if ( $device_id ) {
196
         $owner_type = 'd';
196
         $owner_type = 'd';
197
         $owner_id = $device_id;
197
         $owner_id = $device_id;
198
      } elseif ( $site_id ) {
198
      } elseif ( $site_id ) {
199
         $owner_type = 's';
199
         $owner_type = 's';
Line 202... Line 202...
202
         $owner_type = 'c';
202
         $owner_type = 'c';
203
         $owner_id = $client_id;
203
         $owner_id = $client_id;
204
      }
204
      }
205
      if ( ! isset( $mime_type ) ) {
205
      if ( ! isset( $mime_type ) ) {
206
         $extension = pathinfo( $nameOnDisk, PATHINFO_EXTENSION);
206
         $extension = pathinfo( $nameOnDisk, PATHINFO_EXTENSION);
207
         $mime_type = getOneDBValue( "select mime_type from document_mime_type where extension = '$extension'" );
207
         $mime_type = getOneDBValue( "select mime_type from file_mime_type where extension = '$extension'" );
208
      } // if we need to find the mime_type
208
      } // if we need to find the mime_type
209
      $nameOnDisk = makeSafeSQLConstant( $nameOnDisk );
209
      $nameOnDisk = makeSafeSQLConstant( $nameOnDisk );
210
      $description = makeSafeSQLConstant( $description );
210
      $description = makeSafeSQLConstant( $description );
211
      $mime_type = makeSafeSQLConstant( $mime_type );
211
      $mime_type = makeSafeSQLConstant( $mime_type );
212
      $owner_type = makeSafeSQLConstant( $owner_type );
212
      $owner_type = makeSafeSQLConstant( $owner_type );
213
      $name = makeSafeSQLConstant( $name );
213
      $name = makeSafeSQLConstant( $name );
214
 
214
 
215
      $document_mime_type_id = getOneDBValue( "select document_mime_type_id from document_mime_type where mime_type = $mime_type" );
215
      $file_mime_type_id = getOneDBValue( "select file_mime_type_id from file_mime_type where mime_type = $mime_type" );
216
      if ( ! $document_mime_type_id ) $document_mime_type_id = 'null';
216
      if ( ! $file_mime_type_id ) $file_mime_type_id = 'null';
217
      $query = "insert into document ( name,owner_type,owner_id,description, document_mime_type_id, name_on_disk, added_date )
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, $document_mime_type_id, $nameOnDisk, now() )";
218
                              values ( $name, $owner_type, $owner_id, $description, $file_mime_type_id, $nameOnDisk, now() )";
219
      $result = queryDatabaseExtended( $query );
219
      $result = queryDatabaseExtended( $query );
220
      return isset( $result['insert_id'] ) ? $result['insert_id'] : null;
220
      return isset( $result['insert_id'] ) ? $result['insert_id'] : null;
221
   }
221
   }
222
   
222
   
223
 
223