Example #1
0
 /**
  * read all metadata from theme file.
  * @param type $theme_system_name
  * @return type
  */
 public function readThemeMetadata($theme_system_name)
 {
     // get theme config
     $Config = new \System\Libraries\Config();
     $Config->load('theme');
     $theme_dir = $Config->get('theme_dir', 'theme');
     unset($Config);
     $adapter = new Local($theme_dir);
     $filesystem = new Filesystem($adapter);
     unset($adapter, $theme_dir);
     $output = [];
     if ($filesystem->has($theme_system_name . DS . $theme_system_name . '.php')) {
         $content = $filesystem->read($theme_system_name . DS . $theme_system_name . '.php');
         preg_match_all('|([a-zA-Z0-9-_ ]+):(.*)$|mi', $content, $matches, PREG_PATTERN_ORDER);
         unset($content);
         if (isset($matches) && is_array($matches) && array_key_exists(1, $matches) && array_key_exists(2, $matches) && is_array($matches[1]) && is_array($matches[2])) {
             foreach ($matches[1] as $key => $item) {
                 if (!is_array($item) && array_key_exists($key, $matches[2]) && !is_array($matches[2][$key])) {
                     $output[trim($item)] = trim($matches[2][$key]);
                 }
             }
         }
     }
     // validate available metadata
     foreach ($this->available_metadata as $meta_name) {
         if (!array_key_exists($meta_name, $output)) {
             $output[$meta_name] = '';
         }
     }
     unset($filesystem, $item, $key, $matches, $meta_name);
     return $output;
 }
Example #2
0
 /**
  * connect or prepare db connection.
  * 
  * @param array $db_config array config for override the default configuration.
  */
 private function connect(array $db_config = [])
 {
     if ($this->Conn != null && $this->EntityManager != null) {
         return;
     }
     \System\Libraries\Log::write('db_connection', 'debug', 'Starting open connection to db.', $db_config);
     $db_bootstrap = new \System\Libraries\Db\Bootstrap($db_config);
     $this->EntityManager = $db_bootstrap->EntityManager;
     $this->Conn = $db_bootstrap->Conn;
     unset($db_bootstrap);
     // set table prefix property.
     if (empty($db_config)) {
         $config = new \System\Libraries\Config();
         $config->load('db');
         $this->table_prefix = $config->get('table_prefix', 'db', 'an_');
         unset($config);
     } else {
         if (is_array($db_config) && array_key_exists('table_prefix', $db_config)) {
             $this->table_prefix = $db_config['table_prefix'];
         }
     }
     // check matched multi-site domain in sites table.
     $cache = new \System\Libraries\Cache();
     $cache->setSubFolder('system/Libraries/Db/Db.php');
     if ($cache->contains('Db-getCurrentSiteData-' . $this->getCurrentSiteId())) {
         $site = $cache->fetch('Db-getCurrentSiteData-' . $this->getCurrentSiteId());
         \System\Libraries\Log::write('db_connection', 'debug', 'Getting site data using cache name "Db-getCurrentSiteData-' . $this->getCurrentSiteId() . '"', [$site]);
     } else {
         $stmt = $this->prepare('SELECT `site_id`, `site_domain`, `site_status`, `site_settings` FROM `' . $this->getTableName('sites') . '` WHERE LOWER(`site_domain`) = :site_domain AND `site_status` = 1');
         $stmt->bindValue('site_domain', isset($_SERVER['HTTP_HOST']) ? strtolower($_SERVER['HTTP_HOST']) : '');
         $stmt->execute();
         $site = $stmt->fetch();
         $cache->save('Db-getCurrentSiteData-' . $this->getCurrentSiteId(), $site, 60 * 60 * 24 * 30);
         unset($stmt);
     }
     if (isset($site) && $site !== false && $site->site_id != 1) {
         // this is not main site.
         // @todo [cms][db] add plugin hook if there is multi-site::multi-db module plugs enabled then enable these code.
         /*$site_settings = json_decode($site->site_settings, true);
           if (
               (is_array($site_settings) && array_key_exists('db', $site_settings)) ||
               (is_object($site_settings) && property_exists($site_settings, 'db'))
           ) {
               // there is settings about db for this site. it can re-connect to this site's db (for multi-site::multi-db).
               $site_db_config = (is_array($site_settings) ? $site_settings['db'] : (is_object($site_settings) ? $site_settings->db : ''));
               if ($site_db_config != null && !empty($site_db_config) && empty($db_config)) {
                   $site_db_config['table_siteid_prefix'] = $site->site_id.'_';
                   $this->reconnect($site_db_config);
               }
           }
           unset($site_db_config, $site_settings);*/
         $this->table_siteid_prefix = $site->site_id . '_';
     }
     unset($site);
 }
Example #3
0
 /**
  * get app config and set it to silex app.
  */
 protected function getAppConfig()
 {
     $config = new \System\Libraries\Config();
     $config->load('app');
     $loaded_app_config = $config->get('ALL', 'app');
     if (is_array($loaded_app_config)) {
         foreach ($loaded_app_config as $name => $value) {
             if (isset($this->Silexapp[$name])) {
                 $this->Silexapp[$name] = $value;
             }
         }
         // endforeach;
     }
     unset($config, $loaded_app_config, $name, $value);
 }
Example #4
0
 /**
  * initialize the profiler.
  * 
  * @global \Okvee\Profiler\Profiler $Profiler
  * @return mixed
  */
 public function initProfiler()
 {
     if ($this->Profiler != null) {
         return true;
     }
     $config = new \System\Libraries\Config();
     $config->load('config');
     $profiler_config = $config->get('profiler', 'config');
     if ($profiler_config === true) {
         $this->Profiler = new \Okvee\Profiler\Profiler();
         // get global $Profiler.
         global $Profiler;
         // set new Profiler object to this variable that it can be access over any of php classes/functions.
         $Profiler = $this->Profiler;
         $this->Profiler->Console->registerLogSections(array('Logs', 'Time Load', 'Memory Usage', 'Database', 'Files', 'Session', 'Get', 'Post'));
     }
     unset($config);
 }
Example #5
0
 /**
  * get only theme fallback.
  * 
  * @param \System\Libraries\Config $config previous config with loaded theme config name.
  * @param type $fallback_type admin or front.
  * @return string return only just theme name.
  */
 public function getThemeFallback(\System\Libraries\Config $config = null, $fallback_type = 'front')
 {
     if ($config == null) {
         $config = new \System\Libraries\Config();
         $config->load('theme');
     }
     $theme_fallback = $config->get('theme_fallback', 'theme');
     if (is_array($theme_fallback)) {
         if ($fallback_type == 'admin' && array_key_exists('admin', $theme_fallback)) {
             // this might be admin controller. it should be load with admin theme.
             $theme_name = $theme_fallback['admin'];
         } elseif (array_key_exists('front', $theme_fallback)) {
             $theme_name = $theme_fallback['front'];
         }
     }
     unset($config, $theme_fallback);
     if (isset($theme_name)) {
         return $theme_name;
     }
     return null;
 }
Example #6
0
 /**
  * get index file.
  * 
  * @param mixed $index include index file or not. value is 'auto', true, false. 'auto' and true is up to configuration. false will not include.
  * @return string return the index file.
  */
 public function getIndexFile($index = 'auto')
 {
     if ($index === 'auto' || $index === true) {
         $config = new \System\Libraries\Config();
         $config->load('config');
         $index_file = $config->get('index_file', 'config');
         unset($config);
         if ($index_file != null) {
             return $index_file;
         }
     }
 }
Example #7
0
 /**
  * initialize logger class. this help usage of monolog shorter.
  * 
  * @param string $channel channel name. more detail, see https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#leveraging-channels
  * @param string $log_level log level can be debug, info, notice, warning, error, critical, alert, emergency *(not recommend). more detail, see https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#log-levels
  * @param string $log_file log file name. it is automatic generate. more detail, see https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md
  */
 public function __construct($channel = 'cms', $log_level = 'info', $log_file = '')
 {
     $config = new \System\Libraries\Config();
     $config->load('config');
     $config_log = $config->get('log', 'config');
     if (isset($config_log) && is_array($config_log)) {
         if (array_key_exists('enable', $config_log) && $config_log['enable'] == false) {
             // disabled log.
             $this->log_enabled = false;
             return;
         } else {
             $this->log_enabled = true;
         }
         if (array_key_exists('donot_log_level', $config_log)) {
             $donot_log_level = $config_log['donot_log_level'];
         } else {
             $donot_log_level = 0;
         }
     }
     unset($config, $config_log);
     if ($channel == null) {
         $channel = 'cms';
     }
     switch (strtolower($log_level)) {
         case 'debug':
             $this->log_level_number = 0;
             $this->log_level = Logger::DEBUG;
             break;
         case 'info':
             $this->log_level_number = 1;
             $this->log_level = Logger::INFO;
             break;
         case 'notice':
             $this->log_level_number = 2;
             $this->log_level = Logger::NOTICE;
             break;
         case 'warning':
             $this->log_level_number = 3;
             $this->log_level = Logger::WARNING;
             break;
         case 'error':
             $this->log_level_number = 4;
             $this->log_level = Logger::ERROR;
             break;
         case 'critical':
             $this->log_level_number = 5;
             $this->log_level = Logger::CRITICAL;
             break;
         case 'alert':
             $this->log_level_number = 6;
             $this->log_level = Logger::ALERT;
             break;
         case 'emergency':
             $this->log_level_number = 7;
             $this->log_level = Logger::EMERGENCY;
             break;
         default:
             $log_level = 'debug';
             $this->log_level_number = 0;
             $this->log_level = Logger::DEBUG;
             break;
     }
     $log_level = strtolower($log_level);
     $this->log_level_text = $log_level;
     if (isset($donot_log_level) && $this->log_level_number < $donot_log_level) {
         $this->log_enabled = false;
         return false;
     } else {
         $this->log_enabled = true;
     }
     if ($log_file == null) {
         $log_file = STORAGE_PATH . DS . 'logs' . DS . $log_level . DS . date('Y-m-d') . '.log';
     }
     $this->Logger = new Logger($channel);
     $this->Streamhandler = new StreamHandler($log_file, $this->log_level);
 }
Example #8
0
 /**
  * execute command
  * 
  * @param InputInterface $input
  * @param OutputInterface $output
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $names = $input->getOption('names');
     if ($names == null) {
         $output->writeln('<error>Sorry, you did not enter theme folder name.</error>');
         unset($names);
         return false;
     }
     if ($names != null) {
         // get theme config
         $Config = new \System\Libraries\Config();
         $Config->load('theme');
         $theme_dir = $Config->get('theme_dir', 'theme');
         unset($Config);
         $names_exp = explode(',', str_replace(', ', ',', $names));
         $style = new \Symfony\Component\Console\Formatter\OutputFormatterStyle('white', 'green');
         $output->getFormatter()->setStyle('success', $style);
         unset($style);
         if (is_array($names_exp) && !empty($names_exp)) {
             foreach ($names_exp as $name) {
                 if ($name != null) {
                     $output->write('Theme "' . $name . '": ');
                     if (is_dir($theme_dir . DS . $name)) {
                         if (is_file($theme_dir . DS . $name . DS . $name . '.php')) {
                             $Metadata = new \System\Libraries\Themes\Metadata();
                             $theme_metadata = $Metadata->readThemeMetadata($name);
                             if (is_array($theme_metadata) && array_key_exists('Version', $theme_metadata) && $theme_metadata['Version'] != null) {
                                 // check theme exists or not.
                                 $options['theme_system_name'] = $name;
                                 $ThemesDb = new \System\Core\Models\ThemesDb($this->Db);
                                 $theme = $ThemesDb->getTheme($options);
                                 unset($options, $ThemesDb);
                                 if ($theme == null || empty($theme)) {
                                     // theme is not exists.
                                     $qb = $this->Db->Conn->createQueryBuilder();
                                     $qb->insert($this->Db->getTableName('themes'))->values(['theme_system_name' => ':theme_system_name', 'theme_version' => ':theme_version'])->setParameter('theme_system_name', $name, \PDO::PARAM_STR)->setParameter('theme_version', $theme_metadata['Version'], \PDO::PARAM_STR)->execute();
                                     $theme_id = $this->Db->Conn->lastInsertId();
                                     $qb->resetQueryParts();
                                     $output->writeln('<success>added successfully.</success>');
                                 } else {
                                     $output->writeln('<info>already exists.</info>');
                                 }
                             } else {
                                 $output->writeln('<error>could not add, there is no metadata in theme file.</error>');
                             }
                             unset($Metadata, $theme_metadata);
                         } else {
                             $output->writeln('<error>could not add, theme metadata file not found.</error>');
                         }
                     } else {
                         $output->writeln('<error>could not add, theme folder is not exists.</error>');
                     }
                 }
                 // endif $name not null.
             }
             // endforeach;
         }
         // endif is array $name_exp
     }
     // also clear cache about this thing. ---------------------------
     $Command = $this->getApplication()->find('System\\Core\\Console\\Cache');
     $arguments = ['command' => 'System\\Core\\Console\\Cache', '--subfolder' => 'system/Core/Models/ThemesDb.php'];
     $CmdInput = new \Symfony\Component\Console\Input\ArrayInput($arguments);
     $Command->run($CmdInput, new \Symfony\Component\Console\Output\NullOutput());
     unset($arguments, $CmdInput, $Command);
     // end clear cache ------------------------------------------------
     unset($name, $names, $names_exp, $theme_dir);
 }