Example #1
0
 public function indexAction()
 {
     /** @var \DOMElement $statusNode */
     $statusNode = $this->root->appendChild($this->xml->createElement('status'));
     // stats/difra
     $statusNode->setAttribute('difra', \Difra\Envi\Version::getFrameworkVersion(true));
     $statusNode->setAttribute('cache', \Difra\Cache::getInstance()->adapter);
     $statusNode->setAttribute('webserver', $_SERVER['SERVER_SOFTWARE']);
     $statusNode->setAttribute('phpversion', phpversion());
     // stats/plugins
     /** @var $pluginsNode \DOMElement */
     $plugins = \Difra\Plugger::getAllPlugins();
     $enabledPlugins = $disabledPlugins = [];
     foreach ($plugins as $plugin) {
         if ($plugin->isEnabled()) {
             $enabledPlugins[] = $plugin->getName();
         } else {
             $disabledPlugins[] = $plugin->getName();
         }
     }
     $statusNode->setAttribute('enabledPlugins', implode(', ', $enabledPlugins));
     $statusNode->setAttribute('disabledPlugins', implode(', ', $disabledPlugins));
     // stats/extensions
     /** @var $extensionsNode \DOMElement */
     $extensionsNode = $statusNode->appendChild($this->xml->createElement('extensions'));
     $extensions = get_loaded_extensions();
     $extensionsOk = [];
     $extensionsExtra = [];
     $extensionsRequired = ['dom', 'SimpleXML', 'xsl', 'zlib', 'ctype', 'json', 'mbstring', 'Reflection', 'Phar', 'imagick'];
     foreach ($extensions as $extension) {
         if (in_array($extension, $extensionsRequired)) {
             $extensionsOk[] = $extension;
             unset($extensionsRequired[array_search($extension, $extensionsRequired)]);
         } else {
             $extensionsExtra[] = $extension;
         }
     }
     natcasesort($extensionsOk);
     natcasesort($extensionsRequired);
     natcasesort($extensionsExtra);
     $extensionsNode->setAttribute('ok', implode(', ', $extensionsOk));
     $extensionsNode->setAttribute('required', implode(', ', $extensionsRequired));
     $extensionsNode->setAttribute('extra', implode(', ', $extensionsExtra));
     /** @var $permNode \DOMElement */
     $permNode = $statusNode->appendChild($statusNode->ownerDocument->createElement('permissions'));
     if (!is_dir(DIR_DATA)) {
         $permNode->setAttribute('data', 'Directory ' . DIR_DATA . ' does not exist!');
     } elseif (!is_writable(DIR_DATA)) {
         $permNode->setAttribute('data', 'Directory ' . DIR_DATA . ' is not writeable!');
     }
 }
Example #2
0
 /**
  * Get all available plugins list
  * @return string[]
  */
 private static function getPluginsNames()
 {
     static $plugins = null;
     if (!is_null($plugins)) {
         return $plugins;
     }
     $plugins = [];
     if (!($plugins = Cache::getInstance()->get('plugger_plugins'))) {
         if (is_dir(DIR_PLUGINS) and $dir = opendir(DIR_PLUGINS)) {
             while (false !== ($subdir = readdir($dir))) {
                 if ($subdir != '.' and $subdir != '..' and is_dir(DIR_PLUGINS . '/' . $subdir)) {
                     if (is_readable(DIR_PLUGINS . "/{$subdir}/Plugin.php")) {
                         $plugins[] = $subdir;
                     }
                 }
             }
         }
         Cache::getInstance()->put('plugger_plugins', $plugins, 300);
     }
     return $plugins;
 }
Example #3
0
 public function get()
 {
     if ($this->cached == true) {
         return $this->xml;
     }
     $outPut = $this->rssDoc->saveXml();
     if ($this->settings['cache'] == 1) {
         \Difra\Cache::getInstance()->put('rss', $outPut, $this->settings['ttl'] * 60);
     }
     return $outPut;
 }
Example #4
0
 /**
  * Load menu element data
  * @return bool
  */
 private function load()
 {
     if ($this->loaded) {
         return true;
     }
     if (!$this->id) {
         return false;
     }
     $cache = Cache::getInstance();
     $cacheKey = 'cms_menuitem_' . $this->id;
     if (!($data = $cache->get($cacheKey))) {
         $data = CMS::getDB()->fetchRow("SELECT * FROM `cms_menu_items` WHERE `id`=?", [$this->id]);
         $cache->put($cacheKey, $data);
     }
     if (!$data) {
         return false;
     }
     $this->menu = $data['menu'];
     $this->parent = $data['parent'];
     $this->visible = $data['visible'];
     $this->page = $data['page'];
     $this->link = $data['link'];
     $this->linkLabel = $data['link_label'];
     $this->loaded = true;
     return true;
 }
Example #5
0
 /**
  * Чистит кэш
  * @static
  */
 private static function cleanCache()
 {
     \Difra\Cache::getInstance()->remove('fp_forms');
 }
Example #6
0
 /**
  * Tags::getCloudXml()
  * @desc Устанавливаем xml для облака тегов
  * @param string $module
  * @param \DOMElement $node
  * @param integer $limit
  * @return array
  */
 public function getCloudXml($module, $node, $limit = 25)
 {
     $this->_checkModule($module);
     $cache = \Difra\Cache::getInstance();
     $cacheKey = 'tags_cloud_' . $module . '_' . $limit;
     if (!($res = $cache->get($cacheKey))) {
         $db = Difra\MySQL::getInstance();
         $res = $db->fetch("SELECT `tag`, `link`, `weight` FROM `{$module}_tags` WHERE `cloud`=1 ORDER BY `tag` ASC LIMIT " . $limit);
         $cache->put($cacheKey, $res, 57 + rand(0, 6));
     }
     if (empty($res)) {
         return false;
     }
     $maxWeight = 100;
     /** @var \DOMElement $cloudXml */
     $cloudXml = $node->appendChild($node->ownerDocument->createElement('cloud'));
     foreach ($res as $data) {
         /** @var \DOMElement $tag */
         $tag = $cloudXml->appendChild($node->ownerDocument->createElement('tag'));
         $tag->setAttribute('tag', $data['tag']);
         $tag->setAttribute('link', rawurlencode($data['link']));
         if ($maxWeight != 0) {
             $p = $data['weight'] / $maxWeight * 100;
             $tag->setAttribute('pt', round($this->minPt + ($this->maxPt - $this->minPt) * $p / 100));
         } else {
             $tag->setAttribute('pt', '13');
         }
     }
     return true;
 }
Example #7
0
 /**
  * Load page data
  * @return bool
  */
 private function load()
 {
     if ($this->loaded) {
         return true;
     }
     if (!$this->id) {
         return false;
     }
     $cache = Cache::getInstance();
     if (!($data = $cache->get('cms_page_' . $this->id))) {
         $data = CMS::getDB()->fetchRow("SELECT * FROM `cms` WHERE `id`=?", [$this->id]);
         $cache->put('cms_page_' . $this->id, $data);
     }
     if (!$data) {
         return false;
     }
     $this->title = $data['title'];
     $this->uri = $data['tag'];
     $this->body = $data['body'];
     $this->hidden = $data['hidden'];
     $this->loaded = true;
     return true;
 }
Example #8
0
 /**
  * Set session handler to use current cache, if available
  */
 public function setSessionsInCache()
 {
     static $set = false;
     if ($set) {
         return;
     }
     if (Cache::getInstance()->adapter == Cache::INST_NONE) {
         return;
     }
     /** @noinspection PhpUnusedParameterInspection */
     session_set_save_handler(function ($s, $n) {
         return true;
     }, function () {
         return true;
     }, function ($id) {
         return Cache::getInstance()->get($this->sessionPrefix . $id, false) ?: '';
     }, function ($id, $data) {
         if (!$data) {
             return false;
         }
         Cache::getInstance()->put($this->sessionPrefix . $id, $data, 86400);
         // 24h
         return true;
     }, function ($id) {
         Cache::getInstance()->remove($this->sessionPrefix . $id);
     }, function ($expire) {
         return true;
     });
     $set = true;
 }
Example #9
0
 /**
  * Чистит кэш
  * @static
  */
 private static function cleanCache()
 {
     Cache::getInstance()->remove('announcements_additionals');
 }
Example #10
0
 /**
  * Возвращает все формы в XML
  * @param \DOMNode $node
  */
 public function getListXML($node)
 {
     $cached = true;
     $Cache = \Difra\Cache::getInstance();
     $forms = $Cache->get('fp_forms');
     $formsArray = null;
     if (!$forms) {
         $cached = false;
         $db = \Difra\MySQL::getInstance();
         $query = "SELECT * FROM `fp_forms`";
         $forms = $db->fetch($query);
     }
     foreach ($forms as $k => $data) {
         $formXML = $node->appendChild($node->ownerDocument->createElement('form'));
         $formXML->setAttribute('id', $data['id']);
         $formXML->setAttribute('title', $data['title']);
         $formXML->setAttribute('uri', $data['uri']);
         $formXML->setAttribute('hidden', $data['hidden']);
         $formXML->setAttribute('fieldsCount', count(unserialize($data['fields'])));
         if (!$cached) {
             $formsArray[$data['id']] = $data;
         }
     }
     if (!$cached && !empty($formsArray)) {
         // устанавливаем кэш
         $Cache->put('fp_forms', $formsArray, 10800);
     }
 }
Example #11
0
 /**
  * Get compiled resource
  * @param      $instance
  * @param bool $withSources
  * @return bool|null
  */
 public function compile($instance, $withSources = false)
 {
     if (!$this->checkInstance($instance)) {
         return false;
     }
     // get compiled from cache if available
     $cache = Cache::getInstance();
     if ($cache->adapter != Cache::INST_NONE) {
         $cacheKey = "{$instance}_{$this->type}";
         if (!is_null($cached = $cache->get($cacheKey))) {
             if ($cache->get($cacheKey . '_build') == Version::getBuild()) {
                 return $cached;
             }
         }
         // wait for updated data, try to get lock
         $busyKey = "{$cacheKey}_busy";
         $busyValue = rand(100000, 999999);
         while (true) {
             if (!($currentBusy = $cache->get($busyKey))) {
                 // got updated data?
                 if (!is_null($cached = $cache->get($cacheKey)) and $cache->get($cacheKey . '_build') == Version::getBuild()) {
                     return $cached;
                 }
                 // try to lock cache
                 $cache->put($busyKey, $busyValue, 7);
                 usleep(5000);
             } else {
                 // is cache locked by me?
                 if ($currentBusy == $busyValue) {
                     break;
                 }
                 usleep(19111);
             }
         }
         // compile resource
         $resource = $this->realCompile($instance, $withSources);
         // cache data
         $cache->put($cacheKey, $resource, self::CACHE_TTL);
         $cache->put($cacheKey . '_build', Version::getBuild(), self::CACHE_TTL);
         $cache->put($cacheKey . '_modified', gmdate('D, d M Y H:i:s') . ' GMT', self::CACHE_TTL);
         // unlock cache
         $cache->remove($busyKey);
         return $resource;
     } else {
         return $this->realCompile($instance, $withSources);
     }
 }
Example #12
0
 /**
  * Cache data
  * @param string $result Result type: 'action' or '404'
  */
 private static function saveCache($result = 'action')
 {
     if ($result != '404') {
         // save main variables
         $match = ['vars' => ['controllerClass' => self::$controllerClass, 'controllerFile' => self::$controllerFile, 'controllerUri' => self::$controllerUri, 'parameters' => self::$parameters], 'result' => $result];
         // save action types variables
         foreach (self::$methodTypes as $methodType) {
             $methodVar = "method{$methodType[0]}{$methodType[1]}";
             $match['vars'][$methodVar] = self::${$methodVar};
         }
     } else {
         $match = ['result' => '404'];
     }
     Cache::getInstance()->put(self::getCacheKey(), $match, 300);
 }
Example #13
0
 /**
  * Clear menu caches
  * @static
  */
 public static function clearCache()
 {
     Cache::getInstance()->remove('cms_menu_list');
 }
Example #14
0
 /**
  * Clear cache
  */
 public static function cleanCache()
 {
     Cache::getInstance()->remove(self::CACHE_KEY);
 }
Example #15
0
 /**
  * Возвращает в xml список всех категорий
  * @static
  * @param \DOMNode|\DOMElement $node
  */
 public static function getList($node)
 {
     $Cache = Cache::getInstance();
     $res = $Cache->get('announcements_category');
     if (!$res) {
         $db = MySQL::getInstance();
         $query = "SELECT * FROM `announcements_category`";
         $res = $db->fetch($query);
     } else {
         $node->setAttribute('cached', true);
     }
     if (!empty($res)) {
         $saveToCache = null;
         foreach ($res as $k => $data) {
             $catNode = $node->appendChild($node->ownerDocument->createElement('category'));
             $catNode->setAttribute('id', $data['id']);
             $catNode->setAttribute('category', $data['category']);
             $catNode->setAttribute('name', $data['categoryText']);
             $saveToCache[$data['id']] = $data;
         }
         $Cache->put('announcements_category', $saveToCache, 10800);
     }
 }
Example #16
0
 /**
  * Делает +1 для статистики поста
  * @param      $postId
  * @param null $groupId
  * @param null $userId
  * @return bool
  */
 public function savePostStat($postId, $groupId = null, $userId = null)
 {
     $Cache = \Difra\Cache::getInstance();
     $db = \Difra\MySQL::getInstance();
     if (!$db->fetchRow("SHOW TABLES LIKE 'blogs_stat'")) {
         return false;
     }
     $postsStat = $Cache->get('posts_stat');
     $client = \Difra\Auth::getInstance()->getEmail();
     if (is_null($client)) {
         $client = $_SERVER['REMOTE_ADDR'];
     }
     $groupAdd = $userAdd = '';
     if (!is_null($groupId)) {
         $groupAdd = ", `group_id`='" . intval($groupId) . "' ";
     }
     if (!is_null($userId)) {
         $userAdd = ", `user_id`='" . intval($userId) . "' ";
     }
     $query = "INSERT INTO `blogs_stat` SET `date`='" . date('Y-m-d', time()) . "', `post_id`='" . intval($postId) . "', `count`=1 " . $groupAdd . $userAdd . " ON DUPLICATE KEY UPDATE `count`=`count`+1";
     if (isset($postsStat[$client])) {
         if (!in_array($postId, $postsStat[$client])) {
             if (count($postsStat[$client]) == 3) {
                 array_shift($postsStat[$client]);
             }
             $postsStat[$client][] = $postId;
             $Cache->put('posts_stat', $postsStat);
             $db->query($query);
             return true;
         } else {
             return false;
         }
     }
     $postsStat[$client][] = $postId;
     $Cache->put('posts_stat', $postsStat);
     $db->query($query);
     return true;
 }
Example #17
0
 /**
  * Constructor: load session.
  */
 public function __construct()
 {
     Cache::getInstance()->setSessionsInCache();
     self::load();
 }