/** * @return array|string */ public function process() { if (!($data = $this->handleFile())) { return $this->failure($this->modx->lexicon('ms2gallery_err_gallery_ns')); } $properties = $this->mediaSource->getProperties(); $tmp = explode('.', $data['name']); $extension = strtolower(end($tmp)); $image_extensions = $allowed_extensions = array(); if (!empty($properties['imageExtensions']['value'])) { $image_extensions = array_map('trim', explode(',', strtolower($properties['imageExtensions']['value']))); } if (!empty($properties['allowedFileTypes']['value'])) { $allowed_extensions = array_map('trim', explode(',', strtolower($properties['allowedFileTypes']['value']))); } if (!empty($allowed_extensions) && !in_array($extension, $allowed_extensions)) { return $this->failure($this->modx->lexicon('ms2gallery_err_gallery_ext')); } else { if (in_array($extension, $image_extensions)) { $type = 'image'; } else { $type = $extension; } } $hash = sha1($data['stream']); if ($this->modx->getCount('msResourceFile', array('resource_id' => $this->resource->id, 'hash' => $hash, 'parent' => 0))) { return $this->failure($this->modx->lexicon('ms2gallery_err_gallery_exists')); } $filename = !empty($properties['imageNameType']) && $properties['imageNameType']['value'] == 'friendly' ? $this->resource->cleanAlias($data['name']) : $hash . '.' . $extension; $rank = isset($properties['imageUploadDir']) && empty($properties['imageUploadDir']['value']) ? 0 : $this->modx->getCount('msResourceFile', array('parent' => 0, 'resource_id' => $this->resource->id)); /* @var msResourceFile $product_file */ $product_file = $this->modx->newObject('msResourceFile', array('resource_id' => $this->resource->id, 'parent' => 0, 'name' => $data['name'], 'file' => $filename, 'path' => $this->resource->id . '/', 'source' => $this->mediaSource->get('id'), 'type' => $type, 'rank' => $rank, 'createdon' => date('Y-m-d H:i:s'), 'createdby' => $this->modx->user->id, 'active' => 1, 'hash' => $hash, 'properties' => $data['properties'])); $this->mediaSource->createContainer($product_file->path, '/'); $file = $this->mediaSource->createObject($product_file->get('path'), $product_file->get('file'), $data['stream']); if ($file) { $url = $this->mediaSource->getObjectUrl($product_file->get('path') . $product_file->get('file')); $product_file->set('url', $url); $product_file->save(); if (empty($rank)) { $imagesTable = $this->modx->getTableName('msResourceFile'); $sql = "UPDATE {$imagesTable} SET rank = rank + 1 WHERE resource_id ='" . $this->resource->id . "' AND id !='" . $product_file->get('id') . "'"; $this->modx->exec($sql); } $generate = $product_file->generateThumbnails($this->mediaSource); if ($generate !== true) { $this->modx->log(modX::LOG_LEVEL_ERROR, 'Could not generate thumbnails for image with id = ' . $product_file->get('id') . '. ' . $generate); return $this->failure($this->modx->lexicon('ms2gallery_err_gallery_thumb')); } else { return $this->success(); } } else { return $this->failure($this->modx->lexicon('ms2gallery_err_gallery_save') . ': ' . print_r($this->mediaSource->getErrors(), 1)); } }
/** * Friendly URL alias checks * @return mixed|string */ public function checkFriendlyAlias() { $this->isSiteStart = $this->object->get('id') == $this->workingContext->getOption('site_start') || $this->object->get('id') == $this->modx->getOption('site_start'); $pageTitle = $this->getProperty('pagetitle', null); $alias = $this->getProperty('alias'); if ($this->workingContext->getOption('friendly_urls', false) && (!$this->getProperty('reloadOnly', false) || (!empty($pageTitle) || $this->isSiteStart))) { /* auto assign alias */ if (empty($alias) && ($this->isSiteStart || $this->workingContext->getOption('automatic_alias', false))) { if (empty($pageTitle)) { $alias = 'index'; } else { $alias = $this->object->cleanAlias($pageTitle); } } if (empty($alias)) { $this->addFieldError('alias', $this->modx->lexicon('field_required')); } /* check for duplicate alias */ $duplicateContext = $this->workingContext->getOption('global_duplicate_uri_check', false) ? '' : $this->getProperty('context_key'); $aliasPath = $this->object->getAliasPath($alias, $this->getProperties()); $duplicateId = $this->object->isDuplicateAlias($aliasPath, $duplicateContext); if (!empty($duplicateId)) { $err = $this->modx->lexicon('duplicate_uri_found', array('id' => $duplicateId, 'uri' => $aliasPath)); $this->addFieldError('uri', $err); $uriOverride = $this->getProperty('uri_override', null); if ($uriOverride == null || $uriOverride !== 1) { $this->addFieldError('alias', $err); } } $this->setProperty('alias', $alias); } return $alias; }
/** * Clean and prepare the alias, automatically generating it if the option is set * @return string */ public function prepareAlias() { $alias = ''; /* friendly url alias checks */ $pageTitle = $this->getProperty('pagetitle'); if ($this->workingContext->getOption('friendly_urls', false) && (!$this->getProperty('reloadOnly', false) || !empty($pageTitle))) { $alias = $this->getProperty('alias'); /* auto assign alias */ if (empty($alias) && $this->workingContext->getOption('automatic_alias', false)) { $alias = $this->object->cleanAlias($pageTitle); } if (empty($alias)) { $this->addFieldError('alias', $this->modx->lexicon('field_required')); } else { $duplicateContext = $this->workingContext->getOption('global_duplicate_uri_check', false) ? '' : $this->getProperty('context_key'); $aliasPath = $this->object->getAliasPath($alias, $this->getProperties()); $duplicateId = $this->object->isDuplicateAlias($aliasPath, $duplicateContext); if ($duplicateId) { $err = $this->modx->lexicon('duplicate_uri_found', array('id' => $duplicateId, 'uri' => $aliasPath)); $this->addFieldError('uri', $err); if ($this->getProperty('uri_override', 0) !== 1) { $this->addFieldError('alias', $err); } } } $this->setProperty('alias', $alias); } return $alias; }
/** * Clean and prepare the alias, automatically generating it if the option is set * @return string */ public function prepareAlias() { // The user submitted alias & page title $alias = $this->getProperty('alias'); $pageTitle = $this->getProperty('pagetitle'); $autoGenerated = false; // If we don't have an alias passed, and automatic_alias is enabled, we generate one from the pagetitle. if (empty($alias) && $this->workingContext->getOption('automatic_alias', false)) { $alias = $this->object->cleanAlias($pageTitle); $autoGenerated = true; } $friendlyUrlsEnabled = $this->workingContext->getOption('friendly_urls', false) && (!$this->getProperty('reloadOnly', false) || !empty($pageTitle)); // Check for duplicates $duplicateContext = $this->workingContext->getOption('global_duplicate_uri_check', false) ? '' : $this->getProperty('context_key'); $aliasPath = $this->object->getAliasPath($alias, $this->getProperties()); $duplicateId = $this->object->isDuplicateAlias($aliasPath, $duplicateContext); // We have a duplicate! if ($duplicateId) { // If friendly urls is enabled, we throw an error about the alias if ($friendlyUrlsEnabled) { $err = $this->modx->lexicon('duplicate_uri_found', array('id' => $duplicateId, 'uri' => $aliasPath)); $this->addFieldError('uri', $err); if ($this->getProperty('uri_override', 0) !== 1) { $this->addFieldError('alias', $err); } } elseif ($autoGenerated) { $alias = ''; } } // If the alias is empty yet friendly urls is enabled, add an error to the alias field if (empty($alias) && $friendlyUrlsEnabled) { $this->addFieldError('alias', $this->modx->lexicon('field_required')); } // Set the new alias and return it, too. $this->setProperty('alias', $alias); return $alias; }
/** * @return array|string */ public function process() { if (!($data = $this->handleFile())) { return $this->failure($this->modx->lexicon('ms2gallery_err_gallery_ns')); } $properties = $this->mediaSource->getPropertyList(); $pathinfo = $this->ms2Gallery->pathinfo($data['name']); $extension = strtolower($pathinfo['extension']); $filename = strtolower($pathinfo['filename']); $image_extensions = $allowed_extensions = array(); if (!empty($properties['imageExtensions'])) { $image_extensions = array_map('trim', explode(',', strtolower($properties['imageExtensions']))); } if (!empty($properties['allowedFileTypes'])) { $allowed_extensions = array_map('trim', explode(',', strtolower($properties['allowedFileTypes']))); } if (!empty($allowed_extensions) && !in_array($extension, $allowed_extensions)) { return $this->failure($this->modx->lexicon('ms2gallery_err_wrong_ext')); } else { if (in_array($extension, $image_extensions)) { if (empty($data['properties']['height']) || empty($data['properties']['width'])) { return $this->failure($this->modx->lexicon('ms2gallery_err_wrong_image')); } $type = 'image'; } else { $type = $extension; } } if ($this->modx->getOption('ms2gallery_duplicate_check', null, true, true)) { if ($this->modx->getCount('msResourceFile', array('resource_id' => $this->resource->id, 'hash' => $data['hash'], 'parent' => 0))) { return $this->failure($this->modx->lexicon('ms2gallery_err_gallery_exists')); } } $filename = !empty($properties['imageNameType']) && $properties['imageNameType'] == 'friendly' ? $this->resource->cleanAlias($filename) : $data['hash']; $filename = str_replace(',', '', $filename) . '.' . $extension; $tmp_filename = $filename; $i = 1; while (true) { if (!($count = $this->modx->getCount('msResourceFile', array('resource_id' => $this->resource->id, 'file' => $tmp_filename, 'parent' => 0)))) { $filename = $tmp_filename; break; } else { $pcre = '#(-' . ($i - 1) . '|)\\.' . $extension . '$#'; $tmp_filename = preg_replace($pcre, "-{$i}.{$extension}", $tmp_filename); $i++; } } $rank = isset($properties['imageUploadDir']) && empty($properties['imageUploadDir']) ? 0 : $this->modx->getCount('msResourceFile', array('parent' => 0, 'resource_id' => $this->resource->id)); /* @var msResourceFile $uploaded_file */ $uploaded_file = $this->modx->newObject('msResourceFile', array('resource_id' => $this->resource->id, 'parent' => 0, 'name' => preg_replace('#\\.' . $extension . '$#i', '', $data['name']), 'file' => $filename, 'path' => $this->resource->id . '/', 'source' => $this->mediaSource->get('id'), 'type' => $type, 'rank' => $rank, 'createdon' => date('Y-m-d H:i:s'), 'createdby' => $this->modx->user->id, 'active' => 1, 'hash' => $data['hash'], 'properties' => $data['properties'])); $this->mediaSource->createContainer($uploaded_file->get('path'), '/'); $this->mediaSource->errors = array(); if ($this->mediaSource instanceof modFileMediaSource) { $upload = $this->mediaSource->createObject($uploaded_file->get('path'), $uploaded_file->get('file'), ''); if ($upload) { copy($data['tmp_name'], urldecode($upload)); } } else { $upload = $this->mediaSource->uploadObjectsToContainer($uploaded_file->get('path'), array($data)); } unlink($data['tmp_name']); if ($upload) { $url = $this->mediaSource->getObjectUrl($uploaded_file->get('path') . $uploaded_file->get('file')); $uploaded_file->set('url', $url); $uploaded_file->save(); if (empty($rank)) { $imagesTable = $this->modx->getTableName('msResourceFile'); $sql = "UPDATE {$imagesTable} SET rank = rank + 1 WHERE resource_id ='" . $this->resource->id . "' AND id !='" . $uploaded_file->get('id') . "'"; $this->modx->exec($sql); } $generate = $uploaded_file->generateThumbnails($this->mediaSource); if ($generate !== true) { $this->modx->log(modX::LOG_LEVEL_ERROR, 'Could not generate thumbnails for image with id = ' . $uploaded_file->get('id') . '. ' . $generate); return $this->failure($this->modx->lexicon('ms2gallery_err_gallery_thumb')); } else { return $this->success('', $uploaded_file); } } else { return $this->failure($this->modx->lexicon('ms2gallery_err_gallery_save') . ': ' . print_r($this->mediaSource->getErrors(), 1)); } }
/** * Calculate a resource alias from the imported file * * @param modResource $resource A reference to the new modResource object * @param string $alias A suggested alias * @param integer $parent The parent ID of the Resource * @param string $context The context of the Resource * @return string The formatted alias */ public function getResourceAlias(&$resource, $alias, $parent, $context = 'web') { // auto assign alias if ($alias == '' && $this->modx->getOption('automatic_alias')) { $alias = strtolower(trim($resource->cleanAlias($resource->get('pagetitle')))); } else { $alias = $resource->cleanAlias($alias); } $resourceContext = $this->modx->getObject('modContext', $context); $resourceContext->prepare(true); $isHtml = true; $extension = ''; $containerSuffix = $this->modx->getOption('container_suffix', null, ''); /** @var modContentType $contentType */ if ($contentType = $resource->getOne('ContentType')) { $extension = $contentType->getExtension(); $isHtml = strpos($contentType->get('mime_type'), 'html') !== false; } if ($resource->get('isfolder') && $isHtml && !empty($containerSuffix)) { $extension = $containerSuffix; } $aliasPath = ''; if ($this->modx->getOption('use_alias_path')) { $pathParentId = intval($parent); $parentResources = array(); $currResource = $this->modx->getObject('modResource', $pathParentId); while ($currResource) { $parentAlias = $currResource->get('alias'); if (empty($parentAlias)) { $parentAlias = "{$pathParentId}"; } $parentResources[] = "{$parentAlias}"; $pathParentId = $currResource->get('parent'); $currResource = $currResource->getOne('Parent'); } $aliasPath = !empty($parentResources) ? implode('/', array_reverse($parentResources)) : ''; } $fullAlias = $aliasPath . $alias . $extension; $iterations = 3; $origAlias = $alias; while ($duplicateId = $this->modx->findResource($fullAlias) && $iterations > 0) { $iterations--; $this->log($this->modx->lexicon('import_duplicate_alias_found', array('id' => $duplicateId, 'alias' => $fullAlias))); $alias = $origAlias . '-' . substr(uniqid(''), -3); $fullAlias = $aliasPath . $alias . $extension; } return $alias; }
public function cleanAlias($name) { $res = new modResource($this->xpdo); $name = str_replace('/', '-', $name); return $res->cleanAlias($name); }