/** * Deletes all temporary files in the Scaffold cache * * @example self::clearScaffoldCache(); * @return void */ public static function clearScaffoldCache() { try { Engine_Package_Utilities::fsRmdirRecursive(APPLICATION_PATH . '/temporary/scaffold', false); } catch (Exception $e) { } }
/** * Deletes all temporary files in the Scaffold cache * * @example self::clearScaffoldCache(); * @return void */ public static function clearScaffoldCache() { foreach (glob(APPLICATION_PATH . '/temporary/scaffold/*.css') as $cached_css) { @unlink($cached_css); } try { Engine_Package_Utilities::fsRmdirRecursive(APPLICATION_PATH . '/temporary/scaffold/application/'); } catch (Exception $e) { } }
protected function _runCustomQueries() { $i = 0; // Hack to add 4.0.0rc1 -> 4.0.0rc2 during 4.0.0rc2 -> 4.0.0 $db = $this->getDb(); $data = $db->query('SELECT * FROM `engine4_core_menuitems` WHERE `name` = \'authorization_admin_main_manage\'')->fetch(); if (empty($data) || empty($data['name'])) { $contents = file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'my-upgrade-4.0.0rc1-4.0.0rc2.sql'); foreach (Engine_Package_Utilities::sqlSplit($contents) as $sqlFragment) { //try { $db->query($sqlFragment); $i++; //} catch( Exception $e ) { // return $this->_error('Query failed with error: ' . $e->getMessage()); //} } } return $i; }
protected function _runCustomQueries() { $i = 0; // Hack to add 4.0.0rc1 -> 4.0.0rc2 during 4.0.0rc2 -> 4.0.0 $db = $this->getDb(); $cols = $db->query('SHOW COLUMNS FROM `engine4_core_tasks` LIKE \'success_last\'')->fetch(); if (empty($cols) || empty($cols['Field'])) { $contents = file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'my-upgrade-4.0.0rc1-4.0.0rc2.sql'); foreach (Engine_Package_Utilities::sqlSplit($contents) as $sqlFragment) { //try { $db->query($sqlFragment); $i++; //} catch( Exception $e ) { // return $this->_error('Query failed with error: ' . $e->getMessage()); //} } } return $i; }
public function deleteAction() { $form = $this->view->form = new Core_Form_Admin_Language_Delete(); $languageList = Zend_Locale_Data::getList('en', 'language'); $territoryList = Zend_Locale_Data::getList('en', 'territory'); $localeCode = $this->_getParam('locale', null); if (empty($localeCode)) { return; } if (FALSE !== strpos($localeCode, '_')) { list($locale, $territory) = explode('_', $localeCode); } else { $locale = $localeCode; $territory = null; } $languagePack = $languageList[$locale]; if ($territory && !empty($territoryList[$territory])) { $languagePack .= " ({$territoryList[$territory]})"; } $languagePack .= " [{$localeCode}]"; $form->setDescription(sprintf($form->getDescription(), $languagePack)); if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) { $lang_dir = APPLICATION_PATH . '/application/languages/' . $localeCode; try { @Engine_Package_Utilities::fsRmdirRecursive($lang_dir, true); $this->_forward('success', 'utility', 'core', array('smoothboxClose' => 2000, 'parentRefresh' => 2000, 'format' => 'smoothbox', 'messages' => array(Zend_Registry::get('Zend_Translate')->_('Language has been deleted.')))); } catch (Exception $e) { $form->addError('Unable to delete language files. Please log in through FTP and delete the directory "/application/languages/' . $localeCode . '/ and all of the files inside.'); } } }
public function uploadAction() { $form = $this->view->form = new Core_Form_Admin_Themes_Upload(); if ($this->getRequest()->isPost() && $form->isValid($this->_getAllParams())) { if (!isset($_FILES['theme_file'])) { throw new Engine_Exception("Theme file was too large, or was not uploaded."); } if (!preg_match('/\\.tar$/', $_FILES['theme_file']['name'])) { throw new Engine_Exception("Invalid theme file format; must be a TAR file."); } // extract tar file to temporary directory $tmp_dir = tempnam(APPLICATION_PATH . '/temporary/', 'theme_import'); unlink($tmp_dir); mkdir($tmp_dir, 0777, true); $tar = new Archive_Tar($_FILES['theme_file']['tmp_name']); $tar->extract($tmp_dir); // find theme.css $dir = $tmp_dir; while (!file_exists("{$dir}/theme.css")) { $subdirs = glob("{$dir}/*", GLOB_ONLYDIR); $dir = $subdirs[0]; } // pull manifest.php data $meta = array('package' => array(), 'files' => array()); if (file_exists("{$dir}/manifest.php")) { $meta = (include "{$dir}/manifest.php"); $post = $this->_getAllParams(); // @todo meta key is deprecated and pending removal in 4.1.0; b/c removal in 4.2.0 if (isset($meta['package']['meta'])) { $meta['package'] = array_merge($meta['package']['meta'], $meta['package']); unset($meta['package']['meta']); } if (isset($post['title'])) { $meta['package']['title'] = $post['title']; $meta['package']['name'] = preg_replace('/[^a-z-0-9_]/', '', strtolower($post['title'])); } if (empty($meta['package']['name'])) { $meta['package']['name'] = basename($dir); } if (empty($meta['package']['title'])) { $meta['package']['title'] = ucwords(preg_replace('/_\\-/', ' ', basename($dir))); } if (isset($post['description'])) { $meta['package']['description'] = $post['description']; } } // move files over recursively $destination_dir = APPLICATION_PATH . '/application/themes/' . $meta['package']['name']; rename($dir, $destination_dir); chmod($destination_dir, 0777); foreach (self::rscandir($destination_dir) as $file) { chmod($file, 0777); } // re-write manifest according to POST paramters file_put_contents("{$destination_dir}/manifest.php", '<?php return ' . var_export($meta, true) . '; ?>'); // add to database table $table = Engine_Api::_()->getDbtable('themes', 'core'); $row = $table->createRow(); // @todo meta key is deprecated and pending removal in 4.1.0; b/c removal in 4.2.0 if (isset($meta['package']['meta'])) { $meta['package'] = array_merge($meta['package']['meta'], $meta['package']); unset($meta['package']['meta']); } $row->name = $meta['package']['name']; $row->title = $meta['package']['title']; $row->description = $meta['package']['description']; $row->active = $this->_getParam('enable', false); $row->save(); // delete temporary directory Engine_Package_Utilities::fsRmdirRecursive($tmp_dir, true); // clear scaffold cache Core_Model_DbTable_Themes::clearScaffoldCache(); // Increment site counter $settings = Engine_Api::_()->getApi('settings', 'core'); $settings->core_site_counter = $settings->core_site_counter + 1; // forward back to index $this->_forward('success', 'utility', 'core', array('redirect' => Zend_Controller_Front::getInstance()->getRouter()->assemble(array('action' => 'index')), 'messages' => array(Zend_Registry::get('Zend_Translate')->_('Theme file has been uploaded.')), 'parentRefresh' => 2000)); } }
public function enableAction() { $this->view->form = $form = new Install_Form_Confirm(array('title' => 'Enable Package?', 'description' => 'Are you sure you want to enable this package?', 'submitLabel' => 'Enable Package', 'cancelHref' => $this->view->url(array('action' => 'index')), 'useToken' => true)); if (!$this->getRequest()->isPost()) { return; } if (!$form->isValid($this->getRequest()->getPost())) { return; } // Do the enable $packageName = $this->_getParam('package'); $package = null; foreach ($this->_packageManager->listInstalledPackages() as $installedPackage) { if ($installedPackage->getKey() == $packageName) { $package = $installedPackage; } } // Enable/disable if ($package->hasAction('enable')) { $operation = new Engine_Package_Manager_Operation_Enable($this->_packageManager, $package); $ret = $this->_packageManager->execute($operation, 'enable'); // Try to flush the scaffold cache try { Engine_Package_Utilities::fsRmdirRecursive(APPLICATION_PATH . '/temporary/scaffold', false); } catch (Exception $e) { } // Try to increment the site counter try { $db = Zend_Registry::get('Zend_Db'); $db->update('engine4_core_settings', array('value' => new Zend_Db_Expr('value + 1')), array('name = ?' => 'core.site.counter')); } catch (Exception $e) { // Silence } } return $this->_helper->redirector->gotoRoute(array('action' => 'index')); }
public function restoreAction() { // Require require_once 'PEAR.php'; require_once 'Archive/Tar.php'; // Param $backup = $this->_getParam('backup'); // Verify backup $archiveFilename = $this->_outputPath . DIRECTORY_SEPARATOR . $backup; if ('' == $backup || !is_file($archiveFilename)) { return $this->_helper->redirector->gotoRoute(array('action' => 'index')); } // Check for vfs instance if (!$this->_vfs instanceof Engine_Vfs_Adapter_Abstract) { $this->_session->return = $_SERVER['REQUEST_URI']; return $this->_helper->redirector->gotoRoute(array('controller' => 'vfs', 'action' => 'index')); } // Check for database instance if (!($db = Zend_Registry::get('Zend_Db')) instanceof Zend_Db_Adapter_Abstract) { throw new Engine_Exception('No database instance'); } // Confirm/Options $this->view->form = $form = new Install_Form_Backup_Restore(); if (!$this->getRequest()->isPost()) { return; } if (!$form->isValid($this->getRequest()->getPost())) { return; } // !!IMPORTANT!! set_time_limit(0); ignore_user_abort(true); // Errors $errors = array(); // Make temporary folder $archiveOutputPath = substr($archiveFilename, 0, strrpos($archiveFilename, '.')); if (is_dir($archiveOutputPath)) { Engine_Package_Utilities::fsRmdirRecursive($archiveOutputPath, true); } if (!mkdir($archiveOutputPath, 0777, true)) { throw new Engine_Exception(sprintf('Unable to make path %s', $archiveOutputPath)); } // Extract $archive = new Archive_Tar($archiveFilename); $archive->extract($archiveOutputPath); // Upload $path = APPLICATION_PATH; $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); foreach ($it as $file) { $fullPath = $file->getPathname(); $partialPath = ltrim(str_replace($path, '', $fullPath), '/\\'); if (is_dir($fullPath)) { try { $this->_vfs->makeDirectory($directory, true); } catch (Exception $e) { $errors[] = $e->__toString(); } } else { try { $this->_vfs->put($partialPath, $fullPath); } catch (Exception $e) { $errors[] = $e->__toString(); } } } // Database //$db = new Zend_Db_Adapter_Mysqli(); $queries = Engine_Package_Utilities::sqlSplit(file_get_contents($archiveOutputPath . '/database.sql')); foreach ($queries as $query) { try { $db->query($query); } catch (Exception $e) { $errors[] = $e->__toString(); } } var_dump($errors); die('DONE!'); }
protected function _buildPackage($manifest, $date = null) { if (null === $date) { $date = time(); } // Get manifest data if (is_string($manifest)) { $manifestData = (require $manifest); if (empty($manifestData['package'])) { throw new Exception(sprintf('Missing package data for package in path: %s', $manifestPath)); } $manifestData = $manifestData['package']; } else { if (is_array($manifest)) { $manifestData = $manifest; } else { throw new Exception('Invalid manifest data type'); } } // Override date (for now at least) $manifestData['date'] = $date; // Build package file $package = new Engine_Package_Manifest($manifestData); // Build archive $archiveFile = Engine_Package_Archive::deflate($package, $this->_outputPath); // Verify archive for integrity $extractedPath = Engine_Package_Archive::inflate($archiveFile, $this->_outputPath); $loaded = new Engine_Package_Manifest($extractedPath); // Remove verified archive Engine_Package_Utilities::fsRmdirRecursive($extractedPath, true); return $archiveFile; }
protected function _writeAuthToFile($user, $realm, $password) { // Try using normal fs op if ($this->_htpasswd(APPLICATION_PATH . '/install/config/auth.php', $user, $realm, $password)) { return true; } // Try using ftp if (!empty($this->_session->ftp) && !empty($this->_session->ftp['target'])) { try { $ftp = Engine_Package_Utilities::ftpFactory($this->_session->ftp); $rfile = $this->_session->ftp['target'] . 'install/config/auth.php'; $tmpfile = tempnam('/tmp', md5(time() . rand(0, 1000000))); //chmod($tmpfile, 0777); $ret = $ftp->get($rfile, $tmpfile, true); if ($ftp->isError($ret)) { throw new Engine_Exception($ret->getMessage()); } if (!$this->_htpasswd($tmpfile, $user, $realm, $password)) { throw new Engine_Exception('Unable to write to tmpfile'); } $ret = $ftp->put($tmpfile, $rfile, true); if ($ftp->isError($ret)) { // Try to chmod + write + unchmod $ret2 = $ftp->chmod($rfile, '0777'); if ($ftp->isError($ret2)) { throw new Engine_Exception($ret2->getMessage()); } $ret2 = $ftp->put($tmpfile, $rfile, true); if ($ftp->isError($ret2)) { throw new Engine_Exception($ret2->getMessage()); } $ret2 = $ftp->chmod($rfile, '0755'); if ($ftp->isError($ret2)) { throw new Engine_Exception($ret2->getMessage()); } } } catch (Exception $e) { throw $e; } } throw new Engine_Exception('Unable to write to auth file'); }
public function uploadAction() { $this->view->installNavigation = $this->getInstallNavigation('select'); $this->_helper->contextSwitch->initContext(); // Check method if (!$this->getRequest()->isPost()) { return; } // Check ul bit if (!$this->_getParam('ul')) { return; } // Process // Prepare $info = $_FILES['Filedata']; try { $archiveDir = $this->_packageManager->getTemporaryPath(Engine_Package_Manager::PATH_ARCHIVES); $extractDir = $this->_packageManager->getTemporaryPath(Engine_Package_Manager::PATH_PACKAGES); } catch (Exception $e) { $this->view->error = $e->getMessage(); return; } $targetFile = $archiveDir . '/' . $info['name']; // Check extension if (strtolower(substr($info['name'], -4, 4)) != '.tar') { $this->view->error = 'The file uploaded was not a TAR archive.'; return; } // Check if already exists if (file_exists($targetFile) && !@unlink($targetFile)) { $this->view->error = 'This file has already been uploaded, and the previous file could not be removed. Please try removing it manually in temporary/package/archives'; return; } // Check if already extracted $outputPath = $extractDir . DIRECTORY_SEPARATOR . substr($info['name'], 0, -4); if (file_exists($outputPath) && is_dir($outputPath)) { try { Engine_Package_Utilities::fsRmdirRecursive($outputPath, true); } catch (Exception $e) { $this->view->error = 'Extract path already exists and could not be removed. Please try removing it manually in temporary/package/packages'; return; } } // Try to move uploaded file if (!move_uploaded_file($info['tmp_name'], $targetFile)) { $this->view->error = 'Unable to move file to packages directory. Please set chmod 0777 on the temporary/package/archives directory.'; return; } $this->view->status = 1; $this->view->file = $info['name']; }
public function onInstall() { $package = $this->_operation->getPrimaryPackage(); $db = $this->getDb(); $successCount = 0; $errors = array(); $currentDbVersion = $this->_currentVersion; // Run selected scripts if (!empty($this->_selectedScripts)) { foreach ($this->_selectedScripts as $selectedScript) { $contents = file_get_contents($selectedScript['path']); foreach (Engine_Package_Utilities::sqlSplit($contents) as $sqlFragment) { try { $db->query($sqlFragment); $successCount++; } catch (Exception $e) { return $this->_error('Query failed with error: ' . $e->getMessage()); } } // Update version for this upgrade $currentDbVersion = isset($selectedScript['version2']) ? $selectedScript['version2'] : (isset($selectedScript['version']) ? $selectedScript['version'] : null); if ($currentDbVersion) { try { $count = $db->update('engine4_core_modules', array('version' => $currentDbVersion), array('name = ?' => $package->getName())); if ($count <= 0) { try { $db->insert('engine4_core_modules', array('name' => $package->getName(), 'version' => $currentDbVersion, 'title' => $package->getTitle(), 'description' => $package->getDescription(), 'enabled' => 1)); } catch (Exception $e) { } } } catch (Exception $e) { // Silence? } } } } // Run custom if (method_exists($this, '_runCustomQueries')) { try { $r = $this->_runCustomQueries(); if (is_int($r)) { $successCount += $r; } else { $successCount++; } } catch (Exception $e) { return $this->_error('Query failed with error: ' . $e->getMessage()); } } // Update version if (!$this->hasError()) { if (!$package) { $package = $this->getOperation()->getPrimaryPackage(); } if ($package) { $updateData = array('version' => $this->_targetVersion, 'title' => $package->getTitle(), 'description' => $package->getDescription()); } else { $updateData = array('version' => $this->_targetVersion); } $count = $this->getDb()->update('engine4_core_modules', $updateData, array('name = ?' => $package->getName())); if ($count <= 0) { try { $db->insert('engine4_core_modules', array('name' => $package->getName(), 'version' => $package->getVersion(), 'title' => $package->getTitle(), 'description' => $package->getDescription(), 'enabled' => 1)); } catch (Exception $e) { } } } // Log success messages $this->_message(sprintf('%1$d queries succeeded.', $successCount)); if (!$this->_currentVersion) { $this->_message(sprintf('%1$s installed.', $this->_targetVersion)); } else { if (!$this->_targetVersion) { $this->_message(sprintf('%1$s removed.', $this->_currentVersion)); } else { $this->_message(sprintf('%1$s to %2$s applied.', $this->_currentVersion, $this->_targetVersion)); } } return $this; }