Xataface AJAX Upload Module 0.1
jQuery Ajax Upload Widget for Xataface
/Applications/XAMPP/xamppfiles/htdocs/nanofabrication/modules/ajax_upload/ajax_upload.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  * Xataface Depselect Module
00004  * Copyright (C) 2011  Steve Hannah <steve@weblite.ca>
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Library General Public
00017  * License along with this library; if not, write to the
00018  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
00019  * Boston, MA  02110-1301, USA.
00020  *
00021  */
00022  
00031  class modules_ajax_upload {
00038         private $baseURL = null;
00039         
00040         
00041         
00046         function __construct(){
00047                 $app = Dataface_Application::getInstance();
00048                 
00049                 
00050                 // Now work on our dependencies
00051                 $mt = Dataface_ModuleTool::getInstance();
00052                 
00053                 // We require the XataJax module
00054                 // The XataJax module activates and embeds the Javascript and CSS tools
00055                 $mt->loadModule('modules_XataJax', 'modules/XataJax/XataJax.php');
00056                 
00057                 
00058                 // Register the tagger widget with the form tool so that it responds
00059                 // to widget:type=tagger
00060                 import('Dataface/FormTool.php');
00061                 $ft = Dataface_FormTool::getInstance();
00062                 $ft->registerWidgetHandler('ajax_upload', dirname(__FILE__).'/widget.php', 'Dataface_FormTool_ajax_upload');
00063                 
00064                 $app->registerEventListener('beforeSave', array($this, 'beforeSave'));
00065                 
00066         }
00067         
00068         function block__head_slot(){
00069                 echo '<script>XATAFACE_MODULES_AJAX_UPLOAD_URL='.json_encode($this->getBaseURL()).';</script>';
00070         }
00071         
00072         
00078         public function getBaseURL(){
00079                 if ( !isset($this->baseURL) ){
00080                         $this->baseURL = Dataface_ModuleTool::getInstance()->getModuleURL(__FILE__);
00081                 }
00082                 return $this->baseURL;
00083         }
00084         
00085         
00086         
00087         public function beforeSave($event){
00088                 $record = $event[0];
00089                 if ( $record ){
00090                         foreach ($record->table()->fields(false, true, true) as $fld){
00091                                 if ( @$fld['widget']['type'] == 'ajax_upload' and $record->valueChanged($fld['name']) ){
00092                                         $this->moveUploadedFile($record, $fld['name']);
00093                                         
00094                                 }
00095                         }
00096                 }
00097         
00098         }
00099         
00100         
00101         
00102         
00103         public function moveUploadedFile(Dataface_Record $record, $fieldName){
00104         
00105                 $val = $record->val($fieldName);
00106                 if ( !preg_match('/^xftmpimg:\/\//', $val) ){
00107                         // This isn't a temp file == do nothing
00108                         return;
00109                 }
00110                 $val = substr($val, 11);
00111                 $field =& $record->table()->getField($fieldName);
00112                 $tmpPath = $field['savepath'].DIRECTORY_SEPARATOR.'uploads';
00113                 $filePath = $tmpPath.DIRECTORY_SEPARATOR.basename($val);
00114                 $infoPath = $filePath.'.info';
00115                 
00116                 if ( !file_exists($filePath) ){
00117                         throw new Exception("Upload to field $fieldName failed.  The temp file $filePath could not be found.");
00118                 }
00119                 
00120                 if ( !file_exists($infoPath) ){
00121                         throw new Exception("Upload to field $fieldName failed.  The info file $infoPath for file $filePath could not be found.");
00122                 }
00123                 
00124                 $infoArr = unserialize(file_get_contents($infoPath));
00125                 if ( !is_array($infoArr) ){
00126                         throw new Exception("Upload to field $fieldName failed.  The info file $infoPath is expected to contain an array but contained something else.");
00127                         
00128                 }
00129                 
00130                 $destFileName = basename($infoArr['name']);
00131                 if ( !$destFileName ){
00132                         throw new Exception("No file name specified for uploaded file with id $val");
00133                 }
00134                 
00135                 $pathinfo = pathinfo($destFileName);
00136                 $filebase = $pathinfo['basename'];
00137                 $extension = $pathinfo['extension'];
00138                 $savepath = $field['savepath'];
00139                 
00140                 while ( file_exists( $savepath.DIRECTORY_SEPARATOR.$destFileName) ){
00141                         $pathinfo = pathinfo($destFileName);
00142                         $filebase = $pathinfo['basename'];
00143                         $extension = $pathinfo['extension'];
00144                         
00145                         $matches = array();
00146                         if ( preg_match('/(.*)-{0,1}(\d+)$/', $filebase, $matches) ){
00147                                 $filebase = $matches[1];
00148                                 $fileindex = intval($matches[2]);
00149                         }
00150                         else {
00151                                 $fileindex = 0;
00152                                 // We should just leave the filebase the same.
00153                                 //$filebase = $filename;
00154                                 
00155                         }
00156                         if ( $filebase{strlen($filebase)-1} == '-' ) $filebase = substr($filebase,0, strlen($filebase)-1);
00157                         $fileindex++;
00158                         $filebase = $filebase.'-'.$fileindex;
00159                         $destFileName = $filebase.'.'.$extension;
00160                 }
00161                 
00162                 $infoArr['name'] = $destFileName;
00163                 
00164                 /*
00165                 No validation or permissions checking because this is the wrong place to do it.
00166                 Permissions are checked by QuickForm when the value is attempted to be changed.
00167                 $res = array();
00168                 
00169                 if ( !$this->validate($field, $infoArr, $res) ){
00170                         throw new Exception("Failed to upload file because validation failed: ".$res['message']);
00171                 }
00172                 */
00173                 
00174                 if ( !copy($filePath, $savepath.DIRECTORY_SEPARATOR.$destFileName) ){
00175                         throw new Exception("Failed to move uploaded file into upload directory.");
00176                 }
00177                 
00178                 $record->setValue($fieldName, $destFileName);
00179                 
00180                 
00181                 
00182         }
00183         
00195         function validate(&$field, $value, &$params){
00196                 
00197                 
00198                 // This bit of validation code is executed for files that have just been uploaded from the form.
00199                 // It expects the value to be an array of the form:
00200                 // eg: array('tmp_name'=>'/path/to/uploaded/file', 'name'=>'filename.txt', 'type'=>'image/gif').
00201                 
00202                 if ( !is_array(@$field['allowed_extensions']) and @$field['allowed_extensions']){
00203                         $field['allowed_extensions'] = explode(',',@$field['allowed_extensions']);
00204                 }
00205                 if ( !is_array(@$field['allowed_mimetypes']) and @$field['allowed_mimetypes'] ){
00206                         $field['allowed_mimetypes'] = explode(',',@$field['allowed_mimetypes']);
00207                 }
00208                 if ( !is_array(@$field['disallowed_extensions']) and @$field['disallowed_extensions'] ){
00209                         $field['disallowed_extensions'] = explode(',',@$field['disallowed_extensions']);
00210                 }
00211                 if ( !is_array(@$field['disallowed_mimetypes']) and @$field['disallowed_extensions']){
00212                         $field['disallowed_mimetypes'] = explode(',',@$field['disallowed_mimetypes']);
00213                 }
00214                 
00215                 $field['allowed_extensions'] = @array_map('strtolower', @$field['allowed_extensions']);
00216                 $field['allowed_mimetypes'] = @array_map('strtolower', @$field['allowed_mimetypes']);
00217                 $field['disallowed_extensions'] = @array_map('strtolower', @$field['disallowed_extensions']);
00218                 $field['disallowed_mimetypes'] = @array_map('strtolower', @$field['disallowed_mimetypes']);
00219                 // We do some special validation for file uploads
00220                 // Validate -- make sure that it is the proper mimetype and extension.
00221                 if ( is_array( @$field['allowed_mimetypes'] ) and count($field['allowed_mimetypes']) > 0 ){
00222                         if ( !in_array($value['type'], $field['allowed_mimetypes']) ){
00223                                 $params['message'] = "The file submitted in field '".$field['name']."' is not the correct type.  Received '".$value['type']."' but require one of (".implode(',', $field['allowed_mimetypes']).").";
00224                                 
00225                                 return false;
00226                         }
00227                 }
00228                 
00229                 if ( @is_array(@$field['disallowed_mimetypes']) and in_array($value['type'], $field['disallowed_mimetypes']) ){
00230                         $params['message'] = "The file submitted in field '".$fieldname."' has a restricted mime type.  The mime type received was '".$value['type']."'.";
00231                         return false;
00232                 }
00233                 
00234                 $extension = '';
00235                 $matches = array();
00236                 if ( preg_match('/\.([^\.]+)$/', $value['name'], $matches) ){
00237                         $extension = $matches[1];
00238                 }
00239                 $extension = strtolower($extension);
00240                 
00241                 
00242                 if ( is_array( @$field['allowed_extensions'] ) and count($field['allowed_extensions']) > 0 ){
00243                         if ( !in_array($extension, $field['allowed_extensions']) ){
00244                                 $params['message'] = "The file submitted does not have the correct extension.  Received file has extension '".$extension."' but the field requires either ".implode(' or ', $field['allowed_extensions']).".";
00245                                 
00246                                 return false;
00247                         }
00248                 }
00249 
00250                 if ( @is_array( @$field['disallowed_extensions'] ) and in_array($extension, $field['disallowed_extensions']) ){
00251                         $params['message'] = "The file submitted in field '".$fieldname."' has a restricted extension.  Its extension was '".$extension."' which is disallowed for this form.";
00252                         return false;
00253                 }
00254                 
00255                 if ( @$field['max_size'] and intval($field['max_size']) < intval(@$value['size']) ){
00256                         $params['message'] = "The file submitted in field '".$fieldname."' is {$value['size']} bytes which exceeds the limit of {$field['max_size']} bytes for this field.";
00257                         return false;
00258                 }
00259         
00260                 
00261                 //$delegate =& $this->getDelegate();
00262                 //if ( $delegate !== null and method_exists($delegate, $fieldname."__validate") ){
00263                 //      /*
00264                 //       *
00265                 //       * The delegate defines a custom validation method for this field.  Use it.
00266                 //       *
00267                 //       */
00268                 //      return call_user_func(array(&$delegate, $fieldname."__validate"), $this, $value, $params);
00269                 //}
00270                 return true;
00271         }
00272         
00273         
00274         function getMimeType($path){
00275         
00276                 
00277                 
00278                 
00279         
00280                 $mimetype='';
00281                 if(function_exists('finfo_open')) {
00282                         $res = finfo_open(FILEINFO_MIME); /* return mime type ala mimetype extension */
00283                         $mimetype = finfo_file($res, $path);
00284                 } else if (function_exists('mime_content_type')) {
00285                         
00286                 
00287                         $mimetype = mime_content_type($path);
00288                         
00289                 }
00290                 
00291                 
00292                 return $mimetype;
00293 
00294                 
00295 
00296         }
00297         
00298         
00299         
00300         
00301         function getThumbnail($url, $path){
00302                 if ( isset($url) ){
00303                         $baseUrl = $this->getBaseURL();
00304                 } else {
00305                         $baseUrl = dirname(__FILE__);
00306                 }
00307                                 
00308                 
00309                 $mime = $this->getMimeType($path);
00310                 if ( !$mime ) return $baseUrl.'/images/document_icon.png';
00311                 
00312                 if ( preg_match('/^image\//', $mime) ){
00313                         if ( true or preg_match('/'.preg_quote(DATAFACE_SITE_URL.'/media/photos/', '/').'/', $url) ){
00314                                 return $url .= '?max_width=128&max_height=128';
00315                         } else {
00316                                 return $baseUrl.'/images/image_icon.png';
00317                         }
00318                 } else if ( preg_match('/^audio\//', $mime) ){
00319                         return $baseUrl.'/images/audio_icon.png';
00320                 
00321                 } else if ( preg_match('/^video\//', $mime) ){
00322                         return $baseUrl.'/images/video_icon.png';
00323                 } else if ( preg_match('/msword/', $mime) ){
00324                         return $baseUrl.'/images/msword_icon.png';
00325                 
00326                 } else if ( preg_match('/midi/', $mime) ){
00327                 
00328                         return $baseUrl.'/images/midi_icon.png';
00329                         
00330                 } else if ( preg_match('/powerpoint/', $mime) ){
00331                         
00332                         return $baseUrl.'/images/ppt_icon.png';
00333                 } else if ( preg_match('/wordperfect/', $mime) ){
00334                         return $baseUrl.'/images/rtf_icon.png';
00335                 } else if ( preg_match('/excel/', $mime) ){
00336                         return $baseUrl.'/images/excel_icon.png';
00337                 } else if ( preg_match('/xml/', $mime) ){
00338                 
00339                         return $baseUrl.'/images/xml_icon.png';
00340                 } else {
00341                 
00342                         switch ($mime){
00343                         
00344                                 case 'text/html':
00345                                         return $baseUrl.'/images/html_icon.png';
00346                                 case 'text/css':
00347                                         return $baseUrl.'/images/css_icon.png';
00348                                 case 'application/x-compressed':
00349                                 case 'application/x-gzip':
00350                                 case 'multipart/x-gzip':
00351                                 case 'application/zip':
00352                                 case 'application/x-zip':
00353                                 case 'application/x-zip-compressed':
00354                                         return $baseUrl.'/images/zip_icon.png';
00355                                 case 'application/rtf':
00356                                 case 'application/x-rtf':
00357                                 case 'text/richtext':
00358                                         return $baseUrl.'/images/rtf_icon.png';
00359                                         
00360                                 default:
00361                                         return $baseUrl.'/images/document_icon.png';
00362                                         
00363                                 
00364                                         
00365                                 
00366                         }
00367                 }
00368                 
00369         }
00370 }
 All Data Structures Files Functions Variables