/** * Reads a template from the filesystem * * @param string $strName * @param string $strSection * @param bool $bitForce Force the passed template name, not adding the current area * @param bool $bitThrowErrors If set true, the method throws exceptions in case of errors * * @return string The identifier for further actions * @throws class_exception */ public function readTemplate($strName, $strSection = "", $bitForce = false, $bitThrowErrors = false) { //avoid directory traversals $strName = removeDirectoryTraversals($strName); if (!$bitForce) { try { $strName = class_resourceloader::getInstance()->getTemplate($strName); } catch (class_exception $objEx) { //try to resolve the file in the current skin $strName = class_resourceloader::getInstance()->getTemplate($strName, true); } } $bitKnownTemplate = false; //Is this template already in the cache? $strCacheTemplate = md5($strName); $strCacheSection = md5($strName . $strSection); if (isset($this->arrCacheTemplateSections[$strCacheSection])) { return $strCacheSection; } $this->bitSaveToCacheRequired = true; if (isset($this->arrCacheTemplates[$strCacheTemplate])) { $bitKnownTemplate = true; } if (!$bitKnownTemplate) { //We have to read the whole template from the filesystem if (uniSubstr($strName, -4) == ".tpl" && is_file(_realpath_ . "/" . $strName)) { $strTemplate = file_get_contents(_realpath_ . "/" . $strName); //Saving to the cache $this->arrCacheTemplates[$strCacheTemplate] = $strTemplate; } else { $strTemplate = "Template " . $strName . " not found!"; if ($bitThrowErrors) { throw new class_exception("Template " . $strName . " not found!", class_exception::$level_FATALERROR); } } } else { $strTemplate = $this->arrCacheTemplates[$strCacheTemplate]; } //Now we have to extract the section if ($strSection != "") { //find opening tag $intStart = uniStrpos($strTemplate, "<" . $strSection . ">"); //find closing tag $intEnd = uniStrpos($strTemplate, "</" . $strSection . ">"); $intEnd = $intEnd - $intStart; if ($intStart !== false && $intEnd !== false) { //delete substring before and after $strTemplate = uniSubstr($strTemplate, $intStart, $intEnd); $strTemplate = str_replace("<" . $strSection . ">", "", $strTemplate); $strTemplate = str_replace("</" . $strSection . ">", "", $strTemplate); } else { $strTemplate = ""; } } //Saving the section to the cache $this->arrCacheTemplateSections[$strCacheSection] = $strTemplate; return $strCacheSection; }
/** * Save the image to a file. * * Calling this method will actually start the image processing, * if no cached image is available. * * @param string $strPath * @param string $strFormat * @return bool */ public function save($strPath, $strFormat = null) { $strPath = removeDirectoryTraversals($strPath); if ($strFormat == null) { $strFormat = self::getFormatFromFilename($strPath); } if (!$this->isCached($strFormat)) { if ($this->processImage($strFormat)) { return $this->outputImage($strFormat, $strPath); } else { return false; } } else { $strCacheFile = $this->getCachePath($strFormat); if (!file_exists(_realpath_ . $strPath) || filemtime(_realpath_ . $strCacheFile) > filemtime(_realpath_ . $strPath)) { return copy(_realpath_ . $strCacheFile, _realpath_ . $strPath); } return true; } }
/** * Looks up the real filename of a template passed. * The filename is the relative path, so adding /templates/[packname] is not required and not allowed. * * @param string $strTemplateName * @param bool $bitScanAdminSkin * * @throws class_exception * @return string The path on the filesystem, relative to the root-folder. Null if the file could not be mapped. */ public function getTemplate($strTemplateName, $bitScanAdminSkin = false) { $strTemplateName = removeDirectoryTraversals($strTemplateName); if (isset($this->arrTemplates[$strTemplateName])) { return $this->arrTemplates[$strTemplateName]; } $this->bitCacheSaveRequired = true; $strFilename = null; //first try: load the file in the current template-pack $strDefaultTemplate = class_module_system_setting::getConfigValue("_packagemanager_defaulttemplate_"); if (is_file(_realpath_ . _templatepath_ . "/" . $strDefaultTemplate . "/tpl" . $strTemplateName)) { $this->arrTemplates[$strTemplateName] = _templatepath_ . "/" . $strDefaultTemplate . "/tpl" . $strTemplateName; return _templatepath_ . "/" . $strDefaultTemplate . "/tpl" . $strTemplateName; } //second try: load the file from the default-pack if (is_file(_realpath_ . _templatepath_ . "/default/tpl" . $strTemplateName)) { $this->arrTemplates[$strTemplateName] = _templatepath_ . "/default/tpl" . $strTemplateName; return _templatepath_ . "/default/tpl" . $strTemplateName; } //third try: try to load the file from a given module foreach ($this->arrModules as $strCorePath => $strOneModule) { if (is_file(_realpath_ . "/" . $strCorePath . "/templates/default/tpl" . $strTemplateName)) { $strFilename = "/" . $strCorePath . "/templates/default/tpl" . $strTemplateName; break; } } if ($bitScanAdminSkin) { if (is_file(_realpath_ . class_adminskin_helper::getPathForSkin(class_session::getInstance()->getAdminSkin()) . $strTemplateName)) { $strFilename = class_adminskin_helper::getPathForSkin(class_session::getInstance()->getAdminSkin()) . $strTemplateName; } } if ($strFilename === null) { throw new class_exception("Required file " . $strTemplateName . " could not be mapped on the filesystem.", class_exception::$level_ERROR); } $this->arrTemplates[$strTemplateName] = $strFilename; return $strFilename; }
/** * Saves an image to the filesystem * * @param string $strTarget * @param bool $bitCache * @param int $intJpegQuality * * @return bool */ public function saveImage($strTarget = "", $bitCache = false, $intJpegQuality = 90) { $bitReturn = false; if ($this->bitNeedToSave) { if (!is_numeric($intJpegQuality) || $intJpegQuality < 0) { $intJpegQuality = 90; } if ($strTarget != "") { $strTarget = removeDirectoryTraversals($strTarget); } if ($bitCache) { $this->generateCachename(); $strTarget = $this->strCachepath . $this->strCachename; } //get file type if ($strTarget != "") { $strType = uniSubstr($strTarget, uniStrrpos($strTarget, ".")); } else { $strType = $this->strType; } if ($strTarget == "") { $strTarget = "/" . $this->strCachepath . $this->generateCachename(0, 0, $intJpegQuality); } switch (uniStrtolower($strType)) { case ".jpg": $bitReturn = imagejpeg($this->objImage, _realpath_ . $strTarget, $intJpegQuality); break; case ".png": $bitReturn = imagepng($this->objImage, _realpath_ . $strTarget); break; case ".gif": $bitReturn = imagegif($this->objImage, _realpath_ . $strTarget); break; } } else { $bitReturn = true; } if (!$bitReturn) { class_logger::getInstance()->addLogRow("error saving file to " . $strTarget, class_logger::$levelWarning); } return $bitReturn; }