public static function addLanguageForm(AbstractLegacyBackend $backend, $user = null) { if (!$user) { $user = User::getUser(); } $languages = LanguageQuery::create()->useUserLanguageQuery()->filterByUser($user)->endUse()->find()->toKeyValue('Langcode', 'Name'); if (!count($languages)) { throw new Exception('You do not have access to any languages.'); } $langcode = null; if (isset($_GET['langcode'])) { $langcode = $_GET['langcode']; } if (!array_key_exists($langcode, $languages)) { $langcode = null; } if ($langcode === null) { $langcode = array_keys($languages); $langcode = array_shift($langcode); } if (empty($_GET['action'])) { header('cache-control: no-store'); // dont store state as this may cause problem with the form below $backend->addMainContent(self::getLanguageForm($languages, $langcode)); } return $langcode; }
/** * Constructor. */ public function __construct(\Curry\App $app) { parent::__construct($app); // Override and increase max execution time if set $timeLimit = ini_get('max_execution_time'); if ($timeLimit && $timeLimit < 250) { @set_time_limit(250); } \Propel::disableInstancePooling(); \Propel::setLogger(null); // make sure all classes are included foreach (Propel::getModels() as $classes) { foreach ($classes as $clazz) { class_exists($clazz . 'Peer', true); } } }
/** * Constructor */ public function __construct(\Curry\App $app) { parent::__construct($app); $this->rootPath = $this->app['wwwPath'] . DIRECTORY_SEPARATOR; }
/** * Execute actions. */ public function preShow() { parent::preShow(); Page::setRevisionType(Page::WORKING_REVISION); }
/** * Restore database from file. * * @todo Fix $maxExecutionTime. * * @param string|resource $file * @param array|null $tables * @param float $maxExecutionTime * @param int $continueLine * @param AbstractLegacyBackend|null $backend * @return bool True on success, false otherwise. */ public static function restoreFromFile($file, $tables = null, $maxExecutionTime = 0, $continueLine = 0, AbstractLegacyBackend $backend = null) { global $CURRY_DATABASE_RESTORE; $CURRY_DATABASE_RESTORE = true; $fp = is_string($file) ? fopen($file, "r") : $file; $t = microtime(true); $total = 0; $skipped = 0; $failed = 0; $session = new \Zend\Session\Container(__CLASS__); $con = Propel::getConnection(); $con->beginTransaction(); $adapter = Propel::getDB(); if ($adapter instanceof DBMySQL) { $con->exec("SET foreign_key_checks = 0"); } // Read header $firstline = stream_get_line($fp, self::MAX_LINE_LENGTH, "\n"); $header = json_decode($firstline, true); if (is_array($header) && isset($header['header'])) { $header = $header['header']; // Check header version $version = isset($header['version']) ? (int) $header['version'] : 0; if ($version > self::VERSION) { throw new Exception('Unsupported database version. The file you are trying to restore from is from a newer version of currycms.'); } // Check page version $pageVersion = isset($header['page-version']) ? (int) $header['page-version'] : 0; if ($pageVersion > Page::VERSION) { throw new Exception('Unsupported page version. The file you are trying to restore from is from a newer version of currycms.'); } if ($backend) { $backend->addMessage("Restoring from " . $header['date']); } if ($pageVersion !== Page::VERSION) { if ($backend) { $backend->addMessage("Migrating data from version {$pageVersion} to " . Page::VERSION, AbstractBackend::MSG_WARNING); } Page::preMigrate($pageVersion); } } else { throw new Exception('Invalid header'); } // Empty tables if ($continueLine == 0) { foreach (Propel::getModels() as $classes) { foreach ($classes as $table) { try { if (is_array($tables) && !in_array($table, $tables)) { continue; } if (!method_exists($table, 'delete')) { if ($backend) { $backend->addMessage("Skipping read-only table: {$table}", AbstractBackend::MSG_WARNING); } continue; } $tableName = PropelQuery::from($table)->getTableMap()->getName(); // use basePeer to avoid foreign key emulation in Normal peer class BasePeer::doDeleteAll($tableName, $con); } catch (Exception $e) { throw new Exception('Unable to empty table ' . $table . ': ' . $e->getMessage()); } } } if ($backend) { $backend->addMessage("Cleared tables in " . round(microtime(true) - $t, 2) . "s"); } $t = microtime(true); } else { $total = $session->total; $skipped = $session->skipped; $failed = $session->failed; if ($backend) { $backend->addMessage("Continuing from line {$continueLine}."); } for ($i = 0; $i < $continueLine; ++$i) { stream_get_line($fp, self::MAX_LINE_LENGTH, "\n"); } } $currentTable = null; $buffer = array(); while (!feof($fp)) { // Read line $data = json_decode(stream_get_line($fp, self::MAX_LINE_LENGTH, "\n"), true); ++$total; if (is_array($data) && isset($data['table'])) { if (is_array($tables) && !in_array($data['table'], $tables) || !method_exists($data['table'], 'delete')) { ++$skipped; continue; } // Verify columns for new table if ($data['table'] !== $currentTable && $currentTable !== null && $backend) { $backend->addMessage('Restoring rows for table ' . $data['table']); $columns = ArrayHelper::objectsToArray(PropelQuery::from($data['table'])->getTableMap()->getColumns(), null, 'getPhpName'); $added = array_diff($columns, array_keys($data['values'])); $removed = array_diff(array_keys($data['values']), $columns); if (count($added)) { $backend->addMessage('New column(s): ' . join(', ', $added), AbstractBackend::MSG_WARNING); } if (count($removed)) { $backend->addMessage('Removed column(s): ' . join(', ', $removed), AbstractBackend::MSG_WARNING); } } // Flush buffer when changing tables if ($data['table'] !== $currentTable || count($buffer) >= self::MULTIINSERT_MAXBUFFER) { if ($currentTable !== null && count($buffer)) { Propel::doMultiInsert($currentTable, $buffer); } $currentTable = $data['table']; $buffer = array(); } // Migrate data if ($pageVersion !== Page::VERSION) { if (!Page::migrateData($data['table'], $data['values'], $pageVersion)) { continue; } } $buffer[] = $data['values']; } else { if ($backend) { $backend->addMessage('Unable to read data on line ' . $total, AbstractBackend::MSG_ERROR); } ++$failed; } // check execution time if ($maxExecutionTime && App::getInstance()->getExecutionTime() > $maxExecutionTime) { if ($currentTable !== null && count($buffer)) { Propel::doMultiInsert($currentTable, $buffer); } $session->total = $total; $session->skipped = $skipped; $session->failed = $failed; $params = array('module' => 'Curry_Backend_Database', 'view' => 'ContinueRestore', 'file' => $file, 'tables' => $tables, 'line' => $total, 'max_execution_time' => $maxExecutionTime); AbstractLegacyBackend::redirect(url('', $params)->getAbsolute("&", true)); } } // Flush buffer if ($currentTable !== null && count($buffer)) { Propel::doMultiInsert($currentTable, $buffer); } if ($pageVersion !== Page::VERSION) { Page::postMigrate($pageVersion); } if ($adapter instanceof DBMySQL) { $con->exec("SET foreign_key_checks = 1"); } $con->commit(); $CURRY_DATABASE_RESTORE = false; if ($backend) { if ($skipped) { $backend->addMessage("Skipped {$skipped} rows"); } if ($failed) { $backend->addMessage("Failed to add {$failed} rows", AbstractBackend::MSG_ERROR); } $backend->addMessage("Added " . ($total - $skipped - $failed) . " / {$total} rows in " . round(microtime(true) - $t, 2) . "s", !$failed ? AbstractBackend::MSG_SUCCESS : AbstractBackend::MSG_ERROR); } if (is_string($file)) { fclose($fp); } return !$failed; }
public function showRoles() { if (!$this->hasPermission(self::PERMISSION_ROLES)) { throw new Exception('You dont have permission to access this view.'); } $this->addMenu(); $user = User::getUser(); $backendModules = AbstractLegacyBackend::getBackendList(); $disable = array(); $backend = array("*" => "All"); if (!$user->hasAccess('*')) { $disable[] = '*'; } foreach ($backendModules as $backendClass => $backendName) { $backend[$backendClass] = $backendName; $permissions = method_exists($backendClass, 'getPermissions') ? call_user_func(array($backendClass, 'getPermissions')) : array(); foreach ($permissions as $permission) { $backend[$backendClass . "/" . $permission] = " " . $permission; if (!$user->hasAccess($backendClass . "/" . $permission)) { $disable[] = $backendClass . "/" . $permission; } } if (!$user->hasAccess($backendClass)) { $disable[] = $backendClass; } } $content = array(); $contentAccess = array("*" => "All") + AbstractModule::getModuleList(); $allContentAccess = $user->hasAccess('Curry_Backend_Content/*'); foreach ($contentAccess as $k => $v) { $content['Curry_Backend_Content/' . $k] = $v; if (!$allContentAccess && !$user->hasAccess('Curry_Backend_Content/' . $k)) { $disable[] = 'Curry_Backend_Content/' . $k; } } $form = new Curry_ModelView_Form('UserRole', array('elements' => array('backend' => array('multiselect', array('label' => 'Curry\\Controller\\Backend access', 'multiOptions' => $backend, 'size' => 10, 'order' => 1, 'disable' => $disable, 'validators' => array(array('InArray', true, array(array_diff(array_keys($backend), $disable)))))), 'content' => array('multiselect', array('label' => 'Content access', 'multiOptions' => $content, 'size' => 10, 'order' => 2, 'disable' => $disable, 'validators' => array(array('InArray', true, array(array_diff(array_keys($content), $disable))))))), 'onFillForm' => function (UserRole $role, $form) { $access = UserRoleAccessQuery::create()->filterByUserRole($role)->select('Module')->find()->getArrayCopy(); $form->backend->setValue($access); $form->content->setValue($access); }, 'onFillModel' => function (UserRole $role, $form, $values) { $access = array_merge((array) $values['backend'], (array) $values['content']); $collection = new PropelObjectCollection(); $collection->setModel('UserRoleAccess'); foreach ($access as $a) { $ura = new UserRoleAccess(); $ura->setModule($a); $collection->append($ura); } $role->setUserRoleAccesss($collection); })); $q = UserRoleQuery::create(); $list = new Curry_ModelView_List($q, array('modelForm' => $form)); $list->addAction('file_permissions', array('action' => $this->getFileAccessList(), 'class' => 'inline', 'single' => true)); $this->addMainContent($list); }