Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
file.php
Go to the documentation of this file.
1 <?php
6  function pushValue(&$record, &$field, &$form, &$element, &$metaValues){
7  // The widget is a file upload widget
8  $formTool =& Dataface_FormTool::getInstance();
9  $formFieldName = $element->getName();
10  $table =& $record->_table;
12  if ( $element->isUploadedFile() ){
13  $cachePath = $app->_conf['cache_dir'].'/'.basename($app->_conf['_database']['name']).'-'.basename($table->tablename).'-'.basename($field['name']).'-';
14 
15  $cachedFiles = glob($cachePath.'*');
16  foreach ($cachedFiles as $cachedFile){
17  @unlink($cachedFile);
18  }
19  // Need to delete the cache for this field
20 
21  // a file has been uploaded
22  $val = $element->getValue();
23  // eg: array('tmp_name'=>'/path/to/uploaded/file', 'name'=>'filename.txt', 'type'=>'image/gif').
24  if ( PEAR::isError($val) ){
25  $val->addUserInfo(
26  df_translate(
27  'scripts.Dataface.QuickForm.pushValue.ERROR_GETTING_ELEMENT_VALUE',
28  "Error getting element value for element $field[name] in QuickForm::pushField ",
29  array('fieldname'=>$field['name'],'line'=>0,'file'=>'')
30  )
31  );
32  throw new Exception($val->toString(), E_USER_ERROR);
33  return $val;
34  }
35 
36 
37 
38  if ( $table->isContainer($field['name']) ){
39  $src = $record->getContainerSource($field['name']);
40  if ( strlen($record->strval($field['name']) ) > 0 // if there is already a valud specified in this field.
41  and file_exists($src) // if the old file exists
42  and is_file($src) // make sure that it is only a file we are deleting
43  and !is_dir($src) // don't accidentally delete a directory
44  ){
45  // delete the old file.
46  if ( !is_writable($src) ){
47  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);
48  }
49  @unlink( $src);
50  }
51 
52  // Make sure that the file does not already exist by that name in the destination directory.
53  $savepath = $field['savepath'];
54  $filename = basename($val['name']); // we use basename to guard against maliciously named files.
55  $filename = str_replace(chr(32), "_", $filename);
56  $matches = array();
57  if ( preg_match('/^(.*)\.([^\.]+)$/', $filename, $matches) ){
58  $extension = $matches[2];
59  $filebase = $matches[1];
60  } else {
61  $extension = '';
62  $filebase = $filename;
63  }
64  while ( file_exists( $savepath.'/'.$filename) ){
65  $matches = array();
66  if ( preg_match('/(.*)-{0,1}(\d+)$/', $filebase, $matches) ){
67  $filebase = $matches[1];
68  $fileindex = intval($matches[2]);
69  }
70  else {
71  $fileindex = 0;
72  // We should just leave the filebase the same.
73  //$filebase = $filename;
74 
75  }
76  if ( $filebase{strlen($filebase)-1} == '-' ) $filebase = substr($filebase,0, strlen($filebase)-1);
77  $fileindex++;
78  $filebase = $filebase.'-'.$fileindex;
79  $filename = $filebase.'.'.$extension;
80  }
81 
82  if (!is_writable( $field['savepath']) ){
83  throw new Exception(
84  df_translate(
85  'scripts.Dataface.QuickForm.pushValue.ERROR_INSUFFICIENT_DIRECTORY_PERMISSIONS',
86  "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.",
87  array('fieldname'=>$field['name'], 'savepath'=>$field['savepath'])
88  ), E_USER_ERROR);
89  }
90 
91  move_uploaded_file($val['tmp_name'], $field['savepath'].'/'.$filename);
92  chmod($field['savepath'].'/'.$filename, 0744);
93 
94  $out = $filename;
95 
96 
97  } else {
98  if ( file_exists($val['tmp_name']) ){
99  if ( !@$app->_conf['multilingual_content'] ){
100  // THis is a bit of a hack. If we are using multilingual
101  // content, then Dataface_DB will parse every query
102  // before sending it to the database. It is better if
103  // that query is short - so we only pass the whole value
104  // if we are not parsing the query.
105  $out = file_get_contents($val['tmp_name']);
106  } else {
107  // If we are parsing the query, then we will just store
108  // the path to the blob.
109  $out = $val['tmp_name'];
110  }
111  } else {
112  $out = null;
113  }
114  }
115 
116  if ( is_array( $metaValues ) ){
117  if ( isset( $field['filename'] ) ){
118  // store the file name in another field if one is specified
119  $metaValues[$field['filename']] = $val['name'];
120 
121  }
122  if ( isset( $field['mimetype'] ) ){
123  // store the file mimetype in another field if one is specified
124  $metaValues[$field['mimetype']] = $val['type'];
125 
126  }
127  }
128 
129  return $out;
130 
131 
132  }
133 
134  if ( $table->isContainer($field['name']) ){
135  return $record->val($field['name']);
136  }
137  return null;
138 
139  }
140 
141  function pullValue(&$record, &$field, &$form, &$element){
142  /*
143  *
144  * We don't bother pulling the values of file widgets because it would take too long.
145  *
146  */
147 
148  $widget =& $field['widget'];
149  $formFieldName = $element->getName();
150 
151  $val = null;
152  if ( $widget['type'] == 'webcam' ) $val = $record->getValueAsString($field['name']);
153  if ( $record->getLength($field['name']) > 0 ){
154  // there is already a file set, let's add a preview to it
155  if ( $record->isImage($field['name']) ){
156  $element->setProperty('image_preview', df_absolute_url($record->q($field['name'])));
157  }
158  $element->setProperty('preview', df_absolute_url($record->q($field['name'])));
159  //echo "Adding preview for field '$fieldname':".$record->qq($fieldname);
160  } else {
161  //echo "No data in field '$fieldname'";
162  }
163 
164  return $val;
165  }
166 
167 }