예제 #1
0
 /**
  * Get a module by its codename.
  *
  * @param string $codename the codename of the module to get
  * @return CMS_module The module wanted
  * @access public
  */
 static function getByCodename($datas)
 {
     static $modules;
     if (is_string($datas)) {
         $codename = $datas;
         if (!$codename) {
             parent::raiseError("Codename is null");
             return false;
         }
         if (isset($modules[$codename])) {
             return $modules[$codename];
         }
         //test the codename to see if it is valid
         if ($codename != SensitiveIO::sanitizeAsciiString($codename)) {
             parent::raiseError("Codename is not valid");
             return false;
         }
         //Try to instanciate a module named CMS_module_CODENAME
         $class_name = "CMS_module_" . $codename;
         if (class_exists($class_name)) {
             $modules[$codename] = new $class_name($codename);
             return $modules[$codename];
         } elseif (CMS_modulesCatalog::isPolymod($codename)) {
             $modules[$codename] = new CMS_polymod($codename);
             return $modules[$codename];
         } else {
             parent::raiseError("Unknown codename : " . $codename);
             return false;
         }
     } elseif (is_array($datas)) {
         $codename = isset($datas['codename_mod']) ? $datas['codename_mod'] : '';
         if (!$codename) {
             parent::raiseError("Codename is null");
             return false;
         }
         if (isset($modules[$codename])) {
             return $modules[$codename];
         }
         //test the codename to see if it is valid
         if ($codename != SensitiveIO::sanitizeAsciiString($codename)) {
             parent::raiseError("Codename is not valid");
             return false;
         }
         //Try to instanciate a module named CMS_module_CODENAME
         $class_name = "CMS_module_" . $codename;
         if (class_exists($class_name)) {
             $modules[$codename] = new $class_name($datas);
             return $modules[$codename];
         } elseif ($datas['isPolymod_mod']) {
             $modules[$codename] = new CMS_polymod($datas);
             return $modules[$codename];
         } else {
             parent::raiseError("Unknown codename : " . $codename);
             return false;
         }
     } else {
         parent::raiseError("Unknown datas type : " . gettype($datas));
         return false;
     }
 }
예제 #2
0
 /**
  * Get the filename and optionnaly path of a file given its original name
  * Cleans the name and add the directory where files should reside (when page is un USERSPACE location)
  *
  * @param string $originalName The original name of the file
  * @param CMS_page &$page The page which contains the block
  * @param string &$clientspace The clientspace which contains the block
  * @param string &$row The row which contains the block
  * @param string &$block The block
  * @param boolean $withPath If false, only the filename will be returned
  * @param boolean $isEnlarged Is it the enlarged image we want the path of ?
  * @return string The full pathname
  * @access private
  */
 function getFilePath($originalName, &$page, &$clientspace, &$row, &$block, $withPath = true, $isEnlarged = false)
 {
     $name = md5(mt_rand() . microtime());
     $name .= SensitiveIO::sanitizeAsciiString($originalName);
     $name = "p" . $page->getID() . "_" . $name;
     if (io::strlen($name) > 255) {
         $name = sensitiveIO::ellipsis($name, 255, '-', true);
     }
     if ($withPath) {
         return PATH_MODULES_FILES_STANDARD_FS . "/edition/" . $name;
     } else {
         return $name;
     }
 }
예제 #3
0
 /**
  * Sets the definition from a string. Must write the definition to file and try to parse it
  * The file must be in a specific directory : PATH_TEMPLATES_FS (see constants from rc file)
  *
  * @param string $definition The definition
  * @return boolean true on success, false on failure
  * @access public
  */
 function setDefinition($definition)
 {
     $filename = $this->getDefinitionFile();
     if (!$filename) {
         //must write it to persistence to have its ID
         if (!$this->_id) {
             $this->writeToPersistence();
         }
         //build the filename
         $filename = 'pt' . $this->_id . '_' . SensitiveIO::sanitizeAsciiString($this->_label) . '.xml';
     } else {
         //save old definition (before replacement)
         $old_definition = @file_get_contents(PATH_TEMPLATES_FS . "/" . $filename);
     }
     $file = new CMS_file(PATH_TEMPLATES_FS . "/" . $filename);
     $file->setContent($definition);
     if (!$file->writeToPersistence()) {
         $this->raiseError("Can't write definition file : " . PATH_TEMPLATES_FS . "/" . $filename);
         return false;
     } else {
         $this->_definitionFile = $filename;
     }
     //then parse file to get modules and CS datas
     $modulesTreatment = new CMS_modulesTags(MODULE_TREATMENT_CLIENTSPACE_TAGS, PAGE_VISUALMODE_HTML_EDITED, $this);
     $error = $this->_parseDefinitionFile($modulesTreatment, self::CONVERT_TO_AUTOMNE);
     if ($error !== true) {
         if (isset($old_definition) && $old_definition) {
             $file = new CMS_file(PATH_TEMPLATES_FS . "/" . $filename);
             $file->setContent($old_definition);
             $file->writeToPersistence();
         }
         return $error;
     }
     return true;
 }
예제 #4
0
 /**
  * Get POST vars from a form formatted by such a CMS_dialog_href class
  * and build a CMS_href
  *
  * Uses : _POST['link'] : array $link, the $_POST['link'] built by submiting
  * the form data : array ( 'internal'=>, 'external'=>, 'file'=>,
  * 'edit_file'=>, 'popup'=> array ('width'=>,'height'=>), 'type'=> )
  * @param  string $module, the module concerned by this link
  * @param integer $resourceID, ID to prepend the filename uploaded with
  * @param integer $fieldID, optional field ID to surcharge file name representation ("r".$resourceID."_f".$fieldID."_")
  * @return boolean true on success, false on failure
  * @access public
  */
 function doPost($module = MOD_STANDARD_CODENAME, $resourceID, $fieldID = '')
 {
     $linkLabel = isset($_POST[$this->_prefix . 'link_label']) ? $_POST[$this->_prefix . 'link_label'] : '';
     $linkType = isset($_POST[$this->_prefix . 'link_type']) ? $_POST[$this->_prefix . 'link_type'] : '';
     $internalLink = isset($_POST[$this->_prefix . 'link_internal']) ? $_POST[$this->_prefix . 'link_internal'] : '';
     $externalLink = isset($_POST[$this->_prefix . 'link_external']) ? $_POST[$this->_prefix . 'link_external'] : '';
     $this->_href->setLabel($linkLabel);
     $this->_href->setLinkType($linkType);
     $this->_href->setInternalLink($internalLink);
     $this->_href->setExternalLink($externalLink);
     // Delete/Upload file
     if (isset($_POST[$this->_prefix . 'link_edit_linkfile']) && $_POST[$this->_prefix . 'link_edit_linkfile'] > 0) {
         switch ($module) {
             case MOD_STANDARD_CODENAME:
                 $locationType = RESOURCE_DATA_LOCATION_EDITION;
                 $uniqueName = md5(serialize($this) . microtime());
                 $fileprefix = $fieldID ? 'p' . $resourceID . '_' . $uniqueName . "_f" . $fieldID : 'p' . $resourceID . '_' . $uniqueName;
                 break;
             default:
                 $locationType = RESOURCE_DATA_LOCATION_EDITED;
                 $fileprefix = $fieldID ? 'r' . $resourceID . "_f" . $fieldID . "_" : 'r' . $resourceID . "_";
                 break;
         }
         //remove the old file if any
         if (is_file($this->_href->getFileLink(true, $module, $locationType, PATH_RELATIVETO_FILESYSTEM))) {
             if (!unlink($this->_href->getFileLink(true, $module, $locationType, PATH_RELATIVETO_FILESYSTEM))) {
                 $this->raiseError("Could not delete linked file");
             }
         }
         if ($_FILES[$this->_prefix . 'link_file']['name'] != '' && $resourceID > 0) {
             $path = $this->_href->getFileLink(true, $module, $locationType, PATH_RELATIVETO_FILESYSTEM, false);
             $filename = $fileprefix . SensitiveIO::sanitizeAsciiString($_FILES[$this->_prefix . 'link_file']['name']);
             //move uploaded file
             $fileDatas = CMS_file::uploadFile($this->_prefix . 'link_file', PATH_TMP_FS);
             if ($fileDatas['error']) {
                 return false;
             }
             if (!CMS_file::moveTo(PATH_TMP_FS . '/' . $fileDatas['filename'], $path . "/" . $filename)) {
                 return false;
             }
         } else {
             $filename = '';
         }
         $this->_href->setFileLink($filename);
     }
     // Target and Popup > (width, height)
     if (isset($_POST[$this->_prefix . 'link_target'])) {
         switch ($_POST[$this->_prefix . 'link_target']) {
             case "popup":
                 if ((int) $_POST[$this->_prefix . 'link_popup_width'] > 0 || (int) $_POST[$this->_prefix . 'link_popup_height'] > 0) {
                     $this->_href->setPopup($_POST[$this->_prefix . 'link_popup_width'], $_POST[$this->_prefix . 'link_popup_height']);
                 } else {
                     $this->_href->setPopup('', '');
                 }
                 break;
             case "top":
                 $this->_href->setTarget('_top');
                 $this->_href->setPopup('', '');
                 break;
             case "blank":
                 $this->_href->setTarget('_blank');
                 $this->_href->setPopup('', '');
                 break;
         }
     }
     return true;
 }
예제 #5
0
파일: row.php 프로젝트: davidmottet/automne
 /**
  * Sets the definition from a string. Must write the definition to file and try to parse it
  * The file must be in a specific directory : PATH_TEMPLATES_ROWS_FS (see constants from rc file)
  *
  * @param string $definition The definition
  * @param boolean $haltOnPolymodParsing Stop setting definition if error on polymod parsing are found (default : true)
  * @return boolean true on success, false on failure
  * @access public
  */
 function setDefinition($definition, $haltOnPolymodParsing = true)
 {
     global $cms_language;
     $defXML = new CMS_DOMDocument();
     try {
         $defXML->loadXML($definition);
     } catch (DOMException $e) {
         return $cms_language->getMessage(self::MESSAGE_PAGE_ROW_SYNTAX_ERROR, array($e->getMessage()));
     }
     $blocks = $defXML->getElementsByTagName('block');
     $modules = array();
     foreach ($blocks as $block) {
         if ($block->hasAttribute("module")) {
             $modules[] = $block->getAttribute("module");
         } else {
             return $cms_language->getMessage(self::MESSAGE_PAGE_ROW_SYNTAX_ERROR, array($cms_language->getMessage(self::MESSAGE_PAGE_BLOCK_SYNTAX_ERROR)));
         }
     }
     $modules = array_unique($modules);
     $this->_modules->emptyStack();
     foreach ($modules as $module) {
         $this->_modules->add($module);
     }
     //check if rows use a polymod block, if so pass to module for variables conversion
     $rowConverted = false;
     foreach ($this->getModules(false) as $moduleCodename) {
         if (CMS_modulesCatalog::isPolymod($moduleCodename)) {
             $rowConverted = true;
             $module = CMS_modulesCatalog::getByCodename($moduleCodename);
             $definition = $module->convertDefinitionString($definition, false);
         }
     }
     if ($rowConverted) {
         //check definition parsing
         $parsing = new CMS_polymod_definition_parsing($definition, true, CMS_polymod_definition_parsing::CHECK_PARSING_MODE);
         $errors = $parsing->getParsingError();
         if ($errors && $haltOnPolymodParsing) {
             return $cms_language->getMessage(self::MESSAGE_PAGE_ROW_SYNTAX_ERROR, array($errors));
         }
     }
     $filename = $this->getDefinitionFileName();
     if (!$filename) {
         //must write it to persistence to have its ID
         if (!$this->_id) {
             $this->writeToPersistence();
         }
         //build the filename
         $filename = "r" . $this->_id . "_" . SensitiveIO::sanitizeAsciiString($this->_label) . ".xml";
     }
     $rowFile = new CMS_file(PATH_TEMPLATES_ROWS_FS . "/" . $filename);
     $rowFile->setContent($definition);
     $rowFile->writeToPersistence();
     $this->_definitionFile = $filename;
     return true;
 }
예제 #6
0
 /**
  * Constructor.
  * initializes the module if the codename is given
  *
  * @param string $codename The module codename
  * @return void
  * @access public
  */
 function CMS_module($datas = '')
 {
     static $modules;
     if (is_string($datas)) {
         $codename = $datas;
         if ($codename) {
             if (isset($modules[$codename])) {
                 $this->_id = $modules[$codename]->_id;
                 $this->_labelMessageID = $modules[$codename]->_labelMessageID;
                 $this->_codename = $modules[$codename]->_codename;
                 $this->_administrationFrontend = $modules[$codename]->_administrationFrontend;
                 $this->_hasParameters = $modules[$codename]->_hasParameters;
                 $this->_isPolymod = $modules[$codename]->_isPolymod;
             } else {
                 $sql = "\n\t\t\t\t\t\tselect\n\t\t\t\t\t\t\t*\n\t\t\t\t\t\tfrom\n\t\t\t\t\t\t\tmodules\n\t\t\t\t\t\twhere\n\t\t\t\t\t\t\tcodename_mod='" . SensitiveIO::sanitizeSQLString($codename) . "'\n\t\t\t\t\t";
                 $q = new CMS_query($sql);
                 if ($q->getNumRows()) {
                     $data = $q->getArray();
                     $this->_id = $data["id_mod"];
                     $this->_labelMessageID = $data["label_mod"];
                     $this->_codename = $data["codename_mod"];
                     $this->_administrationFrontend = $data["administrationFrontend_mod"];
                     $this->_hasParameters = $data["hasParameters_mod"];
                     $this->_isPolymod = $data["isPolymod_mod"];
                     $modules[$codename] = $this;
                 } else {
                     $this->raiseError("Unknown codename : " . SensitiveIO::sanitizeAsciiString($codename));
                 }
             }
         }
     } else {
         if (is_array($datas)) {
             $codename = $datas["codename_mod"];
             $this->_id = $datas["id_mod"];
             $this->_labelMessageID = $datas["label_mod"];
             $this->_codename = $datas["codename_mod"];
             $this->_administrationFrontend = $datas["administrationFrontend_mod"];
             $this->_hasParameters = $datas["hasParameters_mod"];
             $this->_isPolymod = $datas["isPolymod_mod"];
             $modules[$codename] = $this;
         } else {
             parent::raiseError("Unknown datas type : " . gettype($datas));
             return false;
         }
     }
 }
예제 #7
0
     $template->setLog(false);
 }
 if (!$cms_message) {
     //description
     $template->setDescription($description);
     //remove the old file if any and if new one is different
     if ($image) {
         if (is_file(PATH_TEMPLATES_IMAGES_FS . '/' . $template->getImage()) && $image != PATH_TEMPLATES_IMAGES_WR . '/' . $template->getImage() && $template->getImage() != 'nopicto.gif') {
             unlink(PATH_TEMPLATES_IMAGES_FS . '/' . $template->getImage());
         }
     }
     if ($image && io::strpos($image, PATH_UPLOAD_WR . '/') !== false) {
         //move and rename uploaded file
         $image = str_replace(PATH_UPLOAD_WR . '/', PATH_UPLOAD_FS . '/', $image);
         $basename = pathinfo($image, PATHINFO_BASENAME);
         $movedImage = PATH_TEMPLATES_IMAGES_FS . '/' . SensitiveIO::sanitizeAsciiString($basename);
         CMS_file::moveTo($image, $movedImage);
         CMS_file::chmodFile(FILES_CHMOD, $movedImage);
         $image = pathinfo($movedImage, PATHINFO_BASENAME);
     } elseif ($template->getImage()) {
         //keep old file
         $image = $template->getImage();
     } else {
         $image = 'nopicto.gif';
     }
     $template->setImage($image);
     //groups
     $template->delAllGroups();
     foreach ($groups as $group) {
         $template->addGroup($group);
     }
예제 #8
0
 /**
  * set object Values
  *
  * @param array $values : the POST result values
  * @param string prefixname : the prefix used for post names
  * @param boolean newFormat : new automne v4 format (default false for compatibility)
  * @param integer $objectID : the current object id. Must be set, but default is blank for compatibility with other objects
  * @return boolean true on success, false on failure
  * @access public
  */
 function setValues($values, $prefixName, $newFormat = false, $objectID = '')
 {
     if (!sensitiveIO::isPositiveInteger($objectID)) {
         $this->raiseError('ObjectID must be a positive integer : ' . $objectID);
         return false;
     }
     //get field parameters
     $params = $this->getParamsValues();
     //get module codename
     $moduleCodename = CMS_poly_object_catalog::getModuleCodenameForField($this->_field->getID());
     if ($newFormat) {
         //delete old images ?
         //thumbnail
         if ($this->_subfieldValues[0]->getValue() && (!$values[$prefixName . $this->_field->getID() . '_0'] || pathinfo($values[$prefixName . $this->_field->getID() . '_0'], PATHINFO_BASENAME) != $this->_subfieldValues[0]->getValue())) {
             @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[0]->getValue());
             $this->_subfieldValues[0]->setValue('');
         }
         //image zoom
         if ($this->_subfieldValues[2]->getValue() && (!isset($values[$prefixName . $this->_field->getID() . '_2']) || !$values[$prefixName . $this->_field->getID() . '_2'] || pathinfo($values[$prefixName . $this->_field->getID() . '_2'], PATHINFO_BASENAME) != $this->_subfieldValues[2]->getValue())) {
             @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[2]->getValue());
             $this->_subfieldValues[2]->setValue('');
         }
         //set label from label field
         if (!$this->_subfieldValues[1]->setValue(io::htmlspecialchars($values[$prefixName . $this->_field->getID() . '_1']))) {
             return false;
         }
         //image zoom (if needed)
         if ((!isset($values[$prefixName . $this->_field->getID() . '_makeZoom']) || $values[$prefixName . $this->_field->getID() . '_makeZoom'] != 1) && isset($values[$prefixName . $this->_field->getID() . '_2']) && $values[$prefixName . $this->_field->getID() . '_2'] && io::strpos($values[$prefixName . $this->_field->getID() . '_2'], PATH_UPLOAD_WR . '/') !== false) {
             $filename = $values[$prefixName . $this->_field->getID() . '_2'];
             //check for image type before doing anything
             if (!in_array(io::strtolower(pathinfo($filename, PATHINFO_EXTENSION)), $this->_allowedExtensions)) {
                 return false;
             }
             //destroy old image if any
             if ($this->_subfieldValues[2]->getValue()) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[2]->getValue());
                 $this->_subfieldValues[2]->setValue('');
             }
             //move and rename uploaded file
             $filename = str_replace(PATH_UPLOAD_WR . '/', PATH_UPLOAD_FS . '/', $filename);
             $basename = pathinfo($filename, PATHINFO_BASENAME);
             //set thumbnail
             $path = PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED;
             $zoomBasename = "r" . $objectID . "_" . $this->_field->getID() . "_" . io::strtolower(SensitiveIO::sanitizeAsciiString($basename));
             if (io::strlen($zoomBasename) > 255) {
                 $zoomBasename = sensitiveIO::ellipsis($zoomBasename, 255, '-', true);
             }
             $zoomFilename = $path . '/' . $zoomBasename;
             CMS_file::moveTo($filename, $zoomFilename);
             CMS_file::chmodFile(FILES_CHMOD, $zoomFilename);
             //set it
             if (!$this->_subfieldValues[2]->setValue($zoomBasename)) {
                 return false;
             }
         }
         //thumbnail
         if ($values[$prefixName . $this->_field->getID() . '_0'] && io::strpos($values[$prefixName . $this->_field->getID() . '_0'], PATH_UPLOAD_WR . '/') !== false) {
             $filename = $values[$prefixName . $this->_field->getID() . '_0'];
             //check for image type before doing anything
             if (!in_array(io::strtolower(pathinfo($filename, PATHINFO_EXTENSION)), $this->_allowedExtensions)) {
                 return false;
             }
             //destroy old image if any
             if ($this->_subfieldValues[0]->getValue()) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[0]->getValue());
                 $this->_subfieldValues[0]->setValue('');
             }
             //move and rename uploaded file
             $filename = str_replace(PATH_UPLOAD_WR . '/', PATH_UPLOAD_FS . '/', $filename);
             $basename = pathinfo($filename, PATHINFO_BASENAME);
             //set thumbnail
             $path = PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED;
             $newBasename = "r" . $objectID . "_" . $this->_field->getID() . "_" . io::strtolower(SensitiveIO::sanitizeAsciiString($basename));
             //rename image
             $path_parts = pathinfo($newBasename);
             $extension = io::strtolower($path_parts['extension']);
             $newBasename = io::substr($path_parts['basename'], 0, -(io::strlen($extension) + 1)) . '_thumbnail.' . $extension;
             if (io::strlen($newBasename) > 255) {
                 $newBasename = sensitiveIO::ellipsis($newBasename, 255, '-', true);
             }
             $newFilename = $path . '/' . $newBasename;
             //move file from upload dir to new dir
             CMS_file::moveTo($filename, $newFilename);
             CMS_file::chmodFile(FILES_CHMOD, $newFilename);
             //if we use original image as image zoom, set it
             if (isset($values[$prefixName . $this->_field->getID() . '_makeZoom']) && $values[$prefixName . $this->_field->getID() . '_makeZoom'] == 1) {
                 $zoomFilename = str_replace('_thumbnail.' . $extension, '.' . $extension, $newFilename);
                 //copy image as zoom
                 CMS_file::copyTo($newFilename, $zoomFilename);
                 $zoomBasename = pathinfo($zoomFilename, PATHINFO_BASENAME);
                 //set image zoom
                 if (!$this->_subfieldValues[2]->setValue($zoomBasename)) {
                     return false;
                 }
             }
             //resize thumbnail if needed
             if ($params['maxWidth'] > 0 || $params['maxHeight'] > 0) {
                 $oImage = new CMS_image($newFilename);
                 //get current file size
                 $sizeX = $oImage->getWidth();
                 $sizeY = $oImage->getHeight();
                 //check thumbnail size
                 if ($params['maxWidth'] && $sizeX > $params['maxWidth'] || $params['maxHeight'] && $sizeY > $params['maxHeight']) {
                     $newSizeX = $sizeX;
                     $newSizeY = $sizeY;
                     // Check width
                     if ($params['maxWidth'] && $newSizeX > $params['maxWidth']) {
                         $newSizeY = round($params['maxWidth'] * $newSizeY / $newSizeX);
                         $newSizeX = $params['maxWidth'];
                     }
                     if ($params['maxHeight'] && $newSizeY > $params['maxHeight']) {
                         $newSizeX = round($params['maxHeight'] * $newSizeX / $newSizeY);
                         $newSizeY = $params['maxHeight'];
                     }
                     if (!$oImage->resize($newSizeX, $newSizeY, $newFilename)) {
                         return false;
                     }
                 }
             }
             //set thumbnail
             if (!$this->_subfieldValues[0]->setValue($newBasename)) {
                 return false;
             }
         }
         // If label not set yet, set it
         /*if(!$this->_subfieldValues[1]->getValue()){
         			if($this->_subfieldValues[0]->getValue()){
         				$this->_subfieldValues[1]->setValue($this->_subfieldValues[0]->getValue());
         			}
         		}*/
         //if we had an imagezoom, check his size
         if ($this->_subfieldValues[2]->getValue() && ($params['maxZoomWidth'] > 0 || $params['maxZoomHeight'] > 0)) {
             //resize zoom if needed
             $path = PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED;
             $basename = $this->_subfieldValues[2]->getValue();
             $filename = $path . '/' . $basename;
             $extension = io::strtolower(pathinfo($basename, PATHINFO_EXTENSION));
             $oImage = new CMS_image($filename);
             //get current file size
             $sizeX = $oImage->getWidth();
             $sizeY = $oImage->getHeight();
             //check zoom size
             if ($params['maxZoomWidth'] && $sizeX > $params['maxZoomWidth'] || $params['maxZoomHeight'] && $sizeY > $params['maxZoomHeight']) {
                 $newSizeX = $sizeX;
                 $newSizeY = $sizeY;
                 // Check width
                 if ($params['maxZoomWidth'] && $newSizeX > $params['maxZoomWidth']) {
                     $newSizeY = round($params['maxZoomWidth'] * $newSizeY / $newSizeX);
                     $newSizeX = $params['maxZoomWidth'];
                 }
                 if ($params['maxZoomHeight'] && $newSizeY > $params['maxZoomHeight']) {
                     $newSizeX = round($params['maxZoomHeight'] * $newSizeX / $newSizeY);
                     $newSizeY = $params['maxZoomHeight'];
                 }
                 if (!$oImage->resize($newSizeX, $newSizeY, $filename)) {
                     return false;
                 }
             }
         }
         //update files infos if needed
         if ($this->_subfieldValues[0]->getValue() && file_exists(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[0]->getValue())) {
             $file = new CMS_file(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[0]->getValue());
             $imageDatas = array('filename' => $file->getName(false), 'filepath' => $file->getFilePath(CMS_file::WEBROOT), 'filesize' => $file->getFileSize(), 'fileicon' => $file->getFileIcon(CMS_file::WEBROOT), 'extension' => $file->getExtension());
         } else {
             $imageDatas = array('filename' => '', 'filepath' => '', 'filesize' => '', 'fileicon' => '', 'extension' => '');
         }
         $imageDatas['module'] = $moduleCodename;
         $imageDatas['visualisation'] = RESOURCE_DATA_LOCATION_EDITED;
         if ($params['useDistinctZoom'] || $this->_subfieldValues[2]->getValue()) {
             //update files infos if needed
             if ($this->_subfieldValues[2]->getValue() && file_exists(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[2]->getValue())) {
                 $file = new CMS_file(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[2]->getValue());
                 $zoomDatas = array('filename' => $file->getName(false), 'filepath' => $file->getFilePath(CMS_file::WEBROOT), 'filesize' => $file->getFileSize(), 'fileicon' => $file->getFileIcon(CMS_file::WEBROOT), 'extension' => $file->getExtension());
             } else {
                 $zoomDatas = array('filename' => '', 'filepath' => '', 'filesize' => '', 'fileicon' => '', 'extension' => '');
             }
             $zoomDatas['module'] = $moduleCodename;
             $zoomDatas['visualisation'] = RESOURCE_DATA_LOCATION_EDITED;
         } else {
             $zoomDatas = '';
         }
         $content = array('datas' => array('polymodFieldsValue[' . $prefixName . $this->_field->getID() . '_0]' => $imageDatas, 'polymodFieldsValue[' . $prefixName . $this->_field->getID() . '_2]' => $zoomDatas, 'polymodFieldsValue[' . $prefixName . $this->_field->getID() . '_1]' => sensitiveIO::decodeEntities($this->_subfieldValues[1]->getValue())));
         $view = CMS_view::getInstance();
         $view->addContent($content);
         return true;
     } else {
         //Old format
         //delete old images ?
         if (isset($values[$prefixName . $this->_field->getID() . '_delete']) && $values[$prefixName . $this->_field->getID() . '_delete'] == 1) {
             if ($this->_subfieldValues[0]->getValue()) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[0]->getValue());
                 $this->_subfieldValues[0]->setValue('');
             } elseif (isset($values[$prefixName . $this->_field->getID() . '_0_hidden']) && $values[$prefixName . $this->_field->getID() . '_0_hidden']) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $values[$prefixName . $this->_field->getID() . '_0_hidden']);
                 $this->_subfieldValues[0]->setValue('');
             }
             if ($this->_subfieldValues[2]->getValue()) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[2]->getValue());
                 $this->_subfieldValues[2]->setValue('');
             } elseif (isset($values[$prefixName . $this->_field->getID() . '_2_hidden']) && $values[$prefixName . $this->_field->getID() . '_2_hidden']) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $values[$prefixName . $this->_field->getID() . '_2_hidden']);
                 $this->_subfieldValues[2]->setValue('');
             }
         }
         //set label from label field
         if (!$this->_subfieldValues[1]->setValue(io::htmlspecialchars(@$values[$prefixName . $this->_field->getID() . '_1']))) {
             return false;
         }
         //thumbnail
         if (isset($_FILES[$prefixName . $this->_field->getID() . '_0']) && $_FILES[$prefixName . $this->_field->getID() . '_0']['name'] && !$_FILES[$prefixName . $this->_field->getID() . '_0']['error']) {
             //check for image type before doing anything
             if (!in_array(io::strtolower(pathinfo($_FILES[$prefixName . $this->_field->getID() . '_0']["name"], PATHINFO_EXTENSION)), $this->_allowedExtensions)) {
                 return false;
             }
             //set label as image name if none set
             /*if (!$values[$prefixName.$this->_field->getID().'_1']) {
             			if (!$this->_subfieldValues[1]->setValue(io::htmlspecialchars($_FILES[$prefixName.$this->_field->getID().'_0']["name"]))) {
             				return false;
             			}
             		}*/
             //destroy all old images if any
             if ($this->_subfieldValues[0]->getValue()) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[0]->getValue());
                 $this->_subfieldValues[0]->setValue('');
             } elseif (isset($values[$prefixName . $this->_field->getID() . '_0_hidden']) && $values[$prefixName . $this->_field->getID() . '_0_hidden']) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $values[$prefixName . $this->_field->getID() . '_0_hidden']);
                 $this->_subfieldValues[0]->setValue('');
             }
             if ($this->_subfieldValues[2]->getValue()) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[2]->getValue());
                 $this->_subfieldValues[2]->setValue('');
             } elseif (isset($values[$prefixName . $this->_field->getID() . '_2_hidden']) && $values[$prefixName . $this->_field->getID() . '_2_hidden']) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $values[$prefixName . $this->_field->getID() . '_2_hidden']);
                 $this->_subfieldValues[2]->setValue('');
             }
             //set thumbnail (resize it if needed)
             //create thumbnail path
             $path = PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED;
             $filename = "r" . $objectID . "_" . $this->_field->getID() . "_" . io::strtolower(SensitiveIO::sanitizeAsciiString($_FILES[$prefixName . $this->_field->getID() . '_0']["name"]));
             if (io::strlen($filename) > 255) {
                 $filename = sensitiveIO::ellipsis($filename, 255, '-', true);
             }
             //move uploaded file
             $fileDatas = CMS_file::uploadFile($prefixName . $this->_field->getID() . '_0', PATH_TMP_FS);
             if ($fileDatas['error']) {
                 return false;
             }
             if (!CMS_file::moveTo(PATH_TMP_FS . '/' . $fileDatas['filename'], $path . "/" . $filename)) {
                 return false;
             }
             if ($params['maxWidth'] > 0) {
                 $oImage = new CMS_image($path . "/" . $filename);
                 //get current file size
                 $sizeX = $oImage->getWidth();
                 $sizeY = $oImage->getHeight();
                 //check thumbnail size
                 if ($sizeX > $params['maxWidth'] || $sizeY > $params['maxHeight']) {
                     $newSizeX = $sizeX;
                     $newSizeY = $sizeY;
                     // Check width
                     if ($params['maxWidth'] && $newSizeX > $params['maxWidth']) {
                         $newSizeY = round($params['maxWidth'] * $newSizeY / $newSizeX);
                         $newSizeX = $params['maxWidth'];
                     }
                     if ($params['maxHeight'] && $newSizeY > $params['maxHeight']) {
                         $newSizeX = round($params['maxHeight'] * $newSizeX / $newSizeY);
                         $newSizeY = $params['maxHeight'];
                     }
                     //resize image
                     $srcfilepath = $path . "/" . $filename;
                     $path_parts = pathinfo($srcfilepath);
                     $thumbnailFilename = io::substr($path_parts['basename'], 0, -(io::strlen($path_parts['extension']) + 1)) . '_thumbnail.' . $path_parts['extension'];
                     $destfilepath = $path . "/" . $thumbnailFilename;
                     $extension = io::strtolower($path_parts['extension']);
                     if (!$oImage->resize($newSizeX, $newSizeY, $destfilepath)) {
                         return false;
                     }
                     //if we use original image as image zoom, set it
                     if ($values[$prefixName . $this->_field->getID() . '_makeZoom'] == 1) {
                         //set image zoom
                         if (!$this->_subfieldValues[2]->setValue($filename)) {
                             return false;
                         }
                     } else {
                         //destroy original image
                         unlink($srcfilepath);
                     }
                     //set resized thumbnail
                     if (!$this->_subfieldValues[0]->setValue($thumbnailFilename)) {
                         return false;
                     }
                 } else {
                     //no need to resize thumbnail (below the maximum width), so set it
                     if (!$this->_subfieldValues[0]->setValue($filename)) {
                         return false;
                     }
                     //if we use original image as image zoom, set it
                     if ($values[$prefixName . $this->_field->getID() . '_makeZoom'] == 1) {
                         //set image zoom
                         if (!$this->_subfieldValues[2]->setValue($filename)) {
                             return false;
                         }
                     }
                 }
             } else {
                 //no need to resize thumbnail, so set it
                 if (!$this->_subfieldValues[0]->setValue($filename)) {
                     return false;
                 }
                 //if we use original image as image zoom, set it
                 if ($values[$prefixName . $this->_field->getID() . '_makeZoom'] == 1) {
                     //set image zoom
                     if (!$this->_subfieldValues[2]->setValue($filename)) {
                         return false;
                     }
                 }
             }
         } elseif (isset($_FILES[$prefixName . $this->_field->getID() . '_0']) && $_FILES[$prefixName . $this->_field->getID() . '_0']['name'] && $_FILES[$prefixName . $this->_field->getID() . '_0']['error'] != 0) {
             return false;
         } elseif (isset($values[$prefixName . $this->_field->getID() . '_0_hidden']) && $values[$prefixName . $this->_field->getID() . '_0_hidden'] && (!isset($values[$prefixName . $this->_field->getID() . '_delete']) || $values[$prefixName . $this->_field->getID() . '_delete'] != 1)) {
             //set label as image name if none set
             if (!$this->_subfieldValues[0]->setValue($values[$prefixName . $this->_field->getID() . '_0_hidden'])) {
                 return false;
             }
         }
         //image zoom (if needed)
         if (isset($values[$prefixName . $this->_field->getID() . '_makeZoom']) && $values[$prefixName . $this->_field->getID() . '_makeZoom'] != 1 && isset($_FILES[$prefixName . $this->_field->getID() . '_2']['name']) && $_FILES[$prefixName . $this->_field->getID() . '_2']['name'] && !$_FILES[$prefixName . $this->_field->getID() . '_2']['error']) {
             //check for image type before doing anything
             if (!in_array(io::strtolower(pathinfo($_FILES[$prefixName . $this->_field->getID() . '_2']["name"], PATHINFO_EXTENSION)), $this->_allowedExtensions)) {
                 return false;
             }
             //create thumbnail path
             $path = PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED;
             $filename = "r" . $objectID . "_" . $this->_field->getID() . "_" . io::strtolower(SensitiveIO::sanitizeAsciiString($_FILES[$prefixName . $this->_field->getID() . '_2']["name"]));
             if (io::strlen($filename) > 255) {
                 $filename = sensitiveIO::ellipsis($filename, 255, '-', true);
             }
             //move uploaded file
             $fileDatas = CMS_file::uploadFile($prefixName . $this->_field->getID() . '_2', PATH_TMP_FS);
             if ($fileDatas['error']) {
                 return false;
             }
             if (!CMS_file::moveTo(PATH_TMP_FS . '/' . $fileDatas['filename'], $path . "/" . $filename)) {
                 return false;
             }
             //set it
             if (!$this->_subfieldValues[2]->setValue($filename)) {
                 return false;
             }
         } elseif (isset($_FILES[$prefixName . $this->_field->getID() . '_2']) && $_FILES[$prefixName . $this->_field->getID() . '_2']['name'] && $_FILES[$prefixName . $this->_field->getID() . '_2']['error'] != 0) {
             return false;
         } elseif (isset($values[$prefixName . $this->_field->getID() . '_2_hidden']) && $values[$prefixName . $this->_field->getID() . '_2_hidden'] && (!isset($values[$prefixName . $this->_field->getID() . '_delete']) || $values[$prefixName . $this->_field->getID() . '_delete'] != 1)) {
             if (!$this->_subfieldValues[2]->setValue($values[$prefixName . $this->_field->getID() . '_2_hidden'])) {
                 return false;
             }
         }
         return true;
     }
 }
예제 #9
0
 /**
  * For an easy acces from a CMS_resource management page
  * Sets the CMS_resource receiving the file uploaded and proceed to upload
  * at the same time
  * Given object must have these methods : getID() and writeToPersistence()
  * they help determining thename of uploaded file
  *
  * @access public
  * @param CMS_resource $obj, the object receiving file uploaded as a field
  * @param string $moduleCodename, the module of the resource
  * @param string $originPath, path of current file to delete before upload
  * @param boolean $overwrite, if set to true, deletes any previous file
  * @param integer $maxwidth, maxwidth allowed for an image
  * @return boolean true on success, false otherwise
  */
 function doUploadForResource(&$obj, $moduleCodename, $originPath = false, $overwrite = false, $maxwidth = false)
 {
     if ($overwrite) {
         $this->_overwrite = true;
         $this->_fileUpload->setAttribute('overwrite', $this->_overwrite);
     }
     if (is_a($obj, 'CMS_resource') && method_exists($obj, 'getID') && method_exists($obj, 'writeToPersistence') && $moduleCodename != '' && $this->_overwrite) {
         if ($originPath) {
             $this->_fileUpload->setPath('origin', $originPath);
         }
         if (SensitiveIO::isPositiveInteger($maxwidth)) {
             $sizes = @getimagesize($this->_fileUpload->getInputValue("tmp_name"));
             if ($sizes[0] > $maxwidth) {
                 $this->_errorMessages = array(self::MESSAGE_ERROR_IMAGE_TOO_WIDE, array($maxwidth));
                 return false;
             }
         }
         $f = SensitiveIO::sanitizeAsciiString($this->_fileUpload->getInputValue("name"));
         if ($f != '' && $obj->writeToPersistence()) {
             $path = PATH_MODULES_FILES_FS . "/" . $moduleCodename . "/" . RESOURCE_DATA_LOCATION_EDITED;
             $path .= "/r" . $obj->getID() . "_" . $f;
             $this->_fileUpload->setPath('destination', $path);
         }
         // Check file size
         if ($this->_fileUpload->inputFileTooWide()) {
             $this->_errorMessages = array(self::MESSAGE_ERROR_FILE_TOO_WIDE);
             return false;
         }
         return $this->doUpload();
     }
     return false;
 }
예제 #10
0
 /**
  * set object Values
  *
  * @param array $values : the POST result values
  * @param string prefixname : the prefix used for post names
  * @param boolean newFormat : new automne v4 format (default false for compatibility)
  * @param integer $objectID : the current object id. Must be set, but default is blank for compatibility with other objects
  * @return boolean true on success, false on failure
  * @access public
  */
 function setValues($values, $prefixName, $newFormat = false, $objectID = '')
 {
     if (!sensitiveIO::isPositiveInteger($objectID)) {
         $this->raiseError('ObjectID must be a positive integer : ' . $objectID);
         return false;
     }
     //get field parameters
     $params = $this->getParamsValues();
     //get module codename
     $moduleCodename = CMS_poly_object_catalog::getModuleCodenameForField($this->_field->getID());
     if ($newFormat) {
         //delete old files ?
         //thumbnail
         if ($this->_subfieldValues[1]->getValue() && (!$values[$prefixName . $this->_field->getID() . '_1'] || pathinfo($values[$prefixName . $this->_field->getID() . '_1'], PATHINFO_BASENAME) != $this->_subfieldValues[1]->getValue())) {
             @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[1]->getValue());
             $this->_subfieldValues[1]->setValue('');
         }
         //file
         if ($this->_subfieldValues[4]->getValue() && (!$values[$prefixName . $this->_field->getID() . '_4'] || pathinfo($values[$prefixName . $this->_field->getID() . '_4'], PATHINFO_BASENAME) != $this->_subfieldValues[4]->getValue())) {
             @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[4]->getValue());
             $this->_subfieldValues[4]->setValue('');
             //reset filesize
             if (!$this->_subfieldValues[2]->setValue(0)) {
                 return false;
             }
         }
         if (!(isset($values[$prefixName . $this->_field->getID() . '_0']) && $this->_subfieldValues[0]->setValue(io::htmlspecialchars($values[$prefixName . $this->_field->getID() . '_0'])))) {
             return false;
         }
         //thumbnail
         if (isset($values[$prefixName . $this->_field->getID() . '_1']) && $values[$prefixName . $this->_field->getID() . '_1'] && io::strpos($values[$prefixName . $this->_field->getID() . '_1'], PATH_UPLOAD_WR . '/') !== false) {
             $filename = $values[$prefixName . $this->_field->getID() . '_1'];
             //check for image type before doing anything
             if (!in_array(io::strtolower(pathinfo($filename, PATHINFO_EXTENSION)), $this->_allowedExtensions)) {
                 return false;
             }
             //destroy old image if any
             if ($this->_subfieldValues[1]->getValue()) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[1]->getValue());
                 $this->_subfieldValues[1]->setValue('');
             }
             //move and rename uploaded file
             $filename = str_replace(PATH_UPLOAD_WR . '/', PATH_UPLOAD_FS . '/', $filename);
             $basename = pathinfo($filename, PATHINFO_BASENAME);
             //set thumbnail
             $path = PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED;
             $newBasename = "r" . $objectID . "_" . $this->_field->getID() . "_" . io::strtolower(SensitiveIO::sanitizeAsciiString($basename));
             //rename image
             $path_parts = pathinfo($newBasename);
             $extension = io::strtolower($path_parts['extension']);
             $newBasename = io::substr($path_parts['basename'], 0, -(io::strlen($extension) + 1)) . '_thumbnail.' . $extension;
             if (io::strlen($newBasename) > 255) {
                 $newBasename = sensitiveIO::ellipsis($newBasename, 255, '-', true);
             }
             $newFilename = $path . '/' . $newBasename;
             //move file from upload dir to new dir
             CMS_file::moveTo($filename, $newFilename);
             CMS_file::chmodFile(FILES_CHMOD, $newFilename);
             //resize thumbnail if needed
             if ($params['thumbMaxWidth'] > 0 || $params['thumbMaxHeight'] > 0) {
                 $oImage = new CMS_image($newFilename);
                 //get current file size
                 $sizeX = $oImage->getWidth();
                 $sizeY = $oImage->getHeight();
                 //check thumbnail size
                 list($sizeX, $sizeY) = @getimagesize($newFilename);
                 if ($params['thumbMaxWidth'] && $sizeX > $params['thumbMaxWidth'] || $params['thumbMaxHeight'] && $sizeY > $params['thumbMaxHeight']) {
                     $newSizeX = $sizeX;
                     $newSizeY = $sizeY;
                     // Check width
                     if ($params['thumbMaxWidth'] && $newSizeX > $params['thumbMaxWidth']) {
                         $newSizeY = round($params['thumbMaxWidth'] * $newSizeY / $newSizeX);
                         $newSizeX = $params['thumbMaxWidth'];
                     }
                     if ($params['thumbMaxHeight'] && $newSizeY > $params['thumbMaxHeight']) {
                         $newSizeX = round($params['thumbMaxHeight'] * $newSizeX / $newSizeY);
                         $newSizeY = $params['thumbMaxHeight'];
                     }
                     if (!$oImage->resize($newSizeX, $newSizeY, $newFilename)) {
                         return false;
                     }
                 }
             }
             //set thumbnail
             if (!$this->_subfieldValues[1]->setValue($newBasename)) {
                 return false;
             }
         }
         //File
         //1- from external location
         if (isset($values[$prefixName . $this->_field->getID() . '_externalfile']) && $values[$prefixName . $this->_field->getID() . '_externalfile']) {
             //from FTP directory
             $filename = $values[$prefixName . $this->_field->getID() . '_externalfile'];
             //check file extension
             if ($params['allowedType'] || $params['disallowedType']) {
                 $extension = io::strtolower(pathinfo($filename, PATHINFO_EXTENSION));
                 if (!$extension) {
                     return false;
                 }
                 //extension must be in allowed list
                 if ($params['allowedType'] && !in_array($extension, explode(',', io::strtolower($params['allowedType'])))) {
                     return false;
                 }
                 //extension must not be in disallowed list
                 if ($params['disallowedType'] && in_array($extension, explode(',', io::strtolower($params['disallowedType'])))) {
                     return false;
                 }
             }
             //destroy old file if any
             if ($this->_subfieldValues[4]->getValue()) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[4]->getValue());
                 $this->_subfieldValues[4]->setValue('');
             }
             $new_filename = 'r' . $objectID . "_" . $this->_field->getID() . "_" . io::strtolower(SensitiveIO::sanitizeAsciiString($filename));
             if (io::strlen($new_filename) > 255) {
                 $new_filename = sensitiveIO::ellipsis($new_filename, 255, '-', true);
             }
             $destination_path = PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/';
             $ftp_dir = PATH_REALROOT_FS . $params['ftpDir'];
             if (@file_exists($ftp_dir . $filename) && is_file($ftp_dir . $filename)) {
                 if (CMS_file::moveTo($ftp_dir . $filename, $destination_path . '/' . $new_filename)) {
                     CMS_file::chmodFile(FILES_CHMOD, $destination_path . '/' . $new_filename);
                     //set label as file name if none set
                     if (!$values[$prefixName . $this->_field->getID() . '_0']) {
                         if (!$this->_subfieldValues[0]->setValue(io::htmlspecialchars($filename))) {
                             return false;
                         }
                     }
                     //set it
                     if (!$this->_subfieldValues[4]->setValue($new_filename)) {
                         return false;
                     }
                     //and set filesize
                     $filesize = @filesize($destination_path . '/' . $new_filename);
                     if ($filesize !== false && $filesize > 0) {
                         //convert in MB
                         $filesize = round($filesize / 1048576, 2);
                     } else {
                         $filesize = '0';
                     }
                     if (!$this->_subfieldValues[2]->setValue($filesize)) {
                         return false;
                     }
                     //set file type
                     if (!$this->_subfieldValues[3]->setValue(self::OBJECT_FILE_TYPE_INTERNAL)) {
                         return false;
                     }
                 } else {
                     return false;
                 }
             } else {
                 return false;
             }
         } else {
             //2- from post
             if ($values[$prefixName . $this->_field->getID() . '_4'] && io::strpos($values[$prefixName . $this->_field->getID() . '_4'], PATH_UPLOAD_WR . '/') !== false) {
                 //check file extension
                 if ($params['allowedType'] || $params['disallowedType']) {
                     $extension = io::strtolower(pathinfo($values[$prefixName . $this->_field->getID() . '_4'], PATHINFO_EXTENSION));
                     if (!$extension) {
                         return false;
                     }
                     //extension must be in allowed list
                     if ($params['allowedType'] && !in_array($extension, explode(',', io::strtolower($params['allowedType'])))) {
                         return false;
                     }
                     //extension must not be in disallowed list
                     if ($params['disallowedType'] && in_array($extension, explode(',', io::strtolower($params['disallowedType'])))) {
                         return false;
                     }
                 }
                 //set file type
                 if (!$this->_subfieldValues[3]->setValue(self::OBJECT_FILE_TYPE_INTERNAL)) {
                     return false;
                 }
                 //destroy old file if any
                 if ($this->_subfieldValues[4]->getValue()) {
                     @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[4]->getValue());
                     $this->_subfieldValues[4]->setValue('');
                 }
                 //move and rename uploaded file
                 $filename = str_replace(PATH_UPLOAD_WR . '/', PATH_UPLOAD_FS . '/', $values[$prefixName . $this->_field->getID() . '_4']);
                 $basename = pathinfo($filename, PATHINFO_BASENAME);
                 //create file path
                 $path = PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED;
                 $newBasename = "r" . $objectID . "_" . $this->_field->getID() . "_" . io::strtolower(SensitiveIO::sanitizeAsciiString($basename));
                 if (io::strlen($newBasename) > 255) {
                     $newBasename = sensitiveIO::ellipsis($newBasename, 255, '-', true);
                 }
                 $newFilename = $path . '/' . $newBasename;
                 if (!CMS_file::moveTo($filename, $newFilename)) {
                     return false;
                 }
                 CMS_file::chmodFile(FILES_CHMOD, $newFilename);
                 //set it
                 if (!$this->_subfieldValues[4]->setValue($newBasename)) {
                     return false;
                 }
                 //and set filesize
                 $filesize = @filesize($newFilename);
                 if ($filesize !== false && $filesize > 0) {
                     //convert in MB
                     $filesize = round($filesize / 1048576, 2);
                 } else {
                     $filesize = '0';
                 }
                 if (!$this->_subfieldValues[2]->setValue($filesize)) {
                     return false;
                 }
             }
         }
         // If label not set yet, set it
         if (!$this->_subfieldValues[0]->getValue()) {
             if ($this->_subfieldValues[4]->getValue()) {
                 $this->_subfieldValues[0]->setValue($this->_subfieldValues[4]->getValue());
             }
         }
         //update files infos if needed
         if ($this->_subfieldValues[1]->getValue() && file_exists(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[1]->getValue())) {
             $file = new CMS_file(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[1]->getValue());
             $imageDatas = array('filename' => $file->getName(false), 'filepath' => $file->getFilePath(CMS_file::WEBROOT), 'filesize' => $file->getFileSize(), 'fileicon' => $file->getFileIcon(CMS_file::WEBROOT), 'extension' => $file->getExtension());
         } else {
             $imageDatas = array('filename' => '', 'filepath' => '', 'filesize' => '', 'fileicon' => '', 'extension' => '');
         }
         //update files infos if needed
         if ($this->_subfieldValues[4]->getValue() && file_exists(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[4]->getValue())) {
             $file = new CMS_file(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[4]->getValue());
             $fileDatas = array('filename' => $file->getName(false), 'filepath' => $file->getFilePath(CMS_file::WEBROOT), 'filesize' => $file->getFileSize(), 'fileicon' => $file->getFileIcon(CMS_file::WEBROOT), 'extension' => $file->getExtension());
         } else {
             $fileDatas = array('filename' => '', 'filepath' => '', 'filesize' => '', 'fileicon' => '', 'extension' => '');
         }
         $imageDatas['module'] = $fileDatas['module'] = $moduleCodename;
         $imageDatas['visualisation'] = $fileDatas['visualisation'] = RESOURCE_DATA_LOCATION_EDITED;
         $content = array('datas' => array('polymodFieldsValue[' . $prefixName . $this->_field->getID() . '_1]' => $imageDatas, 'polymodFieldsValue[' . $prefixName . $this->_field->getID() . '_4]' => $fileDatas, 'polymodFieldsValue[' . $prefixName . $this->_field->getID() . '_externalfile]' => '', 'polymodFieldsValue[' . $prefixName . $this->_field->getID() . '_0]' => sensitiveIO::decodeEntities($this->_subfieldValues[0]->getValue())));
         $view = CMS_view::getInstance();
         $view->addContent($content);
         return true;
     } else {
         //Old format
         //delete old files ?
         if (isset($values[$prefixName . $this->_field->getID() . '_delete']) && $values[$prefixName . $this->_field->getID() . '_delete'] == 1) {
             //thumbnail
             if ($this->_subfieldValues[1]->getValue()) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[1]->getValue());
                 $this->_subfieldValues[1]->setValue('');
             } elseif ($values[$prefixName . $this->_field->getID() . '_1_hidden']) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $values[$prefixName . $this->_field->getID() . '_1_hidden']);
                 $this->_subfieldValues[1]->setValue('');
             }
             //file
             if ($this->_subfieldValues[4]->getValue()) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[4]->getValue());
                 $this->_subfieldValues[4]->setValue('');
             } elseif ($values[$prefixName . $this->_field->getID() . '_4_hidden']) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $values[$prefixName . $this->_field->getID() . '_4_hidden']);
                 $this->_subfieldValues[4]->setValue('');
             }
             //reset filesize
             if (!$this->_subfieldValues[2]->setValue(0)) {
                 return false;
             }
         }
         if (!(isset($values[$prefixName . $this->_field->getID() . '_0']) && $this->_subfieldValues[0]->setValue(io::htmlspecialchars($values[$prefixName . $this->_field->getID() . '_0'])))) {
             return false;
         }
         //thumbnail
         if (isset($_FILES[$prefixName . $this->_field->getID() . '_1']) && $_FILES[$prefixName . $this->_field->getID() . '_1']['name'] && !$_FILES[$prefixName . $this->_field->getID() . '_1']['error']) {
             //check for image type before doing anything
             if (!in_array(io::strtolower(pathinfo($_FILES[$prefixName . $this->_field->getID() . '_1']["name"], PATHINFO_EXTENSION)), $this->_allowedExtensions)) {
                 return false;
             }
             //destroy old image if any
             if ($this->_subfieldValues[1]->getValue()) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[1]->getValue());
                 $this->_subfieldValues[1]->setValue('');
             } elseif ($values[$prefixName . $this->_field->getID() . '_1_hidden']) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $values[$prefixName . $this->_field->getID() . '_1_hidden']);
                 $this->_subfieldValues[1]->setValue('');
             }
             //set thumbnail (resize it if needed)
             //create thumbnail path
             $path = PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED;
             $filename = "r" . $objectID . "_" . $this->_field->getID() . "_" . io::strtolower(SensitiveIO::sanitizeAsciiString($_FILES[$prefixName . $this->_field->getID() . '_1']["name"]));
             if (io::strlen($filename) > 255) {
                 $filename = sensitiveIO::ellipsis($filename, 255, '-', true);
             }
             //move uploaded file
             $fileDatas = CMS_file::uploadFile($prefixName . $this->_field->getID() . '_1', PATH_TMP_FS);
             if ($fileDatas['error']) {
                 return false;
             }
             if (!CMS_file::moveTo(PATH_TMP_FS . '/' . $fileDatas['filename'], $path . "/" . $filename)) {
                 return false;
             }
             if ($params['thumbMaxWidth'] > 0 || $params['thumbMaxHeight'] > 0) {
                 $oImage = new CMS_image($path . "/" . $filename);
                 //get current file size
                 $sizeX = $oImage->getWidth();
                 $sizeY = $oImage->getHeight();
                 //check thumbnail size
                 if ($sizeX > $params['thumbMaxWidth'] || $sizeX > $params['thumbMaxHeight']) {
                     $newSizeX = $sizeX;
                     $newSizeY = $sizeY;
                     // Check width
                     if ($params['thumbMaxWidth'] && $newSizeX > $params['thumbMaxWidth']) {
                         $newSizeY = round($params['thumbMaxWidth'] * $newSizeY / $newSizeX);
                         $newSizeX = $params['thumbMaxWidth'];
                     }
                     if ($params['thumbMaxHeight'] && $newSizeY > $params['thumbMaxHeight']) {
                         $newSizeX = round($params['thumbMaxHeight'] * $newSizeX / $newSizeY);
                         $newSizeY = $params['thumbMaxHeight'];
                     }
                     //resize image
                     $srcfilepath = $path . "/" . $filename;
                     $path_parts = pathinfo($srcfilepath);
                     $thumbnailFilename = io::substr($path_parts['basename'], 0, -(io::strlen($path_parts['extension']) + 1)) . '.png';
                     $destfilepath = $path . "/" . $thumbnailFilename;
                     if (!$oImage->resize($newSizeX, $newSizeY, $destfilepath)) {
                         return false;
                     }
                     //destroy original image
                     @unlink($srcfilepath);
                     //set resized thumbnail
                     if (!$this->_subfieldValues[1]->setValue($thumbnailFilename)) {
                         return false;
                     }
                 } else {
                     //no need to resize thumbnail (below the maximum width), so set it
                     if (!$this->_subfieldValues[1]->setValue($filename)) {
                         return false;
                     }
                 }
             } else {
                 //no need to resize thumbnail (no size limit), so set it
                 if (!$this->_subfieldValues[1]->setValue($filename)) {
                     return false;
                 }
             }
         } elseif (isset($_FILES[$prefixName . $this->_field->getID() . '_1']) && $_FILES[$prefixName . $this->_field->getID() . '_1']['name'] && $_FILES[$prefixName . $this->_field->getID() . '_1']['error'] != 0) {
             return false;
         } elseif (isset($values[$prefixName . $this->_field->getID() . '_1_hidden']) && $values[$prefixName . $this->_field->getID() . '_1_hidden'] && $values[$prefixName . $this->_field->getID() . '_delete'] != 1) {
             if (!$this->_subfieldValues[1]->setValue($values[$prefixName . $this->_field->getID() . '_1_hidden'])) {
                 return false;
             }
         }
         //File
         //1- from external location
         if (isset($values[$prefixName . $this->_field->getID() . '_externalfile']) && $values[$prefixName . $this->_field->getID() . '_externalfile']) {
             //destroy old file if any
             if ($this->_subfieldValues[4]->getValue()) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[4]->getValue());
                 $this->_subfieldValues[4]->setValue('');
             } elseif ($values[$prefixName . $this->_field->getID() . '_4_hidden']) {
                 @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $values[$prefixName . $this->_field->getID() . '_4_hidden']);
                 $this->_subfieldValues[4]->setValue('');
             }
             //from FTP directory
             $filename = $values[$prefixName . $this->_field->getID() . '_externalfile'];
             //io::substr($values[$prefixName.$this->_field->getID().'_externalfile'], 1);
             //check file extension
             if ($params['allowedType'] || $params['disallowedType']) {
                 $extension = io::strtolower(pathinfo($filename, PATHINFO_EXTENSION));
                 if (!$extension) {
                     return false;
                 }
                 //extension must be in allowed list
                 if ($params['allowedType'] && !in_array($extension, explode(',', io::strtolower($params['allowedType'])))) {
                     return false;
                 }
                 //extension must not be in disallowed list
                 if ($params['disallowedType'] && in_array($extension, explode(',', io::strtolower($params['disallowedType'])))) {
                     return false;
                 }
             }
             $new_filename = 'r' . $objectID . "_" . $this->_field->getID() . "_" . io::strtolower(SensitiveIO::sanitizeAsciiString($filename));
             if (io::strlen($new_filename) > 255) {
                 $new_filename = sensitiveIO::ellipsis($new_filename, 255, '-', true);
             }
             $destination_path = PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/';
             $ftp_dir = PATH_REALROOT_FS . $params['ftpDir'];
             if (@file_exists($ftp_dir . $filename) && is_file($ftp_dir . $filename)) {
                 if (@copy($ftp_dir . $filename, $destination_path . '/' . $new_filename)) {
                     @chmod($destination_path . '/' . $new_filename, octdec(FILES_CHMOD));
                     //set label as file name if none set
                     if (!$values[$prefixName . $this->_field->getID() . '_0']) {
                         if (!$this->_subfieldValues[0]->setValue(io::htmlspecialchars($filename))) {
                             return false;
                         }
                     }
                     //set it
                     if (!$this->_subfieldValues[4]->setValue($new_filename)) {
                         return false;
                     }
                     //and set filesize
                     $filesize = @filesize($destination_path . '/' . $new_filename);
                     if ($filesize !== false && $filesize > 0) {
                         //convert in MB
                         $filesize = round($filesize / 1048576, 2);
                     } else {
                         $filesize = '0';
                     }
                     if (!$this->_subfieldValues[2]->setValue($filesize)) {
                         return false;
                     }
                     //set file type
                     if (!$this->_subfieldValues[3]->setValue(self::OBJECT_FILE_TYPE_INTERNAL)) {
                         return false;
                     }
                 } else {
                     return false;
                 }
             } else {
                 return false;
             }
         } else {
             //2- from post
             if (isset($_FILES[$prefixName . $this->_field->getID() . '_4']) && $_FILES[$prefixName . $this->_field->getID() . '_4']['name'] && !$_FILES[$prefixName . $this->_field->getID() . '_4']['error']) {
                 //check file extension
                 if ($params['allowedType'] || $params['disallowedType']) {
                     $extension = io::strtolower(pathinfo($_FILES[$prefixName . $this->_field->getID() . '_4']['name'], PATHINFO_EXTENSION));
                     if (!$extension) {
                         return false;
                     }
                     //extension must be in allowed list
                     if ($params['allowedType'] && !in_array($extension, explode(',', io::strtolower($params['allowedType'])))) {
                         return false;
                     }
                     //extension must not be in disallowed list
                     if ($params['disallowedType'] && in_array($extension, explode(',', io::strtolower($params['disallowedType'])))) {
                         return false;
                     }
                 }
                 //set label as image name if none set
                 if (!$values[$prefixName . $this->_field->getID() . '_0']) {
                     if (!$this->_subfieldValues[0]->setValue(io::htmlspecialchars($_FILES[$prefixName . $this->_field->getID() . '_4']["name"]))) {
                         return false;
                     }
                 }
                 //set file type
                 if (!$this->_subfieldValues[3]->setValue(self::OBJECT_FILE_TYPE_INTERNAL)) {
                     return false;
                 }
                 //destroy old file if any
                 if ($this->_subfieldValues[4]->getValue()) {
                     @unlink(PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED . '/' . $this->_subfieldValues[4]->getValue());
                     $this->_subfieldValues[4]->setValue('');
                 }
                 //create thumnail path
                 $path = PATH_MODULES_FILES_FS . '/' . $moduleCodename . '/' . RESOURCE_DATA_LOCATION_EDITED;
                 $filename = "r" . $objectID . "_" . $this->_field->getID() . "_" . io::strtolower(SensitiveIO::sanitizeAsciiString($_FILES[$prefixName . $this->_field->getID() . '_4']["name"]));
                 if (io::strlen($filename) > 255) {
                     $filename = sensitiveIO::ellipsis($filename, 255, '-', true);
                 }
                 //move uploaded file
                 $fileDatas = CMS_file::uploadFile($prefixName . $this->_field->getID() . '_4', PATH_TMP_FS);
                 if ($fileDatas['error']) {
                     return false;
                 }
                 if (!CMS_file::moveTo(PATH_TMP_FS . '/' . $fileDatas['filename'], $path . "/" . $filename)) {
                     return false;
                 }
                 //set it
                 if (!$this->_subfieldValues[4]->setValue($filename)) {
                     return false;
                 }
                 //and set filesize
                 $filesize = @filesize($path . "/" . $filename);
                 if ($filesize !== false && $filesize > 0) {
                     //convert in MB
                     $filesize = round($filesize / 1048576, 2);
                 } else {
                     $filesize = '0';
                 }
                 if (!$this->_subfieldValues[2]->setValue($filesize)) {
                     return false;
                 }
             } elseif (isset($_FILES[$prefixName . $this->_field->getID() . '_4']) && $_FILES[$prefixName . $this->_field->getID() . '_4']['name'] && $_FILES[$prefixName . $this->_field->getID() . '_4']['error'] != 0) {
                 return false;
             } else {
                 //from hidden fields (previously set but not already saved)
                 if (isset($values[$prefixName . $this->_field->getID() . '_4_hidden']) && $values[$prefixName . $this->_field->getID() . '_4_hidden'] && (!isset($values[$prefixName . $this->_field->getID() . '_delete']) || $values[$prefixName . $this->_field->getID() . '_delete'] != 1)) {
                     //set label as image name if none set
                     if ($values[$prefixName . $this->_field->getID() . '_0']) {
                         if (!$this->_subfieldValues[0]->setValue(io::htmlspecialchars($values[$prefixName . $this->_field->getID() . '_0']))) {
                             return false;
                         }
                     }
                     //set filesize
                     if (!$this->_subfieldValues[2]->setValue($values[$prefixName . $this->_field->getID() . '_2_hidden'])) {
                         return false;
                     }
                     //set file type
                     if (!$this->_subfieldValues[3]->setValue($values[$prefixName . $this->_field->getID() . '_3_hidden'])) {
                         return false;
                     }
                     if (!$this->_subfieldValues[4]->setValue($values[$prefixName . $this->_field->getID() . '_4_hidden'])) {
                         return false;
                     }
                 }
             }
         }
         // If label not set yet, set it
         if (!$this->_subfieldValues[0]->getValue()) {
             if ($this->_subfieldValues[4]->getValue()) {
                 $this->_subfieldValues[0]->setValue($this->_subfieldValues[4]->getValue());
             }
         }
         return true;
     }
 }
예제 #11
0
 /**
  * public static getCloneFromID
  *
  * Clones a Row, changes some attributes
  * and writes it to persistence (MySQL for now)
  *
  * @param anyRowID as the ID of Row to be cloned
  * @param String label receive a new label for this Row
  * @param boolean $setPrivate Should the template be set as a private one ?  ALSO determines if the new row should point to the same file
  * @return a valid new CMS_row
  */
 static function getCloneFromID($rowID = 0, $label = false, $setPrivate = false)
 {
     $ret = false;
     $model = new CMS_row($rowID);
     if ($model->getID() > 0) {
         //New blank one
         $row = new CMS_row();
         //First write a new object to get it's ID
         $row->writeToPersistence();
         //Setting label
         $label = $label ? $label : $model->getLabel();
         $row->setLabel($label);
         //Copying template definition file (if not private template)
         if ($setPrivate) {
             $filename = $model->getDefinitionFileName();
         } else {
             $filename = "r" . $row->getID() . "_" . SensitiveIO::sanitizeAsciiString($row->getLabel()) . ".xml";
         }
         if ($setPrivate || CMS_file::copyTo(PATH_TEMPLATES_ROWS_FS . "/" . $model->getDefinitionFileName(), PATH_TEMPLATES_ROWS_FS . "/" . $filename)) {
             $row->setDefinitionFile($filename);
             //Copying groupsStack from database
             foreach ($model->getGroups() as $grp) {
                 $row->addGroup($grp);
             }
             //Copying image
             $row->setImage($model->getImage(CMS_file::WEBROOT, true));
             //set private if asked to.
             if ($setPrivate) {
                 $row->setPrivate(true);
             }
             //copy description
             $row->setDescription($model->getDescription());
             //add filtered templates
             $row->setFilteredTemplates($model->getFilteredTemplates());
             //Partial update for groups and image
             $row->writeToPersistence();
             $ret = $row;
         }
         unset($model);
     }
     if ($row) {
         //Clean if any error when out
         if (!$ret) {
             $row->destroy();
         }
         unset($row);
     }
     return $ret;
 }
예제 #12
0
 /**
  * public static getCloneFromID
  *
  * Clones a Template, changes some attributes
  * and writes it to persistence (MySQL for now)
  *
  * @param anyTemplateID as the ID of Template to be cloned
  * @param String label receive a new label for this Template
  * @param boolean $setPrivate Should the template be set as a private one ?  ALSO determines if the new template should point to the same file
  * @param boolean $dontCopyClientSpaces Should the clientspaces be copied ?
  * @param integer $tplFrom the original template ID to get good rows
  * @return a valid new CMS_pageTemplate
  */
 static function getCloneFromID($templateID = 0, $label = false, $setPrivate = false, $dontCopyClientSpaces = false, $tplFrom = false)
 {
     $ret = false;
     $model = new CMS_pageTemplate($templateID);
     if ($model->getID() > 0) {
         //New blank one
         $tpl = new CMS_pageTemplate();
         //First write a new object to get it's ID
         $tpl->writeToPersistence();
         //Setting label
         $label = $label ? $label : $model->getLabel();
         $tpl->setLabel($label);
         //Copying template definition file (if not private template)
         if ($setPrivate) {
             $filename = $model->getDefinitionFile();
         } else {
             $filename = "pt" . $tpl->getID() . "_" . SensitiveIO::sanitizeAsciiString($tpl->getLabel()) . ".xml";
         }
         if ($setPrivate || CMS_file::copyTo(PATH_TEMPLATES_FS . "/" . $model->getDefinitionFile(), PATH_TEMPLATES_FS . "/" . $filename)) {
             $tpl->setDefinitionFile($filename);
             //Copying groupsStack from database
             foreach ($model->getGroups() as $grp) {
                 $tpl->addGroup($grp);
             }
             //Copying image file from model to a new one
             if ($setPrivate) {
                 $tpl->setImage($model->getImage());
             } else {
                 if ($model->getImage()) {
                     $ext = io::substr($model->getImage(), strrpos($model->getImage(), "."));
                     $imagefilename = "pt" . $tpl->getID() . "_" . SensitiveIO::sanitizeAsciiString($tpl->getLabel()) . $ext;
                     if (CMS_file::copyTo(PATH_TEMPLATES_IMAGES_FS . "/" . $model->getImage(), PATH_TEMPLATES_IMAGES_FS . "/" . $imagefilename)) {
                         $tpl->setImage($imagefilename);
                     }
                 } else {
                     $tpl->setImage();
                 }
             }
             //set private if asked to.
             if ($setPrivate) {
                 $tpl->setPrivate(true);
             }
             //copy description
             $tpl->setDescription($model->getDescription());
             //websites denied
             $websitesDenied = $model->getWebsitesDenied();
             foreach ($websitesDenied as $websiteId) {
                 if (CMS_websitesCatalog::exists($websiteId)) {
                     //to check if website still exists
                     $tpl->denyWebsite($websiteId);
                 }
             }
             //Copy printing definition
             $tpl->setPrintingClientSpaces($model->getPrintingClientSpaces());
             //Partial update for groups and image
             $tpl->writeToPersistence();
             //Copying template clientspaces rows definitions
             if (!$dontCopyClientSpaces) {
                 $suffixes = array('archived', 'deleted', 'edited', 'edition', 'public');
                 foreach ($suffixes as $suffix) {
                     if ($tplFrom) {
                         $sql = "\n\t\t\t\t\t\t\t\tselect\n\t\t\t\t\t\t\t\t\t*\n\t\t\t\t\t\t\t\tfrom\n\t\t\t\t\t\t\t\t\t`mod_standard_clientSpaces_" . $suffix . "`\n\t\t\t\t\t\t\t\twhere\n\t\t\t\t\t\t\t\t\t`template_cs`='" . $tplFrom . "'\n\t\t\t\t\t\t\t";
                     } else {
                         $sql = "\n\t\t\t\t\t\t\t\tselect\n\t\t\t\t\t\t\t\t\t*\n\t\t\t\t\t\t\t\tfrom\n\t\t\t\t\t\t\t\t\t`mod_standard_clientSpaces_" . $suffix . "`\n\t\t\t\t\t\t\t\twhere\n\t\t\t\t\t\t\t\t\t`template_cs`='" . $model->getID() . "'\n\t\t\t\t\t\t\t";
                     }
                     $q = new CMS_query($sql);
                     while ($arr = $q->getArray()) {
                         $sql1 = "\n\t\t\t\t\t\t\t\tinsert into\n\t\t\t\t\t\t\t\t\t`mod_standard_clientSpaces_" . $suffix . "`\n\t\t\t\t\t\t\t\tset\n\t\t\t\t\t\t\t\t\t`template_cs`='" . $tpl->getID() . "',\n\t\t\t\t\t\t\t\t\t`tagID_cs`='" . SensitiveIO::sanitizeSQLString($arr["tagID_cs"]) . "',\n\t\t\t\t\t\t\t\t\t`rowsDefinition_cs`='" . SensitiveIO::sanitizeSQLString($arr["rowsDefinition_cs"]) . "',\n\t\t\t\t\t\t\t\t\t`type_cs`='" . $arr["type_cs"] . "',\n\t\t\t\t\t\t\t\t\t`order_cs`='" . $arr["order_cs"] . "'\n\t\t\t\t\t\t\t";
                         $q1 = new CMS_query($sql1);
                         unset($q1);
                     }
                     unset($q);
                 }
             }
             //CMS_Template to return
             $ret = $tpl;
         }
         unset($model);
     }
     if ($tpl) {
         //Clean if any error when out
         if (!$ret) {
             $tpl->destroy();
         }
         unset($tpl);
     }
     return $ret;
 }