예제 #1
0
파일: Request.php 프로젝트: tokushima/ebi
 public function __construct()
 {
     if ('' != ($pathinfo = array_key_exists('PATH_INFO', $_SERVER) ? $_SERVER['PATH_INFO'] : '')) {
         if ($pathinfo[0] != '/') {
             $pathinfo = '/' . $pathinfo;
         }
         $this->args = preg_replace("/(.*?)\\?.*/", "\\1", $pathinfo);
     }
     $this->_method = self::method();
     if (isset($this->_method)) {
         if ($this->_method == 'POST') {
             if (isset($_POST) && is_array($_POST)) {
                 foreach ($_POST as $k => $v) {
                     $this->vars[$k] = get_magic_quotes_gpc() && is_string($v) ? stripslashes($v) : $v;
                 }
             }
             if (isset($_FILES) && is_array($_FILES)) {
                 $marge_func = function ($arr, $pk, $files, &$map) use(&$marge_func) {
                     if (is_array($arr)) {
                         foreach ($arr as $k => $v) {
                             $marge_func($v, array_merge($pk, [$k]), $files, $map);
                         }
                     } else {
                         $ks = implode('', array_map(function ($v) {
                             return '[\'' . $v . '\']';
                         }, $pk));
                         $eval = 'if(isset($files[\'tmp_name\']' . $ks . ') && !empty($files[\'tmp_name\']' . $ks . ')){ ';
                         foreach (['name', 'type', 'tmp_name', 'tmp_name', 'size'] as $k) {
                             $eval .= '$map' . $ks . '[\'' . $k . '\']=$files[\'' . $k . '\']' . $ks . ';';
                         }
                         eval($eval . '}');
                     }
                 };
                 foreach ($_FILES as $k => $v) {
                     if (is_array($v['name'])) {
                         $this->files[$k] = [];
                         $marge_func($v['name'], [], $v, $this->files[$k]);
                     } else {
                         if (array_key_exists('tmp_name', $v) && !empty($v['tmp_name'])) {
                             $this->files[$k] = $v;
                         }
                     }
                 }
             }
         } else {
             if (isset($_GET) && is_array($_GET)) {
                 foreach ($_GET as $k => $v) {
                     $this->vars[$k] = get_magic_quotes_gpc() && is_string($v) ? stripslashes($v) : $v;
                 }
             }
         }
         if (isset($_COOKIE) && is_array($_COOKIE)) {
             foreach ($_COOKIE as $k => $v) {
                 if (ctype_alpha($k[0]) && $k != \ebi\Conf::session_name()) {
                     $this->vars[$k] = $v;
                 }
             }
         }
         if (array_key_exists('_method', $this->vars)) {
             if (empty($this->_method)) {
                 $this->_method = strtoupper($this->vars['_method']);
             }
             unset($this->vars['_method']);
         }
         if (($this->_method == 'POST' || $this->_method == 'PUT' || $this->_method == 'DELETE') && (array_key_exists('HTTP_CONTENT_TYPE', $_SERVER) && strpos($_SERVER['HTTP_CONTENT_TYPE'], 'application/json') === 0 || array_key_exists('CONTENT_TYPE', $_SERVER) && strpos($_SERVER['CONTENT_TYPE'], 'application/json') === 0)) {
             $json = json_decode(file_get_contents('php://input'), true);
             if (is_array($json)) {
                 foreach ($json as $k => $v) {
                     $this->vars[$k] = $v;
                 }
             }
         }
     } else {
         if (isset($_SERVER['argv'])) {
             $argv = $_SERVER['argv'];
             array_shift($argv);
             if (isset($argv[0]) && $argv[0][0] != '-') {
                 $this->args = implode(' ', $argv);
             } else {
                 $size = sizeof($argv);
                 for ($i = 0; $i < $size; $i++) {
                     if ($argv[$i][0] == '-') {
                         if (isset($argv[$i + 1]) && $argv[$i + 1][0] != '-') {
                             $this->vars[substr($argv[$i], 1)] = $argv[$i + 1];
                             $i++;
                         } else {
                             $this->vars[substr($argv[$i], 1)] = '';
                         }
                     }
                 }
             }
         }
     }
 }