Api: Add {{share}} & {{share-files_only}} merge placeholders that create an external share link

This commit is contained in:
nathan 2023-07-28 08:26:47 -06:00
parent 3dbdbc0596
commit eca2cc9d46

View File

@ -578,30 +578,49 @@ abstract class Merge
* Get share placeholder * Get share placeholder
* *
* If the placeholder is present in the content, the share will be automatically * If the placeholder is present in the content, the share will be automatically
* created. * created. Valid placeholders:
* $$share$$ - A link to the entry
* $$share-files$$ - A link to filemanager showing files of the entry
* $$share/writable$$ - An editable link to the entry, using current user's permission
* $$share-files/writable$$ - Link to filemanager with write access
*/ */
protected function share_placeholder($app, $id, $prefix, &$content) protected function share_placeholder($app, $id, $prefix, &$content)
{ {
$replacements = array(); $replacements = array();
// Skip if no content or content has no share placeholder // Skip if no content or content has no share placeholder (fast check)
if(!$content || strpos($content, '$$share') === FALSE) if(!$content || strpos($content, '$$' . $this->prefix($prefix, 'share')) === FALSE)
{ {
return $replacements; return $replacements;
} }
$matches = null;
preg_match_all('/\${2}' . $this->prefix($prefix, 'share') . '[^\$]*\${2}/', $content, $matches);
list($placeholders) = $matches;
if(!$GLOBALS['egw_info']['user']['apps']['stylite']) if(!$GLOBALS['egw_info']['user']['apps']['stylite'])
{ {
$replacements['$$' . $prefix . 'share$$'] = lang('EPL Only'); foreach($placeholders as $p)
{
$replacements[$p] = lang('EPL Only');
}
return $replacements; return $replacements;
} }
foreach($placeholders as $index => $placeholder)
// Get or create the share
$share = $this->create_share($app, $id, $content);
if($share)
{ {
$replacements['$$' . $prefix . 'share$$'] = $link = Api\Sharing::share2link($share); // Get or create the share
try
{
$share = $this->create_share($app, $id, $placeholder);
$replacements[$placeholder] = $share ? Api\Sharing::share2link($share) : '';
}
catch (Exception $e)
{
_egw_log_exception($e, $headline);
$replacements[$placeholder] = lang('Error');
continue;
}
} }
return $replacements; return $replacements;
@ -623,17 +642,26 @@ abstract class Merge
$session = \EGroupware\Api\Cache::getSession(Api\Sharing::class, $path); $session = \EGroupware\Api\Cache::getSession(Api\Sharing::class, $path);
if($session && $session['share_path'] == $path) if($session && $session['share_path'] == $path)
{ {
Api\Cache::unsetSession(Api\Sharing::class, $path);
return $session; return $session;
} }
$matches = null;
preg_match_all('/\${2}([^\/\$]+\/)?share(-files(_only)?)?(\/writable)?\${2}/', $content, $matches);
list($placeholders, , $files, , $writable) = $matches;
// Need to create the share here. // Need to create the share here.
// No way to know here if it should be writable, or who it's going to // No way to know here if it should be writable, or who it's going to
$mode = /* ? ? Sharing::WRITABLE :*/ $mode = $writable[0] == "/writable" ? Api\Sharing::WRITABLE : Api\Sharing::READONLY;
Api\Sharing::READONLY; $path = $files[0] !== '-files_only' ? $path : "/apps/$app/$id";
$recipients = array(); $recipients = array();
$extra = array(); $extra = array();
//$extra['share_writable'] |= ($mode == Sharing::WRITABLE ? 1 : 0); if($files[0] == '-files')
{
$extra['include_files'] = true;
}
$extra['share_writable'] |= ($mode == Api\Sharing::WRITABLE ? 1 : 0);
return \EGroupware\Stylite\Link\Sharing::create('', $path, $mode, NULL, $recipients, $extra); return \EGroupware\Stylite\Link\Sharing::create('', $path, $mode, NULL, $recipients, $extra);
} }
@ -2989,7 +3017,18 @@ abstract class Merge
*/ */
public function get_common_replacements() public function get_common_replacements()
{ {
return array( $share_replacements = !$GLOBALS['egw_info']['user']['apps']['stylite'] ? [] : [
'share' => lang("Share this %1 via URL", lang('entry')),
// We don't allow anonymous file access through entries - the links tab will be empty
// 'share-files' => lang("Share this %1 via URL", lang('entry')) . " " . lang("include access to any linked files (links tab)"),
'share-files_only' => lang('Share just the associated filemanager directory, not the %1', lang('entry')),
'share/writable' => lang("Share this %1 via URL", lang('entry')) . '. ' . lang('Allow anonymous editing'),
// 'share-files/writable' => lang("Share this %1 via URL", lang('entry')) . " " . lang("include access to any linked files (links tab)"),
'share-files_only/writable' => lang('Share just the associated filemanager directory, not the %1', lang('entry')) . '. ' . lang('Allow anonymous editing'),
];
return $share_replacements +
array(
// Link to current entry // Link to current entry
'link' => lang('URL of current record'), 'link' => lang('URL of current record'),
'link/href' => lang('HTML link to the current record'), 'link/href' => lang('HTML link to the current record'),
@ -3035,6 +3074,7 @@ abstract class Merge
$placeholders = [ $placeholders = [
'URLs' => [], 'URLs' => [],
'Egroupware links' => [], 'Egroupware links' => [],
'Sharing' => [],
'General' => [], 'General' => [],
'Repeat' => [], 'Repeat' => [],
'Commands' => [] 'Commands' => []
@ -3051,6 +3091,12 @@ abstract class Merge
switch($name) switch($name)
{ {
case 'share':
$group = 'Sharing';
break;
case 'link':
$group = "URLs";
break;
case 'links': case 'links':
$group = 'Egroupware links'; $group = 'Egroupware links';
break; break;