/** * Load a File or an URL * it may use 3 methods: FileRead, CURL or HTTP-Browser class * * @param STRING $y_url_or_path :: /path/to/file | http(s)://some.url:port/path (port is optional) * @param NUMBER $y_timeout :: timeout in seconds * @param ENUM $y_method :: used only for URLs, the browsing method: GET | POST * @param ENUM $y_ssl_method :: SSL Mode: tls | sslv3 | sslv2 | ssl * @param STRING $y_auth_name :: used only for URLs, the auth user name * @param STRING $y_auth_pass :: used only for URLs, the auth password * @param YES/NO y_allow_set_credentials :: DEFAULT MUST BE set to NO ; if YES must be set just for internal URLs ; if the $y_url_or_path to get is detected to be under current URL will send also the Unique / session IDs ; more if detected that is from admin.php and if this is set to YES will send the HTTP-BASIC Auth credentials if detected (using YES with other URLs than SmartFramework's current URL can be a serious SECURITY ISSUE, so don't !) */ public static function load_url_or_file($y_url_or_path, $y_timeout = 30, $y_method = 'GET', $y_ssl_method = '', $y_auth_name = '', $y_auth_pass = '', $y_allow_set_credentials = 'no') { //-- v.2016-01-15 // fixed sessionID with new Dynamic generated // TODO: use the CURL to browse also FTP and SSH ... //-- $y_url_or_path = (string) $y_url_or_path; //-- if ((string) $y_url_or_path == '') { //-- return array('log' => 'ERROR: FILE Name is Empty ...', 'mode' => 'file', 'result' => '0', 'code' => '400', 'headers' => '', 'content' => '', 'debuglog' => ''); //-- } //end if //-- detect if file or url if (substr($y_url_or_path, 0, 7) == 'http://' or substr($y_url_or_path, 0, 8) == 'https://') { $is_file = 0; // it is a url } else { $is_file = 1; // it is a file } //end if //-- if ($is_file == 1) { //-- $y_url_or_path = trim($y_url_or_path); //-- try to detect if data:image/ :: {{{SYNC-DATA-IMAGE}}} if (strtolower(substr($y_url_or_path, 0, 11)) == 'data:image/' and stripos($y_url_or_path, ';base64,') !== false) { //-- $eimg = explode(';base64,', $y_url_or_path); //-- return array('log' => 'OK ? Not sure, decoded from embedded b64 image: ', 'mode' => 'embedded', 'result' => '1', 'code' => '200', 'headers' => SmartUnicode::sub_str($y_url_or_path, 0, 50) . '...', 'content' => @base64_decode(trim($eimg[1])), 'debuglog' => ''); //-- } elseif (is_file($y_url_or_path)) { //-- return array('log' => 'OK: FILE Exists', 'mode' => 'file', 'result' => '1', 'code' => '200', 'headers' => 'Content-Disposition: inline; filename="' . basename($y_url_or_path) . '"' . "\n", 'content' => SmartFileSystem::read($y_url_or_path), 'debuglog' => ''); //-- } else { //-- return array('log' => 'ERROR: FILE Not Found or Invalid Data ...', 'mode' => 'file', 'result' => '0', 'code' => '404', 'headers' => '', 'content' => '', 'debuglog' => ''); //-- } //end if else //-- } else { //-- if ((string) $y_ssl_method == '') { if (defined('SMART_FRAMEWORK_SSL_MODE')) { $y_ssl_method = (string) SMART_FRAMEWORK_SSL_MODE; } else { Smart::log_notice('NOTICE: LibUtils/Load-URL-or-File // The SSL Method not defined and SMART_FRAMEWORK_SSL_MODE was not defined. Using the `tls` as default ...'); $y_ssl_method = 'tls'; } //end if else } //end if //-- $browser = new SmartHttpClient(); //-- $y_timeout = Smart::format_number_int($y_timeout, '+'); if ($y_timeout <= 0) { $y_timeout = 30; // default value } //end if $browser->connect_timeout = (int) $y_timeout; //-- if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') { $browser->debug = 1; } //end if //-- if ((string) self::get_server_current_protocol() == 'https://') { $tmp_current_protocol = 'https://'; } else { $tmp_current_protocol = 'http://'; } //end if else //-- $tmp_current_server = self::get_server_current_domain_name(); $tmp_current_port = self::get_server_current_port(); //-- $tmp_current_path = self::get_server_current_request_uri(); $tmp_current_script = self::get_server_current_full_script(); //-- $tmp_test_url_arr = Smart::separe_url_parts($y_url_or_path); $tmp_test_browser_id = self::get_os_browser_ip(); //-- $tmp_extra_log = ''; if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') { $tmp_extra_log .= "\n" . '===== # =====' . "\n"; } //end if //-- $cookies = array(); $auth_name = (string) $y_auth_name; $auth_pass = (string) $y_auth_pass; //-- if ((string) $y_allow_set_credentials == 'yes') { //-- if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') { $tmp_extra_log .= '[EXTRA]: I will try to detect if this is my current Domain and I will check if it is safe to send my sessionID COOKIE and my Auth CREDENTIALS ...' . "\n"; } //end if //-- if ((string) $tmp_current_protocol == (string) $tmp_test_url_arr['protocol'] and (string) $tmp_current_server == (string) $tmp_test_url_arr['server'] and (string) $tmp_current_port == (string) $tmp_test_url_arr['port']) { //-- if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') { $tmp_extra_log .= '[EXTRA]: OK, Seems that the browsed Domain is identical with my current Domain which is: ' . $tmp_current_protocol . $tmp_current_server . ':' . $tmp_current_port . ' and the browsed one is: ' . $tmp_test_url_arr['protocol'] . $tmp_test_url_arr['server'] . ':' . $tmp_test_url_arr['port'] . "\n"; $tmp_extra_log .= '[EXTRA]: I will also check if my current script and path are identical with the browsed ones ...' . "\n"; } //end if //-- if ((string) $tmp_current_script == (string) $tmp_test_url_arr['scriptname'] and substr($tmp_current_path, 0, strlen($tmp_current_script)) == (string) $tmp_test_url_arr['scriptname']) { //-- if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') { $tmp_extra_log .= '[EXTRA]: OK, Seems that the current script is identical with the browsed one :: ' . 'Current Path is: \'' . $tmp_current_script . '\' / Browsed Path is: \'' . $tmp_test_url_arr['scriptname'] . '\' !' . "\n"; $tmp_extra_log .= '[EXTRA]: I will check if I have to send my SessionID so I will check the browserID ...' . "\n"; } //end if //-- $browser->useragent = (string) self::get_selfrobot_useragent_name(); // this must be set just when detected the same path and script ; it is a requirement to detect it as the self-robot [ @s# ] in order to send the credentials or the current //-- {{{SYNC-SMART-UNIQUE-COOKIE}}} if (defined('SMART_FRAMEWORK_UNIQUE_ID_COOKIE_NAME') and !defined('SMART_FRAMEWORK_UNIQUE_ID_COOKIE_SKIP')) { if ((string) SMART_FRAMEWORK_UNIQUE_ID_COOKIE_NAME != '') { if (SmartFrameworkSecurity::ValidateVariableName(strtolower((string) SMART_FRAMEWORK_UNIQUE_ID_COOKIE_NAME))) { //-- if ((string) SMART_APP_VISITOR_COOKIE != '') { // if set, then forward if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') { $tmp_extra_log .= '[EXTRA]: OK, I will send my current Visitor Unique Cookie ID as it is set and not empty ...' . "\n"; } //end if $cookies[(string) SMART_FRAMEWORK_UNIQUE_ID_COOKIE_NAME] = (string) SMART_APP_VISITOR_COOKIE; // this is a requirement } //end if //-- } //end if } //end if } //end if //-- #end# sync if ((string) SmartAuth::get_login_method() == 'HTTP-BASIC' and (string) $auth_name == '' and (string) $auth_pass == '' and strpos($tmp_current_script, '/admin.php') !== false and strpos($tmp_test_url_arr['scriptname'], '/admin.php') !== false) { //-- if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') { $tmp_extra_log .= '[EXTRA]: HTTP-BASIC Auth method detected / Allowed to pass the Credentials - as the browsed URL belongs to this ADMIN Server as I run, the Auth credentials are set but passed as empty - everything seems to be safe I will send my credentials: USERNAME = \'' . SmartAuth::get_login_id() . '\' ; PASS = *****' . "\n"; } //end if //-- $auth_name = (string) SmartAuth::get_login_id(); $auth_pass = (string) SmartAuth::get_login_password(); //-- } //end if //-- } else { //-- if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') { $tmp_extra_log .= '[EXTRA]: Seems that the scripts are NOT identical :: ' . 'Current Script is: \'' . $tmp_current_script . '\' / Browsed Script is: \'' . $tmp_test_url_arr['scriptname'] . '\' !' . "\n"; $tmp_extra_log .= '[EXTRA]: This is the diff for having a comparation: ' . substr($tmp_current_path, 0, strlen($tmp_current_script)) . "\n"; } //end if //-- } //end if //-- } //end if //-- } //end if //-- $browser->cookies = (array) $cookies; //-- $data = (array) $browser->browse_url($y_url_or_path, $y_method, $y_ssl_method, $auth_name, $auth_pass); // do browse //-- return array('log' => (string) $data['log'] . $tmp_extra_log, 'mode' => (string) $data['mode'], 'result' => (string) $data['result'], 'code' => (string) $data['code'], 'headers' => (string) $data['headers'], 'content' => (string) $data['content'], 'debuglog' => (string) $data['debuglog']); //-- } //end if else //-- }
private function send_request($url, $user = '', $pwd = '', $method = 'GET', $ssl_version = '') { //-- $this->method = (string) strtoupper(trim((string) $method)); //-- //-- $this->connect_timeout = (int) $this->connect_timeout; if ($this->connect_timeout < 1) { $this->connect_timeout = 1; } //end if if ($this->connect_timeout > 120) { $this->connect_timeout = 120; } //end if //-- //-- log action if ($this->debug) { $this->log .= '[INF] Get From URL :: is starting ...' . "\n"; } //end if //-- //-- separations $this->url_parts = (array) Smart::separe_url_parts($url); $protocol = (string) $this->url_parts['protocol']; $server = (string) $this->url_parts['server']; $port = (string) $this->url_parts['port']; $path = (string) $this->url_parts['path']; //-- if ($this->debug) { $this->log .= '[INF] Analize of the URL: ' . @print_r($this->url_parts, 1) . "\n"; } //end if //-- //-- if ((string) $server == '') { if ($this->debug) { $this->log .= '[ERR] Invalid Server to Browse' . "\n"; } //end if Smart::log_warning('LibHTTP // GetFromURL () // Invalid (empty) Server to Browse ...'); return 0; } //end if //-- //-- $browser_protocol = ''; //-- if ((string) $protocol == 'https://') { //-- switch (strtolower((string) $ssl_version)) { case 'ssl': $browser_protocol = 'ssl://'; break; case 'sslv3': $browser_protocol = 'sslv3://'; break; case 'tls': default: $browser_protocol = 'tls://'; } //end switch //-- if (!function_exists('openssl_open')) { if ($this->debug) { $this->log .= '[ERR] PHP OpenSSL Extension is required to perform SSL requests' . "\n"; } //end if Smart::log_warning('LibHTTP // GetFromURL (' . $browser_protocol . $server . ':' . $port . $path . ') // PHP OpenSSL Extension not installed ...'); return 0; } //end if //-- } //end if else //-- //-- $have_cookies = false; if (is_array($this->cookies)) { if (count($this->cookies) > 0) { $have_cookies = true; } //end if } //end if //-- $have_post_vars = false; if ((string) $this->poststring != '') { $have_post_vars = true; } elseif (is_array($this->postvars)) { if (count($this->postvars) > 0) { $have_post_vars = true; } //end if } //end if //-- //-- navigate if ($this->debug) { $this->log .= 'Opening HTTP(S) Browser Connection to: ' . $protocol . $server . ':' . $port . $path . ' using socket protocol: [' . $browser_protocol . ']' . "\n"; $this->log .= '[INF] HTTP Protocol: ' . $this->protocol . "\n"; $this->log .= '[INF] Connection TimeOut: ' . $this->connect_timeout . "\n"; } //end if //-- $stream_context = @stream_context_create(); if ((string) $browser_protocol != '') { if (defined('SMART_FRAMEWORK_SSL_CA_PATH')) { if ((string) SMART_FRAMEWORK_SSL_CA_PATH != '') { @stream_context_set_option($stream_context, 'ssl', 'capath', Smart::real_path((string) SMART_FRAMEWORK_SSL_CA_PATH)); } //end if } //end if @stream_context_set_option($stream_context, 'ssl', 'ciphers', (string) SMART_FRAMEWORK_SSL_CIPHERS); // allow only high ciphers @stream_context_set_option($stream_context, 'ssl', 'verify_host', (bool) SMART_FRAMEWORK_SSL_VFY_HOST); // allways must be set to true ! @stream_context_set_option($stream_context, 'ssl', 'verify_peer', (bool) SMART_FRAMEWORK_SSL_VFY_PEER); // this may fail with some CAs @stream_context_set_option($stream_context, 'ssl', 'verify_peer_name', (bool) SMART_FRAMEWORK_SSL_VFY_PEER_NAME); // allow also wildcard names * @stream_context_set_option($stream_context, 'ssl', 'allow_self_signed', (bool) SMART_FRAMEWORK_SSL_ALLOW_SELF_SIGNED); // must allow self-signed certificates but verified above @stream_context_set_option($stream_context, 'ssl', 'disable_compression', (bool) SMART_FRAMEWORK_SSL_DISABLE_COMPRESS); // help mitigate the CRIME attack vector } //end if else $this->socket = @stream_socket_client($browser_protocol . $server . ':' . $port, $errno, $errstr, $this->connect_timeout, STREAM_CLIENT_CONNECT, $stream_context); //-- if (!is_resource($this->socket)) { if ($this->debug) { $this->log .= '[ERR] Could not open connection. Error : ' . $errno . ': ' . $errstr . "\n"; Smart::log_notice('LibHTTP // GetFromURL (' . $browser_protocol . $server . ':' . $port . $path . ') // Could not open connection. Error : ' . $errno . ': ' . $errstr . ' #'); } //end if return 0; } //end if //-- if ($this->debug) { $this->log .= '[INF] Socket Resource ID: ' . $this->socket . "\n"; } //end if //-- @stream_set_timeout($this->socket, (int) SMART_FRAMEWORK_NETSOCKET_TIMEOUT); if ($this->debug) { $this->log .= '[INF] Set Socket Stream TimeOut to: ' . SMART_FRAMEWORK_NETSOCKET_TIMEOUT . "\n"; } //end if //-- //-- avoid connect normally if SSL/TLS was explicit required $chk_crypto = (array) @stream_get_meta_data($this->socket); if ((string) $browser_protocol != '') { if (stripos($chk_crypto['stream_type'], '/ssl') === false) { // will return something like: tcp_socket/ssl if ($this->debug) { $this->log .= '[ERR] Connection CRYPTO CHECK Failed ...' . "\n"; Smart::log_notice('LibHTTP // GetFromURL (' . $browser_protocol . $server . ':' . $port . $path . ') // Connection CRYPTO CHECK Failed ...'); } //end if return 0; } //end if } //end if //-- //-- $this->raw_headers['Host'] = $server . ':' . $port; //-- //-- auth if ((string) $user != '' and (string) $pwd != '') { //-- if ($this->debug) { $this->log .= '[INF] Authentication will be attempted for USERNAME = \'' . $user . '\' ; PASSWORD(' . strlen($pwd) . ') *****' . "\n"; } //end if //-- $this->raw_headers['Authorization'] = 'Basic ' . base64_encode($user . ':' . $pwd); //-- } //end if //-- //-- cookies $send_cookies = ''; //-- if ($have_cookies) { //-- foreach ($this->cookies as $key => $value) { if ((string) $key != '') { if ((string) $value != '') { $send_cookies .= (string) SmartHttpUtils::encode_var_cookie($key, $value); } //end if } //end if } //end foreach //-- if ((string) $send_cookies != '') { $this->raw_headers['Cookie'] = $send_cookies; if ($this->debug) { $this->log .= '[INF] Cookies will be SET: ' . $send_cookies . "\n"; } //end if } //end if //-- } //end if //-- //-- request if ((string) $this->jsonrequest != '') { // json request //-- if ($this->debug) { $this->log .= '[INF] JSON Request will be sent to server via: ' . $this->method . "\n"; } //end if //-- $request = $this->method . ' ' . $path . ' HTTP/' . $this->protocol . "\r\n"; $this->raw_headers['Content-Type'] = 'application/json'; $this->raw_headers['Content-Length'] = strlen($this->jsonrequest); //-- } elseif ((string) $this->xmlrequest != '') { // xml request //-- if ($this->debug) { $this->log .= '[INF] XML Request will be sent to server via: ' . $this->method . "\n"; } //end if //-- $request = $this->method . ' ' . $path . ' HTTP/' . $this->protocol . "\r\n"; $this->raw_headers['Content-Type'] = 'application/xml'; // may be also: text/xml $this->raw_headers['Content-Length'] = strlen($this->xmlrequest); //-- } elseif ($have_post_vars) { // post vars //-- if ((string) $this->method == 'GET') { $this->method = 'POST'; // FIX: if GET Method is using PostVars, then set method to POST ; this should not be fixed for other methods like: HEAD, PUT, DELETE ... } //end if //-- if ($this->debug) { $this->log .= '[INF] Variables will be sent to server using POST method' . "\n"; } //end if //-- $post_string = ''; if ((string) $this->poststring != '') { $post_string = (string) $this->poststring; } elseif (is_array($this->postvars)) { foreach ($this->postvars as $key => $value) { $post_string .= (string) SmartHttpUtils::encode_var_post($key, $value); } //end foreach } //end if else //-- $request = $this->method . ' ' . $path . ' HTTP/' . $this->protocol . "\r\n"; $this->raw_headers['Content-Type'] = 'application/x-www-form-urlencoded'; $this->raw_headers['Content-Length'] = strlen($post_string); //-- } else { // simple request //-- if ($this->debug) { $this->log .= '[INF] Simple Request via: ' . $this->method . "\n"; } //end if //-- $request = $this->method . ' ' . $path . ' HTTP/' . $this->protocol . "\r\n"; //-- } //end if else //-- //-- check if (!$this->socket) { //-- if ($this->debug) { $this->log .= '[ERR] Premature connection end (1.1)' . "\n"; Smart::log_notice('LibHTTP // GetFromURL // Premature connection end (1.1) ... ' . $url); } //end if return 0; //-- } //end if //-- //-- if (@fwrite($this->socket, $request) === false) { if ($this->debug) { $this->log .= '[ERR] Error writing Request type to socket' . "\n"; Smart::log_notice('LibHTTP // GetFromURL (' . $browser_protocol . $server . ':' . $port . $path . ') // Error writing Request type to socket ...'); } //end if return 0; } //end if //-- //-- raw headers if (!$this->socket) { //-- if ($this->debug) { $this->log .= '[ERR] Premature connection end (1.2)' . "\n"; Smart::log_notice('LibHTTP // GetFromURL // Premature connection end (1.2) ... ' . $url); } //end if return 0; //-- } //end if //-- foreach ($this->raw_headers as $key => $value) { if (@fwrite($this->socket, $key . ": " . $value . "\r\n") === false) { if ($this->debug) { $this->log .= '[ERR] Error writing Raw-Headers to socket' . "\n"; Smart::log_notice('LibHTTP // GetFromURL (' . $browser_protocol . $server . ':' . $port . $path . ') // Error writing Raw-Headers to socket ...'); } //end if return 0; } //end if } //end foreach //-- //-- end-line or blank line before post / cookies if (!$this->socket) { //-- if ($this->debug) { $this->log .= '[ERR] Premature connection end (1.3)' . "\n"; Smart::log_notice('LibHTTP // GetFromURL // Premature connection end (1.3) ... ' . $url); } //end if return 0; //-- } //end if //-- if (@fwrite($this->socket, "\r\n") === false) { if ($this->debug) { $this->log .= '[ERR] Error writing End-Of-Line to socket' . "\n"; Smart::log_notice('LibHTTP // GetFromURL (' . $browser_protocol . $server . ':' . $port . $path . ') // Error writing End-Of-Line to socket ...'); } //end if return 0; } //end if //-- //-- if ((string) $this->jsonrequest != '') { // json request //-- if (!$this->socket) { //-- if ($this->debug) { $this->log .= '[ERR] Premature connection end (1.4)' . "\n"; Smart::log_notice('LibHTTP // GetFromURL // Premature connection end (1.4) ... ' . $url); } //end if return 0; //-- } //end if //-- if (@fwrite($this->socket, $this->jsonrequest . "\r\n") === false) { if ($this->debug) { $this->log .= '[ERR] Error writing JSON Request data to socket' . "\n"; Smart::log_notice('LibHTTP // GetFromURL (' . $browser_protocol . $server . ':' . $port . $path . ') // Error writing JSON Request data to socket ...'); } //end if return 0; } //end if //-- } elseif ((string) $this->xmlrequest != '') { // xml request //-- if (!$this->socket) { //-- if ($this->debug) { $this->log .= '[ERR] Premature connection end (1.5)' . "\n"; Smart::log_notice('LibHTTP // GetFromURL // Premature connection end (1.5) ... ' . $url); } //end if return 0; //-- } //end if //-- if (@fwrite($this->socket, $this->xmlrequest . "\r\n") === false) { if ($this->debug) { $this->log .= '[ERR] Error writing XML Request data to socket' . "\n"; Smart::log_notice('LibHTTP // GetFromURL (' . $browser_protocol . $server . ':' . $port . $path . ') // Error writing XML Request data to socket ...'); } //end if return 0; } //end if //-- } elseif ($have_post_vars) { //-- if (!$this->socket) { //-- if ($this->debug) { $this->log .= '[ERR] Premature connection end (1.6)' . "\n"; Smart::log_notice('LibHTTP // GetFromURL // Premature connection end (1.6) ... ' . $url); } //end if return 0; //-- } //end if //-- if (@fwrite($this->socket, $post_string . "\r\n") === false) { if ($this->debug) { $this->log .= '[ERR] Error writing POST data to socket' . "\n"; Smart::log_notice('LibHTTP // GetFromURL (' . $browser_protocol . $server . ':' . $port . $path . ') // Error writing POST data to socket ...'); } //end if return 0; } //end if //-- } //end if else //-- //-- NOTICE: is this necessary ??? appears that not (it was tested a long time without and appear to be faster) ... //if(@fwrite($this->socket, "\r\n") === false) { // if($this->debug) { // $this->log .= '[ERR] Error writing EOL to socket'."\n"; // Smart::log_notice('LibHTTP // GetFromURL ('.$conex_info.') // Error writing EOL ...'); // FIX: the final \r\n // } //end if //} //end if //-- //-- return 1; //-- }
public function browse_url($url, $method = 'GET', $ssl_version = '', $user = '', $pwd = '', $proxy = array()) { //-- reset $this->reset(); //-- //-- if ($this->debug) { $run_time = microtime(true); } //end if //-- //-- $this->connect_timeout = (int) $this->connect_timeout; if ($this->connect_timeout < 1) { $this->connect_timeout = 1; } //end if if ($this->connect_timeout > 120) { $this->connect_timeout = 120; } //end if //-- $this->exec_timeout = (int) $this->exec_timeout; if ($this->exec_timeout > 0) { if ($this->exec_timeout < 30) { $this->exec_timeout = 30; } //end if if ($this->exec_timeout > 300) { $this->exec_timeout = 300; } //end if } else { $this->exec_timeout = 0; } //end if else //-- //-- $this->status = 999; //-- //-- log action if ($this->debug) { $this->log .= '[INF] CURL HTTP(S)/FTP Robot Browser :: Browse :: url \'' . $url . '\' @ Auth-User: '******' // Auth-Pass-Length: (' . strlen($pwd) . ') // Method: ' . $method . ' // SSLVersion: ' . $ssl_version . "\n"; $this->log .= '[INF] CURL Protocol: ' . $this->protocol . "\n"; $this->log .= '[INF] Connection TimeOut: ' . $this->connect_timeout . "\n"; $this->log .= '[INF] Execution TimeOut: ' . $this->exec_timeout . "\n"; } //end if //-- //-- method $this->method = (string) strtoupper(trim((string) $method)); //-- //-- separations $this->url_parts = (array) Smart::separe_url_parts($url); $protocol = (string) $this->url_parts['protocol']; $server = (string) $this->url_parts['server']; $port = (string) $this->url_parts['port']; $path = (string) $this->url_parts['path']; //-- if ($this->debug) { $this->log .= '[INF] Analize of the URL: ' . @print_r($this->url_parts, 1) . "\n"; } //end if //-- $is_ftp = false; $use_ssl_tls = false; switch ((string) $protocol) { case 'http://': break; case 'https://': $use_ssl_tls = true; break; case 'ftp://': $is_ftp = true; break; case 'ftps://': $is_ftp = true; $use_ssl_tls = true; break; default: //-- if ($this->debug) { $this->log .= '[ERR] Unsupported URL Type: [' . $protocol . '] for URL: ' . $url . "\n"; } //end if //-- Smart::log_warning('LibCurlHttp(s)Ftp // GetFromURL () // Unsupported URL Type: [' . $protocol . '] for URL: ' . $url); //-- return (array) $this->answer(0, $url, $ssl_version, $user); //-- } //end switch //-- //-- if (!function_exists('curl_init')) { //-- if ($this->debug) { $this->log .= '[ERR] PHP CURL Extension is missing' . "\n"; } //end if //-- Smart::log_warning('LibCurlHttp(s)Ftp // GetFromURL () // CURL Extension is missing ...'); //-- return (array) $this->answer(0, $url, $ssl_version, $user); //-- } //end if //-- //-- $this->curl = @curl_init(); // Initialise a cURL handle //-- if (!$this->curl) { //-- if ($this->debug) { $this->log .= '[ERR] PHP CURL Init Failed' . "\n"; } //end if //-- Smart::log_warning('LibCurlHttp(s)Ftp // GetFromURL () // CURL Init Failed ...'); //-- return (array) $this->answer(0, $url, $ssl_version, $user); //-- } //end if //-- if (Smart::array_size($this->rawheaders) > 0) { foreach ($this->rawheaders as $key => $val) { $this->raw_headers[] = (string) $key . ': ' . $val; } //end foreach } //end if //-- set allowed protocols: HTTP / HTTPS / FTP / FTPS @curl_setopt($this->curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS | CURLPROTO_FTP | CURLPROTO_FTPS); //-- //-- set user agent @curl_setopt($this->curl, CURLOPT_USERAGENT, (string) $this->useragent); //-- //-- timeouts @curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, (int) $this->connect_timeout); if ($this->exec_timeout > 0) { @curl_setopt($this->curl, CURLOPT_TIMEOUT, (int) $this->exec_timeout); } //end if //-- //-- protocol if ((string) $this->protocol == '1.1') { $this->raw_headers[] = (string) 'Connection: close'; @curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); } else { // 1.0 @curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); } //end if else //-- //-- proxy $is_using_proxy = false; if (Smart::array_size($proxy) > 0) { // If the $proxy variable is set, then use: $proxy['ip:port'] ; $proxy['type'] ; $proxy['auth-user'] ; $proxy['auth-pass'] //-- if ((string) $proxy['ip:port'] != '') { //-- $pxy_type = ''; switch ((string) strtoupper(trim((string) $proxy['type']))) { case 'SOCKS4': $is_using_proxy = true; $pxy_type = CURLPROXY_SOCKS4; break; case 'SOCKS4A': $is_using_proxy = true; $pxy_type = CURLPROXY_SOCKS4A; break; case 'SOCKS5': $is_using_proxy = true; $pxy_type = CURLPROXY_SOCKS5; break; case 'SOCKS5H': $is_using_proxy = true; $pxy_type = CURLPROXY_SOCKS5_HOSTNAME; break; case 'HTTP': default: if ($is_ftp) { $proxy['type'] = 'N/A'; } else { $is_using_proxy = true; $proxy['type'] = 'HTTP'; } //end if $pxy_type = CURLPROXY_HTTP; } //end switch //-- if ($is_using_proxy) { //-- if ($this->debug) { $this->log .= '[INF] Using Proxy: ' . $proxy['ip:port'] . ' [Type: ' . $proxy['type'] . ']' . "\n"; } //end if //-- $this->cproxy = (array) $proxy; if ((string) $this->cproxy['auth-pass'] != '') { $this->cproxy['auth-pass'] = '******' . strlen($proxy['auth-pass']) . ') *****'; } //end if //-- @curl_setopt($this->curl, CURLOPT_PROXY, (string) $proxy['ip:port']); @curl_setopt($this->curl, CURLOPT_PROXYTYPE, $pxy_type); //-- if ((string) $proxy['auth-user'] != '') { //-- if ($this->debug) { $this->log .= '[INF] Proxy Authentication will be attempted for USERNAME = \'' . $proxy['auth-user'] . '\' ; PASSWORD(' . strlen($proxy['auth-pass']) . ') *****' . "\n"; } //end if //-- @curl_setopt($this->curl, CURLOPT_PROXYUSERPWD, (string) $proxy['auth-user'] . ':' . $proxy['auth-pass']); //@curl_setopt($this->curl, CURLOPT_PROXYAUTH, CURLAUTH_ANY); // this does not work at all, thus let CURL choose ...: CURLAUTH_BASIC | CURLAUTH_DIGEST //-- } //end if //-- } //end if //-- } //end if //-- } //end if //-- //-- auth if ((string) $user != '') { //-- if ($this->debug) { $this->log .= '[INF] Authentication will be attempted for USERNAME = \'' . $user . '\' ; PASSWORD(' . strlen($pwd) . ') *****' . "\n"; } //end if //-- $this->raw_headers[] = 'Authorization: Basic '.base64_encode($user.':'.$pwd); // it is better to use as below as it can handle more auth types :-) @curl_setopt($this->curl, CURLOPT_USERPWD, (string) $user . ':' . $pwd); //@curl_setopt($this->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); // this does not work at all, thus let CURL choose ...: CURLAUTH_BASIC | CURLAUTH_DIGEST //-- } //end if //-- //-- SSL/TLS Options $browser_protocol = ''; //-- if ($use_ssl_tls) { //-- if (!function_exists('openssl_open')) { //-- if ($this->debug) { $this->log .= '[ERR] PHP OpenSSL Extension is required to perform SSL requests' . "\n"; } //end if //-- Smart::log_warning('LibCurlHttp(s)Ftp // GetFromURL (' . $browser_protocol . $server . ':' . $port . $path . ') // PHP OpenSSL Extension not installed ...'); //-- return (array) $this->answer(0, $url, $ssl_version, $user); //-- } //end if //-- switch (strtolower((string) $ssl_version)) { case 'ssl': $browser_protocol = CURL_SSLVERSION_DEFAULT; // default SSL break; case 'sslv3': $browser_protocol = CURL_SSLVERSION_SSLv3; // SSLv3 break; case 'tls': default: $browser_protocol = CURL_SSLVERSION_TLSv1; // TLSv1.x } //end switch //-- @curl_setopt($this->curl, CURLOPT_SSLVERSION, $browser_protocol); //-- if (defined('SMART_FRAMEWORK_SSL_CA_PATH')) { if ((string) SMART_FRAMEWORK_SSL_CA_PATH != '') { @curl_setopt($this->curl, CURLOPT_CAPATH, Smart::real_path((string) SMART_FRAMEWORK_SSL_CA_PATH)); } //end if } //end if @curl_setopt($this->curl, CURLOPT_SSL_CIPHER_LIST, (string) SMART_FRAMEWORK_SSL_CIPHERS); @curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, (bool) SMART_FRAMEWORK_SSL_VFY_PEER_NAME); // FIX: use vfy peer name instead of SMART_FRAMEWORK_SSL_VFY_HOST as there is no fine tunning here ... @curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, (bool) SMART_FRAMEWORK_SSL_VFY_PEER); // (bool)SMART_FRAMEWORK_SSL_VFY_PEER_NAME :: CURL is missing the option to specific allow/dissalow the peer name (allow also wildcard names *) // (bool)SMART_FRAMEWORK_SSL_ALLOW_SELF_SIGNED :: CURL is missing the option to specific allow/disallow self-signed certificates but verified above // (bool)SMART_FRAMEWORK_SSL_DISABLE_COMPRESS :: CURL is missing the option to disable SSL/TLS compression (help mitigate the CRIME attack vector) //-- } //end if //-- //-- other cURL options that are required @curl_setopt($this->curl, CURLOPT_HEADER, true); @curl_setopt($this->curl, CURLOPT_COOKIESESSION, true); @curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true); @curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); //-- //-- if (Smart::array_size($this->cookies) > 0) { $send_cookies = ''; foreach ($this->cookies as $key => $value) { if ((string) $key != '') { if ((string) $value != '') { $send_cookies .= (string) SmartHttpUtils::encode_var_cookie($key, $value); } //end if } //end if } //end foreach if ((string) $send_cookies != '') { $this->raw_headers[] = (string) 'Cookie: ' . $send_cookies; } //end if $send_cookies = ''; } //end if //-- $have_post_vars = false; if (Smart::array_size($this->postvars) > 0) { $post_string = ''; foreach ((array) $this->postvars as $key => $val) { $post_string .= (string) SmartHttpUtils::encode_var_post($key, $val); } //end foreach if ((string) $post_string != '') { if ((string) $this->method == 'GET') { $this->method = 'POST'; } //end if $have_post_vars = true; @curl_setopt($this->curl, CURLOPT_POSTFIELDS, (string) $post_string); } //end if $post_string = ''; } elseif ((string) $this->poststring != '') { if ((string) $this->method == 'GET') { $this->method = 'POST'; } //end if $have_post_vars = true; @curl_setopt($this->curl, CURLOPT_POSTFIELDS, (string) $this->poststring); } elseif ((string) $this->jsonrequest != '') { $this->raw_headers[] = 'Content-Type: application/json'; $this->raw_headers[] = 'Content-Length: ' . strlen($this->jsonrequest); } elseif ((string) $this->xmlrequest != '') { $this->raw_headers[] = 'Content-Type: application/xml'; $this->raw_headers[] = 'Content-Length: ' . strlen($this->xmlrequest); } //end if else //-- switch ((string) $this->method) { case 'GET': break; case 'POST': if ($have_post_vars) { @curl_setopt($this->curl, CURLOPT_POST, true); } //end if break; case 'HEAD': case 'PUT': case 'DELETE': default: if ($have_post_vars) { @curl_setopt($this->curl, CURLOPT_POST, true); } //end if @curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, (string) $this->method); } //end switch //-- if (Smart::array_size($this->raw_headers) > 0) { // request headers are constructed above @curl_setopt($this->curl, CURLOPT_HTTPHEADER, (array) $this->raw_headers); } //end if //-- //-- Execute a Curl request @curl_setopt($this->curl, CURLOPT_DNS_USE_GLOBAL_CACHE, false); @curl_setopt($this->curl, CURLOPT_FRESH_CONNECT, true); @curl_setopt($this->curl, CURLOPT_FORBID_REUSE, true); @curl_setopt($this->curl, CURLOPT_URL, (string) $url); //-- if (!$this->curl) { //-- if ($this->debug) { $this->log .= '[ERR] CURL Aborted before Execution' . "\n"; } //end if //-- Smart::log_warning('LibCurlHttp(s)Ftp // GetFromURL () // CURL Aborted before Execution ...'); //-- return (array) $this->answer(0, $url, $ssl_version, $user); //-- } //end if //-- $results = @curl_exec($this->curl); $error = @curl_errno($this->curl); //-- //-- eval results $bw_info = array(); $is_ok = 0; //-- if ($results) { //-- $is_ok = 1; //-- $bw_info = (array) @curl_getinfo($this->curl); //-- if ($is_ftp) { //-- $this->header = 'CURL Browser :: FTP(s) have no headers ...'; $this->body = (string) $results; //-- } else { // http //-- $hd_len = (int) $bw_info['header_size']; // get header length //-- if ($hd_len > 0) { //-- $this->header = (string) substr((string) $results, 0, $hd_len); $this->body = (string) substr((string) $results, $hd_len); //-- } else { //-- $this->header = (string) $results; $this->body = ''; //-- $is_ok = 0; //-- if ($this->debug) { Smart::log_notice('LibCurlHttp(s)Ftp // GetFromURL () // CURL Execution Failed to Separe HTTP Header from Body. Reported (invalid) Header size is: [' . $hd_len . ']'); $this->log .= '[ERR] CURL Execution Failed to Separe HTTP Header from Body. Invalid Header size: [' . $hd_len . ']' . "\n"; } //end if //-- } //end if else //-- } //end if else //-- $results = ''; // free memory //-- $is_unauth = false; if ((string) $bw_info['http_code'] == '401') { //-- $is_unauth = true; //-- if ($this->debug) { if ((string) $user != '') { $this->log .= '[ERR] HTTP Authentication Failed for URL: [User='******']: ' . $url . "\n"; Smart::log_notice('LibCurlHttp(s)Ftp // GetFromURL // HTTP Authentication Failed for URL: [User='******']: ' . $url); } else { $this->log .= '[ERR] HTTP Authentication is Required for URL: ' . $url . "\n"; Smart::log_notice('LibCurlHttp(s)Ftp // GetFromURL // HTTP Authentication is Required for URL: ' . $url); } //end if } //end if //-- } //end if //-- if ($is_unauth and $this->no_content_stop_if_unauth) { //-- $this->body = ''; // in this case (by settings) no content (response body) should be returned //-- } //end if //-- if ($error) { //-- $is_ok = 0; //-- if ($this->debug) { $this->log .= '[ERR] CURL Execution Reported some Errors. ErrorCode: [' . $error . ']' . "\n"; Smart::log_notice('LibCurlHttp(s)Ftp // GetFromURL () // CURL Execution Reported some Errors. ErrorCode: [' . $error . ']'); } //end if //-- } //end if //-- $this->status = (int) $bw_info['http_code']; //-- } else { //-- $is_ok = 0; //-- $this->log .= '[ERR] CURL Returned No Results. ErrorCode: [' . $error . ']' . "\n"; //-- } //end if //-- if ($is_unauth) { //-- $is_ok = 0; //-- } //end if //-- //-- $this->close_connection(); //-- //-- if ($this->debug) { $run_time = microtime(true) - $run_time; $this->log .= '[INF] Total Time: ' . $run_time . ' sec.' . "\n"; } //end if //-- //-- return (array) $this->answer($is_ok, $url, $ssl_version, $user, $bw_info); //-- }