/**
  * Returns the full path of the document including the key (path+key)
  *
  * @return string
  */
 public function getFullPath()
 {
     // check if this document is also the site root, if so return /
     try {
         if (Site::isSiteRequest()) {
             $site = Site::getCurrentSite();
             if ($site instanceof Site) {
                 if ($site->getRootDocument()->getId() == $this->getId()) {
                     return "/";
                 }
             }
         }
     } catch (\Exception $e) {
         \Logger::error($e);
     }
     // @TODO please forgive me, this is the dirtiest hack I've ever made :(
     // if you got confused by this functionality drop me a line and I'll buy you some beers :)
     // this is for the case that a link points to a document outside of the current site
     // in this case we look for a hardlink in the current site which points to the current document
     // why this could happen: we have 2 sites, in one site there's a hardlink to the other site and on a page inside
     // the hardlink there are snippets embedded and this snippets have links pointing to a document which is also
     // inside the hardlink scope, but this is an ID link, so we cannot rewrite the link the usual way because in the
     // snippet / link we don't know anymore that whe a inside a hardlink wrapped document
     if (!\Pimcore::inAdmin() && Site::isSiteRequest() && !FrontendTool::isDocumentInCurrentSite($this)) {
         $documentService = new Document\Service();
         $parent = $this;
         while ($parent) {
             if ($hardlinkId = $documentService->getDocumentIdFromHardlinkInSameSite(Site::getCurrentSite(), $parent)) {
                 $hardlink = Document::getById($hardlinkId);
                 if (FrontendTool::isDocumentInCurrentSite($hardlink)) {
                     $siteRootPath = Site::getCurrentSite()->getRootPath();
                     $siteRootPath = preg_quote($siteRootPath);
                     $hardlinkPath = preg_replace("@^" . $siteRootPath . "@", "", $hardlink->getRealFullPath());
                     return preg_replace("@^" . preg_quote($parent->getRealFullPath()) . "@", $hardlinkPath, $this->getRealFullPath());
                     break;
                 }
             }
             $parent = $parent->getParent();
         }
         $config = \Pimcore\Config::getSystemConfig();
         $front = \Zend_Controller_Front::getInstance();
         $scheme = ($front->getRequest()->isSecure() ? "https" : "http") . "://";
         if ($site = FrontendTool::getSiteForDocument($this)) {
             if ($site->getMainDomain()) {
                 // check if current document is the root of the different site, if so, preg_replace below doesn't work, so just return /
                 if ($site->getRootDocument()->getId() == $this->getId()) {
                     return $scheme . $site->getMainDomain() . "/";
                 }
                 return $scheme . $site->getMainDomain() . preg_replace("@^" . $site->getRootPath() . "/@", "/", $this->getRealFullPath());
             }
         }
         if ($config->general->domain) {
             return $scheme . $config->general->domain . $this->getRealFullPath();
         }
     }
     $path = $this->getPath() . $this->getKey();
     return $path;
 }
Esempio n. 2
0
 /**
  * @static
  * @return mixed|\Zend_Config
  */
 public static function getWebsiteConfig()
 {
     if (\Zend_Registry::isRegistered("pimcore_config_website")) {
         $config = \Zend_Registry::get("pimcore_config_website");
     } else {
         $cacheKey = "website_config";
         $siteId = null;
         if (Model\Site::isSiteRequest()) {
             $siteId = Model\Site::getCurrentSite()->getId();
         } elseif (Tool::isFrontentRequestByAdmin()) {
             // this is necessary to set the correct settings in editmode/preview (using the main domain)
             $front = \Zend_Controller_Front::getInstance();
             $originDocument = $front->getRequest()->getParam("document");
             if ($originDocument) {
                 $site = Tool\Frontend::getSiteForDocument($originDocument);
                 if ($site) {
                     $siteId = $site->getId();
                 }
             }
         }
         if ($siteId) {
             $cacheKey = $cacheKey . "_site_" . $siteId;
         }
         if (!($config = Cache::load($cacheKey))) {
             $settingsArray = [];
             $cacheTags = ["website_config", "system", "config", "output"];
             $list = new Model\WebsiteSetting\Listing();
             $list = $list->load();
             foreach ($list as $item) {
                 $key = $item->getName();
                 $itemSiteId = $item->getSiteId();
                 if ($itemSiteId != 0 && $itemSiteId != $siteId) {
                     continue;
                 }
                 $s = null;
                 switch ($item->getType()) {
                     case "document":
                     case "asset":
                     case "object":
                         $s = Model\Element\Service::getElementById($item->getType(), $item->getData());
                         break;
                     case "bool":
                         $s = (bool) $item->getData();
                         break;
                     case "text":
                         $s = (string) $item->getData();
                         break;
                 }
                 if ($s instanceof Model\Element\ElementInterface) {
                     $cacheTags = $s->getCacheTags($cacheTags);
                 }
                 if (isset($s)) {
                     $settingsArray[$key] = $s;
                 }
             }
             $config = new \Zend_Config($settingsArray, true);
             Cache::save($config, $cacheKey, $cacheTags, null, 998);
         }
         self::setWebsiteConfig($config);
     }
     return $config;
 }
Esempio n. 3
0
 /**
  * @throws \Zend_Controller_Router_Exception
  */
 public function init()
 {
     // this is only executed once per request (first request)
     if (self::$isInitial) {
         \Pimcore::getEventManager()->trigger("frontend.controller.preInit", $this);
     }
     parent::init();
     // log exceptions if handled by error_handler
     $this->checkForErrors();
     // general definitions
     if (self::$isInitial) {
         \Pimcore::unsetAdminMode();
         Document::setHideUnpublished(true);
         Object\AbstractObject::setHideUnpublished(true);
         Object\AbstractObject::setGetInheritedValues(true);
         Object\Localizedfield::setGetFallbackValues(true);
     }
     // assign variables
     $this->view->controller = $this;
     // init website config
     $config = Config::getWebsiteConfig();
     $this->config = $config;
     $this->view->config = $config;
     $document = $this->getParam("document");
     if (!$document instanceof Document) {
         \Zend_Registry::set("pimcore_editmode", false);
         $this->editmode = false;
         $this->view->editmode = false;
         self::$isInitial = false;
         // check for a locale first, and set it if available
         if ($this->getParam("pimcore_parentDocument")) {
             // this is a special exception for renderlets in editmode (ajax request), because they depend on the locale of the parent document
             // otherwise there'll be notices like:  Notice: 'No translation for the language 'XX' available.'
             if ($parentDocument = Document::getById($this->getParam("pimcore_parentDocument"))) {
                 if ($parentDocument->getProperty("language")) {
                     $this->setLocaleFromDocument($parentDocument->getProperty("language"));
                 }
             }
         }
         // no document available, continue, ...
         return;
     } else {
         $this->setDocument($document);
         // register global locale if the document has the system property "language"
         if ($this->getDocument()->getProperty("language")) {
             $this->setLocaleFromDocument($this->getDocument()->getProperty("language"));
         }
         if (self::$isInitial) {
             // append meta-data to the headMeta() view helper,  if it is a document-request
             if (!Model\Staticroute::getCurrentRoute() && $this->getDocument() instanceof Document\Page) {
                 if (is_array($this->getDocument()->getMetaData())) {
                     foreach ($this->getDocument()->getMetaData() as $meta) {
                         $this->view->headMeta()->addRaw($meta);
                     }
                 }
             }
         }
     }
     // this is only executed once per request (first request)
     if (self::$isInitial) {
         // contains the logged in user if necessary
         $user = null;
         // default is to set the editmode to false, is enabled later if necessary
         \Zend_Registry::set("pimcore_editmode", false);
         if (Tool::isFrontentRequestByAdmin()) {
             $this->disableBrowserCache();
             // start admin session & get logged in user
             $user = Authentication::authenticateSession();
         }
         if (\Pimcore::inDebugMode()) {
             $this->disableBrowserCache();
         }
         if (!$this->document->isPublished()) {
             if (Tool::isFrontentRequestByAdmin()) {
                 if (!$user) {
                     throw new \Zend_Controller_Router_Exception("access denied for " . $this->document->getFullPath());
                 }
             } else {
                 throw new \Zend_Controller_Router_Exception("access denied for " . $this->document->getFullPath());
             }
         }
         // logged in users only
         if ($user) {
             // set the user to registry so that it is available via \Pimcore\Tool\Admin::getCurrentUser();
             \Zend_Registry::set("pimcore_admin_user", $user);
             // document editmode
             if ($this->getParam("pimcore_editmode")) {
                 \Zend_Registry::set("pimcore_editmode", true);
                 // check if there is the document in the session
                 $docKey = "document_" . $this->getDocument()->getId();
                 $docSession = Session::getReadOnly("pimcore_documents");
                 if ($docSession->{$docKey}) {
                     // if there is a document in the session use it
                     $this->setDocument($docSession->{$docKey});
                 } else {
                     // set the latest available version for editmode if there is no doc in the session
                     $latestVersion = $this->getDocument()->getLatestVersion();
                     if ($latestVersion) {
                         $latestDoc = $latestVersion->loadData();
                         if ($latestDoc instanceof Document\PageSnippet) {
                             $this->setDocument($latestDoc);
                         }
                     }
                 }
                 // register editmode plugin
                 $front = \Zend_Controller_Front::getInstance();
                 $front->registerPlugin(new \Pimcore\Controller\Plugin\Frontend\Editmode($this), 1000);
             }
             // document preview
             if ($this->getParam("pimcore_preview")) {
                 // get document from session
                 $docKey = "document_" . $this->getParam("document")->getId();
                 $docSession = Session::getReadOnly("pimcore_documents");
                 if ($docSession->{$docKey}) {
                     $this->setDocument($docSession->{$docKey});
                 }
             }
             // object preview
             if ($this->getParam("pimcore_object_preview")) {
                 $key = "object_" . $this->getParam("pimcore_object_preview");
                 $session = Session::getReadOnly("pimcore_objects");
                 if ($session->{$key}) {
                     $object = $session->{$key};
                     // add the object to the registry so every call to Object::getById() will return this object instead of the real one
                     \Zend_Registry::set("object_" . $object->getId(), $object);
                 }
             }
             // for version preview
             if ($this->getParam("pimcore_version")) {
                 // only get version data at the first call || because of embedded Snippets ...
                 if (!\Zend_Registry::isRegistered("pimcore_version_active")) {
                     $version = Model\Version::getById($this->getParam("pimcore_version"));
                     $this->setDocument($version->getData());
                     \Zend_Registry::set("pimcore_version_active", true);
                 }
             }
         }
         // for public versions
         if ($this->getParam("v")) {
             try {
                 $version = Model\Version::getById($this->getParam("v"));
                 if ($version->getPublic()) {
                     $this->setDocument($version->getData());
                 }
             } catch (\Exception $e) {
             }
         }
         // check for persona
         if ($this->getDocument() instanceof Document\Page) {
             $this->getDocument()->setUsePersona(null);
             // reset because of preview and editmode (saved in session)
             if ($this->getParam("_ptp") && self::$isInitial) {
                 $this->getDocument()->setUsePersona($this->getParam("_ptp"));
             }
         }
         // check if document is a wrapped hardlink, if this is the case send a rel=canonical header to the source document
         if ($this->getDocument() instanceof Document\Hardlink\Wrapper\WrapperInterface) {
             // get the cononical (source) document
             $hardlinkCanonicalSourceDocument = Document::getById($this->getDocument()->getId());
             $request = $this->getRequest();
             $canonical = null;
             if (\Pimcore\Tool\Frontend::isDocumentInCurrentSite($hardlinkCanonicalSourceDocument)) {
                 $canonical = $request->getScheme() . "://" . $request->getHttpHost() . $hardlinkCanonicalSourceDocument->getFullPath();
             } elseif (Model\Site::isSiteRequest()) {
                 $sourceSite = \Pimcore\Tool\Frontend::getSiteForDocument($hardlinkCanonicalSourceDocument);
                 if ($sourceSite) {
                     if ($sourceSite->getMainDomain()) {
                         $sourceSiteRelPath = preg_replace("@^" . preg_quote($sourceSite->getRootPath(), "@") . "@", "", $hardlinkCanonicalSourceDocument->getRealFullPath());
                         $canonical = $request->getScheme() . "://" . $sourceSite->getMainDomain() . $sourceSiteRelPath;
                     }
                 }
             }
             if ($canonical) {
                 $this->getResponse()->setHeader("Link", '<' . $canonical . '>; rel="canonical"');
             }
         }
         \Pimcore::getEventManager()->trigger("frontend.controller.postInit", $this);
     }
     // set some parameters
     $this->editmode = \Zend_Registry::get("pimcore_editmode");
     $this->view->editmode = \Zend_Registry::get("pimcore_editmode");
     self::$isInitial = false;
 }
Esempio n. 4
0
 /**
  * @param $string
  * @param null $document
  * @param null $hostUrl
  * @return mixed
  * @throws \Exception
  */
 public static function setAbsolutePaths($string, $document = null, $hostUrl = null)
 {
     if ($document && $document instanceof Model\Document == false) {
         throw new \Exception('$document has to be an instance of Document');
     }
     $replacePrefix = "";
     if (!$hostUrl && $document) {
         // try to determine if the newsletter is within a site
         $site = \Pimcore\Tool\Frontend::getSiteForDocument($document);
         if ($site) {
             $hostUrl = "http://" . $site->getMainDomain();
             $replacePrefix = $site->getRootPath();
         }
         // fallback
         if (!$hostUrl) {
             $hostUrl = \Pimcore\Tool::getHostUrl();
         }
     }
     //matches all links
     preg_match_all("@(href|src)\\s*=[\"']([^(http|mailto|javascript|data:|#)].*?(css|jpe?g|gif|png)?)[\"']@is", $string, $matches);
     if (!empty($matches[0])) {
         foreach ($matches[0] as $key => $value) {
             $path = $matches[2][$key];
             if (strpos($path, '//') === 0) {
                 $absolutePath = "http:" . $path;
             } elseif (strpos($path, '/') === 0) {
                 $absolutePath = preg_replace("@^" . $replacePrefix . "/@", "/", $path);
                 $absolutePath = $hostUrl . $absolutePath;
             } else {
                 $absolutePath = $hostUrl . "/{$path}";
                 $netUrl = new \Net_URL2($absolutePath);
                 $absolutePath = $netUrl->getNormalizedURL();
             }
             $path = preg_quote($path);
             $string = preg_replace("!([\"']){$path}([\"'])!is", "\\1" . $absolutePath . "\\2", $string);
         }
     }
     preg_match_all("@srcset\\s*=[\"'](.*?)[\"']@is", $string, $matches);
     foreach ((array) $matches[1] as $i => $value) {
         $parts = explode(',', $value);
         foreach ($parts as $key => $v) {
             $parts[$key] = $hostUrl . trim($v);
         }
         $s = ' srcset="' . implode(', ', $parts) . '" ';
         if ($matches[0][$i]) {
             $string = str_replace($matches[0][$i], $s, $string);
         }
     }
     return $string;
 }
Esempio n. 5
0
 /**
  * @param $element Document
  * @return array
  */
 protected function getTreeNodeConfig($element)
 {
     $childDocument = $element;
     $tmpDocument = ["id" => $childDocument->getId(), "idx" => intval($childDocument->getIndex()), "text" => $childDocument->getKey(), "type" => $childDocument->getType(), "path" => $childDocument->getRealFullPath(), "basePath" => $childDocument->getRealPath(), "locked" => $childDocument->isLocked(), "lockOwner" => $childDocument->getLocked() ? true : false, "published" => $childDocument->isPublished(), "elementType" => "document", "leaf" => true, "permissions" => ["view" => $childDocument->isAllowed("view"), "remove" => $childDocument->isAllowed("delete"), "settings" => $childDocument->isAllowed("settings"), "rename" => $childDocument->isAllowed("rename"), "publish" => $childDocument->isAllowed("publish"), "unpublish" => $childDocument->isAllowed("unpublish")]];
     // add icon
     $tmpDocument["iconCls"] = "pimcore_icon_" . $childDocument->getType();
     if (\Pimcore\Tool\Admin::isExtJS6()) {
         $tmpDocument["expandable"] = $childDocument->hasChilds();
         $tmpDocument["loaded"] = !$childDocument->hasChilds();
     }
     // set type specific settings
     if ($childDocument->getType() == "page") {
         $tmpDocument["leaf"] = false;
         $tmpDocument["expanded"] = $childDocument->hasNoChilds();
         $tmpDocument["permissions"]["create"] = $childDocument->isAllowed("create");
         $tmpDocument["iconCls"] = "pimcore_icon_page";
         // test for a site
         try {
             $site = Site::getByRootId($childDocument->getId());
             $tmpDocument["iconCls"] = "pimcore_icon_site";
             unset($site->rootDocument);
             $tmpDocument["site"] = $site;
         } catch (\Exception $e) {
         }
     } elseif ($childDocument->getType() == "folder" || $childDocument->getType() == "link" || $childDocument->getType() == "hardlink") {
         $tmpDocument["leaf"] = false;
         $tmpDocument["expanded"] = $childDocument->hasNoChilds();
         if ($childDocument->hasNoChilds() && $childDocument->getType() == "folder") {
             $tmpDocument["iconCls"] = "pimcore_icon_folder";
         }
         $tmpDocument["permissions"]["create"] = $childDocument->isAllowed("create");
     } elseif (method_exists($childDocument, "getTreeNodeConfig")) {
         $tmp = $childDocument->getTreeNodeConfig();
         $tmpDocument = array_merge($tmpDocument, $tmp);
     }
     $tmpDocument["qtipCfg"] = ["title" => "ID: " . $childDocument->getId(), "text" => "Type: " . $childDocument->getType()];
     // PREVIEWS temporary disabled, need's to be optimized some time
     if ($childDocument instanceof Document\Page && Config::getSystemConfig()->documents->generatepreview) {
         $thumbnailFile = PIMCORE_TEMPORARY_DIRECTORY . "/document-page-previews/document-page-screenshot-" . $childDocument->getId() . ".jpg";
         // only if the thumbnail exists and isn't out of time
         if (file_exists($thumbnailFile) && filemtime($thumbnailFile) > $childDocument->getModificationDate() - 20) {
             $thumbnailPath = str_replace(PIMCORE_DOCUMENT_ROOT, "", $thumbnailFile);
             $tmpDocument["thumbnail"] = $thumbnailPath;
         }
     }
     if ($childDocument instanceof Document\Page) {
         $tmpDocument["url"] = $childDocument->getFullPath();
         $site = Tool\Frontend::getSiteForDocument($childDocument);
         if ($site) {
             $tmpDocument["url"] = "http://" . $site->getMainDomain() . preg_replace("@^" . $site->getRootPath() . "/?@", "/", $childDocument->getRealFullPath());
         }
     }
     $tmpDocument["cls"] = "";
     if (!$childDocument->isPublished()) {
         $tmpDocument["cls"] .= "pimcore_unpublished ";
     }
     if ($childDocument->isLocked()) {
         $tmpDocument["cls"] .= "pimcore_treenode_locked ";
     }
     if ($childDocument->getLocked()) {
         $tmpDocument["cls"] .= "pimcore_treenode_lockOwner ";
     }
     return $tmpDocument;
 }