_fixTimeWarning();
// get joomla application object
$app = JFactory::getApplication();
// check shLib is available
if (!defined('SHLIB_VERSION'))
{
$msg = 'sh404SEF requires the shLib system plugin to be enabled, but you appear to have disabled it. Please enable it again!';
if ($app->isAdmin())
{
$app->enqueuemessage($msg, 'error');
}
return;
}
// register our autoloader
$this->_registerAutoloader();
if (!defined('SH404SEF_AUTOLOADER_LOADED'))
{
$msg = 'sh404SEF autoloader failed to initialize, sh404SEF cannot start.';
if ($app->isAdmin())
{
$app->enqueuemessage($msg, 'error');
}
ShlSystem_Log::error('sh404sef', $msg);
return;
}
// init hooks
ShlHook::load('', 'sh404sef_functions.php');
// init the sh404SEF wbalib app - post sh404SEF version 4.20
if (defined('SH404SEF_APP_PATH'))
{
try
{
$this->sh4App = Factory::get()->getThis(
'app',
'sh404sef',
array(
'id' => 'sh404sef',
'namespace' => '\Weeblr\Sh404sef',
'rootpath' => SH404SEF_APP_PATH
)
);
}
catch (\Exception $e)
{
ShlSystem_Log::error('sh404sef', 'Error loading wbLib sh404SEF app: ' . $e->getMessage());
}
}
else
{
ShlSystem_Log::error('sh404sef', 'Unable to load wbLib sh404SEF app, SH404SEF_APP_PATH is not defined.');
}
// create the unique page info object, and initialize it
$pageInfo = Sh404sefFactory::getPageInfo();
$pageInfo->init();
// base class
$baseClassFile = JPATH_ADMINISTRATOR . '/components/com_sh404sef/sh404sef.class.php';
if (file_exists($baseClassFile))
{
include_once($baseClassFile);
}
else
{
$msg = 'sh404SEF base class file is missing or unreadable: ' . $baseClassFile . ', sh404SEF cannot start.';
if ($app->isAdmin())
{
$app->enqueueMessage($msg, 'error');
}
ShlSystem_Log::error('sh404sef', $msg);
return;
}
// define a default path for loading layouts
ShlMvcLayout_Helper::$defaultBasePath = sh404SEF_LAYOUTS;
// get our configuration
$sefConfig = Sh404sefFactory::getConfig();
// hook for a few SEO hacks
if ($sefConfig->Enabled && $app->isSite())
{
$this->_hacks();
}
// security layer
$secLayerFile = JPATH_ROOT . '/components/com_sh404sef/shSec.php';
if (!$app->isAdmin() && $sefConfig->shSecEnableSecurity && shFileExists($secLayerFile))
{
include_once($secLayerFile);
// do security checks
shDoSecurityChecks();
shCleanUpSecLogFiles(); // see setting in class file for clean up frequency
}
else
{
if ($sefConfig->shSecEnableSecurity && !shFileExists($secLayerFile))
{
$msg = 'sh404SEF security layer file is missing or unreadable: ' . $secLayerFile . ', sh404SEF security feature will not operate properly.';
if ($app->isAdmin())
{
$app->enqueueMessage($msg);
}
ShlSystem_Log::error('sh404sef', $msg);
return;
}
}
// optionnally collect page creation time
if (!$app->isAdmin() && $sefConfig->analyticsEnableTimeCollection)
{
jimport('joomla.error.profiler');
// creating the profiler object will start the counter
$profiler = JProfiler::getInstance('sh404sef_profiler');
}
// load plugins, as per configuration
$this->_loadPlugins($type = 'sh404sefcore');
// load extension plugins, created by others
$this->_loadPlugins($type = 'sh404sefextplugins');
// hook to be able to install other SEF extension plugins
//Sh404sefHelperExtplugins::loadInstallAdapters();
// another hook to allow other SEF extensions language file to be loaded
Sh404sefHelperExtplugins::loadLanguageFiles();
if (!$sefConfig->Enabled)
{
// go away if not enabled
return;
}
if (!defined('SH404SEF_IS_RUNNING'))
{
DEFINE('SH404SEF_IS_RUNNING', 1);
}
/**
* Hook to run the registered API handlers.
*
* @api
* @package wbLib\action\api
* @var wblib_api_process_request
* @since 0.0.1
*
*/
Factory::get()->getThe('hook')->run(
'wblib_api_process_request'
);
if (!$app->isAdmin())
{
// setup our JPagination replacement, so as to bring
// back # of items per page in the url, in order
// to properly calculate pagination
// will only work if php > 5, so test for that
if (version_compare(phpversion(), '5.0') >= 0)
{
// this register the old file, but do not load it if PHP5
// will prevent further calls to the same jimport()
// to actually do anything, because the 'joomla.html.pagination' key
// is now registered statically in Jloader::import()
jimport('joomla.html.pagination');
// now we can register our own path
JLoader::register('JPagination', JPATH_ADMINISTRATOR . '/components/com_sh404sef/pagination.php');
}
// attach parse and build rules to Joomla router
$joomlaRouter = $app->getRouter();
$pageInfo->router = new Sh404sefClassRouter();
if (version_compare(JVERSION, '3.4', 'ge'))
{
$joomlaRouter->attachParseRule(
function (&$router, &$uri) {
if (Sh404sefHelperAnalytics::shouldDisplayFrontend($uri, $router))
{
Factory::get()
->getA('Weeblr\Sh404sef\Helper\Analytics')
->displayFrontend();
}
},
JRouter::PROCESS_BEFORE
);
$joomlaRouter->attachParseRule(array($pageInfo->router, 'preprocessParseRule'), JRouter::PROCESS_BEFORE);
}
$joomlaRouter->attachParseRule(array($pageInfo->router, 'parseRule'));
$joomlaRouter->attachBuildRule(array($pageInfo->router, 'buildRule'));
// pretend SEF is on, mostly for Joomla SEF plugin to work
// as it checks directly 'sef' value in config, instead of
// using $router->getMode()
JFactory::$config->set('sef', 1);
// perform startup operations, such as detecting request caracteristics
// and checking redirections
$uri = JURI::getInstance();
$pageInfo->router->startup($uri);
}
}
public function onAfterRoute()
{
if (!defined('SH404SEF_IS_RUNNING'))
{
return;
}
// set template, to perform alternate template output, if set to
$app = JFactory::getApplication();
if (!$app->isAdmin())
{
$this->_setAlternateTemplate();
}
}
public function onAfterDispatch()
{
if (defined('SH404SEF_IS_RUNNING'))
{
$app = JFactory::getApplication();
if (!$app->isAdmin())
{
// reset alternate template
$this->_resetAlternateTemplate();
// create shurl on the fly for this page
// if not already done
if (Sh404sefConfigurationEdition::$id == 'full' && JFactory::getDocument()->getType() == 'html')
{
// shortlinks
Sh404sefHelperShurl::updateShurls();
}
// insert (some) meta data
if (
JFactory::getDocument()->getType() == 'html'
&&
$app->input->getCmd('format') != 'raw'
&&
Sh404sefFactory::getConfig()->shMetaManagementActivated
)
{
Sh404sefHelperMetadata::includeMetaPlugin();
$pageInfo = Sh404sefFactory::getPageInfo();
if (!preg_match('/(&|\?)lang=[a-zA-Z]{2,3}/iuU', $pageInfo->currentNonSefUrl))
{
// no lang string, let's add default
$shTemp = explode('-', $pageInfo->currentLanguageTag);
$shLangTemp = $shTemp[0] ? $shTemp[0] : 'en';
$pageInfo->currentNonSefUrl .= '&lang=' . $shLangTemp;
}
Sh404sefHelperMetadata::insertMetaData();
}
}
}
}
/**
* Optionally operate searches of adjustment on page content
*
* @param string $context The context of the content being passed to the plugin.
* @param mixed &$row An object with a "text" property or the string to be processed.
* @param mixed &$params Additional parameters.
* @param integer $page Optional page number. Unused. Defaults to zero.
*
* @return boolean True on success.
* @throws Exception
*/
public function onContentPrepare($context, &$row, &$params, $page = 0)
{
// Don't run this plugin when the content is being indexed
if ($context == 'com_finder.indexer')
{
return true;
}
if (JFactory::getApplication()->isAdmin())
{
return true;
}
if (!defined('SH404SEF_IS_RUNNING'))
{
return true;
}
if (JFactory::getApplication()->input->getCmd('format') == 'raw')
{
return true;
}
// should we spend time on this?
$config = Sh404sefFactory::getConfig();
if (is_object($row) && !empty($row->text))
{
$row->text = $this->searchOGPImage($row->text, $config, $context, $row);
if ($config->autoBuildDescription)
{
$row->text = Sh404sefHelperMetadata::buildAutoDescription($context, $row->text, $params, $page);
}
}
else if (!is_object($row))
{
$row = $this->searchOGPImage($row, $config);
if ($config->autoBuildDescription)
{
$row = Sh404sefHelperMetadata::buildAutoDescription($context, $row, $params, $page);
}
}
return true;
}
public function onAfterRender()
{
if (defined('SH404SEF_IS_RUNNING'))
{
$config = Sh404sefFactory::getConfig();
$document = JFactory::getDocument();
if (JFactory::getApplication()->isAdmin())
{
if (version_compare(JVERSION, '3.0', 'ge'))
{
// are we on an edit page?
$app = JFactory::getApplication();
$option = $app->input->getCmd('option');
$view = $app->input->getCmd('view');
$layout = $app->input->getCmd('layout');
if ($layout == 'edit'
&& (($option == 'com_content' && $view == 'article') || ($option == 'com_categories' && $view == 'category')
|| ($option == 'com_contact' && $view == 'contact') || ($option == 'com_newsfeeds' && $view == 'newsfeed')
|| ($option == 'com_weblinks' && $view == 'weblink'))
)
{
// variations in field name
if ($option == 'com_newsfeeds' || $option == 'com_contact')
{
$titleFieldName = 'jform_name';
}
else
{
$titleFieldName = 'jform_title';
}
// attach an input counter to the title input boxes
if ($document->getType() == 'html')
{
$page = JResponse::getBody();
// insert css and js
$link = '';
$cssLink = ShlHtml_Manager::getInstance()->getMediaLink('bs', 'css');
if (strpos($page, $cssLink) === false)
{
$link .= '';
}
$jsLink = ShlHtml_Manager::getInstance()->getMediaLink('bs', 'js');
if (strpos($page, $jsLink) === false)
{
$link .= "\n" . '';
}
if (!empty($link))
{
$page = str_replace('', $link . '', $page);
}
// insert custom js to attach counters to title and metadesc fields
$script = ShlHtmlBs_Helper::renderInputCounter(
$titleFieldName,
Sh404sefFactory::getPConfig()->metaDataSpecs['metatitle-joomla-be']
);
$script .= ShlHtmlBs_Helper::renderInputCounter(
'jform_metadesc',
Sh404sefFactory::getPConfig()->metaDataSpecs['metadesc-joomla-be']
);
$page = str_replace('