/** * Recursively copy a folder and its contents * http://aidan.dotgeek.org/lib/?file=function.copyr.php * * @author Aidan Lister <*****@*****.**> * @version 1.0.1 * @param string $source Source path * @param string $dest Destination path * @return bool Returns TRUE on success, FALSE on failure */ function copyDir($source, $dest) { clearstatcache(); // Simple copy for a file if (is_file($source)) { return File::copy($source, $dest); } // Make destination directory if (!File::isDir($dest)) { File::createDir($dest); } // Loop through the folder $dir = dir($source); while (false !== ($entry = $dir->read())) { // Skip pointers if ($entry == '.' || $entry == '..') { continue; } // Deep copy directories if ($dest !== "{$source}/{$entry}") { MyFile::copyDir("{$source}/{$entry}", "{$dest}/{$entry}"); } } // Clean up $dir->close(); return true; }
function register() { $config =& Config::getConfig(); $this->cacheFolder = $config->getValue('temp_folder'); $this->cacheFolder = $this->cacheFolder . '/sitemap/' . $this->blogInfo->getId(); if (!File::exists($this->cacheFolder)) { File::createDir($this->cacheFolder); } $blogSettings = $this->blogInfo->getSettings(); $this->pluginEnabled = $blogSettings->getValue("plugin_sitemap_enabled"); }
function perform() { $this->_fileContent = $this->_request->getValue("fileContent"); // get a list with all the specific template files $ts = new TemplateSetStorage(); $blogId = $this->_blogInfo->getId(); $templateFolder = $ts->getTemplateFolder($this->_templateId, $blogId); if (!empty($this->_subFolderId)) { $templateFolder = $templateFolder . $this->_subFolderId . "/"; } $backupFolder = $templateFolder . "backups/"; if (!File::exists($backupFolder)) { File::createDir($backupFolder); } $fileName = $templateFolder . $this->_fileId; $backupFileName = $backupFolder . $this->_fileId . "_" . time(); if (!File::copy($fileName, $backupFileName)) { if (empty($this->_subFolderId)) { $this->_view = new PluginBlogEditTemplateFileView($this->_blogInfo, $this->_templateId, $this->_fileId, $this->_backupId); } else { $this->_view = new PluginBlogEditSubFolderTemplateFileView($this->_blogInfo, $this->_templateId, $this->_subFolderId, $this->_fileId, $this->_backupId); } $this->_view->setErrorMessage($this->_locale->tr("error_backup_template_file")); $this->setCommonData(); return false; } $file = new MyFile($fileName); if (!$file->isWritable()) { if (empty($this->_subFolderId)) { $this->_view = new PluginBlogEditTemplateFileView($this->_blogInfo, $this->_templateId, $this->_fileId, $this->_backupId); } else { $this->_view = new PluginBlogEditSubFolderTemplateFileView($this->_blogInfo, $this->_templateId, $this->_subFolderId, $this->_fileId, $this->_backupId); } $this->_view->setErrorMessage($this->_locale->tr("error_updating_template_file")); $this->setCommonData(); return false; } $fileContent = $file->writeFileContent($this->_fileContent); // if everything went ok... $this->_session->setValue("blogInfo", $this->_blogInfo); $this->saveSession(); if (empty($this->_subFolderId)) { $this->_view = new PluginBlogTemplatesListView($this->_blogInfo, $this->_templateId); } else { $this->_view = new PluginBlogTemplateSubFolderListView($this->_blogInfo, $this->_templateId, $this->_subFolderId); } $this->_view->setSuccessMessage($this->_locale->tr("templateeditor_file_saved_ok")); $this->setCommonData(); // clear the cache CacheControl::resetBlogCache($this->_blogInfo->getId()); return true; }
function render() { $config =& Config::getConfig(); $maxBackupFiles = $config->getValue("plugin_templateeditor_maxbackupfiles"); if ($maxBackupFiles == "") { $maxBackupFiles = 5; } // get a list with all the specific template files $ts = new TemplateSetStorage(); $blogId = $this->_blogInfo->getId(); $templateFolder = $ts->getTemplateFolder($this->_templateId); $templateFolder = $templateFolder . $this->_subFolderId . "/"; $backupFolder = $templateFolder . "backups/"; if (!File::exists($backupFolder)) { File::createDir($backupFolder); } if (!$this->_backupId) { $filename = $templateFolder . $this->_fileId; } else { $filename = $backupFolder . $this->_fileId . "_" . $this->_backupId; } $backupFilePattern = $this->_fileId . "_*"; $bakFiles = Glob::myGlob($backupFolder, $backupFilePattern); sort($bakFiles); $backupFiles = array(); $backupFileCount = 0; for ($i = count($bakFiles) - 1; $i >= 0; $i--) { $bakFile = $bakFiles[$i]; if ($backupFileCount < $maxBackupFiles) { $bakElements = explode("_", $bakFile); $bakId = $bakElements[count($bakElements) - 1]; $bakTime = strftime("%Y/%m/%d - %H:%M:%S", $bakId); $file['time'] = $bakTime; $file['backupId'] = basename($bakId); array_push($backupFiles, $file); $backupFileCount++; } else { File::delete($bakFile); } } $file = new MyFile($filename); $fileContent = $file->readFileContent(); $this->setValue("backupId", $this->_backupId); $this->setValue("backupFiles", $backupFiles); $this->setValue("currentTemplate", $this->_templateId); $this->setValue("currentSubFolder", $this->_subFolderId); $this->setValue("currentFile", $this->_fileId); $this->setValue("fileContent", $fileContent); parent::render(); }
function register() { $config =& Config::getConfig(); $this->cacheFolder = $config->getValue('temp_folder'); $this->cacheFolder = $this->cacheFolder . '/authimage/' . $this->blogInfo->getId(); if (!File::exists($this->cacheFolder)) { File::createDir($this->cacheFolder); } $blogSettings = $this->blogInfo->getSettings(); $this->pluginEnabled = $blogSettings->getValue("plugin_authimage_enabled"); $this->length = $blogSettings->getValue("plugin_authimage_length"); $this->key = $blogSettings->getValue("plugin_authimage_key"); $this->expiredTime = $blogSettings->getValue("plugin_authimage_expiredtime"); if ($this->expiredTime == "") { $this->expiredTime = 3600; } $this->default = $blogSettings->getValue("plugin_authimage_default"); }
/** * Makes sure that the file is a valid template set. The file can be packed * in any of the formats supported by the Unpacker class (.tar.gz, .tar.bz2 * and .zip as of the time of writing these lines) * Returns true if the template is valid or a negative value carrying an * error code. * * @param file The file that contains the template set * @return Returns true (positive value) if template set is ok or a negative * value otherwise. */ function checkTemplateSet($file, $filePath) { // get the temporary folder $config =& Config::getConfig(); $tmpFolder = $config->getValue('temp_folder'); if ($tmpFolder[strlen($tmpFolder) - 1] != '/') { $tmpFolder .= '/'; } // get the name of the file, which we will use in many places $fileNameParts = explode('.', $file); $fileNameNoExt = $fileNameParts[0]; // create our working folder $workFolder = $tmpFolder . File::getTempName() . '/'; if (!File::createDir($workFolder, 0777)) { return TEMPLATE_SANDBOX_ERROR_CREATING_WORKING_FOLDER; } // now we can unpack the file to the temporary folder $unpacker = new Unpacker(); if (!$unpacker->unpack($filePath . $file, $workFolder)) { $this->cleanUp($workFolder . $fileNameNoExt); if (File::exists($workFolder)) { File::delete($workFolder); } return TEMPLATE_SANDBOX_ERROR_UNPACKING; } // if the file was correctly unpacked, now we will need the TemplateValidator // class to do some work for us $fileNameParts = explode('.', $file); $fileNameNoExt = $fileNameParts[0]; // we can use the checkTenmplateFolder which will do all the rest of // the work for us... $res = $this->checkTemplateFolder($fileNameNoExt, $workFolder); if ($res < 0) { //$this->cleanUp( $workFolder.$fileNameNoExt ); $this->cleanUp($workFolder); if (File::isReadable($workFolder) && File::isDir($workFolder)) { File::delete($workFolder); } return $res; } $this->cleanUp($workFolder . $fileNameNoExt); File::delete($workFolder); return true; }
/** * Creates a new folder. If the folder name is /a/b/c and neither * /a or /a/b exist, this method will take care of creating the * whole folder structure automatically. * * @static * @param dirName The name of the new folder * @param mode Attributes that will be given to the folder * @return Returns true if no problem or false otherwise. */ function createDir($dirName, $mode = FILE_DEFAULT_DIRECTORY_CREATION_MODE) { if (File::exists($dirName)) { return true; } if (substr($dirName, strlen($dirName) - 1) == "/") { $dirName = substr($dirName, 0, strlen($dirName) - 1); } // for example, we will create dir "/a/b/c" // $firstPart = "/a/b" $firstPart = substr($dirName, 0, strrpos($dirName, "/")); if (file_exists($firstPart)) { if (!mkdir($dirName, $mode)) { return false; } chmod($dirName, $mode); } else { File::createDir($firstPart, $mode); if (!mkdir($dirName, $mode)) { return false; } chmod($dirName, $mode); } return true; }
/** * Sets the folder where sessions should be saved, in case we'd like to save * them somewhere else. This class will check the config parameter <b>session_save_path</b>: if * it's not empty, it will use its value as the name of the folder where sessions should be saved, and it * will also take care of creating the folder if it does not exist. If the folder exists but it cannot * be read, it will throw an exception and quit (because this is a big issue) * If the value of this parameter is empty, it will not do anything and use PHP's default settings for this. * * @static */ function setSessionSavePath() { $config =& Config::getConfig(); $sessionFolder = $config->getValue("session_save_path"); // do we need to do anything if we are using the default // session path? PHP defaults to /tmp/, so there isn't // anything to do if (isset($sessionFolder)) { if (!File::exists($sessionFolder)) { // create folder with only user permissions // since we want to protect the session data if (!File::createDir($sessionFolder, 0700)) { throw new Exception("Sessions should be " . "saved in {$sessionFolder} but it " . "doesn't exist and I can't create it!"); die; } } // check if the folder is accessible if (!File::isReadable($sessionFolder) || !File::isWritable($sessionFolder)) { if (!File::chMod($sessionFolder, 0700)) { throw new Exception("Sessions should be " . "saved in {$sessionFolder} but it is " . "not accessible!"); die; } } // if everything ok, we can continue... session_save_path($sessionFolder); } return true; }
/** * @private */ function _configureTemplateSettings($t, $blogInfo, $layout = "") { // change a few things... $config =& Config::getConfig(); $tmpFolder = $config->getValue('temp_folder'); if ($blogInfo == null) { $blogTmpFolder = $tmpFolder; } else { $blogTmpFolder = $tmpFolder . '/' . $blogInfo->getId(); if (!File::exists($blogTmpFolder)) { File::createDir($blogTmpFolder, DEFAULT_TEMPLATE_TEMP_FOLDER_PERMISSIONS); } $t->secure_dir[] = "./templates/blog_" . $blogInfo->getId() . "/{$layout}"; } $t->cache_dir = $blogTmpFolder; $t->compile_dir = $blogTmpFolder; $t->compile_check = $config->getValue('template_compile_check'); return $t; }
public static function createDataDir($uid, $type = 'img') { $fileid = self::creatNumFileId($uid); $d = date('Y'); $m = date('m') . '-' . date('d'); File::createDir(__DATA . $d); File::createDir(__DATA . $d . '/' . $m); File::createDir(__DATA . $d . '/' . $m . '/' . $fileid); File::createDir(__DATA . $d . '/' . $m . '/' . $fileid . '/' . $uid); File::createDir(__DATA . $d . '/' . $m . '/' . $fileid . '/' . $uid . '/' . $type); return $d . '/' . $m . '/' . $fileid . '/' . $uid . '/' . $type . '/'; }
/** * @public */ function checkMediumSizePreviewsStorageFolder($ownerId) { $previewsFolder = GalleryResourceStorage::getMediumSizePreviewsFolder($ownerId); if ($previewsFolder[strlen($previewsFolder) - 1] == "/") { $previewsFolder = substr($previewsFolder, 0, strlen($previewsFolder) - 1); } if (!File::isDir($previewsFolder)) { // folder does not exist, so we should try to create it if (!File::createDir($previewsFolder, 0755)) { throw new Exception("Could not create user storage folder for medium size previews: " . $previewsFolder); //die(); return false; } } if (!File::isReadable($previewsFolder)) { throw new Exception($previewsFolder . " user previews storage folder exists but it is not readable!"); //die(); return false; } return true; }
/** * checks if there are any mime parts, perhaps attachments or something like * that... */ function parseMimeParts($parts) { $this->_attachments = array(); foreach ($parts as $part) { MoblogLogger::log("attachment type = " . $part->ctype_primary . "/" . $part->ctype_secondary); if (strtolower($part->ctype_primary) == "text" && strtolower($part->ctype_secondary) == "plain") { // if ctype_primary == text and ctype_secondary == plain, it should be text format e-mail MoblogLogger::log("Reparsing the body of the message!"); MoblogLogger::log("text/plain part - contents = " . $part->body); $this->parseBody($part->body); $this->_inputEncoding = strtoupper($part->ctype_parameters['charset']); } elseif (strtolower($part->ctype_primary) == "multipart" && strtolower($part->ctype_secondary) == "alternative") { // if ctype_primary == multipart and ctype_secondary == alternative, it should be html format e-mail // We should look into it's child to get the real body foreach ($part->parts as $childPart) { if (strtolower($childPart->ctype_primary) == "text" && strtolower($childPart->ctype_secondary) == "plain") { $this->parseBody($childPart->body); $this->_inputEncoding = strtoupper($childPart->ctype_parameters['charset']); } } } else { // whatever comes as a part, we will take it and treat it as if it was // a normal resource as long as it's not an application/smil thingie... if (strtolower($part->ctype_secondary) != 'html' && strtolower($part->ctype_secondary) != 'smil' && MOBLOG_IGNORE_SMIL_ATTACHMENTS) { // GalleryResources::addResource only deals with uploaded files, so we'll // have to pretend that this is one of them! $config =& Config::getConfig(); // $tmpFolder = $config->getValue( "temp_folder" ); // $tmpFolderName = $tmpFolder."/".md5(microtime()); $tmpFolderName = $config->getValue("temp_folder"); File::createDir($tmpFolderName); MoblogLogger::log("Creating temp folder = {$tmpFolderName}"); $fileName = stripslashes($part->ctype_parameters["name"]); if ($fileName == "") { $fileName = stripslashes($part->d_parameters["filename"]); } $fileName = str_replace("\"", "", $fileName); $fileName = str_replace("'", "", $fileName); $tmpName = $tmpFolderName . "/" . md5(microtime()) . "_" . $fileName; $fileName = urldecode($fileName); if (MOBLOG_FORCE_ENCODE_TO_UTF8) { $fileName = $this->encodeToUTF8($fileName, $this->_inputEncoding); } $f = new File($tmpName); $f->open("ab+"); MoblogLogger::log("fileName = " . $tmpName); MoblogLogger::log("Saving attachment as = " . $tmpName); //MoblogLogger::log( "base64 encoded data = ".$part->body ); if (!$f->write($part->body)) { MoblogLogger::log("There was an error writing the temporary file"); die; } MoblogLogger::log("File saved ok!"); MoblogLogger::log("FILENAME = {$fileName}"); $uploadInfo = array("name" => urldecode($fileName), "type" => $part->ctype_primary . "/" . $part->ctype_secondary, "tmp_name" => $tmpName, "size" => $f->getSize(), "error" => 0); $upload = new FileUpload($uploadInfo); $upload->setFolder($tmpFolderName); array_push($this->_attachments, $upload); } } } }
/** * Creates the blog-specific folder where templates are stored. * If the folder is not there, it will be created. If it is already there, * then nothing will happen. * * @param blogId The identifier of the blog. */ function createBlogTemplateFolder($blogId) { // make sure that the blog-specific folder exists $templateFolder = $this->getBaseTemplateFolder(); $blogTemplateFolder = "{$templateFolder}/blog_" . $blogId; if (!File::isDir($blogTemplateFolder)) { File::createDir($blogTemplateFolder); } return $blogTemplateFolder; }