(.*?)<\/span>/', '$1', $content, -1, $count);
$i++;
- } while($count > 0 && $i < 10);
+ }
+ while($count > 0 && $i < 10);
$doc = new DOMDocument();
$xslt = new XSLTProcessor();
$xslt_file = $mimetype == 'application/xml' ? 'wordml.xslt' : 'msoffice.xslt';
- $doc->load(EGW_INCLUDE_ROOT.'/api/templates/default/Merge/'.$xslt_file);
+ $doc->load(EGW_INCLUDE_ROOT . '/api/templates/default/Merge/' . $xslt_file);
$xslt->importStyleSheet($doc);
break;
}
@@ -822,8 +887,9 @@ abstract class Merge
$content = $xslt->transformToXml($element);
//echo $content;die();
// Word 2003 needs two declarations, add extra declaration back in
- if($mimetype == 'application/xml' && $mso_application_progid == 'Word.Document' && strpos($content, ''.$content;
+ if($mimetype == 'application/xml' && $mso_application_progid == 'Word.Document' && strpos($content, '' . $content;
}
// Validate
/*
@@ -845,12 +911,12 @@ abstract class Merge
* @param string $charset =null charset to override default set by mimetype or export charset
* @return string|boolean merged document or false on error
*/
- public function &merge_string($_content,$ids,&$err,$mimetype,array $fix=null,$charset=null)
+ public function &merge_string($_content, $ids, &$err, $mimetype, array $fix = null, $charset = null)
{
$ids = empty($ids) ? [] : (array)$ids;
$matches = null;
- if ($mimetype == 'application/xml' &&
- preg_match('/'.preg_quote('', '/').'/',substr($_content,0,200),$matches))
+ if($mimetype == 'application/xml' &&
+ preg_match('/' . preg_quote('', '/') . '/', substr($_content, 0, 200), $matches))
{
$mso_application_progid = $matches[1];
}
@@ -860,33 +926,38 @@ abstract class Merge
}
// alternative syntax using double curly brackets (eg. {{cat_id}} instead $$cat_id$$),
// agressivly removing all xml-tags eg. Word adds within placeholders
- $content = preg_replace_callback('/{{[^}]+}}/i', function($matches)
+ $content = preg_replace_callback('/{{[^}]+}}/i', function ($matches)
{
- return '$$'.strip_tags(substr($matches[0], 2, -2)).'$$';
- }, $_content);
+ return '$$' . strip_tags(substr($matches[0], 2, -2)) . '$$';
+ }, $_content);
// Handle escaped placeholder markers in RTF, they won't match when escaped
if($mimetype == 'application/rtf')
{
- $content = preg_replace('/\\\{\\\{([^\\}]+)\\\}\\\}/i','$$\1$$',$content);
+ $content = preg_replace('/\\\{\\\{([^\\}]+)\\\}\\\}/i', '$$\1$$', $content);
}
// make currently processed mimetype available to class methods;
$this->mimetype = $mimetype;
// fix garbled placeholders
- if ($fix && is_array($fix))
+ if($fix && is_array($fix))
{
- $content = preg_replace(array_keys($fix),array_values($fix),$content);
+ $content = preg_replace(array_keys($fix), array_values($fix), $content);
//die("".htmlspecialchars($content)."
\n");
}
- list($contentstart,$contentrepeat,$contentend) = preg_split('/\$\$pagerepeat\$\$/',$content,-1, PREG_SPLIT_NO_EMPTY)+[null,null,null]; //get differt parts of document, seperatet by Pagerepeat
- if ($mimetype == 'text/plain' && $ids && count($ids) > 1)
+ list($contentstart, $contentrepeat, $contentend) = preg_split('/\$\$pagerepeat\$\$/', $content, -1, PREG_SPLIT_NO_EMPTY) + [null,
+ null,
+ null]; //get differt parts of document, seperatet by Pagerepeat
+ if($mimetype == 'text/plain' && $ids && count($ids) > 1)
{
// textdocuments are simple, they do not hold start and end, but they may have content before and after the $$pagerepeat$$ tag
// header and footer should not hold any $$ tags; if we find $$ tags with the header, we assume it is the pagerepeatcontent
$nohead = false;
- if (stripos($contentstart,'$$') !== false) $nohead = true;
- if ($nohead)
+ if(stripos($contentstart, '$$') !== false)
+ {
+ $nohead = true;
+ }
+ if($nohead)
{
$contentend = $contentrepeat;
$contentrepeat = $contentstart;
@@ -894,51 +965,58 @@ abstract class Merge
}
}
- if (in_array($mimetype, array('application/vnd.oasis.opendocument.text','application/vnd.oasis.opendocument.text-template')) && count($ids) > 1)
+ if(in_array($mimetype, array('application/vnd.oasis.opendocument.text',
+ 'application/vnd.oasis.opendocument.text-template')) && count($ids) > 1)
{
if(strpos($content, '$$pagerepeat') === false)
{
//for odt files we have to split the content and add a style for page break to the style area
- list($contentstart,$contentrepeat,$contentend) = preg_split('/office:body>/',$content,-1, PREG_SPLIT_NO_EMPTY); //get differt parts of document, seperatet by Pagerepeat
- $contentstart = substr($contentstart,0,strlen($contentstart)-1); //remove "<"
- $contentrepeat = substr($contentrepeat,0,strlen($contentrepeat)-2); //remove "";
+ list($contentstart, $contentrepeat, $contentend) = preg_split('/office:body>/', $content, -1, PREG_SPLIT_NO_EMPTY); //get differt parts of document, seperatet by Pagerepeat
+ $contentstart = substr($contentstart, 0, strlen($contentstart) - 1); //remove "<"
+ $contentrepeat = substr($contentrepeat, 0, strlen($contentrepeat) - 2); //remove "";
// need to add page-break style to the style list
- list($stylestart,$stylerepeat,$styleend) = preg_split('/<\/office:automatic-styles>/',$content,-1, PREG_SPLIT_NO_EMPTY); //get differt parts of document style sheets
- $contentstart = $stylestart.'';
+ list($stylestart, $stylerepeat, $styleend) = preg_split('/<\/office:automatic-styles>/', $content, -1, PREG_SPLIT_NO_EMPTY); //get differt parts of document style sheets
+ $contentstart = $stylestart . '';
$contentstart .= '';
$contentend = '';
}
else
{
// Template specifies where to repeat
- list($contentstart,$contentrepeat,$contentend) = preg_split('/\$\$pagerepeat\$\$/',$content,-1, PREG_SPLIT_NO_EMPTY); //get different parts of document, seperated by pagerepeat
+ list($contentstart, $contentrepeat, $contentend) = preg_split('/\$\$pagerepeat\$\$/', $content, -1, PREG_SPLIT_NO_EMPTY); //get different parts of document, seperated by pagerepeat
}
}
- if (in_array($mimetype, array('application/vnd.ms-word.document.macroenabled.12', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')) && count($ids) > 1)
+ if(in_array($mimetype, array('application/vnd.ms-word.document.macroenabled.12',
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')) && count($ids) > 1)
{
//for Word 2007 XML files we have to split the content and add a style for page break to the style area
- list($contentstart,$contentrepeat,$contentend) = preg_split('/w:body>/',$content,-1, PREG_SPLIT_NO_EMPTY); //get differt parts of document, seperatet by Pagerepeat
- $contentstart = substr($contentstart,0,strlen($contentstart)-1); //remove ""
- $contentrepeat = substr($contentrepeat,0,strlen($contentrepeat)-2); //remove "";
+ list($contentstart, $contentrepeat, $contentend) = preg_split('/w:body>/', $content, -1, PREG_SPLIT_NO_EMPTY); //get differt parts of document, seperatet by Pagerepeat
+ $contentstart = substr($contentstart, 0, strlen($contentstart) - 1); //remove ""
+ $contentrepeat = substr($contentrepeat, 0, strlen($contentrepeat) - 2); //remove "";
$contentstart .= '';
$contentend = '';
}
- list($Labelstart,$Labelrepeat,$Labeltend) = preg_split('/\$\$label\$\$/',$contentrepeat,-1, PREG_SPLIT_NO_EMPTY)+[null,null,null]; //get the label content
- preg_match_all('/\$\$labelplacement\$\$/',$contentrepeat,$countlables, PREG_SPLIT_NO_EMPTY);
+ list($Labelstart, $Labelrepeat, $Labeltend) = preg_split('/\$\$label\$\$/', $contentrepeat, -1, PREG_SPLIT_NO_EMPTY) + [null,
+ null,
+ null]; //get the label content
+ preg_match_all('/\$\$labelplacement\$\$/', $contentrepeat, $countlables, PREG_SPLIT_NO_EMPTY);
$countlables = count($countlables[0]);
- preg_replace('/\$\$labelplacement\$\$/','',$Labelrepeat,1);
+ preg_replace('/\$\$labelplacement\$\$/', '', $Labelrepeat, 1);
$lableprint = $countlables > 1;
- if (count($ids) > 1 && !$contentrepeat)
+ if(count($ids) > 1 && !$contentrepeat)
{
$err = lang('for more than one contact in a document use the tag pagerepeat!');
$ret = false;
return $ret;
}
- if ($this->report_memory_usage) error_log(__METHOD__."(count(ids)=".count($ids).") strlen(contentrepeat)=".strlen($contentrepeat).', strlen(labelrepeat)='.strlen($Labelrepeat));
-
- if ($contentrepeat)
+ if($this->report_memory_usage)
{
- $content_stream = fopen('php://temp','r+');
+ error_log(__METHOD__ . "(count(ids)=" . count($ids) . ") strlen(contentrepeat)=" . strlen($contentrepeat) . ', strlen(labelrepeat)=' . strlen($Labelrepeat));
+ }
+
+ if($contentrepeat)
+ {
+ $content_stream = fopen('php://temp', 'r+');
fwrite($content_stream, $contentstart);
$joiner = '';
switch($mimetype)
@@ -947,8 +1025,8 @@ abstract class Merge
case 'text/rtf':
$joiner = '\\par \\page\\pard\\plain';
break;
- case 'application/vnd.oasis.opendocument.text': // oo text
- case 'application/vnd.oasis.opendocument.spreadsheet': // oo spreadsheet
+ case 'application/vnd.oasis.opendocument.text': // oo text
+ case 'application/vnd.oasis.opendocument.spreadsheet': // oo spreadsheet
case 'application/vnd.oasis.opendocument.presentation':
case 'application/vnd.oasis.opendocument.text-template':
case 'application/vnd.oasis.opendocument.spreadsheet-template':
@@ -968,20 +1046,26 @@ abstract class Merge
$joiner = "\r\n";
break;
default:
- $err = lang('%1 not implemented for %2!','$$pagerepeat$$',$mimetype);
+ $err = lang('%1 not implemented for %2!', '$$pagerepeat$$', $mimetype);
$ret = false;
return $ret;
}
}
- foreach ((array)$ids as $n => $id)
+ foreach((array)$ids as $n => $id)
{
- if ($contentrepeat) $content = $contentrepeat; //content to repeat
- if ($lableprint) $content = $Labelrepeat;
+ if($contentrepeat)
+ {
+ $content = $contentrepeat;
+ } //content to repeat
+ if($lableprint)
+ {
+ $content = $Labelrepeat;
+ }
// generate replacements; if exception is thrown, catch it set error message and return false
try
{
- if(!($replacements = $this->get_replacements($id,$content)))
+ if(!($replacements = $this->get_replacements($id, $content)))
{
$err = lang('Entry not found!');
$ret = false;
@@ -995,114 +1079,120 @@ abstract class Merge
$ret = false;
return $ret;
}
- if ($this->report_memory_usage) error_log(__METHOD__."() $n: $id ".Api\Vfs::hsize(memory_get_usage(true)));
+ if($this->report_memory_usage)
+ {
+ error_log(__METHOD__ . "() $n: $id " . Api\Vfs::hsize(memory_get_usage(true)));
+ }
// some general replacements: current user, date and time
if(strpos($content, '$$user/') !== false && ($user = $GLOBALS['egw']->accounts->id2name($GLOBALS['egw_info']['user']['account_id'], 'person_id')))
{
$replacements += $this->contact_replacements($user, 'user', false, $content);
$replacements['$$user/primary_group$$'] = $GLOBALS['egw']->accounts->id2name($GLOBALS['egw']->accounts->id2name($GLOBALS['egw_info']['user']['account_id'], 'account_primary_group'));
}
- $replacements['$$date$$'] = Api\DateTime::to('now',true);
+ $replacements['$$date$$'] = Api\DateTime::to('now', true);
$replacements['$$datetime$$'] = Api\DateTime::to('now');
- $replacements['$$time$$'] = Api\DateTime::to('now',false);
+ $replacements['$$time$$'] = Api\DateTime::to('now', false);
$app = $this->get_app();
$replacements += $this->share_placeholder($app, $id, '', $content);
// does our extending class registered table-plugins AND document contains table tags
- if ($this->table_plugins && preg_match_all('/\\$\\$table\\/([A-Za-z0-9_]+)\\$\\$(.*?)\\$\\$endtable\\$\\$/s',$content,$matches,PREG_SET_ORDER))
+ if($this->table_plugins && preg_match_all('/\\$\\$table\\/([A-Za-z0-9_]+)\\$\\$(.*?)\\$\\$endtable\\$\\$/s', $content, $matches, PREG_SET_ORDER))
{
// process each table
foreach($matches as $match)
{
- $plugin = $match[1]; // plugin name
+ $plugin = $match[1]; // plugin name
$callback = $this->table_plugins[$plugin];
- $repeat = $match[2]; // line to repeat
+ $repeat = $match[2]; // line to repeat
$repeats = '';
- if (isset($callback))
+ if(isset($callback))
{
- for($n = 0; ($row_replacements = $this->$callback($plugin,$id,$n,$repeat)); ++$n)
+ for($n = 0; ($row_replacements = $this->$callback($plugin, $id, $n, $repeat)); ++$n)
{
$_repeat = $this->process_commands($repeat, $row_replacements);
- $repeats .= $this->replace($_repeat,$row_replacements,$mimetype,$mso_application_progid);
+ $repeats .= $this->replace($_repeat, $row_replacements, $mimetype, $mso_application_progid);
}
}
- $content = str_replace($match[0],$repeats,$content);
+ $content = str_replace($match[0], $repeats, $content);
}
}
- $content = $this->process_commands($this->replace($content,$replacements,$mimetype,$mso_application_progid,$charset), $replacements);
+ $content = $this->process_commands($this->replace($content, $replacements, $mimetype, $mso_application_progid, $charset), $replacements);
// remove not existing replacements (eg. from calendar array)
- if (strpos($content,'$$') !== null)
+ if(strpos($content, '$$') !== null)
{
- $content = preg_replace('/\$\$[a-z0-9_\/]+\$\$/i','',$content);
+ $content = preg_replace('/\$\$[a-z0-9_\/]+\$\$/i', '', $content);
}
- if ($contentrepeat)
+ if($contentrepeat)
{
fwrite($content_stream, ($n == 0 ? '' : $joiner) . $content);
}
if($lableprint)
{
- $contentrep[is_array($id) ? implode(':',$id) : $id] = $content;
+ $contentrep[is_array($id) ? implode(':', $id) : $id] = $content;
}
}
- if ($Labelrepeat)
+ if($Labelrepeat)
{
- $countpage=0;
- $count=0;
- $contentrepeatpages[$countpage] = $Labelstart.$Labeltend;
+ $countpage = 0;
+ $count = 0;
+ $contentrepeatpages[$countpage] = $Labelstart . $Labeltend;
- foreach ($contentrep as $Label)
+ foreach($contentrep as $Label)
{
- $contentrepeatpages[$countpage] = preg_replace('/\$\$labelplacement\$\$/',$Label,$contentrepeatpages[$countpage],1);
- $count=$count+1;
- if (($count % $countlables) == 0 && count($contentrep)>$count) //new page
+ $contentrepeatpages[$countpage] = preg_replace('/\$\$labelplacement\$\$/', $Label, $contentrepeatpages[$countpage], 1);
+ $count = $count + 1;
+ if(($count % $countlables) == 0 && count($contentrep) > $count) //new page
{
- $countpage = $countpage+1;
- $contentrepeatpages[$countpage] = $Labelstart.$Labeltend;
+ $countpage = $countpage + 1;
+ $contentrepeatpages[$countpage] = $Labelstart . $Labeltend;
}
}
- $contentrepeatpages[$countpage] = preg_replace('/\$\$labelplacement\$\$/','',$contentrepeatpages[$countpage],-1); //clean empty fields
+ $contentrepeatpages[$countpage] = preg_replace('/\$\$labelplacement\$\$/', '', $contentrepeatpages[$countpage], -1); //clean empty fields
switch($mimetype)
{
case 'application/rtf':
case 'text/rtf':
- $ret = $contentstart.implode('\\par \\page\\pard\\plain',$contentrepeatpages).$contentend;
+ $ret = $contentstart . implode('\\par \\page\\pard\\plain', $contentrepeatpages) . $contentend;
break;
case 'application/vnd.oasis.opendocument.text':
case 'application/vnd.oasis.opendocument.presentation':
case 'application/vnd.oasis.opendocument.text-template':
case 'application/vnd.oasis.opendocument.presentation-template':
- $ret = $contentstart.implode('',$contentrepeatpages).$contentend;
+ $ret = $contentstart . implode('', $contentrepeatpages) . $contentend;
break;
case 'application/vnd.oasis.opendocument.spreadsheet':
case 'application/vnd.oasis.opendocument.spreadsheet-template':
- $ret = $contentstart.implode('',$contentrepeatpages).$contentend;
+ $ret = $contentstart . implode('', $contentrepeatpages) . $contentend;
break;
case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
case 'application/vnd.ms-word.document.macroenabled.12':
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
case 'application/vnd.ms-excel.sheet.macroenabled.12':
- $ret = $contentstart.implode('',$contentrepeatpages).$contentend;
+ $ret = $contentstart . implode('', $contentrepeatpages) . $contentend;
break;
case 'text/plain':
- $ret = $contentstart.implode("\r\n",$contentrep).$contentend;
+ $ret = $contentstart . implode("\r\n", $contentrep) . $contentend;
break;
default:
- $err = lang('%1 not implemented for %2!','$$labelplacement$$',$mimetype);
+ $err = lang('%1 not implemented for %2!', '$$labelplacement$$', $mimetype);
$ret = false;
}
return $ret;
}
- if ($contentrepeat)
+ if($contentrepeat)
{
fwrite($content_stream, $contentend);
rewind($content_stream);
$content = stream_get_contents($content_stream);
}
- if ($this->report_memory_usage) error_log(__METHOD__."() returning ".Api\Vfs::hsize(memory_get_peak_usage(true)));
+ if($this->report_memory_usage)
+ {
+ error_log(__METHOD__ . "() returning " . Api\Vfs::hsize(memory_get_peak_usage(true)));
+ }
return $content;
}
@@ -1117,54 +1207,57 @@ abstract class Merge
* @param string $charset =null charset to override default set by mimetype or export charset
* @return string
*/
- protected function replace($content,array $replacements,$mimetype,$mso_application_progid='',$charset=null)
+ protected function replace($content, array $replacements, $mimetype, $mso_application_progid = '', $charset = null)
{
switch($mimetype)
{
- case 'application/vnd.oasis.opendocument.text': // open office
+ case 'application/vnd.oasis.opendocument.text': // open office
case 'application/vnd.oasis.opendocument.spreadsheet':
case 'application/vnd.oasis.opendocument.presentation':
case 'application/vnd.oasis.opendocument.text-template':
case 'application/vnd.oasis.opendocument.spreadsheet-template':
case 'application/vnd.oasis.opendocument.presentation-template':
- case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': // ms office 2007
+ case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': // ms office 2007
case 'application/vnd.ms-word.document.macroenabled.12':
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
case 'application/vnd.ms-excel.sheet.macroenabled.12':
case 'application/xml':
case 'text/xml':
$is_xml = true;
- $charset = 'utf-8'; // xml files --> always use utf-8
+ $charset = 'utf-8'; // xml files --> always use utf-8
break;
case 'application/rtf':
case 'text/rtf':
- $charset = 'iso-8859-1'; // rtf seems to user iso-8859-1 or equivalent windows charset, not utf-8
+ $charset = 'iso-8859-1'; // rtf seems to user iso-8859-1 or equivalent windows charset, not utf-8
break;
case 'text/html':
$is_xml = true;
$matches = null;
- if (preg_match('/ use our export-charset, defined in addressbook prefs
- if (empty($charset)) $charset = $this->contacts->prefs['csv_charset'];
+ default: // div. text files --> use our export-charset, defined in addressbook prefs
+ if(empty($charset))
+ {
+ $charset = $this->contacts->prefs['csv_charset'];
+ }
break;
}
//error_log(__METHOD__."('$document', ... ,$mimetype) --> $charset (egw=".Api\Translation::charset().', export='.$this->contacts->prefs['csv_charset'].')');
// do we need to convert charset
- if ($charset && $charset != Api\Translation::charset())
+ if($charset && $charset != Api\Translation::charset())
{
- $replacements = Api\Translation::convert($replacements,Api\Translation::charset(),$charset);
+ $replacements = Api\Translation::convert($replacements, Api\Translation::charset(), $charset);
}
// Date only placeholders for timestamps
@@ -1172,14 +1265,14 @@ abstract class Merge
{
foreach($this->date_fields as $field)
{
- if(($value = $replacements['$$'.$field.'$$'] ?? null))
+ if(($value = $replacements['$$' . $field . '$$'] ?? null))
{
- $time = Api\DateTime::createFromFormat('+'.Api\DateTime::$user_dateformat.' '.Api\DateTime::$user_timeformat.'*', $value);
- $replacements['$$'.$field.'/date$$'] = $time ? $time->format(Api\DateTime::$user_dateformat) : '';
+ $time = Api\DateTime::createFromFormat('+' . Api\DateTime::$user_dateformat . ' ' . Api\DateTime::$user_timeformat . '*', $value);
+ $replacements['$$' . $field . '/date$$'] = $time ? $time->format(Api\DateTime::$user_dateformat) : '';
}
}
}
- if ($is_xml) // zip'ed xml document (eg. OO)
+ if($is_xml) // zip'ed xml document (eg. OO)
{
// Numeric fields
$names = array();
@@ -1187,35 +1280,35 @@ abstract class Merge
// Tags we can replace with the target document's version
$replace_tags = array();
// only keep tags, if we have xsl extension available
- if (class_exists('XSLTProcessor') && class_exists('DOMDocument') && $this->parse_html_styles)
+ if(class_exists('XSLTProcessor') && class_exists('DOMDocument') && $this->parse_html_styles)
{
- switch($mimetype.$mso_application_progid)
+ switch($mimetype . $mso_application_progid)
{
case 'text/html':
$replace_tags = array(
- '','','','','','','','