request_handler.php
Go to the documentation of this file.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 if (!defined('REQUEST_MOBILE_UA')) {
00030 define('REQUEST_MOBILE_UA',
00031 '(AvantGo|BlackBerry|DoCoMo|NetFront|Nokia|PalmOS|PalmSource|portalmmm|Plucker|ReqwirelessWeb|SonyEricsson|Symbian|UP\.Browser|Windows CE|Xiino)');
00032 }
00033
00034
00035
00036
00037
00038
00039
00040 class RequestHandlerComponent extends Object{
00041
00042
00043
00044
00045
00046
00047 var $controller = true;
00048
00049
00050
00051
00052
00053
00054
00055 var $ajaxLayout = 'ajax';
00056
00057
00058
00059
00060
00061
00062 var $disableStartup = false;
00063
00064
00065
00066
00067
00068
00069
00070
00071 var $__requestContent = array(
00072 'js' => 'text/javascript',
00073 'css' => 'text/css',
00074 'html' => 'text/html',
00075 'form' => 'application/x-www-form-urlencoded',
00076 'file' => 'multipart/form-data',
00077 'xhtml' => array('application/xhtml+xml', 'application/xhtml', 'text/xhtml'),
00078 'xml' => array('application/xml', 'text/xml'),
00079 'rss' => 'application/rss+xml',
00080 'atom' => 'application/atom+xml'
00081 );
00082
00083
00084
00085
00086
00087
00088
00089
00090 var $__acceptTypes = array();
00091
00092
00093
00094
00095
00096
00097
00098 function __construct() {
00099 $this->__acceptTypes = explode(',', env('HTTP_ACCEPT'));
00100
00101 foreach ($this->__acceptTypes as $i => $type) {
00102 if (strpos($type, ';')) {
00103 $type = explode(';', $type);
00104 $this->__acceptTypes[$i] = $type[0];
00105 }
00106 }
00107 parent::__construct();
00108 }
00109
00110
00111
00112
00113
00114
00115
00116 function startup(&$controller) {
00117 if ($this->disableStartup) {
00118 return;
00119 }
00120 $this->setAjax($controller);
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 function setAjax(&$controller) {
00132 if ($this->isAjax()) {
00133 $controller->layout = $this->ajaxLayout;
00134
00135 header ('Content-Type: text/html; charset=UTF-8');
00136 }
00137 }
00138
00139
00140
00141
00142
00143
00144 function isAjax() {
00145 if (env('HTTP_X_REQUESTED_WITH') != null) {
00146 return env('HTTP_X_REQUESTED_WITH') == "XMLHttpRequest";
00147 } else {
00148 return false;
00149 }
00150 }
00151
00152
00153
00154
00155
00156
00157 function isXml() {
00158 return $this->accepts('xml');
00159 }
00160
00161
00162
00163
00164
00165
00166 function isRss() {
00167 return $this->accepts('rss');
00168 }
00169
00170
00171
00172
00173
00174
00175 function isAtom() {
00176 return $this->accepts('atom');
00177 }
00178
00179
00180
00181
00182
00183
00184 function isPost() {
00185 return (strtolower(env('REQUEST_METHOD')) == 'post');
00186 }
00187
00188
00189
00190
00191
00192
00193 function isPut() {
00194 return (strtolower(env('REQUEST_METHOD')) == 'put');
00195 }
00196
00197
00198
00199
00200
00201
00202 function isGet() {
00203 return (strtolower(env('REQUEST_METHOD')) == 'get');
00204 }
00205
00206
00207
00208
00209
00210
00211 function isDelete() {
00212 return (strtolower(env('REQUEST_METHOD')) == 'delete');
00213 }
00214
00215
00216
00217
00218
00219
00220
00221 function getAjaxVersion() {
00222 if (env('HTTP_X_PROTOTYPE_VERSION') != null) {
00223 return env('HTTP_X_PROTOTYPE_VERSION');
00224 }
00225 return false;
00226 }
00227
00228
00229
00230
00231
00232
00233
00234
00235 function setContent($name, $type) {
00236 $this->__requestContent[$name] = $type;
00237 }
00238
00239
00240
00241
00242
00243
00244 function getReferrer() {
00245 if (env('HTTP_HOST') != null) {
00246 $sess_host = env('HTTP_HOST');
00247 }
00248
00249 if (env('HTTP_X_FORWARDED_HOST') != null) {
00250 $sess_host = env('HTTP_X_FORWARDED_HOST');
00251 }
00252 return trim(preg_replace('/:.*/', '', $sess_host));
00253 }
00254
00255
00256
00257
00258
00259
00260 function getClientIP() {
00261 if (env('HTTP_X_FORWARDED_FOR') != null) {
00262 $ipaddr = preg_replace('/,.*/', '', env('HTTP_X_FORWARDED_FOR'));
00263 } else {
00264 if (env('HTTP_CLIENT_IP') != null) {
00265 $ipaddr = env('HTTP_CLIENT_IP');
00266 } else {
00267 $ipaddr = env('REMOTE_ADDR');
00268 }
00269 }
00270
00271 if (env('HTTP_CLIENTADDRESS') != null) {
00272 $tmpipaddr = env('HTTP_CLIENTADDRESS');
00273
00274 if (!empty($tmpipaddr)) {
00275 $ipaddr = preg_replace('/,.*/', '', $tmpipaddr);
00276 }
00277 }
00278 return trim($ipaddr);
00279 }
00280
00281
00282
00283
00284
00285
00286 function isMobile() {
00287 return (preg_match('/' . REQUEST_MOBILE_UA . '/i', env('HTTP_USER_AGENT')) > 0);
00288 }
00289
00290
00291
00292
00293
00294
00295
00296 function stripWhitespace($str) {
00297 $r = preg_replace('/[\n\r\t]+/', '', $str);
00298 return preg_replace('/\s{2,}/', ' ', $r);
00299 }
00300
00301
00302
00303
00304
00305
00306
00307 function stripImages($str) {
00308 $str = preg_replace('/(<a[^>]*>)(<img[^>]+alt=")([^"]*)("[^>]*>)(<\/a>)/i', '$1$3$5<br />', $str);
00309 $str = preg_replace('/(<img[^>]+alt=")([^"]*)("[^>]*>)/i', '$2<br />', $str);
00310 $str = preg_replace('/<img[^>]*>/i', '', $str);
00311 return $str;
00312 }
00313
00314
00315
00316
00317
00318
00319
00320 function stripScripts($str) {
00321 return preg_replace('/(<link[^>]+rel="[^"]*stylesheet"[^>]*>|<img[^>]*>|style="[^"]*")|<script[^>]*>.*?<\/script>|<style[^>]*>.*?<\/style>|<!--.*?-->/i', '', $str);
00322 }
00323
00324
00325
00326
00327
00328
00329
00330 function stripAll($str) {
00331 $str = $this->stripWhitespace($str);
00332 $str = $this->stripImages($str);
00333 $str = $this->stripScripts($str);
00334 return $str;
00335 }
00336
00337
00338
00339
00340
00341
00342 function stripTags() {
00343 $params = params(func_get_args());
00344 $str = $params[0];
00345
00346 for ($i = 1; $i < count($params); $i++) {
00347 $str = preg_replace('/<' . $params[$i] . '[^>]*>/i', '', $str);
00348 $str = preg_replace('/<\/' . $params[$i] . '[^>]*>/i', '', $str);
00349 }
00350 return $str;
00351 }
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 function accepts($type = null) {
00365 if ($type == null) {
00366 return $this->__acceptTypes;
00367 } elseif (is_array($type)) {
00368 foreach ($type as $t) {
00369 if ($this->accepts($t) == true) {
00370 return true;
00371 }
00372 }
00373 return false;
00374 } elseif (is_string($type)) {
00375
00376 if ($type == 'html' && $this->__acceptTypes === array('*
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406 function prefers($type = null) {
00407 if ($type == null) {
00408 return $this->accepts(null);
00409 }
00410 }
00411 }
00412 ?>