|
<?php |
|
|
|
/* Author: Angel Magaña -- cheleguanaco@cheleguanaco.com |
|
* File: ./custom/include/CustomSplitAttachFolder.php |
|
* |
|
* UploadStream extension to add use of date named |
|
* subfolders to ./upload directory. |
|
* |
|
* e.g. ./upload/2015/04/02/<MyApril2nd2015File.docRepresentedAsGUID> |
|
* |
|
* Enabled via: $sugar_config['upload_wrapper_class'] = 'CustomSplitAttachFolder'; |
|
*/ |
|
|
|
class CustomSplitAttachFolder extends UploadStream |
|
{ |
|
public function __construct() |
|
{ |
|
parent::__construct(); |
|
} |
|
|
|
/** |
|
* Get upload directory |
|
* @return string |
|
*/ |
|
public static function getDir() |
|
{ |
|
if(empty(parent::$upload_dir)) { |
|
parent::$upload_dir = rtrim($GLOBALS['sugar_config']['upload_dir'], '/\\') . self::getSubDir(); |
|
|
|
if(empty(parent::$upload_dir)) { |
|
parent::$upload_dir = 'upload' . self::getSubDir(); |
|
} |
|
|
|
if(!file_exists(parent::$upload_dir)) { |
|
sugar_mkdir(parent::$upload_dir, 0755, true); |
|
} |
|
} |
|
|
|
return parent::$upload_dir; |
|
} |
|
|
|
public static function getSubDir() |
|
{ |
|
$subdir = ''; |
|
$id = $GLOBALS['_REQUEST']['id']; |
|
|
|
if (empty($id)) |
|
{ |
|
$subdir = gmdate('/Y/m/d/'); |
|
} |
|
elseif (!empty($id)) |
|
{ |
|
$bean_type = $GLOBALS['_REQUEST']['type']; |
|
if ($bean_type == 'Documents') |
|
{ |
|
$bean_type = 'DocumentRevisions'; |
|
} |
|
$bean = BeanFactory::getBean($bean_type, $id); |
|
|
|
$dt = strtotime($bean->date_entered); |
|
$subdir = gmdate('/Y/m/d/', $dt); |
|
} |
|
|
|
return $subdir; |
|
} |
|
|
|
/** |
|
* Get real FS path of the upload stream file |
|
* Non-static version for overrides |
|
* @param string $path Upload stream path (with upload://) |
|
* @return string FS path |
|
*/ |
|
public function getFSPath($path, $mode = 0) |
|
{ |
|
$path = substr($path, strlen(parent::STREAM_NAME)+3); // cut off upload:// |
|
$path = str_replace("\\", "/", $path); // canonicalize path |
|
if($path == ".." || substr($path, 0, 3) == "../" || substr($path, -3, 3) == "/.." || strstr($path, "/../")) { |
|
return null; |
|
} |
|
|
|
if ($mode == 0) |
|
{ |
|
$path = self::getDir() . self::getSubDir() . $path; |
|
} |
|
elseif ($mode == 1) |
|
{ |
|
$path = self::getDir() . $path; |
|
} |
|
|
|
return $path; |
|
} |
|
|
|
//Here we open stream to download an attachment |
|
public function stream_open($path, $mode) |
|
{ |
|
$fullpath = $this->getFSPath($path, 1); |
|
if(empty($fullpath)) return false; |
|
if($mode == 'r') { |
|
$this->fp = fopen($fullpath, $mode); |
|
} else { |
|
// if we will be writing, try to transparently create the directory |
|
$this->fp = @fopen($fullpath, $mode); |
|
if(!$this->fp && !file_exists(dirname($fullpath))) { |
|
mkdir(dirname($fullpath), 0755, true); |
|
$this->fp = fopen($fullpath, $mode); |
|
} |
|
} |
|
return !empty($this->fp); |
|
} |
|
|
|
public function url_stat($path, $flags) |
|
{ |
|
return @stat($this->getFSPath($path, 1)); |
|
} |
|
|
|
} |