| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | namespace SimpleID\Util\UI; |
| 24: | |
| 25: | use \Base; |
| 26: | use \Markdown; |
| 27: | use \Template as F3Template; |
| 28: | use SimpleID\Util\ArrayWrapper; |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | class Template extends F3Template implements AttachmentManagerInterface { |
| 36: | use AttachmentManagerTrait; |
| 37: | |
| 38: | |
| 39: | protected $returnValues; |
| 40: | |
| 41: | public function __construct() { |
| 42: | |
| 43: | parent::__construct(); |
| 44: | |
| 45: | $this->returnValues = new ArrayWrapper(); |
| 46: | |
| 47: | |
| 48: | |
| 49: | if (!$this->fw->exists('attachments')) $this->fw->set('attachments', []); |
| 50: | $this->attachments = &$this->fw->ref('attachments'); |
| 51: | |
| 52: | |
| 53: | $this->filter('attr', static::class . '::instance()->attr'); |
| 54: | $this->filter('js', static::class . '::instance()->js'); |
| 55: | $this->filter('markdown', static::class . '::instance()->markdown'); |
| 56: | $this->filter('html_to_text', static::class . '::instance()->html_to_text'); |
| 57: | } |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | public function _capture(array $node) { |
| 70: | $attrib = $node['@attrib']; |
| 71: | unset($node['@attrib']); |
| 72: | |
| 73: | if ($attrib['to'] && preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $attrib['to'])) { |
| 74: | return '<?php ob_start(); ?>' |
| 75: | . $this->build($node) |
| 76: | . '<?php $' . $attrib['to'] .' = ob_get_clean(); ?>'; |
| 77: | } else { |
| 78: | return ''; |
| 79: | } |
| 80: | } |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | public function _return(array $node) { |
| 93: | $out = ''; |
| 94: | foreach ($node['@attrib'] as $key => $val) { |
| 95: | $out .= '$this->setReturnValue(' . Base::instance()->stringify($key) . ', '. |
| 96: | (preg_match('/\{\{(.+?)\}\}/',$val?:'')? |
| 97: | $this->token($val): |
| 98: | Base::instance()->stringify($val)).'); '; |
| 99: | } |
| 100: | return '<?php '.$out.'?>'; |
| 101: | } |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | public function _mail_callout(array $node) { |
| 110: | $callout_bg = '<?= (' . $this->token('@@lightmode.callout_bg') . ') ?>'; |
| 111: | $callout_text = '<?= (' . $this->token('@@lightmode.callout_text') . ') ?>'; |
| 112: | |
| 113: | $out = '<div style="padding: 0 20px 20px;">'; |
| 114: | $out .= '<table align="center" role="presentation" cellspacing="0" cellpadding="0" border="0" style="margin: auto;">'; |
| 115: | $out .= '<tr>'; |
| 116: | $out .= '<td class="callout-td" style="background:' . $callout_bg . ';">'; |
| 117: | $out .= '<div class="callout-div" style="background: ' . $callout_bg . '; font-family: sans-serif; font-size: 21px; line-height: 21px; text-decoration: none; padding: 19px 23px; color: ' . $callout_text . ';">'; |
| 118: | $out .= $this->build($node); |
| 119: | $out .= '</div></td>'; |
| 120: | $out .= '</tr>'; |
| 121: | $out .= '</table>'; |
| 122: | $out .= '</div>'; |
| 123: | |
| 124: | return $out; |
| 125: | } |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | public function _mail_button(array $node) { |
| 138: | $attrib = $node['@attrib']; |
| 139: | unset($node['@attrib']); |
| 140: | |
| 141: | $button_bg = '<?= (' . $this->token('@@lightmode.button_bg') . ') ?>'; |
| 142: | $button_border = '<?= (' . $this->token('@@lightmode.button_border') . ') ?>'; |
| 143: | $button_text = '<?= (' . $this->token('@@lightmode.button_text') . ') ?>'; |
| 144: | $href = (preg_match('/\{\{(.+?)\}\}/',$attrib['href']?:'')? |
| 145: | $this->token($attrib['href']): |
| 146: | Base::instance()->stringify($attrib['href'])); |
| 147: | |
| 148: | $out = '<div style="padding: 0 20px 20px;">'; |
| 149: | $out .= '<table align="center" role="presentation" cellspacing="0" cellpadding="0" border="0" style="margin: auto;">'; |
| 150: | $out .= '<tr>'; |
| 151: | $out .= '<td class="button-td button-td-primary" style="border-radius: 4px; background:' . $button_bg . ';">'; |
| 152: | $out .= '<a class="button-a button-a-primary" href="' . $href . '" style="background: ' . $button_bg . '; border: 1px solid ' . $button_border . '; font-family: sans-serif; font-size: 15px; line-height: 15px; text-decoration: none; padding: 13px 17px; color: ' . $button_text . '; display: block; border-radius: 4px;">'; |
| 153: | $out .= $this->build($node); |
| 154: | $out .= '</a></td>'; |
| 155: | $out .= '</tr>'; |
| 156: | $out .= '</table>'; |
| 157: | $out .= '</div>'; |
| 158: | |
| 159: | return $out; |
| 160: | } |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: | protected function setReturnValue(string $path, $val) { |
| 171: | $this->returnValues->set($path, $val); |
| 172: | } |
| 173: | |
| 174: | |
| 175: | |
| 176: | |
| 177: | |
| 178: | |
| 179: | public function getReturnValues(): array { |
| 180: | return $this->returnValues->toArray(); |
| 181: | } |
| 182: | |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | public function getReturnValue(string $path) { |
| 191: | return $this->returnValues->get($path); |
| 192: | } |
| 193: | |
| 194: | |
| 195: | |
| 196: | |
| 197: | |
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | public function attr(mixed $val = null, string $name = null): string { |
| 204: | if (($val == null) || ($val == false)) return ''; |
| 205: | if ($val === true) return $this->esc($name); |
| 206: | return $this->esc($name) . '="' . $this->esc($val) . '"'; |
| 207: | } |
| 208: | |
| 209: | |
| 210: | |
| 211: | |
| 212: | |
| 213: | |
| 214: | |
| 215: | |
| 216: | |
| 217: | |
| 218: | |
| 219: | |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | |
| 225: | public function js(mixed $data = null): string { |
| 226: | |
| 227: | |
| 228: | |
| 229: | |
| 230: | $json_flags = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_THROW_ON_ERROR; |
| 231: | $json = json_encode($data, $json_flags); |
| 232: | |
| 233: | if (is_null($data) || is_numeric($data) || is_bool($data)) { |
| 234: | |
| 235: | return $json; |
| 236: | } elseif (is_string($data)) { |
| 237: | |
| 238: | return "'" . substr($json, 1, -1) . "'"; |
| 239: | } elseif (($json == '[]') || ($json == '{}')) { |
| 240: | |
| 241: | return $json; |
| 242: | } else { |
| 243: | |
| 244: | $json = json_encode($json, $json_flags); |
| 245: | return 'JSON.parse(\'' . substr($json, 1, -1) . '\')'; |
| 246: | } |
| 247: | } |
| 248: | |
| 249: | |
| 250: | |
| 251: | |
| 252: | |
| 253: | |
| 254: | |
| 255: | public function markdown(string $md): string { |
| 256: | $parsedown = new \Parsedown(); |
| 257: | return $parsedown->text($md); |
| 258: | } |
| 259: | |
| 260: | |
| 261: | |
| 262: | |
| 263: | |
| 264: | |
| 265: | |
| 266: | |
| 267: | |
| 268: | |
| 269: | |
| 270: | public function html_to_text(string $html, int $wordwrap = 70): string { |
| 271: | return wordwrap(strip_tags(preg_replace('{<(head|style|script)\b.*?</\1>}is', '', $html)), $wordwrap); |
| 272: | } |
| 273: | } |
| 274: | |
| 275: | ?> |