示例#1
0
 /**
  * Loads in the file containing mime types and extensions
  */
 protected static function loadMimeTypes()
 {
     self::$extToMimeTypes = array();
     $mime_file = null;
     if (I2CE::getConfig()->setIfIsSet($mime_file, "/modules/MimeTypes/mime_types")) {
         $mime_file = I2CE::getFileSearch()->search('MIME', $mime_file);
     }
     if (empty($mime_file)) {
         I2CE::raiseError('Unable to find mime.types file.', E_USER_WARNING);
         return;
     }
     $a = file($mime_file);
     if (empty($a)) {
         I2CE::raiseError('mime.types file is empty.', E_USER_WARNING);
         return;
     }
     foreach ($a as $l) {
         $l = trim($l);
         if (strlen($l) < 1 || $l[0] == '#') {
             //skip comments
             continue;
         }
         $pieces = preg_split("/\\s+/", $l, -1, PREG_SPLIT_NO_EMPTY);
         if (empty($pieces)) {
             //a blank line
             continue;
         }
         $mime = strtolower(array_shift($pieces));
         foreach ($pieces as $ext) {
             self::$extToMimeTypes[strtolower($ext)] = $mime;
         }
     }
 }
示例#2
0
 /**
  * Function to return the proper header type from a file's extension
  * some of the code extracted gratefully from  http://us3.php.net/manual/en/function.fread.php#72716
  * @param string $file the file''s name
  * @param string $ext the file''s (possible forced) extension -- lower case.  If null/empty it is not used.
  * @param string $mime_type  the file''s mime type.  Defaults to null.  
  * Will be overidden if $ext is not empty 
  * @returns array of string. the headers;
  */
 public function doHeader($file, $ext, $content, $apdContent)
 {
     $headers = array();
     if (!$content) {
         if ($ext) {
             $content = I2CE_MimeTypes::extToMimeType($ext);
         }
         if (!$content) {
             I2CE::raiseError("No mime type identified for ({$file})", E_USER_NOTICE);
             $content = "application/force-download";
         }
     }
     if ($apdContent) {
         $content .= "; " . $apdContent;
     }
     $headers[] = "Content-Type: " . $content;
     $headers[] = "Content-disposition: inline; filename=\"{$file}\"";
     return $headers;
 }
 protected function action()
 {
     parent::action();
     if (!$this->config instanceof I2CE_MagicDataNode) {
         $this->reshow(true);
         //show the parent node.
         return;
     }
     $reshow = false;
     switch ($this->page()) {
         case 'upload':
             I2CE::raiseError("Upload on " . $this->config->getPath(false) . "\n" . print_r($_FILES, true));
             if (!array_key_exists('upload', $_FILES) || array_key_exists('error', $_FILES['upload']) && $_FILES['upload']['error'] > 0 || ($upload = file_get_contents($_FILES['upload']['tmp_name'])) === false) {
                 $this->userMessage("Could not upload " . $_FILES['upload']['name']);
                 break;
             }
             if (strlen($upload) != $_FILES['upload']['size']) {
                 I2CE::raiseError("Upload size mismatch " . strlen($upload) . ' != ' . $_FILES['upload']['size']);
                 $this->userMessage("Could not upload " . $_FILES['upload']['name']);
                 break;
             }
             if ($this->config->is_parent()) {
                 $this->userMessage("Cannot set value on parent node");
                 break;
             }
             $content = $this->config;
             if ($content->is_parent()) {
                 $this->userMessage("Cannot overwrite content node");
                 break;
             }
             if ($content->hasAttribute('binary') && $content->getAttribute('binary')) {
                 $upload = base64_encode($upload);
                 $content->setValue($upload);
                 $content->setAttribute('encoding', 'base64');
                 $content->setAttribute('binary', '1');
             } else {
                 $content->setValue($upload);
             }
             break;
         case 'upload_binary':
             I2CE::raiseError("Upload Binary on " . $this->config->getPath(false) . "\n" . print_r($_FILES, true));
             if (!array_key_exists('upload', $_FILES) || array_key_exists('error', $_FILES['upload']) && $_FILES['upload']['error'] > 0 || ($upload = file_get_contents($_FILES['upload']['tmp_name'])) === false) {
                 $this->userMessage("Could not upload " . $_FILES['upload']['name']);
                 break;
             }
             if (strlen($upload) != $_FILES['upload']['size']) {
                 I2CE::raiseError("Upload size mismatch " . strlen($upload) . ' != ' . $_FILES['upload']['size']);
                 $this->userMessage("Could not upload " . $_FILES['upload']['name']);
                 break;
             }
             if ($this->config->is_scalar()) {
                 $this->userMessage("Cannot set value on parent node");
                 break;
             }
             $content = $this->config->traverse('content', true, false);
             $name = $this->config->traverse('name', true, false);
             $type = $this->config->traverse('type', true, false);
             if ($content->is_parent()) {
                 $this->userMessage("Cannot overwrite content node");
                 break;
             }
             if ($name->is_parent()) {
                 $this->userMessage("Cannot overwrite content node");
                 break;
             }
             if ($type->is_parent()) {
                 $this->userMessage("Cannot overwrite type node");
                 break;
             }
             //I2CE::raiseError("Setting " . $this->config->getPath() );
             $upload = base64_encode($upload);
             $content->setValue($upload);
             $name->setValue($_FILES['upload']['name']);
             $type->setValue($_FILES['upload']['type']);
             $content->setAttribute('binary', 1);
             $content->setAttribute('encoding', 'base64');
             break;
         case 'load':
             I2CE::raiseError("Begin load:" . print_r($this->request(), true));
             $transform = false;
             if (($transform_key = $this->request('transform_key')) && I2CE_MagicDataNode::checkKey($transform_key) && I2CE::getConfig()->setIfIsSet($transform, "/modules/magicDataBrowser/transforms/" . $this->request('transform_key')) && $transform) {
                 if (substr($transform, 0, 7) == 'file://' && (!($transform_file = I2CE::getFileSearch()->search('XSL', $file_name = substr($transform, 7))) || !($transform = file_get_contents($transform_file)))) {
                     I2CE::raiseError("Could not load {$file_name} for transform");
                     $this->userMessage("Invalid registered transform");
                     return false;
                 }
             } else {
                 if (array_key_exists('transform', $_FILES) && !(array_key_exists('error', $_FILES['transform']) && $_FILES['transform']['error'] > 0)) {
                     $transform = file_get_contents($_FILES['transform']['tmp_name']);
                 }
             }
             I2CE::raiseError("Loading with transform:{$transform}");
             if ($this->actionLoad($transform)) {
                 $this->userMessage("Data successuly loaded");
             } else {
                 $this->userMessage("There was a problem loading the data");
             }
             break;
         case 'erase':
             $name = $this->config->getName();
             $parent = $this->config->traverse('../');
             if ($this->isPost() && $this->config->getPath() != $this->config->traverse('/')->getPath()) {
                 //don't allow an erase of the top level node
                 unset($parent->{$name});
             }
             $reshow = true;
             break;
         case 'parent':
             if ($this->isPost() && $this->config->is_indeterminate()) {
                 $this->config->set_parent();
             }
             break;
         case 'scalar':
             if ($this->isPost() && $this->config->is_indeterminate()) {
                 $this->config->set_scalar();
             }
             break;
         case 'add':
             if ($this->post_exists('browser_magic_data_add_key')) {
                 $key = $this->post('browser_magic_data_add_key');
                 if (I2CE_MagicDataNode::checkKey($key) && !$this->config->pathExists($key)) {
                     $this->config->traverse($key, true, false);
                 }
             }
             break;
         case 'add_parent':
             if ($this->post_exists('browser_magic_data_add_key')) {
                 $key = $this->post('browser_magic_data_add_key');
                 if (I2CE_MagicDataNode::checkKey($key) && !$this->config->pathExists($key) && ($newNode = $this->config->traverse($key, true, false)) instanceof I2CE_MagicDataNode) {
                     $newNode->set_parent();
                 }
             }
             break;
         case 'add_scalar':
             if ($this->post_exists('browser_magic_data_add_key')) {
                 $key = $this->post('browser_magic_data_add_key');
                 if (I2CE_MagicDataNode::checkKey($key) && !$this->config->pathExists($key) && ($newNode = $this->config->traverse($key, true, false)) instanceof I2CE_MagicDataNode) {
                     $newNode->set_scalar();
                 }
             }
             break;
         case 'set':
             I2CE::raiseError("Try to set:0");
             if ($this->post_exists('browser_magic_data_value') && $this->post_exists('browser_magic_data_key')) {
                 I2CE::raiseError("Try to set:1");
                 $key = $this->post('browser_magic_data_key');
                 $value = $this->post('browser_magic_data_value');
                 if ($this->config->offsetExists($key) && is_scalar($value)) {
                     I2CE::raiseError("Try to set:2");
                     if ($this->config->is_translatable($key)) {
                         $locales = I2CE_Locales::getPreferredLocales();
                         reset($locales);
                         $locale = current($locales);
                         $this->config->setTranslation($locale, $value, $key);
                         if ($locale == I2CE_Locales::DEFAULT_LOCALE) {
                             $this->config->__set($key, $value);
                         }
                     } else {
                         $this->config[$key] = $value;
                     }
                 }
             }
             //we redirect so a reload does not post and so that if the page's display depends on the value that is being
             //set, we redisplay it.
             break;
         case 'download':
             if ($errors = I2CE_Dumper::cleanlyEndOutputBuffers()) {
                 I2CE::raiseError("Errors:\n" . $errors);
             }
             $value = $this->config->getValue();
             header("Cache-Control: max-age=1, s-maxage=1, no-store, no-cache, must-revalidate");
             header('Cache-Control: post-check=0, pre-check=0', false);
             header('Pragma: no-cache');
             header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
             header("Last-Modified: " . gmdate("D, d M Y H:i:s", time() - 10) . " GMT");
             header("ETag: PUB" . time());
             header("Pragma: no-cache");
             header("Content-length: " . strlen($value));
             header('Content-Disposition: attachment; filename="' . $this->config->getName() . '"');
             $mime_type = I2CE_MimeTypes::magicMimeType($value);
             I2CE::raiseError($mime_type);
             header('Content-type:' . $mime_type);
             session_cache_limiter("nocache");
             echo $value;
             die;
         case 'trans':
             $this->config->setTranslatable(null, !$this->config->is_translatable());
             break;
         case 'show':
         case 'mini':
             $this->actionDisplayConfig();
             $reshow = null;
             break;
         default:
             break;
     }
     if ($reshow !== null) {
         $this->reshow($reshow);
     }
 }
 /**
  * Get the extension associated with this binary file
  */
 public function getExtension()
 {
     $mime_type = $this->getMimeType();
     if (!$mime_type) {
         $mime_type = $this->defaultMimeType();
     }
     return I2CE_MimeTypes::mimeTypeToExt($mime_type);
 }