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