Your IP : 216.73.216.170


Current Path : /var/www/iplanru/data/www/i-plan.ru/components/com_sef/sef_ext/
Upload File :
Current File : /var/www/iplanru/data/www/i-plan.ru/components/com_sef/sef_ext/com_content.php

<?php
/**
 * SEF component for Joomla!
 * 
 * @package   JoomSEF
 * @version   4.7.8
 * @author    ARTIO s.r.o., http://www.artio.net
 * @copyright Copyright (C) 2020 ARTIO s.r.o. 
 * @license   GNU/GPLv3 http://www.artio.net/license/gnu-general-public-license
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access.');

define( '_COM_SEF_PRIORITY_CONTENT_ARTICLE_ITEMID',         15 );
define( '_COM_SEF_PRIORITY_CONTENT_ARTICLE',                20 );
define( '_COM_SEF_PRIORITY_CONTENT_CATEGORYLIST_ITEMID',    35 );
define( '_COM_SEF_PRIORITY_CONTENT_CATEGORYLIST',           40 );
define( '_COM_SEF_PRIORITY_CONTENT_CATEGORYBLOG_ITEMID',    55 );
define( '_COM_SEF_PRIORITY_CONTENT_CATEGORYBLOG',           60 );

// Backwards compatibility with Joomla <3.7
if (!version_compare(JVERSION, '3.7', 'ge')) {
    require_once(JPATH_ROOT.'/components/com_content/helpers/route.php');

    class SefExtContentRouteHelper extends ContentHelperRoute
    {
        public static function getItemid($id)
        {
            $needles = array('article' => array((int)$id));
            $Itemid = self::_findItem($needles);
            return $Itemid;
        }
    }
}

class SefExt_com_content extends SefExt
{
    public function getNonSefVars(&$uri)
    {
        $this->_createNonSefVars($uri);

        return array($this->nonSefVars, $this->ignoreVars);
    }

    protected function _createNonSefVars(&$uri)
    {
        if (!isset($this->nonSefVars) && !isset($this->ignoreVars)) {
            $this->nonSefVars = array();
            $this->ignoreVars = array();
        }

        $this->params =& SEFTools::GetExtParams('com_content');
        $sefConfig = SEFConfig::getConfig();

        if ($sefConfig->appendNonSef && ($this->params->get('pagination', '0') != '0')) {

            if (!is_null($uri->getVar('limit'))) {
                $this->nonSefVars['limit'] = $uri->getVar('limit');
            }
            if (!is_null($uri->getVar('limitstart'))) {
                $this->nonSefVars['limitstart'] = $uri->getVar('limitstart');
            }
        }
        if (!is_null($uri->getVar('filter'))) {
            $this->nonSefVars['filter'] = $uri->getVar('filter');
        }
        if (!is_null($uri->getVar('return')))
            $this->nonSefVars['return'] = $uri->getVar('return');
    }

    protected function _getArticle($id)
    {
        $sefConfig = SEFConfig::getConfig();
        $title=array();

        $field = 'title';
        if (SEFTools::UseAlias($this->params, 'title_alias')) {
            $field = 'alias';
        }

        $id = intval($id);
        $query = "SELECT `id`, `title`, `alias`, `introtext`, `fulltext`, `language`, `metakey`, `metadesc`, `metadata`, `catid` FROM `#__content` WHERE `id` = '{$id}'";
        $this->_db->setQuery($query);
        $row = $this->_db->loadObject('stdClass', $this->config->translateItems);
        // Article dont exists
        if (!is_object($row)) {
            JoomSefLogger::Log("Article with ID {$id} could not be found.", $this, 'com_content');
            return array();
        }

        $catInfo = $this->getCategory($row->catid, true);
        if ($catInfo === false) {
            return array();
        }

        if($this->params->get('show_category', '2') != 0) {
            if (is_array($catInfo->path) && (count($catInfo->path) > 0)) {
                $title = array_merge($title, $catInfo->path);
            }
        }

        //$this->item_desc = $row->introtext;
        if ($this->params->get('googlenewsnum', 0) == 0) {
            $title[] = (($this->params->get('articleid', '0') == 1) ? $id.'-' : '').$row->$field;
        } else {
            $title = array_merge($title, $this->GoogleNews($row->$field, $id));
        }
        $this->getMetaData($row);

        if ($this->params->get('meta_titlecat',0) == 1) {
            $this->pageTitle = $row->title;
            $metatitle = array_merge(array($row->title), $catInfo->titles);
            $this->metatags["metatitle"] = implode(" - ", $metatitle);
        }

        $this->metadesc = $row->introtext;
        $this->origmetadesc = $row->metadesc;
        $this->metakeySource = $row->fulltext;
        $this->origmetakey = $row->metakey;

        $this->articleText = $row->introtext . chr(13) . chr(13) . $row->fulltext;

        return $title;
    }

    /**
     * Returns cached instance of content router menu rules, used to search correct Itemid
     *
     * @return JComponentRouterRulesMenu
     */
    private function getContentMenuRules()
    {
        static $rules = null;

        if (!$rules) {
            $app = JFactory::getApplication();
            $router = new ContentRouter($app, $app->getMenu());
            $rules = new JComponentRouterRulesMenu($router);
        }

        return $rules;
    }

    /**
     * Called before the non-SEF URL is searched in the database
     *
     * @param JUri $uri
     */
    public function beforeCreate(&$uri)
    {
        // remove the limitstart and limit variables if they point to the first page
        if (!is_null($uri->getVar('limitstart')) && ($uri->getVar('limitstart') == '0')) {
            $uri->delVar('limitstart');
            $uri->delVar('limit');
        }

        // Remove empty variables
        if ($uri->getVar('limitstart') == '') {
            $uri->delVar('limitstart');
        }
        if ($uri->getVar('showall') == '') {
            $uri->delVar('showall');
        }

        // Try to guess the correct Itemid if set to
        if ($this->params->get('guessId', '0') != '0') {
            if (version_compare(JVERSION, '3.7', 'ge')) {
                // Use new objects
                if (!is_null($uri->getVar('id'))) {
                    $query = $uri->getQuery(true);

                    // Convert lang code to tag if set, so the menu rules can find menu items for correct language
                    if (!empty($query['lang'])) {
                        $query['lang'] = JoomSEF::getLangCode($query['lang']);
                    }

                    $this->getContentMenuRules()->preprocess($query);
                    if (!empty($query['Itemid'])) {
                        $uri->setVar('Itemid', $query['Itemid']);
                    }
                }

            }
            else {
                // Backwards compatibility for Joomla <3.7
                if (!is_null($uri->getVar('Itemid')) && !is_null($uri->getVar('id'))) {
                    $itemid = SefExtContentRouteHelper::getItemid($uri->getVar('id'));
                    if (!is_null($itemid)) {
                        $uri->setVar('Itemid', $itemid);
                    }
                }
            }
        }

        // Remove the part after ':' from variables
        if (!is_null($uri->getVar('id')))    SEFTools::fixVariable($uri, 'id');
        if (!is_null($uri->getVar('catid'))) SEFTools::fixVariable($uri, 'catid');

        // TODO: We should remove this, as it generates 1 unnecessary SQL query for each article link,
        // instead the catid should just be always removed from article URL (but when updating JoomSEF,
        // we'll need to update URLs already in database to reflect such change = remove catid from them!)
        // If catid not given, try to find it
        $catid = $uri->getVar('catid');
        if (!is_null($uri->getVar('view')) && ($uri->getVar('view') == 'article') && !is_null($uri->getVar('id')) && empty($catid)) {
            $id = $uri->getVar('id');
            $query = "SELECT `catid` FROM `#__content` WHERE `id` = ".(int)$id;
            $this->_db->setQuery($query);
            $catid = $this->_db->loadResult();

            if (is_null($catid)) {
                JoomSefLogger::Log("Article with ID {$id} could not be found.", $this, 'com_content');
            }

            if (!empty($catid)) {
                $uri->setVar('catid', $catid);
            }
        }

        // remove empty id in categories list
        if ($uri->getVar('view') == 'categories' && ! (int) $uri->getVar('id'))
            $uri->delVar('id');

        return;
    }

    protected function GoogleNews($title, $id)
    {
        $db = JFactory::getDBO();

        $num = '';
        $add = $this->params->get('googlenewsnum', '0');

        if ($add == '1' || $add == '3') {
            // Article ID
            $digits = trim($this->params->get('digits', '3'));
            if (!is_numeric($digits)) {
                $digits = '3';
            }

            $num1 = sprintf('%0'.$digits.'d', $id);
        }
        if ($add == '2' || $add == '3') {
            // Publish date
            $query = "SELECT `publish_up` FROM `#__content` WHERE `id` = ".(int)$id;
            $db->setQuery($query);
            $time = $db->loadResult();

            $time = strtotime($time);

            $date = $this->params->get('dateformat', 'ddmm');

            $search = array('dd', 'd', 'mm', 'm', 'yyyy', 'yy');
            $replace = array(date('d', $time),
            date('j', $time),
            date('m', $time),
            date('n', $time),
            date('Y', $time),
            date('y', $time) );
            $num2 = str_replace($search, $replace, $date);
        }

        if ($add == '1') {
            $num = $num1;
        }
        else if ($add == '2') {
            $num = $num2;
        }
        else if ($add == '3') {
            $sep = $this->params->get('iddatesep', '');
            if ($this->params->get('iddateorder', '0') == '0') {
                $num = $num2.$sep.$num1;
            }
            else {
                $num = $num1.$sep.$num2;
            }
        }

        if (!empty($num)) {
            $onlyNum = ($this->params->get('title_alias', 'global') == 'googlenews');

            if ($onlyNum) {
                $title = $num;
            }
            else {
                $sep = $this->params->get('iddatesep', '');
                if (empty($sep)) {
                    $sefConfig = SEFConfig::getConfig();
                    $sep = $sefConfig->replacement;
                }

                $where = $this->params->get('numberpos', '1');

                if( $where == '1' ) {
                    $title = $title.$sep.$num;
                } else {
                    $title = $num.$sep.$title;
                }
            }
        }

        // Support for slashes
        $title = explode('/', $title);

        return $title;
    }

    function _processPagination(&$uri) {
        $title=array();
        $sefConfig = SEFConfig::getConfig();
        $handle=$this->params->get('pagination',0);
        if($sefConfig->appendNonSef==true && $handle==1 ) {
            $this->nonSefVars['limitstart'] = $uri->getVar('limitstart');
            return array();
        }
        //$appParams=JFactory::getApplication('site')->getParams();
        $appParams=JApplication::getInstance('site')->getParams('com_content');
        $menu = JFactory::getApplication()->getMenu('site');
        $Itemid = $uri->getVar('Itemid');
        if (!$Itemid) {
            // We need to find Itemid first
            $active = $menu->getActive();
            if (is_null($active)) {
                $active = $menu->getDefault();
            }
            $Itemid = $active->id;
        }
        $menuParams = $menu->getParams($Itemid);

        // Combine app and menu parameters
        $params = clone $appParams;
        $params->merge($menuParams);

        // View: Article
        if($uri->getVar("view")=="article") {
            if(($limitstart=$uri->getVar('limitstart'))>0) {
                $pagetext = null;
                if ($this->params->get('multipagetitles', '1') == '1') {
                    $pagetext = $this->_getPageTitle($limitstart);
                }

                if (!is_null($pagetext)) {
                    $title[] = $pagetext;
                }
                else {
                    $pagetext = strval($limitstart+1);
                    if (($cnfPageText = $sefConfig->getPageText())) {
                        $this->pageNumberText = str_replace('%s', $limitstart+1, $cnfPageText);
                        $title[] = $this->pageNumberText;
                    }
                }
            }
            if($uri->getVar('showall')==1) {
                if ($this->params->get('always_en', '0') == '1') {
                    $title[] = 'All pages';
                }
                else {
                    $title[] = JText::_('COM_SEF_ALL_PAGES');
                }
            }
        // Layouts: category default list; View: Archive
        } else if($uri->getVar("layout")!="blog" && $uri->getVar("view")!="featured") {
            // If pagination filter is disabled we can make sef URL's with pagination
            if($params->get('show_pagination_limit',1)==0 || $uri->getVar("view")!="featured") {
                $limit=$params->get('display_num');
                $limitstart=$uri->getVar('limitstart');
                if (intval($limit) == 0) {
                    $limit = 1;
                }
                @$page=intval($limitstart/$limit)+1;
                if($page!=1) {
                    $pagetext = strval($page);
                    if (($cnfPageText = $sefConfig->getPageText())) {
                        $this->pageNumberText = str_replace('%s', $page, $cnfPageText);
                        $title[] = $this->pageNumberText;
                    }
                }
            } else {
                if(!is_null($uri->getVar('limitstart'))) {
                    $this->nonSefVars['limitstart'] = $uri->getVar('limitstart');
                }
            }
        // Layout: category blog; View: featured
        } else {
            $leading = $params->get('num_leading_articles', 1);
            $intro   = $params->get('num_intro_articles', 4);
            $limit = $leading + $intro;
            if (intval($limit) == 0) {
                $limit = 1;
            }
            $limitstart=$uri->getVar('limitstart');
            $page = intval($limitstart / $limit)  + 1;
            if($page!=1) {
                $pagetext = strval($page);
                if (($cnfPageText = $sefConfig->getPageText())) {
                    $this->pageNumberText = str_replace('%s', $page, $cnfPageText);
                    $title[] = $this->pageNumberText;
                }
            }
        }

        return $title;
    }

    private function _getPageTitle($page)
    {
        jimport('joomla.utilities.utility');

        if (empty($this->articleText)) {
            return null;
        }

        // simple performance check
        if (JString::strpos($this->articleText, 'class="system-pagebreak') === false) {
            return null;
        }

        // regex
        $regex = '#<hr(.*)class="system-pagebreak"(.*)\/>#iU';

        // Find all occurences
        $matches = array();
        preg_match_all($regex, $this->articleText, $matches, PREG_SET_ORDER);

        if (!isset($matches[$page-1]) || !isset($matches[$page-1][2])) {
            return null;
        }

        $attrs = JUtility::parseAttributes($matches[$page-1][0]);

        if (isset($attrs['alt'])) {
            return stripslashes($attrs['alt']);
        }
        else if (isset($attrs['title'])) {
            return stripslashes($attrs['title']);
        }
        else {
            return null;
        }
    }

    /**
     * Returns category with filtered path (excludes selected categories, always leaves the last one)
     *
     * @param int $catId
     * @param bool $removeAll
     * @return object|bool
     */
    private function getCategory($catId, $removeAll = false)
    {
        $catInfo = $this->getCategoryInfo($catId);
        if ($catInfo === false) {
            JoomSefLogger::Log("Category with ID {$catId} could not be found.", $this, 'com_content');
            return false;
        }

        $exclude = $this->params->get('exclude_catid', '');
        if (is_array($exclude) && is_array($catInfo->path) && ($removeAll || (count($catInfo->path) > 1))) {
            foreach ($catInfo->path_ids as $key => $id) {
                if (in_array($id, $exclude)) {
                    // Exclude this category
                    unset($catInfo->path[$key]);
                    unset($catInfo->path_ids[$key]);
                    unset($catInfo->titles[$key]);

                    if (!$removeAll && count($catInfo->path) < 2)
                        break;
                }
            }
        }

        return $catInfo;
    }

    public function create(&$uri)
    {
        $this->metadesc = null;
        $this->cat_desc = null;
        $this->item_desc = null;

        $sefConfig = SEFConfig::getConfig();
        $title = array();

        $vars = $uri->getQuery(true);
        extract($vars);
        $this->_createNonSefVars($uri);

        // Set title.
        $title[] = JoomSEF::_getMenuTitleLang(@$option, $this->lang, @$Itemid);

        // #19413: Handle task first, it has higher priority than view
        if (isset($task) && $task == 'article.add') {
            if (isset($catid)) {
                $catInfo = $this->getCategory($catid);
                if ($catInfo === false) {
                    return $uri;
                }

                if (is_array($catInfo->path) && (count($catInfo->path) > 0)) {
                    $title = array_merge($title, $catInfo->path);
                }
            }

            if ($this->params->get('always_en', '0') == '1') {
                $title[] = 'New';
            }
            else {
                $title[]=JText::_('COM_SEF_NEW');
            }
        }
        else if (isset($task) && $task == 'article.edit') {
            if(isset($a_id)) {
                $title=array_merge($title,$this->_getArticle($a_id));
            }
            if ($this->params->get('always_en', '0') == '1') {
                $title[] = 'Edit';
            }
            else {
                $title[]=JText::_('COM_SEF_EDIT');
            }
        }
        else {
            switch (@$view) {
                case 'form':
                    // 13.2.2012, dajo: Don't SEF
                    return $uri;

                    //$this->nonSefVars = array();
                    //$this->nonSefVars["return"]=@$return;
                    //if(isset($a_id)) {
                    //    $title=array_merge($title,$this->_getArticle($a_id));
                    //}
                    //$title[] = JText::_('COM_SEF_FORM');
                     //break;
                case 'featured':
                    if(isset($format) && $format=="feed") {
                        @$title[]=$type;
                    }
                    $title=array_merge($title,$this->_processPagination($uri));
                    break;
                case 'categories':
                    break;
                case 'category':
                    if (isset($id)) {
                        $catInfo = $this->getCategory($id);
                        if ($catInfo === false) {
                            return $uri;
                        }

                        if (is_array($catInfo->path) && (count($catInfo->path) > 0)) {
                            $title = array_merge($title, $catInfo->path);
                        }
                    }
                    if($this->params->get('add_layout', 0) == 2 || ($this->params->get('add_layout', 0) == 1 && $this->params->get('def_layout', 'default') != @$layout)) {
                        @$title[]=$layout;
                    }
                    if(isset($format) && $format=="feed") {
                        @$title[]=$type;
                    }
                    $title=array_merge($title,$this->_processPagination($uri));
                    break;
                case 'article':
                    $articleInfo = $this->_getArticle($id);
                    if (!$articleInfo)
                        return $uri;

                    $title = array_merge($title, $articleInfo);
                    $title = array_merge($title, $this->_processPagination($uri));
                    break;
                case 'archive':
                    if( !empty($year) ) {
                        $title[] = $year;
                    }
                    if( !empty($month) ) {
                        $title[] = $month;
                    }
                    $title=array_merge($title,$this->_processPagination($uri));
                    break;
                default:
                    // Don't SEF
                    return $uri;
            }
        }

        // #34487: Handle format=pdf
        if (isset($format) && $format == 'pdf') {
            $title[] = $format;
        }

        // Handle printing
        if (isset($print) && (intval($print) == 1)) {
            if ($this->params->get('always_en', '0') == '1') {
                $title[] = 'Print';
            }
            else {
                $title[] = JText::_('JGLOBAL_PRINT');
            }
        }

        $newUri = $uri;
        if (count($title) > 0) {
            // Generate meta tags
            $this->metatags=$this->getMetaTags();
            if (($this->params->get('meta_titlepage', '0') == '1') && !empty($this->pageNumberText)) {
                // Add page number to page title
                if (!empty($this->metatags["metatitle"])) {
                    $this->metatags["metatitle"] .= ' - '.$this->pageNumberText;
                }
                else {
                    $this->metatags["metatitle"] = (!empty($this->pageTitle) ? $this->pageTitle.' - ' : '') . $this->pageNumberText;
                }
            }

            $priority = $this->getPriority($uri);
            $sitemap = $this->getSitemapParams($uri);
            if(isset($this->lang)) {
                $lang=$this->lang;
            }

            $newUri = JoomSEF::_sefGetLocation($uri, $title, null, null, null, @$lang, $this->nonSefVars, null, $this->metatags, $priority, true,null, $sitemap);

        }

        return $newUri;
    }

    function getSitemapParams(&$uri)
    {
        if ($uri->getVar('format', 'html') != 'html') {
            // Handle only html links
            return array();
        }

        $view = $uri->getVar('view');

        $sm = array();
        switch ($view)
        {
            case 'article':
            case 'category':
                $indexed = $this->params->get('sm_'.$view.'_indexed', '1');
                $freq = $this->params->get('sm_'.$view.'_freq', '');
                $priority = $this->params->get('sm_'.$view.'_priority', '');

                if (!empty($indexed)) $sm['indexed'] = $indexed;
                if (!empty($freq)) $sm['frequency'] = $freq;
                if (!empty($priority)) $sm['priority'] = $priority;

                break;
        }

        return $sm;
    }

    public function getPriority(&$uri)
    {
        $itemid = $uri->getVar('Itemid');
        $view = $uri->getVar('view');
        $layout = $uri->getVar('layout');

        switch($view)
        {
            case 'article':
                if( is_null($itemid) ) {
                    return _COM_SEF_PRIORITY_CONTENT_ARTICLE;
                } else {
                    return _COM_SEF_PRIORITY_CONTENT_ARTICLE_ITEMID;
                }
                break;

            case 'category':
                if( $layout == 'blog' ) {
                    if( is_null($itemid) ) {
                        return _COM_SEF_PRIORITY_CONTENT_CATEGORYBLOG;
                    } else {
                        return _COM_SEF_PRIORITY_CONTENT_CATEGORYBLOG_ITEMID;
                    }
                } else {
                    if( is_null($itemid) ) {
                        return _COM_SEF_PRIORITY_CONTENT_CATEGORYLIST;
                    } else {
                        return _COM_SEF_PRIORITY_CONTENT_CATEGORYLIST_ITEMID;
                    }
                }
                break;

            default:
                return null;
                break;
        }
    }

    function getURLPatterns($item) {
        $urls=array();
        if($item->getTableName()=='#__categories') {
            // Category view
            $urls[]='index\.php\?option=com_content(&format=feed)?&id='.$item->id.'&';
            // Content View
            $urls[]='index\.php\?option=com_content&catid='.$item->id.'&id=';
            $tree=$item->getTree($item->id);
            foreach($tree as $catitem) {
                $urls[]='index\.php\?option=com_content(&format=feed)?&id='.$catitem->id.'&';
                $urls[]='index\.php\?option=com_content&catid='.$catitem->id.'&id=';
            }
        } else {
            $urls[]='index\.php\?option=com_content(&catid=([0-9])*)*&id='.$item->id.'(&lang=[a-z]+)?(&limitstart=[0-9]+)?(&type=(atom|rss))?&view=article';
        }
        return $urls;
    }
}