getValidKey() public static method

public static getValidKey ( $key, null $type ) : mixed | string
$key
$type null
return mixed | string
Exemplo n.º 1
0
 /**
  * @param string $name
  * @return $this|void
  * @throws DAV\Exception\Forbidden
  * @throws \Exception
  */
 public function setName($name)
 {
     if ($this->asset->isAllowed("rename")) {
         $user = AdminTool::getCurrentUser();
         $this->asset->setUserModification($user->getId());
         $this->asset->setFilename(Element\Service::getValidKey($name), "asset");
         $this->asset->save();
     } else {
         throw new DAV\Exception\Forbidden();
     }
     return $this;
 }
Exemplo n.º 2
0
 /**
  * Moves a file/directory
  *
  * @param string $sourcePath
  * @param string $destinationPath
  * @return void
  */
 public function move($sourcePath, $destinationPath)
 {
     $nameParts = explode("/", $sourcePath);
     $nameParts[count($nameParts) - 1] = Element\Service::getValidKey($nameParts[count($nameParts) - 1], "asset");
     $sourcePath = implode("/", $nameParts);
     $nameParts = explode("/", $destinationPath);
     $nameParts[count($nameParts) - 1] = Element\Service::getValidKey($nameParts[count($nameParts) - 1], "asset");
     $destinationPath = implode("/", $nameParts);
     try {
         if (dirname($sourcePath) == dirname($destinationPath)) {
             $asset = null;
             if ($asset = Asset::getByPath("/" . $destinationPath)) {
                 // If we got here, this means the destination exists, and needs to be overwritten
                 $sourceAsset = Asset::getByPath("/" . $sourcePath);
                 $asset->setData($sourceAsset->getData());
                 $sourceAsset->delete();
             }
             // see: Asset\WebDAV\File::delete() why this is necessary
             $log = Asset\WebDAV\Service::getDeleteLog();
             if (!$asset && array_key_exists("/" . $destinationPath, $log)) {
                 $asset = \Pimcore\Tool\Serialize::unserialize($log["/" . $destinationPath]["data"]);
                 if ($asset) {
                     $sourceAsset = Asset::getByPath("/" . $sourcePath);
                     $asset->setData($sourceAsset->getData());
                     $sourceAsset->delete();
                 }
             }
             if (!$asset) {
                 $asset = Asset::getByPath("/" . $sourcePath);
             }
             $asset->setFilename(basename($destinationPath));
         } else {
             $asset = Asset::getByPath("/" . $sourcePath);
             $parent = Asset::getByPath("/" . dirname($destinationPath));
             $asset->setPath($parent->getRealFullPath() . "/");
             $asset->setParentId($parent->getId());
         }
         $user = \Pimcore\Tool\Admin::getCurrentUser();
         $asset->setUserModification($user->getId());
         $asset->save();
     } catch (\Exception $e) {
         Logger::error($e);
     }
 }
Exemplo n.º 3
0
 public function getValidFilenameAction()
 {
     $this->_helper->json(["filename" => \Pimcore\Model\Element\Service::getValidKey($this->getParam("value"), $this->getParam("type"))]);
 }
Exemplo n.º 4
0
 public static function getUniqueKey($item, $nr = 0)
 {
     $list = new Listing();
     $list->setUnpublished(true);
     $key = Element\Service::getValidKey($item->getKey(), "object");
     if (!$key) {
         throw new \Exception("No item key set.");
     }
     if ($nr) {
         $key = $key . '_' . $nr;
     }
     $parent = $item->getParent();
     if (!$parent) {
         throw new \Exception("You have to set a parent Object to determine a unique Key");
     }
     if (!$item->getId()) {
         $list->setCondition('o_parentId = ? AND `o_key` = ? ', [$parent->getId(), $key]);
     } else {
         $list->setCondition('o_parentId = ? AND `o_key` = ? AND o_id != ? ', [$parent->getId(), $key, $item->getId()]);
     }
     $check = $list->loadIdList();
     if (!empty($check)) {
         $nr++;
         $key = self::getUniqueKey($item, $nr);
     }
     return $key;
 }
Exemplo n.º 5
0
 public function importUrlAction()
 {
     $success = true;
     $data = Tool::getHttpData($this->getParam("url"));
     $filename = basename($this->getParam("url"));
     $parentId = $this->getParam("id");
     $parentAsset = Asset::getById(intval($parentId));
     $filename = Element\Service::getValidKey($filename, "asset");
     $filename = $this->getSafeFilename($parentAsset->getRealFullPath(), $filename);
     if (empty($filename)) {
         throw new \Exception("The filename of the asset is empty");
     }
     // check for duplicate filename
     $filename = $this->getSafeFilename($parentAsset->getRealFullPath(), $filename);
     if ($parentAsset->isAllowed("create")) {
         $asset = Asset::create($parentId, ["filename" => $filename, "data" => $data, "userOwner" => $this->user->getId(), "userModification" => $this->user->getId()]);
         $success = true;
     } else {
         Logger::debug("prevented creating asset because of missing permissions");
     }
     $this->_helper->json(["success" => $success]);
 }
Exemplo n.º 6
0
 /**
  * @param string $name
  * @return $this|void
  * @throws DAV\Exception\Forbidden
  * @throws \Exception
  */
 public function setName($name)
 {
     if ($this->asset->isAllowed("rename")) {
         $this->asset->setFilename(Element\Service::getValidKey($name, "asset"));
         $this->asset->save();
     } else {
         throw new DAV\Exception\Forbidden();
     }
     return $this;
 }
Exemplo n.º 7
0
 /**
  * @param $item \Pimcore\Model\Asset
  * @param int $nr
  * @return string
  * @throws \Exception
  */
 public static function getUniqueKey($item, $nr = 0)
 {
     $list = new Listing();
     $key = Element\Service::getValidKey($item->getKey(), "asset");
     if (!$key) {
         throw new \Exception("No item key set.");
     }
     if ($nr) {
         if ($item->getType() == 'folder') {
             $key = $key . '_' . $nr;
         } else {
             $keypart = substr($key, 0, strrpos($key, '.'));
             $extension = str_replace($keypart, '', $key);
             $key = $keypart . '_' . $nr . $extension;
         }
     }
     $parent = $item->getParent();
     if (!$parent) {
         throw new \Exception("You have to set a parent folder to determine a unique Key");
     }
     if (!$item->getId()) {
         $list->setCondition('parentId = ? AND `filename` = ? ', [$parent->getId(), $key]);
     } else {
         $list->setCondition('parentId = ? AND `filename` = ? AND id != ? ', [$parent->getId(), $key, $item->getId()]);
     }
     $check = $list->loadIdList();
     if (!empty($check)) {
         $nr++;
         $key = self::getUniqueKey($item, $nr);
     }
     return $key;
 }
Exemplo n.º 8
0
 private static function map($wsData, $data)
 {
     foreach ($data as $key => $value) {
         if (is_array($value)) {
             $tmp = [];
             foreach ($value as $subkey => $subvalue) {
                 if (is_array($subvalue)) {
                     $object = new stdClass();
                     $object = self::map($object, $subvalue);
                     $tmp[$subkey] = $object;
                 } else {
                     $tmp[$subkey] = $subvalue;
                 }
             }
             $value = $tmp;
         }
         $wsData->{$key} = $value;
     }
     if ($wsData instanceof Pimcore\Model\Webservice\Data\Object) {
         /** @var Pimcore\Model\Webservice\Data\Object key */
         $wsData->key = Element\Service::getValidKey($wsData->key, "object");
     } elseif ($wsData instanceof Pimcore\Model\Webservice\Data\Document) {
         /** @var Pimcore\Model\Webservice\Data\Document key */
         $wsData->key = Element\Service::getValidKey($wsData->key, "document");
     } elseif ($wsData instanceof Pimcore\Model\Webservice\Data\Asset) {
         /** @var Pimcore\Model\Webservice\Data\Asset $wsData */
         $wsData->filename = Element\Service::getValidKey($wsData->filename, "asset");
     }
     return $wsData;
 }
Exemplo n.º 9
0
 public function addAction()
 {
     $success = false;
     $errorMessage = "";
     // check for permission
     $parentDocument = Document::getById(intval($this->getParam("parentId")));
     if ($parentDocument->isAllowed("create")) {
         $intendedPath = $parentDocument->getRealFullPath() . "/" . $this->getParam("key");
         if (!Document\Service::pathExists($intendedPath)) {
             $createValues = ["userOwner" => $this->getUser()->getId(), "userModification" => $this->getUser()->getId(), "published" => false];
             $createValues["key"] = \Pimcore\Model\Element\Service::getValidKey($this->getParam("key"), "document");
             // check for a docType
             $docType = Document\DocType::getById(intval($this->getParam("docTypeId")));
             if ($docType) {
                 $createValues["template"] = $docType->getTemplate();
                 $createValues["controller"] = $docType->getController();
                 $createValues["action"] = $docType->getAction();
                 $createValues["module"] = $docType->getModule();
             } elseif ($this->getParam("translationsBaseDocument")) {
                 $translationsBaseDocument = Document::getById($this->getParam("translationsBaseDocument"));
                 $createValues["template"] = $translationsBaseDocument->getTemplate();
                 $createValues["controller"] = $translationsBaseDocument->getController();
                 $createValues["action"] = $translationsBaseDocument->getAction();
                 $createValues["module"] = $translationsBaseDocument->getModule();
             } elseif ($this->getParam("type") == "page" || $this->getParam("type") == "snippet" || $this->getParam("type") == "email") {
                 $createValues["controller"] = Config::getSystemConfig()->documents->default_controller;
                 $createValues["action"] = Config::getSystemConfig()->documents->default_action;
             }
             if ($this->getParam("inheritanceSource")) {
                 $createValues["contentMasterDocumentId"] = $this->getParam("inheritanceSource");
             }
             switch ($this->getParam("type")) {
                 case "page":
                     $document = Document\Page::create($this->getParam("parentId"), $createValues, false);
                     $document->setTitle($this->getParam('title', null));
                     $document->setProperty("navigation_name", "text", $this->getParam('name', null), false);
                     $document->save();
                     $success = true;
                     break;
                 case "snippet":
                     $document = Document\Snippet::create($this->getParam("parentId"), $createValues);
                     $success = true;
                     break;
                 case "email":
                     //ckogler
                     $document = Document\Email::create($this->getParam("parentId"), $createValues);
                     $success = true;
                     break;
                 case "link":
                     $document = Document\Link::create($this->getParam("parentId"), $createValues);
                     $success = true;
                     break;
                 case "hardlink":
                     $document = Document\Hardlink::create($this->getParam("parentId"), $createValues);
                     $success = true;
                     break;
                 case "folder":
                     $document = Document\Folder::create($this->getParam("parentId"), $createValues);
                     $document->setPublished(true);
                     try {
                         $document->save();
                         $success = true;
                     } catch (\Exception $e) {
                         $this->_helper->json(["success" => false, "message" => $e->getMessage()]);
                     }
                     break;
                 default:
                     $classname = "\\Pimcore\\Model\\Document\\" . ucfirst($this->getParam("type"));
                     // this is the fallback for custom document types using prefixes
                     // so we need to check if the class exists first
                     if (!\Pimcore\Tool::classExists($classname)) {
                         $oldStyleClass = "\\Document_" . ucfirst($this->getParam("type"));
                         if (\Pimcore\Tool::classExists($oldStyleClass)) {
                             $classname = $oldStyleClass;
                         }
                     }
                     if (Tool::classExists($classname)) {
                         $document = $classname::create($this->getParam("parentId"), $createValues);
                         try {
                             $document->save();
                             $success = true;
                         } catch (\Exception $e) {
                             $this->_helper->json(["success" => false, "message" => $e->getMessage()]);
                         }
                         break;
                     } else {
                         Logger::debug("Unknown document type, can't add [ " . $this->getParam("type") . " ] ");
                     }
                     break;
             }
         } else {
             $errorMessage = "prevented adding a document because document with same path+key [ {$intendedPath} ] already exists";
             Logger::debug($errorMessage);
         }
     } else {
         $errorMessage = "prevented adding a document because of missing permissions";
         Logger::debug($errorMessage);
     }
     if ($success) {
         if ($this->getParam("translationsBaseDocument")) {
             $translationsBaseDocument = Document::getById($this->getParam("translationsBaseDocument"));
             $properties = $translationsBaseDocument->getProperties();
             $properties = array_merge($properties, $document->getProperties());
             $document->setProperties($properties);
             $document->setProperty("language", "text", $this->getParam("language"));
             $document->save();
             $service = new Document\Service();
             $service->addTranslation($translationsBaseDocument, $document);
         }
         $this->_helper->json(["success" => $success, "id" => $document->getId(), "type" => $document->getType()]);
     } else {
         $this->_helper->json(["success" => $success, "message" => $errorMessage]);
     }
 }
Exemplo n.º 10
0
 public function importProcessAction()
 {
     $success = true;
     $parentId = $this->getParam("parentId");
     $job = $this->getParam("job");
     $id = $this->getParam("id");
     $mappingRaw = \Zend_Json::decode($this->getParam("mapping"));
     $class = Object\ClassDefinition::getById($this->getParam("classId"));
     $skipFirstRow = $this->getParam("skipHeadRow") == "true";
     $fields = $class->getFieldDefinitions();
     $file = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_" . $id;
     // currently only csv supported
     // determine type
     $dialect = Tool\Admin::determineCsvDialect(PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_" . $id . "_original");
     $count = 0;
     if (($handle = fopen($file, "r")) !== false) {
         $data = fgetcsv($handle, 0, $dialect->delimiter, $dialect->quotechar, $dialect->escapechar);
     }
     if ($skipFirstRow && $job == 1) {
         //read the next row, we need to skip the head row
         $data = fgetcsv($handle, 0, $dialect->delimiter, $dialect->quotechar, $dialect->escapechar);
     }
     $tmpFile = $file . "_tmp";
     $tmpHandle = fopen($tmpFile, "w+");
     while (!feof($handle)) {
         $buffer = fgets($handle);
         fwrite($tmpHandle, $buffer);
     }
     fclose($handle);
     fclose($tmpHandle);
     unlink($file);
     rename($tmpFile, $file);
     // prepare mapping
     foreach ($mappingRaw as $map) {
         if ($map[0] !== "" && $map[1] && !empty($map[2])) {
             $mapping[$map[2]] = $map[0];
         } elseif ($map[1] == "published (system)") {
             $mapping["published"] = $map[0];
         } elseif ($map[1] == "type (system)") {
             $mapping["type"] = $map[0];
         }
     }
     // create new object
     $className = "Pimcore\\Model\\Object\\" . ucfirst($this->getParam("className"));
     $parent = Object::getById($this->getParam("parentId"));
     $objectKey = "object_" . $job;
     if ($this->getParam("filename") == "id") {
         $objectKey = null;
     } elseif ($this->getParam("filename") != "default") {
         $objectKey = Element\Service::getValidKey($data[$this->getParam("filename")], "object");
     }
     $overwrite = false;
     if ($this->getParam("overwrite") == "true") {
         $overwrite = true;
     }
     if ($parent->isAllowed("create")) {
         $intendedPath = $parent->getRealFullPath() . "/" . $objectKey;
         if ($overwrite) {
             $object = Object::getByPath($intendedPath);
             if (!$object instanceof Object\Concrete) {
                 //create new object
                 $object = \Pimcore::getDiContainer()->make($className);
             } elseif ($object instanceof Object\Concrete and !$object instanceof $className) {
                 //delete the old object it is of a different class
                 $object->delete();
                 $object = \Pimcore::getDiContainer()->make($className);
             } elseif ($object instanceof Object\Folder) {
                 //delete the folder
                 $object->delete();
                 $object = \Pimcore::getDiContainer()->make($className);
             } else {
                 //use the existing object
             }
         } else {
             $counter = 1;
             while (Object::getByPath($intendedPath) != null) {
                 $objectKey .= "_" . $counter;
                 $intendedPath = $parent->getRealFullPath() . "/" . $objectKey;
                 $counter++;
             }
             $object = new $className();
         }
         $object->setClassId($this->getParam("classId"));
         $object->setClassName($this->getParam("className"));
         $object->setParentId($this->getParam("parentId"));
         $object->setKey($objectKey);
         $object->setCreationDate(time());
         $object->setUserOwner($this->getUser()->getId());
         $object->setUserModification($this->getUser()->getId());
         if (in_array($data[$mapping["type"]], ["object", "variant"])) {
             $object->setType($data[$mapping["type"]]);
         } else {
             $object->setType("object");
         }
         if ($data[$mapping["published"]] === "1") {
             $object->setPublished(true);
         } else {
             $object->setPublished(false);
         }
         foreach ($class->getFieldDefinitions() as $key => $field) {
             $value = $data[$mapping[$key]];
             if (array_key_exists($key, $mapping) and $value != null) {
                 // data mapping
                 $value = $field->getFromCsvImport($value, $object);
                 if ($value !== null) {
                     $object->setValue($key, $value);
                 }
             }
         }
         try {
             $object->save();
             $this->_helper->json(["success" => true]);
         } catch (\Exception $e) {
             $this->_helper->json(["success" => false, "message" => $object->getKey() . " - " . $e->getMessage()]);
         }
     }
     $this->_helper->json(["success" => $success]);
 }