/** * Check whether Connector is enabled * @access protected * */ function checkConnector() { $_config =& CKEditor_Connector_Core_Factory::getInstance("Core_Config"); if (!$_config->getIsEnabled()) { $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_CONNECTOR_DISABLED); } }
/** * handle request and build XML * @access protected * */ function buildXml() { $_config =& CKEditor_Connector_Core_Factory::getInstance("Core_Config"); // Map the virtual path to the local server path. $_sServerDir = $this->_currentFolder->getServerPath(); if (!is_dir($_sServerDir)) { $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_FOLDER_NOT_FOUND); } // Create the "Folders" node. $oFoldersNode = new CKEditor_Connector_Utils_XmlNode("Folders"); $this->_connectorNode->addChild($oFoldersNode); $files = array(); if ($dh = @opendir($_sServerDir)) { while (($file = readdir($dh)) !== false) { if ($file != "." && $file != ".." && is_dir($_sServerDir . $file)) { $files[] = $file; } } closedir($dh); } else { $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_ACCESS_DENIED); } $resourceTypeInfo = $this->_currentFolder->getResourceTypeConfig(); if (sizeof($files) > 0) { natcasesort($files); $i = 0; foreach ($files as $file) { // Create the "Folder" node. $oFolderNode[$i] = new CKEditor_Connector_Utils_XmlNode("Folder"); $oFoldersNode->addChild($oFolderNode[$i]); $oFolderNode[$i]->addAttribute("name", CKEditor_Connector_Utils_FileSystem::convertToConnectorEncoding($file)); $i++; } } }
/** * send response * @access public * */ function sendResponse() { $xml =& CKEditor_Connector_Core_Factory::getInstance("Core_Xml"); $this->_connectorNode =& $xml->getConnectorNode(); $this->checkConnector(); if ($this->mustCheckRequest()) { $this->checkRequest(); } $resourceTypeName = $this->_currentFolder->getResourceTypeName(); if (!empty($resourceTypeName)) { $this->_connectorNode->addAttribute("resourceType", $this->_currentFolder->getResourceTypeName()); } if ($this->mustAddCurrentFolderNode()) { $_currentFolder = new CKEditor_Connector_Utils_XmlNode("CurrentFolder"); $this->_connectorNode->addChild($_currentFolder); $_currentFolder->addAttribute("path", CKEditor_Connector_Utils_FileSystem::convertToConnectorEncoding($this->_currentFolder->getClientPath())); $this->_errorHandler->setCatchAllErros(true); $_url = $this->_currentFolder->getUrl(); $_currentFolder->addAttribute("url", is_null($_url) ? "" : CKEditor_Connector_Utils_FileSystem::convertToConnectorEncoding($_url)); $this->_errorHandler->setCatchAllErros(false); } $this->buildXml(); $_oErrorNode =& $xml->getErrorNode(); $_oErrorNode->addAttribute("number", "0"); echo $this->_connectorNode->asXML(); exit; }
/** * handle request and build XML * @access protected * */ function buildXml() { $_config =& CKEditor_Connector_Core_Factory::getInstance("Core_Config"); $_resourceTypeConfig = $this->_currentFolder->getResourceTypeConfig(); $sNewFolderName = isset($_POST["newFolderName"]) ? $_POST["newFolderName"] : ""; $sNewFolderName = CKEditor_Connector_Utils_FileSystem::convertToFilesystemEncoding($sNewFolderName); if (!CKEditor_Connector_Utils_FileSystem::checkFileName($sNewFolderName)) { $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_INVALID_NAME); } $sServerDir = CKEditor_Connector_Utils_FileSystem::combinePaths($this->_currentFolder->getServerPath(), $sNewFolderName); if (!is_writeable($this->_currentFolder->getServerPath())) { $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_ACCESS_DENIED); } $bCreated = false; if (file_exists($sServerDir)) { $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_ALREADY_EXIST); } if ($perms = $_config->getChmodFolders()) { $oldUmask = umask(0); $bCreated = @mkdir($sServerDir, $perms); umask($oldUmask); } else { $bCreated = @mkdir($sServerDir); } if (!$bCreated) { $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_ACCESS_DENIED); } else { $oNewFolderNode = new CKEditor_Connector_Utils_XmlNode("NewFolder"); $this->_connectorNode->addChild($oNewFolderNode); $oNewFolderNode->addAttribute("name", CKEditor_Connector_Utils_FileSystem::convertToConnectorEncoding($sNewFolderName)); } }
/** * handle request and build XML * @access protected * */ protected function buildXml() { $_config =& CKEditor_Connector_Core_Factory::getInstance("Core_Config"); // Create the "ConnectorInfo" node. $_oConnInfo = new CKEditor_Connector_Utils_XmlNode("ConnectorInfo"); $this->_connectorNode->addChild($_oConnInfo); $_oConnInfo->addAttribute("enabled", $_config->getIsEnabled() ? "true" : "false"); if (!$_config->getIsEnabled()) { $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_CONNECTOR_DISABLED); } // Create the "ResourceTypes" node. $_oResourceTypes = new CKEditor_Connector_Utils_XmlNode("ResourceTypes"); $this->_connectorNode->addChild($_oResourceTypes); // Load the resource types in an array. $_aTypes = $_config->getDefaultResourceTypes(); if (!sizeof($_aTypes)) { $_aTypes = $_config->getResourceTypeNames(); } $_aTypesSize = sizeof($_aTypes); if ($_aTypesSize) { for ($i = 0; $i < $_aTypesSize; $i++) { $_resourceTypeName = $_aTypes[$i]; if (!isset($_GET['type']) || $_GET['type'] === $_resourceTypeName) { $_oTypeInfo = $_config->getResourceTypeConfig($_resourceTypeName); $_oResourceType[$i] = new CKEditor_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())); } } } }
/** * Throw connector error, return true if error has been thrown, false if error has been catched * * @param int $number * @param string $text * @access public */ function throwError($number, $text = false) { if ($this->_catchAllErrors || in_array($number, $this->_skipErrorsArray)) { return false; } $_xml =& CKEditor_Connector_Core_Factory::getInstance("Core_Xml"); $_xml->raiseError($number, $text); exit; }
/** * Throw file upload error, return true if error has been thrown, false if error has been catched * * @param int $number * @param string $text * @access public */ public function throwError($number, $text = false, $exit = true) { if ($this->_catchAllErrors || in_array($number, $this->_skipErrorsArray)) { return false; } $oRegistry =& CKEditor_Connector_Core_Factory::getInstance("Core_Registry"); $sFileName = $oRegistry->get("FileUpload_fileName"); echo "<script type=\"text/javascript\">"; if (empty($text)) { echo "window.parent.OnUploadCompleted(" . $number . ") ;"; } else { echo "window.parent.OnUploadCompleted(" . $number . ",'" . str_replace("'", "\\'", $sFileName) . "') ;"; } echo "</script>"; if ($exit) { exit; } }
/** * Get Url * * @return string * @access public */ public function getUrl() { if (is_null($this->_url)) { $this->_resourceTypeConfig = $this->getResourceTypeConfig(); if (is_null($this->_resourceTypeConfig)) { $connector =& CKEditor_Connector_Core_Factory::getInstance("Core_Connector"); $oErrorHandler =& $connector->getErrorHandler(); $oErrorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_INVALID_TYPE); $this->_url = ""; } else { $this->_url = $this->_resourceTypeConfig->getUrl() . ltrim($this->getClientPath(), "/"); } } return $this->_url; }
/** * Simple function required by config.php - discover the server side path * to the directory relative to the "$baseUrl" attribute * * @package CKEditor * @subpackage Connector * @param string $baseUrl * @return string */ function resolveUrl($baseUrl) { $fileSystem =& CKEditor_Connector_Core_Factory::getInstance("Utils_FileSystem"); return $fileSystem->getDocumentRootPath() . $baseUrl; }
/** * Check extension, return true if file name is valid. * Return false if extension is on denied list. * If allowed extensions are defined, return false if extension isn't on allowed list. * * @access public * @param string $extension extension * @param boolean $renameIfRequired whether try to rename file or not * @return boolean */ function checkExtension(&$fileName, $renameIfRequired = true) { if (strpos($fileName, '.') === false) { return true; } if (is_null($this->_config)) { $this->_config =& CKEditor_Connector_Core_Factory::getInstance("Core_Config"); } $toCheck = array(); if ($this->_config->getCheckDoubleExtension()) { $pieces = explode('.', $fileName); // First, check the last extension (ex. in file.php.jpg, the "jpg"). if (!$this->checkSingleExtension($pieces[sizeof($pieces) - 1])) { return false; } if ($renameIfRequired) { // Check the other extensions, rebuilding the file name. If an extension is // not allowed, replace the dot with an underscore. $fileName = $pieces[0]; for ($i = 1; $i < sizeof($pieces) - 1; $i++) { $fileName .= $this->checkSingleExtension($pieces[$i]) ? '.' : '_'; $fileName .= $pieces[$i]; } // Add the last extension to the final name. $fileName .= '.' . $pieces[sizeof($pieces) - 1]; } } else { // Check only the last extension (ex. in file.php.jpg, only "jpg"). return $this->checkSingleExtension(substr($fileName, strrpos($fileName, '.') + 1)); } return true; }
/** * Get error handler * * @access public * @return CKEditor_Connector_ErrorHandler_Base|CKEditor_Connector_ErrorHandler_FileUpload|CKEditor_Connector_ErrorHandler_Http */ function &getErrorHandler() { $_errorHandler = $this->_registry->get("errorHandler"); $oErrorHandler =& CKEditor_Connector_Core_Factory::getInstance($_errorHandler); return $oErrorHandler; }
function sendResponse() { $oRegistry =& CKEditor_Connector_Core_Factory::getInstance("Core_Registry"); $oRegistry->set("FileUpload_url", $this->_currentFolder->getUrl()); return parent::sendResponse(); }
/** * handle request and build XML * @access protected * */ function buildXml() { $_config =& CKEditor_Connector_Core_Factory::getInstance("Core_Config"); // Map the virtual path to the local server path. $_sServerDir = $this->_currentFolder->getServerPath(); $files = array(); $folders = array(); if ($dh = @opendir($_sServerDir)) { while (($file = readdir($dh)) !== false) { if ($file == "." || $file == "..") { continue; } else { if (!is_dir($_sServerDir . $file)) { $files[] = $file; } else { $folders[] = $file; } } } closedir($dh); } else { $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_ACCESS_DENIED); } // Create the "Folders" node. $oFoldersNode = new CKEditor_Connector_Utils_XmlNode("Folders"); $this->_connectorNode->addChild($oFoldersNode); $resourceTypeInfo = $this->_currentFolder->getResourceTypeConfig(); if (sizeof($folders) > 0) { natcasesort($folders); $i = 0; foreach ($folders as $file) { // Create the "Folder" node. $oFolderNode[$i] = new CKEditor_Connector_Utils_XmlNode("Folder"); $oFoldersNode->addChild($oFolderNode[$i]); $oFolderNode[$i]->addAttribute("name", CKEditor_Connector_Utils_FileSystem::convertToConnectorEncoding($file)); $i++; } } // Create the "Files" node. $oFilesNode = new CKEditor_Connector_Utils_XmlNode("Files"); $this->_connectorNode->addChild($oFilesNode); if (!is_dir($_sServerDir)) { $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_FOLDER_NOT_FOUND); } if (sizeof($files) > 0) { natcasesort($files); $i = 0; foreach ($files as $file) { $filemtime = @filemtime($_sServerDir . $file); //otherwise file doesn't exist or we can't get it's filename properly if ($filemtime !== false) { $filename = basename($file); if (!$resourceTypeInfo->checkExtension($filename, false)) { continue; } $oFileNode[$i] = new CKEditor_Connector_Utils_XmlNode("File"); $oFilesNode->addChild($oFileNode[$i]); $oFileNode[$i]->addAttribute("name", CKEditor_Connector_Utils_FileSystem::convertToConnectorEncoding(basename($file))); $oFileNode[$i]->addAttribute("date", @date("YmdHi", $filemtime)); $size = filesize($_sServerDir . $file); if ($size && $size < 1024) { $size = 1; } else { $size = (int) round($size / 1024); } $oFileNode[$i]->addAttribute("size", $size); $i++; } } } }
/** * send response (save uploaded file) * @access public * */ function sendResponse() { $iErrorNumber = CKEDITOR_CONNECTOR_ERROR_NONE; $oRegistry =& CKEditor_Connector_Core_Factory::getInstance("Core_Registry"); $oRegistry->set("FileUpload_fileName", "unknown file"); $uploadedFile = array_shift($_FILES); if (!isset($uploadedFile['name'])) { $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_UPLOADED_INVALID); } $sFileName = CKEditor_Connector_Utils_FileSystem::convertToFilesystemEncoding(basename($uploadedFile['name'])); $oRegistry->set("FileUpload_fileName", $sFileName); $this->checkConnector(); $this->checkRequest(); if (!CKEditor_Connector_Utils_FileSystem::checkFileName($sFileName)) { $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_INVALID_NAME); } $_config =& CKEditor_Connector_Core_Factory::getInstance("Core_Config"); $_resourceTypeConfig = $this->_currentFolder->getResourceTypeConfig(); $resourceTypeInfo = $this->_currentFolder->getResourceTypeConfig(); if (!$resourceTypeInfo->checkExtension($sFileName)) { $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_INVALID_EXTENSION); } $sFileNameOrginal = $sFileName; $oRegistry->set("FileUpload_fileName", $sFileName); $htmlExtensions = $_config->getHtmlExtensions(); $sExtension = CKEditor_Connector_Utils_FileSystem::getExtension($sFileNameOrginal); if ($htmlExtensions && !CKEditor_Connector_Utils_Misc::inArrayCaseInsensitive($sExtension, $htmlExtensions) && ($detectHtml = CKEditor_Connector_Utils_FileSystem::detectHtml($uploadedFile['tmp_name'])) === true) { $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_UPLOADED_WRONG_HTML_FILE); } $sExtension = CKEditor_Connector_Utils_FileSystem::getExtension($sFileNameOrginal); $secureImageUploads = $_config->getSecureImageUploads(); if ($secureImageUploads && ($isImageValid = CKEditor_Connector_Utils_FileSystem::isImageValid($uploadedFile['tmp_name'], $sExtension)) === false) { $this->_errorHandler->throwError(CKEDITOR_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(CKEDITOR_CONNECTOR_ERROR_UPLOADED_TOO_BIG); break; case UPLOAD_ERR_PARTIAL: case UPLOAD_ERR_NO_FILE: $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_UPLOADED_CORRUPT); break; case UPLOAD_ERR_NO_TMP_DIR: $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_UPLOADED_NO_TMP_DIR); break; case UPLOAD_ERR_CANT_WRITE: $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_ACCESS_DENIED); break; case UPLOAD_ERR_EXTENSION: $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_ACCESS_DENIED); break; } $sServerDir = $this->_currentFolder->getServerPath(); $iCounter = 0; while (true) { $sFilePath = CKEditor_Connector_Utils_FileSystem::combinePaths($sServerDir, $sFileName); if (file_exists($sFilePath)) { $iCounter++; $sFileName = CKEditor_Connector_Utils_FileSystem::getFileNameWithoutExtension($sFileNameOrginal) . "(" . $iCounter . ")" . "." . CKEditor_Connector_Utils_FileSystem::getExtension($sFileNameOrginal); $oRegistry->set("FileUpload_fileName", $sFileName); $iErrorNumber = CKEDITOR_CONNECTOR_ERROR_UPLOADED_FILE_RENAMED; } else { if (false === move_uploaded_file($uploadedFile['tmp_name'], $sFilePath)) { $iErrorNumber = CKEDITOR_CONNECTOR_ERROR_ACCESS_DENIED; } else { if (isset($detectHtml) && $detectHtml === -1 && CKEditor_Connector_Utils_FileSystem::detectHtml($sFilePath) === true) { @unlink($sFilePath); $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_UPLOADED_WRONG_HTML_FILE); } else { if (isset($isImageValid) && $isImageValid === -1 && CKEditor_Connector_Utils_FileSystem::isImageValid($sFilePath, $sExtension) === false) { @unlink($sFilePath); $this->_errorHandler->throwError(CKEDITOR_CONNECTOR_ERROR_UPLOADED_CORRUPT); } } } if (is_file($sFilePath) && ($perms = $_config->getChmodFiles())) { $oldumask = umask(0); chmod($sFilePath, $perms); umask($oldumask); } break; } } $this->_errorHandler->throwError($iErrorNumber, $sFileName, false); }
/** * Create directory recursively * * @access public * @static * @param string $dir * @return boolean */ function createDirectoryRecursively($dir) { if (DIRECTORY_SEPARATOR === "\\") { $dir = str_replace("/", "\\", $dir); } else { if (DIRECTORY_SEPARATOR === "/") { $dir = str_replace("\\", "/", $dir); } } $_config =& CKEditor_Connector_Core_Factory::getInstance("Core_Config"); if ($perms = $_config->getChmodFolders()) { $oldUmask = umask(0); $bCreated = @mkdir($dir, $perms, true); umask($oldUmask); } else { $bCreated = @mkdir($dir, 0777, true); } return $bCreated; }