2012-05-03 19:58:16 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Filemanager - document merge
|
|
|
|
*
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
* @package filemanager
|
|
|
|
* @copyright 2012 Nathan Gray
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
2016-05-05 09:20:07 +02:00
|
|
|
use EGroupware\Api;
|
|
|
|
use EGroupware\Api\Link;
|
|
|
|
use EGroupware\Api\Vfs;
|
|
|
|
|
2012-05-03 19:58:16 +02:00
|
|
|
/**
|
|
|
|
* Filemanager - document merge object
|
|
|
|
* Only merges information about the files, not the files themselves
|
|
|
|
*/
|
2016-05-05 09:20:07 +02:00
|
|
|
class filemanager_merge extends Api\Storage\Merge
|
2012-05-03 19:58:16 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Functions that can be called via menuaction
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $public_functions = array(
|
2021-10-14 21:18:21 +02:00
|
|
|
'show_replacements' => true,
|
|
|
|
'merge_entries' => true
|
2012-05-03 19:58:16 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fields that are numeric, for special numeric handling
|
|
|
|
*/
|
2021-10-14 21:18:21 +02:00
|
|
|
protected $numeric_fields = array();
|
2012-05-03 19:58:16 +02:00
|
|
|
|
2012-07-30 19:48:56 +02:00
|
|
|
/**
|
|
|
|
* Fields that are dates or timestamps
|
|
|
|
*/
|
|
|
|
public $date_fields = array(
|
|
|
|
'mtime', 'ctime'
|
|
|
|
);
|
|
|
|
|
2012-05-03 19:58:16 +02:00
|
|
|
/**
|
|
|
|
* Business object to pull records from
|
|
|
|
*/
|
|
|
|
protected $bo = null;
|
|
|
|
|
2012-05-07 17:53:28 +02:00
|
|
|
/**
|
|
|
|
* Current directory - affects how files are named
|
|
|
|
*/
|
|
|
|
protected $dir = '';
|
|
|
|
|
2012-05-03 19:58:16 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
*/
|
2012-05-07 17:53:28 +02:00
|
|
|
function __construct($_dir = '')
|
2012-05-03 19:58:16 +02:00
|
|
|
{
|
|
|
|
parent::__construct();
|
2014-11-19 13:20:59 +01:00
|
|
|
|
2012-05-07 17:53:28 +02:00
|
|
|
if($_dir)
|
|
|
|
{
|
|
|
|
$this->dir = $_dir;
|
|
|
|
}
|
2014-11-19 13:20:59 +01:00
|
|
|
|
2016-05-05 09:20:07 +02:00
|
|
|
// switch off handling of html formated content, if html is not used
|
|
|
|
$this->parse_html_styles = Api\Storage\Customfields::use_html('filemanager');
|
2012-05-03 19:58:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get replacements
|
|
|
|
*
|
|
|
|
* @param int $id id of entry
|
2021-10-14 21:18:21 +02:00
|
|
|
* @param string &$content =null content to create some replacements only if they are use
|
2012-05-03 19:58:16 +02:00
|
|
|
* @return array|boolean
|
|
|
|
*/
|
2021-10-14 21:18:21 +02:00
|
|
|
protected function get_replacements($id, &$content = null)
|
2012-05-03 19:58:16 +02:00
|
|
|
{
|
2021-10-14 21:18:21 +02:00
|
|
|
if(!($replacements = $this->filemanager_replacements($id, '', $content)))
|
2012-05-03 19:58:16 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $replacements;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get filemanager replacements
|
|
|
|
*
|
|
|
|
* @param int $id id (vfs path) of entry
|
2021-10-14 21:18:21 +02:00
|
|
|
* @param string $prefix ='' prefix like eg. 'erole'
|
2012-05-03 19:58:16 +02:00
|
|
|
* @return array|boolean
|
|
|
|
*/
|
2021-10-14 21:18:21 +02:00
|
|
|
public function filemanager_replacements($id, $prefix = '', &$content = null)
|
2012-05-03 19:58:16 +02:00
|
|
|
{
|
|
|
|
$info = array();
|
2021-10-14 21:18:21 +02:00
|
|
|
$file = Vfs::lstat($id, true);
|
2012-05-03 19:58:16 +02:00
|
|
|
|
2016-05-05 09:20:07 +02:00
|
|
|
$file['mtime'] = Api\DateTime::to($file['mtime']);
|
|
|
|
$file['ctime'] = Api\DateTime::to($file['ctime']);
|
2012-05-03 19:58:16 +02:00
|
|
|
|
2016-05-05 09:20:07 +02:00
|
|
|
$file['name'] = Vfs::basename($id);
|
2016-07-02 12:28:58 +02:00
|
|
|
$file['dir'] = ($dir = Vfs::dirname($id)) ? Vfs::decodePath($dir) : '';
|
2021-10-14 21:18:21 +02:00
|
|
|
$dirlist = explode('/', $file['dir']);
|
2012-06-05 16:50:22 +02:00
|
|
|
$file['folder'] = array_pop($dirlist);
|
2021-10-14 21:18:21 +02:00
|
|
|
$file['folder_file'] = $file['folder'] . '/' . $file['name'];
|
2012-05-03 19:58:16 +02:00
|
|
|
$file['path'] = $id;
|
2021-10-14 21:18:21 +02:00
|
|
|
$file['rel_path'] = str_replace($this->dir . '/', '', $id);
|
2016-05-05 09:20:07 +02:00
|
|
|
$file['hsize'] = Vfs::hsize($file['size']);
|
|
|
|
$file['mime'] = Vfs::mime_content_type($id);
|
2012-05-03 19:58:16 +02:00
|
|
|
$file['gid'] *= -1; // our widgets use negative gid's
|
2021-10-14 21:18:21 +02:00
|
|
|
if(($props = Vfs::propfind($id)))
|
2012-05-03 19:58:16 +02:00
|
|
|
{
|
2014-11-19 13:20:59 +01:00
|
|
|
foreach($props as $prop)
|
|
|
|
{
|
|
|
|
$file[$prop['name']] = $prop['val'];
|
|
|
|
}
|
2012-05-03 19:58:16 +02:00
|
|
|
}
|
2021-10-14 21:18:21 +02:00
|
|
|
if(($file['is_link'] = Vfs::is_link($id)))
|
2012-05-03 19:58:16 +02:00
|
|
|
{
|
2016-05-05 09:20:07 +02:00
|
|
|
$file['symlink'] = Vfs::readlink($id);
|
2012-05-03 19:58:16 +02:00
|
|
|
}
|
2012-05-07 17:53:28 +02:00
|
|
|
// Custom fields
|
2012-05-03 19:58:16 +02:00
|
|
|
if($content && strpos($content, '#') !== 0)
|
2021-10-14 21:18:21 +02:00
|
|
|
{
|
2012-05-07 17:53:28 +02:00
|
|
|
// Expand link-to custom fields
|
2021-10-14 21:18:21 +02:00
|
|
|
$this->cf_link_to_expand($file, $content, $info);
|
2014-11-19 13:20:59 +01:00
|
|
|
|
2016-05-05 09:20:07 +02:00
|
|
|
foreach(Api\Storage\Customfields::get('filemanager') as $name => $field)
|
2012-05-07 17:53:28 +02:00
|
|
|
{
|
|
|
|
// Set any missing custom fields, or the marker will stay
|
2021-10-14 21:18:21 +02:00
|
|
|
if(!$file['#' . $name])
|
2012-05-07 17:53:28 +02:00
|
|
|
{
|
2021-10-14 21:18:21 +02:00
|
|
|
$file['#' . $name] = '';
|
2012-05-07 17:53:28 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-05-05 09:20:07 +02:00
|
|
|
// Format date cfs per user Api\Preferences
|
2012-05-07 17:53:28 +02:00
|
|
|
if($field['type'] == 'date' || $field['type'] == 'date-time')
|
|
|
|
{
|
2021-10-14 21:18:21 +02:00
|
|
|
$this->date_fields[] = '#' . $name;
|
|
|
|
$file['#' . $name] = Api\DateTime::to($file['#' . $name], $field['type'] == 'date' ? true : '');
|
2012-05-07 17:53:28 +02:00
|
|
|
}
|
|
|
|
}
|
2012-05-03 19:58:16 +02:00
|
|
|
}
|
2012-06-18 17:02:52 +02:00
|
|
|
|
|
|
|
// If in apps folder, try for app-specific placeholders
|
|
|
|
if($dirlist[1] == 'apps' && count($dirlist) > 1)
|
|
|
|
{
|
2012-06-25 19:54:43 +02:00
|
|
|
// Try this first - a normal path /apps/appname/id/file
|
2021-10-14 21:18:21 +02:00
|
|
|
list($app, $app_id) = explode('/', substr($file['path'], strpos($file['path'], 'apps/') + 5));
|
2012-06-25 19:54:43 +02:00
|
|
|
// Symlink?
|
2021-10-14 21:18:21 +02:00
|
|
|
if(!$app || !(int)$app_id || !array_key_exists($app, $GLOBALS['egw_info']['user']['apps']))
|
|
|
|
{
|
2012-06-25 19:54:43 +02:00
|
|
|
// Try resolving just app + ID - /apps/App Name/Record Title/file
|
2021-10-14 21:18:21 +02:00
|
|
|
$resolved = Vfs::resolve_url_symlinks(implode('/', array_slice(explode('/', $file['dir']), 0, 4)));
|
|
|
|
list($app, $app_id) = explode('/', substr($resolved, strpos($resolved, 'apps/') + 5));
|
2012-06-25 19:54:43 +02:00
|
|
|
|
2021-10-14 21:18:21 +02:00
|
|
|
if(!$app || !(int)$app_id || !array_key_exists($app, $GLOBALS['egw_info']['user']['apps']))
|
|
|
|
{
|
2012-06-25 19:54:43 +02:00
|
|
|
// Get rid of any virtual folders (eg: All$) and symlinks
|
2016-05-05 09:20:07 +02:00
|
|
|
$resolved = Vfs::resolve_url_symlinks($file['path']);
|
2021-10-14 21:18:21 +02:00
|
|
|
list($app, $app_id) = explode('/', substr($resolved, strpos($resolved, 'apps/') + 5));
|
2012-06-25 19:54:43 +02:00
|
|
|
}
|
|
|
|
}
|
2012-06-26 17:24:20 +02:00
|
|
|
if($app && $app_id)
|
2012-06-25 19:54:43 +02:00
|
|
|
{
|
2012-06-18 17:02:52 +02:00
|
|
|
if($app && $GLOBALS['egw_info']['user']['apps'][$app])
|
|
|
|
{
|
|
|
|
$app_merge = null;
|
|
|
|
try
|
|
|
|
{
|
2021-10-14 21:18:21 +02:00
|
|
|
$classname = $app . '_merge';
|
2012-06-18 17:02:52 +02:00
|
|
|
if(class_exists($classname))
|
|
|
|
{
|
|
|
|
$app_merge = new $classname();
|
|
|
|
if($app_merge && method_exists($app_merge, 'get_replacements'))
|
|
|
|
{
|
2012-06-26 17:24:20 +02:00
|
|
|
$app_placeholders = $app_merge->get_replacements($app_id, $content);
|
2012-06-18 17:02:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-14 21:18:21 +02:00
|
|
|
// Silently discard & continue
|
|
|
|
catch (Exception $e)
|
|
|
|
{
|
|
|
|
unset($e); // not used
|
2014-11-19 13:20:59 +01:00
|
|
|
}
|
2012-06-18 17:02:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-05 09:20:07 +02:00
|
|
|
$link = Link::mime_open($file['url'], $file['mime']);
|
2012-05-08 17:10:43 +02:00
|
|
|
if(is_array($link))
|
|
|
|
{
|
|
|
|
// Directories have their internal protocol in path here
|
|
|
|
if($link['path'] && strpos($link['path'], '://') !== false) $link['path'] = $file['path'];
|
2016-05-05 09:20:07 +02:00
|
|
|
$link = Api\Session::link('/index.php', $link);
|
2012-05-08 17:10:43 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Regular files
|
2016-05-05 09:20:07 +02:00
|
|
|
$link = Api\Session::link($link);
|
2012-05-08 17:10:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prepend site, if missing
|
2018-03-21 12:40:59 +01:00
|
|
|
if ($link[0] == '/') $link = Api\Framework::getUrl($link);
|
|
|
|
|
2016-05-05 09:20:07 +02:00
|
|
|
$file['link'] = Api\Html::a_href(Api\Html::htmlspecialchars($file['name']), $link);
|
2018-07-24 22:53:38 +02:00
|
|
|
$file['webdav_url'] = Api\Session::link(Vfs::download_url($file['url']));
|
2012-05-07 17:53:28 +02:00
|
|
|
$file['url'] = $link;
|
2012-05-03 19:58:16 +02:00
|
|
|
|
|
|
|
// Add markers
|
|
|
|
foreach($file as $key => &$value)
|
|
|
|
{
|
|
|
|
if(!$value) $value = '';
|
2021-10-14 21:18:21 +02:00
|
|
|
$info['$$' . ($prefix ? $prefix . '/' : '') . $key . '$$'] = $value;
|
2012-05-03 19:58:16 +02:00
|
|
|
}
|
2012-06-18 17:02:52 +02:00
|
|
|
if($app_placeholders)
|
|
|
|
{
|
|
|
|
$info = array_merge($app_placeholders, $info);
|
|
|
|
}
|
2012-05-03 19:58:16 +02:00
|
|
|
return $info;
|
|
|
|
}
|
|
|
|
|
2019-05-07 23:06:19 +02:00
|
|
|
/**
|
|
|
|
* Create a share for an entry
|
|
|
|
*
|
|
|
|
* @param string $app
|
|
|
|
* @param string $id
|
|
|
|
* @param string $path
|
|
|
|
* @param type $content
|
|
|
|
* @return type
|
|
|
|
*/
|
|
|
|
protected function create_share($app, $id, &$content)
|
|
|
|
{
|
|
|
|
// Check if some other process created the share (with custom options)
|
|
|
|
// and put it in the session cache for us
|
|
|
|
$path = $id;
|
|
|
|
$session = \EGroupware\Api\Cache::getSession(Api\Sharing::class, $path);
|
|
|
|
if($session && $session['share_path'] == $path)
|
|
|
|
{
|
|
|
|
return $session;
|
|
|
|
}
|
2021-10-14 21:18:21 +02:00
|
|
|
else
|
2019-07-16 21:44:20 +02:00
|
|
|
{
|
2021-10-14 21:18:21 +02:00
|
|
|
if(($session = \EGroupware\Api\Cache::getSession(Api\Sharing::class, "$app::$id")) &&
|
|
|
|
substr($session['share_path'], -strlen($path)) === $path)
|
|
|
|
{
|
|
|
|
return $session;
|
|
|
|
}
|
2019-07-16 21:44:20 +02:00
|
|
|
}
|
2019-05-07 23:06:19 +02:00
|
|
|
// Need to create the share here.
|
|
|
|
// No way to know here if it should be writable, or who it's going to
|
2021-10-14 21:18:21 +02:00
|
|
|
$mode = /* ? ? Sharing::WRITABLE :*/
|
|
|
|
Api\Sharing::READONLY;
|
2019-05-07 23:06:19 +02:00
|
|
|
$recipients = array();
|
|
|
|
$extra = array();
|
|
|
|
|
2020-03-25 03:34:04 +01:00
|
|
|
return \EGroupware\Api\Vfs\Sharing::create('', $path, $mode, NULL, $recipients, $extra);
|
2019-05-07 23:06:19 +02:00
|
|
|
}
|
|
|
|
|
2012-05-03 19:58:16 +02:00
|
|
|
/**
|
2021-10-14 21:18:21 +02:00
|
|
|
* Hook for extending apps to customise the replacements UI without having to override the whole method
|
2012-05-03 19:58:16 +02:00
|
|
|
*
|
2021-10-14 21:18:21 +02:00
|
|
|
* @param string $template_name
|
|
|
|
* @param $content
|
|
|
|
* @param $sel_options
|
|
|
|
* @param $readonlys
|
2012-05-03 19:58:16 +02:00
|
|
|
*/
|
2021-10-14 21:18:21 +02:00
|
|
|
protected function show_replacements_hook(&$template_name, &$content, &$sel_options, &$readonlys)
|
2012-05-03 19:58:16 +02:00
|
|
|
{
|
2021-10-14 21:18:21 +02:00
|
|
|
$content['extra_template'] = 'filemanager.replacements';
|
|
|
|
}
|
2012-05-03 19:58:16 +02:00
|
|
|
|
2021-10-14 21:18:21 +02:00
|
|
|
/**
|
|
|
|
* Get a list of placeholders provided.
|
|
|
|
*
|
|
|
|
* Placeholders are grouped logically. Group key should have a user-friendly translation.
|
|
|
|
*/
|
|
|
|
public function get_placeholder_list($prefix = '')
|
|
|
|
{
|
|
|
|
$placeholders = parent::get_placeholder_list($prefix);
|
2012-05-03 19:58:16 +02:00
|
|
|
|
|
|
|
$fields = array(
|
2021-10-14 21:18:21 +02:00
|
|
|
'name' => 'name',
|
|
|
|
'path' => 'Absolute path',
|
|
|
|
'rel_path' => 'Path relative to current directory',
|
|
|
|
'folder' => 'Containing folder',
|
2012-06-05 16:50:22 +02:00
|
|
|
'folder_file' => 'Containing folder and file name',
|
2021-10-14 21:18:21 +02:00
|
|
|
'url' => 'url',
|
|
|
|
'webdav_url' => 'External path using webdav',
|
|
|
|
'link' => 'Clickable link to file',
|
|
|
|
'comment' => 'comment',
|
|
|
|
'mtime' => 'modified',
|
|
|
|
'ctime' => 'created',
|
|
|
|
'mime' => 'Type',
|
|
|
|
'hsize' => 'Size',
|
|
|
|
'size' => 'Size (in bytes)',
|
2012-05-03 19:58:16 +02:00
|
|
|
);
|
2021-10-14 21:18:21 +02:00
|
|
|
$group = 'placeholders';
|
2012-05-03 19:58:16 +02:00
|
|
|
foreach($fields as $name => $label)
|
|
|
|
{
|
2021-10-14 21:18:21 +02:00
|
|
|
$marker = $this->prefix($prefix, $name, '{');
|
|
|
|
if(!array_filter($placeholders, function ($a) use ($marker)
|
|
|
|
{
|
|
|
|
return array_key_exists($marker, $a);
|
|
|
|
}))
|
|
|
|
{
|
|
|
|
$placeholders[$group][] = [
|
|
|
|
'value' => $marker,
|
|
|
|
'label' => $label
|
|
|
|
];
|
|
|
|
}
|
2012-05-03 19:58:16 +02:00
|
|
|
}
|
|
|
|
|
2021-10-14 21:18:21 +02:00
|
|
|
return $placeholders;
|
2012-05-03 19:58:16 +02:00
|
|
|
}
|
|
|
|
}
|