1: <?php
2:
3: 4: 5:
6: class Quform_TokenReplacer
7: {
8: 9: 10: 11: 12:
13: const NEWLINE = "\r\n";
14:
15: 16: 17:
18: protected $options;
19:
20: 21: 22:
23: public function __construct(Quform_Options $options)
24: {
25: $this->options = $options;
26: }
27:
28: 29: 30: 31: 32: 33: 34: 35:
36: public function replaceVariablesPreProcess($text, $format, Quform_Form $form)
37: {
38: if ( ! Quform::isNonEmptyString($text)) {
39: return '';
40: }
41:
42: if (strpos($text, '{') === false) {
43: return $text;
44: }
45:
46: return preg_replace_callback('/({(.+?)})/', function ($matches) use ($format, $form) {
47: return $this->replaceVariablePreProcess($matches, $format, $form);
48: }, $text);
49: }
50:
51: 52: 53: 54: 55: 56: 57: 58:
59: protected function replaceVariablePreProcess($matches, $format, Quform_Form $form)
60: {
61: $replaced = $matches[1];
62: $token = $this->parseToken($matches[2]);
63:
64: switch ($token['name']) {
65: case 'site_title':
66: $replaced = get_bloginfo('name');
67: break;
68: case 'site_tagline':
69: $replaced = get_bloginfo('description');
70: break;
71: case 'ip':
72: $replaced = Quform::getClientIp();
73: break;
74: case 'post':
75: $replaced = Quform::getPostProperty(count($token['params']) ? key($token['params']) : 'ID');
76: break;
77: case 'custom_field':
78: $replaced = Quform::getPostMeta(count($token['params']) ? key($token['params']) : '');
79: break;
80: case 'url':
81: $replaced = Quform::getCurrentUrl();
82: break;
83: case 'user':
84: $replaced = Quform::getUserProperty(count($token['params']) ? key($token['params']) : 'display_name');
85: break;
86: case 'user_meta':
87: $replaced = Quform::getUserMeta(count($token['params']) ? key($token['params']) : '');
88: break;
89: case 'referring_url':
90: $replaced = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
91: break;
92: case 'user_agent':
93: $replaced = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
94: break;
95: case 'date':
96: if (isset($token['params']['format'])) {
97: $format = $token['params']['format'];
98: } else {
99: $format = $this->getDateFormat($form);
100: }
101:
102: $replaced = Quform::date($format);
103: break;
104: case 'time':
105: if (isset($token['params']['format'])) {
106: $format = $token['params']['format'];
107: } else {
108: $format = $this->getTimeFormat($form);
109: }
110:
111: $replaced = Quform::date($format);
112: break;
113: case 'datetime':
114: if (isset($token['params']['format'])) {
115: $format = $token['params']['format'];
116: } else {
117: $format = $this->getDateTimeFormat($form);
118: }
119:
120: $replaced = Quform::date($format);
121: break;
122: case 'uniqid':
123: $prefix = isset($token['params']['prefix']) ? $token['params']['prefix'] : '';
124: $moreEntropy = isset($token['params']['moreEntropy']) && $token['params']['moreEntropy'] === 'true';
125:
126: $replaced = uniqid($prefix, $moreEntropy);
127: break;
128: }
129:
130: $replaced = apply_filters('quform_replace_variables_pre_process', $replaced, $token, $format);
131:
132: if ($format == 'url') {
133: $replaced = urlencode($replaced);
134: } elseif ($format == 'rawurl') {
135: $replaced = rawurlencode($replaced);
136: }
137:
138: return $replaced;
139: }
140:
141: 142: 143: 144: 145: 146:
147: protected function getDateFormat(Quform_Form $form)
148: {
149: $format = $form->getDateFormat();
150:
151: if ( ! Quform::isNonEmptyString($format)) {
152: $locale = Quform::getLocale($form->getLocale());
153: $format = $locale['dateFormat'];
154: }
155:
156: return $format;
157: }
158:
159: 160: 161: 162: 163: 164:
165: protected function getTimeFormat(Quform_Form $form)
166: {
167: $format = $form->getTimeFormat();
168:
169: if ( ! Quform::isNonEmptyString($format)) {
170: $locale = Quform::getLocale($form->getLocale());
171: $format = $locale['timeFormat'];
172: }
173:
174: return $format;
175: }
176:
177: 178: 179: 180: 181: 182:
183: protected function getDateTimeFormat(Quform_Form $form)
184: {
185: $format = $form->getDateTimeFormat();
186:
187: if ( ! Quform::isNonEmptyString($format)) {
188: $locale = Quform::getLocale($form->getLocale());
189: $format = $locale['dateTimeFormat'];
190: }
191:
192: return $format;
193: }
194:
195: 196: 197: 198: 199: 200: 201: 202:
203: public function replaceVariables($text, $format, Quform_Form $form)
204: {
205: if ( ! Quform::isNonEmptyString($text)) {
206: return '';
207: }
208:
209: if (strpos($text, '{') === false) {
210: return $text;
211: }
212:
213: return preg_replace_callback('/({(.+?)})/', function ($matches) use ($format, $form) {
214: return $this->replaceVariable($matches, $format, $form);
215: }, $text);
216: }
217:
218: 219: 220: 221: 222: 223: 224: 225:
226: protected function replaceVariable($matches, $format, Quform_Form $form)
227: {
228: $replaced = $original = $matches[1];
229: $token = $this->parseToken($matches[2]);
230:
231: switch ($token['name']) {
232: case 'post':
233: $replaced = Quform::getPostProperty(count($token['params']) ? key($token['params']) : 'ID', Quform::get($_POST, 'post_id'));
234: break;
235: case 'custom_field':
236: $replaced = Quform::getPostMeta(count($token['params']) ? key($token['params']) : '', Quform::get($_POST, 'post_id'));
237: break;
238: case 'referring_url':
239: $replaced = Quform::get($_POST, 'referring_url');
240: break;
241: case 'default_email_address':
242: $replaced = $this->options->get('defaultEmailAddress');
243: break;
244: case 'default_email_name':
245: $replaced = $this->options->get('defaultEmailName');
246: break;
247: case 'default_from_email_address':
248: $replaced = $this->options->get('defaultFromEmailAddress');
249: break;
250: case 'default_from_email_name':
251: $replaced = $this->options->get('defaultFromEmailName');
252: break;
253: case 'admin_email':
254: $replaced = get_bloginfo('admin_email');
255: break;
256: case 'element':
257: $replaced = $this->replaceElement($token, $format, $form);
258: break;
259: case 'form_name':
260: $replaced = $form->config('name');
261: break;
262: case 'entry_id':
263: $replaced = $form->getEntryId();
264: break;
265: case 'all_form_data':
266: $replaced = $this->replaceAllSubmittedData($token, $format, $form);
267: break;
268: }
269:
270: if ($replaced === $original) {
271: $replaced = $this->replaceVariablesPreProcess($original, $format, $form);
272: } else {
273: $replaced = apply_filters('quform_replace_variables', $replaced, $token, $format, $form);
274:
275: if ($format == 'url') {
276: $replaced = urlencode($replaced);
277: } elseif ($format == 'rawurl') {
278: $replaced = rawurlencode($replaced);
279: }
280: }
281:
282: return $replaced;
283: }
284:
285: 286: 287: 288: 289: 290: 291: 292:
293: protected function replaceElement($token, $format, Quform_Form $form)
294: {
295: $value = '';
296:
297: if (isset($token['params']['id'])) {
298: $id = $token['params']['id'];
299: $part = null;
300:
301: if (strpos($id, '.') !== false) {
302: list($id, $part) = explode('.', $id, 2);
303: }
304:
305: $element = $form->getElementById($id);
306:
307: if ($element instanceof Quform_Element_Field && ! $element->isConditionallyHidden()) {
308: $format = isset($token['params']['format']) ? $token['params']['format'] : $format;
309: $separator = isset($token['params']['separator']) ? $token['params']['separator'] : ', ';
310:
311: if (Quform::isNonEmptyString($part)) {
312:
313: $elementValue = $element->getValue();
314:
315: if (is_array($elementValue)) {
316: $partValue = Quform::get($elementValue, $part, '');
317:
318: if (is_scalar($partValue)) {
319: $value = $format == 'html' ? Quform::escape($partValue) : $partValue;
320: }
321: }
322: } else {
323: $value = $format == 'html' ? $element->getValueHtml() : $element->getValueText($separator);
324: }
325:
326: $value = apply_filters('quform_element_token_value_' . $element->getIdentifier(), $value, $element, $format, $separator, $token);
327: }
328: }
329:
330: return $value;
331: }
332:
333: 334: 335: 336: 337: 338: 339: 340:
341: protected function replaceAllSubmittedData($token, $format, Quform_Form $form)
342: {
343: $showEmptyFields = isset($token['params']['showEmptyFields']) && $token['params']['showEmptyFields'] === 'true';
344:
345: if ($format == 'html') {
346: $content = $this->renderAllSubmittedDataHtml($showEmptyFields, $form);
347: } else {
348: $content = $this->renderAllSubmittedDataText($showEmptyFields, $form);
349: }
350:
351: return $content;
352: }
353:
354: 355: 356: 357: 358: 359: 360:
361: protected function renderAllSubmittedDataText($showEmptyFields, Quform_Form $form)
362: {
363: $content = '';
364:
365: foreach ($form->getRecursiveIterator(RecursiveIteratorIterator::SELF_FIRST) as $element) {
366: if ( ! $element instanceof Quform_Element_Field && ! $element instanceof Quform_Element_Container && ! $element instanceof Quform_Element_Html) {
367: continue;
368: }
369:
370:
371: if ($element->isHidden()) {
372: continue;
373: }
374:
375:
376: if ($element->isEmpty() && ! $showEmptyFields) {
377: continue;
378: }
379:
380: if ($element instanceof Quform_Element_Html) {
381: if ($element->config('showInEmail')) {
382: $content .= $element->getContent('text');
383: $content .= self::NEWLINE . self::NEWLINE;
384: }
385: } else if ($element instanceof Quform_Element_Group) {
386: if ($element->config('showLabelInEmail') && Quform::isNonEmptyString($label = $element->getLabel())) {
387: $content .= str_repeat('=', 25) . self::NEWLINE;
388: $content .= $label . self::NEWLINE;
389: $content .= str_repeat('=', 25);
390: $content .= self::NEWLINE . self::NEWLINE;
391: }
392: } else if ($element instanceof Quform_Element_Field) {
393: if ($element->config('showInEmail')) {
394: $content .= $element->getAdminLabel() . self::NEWLINE;
395: $content .= str_repeat('-', 25) . self::NEWLINE;
396: $content .= $element->getValueText(self::NEWLINE);
397: $content .= self::NEWLINE . self::NEWLINE;
398: }
399: }
400: }
401:
402: $content = apply_filters('quform_all_form_data_text', $content, $form, $showEmptyFields);
403:
404: return $content;
405: }
406:
407: 408: 409: 410: 411: 412: 413:
414: protected function renderAllSubmittedDataHtml($showEmptyFields, Quform_Form $form)
415: {
416: $content = '<table width="100%" cellpadding="10" cellspacing="0" border="0" style="table-layout: fixed; background: #ffffff; border-spacing: 0; border-collapse: separate; border-bottom: 1px solid #d4d4d4; box-shadow: 0 2px 7px 0 rgba(0, 0, 0, 0.07);">' . self::NEWLINE;
417:
418: foreach ($form->getRecursiveIterator(RecursiveIteratorIterator::SELF_FIRST) as $element) {
419: if ( ! $element instanceof Quform_Element_Field && ! $element instanceof Quform_Element_Container && ! $element instanceof Quform_Element_Html) {
420: continue;
421: }
422:
423:
424: if ($element->isHidden()) {
425: continue;
426: }
427:
428:
429: if ($element->isEmpty() && ! $showEmptyFields) {
430: continue;
431: }
432:
433: if ($element instanceof Quform_Element_Html) {
434: if ($element->config('showInEmail')) {
435: $content .= '<tr><td valign="top" style="padding: 10px; font-family: Helvetica, Arial, sans-serif; font-size: 16px; color: #282828; line-height: 130%; border: 1px solid #d4d4d4; border-bottom: 0; word-wrap: break-word;">' . $element->getContent() . '</td></tr>' . self::NEWLINE;
436: }
437: } else if ($element instanceof Quform_Element_Page) {
438: if ($element->config('showLabelInEmail') && Quform::isNonEmptyString($label = $element->getLabel())) {
439: $content .= '<tr><td valign="top" style="padding: 10px; font-family: Helvetica, Arial, sans-serif; font-size: 22px; font-weight: bold; background-color: #c73412; color: #ffffff; border-bottom: 1px solid #e14e2c; word-wrap: break-word;">' . esc_html($label) . '</td></tr>' . self::NEWLINE;
440: }
441: } else if ($element instanceof Quform_Element_Group) {
442: if ($element->config('showLabelInEmail') && Quform::isNonEmptyString($label = $element->getLabel())) {
443: $content .= '<tr><td valign="top" style="padding: 10px; font-family: Helvetica, Arial, sans-serif; font-size: 17px; background-color: #c73412; color: #ffffff; word-wrap: break-word;">' . esc_html($label) . '</td></tr>' . self::NEWLINE;
444: }
445: } else if ($element instanceof Quform_Element_Field) {
446: if ($element->config('showInEmail')) {
447: $content .= '<tr bgcolor="#efefef"><td valign="top" style="padding: 10px; font-family: Helvetica, Arial, sans-serif; font-size: 15px; font-weight: bold; color: #282828; border: 1px solid #d4d4d4; border-bottom: 0; word-wrap: break-word;">' . esc_html($element->getAdminLabel()) . '</td></tr>' . self::NEWLINE;
448: $content .= '<tr bgcolor="#fcfcfc"><td valign="top" style="padding: 10px; font-family: Helvetica, Arial, sans-serif; font-size: 14px; color: #282828; line-height: 130%; border: 1px solid #d4d4d4; border-bottom-color: #fff; word-wrap: break-word;">' . $element->getValueHtml() . '</td></tr>' . self::NEWLINE;
449: }
450: }
451: }
452:
453: $content .= '</table>' . self::NEWLINE;
454:
455: $content = apply_filters('quform_all_form_data_html', $content, $form, $showEmptyFields);
456:
457: return $content;
458: }
459:
460: 461: 462: 463: 464: 465:
466: protected function parseToken($token)
467: {
468: $parts = explode('|', $token);
469:
470:
471: $name = trim(array_shift($parts));
472:
473:
474: $params = array();
475: foreach ($parts as $part) {
476: $paramParts = explode(':', $part, 2);
477: $params[$paramParts[0]] = isset($paramParts[1]) ? $paramParts[1] : true;
478: }
479:
480: return array(
481: 'name' => $name,
482: 'params' => $params
483: );
484: }
485: }
486: