public function testChmod() { $file = new File('file'); $this->assertEquals(Node::DEF_MODE | File::S_IFTYPE, $file->mode()); $file->chmod(0200); $this->assertEquals(0200 | File::S_IFTYPE, $file->mode()); $file->chmod(0777); $this->assertEquals(0777 | File::S_IFTYPE, $file->mode()); }
/** * Change the permissions of a file * * @param string $fileName The full path of the file whose permissions will change * @param integer $permissions The new permissions, e.g. 0644 (remember the leading zero in octal numbers!) * * @return boolean True on success */ public function chmod($fileName, $permissions) { $ret = $this->fileAdapter->chmod($fileName, $permissions); if (!$ret && is_object($this->abstractionAdapter)) { return $this->abstractionAdapter->chmod($fileName, $permissions); } return $ret; }
/** * Saves the source of this template. * * @param string $source */ public function setSource($source) { $path = $this->getPath(); // create dir $folder = dirname($path); if (!file_exists($folder) && !FileUtil::getSafeMode()) { mkdir($folder, 0777); } // set source require_once WCF_DIR . 'lib/system/io/File.class.php'; $file = new File($path); $file->write($source); $file->close(); @$file->chmod(0777); }
/** * "Ignites" the post installation process. * * @return void */ public static function ignite() { $autoloadPath = 'realpath(\'vendor\') . \'/autoload.php\''; $configPath = APPPATH . 'config'; $templatePath = __DIR__ . '/../Templates'; // Gets data from application/config/config.php $config = new Config('config', $configPath); $config->set('composer_autoload', 138, $autoloadPath, 'string', true); $config->set('index_page', 37, '', 'string'); $config->set('encryption_key', 316, md5('rougin'), 'string'); $config->save(); // Gets data from application/config/autoload.php $autoload = new Config('autoload', $configPath); // Gets the currently included drivers. $drivers = $autoload->get('drivers', 81, 'array'); // Includes "session" driver. if (!in_array('session', $drivers)) { array_push($drivers, 'session'); } // Gets the currently included helpers $defaultHelpers = ['form', 'url']; $helpers = $autoload->get('helper', 91, 'array'); foreach ($defaultHelpers as $helper) { if (!in_array($helper, $helpers)) { array_push($helpers, $helper); } } $autoload->set('drivers', 81, $drivers, 'array'); $autoload->set('helper', 91, $helpers, 'array'); $autoload->save(); $templates = [['file' => '.htaccess', 'name' => 'Htaccess'], ['file' => APPPATH . 'config/pagination.php', 'name' => 'Pagination']]; foreach ($templates as $template) { $file = new File($template['file']); $path = $templatePath . '/' . $template['name'] . '.tpl'; if ($template['file'] == '.htaccess') { $file->chmod(0777); } $file->putContents(file_get_contents($path)); $file->close(); } }
/** * Checks that directories are writeable * * Checks the 'plugin' prefix class variable * to ensure that the plugin path, and all * subpaths, are writeable by the system. * * Also ensures that templates are available for * all actions, plugins, etc * * @param CakeAdmin $admin * @return mixed boolean true if successful, string error message otherwise * @todo test me */ function checkBuild($admin) { $this->handler(); $this->handler->cd(APP); $contents = $this->handler->read(); if (!in_array('plugins', $contents[0])) { return __("Missing Plugin directory", true); } $this->handler->cd(APP . 'plugins'); $contents = $this->handler->read(); $path = APP . 'plugins' . DS . $admin->plugin; // Recover if the required plugin directory is missing if (!in_array($admin->plugin, $contents[0])) { $this->handler->create($path); } $contents = $this->handler->read(); if (!in_array($admin->plugin, $contents[0])) { return sprintf(__("Unable to create path: %s", true), $path); } // Check all the required MVC directories $required = array('controllers', 'models', 'views'); $this->handler->cd($path); $content = $this->handler->read(); foreach ($required as $directory) { if (!in_array($directory, $content[0])) { $this->handler->create($path . DS . $directory); } $content = $this->handler->read(); if (!in_array($directory, $content[0])) { return sprintf(__('Missing directory: %s', true), $directory); } } // Check that the directories and files are writeable by shell foreach ($required as $directory) { if (!$this->handler->chmod($path . DS . $directory)) { return sprintf(__('Directory not writeable: %s', true), $directory); } } return true; }
/** * Checks that directories are writeable * * Checks the 'plugin' prefix class variable * to ensure that the plugin path, and all * subpaths, are writeable by the system. * * Also ensures that templates are available for * all actions, plugins, etc * * @param CakeAdmin $admin * @return mixed boolean true if successful, string error message otherwise * @todo test me */ public function _checkBuild($admin) { $this->_handler(); $this->handler->cd(APP); $contents = $this->handler->read(); if (!in_array('Plugin', $contents[0])) { return __("Missing Plugin directory"); } $this->handler->cd(APP . 'Plugin'); $contents = $this->handler->read(); $path = APP . 'Plugin' . DS . Inflector::camelize($admin->plugin); // Recover if the required plugin directory is missing if (!in_array($admin->plugin, $contents[0])) { $this->handler->create($path); } $contents = $this->handler->read(); if (!in_array(Inflector::camelize($admin->plugin), $contents[0])) { return __("Unable to create path: %s", $path); } // Check all the required MVC directories $required = array('Controller', 'Model', 'View'); $this->handler->cd($path); $content = $this->handler->read(); foreach ($required as $directory) { if (!in_array($directory, $content[0])) { $this->handler->create($path . DS . $directory); } $content = $this->handler->read(); if (!in_array($directory, $content[0])) { return __('Missing directory: %s', $directory); } } // Check that the directories and files are writeable by shell foreach ($required as $directory) { if (!$this->handler->chmod($path . DS . $directory)) { return __('Directory not writeable: %s', $directory); } } return true; }
/** * Rebuilds cached options * * @param string filename * @param integer $packageID */ public static function rebuildFile($filename, $packageID = PACKAGE_ID) { $buffer = ''; // file header $buffer .= "<?php\n/**\n* generated at " . gmdate('r') . "\n*/\n"; // get all options $options = self::getOptions($packageID); foreach ($options as $optionName => $option) { $buffer .= "define('" . $optionName . "', " . ($option['optionType'] == 'boolean' || $option['optionType'] == 'integer' ? intval($option['optionValue']) : "'" . addcslashes($option['optionValue'], "'\\") . "'") . ");\n"; } unset($options); // file footer $buffer .= "?>"; // open file require_once WCF_DIR . 'lib/system/io/File.class.php'; $file = new File($filename); // write buffer $file->write($buffer); unset($buffer); // close file $file->close(); @$file->chmod(0777); }
/** * Extracts a specific file and writes it's content * to the file specified with $destination. * * @param mixed $index index or name of the requested file * @param string $destination * @return boolean $success */ public function extract($index, $destination) { if (!$this->read) { $this->open(); $this->readContent(); } $header = $this->getFileInfo($index); // can not extract a folder if ($header['type'] != 'file') { return false; } // seek to offset $this->file->seek($header['offset']); $targetFile = new File($destination); // read data $n = floor($header['size'] / 512); for ($i = 0; $i < $n; $i++) { $content = $this->file->read(512); $targetFile->write($content, 512); } if (($header['size'] % 512) != 0) { $content = $this->file->read(512); $targetFile->write($content, ($header['size'] % 512)); } $targetFile->close(); if (function_exists('apache_get_version') || !@$targetFile->is_writable()) { @$targetFile->chmod(0777); } else { @$targetFile->chmod(0755); } if ($header['mtime']) { @$targetFile->touch($header['mtime']); } // check filesize if (filesize($destination) != $header['size']) { throw new SystemException("Could not untar file '".$header['filename']."' to '".$destination."'. Maybe disk quota exceeded in folder '".dirname($destination)."'."); } return true; }
/** * Uncompresses a gzipped file * * @param string $gzipped * @param string $destination * @return boolean result */ public static function uncompressFile($gzipped, $destination) { if (!@is_file($gzipped)) { return false; } $sourceFile = new ZipFile($gzipped, 'rb'); $filesize = $sourceFile->getFileSize(); $targetFile = new File($destination); while (!$sourceFile->eof()) { $targetFile->write($sourceFile->read(512), 512); } $targetFile->close(); $sourceFile->close(); @$targetFile->chmod(0777); if ($filesize != filesize($destination)) { @unlink($destination); return false; } return true; }
/** * Write the languages files. * * @param array $categoryIDs * @param array $packageIDs */ protected function writeLanguageFiles($categoryIDs, $packageIDs) { // get categories $sql = "SELECT\t*\n\t\t\tFROM\twcf" . WCF_N . "_language_category\n\t\t\tWHERE \tlanguageCategoryID IN (" . implode(',', $categoryIDs) . ")"; $categories = WCF::getDB()->sendQuery($sql); while ($category = WCF::getDB()->fetchArray($categories)) { $categoryName = $category['languageCategory']; $categoryID = $category['languageCategoryID']; // loop packages foreach ($packageIDs as $packageID) { // get language items if ($packageID === 0) { // update after wcf installation $sql = "SELECT \tlanguageItem, languageItemValue, languageCustomItemValue, languageUseCustomValue\n\t\t\t\t\t\tFROM\twcf" . WCF_N . "_language_item\n\t\t\t\t\t\tWHERE\tlanguageID = " . $this->languageID . "\n\t\t\t\t\t\t\tAND languageCategoryID = " . $categoryID . "\n\t\t\t\t\t\t\tAND packageID = 0"; } else { // update after regular package installation or update or manual import $sql = "SELECT\t\tlanguageItem, languageItemValue, languageCustomItemValue, languageUseCustomValue\n\t\t\t\t\t\tFROM\t\twcf" . WCF_N . "_language_item language_item,\n\t\t\t\t\t\t\t\twcf" . WCF_N . "_package_dependency package_dependency\n\t\t\t\t\t\tWHERE \t\tlanguage_item.packageID = package_dependency.dependency\n\t\t\t\t\t\t\t\tAND languageID = " . $this->languageID . "\n\t\t\t\t\t\t\t\tAND languageCategoryID = " . $categoryID . "\n\t\t\t\t\t\t\t\tAND package_dependency.packageID = " . $packageID . "\n\t\t\t\t\t\tORDER BY \tpackage_dependency.priority ASC"; } $result = WCF::getDB()->sendQuery($sql); $items = array(); while ($row = WCF::getDB()->fetchArray($result)) { if ($row['languageUseCustomValue'] == 1) { $items[$row['languageItem']] = $row['languageCustomItemValue']; } else { $items[$row['languageItem']] = $row['languageItemValue']; } } if (count($items) > 0) { $file = new File(WCF_DIR . 'language/' . $packageID . '_' . $this->languageID . '_' . $categoryName . '.php'); @$file->chmod(0777); $file->write("<?php\n/**\n* WoltLab Community Framework\n* language: " . $this->data['languageCode'] . "\n* encoding: " . $this->data['languageEncoding'] . "\n* category: " . $categoryName . "\n* generated at " . gmdate("r") . "\n* \n* DO NOT EDIT THIS FILE\n*/\n"); foreach ($items as $languageItem => $languageItemValue) { $file->write("\$this->items[\$this->languageID]['" . $languageItem . "'] = '" . str_replace("'", "\\'", $languageItemValue) . "';\n"); // compile dynamic language variables if ($categoryName != 'wcf.global' && strpos($languageItemValue, '{') !== false) { $file->write("\$this->dynamicItems[\$this->languageID]['" . $languageItem . "'] = '" . str_replace("'", "\\'", self::getScriptingCompiler()->compileString($languageItem, $languageItemValue)) . "';\n"); } } $file->write("?>"); $file->close(); } } } }
/** * Save the attached file. * @param array * @return string */ private function saveFile(&$attach) { $filename = $attach['filename'] . '.' . $attach['ext']; $fl = new \File($filename); $fl->write($attach['data']); $fl->close(); $fl->chmod(0644); return $filename; }
/** * Applies this CHMOD to the specified file or folder. * * The script will abort if no CHMOD is set. * * @param mixed Path to the file or directory * @return boolean Returns TRUE on success or FALSE on failure. */ public function apply($path) { if (is_dir($path) == true) { $dir = new Folder(path); return $dir->chmod($this); } else { $file = new File($path); return $file->chmod($this); } }
/** * @param string * @param string * @param string * @return File */ public static function createTempFile($prefix = '', $suffix = '', $inDir = '/tmp', $deleteOnExit = true) { if (($file = tempnam($inDir, $prefix) . $suffix) === false) { throw new IOException('Failed to create temporary file ' . $file); } $file = new File($file); @$file->touch(); @$file->chmod(0664); if ($deleteOnExit) { $file->deleteOnExit(); } return $file; }