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 App::import('Core', 'Overloadable');
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 class Helper extends Overloadable {
00041
00042
00043
00044
00045
00046 var $helpers = null;
00047
00048
00049
00050
00051
00052 var $base = null;
00053
00054
00055
00056
00057
00058 var $webroot = null;
00059
00060
00061
00062
00063
00064 var $themeWeb = null;
00065
00066
00067
00068
00069
00070 var $here = null;
00071
00072
00073
00074
00075
00076 var $params = array();
00077
00078
00079
00080
00081
00082 var $action = null;
00083
00084
00085
00086
00087
00088 var $plugin = null;
00089
00090
00091
00092
00093
00094 var $data = null;
00095
00096
00097
00098
00099
00100 var $namedArgs = null;
00101
00102
00103
00104
00105
00106 var $argSeparator = null;
00107
00108
00109
00110
00111
00112
00113 var $validationErrors = null;
00114
00115
00116
00117
00118
00119
00120 var $tags = array();
00121
00122
00123
00124
00125
00126
00127 var $__tainted = null;
00128
00129
00130
00131
00132
00133
00134 var $__cleaned = null;
00135
00136
00137
00138
00139
00140 function get__($name) {}
00141 function set__($name, $value) {}
00142 function call__($method, $params) {
00143 trigger_error(sprintf(__('Method %1$s::%2$s does not exist', true), get_class($this), $method), E_USER_WARNING);
00144 }
00145
00146
00147
00148
00149
00150
00151
00152 function loadConfig($name = 'tags') {
00153 if (file_exists(CONFIGS . $name .'.php')) {
00154 require(CONFIGS . $name .'.php');
00155 if (isset($tags)) {
00156 $this->tags = array_merge($this->tags, $tags);
00157 }
00158 }
00159 return $this->tags;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 function url($url = null, $full = false) {
00178 return h(Router::url($url, $full));
00179 }
00180
00181
00182
00183
00184
00185
00186 function webroot($file) {
00187 $webPath = "{$this->webroot}" . $file;
00188 if (!empty($this->themeWeb)) {
00189 $os = env('OS');
00190 if (!empty($os) && strpos($os, 'Windows') !== false) {
00191 if (strpos(WWW_ROOT . $this->themeWeb . $file, '\\') !== false) {
00192 $path = str_replace('/', '\\', WWW_ROOT . $this->themeWeb . $file);
00193 }
00194 } else {
00195 $path = WWW_ROOT . $this->themeWeb . $file;
00196 }
00197 if (file_exists($path)) {
00198 $webPath = "{$this->webroot}" . $this->themeWeb . $file;
00199 }
00200 }
00201 if (strpos($webPath, '
00202 return str_replace('
00203 }
00204 return $webPath;
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, ($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(explode('.', $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 $modelName = $this->model();
00571 $fieldName = $this->field();
00572 $modelID = $this->modelID();
00573
00574 if (is_null($fieldName)) {
00575 $fieldName = $modelName;
00576 $modelName = null;
00577 }
00578
00579 if (isset($this->data[$fieldName]) && $modelName === null) {
00580 $result = $this->data[$fieldName];
00581 } elseif (isset($this->data[$modelName][$fieldName])) {
00582 $result = $this->data[$modelName][$fieldName];
00583 } elseif (isset($this->data[$fieldName]) && is_array($this->data[$fieldName])) {
00584 if (ClassRegistry::isKeySet($fieldName)) {
00585 $model =& ClassRegistry::getObject($fieldName);
00586 $result = $this->__selectedArray($this->data[$fieldName], $model->primaryKey);
00587 }
00588 } elseif (isset($this->data[$modelName][$modelID][$fieldName])) {
00589 $result = $this->data[$modelName][$modelID][$fieldName];
00590 }
00591
00592 if (is_array($result)) {
00593 $view =& ClassRegistry::getObject('view');
00594 if (array_key_exists($view->fieldSuffix, $result)) {
00595 $result = $result[$view->fieldSuffix];
00596 }
00597 }
00598
00599 if (is_array($options)) {
00600 if (empty($result) && isset($options['default'])) {
00601 $result = $options['default'];
00602 }
00603 unset($options['default']);
00604 }
00605
00606 if (is_array($options)) {
00607 $options[$key] = $result;
00608 return $options;
00609 } else {
00610 return $result;
00611 }
00612 }
00613
00614
00615
00616
00617
00618
00619
00620
00621 function _initInputField($field, $options = array()) {
00622 if ($field !== null) {
00623 $this->setEntity($field);
00624 }
00625 $options = (array)$options;
00626 $options = $this->__name($options);
00627 $options = $this->value($options);
00628 $options = $this->domId($options);
00629 if ($this->tagIsInvalid()) {
00630 $options = $this->addClass($options, 'form-error');
00631 }
00632 return $options;
00633 }
00634
00635
00636
00637
00638
00639
00640
00641
00642 function addClass($options = array(), $class = null, $key = 'class') {
00643 if (isset($options[$key]) && trim($options[$key]) != '') {
00644 $options[$key] .= ' ' . $class;
00645 } else {
00646 $options[$key] = $class;
00647 }
00648 return $options;
00649 }
00650
00651
00652
00653
00654
00655
00656
00657
00658 function output($str) {
00659 return $str;
00660 }
00661
00662
00663
00664
00665 function beforeRender() {
00666 }
00667
00668
00669
00670
00671 function afterRender() {
00672 }
00673
00674
00675
00676
00677 function beforeLayout() {
00678 }
00679
00680
00681
00682
00683 function afterLayout() {
00684 }
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694 function __selectedArray($data, $key = 'id') {
00695 if (!is_array($data)) {
00696 $model = $data;
00697 if (!empty($this->data[$model][$model])) {
00698 return $this->data[$model][$model];
00699 }
00700 if (!empty($this->data[$model])) {
00701 $data = $this->data[$model];
00702 }
00703 }
00704 $array = array();
00705 if (!empty($data)) {
00706 foreach ($data as $var) {
00707 $array[$var[$key]] = $var[$key];
00708 }
00709 }
00710 return $array;
00711 }
00712
00713
00714
00715
00716
00717 function __reset() {
00718 $this->__tainted = null;
00719 $this->__cleaned = null;
00720 }
00721
00722
00723
00724
00725
00726 function __clean() {
00727 if (get_magic_quotes_gpc()) {
00728 $this->__cleaned = stripslashes($this->__tainted);
00729 } else {
00730 $this->__cleaned = $this->__tainted;
00731 }
00732
00733 $this->__cleaned = str_replace(array("&", "<", ">"), array("&amp;", "&lt;", "&gt;"), $this->__cleaned);
00734 $this->__cleaned = preg_replace('#(&\#*\w+)[\x00-\x20]+;#u', "$1;", $this->__cleaned);
00735 $this->__cleaned = preg_replace('#(&\#x*)([0-9A-F]+);*#iu', "$1$2;", $this->__cleaned);
00736 $this->__cleaned = html_entity_decode($this->__cleaned, ENT_COMPAT, "UTF-8");
00737 $this->__cleaned = preg_replace('#(<[^>]+[\x00-\x20\"\'\/])(on|xmlns)[^>]*>#iUu', "$1>", $this->__cleaned);
00738 $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);
00739 $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);
00740 $this->__cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=*([\'\"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#iUu','$1=$2nomozbinding...', $this->__cleaned);
00741 $this->__cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=([\'\"]*)[\x00-\x20]*data[\x00-\x20]*:#Uu', '$1=$2nodata...', $this->__cleaned);
00742 $this->__cleaned = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*expression[\x00-\x20]*\([^>]*>#iU', "$1>", $this->__cleaned);
00743 $this->__cleaned = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*behaviour[\x00-\x20]*\([^>]*>#iU', "$1>", $this->__cleaned);
00744 $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);
00745 $this->__cleaned = preg_replace('#</*\w+:\w[^>]*>#i', "", $this->__cleaned);
00746 do {
00747 $oldstring = $this->__cleaned;
00748 $this->__cleaned = preg_replace('#</*(applet|meta|xml|blink|link|style|script|embed|object|iframe|frame|frameset|ilayer|layer|bgsound|title|base)[^>]*>#i', "", $this->__cleaned);
00749 } while ($oldstring != $this->__cleaned);
00750 $this->__cleaned = str_replace(array("&", "<", ">"), array("&amp;", "&lt;", "&gt;"), $this->__cleaned);
00751 }
00752 }
00753 ?>