/** * @module org.rhaco.store.db.Dbc * @param string $name * @param string $host * @param number $port * @param string $user * @param string $password * @param string $sock * @param boolean $autocommit * @see org\rhaco\store\db\module.Base::connect() */ public function connect($name, $host, $port, $user, $password, $sock, $autocommit) { if (!extension_loaded('pdo_sqlite')) { throw new \RuntimeException('pdo_sqlite not supported'); } $con = $path = null; if (empty($host)) { $host = \org\rhaco\Conf::get('host'); if (empty($host)) { $host = empty($name) ? ':memory:' : getcwd(); } } if ($host != ':memory:') { $host = str_replace('\\', '/', $host); if (substr($host, -1) != '/') { $host = $host . '/'; } $path = \org\rhaco\net\Path::absolute($host, $name); \org\rhaco\io\File::mkdir(dirname($path)); } try { $con = new \PDO(sprintf('sqlite:%s', $host == ':memory:' ? ':memory:' : $host . $name)); } catch (\PDOException $e) { throw new \org\rhaco\store\db\exception\ConnectionException($e->getMessage()); } return $con; }
protected function send($level, \org\rhaco\Log $log) { if (empty($this->template_base)) { $this->template_base = \org\rhaco\Conf::get('template_base', \org\rhaco\io\File::resource_path('log_mail')); } $template = \org\rhaco\net\Path::absolute($this->template_base, $level . '_log.xml'); if (is_file($template)) { $mail = new \org\rhaco\net\mail\Mail(); $mail->send_template($template, array('log' => $log, 'env' => new \org\rhaco\lang\Env())); } }
protected function __init__() { $name = $summary = $description = null; $d = debug_backtrace(false); $d = array_pop($d); $this->vars('app_mode', \org\rhaco\Conf::appmode()); $this->vars('f', new Dt\Helper()); $this->vars('has_coverage', function_exists('xdebug_get_code_coverage')); $this->vars('has_test', is_dir(getcwd() . '/test')); $this->vars('media_url', \org\rhaco\net\Path::slash(\org\rhaco\Conf::get('media_url'), null, false)); }
/** * アプリケーションのURLを返す * @param string $url ベースのURLに続く相対パス * @retunr string */ public function app_url($url = null) { return \org\rhaco\net\Path::absolute($this->app_url, $url); }
<?php eq("./overview.html", \org\rhaco\net\Path::relative("http://www.rhaco.org/doc/ja/", "http://www.rhaco.org/doc/ja/overview.html")); eq("../overview.html", \org\rhaco\net\Path::relative("http://www.rhaco.org/doc/ja/", "http://www.rhaco.org/doc/overview.html")); eq("../../overview.html", \org\rhaco\net\Path::relative("http://www.rhaco.org/doc/ja/", "http://www.rhaco.org/overview.html")); eq("../en/overview.html", \org\rhaco\net\Path::relative("http://www.rhaco.org/doc/ja/", "http://www.rhaco.org/doc/en/overview.html")); eq("./doc/ja/overview.html", \org\rhaco\net\Path::relative("http://www.rhaco.org/", "http://www.rhaco.org/doc/ja/overview.html")); eq("./ja/overview.html", \org\rhaco\net\Path::relative("http://www.rhaco.org/doc/", "http://www.rhaco.org/doc/ja/overview.html")); eq("http://www.goesby.com/user.php/rhaco", \org\rhaco\net\Path::relative("http://www.rhaco.org/doc/ja/", "http://www.goesby.com/user.php/rhaco")); eq("./doc/ja/overview.html", \org\rhaco\net\Path::relative("/www.rhaco.org/", "/www.rhaco.org/doc/ja/overview.html")); eq("./ja/overview.html", \org\rhaco\net\Path::relative("/www.rhaco.org/doc/", "/www.rhaco.org/doc/ja/overview.html")); eq("/www.goesby.com/user.php/rhaco", \org\rhaco\net\Path::relative("/www.rhaco.org/doc/ja/", "/www.goesby.com/user.php/rhaco")); eq("./ja/overview.html", \org\rhaco\net\Path::relative("c:/www.rhaco.org/doc/", "c:/www.rhaco.org/doc/ja/overview.html")); eq("c:/www.goesby.com/user.php/rhaco", \org\rhaco\net\Path::relative("c:/www.rhaco.org/doc/ja/", "c:/www.goesby.com/user.php/rhaco")); eq("./Documents/workspace/prhagger/__settings__.php", \org\rhaco\net\Path::relative("/Users/kaz/", "/Users/kaz/Documents/workspace/prhagger/__settings__.php")); eq("./", \org\rhaco\net\Path::relative("C:/xampp/htdocs/rhaco/test/template/sub", "C:/xampp/htdocs/rhaco/test/template/sub")); eq("./", \org\rhaco\net\Path::relative('C:\\xampp\\htdocs\\rhaco\\test\\template\\sub', 'C:\\xampp\\htdocs\\rhaco\\test\\template\\sub'));
eq("http://www.rhaco.org/doc/index.html", \org\rhaco\net\Path::absolute("http://www.rhaco.org/doc/ja/", "../index.html")); eq("http://www.rhaco.org/index.html", \org\rhaco\net\Path::absolute("http://www.rhaco.org/doc/ja/", "../../index.html")); eq("http://www.rhaco.org/index.html", \org\rhaco\net\Path::absolute("http://www.rhaco.org/doc/ja/", "../././.././index.html")); eq("/www/rhaco/index.html", \org\rhaco\net\Path::absolute("/www/rhaco/", "index.html")); eq("/www.rhaco.org/doc/ja/index.html", \org\rhaco\net\Path::absolute("/www.rhaco.org/doc/ja/", "index.html")); eq("/www.rhaco.org/doc/index.html", \org\rhaco\net\Path::absolute("/www.rhaco.org/doc/ja/", "../index.html")); eq("/www.rhaco.org/doc/ja/action.html/index.html", \org\rhaco\net\Path::absolute('/www.rhaco.org/doc/ja/action.html', 'index.html')); eq("/www.rhaco.org/index.html", \org\rhaco\net\Path::absolute("/www.rhaco.org/doc/ja/", "../../index.html")); eq("/www.rhaco.org/index.html", \org\rhaco\net\Path::absolute("/www.rhaco.org/doc/ja/", "../././.././index.html")); eq("c:/www.rhaco.org/doc/index.html", \org\rhaco\net\Path::absolute("c:/www.rhaco.org/doc/ja/", "../index.html")); eq("http://www.rhaco.org/index.html", \org\rhaco\net\Path::absolute("http://www.rhaco.org/doc/ja", "/index.html")); eq("http://www.rhaco.org/doc/ja/index.html", \org\rhaco\net\Path::absolute('http://www.rhaco.org/doc/ja/action.html', 'index.html')); eq("http://www.rhaco.org/doc/ja/sample.cgi?param=test", \org\rhaco\net\Path::absolute('http://www.rhaco.org/doc/ja/sample.cgi?query=key', '?param=test')); eq("http://www.rhaco.org/doc/index.html", \org\rhaco\net\Path::absolute('http://www.rhaco.org/doc/ja/action.html', '../../index.html')); eq("http://www.rhaco.org/?param=test", \org\rhaco\net\Path::absolute('http://www.rhaco.org/doc/ja/sample.cgi?query=key', '../../../?param=test')); eq("/doc/ja/index.html", \org\rhaco\net\Path::absolute('/', "/doc/ja/index.html")); eq("/index.html", \org\rhaco\net\Path::absolute('/', "index.html")); eq("http://www.rhaco.org/login", \org\rhaco\net\Path::absolute("http://www.rhaco.org", "/login")); eq("http://www.rhaco.org/login", \org\rhaco\net\Path::absolute("http://www.rhaco.org/login", "")); eq("http://www.rhaco.org/login.cgi", \org\rhaco\net\Path::absolute("http://www.rhaco.org/logout.cgi", "login.cgi")); eq("http://www.rhaco.org/hoge/login.cgi", \org\rhaco\net\Path::absolute("http://www.rhaco.org/hoge/logout.cgi", "login.cgi")); eq("http://www.rhaco.org/hoge/login.cgi", \org\rhaco\net\Path::absolute("http://www.rhaco.org/hoge/#abc/aa", "login.cgi")); eq("http://www.rhaco.org/hoge/abc.html#login", \org\rhaco\net\Path::absolute("http://www.rhaco.org/hoge/abc.html", "#login")); eq("http://www.rhaco.org/hoge/abc.html#login", \org\rhaco\net\Path::absolute("http://www.rhaco.org/hoge/abc.html#logout", "#login")); eq("http://www.rhaco.org/hoge/abc.html?abc=aa#login", \org\rhaco\net\Path::absolute("http://www.rhaco.org/hoge/abc.html?abc=aa#logout", "#login")); eq("javascript::alert('')", \org\rhaco\net\Path::absolute("http://www.rhaco.org/hoge/abc.html", "javascript::alert('')")); eq("mailto::hoge@rhaco.org", \org\rhaco\net\Path::absolute("http://www.rhaco.org/hoge/abc.html", "mailto::hoge@rhaco.org")); eq("http://www.rhaco.org/hoge/login.cgi", \org\rhaco\net\Path::absolute("http://www.rhaco.org/hoge/?aa=bb/", "login.cgi")); eq("http://www.rhaco.org/login", \org\rhaco\net\Path::absolute("http://rhaco.org/hoge/hoge", "http://www.rhaco.org/login")); eq("http://localhost:8888/spec/css/style.css", \org\rhaco\net\Path::absolute("http://localhost:8888/spec/", "./css/style.css"));
private function base() { $base = Path::slash(\org\rhaco\Conf::get('document_root'), null, true); return $base . Path::slash($this->map_arg('path'), null, true); }
private function print_template($template_path, $template, $media_url, $put_block, $obj, $apps, $index, $path_replace = true) { if ($path_replace) { if (isset($apps[$index]['media_path'])) { $media_url = $media_url . \org\rhaco\net\Path::slash($apps[$index]['media_path'], true, false); } if (isset($apps[$index]['template_path'])) { $template_path = $template_path . \org\rhaco\net\Path::slash($apps[$index]['template_path'], false, true); } } if (!empty($put_block)) { $this->template->put_block(\org\rhaco\net\Path::absolute($this->template_path, $put_block)); } if (isset($apps[$index]['template_super'])) { $this->template->template_super($this->template_path . $apps[$index]['template_super']); } if (is_array($obj) && isset($obj[0]) && isset($obj[1])) { foreach (is_array($obj[1]) ? $obj[1] : array($obj[1]) as $o) { $this->template->set_object_module($o); } $obj = $obj[0]; } $this->template->media_url($media_url); $this->template->cp($obj); if (isset($apps[$index]['vars'])) { $this->template->cp($apps[$index]['vars']); } $this->template->vars('t', new \org\rhaco\flow\module\Helper($this->app_url, $media_url, $apps[$index]['name'], $apps[$index]['num'], $this->entry_file(), $apps, $obj)); $src = $this->template->read(\org\rhaco\net\Path::absolute($template_path, $template)); /** * テンプレートの出力 * @param org.rhaco.lang.Str $obj */ $this->object_module('before_flow_print_template', \org\rhaco\lang\Str::ref($obj, $src)); $src = (string) $obj; header('Content-Length: ' . strlen($src)); print $src; exit; }
/** * テンプレートから内容を取得しセットする * * テンプレートサンプル * <mail> * <from address="*****@*****.**" name="tokushima" /> * <subject>メールのタイトル</subject> * <body> * メールの本文 * </body> * </mail> * * @param string $template_path テンプレートファイルパス * @param mixed{} $vars テンプレートへ渡す変数 * @return $this */ public function set_template($template_path, $vars = array()) { $resource_path = empty($this->resource_path) ? \org\rhaco\Conf::get('resource_path', \org\rhaco\io\File::resource_path('mail')) : $this->resource_path; $template_path = \org\rhaco\net\Path::absolute($resource_path, $template_path); if (!is_file($template_path)) { throw new \InvalidArgumentException($template_path . ' not found'); } if (\org\rhaco\Xml::set($xml, file_get_contents($template_path), 'mail')) { $from = $xml->f('from'); if ($from !== null) { $this->from($from->in_attr('address'), $from->in_attr('name')); } foreach ($xml->in('to') as $to) { $this->to($to->in_attr('address'), $to->in_attr('name')); } $return_path = $xml->f('return_path'); if ($return_path !== null) { $this->return_path($return_path->in_attr('address')); } $notification = $xml->f('notification'); if ($notification !== null) { $this->notification($notification->in_attr('address')); } $reply_to = $xml->f('reply_to'); if ($reply_to !== null) { $this->reply_to($reply_to->in_attr('address')); } $errors_to = $xml->f('errors_to'); if ($errors_to !== null) { $this->errors_to($errors_to->in_attr('address')); } $subject = trim(str_replace(array("\r\n", "\r", "\n"), '', $xml->f('subject.value()'))); $body = $xml->f('body.value()'); $template = new \org\rhaco\Template(); $template->cp($vars); $template->vars('t', new \org\rhaco\flow\module\Helper()); $this->subject($template->get($subject)); $this->message(\org\rhaco\lang\Text::plain("\n" . $template->get($body) . "\n")); $html = $xml->f('html'); if ($html !== null) { $html_path = \org\rhaco\net\Path::absolute($resource_path, $html->in_attr('src')); foreach ($html->in('media') as $media) { $file = \org\rhaco\net\Path::absolute($resource_path, $media->in_attr('src')); if (!is_file($file)) { throw new \InvalidArgumentException($media->in_attr('src') . ' invalid media'); } $this->media($media->in_attr('src'), file_get_contents($file)); } $template = new \org\rhaco\Template(); $template->cp($vars); $template->vars('t', new \org\rhaco\flow\module\Helper()); $this->html($template->read($html_path)); } foreach ($xml->in('attach') as $attach) { $file = \org\rhaco\net\Path::absolute($resource_path, $attach->in_attr('src')); if (!is_file($file)) { throw new \InvalidArgumentException($attach->in_attr('src') . ' invalid media'); } $this->attach($attach->in_attr('name', $attach->in_attr('src')), file_get_contents($file)); } return $this; } throw new \InvalidArgumentException($template_path . ' invalid data'); }
/** * ファイルの実際のパスを返す * @conf string $base_path ストレージのベースパス * @param string $path ベースパスからの相対パス * @return string */ public static function get_path($path) { $base_path = \org\rhaco\Conf::get('base_path'); if (empty($base_path)) { throw new \org\rhaco\io\Storage\StorageException('ベースパスが指定されていません'); } return \org\rhaco\net\Path::absolute(\org\rhaco\net\Path::slash($base_path, null, true), $path); }
/** * @module org.rhaco.net.listener.SocketListener * @param org.rhaco.net.listener.Channel $channel */ public function connect($channel) { $head = $body = null; $method = $uri = $query = $boundary = null; $POST = $GET = $FILES = $SERVER = array(); $message_len = $null_cnt = 0; try { $uid = uniqid(''); while (true) { $message = $channel->read(); if ($message === '') { $null_cnt++; } if ($null_cnt > 5) { break; } if ($method === null) { $head .= $message; if (substr($head, -4) === "\r\n\r\n") { $lines = explode("\n", trim($head)); if (!empty($lines)) { $exp = explode(' ', array_shift($lines)); if (sizeof($exp) >= 2) { list($method, $uri) = $exp; } if (strpos($uri, '?')) { list($uri, $SERVER['QUERY_STRING']) = explode('?', $uri); parse_str($SERVER['QUERY_STRING'], $GET); } foreach ($lines as $line) { $exp = explode(':', $line, 2); if (sizeof($exp) == 2) { list($name, $value) = $exp; $SERVER['HTTP_' . str_replace(array('-'), array('_'), strtoupper(trim($name)))] = trim($value); } } } if ($method === null || $method == 'GET') { break; } if (isset($SERVER['HTTP_CONTENT_TYPE']) && preg_match("/multipart\\/form-data; boundary=(.+)\$/", $SERVER['HTTP_CONTENT_TYPE'], $m)) { $boundary = '--' . $m[1]; } } } else { if ($method == 'POST') { $message_len += strlen($message); $body .= $message; if (isset($SERVER['HTTP_CONTENT_LENGTH']) && $message_len >= $SERVER['HTTP_CONTENT_LENGTH'] || !isset($SERVER['HTTP_CONTENT_LENGTH']) && substr($body, -4) === "\r\n\r\n") { if (isset($boundary)) { list($body) = explode($boundary . "--\r\n", $body, 2); foreach (explode($boundary . "\r\n", $body) as $k => $block) { if (!empty($block)) { list($h, $b) = explode("\r\n\r\n", $block); list($b) = explode("\r\n", $b, 2); if (preg_match("/\\sname=([\"'])(.+?)\\1/", $h, $m)) { $name = $m[2]; if (preg_match("/filename=([\"'])(.+?)\\1/", $h, $m)) { $tmp_name = self::work_path($uid, $k); \org\rhaco\io\File::write($tmp_name, $b); $FILES[$name] = array('name' => $m[2], 'tmp_name' => $tmp_name, 'size' => filesize($tmp_name), 'error' => 0); } else { $POST[$name] = $b; } } } } } else { parse_str($body, $POST); } break; } else { if (!isset($SERVER['HTTP_CONTENT_LENGTH'])) { break; } } } else { $this->output('Unknown method: ' . $method); break; } } } if (!empty($uri)) { $request_uri = $uri; $this->output('request uri: ' . $uri); $uri = preg_replace("/\\/+/", "/", $uri); $uri_path = \org\rhaco\net\Path::absolute(getcwd(), \org\rhaco\net\Path::slash($uri, false, null)); if (strpos($uri, '.php') === false && !is_file($uri_path) && $uri != '/favicon.ico') { $exp = explode('/', \org\rhaco\net\Path::slash($uri, false, null), 2); if (is_file(\org\rhaco\net\Path::absolute(getcwd(), $exp[0] . '.php')) && isset($exp[1])) { $uri = '/' . $exp[0] . '.php/' . $exp[1]; } else { if (is_file(\org\rhaco\net\Path::absolute(getcwd(), 'index.php'))) { $uri = '/index.php' . $uri; } } } if ($request_uri != $uri) { $this->output(' - rewrite uri: ' . $uri); } if (strpos($uri, '.php/') !== false) { $exp = explode('.php/', $uri, 2); $uri = $exp[0] . '.php'; $SERVER['PATH_INFO'] = '/' . $exp[1]; } $path = \org\rhaco\net\Path::absolute(getcwd(), \org\rhaco\net\Path::slash($uri, false, null)); if (is_file($path)) { $file = new \org\rhaco\io\File($path); if (substr($path, -4) == '.php') { $file->mime('text/html'); } $headers = array(); $headers[] = 'Content-Type: ' . $file->mime(); $headers[] = 'Connection: close'; if (substr($path, -4) == '.php') { $SERVER['REQUEST_METHOD'] = $method; $REQUEST = array('_SERVER' => $SERVER, '_POST' => $POST, '_GET' => $GET, '_FILES' => $FILES); \org\rhaco\io\File::write(self::work_path($uid, 'request'), serialize($REQUEST)); $exec_command = $this->php_cmd . ' ' . $file->fullname() . ' -emulator ' . $uid; $this->output(' -- ' . $exec_command); $cmd = new \org\rhaco\Command($exec_command); if (is_file(self::work_path($uid, 'header'))) { $send_header = unserialize(\org\rhaco\io\File::read(self::work_path($uid, 'header'))); if (!empty($send_header) && is_array($send_header)) { $headers = array_merge($headers, $send_header); } } foreach ($headers as $k => $v) { if (strpos($v, ':') === false) { $top = $headers[$k]; unset($headers[$k]); array_unshift($headers, $top); break; } } $output_header = trim(implode("\r\n", $headers)); if (strpos($output_header, 'HTTP/') !== 0) { if (strpos($output_header, 'Location: ') !== false) { $output_header = "HTTP/1.1 302 Found\r\n" . $output_header; } else { $output_header = "HTTP/1.1 200 OK\r\n" . $output_header; } } $channel->write($output_header . "\r\n\r\n"); $channel->write($cmd->stdout()); \org\rhaco\io\File::rm(self::work_path($uid)); } else { $channel->write("HTTP/1.1 200 OK\r\n" . implode("\r\n", $headers) . "\r\n\r\n"); $fp = fopen($file->fullname(), 'rb'); while (!feof($fp)) { $channel->write(fread($fp, 4096)); } fclose($fp); } } else { $this->output($path . ' not found'); $channel->write($this->error(404, 'Not Found', 'The requested URL ' . $uri . ' was not found on this server.')); } } } catch (\org\rhaco\net\listener\exception\ErrorException $e) { $this->output($e->getMessage()); } }
<?php eq("/abc/", \org\rhaco\net\Path::slash("/abc/", null, null)); eq("/abc/", \org\rhaco\net\Path::slash("abc", true, true)); eq("/abc/", \org\rhaco\net\Path::slash("/abc/", true, true)); eq("abc/", \org\rhaco\net\Path::slash("/abc/", false, true)); eq("/abc", \org\rhaco\net\Path::slash("/abc/", true, false)); eq("abc", \org\rhaco\net\Path::slash("/abc/", false, false));
private function request($method, $url, $download_path = null) { if (!isset($this->resource)) { $this->resource = curl_init(); } $url_info = parse_url($url); $cookie_base_domain = (isset($url_info['host']) ? $url_info['host'] : '') . (isset($url_info['path']) ? $url_info['path'] : ''); if (isset($url_info['query'])) { parse_str($url_info['query'], $vars); foreach ($vars as $k => $v) { if (!isset($this->request_vars[$k])) { $this->request_vars[$k] = $v; } } list($url) = explode('?', $url, 2); } switch ($method) { case 'RAW': case 'POST': curl_setopt($this->resource, CURLOPT_POST, true); break; case 'GET': curl_setopt($this->resource, CURLOPT_HTTPGET, true); break; case 'HEAD': curl_setopt($this->resource, CURLOPT_NOBODY, true); break; case 'PUT': curl_setopt($this->resource, CURLOPT_PUT, true); break; case 'DELETE': curl_setopt($this->resource, CURLOPT_CUSTOMREQUEST, 'DELETE'); break; } switch ($method) { case 'POST': if (!empty($this->request_file_vars)) { $vars = array(); if (!empty($this->request_vars)) { foreach (explode('&', http_build_query($this->request_vars)) as $q) { $s = explode('=', $q, 2); $vars[urldecode($s[0])] = isset($s[1]) ? urldecode($s[1]) : null; } } foreach (explode('&', http_build_query($this->request_file_vars)) as $q) { $s = explode('=', $q, 2); if (isset($s[1])) { if (!is_file($f = urldecode($s[1]))) { throw new \RuntimeException($f . ' not found'); } $vars[urldecode($s[0])] = class_exists('\\CURLFile', false) ? new \CURLFile($f) : '@' . $f; } } curl_setopt($this->resource, CURLOPT_POSTFIELDS, $vars); } else { curl_setopt($this->resource, CURLOPT_POSTFIELDS, http_build_query($this->request_vars)); } break; case 'RAW': $this->request_header['Content-Type'] = 'text/plain'; curl_setopt($this->resource, CURLOPT_POSTFIELDS, $this->raw); break; case 'GET': case 'HEAD': case 'PUT': case 'DELETE': $url = $url . (!empty($this->request_vars) ? '?' . http_build_query($this->request_vars) : ''); } curl_setopt($this->resource, CURLOPT_URL, $url); curl_setopt($this->resource, CURLOPT_FOLLOWLOCATION, false); curl_setopt($this->resource, CURLOPT_HEADER, false); curl_setopt($this->resource, CURLOPT_RETURNTRANSFER, false); curl_setopt($this->resource, CURLOPT_FORBID_REUSE, true); curl_setopt($this->resource, CURLOPT_FAILONERROR, false); curl_setopt($this->resource, CURLOPT_TIMEOUT, $this->timeout); if (!empty($this->user)) { curl_setopt($this->resource, CURLOPT_USERPWD, $this->user . ':' . $this->password); } if (!isset($this->request_header['Expect'])) { $this->request_header['Expect'] = null; } if (!isset($this->request_header['Cookie'])) { $cookies = ''; foreach ($this->cookie as $domain => $cookie_value) { if (strpos($cookie_base_domain, $domain) === 0 || strpos($cookie_base_domain, $domain[0] == '.' ? $domain : '.' . $domain) !== false) { foreach ($cookie_value as $k => $v) { if (!$v['secure'] || $v['secure'] && substr($url, 0, 8) == 'https://') { $cookies .= sprintf('%s=%s; ', $k, $v['value']); } } } } curl_setopt($this->resource, CURLOPT_COOKIE, $cookies); } if (!isset($this->request_header['User-Agent'])) { curl_setopt($this->resource, CURLOPT_USERAGENT, empty($this->agent) ? isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null : $this->agent); } if (!isset($this->request_header['Accept']) && isset($_SERVER['HTTP_ACCEPT'])) { $this->request_header['Accept'] = $_SERVER['HTTP_ACCEPT']; } if (!isset($this->request_header['Accept-Language']) && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { $this->request_header['Accept-Language'] = $_SERVER['HTTP_ACCEPT_LANGUAGE']; } if (!isset($this->request_header['Accept-Charset']) && isset($_SERVER['HTTP_ACCEPT_CHARSET'])) { $this->request_header['Accept-Charset'] = $_SERVER['HTTP_ACCEPT_CHARSET']; } curl_setopt($this->resource, CURLOPT_HTTPHEADER, array_map(function ($k, $v) { return $k . ': ' . $v; }, array_keys($this->request_header), $this->request_header)); curl_setopt($this->resource, CURLOPT_HEADERFUNCTION, array($this, 'callback_head')); if (empty($download_path)) { curl_setopt($this->resource, CURLOPT_WRITEFUNCTION, array($this, 'callback_body')); } else { if (!is_dir(dirname($download_path))) { mkdir(dirname($download_path), 0777, true); } $fp = fopen($download_path, 'wb'); curl_setopt($this->resource, CURLOPT_WRITEFUNCTION, function ($c, $data) use(&$fp) { if ($fp) { fwrite($fp, $data); } return strlen($data); }); } $this->request_header = $this->request_vars = array(); $this->head = $this->body = $this->raw = ''; curl_exec($this->resource); if (!empty($download_path) && $fp) { fclose($fp); } $this->url = curl_getinfo($this->resource, CURLINFO_EFFECTIVE_URL); $this->status = curl_getinfo($this->resource, CURLINFO_HTTP_CODE); if (($err_code = curl_errno($this->resource)) > 0) { if ($err_code == 47) { return $this; } throw new \RuntimeException($err_code . ': ' . curl_error($this->resource)); } if (preg_match_all('/Set-Cookie:[\\s]*(.+)/i', $this->head, $match)) { $unsetcookie = $setcookie = array(); foreach ($match[1] as $cookies) { $cookie_name = $cookie_value = $cookie_domain = $cookie_path = $cookie_expires = null; $cookie_domain = $cookie_base_domain; $cookie_path = '/'; $secure = false; foreach (explode(';', $cookies) as $cookie) { $cookie = trim($cookie); if (strpos($cookie, '=') !== false) { list($k, $v) = explode('=', $cookie, 2); $k = trim($k); $v = trim($v); switch (strtolower($k)) { case 'expires': $cookie_expires = ctype_digit($v) ? (int) $v : strtotime($v); break; case 'domain': $cookie_domain = preg_replace('/^[\\w]+:\\/\\/(.+)$/', '\\1', $v); break; case 'path': $cookie_path = $v; break; default: if (!isset($cookie_name)) { $cookie_name = $k; $cookie_value = $v; } } } else { if (strtolower($cookie) == 'secure') { $secure = true; } } } $cookie_domain = substr(\org\rhaco\net\Path::absolute('http://' . $cookie_domain, $cookie_path), 7); if ($cookie_expires !== null && $cookie_expires < time()) { if (isset($this->cookie[$cookie_domain][$cookie_name])) { unset($this->cookie[$cookie_domain][$cookie_name]); } } else { $this->cookie[$cookie_domain][$cookie_name] = array('value' => $cookie_value, 'expires' => $cookie_expires, 'secure' => $secure); } } } curl_close($this->resource); unset($this->resource); if ($this->redirect_count++ < $this->redirect_max) { switch ($this->status) { case 300: case 301: case 302: case 303: case 307: if (preg_match('/Location:[\\040](.*)/i', $this->head, $redirect_url)) { return $this->request('GET', trim(\org\rhaco\net\Path::absolute($url, $redirect_url[1])), $download_path); } } } $this->redirect_count = 1; return $this; }