示例#1
0
 protected function setRequestOptions($url, $method, $vars, $files = null)
 {
     $purl = parse_url($url);
     if ($purl['scheme'] == 'https') {
         curl_setopt($this->request, CURLOPT_PORT, empty($purl['port']) ? 443 : $purl['port']);
         if ($this->validate_ssl) {
             curl_setopt($this->request, CURLOPT_SSL_VERIFYPEER, true);
         } elseif ($this->validate_ssl_host) {
             curl_setopt($this->request, CURLOPT_SSL_VERIFYPEER, false);
             curl_setopt($this->request, CURLOPT_SSL_VERIFYHOST, 0);
         } else {
             curl_setopt($this->request, CURLOPT_SSL_VERIFYPEER, false);
             curl_setopt($this->request, CURLOPT_SSL_VERIFYHOST, 2);
         }
     }
     $method = strtoupper($method);
     switch ($method) {
         case 'HEAD':
             curl_setopt($this->request, CURLOPT_NOBODY, true);
             break;
         case 'GET':
             curl_setopt($this->request, CURLOPT_HTTPGET, true);
             break;
         case 'PUT':
             curl_setopt($this->request, CURLOPT_CUSTOMREQUEST, "PUT");
             $toPost = true;
             break;
         case 'POST':
             curl_setopt($this->request, CURLOPT_POST, true);
             $toPost = true;
             break;
         default:
             curl_setopt($this->request, CURLOPT_CUSTOMREQUEST, $method);
     }
     curl_setopt($this->request, CURLOPT_URL, $url);
     if ($files || !empty($vars)) {
         if (!$toPost) {
             throw new \InvalidArgumentException('POST-vars may only be set for a POST or PUT Request.');
         }
         if (!is_array($vars)) {
             curl_setopt($this->request, CURLOPT_POSTFIELDS, $vars);
         } elseif ($files) {
             foreach ($files as &$file) {
                 if ($file[0] != '@') {
                     $file = '@' . $file;
                 }
             }
             unset($file);
             curl_setopt($this->request, CURLOPT_POSTFIELDS, Arrays::merge($files, $vars));
         } else {
             curl_setopt($this->request, CURLOPT_POSTFIELDS, Http::buildQuery($vars));
         }
     } elseif ($toPost) {
         throw new \InvalidArgumentException('POST-vars must be set for a POST-Request.');
     }
     # Set some default CURL options
     curl_setopt($this->request, CURLOPT_HEADER, true);
     curl_setopt($this->request, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($this->request, CURLOPT_USERAGENT, $this->user_agent);
     curl_setopt($this->request, CURLOPT_TIMEOUT, $this->timeout);
     if ($this->cookie_file) {
         curl_setopt($this->request, CURLOPT_COOKIEFILE, $this->cookie_file);
         curl_setopt($this->request, CURLOPT_COOKIEJAR, $this->cookie_file);
     }
     /* relative paths fix, see requestFix
     		if ($this->follow_redirects){
     			curl_setopt($this->request, CURLOPT_FOLLOWLOCATION, true);
     		}
     		*/
     if ($this->referrer) {
         curl_setopt($this->request, CURLOPT_REFERER, $this->referrer);
     }
     # Set any custom CURL options
     foreach ($this->options as $option => $value) {
         curl_setopt($this->request, $option, $value);
     }
 }
示例#2
0
 /** adds file and rules to ruleSets and parses all active rules in current file and former files
 	@param	path	str	file location string
 	*/
 function matchRules($path, &$rules)
 {
     foreach ($rules as $ruleKey => &$rule) {
         if (!$rule) {
             # rule may have been flagged "once"
             continue;
         }
         unset($matched);
         if (!isset($rule['flags'])) {
             $flags = $rule[2] ? explode(',', $rule[2]) : array();
             $rule['flags'] = array_fill_keys(array_values($flags), true);
             //parse flags for determining match string
             if ($rule['flags']['regex']) {
                 $rule['matcher'] = \Grithin\Strings::pregDelimit($rule[0]);
                 if ($rule['flags']['caseless']) {
                     $rule['matcher'] .= 'i';
                 }
             } else {
                 if ($rule['flags']['caseless']) {
                     $rule['matcher'] = strtolower($rule[0]);
                 } else {
                     $rule['matcher'] = $rule[0];
                 }
             }
         }
         if ($rule['flags']['caseless']) {
             $subject = $this->caselessPath;
         } else {
             $subject = $this->path;
         }
         //test match
         if ($rule['flags']['regex']) {
             if (preg_match($rule['matcher'], $subject, $this->regexMatch)) {
                 $matched = true;
             }
         } else {
             if ($rule['matcher'] == $subject) {
                 $matched = true;
             }
         }
         if ($matched) {
             if ($this->debug) {
                 Debug::log(['Matched Rule', $rule], ['title' => 'Route']);
             }
             $this->matchedRules[] = $rule;
             //++ apply replacement logic {
             if ($rule['flags']['regex']) {
                 if (is_callable($rule[1])) {
                     $this->matcher = $rule['matcher'];
                     $bound = new Bound($rule[1], [$this]);
                 } else {
                     $bound = new Bound('\\Grithin\\Route::regexReplacer', [$rule[1]]);
                 }
                 $replacement = preg_replace_callback($rule['matcher'], $bound, $this->path, 1);
             } else {
                 if (is_callable($rule[1])) {
                     $this->matcher = $rule['matcher'];
                     $replacement = call_user_func($rule[1], $this);
                 } else {
                     $replacement = $rule[1];
                 }
             }
             //handle redirects
             if ($rule['flags'][301]) {
                 $httpRedirect = 301;
             }
             if ($rule['flags'][303]) {
                 $httpRedirect = 303;
             }
             if ($rule['flags'][307]) {
                 $httpRedirect = 307;
             }
             if ($httpRedirect) {
                 if ($rule['flags']['params']) {
                     $replacement = Http::appendsUrl(Http::parseQuery($_SERVER['QUERY_STRING']), $replacement);
                 }
                 Http::redirect($replacement, 'head', $httpRedirect);
             }
             //remake url with replacement
             $this->applyPath($replacement);
             $this->parsedTokens = [];
             $this->unparsedTokens = array_merge([''], $this->tokens);
             //++ }
             //++ apply parse flag {
             if ($rule['flags']['once']) {
                 $rules[$ruleKey] = null;
             } elseif ($rule['flags']['file:last']) {
                 $this->ruleSets[$path] = [];
             } elseif ($rule['flags']['last']) {
                 $this->unparsedTokens = [];
             }
             //++ }
             return true;
         }
     }
     unset($rule);
     return false;
 }
示例#3
0
文件: Http.php 项目: grithin/phpbase
 static function redirectHttps($location)
 {
     Http::redirect("https://{$_SERVER['HTTP_HOST']}" . $location);
 }