/**
  * Initializes component into different contexts.
  *
  * @param string $ctx The context to load. Defaults to web.
  * @param array $scriptProperties
  *
  * @return boolean
  */
 public function initialize($ctx = 'web', $scriptProperties = array())
 {
     $this->config = array_merge($this->config, $scriptProperties);
     $this->config['ctx'] = $ctx;
     if (empty($this->initialized[$ctx])) {
         $properties = $this->ms2Gallery->getSourceProperties();
         $config_js = array('ctx' => $ctx, 'jsUrl' => $this->config['jsUrl'] . 'web/', 'cssUrl' => $this->config['cssUrl'] . 'web/', 'actionUrl' => $this->config['actionUrl'], 'source' => array('size' => !empty($properties['maxUploadSize']) ? $properties['maxUploadSize'] : 3145728, 'height' => !empty($properties['maxUploadHeight']) ? $properties['maxUploadHeight'] : 1080, 'width' => !empty($properties['maxUploadWidth']) ? $properties['maxUploadWidth'] : 1920, 'extensions' => !empty($properties['allowedFileTypes']) ? $properties['allowedFileTypes'] : 'jpg,jpeg,png,gif'));
         $this->modx->regClientStartupScript('<script type="text/javascript">ms2GalleryFormConfig=' . $this->modx->toJSON($config_js) . '</script>', true);
         $css = !empty($this->config['frontend_css']) ? $this->config['frontend_css'] : $this->config['cssUrl'] . 'web/default.css';
         if (!empty($css) && preg_match('/\\.css/i', $css)) {
             $this->modx->regClientCSS($css);
         }
         $js = !empty($this->config['frontend_js']) ? $this->config['frontend_js'] : $this->config['jsUrl'] . 'web/default.js';
         if (!empty($js) && preg_match('/\\.js/i', $js)) {
             $this->modx->regClientScript($js);
         }
         $this->modx->regClientScript($this->config['jsUrl'] . 'web/lib/plupload/plupload.full.min.js');
         $this->modx->regClientScript($this->config['jsUrl'] . 'web/files.js');
         $lang = $this->modx->getOption('cultureKey');
         if ($lang != 'en' && file_exists($this->config['jsUrl'] . 'web/lib/plupload/i18n/' . $lang . '.js')) {
             $this->modx->regClientScript($this->config['jsUrl'] . 'web/lib/plupload/i18n/' . $lang . '.js');
         }
         $this->initialized[$ctx] = true;
     }
     return true;
 }
Example #2
0
 /**
  * @return void
  */
 public function setUp()
 {
     parent::setUp();
     $this->modx->loadClass('sources.modMediaSource');
     $this->source = $this->modx->newObject('sources.modMediaSource');
     $this->source->fromArray(array('name' => 'UnitTestSource', 'description' => '', 'class_key' => 'sources.modFileMediaSource', 'properties' => array()), '', true);
 }
Example #3
0
 /**
  * Get the active Source
  * @return modMediaSource|boolean
  */
 public function getSource()
 {
     $this->modx->loadClass('sources.modMediaSource');
     $this->source = modMediaSource::getDefaultSource($this->modx, $this->getProperty('source'));
     if (empty($this->source) || !$this->source->getWorkingContext()) {
         return false;
     }
     return $this->source;
 }
Example #4
0
 /**
  * Get the active Source
  * @return modMediaSource|boolean
  */
 public function getSource()
 {
     $this->modx->loadClass('sources.modMediaSource');
     $this->source = $this->modx->getObject('modMediaSource', $_POST['sourceID']);
     if (empty($this->source) || !$this->source->getWorkingContext()) {
         return false;
     }
     return $this->source;
 }
Example #5
0
 public function process()
 {
     if (!($data = $this->handleFile())) {
         return $this->failure($this->modx->lexicon('ticket_err_file_ns'));
     }
     $properties = $this->mediaSource->getPropertyList();
     $tmp = explode('.', $data['name']);
     $extension = strtolower(end($tmp));
     $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('ticket_err_file_ext'));
     } elseif (in_array($extension, $image_extensions)) {
         $type = 'image';
     } else {
         $type = $extension;
     }
     $hash = sha1($data['stream']);
     $path = '0/';
     $filename = !empty($properties['imageNameType']) && $properties['imageNameType'] == 'friendly' ? $this->ticket->cleanAlias($data['name']) : $hash . '.' . $extension;
     if (strpos($filename, '.' . $extension) === false) {
         $filename .= '.' . $extension;
     }
     // Check for existing file
     $where = $this->modx->newQuery($this->classKey, array('class' => $this->class));
     if (!empty($this->ticket->id)) {
         $where->andCondition(array('parent:IN' => array(0, $this->ticket->id)));
     } else {
         $where->andCondition(array('parent' => 0));
     }
     $where->andCondition(array('file' => $filename, 'OR:hash:=' => $hash), null, 1);
     if ($this->modx->getCount($this->classKey, $where)) {
         return $this->failure($this->modx->lexicon('ticket_err_file_exists', array('file' => $data['name'])));
     }
     /* @var TicketFile $ticket_file */
     $ticket_file = $this->modx->newObject('TicketFile', array('parent' => 0, 'name' => $data['name'], 'file' => $filename, 'path' => $path, 'source' => $this->mediaSource->id, 'type' => $type, 'createdon' => date('Y-m-d H:i:s'), 'createdby' => $this->modx->user->id, 'deleted' => 0, 'hash' => $hash, 'size' => $data['size'], 'class' => $this->class, 'properties' => $data['properties']));
     $this->mediaSource->createContainer($ticket_file->path, '/');
     unset($this->mediaSource->errors['file']);
     $file = $this->mediaSource->createObject($ticket_file->get('path'), $ticket_file->get('file'), $data['stream']);
     if ($file) {
         $url = $this->mediaSource->getObjectUrl($ticket_file->get('path') . $ticket_file->get('file'));
         $ticket_file->set('url', $url);
         $ticket_file->save();
         $ticket_file->generateThumbnail($this->mediaSource);
         return $this->success('', $ticket_file->toArray());
     } else {
         $this->modx->log(modX::LOG_LEVEL_ERROR, '[Tickets] Could not save file: ' . print_r($this->mediaSource->getErrors(), 1));
         return $this->failure($this->modx->lexicon('ticket_err_file_save'));
     }
 }
Example #6
0
 /**
  * @return boolean|string
  */
 public function getSource()
 {
     /** @var modMediaSource $source */
     $this->modx->loadClass('sources.modMediaSource');
     $this->source = $this->modx->getObject('modMediaSource', $_POST['sourceID']);
     if (!$this->source->getWorkingContext()) {
         return $this->modx->lexicon('permission_denied');
     }
     $this->source->setRequestProperties($this->getProperties());
     return $this->source->initialize();
 }
Example #7
0
 /**
  * @return boolean|string
  */
 public function getSource()
 {
     $source = $this->getProperty('source', 1);
     /** @var modMediaSource $source */
     $this->modx->loadClass('sources.modMediaSource');
     $this->source = modMediaSource::getDefaultSource($this->modx, $source);
     if (!$this->source->getWorkingContext()) {
         return $this->modx->lexicon('permission_denied');
     }
     $this->source->setRequestProperties($this->getProperties());
     return $this->source->initialize();
 }
Example #8
0
 /**
  * Get the source to load the paths from
  * 
  * @param int $sourceId
  * @return modMediaSource|modFileMediaSource
  */
 public function getSource($sourceId)
 {
     /** @var modMediaSource|modWebDAVMediaSource $source */
     $this->modx->loadClass('sources.modMediaSource');
     $this->source = modMediaSource::getDefaultSource($this->modx, $sourceId, false);
     if (empty($this->source)) {
         return false;
     }
     if (!$this->source->getWorkingContext()) {
         return false;
     }
     $this->source->setRequestProperties($this->getProperties());
     $this->source->initialize();
     return $this->source;
 }
Example #9
0
 public function process()
 {
     if (!$this->validate()) {
         return $this->failure();
     }
     $source = $this->getProperty('source', 1);
     /** @var modMediaSource $source */
     $this->modx->loadClass('sources.modMediaSource');
     $source = modMediaSource::getDefaultSource($this->modx, $source);
     if (!$source->getWorkingContext()) {
         return $this->failure($this->modx->lexicon('permission_denied'));
     }
     $source->setRequestProperties($this->getProperties());
     $source->initialize();
     $success = $source->renameContainer($this->getProperty('dir'), $this->getProperty('name'));
     if (!$success) {
         $msg = '';
         $errors = $source->getErrors();
         foreach ($errors as $k => $msg) {
             $this->addFieldError($k, $msg);
         }
         return $this->failure($msg);
     }
     return $this->success();
 }
Example #10
0
 public function process()
 {
     $from = $this->getProperty('from');
     $to = $this->getProperty('to');
     $point = $this->getProperty('point', 'append');
     if (empty($from)) {
         return $this->failure($this->modx->lexicon('file_folder_err_ns'));
     }
     if (empty($to)) {
         return $this->failure($this->modx->lexicon('file_folder_err_ns'));
     }
     $source = $this->getProperty('source', 1);
     /** @var modMediaSource $source */
     $this->modx->loadClass('sources.modMediaSource');
     $source = modMediaSource::getDefaultSource($this->modx, $source);
     if (!$source->getWorkingContext()) {
         return $this->failure($this->modx->lexicon('permission_denied'));
     }
     $source->setRequestProperties($this->getProperties());
     $source->initialize();
     if (!$source->checkPolicy('save')) {
         return $this->failure($this->modx->lexicon('permission_denied'));
     }
     $success = $source->moveObject($from, $to, $point);
     if (!$success) {
         $errors = $source->getErrors();
         foreach ($errors as $k => $msg) {
             $this->addFieldError($k, $msg);
         }
         return $this->failure($this->modx->error->message);
     }
     return $this->success();
 }
Example #11
0
 /**
  * @param $new_name
  * @param $file
  * @return string $url_name
  */
 protected function createAsset($new_name, $file, $remote = true)
 {
     //$this->modx->importx->log('error', 'Create Asset ');
     if (is_object($this->mediaSource)) {
         //$this->modx->importx->log('error', 'Yes: '.$file);
         if ($remote && strpos($file, 'http') !== 0) {
             $file = rtrim($this->config['link'], '/') . '/' . ltrim($file, '/');
         }
         // directory:
         $object_path = '';
         if (in_array(strtolower(pathinfo($new_name, PATHINFO_EXTENSION)), array('doc', 'pdf', 'docx', 'csv', 'xlsx'))) {
             $object_path = 'docs' . DIRECTORY_SEPARATOR;
         }
         $content = $remote ? $this->getRemoteData($file) : file_get_contents($file);
         $file = rawurldecode($this->mediaSource->createObject($object_path, $new_name, $content));
         $basePath = $this->mediaSource->getBasePath($file);
         $baseUrl = $this->mediaSource->getBaseUrl($file);
         $file = str_replace($basePath, $baseUrl, $file);
         if ($file !== false) {
             $this->modx->importx->log('info', 'File transferred to: ' . $file);
         }
         return $file;
     }
     return false;
 }
Example #12
0
 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);
 }
 /**
  * @param string $ctx
  * @param $source
  *
  * @return bool|null|object
  */
 public function initializeMediaSource($ctx = '', $source)
 {
     if ($this->mediaSource = $this->modx->getObject('sources.modMediaSource', $source)) {
         $this->mediaSource->set('ctx', $ctx);
         $this->mediaSource->initialize();
         return $this->mediaSource;
     } else {
         return false;
     }
 }
Example #14
0
 /**
  * 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();
             }
         }
     }
 }
 /**
  * {@inheritDoc}
  * @return boolean
  */
 public function initialize()
 {
     parent::initialize();
     $options = array();
     if (!$this->ctx) {
         $this->ctx =& $this->xpdo->context;
     }
     $options['context'] = $this->ctx->get('key');
     $this->fileHandler = $this->xpdo->getService('fileHandler', 'modFileHandler', '', $options);
     return true;
 }
Example #16
0
 /**
  * @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));
     }
 }
 /**
  * Initialize the source
  * @return boolean
  */
 public function initialize()
 {
     $ok = parent::initialize();
     if ($ok !== true) {
         return $ok;
     }
     if (!$this->ctx) {
         $this->ctx =& $this->xpdo->context;
     }
     $this->ctx->prepare();
     $this->_properties = $this->getPropertyList();
     return true;
 }
 /**
  * @param array $ancestors
  *
  * @return bool
  */
 public function remove(array $ancestors = array())
 {
     $filename = $this->get('path') . $this->get('file');
     if ($this->prepareSource()) {
         $this->mediaSource->removeObject($filename);
         if (!empty($this->mediaSource->errors['file'])) {
             $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, '[Uploadify] Could not remove file at "' . $filename . '": ' . $this->mediaSource->errors['file']);
         }
     }
     $children = $this->xpdo->getIterator('uFile', array('parent' => $this->get('id')));
     /** @var uFile $child */
     foreach ($children as $child) {
         $child->remove();
     }
     return parent::remove($ancestors);
 }
Example #19
0
 public function getElementSources()
 {
     $c = $this->modx->newQuery('modContext');
     $c->where(array('key:!=' => 'mgr'));
     $c->sortby($this->modx->escape('rank'), 'ASC');
     $c->sortby($this->modx->escape('key'), 'DESC');
     $contexts = $this->modx->getCollection('modContext', $c);
     $list = array();
     $this->modx->loadClass('sources.modMediaSource');
     /** @var $source modMediaSource */
     $source = modMediaSource::getDefaultSource($this->modx);
     /** @var modContext $context */
     foreach ($contexts as $context) {
         $list[] = array($context->get('key'), $source->get('id'), $source->get('name'));
     }
     return $list;
 }
Example #20
0
 /**
  * Get the active source
  * @return modMediaSource
  */
 public function getSource()
 {
     /** @var modMediaSource|modFileMediaSource $source */
     $this->modx->loadClass('sources.modMediaSource');
     $source = $this->modx->getOption('source', $this->scriptProperties, false);
     if (!empty($source)) {
         $source = $this->modx->getObject('source.modMediaSource', $source);
     }
     if (empty($source)) {
         $source = modMediaSource::getDefaultSource($this->modx);
     }
     if (!$source->getWorkingContext()) {
         return $this->failure($this->modx->lexicon('permission_denied'));
     }
     $source->setRequestProperties($this->scriptProperties);
     $source->initialize();
     return $source;
 }
 /**
  * Initializes S3 media class, getting the S3 driver and loading the bucket
  * @return boolean
  */
 public function initialize()
 {
     parent::initialize();
     $properties = $this->getPropertyList();
     if (!defined('AWS_KEY')) {
         define('AWS_KEY', $this->xpdo->getOption('key', $properties, ''));
         define('AWS_SECRET_KEY', $this->xpdo->getOption('secret_key', $properties, ''));
         /* (Not needed at this time)
            define('AWS_ACCOUNT_ID',$modx->getOption('aws.account_id',$config,''));
            define('AWS_CANONICAL_ID',$modx->getOption('aws.canonical_id',$config,''));
            define('AWS_CANONICAL_NAME',$modx->getOption('aws.canonical_name',$config,''));
            define('AWS_MFA_SERIAL',$modx->getOption('aws.mfa_serial',$config,''));
            define('AWS_CLOUDFRONT_KEYPAIR_ID',$modx->getOption('aws.cloudfront_keypair_id',$config,''));
            define('AWS_CLOUDFRONT_PRIVATE_KEY_PEM',$modx->getOption('aws.cloudfront_private_key_pem',$config,''));
            define('AWS_ENABLE_EXTENSIONS', 'false');*/
     }
     include_once $this->xpdo->getOption('core_path', null, MODX_CORE_PATH) . 'model/aws/sdk.class.php';
     $this->getDriver();
     $this->setBucket($this->xpdo->getOption('bucket', $properties, ''));
     return true;
 }
Example #22
0
 /**
  * Get the active Source
  * @return modMediaSource|boolean
  */
 public function getSource()
 {
     $path = $this->modx->getOption('modxsdk.core_path', null);
     if (!$path) {
         $path = MODX_CORE_PATH . 'components/modxsdk/';
     }
     $path .= 'model/modxSDK/';
     if (!$this->modx->loadClass('ModxsdkFileMediaSource', $path)) {
         return false;
     }
     $source = modMediaSource::getDefaultSource($this->modx, $this->getProperty('source'));
     if (empty($source) || !$source->getWorkingContext()) {
         return false;
     }
     $this->source = $this->modx->newObject('ModxsdkFileMediaSource');
     if (!$this->source->checkPolicy('list')) {
         return 'Source access denied';
     }
     $this->source->fromArray($source->toArray());
     $this->source->setRequestProperties($this->getProperties());
     $this->source->initialize();
     return true;
 }
 /**
  * {@inheritDoc}
  * @return boolean
  */
 public function initialize()
 {
     parent::initialize();
     $properties = $this->getPropertyList();
     $this->cached = $this->getOption('cached', $properties, false);
     $this->cacheTime = $this->getOption('cacheTime', $properties, 10) * 60;
     $this->proxified = $this->getOption('proxy', $properties, false);
     $this->preview = $this->getOption('preview', $properties, false);
     $this->basePath = $this->getOption('basePath', $properties);
     if (substr($this->basePath, -1) != '/') {
         $this->basePath .= '/';
     }
     if ($this->proxified) {
         $this->baseUrl = $this->xpdo->getOption('site_url') . ltrim($this->xpdo->getOption('assets_url'), '/') . 'components/webdav/index.php?action=proxy&source=' . $this->get('id') . '&ctx=' . $this->xpdo->context->key . '&src=';
     } else {
         $this->baseUrl = $this->getOption('baseUrl', $properties) . '/';
         if ($this->basePath != '/') {
             $this->baseUrl .= $this->basePath;
         }
     }
     $this->client = new WebDAV_Client(array('uri' => $this->getOption('server', $properties), 'path' => $this->basePath, 'user' => $this->getOption('login', $properties), 'password' => $this->getOption('password', $properties), 'auth' => $this->getOption('authMethod', $properties), 'ssl' => $this->getOption('verifySSL', $properties)));
     $this->xpdo->lexicon->load('webdav:default', 'webdav:source');
     return true;
 }
Example #24
0
 /**
  * @param $source_id
  * @return bool|modFileMediaSource|modMediaSource|null
  */
 protected function _getSource($source_id)
 {
     $this->modx->loadClass('sources.modMediaSource');
     $source = modMediaSource::getDefaultSource($this->modx, $source_id);
     if (empty($source) || !$source->getWorkingContext()) {
         return false;
     }
     $images = $this->modx->getOption('imageExtensions', $source->getPropertyList(), 'jpg,jpeg,png,gif');
     $this->_images = array_map('trim', explode(',', $images));
     return $source;
 }
Example #25
0
 function getFieldSource($field, &$tv)
 {
     //source from config
     $sourcefrom = isset($field['sourceFrom']) && !empty($field['sourceFrom']) ? $field['sourceFrom'] : 'config';
     if ($sourcefrom == 'config' && isset($field['sources'])) {
         if (is_array($field['sources'])) {
             foreach ($field['sources'] as $context => $sourceid) {
                 $sources[$context] = $sourceid;
             }
         } else {
             $fsources = $this->modx->fromJson($field['sources']);
             if (is_array($fsources)) {
                 foreach ($fsources as $source) {
                     if (isset($source['context']) && isset($source['sourceid'])) {
                         $sources[$source['context']] = $source['sourceid'];
                     }
                 }
             }
         }
     }
     if (isset($sources[$this->working_context]) && !empty($sources[$this->working_context])) {
         //try using field-specific mediasource from config
         if ($mediasource = $this->modx->getObject('sources.modMediaSource', $sources[$this->working_context])) {
             return $mediasource;
         }
     }
     if ($this->source && $sourcefrom == 'migx') {
         //use global MIGX-mediasource for all TVs
         $tv->setSource($this->source);
         $mediasource = $this->source;
     } else {
         //useTV-specific mediasource
         $mediasource = $tv->getSource($this->working_context, false);
     }
     //try to get the context-default-media-source
     if (!$mediasource) {
         $defaultSourceId = null;
         if ($contextSetting = $this->modx->getObject('modContextSetting', array('key' => 'default_media_source', 'context_key' => $this->working_context))) {
             $defaultSourceId = $contextSetting->get('value');
         }
         $mediasource = modMediaSource::getDefaultSource($this->modx, $defaultSourceId);
     }
     return $mediasource;
 }
 /**
  * @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));
     }
 }
 /**
  * Get the Source for this Element
  *
  * @param string $contextKey
  * @param boolean $fallbackToDefault
  * @return modMediaSource|null
  */
 public function getSource($contextKey = '', $fallbackToDefault = true)
 {
     if (empty($contextKey)) {
         $contextKey = $this->xpdo->context->get('key');
     }
     $source = $this->_source;
     if (empty($source)) {
         $c = $this->xpdo->newQuery('sources.modMediaSource');
         $c->innerJoin('sources.modMediaSourceElement', 'SourceElement');
         $c->where(array('SourceElement.object' => $this->get('id'), 'SourceElement.object_class' => $this->_class, 'SourceElement.context_key' => $contextKey));
         $source = $this->xpdo->getObject('sources.modMediaSource', $c);
         if (!$source && $fallbackToDefault) {
             $source = modMediaSource::getDefaultSource($this->xpdo);
         }
         $this->setSource($source);
     }
     return $source;
 }
Example #28
0
    $resource = $modx->newObject('modResource');
}
//set objectid for migxObjectMediaPath - snippet
$co_id = $modx->getOption('co_id', $scriptProperties, '');
if (!empty($co_id)) {
    $modx->setPlaceholder('objectid', $co_id);
}
if ($tv = $modx->getObject('modTemplateVar', array('name' => $tvname))) {
    $source = $tv->getSource($wctx, false);
}
if (!$source instanceof modMediaSource) {
    $sourceid = $modx->getOption('source', $_REQUEST, '');
    /**
     *  *  *  * @var modMediaSource $source */
    $modx->loadClass('sources.modMediaSource');
    $source = modMediaSource::getDefaultSource($modx, $sourceid);
    if (!$source->getWorkingContext()) {
        return $modx->lexicon('permission_denied');
    }
    $source->setRequestProperties($_REQUEST);
}
$result = array();
if ($source instanceof modMediaSource) {
    $dirTree = $modx->getOption('dirtree', $_REQUEST, '');
    $modx->setPlaceholder('docid', $resource_id);
    $source->initialize();
    $cachepath = str_replace('/./', '/', $source->getBasePath());
    $cachepath = $cachepath . $dirTree;
    $baseUrl = $modx->getOption('site_url') . $source->getBaseUrl();
    $baseUrl = $baseUrl . $dirTree;
    $sourceProperties = $source->getPropertyList();
Example #29
0
 /**
  * Retrieve the profile photo, if any
  *
  * @param int $width The desired photo width
  * @param int $height The desired photo height
  *
  * @return string The photo URL
  */
 public function getProfilePhoto($width = 128, $height = 128)
 {
     if (empty($this->Profile->photo)) {
         return '';
     }
     $this->xpdo->loadClass('sources.modMediaSource');
     /** @var modMediaSource $source */
     $source = modMediaSource::getDefaultSource($this->xpdo, $this->xpdo->getOption('photo_profile_source'));
     $source->initialize();
     $path = $source->getBasePath($this->Profile->photo) . $this->Profile->photo;
     return $this->xpdo->getOption('connectors_url', MODX_CONNECTORS_URL) . "system/phpthumb.php?zc=1&h={$height}&w={$width}&src={$path}";
 }
Example #30
0
 function getDefaultSource($return = 'object')
 {
     $defaultSourceId = null;
     if ($contextSetting = $this->modx->getObject('modContextSetting', array('key' => 'default_media_source', 'context_key' => $this->working_context))) {
         $defaultSourceId = $contextSetting->get('value');
     }
     $mediasource = modMediaSource::getDefaultSource($this->modx, $defaultSourceId);
     return $return == 'object' ? $mediasource : $mediasource->get($return);
 }