2012-03-29 01:27:18 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* EGroupware - eTemplate serverside vfs widget
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
* @copyright 2011 Nathan Gray
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* eTemplate VFS widget
|
|
|
|
* Deals with the Virtual File System
|
|
|
|
*/
|
|
|
|
class etemplate_widget_vfs extends etemplate_widget_file
|
|
|
|
{
|
|
|
|
|
2012-05-14 22:12:12 +02:00
|
|
|
// Legacy option for vfs-upload
|
|
|
|
protected $legacy_options = "mime";
|
|
|
|
|
2012-03-29 01:27:18 +02:00
|
|
|
public function __construct($xml='') {
|
|
|
|
if($xml) parent::__construct($xml);
|
|
|
|
}
|
|
|
|
|
2013-02-04 13:35:30 +01:00
|
|
|
/**
|
|
|
|
* If widget is a vfs-file widget, and there are files in the specified directory,
|
|
|
|
* they should be displayed.
|
|
|
|
*/
|
|
|
|
public function beforeSendToClient($cname, $expand = array())
|
|
|
|
{
|
2015-09-02 21:52:47 +02:00
|
|
|
if($this->type == 'vfs-upload' || $this->attrs['type'] == 'vfs-upload')
|
2013-02-04 13:35:30 +01:00
|
|
|
{
|
|
|
|
$form_name = self::form_name($cname, $this->id, $expand ? $expand : array('cont'=>self::$request->content));
|
2015-09-02 21:52:47 +02:00
|
|
|
if($this->attrs['path'])
|
|
|
|
{
|
|
|
|
$path = $this->attrs['path'];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$path = $this->id;
|
|
|
|
}
|
2013-02-04 13:35:30 +01:00
|
|
|
|
2015-09-02 21:52:47 +02:00
|
|
|
$this->setElementAttribute($form_name, 'path', $path);
|
2013-02-04 13:35:30 +01:00
|
|
|
// ID maps to path - check there for any existing files
|
2015-09-02 21:52:47 +02:00
|
|
|
list($app,$id,$relpath) = explode(':',$path,3);
|
2013-02-04 13:35:30 +01:00
|
|
|
if($app && $id)
|
|
|
|
{
|
|
|
|
if(!is_numeric($id))
|
|
|
|
{
|
|
|
|
$_id = self::expand_name($id,0,0,0,0,self::$request->content);
|
|
|
|
if($_id != $id)
|
|
|
|
{
|
|
|
|
$id = $_id;
|
|
|
|
$form_name = "$app:$id:$relpath";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$value =& self::get_array(self::$request->content, $form_name, true);
|
|
|
|
$path = egw_link::vfs_path($app,$id,'',true);
|
|
|
|
if (!empty($relpath)) $path .= '/'.$relpath;
|
|
|
|
|
2015-09-02 21:52:47 +02:00
|
|
|
$value = array();
|
|
|
|
|
2013-02-04 13:35:30 +01:00
|
|
|
// Single file, already existing
|
|
|
|
if (substr($path,-1) != '/' && egw_vfs::file_exists($path) && !egw_vfs::is_dir($path))
|
|
|
|
{
|
2015-09-01 00:32:50 +02:00
|
|
|
$file = egw_vfs::stat($path);
|
2015-09-02 21:52:47 +02:00
|
|
|
$file['path'] = $path;
|
2015-09-01 00:32:50 +02:00
|
|
|
$file['name'] = egw_vfs::basename($file['path']);
|
|
|
|
$file['mime'] = egw_vfs::mime_content_type($file['path']);
|
|
|
|
$value = array($file);
|
2013-02-04 13:35:30 +01:00
|
|
|
}
|
2015-09-02 21:52:47 +02:00
|
|
|
// Single file, missing extension in path
|
|
|
|
else if (substr($path, -1) != '/' && !egw_vfs::file_exists($path) && $relpath && substr($relpath,-4,1) !== '.')
|
|
|
|
{
|
|
|
|
$find = egw_vfs::find(substr($path,0, - strlen($relpath)), array(
|
|
|
|
'type' => 'f',
|
|
|
|
'maxdepth' => 1,
|
|
|
|
'name' => $relpath . '*'
|
|
|
|
));
|
|
|
|
foreach($find as $file)
|
|
|
|
{
|
|
|
|
$file_info = egw_vfs::stat($file);
|
|
|
|
$file_info['path'] = $file;
|
|
|
|
$file_info['name'] = egw_vfs::basename($file_info['path']);
|
|
|
|
$file_info['mime'] = egw_vfs::mime_content_type($file_info['path']);
|
|
|
|
$value[] = $file_info;
|
|
|
|
}
|
|
|
|
}
|
2013-02-04 13:35:30 +01:00
|
|
|
else if (substr($path, -1) == '/' && egw_vfs::is_dir($path))
|
|
|
|
{
|
2015-09-02 21:52:47 +02:00
|
|
|
$scan = egw_vfs::scandir($path);
|
|
|
|
foreach($scan as $file)
|
2013-02-04 13:35:30 +01:00
|
|
|
{
|
2015-09-02 21:52:47 +02:00
|
|
|
$file_info = egw_vfs::stat("$path$file");
|
|
|
|
$file_info['path'] = "$path$file";
|
|
|
|
$file_info['name'] = egw_vfs::basename($file_info['path']);
|
|
|
|
$file_info['mime'] = egw_vfs::mime_content_type($file_info['path']);
|
|
|
|
$value[] = $file_info;
|
2013-02-04 13:35:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-29 01:27:18 +02:00
|
|
|
public static function ajax_upload() {
|
|
|
|
parent::ajax_upload();
|
|
|
|
foreach($_FILES as $field => $file)
|
|
|
|
{
|
2015-09-02 21:52:47 +02:00
|
|
|
self::store_file($_REQUEST['path'] ? $_REQUEST['path'] : $_REQUEST['widget_id'], $file);
|
2012-03-29 01:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
2012-05-03 16:17:47 +02:00
|
|
|
|
2015-08-07 16:18:07 +02:00
|
|
|
/**
|
|
|
|
* Upload via dragging images into ckeditor
|
|
|
|
*/
|
|
|
|
public static function ajax_htmlarea_upload()
|
|
|
|
{
|
|
|
|
$request_id = str_replace(' ', '+', rawurldecode($_REQUEST['request_id']));
|
|
|
|
$widget_id = $_REQUEST['widget_id'];
|
|
|
|
if(!self::$request = etemplate_request::read($request_id))
|
|
|
|
{
|
|
|
|
$error = lang("Could not read session");
|
|
|
|
}
|
|
|
|
elseif (!($template = etemplate_widget_template::instance(self::$request->template['name'], self::$request->template['template_set'],
|
|
|
|
self::$request->template['version'], self::$request->template['load_via'])))
|
|
|
|
{
|
|
|
|
// Can't use callback
|
|
|
|
$error = lang("Could not get template for file upload, callback skipped");
|
|
|
|
}
|
|
|
|
/*elseif (!($widget = $template->getElementById($widget_id)))
|
|
|
|
{
|
|
|
|
$error = "Widget '$widget_id' not found!";
|
|
|
|
}*/
|
|
|
|
elseif (!isset($_FILES['upload']))
|
|
|
|
{
|
|
|
|
$error = lang('No _FILES[upload] found!');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$data = self::$request->content[$widget_id];
|
|
|
|
$path = self::store_file($path = self::get_vfs_path($data['to_app'].':'.$data['to_id']).'/', $_FILES['upload']);
|
|
|
|
}
|
|
|
|
// switch regular JSON response handling off
|
|
|
|
egw_json_request::isJSONRequest(false);
|
|
|
|
|
|
|
|
$file = array(
|
|
|
|
"uploaded" => (int)empty($error),
|
|
|
|
"fileName" => html::htmlspecialchars($_FILES['upload']['name']),
|
|
|
|
"url" => egw::link(egw_vfs::download_url($path)),
|
|
|
|
"error" => array(
|
|
|
|
"message" => $error,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
echo json_encode($file);
|
|
|
|
|
|
|
|
common::egw_exit();
|
|
|
|
}
|
|
|
|
|
2012-03-29 01:27:18 +02:00
|
|
|
/**
|
2015-01-26 18:30:10 +01:00
|
|
|
* Ajax callback to receive an incoming file
|
|
|
|
*
|
|
|
|
* The incoming file is automatically placed into the appropriate VFS location.
|
|
|
|
* If the entry is not yet created, the file information is stored into the widget's value.
|
|
|
|
* When the form is submitted, the information for all files uploaded is available in the returned
|
|
|
|
* $content array and the application should deal with the file.
|
|
|
|
*/
|
2012-03-29 01:27:18 +02:00
|
|
|
public static function store_file($path, $file) {
|
2015-09-01 00:32:50 +02:00
|
|
|
$name = $_REQUEST['widget_id'];
|
2012-05-14 22:12:12 +02:00
|
|
|
|
2012-03-29 01:27:18 +02:00
|
|
|
// Find real path
|
|
|
|
if($path[0] != '/')
|
|
|
|
{
|
2015-08-07 16:18:07 +02:00
|
|
|
$path = self::get_vfs_path($path);
|
2012-03-29 01:27:18 +02:00
|
|
|
}
|
|
|
|
$filename = $file['name'];
|
|
|
|
if (substr($path,-1) != '/')
|
|
|
|
{
|
|
|
|
// add extension to path
|
|
|
|
$parts = explode('.',$filename);
|
|
|
|
if (($extension = array_pop($parts)) && mime_magic::ext2mime($extension)) // really an extension --> add it to path
|
|
|
|
{
|
|
|
|
$path .= '.'.$extension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // multiple upload with dir given (trailing slash)
|
|
|
|
{
|
|
|
|
$path .= egw_vfs::encodePathComponent($filename);
|
|
|
|
}
|
|
|
|
if (!egw_vfs::file_exists($dir = egw_vfs::dirname($path)) && !egw_vfs::mkdir($dir,null,STREAM_MKDIR_RECURSIVE))
|
|
|
|
{
|
2012-05-14 22:12:12 +02:00
|
|
|
self::set_validation_error($name,lang('Error create parent directory %1!',egw_vfs::decodePath($dir)));
|
2012-03-29 01:27:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!copy($file['tmp_name'],egw_vfs::PREFIX.$path))
|
|
|
|
{
|
2012-05-14 22:12:12 +02:00
|
|
|
self::set_validation_error($name,lang('Error copying uploaded file to vfs!'));
|
2012-03-29 01:27:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-05-14 22:12:12 +02:00
|
|
|
|
|
|
|
// Try to remove temp file
|
|
|
|
unlink($file['tmp_name']);
|
2015-08-07 16:18:07 +02:00
|
|
|
|
|
|
|
return $path;
|
2012-03-29 01:27:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate input
|
|
|
|
* Merge any already uploaded files into the content array
|
|
|
|
*
|
|
|
|
* @param string $cname current namespace
|
2012-05-03 16:17:47 +02:00
|
|
|
* @param array $expand values for keys 'c', 'row', 'c_', 'row_', 'cont'
|
2012-03-29 01:27:18 +02:00
|
|
|
* @param array $content
|
|
|
|
* @param array &$validated=array() validated content
|
|
|
|
*/
|
2012-05-03 16:17:47 +02:00
|
|
|
public function validate($cname, array $expand, array $content, &$validated=array())
|
2012-03-29 01:27:18 +02:00
|
|
|
{
|
2014-12-02 20:17:03 +01:00
|
|
|
// do not validate, as it would overwrite preserved values with null!
|
|
|
|
if (in_array($this->type, array('vfs-size', 'vfs-uid', 'vfs-gid', 'vfs', 'vfs-mime')))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-05-03 16:17:47 +02:00
|
|
|
$form_name = self::form_name($cname, $this->id, $expand);
|
2012-03-29 01:27:18 +02:00
|
|
|
$value = $value_in = self::get_array($content, $form_name);
|
|
|
|
$valid =& self::get_array($validated, $form_name, true);
|
|
|
|
|
2012-05-14 22:12:12 +02:00
|
|
|
switch($this->type)
|
2012-03-29 01:27:18 +02:00
|
|
|
{
|
2012-05-14 22:12:12 +02:00
|
|
|
case 'vfs-upload':
|
|
|
|
if(!is_array($value)) $value = array();
|
|
|
|
// Check & skip files that made it asyncronously
|
|
|
|
list($app,$id,$relpath) = explode(':',$this->id,3);
|
|
|
|
//...
|
|
|
|
foreach($value as $tmp => $file)
|
|
|
|
{
|
|
|
|
if(egw_vfs::file_exists(self::get_vfs_path($id) . $relpath)) {}
|
|
|
|
}
|
|
|
|
parent::validate($cname, $content, $validated);
|
|
|
|
break;
|
2012-03-29 01:27:18 +02:00
|
|
|
}
|
2012-05-14 22:12:12 +02:00
|
|
|
$valid = $value;
|
|
|
|
}
|
2012-03-29 01:27:18 +02:00
|
|
|
|
2012-05-14 22:12:12 +02:00
|
|
|
/**
|
|
|
|
* Change an ID like app:id:relative/path to an actual VFS location
|
|
|
|
*/
|
|
|
|
public static function get_vfs_path($path) {
|
|
|
|
list($app,$id,$relpath) = explode(':',$path,3);
|
|
|
|
if (empty($id) || $id == 'undefined')
|
|
|
|
{
|
|
|
|
static $tmppath = array(); // static var, so all vfs-uploads get created in the same temporary dir
|
|
|
|
if (!isset($tmppath[$app])) $tmppath[$app] = '/home/'.$GLOBALS['egw_info']['user']['account_lid'].'/.'.$app.'_'.md5(time().session_id());
|
|
|
|
$path = $tmppath[$app];
|
|
|
|
unset($cell['onchange']); // no onchange, if we have to use a temporary dir
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$path = egw_link::vfs_path($app,$id,'',true);
|
|
|
|
}
|
|
|
|
if (!empty($relpath)) $path .= '/'.$relpath;
|
|
|
|
return $path;
|
2012-03-29 01:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
etemplate_widget::registerWidget('etemplate_widget_vfs', array('vfs-upload'));
|