PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_acymailing/extensions/plg_acymailing_template/template.php

https://gitlab.com/endomorphosis/OLAAaction
PHP | 97 lines | 93 code | 0 blank | 4 comment | 22 complexity | 3c79212c02c1ec4cffb67d1538bed609 MD5 | raw file
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2009-2011 ACYBA SARL - All rights reserved.
  4. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU/GPL
  5. */
  6. defined('_JEXEC') or die('Restricted access');
  7. ?>
  8. <?php
  9. class plgAcymailingTemplate extends JPlugin
  10. {
  11. var $templates = array();
  12. var $tags = array();
  13. var $headerstyles = array();
  14. var $others = array();
  15. var $stylesheets = array();
  16. var $templateClass = '';
  17. var $config;
  18. function plgAcymailingTemplate(&$subject, $config){
  19. parent::__construct($subject, $config);
  20. if(!isset($this->params)){
  21. $plugin =& JPluginHelper::getPlugin('acymailing', 'template');
  22. $this->params = new JParameter( $plugin->params );
  23. }
  24. $this->config = acymailing_config();
  25. if(version_compare(PHP_VERSION, '5.0.0', '>=') && class_exists('DOMDocument') && function_exists('mb_convert_encoding')){
  26. require_once(ACYMAILING_FRONT.'inc'.DS.'emogrifier'.DS.'emogrifier.php');
  27. }
  28. }
  29. function acymailing_replaceusertagspreview(&$email,&$user){
  30. return $this->acymailing_replaceusertags($email,$user,false);
  31. }
  32. function _applyTemplate(&$email,$addbody){
  33. if(empty($email->tempid)) return;
  34. if(!isset($this->templates[$email->tempid])){
  35. $this->headerstyles[$email->tempid] = array();
  36. $this->headerstyles[$email->tempid][] = '.ReadMsgBody{width: 100%;}';
  37. $this->headerstyles[$email->tempid][] = '.ExternalClass{width: 100%;}';
  38. $this->templates[$email->tempid] = array();
  39. if(empty($this->templateClass)){
  40. $this->templateClass = acymailing_get('class.template');
  41. }
  42. $template = $this->templateClass->get($email->tempid);
  43. if(!empty($template->styles) OR !empty($template->stylesheet)) $this->stylesheets[$email->tempid] = $this->templateClass->buildCSS($template->styles,$template->stylesheet);
  44. if(!empty($template->styles)){
  45. foreach($template->styles as $class => $style){
  46. if(empty($style)) continue;
  47. if(preg_match('#^tag_(.*)$#',$class,$result)){
  48. $this->tags[$email->tempid]['#< *'.$result[1].'((?:(?!style).)*)>#Ui'] = '<'.$result[1].' style="'.$style.'" $1>';
  49. if(strpos($style,'!important')) $this->headerstyles[$email->tempid][] = $result[1].'{ '.str_replace('!important','',$style).' }';
  50. }elseif($class == 'color_bg'){
  51. $this->others[$email->tempid][$class] = $style;
  52. }else{
  53. $this->templates[$email->tempid]['class="'.$class.'"'] = 'style="'.$style.'"';
  54. }
  55. }
  56. }
  57. }
  58. if($addbody AND !strpos($email->body,'</body>')){
  59. $before = '<html><head>'."\n";
  60. $before .= '<meta http-equiv="Content-Type" content="text/html; charset='.strtolower($this->config->get('charset')).'">'."\n";
  61. $before .= '<title>'.$email->subject.'</title>'."\n";
  62. if(!empty($this->headerstyles[$email->tempid])){
  63. $before .= '<style type="text/css">'."\n";
  64. $before .= implode("\n",$this->headerstyles[$email->tempid])."\n";
  65. $before .= '</style>'."\n";
  66. }
  67. $before .= '</head>'."\n".'<body';
  68. if(!empty($this->others[$email->tempid]['color_bg'])) $before .= ' bgcolor="'.$this->others[$email->tempid]['color_bg'].'" ';
  69. $before .= '>'."\n";
  70. $email->body = $before.$email->body.'</body>'."\n".'</html>';
  71. }
  72. if(!empty($this->templates[$email->tempid])){
  73. $email->body = str_replace(array_keys($this->templates[$email->tempid]),$this->templates[$email->tempid],$email->body);
  74. }
  75. if(!empty($this->stylesheets[$email->tempid]) AND class_exists('Emogrifier')){
  76. $emogrifier = new Emogrifier($email->body,$this->stylesheets[$email->tempid]);
  77. $email->body = $emogrifier->emogrify();
  78. if(!$addbody AND strpos($email->body,'<!DOCTYPE') !== false){
  79. $email->body = preg_replace('#<\!DOCTYPE.*<body([^>]*)>#Usi','',$email->body);
  80. $email->body = preg_replace('#</body>.*$#si','',$email->body);
  81. }
  82. }elseif(!empty($this->tags[$email->tempid])){
  83. $email->body = preg_replace(array_keys($this->tags[$email->tempid]),$this->tags[$email->tempid],$email->body);
  84. }
  85. if($addbody) $email->body = preg_replace('#(<[^>]*)(class|id)="[^"]*"#Ui','$1',$email->body);
  86. }
  87. function acymailing_replaceusertags(&$email,&$user,$send = true){
  88. if(!$email->sendHTML) return;
  89. $this->_applyTemplate($email,$send);
  90. $email->body = preg_replace('#< *(tr|td|table)([^>]*)(style="[^"]*)background-image *: *url\(\'?([^)\']*)\'?\);?#Ui','<$1 background="$4" $2 $3',$email->body);
  91. $email->body = acymailing_absoluteURL($email->body);
  92. $email->body = preg_replace('#< *img([^>]*)(style="[^"]*)(float *: *)(right|left|top|bottom|middle)#Ui','<img$1 align="$4" hspace="5" $2$3$4',$email->body);
  93. if(!preg_match('#(<thead|<tfoot|< *tbody *[^> ]+ *>)#Ui',$email->body)){
  94. $email->body = preg_replace('#< *\/? *tbody *>#Ui','',$email->body);
  95. }
  96. }//endfct
  97. }//endclass