3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /* Disclaimer While we make every effort to ensure that this code is fit for its intended purpose, we make no guarantees as to its functionality. CoreTrek AS will accept no responsibility for the loss of data or any other damage or financial loss caused by use of this code. Copyright This programming code is copyright of CoreTrek AS. Permission to run this code is given to approved users of CoreTrek's publishing system CorePublish. This source code may not be copied, modified or otherwise repurposed for use by a third party without the written permission of CoreTrek AS. Contact webmaster@coretrek.com for information. */ /** * Class SeoFrontendUtil is a class for getting seo information on a frontend site. * <p> * You can create your own frontend class to override the default behaviour. * Create a class in your site code, and set the domain config SeoFrontendUtilClass * to the full path of your class. * <p> * Example: SeoFrontendUtilClass config ==> 'site:misc.MySeoFrontendUtil' * * @package cplib.common.seo.util */ class SeoFrontendUtil extends CtBase { public static $canonicalUrlParameterWhitelist = array(); /** * Get the dominant seo entity object from the current page rendering. * * The domination rules: * * 1. If there is a runtime article, this article is used. * 2. If there is a runtime category, this is used. * 3. Fallback to site, if no article of category. * * @return CpSeoObject */ public function getDominantSeoObject() { $log = $this->getCorePublish()->getLog(); $site = $this->getCorePublish()->getSite(); // Caching seoObejct in static variable static $dominantSeoObject = null; if(!isset($dominantSeoObject)) { $log->debug(__FILE__,__LINE__,'Getting dominant SeoObject, setting to static variable cache.'); $runtimeArticle = $site->getRuntimeValue('article'); $runtimeCategory = $site->getRunTimeValue('category'); if($runtimeArticle instanceof Article) { $dominantSeoObject = $runtimeArticle; } else if($runtimeCategory instanceof Category) { $dominantSeoObject = $runtimeCategory; } else { $dominantSeoObject = $site; } } return $dominantSeoObject; } /** * Build page title from the current rendering page SeoObjects. * * <p> * The title is contructed by these rules: * <ol> * <li>If there is a runtime article, and this article has set a custom seo title, use this title.</li> * <li>If the article seo title is the same as article header, we use: "article header - site seo title"</li> * <li>If there is a runtime category, and this category has set a custom seo title, use this title.</li> * <li>If the category seo title is the same as category name, we use "category name - site seo title".</li> * <li>If none of the above, use site seo title.</li> * </ol> * * @return string */ public function getPageSeoTitle() { $site = $this->getCorePublish()->getSite(); $runtimeArticle = $site->getRuntimeValue('article'); $runtimeCategory = $site->getRunTimeValue('category'); $title = $site->getSeoTitle(); if($runtimeArticle instanceof Article) { if($runtimeArticle->getSeoTitle() == $runtimeArticle->getHeader()) { $title = $runtimeArticle->getHeader() . (strlen($title) > 0?' - ' . $title:''); } else { $title = $runtimeArticle->getSeoTitle(); } } else if($runtimeCategory instanceof Category) { // XXX should we check for main article here? if($runtimeCategory->getSeoTitle() == $runtimeCategory->getName()) { if ($runtimeCategory->getCategoryID() == $site->getSiteConfig("frontpage", $site->getActiveLanguage())) { $title = $title .= ' - '.$runtimeCategory->getName(); } else { $title = $runtimeCategory->getName() . (strlen($title) > 0?' - ' . $title:''); } } else { $title = $runtimeCategory->getSeoTitle(); } } return $title; } /** * Get meta description for the current rendering page. * Description is fetched from the dominant SeoObject returned by * getDominantSeoObject method. * * @return string */ public function getPageSeoDescription() { return $this->getDominantSeoObject()->getSeoDescription(); } /** * Get meta keywords for the the current rendering page. * Keywords is fetched from the dominant SeoObject as returned by * getDominantSeoObject method. * * @return string */ public function getPageSeoKeywordString() { return $this->getDominantSeoObject()->getSeoKeywordString(); } /** * Registers a whitelist parameter for the canonical url. * * <p> * See the documentation for getPageCanonicalUrl() for more details * about what white list parameters are * </p> * * @see getPageCanonicalUrl() * @param string $parameter * @param boolean $resetExisting */ public static function registerCanonicalUrlWhitelistParameter($parameter, $resetExisting = false) { if ($resetExisting == true) { self::$canonicalUrlParameterWhitelist = array(); } self::$canonicalUrlParameterWhitelist[] = $parameter; } /** * Returns the canonical url for the current page. * * <p> * A known problem within web pages in general, and dynamic page from publishing * systems in particular, is that there might be several url's to the exact * same page. E.g the two following urls: * <br/> * <pre> * http://www.coretrek.no/category.php?categoryID=123 * http://www.coretrek.no/category123.html * </pre> * will present the exact same content, because the latter url is simply an * alias to the first one, its just another way of reaching the same page. * <br/> * The problem is that search engines do not know this, and they see these * pages as two different pages. E.g. google has functionality that detects * duplicate pages, and marks them as duplicates. * by specifying canonical link, search engines can see which urls are related * and they can detect the main url. * * <p> * See http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html * and http://bugzilla.coretrek.com/show_bug.cgi?id=4176 * </p> * * <p> * You can also register whitelist parameters for the url, by using the * method SeoFrontendUtil::registerCanonicalUrlWhitelistParameter().<br/> * whitelist parameters are page/url parameters which do NOT affect page * contents. <br/> * For example, the following two urls are normally expected to give * different output:<br/> * <pre> * http://site.com/category.php?categoryID=1&page=1 * http://site.com/category.php?categoryID=1&page=2 * </pre> * <br/> * This is because you'd normally expect the page parameter to mean that * there is a new page displaying, having different contents. * <br/> * However, if the two url parameters switch place, it would still be the * same page. The same applies if another url parameter is added, which does * not affect the content of the page: * <br/> * <pre> * http://site.com/category.php?categoryID=1&page=1 * http://site.com/category.php?categoryID=1&page=1&fromurl=htt://www.coretrek.no * </pre> * <br/> * The urls above will probably return the exact same content, because it is * unlikely that the "fromurl" parameter affects the content. This means * that this parameter should be skipped from the canonical url, and this can * be done by registering it as a whitelist parameter. This can be done like * this: <br/> * <code> * SeoFrontendUtil::registerCanonicalUrlWhitelistParameter('fromurl); * </code> * This call has to be done early, before the getPageCanonicalUrl() is called, which * normally happens through the method CorePublishSite.getCommonHeadHtmlElements(), * which is called in include/layouttemplates/head.php. * </p> * <p> * Global whitelist parameters can also be set as a site config named * "seo.canonical.canonicalUrlParameterWhitelist". * </p> * * @return string a string containing the canonical url for the current page */ public function getPageCanonicalUrl() { $site = $this->getCorePublish()->getSite(); $domain = $this->getCorePublish()->getDomain(); $urlutil = $this->getCorePublish()->getUrlUtil(); $sysdb = $this->getCorePublish()->getSystemDatabase(); $db = $this->getCorePublish()->getDb(); $urlutil->setURL(); $parameterKeys = $urlutil->getParameterKeys(); // Whitelist of parameters that are ok to remove $whitelist = array(); $whitelistConfig = $site->getConfig("seo.canonical.canonicalUrlParameterWhitelist"); if ($whitelistConfig instanceof CpParameter) { $whitelist = explode(",", $whitelistConfig->getValueAsString()); } // Append default whitelist parameters $whitelist[] = "printMe"; // merge with static whitelist for early registrations $whitelist = array_merge($whitelist, self::$canonicalUrlParameterWhitelist); $whitelist = array_fill_keys($whitelist, true); $currentUrl = $urlutil->getURL(); $currentEntity = $this->getDominantSeoObject(); if ($currentEntity instanceof Article) { $currentUrl = $domain->getArticleUrlSimple($currentEntity); $whitelist["articleID"] = true; } elseif ($currentEntity instanceof Category) { // If requested category is frontpage category, we always // return the site's html root $frontpageCategoryID = $site->getConfigValue('frontpage'); if($currentEntity->getExternalLinkCategoryID() == $frontpageCategoryID || $currentEntity->getExternalLinkCategoryID() < 1 && $currentEntity->getCategoryId() == $frontpageCategoryID) { // category is front page category $frontpageUrl = $site->getUrl(); if($site->getActiveLanguage() != $site->getDefaultLanguage()) { $fpUrlObject = new CtUrlUtil($frontpageUrl); $fpUrlObject->setParameter('lang', $site->getActiveLanguage()); $frontpageUrl = $fpUrlObject->getUrl(); } $currentUrl = $frontpageUrl; } else { // regular category $currentUrl = $domain->getCategoryUrl($currentEntity); } $whitelist["categoryID"] = true; } $newurl = new CtUrlUtil($currentUrl); // Remove any whitelisted parameters foreach ($parameterKeys as $parameterKey) { if (!isset($whitelist[$parameterKey])) { $newurl->setParameter($parameterKey, $urlutil->getParameter($parameterKey)); } } return $newurl->getURL(); } /** * Returns a link rel element in the format used by getCommonHeadHtmlElements in CorePublish site. * * <p> * This methods tries to create the proper link rel tag to print out for the current * entity (site, category, article) according to the rules given here: * https://developers.google.com/webmasters/smartphone-sites/details * </p> * * @return array */ public function getMultiChannelPageCanonicalHeadElements() { // Global objects $domain = $this->getCorePublish()->getDomain(); $urlutil = $this->getCorePublish()->getUrlUtil(); $sysdb = $this->getCorePublish()->getSystemDatabase(); $db = $this->getCorePublish()->getDb(); // Manager objects $channelManager = $this->getCorePublish()->getChannelManager(); // current runtime objects $currentSite = $this->getCorePublish()->getSite(); $currentCategory= $this->getCorePublish()->getCategory(); $currentChannel = $channelManager->getChannel(); // Base/fallback values $eqSite = $currentSite; $eqChannel = $currentChannel; $currentUrl = null; // Resolve 'the other' site if ($currentChannel->isMobile()) { // If we are on mobile, we should show a canonical url to the // regular web page $eqSiteId = $channelManager->getDefaultSiteforSite($currentSite->getSiteID()); } else { // If we are on the regular site, we should show a canonical url // to the mobile site $eqSiteId = $channelManager->getMobileSiteforSite($currentSite->getSiteID()); } // If 'the other site' has another site ID than the current, we must // resolve other equivalent categories. // If it's the same ID we are using the same tree for both mobile and default web, // and there is no need to handle this at all as the url's will be the same. // In this case we fall back to the regular canonical url if ($eqSiteId != $currentSite->getSiteID()) { if ($eqSiteId > 0) { $tmpSite = $domain->getSiteById($db, $eqSiteId); if ($tmpSite instanceof CorePublishSite) { $eqSite = $tmpSite; $eqChannel = $channelManager->getChannelForSite($eqSite); } } // Get the current entity we are processing $currentEntity = $this->getDominantSeoObject(); // Special handling of front page url if ($currentEntity instanceof Category && $currentEntity->isFrontpage()) { $currentUrl = $eqSite->getUrl(); $eqEntity = $eqSite; } elseif ($currentEntity instanceof CtChannelExposable) { try { // Regular articles and categories $eqEntity = $currentEntity->getChannelEquivalentEntity($eqChannel); if ($eqEntity instanceof CtEntity == false) { // No given entity was found // If the current entity is an article, // check if current article is cross published // to the other site/channel. If it is, just // use the same article if ($currentEntity instanceof Article && $currentEntity->isExposedToChannel($eqChannel)) { $eqEntity = $currentEntity; } } // Try getting the url if ($eqEntity instanceof Article) { $currentUrl = $eqEntity->getUrl($eqSite); } elseif($eqEntity instanceof Category) { $currentUrl = $eqEntity->getUrl(); } } catch (CtNestedException $ex) { $this->getCorePublish()->getLog()->error(__FILE__, __LINE__, "Got exception when resolving url", $ex); $currentUrl = null; } } // If we found a url, return the proper elements if (strlen($currentUrl)) { if ($currentChannel->isMobile()) { // If we are on mobile, return canonical url to the desktop url return array(array ("rel"=>"canonical","href"=>$currentUrl)); } else { // If we are on default, return alternate link AND canonical to // ourselves. $locale = $eqSite->getActiveLanguage(); return array( array ("rel"=>"canonical","href"=>$this->getPageCanonicalUrl()), array( 'rel'=>'alternate', 'media'=>'only screen and (max-width: 640px)', 'type'=>'text/html', 'title'=> $eqEntity->getSeoTitle(), "href"=>$currentUrl ) ); } } } // If we did not find anything, just return the default canonical url return array(array ("rel"=>"canonical","href"=>$this->getPageCanonicalUrl())); } }
based on pv9Lt
Output for 5.4.0 - 5.6.28, 7.0.0 - 7.1.0
Fatal error: Class 'CtBase' not found in /in/CoLSe on line 36
Process exited with code 255.
Output for hhvm-3.10.0 - 3.12.0
Fatal error: unknown class CtBase in /in/CoLSe on line 36
Process exited with code 255.