/** * @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)); } }
/** * Register custom CSS/JS for the page * @return void */ public function loadCustomCssJs() { $this->addJavascript($this->modx->getOption('manager_url') . 'assets/modext/sections/system/file/create.js'); $this->addHtml('<script type="text/javascript">Ext.onReady(function() { MODx.load({ xtype: "modx-page-file-create" ,record: { directory: "' . $this->directory . '", source: "' . $this->source->get('id') . '" } }); });</script>'); }
public function getAccess() { $c = $this->modx->newQuery('sources.modAccessMediaSource'); $c->innerJoin('sources.modMediaSource', 'Target'); $c->innerJoin('modAccessPolicy', 'Policy'); $c->innerJoin('modUserGroup', 'Principal'); $c->innerJoin('modUserGroupRole', 'MinimumRole'); $c->where(array('target' => $this->source->get('id'))); $c->select($this->modx->getSelectColumns('sources.modAccessMediaSource', 'modAccessMediaSource')); $c->select(array('target_name' => 'Target.name', 'principal_name' => 'Principal.name', 'policy_name' => 'Policy.name', 'authority_name' => 'MinimumRole.name')); $acls = $this->modx->getCollection('sources.modAccessMediaSource', $c); $access = array(); /** @var modAccessMediaSource $acl */ foreach ($acls as $acl) { $access[] = array($acl->get('id'), $acl->get('target'), $acl->get('target_name'), $acl->get('principal_class'), $acl->get('principal'), $acl->get('principal_name'), $acl->get('authority'), $acl->get('authority_name'), $acl->get('policy'), $acl->get('policy_name'), $acl->get('context_key')); } $this->sourceArray['access'] = $this->modx->toJSON($access); }
/** * Sets access permissions for the source * @return void */ public function setAccess() { $access = $this->getProperty('access', null); if ($access !== null) { $acls = $this->modx->getCollection('sources.modAccessMediaSource', array('target' => $this->object->get('id'))); /** @var modAccessMediaSource $acl */ foreach ($acls as $acl) { $acl->remove(); } $access = is_array($access) ? $access : $this->modx->fromJSON($access); if (!empty($access) && is_array($access)) { foreach ($access as $data) { $acl = $this->modx->newObject('sources.modAccessMediaSource'); $acl->fromArray(array('target' => $this->object->get('id'), 'principal_class' => $data['principal_class'], 'principal' => $data['principal'], 'authority' => $data['authority'], 'policy' => $data['policy'], 'context_key' => $data['context_key']), '', true, true); $acl->save(); } } } }
/** * @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)); } }
/** * Log a manager action * @return void */ public function logManagerAction() { $this->modx->logManagerAction('source_delete', 'sources.modMediaSource', $this->source->get('id')); }