public function storeMedia($originalFilePath, Node $mediaNode, $desiredFilename = null, StorageFacilityFile $preconfiguredFile = null) { //CREATE DATE STAMP DIRECTORY PREFIX $dateStamp = '/'; if ($this->mediaOrganizeByDate) { $dateStamp = $this->getDateStamp($mediaNode); } if (is_null($desiredFilename)) { $uploadName = basename($originalFilePath); $path = pathinfo($uploadName); $ext = !empty($path['extension']) ? strtolower($path['extension']) : $this->getDefaultExtension(); $path['filename'] = substr($path['filename'], 0, 128); $mediaNode->Title = str_replace('.' . $ext, '', $mediaNode->Title); $desiredFileWithoutExtension = SlugUtils::createSlug($mediaNode->Title, $this->mediaAllowCustomSubdirectories); $desiredFilename = $desiredFileWithoutExtension . '.' . $ext; //ENSURE FILENAME IS 128 OR LESS $len = strlen($desiredFilename) + ($this->mediaOrganizeByDate ? 11 : 0) + 1; if ($len > 128) { $desiredFilename = substr($desiredFileWithoutExtension, 0, 128 - (strlen('.' . $ext) + ($this->mediaOrganizeByDate ? 11 : 0) + 1)) . '.' . $ext; } } else { $path = pathinfo($desiredFilename); if (!empty($path['extension'])) { $ext = strtolower($path['extension']); $desiredFileWithoutExtension = substr($desiredFilename, 0, strripos($desiredFilename, '.' . $ext)); } else { $ext = $this->getDefaultExtension(); $desiredFileWithoutExtension = $desiredFilename; $desiredFilename .= '.' . $ext; } } if (empty($path['extension'])) { $oldOriginalFilePath = $originalFilePath; $originalFilePath = dirname($originalFilePath) . '/' . $desiredFileWithoutExtension . '.' . $ext; if (!@rename($oldOriginalFilePath, $originalFilePath)) { throw new Exception('Cannot rename file [' . $oldOriginalFilePath . '] to [' . $originalFilePath . ']'); } } if (in_array(strtolower($ext), array_map("strtolower", $this->mediaRestrictedExtensions))) { throw new Exception('Cannot add media with restricted extension [' . $ext . ']'); } $desiredFilename = $dateStamp . $desiredFilename; $desiredFileWithoutExtension = $dateStamp . $desiredFileWithoutExtension; $this->processSpecificMedia($mediaNode, $originalFilePath, $desiredFilename, $desiredFileWithoutExtension, $preconfiguredFile); return $mediaNode; }
public function __construct($tagOrElement, $slug = '', $role = '', $value = '') { if ($tagOrElement instanceof TagPartial || $tagOrElement instanceof Tag) { $this->fields = $tagOrElement->toArray(); return; } $this->fields = array_merge($this->fields, array('TagElement' => '', 'TagAspect' => '', 'TagSlug' => '', 'TagRole' => '', 'TagValue' => '')); if (is_array($tagOrElement)) { $this->fields = array_merge($this->fields, $tagOrElement); } else { if (is_string($tagOrElement)) { // assume first param is element if (!empty($slug) || !empty($role) || !empty($value)) { if (substr($tagOrElement, 0, 1) == '@') { $this->fields['TagAspect'] = substr($tagOrElement, 1); } else { $this->fields['TagElement'] = $tagOrElement; } $this->fields['TagSlug'] = $slug; $this->fields['TagRole'] = $role; $this->fields['TagValue'] = $value; // assume first param is tag string } else { $expressions = StringUtils::smartSplit($tagOrElement, ".", '"', '\\"', 2); if (preg_match("/^(((?P<ai>@)?(?P<el>[a-z0-9-]+))?(:(?P<s>[a-z0-9\\/\\-]+)?)?)?(#(?P<r>[a-z0-9-]+)?)?(=(?P<v>.+?))?\$/", array_shift($expressions), $m)) { if (!empty($m['ai'])) { $this->fields['TagAspect'] = !empty($m['el']) ? $m['el'] : ''; } else { $this->fields['TagElement'] = !empty($m['el']) ? $m['el'] : ''; } $this->fields['TagSlug'] = !empty($m['s']) ? $m['s'] : ''; $this->fields['TagRole'] = !empty($m['r']) ? $m['r'] : ''; $this->fields['TagValue'] = !empty($m['v']) ? $m['v'] : ''; if (!empty($expressions)) { // if(count($expressions) > 1) $this->fields['ChildPartials'] = current($expressions); // else // $this->fields['ChildPartial'] = $expressions[0]; } } } } else { throw new Exception('Invalid parameter to TagPartial: ' . ClassUtils::getQualifiedType($tagOrElement)); } } if (empty($this->fields['TagElement']) && empty($this->fields['TagAspect']) && empty($this->fields['TagRole'])) { throw new TagException('Invalid partial: No element, aspect, or role was specified [' . print_r($tagOrElement, true) . ']'); } if (!empty($this->fields['TagAspect'])) { // $this->fields['TagAspect'] = strtolower($this->fields['TagAspect']); if (!preg_match("/[a-z0-9-]+/", $this->fields['TagAspect'])) { throw new TagException('Invalid partial: Aspect "' . $this->fields['TagAspect'] . '" must contain only characters or dash'); } } if (!empty($this->fields['TagSlug'])) { $this->fields['TagSlug'] = strtolower($this->fields['TagSlug']); if (!SlugUtils::isSlug($this->fields['TagSlug'], true)) { throw new TagException('Invalid tag: "' . $this->fields['TagSlug'] . '" must be valid slug'); } } if (!empty($this->fields['TagRole']) && !SlugUtils::isSlug($this->fields['TagRole'])) { $this->fields['TagRole'] = SlugUtils::createSlug($this->fields['TagRole']); } if (!empty($this->fields['TagValue']) && !SlugUtils::isSlug($this->fields['TagValue'])) { $this->fields['TagValue'] = SlugUtils::createSlug($this->fields['TagValue']); } // lowercase all parts foreach (array('TagElement', 'TagAspect', 'TagSlug', 'TagRole', 'TagValue') as $name) { $this->fields[$name] = strtolower($this->fields[$name]); } }
/** * Generates key. * * @param string $slug * @param string $prefix * * @return string */ protected function generateKey($slug, $prefix = 'active-edits') { return sprintf('%s-%s', $prefix, \SlugUtils::createSlug($slug)); }
public function xmlToCMSNavItem(ModelObject $CMSNavItem, SimpleXMLExtended $xml) { if (($id = $xml->attribute('id')) !== null) { $CMSNavItem->CMSNavItemID = intval($id); } if (($id = $xml->attribute('slug')) !== null) { $CMSNavItem->Slug = strval($id); } if (($id = $xml->attribute('pluginid')) !== null) { $CMSNavItem->PluginID = strval($id); } if (($id = $xml->attribute('uri')) !== null) { $CMSNavItem->URI = strval($id); } if (($id = $xml->attribute('create_add_menu')) !== null) { $CMSNavItem->DoAddLinksFor = strval($id); } if (($id = $xml->attribute('enabled')) !== null) { $CMSNavItem->Enabled = StringUtils::strToBool($id); } else { $CMSNavItem->Enabled = true; } foreach (array('label', 'sort_order', 'permissions', 'parent_slug') as $key) { $camel = StringUtils::camelize($key); $CMSNavItem->{$camel} = trim($xml->attribute($key)); } if ($CMSNavItem->Slug == '') { $CMSNavItem->Slug = SlugUtils::createSlug($CMSNavItem->Label); } if (empty($CMSNavItem->SortOrder)) { $CMSNavItem->SortOrder = PHP_INT_MAX; } $children = array(); foreach ($xml as $childNode) { if ($childNode->getName() == 'item') { $child = $this->xmlToCMSNavItem(new CMSNavItem(), $childNode); $child->ParentSlug = $CMSNavItem->Slug; $children[] = $child; $sort_array[] = $child->SortOrder; $sort_array2[] = $child->Slug; } } if (!empty($children)) { array_multisort($sort_array, SORT_ASC, $sort_array2, SORT_ASC, $children); } $CMSNavItem->Children = $children; // if($xml->creation_date) // $CMSNavItem->CreationDate = $this->DateFactory->newStorageDate(strval($xml->creation_date)); // if($xml->modified_date) // $CMSNavItem->ModifiedDate = $this->DateFactory->newStorageDate(strval($xml->modified_date)); return $CMSNavItem; }
public function replace() { $this->checkNonce(); if (empty($this->params['Title'])) { throw new Exception('Title parameter is required', 210); } if (empty($this->params['ElementSlug'])) { throw new Exception('ElementSlug parameter is required', 220); } $title = $this->params['Title']; $slug = SlugUtils::createSlug($title); $noderef = new NodeRef($this->ElementService->getBySlug($this->Request->getParameter('ElementSlug')), $slug); // create node $node = $noderef->generateNode(); $this->NodeMapper->defaultsOnNode($node); $node->Title = $title; $node->Slug = $slug; $this->getErrors()->throwOnError(); // edit existing record if ($this->RegulatedNodeService->refExists($noderef)) { $existing = $this->RegulatedNodeService->getByNodeRef($noderef); $existing->setNodePartials($node->getNodePartials()); $existing->setMetas($node->getMetas()); $existing->setOutTags($node->getOutTags()); $node = $this->RegulatedNodeService->edit($node); //create new record } else { $node = $this->RegulatedNodeService->quickAdd($node); } $node = $this->RegulatedNodeService->getByNodeRef($node->getNodeRef(), new NodePartials()); $this->bindToActionDatasource(array($node)); return new View($this->successView()); }
public function filenameForNode($node) { $ext = pathinfo($node->Title, PATHINFO_EXTENSION); $filename = str_replace('.' . $ext, '', $node->Title); $dateStamp = '/'; if ($this->mediaOrganizeByDate) { $dateStamp = $this->getDateStamp($node); } return $dateStamp . SlugUtils::createSlug($filename); }
/** * Creates a slug from the camel case text given. Adds space in between words starting with capital letters and acronyms. * * @param string $string A bit of camel case text that we will generate our slug from. * @param boolean $allowSlashes If set to false, any slashes will be removed from the slug. * Default: false * * @return string Our sluggified version of the {@link $string} param **/ public static function createSlugFromCamelCase($string, $allowSlashes = false) { $string = trim(preg_replace('/(([A-Z]|[0-9])[^A-Z])/', ' $1', $string)); return SlugUtils::createSlug($string, $allowSlashes); }
public function generateNodeRef(NodeRef $nodeRef, $title = null, $useTime = false) { if (is_null($nodeRef)) { throw new NodeException('Cannot generate NodeRef, $nodeRef is null'); } $slug = $nodeRef->getSlug(); if (empty($slug)) { if (empty($title)) { throw new NodeException('Cannot generate NodeRef without title'); } if ($useTime) { $slug = SlugUtils::createSlug(substr($title, 0, 237) . '-' . floor(microtime(true) * 100), $nodeRef->getElement()->isAllowSlugSlashes()); } else { $slug = SlugUtils::createSlug(substr($title, 0, 255), $nodeRef->getElement()->isAllowSlugSlashes()); } $nodeRef = new NodeRef($nodeRef->getElement(), $slug); } else { if ($useTime) { $title = substr($slug, 0, 237) . '-' . floor(microtime(true) * 100); } else { $title = substr($slug, 0, 255); } $nodeRef = new NodeRef($nodeRef->getElement(), SlugUtils::createSlug($title, $nodeRef->getElement()->isAllowSlugSlashes())); } return $nodeRef; }
public function __construct($tagOrElement, $slug = '', $role = '', $value = '', $valuedisplay = '') { $this->fields = array('TagDirection' => 'out', 'TagElement' => '', 'TagSlug' => '', 'TagRole' => '', 'TagRoleDisplay' => '', 'TagValue' => '', 'TagValueDisplay' => '', 'TagSortOrder' => 0, 'TagSectionID' => 0); if ($tagOrElement instanceof Tag || $tagOrElement instanceof TagPartial) { $this->fields = array_merge($this->fields, $tagOrElement->toArray()); } else { if (is_array($tagOrElement)) { foreach ($tagOrElement as $key => $val) { $this->fields[$key] = $val; unset($key); unset($val); } } else { if (is_string($tagOrElement) || $tagOrElement instanceof NodeRef) { if ($tagOrElement instanceof NodeRef) { $slug = $tagOrElement->getSlug(); $tagOrElement = $tagOrElement->getElement()->getSlug(); } // assume first param is element if (!empty($slug) || !empty($role) || !empty($value) || !empty($valuedisplay)) { $this->fields['TagElement'] = $tagOrElement; $this->fields['TagSlug'] = $slug; $this->fields['TagRole'] = $role; // $this->fields['TagRoleDisplay'] = $roledisplay; $this->fields['TagValue'] = $value; $this->fields['TagValueDisplay'] = $valuedisplay; } else { if (($pos = strpos($tagOrElement, '"')) !== FALSE) { $parseTag = substr($tagOrElement, 0, $pos); if (substr($tagOrElement, -1) != '"') { throw new TagException('Invalid tag: TagValueDisplay must end in "'); } $this->fields['TagValueDisplay'] = substr($tagOrElement, $pos + 1, -1); } else { $parseTag = $tagOrElement; } if (preg_match("/^(?P<el>[a-z0-9-]+):(?P<s>[a-z0-9\\/\\-]+)#(?P<r>[a-z0-9-]+)(=(?P<v>.+?)?)?\$/", $parseTag, $m)) { $this->fields['TagElement'] = !empty($m['el']) ? $m['el'] : ''; $this->fields['TagSlug'] = !empty($m['s']) ? $m['s'] : ''; $this->fields['TagRole'] = !empty($m['r']) ? $m['r'] : ''; $this->fields['TagValue'] = !empty($m['v']) ? $m['v'] : ''; } } } } } if (isset($this->fields['NoValidation'])) { return; } if (empty($this->fields['TagElement'])) { throw new TagException('Invalid tag: No TagElement was specified'); } if (strtolower($this->fields['TagElement']) !== $this->fields['TagElement']) { $this->fields['TagElement'] = strtolower($this->fields['TagElement']); } // $elementObj = AppContext::Element($this->fields['TagElement']); // if(empty($elementObj)) // throw new TagException('Invalid tag: "'.$this->fields['TagElement'].'" is not a valid element'); if (!isset($this->fields['TagSlug'])) { throw new TagException('Invalid tag: No TagSlug was specified'); } if (strtolower($this->fields['TagSlug']) !== $this->fields['TagSlug']) { $this->fields['TagSlug'] = strtolower($this->fields['TagSlug']); } if (!preg_match("/^[a-z0-9-\\/]+\$/", $this->fields['TagSlug'])) { throw new TagException('Invalid tag: "' . $this->fields['TagSlug'] . '" must be valid slug'); } if (empty($this->fields['TagRole'])) { throw new Exception('Invalid tag: No TagRole was specified'); } if (!preg_match("/^[a-z0-9-\\/]+\$/", $this->fields['TagRole'])) { $this->fields['TagRole'] = SlugUtils::createSlug($this->fields['TagRole']); } // TagRoleDisplay inherits TagRole if empty // if(empty($this->fields['TagRoleDisplay'])) { // $this->fields['TagRoleDisplay'] = SlugUtils::unsluggify($this->fields['TagRole']); // } // if(!empty($this->tag['TagRoleDisplay'])) // $this->tag['TagRoleDisplay'] = preg_replace("/\s+/s",' ',$this->tag['TagRoleDisplay']); if (!empty($this->fields['TagValue'])) { if (!preg_match("/^[a-z0-9-\\/]+\$/", $this->fields['TagValue'])) { $this->fields['TagValue'] = SlugUtils::createSlug($this->fields['TagValue']); } // TagValueDisplay inherits TagValue if empty if (empty($this->fields['TagValueDisplay'])) { $this->fields['TagValueDisplay'] = $this->fields['TagValue']; } } else { if (!empty($this->fields['TagValueDisplay'])) { $this->fields['TagValue'] = SlugUtils::createSlug($this->fields['TagValueDisplay']); } } if (!empty($this->fields['TagValueDisplay'])) { $this->fields['TagValueDisplay'] = preg_replace("/\\s+/s", ' ', $this->fields['TagValueDisplay']); } // lowercase all parts foreach (array('TagElement', 'TagSlug', 'TagRole', 'TagValue') as $name) { $this->fields[$name] = strtolower($this->fields[$name]); } if (!empty($this->fields['MatchPartial'])) { $this->setMatchPartial(new TagPartial($this->fields['MatchPartial'])); } // set TagString to empty so toString forces rebuild // $this->fields['TagString'] = ''; // $this->toString(); }
protected function _importPhotosFromJson($file) { try { $contents = file_get_contents($file); $json = JSONUtils::decode($contents); foreach ($json as $v) { echo "importing {$v->title}..."; $url = $v->src; $parts = parse_url($url); $slug = SlugUtils::createSlug(basename($parts['path'])); preg_match('/(\\.\\w*)$/', $parts['path'], $ext); $nodeRef = $this->NodeRefService->oneFromAspect('@images'); $nodeRef = $this->NodeRefService->generateNodeRef($nodeRef, $slug); $node = $nodeRef->generateNode(); if (!$this->NodeService->refExists($node->getNodeRef())) { // go fetch file from url $data = $this->HttpRequest->fetchURL($url); // create a unique output file name $sourceFile = FileSystemUtils::secureTmpname($this->workDir, 'urlfetch', !empty($ext[1]) ? strtolower($ext[1]) : null); file_put_contents($sourceFile, $data); $node->Title = rtrim($v->title); $node->Status = "published"; $node->ActiveDate = $this->DateFactory->newStorageDate(); $node = $this->ImageService->storeMedia($sourceFile, $node, basename($parts['path'])); $this->NodeService->add($node); echo "done\n"; } else { echo "exists\n"; } unset($nodeRef); unset($node); } } catch (Exception $e) { echo "Exception: " . $e->getMessage() . "\n"; } }
protected function sanitizeURI($uri) { $sluggedUri = SlugUtils::createSlug($uri, true); if (strcmp($sluggedUri, $uri) !== 0) { $this->Response->sendStatus(Response::SC_MOVED_PERMANENTLY); $this->Response->sendRedirect($sluggedUri); } return $sluggedUri; }
/** * Creates a filename from title, prefixed with date if required. * * @param string The title to use e.g. IMG-1234.jpg * @return string The filename */ protected function buildFilename($title) { $ext = pathinfo($title, PATHINFO_EXTENSION); $filename = str_replace('.' . $ext, '', $title); $datePrefix = '/'; if ($this->mediaOrganizeByDate) { $now = $this->DateFactory->newStorageDate(); $datePrefix = $now->format('/Y/m/d/'); } return $datePrefix . SlugUtils::createSlug($filename) . '.' . strtolower($ext); }
/** * Returns a slug for the specified parameter * * Expected Parameters: * value string The string to turn into a slug * * @return string */ public function create() { $slug = $this->getParameter('value'); $allowSlashes = StringUtils::strToBool($this->getParameter('allowSlashes')); return SlugUtils::createSlug($slug, $allowSlashes); }
protected function filenameFromBasename($basename) { $path = pathinfo($basename); $ext = !empty($path['extension']) ? strtolower($path['extension']) : ''; $path['filename'] = substr($path['filename'], 0, 128); $desiredFileWithoutExtension = SlugUtils::createSlug($path['filename'], $this->mediaAllowCustomSubdirectories); $desiredFilename = $desiredFileWithoutExtension . (!empty($ext) ? '.' . $ext : ''); //ENSURE FILENAME IS 128 OR LESS $len = strlen($desiredFilename) + ($this->mediaOrganizeByDate ? 11 : 0) + 1; if ($len > 128) { $desiredFilename = substr($desiredFileWithoutExtension, 0, 128 - (strlen(!empty($ext) ? '.' . $ext : '') + ($this->mediaOrganizeByDate ? 11 : 0) + 1)) . (!empty($ext) ? '.' . $ext : ''); } $dateStamp = '/'; if ($this->mediaOrganizeByDate) { $now = $this->DateFactory->newStorageDate(); $dateStamp = $now->format('/Y/m/d/'); } return $dateStamp . $desiredFilename; }
/** * @param NodeRef $nodeRef * @param null $title * @param bool $useTime * @return NodeRef * @throws NodeException * @events slugUniquing validation event with $slugTransporter as param2 */ public function generateUniqueNodeRef(NodeRef $nodeRef, $title = null, $useTime = false) { $slug = $nodeRef->getSlug(); $slugTransporter = new Transport(); $slugTransporter->Slug = $slug; $errors = new Errors(); $this->NodeEvents->fireValidationEvents('slugUniquing', $errors, $nodeRef, $slugTransporter); $slug = $slugTransporter->Slug; $nodeRef = new NodeRef($nodeRef->getElement(), $slug); $errors->throwOnError(); if (empty($slug)) { if (empty($title)) { throw new NodeException('Cannot generate unique NodeRef without title'); } if ($useTime) { $slug = SlugUtils::createSlug(substr($title, 0, 237) . '-' . floor(microtime(true) * 100), $nodeRef->getElement()->isAllowSlugSlashes()); } else { $slug = SlugUtils::createSlug(substr($title, 0, 255), $nodeRef->getElement()->isAllowSlugSlashes()); } $nodeRef = new NodeRef($nodeRef->getElement(), $slug); } else { if ($useTime) { $title = substr($slug, 0, 237) . '-' . floor(microtime(true) * 100); } else { $title = substr($slug, 0, 255); } $nodeRef = new NodeRef($nodeRef->getElement(), SlugUtils::createSlug($title, $nodeRef->getElement()->isAllowSlugSlashes())); } $i = 0; while ($this->refExists($nodeRef)) { // Generate a unique slug $slug = SlugUtils::createSlug(substr($title, 0, 250) . " " . ++$i, $nodeRef->getElement()->isAllowSlugSlashes()); $nodeRef = new NodeRef($nodeRef->getElement(), $slug); } return $nodeRef; }