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