1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: App::uses('Router', 'Routing');
18: App::uses('Hash', 'Utility');
19:
20: 21: 22: 23: 24: 25:
26: class Helper extends Object {
27:
28: 29: 30: 31: 32:
33: public $settings = array();
34:
35: 36: 37: 38: 39:
40: public $helpers = array();
41:
42: 43: 44: 45: 46:
47: protected $_helperMap = array();
48:
49: 50: 51: 52: 53:
54: public $theme = null;
55:
56: 57: 58: 59: 60:
61: public $request = null;
62:
63: 64: 65: 66: 67:
68: public $plugin = null;
69:
70: 71: 72: 73: 74: 75:
76: public $fieldset = array();
77:
78: 79: 80: 81: 82:
83: public $tags = array();
84:
85: 86: 87: 88: 89:
90: protected $_tainted = null;
91:
92: 93: 94: 95: 96:
97: protected $_cleaned = null;
98:
99: 100: 101: 102: 103:
104: protected $_View;
105:
106: 107: 108: 109: 110: 111: 112:
113: protected $_fieldSuffixes = array(
114: 'year', 'month', 'day', 'hour', 'min', 'second', 'meridian'
115: );
116:
117: 118: 119: 120: 121: 122:
123: protected $_modelScope;
124:
125: 126: 127: 128: 129: 130:
131: protected $_association;
132:
133: 134: 135: 136: 137: 138:
139: protected $_entityPath;
140:
141: 142: 143: 144: 145:
146: protected $_minimizedAttributes = array(
147: 'compact', 'checked', 'declare', 'readonly', 'disabled', 'selected',
148: 'defer', 'ismap', 'nohref', 'noshade', 'nowrap', 'multiple', 'noresize',
149: 'autoplay', 'controls', 'loop', 'muted', 'required', 'novalidate', 'formnovalidate'
150: );
151:
152: 153: 154: 155: 156:
157: protected $_attributeFormat = '%s="%s"';
158:
159: 160: 161: 162: 163:
164: protected $_minimizedAttributeFormat = '%s="%s"';
165:
166: 167: 168: 169: 170: 171:
172: public function __construct(View $View, $settings = array()) {
173: $this->_View = $View;
174: $this->request = $View->request;
175: if ($settings) {
176: $this->settings = Hash::merge($this->settings, $settings);
177: }
178: if (!empty($this->helpers)) {
179: $this->_helperMap = ObjectCollection::normalizeObjectArray($this->helpers);
180: }
181: }
182:
183: 184: 185: 186: 187: 188: 189:
190: public function __call($method, $params) {
191: trigger_error(__d('cake_dev', 'Method %1$s::%2$s does not exist', get_class($this), $method), E_USER_WARNING);
192: }
193:
194: 195: 196: 197: 198: 199:
200: public function __get($name) {
201: if (isset($this->_helperMap[$name]) && !isset($this->{$name})) {
202: $settings = array_merge((array)$this->_helperMap[$name]['settings'], array('enabled' => false));
203: $this->{$name} = $this->_View->loadHelper($this->_helperMap[$name]['class'], $settings);
204: }
205: if (isset($this->{$name})) {
206: return $this->{$name};
207: }
208: switch ($name) {
209: case 'base':
210: case 'here':
211: case 'webroot':
212: case 'data':
213: return $this->request->{$name};
214: case 'action':
215: return isset($this->request->params['action']) ? $this->request->params['action'] : '';
216: case 'params':
217: return $this->request;
218: }
219: }
220:
221: 222: 223: 224: 225: 226: 227:
228: public function __set($name, $value) {
229: switch ($name) {
230: case 'base':
231: case 'here':
232: case 'webroot':
233: case 'data':
234: $this->request->{$name} = $value;
235: return;
236: case 'action':
237: $this->request->params['action'] = $value;
238: return;
239: }
240: $this->{$name} = $value;
241: }
242:
243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254:
255: public function url($url = null, $full = false) {
256: return h(Router::url($url, $full));
257: }
258:
259: 260: 261: 262: 263: 264:
265: public function webroot($file) {
266: $asset = explode('?', $file);
267: $asset[1] = isset($asset[1]) ? '?' . $asset[1] : null;
268: $webPath = "{$this->request->webroot}" . $asset[0];
269: $file = $asset[0];
270:
271: if (!empty($this->theme)) {
272: $file = trim($file, '/');
273: $theme = $this->theme . '/';
274:
275: if (DS === '\\') {
276: $file = str_replace('/', '\\', $file);
277: }
278:
279: if (file_exists(Configure::read('App.www_root') . 'theme' . DS . $this->theme . DS . $file)) {
280: $webPath = "{$this->request->webroot}theme/" . $theme . $asset[0];
281: } else {
282: $themePath = App::themePath($this->theme);
283: $path = $themePath . 'webroot' . DS . $file;
284: if (file_exists($path)) {
285: $webPath = "{$this->request->webroot}theme/" . $theme . $asset[0];
286: }
287: }
288: }
289: if (strpos($webPath, '//') !== false) {
290: return str_replace('//', '/', $webPath . $asset[1]);
291: }
292: return $webPath . $asset[1];
293: }
294:
295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306:
307: public function assetUrl($path, $options = array()) {
308: if (is_array($path)) {
309: return $this->url($path, !empty($options['fullBase']));
310: }
311: if (strpos($path, '://') !== false) {
312: return $path;
313: }
314: if (!array_key_exists('plugin', $options) || $options['plugin'] !== false) {
315: list($plugin, $path) = $this->_View->pluginSplit($path, false);
316: }
317: if (!empty($options['pathPrefix']) && $path[0] !== '/') {
318: $path = $options['pathPrefix'] . $path;
319: }
320: if (
321: !empty($options['ext']) &&
322: strpos($path, '?') === false &&
323: substr($path, -strlen($options['ext'])) !== $options['ext']
324: ) {
325: $path .= $options['ext'];
326: }
327: if (isset($plugin)) {
328: $path = Inflector::underscore($plugin) . '/' . $path;
329: }
330: $path = $this->_encodeUrl($this->assetTimestamp($this->webroot($path)));
331:
332: if (!empty($options['fullBase'])) {
333: $path = rtrim(FULL_BASE_URL, '/') . '/' . ltrim($path, '/');
334: }
335: return $path;
336: }
337:
338: 339: 340: 341: 342: 343:
344: protected function _encodeUrl($url) {
345: $path = parse_url($url, PHP_URL_PATH);
346: $parts = array_map('urldecode', explode('/', $path));
347: $parts = array_map('rawurlencode', $parts);
348: $encoded = implode('/', $parts);
349: return h(str_replace($path, $encoded, $url));
350: }
351:
352: 353: 354: 355: 356: 357: 358: 359:
360: public function assetTimestamp($path) {
361: $stamp = Configure::read('Asset.timestamp');
362: $timestampEnabled = $stamp === 'force' || ($stamp === true && Configure::read('debug') > 0);
363: if ($timestampEnabled && strpos($path, '?') === false) {
364: $filepath = preg_replace(
365: '/^' . preg_quote($this->request->webroot, '/') . '/',
366: '',
367: urldecode($path)
368: );
369: $webrootPath = WWW_ROOT . str_replace('/', DS, $filepath);
370: if (file_exists($webrootPath)) {
371:
372: return $path . '?' . @filemtime($webrootPath);
373:
374: }
375: $segments = explode('/', ltrim($filepath, '/'));
376: if ($segments[0] === 'theme') {
377: $theme = $segments[1];
378: unset($segments[0], $segments[1]);
379: $themePath = App::themePath($theme) . 'webroot' . DS . implode(DS, $segments);
380:
381: return $path . '?' . @filemtime($themePath);
382:
383: } else {
384: $plugin = Inflector::camelize($segments[0]);
385: if (CakePlugin::loaded($plugin)) {
386: unset($segments[0]);
387: $pluginPath = CakePlugin::path($plugin) . 'webroot' . DS . implode(DS, $segments);
388:
389: return $path . '?' . @filemtime($pluginPath);
390:
391: }
392: }
393: }
394: return $path;
395: }
396:
397: 398: 399: 400: 401: 402: 403: 404:
405: public function clean($output) {
406: $this->_reset();
407: if (empty($output)) {
408: return null;
409: }
410: if (is_array($output)) {
411: foreach ($output as $key => $value) {
412: $return[$key] = $this->clean($value);
413: }
414: return $return;
415: }
416: $this->_tainted = $output;
417: $this->_clean();
418: return $this->_cleaned;
419: }
420:
421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446:
447: protected function _parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
448: if (!is_string($options)) {
449: $options = (array)$options + array('escape' => true);
450:
451: if (!is_array($exclude)) {
452: $exclude = array();
453: }
454:
455: $exclude = array('escape' => true) + array_flip($exclude);
456: $escape = $options['escape'];
457: $attributes = array();
458:
459: foreach ($options as $key => $value) {
460: if (!isset($exclude[$key]) && $value !== false && $value !== null) {
461: $attributes[] = $this->_formatAttribute($key, $value, $escape);
462: }
463: }
464: $out = implode(' ', $attributes);
465: } else {
466: $out = $options;
467: }
468: return $out ? $insertBefore . $out . $insertAfter : '';
469: }
470:
471: 472: 473: 474: 475: 476: 477: 478: 479: 480:
481: protected function _formatAttribute($key, $value, $escape = true) {
482: if (is_array($value)) {
483: $value = implode(' ', $value);
484: }
485: if (is_numeric($key)) {
486: return sprintf($this->_minimizedAttributeFormat, $value, $value);
487: }
488: $truthy = array(1, '1', true, 'true', $key);
489: $isMinimized = in_array($key, $this->_minimizedAttributes);
490: if ($isMinimized && in_array($value, $truthy, true)) {
491: return sprintf($this->_minimizedAttributeFormat, $key, $key);
492: }
493: if ($isMinimized) {
494: return '';
495: }
496: return sprintf($this->_attributeFormat, $key, ($escape ? h($value) : $value));
497: }
498:
499: 500: 501: 502: 503: 504: 505:
506: public function setEntity($entity, $setScope = false) {
507: if ($entity === null) {
508: $this->_modelScope = false;
509: }
510: if ($setScope === true) {
511: $this->_modelScope = $entity;
512: }
513: $parts = array_values(Hash::filter(explode('.', $entity)));
514: if (empty($parts)) {
515: return;
516: }
517: $count = count($parts);
518: $lastPart = isset($parts[$count - 1]) ? $parts[$count - 1] : null;
519:
520:
521: if (
522: ($count === 1 && $this->_modelScope && !$setScope) ||
523: (
524: $count === 2 &&
525: in_array($lastPart, $this->_fieldSuffixes) &&
526: $this->_modelScope &&
527: $parts[0] !== $this->_modelScope
528: )
529: ) {
530: $entity = $this->_modelScope . '.' . $entity;
531: }
532:
533:
534: if (
535: $count >= 2 &&
536: is_numeric($parts[0]) &&
537: !is_numeric($parts[1]) &&
538: $this->_modelScope &&
539: strpos($entity, $this->_modelScope) === false
540: ) {
541: $entity = $this->_modelScope . '.' . $entity;
542: }
543:
544: $this->_association = null;
545:
546: $isHabtm = (
547: isset($this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type']) &&
548: $this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type'] === 'multiple'
549: );
550:
551:
552: if ($count === 1 && $isHabtm) {
553: $this->_association = $parts[0];
554: $entity = $parts[0] . '.' . $parts[0];
555: } else {
556:
557: $reversed = array_reverse($parts);
558: foreach ($reversed as $i => $part) {
559: if ($i > 0 && preg_match('/^[A-Z]/', $part)) {
560: $this->_association = $part;
561: break;
562: }
563: }
564: }
565: $this->_entityPath = $entity;
566: }
567:
568: 569: 570: 571: 572:
573: public function entity() {
574: return explode('.', $this->_entityPath);
575: }
576:
577: 578: 579: 580: 581:
582: public function model() {
583: if ($this->_association) {
584: return $this->_association;
585: }
586: return $this->_modelScope;
587: }
588:
589: 590: 591: 592: 593: 594: 595:
596: public function field() {
597: $entity = $this->entity();
598: $count = count($entity);
599: $last = $entity[$count - 1];
600: if ($count > 2 && in_array($last, $this->_fieldSuffixes)) {
601: $last = isset($entity[$count - 2]) ? $entity[$count - 2] : null;
602: }
603: return $last;
604: }
605:
606: 607: 608: 609: 610: 611: 612: 613: 614: 615:
616: public function domId($options = null, $id = 'id') {
617: if (is_array($options) && array_key_exists($id, $options) && $options[$id] === null) {
618: unset($options[$id]);
619: return $options;
620: } elseif (!is_array($options) && $options !== null) {
621: $this->setEntity($options);
622: return $this->domId();
623: }
624:
625: $entity = $this->entity();
626: $model = array_shift($entity);
627: $dom = $model . implode('', array_map(array('Inflector', 'camelize'), $entity));
628:
629: if (is_array($options) && !array_key_exists($id, $options)) {
630: $options[$id] = $dom;
631: } elseif ($options === null) {
632: return $dom;
633: }
634: return $options;
635: }
636:
637: 638: 639: 640: 641: 642: 643: 644: 645: 646: 647:
648: protected function _name($options = array(), $field = null, $key = 'name') {
649: if ($options === null) {
650: $options = array();
651: } elseif (is_string($options)) {
652: $field = $options;
653: $options = 0;
654: }
655:
656: if (!empty($field)) {
657: $this->setEntity($field);
658: }
659:
660: if (is_array($options) && array_key_exists($key, $options)) {
661: return $options;
662: }
663:
664: switch ($field) {
665: case '_method':
666: $name = $field;
667: break;
668: default:
669: $name = 'data[' . implode('][', $this->entity()) . ']';
670: }
671:
672: if (is_array($options)) {
673: $options[$key] = $name;
674: return $options;
675: }
676: return $name;
677: }
678:
679: 680: 681: 682: 683: 684: 685: 686: 687: 688:
689: public function value($options = array(), $field = null, $key = 'value') {
690: if ($options === null) {
691: $options = array();
692: } elseif (is_string($options)) {
693: $field = $options;
694: $options = 0;
695: }
696:
697: if (is_array($options) && isset($options[$key])) {
698: return $options;
699: }
700:
701: if (!empty($field)) {
702: $this->setEntity($field);
703: }
704: $result = null;
705: $data = $this->request->data;
706:
707: $entity = $this->entity();
708: if (!empty($data) && is_array($data) && !empty($entity)) {
709: $result = Hash::get($data, implode('.', $entity));
710: }
711:
712: $habtmKey = $this->field();
713: if (empty($result) && isset($data[$habtmKey][$habtmKey]) && is_array($data[$habtmKey])) {
714: $result = $data[$habtmKey][$habtmKey];
715: } elseif (empty($result) && isset($data[$habtmKey]) && is_array($data[$habtmKey])) {
716: if (ClassRegistry::isKeySet($habtmKey)) {
717: $model = ClassRegistry::getObject($habtmKey);
718: $result = $this->_selectedArray($data[$habtmKey], $model->primaryKey);
719: }
720: }
721:
722: if (is_array($options)) {
723: if ($result === null && isset($options['default'])) {
724: $result = $options['default'];
725: }
726: unset($options['default']);
727: }
728:
729: if (is_array($options)) {
730: $options[$key] = $result;
731: return $options;
732: }
733: return $result;
734: }
735:
736: 737: 738: 739: 740: 741: 742: 743:
744: protected function _initInputField($field, $options = array()) {
745: if ($field !== null) {
746: $this->setEntity($field);
747: }
748: $options = (array)$options;
749: $options = $this->_name($options);
750: $options = $this->value($options);
751: $options = $this->domId($options);
752: return $options;
753: }
754:
755: 756: 757: 758: 759: 760: 761: 762:
763: public function addClass($options = array(), $class = null, $key = 'class') {
764: if (isset($options[$key]) && trim($options[$key])) {
765: $options[$key] .= ' ' . $class;
766: } else {
767: $options[$key] = $class;
768: }
769: return $options;
770: }
771:
772: 773: 774: 775: 776: 777: 778: 779: 780:
781: public function output($str) {
782: return $str;
783: }
784:
785: 786: 787: 788: 789: 790: 791: 792:
793: public function beforeRender($viewFile) {
794: }
795:
796: 797: 798: 799: 800: 801: 802: 803: 804:
805: public function afterRender($viewFile) {
806: }
807:
808: 809: 810: 811: 812: 813: 814: 815:
816: public function beforeLayout($layoutFile) {
817: }
818:
819: 820: 821: 822: 823: 824: 825: 826:
827: public function afterLayout($layoutFile) {
828: }
829:
830: 831: 832: 833: 834: 835: 836: 837: 838:
839: public function beforeRenderFile($viewFile) {
840: }
841:
842: 843: 844: 845: 846: 847: 848: 849: 850: 851:
852: public function afterRenderFile($viewFile, $content) {
853: }
854:
855: 856: 857: 858: 859: 860: 861: 862:
863: protected function _selectedArray($data, $key = 'id') {
864: if (!is_array($data)) {
865: $model = $data;
866: if (!empty($this->request->data[$model][$model])) {
867: return $this->request->data[$model][$model];
868: }
869: if (!empty($this->request->data[$model])) {
870: $data = $this->request->data[$model];
871: }
872: }
873: $array = array();
874: if (!empty($data)) {
875: foreach ($data as $row) {
876: if (isset($row[$key])) {
877: $array[$row[$key]] = $row[$key];
878: }
879: }
880: }
881: return empty($array) ? null : $array;
882: }
883:
884: 885: 886: 887: 888:
889: protected function _reset() {
890: $this->_tainted = null;
891: $this->_cleaned = null;
892: }
893:
894: 895: 896: 897: 898:
899: protected function _clean() {
900: if (get_magic_quotes_gpc()) {
901: $this->_cleaned = stripslashes($this->_tainted);
902: } else {
903: $this->_cleaned = $this->_tainted;
904: }
905:
906: $this->_cleaned = str_replace(array("&", "<", ">"), array("&amp;", "&lt;", "&gt;"), $this->_cleaned);
907: $this->_cleaned = preg_replace('#(&\#*\w+)[\x00-\x20]+;#u', "$1;", $this->_cleaned);
908: $this->_cleaned = preg_replace('#(&\#x*)([0-9A-F]+);*#iu', "$1$2;", $this->_cleaned);
909: $this->_cleaned = html_entity_decode($this->_cleaned, ENT_COMPAT, "UTF-8");
910: $this->_cleaned = preg_replace('#(<[^>]+[\x00-\x20\"\'\/])(on|xmlns)[^>]*>#iUu', "$1>", $this->_cleaned);
911: $this->_cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*)[\\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iUu', '$1=$2nojavascript...', $this->_cleaned);
912: $this->_cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=([\'\"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iUu', '$1=$2novbscript...', $this->_cleaned);
913: $this->_cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=*([\'\"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#iUu', '$1=$2nomozbinding...', $this->_cleaned);
914: $this->_cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=([\'\"]*)[\x00-\x20]*data[\x00-\x20]*:#Uu', '$1=$2nodata...', $this->_cleaned);
915: $this->_cleaned = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*expression[\x00-\x20]*\([^>]*>#iU', "$1>", $this->_cleaned);
916: $this->_cleaned = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*behaviour[\x00-\x20]*\([^>]*>#iU', "$1>", $this->_cleaned);
917: $this->_cleaned = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*>#iUu', "$1>", $this->_cleaned);
918: $this->_cleaned = preg_replace('#</*\w+:\w[^>]*>#i', "", $this->_cleaned);
919: do {
920: $oldstring = $this->_cleaned;
921: $this->_cleaned = preg_replace('#</*(applet|meta|xml|blink|link|style|script|embed|object|iframe|frame|frameset|ilayer|layer|bgsound|title|base)[^>]*>#i', "", $this->_cleaned);
922: } while ($oldstring !== $this->_cleaned);
923: $this->_cleaned = str_replace(array("&", "<", ">"), array("&amp;", "&lt;", "&gt;"), $this->_cleaned);
924: }
925:
926: }
927: