Esempio n. 1
0
 /**
  * @param string $encryptKey
  * @return ZCrypt
  */
 public static function getInstance($encryptKey = null)
 {
     if (!is_object(self::$instance)) {
         self::$instance = new ZCrypt($encryptKey);
         self::$instance->setPadding(Crypt::PADDING_ZERO);
     }
     return self::$instance;
 }
Esempio n. 2
0
 /**
  * @return bool|\Phalcon\Http\ResponseInterface
  */
 public function indexAction()
 {
     //Add tool bar
     $this->_toolbar->addBreadcrumb(['title' => 'm_system_system_manager']);
     $this->_toolbar->addBreadcrumb(['title' => 'm_system_manage_manager']);
     $this->_toolbar->addHeaderPrimary('m_system_manage_manager');
     $this->_toolbar->addHeaderSecond('m_system_system_manager');
     $error_is_writable = false;
     if (!is_writable(APP_DIR . '/backup')) {
         $this->flashSession->error('gb_backup_folder_cannot_be_writable ');
         $error_is_writable = true;
     }
     if (!is_writable(APP_DIR . '/config')) {
         $error_is_writable = true;
         $this->flashSession->error('gb_config_folder_cannot_be_writable ');
     }
     if (!$error_is_writable) {
         $this->_toolbar->addSaveButton('system|config|manage');
     }
     $core_config = CoreConfigs::find();
     $config = [];
     foreach ($core_config as $item) {
         $config[$item->key] = $item->value;
     }
     $config = (object) $config;
     $configForm = new ConfigForm($config);
     $this->view->setVar('configForm', $configForm);
     if ($this->request->isPost()) {
         if ($configForm->isValid($_POST, $config)) {
             $error = 0;
             foreach ($configForm->getElements() as $element) {
                 $name = $element->getName();
                 /**
                  * @var $coreConfig CoreConfigs
                  */
                 $coreConfig = CoreConfigs::findFirst(['conditions' => 'key = ?0', 'bind' => [$name]]);
                 if ($coreConfig && $config->{$name} != '') {
                     if ($coreConfig->scope == 'database') {
                         continue;
                     }
                     //Crypt value
                     if ($coreConfig->is_crypt_value) {
                         $coreConfig->value = ZCrypt::getInstance()->encrypt($config->{$name});
                     } else {
                         $coreConfig->value = $config->{$name};
                     }
                     if ($coreConfig->save()) {
                         if ($name == 'defaultTemplate') {
                             $coreTemplate = new CoreTemplates();
                             $coreTemplate->setDefaultTemplate($config->{$name}, 'backend');
                         }
                         if ($name == 'defaultTemplate') {
                             $coreTemplate = new CoreTemplates();
                             $coreTemplate->setDefaultTemplate($config->{$name}, 'frontend');
                         }
                         if ($name == 'language') {
                             $coreLanguage = new CoreLanguages();
                             $coreLanguage->setDefaultLanguage($config->{$name});
                         }
                     } else {
                         $error++;
                         $this->setFlashSession($coreConfig->getMessages(), 'warning');
                     }
                 }
             }
             if ($error != 0) {
                 $this->flashSession->warning('m_system_config_message_some_config_cannot_save');
             } else {
                 //$defaultLanguage = CoreLanguage::findFirst('is_default = 1');
                 $this->flashSession->success('m_system_config_message_config_save_successfully');
                 $configFileContent = file_get_contents(APP_DIR . '/config/config.php');
                 if (file_put_contents(APP_DIR . '/backup/config.backup_' . date('Y-m-d') . '_at_' . date('H-i-s') . '.php', $configFileContent)) {
                     $newConfig = [];
                     $sections = $this->getSectionConfig();
                     foreach ($sections as $section) {
                         $itemsInSection = $this->getItemInSectionConfig($section);
                         if ($section == 'system') {
                             foreach ($itemsInSection as $item) {
                                 if (is_numeric($item->value)) {
                                     $item->value = (int) $item->value;
                                 }
                                 $newConfig[$item->key] = $item->value;
                             }
                         } else {
                             foreach ($itemsInSection as $item) {
                                 if (is_numeric($item->value)) {
                                     $item->value = (int) $item->value;
                                 }
                                 $newConfig[$section][$item->key] = $item->value;
                                 if ($section == 'website') {
                                     /**
                                      * @var CoreLanguages $defaultLanguage
                                      */
                                     $defaultLanguage = CoreLanguages::findFirst('is_default = 1');
                                     /**
                                      * @var CoreConfigs $directionConfig
                                      */
                                     $directionConfig = CoreConfigs::findFirst("scope = 'website' AND key = 'direction'");
                                     $newConfig['website']['language'] = $defaultLanguage->language_code;
                                     $newConfig['website']['metakey'] = htmlspecialchars_decode($defaultLanguage->metaKey);
                                     $newConfig['website']['metadesc'] = htmlspecialchars_decode($defaultLanguage->metaDesc);
                                     $newConfig['website']['sitename'] = htmlspecialchars_decode($defaultLanguage->siteName);
                                     $newConfig['website']['direction'] = $directionConfig->key;
                                     $newConfig['website']['country_image'] = $defaultLanguage->image;
                                 }
                             }
                         }
                     }
                     /**
                      * Todo
                      * Add new on feature ZCMS
                      */
                     $newConfig['apc_cache'] = ['apc_prefix' => 'cache', 'apc_lifetime' => 1800, 'apc_status' => 1];
                     $var_str = var_export($newConfig, true);
                     $newConfigFileContent = "<?php\n\n return {$var_str};\n";
                     if (!file_put_contents(APP_DIR . '/config/config.php', $newConfigFileContent)) {
                         $this->flashSession->warning('gb_config_folder_cannot_be_writable');
                         return false;
                     }
                 } else {
                 }
             }
         } else {
             $this->flashSession->warning('m_system_config_message_please_check_error_field');
             $this->setFlashSession($configForm->getMessages(), 'warning');
             return false;
         }
         $this->view->disable();
         return $this->response->redirect('/admin/system/config');
     }
     return true;
 }
Esempio n. 3
0
 /**
  * Init Services
  *
  * @param mixed $config
  * @param \Phalcon\DiInterface $di
  */
 public function _initServices($di, $config)
 {
     /**
      * The URL component is used to generate all kind of urls in the application
      */
     $di->set('url', function () use($config) {
         $url = new UrlResolver();
         $url->setBaseUri($config->website->baseUri);
         return $url;
     }, true);
     /**
      * Start the session the first time some component request the session service
      */
     $di->set('session', function () use($config) {
         $session = new ZSession(['uniqueId' => $config->auth->salt]);
         $session->start();
         return $session;
     }, true);
     /**
      * Set view cache
      */
     $di->set('viewCache', function () use($config) {
         //Cache data for one day by default
         $frontCache = new FrontendOutput(['lifetime' => $config->viewCache->lifetime]);
         //File backend settings
         $cache = new CacheFile($frontCache, ['cacheDir' => ROOT_PATH . $config->viewCache->dir]);
         return $cache;
     });
     if ($config->modelMetadataCache->status) {
         /**
          * Set models metadata
          */
         $di->set('modelsMetadata', function () use($config) {
             if ($config->modelMetadataCache->type == 'apc') {
                 return new MetaDataApc(['lifetime' => $config->modelMetadataCache->lifetime, 'prefix' => $config->modelMetadataCache->prefix]);
             } else {
                 return new MetadataFiles(['metaDataDir' => ROOT_PATH . '/cache/metadata/', 'lifetime' => $config->modelMetadataCache->lifetime]);
             }
         });
     }
     /**
      * Crypt service
      */
     $di->set('crypt', function () use($config) {
         $crypt = ZCrypt::getInstance();
         $crypt->setKey($config->crypt->key);
         return $crypt;
     });
     /**
      * Set security
      */
     $di->set('security', function () {
         $security = new Security();
         $security->setWorkFactor(8);
         return $security;
     });
     /**
      * Set up database connection
      */
     $di->set('db', function () use($config) {
         $adapter = 'Phalcon\\Db\\Adapter\\Pdo\\' . $config->database->adapter;
         /**
          * @var \Phalcon\Db\Adapter\Pdo\Postgresql $db
          */
         if ($config->database->adapter == 'Mysql') {
             $db = new $adapter($config->database->toArray());
         } else {
             $db = new $adapter(array('host' => $config->database->host, 'username' => $config->database->username, 'password' => $config->database->password, 'dbname' => $config->database->dbname));
         }
         if ($config->database->log) {
             $eventsManager = new EventsManager();
             if (!file_exists(ROOT_PATH . '/cache/logs/db.log')) {
                 file_put_contents(ROOT_PATH . '/cache/logs/db.log', '');
             }
             $logger = new FileLogger(ROOT_PATH . '/cache/logs/db.log');
             //Listen all the database events
             $eventsManager->attach('db', function ($event, $db) use($logger) {
                 /**
                  * @var \Phalcon\Events\Event $event
                  */
                 if ($event->getType() == 'beforeQuery') {
                     /**
                      * @var \Phalcon\Db\Adapter\Pdo\Postgresql $db
                      */
                     $logger->log($db->getSQLStatement(), Logger::INFO);
                 }
             });
             //Assign the eventsManager to the db adapter instance
             $db->setEventsManager($eventsManager);
         }
         return $db;
     });
     /**
      * Set a models manager
      */
     $di->set('modelsManager', new ModelsManager());
     /**
      * Set up model cache for Phalcon model
      */
     $di->set('modelsCache', function () {
         return ZCache::getInstance('_ZCMS_MODEL');
     });
     /**
      * Set up asset add css, js
      */
     $di->set('assets', new ZAssets());
     /**
      * Loading routes from the routes.php file
      */
     $di->set('router', function () {
         return require APP_DIR . '/config/router.php';
     });
     $di->set('acl', ZAcl::getInstance());
     /**
      * Set up the flash service (custom with bootstrap)
      */
     $di->set('flashSession', function () {
         $flashSession = new FlashSession(['warning' => 'alert alert-warning', 'notice' => 'alert alert-info', 'success' => 'alert alert-success', 'error' => 'alert alert-danger']);
         return $flashSession;
     });
     /**
      * Set up cache
      */
     $di->set('cache', ZCache::getInstance('_ZCMS_GLOBAL'));
 }