![]() |
Xataface 2.0
Xataface Application Framework
|
00001 <?php 00002 /*------------------------------------------------------------------------------- 00003 * Xataface Web Application Framework 00004 * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca) 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program 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 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 *------------------------------------------------------------------------------- 00020 */ 00021 00031 class Dataface_Application_blob { 00032 00033 00034 function _parseRelatedBlobRequest($request){ 00035 if ( !is_a($this, 'Dataface_Application') ){ 00036 throw new Exception('Dataface_Application_blob methods can only be accessed via Dataface_Application.', E_USER_ERROR); 00037 } 00038 import('dataface-public-api.php'); 00039 if ( !isset( $request['-field'] ) ) die("Could not complete request. No field name specified."); 00040 if ( !isset( $request['-table'] ) ) die("Could not complete request. No table specified."); 00041 00042 $record =& df_get_record($request['-table'], $request); 00043 if ( strpos($request['-field'], '.') === false ){ 00044 die("ParseRelatedBlobRequest only works for -field parameters refering to related fields."); 00045 } 00046 list($relationship, $relativeField) = explode('.', $request['-field']); 00047 if ( @$request['-where'] ) $where = stripslashes($request['-where']); 00048 else $where = 0; 00049 $rrecords =& $record->getRelatedRecordObjects($relationship, 0, 1, $where); 00050 if (count($rrecords) == 0 ){ 00051 die("No records found"); 00052 } 00053 $rrecord =& $rrecords[0]; 00054 00055 $relationshipRef =& $rrecord->_relationship; 00056 $domainTable =& $relationshipRef->getDomainTable(); 00057 if ( !$domainTable || PEAR::isError($domainTable) ){ 00058 unset($domainTable); 00059 $destinationTables = $relationshipRef->destinationTables(); 00060 $domainTable = reset($destinationTables); 00061 } 00062 $out = array('-table'=>$domainTable, '-field'=>$relativeField); 00063 00064 $domainTableRef =& Dataface_Table::loadTable($domainTable); 00065 foreach ( array_keys($domainTableRef->keys()) as $key){ 00066 $out[$key] = $rrecord->strval($key); 00067 } 00068 00069 return $out; 00070 00071 00072 } 00073 00074 00084 function _handleGetBlob($request){ 00085 if ( !is_a($this, 'Dataface_Application') ){ 00086 throw new Exception('Dataface_Application_blob methods can only be accessed via Dataface_Application.', E_USER_ERROR); 00087 } 00088 import( 'Dataface/Table.php'); 00089 import('Dataface/QueryTool.php'); 00090 00091 if ( strpos(@$request['-field'], '.') !== false ){ 00092 $request = $this->_parseRelatedBlobRequest($request); 00093 } 00094 00095 if ( !isset( $request['-field'] ) ) die("Could not complete request. No field name specified."); 00096 if ( !isset( $request['-table'] ) ) die("Could not complete request. No table specified."); 00097 $fieldname = $request['-field']; 00098 $tablename = $request['-table']; 00099 00100 $table =& Dataface_Table::loadTable($tablename); 00101 $keys = array_keys($table->keys()); 00102 00103 00104 $lastTableUpdate = $table->getUpdateTime(); 00105 $lastTableUpdate = strtotime($lastTableUpdate); 00106 00107 if ( $table->isContainer($fieldname) ){ 00108 $field =& $table->getField($fieldname); 00109 if ( PEAR::isError($field) ){ 00110 header('HTTP/1.0 500 Internal Server Error'); 00111 echo '<h1>Internal Server Error</h1>'; 00112 error_log($field->getMessage()); 00113 exit; 00114 } 00115 $savepath = $field['savepath']; 00116 $app =& Dataface_Application::getInstance(); 00117 $query =& $app->getQuery(); 00118 $rec =& df_get_record($table->tablename, $query); 00119 if ( !$rec ) throw new Exception("No record found to match the request.", E_USER_ERROR); 00120 00121 if ( !$rec->val($fieldname) ){ 00122 header('HTTP/1.0 404 Not Found'); 00123 echo '<h1>404 File Not Found</h1>'; 00124 exit; 00125 } 00126 00127 if ( !$rec->checkPermission('view', array('field'=>$fieldname)) ){ 00128 header('HTTP/1.1 403 Forbidden'); 00129 echo '<h1>Access Forbidden</h1>'; 00130 exit; 00131 } 00132 header('Content-type: '.$rec->getMimetype($fieldname)); 00133 header('Content-disposition: attachment; filename="'.basename($rec->val($fieldname)).'"'); 00134 echo file_get_contents($savepath.'/'.basename($rec->val($fieldname))); 00135 exit; 00136 00137 } 00138 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"); 00139 $field =& $table->getField($fieldname); 00140 00141 if ( isset($request['-index']) ) $index = $request['-index']; 00142 else $index = 0; 00143 00144 $cachePath = $this->_conf['cache_dir'].'/'.basename($this->_conf['_database']['name']).'-'.basename($tablename).'-'.basename($fieldname).'-'.basename($index).'?'; 00145 foreach ($keys as $key){ 00146 $cachePath .= urlencode($key).'='.urlencode($_REQUEST[$key]).'&'; 00147 } 00148 00149 $queryTool =& Dataface_QueryTool::loadResult($tablename, null, $request); 00150 00151 // No mimetype was recorded. Use the PECL Fileinto extension if it is available. 00152 00153 $files = glob($cachePath.'-*'); 00154 $found = false; 00155 00156 if ( is_array($files) ){ 00157 foreach ($files as $file){ 00158 $matches = array(); 00159 if ( preg_match('/.*-([^\-]+)$/', $file, $matches) ){ 00160 $time = $matches[1]; 00161 if ( intval($time)>$lastTableUpdate){ 00162 $found = $file; 00163 break; 00164 } else { 00165 @unlink($file); 00166 } 00167 } 00168 } 00169 } 00170 00171 if ( $found !== false ){ 00172 $contents = file_get_contents($found); 00173 } else { 00174 $columns = array($fieldname); 00175 00176 if ( isset($field['mimetype']) and $field['mimetype']){ 00177 $columns[] = $field['mimetype']; 00178 } 00179 if ( isset($field['filename']) and $field['filename']){ 00180 $columns[] = $field['filename']; 00181 } 00182 $record =& $queryTool->loadCurrent($columns, true, true); 00183 $record->loadBlobs = true; 00184 $contents = $record->getValue($fieldname, $index); 00185 $found = $cachePath.'-'.time(); 00186 $found=str_replace("?","-",$found); 00187 if ( $fh = fopen($found, "w") ){ 00188 fwrite($fh, $contents); 00189 fclose($fh); 00190 } else { 00191 $found = false; 00192 } 00193 } 00194 00195 if ( !isset( $record ) ){ 00196 $columns = array(); 00197 if ( isset($field['mimetype']) and $field['mimetype']){ 00198 $columns[] = $field['mimetype']; 00199 } 00200 if ( isset($field['filename']) and $field['filename']){ 00201 $columns[] = $field['filename']; 00202 } 00203 00204 $record =& $queryTool->loadCurrent($columns); 00205 } 00206 00207 if ( isset($field['mimetype']) and $field['mimetype']){ 00208 $mimetype = $record->getValue($field['mimetype'], $index); 00209 } 00210 if ( isset($field['filename']) and $field['filename']){ 00211 $filename = $record->getValue($field['filename'], $index); 00212 } 00213 //$mimetype = $record->getValue($field['mimetype'], $index); 00214 //echo $mimetype; exit; 00215 00216 00217 if ( (!isset($mimetype) or !$mimetype) and $found !== false ){ 00218 00219 //if(!extension_loaded('fileinfo')) { 00220 // @dl('fileinfo.' . PHP_SHLIB_SUFFIX); 00221 //} 00222 if(extension_loaded('fileinfo')) { 00223 $res = finfo_open(FILEINFO_MIME_TYPE); /* return mime type ala mimetype extension */ 00224 $mimetype = finfo_file($res, $found); 00225 } else if (function_exists('mime_content_type')) { 00226 00227 00228 $mimetype = mime_content_type($found); 00229 00230 } else { 00231 throw new Exception("Could not find mimetype for field '$fieldname'", E_USER_ERROR); 00232 } 00233 } 00234 00235 if ( !isset($filename) ){ 00236 $filename = $request['-table'].'_'.$request['-field'].'_'.date('Y_m_d_H_i_s'); 00237 } 00238 //echo "here"; 00239 //echo "here: $mimetype"; 00240 //echo $contents; 00241 //echo $mimetype; exit; 00242 header('Content-type: '.$mimetype); 00243 header('Content-disposition: attachment; filename="'.$filename.'"'); 00244 echo $contents; 00245 exit; 00246 00247 00248 00249 } 00250 00251 }