Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
blob.php
Go to the documentation of this file.
1 <?php
2 /*-------------------------------------------------------------------------------
3  * Xataface Web Application Framework
4  * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *-------------------------------------------------------------------------------
20  */
21 
32 
33 
34  static function _parseRelatedBlobRequest($request){
36  import('dataface-public-api.php');
37  if ( !isset( $request['-field'] ) ) die("Could not complete request. No field name specified.");
38  if ( !isset( $request['-table'] ) ) die("Could not complete request. No table specified.");
39 
40  $record =& df_get_record($request['-table'], $request);
41  if ( strpos($request['-field'], '.') === false ){
42  die("ParseRelatedBlobRequest only works for -field parameters refering to related fields.");
43  }
44  list($relationship, $relativeField) = explode('.', $request['-field']);
45  if ( @$request['-where'] ) $where = stripslashes($request['-where']);
46  else $where = 0;
47  $rrecords =& $record->getRelatedRecordObjects($relationship, 0, 1, $where);
48  if (count($rrecords) == 0 ){
49  die("No records found");
50  }
51  $rrecord =& $rrecords[0];
52 
53  $relationshipRef =& $rrecord->_relationship;
54  $domainTable =& $relationshipRef->getDomainTable();
55  if ( !$domainTable || PEAR::isError($domainTable) ){
56  unset($domainTable);
57  $destinationTables = $relationshipRef->destinationTables();
58  $domainTable = reset($destinationTables);
59  }
60  $out = array('-table'=>$domainTable, '-field'=>$relativeField);
61 
62  $domainTableRef =& Dataface_Table::loadTable($domainTable);
63  foreach ( array_keys($domainTableRef->keys()) as $key){
64  $out[$key] = $rrecord->strval($key);
65  }
66 
67  return $out;
68 
69 
70  }
71 
72 
82  static function _handleGetBlob($request){
84  import( 'Dataface/Table.php');
85  import('Dataface/QueryTool.php');
86 
87  if ( strpos(@$request['-field'], '.') !== false ){
88  $request = self::_parseRelatedBlobRequest($request);
89  }
90 
91  if ( !isset( $request['-field'] ) ) die("Could not complete request. No field name specified.");
92  if ( !isset( $request['-table'] ) ) die("Could not complete request. No table specified.");
93  $fieldname = $request['-field'];
94  $tablename = $request['-table'];
95 
97  $keys = array_keys($table->keys());
98 
99 
100  $lastTableUpdate = $table->getUpdateTime();
101  $lastTableUpdate = strtotime($lastTableUpdate);
103  $query =& $app->getQuery();
104  $rec =& df_get_record($table->tablename, $query);
105  if ( !$rec ) throw new Exception("No record found to match the request.", E_USER_ERROR);
106 
107  $field =& $table->getField($fieldname);
108  if ( PEAR::isError($field) ){
109  header('HTTP/1.0 500 Internal Server Error');
110  echo '<h1>Internal Server Error</h1>';
111  error_log($field->getMessage());
112  exit;
113  }
114 
115  if ( !$rec->checkPermission('view', array('field'=>$fieldname)) ){
116  header('HTTP/1.1 403 Forbidden');
117  echo '<h1>Access Forbidden</h1>';
118  exit;
119  }
120 
121  if ( $table->isContainer($fieldname) ){
122 
123  $savepath = $field['savepath'];
124 
125 
126  if ( !$rec->val($fieldname) ){
127  header('HTTP/1.0 404 Not Found');
128  echo '<h1>404 File Not Found</h1>';
129  exit;
130  }
131 
132 
133  header('Content-type: '.$rec->getMimetype($fieldname));
134  header('Content-disposition: attachment; filename="'.basename($rec->val($fieldname)).'"');
135  echo file_get_contents($savepath.'/'.basename($rec->val($fieldname)));
136  exit;
137 
138  }
139  if ( !$table->isBlob($fieldname) ) die("blob.php can only be used to load BLOB or Binary columns. The requested field '$fieldname' is not a blob");
140  //$field =& $table->getField($fieldname);
141 
142  if ( isset($request['-index']) ) $index = $request['-index'];
143  else $index = 0;
144 
145  $cachePath = $app->_conf['cache_dir'].'/'.basename($app->_conf['_database']['name']).'-'.basename($tablename).'-'.basename($fieldname).'-'.basename($index).'?';
146  foreach ($keys as $key){
147  $cachePath .= urlencode($key).'='.urlencode($_REQUEST[$key]).'&';
148  }
149 
151 
152  // No mimetype was recorded. Use the PECL Fileinto extension if it is available.
153 
154  $files = glob($cachePath.'-*');
155  $found = false;
156 
157  if ( is_array($files) ){
158  foreach ($files as $file){
159  $matches = array();
160  if ( preg_match('/.*-([^\-]+)$/', $file, $matches) ){
161  $time = $matches[1];
162  if ( intval($time)>$lastTableUpdate){
163  $found = $file;
164  break;
165  } else {
166  @unlink($file);
167  }
168  }
169  }
170  }
171 
172  if ( $found !== false ){
173  $contents = file_get_contents($found);
174  } else {
175  $columns = array($fieldname);
176 
177  if ( isset($field['mimetype']) and $field['mimetype']){
178  $columns[] = $field['mimetype'];
179  }
180  if ( isset($field['filename']) and $field['filename']){
181  $columns[] = $field['filename'];
182  }
183  $record =& $queryTool->loadCurrent($columns, true, true);
184  $record->loadBlobs = true;
185  $contents = $record->getValue($fieldname, $index);
186  $found = $cachePath.'-'.time();
187  $found=str_replace("?","-",$found);
188  if ( $fh = fopen($found, "w") ){
189  fwrite($fh, $contents);
190  fclose($fh);
191  } else {
192  $found = false;
193  }
194  }
195 
196  if ( !isset( $record ) ){
197  $columns = array();
198  if ( isset($field['mimetype']) and $field['mimetype']){
199  $columns[] = $field['mimetype'];
200  }
201  if ( isset($field['filename']) and $field['filename']){
202  $columns[] = $field['filename'];
203  }
204 
205  $record =& $queryTool->loadCurrent($columns);
206  }
207 
208  if ( isset($field['mimetype']) and $field['mimetype']){
209  $mimetype = $record->getValue($field['mimetype'], $index);
210  }
211  if ( isset($field['filename']) and $field['filename']){
212  $filename = $record->getValue($field['filename'], $index);
213  }
214  //$mimetype = $record->getValue($field['mimetype'], $index);
215  //echo $mimetype; exit;
216 
217 
218  if ( (!isset($mimetype) or !$mimetype) and $found !== false ){
219 
220  if(extension_loaded('fileinfo')) {
221  $res = finfo_open(FILEINFO_MIME_TYPE); /* return mime type ala mimetype extension */
222  $mimetype = finfo_file($res, $found);
223  } else if (function_exists('mime_content_type')) {
224 
225 
226  $mimetype = mime_content_type($found);
227 
228  } else {
229  throw new Exception("Could not find mimetype for field '$fieldname'", E_USER_ERROR);
230  }
231  }
232 
233  if ( !isset($filename) ){
234  $filename = $request['-table'].'_'.$request['-field'].'_'.date('Y_m_d_H_i_s');
235  }
236  //echo "here";
237  //echo "here: $mimetype";
238  //echo $contents;
239  //echo $mimetype; exit;
240  header('Content-type: '.$mimetype);
241  header('Content-disposition: attachment; filename="'.$filename.'"');
242  echo $contents;
243  exit;
244 
245 
246 
247  }
248 
249 }