00001 <?php
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00035
00036 class FF_FileCache {
00037
00038
00043 var $o_registry;
00044
00045
00046
00047
00054 function FF_FileCache()
00055 {
00056 $this->o_registry =& FF_Registry::singleton();
00057 }
00058
00059
00060
00061
00068 function &singleton()
00069 {
00070 static $instance;
00071 if (!isset($instance)) {
00072 $instance = new FF_FileCache();
00073 }
00074
00075 return $instance;
00076 }
00077
00078
00079
00080
00095 function save($in_data, $in_fileParts, $in_useApp = false)
00096 {
00097 $o_result = new FF_Result();
00098 $this->checkDir($in_fileParts, $in_useApp);
00099 $s_path = $this->getPath($in_fileParts, $in_useApp);
00100 require_once 'File.php';
00101 $tmp_result = File::write($s_path, $in_data, FILE_MODE_WRITE);
00102 if (PEAR::isError($tmp_result)) {
00103 $o_result->setSuccess(false);
00104 $o_result->addMessage($tmp_result->getMessage());
00105 }
00106
00107 File::close($s_path, FILE_MODE_WRITE);
00108 return $o_result;
00109 }
00110
00111
00112
00113
00132 function saveUploadedFile($in_uploadData, $in_fileParts, $in_maxSize = 0, $in_validTypes = array(), $in_useApp = false)
00133 {
00134 $o_result = new FF_Result();
00135
00136 if ($in_uploadData['error'] == 4 || empty($in_uploadData['name']) || $in_uploadData['name'] == 'none') {
00137 return $o_result;
00138 }
00139
00140 if ($in_uploadData['size'] == 0) {
00141 $o_result->addMessage(_('The file you tried to attach contains no data.'));
00142 $o_result->setSuccess(false);
00143 return $o_result;
00144 }
00145
00146
00147 if ($in_maxSize > 0 &&
00148 ((isset($in_uploadData['error']) &&
00149 ($in_uploadData['error'] == 1 || $in_uploadData['error'] == 2)) ||
00150 $in_uploadData['size'] > $in_maxSize)) {
00151 require_once dirname(__FILE__) . '/Util.php';
00152 $o_result->addMessage(sprintf(_('The uploaded file %s is too big. The maximum size is %s.'),
00153 $in_uploadData['name'],
00154 FF_Util::bytesToHuman($in_maxSize)));
00155 $o_result->setSuccess(false);
00156 return $o_result;
00157 }
00158
00159 if (!is_uploaded_file($in_uploadData['tmp_name'])) {
00160 $o_result->addMessage(_('There was an error uploading the file.'));
00161 $o_result->setSuccess(false);
00162 return $o_result;
00163 }
00164
00165 if (count($in_validTypes) > 0 && !in_array($in_uploadData['type'], $in_validTypes)) {
00166 $o_result->addMessage(_('The uploaded file was not a valid type.'));
00167 $o_result->setSuccess(false);
00168 return $o_result;
00169 }
00170
00171 if (!isset($in_fileParts['name'])) {
00172 $in_fileParts['name'] = $in_uploadData['name'];
00173 }
00174
00175 $this->checkDir($in_fileParts, $in_useApp);
00176 if (!move_uploaded_file($in_uploadData['tmp_name'], $this->getPath($in_fileParts, $in_useApp))) {
00177 $o_result->addMessage(_('Could not save the uploaded file.'));
00178 $o_result->setSuccess(false);
00179 return $o_result;
00180 }
00181
00182 return $o_result;
00183 }
00184
00185
00186
00187
00201 function makeViewableFromWeb($in_fileParts, $in_useApp = false)
00202 {
00203 $in_fileParts['name'] = '.htaccess';
00204 if (!$this->exists($in_fileParts, $in_useApp)) {
00205 $o_reult =& $this->save('Allow from all', $in_fileParts, $in_useApp);
00206 }
00207 }
00208
00209
00210
00211
00224 function exists($in_fileParts, $in_useApp = false)
00225 {
00226 $s_path = $this->getPath($in_fileParts, $in_useApp);
00227
00228 if (is_dir($s_path)) {
00229 return false;
00230 }
00231
00232 return file_exists($s_path);
00233 }
00234
00235
00236
00237
00247 function getFilesForObject($in_id, $in_filePath)
00248 {
00249 $a_files = array();
00250 if ($handle = @opendir($this->getPath(array('subdir' => $in_filePath, 'id' => $in_id), true))) {
00251 while (false !== ($file = readdir($handle))) {
00252 if ($file != '.' && $file != '..') {
00253 $a_files[] = $file;
00254 }
00255 }
00256
00257 closedir($handle);
00258 }
00259
00260 return $a_files;
00261 }
00262
00263
00264
00265
00278 function remove($in_fileParts, $in_useApp = false)
00279 {
00280 $s_path = $this->getPath($in_fileParts, $in_useApp);
00281 @unlink($s_path);
00282
00283 if (isset($in_fileParts['id']) && isset($in_fileParts['subdir'])) {
00284 $a_files = $this->getFilesForObject($in_fileParts['id'], $in_fileParts['subdir']);
00285 if (!count($a_files)) {
00286 @rmdir(dirname($s_path));
00287 }
00288 }
00289 }
00290
00291
00292
00293
00308 function getPath($in_fileParts, $in_useApp = false, $in_type = FASTFRAME_FILEPATH)
00309 {
00310 $s_app = $in_useApp ? $this->o_registry->getCurrentApp() . '/' : '';
00311 $s_path = '';
00312 $s_path .= isset($in_fileParts['subdir']) ? ($in_fileParts['subdir'] . '/') : '';
00313 $s_path .= isset($in_fileParts['id']) ? ($in_fileParts['id'] . '/') : '';
00314 $s_path .= $in_fileParts['name'];
00315 return $this->o_registry->getRootFile($s_app . $s_path, 'data', $in_type);
00316 }
00317
00318
00319
00320
00333 function checkDir($in_fileParts, $in_useApp = false)
00334 {
00335 if (!isset($in_fileParts['name'])) {
00336 $in_fileParts['name'] = $in_fileParts['subdir'];
00337 unset($in_fileParts['subdir']);
00338 $s_dir = $this->getPath($in_fileParts, $in_useApp);
00339 }
00340 else {
00341 $s_dir = dirname($this->getPath($in_fileParts, $in_useApp));
00342 }
00343
00344 if (!is_dir($s_dir)) {
00345 require_once 'System.php';
00346 if (!@System::mkdir("-p $s_dir")) {
00347 trigger_error('Could not write to the FastFrame data directory.', E_USER_ERROR);
00348 }
00349 }
00350 }
00351
00352
00353
00354
00363 function isWebViewable($in_name)
00364 {
00365 $a_allowedTypes = array('image/bmp', 'image/png', 'image/jpeg', 'image/gif',
00366 'application/pdf',
00367 'text/css', 'text/plain', 'text/html', 'text/xml');
00368 return in_array($this->getContentType($in_name), $a_allowedTypes);
00369 }
00370
00371
00372
00373
00382 function isWebPlayable($in_name)
00383 {
00384 $a_allowedTypes = array('audio/mp3', 'audio/x-realaudio', 'audio/x-pn-realaudio', 'audio/wav', 'audio/x-gsm');
00385 return in_array($this->getContentType($in_name), $a_allowedTypes);
00386 }
00387
00388
00389
00390
00399 function getContentType($in_filename)
00400 {
00401
00402
00403 $a_mime = array(
00404 'xxx' => 'document/unknown',
00405 '3gp' => 'video/quicktime',
00406 'ai' => 'application/postscript',
00407 'aif' => 'audio/x-aiff',
00408 'aiff' => 'audio/x-aiff',
00409 'aifc' => 'audio/x-aiff',
00410 'applescript' => 'text/plain',
00411 'asc' => 'text/plain',
00412 'au' => 'audio/au' ,
00413 'avi' => 'video/x-ms-wm',
00414 'bmp' => 'image/bmp',
00415 'cs' => 'application/x-csh',
00416 'csv' => 'text/plain',
00417 'css' => 'text/css',
00418 'dv' => 'video/x-dv',
00419 'doc' => 'application/msword',
00420 'dif' => 'video/x-dv',
00421 'eps' => 'application/postscript',
00422 'gif' => 'image/gif',
00423 'gsm' => 'audio/x-gsm',
00424 'gtar' => 'application/x-gtar',
00425 'gz' => 'application/g-zip',
00426 'gzip' => 'application/g-zip',
00427 'h' => 'text/plain',
00428 'hqx' => 'application/mac-binhex40',
00429 'html' => 'text/html',
00430 'htm' => 'text/html',
00431 'jpe' => 'image/jpeg',
00432 'jpeg' => 'image/jpeg',
00433 'jpg' => 'image/jpeg',
00434 'js' => 'application/x-javascript',
00435 'latex' => 'application/x-latex',
00436 'm' => 'text/plain',
00437 'mov' => 'video/quicktime',
00438 'movie' => 'video/x-sgi-movie',
00439 'm3u' => 'audio/x-mpegurl',
00440 'mp3' => 'audio/mp3',
00441 'mp4' => 'video/mp4',
00442 'mpeg' => 'video/mpeg',
00443 'mpe' => 'video/mpeg',
00444 'mpg' => 'video/mpeg',
00445 'pct' => 'image/pict',
00446 'pdf' => 'application/pdf',
00447 'php' => 'text/plain',
00448 'pic' => 'image/pict',
00449 'pict' => 'image/pict',
00450 'png' => 'image/png',
00451 'ppt' => 'application/vnd.ms-powerpoint',
00452 'ps' => 'application/postscript',
00453 'qt' => 'video/quicktime',
00454 'ra' => 'audio/x-realaudio',
00455 'ram' => 'audio/x-pn-realaudio',
00456 'rm' => 'audio/x-pn-realaudio',
00457 'rtf' => 'text/rtf',
00458 'rtx' => 'text/richtext',
00459 'sh' => 'application/x-sh',
00460 'sit' => 'application/x-stuffit',
00461 'smi' => 'application/smil',
00462 'smil' => 'application/smil',
00463 'swf' => 'application/x-shockwave-flash',
00464 'tar' => 'application/x-tar',
00465 'tif' => 'image/tiff',
00466 'tiff' => 'image/tiff',
00467 'tex' => 'application/x-tex',
00468 'texi' => 'application/x-texinfo',
00469 'texinfo' => 'application/x-texinfo',
00470 'tsv' => 'text/tab-separated-values',
00471 'txt' => 'text/plain',
00472 'wav' => 'audio/wav',
00473 'wmv' => 'video/x-ms-wmv',
00474 'asf' => 'video/x-ms-asf',
00475 'xls' => 'application/vnd.ms-excel',
00476 'xml' => 'text/xml',
00477 'xsl' => 'text/xml',
00478 'zip' => 'application/zip');
00479
00480
00481 $a_pth = pathinfo(strtolower($in_filename));
00482 if (!isset($a_pth['extension'])) {
00483 $a_pth['extension'] = 'xxx';
00484 }
00485
00486 return isset($a_mime[$a_pth['extension']]) ? $a_mime[$a_pth['extension']] : $a_mime['xxx'];
00487 }
00488
00489
00490 }
00491 ?>