/** * Copy JS/CSS file to cache with replace code: * ~~~URL~TYPE=<type>~MODULE=<module>~FILE=<file>~PRESET=<preset>~TAG=<tag>~ATTR=<attr>~~~ * @static * @param string $origFile * @param string $cachedFile * @return int */ public static function copyToCache($origFile, $cachedFile) { //TODO: it's not a good method to extract theme name back from $cachedFile path $_ = str_replace('\\', '\\\\', DS); if (preg_match('~' . $_ . 'cache' . $_ . '(?:js|css|img)' . $_ . '([a-zA-Z][a-zA-Z0-9]{2,31})' . $_ . '~', $cachedFile, $matches)) { $themeName = $matches[1]; } else { throw new rad_exception("Incorrect cached file name: {$cachedFile} - cannot extract theme name!"); } //TODO: maybe it'd be much better to process source file line by line to reduce memory cosumption. $s = file_get_contents($origFile); if (!function_exists('smarty_function_url')) { rad_rsmarty::getSmartyObject()->loadPlugin('smarty_function_url'); } $s = preg_replace_callback('/~~~URL~~(.+)~~~/iU', function ($match) { $rows = explode('~~', trim($match[1], '~')); $params = array('load' => 'inplace'); foreach ($rows as $row) { $parts = explode('=', $row, 2); if (count($parts) == 2) { $key = strtolower($parts[0]); if (!isset($params[$key])) { $params[$key] = $parts[1]; } } } return smarty_function_url($params, null); }, $s); $s = preg_replace('/~~~THEMENAME~~~/', $themeName, $s); return file_put_contents($cachedFile, $s); }
/** * Init and gets all the params and make the html strings! * @return Boolean - if all is good */ public static function initandmake(struct_core_alias $alias) { $o = rad_rsmarty::getSmartyObject(); $o->assign('lang', call_user_func(array(rad_config::getParam('loader_class'), 'getLangContainer'))); foreach (self::$_varvals as $classname => $vrnn) { foreach ($vrnn as $varname => $varvalue) { $o->assign('<%' . $classname . '.' . $varname . '%>', $varvalue); } $o->assign($classname, $vrnn); } $srcPrefix = SMARTYBCCACHE . 'alias_' . $alias->id . '_'; self::$_title = self::_fetchSrc($o, $srcPrefix . 'title.tpl', $alias->title_script); self::$_breadcrumbs = self::_fetchSrc($o, $srcPrefix . 'bc.tpl', $alias->navi_script); self::$_tags = self::_fetchSrc($o, $srcPrefix . 'meta.tpl', $alias->metatitle_script); self::$_description = self::_fetchSrc($o, $srcPrefix . 'descr.tpl', $alias->metadescription_script); $o->clearAllAssign(); return true; }
/** * Returns Smarty Object or null * @var Smarty * @return Smarty */ public static function getSmartyObject() { if (!self::$object) { if (!class_exists('Smarty', false)) { if (!file_exists(SMARTYPATH . 'Smarty.class.php')) { die(SMARTYPATH . 'Smarty.class.php does not exists!'); } include_once SMARTYPATH . 'Smarty.class.php'; } self::$object = new Smarty(); self::$object->compile_check = rad_config::getParam('smarty.compile_check', true); self::$object->debugging = rad_config::getParam('smarty.debugging;', false); self::$object->template_dir = rad_config::getParam('rootPath'); //NB: Looks like this parameter is never used since we use absolute adressing. self::$object->compile_dir = SMARTYCOMPILEPATH; self::$object->caching = rad_config::getParam('smarty.caching', false); self::$object->cache_dir = SMARTYCACHEPATH; self::$object->muteExpectedErrors(); self::$object->loadPlugin('smarty_compiler_switch'); } return self::$object; }
/** * Send the mail * @param string|array $to - mail reciver, can be also as array('*****@*****.**' => 'John Doe') * @param enum(html|text) $format - format of letter (html or text) * @return boolean */ public function send($to, $format = 'text') { //include_once LIBPATH rad_mailtemplate::setCurrentItem($this); $o = rad_rsmarty::getSmartyObject(); if ($this->getVars()) { foreach ($this->getVars() as $key => $value) { $o->assign($key, $value); } } if (!is_file(MAILTEMPLATESPATH . $this->getTemplateName())) { throw new rad_exception('File "' . MAILTEMPLATESPATH . $this->getTemplateName() . '" not found!'); } $o->fetch(MAILTEMPLATESPATH . $this->getTemplateName()); $o->clearAllAssign(); if (empty($this->_blocks[$format])) { throw new rad_exception('Format "' . $format . '" is not declared in file: "' . MAILTEMPLATESPATH . $this->getTemplateName() . '"'); } if (!empty($this->_mailer)) { $this->_mailer->setSubject($this->_blocks[$format]['subject']); if (!empty($this->_blocks[$format]['Cc'])) { $this->_mailer->setCc($this->_blocks[$format]['Cc']); } if (!empty($this->_blocks[$format]['Bcc'])) { $this->_mailer->setBcc($this->_blocks[$format]['Bcc']); } if (!empty($this->_blocks[$format]['headers'])) { $headers = rad_mailtemplate::parseHeader($this->_blocks[$format]['headers']); if (!empty($headers)) { foreach ($headers as $headerName => $headerValue) { switch (strtolower($headerName)) { case 'x-priority': $this->_mailer->setPriority((int) $headerValue); break; default: $this->_mailer->getHeaders()->addTextHeader($headerName, $headerValue); break; } } } } if (!empty($this->_blocks[$format]['body'])) { $this->_mailer->setBody($this->_blocks[$format]['body'], $format == 'text' ? 'text/plain' : 'text/html'); } if (!empty($this->_blocks[$format]['from'])) { $from = explode("\n", str_replace("\r", '', $this->_blocks[$format]['from'])); if (count($from)) { foreach ($from as $fromString) { $fromItem = explode('<', $fromString); if (count($fromItem) > 1) { $fromName = trim($fromItem[0]); $fromEmail = trim(str_replace('>', '', $fromItem[1])); } else { $fromName = trim($fromItem[0]); $fromEmail = trim($fromItem[0]); } $this->_mailer->setFrom(array($fromEmail => $fromName)); $this->_mailer->setReturnPath($fromEmail); } } } if (!empty($this->_blocks[$format]['transport'])) { $transport = explode("\n", str_replace("\r", '', $this->_blocks[$format]['transport'])); if (!empty($transport)) { $transportParams = array(); foreach ($transport as $transportKey => $transportString) { $transportString = trim($transportString); if (!empty($transportString)) { $transportItem = explode(':', $transportString); if (count($transportItem) > 1) { $transportItemKey = trim($transportItem[0]); unset($transportItem[0]); $transportItemValue = trim(implode(':', $transportItem)); $transportParams[$transportItemKey] = $transportItemValue; } } } } if (empty($transportParams['type'])) { throw new rad_exception('Error in mailtemplate "' . $this->getTemplateName() . '" at transport block: type of the transport required!'); } switch (strtolower($transportParams['type'])) { case 'smtp': if (empty($transportParams['host']) or empty($transportParams['port']) or empty($transportParams['user']) or !isset($transportParams['password'])) { throw new rad_exception('Error in mailtemplate "' . $this->getTemplateName() . '" at transport block: Not enouph actual params!'); } $this->_transportInstance = Swift_SmtpTransport::newInstance($transportParams['host'], $transportParams['port'])->setUsername($transportParams['user'])->setPassword($transportParams['password']); if (!empty($transportParams['security'])) { $this->_transportInstance->setEncryption($transportParams['security']); } break; case 'mail': $this->_transportInstance = Swift_MailTransport::newInstance(); break; default: throw new rad_exception('Error in mailtemplate "' . $this->getTemplateName() . '" Unknown transport type "' . $transportParams['type'] . '"!'); break; } //switch } $this->_mailer->setTo($to); $this->_mailer->setCharset('utf-8'); if (!$this->_transportInstance) { $this->_transportInstance = Swift_MailTransport::newInstance(); } return rad_mailtemplate::getMailer($this->_transportInstance)->send($this->_mailer); } else { $headers = 'MIME-Version: 1.0' . PHP_EOL; $headers .= 'Content-Transfer-Encoding: base64' . PHP_EOL; $headers .= 'From: ' . $this->_blocks[$format]['from'] . PHP_EOL; switch ($format) { case 'text': $headers = 'Content-Type: text/plain; charset=utf-8' . PHP_EOL; break; case 'html': $headers .= 'Content-type: text/html; charset=utf-8' . PHP_EOL; break; default: throw new rad_exception('Unknown format: "' . $format . '"'); break; } if (!empty($this->_blocks[$format]['Cc'])) { $headers .= 'Cc: ' . $this->_blocks[$format]['Cc'] . PHP_EOL; } if (!empty($this->_blocks[$format]['Bcc'])) { $headers .= 'Bcc: ' . $this->_blocks[$format]['Bcc'] . PHP_EOL; } if (!empty($this->_blocks[$format]['headers'])) { $headers .= $this->_blocks[$format]['headers']; } if (is_array($to)) { $toString = ''; foreach ($to as $toEmail => $toName) { $toString .= $toName . ' <' . $toEmail . '>,'; } $to = substr($toString, 0, strlen($toString) - 1); } return mail($to, $this->_blocks[$format]['subject'], chunk_split(base64_encode($this->_blocks[$format]['body'])), $headers); } }
/** * Function that is called to show all variables and all the page! */ public static function show() { $o = rad_rsmarty::getSmartyObject(); //TODO: optimize memory if (count(self::$_html)) { foreach (self::$_html as $key => $value) { $o->assign($key, $value); } } $o->assign('_CURR_LANG_', rad_lang::getCurrentLanguage()); if (isset(self::$sendedParams[self::$_alias->filename])) { foreach (self::$sendedParams[self::$_alias->filename] as $pkey => $pval) { $o->assign($pkey, $pval); } } $file = rad_themer::getFilePath(null, 'templates', 'core', 'maintemplates' . DS . self::$_alias->filename . '.tpl'); rad_instances::setCurrentTemplate($file, true); $o->display($file); }