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_merge((array)$this->_helperMap[$name]['settings'], array('enabled' => false));
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 (
324: !empty($options['ext']) &&
325: strpos($path, '?') === false &&
326: substr($path, -strlen($options['ext'])) !== $options['ext']
327: ) {
328: $path .= $options['ext'];
329: }
330: if (preg_match('|^([a-z0-9]+:)?//|', $path)) {
331: return $path;
332: }
333: if (isset($plugin)) {
334: $path = Inflector::underscore($plugin) . '/' . $path;
335: }
336: $path = $this->_encodeUrl($this->assetTimestamp($this->webroot($path)));
337:
338: if (!empty($options['fullBase'])) {
339: $path = rtrim(Router::fullBaseUrl(), '/') . '/' . ltrim($path, '/');
340: }
341: return $path;
342: }
343:
344: 345: 346: 347: 348: 349:
350: protected function _encodeUrl($url) {
351: $path = parse_url($url, PHP_URL_PATH);
352: $parts = array_map('rawurldecode', explode('/', $path));
353: $parts = array_map('rawurlencode', $parts);
354: $encoded = implode('/', $parts);
355: return h(str_replace($path, $encoded, $url));
356: }
357:
358: 359: 360: 361: 362: 363: 364: 365:
366: public function assetTimestamp($path) {
367: $stamp = Configure::read('Asset.timestamp');
368: $timestampEnabled = $stamp === 'force' || ($stamp === true && Configure::read('debug') > 0);
369: if ($timestampEnabled && strpos($path, '?') === false) {
370: $filepath = preg_replace(
371: '/^' . preg_quote($this->request->webroot, '/') . '/',
372: '',
373: urldecode($path)
374: );
375: $webrootPath = WWW_ROOT . str_replace('/', DS, $filepath);
376: if (file_exists($webrootPath)) {
377:
378: return $path . '?' . @filemtime($webrootPath);
379:
380: }
381: $segments = explode('/', ltrim($filepath, '/'));
382: if ($segments[0] === 'theme') {
383: $theme = $segments[1];
384: unset($segments[0], $segments[1]);
385: $themePath = App::themePath($theme) . 'webroot' . DS . implode(DS, $segments);
386:
387: return $path . '?' . @filemtime($themePath);
388:
389: } else {
390: $plugin = Inflector::camelize($segments[0]);
391: if (CakePlugin::loaded($plugin)) {
392: unset($segments[0]);
393: $pluginPath = CakePlugin::path($plugin) . 'webroot' . DS . implode(DS, $segments);
394:
395: return $path . '?' . @filemtime($pluginPath);
396:
397: }
398: }
399: }
400: return $path;
401: }
402:
403: 404: 405: 406: 407: 408: 409: 410: 411:
412: public function clean($output) {
413: $this->_reset();
414: if (empty($output)) {
415: return null;
416: }
417: if (is_array($output)) {
418: foreach ($output as $key => $value) {
419: $return[$key] = $this->clean($value);
420: }
421: return $return;
422: }
423: $this->_tainted = $output;
424: $this->_clean();
425: return $this->_cleaned;
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:
454: protected function _parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
455: if (!is_string($options)) {
456: $options = (array)$options + array('escape' => true);
457:
458: if (!is_array($exclude)) {
459: $exclude = array();
460: }
461:
462: $exclude = array('escape' => true) + array_flip($exclude);
463: $escape = $options['escape'];
464: $attributes = array();
465:
466: foreach ($options as $key => $value) {
467: if (!isset($exclude[$key]) && $value !== false && $value !== null) {
468: $attributes[] = $this->_formatAttribute($key, $value, $escape);
469: }
470: }
471: $out = implode(' ', $attributes);
472: } else {
473: $out = $options;
474: }
475: return $out ? $insertBefore . $out . $insertAfter : '';
476: }
477:
478: 479: 480: 481: 482: 483: 484: 485: 486: 487:
488: protected function _formatAttribute($key, $value, $escape = true) {
489: if (is_array($value)) {
490: $value = implode(' ', $value);
491: }
492: if (is_numeric($key)) {
493: return sprintf($this->_minimizedAttributeFormat, $value, $value);
494: }
495: $truthy = array(1, '1', true, 'true', $key);
496: $isMinimized = in_array($key, $this->_minimizedAttributes);
497: if ($isMinimized && in_array($value, $truthy, true)) {
498: return sprintf($this->_minimizedAttributeFormat, $key, $key);
499: }
500: if ($isMinimized) {
501: return '';
502: }
503: return sprintf($this->_attributeFormat, $key, ($escape ? h($value) : $value));
504: }
505:
506: 507: 508: 509: 510: 511: 512: 513: 514:
515: protected function _confirm($message, $okCode, $cancelCode = '', $options = array()) {
516: $message = json_encode($message);
517: $confirm = "if (confirm({$message})) { {$okCode} } {$cancelCode}";
518: if (isset($options['escape']) && $options['escape'] === false) {
519: $confirm = h($confirm);
520: }
521: return $confirm;
522: }
523:
524: 525: 526: 527: 528: 529: 530:
531: public function setEntity($entity, $setScope = false) {
532: if ($entity === null) {
533: $this->_modelScope = false;
534: }
535: if ($setScope === true) {
536: $this->_modelScope = $entity;
537: }
538: $parts = array_values(Hash::filter(explode('.', $entity)));
539: if (empty($parts)) {
540: return;
541: }
542: $count = count($parts);
543: $lastPart = isset($parts[$count - 1]) ? $parts[$count - 1] : null;
544:
545:
546: if (
547: ($count === 1 && $this->_modelScope && !$setScope) ||
548: (
549: $count === 2 &&
550: in_array($lastPart, $this->_fieldSuffixes) &&
551: $this->_modelScope &&
552: $parts[0] !== $this->_modelScope
553: )
554: ) {
555: $entity = $this->_modelScope . '.' . $entity;
556: }
557:
558:
559: if (
560: $count >= 2 &&
561: is_numeric($parts[0]) &&
562: !is_numeric($parts[1]) &&
563: $this->_modelScope &&
564: strpos($entity, $this->_modelScope) === false
565: ) {
566: $entity = $this->_modelScope . '.' . $entity;
567: }
568:
569: $this->_association = null;
570:
571: $isHabtm = (
572: isset($this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type']) &&
573: $this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type'] === 'multiple'
574: );
575:
576:
577: if ($count === 1 && $isHabtm) {
578: $this->_association = $parts[0];
579: $entity = $parts[0] . '.' . $parts[0];
580: } else {
581:
582: $reversed = array_reverse($parts);
583: foreach ($reversed as $i => $part) {
584: if ($i > 0 && preg_match('/^[A-Z]/', $part)) {
585: $this->_association = $part;
586: break;
587: }
588: }
589: }
590: $this->_entityPath = $entity;
591: }
592:
593: 594: 595: 596: 597:
598: public function entity() {
599: return explode('.', $this->_entityPath);
600: }
601:
602: 603: 604: 605: 606:
607: public function model() {
608: if ($this->_association) {
609: return $this->_association;
610: }
611: return $this->_modelScope;
612: }
613:
614: 615: 616: 617: 618: 619: 620:
621: public function field() {
622: $entity = $this->entity();
623: $count = count($entity);
624: $last = $entity[$count - 1];
625: if ($count > 2 && in_array($last, $this->_fieldSuffixes)) {
626: $last = isset($entity[$count - 2]) ? $entity[$count - 2] : null;
627: }
628: return $last;
629: }
630:
631: 632: 633: 634: 635: 636: 637: 638: 639: 640:
641: public function domId($options = null, $id = 'id') {
642: if (is_array($options) && array_key_exists($id, $options) && $options[$id] === null) {
643: unset($options[$id]);
644: return $options;
645: } elseif (!is_array($options) && $options !== null) {
646: $this->setEntity($options);
647: return $this->domId();
648: }
649:
650: $entity = $this->entity();
651: $model = array_shift($entity);
652: $dom = $model . implode('', array_map(array('Inflector', 'camelize'), $entity));
653:
654: if (is_array($options) && !array_key_exists($id, $options)) {
655: $options[$id] = $dom;
656: } elseif ($options === null) {
657: return $dom;
658: }
659: return $options;
660: }
661:
662: 663: 664: 665: 666: 667: 668: 669: 670: 671: 672:
673: protected function _name($options = array(), $field = null, $key = 'name') {
674: if ($options === null) {
675: $options = array();
676: } elseif (is_string($options)) {
677: $field = $options;
678: $options = 0;
679: }
680:
681: if (!empty($field)) {
682: $this->setEntity($field);
683: }
684:
685: if (is_array($options) && array_key_exists($key, $options)) {
686: return $options;
687: }
688:
689: switch ($field) {
690: case '_method':
691: $name = $field;
692: break;
693: default:
694: $name = 'data[' . implode('][', $this->entity()) . ']';
695: }
696:
697: if (is_array($options)) {
698: $options[$key] = $name;
699: return $options;
700: }
701: return $name;
702: }
703:
704: 705: 706: 707: 708: 709: 710: 711: 712: 713:
714: public function value($options = array(), $field = null, $key = 'value') {
715: if ($options === null) {
716: $options = array();
717: } elseif (is_string($options)) {
718: $field = $options;
719: $options = 0;
720: }
721:
722: if (is_array($options) && isset($options[$key])) {
723: return $options;
724: }
725:
726: if (!empty($field)) {
727: $this->setEntity($field);
728: }
729: $result = null;
730: $data = $this->request->data;
731:
732: $entity = $this->entity();
733: if (!empty($data) && is_array($data) && !empty($entity)) {
734: $result = Hash::get($data, implode('.', $entity));
735: }
736:
737: $habtmKey = $this->field();
738: if (empty($result) && isset($data[$habtmKey][$habtmKey]) && is_array($data[$habtmKey])) {
739: $result = $data[$habtmKey][$habtmKey];
740: } elseif (empty($result) && isset($data[$habtmKey]) && is_array($data[$habtmKey])) {
741: if (ClassRegistry::isKeySet($habtmKey)) {
742: $model = ClassRegistry::getObject($habtmKey);
743: $result = $this->_selectedArray($data[$habtmKey], $model->primaryKey);
744: }
745: }
746:
747: if (is_array($options)) {
748: if ($result === null && isset($options['default'])) {
749: $result = $options['default'];
750: }
751: unset($options['default']);
752: }
753:
754: if (is_array($options)) {
755: $options[$key] = $result;
756: return $options;
757: }
758: return $result;
759: }
760:
761: 762: 763: 764: 765: 766: 767: 768:
769: protected function _initInputField($field, $options = array()) {
770: if ($field !== null) {
771: $this->setEntity($field);
772: }
773: $options = (array)$options;
774: $options = $this->_name($options);
775: $options = $this->value($options);
776: $options = $this->domId($options);
777: return $options;
778: }
779:
780: 781: 782: 783: 784: 785: 786: 787:
788: public function addClass($options = array(), $class = null, $key = 'class') {
789: if (isset($options[$key]) && trim($options[$key])) {
790: $options[$key] .= ' ' . $class;
791: } else {
792: $options[$key] = $class;
793: }
794: return $options;
795: }
796:
797: 798: 799: 800: 801: 802: 803: 804: 805:
806: public function output($str) {
807: return $str;
808: }
809:
810: 811: 812: 813: 814: 815: 816: 817:
818: public function beforeRender($viewFile) {
819: }
820:
821: 822: 823: 824: 825: 826: 827: 828: 829:
830: public function afterRender($viewFile) {
831: }
832:
833: 834: 835: 836: 837: 838: 839: 840:
841: public function beforeLayout($layoutFile) {
842: }
843:
844: 845: 846: 847: 848: 849: 850: 851:
852: public function afterLayout($layoutFile) {
853: }
854:
855: 856: 857: 858: 859: 860: 861: 862: 863:
864: public function beforeRenderFile($viewFile) {
865: }
866:
867: 868: 869: 870: 871: 872: 873: 874: 875: 876:
877: public function afterRenderFile($viewFile, $content) {
878: }
879:
880: 881: 882: 883: 884: 885: 886: 887:
888: protected function _selectedArray($data, $key = 'id') {
889: if (!is_array($data)) {
890: $model = $data;
891: if (!empty($this->request->data[$model][$model])) {
892: return $this->request->data[$model][$model];
893: }
894: if (!empty($this->request->data[$model])) {
895: $data = $this->request->data[$model];
896: }
897: }
898: $array = array();
899: if (!empty($data)) {
900: foreach ($data as $row) {
901: if (isset($row[$key])) {
902: $array[$row[$key]] = $row[$key];
903: }
904: }
905: }
906: return empty($array) ? null : $array;
907: }
908:
909: 910: 911: 912: 913:
914: protected function _reset() {
915: $this->_tainted = null;
916: $this->_cleaned = null;
917: }
918:
919: 920: 921: 922: 923:
924: protected function _clean() {
925: if (get_magic_quotes_gpc()) {
926: $this->_cleaned = stripslashes($this->_tainted);
927: } else {
928: $this->_cleaned = $this->_tainted;
929: }
930:
931: $this->_cleaned = str_replace(array("&", "<", ">"), array("&amp;", "&lt;", "&gt;"), $this->_cleaned);
932: $this->_cleaned = preg_replace('#(&\#*\w+)[\x00-\x20]+;#u', "$1;", $this->_cleaned);
933: $this->_cleaned = preg_replace('#(&\#x*)([0-9A-F]+);*#iu', "$1$2;", $this->_cleaned);
934: $this->_cleaned = html_entity_decode($this->_cleaned, ENT_COMPAT, "UTF-8");
935: $this->_cleaned = preg_replace('#(<[^>]+[\x00-\x20\"\'\/])(on|xmlns)[^>]*>#iUu', "$1>", $this->_cleaned);
936: $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);
937: $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);
938: $this->_cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=*([\'\"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#iUu', '$1=$2nomozbinding...', $this->_cleaned);
939: $this->_cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=([\'\"]*)[\x00-\x20]*data[\x00-\x20]*:#Uu', '$1=$2nodata...', $this->_cleaned);
940: $this->_cleaned = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*expression[\x00-\x20]*\([^>]*>#iU', "$1>", $this->_cleaned);
941: $this->_cleaned = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*behaviour[\x00-\x20]*\([^>]*>#iU', "$1>", $this->_cleaned);
942: $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);
943: $this->_cleaned = preg_replace('#</*\w+:\w[^>]*>#i', "", $this->_cleaned);
944: do {
945: $oldstring = $this->_cleaned;
946: $this->_cleaned = preg_replace('#</*(applet|meta|xml|blink|link|style|script|embed|object|iframe|frame|frameset|ilayer|layer|bgsound|title|base)[^>]*>#i', "", $this->_cleaned);
947: } while ($oldstring !== $this->_cleaned);
948: $this->_cleaned = str_replace(array("&", "<", ">"), array("&amp;", "&lt;", "&gt;"), $this->_cleaned);
949: }
950:
951: }
952: