1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Core_Model_Email_Template_Filter extends Varien_Filter_Template
36: {
37: 38: 39: 40: 41:
42: protected $_useAbsoluteLinks = false;
43:
44: 45: 46: 47: 48:
49: protected $_useSessionInUrl = false;
50:
51: 52: 53: 54:
55: protected static $_urlInstance;
56:
57: 58: 59: 60: 61:
62: protected $_modifiers = array('nl2br' => '');
63:
64: protected $_storeId = null;
65:
66: protected $_plainTemplateMode = false;
67:
68: 69: 70: 71:
72: public function __construct()
73: {
74: $this->_modifiers['escape'] = array($this, 'modifierEscape');
75: }
76:
77: 78: 79: 80: 81: 82:
83: public function setUseAbsoluteLinks($flag)
84: {
85: $this->_useAbsoluteLinks = $flag;
86: return $this;
87: }
88:
89: 90: 91: 92: 93: 94: 95:
96: public function setUseSessionInUrl($flag)
97: {
98: $this->_useSessionInUrl = $flag;
99: return $this;
100: }
101:
102: 103: 104: 105: 106: 107:
108: public function setPlainTemplateMode($plainTemplateMode)
109: {
110: $this->_plainTemplateMode = (bool)$plainTemplateMode;
111: return $this;
112: }
113:
114: 115: 116: 117: 118:
119: public function getPlainTemplateMode()
120: {
121: return $this->_plainTemplateMode;
122: }
123:
124: 125: 126: 127: 128: 129:
130: public function setStoreId($storeId)
131: {
132: $this->_storeId = $storeId;
133: return $this;
134: }
135:
136: 137: 138: 139: 140: 141:
142: public function getStoreId()
143: {
144: if (null === $this->_storeId) {
145: $this->_storeId = Mage::app()->getStore()->getId();
146: }
147: return $this->_storeId;
148: }
149:
150: 151: 152: 153: 154: 155:
156: public function blockDirective($construction)
157: {
158: $skipParams = array('type', 'id', 'output');
159: $blockParameters = $this->_getIncludeParameters($construction[2]);
160: $layout = Mage::app()->getLayout();
161:
162: if (isset($blockParameters['type'])) {
163: $type = $blockParameters['type'];
164: $block = $layout->createBlock($type, null, $blockParameters);
165: } elseif (isset($blockParameters['id'])) {
166: $block = $layout->createBlock('cms/block');
167: if ($block) {
168: $block->setBlockId($blockParameters['id']);
169: }
170: }
171:
172: if ($block) {
173: $block->setBlockParams($blockParameters);
174: foreach ($blockParameters as $k => $v) {
175: if (in_array($k, $skipParams)) {
176: continue;
177: }
178: $block->setDataUsingMethod($k, $v);
179: }
180: }
181:
182: if (!$block) {
183: return '';
184: }
185: if (isset($blockParameters['output'])) {
186: $method = $blockParameters['output'];
187: }
188: if (!isset($method) || !is_string($method) || !is_callable(array($block, $method))) {
189: $method = 'toHtml';
190: }
191: return $block->$method();
192: }
193:
194: 195: 196: 197: 198: 199:
200: public function layoutDirective($construction)
201: {
202: $skipParams = array('handle', 'area');
203:
204: $params = $this->_getIncludeParameters($construction[2]);
205: $layout = Mage::getModel('core/layout');
206:
207: if (isset($params['area'])) {
208: $layout->setArea($params['area']);
209: }
210: else {
211: $layout->setArea(Mage::app()->getLayout()->getArea());
212: }
213:
214: $layout->getUpdate()->addHandle($params['handle']);
215: $layout->getUpdate()->load();
216:
217: $layout->generateXml();
218: $layout->generateBlocks();
219:
220: foreach ($layout->getAllBlocks() as $blockName => $block) {
221:
222: foreach ($params as $k => $v) {
223: if (in_array($k, $skipParams)) {
224: continue;
225: }
226:
227: $block->setDataUsingMethod($k, $v);
228: }
229: }
230:
231: 232: 233:
234: $allBlocks = $layout->getAllBlocks();
235: $firstBlock = reset($allBlocks);
236: if ($firstBlock) {
237: $layout->addOutputBlock($firstBlock->getNameInLayout());
238: }
239:
240: $layout->setDirectOutput(false);
241: return $layout->getOutput();
242: }
243:
244: 245: 246: 247: 248: 249:
250: protected function _getBlockParameters($value)
251: {
252: $tokenizer = new Varien_Filter_Template_Tokenizer_Parameter();
253: $tokenizer->setString($value);
254:
255: return $tokenizer->tokenize();
256: }
257:
258: 259: 260: 261: 262: 263:
264: public function skinDirective($construction)
265: {
266: $params = $this->_getIncludeParameters($construction[2]);
267: $params['_absolute'] = $this->_useAbsoluteLinks;
268:
269: $url = Mage::getDesign()->getSkinUrl($params['url'], $params);
270:
271: return $url;
272: }
273:
274: 275: 276: 277: 278: 279:
280: public function mediaDirective($construction)
281: {
282: $params = $this->_getIncludeParameters($construction[2]);
283: return Mage::getBaseUrl('media') . $params['url'];
284: }
285:
286: 287: 288: 289: 290: 291: 292:
293: public function storeDirective($construction)
294: {
295: $params = $this->_getIncludeParameters($construction[2]);
296: if (!isset($params['_query'])) {
297: $params['_query'] = array();
298: }
299: foreach ($params as $k => $v) {
300: if (strpos($k, '_query_') === 0) {
301: $params['_query'][substr($k, 7)] = $v;
302: unset($params[$k]);
303: }
304: }
305: $params['_absolute'] = $this->_useAbsoluteLinks;
306:
307: if ($this->_useSessionInUrl === false) {
308: $params['_nosid'] = true;
309: }
310:
311: if (isset($params['direct_url'])) {
312: $path = '';
313: $params['_direct'] = $params['direct_url'];
314: unset($params['direct_url']);
315: }
316: else {
317: $path = isset($params['url']) ? $params['url'] : '';
318: unset($params['url']);
319: }
320:
321: return Mage::app()->getStore(Mage::getDesign()->getStore())->getUrl($path, $params);
322: }
323:
324: 325: 326: 327: 328: 329: 330: 331:
332: public function htmlescapeDirective($construction)
333: {
334: $params = $this->_getIncludeParameters($construction[2]);
335: if (!isset($params['var'])) {
336: return '';
337: }
338:
339: $allowedTags = null;
340: if (isset($params['allowed_tags'])) {
341: $allowedTags = preg_split('/\s*\,\s*/', $params['allowed_tags'], 0, PREG_SPLIT_NO_EMPTY);
342: }
343:
344: return Mage::helper('core')->htmlEscape($params['var'], $allowedTags);
345: }
346:
347: 348: 349: 350: 351: 352:
353: public function varDirective($construction)
354: {
355: if (count($this->_templateVars)==0) {
356:
357: return $construction[0];
358: }
359:
360: $parts = explode('|', $construction[2], 2);
361: if (2 === count($parts)) {
362: list($variableName, $modifiersString) = $parts;
363: return $this->_amplifyModifiers($this->_getVariable($variableName, ''), $modifiersString);
364: }
365: return $this->_getVariable($construction[2], '');
366: }
367:
368: 369: 370: 371: 372: 373: 374: 375: 376:
377: protected function _amplifyModifiers($value, $modifiers)
378: {
379: foreach (explode('|', $modifiers) as $part) {
380: if (empty($part)) {
381: continue;
382: }
383: $params = explode(':', $part);
384: $modifier = array_shift($params);
385: if (isset($this->_modifiers[$modifier])) {
386: $callback = $this->_modifiers[$modifier];
387: if (!$callback) {
388: $callback = $modifier;
389: }
390: array_unshift($params, $value);
391: $value = call_user_func_array($callback, $params);
392: }
393: }
394: return $value;
395: }
396:
397: 398: 399: 400: 401: 402: 403:
404: public function modifierEscape($value, $type = 'html')
405: {
406: switch ($type) {
407: case 'html':
408: return htmlspecialchars($value, ENT_QUOTES);
409:
410: case 'htmlentities':
411: return htmlentities($value, ENT_QUOTES);
412:
413: case 'url':
414: return rawurlencode($value);
415: }
416: return $value;
417: }
418:
419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430:
431: public function protocolDirective($construction)
432: {
433: $params = $this->_getIncludeParameters($construction[2]);
434: $store = null;
435: if (isset($params['store'])) {
436: $store = Mage::app()->getSafeStore($params['store']);
437: }
438: $isSecure = Mage::app()->getStore($store)->isCurrentlySecure();
439: $protocol = $isSecure ? 'https' : 'http';
440: if (isset($params['url'])) {
441: return $protocol . '://' . $params['url'];
442: }
443: elseif (isset($params['http']) && isset($params['https'])) {
444: if ($isSecure) {
445: return $params['https'];
446: }
447: return $params['http'];
448: }
449:
450: return $protocol;
451: }
452:
453: 454: 455: 456: 457: 458:
459: public function configDirective($construction)
460: {
461: $configValue = '';
462: $params = $this->_getIncludeParameters($construction[2]);
463: $storeId = $this->getStoreId();
464: if (isset($params['path'])) {
465: $configValue = Mage::getStoreConfig($params['path'], $storeId);
466: }
467: return $configValue;
468: }
469:
470: 471: 472: 473: 474: 475:
476: public function customvarDirective($construction)
477: {
478: $customVarValue = '';
479: $params = $this->_getIncludeParameters($construction[2]);
480: if (isset($params['code'])) {
481: $variable = Mage::getModel('core/variable')
482: ->setStoreId($this->getStoreId())
483: ->loadByCode($params['code']);
484: $mode = $this->getPlainTemplateMode()?Mage_Core_Model_Variable::TYPE_TEXT:Mage_Core_Model_Variable::TYPE_HTML;
485: if ($value = $variable->getValue($mode)) {
486: $customVarValue = $value;
487: }
488: }
489: return $customVarValue;
490: }
491:
492: 493: 494: 495: 496: 497: 498:
499: public function filter($value)
500: {
501: try {
502: $value = parent::filter($value);
503: } catch (Exception $e) {
504: $value = '';
505: Mage::logException($e);
506: }
507: return $value;
508: }
509: }
510: