Skip to content
9 changes: 9 additions & 0 deletions _build/data/transport.settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@
'area' => '',
),'',true,true);

$settings['gallery.mediaSource']= $modx->newObject('modSystemSetting');
$settings['gallery.mediaSource']->fromArray(array(
'key' => 'gallery.mediaSource',
'value' => 1,
'xtype' => 'modx-combo-source',
'namespace' => 'gallery',
'area' => '',
),'',true,true);

/*
$settings['gallery.']= $modx->newObject('modSystemSetting');
$settings['gallery.']->fromArray(array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
$itemArray['cls'] .= ' '.$activeCls;
}
$itemArray['filename'] = basename($item->get('filename'));
$itemArray['image_absolute'] = $filesUrl.$item->get('filename');
$itemArray['image_absolute'] = $item->get('base_url').$filesUrl.$item->get('filename');
$itemArray['fileurl'] = $itemArray['image_absolute'];
$itemArray['filepath'] = $filesPath.$item->get('filename');
$itemArray['filesize'] = $item->get('filesize');
Expand Down
38 changes: 23 additions & 15 deletions core/components/gallery/model/gallery/galalbum.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,36 +195,44 @@ public function ensurePathExists() {
return $exists;
}

public function uploadItem(galItem $item,$filePath,$name) {
public function uploadItem(galItem $item,$filePath,$name,$mediaSource) {
$fileName = false;

$albumDir = $this->getPath(false);
$targetDir = $this->getPath();
$targetDir = str_ireplace(MODX_BASE_PATH, '', $this->getPath());

/* if directory doesnt exist, create it */
if (!$this->ensurePathExists()) {
$this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not create directory: '.$targetDir);
return $fileName;
}
if (!$this->isPathWritable()) {
$this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not write to directory: '.$targetDir);
return $fileName;
if (!$mediaSource->createContainer($targetDir,'/')) {
$this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not create directory (possibly already exists?): '.$targetDir);
}

/* upload the file */

$extension = pathinfo($name,PATHINFO_EXTENSION);
$shortName = $item->get('id').'.'.$extension;
$relativePath = $albumDir.$shortName;
$absolutePath = $targetDir.$shortName;

if (@file_exists($absolutePath)) {
@unlink($absolutePath);
}
if (!@move_uploaded_file($filePath,$absolutePath)) {
$this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] An error occurred while trying to upload the file: '.$filePath.' to '.$absolutePath);
$fileName = str_replace(' ','',$relativePath);

$file = array("name" => $shortName, "tmp_name" => $filePath,"error" => "0"); // emulate a $_FILES object

$success = true;
// modFileMediaSource class uses move_uploaded_file - because we create a local file - we cannot use this function and we use streams instead
if(!is_uploaded_file($filePath) && get_class($mediaSource) == 'modFileMediaSource_mysql') {
$input = fopen($filePath, "r");
$target = fopen($this->getPath(true).$shortName, "w");
$bytes = stream_copy_to_stream($input, $target);
fclose($input);
fclose($target);
} else {
$fileName = str_replace(' ','',$relativePath);
$success = $mediaSource->uploadObjectsToContainer($targetDir,array($file));
}

// if(!$success) {
// $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] An error occurred while trying to upload the file: '.$filePath.' to '.$absolutePath);
// return false;
// }
return $fileName;
}

Expand Down
56 changes: 54 additions & 2 deletions core/components/gallery/model/gallery/galitem.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@
* @package gallery
*/
class galItem extends xPDOSimpleObject {
private $mediaSource = false;

private function getMediaSource() {
if($this->mediaSource) return $this->mediaSource;
//get modMediaSource
$mediaSource = $this->xpdo->getOption('gallery.mediaSource',null,1);

$def = $this->xpdo->getObject('sources.modMediaSource',array(
'id' => $mediaSource,
));

$def->initialize();

$this->mediaSource = $def;

return $this->mediaSource;
}

public function get($k, $format = null, $formatTemplate= null) {
switch ($k) {
case 'thumbnail':
Expand All @@ -35,6 +53,12 @@ public function get($k, $format = null, $formatTemplate= null) {
$format['src'] = $this->getSiteUrl();
$format['src'] .= $this->xpdo->call('galAlbum','getFilesUrl',array(&$this->xpdo)).$filename;
}

$ms = $this->getMediaSource();
if($ms->getBaseUrl() != '/') {
$format['src'] = $ms->getBaseUrl().$this->xpdo->call('galAlbum','getFilesUrl',array(&$this->xpdo)).$filename;
}

$url = $value.'&'.http_build_query($format,'','&');
if ($this->xpdo->getOption('xhtml_urls',null,false)) {
$value = str_replace('&','&',$url);
Expand All @@ -52,12 +76,24 @@ public function get($k, $format = null, $formatTemplate= null) {
$format['src'] = $this->getSiteUrl();
$format['src'] .= $this->xpdo->call('galAlbum','getFilesUrl',array(&$this->xpdo)).$filename;
}

$ms = $this->getMediaSource();
if($ms->getBaseUrl() != '/') {
$format['src'] = $ms->getBaseUrl().$this->xpdo->call('galAlbum','getFilesUrl',array(&$this->xpdo)).$filename;
}

$value = $this->getPhpThumbUrl().'&'.http_build_query($format,'','&');
$value = $this->xpdo->getOption('xhtml_urls',null,false) ? str_replace('&','&',$value) : $value;
break;
case 'absoluteImage':
$siteUrl = $this->getSiteUrl();
$value = $siteUrl.$this->xpdo->call('galAlbum','getFilesUrl',array(&$this->xpdo)).$this->get('filename');

// $ms = $this->getMediaSource();
// if($ms->getBaseUrl() != '/') {
// $value = $ms->getBaseUrl().$this->xpdo->call('galAlbum','getFilesUrl',array(&$this->xpdo)).$filename;
// }

break;
case 'relativeImage':
$baseUrl = $this->getOption('base_url');
Expand All @@ -67,6 +103,12 @@ public function get($k, $format = null, $formatTemplate= null) {
} else {
$value = str_replace($baseUrl,'',$path);
}

// $ms = $this->getMediaSource(); // for absolute + relative the link NEEDS the http:// domain
// if($ms->getBaseUrl() != '/') {
// $value = $ms->getBaseUrl().$this->xpdo->call('galAlbum','getFilesUrl',array(&$this->xpdo)).$baseUrl;
// }

break;
case 'filesize':
$filename = $this->xpdo->call('galAlbum','getFilesPath',array(&$this->xpdo)).$this->get('filename');
Expand All @@ -75,6 +117,14 @@ public function get($k, $format = null, $formatTemplate= null) {
break;
case 'image_path':
$value = $this->xpdo->call('galAlbum','getFilesPath',array(&$this->xpdo)).$this->get('filename');
break;
case 'base_url':
$ms = $this->getMediaSource();
$value='';
if($ms->getBaseUrl() != '/') {
$value = $ms->getBaseUrl();
}

break;
default:
$value = parent::get($k,$format,$formatTemplate);
Expand Down Expand Up @@ -136,7 +186,7 @@ public function upload($file,$albumId) {
$album = $this->xpdo->getObject('galAlbum',$albumId);
if (empty($album)) return false;

$fileName = $album->uploadItem($this,$file['tmp_name'],$file['name']);
$fileName = $album->uploadItem($this,$file['tmp_name'], $file['name'], $this->getMediaSource());
if (empty($fileName)) {
return false;
}
Expand Down Expand Up @@ -168,7 +218,9 @@ public function remove(array $ancestors = array()) {
$filename = $this->get('filename');
if (!empty($filename)) {
$filename = $this->xpdo->call('galAlbum','getFilesPath',array(&$this->xpdo)).$filename;
if (!@unlink($filename)) {
$filename = str_ireplace(MODX_BASE_PATH, '', $filename);
$ms = $this->getMediaSource();
if (!@$ms->removeObject($filename)) {
$this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] An error occurred while trying to remove the attachment file at: '.$filename);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ public function importFile($file,array $options = array()) {
$newRelativePath = $this->albumId.'/'.$newFileName;
$newAbsolutePath = $this->target.'/'.$newFileName;

if (@file_exists($newAbsolutePath)) {
@unlink($newAbsolutePath);
}
if (!@copy($filePathName,$newAbsolutePath)) {
$file = array("name" => $newRelativePath, "tmp_name" => $filePathName, "error" => "0"); // emulate a $_FILES object

$success = $item->upload($file,$options['album']);
if(!$success) {
$errors[] = $this->modx->lexicon('gallery.file_err_move',array(
'file' => $newFileName,
'target' => $newAbsolutePath,
Expand Down
51 changes: 30 additions & 21 deletions core/components/gallery/processors/mgr/item/ajaxupload.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,59 @@
$albumDir = $album.'/';
$targetDir = $modx->call('galAlbum','getFilesPath',array(&$modx)).$albumDir;

$cacheManager = $modx->getCacheManager();
/* if directory doesnt exist, create it */
if (!file_exists($targetDir) || !is_dir($targetDir)) {
if (!$cacheManager->writeTree($targetDir)) {
$modx->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not create directory: '.$targetDir);
return $modx->toJSON(array('error' => 'Could not create directory: ' . $targetDir));
}
}
/* make sure directory is readable/writable */
if (!is_readable($targetDir) || !is_writable($targetDir)) {
$modx->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not write to directory: '.$targetDir);
return $modx->toJSON(array('error' => 'Could not write to directory: ' . $targetDir));
}
// $cacheManager = $modx->getCacheManager();
// /* if directory doesnt exist, create it */
// if (!file_exists($targetDir) || !is_dir($targetDir)) {
// if (!$cacheManager->writeTree($targetDir)) {
// $modx->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not create directory: '.$targetDir);
// return $modx->toJSON(array('error' => 'Could not create directory: ' . $targetDir));
// }
// }
// /* make sure directory is readable/writable */
// if (!is_readable($targetDir) || !is_writable($targetDir)) {
// $modx->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not write to directory: '.$targetDir);
// return $modx->toJSON(array('error' => 'Could not write to directory: ' . $targetDir));
// }

/* upload the file */
$extension = end(explode('.', $filenm));
$extension = @end(explode('.', $filenm));
$filename = $item->get('id').'.'.$extension;
$relativePath = $albumDir.$filename;
$absolutePath = $targetDir.$filename;

if (@file_exists($absolutePath)) {
@unlink($absolutePath);
}

if (!empty($_FILES['qqfile'])) {
if (!$item->upload($_FILES['qqfile'],$scriptProperties['album'])) {
$item->remove();
return $modx->error->failure($modx->lexicon('gallery.item_err_upload'));
}
} else {
/* Using AJAX upload */
$modx->log(xPDO::LOG_LEVEL_ERROR,'[GalleryAjaxUpload] filenm '.$filenm);

$length = 10;
$randomFilename = "/tmp/".substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length).".$extension";

/* Using AJAX upload - to tmp file then use the correct media source to upload */
$input = fopen("php://input", "r");
$target = fopen($absolutePath, "w");
$target = fopen($randomFilename, "w");
$bytes = stream_copy_to_stream($input, $target);
fclose($input);
fclose($target);

if ($bytes == 0) {

$file = array("name" => $relativePath, "tmp_name" => $randomFilename, "error" => "0"); // emulate a $_FILES object

$modx->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Album Type: '.$scriptProperties['album']);


if ($bytes == 0 || !$item->upload($file,$scriptProperties['album'])) {
$modx->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] An error occurred while trying to upload the file to '.$absolutePath);
$item->remove();
return $modx->toJSON(array('error' => 'gallery.item_err_upload'));
} else {
$item->set('filename',str_replace(' ','',$relativePath));
}

@unlink($target);
}

$item->save();
Expand Down
24 changes: 5 additions & 19 deletions core/components/gallery/processors/mgr/item/batchupload.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,6 @@

$targetDir = $modx->call('galAlbum','getFilesPath',array(&$modx)).$scriptProperties['album'].'/';

$cacheManager = $modx->getCacheManager();
/* if directory doesnt exist, create it */
if (!file_exists($targetDir) || !is_dir($targetDir)) {
if (!$cacheManager->writeTree($targetDir)) {
$modx->log(modX::LOG_LEVEL_ERROR,'[Gallery] Could not create directory: '.$targetDir);
return $modx->error->failure($modx->lexicon('gallery.directory_err_create',array('directory' => $targetDir)));
}
}
/* make sure directory is readable/writable */
if (!is_readable($targetDir) || !is_writable($targetDir)) {
$modx->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not write to directory: '.$targetDir);
return $modx->error->failure($modx->lexicon('gallery.directory_err_write',array('directory' => $targetDir)));
}

$imagesExts = array('jpg','jpeg','png','gif','bmp');
$use_multibyte = $modx->getOption('use_multibyte',null,false);
$encoding = $modx->getOption('modx_charset',null,'UTF-8');
Expand Down Expand Up @@ -101,11 +87,11 @@
$newFileName = $item->get('id').'.'.$fileExtension;
$newRelativePath = $scriptProperties['album'].'/'.$newFileName;
$newAbsolutePath = $targetDir.'/'.$newFileName;
if (@file_exists($newAbsolutePath)) {
@unlink($newAbsolutePath);
}
if (!@copy($filePathName,$newAbsolutePath)) {

$file = array("name" => $newRelativePath, "tmp_name" => $filePathName, "error" => "0"); // emulate a $_FILES object

$success = $item->upload($file,$scriptProperties['album']);
if(!$success) {
$errors[] = $modx->lexicon('gallery.file_err_move',array(
'file' => $newFileName,
'target' => $newAbsolutePath,
Expand Down