/** * doExecute * * @return mixed */ protected function doExecute() { $xml = $this->config['dest'] . '/{{extension.name.lower}}.xml'; $content = file_get_contents($xml); $content = str_replace('client="site"', '{{module.client}}', $content); File::write($content, $xml); }
/** * copyFile * * @param string $src * @param string $dest * @param array $replace * * @return void */ protected function copyFile($src, $dest, $replace = array()) { // Replace dest file name. $dest = strtr($dest, $replace); if (is_file($dest)) { $this->io->out('File exists: ' . $dest); } else { $content = strtr(file_get_contents($src), $replace); if (File::write($dest, $content)) { $this->io->out('File created: ' . $dest); } } }
/** * Test move method * * @return void * * @covers Joomla\Filesystem\File::move * @since 1.0 */ public function testMove() { $name = 'tempFile'; $path = __DIR__; $movedFileName = 'movedTempFile'; $data = 'Lorem ipsum dolor sit amet'; // Create a temp file to test copy operation $this->object->write($path . '/' . $name, $data); $this->assertThat(File::move($path . '/' . $name, $path . '/' . $movedFileName), $this->isTrue(), 'Line:' . __LINE__ . ' File should be moved successfully.'); $this->assertThat(File::move($movedFileName, $name, $path), $this->isTrue(), 'Line:' . __LINE__ . ' File should be moved successfully.'); // Using streams. $this->assertThat(File::move($name, $movedFileName, $path, true), $this->isTrue(), 'Line:' . __LINE__ . ' File should be moved successfully.'); File::delete($path . '/' . $movedFileName); }
/** * Extract a Bzip2 compressed file to a given path * * @param string $archive Path to Bzip2 archive to extract * @param string $destination Path to extract archive to * * @return boolean True if successful * * @since 1.0 * @throws \RuntimeException */ public function extract($archive, $destination) { $this->data = null; if (!isset($this->options['use_streams']) || $this->options['use_streams'] == false) { // Old style: read the whole file and then parse it $this->data = file_get_contents($archive); if (!$this->data) { throw new \RuntimeException('Unable to read archive'); } $buffer = bzdecompress($this->data); unset($this->data); if (empty($buffer)) { throw new \RuntimeException('Unable to decompress data'); } if (File::write($destination, $buffer) === false) { throw new \RuntimeException('Unable to write archive'); } } else { // New style! streams! $input = Stream::getStream(); // Use bzip $input->set('processingmethod', 'bz'); if (!$input->open($archive)) { throw new \RuntimeException('Unable to read archive (bz2)'); } $output = Stream::getStream(); if (!$output->open($destination, 'w')) { $input->close(); throw new \RuntimeException('Unable to write archive (bz2)'); } do { $this->data = $input->read($input->get('chunksize', 8196)); if ($this->data) { if (!$output->write($this->data)) { $input->close(); throw new \RuntimeException('Unable to write archive (bz2)'); } } } while ($this->data); $output->close(); $input->close(); } return true; }
/** * Extract a Gzip compressed file to a given path * * @param string $archive Path to ZIP archive to extract * @param string $destination Path to extract archive to * * @return boolean True if successful * * @since 1.0 * @throws \RuntimeException */ public function extract($archive, $destination) { $this->data = null; if (!isset($this->options['use_streams']) || $this->options['use_streams'] == false) { $this->data = file_get_contents($archive); if (!$this->data) { throw new \RuntimeException('Unable to read archive'); } $position = $this->getFilePosition(); $buffer = gzinflate(substr($this->data, $position, strlen($this->data) - $position)); if (empty($buffer)) { throw new \RuntimeException('Unable to decompress data'); } if (File::write($destination, $buffer) === false) { throw new \RuntimeException('Unable to write archive'); } } else { // New style! streams! $input = Stream::getStream(); // Use gz $input->set('processingmethod', 'gz'); if (!$input->open($archive)) { throw new \RuntimeException('Unable to read archive (gz)'); } $output = Stream::getStream(); if (!$output->open($destination, 'w')) { $input->close(); throw new \RuntimeException('Unable to write archive (gz)'); } do { $this->data = $input->read($input->get('chunksize', 8196)); if ($this->data) { if (!$output->write($this->data)) { $input->close(); throw new \RuntimeException('Unable to write file (gz)'); } } } while ($this->data); $output->close(); $input->close(); } return true; }
/** * Apply the patches * * @throw RuntimeException * * @return integer the number of files patched */ public function apply() { foreach ($this->patches as $patch) { // Separate the input into lines $lines = self::splitLines($patch['udiff']); // Loop for each header while (self::findHeader($lines, $src, $dst)) { $done = false; if ($patch['strip'] === null) { $src = $patch['root'] . preg_replace('#^([^/]*/)*#', '', $src); $dst = $patch['root'] . preg_replace('#^([^/]*/)*#', '', $dst); } else { $src = $patch['root'] . preg_replace('#^([^/]*/){' . (int) $patch['strip'] . '}#', '', $src); $dst = $patch['root'] . preg_replace('#^([^/]*/){' . (int) $patch['strip'] . '}#', '', $dst); } // Loop for each hunk of differences while (self::findHunk($lines, $src_line, $src_size, $dst_line, $dst_size)) { $done = true; // Apply the hunk of differences $this->applyHunk($lines, $src, $dst, $src_line, $src_size, $dst_line, $dst_size); } // If no modifications were found, throw an exception if (!$done) { throw new RuntimeException('Invalid Diff'); } } } // Initialize the counter $done = 0; // Patch each destination file foreach ($this->destinations as $file => $content) { if (File::write($file, implode("\n", $content))) { if (isset($this->sources[$file])) { $this->sources[$file] = $content; } $done++; } } // Remove each removed file foreach ($this->removals as $file) { if (File::delete($file)) { if (isset($this->sources[$file])) { unset($this->sources[$file]); } $done++; } } // Clear the destinations cache $this->destinations = array(); // Clear the removals $this->removals = array(); // Clear the patches $this->patches = array(); return $done; }
/** * Save config to file. * * @return void */ public static function saveConfig() { File::write(static::getPath(), static::getConfig()->toString(static::$type)); }
/** * Extract a Gzip compressed file to a given path * * @param string $archive Path to ZIP archive to extract * @param string $destination Path to extract archive to * @param array $options Extraction options [unused] * * @return boolean True if successful * * @since 11.1 * @throws RuntimeException */ public function extract($archive, $destination, array $options = array()) { $this->_data = null; if (!extension_loaded('zlib')) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'The zlib extension is not available.'); } else { throw new RuntimeException('The zlib extension is not available.'); } } if (!isset($options['use_streams']) || $options['use_streams'] == false) { $this->_data = file_get_contents($archive); if (!$this->_data) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to read archive'); } else { throw new RuntimeException('Unable to read archive'); } } $position = $this->_getFilePosition(); $buffer = gzinflate(substr($this->_data, $position, strlen($this->_data) - $position)); if (empty($buffer)) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to decompress data'); } else { throw new RuntimeException('Unable to decompress data'); } } if (File::write($destination, $buffer) === false) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to write archive'); } else { throw new RuntimeException('Unable to write archive'); } } } else { // New style! streams! $input = Factory::getStream(); // Use gz $input->set('processingmethod', 'gz'); if (!$input->open($archive)) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to read archive (gz)'); } else { throw new RuntimeException('Unable to read archive (gz)'); } } $output = Factory::getStream(); if (!$output->open($destination, 'w')) { $input->close(); if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to write archive (gz)'); } else { throw new RuntimeException('Unable to write archive (gz)'); } } do { $this->_data = $input->read($input->get('chunksize', 8196)); if ($this->_data) { if (!$output->write($this->_data)) { $input->close(); if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to write file (gz)'); } else { throw new RuntimeException('Unable to write file (gz)'); } } } } while ($this->_data); $output->close(); $input->close(); } return true; }
/** * Run Install Process * * @param array $postData Input data * * @return boolean * * @since 1.0 */ public function install(array $postData) { // Validation post data $check = array_filter(array_values($postData)); if (empty($postData['database_password'])) { $check[] = ''; } if (empty($check) || count($check) < count($postData)) { $this->setError(Text::_('INSTL_CHECK_REQUIRED_FIELDS')); return false; } if ($this->canUpload()) { if (!$this->uploadLogo()) { return false; } } $config = array('sitename' => $postData['site_name'], 'host' => $postData['database_host'], 'user' => $postData['database_user'], 'password' => $postData['database_password'], 'db' => $postData['database_name'], 'dbprefix' => $postData['database_prefix'], 'dbtype' => strtolower($postData['db_drive']), 'mailfrom' => $postData['email'], 'fromname' => $postData['first_name'] . ' ' . $postData['last_name'], 'sendmail' => '/usr/sbin/sendmail', 'log_path' => JPATH_ROOT . '/logs', 'tmp_path' => JPATH_ROOT . '/tmp', 'offset' => 'UTC', 'error_reporting' => 'maximum', 'debug' => '1', 'secret' => $this->genRandomPassword(16), 'sef' => '1', 'sef_rewrite' => '1', 'sef_suffix' => '1', 'unicodeslugs' => '0', 'language' => 'en-GB'); $this->config = new Registry($config); $file = JPATH_CONFIGURATION . '/configuration.php'; $content = $this->config->toString('php', array('class' => 'JConfig')); if (!is_writable($file) || !is_writable(JPATH_CONFIGURATION) || !JFile::write($file, $content)) { $this->setError(Text::_('INSTL_NOTICEYOUCANSTILLINSTALL')); return false; } // Populate database if (!$this->createDb()) { $this->setError(Text::_('INSTL_ERROR_IMPORT_DATABASE')); return false; } // Populate crm if (!$this->createCrm()) { $this->setError(Text::_('INSTL_ERROR_IMPORT_DATABASE')); return false; } //create admin user $admin = array('username' => $postData['username'], 'password' => $postData['password'], 'email' => $postData['email'], 'first_name' => $postData['first_name'], 'last_name' => $postData['last_name']); if (!$this->createAdmin($admin)) { $this->setError(Text::_('INSTL_ERROR_CREATE_USER')); return false; } return true; }
/** * Extract a ZIP compressed file to a given path * * @param string $archive Path to ZIP archive to extract * @param string $destination Path to extract archive into * @param array $options Extraction options [unused] * * @return boolean True if successful * * @throws RuntimeException * @since 11.1 */ public function extract($archive, $destination, array $options = array()) { $this->_data = null; $this->_metadata = null; $this->_data = file_get_contents($archive); if (!$this->_data) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to read archive'); } else { throw new RuntimeException('Unable to read archive'); } } $this->_getTarInfo($this->_data); for ($i = 0, $n = count($this->_metadata); $i < $n; $i++) { $type = strtolower($this->_metadata[$i]['type']); if ($type == 'file' || $type == 'unix file') { $buffer = $this->_metadata[$i]['data']; $path = Path::clean($destination . '/' . $this->_metadata[$i]['name']); // Make sure the destination folder exists if (!Folder::create(dirname($path))) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to create destination'); } else { throw new RuntimeException('Unable to create destination'); } } if (File::write($path, $buffer) === false) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to write entry'); } else { throw new RuntimeException('Unable to write entry'); } } } } return true; }
/** * Test write method * * @return void * * @covers Joomla\Filesystem\File::write * @since 1.0 */ public function testWrite() { $name = 'tempFile'; $path = __DIR__; $data = 'Lorem ipsum dolor sit amet'; // Create a file on pre existing path. $this->assertThat(File::write($path . '/' . $name, $data), $this->isTrue(), 'Line:' . __LINE__ . ' File should be written successfully.'); // Create a file on pre existing path by using streams. $this->assertThat(File::write($path . '/' . $name, $data, true), $this->isTrue(), 'Line:' . __LINE__ . ' File should be written successfully.'); // Create a file on non-existing path. $this->assertThat(File::write($path . '/TempFolder/' . $name, $data), $this->isTrue(), 'Line:' . __LINE__ . ' File should be written successfully.'); // Removes file and folder. File::delete($path . '/' . $name); Folder::delete($path . '/TempFolder'); }
/** * Extract a ZIP compressed file to a given path using native php api calls for speed * * @param string $archive Path to ZIP archive to extract * @param string $destination Path to extract archive into * * @return boolean True on success * * @since 11.1 * @throws RuntimeException */ protected function extractNative($archive, $destination) { $zip = zip_open($archive); if (is_resource($zip)) { // Make sure the destination folder exists if (!Folder::create($destination)) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to create destination'); } else { throw new RuntimeException('Unable to create destination'); } } // Read files in the archive while ($file = @zip_read($zip)) { if (zip_entry_open($zip, $file, "r")) { if (substr(zip_entry_name($file), strlen(zip_entry_name($file)) - 1) != "/") { $buffer = zip_entry_read($file, zip_entry_filesize($file)); if (File::write($destination . '/' . zip_entry_name($file), $buffer) === false) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to write entry'); } else { throw new RuntimeException('Unable to write entry'); } } zip_entry_close($file); } } else { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to read entry'); } else { throw new RuntimeException('Unable to read entry'); } } } @zip_close($zip); } else { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to open archive'); } else { throw new RuntimeException('Unable to open archive'); } } return true; }
/** * Method to determine if script owns the path. * * @param string $path Path to check ownership. * * @return boolean True if the php script owns the path passed. * * @since 11.1 */ public static function isOwner($path) { jimport('joomla.filesystem.file'); $tmp = md5(mt_rand()); $ssp = ini_get('session.save_path'); $jtp = JPATH_SITE . '/tmp'; // Try to find a writable directory $dir = is_writable('/tmp') ? '/tmp' : false; $dir = !$dir && is_writable($ssp) ? $ssp : false; $dir = !$dir && is_writable($jtp) ? $jtp : false; if ($dir) { $test = $dir . '/' . $tmp; // Create the test file $blank = ''; File::write($test, $blank, false); // Test ownership $return = fileowner($test) == fileowner($path); // Delete the test file File::delete($test); return $return; } return false; }
/** * Extract a ZIP compressed file to a given path * * @param string $archive Path to ZIP archive to extract * @param string $destination Path to extract archive into * * @return boolean True if successful * * @since 1.0 * @throws \RuntimeException */ public function extract($archive, $destination) { $this->data = null; $this->metadata = null; $this->data = file_get_contents($archive); if (!$this->data) { throw new \RuntimeException('Unable to read archive'); } $this->getTarInfo($this->data); for ($i = 0, $n = count($this->metadata); $i < $n; $i++) { $type = strtolower($this->metadata[$i]['type']); if ($type == 'file' || $type == 'unix file') { $buffer = $this->metadata[$i]['data']; $path = Path::clean($destination . '/' . $this->metadata[$i]['name']); // Make sure the destination folder exists if (!Folder::create(dirname($path))) { throw new \RuntimeException('Unable to create destination'); } if (File::write($path, $buffer) === false) { throw new \RuntimeException('Unable to write entry'); } } } return true; }
/** * Extract a Bzip2 compressed file to a given path * * @param string $archive Path to Bzip2 archive to extract * @param string $destination Path to extract archive to * @param array $options Extraction options [unused] * * @return boolean True if successful * * @since 11.1 * @throws RuntimeException */ public function extract($archive, $destination, array $options = array()) { $this->_data = null; if (!extension_loaded('bz2')) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'The bz2 extension is not available.'); } else { throw new RuntimeException('The bz2 extension is not available.'); } } if (!isset($options['use_streams']) || $options['use_streams'] == false) { // Old style: read the whole file and then parse it $this->_data = file_get_contents($archive); if (!$this->_data) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to read archive'); } else { throw new RuntimeException('Unable to read archive'); } } $buffer = bzdecompress($this->_data); unset($this->_data); if (empty($buffer)) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to decompress data'); } else { throw new RuntimeException('Unable to decompress data'); } } if (File::write($destination, $buffer) === false) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to write archive'); } else { throw new RuntimeException('Unable to write archive'); } } } else { // New style! streams! $input = Factory::getStream(); // Use bzip $input->set('processingmethod', 'bz'); if (!$input->open($archive)) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to read archive (bz2)'); } else { throw new RuntimeException('Unable to read archive (bz2)'); } } $output = Factory::getStream(); if (!$output->open($destination, 'w')) { $input->close(); if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to write archive (bz2)'); } else { throw new RuntimeException('Unable to write archive (bz2)'); } } do { $this->_data = $input->read($input->get('chunksize', 8196)); if ($this->_data) { if (!$output->write($this->_data)) { $input->close(); if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to write archive (bz2)'); } else { throw new RuntimeException('Unable to write archive (bz2)'); } } } } while ($this->_data); $output->close(); $input->close(); } return true; }
/** * Method to store a record * * @return boolean True on success */ public function store($data = null) { if ($data) { $data = (array) $data; $_FILES = array(); $_FILES['document'] = $data; $_FILES['tmp_name'] = $data['attachment']; $fileName = $data['value']; $fileTemp = $data['attachment']; $association_id = $data['association_id']; $association_type = $data['association_type']; $uploadedFileExtension = substr(strrchr($fileName, '.'), 1); $data['is_attachment'] = 1; $data['email'] = 1; } else { $association_id = $_POST['association_id']; $association_type = $_POST['association_type']; //this is the name of the field in the html form, filedata is the default name for swfupload //so we will leave it as that $fieldName = 'document'; //any errors the server registered on uploading $fileError = $_FILES[$fieldName]['error']; if ($fileError > 0) { switch ($fileError) { case 1: echo TextHelper::_('FILE TO LARGE THAN PHP INI ALLOWS'); return; case 2: echo TextHelper::_('FILE TO LARGE THAN HTML FORM ALLOWS'); return; case 3: echo TextHelper::_('ERROR PARTIAL UPLOAD'); return; case 4: echo TextHelper::_('ERROR NO FILE'); return; } } //check the file extension is ok $fileName = $_FILES[$fieldName]['name']; $fileTemp = $_FILES[$fieldName]['tmp_name']; } $uploadedFileNameParts = explode('.', $fileName); $uploadedFileExtension = array_pop($uploadedFileNameParts); $validFileExts = explode(',', 'jpeg,jpg,png,gif,pdf,doc,docx,odt,rtf,ppt,xls,txt'); //assume the extension is false until we know its ok $extOk = false; //go through every ok extension, if the ok extension matches the file extension (case insensitive) //then the file extension is ok foreach ($validFileExts as $key => $value) { if (preg_match("/{$value}/i", $uploadedFileExtension)) { $extOk = true; } } if ($extOk == false) { echo TextHelper::_('INVALID EXTENSION'); return; } //data generation $date = DateHelper::formatDBDate(date('Y-m-d H:i:s')); $hashFilename = md5($fileName . $date) . "." . $uploadedFileExtension; //lose any special characters in the filename $fileName = preg_replace("[^A-Za-z0-9.]", "-", $fileName); //always use constants when making file paths, to avoid the possibilty of remote file inclusion $uploadPath = JPATH_SITE . '//documents/' . $hashFilename; if ($data['is_attachment']) { if (!File::write($uploadPath, $fileTemp)) { echo TextHelper::_('ERROR MOVING FILE'); return; } } else { if (!File::upload($fileTemp, $uploadPath)) { echo TextHelper::_('ERROR MOVING FILE'); return; } } $fileSize = filesize($uploadPath); //update the database $newData = array('name' => $fileName, 'filename' => $hashFilename, 'association_id' => $association_id, 'association_type' => $association_type, 'filetype' => $uploadedFileExtension, 'size' => $fileSize / 1024, 'created' => $date); if (array_key_exists('email', $data) && $data['email']) { $newData['email'] = 1; } //Load Tables $row = new DocumentTable(); $oldRow = new DocumentTable(); //date generation $date = DateHelper::formatDBDate(date('Y-m-d H:i:s')); if (!array_key_exists('id', $newData)) { $newData['created'] = $date; $status = "created"; } else { $row->load($data['id']); $oldRow->load($data['id']); $status = "updated"; } $is_image = is_array(getimagesize($uploadPath)) ? true : false; $newData['modified'] = $date; $newData['owner_id'] = UsersHelper::getUserId(); $newData['is_image'] = $is_image; // Bind the form fields to the table if (!$row->bind($newData)) { $this->setError($this->db->getErrorMsg()); return false; } $app = \Cobalt\Container::fetch('app'); //$app->triggerEvent('onBeforeDocumentSave', array(&$row)); // Make sure the record is valid if (!$row->check()) { $this->setError($this->db->getErrorMsg()); return false; } // Store the web link table to the database if (!$row->store()) { $this->setError($this->db->getErrorMsg()); return false; } $id = array_key_exists('id', $data) ? $data['id'] : $this->db->insertId(); ActivityHelper::saveActivity($oldRow, $row, 'document', $status); //$app->triggerEvent('onAfterDocumentSave', array(&$row)); return $id; }
/** * Creates the ZIP file. * * Official ZIP file format: http://www.pkware.com/appnote.txt * * @param array &$contents An array of existing zipped files. * @param array &$ctrlDir An array of central directory information. * @param string $path The path to store the archive. * * @return boolean True if successful * * @since 1.0 * @todo Review and finish implementation */ private function createZIPFile(array &$contents, array &$ctrlDir, $path) { $data = implode('', $contents); $dir = implode('', $ctrlDir); $buffer = $data . $dir . $this->ctrlDirEnd . pack('v', count($ctrlDir)) . pack('v', count($ctrlDir)) . pack('V', strlen($dir)) . pack('V', strlen($data)) . ""; if (File::write($path, $buffer) === false) { return false; } else { return true; } }