00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 App::import('Core', 'Overloadable');
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 class Helper extends Overloadable {
00045
00046
00047
00048
00049
00050
00051 var $helpers = null;
00052
00053
00054
00055
00056
00057 var $base = null;
00058
00059
00060
00061
00062
00063 var $webroot = null;
00064
00065
00066
00067
00068
00069 var $themeWeb = null;
00070
00071
00072
00073
00074
00075 var $here = null;
00076
00077
00078
00079
00080
00081 var $params = array();
00082
00083
00084
00085
00086
00087 var $action = null;
00088
00089
00090
00091
00092
00093 var $plugin = null;
00094
00095
00096
00097
00098
00099 var $data = null;
00100
00101
00102
00103
00104
00105 var $namedArgs = null;
00106
00107
00108
00109
00110
00111 var $argSeparator = null;
00112
00113
00114
00115
00116
00117
00118 var $validationErrors = null;
00119
00120
00121
00122
00123
00124
00125 var $tags = array();
00126
00127
00128
00129
00130
00131
00132 var $__tainted = null;
00133
00134
00135
00136
00137
00138
00139 var $__cleaned = null;
00140
00141
00142
00143
00144
00145 function get__($name) {}
00146 function set__($name, $value) {}
00147 function call__($method, $params) {
00148 trigger_error(sprintf(__('Method %1$s::%2$s does not exist', true), get_class($this), $method), E_USER_WARNING);
00149 }
00150
00151
00152
00153
00154
00155
00156
00157 function loadConfig($name = 'tags') {
00158 if (file_exists(CONFIGS . $name .'.php')) {
00159 require(CONFIGS . $name .'.php');
00160 if (isset($tags)) {
00161 $this->tags = array_merge($this->tags, $tags);
00162 }
00163 }
00164 return $this->tags;
00165 }
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 function url($url = null, $full = false) {
00183 return Router::url($url, $full);
00184 }
00185
00186
00187
00188
00189
00190
00191 function webroot($file) {
00192 $webPath = "{$this->webroot}" . $file;
00193 if (!empty($this->themeWeb)) {
00194 $os = env('OS');
00195 if (!empty($os) && strpos($os, 'Windows') !== false) {
00196 $path = str_replace('/', '\\', WWW_ROOT . $this->themeWeb . $file);
00197 } else {
00198 $path = WWW_ROOT . $this->themeWeb . $file;
00199 }
00200 if (file_exists($path)) {
00201 $webPath = "{$this->webroot}" . $this->themeWeb . $file;
00202 }
00203 }
00204 return str_replace('
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00214 function clean($output) {
00215 $this->__reset();
00216 if (empty($output)) {
00217 return null;
00218 }
00219 if (is_array($output)) {
00220 foreach ($output as $key => $value) {
00221 $return[$key] = $this->clean($value);
00222 }
00223 return $return;
00224 }
00225 $this->__tainted = $output;
00226 $this->__clean();
00227 return $this->__cleaned;
00228 }
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260 function _parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
00261 if (is_array($options)) {
00262 $options = array_merge(array('escape' => true), $options);
00263
00264 if (!is_array($exclude)) {
00265 $exclude = array();
00266 }
00267 $keys = array_diff(array_keys($options), array_merge((array)$exclude, array('escape')));
00268 $values = array_intersect_key(array_values($options), $keys);
00269 $escape = $options['escape'];
00270 $attributes = array();
00271
00272 foreach ($keys as $index => $key) {
00273 $attributes[] = $this->__formatAttribute($key, $values[$index], $escape);
00274 }
00275 $out = implode(' ', $attributes);
00276 } else {
00277 $out = $options;
00278 }
00279 return $out ? $insertBefore . $out . $insertAfter : '';
00280 }
00281
00282
00283
00284
00285
00286
00287 function __formatAttribute($key, $value, $escape = true) {
00288 $attribute = '';
00289 $attributeFormat = '%s="%s"';
00290 $minimizedAttributes = array('compact', 'checked', 'declare', 'readonly', 'disabled', 'selected', 'defer', 'ismap', 'nohref', 'noshade', 'nowrap', 'multiple', 'noresize');
00291 if (is_array($value)) {
00292 $value = '';
00293 }
00294
00295 if (in_array($key, $minimizedAttributes)) {
00296 if ($value === 1 || $value === true || $value === 'true' || $value == $key) {
00297 $attribute = sprintf($attributeFormat, $key, $key);
00298 }
00299 } else {
00300 $attribute = sprintf($attributeFormat, $key, ife($escape, h($value), $value));
00301 }
00302 return $attribute;
00303 }
00304
00305
00306
00307
00308
00309
00310
00311 function setEntity($entity, $setScope = false) {
00312 $view =& ClassRegistry::getObject('view');
00313
00314 if ($setScope) {
00315 $view->modelScope = false;
00316 } elseif (join('.', $view->entity()) == $entity) {
00317 return;
00318 }
00319
00320 if ($entity === null) {
00321 $view->model = null;
00322 $view->association = null;
00323 $view->modelId = null;
00324 $view->modelScope = false;
00325 return;
00326 }
00327
00328 $model = $view->model;
00329 $sameScope = $hasField = false;
00330 $parts = array_values(Set::filter(preg_split('/\/|\./', $entity), true));
00331
00332 if (empty($parts)) {
00333 return;
00334 }
00335
00336 if (count($parts) === 1 || is_numeric($parts[0])) {
00337 $sameScope = true;
00338 } else {
00339 if (ClassRegistry::isKeySet($parts[0])) {
00340 $model = $parts[0];
00341 }
00342 }
00343
00344 if (ClassRegistry::isKeySet($model)) {
00345 $ModelObj =& ClassRegistry::getObject($model);
00346 for ($i = 0; $i < count($parts); $i++) {
00347 if ($ModelObj->hasField($parts[$i]) || array_key_exists($parts[$i], $ModelObj->validate)) {
00348 $hasField = $i;
00349 if ($hasField === 0 || ($hasField === 1 && is_numeric($parts[0]))) {
00350 $sameScope = true;
00351 }
00352 break;
00353 }
00354 }
00355
00356 if ($sameScope === true && in_array($parts[0], array_keys($ModelObj->hasAndBelongsToMany))) {
00357 $sameScope = false;
00358 }
00359 }
00360
00361 if (!$view->association && $parts[0] == $view->field && $view->field != $view->model) {
00362 array_unshift($parts, $model);
00363 $hasField = true;
00364 }
00365 $view->field = $view->modelId = $view->fieldSuffix = $view->association = null;
00366
00367 switch (count($parts)) {
00368 case 1:
00369 if($view->modelScope === false) {
00370 $view->model = $parts[0];
00371 } else {
00372 $view->field = $parts[0];
00373 if($sameScope === false) {
00374 $view->association = $parts[0];
00375 }
00376 }
00377 break;
00378 case 2:
00379 if ($view->modelScope === false) {
00380 list($view->model, $view->field) = $parts;
00381 } elseif ($sameScope === true && $hasField === 0) {
00382 list($view->field, $view->fieldSuffix) = $parts;
00383 } elseif ($sameScope === true && $hasField === 1) {
00384 list($view->modelId, $view->field) = $parts;
00385 } else {
00386 list($view->association, $view->field) = $parts;
00387 }
00388 break;
00389 case 3:
00390 if ($sameScope === true && $hasField === 1) {
00391 list($view->modelId, $view->field, $view->fieldSuffix) = $parts;
00392 } elseif ($hasField === 2) {
00393 list($view->association, $view->modelId, $view->field) = $parts;
00394 } else {
00395 list($view->association, $view->field, $view->fieldSuffix) = $parts;
00396 }
00397 break;
00398 case 4:
00399 if ($parts[0] === $view->model) {
00400 list($view->model, $view->modelId, $view->field, $view->fieldSuffix) = $parts;
00401 } else {
00402 list($view->association, $view->modelId, $view->field, $view->fieldSuffix) = $parts;
00403 }
00404 break;
00405 }
00406
00407 if (!isset($view->model) || empty($view->model)) {
00408 $view->model = $view->association;
00409 $view->association = null;
00410 } elseif ($view->model === $view->association) {
00411 $view->association = null;
00412 }
00413
00414 if ($setScope) {
00415 $view->modelScope = true;
00416 }
00417 }
00418
00419
00420
00421
00422
00423 function model() {
00424 $view =& ClassRegistry::getObject('view');
00425 if (!empty($view->association)) {
00426 return $view->association;
00427 } else {
00428 return $view->model;
00429 }
00430 }
00431
00432
00433
00434
00435
00436 function modelID() {
00437 $view =& ClassRegistry::getObject('view');
00438 return $view->modelId;
00439 }
00440
00441
00442
00443
00444
00445 function field() {
00446 $view =& ClassRegistry::getObject('view');
00447 return $view->field;
00448 }
00449
00450
00451
00452
00453
00454
00455
00456
00457 function tagIsInvalid($model = null, $field = null, $modelID = null) {
00458 foreach (array('model', 'field', 'modelID') as $key) {
00459 if (empty(${$key})) {
00460 ${$key} = $this->{$key}();
00461 }
00462 }
00463 $view =& ClassRegistry::getObject('view');
00464 $errors = $this->validationErrors;
00465
00466 if ($view->model !== $model && isset($errors[$view->model][$model])) {
00467 $errors = $errors[$view->model];
00468 }
00469
00470 if (!isset($modelID)) {
00471 return empty($errors[$model][$field]) ? 0 : $errors[$model][$field];
00472 } else {
00473 return empty($errors[$model][$modelID][$field]) ? 0 : $errors[$model][$modelID][$field];
00474 }
00475 }
00476
00477
00478
00479
00480
00481
00482
00483 function domId($options = null, $id = 'id') {
00484 $view =& ClassRegistry::getObject('view');
00485
00486 if (is_array($options) && array_key_exists($id, $options) && $options[$id] === null) {
00487 unset($options[$id]);
00488 return $options;
00489 } elseif (!is_array($options) && $options !== null) {
00490 $this->setEntity($options);
00491 return $this->domId();
00492 }
00493
00494 $dom = $this->model() . $this->modelID() . Inflector::camelize($view->field) . Inflector::camelize($view->fieldSuffix);
00495
00496 if (is_array($options) && !array_key_exists($id, $options)) {
00497 $options[$id] = $dom;
00498 } elseif ($options === null) {
00499 return $dom;
00500 }
00501 return $options;
00502 }
00503
00504
00505
00506
00507
00508
00509
00510 function __name($options = array(), $field = null, $key = 'name') {
00511 $view =& ClassRegistry::getObject('view');
00512
00513 if ($options === null) {
00514 $options = array();
00515 } elseif (is_string($options)) {
00516 $field = $options;
00517 $options = 0;
00518 }
00519
00520 if (!empty($field)) {
00521 $this->setEntity($field);
00522 }
00523
00524 if (is_array($options) && array_key_exists($key, $options)) {
00525 return $options;
00526 }
00527
00528 switch ($field) {
00529 case '_method':
00530 $name = $field;
00531 break;
00532 default:
00533 $name = 'data[' . join('][', $view->entity()) . ']';
00534 break;
00535 }
00536
00537 if (is_array($options)) {
00538 $options[$key] = $name;
00539 return $options;
00540 } else {
00541 return $name;
00542 }
00543 }
00544
00545
00546
00547
00548
00549
00550
00551
00552 function value($options = array(), $field = null, $key = 'value') {
00553 if ($options === null) {
00554 $options = array();
00555 } elseif (is_string($options)) {
00556 $field = $options;
00557 $options = 0;
00558 }
00559
00560 if (!empty($field)) {
00561 $this->setEntity($field);
00562 }
00563
00564 if (is_array($options) && isset($options[$key])) {
00565 return $options;
00566 }
00567
00568 $result = null;
00569
00570 if (isset($this->data[$this->model()][$this->field()])) {
00571 $result = $this->data[$this->model()][$this->field()];
00572 } elseif (isset($this->data[$this->field()]) && is_array($this->data[$this->field()])) {
00573 if (ClassRegistry::isKeySet($this->field())) {
00574 $model =& ClassRegistry::getObject($this->field());
00575 $result = $this->__selectedArray($this->data[$this->field()], $model->primaryKey);
00576 }
00577 } elseif (isset($this->data[$this->model()][$this->modelID()][$this->field()])) {
00578 $result = $this->data[$this->model()][$this->modelID()][$this->field()];
00579 }
00580
00581 if (is_array($result)) {
00582 $view =& ClassRegistry::getObject('view');
00583 if(isset($result[$view->fieldSuffix])) {
00584 $result = $result[$view->fieldSuffix];
00585 }
00586 }
00587
00588 if (is_array($options)) {
00589 if (empty($result) && isset($options['default'])) {
00590 $result = $options['default'];
00591 }
00592 unset($options['default']);
00593 }
00594
00595 if (is_array($options)) {
00596 $options[$key] = $result;
00597 return $options;
00598 } else {
00599 return $result;
00600 }
00601 }
00602
00603
00604
00605
00606
00607
00608
00609 function __initInputField($field, $options = array()) {
00610 if ($field !== null) {
00611 $this->setEntity($field);
00612 }
00613 $options = (array)$options;
00614 $options = $this->__name($options);
00615 $options = $this->value($options);
00616 $options = $this->domId($options);
00617 if ($this->tagIsInvalid()) {
00618 $options = $this->addClass($options, 'form-error');
00619 }
00620 return $options;
00621 }
00622
00623
00624
00625
00626
00627
00628
00629
00630 function addClass($options = array(), $class = null, $key = 'class') {
00631 if (isset($options[$key]) && trim($options[$key]) != '') {
00632 $options[$key] .= ' ' . $class;
00633 } else {
00634 $options[$key] = $class;
00635 }
00636 return $options;
00637 }
00638
00639
00640
00641
00642
00643
00644
00645
00646 function output($str) {
00647 return $str;
00648 }
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659 function assign($keyName, $values) {
00660 $out = $keyName;
00661 if (isset($this->tags) && isset($this->tags[$keyName])) {
00662 $out = $this->tags[$keyName];
00663 }
00664
00665
00666 }
00667
00668
00669
00670
00671 function beforeRender() {
00672 }
00673
00674
00675
00676
00677 function afterRender() {
00678 }
00679
00680
00681
00682
00683 function beforeLayout() {
00684 }
00685
00686
00687
00688
00689 function afterLayout() {
00690 }
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700 function __selectedArray($data, $key = 'id') {
00701 if (!is_array($data)) {
00702 $model = $data;
00703 if (!empty($this->data[$model][$model])) {
00704 return $this->data[$model][$model];
00705 }
00706 if (!empty($this->data[$model])) {
00707 $data = $this->data[$model];
00708 }
00709 }
00710 $array = array();
00711 if (!empty($data)) {
00712 foreach ($data as $var) {
00713 $array[$var[$key]] = $var[$key];
00714 }
00715 }
00716 return $array;
00717 }
00718
00719
00720
00721
00722
00723 function __reset() {
00724 $this->__tainted = null;
00725 $this->__cleaned = null;
00726 }
00727
00728
00729
00730
00731
00732 function __clean() {
00733 if (get_magic_quotes_gpc()) {
00734 $this->__cleaned = stripslashes($this->__tainted);
00735 } else {
00736 $this->__cleaned = $this->__tainted;
00737 }
00738
00739 $this->__cleaned = str_replace(array("&","<",">"),array("&amp;","&lt;","&gt;"), $this->__cleaned);
00740 $this->__cleaned = preg_replace('#(&\#*\w+)[\x00-\x20]+;#u',"$1;", $this->__cleaned);
00741 $this->__cleaned = preg_replace('#(&\#x*)([0-9A-F]+);*#iu',"$1$2;", $this->__cleaned);
00742 $this->__cleaned = html_entity_decode($this->__cleaned, ENT_COMPAT, "UTF-8");
00743 $this->__cleaned = preg_replace('#(<*[^>]*[\x00-\x20\"\'])(on|xmlns)[^>]*>#iUu',"$1>", $this->__cleaned);
00744 $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);
00745 $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);
00746 $this->__cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=*([\'\"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#iUu','$1=$2nomozbinding...', $this->__cleaned);
00747 $this->__cleaned = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*expression[\x00-\x20]*\([^>]*>#iU',"$1>", $this->__cleaned);
00748 $this->__cleaned = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*behaviour[\x00-\x20]*\([^>]*>#iU',"$1>", $this->__cleaned);
00749 $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);
00750 $this->__cleaned = preg_replace('#</*\w+:\w[^>]*>#i',"", $this->__cleaned);
00751 do {
00752 $oldstring = $this->__cleaned;
00753 $this->__cleaned = preg_replace('#</*(applet|meta|xml|blink|link|style|script|embed|object|iframe|frame|frameset|ilayer|layer|bgsound|title|base)[^>]*>#i',"",$this->__cleaned);
00754 } while ($oldstring != $this->__cleaned);
00755 }
00756
00757
00758
00759
00760 function setFormTag($tagValue, $setScope = false) {
00761 trigger_error(__('Helper::setFormTag() Deprecated, use Helper::setEntity()', true), E_USER_WARNING);
00762 return $this->setEntity($tagValue, $setScope);
00763 }
00764 }
00765 ?>