示例#1
0
 /**
  * Execute command
  *
  * @param string $command
  * @access public
  */
 public function executeCommand($command)
 {
     if (!QFinder_Connector_Core_Hooks::run('BeforeExecuteCommand', array(&$command))) {
         return;
     }
     switch ($command) {
         case 'FileUpload':
             $this->_registry->set("errorHandler", "ErrorHandler_FileUpload");
             $obj =& QFinder_Connector_Core_Factory::getInstance("CommandHandler_" . $command);
             $obj->sendResponse();
             break;
         case 'QuickUpload':
             $this->_registry->set("errorHandler", "ErrorHandler_QuickUpload");
             $obj =& QFinder_Connector_Core_Factory::getInstance("CommandHandler_" . $command);
             $obj->sendResponse();
             break;
         case 'DownloadFile':
         case 'Thumbnail':
             $this->_registry->set("errorHandler", "ErrorHandler_Http");
             $obj =& QFinder_Connector_Core_Factory::getInstance("CommandHandler_" . $command);
             $obj->sendResponse();
             break;
         case 'CopyFiles':
         case 'CreateFolder':
         case 'DeleteFiles':
         case 'DeleteFolder':
         case 'GetFiles':
         case 'GetFolders':
         case 'Init':
         case 'LoadCookies':
         case 'MoveFiles':
         case 'RenameFile':
         case 'RenameFolder':
             $obj =& QFinder_Connector_Core_Factory::getInstance("CommandHandler_" . $command);
             $obj->sendResponse();
             break;
         default:
             $this->handleInvalidCommand();
             break;
     }
 }
示例#2
0
 /**
  * send response (save uploaded file, resize if required)
  * @access public
  *
  */
 public function sendResponse()
 {
     $iErrorNumber = QFINDER_CONNECTOR_ERROR_NONE;
     $_config =& QFinder_Connector_Core_Factory::getInstance("Core_Config");
     $oRegistry =& QFinder_Connector_Core_Factory::getInstance("Core_Registry");
     $oRegistry->set("FileUpload_fileName", "unknown file");
     $uploadedFile = array_shift($_FILES);
     if (!isset($uploadedFile['name'])) {
         $this->_errorHandler->throwError(QFINDER_CONNECTOR_ERROR_UPLOADED_INVALID);
     }
     $sUnsafeFileName = QFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding(QFinder_Connector_Utils_Misc::mbBasename($uploadedFile['name']));
     $sFileName = QFinder_Connector_Utils_FileSystem::secureFileName($sUnsafeFileName);
     if ($sFileName != $sUnsafeFileName) {
         $iErrorNumber = QFINDER_CONNECTOR_ERROR_UPLOADED_INVALID_NAME_RENAMED;
     }
     $oRegistry->set("FileUpload_fileName", $sFileName);
     $this->checkConnector();
     $this->checkRequest();
     if (!$this->_currentFolder->checkAcl(QFINDER_CONNECTOR_ACL_FILE_UPLOAD)) {
         $this->_errorHandler->throwError(QFINDER_CONNECTOR_ERROR_UNAUTHORIZED);
     }
     $_resourceTypeConfig = $this->_currentFolder->getResourceTypeConfig();
     if (!QFinder_Connector_Utils_FileSystem::checkFileName($sFileName) || $_resourceTypeConfig->checkIsHiddenFile($sFileName)) {
         $this->_errorHandler->throwError(QFINDER_CONNECTOR_ERROR_INVALID_NAME);
     }
     $resourceTypeInfo = $this->_currentFolder->getResourceTypeConfig();
     if (!$resourceTypeInfo->checkExtension($sFileName)) {
         $this->_errorHandler->throwError(QFINDER_CONNECTOR_ERROR_INVALID_EXTENSION);
     }
     $oRegistry->set("FileUpload_fileName", $sFileName);
     $oRegistry->set("FileUpload_url", $this->_currentFolder->getUrl());
     $maxSize = $resourceTypeInfo->getMaxSize();
     if (!$_config->checkSizeAfterScaling() && $maxSize && $uploadedFile['size'] > $maxSize) {
         $this->_errorHandler->throwError(QFINDER_CONNECTOR_ERROR_UPLOADED_TOO_BIG);
     }
     $htmlExtensions = $_config->getHtmlExtensions();
     $sExtension = QFinder_Connector_Utils_FileSystem::getExtension($sFileName);
     if ($htmlExtensions && !QFinder_Connector_Utils_Misc::inArrayCaseInsensitive($sExtension, $htmlExtensions) && ($detectHtml = QFinder_Connector_Utils_FileSystem::detectHtml($uploadedFile['tmp_name'])) === true) {
         $this->_errorHandler->throwError(QFINDER_CONNECTOR_ERROR_UPLOADED_WRONG_HTML_FILE);
     }
     $secureImageUploads = $_config->getSecureImageUploads();
     if ($secureImageUploads && ($isImageValid = QFinder_Connector_Utils_FileSystem::isImageValid($uploadedFile['tmp_name'], $sExtension)) === false) {
         $this->_errorHandler->throwError(QFINDER_CONNECTOR_ERROR_UPLOADED_CORRUPT);
     }
     switch ($uploadedFile['error']) {
         case UPLOAD_ERR_OK:
             break;
         case UPLOAD_ERR_INI_SIZE:
         case UPLOAD_ERR_FORM_SIZE:
             $this->_errorHandler->throwError(QFINDER_CONNECTOR_ERROR_UPLOADED_TOO_BIG);
             break;
         case UPLOAD_ERR_PARTIAL:
         case UPLOAD_ERR_NO_FILE:
             $this->_errorHandler->throwError(QFINDER_CONNECTOR_ERROR_UPLOADED_CORRUPT);
             break;
         case UPLOAD_ERR_NO_TMP_DIR:
             $this->_errorHandler->throwError(QFINDER_CONNECTOR_ERROR_UPLOADED_NO_TMP_DIR);
             break;
         case UPLOAD_ERR_CANT_WRITE:
             $this->_errorHandler->throwError(QFINDER_CONNECTOR_ERROR_ACCESS_DENIED);
             break;
         case UPLOAD_ERR_EXTENSION:
             $this->_errorHandler->throwError(QFINDER_CONNECTOR_ERROR_ACCESS_DENIED);
             break;
     }
     $sServerDir = $this->_currentFolder->getServerPath();
     while (true) {
         $sFilePath = QFinder_Connector_Utils_FileSystem::combinePaths($sServerDir, $sFileName);
         if (file_exists($sFilePath)) {
             $sFileName = QFinder_Connector_Utils_FileSystem::autoRename($sServerDir, $sFileName);
             $oRegistry->set("FileUpload_fileName", $sFileName);
             $iErrorNumber = QFINDER_CONNECTOR_ERROR_UPLOADED_FILE_RENAMED;
         } else {
             if (false === move_uploaded_file($uploadedFile['tmp_name'], $sFilePath)) {
                 $iErrorNumber = QFINDER_CONNECTOR_ERROR_ACCESS_DENIED;
             } else {
                 if (isset($detectHtml) && $detectHtml === -1 && QFinder_Connector_Utils_FileSystem::detectHtml($sFilePath) === true) {
                     @unlink($sFilePath);
                     $this->_errorHandler->throwError(QFINDER_CONNECTOR_ERROR_UPLOADED_WRONG_HTML_FILE);
                 } else {
                     if (isset($isImageValid) && $isImageValid === -1 && QFinder_Connector_Utils_FileSystem::isImageValid($sFilePath, $sExtension) === false) {
                         @unlink($sFilePath);
                         $this->_errorHandler->throwError(QFINDER_CONNECTOR_ERROR_UPLOADED_CORRUPT);
                     }
                 }
             }
             if (is_file($sFilePath) && ($perms = $_config->getChmodFiles())) {
                 $oldumask = umask(0);
                 chmod($sFilePath, $perms);
                 umask($oldumask);
             }
             break;
         }
     }
     if (!$_config->checkSizeAfterScaling()) {
         $this->_errorHandler->throwError($iErrorNumber, true, false);
     }
     //resize image if required
     require_once QFINDER_CONNECTOR_LIB_DIR . "/CommandHandler/Thumbnail.php";
     $_imagesConfig = $_config->getImagesConfig();
     if ($_imagesConfig->getMaxWidth() > 0 && $_imagesConfig->getMaxHeight() > 0 && $_imagesConfig->getQuality() > 0) {
         QFinder_Connector_CommandHandler_Thumbnail::createThumb($sFilePath, $sFilePath, $_imagesConfig->getMaxWidth(), $_imagesConfig->getMaxHeight(), $_imagesConfig->getQuality(), true);
     }
     if ($_config->checkSizeAfterScaling()) {
         //check file size after scaling, attempt to delete if too big
         clearstatcache();
         if ($maxSize && filesize($sFilePath) > $maxSize) {
             @unlink($sFilePath);
             $this->_errorHandler->throwError(QFINDER_CONNECTOR_ERROR_UPLOADED_TOO_BIG);
         } else {
             $this->_errorHandler->throwError($iErrorNumber, true, false);
         }
     }
     QFinder_Connector_Core_Hooks::run('AfterFileUpload', array(&$this->_currentFolder, &$uploadedFile, &$sFilePath));
 }
示例#3
0
文件: Init.php 项目: wharin/quantum
 /**
  * handle request and build XML
  * @access protected
  *
  */
 protected function buildXml()
 {
     $_config =& QFinder_Connector_Core_Factory::getInstance("Core_Config");
     // Create the "ConnectorInfo" node.
     $_oConnInfo = new Qfinder_Connector_Utils_XmlNode("ConnectorInfo");
     $this->_connectorNode->addChild($_oConnInfo);
     $_oConnInfo->addAttribute("enabled", $_config->getIsEnabled() ? "true" : "false");
     if (!$_config->getIsEnabled()) {
         $this->_errorHandler->throwError(QFINDER_CONNECTOR_ERROR_CONNECTOR_DISABLED);
     }
     $_ln = '';
     $_lc = $_config->getLicenseKey() . '                                  ';
     $pos = strpos(QFINDER_CHARS, $_lc[0]) % 5;
     if ($pos == 1 || $pos == 4) {
         $_ln = $_config->getLicenseName();
     }
     $_oConnInfo->addAttribute("s", $_ln);
     $_oConnInfo->addAttribute("c", trim($_lc[11] . $_lc[0] . $_lc[8] . $_lc[12] . $_lc[26] . $_lc[2] . $_lc[3] . $_lc[25] . $_lc[1]));
     $_thumbnailsConfig = $_config->getThumbnailsConfig();
     $_thumbnailsEnabled = $_thumbnailsConfig->getIsEnabled();
     $_oConnInfo->addAttribute("thumbsEnabled", $_thumbnailsEnabled ? "true" : "false");
     if ($_thumbnailsEnabled) {
         $_oConnInfo->addAttribute("thumbsUrl", $_thumbnailsConfig->getUrl());
         $_oConnInfo->addAttribute("thumbsDirectAccess", $_thumbnailsConfig->getDirectAccess() ? "true" : "false");
         $_oConnInfo->addAttribute("thumbsWidth", $_thumbnailsConfig->getMaxWidth());
         $_oConnInfo->addAttribute("thumbsHeight", $_thumbnailsConfig->getMaxHeight());
     }
     $_imagesConfig = $_config->getImagesConfig();
     $_oConnInfo->addAttribute("imgWidth", $_imagesConfig->getMaxWidth());
     $_oConnInfo->addAttribute("imgHeight", $_imagesConfig->getMaxHeight());
     // Create the "ResourceTypes" node.
     $_oResourceTypes = new Qfinder_Connector_Utils_XmlNode("ResourceTypes");
     $this->_connectorNode->addChild($_oResourceTypes);
     // Create the "PluginsInfo" node.
     $_oPluginsInfo = new Qfinder_Connector_Utils_XmlNode("PluginsInfo");
     $this->_connectorNode->addChild($_oPluginsInfo);
     // Load the resource types in an array.
     $_aTypes = $_config->getDefaultResourceTypes();
     if (!sizeof($_aTypes)) {
         $_aTypes = $_config->getResourceTypeNames();
     }
     $_aTypesSize = sizeof($_aTypes);
     if ($_aTypesSize) {
         $phpMaxSize = 0;
         $max_upload = QFinder_Connector_Utils_Misc::returnBytes(ini_get('upload_max_filesize'));
         if ($max_upload) {
             $phpMaxSize = $max_upload;
         }
         $max_post = QFinder_Connector_Utils_Misc::returnBytes(ini_get('post_max_size'));
         if ($max_post) {
             $phpMaxSize = $phpMaxSize ? min($phpMaxSize, $max_post) : $max_post;
         }
         //ini_get('memory_limit') only works if compiled with "--enable-memory-limit"
         $memory_limit = QFinder_Connector_Utils_Misc::returnBytes(@ini_get('memory_limit'));
         if ($memory_limit && $memory_limit != -1) {
             $phpMaxSize = $phpMaxSize ? min($phpMaxSize, $memory_limit) : $memory_limit;
         }
         $_oConnInfo->addAttribute("uploadMaxSize", $phpMaxSize);
         $_oConnInfo->addAttribute("uploadCheckImages", $_config->checkSizeAfterScaling() ? "false" : "true");
         for ($i = 0; $i < $_aTypesSize; $i++) {
             $_resourceTypeName = $_aTypes[$i];
             $_acl = $_config->getAccessControlConfig();
             $_aclMask = $_acl->getComputedMask($_resourceTypeName, "/");
             if (($_aclMask & QFINDER_CONNECTOR_ACL_FOLDER_VIEW) != QFINDER_CONNECTOR_ACL_FOLDER_VIEW) {
                 continue;
             }
             if (!isset($_GET['type']) || $_GET['type'] === $_resourceTypeName) {
                 //print $_resourceTypeName;
                 $_oTypeInfo = $_config->getResourceTypeConfig($_resourceTypeName);
                 //print_r($_oTypeInfo);
                 $_oResourceType[$i] = new Qfinder_Connector_Utils_XmlNode("ResourceType");
                 $_oResourceTypes->addChild($_oResourceType[$i]);
                 $_oResourceType[$i]->addAttribute("name", $_resourceTypeName);
                 $_oResourceType[$i]->addAttribute("url", $_oTypeInfo->getUrl());
                 $_oResourceType[$i]->addAttribute("allowedExtensions", implode(",", $_oTypeInfo->getAllowedExtensions()));
                 $_oResourceType[$i]->addAttribute("deniedExtensions", implode(",", $_oTypeInfo->getDeniedExtensions()));
                 $_oResourceType[$i]->addAttribute("hash", $_oTypeInfo->getHash());
                 $_oResourceType[$i]->addAttribute("hasChildren", QFinder_Connector_Utils_FileSystem::hasChildren('/', $_oTypeInfo) ? "true" : "false");
                 $_oResourceType[$i]->addAttribute("acl", $_aclMask);
                 $maxSize = $_oTypeInfo->getMaxSize();
                 if ($phpMaxSize) {
                     $maxSize = $maxSize ? min($maxSize, $phpMaxSize) : $phpMaxSize;
                 }
                 $_oResourceType[$i]->addAttribute("maxSize", $maxSize);
             }
         }
     }
     $config = $GLOBALS['config'];
     if (!empty($config['Plugins']) && is_array($config['Plugins'])) {
         $_oConnInfo->addAttribute("plugins", implode(",", $config['Plugins']));
     }
     QFinder_Connector_Core_Hooks::run('InitCommand', array(&$this->_connectorNode));
 }
示例#4
0
文件: Hooks.php 项目: wharin/quantum
 /**
  * Run user defined hooks
  *
  * @param string $event
  * @param object $errorHandler
  * @param array $args
  * @return boolean (true to continue processing, false otherwise)
  */
 public static function run($event, $args = array())
 {
     $config = $GLOBALS['config'];
     if (!isset($config['Hooks'])) {
         return true;
     }
     $hooks =& $config['Hooks'];
     if (!is_array($hooks) || !array_key_exists($event, $hooks) || !is_array($hooks[$event])) {
         return true;
     }
     $errorHandler = $GLOBALS['connector']->getErrorHandler();
     foreach ($hooks[$event] as $i => $hook) {
         $object = NULL;
         $method = NULL;
         $function = NULL;
         $data = NULL;
         $passData = false;
         /* $hook can be: a function, an object, an array of $functiontion and $data,
          * an array of just a function, an array of object and method, or an
          * array of object, method, and data.
          */
         //function
         if (is_string($hook)) {
             $function = $hook;
         } else {
             if (is_object($hook)) {
                 $object = $hooks[$event][$i];
                 $method = "on" . $event;
             } else {
                 if (is_array($hook)) {
                     $count = count($hook);
                     if ($count) {
                         //...object
                         if (is_object($hook[0])) {
                             $object = $hooks[$event][$i][0];
                             if ($count < 2) {
                                 $method = "on" . $event;
                             } else {
                                 //...object and method
                                 $method = $hook[1];
                                 if (count($hook) > 2) {
                                     //...object, method and data
                                     $passData = true;
                                     $data = $hook[2];
                                 }
                             }
                         } else {
                             if (is_string($hook[0])) {
                                 $function = $hook[0];
                                 if ($count > 1) {
                                     //...function with data
                                     $passData = true;
                                     $data = $hook[1];
                                 }
                             }
                         }
                     }
                 }
             }
         }
         /* If defined, add data to the arguments array */
         if ($passData) {
             $args = array_merge(array($data), $args);
         }
         if (isset($object)) {
             $callback = array($object, $method);
         } else {
             if (false !== ($pos = strpos($function, '::'))) {
                 $callback = array(substr($function, 0, $pos), substr($function, $pos + 2));
             } else {
                 $callback = $function;
             }
         }
         if (is_callable($callback)) {
             $ret = call_user_func_array($callback, $args);
         } else {
             $functionName = QFinder_Connector_Core_Hooks::_printCallback($callback);
             $errorHandler->throwError(QFINDER_CONNECTOR_ERROR_CUSTOM_ERROR, "QFinder failed to call a hook: " . $functionName);
             return false;
         }
         //String return is a custom error
         if (is_string($ret)) {
             $errorHandler->throwError(QFINDER_CONNECTOR_ERROR_CUSTOM_ERROR, $ret);
             return false;
         } else {
             if (is_int($ret)) {
                 $errorHandler->throwError($ret);
                 return false;
             } else {
                 if ($ret === null) {
                     $functionName = QFinder_Connector_Core_Hooks::_printCallback($callback);
                     $errorHandler->throwError(QFINDER_CONNECTOR_ERROR_CUSTOM_ERROR, "QFinder extension returned an invalid value (null)." . "Hook " . $functionName . " should return a value.");
                     return false;
                 } else {
                     if (!$ret) {
                         return false;
                     }
                 }
             }
         }
     }
     return true;
 }