function pkwk_session_start() { global $use_trans_sid_address; static $use_session; if (!isset($use_session)) { $use_session = intval(PLUS_ALLOW_SESSION); if ($use_session > 0) { if (!is_array($use_trans_sid_address)) { $use_trans_sid_address = array(); } if (in_the_net($use_trans_sid_address, $_SERVER['REMOTE_ADDR'])) { ini_set('session.use_cookies', 0); } else { ini_set('session.use_cookies', 1); ini_set('session.use_only_cookies', 1); } session_name('pukiwiki'); @session_start(); if (ini_get('session.use_cookies') == 0 && ini_get('session.use_trans_sid') == 0) { output_add_rewrite_var(session_name(), session_id()); } } } return $use_session; }
function http_req($url, $params = array()) { //init params $config = array('method' => 'GET', 'headers' => '', 'post' => array(), 'redirect_max' => 2, 'content_charset' => '', 'ua' => 'PHPScript', 'proxy_host' => '', 'no_proxy' => array('localhost', '127.0.0.0/8', '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16', 'no-proxy.com'), 'use_proxy' => FALSE, 'proxy_port' => '', 'need_proxy_auth' => 'FALSE', 'proxy_auth_user' => '', 'proxy_auth_pass' => ''); //update foreach ($config as $key => $val) { if (isset($params[$key])) { $config[$key] = $params[$key]; } } //make local values extract($config); if ($proxy_host != '') { if (!preg_match('#^[a-z]+://#', $proxy_host)) { $proxy_host = 'http://' . $proxy_host; } $proxy_params = parse_url($proxy_host); $proxy_host = $proxy_params['host']; $use_proxy = TRUE; $proxy_port = isset($proxy_params['port']) ? $proxy_params['port'] : '8080'; $need_proxy_auth = isset($proxy_params['user']); $proxy_auth_user = isset($proxy_params['user']) ? $proxy_params['user'] : ''; $proxy_auth_pass = isset($proxy_params['pass']) ? $proxy_params['pass'] : ''; } // var_dump($method, $headers, $post, $redirect_max, $content_charset, $ua); // var_dump($proxy_host, $use_proxy, $proxy_port, $need_proxy_auth, $proxy_auth_user, $proxy_auth_pass); $rc = array(); $arr = parse_url($url); $via_proxy = $use_proxy ? !in_the_net($no_proxy, $arr['host']) : FALSE; // query $arr['query'] = isset($arr['query']) ? '?' . $arr['query'] : ''; // port $arr['port'] = isset($arr['port']) ? $arr['port'] : 80; $url_base = $arr['scheme'] . '://' . $arr['host'] . ':' . $arr['port']; $url_path = isset($arr['path']) ? $arr['path'] : '/'; $url = ($via_proxy ? $url_base : '') . $url_path . $arr['query']; $query = $method . ' ' . $url . ' HTTP/1.0' . "\r\n"; $query .= 'Host: ' . $arr['host'] . "\r\n"; $query .= 'User-Agent: ' . $ua . "\r\n"; // Basic-auth for HTTP proxy server if ($need_proxy_auth && isset($proxy_auth_user) && isset($proxy_auth_pass)) { $query .= 'Proxy-Authorization: Basic ' . base64_encode($proxy_auth_user . ':' . $proxy_auth_pass) . "\r\n"; } // (Normal) Basic-auth for remote host if (isset($arr['user']) && isset($arr['pass'])) { $query .= 'Authorization: Basic ' . base64_encode($arr['user'] . ':' . $arr['pass']) . "\r\n"; } $query .= $headers; if (strtoupper($method) == 'POST') { // 'application/x-www-form-urlencoded', especially for TrackBack ping $POST = array(); foreach ($post as $name => $val) { $POST[] = $name . '=' . urlencode($val); } $data = join('&', $POST); if (preg_match('/^[a-zA-Z0-9_-]+$/', $content_charset)) { // Legacy but simple $query .= 'Content-Type: application/x-www-form-urlencoded' . "\r\n"; } else { // With charset (NOTE: Some implementation may hate this) $query .= 'Content-Type: application/x-www-form-urlencoded' . '; charset=' . strtolower($content_charset) . "\r\n"; } $query .= 'Content-Length: ' . strlen($data) . "\r\n"; $query .= "\r\n"; $query .= $data; } else { $query .= "\r\n"; } $errno = 0; $errstr = ''; $fp = fsockopen($via_proxy ? $proxy_host : $arr['host'], $via_proxy ? $proxy_port : $arr['port'], $errno, $errstr, 30); if ($fp === FALSE) { return array('query' => $query, 'rc' => $errno, 'header' => '', 'data' => $errstr); } fputs($fp, $query); $response = ''; while (!feof($fp)) { $response .= fread($fp, 4096); } fclose($fp); $resp = explode("\r\n\r\n", $response, 2); $rccd = explode(' ', $resp[0], 3); // array('HTTP/1.1', '200', 'OK\r\n...') $rc = (int) $rccd[1]; switch ($rc) { case 301: // Moved Permanently // Moved Permanently case 302: // Moved Temporarily $matches = array(); if (preg_match('/^Location: (.+)$/m', $resp[0], $matches) && --$redirect_max > 0) { $url = trim($matches[1]); if (!preg_match('/^https?:\\//', $url)) { // Relative path to Absolute if ($url[0] != '/') { $url = substr($url_path, 0, strrpos($url_path, '/')) . '/' . $url; } $url = $url_base . $url; // Add sheme, host } // Redirect return http_request($url, $method, $headers, $post, $redirect_max); } } return array('query' => $query, 'rc' => $rc, 'header' => $resp[0], 'data' => $resp[1]); }
function tb_get_url($url) { global $use_proxy, $no_proxy; // Don't go across HTTP-proxy server $parse_url = parse_url($url); if (empty($parse_url['host']) || $use_proxy && !in_the_net($no_proxy, $parse_url['host'])) { return ''; } $data = http_request($url); if ($data['rc'] !== 200) { return ''; } $matches = array(); if (!preg_match_all('#<rdf:RDF[^>]*xmlns:trackback=[^>]*>(.*?)</rdf:RDF>#si', $data['data'], $matches, PREG_PATTERN_ORDER)) { return ''; } $obj = new TrackBack_XML(); foreach ($matches[1] as $body) { $tb_url = $obj->parse($body, $url); if ($tb_url !== FALSE) { return $tb_url; } } return ''; }
function http_request($url, $method = 'GET', $headers = '', $post = array(), $redirect_max = PKWK_HTTP_REQUEST_URL_REDIRECT_MAX, $content_charset = '') { global $use_proxy, $no_proxy, $proxy_host, $proxy_port; global $need_proxy_auth, $proxy_auth_user, $proxy_auth_pass; $rc = array(); $arr = parse_url($url); $via_proxy = $use_proxy ? !in_the_net($no_proxy, $arr['host']) : FALSE; // query $arr['query'] = isset($arr['query']) ? '?' . $arr['query'] : ''; // port $arr['port'] = isset($arr['port']) ? $arr['port'] : 80; $url_base = $arr['scheme'] . '://' . $arr['host'] . ':' . $arr['port']; $url_path = isset($arr['path']) ? $arr['path'] : '/'; $url = ($via_proxy ? $url_base : '') . $url_path . $arr['query']; $query = $method . ' ' . $url . ' HTTP/1.0' . "\r\n"; $query .= 'Host: ' . $arr['host'] . "\r\n"; $query .= 'User-Agent: PukiWiki/' . S_VERSION . "\r\n"; // Basic-auth for HTTP proxy server if ($need_proxy_auth && isset($proxy_auth_user) && isset($proxy_auth_pass)) { $query .= 'Proxy-Authorization: Basic ' . base64_encode($proxy_auth_user . ':' . $proxy_auth_pass) . "\r\n"; } // (Normal) Basic-auth for remote host if (isset($arr['user']) && isset($arr['pass'])) { $query .= 'Authorization: Basic ' . base64_encode($arr['user'] . ':' . $arr['pass']) . "\r\n"; } $query .= $headers; if (strtoupper($method) == 'POST') { // 'application/x-www-form-urlencoded', especially for TrackBack ping $POST = array(); foreach ($post as $name => $val) { $POST[] = $name . '=' . urlencode($val); } $data = join('&', $POST); if (preg_match('/^[a-zA-Z0-9_-]+$/', $content_charset)) { // Legacy but simple $query .= 'Content-Type: application/x-www-form-urlencoded' . "\r\n"; } else { // With charset (NOTE: Some implementation may hate this) $query .= 'Content-Type: application/x-www-form-urlencoded' . '; charset=' . strtolower($content_charset) . "\r\n"; } $query .= 'Content-Length: ' . strlen($data) . "\r\n"; $query .= "\r\n"; $query .= $data; } else { $query .= "\r\n"; } if ($arr['scheme'] === 'https') { $arr['port'] = 443; $arr['host'] = "ssl://{$arr['host']}"; } $errno = 0; $errstr = ''; $fp = fsockopen($via_proxy ? $proxy_host : $arr['host'], $via_proxy ? $proxy_port : $arr['port'], $errno, $errstr, 30); if ($fp === FALSE) { return array('query' => $query, 'rc' => $errno, 'header' => '', 'data' => $errstr); } fputs($fp, $query); $response = ''; while (!feof($fp)) { $response .= fread($fp, 4096); } fclose($fp); $resp = explode("\r\n\r\n", $response, 2); $rccd = explode(' ', $resp[0], 3); // array('HTTP/1.1', '200', 'OK\r\n...') $rc = (int) $rccd[1]; switch ($rc) { case 301: // Moved Permanently // Moved Permanently case 302: // Moved Temporarily $matches = array(); if (preg_match('/^Location: (.+)$/m', $resp[0], $matches) && --$redirect_max > 0) { $url = trim($matches[1]); if (!preg_match('/^https?:\\//', $url)) { // Relative path to Absolute if ($url[0] != '/') { $url = substr($url_path, 0, strrpos($url_path, '/')) . '/' . $url; } $url = $url_base . $url; // Add sheme, host } // Redirect return http_request($url, $method, $headers, $post, $redirect_max); } } return array('query' => $query, 'rc' => $rc, 'header' => $resp[0], 'data' => $resp[1]); }
function http_req($url, $params = array()) { $config = array('method' => 'GET', 'headers' => '', 'post' => array(), 'redirect_max' => 2, 'content_charset' => '', 'ua' => 'PHPScript', 'proxy_host' => '', 'no_proxy' => array('localhost', '127.0.0.0/8', '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16', 'no-proxy.com'), 'use_proxy' => FALSE, 'proxy_port' => '', 'need_proxy_auth' => 'FALSE', 'proxy_auth_user' => '', 'proxy_auth_pass' => ''); foreach ($config as $key => $val) { if (isset($params[$key])) { $config[$key] = $params[$key]; } } extract($config); if ($proxy_host != '') { if (!preg_match('#^[a-z]+://#', $proxy_host)) { $proxy_host = 'http://' . $proxy_host; } $proxy_params = parse_url($proxy_host); $proxy_host = $proxy_params['host']; $use_proxy = TRUE; $proxy_port = isset($proxy_params['port']) ? $proxy_params['port'] : '8080'; $need_proxy_auth = isset($proxy_params['user']); $proxy_auth_user = isset($proxy_params['user']) ? $proxy_params['user'] : ''; $proxy_auth_pass = isset($proxy_params['pass']) ? $proxy_params['pass'] : ''; } $rc = array(); $arr = parse_url($url); $via_proxy = $use_proxy ? !in_the_net($no_proxy, $arr['host']) : FALSE; $arr['query'] = isset($arr['query']) ? '?' . $arr['query'] : ''; $arr['port'] = isset($arr['port']) ? $arr['port'] : 80; $url_base = $arr['scheme'] . '://' . $arr['host'] . ':' . $arr['port']; $url_path = isset($arr['path']) ? $arr['path'] : '/'; $url = ($via_proxy ? $url_base : '') . $url_path . $arr['query']; $query = $method . ' ' . $url . ' HTTP/1.0' . "\r\n"; $query .= 'Host: ' . $arr['host'] . "\r\n"; $query .= 'User-Agent: ' . $ua . "\r\n"; if ($need_proxy_auth && isset($proxy_auth_user) && isset($proxy_auth_pass)) { $query .= 'Proxy-Authorization: Basic ' . base64_encode($proxy_auth_user . ':' . $proxy_auth_pass) . "\r\n"; } if (isset($arr['user']) && isset($arr['pass'])) { $query .= 'Authorization: Basic ' . base64_encode($arr['user'] . ':' . $arr['pass']) . "\r\n"; } $query .= $headers; if (strtoupper($method) == 'POST') { $POST = array(); foreach ($post as $name => $val) { $POST[] = $name . '=' . urlencode($val); } $data = join('&', $POST); if (preg_match('/^[a-zA-Z0-9_-]+$/', $content_charset)) { $query .= 'Content-Type: application/x-www-form-urlencoded' . "\r\n"; } else { $query .= 'Content-Type: application/x-www-form-urlencoded' . '; charset=' . strtolower($content_charset) . "\r\n"; } $query .= 'Content-Length: ' . strlen($data) . "\r\n"; $query .= "\r\n"; $query .= $data; } else { $query .= "\r\n"; } $errno = 0; $errstr = ''; $fp = fsockopen($via_proxy ? $proxy_host : $arr['host'], $via_proxy ? $proxy_port : $arr['port'], $errno, $errstr, 30); if ($fp === FALSE) { return array('query' => $query, 'rc' => $errno, 'header' => '', 'data' => $errstr); } fputs($fp, $query); $response = ''; while (!feof($fp)) { $response .= fread($fp, 4096); } fclose($fp); $resp = explode("\r\n\r\n", $response, 2); $rccd = explode(' ', $resp[0], 3); $rc = (int) $rccd[1]; switch ($rc) { case 301: case 302: $matches = array(); if (preg_match('/^Location: (.+)$/m', $resp[0], $matches) && --$redirect_max > 0) { $url = trim($matches[1]); if (!preg_match('/^https?:\\//', $url)) { if ($url[0] != '/') { $url = substr($url_path, 0, strrpos($url_path, '/')) . '/' . $url; } $url = $url_base . $url; } return http_request($url, $method, $headers, $post, $redirect_max); } } return array('query' => $query, 'rc' => $rc, 'header' => $resp[0], 'data' => $resp[1]); }
function http_request($url, $method = 'GET', $headers = array(), $post = array(), $redirect_max = PKWK_HTTP_REQUEST_URL_REDIRECT_MAX, $content_charset = '') { global $use_proxy, $no_proxy, $proxy_host, $proxy_port; global $need_proxy_auth, $proxy_auth_user, $proxy_auth_pass; $rc = array(); $arr = parse_url($url); $via_proxy = $use_proxy ? !in_the_net($no_proxy, $arr['host']) : FALSE; $arr['query'] = isset($arr['query']) ? '?' . $arr['query'] : ''; $arr['port'] = isset($arr['port']) ? $arr['port'] : 80; $url_base = $arr['scheme'] . '://' . $arr['host'] . ':' . $arr['port']; $url_path = isset($arr['path']) ? $arr['path'] : '/'; $url = ($via_proxy ? $url_base : '') . $url_path . $arr['query']; // HTTP request method $query = $method . ' ' . $url . ' HTTP/' . PKWK_HTTP_VERSION . "\r\n"; $query .= 'Host: ' . $arr['host'] . "\r\n"; $query .= 'User-Agent: ' . PKWK_HTTP_CLIENT . "\r\n"; $query .= 'Connection: close' . "\r\n"; // Basic-auth for HTTP proxy server if ($need_proxy_auth && isset($proxy_auth_user) && isset($proxy_auth_pass)) { $query .= 'Proxy-Authorization: Basic ' . base64_encode($proxy_auth_user . ':' . $proxy_auth_pass) . "\r\n"; } // (Normal) Basic-auth for remote host if (isset($arr['user']) && isset($arr['pass'])) { $query .= 'Authorization: Basic ' . base64_encode($arr['user'] . ':' . $arr['pass']) . "\r\n"; } //@miko added // Gzipped encoding if (PKWK_HTTP_VERSION == '1.1' && extension_loaded('zlib') && (ini_get('mbstring.func_overload') & 2) == 0) { $query .= 'Accept-Encoding: gzip' . "\r\n"; } // Add Headers if (is_array($headers)) { foreach ($headers as $key => $val) { $query .= $key . ': ' . $val . "\r\n"; } } else { $query .= $headers; } //@miko added if (strtoupper($method) == 'POST') { // 'application/x-www-form-urlencoded', especially for TrackBack ping $POST = array(); // foreach ($post as $name=>$val) $POST[] = $name . '=' . urlencode($val); foreach ($post as $name => $val) { $POST[] = urlencode($name) . '=' . urlencode($val); } $data = join('&', $POST); if (preg_match('/^[a-zA-Z0-9_-]+$/', $content_charset)) { // Legacy but simple $query .= 'Content-Type: application/x-www-form-urlencoded' . "\r\n"; } else { // With charset (NOTE: Some implementation may hate this) $query .= 'Content-Type: application/x-www-form-urlencoded' . '; charset=' . strtolower($content_charset) . "\r\n"; } $query .= 'Content-Length: ' . strlen($data) . "\r\n"; $query .= "\r\n"; $query .= $data; //@miko_patched //@for use propfind, use "Depth:infinity, noroot" } elseif (strtoupper($method) == 'PROPFIND') { // 'text/xml', especially for svn $data = implode('', $post); if (preg_match('/^[a-zA-Z0-9_-]+$/', $content_charset)) { // Legacy but simple $query .= 'Content-Type: text/xml' . "\r\n"; } else { // With charset (NOTE: Some implementation may hate this) $query .= 'Content-Type: text/xml' . '; charset=' . strtolower($content_charset) . "\r\n"; } $query .= 'Content-Length: ' . strlen($data) . "\r\n"; $query .= "\r\n"; $query .= $data; //@miko_patched } else { $query .= "\r\n"; } $errno = 0; $errstr = ''; $fp = fsockopen($via_proxy ? $proxy_host : $arr['host'], $via_proxy ? $proxy_port : $arr['port'], $errno, $errstr, PKWK_HTTP_CONNECT_TIMEOUT); if ($fp === FALSE) { return array('query' => $query, 'rc' => $errno, 'header' => '', 'data' => $errstr); } socket_set_timeout($fp, PKWK_HTTP_REQUEST_TIMEOUT, 0); fwrite($fp, $query); // Get a Head $head = ''; $status = array(); while (!feof($fp)) { $line = rtrim(fgets($fp, 4096)); $status = socket_get_status($fp); if ($status['timed_out']) { break; } if ($line == '') { break; } if ($head != '') { $r = explode(':', $line, 2); $response[strtolower(trim($r[0]))] = strtolower(trim($r[1])); } $head .= $line . "\r\n"; } // Get a Body $chunked = isset($response['transfer-encoding']) && $response['transfer-encoding'] == 'chunked'; $gzipped = isset($response['content-encoding']) && $response['content-encoding'] == 'gzip'; $body = ''; $length = 0; $status = array(); if (!isset($response['content-length']) || $response['content-length'] != 0) { while (!feof($fp)) { if ($chunked) { $body .= fread_chunked($fp, $length); } else { $body .= fread($fp, 4096); } $status = socket_get_status($fp); if ($status['timed_out']) { break; } } } // fputs($fp, $query); // $response = ''; // while (! feof($fp)) $response .= fread($fp, 4096); fclose($fp); //@miko added if ($body != '' && $gzipped) { $body = gzinflate(substr($body, 10)); } //@miko added // $resp = explode("\r\n\r\n", $response, 2); // $rccd = explode(' ', $resp[0], 3); // array('HTTP/1.1', '200', 'OK\r\n...') $rccd = explode(' ', $head, 3); // array('HTTP/1.1', '200', 'OK\r\n...') $rc = (int) $rccd[1]; switch ($rc) { case 301: // Moved Permanently // Moved Permanently case 302: // Moved Temporarily $matches = array(); // if (preg_match('/^Location: (.+)$/m', $resp[0], $matches) if (preg_match('/^Location: (.+)$/m', $head, $matches) && --$redirect_max > 0) { $url = trim($matches[1]); if (!preg_match('/^https?:\\//', $url)) { // Relative path to Absolute if ($url[0] != '/') { $url = substr($url_path, 0, strrpos($url_path, '/')) . '/' . $url; } $url = $url_base . $url; // Add sheme, host } // Redirect return http_request($url, $method, $headers, $post, $redirect_max); } } return array('query' => $query, 'rc' => $rc, 'header' => $head, 'data' => $body, 'timeout' => $status['timed_out']); }