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 if (!defined('REQUEST_MOBILE_UA')) {
00031 define('REQUEST_MOBILE_UA', '(iPhone|MIDP|AvantGo|BlackBerry|J2ME|Opera Mini|DoCoMo|NetFront|Nokia|PalmOS|PalmSource|portalmmm|Plucker|ReqwirelessWeb|SonyEricsson|Symbian|UP\.Browser|Windows CE|Xiino)');
00032 }
00033
00034
00035
00036
00037
00038
00039
00040
00041 class RequestHandlerComponent extends Object {
00042
00043
00044
00045
00046
00047
00048
00049 var $ajaxLayout = 'ajax';
00050
00051
00052
00053
00054
00055
00056 var $enabled = true;
00057
00058
00059
00060
00061
00062
00063
00064 var $__responseTypeSet = null;
00065
00066
00067
00068
00069
00070
00071 var $params = array();
00072
00073
00074
00075
00076
00077
00078
00079
00080 var $__requestContent = array(
00081 'javascript' => 'text/javascript',
00082 'js' => 'text/javascript',
00083 'json' => 'application/json',
00084 'css' => 'text/css',
00085 'html' => array('text/html', '*
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 var $__acceptTypes = array();
00115
00116
00117
00118
00119
00120
00121 var $__renderType = null;
00122
00123
00124
00125
00126
00127
00128
00129 var $ext = null;
00130
00131
00132
00133
00134
00135
00136
00137 var $__typesInitialized = false;
00138
00139
00140
00141
00142 function __construct() {
00143 $this->__acceptTypes = explode(',', env('HTTP_ACCEPT'));
00144
00145 foreach ($this->__acceptTypes as $i => $type) {
00146 if (strpos($type, ';')) {
00147 $type = explode(';', $type);
00148 $this->__acceptTypes[$i] = $type[0];
00149 }
00150 }
00151 parent::__construct();
00152 }
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 function initialize(&$controller) {
00164 if (isset($controller->params['url']['ext'])) {
00165 $this->ext = $controller->params['url']['ext'];
00166 }
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 function startup(&$controller) {
00186 if (!$this->enabled) {
00187 return;
00188 }
00189 $this->__initializeTypes();
00190 $controller->params['isAjax'] = $this->isAjax();
00191
00192 if (!empty($this->ext) && !in_array($this->ext, array('html', 'htm')) && in_array($this->ext, array_keys($this->__requestContent))) {
00193 $this->renderAs($controller, $this->ext);
00194 } elseif ($this->isAjax()) {
00195 $this->renderAs($controller, 'ajax');
00196 }
00197
00198 if ($this->requestedWith('xml')) {
00199 if (!class_exists('XmlNode')) {
00200 App::import('Core', 'Xml');
00201 }
00202 $xml = new Xml(trim(file_get_contents('php://input')));
00203 if (is_object($xml->child('data')) && count($xml->children) == 1) {
00204 $controller->data = $xml->child('data');
00205 } else {
00206 $controller->data = $xml;
00207 }
00208 }
00209 }
00210
00211
00212
00213
00214
00215
00216
00217 function beforeRedirect(&$controller, $url) {
00218 if (!$this->isAjax()) {
00219 return;
00220 }
00221 foreach ($_POST as $key => $val) {
00222 unset($_POST[$key]);
00223 }
00224 echo $this->requestAction($url, array('return'));
00225 $this->_stop();
00226 }
00227
00228
00229
00230
00231
00232
00233 function isAjax() {
00234 return env('HTTP_X_REQUESTED_WITH') === "XMLHttpRequest";
00235 }
00236
00237
00238
00239
00240
00241
00242 function isFlash() {
00243 return env('HTTP_USER_AGENT') === "Shockwave Flash";
00244 }
00245
00246
00247
00248
00249
00250
00251 function isSSL() {
00252 return env('HTTPS');
00253 }
00254
00255
00256
00257
00258
00259
00260 function isXml() {
00261 return $this->prefers('xml');
00262 }
00263
00264
00265
00266
00267
00268
00269 function isRss() {
00270 return $this->prefers('rss');
00271 }
00272
00273
00274
00275
00276
00277
00278 function isAtom() {
00279 return $this->prefers('atom');
00280 }
00281
00282
00283
00284
00285
00286
00287
00288 function isMobile() {
00289 preg_match('/' . REQUEST_MOBILE_UA . '/i', env('HTTP_USER_AGENT'), $match);
00290 if (!empty($match) || $this->accepts('wap')) {
00291 return true;
00292 }
00293 return false;
00294 }
00295
00296
00297
00298
00299
00300
00301 function isWap() {
00302 return $this->prefers('wap');
00303 }
00304
00305
00306
00307
00308
00309
00310 function isPost() {
00311 return (strtolower(env('REQUEST_METHOD')) == 'post');
00312 }
00313
00314
00315
00316
00317
00318
00319 function isPut() {
00320 return (strtolower(env('REQUEST_METHOD')) == 'put');
00321 }
00322
00323
00324
00325
00326
00327
00328 function isGet() {
00329 return (strtolower(env('REQUEST_METHOD')) == 'get');
00330 }
00331
00332
00333
00334
00335
00336
00337 function isDelete() {
00338 return (strtolower(env('REQUEST_METHOD')) == 'delete');
00339 }
00340
00341
00342
00343
00344
00345
00346
00347 function getAjaxVersion() {
00348 if (env('HTTP_X_PROTOTYPE_VERSION') != null) {
00349 return env('HTTP_X_PROTOTYPE_VERSION');
00350 }
00351 return false;
00352 }
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 function setContent($name, $type = null) {
00365 if (is_array($name)) {
00366 $this->__requestContent = array_merge($this->__requestContent, $name);
00367 return;
00368 }
00369 $this->__requestContent[$name] = $type;
00370 }
00371
00372
00373
00374
00375
00376
00377 function getReferrer() {
00378 if (env('HTTP_HOST') != null) {
00379 $sess_host = env('HTTP_HOST');
00380 }
00381
00382 if (env('HTTP_X_FORWARDED_HOST') != null) {
00383 $sess_host = env('HTTP_X_FORWARDED_HOST');
00384 }
00385 return trim(preg_replace('/:.*/', '', $sess_host));
00386 }
00387
00388
00389
00390
00391
00392
00393 function getClientIP() {
00394 if (env('HTTP_X_FORWARDED_FOR') != null) {
00395 $ipaddr = preg_replace('/,.*/', '', env('HTTP_X_FORWARDED_FOR'));
00396 } else {
00397 if (env('HTTP_CLIENT_IP') != null) {
00398 $ipaddr = env('HTTP_CLIENT_IP');
00399 } else {
00400 $ipaddr = env('REMOTE_ADDR');
00401 }
00402 }
00403
00404 if (env('HTTP_CLIENTADDRESS') != null) {
00405 $tmpipaddr = env('HTTP_CLIENTADDRESS');
00406
00407 if (!empty($tmpipaddr)) {
00408 $ipaddr = preg_replace('/,.*/', '', $tmpipaddr);
00409 }
00410 }
00411 return trim($ipaddr);
00412 }
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427 function accepts($type = null) {
00428 $this->__initializeTypes();
00429
00430 if ($type == null) {
00431 return $this->mapType($this->__acceptTypes);
00432
00433 } elseif (is_array($type)) {
00434 foreach ($type as $t) {
00435 if ($this->accepts($t) == true) {
00436 return true;
00437 }
00438 }
00439 return false;
00440 } elseif (is_string($type)) {
00441
00442 if (!in_array($type, array_keys($this->__requestContent))) {
00443 return false;
00444 }
00445
00446 $content = $this->__requestContent[$type];
00447
00448 if (is_array($content)) {
00449 foreach ($content as $c) {
00450 if (in_array($c, $this->__acceptTypes)) {
00451 return true;
00452 }
00453 }
00454 } else {
00455 if (in_array($content, $this->__acceptTypes)) {
00456 return true;
00457 }
00458 }
00459 }
00460 }
00461
00462
00463
00464
00465
00466
00467 function requestedWith($type = null) {
00468 if (!$this->isPost() && !$this->isPut()) {
00469 return null;
00470 }
00471
00472 if ($type == null) {
00473 return $this->mapType(env('CONTENT_TYPE'));
00474 } elseif (is_array($type)) {
00475 foreach ($type as $t) {
00476 if ($this->requestedWith($t)) {
00477 return $this->mapType($t);
00478 }
00479 }
00480 return false;
00481 } elseif (is_string($type)) {
00482 return ($type == $this->mapType(env('CONTENT_TYPE')));
00483 }
00484 }
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500 function prefers($type = null) {
00501 $this->__initializeTypes();
00502 if ($type == null) {
00503 if (empty($this->ext)) {
00504 $accept = $this->accepts(null);
00505 if (is_array($accept)) {
00506 return $accept[0];
00507 }
00508 return $accept;
00509 } else {
00510 return $this->ext;
00511 }
00512 }
00513 App::import('Core', 'Set');
00514 $types = Set::normalize($type, false);
00515 $accepts = array();
00516
00517 foreach ($types as $type) {
00518 if ($this->accepts($type)) {
00519 $accepts[] = $type;
00520 }
00521 }
00522
00523 if (count($accepts) == 0) {
00524 return false;
00525 } elseif (count($accepts) == 1) {
00526 return $accepts[0];
00527 } else {
00528 $accepts = array_intersect($this->__acceptTypes, $accepts);
00529 return $accepts[0];
00530 }
00531 }
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541 function renderAs(&$controller, $type) {
00542 $this->__initializeTypes();
00543 $options = array('charset' => 'UTF-8');
00544
00545 if (Configure::read('App.encoding') !== null) {
00546 $options = array('charset' => Configure::read('App.encoding'));
00547 }
00548
00549 if ($type == 'ajax') {
00550 $controller->layout = $this->ajaxLayout;
00551 return $this->respondAs('html', $options);
00552 }
00553 $controller->ext = '.ctp';
00554
00555 if (empty($this->__renderType)) {
00556 $controller->viewPath .= '/' . $type;
00557 } else {
00558 $controller->viewPath = preg_replace("/\/{$type}$/", '/' . $type, $controller->viewPath);
00559 }
00560 $this->__renderType = $type;
00561 $controller->layoutPath = $type;
00562
00563 if (in_array($type, array_keys($this->__requestContent))) {
00564 $this->respondAs($type, $options);
00565 }
00566
00567 $helper = ucfirst($type);
00568 if (!in_array($helper, $controller->helpers) && !array_key_exists($helper, $controller->helpers)) {
00569 if (App::import('Helper', $helper)) {
00570 $controller->helpers[] = $helper;
00571 }
00572 }
00573 }
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589 function respondAs($type, $options = array()) {
00590 $this->__initializeTypes();
00591 if ($this->__responseTypeSet != null) {
00592 return false;
00593 }
00594 if (!array_key_exists($type, $this->__requestContent) && strpos($type, '/') === false) {
00595 return false;
00596 }
00597 $options = array_merge(array('index' => 0, 'charset' => null, 'attachment' => false), $options);
00598
00599 if (strpos($type, '/') === false && isset($this->__requestContent[$type])) {
00600 $cType = null;
00601 if (is_array($this->__requestContent[$type]) && isset($this->__requestContent[$type][$options['index']])) {
00602 $cType = $this->__requestContent[$type][$options['index']];
00603 } elseif (is_array($this->__requestContent[$type]) && isset($this->__requestContent[$type][0])) {
00604 $cType = $this->__requestContent[$type][0];
00605 } elseif (isset($this->__requestContent[$type])) {
00606 $cType = $this->__requestContent[$type];
00607 } else {
00608 return false;
00609 }
00610 if (is_array($cType)) {
00611 if ($this->prefers($cType)) {
00612 $cType = $this->prefers($cType);
00613 } else {
00614 $cType = $cType[0];
00615 }
00616 }
00617 } else {
00618 $cType = $type;
00619 }
00620
00621 if ($cType != null) {
00622 $header = 'Content-type: ' . $cType;
00623
00624 if (!empty($options['charset'])) {
00625 $header .= '; charset=' . $options['charset'];
00626 }
00627 if (!empty($options['attachment'])) {
00628 header('Content-Disposition: attachment; filename="' . $options['attachment'] . '"');
00629 }
00630 if (Configure::read() < 2 && !defined('CAKEPHP_SHELL')) {
00631 @header($header);
00632 }
00633 $this->__responseTypeSet = $cType;
00634 return true;
00635 } else {
00636 return false;
00637 }
00638 }
00639
00640
00641
00642
00643
00644
00645
00646 function responseType() {
00647 if ($this->__responseTypeSet == null) {
00648 return null;
00649 }
00650 return $this->mapType($this->__responseTypeSet);
00651 }
00652
00653
00654
00655
00656
00657
00658
00659 function mapType($ctype) {
00660 if (is_array($ctype)) {
00661 $out = array();
00662 foreach ($ctype as $t) {
00663 $out[] = $this->mapType($t);
00664 }
00665 return $out;
00666 } else {
00667 $keys = array_keys($this->__requestContent);
00668 $count = count($keys);
00669
00670 for ($i = 0; $i < $count; $i++) {
00671 $name = $keys[$i];
00672 $type = $this->__requestContent[$name];
00673
00674 if (is_array($type) && in_array($ctype, $type)) {
00675 return $name;
00676 } elseif (!is_array($type) && $type == $ctype) {
00677 return $name;
00678 }
00679 }
00680 return $ctype;
00681 }
00682 }
00683
00684
00685
00686
00687
00688
00689 function __initializeTypes() {
00690 if ($this->__typesInitialized) {
00691 return;
00692 }
00693 if (isset($this->__requestContent[$this->ext])) {
00694 $content = $this->__requestContent[$this->ext];
00695 if (is_array($content)) {
00696 $content = $content[0];
00697 }
00698 array_unshift($this->__acceptTypes, $content);
00699 }
00700 $this->__typesInitialized = true;
00701 }
00702 }
00703
00704 ?>