1<?php 2/* 3 * e107 website system 4 * 5 * Copyright (C) 2008-2013 e107 Inc (e107.org) 6 * Released under the terms and conditions of the 7 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) 8 * 9 * Mailout - template-related functions 10 * 11 * $URL: https://e107.svn.sourceforge.net/svnroot/e107/trunk/e107_0.8/e107_handlers/redirection_class.php $ 12 * $Id: redirection_class.php 11922 2010-10-27 11:31:18Z secretr $ 13 * $Revision: 11315 $ 14 * 15 */ 16 17/** 18 * Various template-related mailout functions 19 * 20 * @package e107 21 * @subpackage e107_handlers 22 * @version $Id: mailout_admin_class.php 11315 2010-02-10 18:18:01Z secretr $; 23 * 24 * Handles common aspects of template-based emails 25 */ 26 27 28class e107MailTemplate 29{ 30 public $lastTemplateData = FALSE; // Mailer template info - cache 31 public $mainBodyText = FALSE; // Cache for body text 32 public $altBodyText = FALSE; // Cache for alternate body text 33 34 35 36 /** 37 * Empty our template cache 38 * 39 * @return none 40 */ 41 public function clearTemplateCache() 42 { 43 $this->lastTemplateData = FALSE; 44 } 45 46 47 /** 48 * Set a template to be used. 49 * Clears any cached data 50 * 51 * @param array|string $newTemplate - if a string, the name of a template. (The internal name of the variable, not the associated name) 52 * If an array, an existing template - must be in the correct format - no checking done 53 * 54 * @return boolean TRUE if accepted, FALSE if rejected 55 */ 56 public function setNewTemplate($newTemplate) 57 { 58 $this->mainBodyText = FALSE; 59 $this->altBodyText = FALSE; 60 61 if (is_array($newTemplate)) 62 { 63 $this->lastTemplateData = $newTemplate; 64 return TRUE; 65 } 66 return $this->loadTemplateInfo($newTemplate); 67 } 68 69 70 71 /** 72 * Given a template name, assembles the array of data required by sendTemplated() and saves in our cache 73 * 74 * Template file name is 'email_template.php' 75 * The template is first sought in the template file of the current theme directory, and data read as available. 76 * If $extraFile is specified, that is searched next 77 * Gaps are then filled in from the core template file. 78 * 79 * @param string $templateName - name of required template 80 * @param string $extraFile - optional path to additional template file (intended for plugins) 81 * (This is read between the theme-specific file and the defaults) 82 * 83 * @return boolean TRUE - template found and loaded. FALSE if not found. 84 * If successful, we store an array in $lastTemplateData, with exactly six elements: 85 * 'template_name' 86 * 'email_overrides' - any override information (often an empty array) 87 * 'email_header' - any header information (usually loaded from the default) 88 * 'email_body' 89 * 'email_footer' 90 * 'email_plainText' - optional template for plain text part of email 91 */ 92 public function loadTemplateInfo($templateName, $extraFile = FALSE) 93 { 94 static $requiredFields = array ('email_overrides', 'email_header', 'email_body', 'email_footer', 'email_plainText'); 95 96 if (is_array($this->lastTemplateData)) 97 { 98 if ($this->lastTemplateData['template_name'] == $templateName) 99 { 100 return $this->lastTemplateData; 101 } 102 $this->lastTemplateData = FALSE; // Obviously a new template 103 } 104 105 $ret = array('email_overrides' => '', 'email_header' => '', 'email_body' => '', 'email_footer' => '', 'email_plainText' => ''); 106 if (!in_array($templateName, array('textonly', 'texthtml', 'texttheme'))) 107 { 108 $found = 0; // Count number of field definitions we've found 109 110 $fileList = array(THEME.'templates/email_template.php'); 111 if ($extraFile) 112 { 113 $fileList[] = $extraFile; 114 } 115 $fileList[] = e_CORE.'templates/email_template.php'; 116 foreach ($fileList as $templateFileName ) // Override file, optional plugin file then defaults 117 { 118 119 if (($found < count($requiredFields)) && is_readable($templateFileName)) 120 { 121 require_once($templateFileName); 122 123 //$tVars = get_defined_vars(); 124 //if (isset($tVars['GLOBALS'])) unset($tVars['GLOBALS']); 125 //print_a($tVars); 126 127 if (isset($$templateName)) 128 { 129 if (is_array($$templateName)) 130 { 131 foreach ($requiredFields as $k) 132 { 133 if (!$ret[$k] && isset(${$templateName}[$k])) 134 { 135 $ret[$k] = ${$templateName}[$k]; 136 $found++; 137 } 138 } 139 } 140 else 141 { 142 $ret['email_body'] = $$templateName; // Non-array just defines body of email 143 $found++; 144 } 145 } 146 } 147 } 148 149 // Now fill in the gaps from the defaults 150 if ($found < count($requiredFields)) 151 { 152 foreach ($requiredFields as $k) 153 { 154 $override = strtoupper($k); 155 if (!$ret[$k] && isset($$override)) 156 { 157 $ret[$k] = $$override; 158 $found++; 159 } 160 } 161 } 162 if (($found == 0) || !$ret['email_body']) // Pointless if we haven't defined a body 163 { 164 return FALSE; 165 } 166 } 167 168 $this->lastTemplateData = $ret; 169 $this->lastTemplateData['template_name'] = $templateName; // Cache template 170 return $this->lastTemplateData; // Return this rather than $ret, so return is consistent with cached data 171 } 172 173 174 175 /** 176 * Creates email body text according to options, using the cached template information. 177 * Caches body, and potentially alternate body 178 * 179 * @param $text string - text to process 180 * @param boolean $incImages - valid only with HTML and templated output: 181 * if true any 'absolute' format images are embedded in the source of the email. 182 * if FALSE, absolute links are converted to URLs on the local server 183 * 184 * @return boolean TRUE for success, FALSE on error (no template defined) 185 */ 186 public function makeEmailBody($text, $incImages = TRUE) 187 { 188 if (!is_array( $this->lastTemplateData)) return FALSE; 189 if (!isset($this->lastTemplateData['template_name'])) return FALSE; 190 191 $tp = e107::getParser(); 192 193 // textonly - generate plain text email 194 // texthtml - HTML format email, no theme info 195 // texttheme - HTML format email, including current theme stylesheet etc 196 $format = $this->lastTemplateData['template_name']; 197 if (!$format) 198 { 199 echo 'No format specified!'; 200 return FALSE; 201 } 202 203 if ($format == 'textonly') 204 { // Plain text email - strip bbcodes etc 205 $temp = $tp->toHTML($text, TRUE, 'E_BODY_PLAIN'); // Decode bbcodes into HTML, plain text as far as possible etc 206 $temp = stripslashes(strip_tags($temp)); // Have to do strip_tags() again in case bbcode added some 207 $this->mainBodyText = $temp; 208 $this->altBodyText = ''; 209 return TRUE; 210 } 211 212 $consts = $incImages ? ',consts_abs' : 'consts_full'; // If inline images, absolute constants so we can change them 213 214 if (($format != 'texthtml') && ($format != 'texttheme')) 215 { // Specific theme - loaded already 216 $mailHeader = $tp->parseTemplate($this->lastTemplateData['email_header'], TRUE); 217 $mailBody = $tp->parseTemplate(str_replace('{BODY}', $text, $this->lastTemplateData['email_body']), TRUE); 218 $mailFooter = $tp->parseTemplate($this->lastTemplateData['email_footer'], TRUE); 219 220 $mailBody = $mailHeader.$mailBody.$mailFooter; 221 } 222 223 224 if (($format == 'texthtml') || ($format == 'texttheme')) 225 { 226 // HTML format email here, using hard-coded defaults 227 $mailHeader = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"; 228 $mailHeader .= "<html xmlns='http://www.w3.org/1999/xhtml' >\n"; 229 $mailHeader .= "<head><meta http-equiv='content-type' content='text/html; charset=utf-8' />\n"; 230 if ($format == 'texttheme') 231 { 232 $styleFile = THEME.'emailstyle.css'; 233 if (!is_readable($styleFile)) { $styleFile = THEME."/style.css"; } 234 $style_css = file_get_contents($styleFile); 235 $mailHeader .= "<style>\n".$style_css."\n</style>"; 236 } 237 $mailHeader .= "</head>\n"; 238 239 240 $mailBody = $mailHeader."<body>\n"; 241 if ($format == 'texttheme') 242 { 243 $mailBody .= "<div style='padding:10px;width:97%'><div class='forumheader3'>\n"; 244 $mailBody .= $tp->toHTML($text, TRUE, 'E_BODY'.$consts)."</div></div></body></html>"; 245 } 246 else 247 { 248 $mailBody .= $tp->toHTML($text, TRUE, 'E_BODY'.$consts)."</body></html>"; 249 $mailBody = str_replace(""", '"', $mailBody); 250 } 251 252 $mailBody = stripslashes($mailBody); 253 } 254 255 256 if (!$incImages) 257 { 258 // Handle internally generated 'absolute' links - they need the full URL 259 $mailBody = str_replace("src='".e_HTTP, "src='".SITEURL, $mailBody); 260 $mailBody = str_replace('src="'.e_HTTP, 'src="'.SITEURL, $mailBody); 261 $mailBody = str_replace("href='".e_HTTP, "src='".SITEURL, $mailBody); 262 $mailBody = str_replace('href="'.e_HTTP, 'src="'.SITEURL, $mailBody); 263 } 264 265// print_a($mailBody); 266 $this->mainBodyText = $mailBody; 267 $this->altBodyText = ''; 268 if ($this->lastTemplateData['email_plainText']) 269 { 270 $this->altBodyText = $tp->parseTemplate(str_replace('{BODY}', $text, $this->lastTemplateData['email_plainText']), TRUE); 271 } 272 return TRUE; 273 } 274} 275 276 277