/** * @return bool */ public function start() { if (\Pimcore\Tool::isFrontentRequestByAdmin() && !$this->force) { return false; } if ($content = CacheManager::load($this->key)) { echo $content; return true; } $this->captureEnabled = true; ob_start(); return false; }
public function init() { parent::init(); $pimUser = false; if (\Pimcore\Tool::isFrontentRequestByAdmin()) { $pimUser = \Pimcore\Tool\Admin::getCurrentUser(); if ($pimUser) { //echo "IS ADMIN"; } } $identity = \Zend_Auth::getInstance()->getIdentity(); if (!$identity && !$pimUser or $this->getParam('oid') != $identity['oid']) { $this->redirect("/"); } else { // login ok } }
/** * @param $asset * @param Config $config * @param null $fileSystemPath * @param bool $deferred deferred means that the image will be generated on-the-fly (details see below) * @return mixed|string */ public static function process($asset, Config $config, $fileSystemPath = null, $deferred = false) { $format = strtolower($config->getFormat()); $contentOptimizedFormat = false; if (!$fileSystemPath && $asset instanceof Asset) { $fileSystemPath = $asset->getFileSystemPath(); } if ($asset instanceof Asset) { $id = $asset->getId(); } else { $id = "dyn~" . crc32($fileSystemPath); } if (!file_exists($fileSystemPath)) { return "/pimcore/static/img/filetype-not-supported.png"; } $modificationDate = filemtime($fileSystemPath); $fileExt = File::getFileExtension(basename($fileSystemPath)); // simple detection for source type if SOURCE is selected if ($format == "source" || empty($format)) { $format = self::getAllowedFormat($fileExt, array("jpeg", "gif", "png"), "png"); $contentOptimizedFormat = true; // format can change depending of the content (alpha-channel, ...) } if ($format == "print") { $format = self::getAllowedFormat($fileExt, array("svg", "jpeg", "png", "tiff"), "png"); if (($format == "tiff" || $format == "svg") && \Pimcore\Tool::isFrontentRequestByAdmin()) { // return a webformat in admin -> tiff cannot be displayed in browser $format = "png"; } else { if ($format == "tiff") { $transformations = $config->getItems(); if (is_array($transformations) && count($transformations) > 0) { foreach ($transformations as $transformation) { if (!empty($transformation)) { if ($transformation["method"] == "tifforiginal") { return str_replace(PIMCORE_DOCUMENT_ROOT, "", $fileSystemPath); } } } } } else { if ($format == "svg") { return str_replace(PIMCORE_DOCUMENT_ROOT, "", $fileSystemPath); } } } } $thumbDir = $asset->getImageThumbnailSavePath() . "/thumb__" . $config->getName(); $filename = preg_replace("/\\." . preg_quote(File::getFileExtension($asset->getFilename())) . "/", "", $asset->getFilename()); // add custom suffix if available if ($config->getFilenameSuffix()) { $filename .= "~-~" . $config->getFilenameSuffix(); } // add high-resolution modifier suffix to the filename if ($config->getHighResolution() > 1) { $filename .= "@" . $config->getHighResolution() . "x"; } $filename .= "." . $format; $fsPath = $thumbDir . "/" . $filename; if (!is_dir(dirname($fsPath))) { File::mkdir(dirname($fsPath)); } $path = str_replace(PIMCORE_DOCUMENT_ROOT, "", $fsPath); // check for existing and still valid thumbnail if (is_file($fsPath) and filemtime($fsPath) >= $modificationDate) { return $path; } // deferred means that the image will be generated on-the-fly (when requested by the browser) // the configuration is saved for later use in Pimcore\Controller\Plugin\Thumbnail::routeStartup() // so that it can be used also with dynamic configurations if ($deferred) { $configId = "thumb_" . $id . "__" . md5($path); TmpStore::add($configId, $config, "thumbnail_deferred"); return $path; } // transform image $image = Asset\Image::getImageTransformInstance(); if (!$image->load($fileSystemPath)) { return "/pimcore/static/img/filetype-not-supported.png"; } $image->setUseContentOptimizedFormat($contentOptimizedFormat); $startTime = StopWatch::microtime_float(); $transformations = $config->getItems(); // check if the original image has an orientation exif flag // if so add a transformation at the beginning that rotates and/or mirrors the image if (function_exists("exif_read_data")) { $exif = @exif_read_data($fileSystemPath); if (is_array($exif)) { if (array_key_exists("Orientation", $exif)) { $orientation = intval($exif["Orientation"]); if ($orientation > 1) { $angleMappings = [2 => 180, 3 => 180, 4 => 180, 5 => 90, 6 => 90, 7 => 90, 8 => 270]; if (array_key_exists($orientation, $angleMappings)) { array_unshift($transformations, ["method" => "rotate", "arguments" => ["angle" => $angleMappings[$orientation]]]); } // values that have to be mirrored, this is not very common, but should be covered anyway $mirrorMappings = [2 => "vertical", 4 => "horizontal", 5 => "vertical", 7 => "horizontal"]; if (array_key_exists($orientation, $mirrorMappings)) { array_unshift($transformations, ["method" => "mirror", "arguments" => ["mode" => $mirrorMappings[$orientation]]]); } } } } } if (is_array($transformations) && count($transformations) > 0) { foreach ($transformations as $transformation) { if (!empty($transformation)) { $arguments = array(); $mapping = self::$argumentMapping[$transformation["method"]]; if (is_array($transformation["arguments"])) { foreach ($transformation["arguments"] as $key => $value) { $position = array_search($key, $mapping); if ($position !== false) { // high res calculations if enabled if (!in_array($transformation["method"], ["cropPercent"]) && in_array($key, array("width", "height", "x", "y"))) { if ($config->getHighResolution() && $config->getHighResolution() > 1) { $value *= $config->getHighResolution(); } } $arguments[$position] = $value; } } } ksort($arguments); call_user_func_array(array($image, $transformation["method"]), $arguments); } } } $image->save($fsPath, $format, $config->getQuality()); if ($contentOptimizedFormat) { $tmpStoreKey = str_replace(PIMCORE_TEMPORARY_DIRECTORY . "/", "", $fsPath); TmpStore::add($tmpStoreKey, "-", "image-optimize-queue"); } clearstatcache(); \Logger::debug("Thumbnail " . $path . " generated in " . (StopWatch::microtime_float() - $startTime) . " seconds"); // set proper permissions @chmod($fsPath, File::getDefaultMode()); // quick bugfix / workaround, it seems that imagemagick / image optimizers creates sometimes empty PNG chunks (total size 33 bytes) // no clue why it does so as this is not continuous reproducible, and this is the only fix we can do for now // if the file is corrupted the file will be created on the fly when requested by the browser (because it's deleted here) if (is_file($fsPath) && filesize($fsPath) < 50) { unlink($fsPath); } return $path; }
/** * @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) { // only name if (!empty($meta["idName"]) && !empty($meta["idValue"]) && !empty($meta["contentValue"])) { $method = "append" . ucfirst($meta["idName"]); $this->view->headMeta()->{$method}($meta["idValue"], $meta["contentValue"]); } } } } } } // 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(); if (\Pimcore\Tool\Frontend::isDocumentInCurrentSite($hardlinkCanonicalSourceDocument)) { $this->getResponse()->setHeader("Link", '<' . $request->getScheme() . "://" . $request->getHttpHost() . $hardlinkCanonicalSourceDocument->getFullPath() . '>; 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; }
/** * Checks for a suitable redirect * @throws Exception * @param bool $override * @return void */ protected function checkForRedirect($override = false) { // not for admin requests if (Tool::isFrontentRequestByAdmin()) { return; } try { $front = \Zend_Controller_Front::getInstance(); $config = Config::getSystemConfig(); // get current site if available $sourceSite = null; if (Site::isSiteRequest()) { $sourceSite = Site::getCurrentSite(); } $cacheKey = "system_route_redirect"; if (empty($this->redirects) && !($this->redirects = Cache::load($cacheKey))) { $list = new Redirect\Listing(); $list->setOrder("DESC"); $list->setOrderKey("priority"); $this->redirects = $list->load(); Cache::save($this->redirects, $cacheKey, array("system", "redirect", "route"), null, 998); } $requestScheme = $_SERVER['HTTPS'] == 'on' ? \Zend_Controller_Request_Http::SCHEME_HTTPS : \Zend_Controller_Request_Http::SCHEME_HTTP; $matchRequestUri = $_SERVER["REQUEST_URI"]; $matchUrl = $requestScheme . "://" . $_SERVER["HTTP_HOST"] . $matchRequestUri; foreach ($this->redirects as $redirect) { $matchAgainst = $matchRequestUri; if ($redirect->getSourceEntireUrl()) { $matchAgainst = $matchUrl; } // if override is true the priority has to be 99 which means that overriding is ok if (!$override || $override && $redirect->getPriority() == 99) { if (@preg_match($redirect->getSource(), $matchAgainst, $matches)) { // check for a site if ($sourceSite) { if ($sourceSite->getId() != $redirect->getSourceSite()) { continue; } } array_shift($matches); $target = $redirect->getTarget(); if (is_numeric($target)) { $d = Document::getById($target); if ($d instanceof Document\Page || $d instanceof Document\Link || $d instanceof Document\Hardlink) { $target = $d->getFullPath(); } else { \Logger::error("Target of redirect no found (Document-ID: " . $target . ")!"); continue; } } // replace escaped % signs so that they didn't have effects to vsprintf (PIMCORE-1215) $target = str_replace("\\%", "###URLENCODE_PLACEHOLDER###", $target); $url = vsprintf($target, $matches); $url = str_replace("###URLENCODE_PLACEHOLDER###", "%", $url); // support for pcre backreferences $url = replace_pcre_backreferences($url, $matches); if ($redirect->getTargetSite() && !preg_match("@http(s)?://@i", $url)) { try { $targetSite = Site::getById($redirect->getTargetSite()); // if the target site is specified and and the target-path is starting at root (not absolute to site) // the root-path will be replaced so that the page can be shown $url = preg_replace("@^" . $targetSite->getRootPath() . "/@", "/", $url); $url = $requestScheme . "://" . $targetSite->getMainDomain() . $url; } catch (\Exception $e) { \Logger::error("Site with ID " . $redirect->getTargetSite() . " not found."); continue; } } else { if (!preg_match("@http(s)?://@i", $url) && $config->general->domain && $redirect->getSourceEntireUrl()) { // prepend the host and scheme to avoid infinite loops when using "domain" redirects $url = ($front->getRequest()->isSecure() ? "https" : "http") . "://" . $config->general->domain . $url; } } // pass-through parameters if specified $queryString = $_SERVER["QUERY_STRING"]; if ($redirect->getPassThroughParameters() && !empty($queryString)) { $glue = "?"; if (strpos($url, "?")) { $glue = "&"; } $url .= $glue; $url .= $queryString; } header($redirect->getHttpStatus()); header("Location: " . $url, true, $redirect->getStatusCode()); // log all redirects to the redirect log \Pimcore\Log\Simple::log("redirect", Tool::getAnonymizedClientIp() . " \t Custom-Redirect ID: " . $redirect->getId() . " , Source: " . $_SERVER["REQUEST_URI"] . " -> " . $url); exit; } } } } catch (\Exception $e) { // no suitable route found } }
/** * @static * @throws Exception|\Zend_Controller_Router_Exception */ public static function run() { self::setSystemRequirements(); // detect frontend (website) $frontend = Tool::isFrontend(); // enable the output-buffer, why? see in self::outputBufferStart() //if($frontend) { self::outputBufferStart(); //} self::initAutoloader(); self::initConfiguration(); self::setupFramework(); // config is loaded now init the real logger self::initLogger(); // initialize cache Cache::init(); // load plugins and modules (=core plugins) self::initModules(); self::initPlugins(); // init front controller $front = \Zend_Controller_Front::getInstance(); $conf = Config::getSystemConfig(); if (!$conf) { // redirect to installer if configuration isn't present if (!preg_match("/^\\/install.*/", $_SERVER["REQUEST_URI"])) { header("Location: /install/"); exit; } } if (self::inDebugMode() && $frontend && !$conf->general->disable_whoops && !defined("HHVM_VERSION")) { $whoops = new \Whoops\Run(); $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler()); $jsonErrorHandler = new \Whoops\Handler\JsonResponseHandler(); $jsonErrorHandler->onlyForAjaxRequests(true); $whoops->pushHandler($jsonErrorHandler); $whoops->register(); // add event handler before Pimcore::shutdown() to ensure fatal errors are handled by Whoops self::getEventManager()->attach("system.shutdown", array($whoops, "handleShutdown"), 10000); } $front->registerPlugin(new Controller\Plugin\ErrorHandler(), 1); $front->registerPlugin(new Controller\Plugin\Maintenance(), 2); // register general pimcore plugins for frontend if ($frontend) { $front->registerPlugin(new Controller\Plugin\Thumbnail(), 795); $front->registerPlugin(new Controller\Plugin\Less(), 799); $front->registerPlugin(new Controller\Plugin\AdminButton(), 806); } if (Tool::useFrontendOutputFilters(new \Zend_Controller_Request_Http())) { $front->registerPlugin(new Controller\Plugin\HybridAuth(), 792); $front->registerPlugin(new Controller\Plugin\QrCode(), 793); $front->registerPlugin(new Controller\Plugin\CommonFilesFilter(), 794); $front->registerPlugin(new Controller\Plugin\WysiwygAttributes(), 796); $front->registerPlugin(new Controller\Plugin\Webmastertools(), 797); $front->registerPlugin(new Controller\Plugin\Analytics(), 798); $front->registerPlugin(new Controller\Plugin\TagManagement(), 804); $front->registerPlugin(new Controller\Plugin\Targeting(), 805); $front->registerPlugin(new Controller\Plugin\EuCookieLawNotice(), 807); $front->registerPlugin(new Controller\Plugin\HttpErrorLog(), 850); $front->registerPlugin(new Controller\Plugin\Cache(), 901); // for caching } self::initControllerFront($front); // set router $router = $front->getRouter(); $routeAdmin = new \Zend_Controller_Router_Route('admin/:controller/:action/*', array('module' => 'admin', "controller" => "index", "action" => "index")); $routeInstall = new \Zend_Controller_Router_Route('install/:controller/:action/*', array('module' => 'install', "controller" => "index", "action" => "index")); $routeUpdate = new \Zend_Controller_Router_Route('admin/update/:controller/:action/*', array('module' => 'update', "controller" => "index", "action" => "index")); $routePlugins = new \Zend_Controller_Router_Route('admin/plugin/:controller/:action/*', array('module' => 'pluginadmin', "controller" => "index", "action" => "index")); $routeExtensions = new \Zend_Controller_Router_Route('admin/extensionmanager/:controller/:action/*', array('module' => 'extensionmanager', "controller" => "index", "action" => "index")); $routeReports = new \Zend_Controller_Router_Route('admin/reports/:controller/:action/*', array('module' => 'reports', "controller" => "index", "action" => "index")); $routePlugin = new \Zend_Controller_Router_Route('plugin/:module/:controller/:action/*', array("controller" => "index", "action" => "index")); $routeWebservice = new \Zend_Controller_Router_Route('webservice/:controller/:action/*', array("module" => "webservice", "controller" => "index", "action" => "index")); $routeSearchAdmin = new \Zend_Controller_Router_Route('admin/search/:controller/:action/*', array("module" => "searchadmin", "controller" => "index", "action" => "index")); // website route => custom router which check for a suitable document $routeFrontend = new Controller\Router\Route\Frontend(); $router->addRoute('default', $routeFrontend); // only do this if not frontend => performance issue if (!$frontend) { $router->addRoute("install", $routeInstall); $router->addRoute('plugin', $routePlugin); $router->addRoute('admin', $routeAdmin); $router->addRoute('update', $routeUpdate); $router->addRoute('plugins', $routePlugins); $router->addRoute('extensionmanager', $routeExtensions); $router->addRoute('reports', $routeReports); $router->addRoute('searchadmin', $routeSearchAdmin); if ($conf instanceof \Zend_Config and $conf->webservice and $conf->webservice->enabled) { $router->addRoute('webservice', $routeWebservice); } // check if this request routes into a plugin, if so check if the plugin is enabled if (preg_match("@^/plugin/([^/]+)/.*@", $_SERVER["REQUEST_URI"], $matches)) { $pluginName = $matches[1]; if (!Pimcore\ExtensionManager::isEnabled("plugin", $pluginName)) { \Pimcore\Tool::exitWithError("Plugin is disabled. To use this plugin please enable it in the extension manager!"); } } // force the main (default) domain for "admin" requests if ($conf->general->domain && $conf->general->domain != Tool::getHostname()) { $url = ($_SERVER['HTTPS'] == "on" ? "https" : "http") . "://" . $conf->general->domain . $_SERVER["REQUEST_URI"]; header("HTTP/1.1 301 Moved Permanently"); header("Location: " . $url, true, 301); exit; } } // check if webdav is configured and add router if ($conf instanceof \Zend_Config) { if ($conf->assets->webdav->hostname) { $routeWebdav = new \Zend_Controller_Router_Route_Hostname($conf->assets->webdav->hostname, array("module" => "admin", 'controller' => 'asset', 'action' => 'webdav')); $router->addRoute('webdav', $routeWebdav); } } $front->setRouter($router); self::getEventManager()->trigger("system.startup", $front); // throw exceptions also when in preview or in editmode (documents) to see it immediately when there's a problem with this page $throwExceptions = false; if (Tool::isFrontentRequestByAdmin()) { $user = \Pimcore\Tool\Authentication::authenticateSession(); if ($user instanceof User) { $throwExceptions = true; } } // run dispatcher // this is also standard for /admin/ requests -> error handling is done in Pimcore_Controller_Action_Admin if (!PIMCORE_DEBUG && !$throwExceptions && !PIMCORE_DEVMODE) { @ini_set("display_errors", "Off"); @ini_set("display_startup_errors", "Off"); $front->dispatch(); } else { @ini_set("display_errors", "On"); @ini_set("display_startup_errors", "On"); $front->throwExceptions(true); try { $front->dispatch(); } catch (\Zend_Controller_Router_Exception $e) { if (!headers_sent()) { header("HTTP/1.0 404 Not Found"); } \Logger::err($e); throw new \Zend_Controller_Router_Exception("No route, document, custom route or redirect is matching the request: " . $_SERVER["REQUEST_URI"] . " | \n" . "Specific ERROR: " . $e->getMessage()); } catch (\Exception $e) { if (!headers_sent()) { header("HTTP/1.0 500 Internal Server Error"); } throw $e; } } }
/** * @param array $urlOptions * @param null $name * @param bool $reset * @param bool $encode * @return string|void * @throws \Exception */ public function url(array $urlOptions = array(), $name = null, $reset = false, $encode = true) { if (!$urlOptions) { $urlOptions = array(); } // when using $name = false we don't use the default route (happens when $name = null / ZF default behavior) // but just the query string generation using the given parameters // eg. $this->url(["foo" => "bar"], false) => /?foo=bar if ($name === null) { if (Staticroute::getCurrentRoute() instanceof Staticroute) { $name = Staticroute::getCurrentRoute()->getName(); } } $siteId = null; if (Site::isSiteRequest()) { $siteId = Site::getCurrentSite()->getId(); } // check for a site in the options, if valid remove it from the options $hostname = null; if (isset($urlOptions["site"])) { $config = Config::getSystemConfig(); $site = $urlOptions["site"]; if (!empty($site)) { try { $site = Site::getBy($site); unset($urlOptions["site"]); $hostname = $site->getMainDomain(); $siteId = $site->getId(); } catch (\Exception $e) { \Logger::warn("passed site doesn't exists"); \Logger::warn($e); } } else { if ($config->general->domain) { $hostname = $config->general->domain; } } } if ($name && ($route = Staticroute::getByName($name, $siteId))) { // assemble the route / url in Staticroute::assemble() $url = $route->assemble($urlOptions, $reset, $encode); // if there's a site, prepend the host to the generated URL if ($hostname && !preg_match("/^http/i", $url)) { $url = "//" . $hostname . $url; } if (Config::getSystemConfig()->documents->allowcapitals == 'no') { $urlParts = parse_url($url); $url = str_replace($urlParts["path"], strtolower($urlParts["path"]), $url); } return $url; } // this is to add support for arrays as values for the default \Zend_View_Helper_Url $unset = array(); foreach ($urlOptions as $optionName => $optionValues) { if (is_array($optionValues)) { foreach ($optionValues as $key => $value) { $urlOptions[$optionName . "[" . $key . "]"] = $value; } $unset[] = $optionName; } } foreach ($unset as $optionName) { unset($urlOptions[$optionName]); } try { return parent::url($urlOptions, $name, $reset, $encode); } catch (\Exception $e) { if (Tool::isFrontentRequestByAdmin()) { // routes can be site specific, so in editmode it's possible that we don't get // the right route (sites are not registered in editmode), so we cannot throw exceptions there return "ERROR_IN_YOUR_URL_CONFIGURATION:~ROUTE--" . $name . "--DOES_NOT_EXIST"; } throw new \Exception("Route '" . $name . "' for building the URL not found"); } }
/** * @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; }
/** * Get the height of the generated thumbnail image in pixels. * @return string HTTP Mime Type of the generated thumbnail image. */ public function getMimeType() { if (!$this->mimetype) { // get target mime type without actually generating the thumbnail (deferred) $mapping = ["png" => "image/png", "jpg" => "image/jpeg", "jpeg" => "image/jpeg", "pjpeg" => "image/jpeg", "gif" => "image/gif", "tiff" => "image/tiff", "svg" => "image/svg+xml"]; $targetFormat = strtolower($this->getConfig()->getFormat()); $format = $targetFormat; $fileExt = \Pimcore\File::getFileExtension($this->getAsset()->getFilename()); if ($targetFormat == "source" || empty($targetFormat)) { $format = Thumbnail\Processor::getAllowedFormat($fileExt, ["jpeg", "gif", "png"], "png"); } elseif ($targetFormat == "print") { $format = Thumbnail\Processor::getAllowedFormat($fileExt, ["svg", "jpeg", "png", "tiff"], "png"); if (($format == "tiff" || $format == "svg") && \Pimcore\Tool::isFrontentRequestByAdmin()) { // return a webformat in admin -> tiff cannot be displayed in browser $format = "png"; } } if (array_key_exists($format, $mapping)) { $this->mimetype = $mapping[$format]; } else { // unknown $this->mimetype = "application/octet-stream"; } } return $this->mimetype; }
/** * @param $asset * @param Config $config * @param null $fileSystemPath * @param bool $deferred deferred means that the image will be generated on-the-fly (details see below) * @param bool $returnAbsolutePath * @param bool $generated * @return mixed|string */ public static function process($asset, Config $config, $fileSystemPath = null, $deferred = false, $returnAbsolutePath = false, &$generated = false) { $generated = false; $errorImage = PIMCORE_PATH . "/static6/img/filetype-not-supported.png"; $format = strtolower($config->getFormat()); $contentOptimizedFormat = false; if (!$fileSystemPath && $asset instanceof Asset) { $fileSystemPath = $asset->getFileSystemPath(); } if ($asset instanceof Asset) { $id = $asset->getId(); } else { $id = "dyn~" . crc32($fileSystemPath); } $fileExt = File::getFileExtension(basename($fileSystemPath)); // simple detection for source type if SOURCE is selected if ($format == "source" || empty($format)) { $format = self::getAllowedFormat($fileExt, ["jpeg", "gif", "png"], "png"); $contentOptimizedFormat = true; // format can change depending of the content (alpha-channel, ...) } if ($format == "print") { $format = self::getAllowedFormat($fileExt, ["svg", "jpeg", "png", "tiff"], "png"); if (($format == "tiff" || $format == "svg") && \Pimcore\Tool::isFrontentRequestByAdmin()) { // return a webformat in admin -> tiff cannot be displayed in browser $format = "png"; $deferred = false; // deferred is default, but it's not possible when using isFrontentRequestByAdmin() } elseif ($format == "tiff") { $transformations = $config->getItems(); if (is_array($transformations) && count($transformations) > 0) { foreach ($transformations as $transformation) { if (!empty($transformation)) { if ($transformation["method"] == "tifforiginal") { return self::returnPath($fileSystemPath, $returnAbsolutePath); } } } } } elseif ($format == "svg") { return self::returnPath($fileSystemPath, $returnAbsolutePath); } } elseif ($format == "tiff") { if (\Pimcore\Tool::isFrontentRequestByAdmin()) { // return a webformat in admin -> tiff cannot be displayed in browser $format = "png"; $deferred = false; // deferred is default, but it's not possible when using isFrontentRequestByAdmin() } } $thumbDir = $asset->getImageThumbnailSavePath() . "/thumb__" . $config->getName(); $filename = preg_replace("/\\." . preg_quote(File::getFileExtension($asset->getFilename())) . "/", "", $asset->getFilename()); // add custom suffix if available if ($config->getFilenameSuffix()) { $filename .= "~-~" . $config->getFilenameSuffix(); } // add high-resolution modifier suffix to the filename if ($config->getHighResolution() > 1) { $filename .= "@" . $config->getHighResolution() . "x"; } $fileExtension = $format; if ($format == "original") { $fileExtension = \Pimcore\File::getFileExtension($fileSystemPath); } $filename .= "." . $fileExtension; $fsPath = $thumbDir . "/" . $filename; // deferred means that the image will be generated on-the-fly (when requested by the browser) // the configuration is saved for later use in Pimcore\Controller\Plugin\Thumbnail::routeStartup() // so that it can be used also with dynamic configurations if ($deferred) { // only add the config to the TmpStore if necessary (the config is auto-generated) if (!Config::getByName($config->getName())) { $configId = "thumb_" . $id . "__" . md5(str_replace(PIMCORE_TEMPORARY_DIRECTORY, "", $fsPath)); TmpStore::add($configId, $config, "thumbnail_deferred"); } return self::returnPath($fsPath, $returnAbsolutePath); } // all checks on the file system should be below the deferred part for performance reasons (remote file systems) if (!file_exists($fileSystemPath)) { return self::returnPath($errorImage, $returnAbsolutePath); } if (!is_dir(dirname($fsPath))) { File::mkdir(dirname($fsPath)); } $path = self::returnPath($fsPath, false); // check for existing and still valid thumbnail if (is_file($fsPath) and filemtime($fsPath) >= filemtime($fileSystemPath)) { return self::returnPath($fsPath, $returnAbsolutePath); } // transform image $image = Asset\Image::getImageTransformInstance(); $image->setPreserveColor($config->isPreserveColor()); $image->setPreserveMetaData($config->isPreserveMetaData()); if (!$image->load($fileSystemPath)) { return self::returnPath($errorImage, $returnAbsolutePath); } $image->setUseContentOptimizedFormat($contentOptimizedFormat); $startTime = StopWatch::microtime_float(); $transformations = $config->getItems(); // check if the original image has an orientation exif flag // if so add a transformation at the beginning that rotates and/or mirrors the image if (function_exists("exif_read_data")) { $exif = @exif_read_data($fileSystemPath); if (is_array($exif)) { if (array_key_exists("Orientation", $exif)) { $orientation = intval($exif["Orientation"]); if ($orientation > 1) { $angleMappings = [2 => 180, 3 => 180, 4 => 180, 5 => 90, 6 => 90, 7 => 90, 8 => 270]; if (array_key_exists($orientation, $angleMappings)) { array_unshift($transformations, ["method" => "rotate", "arguments" => ["angle" => $angleMappings[$orientation]]]); } // values that have to be mirrored, this is not very common, but should be covered anyway $mirrorMappings = [2 => "vertical", 4 => "horizontal", 5 => "vertical", 7 => "horizontal"]; if (array_key_exists($orientation, $mirrorMappings)) { array_unshift($transformations, ["method" => "mirror", "arguments" => ["mode" => $mirrorMappings[$orientation]]]); } } } } } if (is_array($transformations) && count($transformations) > 0) { $sourceImageWidth = PHP_INT_MAX; $sourceImageHeight = PHP_INT_MAX; if ($asset instanceof Asset\Image) { $sourceImageWidth = $asset->getWidth(); $sourceImageHeight = $asset->getHeight(); } $highResFactor = $config->getHighResolution(); $calculateMaxFactor = function ($factor, $original, $new) { $newFactor = $factor * $original / $new; if ($newFactor < 1) { // don't go below factor 1 $newFactor = 1; } return $newFactor; }; // sorry for the goto/label - but in this case it makes life really easier and the code more readable prepareTransformations: foreach ($transformations as $transformation) { if (!empty($transformation)) { $arguments = []; $mapping = self::$argumentMapping[$transformation["method"]]; if (is_array($transformation["arguments"])) { foreach ($transformation["arguments"] as $key => $value) { $position = array_search($key, $mapping); if ($position !== false) { // high res calculations if enabled if (!in_array($transformation["method"], ["cropPercent"]) && in_array($key, ["width", "height", "x", "y"])) { if ($highResFactor && $highResFactor > 1) { $value *= $highResFactor; $value = (int) ceil($value); // check if source image is big enough otherwise adjust the high-res factor if (in_array($key, ["width", "x"])) { if ($sourceImageWidth < $value) { $highResFactor = $calculateMaxFactor($highResFactor, $sourceImageWidth, $value); goto prepareTransformations; } } elseif (in_array($key, ["height", "y"])) { if ($sourceImageHeight < $value) { $highResFactor = $calculateMaxFactor($highResFactor, $sourceImageHeight, $value); goto prepareTransformations; } } } } $arguments[$position] = $value; } } } ksort($arguments); if (method_exists($image, $transformation["method"])) { call_user_func_array([$image, $transformation["method"]], $arguments); } } } } $image->save($fsPath, $format, $config->getQuality()); $generated = true; if ($contentOptimizedFormat) { $tmpStoreKey = str_replace(PIMCORE_TEMPORARY_DIRECTORY . "/", "", $fsPath); TmpStore::add($tmpStoreKey, "-", "image-optimize-queue"); } clearstatcache(); Logger::debug("Thumbnail " . $path . " generated in " . (StopWatch::microtime_float() - $startTime) . " seconds"); // set proper permissions @chmod($fsPath, File::getDefaultMode()); // quick bugfix / workaround, it seems that imagemagick / image optimizers creates sometimes empty PNG chunks (total size 33 bytes) // no clue why it does so as this is not continuous reproducible, and this is the only fix we can do for now // if the file is corrupted the file will be created on the fly when requested by the browser (because it's deleted here) if (is_file($fsPath) && filesize($fsPath) < 50) { unlink($fsPath); } return self::returnPath($fsPath, $returnAbsolutePath); }