![]() |
Xataface 2.0
Xataface Application Framework
|
00001 <?php 00005 class Dataface_FormTool_file { 00006 function pushValue(&$record, &$field, &$form, &$element, &$metaValues){ 00007 // The widget is a file upload widget 00008 $formTool =& Dataface_FormTool::getInstance(); 00009 $formFieldName = $element->getName(); 00010 $table =& $record->_table; 00011 $app =& Dataface_Application::getInstance(); 00012 if ( $element->isUploadedFile() ){ 00013 $cachePath = $app->_conf['cache_dir'].'/'.basename($app->_conf['_database']['name']).'-'.basename($table->tablename).'-'.basename($field['name']).'-'; 00014 00015 $cachedFiles = glob($cachePath.'*'); 00016 foreach ($cachedFiles as $cachedFile){ 00017 @unlink($cachedFile); 00018 } 00019 // Need to delete the cache for this field 00020 00021 // a file has been uploaded 00022 $val =& $element->getValue(); 00023 // eg: array('tmp_name'=>'/path/to/uploaded/file', 'name'=>'filename.txt', 'type'=>'image/gif'). 00024 if ( PEAR::isError($val) ){ 00025 $val->addUserInfo( 00026 df_translate( 00027 'scripts.Dataface.QuickForm.pushValue.ERROR_GETTING_ELEMENT_VALUE', 00028 "Error getting element value for element $field[name] in QuickForm::pushField ", 00029 array('fieldname'=>$field['name'],'line'=>0,'file'=>'') 00030 ) 00031 ); 00032 throw new Exception($val->toString(), E_USER_ERROR); 00033 return $val; 00034 } 00035 00036 00037 00038 if ( $table->isContainer($field['name']) ){ 00039 $src = $record->getContainerSource($field['name']); 00040 if ( strlen($record->strval($field['name']) ) > 0 // if there is already a valud specified in this field. 00041 and file_exists($src) // if the old file exists 00042 and is_file($src) // make sure that it is only a file we are deleting 00043 and !is_dir($src) // don't accidentally delete a directory 00044 ){ 00045 // delete the old file. 00046 if ( !is_writable($src) ){ 00047 throw new Exception("Could not save field '".$field['name']."' because there are insufficient permissions to delete the old file '".$src."'. Please check the permissions on the directory '".dirname($src)."' to make sure that it is writable by the web server.", E_USER_ERROR); 00048 } 00049 @unlink( $src); 00050 } 00051 00052 // Make sure that the file does not already exist by that name in the destination directory. 00053 $savepath = $field['savepath']; 00054 $filename = basename($val['name']); // we use basename to guard against maliciously named files. 00055 $filename = str_replace(chr(32), "_", $filename); 00056 $matches = array(); 00057 if ( preg_match('/^(.*)\.([^\.]+)$/', $filename, $matches) ){ 00058 $extension = $matches[2]; 00059 $filebase = $matches[1]; 00060 } else { 00061 $extension = ''; 00062 $filebase = $filename; 00063 } 00064 while ( file_exists( $savepath.'/'.$filename) ){ 00065 $matches = array(); 00066 if ( preg_match('/(.*)-{0,1}(\d+)$/', $filebase, $matches) ){ 00067 $filebase = $matches[1]; 00068 $fileindex = intval($matches[2]); 00069 } 00070 else { 00071 $fileindex = 0; 00072 // We should just leave the filebase the same. 00073 //$filebase = $filename; 00074 00075 } 00076 if ( $filebase{strlen($filebase)-1} == '-' ) $filebase = substr($filebase,0, strlen($filebase)-1); 00077 $fileindex++; 00078 $filebase = $filebase.'-'.$fileindex; 00079 $filename = $filebase.'.'.$extension; 00080 } 00081 00082 if (!is_writable( $field['savepath']) ){ 00083 throw new Exception( 00084 df_translate( 00085 'scripts.Dataface.QuickForm.pushValue.ERROR_INSUFFICIENT_DIRECTORY_PERMISSIONS', 00086 "Could not save field '".$field['name']."' because there are insufficient permissions to save the file to the save directory '".$field['savepath']."'. Please Check the permissions on the directory '".$field['savepath']."' to make sure that it is writable by the web server.", 00087 array('fieldname'=>$field['name'], 'savepath'=>$field['savepath']) 00088 ), E_USER_ERROR); 00089 } 00090 00091 move_uploaded_file($val['tmp_name'], $field['savepath'].'/'.$filename); 00092 chmod($field['savepath'].'/'.$filename, 0744); 00093 00094 $out = $filename; 00095 00096 00097 } else { 00098 if ( file_exists($val['tmp_name']) ){ 00099 if ( !@$app->_conf['multilingual_content'] ){ 00100 // THis is a bit of a hack. If we are using multilingual 00101 // content, then Dataface_DB will parse every query 00102 // before sending it to the database. It is better if 00103 // that query is short - so we only pass the whole value 00104 // if we are not parsing the query. 00105 $out = file_get_contents($val['tmp_name']); 00106 } else { 00107 // If we are parsing the query, then we will just store 00108 // the path to the blob. 00109 $out = $val['tmp_name']; 00110 } 00111 } else { 00112 $out = null; 00113 } 00114 } 00115 00116 if ( is_array( $metaValues ) ){ 00117 if ( isset( $field['filename'] ) ){ 00118 // store the file name in another field if one is specified 00119 $metaValues[$field['filename']] = $val['name']; 00120 00121 } 00122 if ( isset( $field['mimetype'] ) ){ 00123 // store the file mimetype in another field if one is specified 00124 $metaValues[$field['mimetype']] = $val['type']; 00125 00126 } 00127 } 00128 00129 return $out; 00130 00131 00132 } 00133 00134 if ( $table->isContainer($field['name']) ){ 00135 return $record->val($field['name']); 00136 } 00137 return null; 00138 00139 } 00140 00141 function pullValue(&$record, &$field, &$form, &$element){ 00142 /* 00143 * 00144 * We don't bother pulling the values of file widgets because it would take too long. 00145 * 00146 */ 00147 00148 $widget =& $field['widget']; 00149 $formFieldName = $element->getName(); 00150 00151 $val = null; 00152 if ( $widget['type'] == 'webcam' ) $val = $record->getValueAsString($field['name']); 00153 if ( $record->getLength($field['name']) > 0 ){ 00154 // there is already a file set, let's add a preview to it 00155 if ( $record->isImage($field['name']) ){ 00156 $element->setProperty('image_preview', df_absolute_url($record->q($field['name']))); 00157 } 00158 $element->setProperty('preview', df_absolute_url($record->q($field['name']))); 00159 //echo "Adding preview for field '$fieldname':".$record->qq($fieldname); 00160 } else { 00161 //echo "No data in field '$fieldname'"; 00162 } 00163 00164 return $val; 00165 } 00166 00167 }