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 static function test_fs() { //-- if (SMART_FRAMEWORK_TESTUNIT_ALLOW_FS_TESTS !== true) { return SmartComponents::operation_notice('Test Unit File System Tests are DISABLED ...'); } //end if //-- //-- $time = microtime(true); //-- //-- $err = ''; $tests = array(); //-- //-- if ((string) DIRECTORY_SEPARATOR != '\\') { // broken links do not work on Windows ! $tests[] = '##### FileSystem OPERATIONS / TESTS - ALL: #####'; } else { $tests[] = '##### FileSystem OPERATIONS / TESTS *** PARTIAL SUPPORT ONLY (BY PLATFORM) ***: #####'; } //end if else //-- //-- $test_string = '#START#' . "\n" . 'グッド' . "\n" . 'SmartFramework/Test/FileSystem' . "\n" . time() . "\n" . SMART_FRAMEWORK_HTACCESS_NOINDEXING . SMART_FRAMEWORK_HTACCESS_FORBIDDEN . SMART_FRAMEWORK_HTACCESS_NOEXECUTION . "\n" . '#END#'; $test_str_cksum = SmartHashCrypto::sha512($test_string); $long_prefixed = SmartFileSysUtils::prefixed_sha1_path(sha1(time())); $short_prefixed = SmartFileSysUtils::prefixed_uuid10_dir(Smart::uuid_10_seq()); //-- $the_base_folder = 'tmp/tests/'; $the_sufx_folder = 'Folder1'; $the_base_file = 'NORMAL-Write_123_@#.txt'; //-- $the_folder = $the_base_folder . $the_sufx_folder . '/'; $the_copy_folder = $the_base_folder . 'folder2'; $the_move_folder = $the_base_folder . 'FOLDER3'; $the_extra_folder = $the_folder . 'extra/'; $the_file = $the_folder . $the_base_file; //-- $get_folder = SmartFileSysUtils::add_dir_last_slash(SmartFileSysUtils::get_dir_from_path($the_folder)); $get_file = SmartFileSysUtils::get_file_name_from_path($the_file); $get_xfile = SmartFileSysUtils::get_noext_file_name_from_path($the_file); $get_ext = SmartFileSysUtils::get_file_extension_from_path($the_file); //-- $the_copy_file = $the_file . '.copy.txt'; $the_move_file = $the_extra_folder . $the_base_file . '.copy.moved.txt'; $the_broken_link = $the_extra_folder . 'a-broken-link'; $the_broken_dir_link = $the_extra_folder . 'a-broken-dir-link'; $the_good_link = $the_extra_folder . 'a-good-link'; $the_good_dir_link = $the_extra_folder . 'a-good-dir-link'; //-- //-- $tests[] = 'INITIAL-FOLDER: ' . $get_folder; $tests[] = 'NEW-FOLDER: ' . $the_folder; $tests[] = 'NEW-FILE: ' . $the_file; //-- //-- if ((string) $err == '') { $the_test = 'CHECK TEST SAFE PATH NAME: DIR / FILE ...'; $tests[] = $the_test; if ((string) Smart::safe_pathname((string) $get_folder) !== (string) $get_folder or (string) Smart::safe_pathname((string) $the_copy_file) !== (string) $the_copy_file) { $err = 'ERROR: SAFE PATH NAME TEST ... FAILED !!!'; } //end if } //end if //-- if ((string) $err == '') { $the_test = 'CHECK TEST ABSOLUTE / BACKWARD PATHS ...'; $tests[] = $the_test; if (!SmartFileSysUtils::check_file_or_dir_name('/this/is/absolute', 'no') or SmartFileSysUtils::check_file_or_dir_name('/this/is/absolute') or SmartFileSysUtils::check_file_or_dir_name('/this/is/../backward/path')) { $err = 'ERROR: CHECK TEST ABSOLUTE / BACKWARD PATHS ... FAILED !!!'; } //end if } //end if //-- if ((string) $err == '') { $the_test = 'CHECK EXTRACT FOLDER FROM PATH ...'; $tests[] = $the_test; if ((string) $get_folder != SmartFileSysUtils::add_dir_last_slash(Smart::dir_name($the_folder))) { $err = 'ERROR: Path Extraction FAILED: Dir=' . $get_folder . ' ; DirName=' . SmartFileSysUtils::add_dir_last_slash(Smart::dir_name($the_folder)); } //end if } //end if if ((string) $err == '') { $the_test = 'CHECK EXTRACT FILE AND EXTENSION FROM PATH (1) ...'; $tests[] = $the_test; if ((string) $get_folder . SmartFileSysUtils::add_dir_last_slash($the_sufx_folder) . $get_file != $the_file) { $err = 'ERROR :: Path Extraction FAILED: Re-Composed-File=' . $get_folder . SmartFileSysUtils::add_dir_last_slash($the_sufx_folder) . $get_file . ' ; File=' . $the_file; } //end if } //end if if ((string) $err == '') { $the_test = 'CHECK EXTRACT FILE AND EXTENSION FROM PATH (2) ...'; $tests[] = $the_test; if ((string) $get_file != $get_xfile . '.' . $get_ext) { $err = 'ERROR :: Path Extraction FAILED: File=' . $get_file . ' ; XFile=' . $get_xfile . ' ; Ext=' . $get_ext; } //end if } //end if //-- SmartFileSysUtils::raise_error_if_unsafe_path($the_folder); if ((string) $err == '') { $the_test = 'CHECK PATH NAME DIR: check_file_or_dir_name() : ' . $the_folder; $tests[] = $the_test; $result = 0; $result = SmartFileSysUtils::check_file_or_dir_name($the_folder); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if SmartFileSysUtils::raise_error_if_unsafe_path($the_file); if ((string) $err == '') { $the_test = 'CHECK PATH NAME FILE: check_file_or_dir_name() : ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSysUtils::check_file_or_dir_name($the_file); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- //-- if ((string) $err == '') { $parent_folder = SmartFileSysUtils::add_dir_last_slash(''); $the_test = 'Check Add Dir Last (trailing) Slash: Empty Folder Name'; $tests[] = $the_test; if ((string) $parent_folder != './') { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $parent_folder = SmartFileSysUtils::add_dir_last_slash('.'); $the_test = 'Check Add Dir Last (trailing) Slash: Dot Folder Name: ' . $parent_folder; $tests[] = $the_test; if ((string) $parent_folder != './') { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $parent_folder = SmartFileSysUtils::add_dir_last_slash('./'); $the_test = 'Check Add Dir Last (trailing) Slash: DotSlash Folder Name: ' . $parent_folder; $tests[] = $the_test; if ((string) $parent_folder != './') { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $parent_folder = SmartFileSysUtils::add_dir_last_slash(Smart::dir_name($the_base_folder)); $the_test = 'Check Parent Dir Name with Add Dir Last (trailing) Slash: ' . $parent_folder . ' # from: ' . $the_base_folder; $tests[] = $the_test; if ((string) $parent_folder != 'tmp/') { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- //-- if ((string) $err == '') { if (is_dir($get_folder)) { $the_test = 'DIR DELETE - INIT CLEANUP: dir_delete() + recursive: ' . $get_folder; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::dir_delete($the_base_folder, true); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } else { $tests[] = 'DIR DELETE - INIT CLEANUP: Test Not Run (folder does not exists): ' . $get_folder; } //end if else } //end if //-- if ((string) $err == '') { $the_test = 'DIR CREATE RECURSIVE: dir_recursive_create() : ' . $the_folder . $long_prefixed . $short_prefixed; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::dir_recursive_create($the_folder . $long_prefixed . $short_prefixed); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'DIR CREATE NON-RECURSIVE: dir_create() : extra/ in : ' . $the_extra_folder; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::dir_recursive_create($the_extra_folder); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- if ((string) DIRECTORY_SEPARATOR != '\\') { // broken links do not work on Windows ! if ((string) $err == '') { $the_test = 'CREATE BROKEN FILE LINK FOR DELETION (1): link_create() : as : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::link_create('tmp/cache', $the_broken_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'DELETE BROKEN FILE LINK (1): delete() : as : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::delete($the_broken_link); if ($result !== 1 || is_link($the_broken_link)) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'CREATE BROKEN FILE LINK FOR DELETION (2): link_create() : as : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::link_create('tmp/index.html', $the_broken_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'DELETE BROKEN FILE LINK (2): dir_delete() : as : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::dir_delete($the_broken_link); if ($result !== 1 || is_link($the_broken_link)) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'CREATE BROKEN FILE LINK: link_create() : as : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::link_create('tmp/index.html', $the_broken_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'CREATE BROKEN DIR LINK: link_create() : as : ' . $the_broken_dir_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::link_create('tmp/', $the_broken_dir_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'CREATE A FILE LINK: link_create() : as : ' . $the_good_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::link_create(Smart::real_path('tmp/index.html'), $the_good_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'CREATE A DIR LINK: link_create() : as : ' . $the_good_dir_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::link_create(Smart::real_path('tmp/'), $the_good_dir_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if } //end if //-- if ((string) $err == '') { $the_test = 'FILE WRITE with empty content: write() : ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::write($the_file, ''); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'FILE WRITE: write() / before append : ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::write($the_file, $test_string); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'FILE WRITE: write() +append : ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::write($the_file, $test_string, 'a'); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'FILE READ / Append: read() Full Size: ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::read($the_file); if ((string) SmartHashCrypto::sha512($result) != (string) SmartHashCrypto::sha512($test_string . $test_string)) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'FILE WRITE: re-write() : ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::write($the_file, $test_string); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- if ((string) DIRECTORY_SEPARATOR != '\\') { // broken links do not work on Windows ! if ((string) $err == '') { $the_test = 'FILE WRITE TO A BROKEN LINK: write() : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::write($the_broken_link, $test_string); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'DELETE THE BROKEN LINK AFTER write() and RE-CREATE IT : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::delete($the_broken_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'RE-CREATE BROKEN FILE LINK [AFTER WRITE]: link_create() : as : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::link_create('tmp/index.html', $the_broken_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'FILE WRITE: write_if_not_exists() with Content Compare to a broken link : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::write_if_not_exists($the_broken_link, $test_string, 'yes'); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'DELETE THE BROKEN LINK AFTER write_if_not_exists() and RE-CREATE IT : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::delete($the_broken_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'RE-CREATE BROKEN FILE LINK [AFTER WRITE-IF-NOT-EXISTS]: link_create() : as : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::link_create('tmp/index.html', $the_broken_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if } //end if //-- if ((string) $err == '') { $the_test = 'FILE WRITE: write_if_not_exists() without Content Compare : ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::write_if_not_exists($the_file, $test_string, 'no'); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- if ((string) $err == '') { $the_test = 'FILE READ: read() Full Size: ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::read($the_file); if ((string) SmartHashCrypto::sha512($result) != (string) $test_str_cksum) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'FILE READ: read() Partial Size, First 10 bytes: ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::read($the_file, 10); if ((string) sha1($result) != (string) sha1(substr($test_string, 0, 10))) { // here we read bytes so substr() not SmartUnicode::sub_str() should be used $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- if ((string) $err == '') { $the_test = 'FILE STATIC-READ: staticread() Full Size: ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::staticread($the_file); if ((string) SmartHashCrypto::sha512($result) != (string) $test_str_cksum) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'FILE STATIC-READ: staticread() Partial Size, First 10 bytes: ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::staticread($the_file, 10); if ((string) sha1($result) != (string) sha1(substr($test_string, 0, 10))) { // here we read bytes so substr() not SmartUnicode::sub_str() should be used $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- if ((string) $err == '') { $the_test = 'FILE COPY: copy() : ' . $the_file . ' to: ' . $the_copy_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::copy($the_file, $the_copy_file); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'FILE COPY with OVERWRITE: copy() : ' . $the_file . ' to: ' . $the_copy_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::copy($the_file, $the_copy_file, true); // overwrite destination file(s) if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'FILE RE-COPY (test should re-write the destination): copy() : ' . $the_file . ' to: ' . $the_move_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::copy($the_file, $the_move_file); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } else { $the_test = 'FILE DELETE: delete() : ' . $the_move_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::delete($the_move_file); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if } //end if if ((string) $err == '') { $the_test = 'FILE RENAME/MOVE: rename() : ' . $the_copy_file . ' to: ' . $the_move_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::rename($the_copy_file, $the_move_file); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- if ((string) $err == '') { if (is_dir('__development/')) { //-- $the_test = 'RECURSIVE COPY (CLONE) DIR [DEVELOPMENT]: dir_copy() : ' . '__development/' . ' to: ' . $the_folder . '__development'; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::dir_copy('__development/', $the_folder . '__development'); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if //-- if ((string) $err == '') { $the_test = 'DIR COMPARE THE [DEVELOPMENT] SOURCE WITH [DEVELOPMENT] DESTINATION AFTER DIR COPY AND DIR MOVE:' . "\n" . 'compare_folders() : ' . '__development/' . ' with: ' . $the_folder . '__development/'; $tests[] = $the_test; $arr_diff = array(); $arr_diff = SmartFileSystem::compare_folders('__development', $the_folder . '__development', true, true); if (Smart::array_size($arr_diff) > 0) { $err = 'ERROR :: ' . $the_test . ' #DIFFERENCES=' . print_r($arr_diff, 1); } //end if } //end if //-- } else { $tests[] = 'RECURSIVE COPY (CLONE) DIR [DEVELOPMENT]: Test Not Run (Development environment not detected) ...'; } //end if else } //end if //-- if ((string) $err == '') { $the_test = 'RECURSIVE COPY (CLONE) DIR: dir_copy() : ' . $the_folder . ' to: ' . $the_copy_folder; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::dir_copy($the_folder, $the_copy_folder); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- if ((string) $err == '') { $the_test = 'MOVE/RENAME DIR: dir_rename() : ' . $the_copy_folder . ' to: ' . $the_move_folder; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::dir_rename($the_copy_folder, $the_move_folder); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- if ((string) $err == '') { $the_test = 'DIR COMPARE THE SOURCE WITH DESTINATION AFTER DIR COPY AND DIR MOVE: ' . $the_folder . ' with: ' . $the_move_folder; $tests[] = $the_test; $arr_diff = array(); $arr_diff = SmartFileSystem::compare_folders($the_folder, $the_move_folder, true, true); if (Smart::array_size($arr_diff) > 0) { $err = 'ERROR :: ' . $the_test . ' #DIFFERENCES=' . print_r($arr_diff, 1); } //end if } //end if //-- if ((string) $err == '') { $the_test = 'DIR DELETE - SIMPLE: dir_delete() non-recursive: ' . $the_extra_folder; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::dir_delete($the_extra_folder, false); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'DIR DELETE - LAST CLEANUP: dir_delete() + recursive: ' . $get_folder; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::dir_delete($the_base_folder, true); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- //-- $time = 'TOTAL TIME was: ' . (microtime(true) - $time); //-- //-- $end_tests = '##### END TESTS ... ' . $time . ' sec. #####'; //-- //-- if ((string) $err == '') { $img_sign = 'lib/core/img/sign_info.png'; $img_check = 'lib/core/img/q_completed.png'; $text_main = Smart::escape_js('<span style="color:#83B953;">Good ... Perfect :: グッド ... パーフェクト</span>'); $text_info = Smart::escape_js('<h2><span style="color:#83B953;">All</span> the SmartFramework FS Operations <span style="color:#83B953;">Tests PASSED on PHP</span><hr></h2><span style="font-size:14px;">' . Smart::nl_2_br(Smart::escape_html(implode("\n" . '* ', $tests) . "\n" . $end_tests)) . '</span>'); } else { $img_sign = 'lib/core/img/sign_error.png'; $img_check = 'lib/core/img/q_warning.png'; $text_main = Smart::escape_js('<span style="color:#FF5500;">An ERROR occured ... :: エラーが発生しました ...</span>'); $text_info = Smart::escape_js('<h2><span style="color:#FF5500;">A test FAILED</span> when testing FS Operations.<span style="color:#FF5500;"><hr>FAILED Test Details</span>:</h2><br><h3>' . Smart::escape_html($tests[Smart::array_size($tests) - 1]) . '</h3><br><span style="font-size:14px;"><pre>' . Smart::escape_html($err) . '</pre></span>'); } //end if else //-- //-- $html = <<<HTML <h1>SmartFramework LibFileSystem Tests: DONE ... [ Time: {$time} sec. ]</h1> <script type="text/javascript"> \tSmartJS_BrowserUtils.alert_Dialog( \t\t'<img src="{$img_sign}" align="right"><h1>{$text_main}</h1><hr><span style="color:#333333;"><img src="{$img_check}" align="right">{$text_info}<br>', \t\t'', \t\t'FileSystem Operations Test Suite for SmartFramework: PHP', \t\t'920', \t\t'480' \t); </script> HTML; //-- //-- return $html; //-- }
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); //-- }
public function connect($server, $port = 110, $sslversion = '') { //-- inits $this->socket = false; $this->apop_banner = ''; //-- //-- checks $server = trim($server); if (strlen($server) <= 0 or strlen($server) > 255) { $this->error = '[ERR] Invalid Server to Connect ! [' . $server . ']'; return 0; } //end if //-- $port = (int) $port; if ($port <= 0 or $port > 65535) { $this->error = '[ERR] Invalid Port to Connect ! [' . $port . ']'; return 0; } //end if //-- //-- $protocol = ''; //-- if (strlen($sslversion) > 0) { //-- if (!function_exists('openssl_open')) { $this->error = '[ERR] PHP OpenSSL Extension is required to perform SSL requests !'; return 0; } //end if //-- switch (strtolower($sslversion)) { case 'ssl': $protocol = 'ssl://'; break; case 'sslv3': $protocol = 'sslv3://'; break; case 'tls': default: $protocol = 'tls://'; } //end switch //-- } //end if else //-- //-- if ($this->debug) { $this->log .= '[INF] Connecting to Mail Server: ' . $protocol . $server . ':' . $port . "\n"; } //end if //-- //-- //$sock = @fsockopen($protocol.$server, $port, $errno, $errstr, $this->timeout); $stream_context = @stream_context_create(); if ((string) $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 $sock = @stream_socket_client($protocol . $server . ':' . $port, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $stream_context); //-- if (!is_resource($sock)) { $this->error = '[ERR] Could not open connection. Error: ' . $errno . ' :: ' . $errstr; return 0; } //end if //-- $this->socket = $sock; unset($sock); //-- @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 //-- //-- If mode is 0, the given stream will be switched to non-blocking mode, and if 1, it will be switched to blocking mode. This affects calls like fgets() and fread() that read from the stream. In non-blocking mode an fgets() call will always return right away while in blocking mode it will wait for data to become available on the stream. @socket_set_blocking($this->socket, 1); // set to blocking mode //-- //-- avoid connect normally if SSL/TLS was explicit required $chk_crypto = (array) @stream_get_meta_data($this->socket); if ((string) $protocol != '') { if (!SmartUnicode::str_icontains($chk_crypto['stream_type'], '/ssl')) { // will return something like: tcp_socket/ssl //-- $this->error = '[ERR] Connection CRYPTO CHECK Failed ...' . "\n"; //-- @socket_set_blocking($this->socket, 0); @fclose($this->socket); $this->socket = false; //-- return 0; //-- } //end if } //end if //-- //-- $reply = @fgets($this->socket, $this->buffer); $reply = $this->strip_clf($reply); $test = $this->is_ok($reply); //-- if ((string) $test != 'ok') { //-- $this->error = '[ERR] Server Reply is NOT OK // ' . $test . ' // ' . $reply; //-- @socket_set_blocking($this->socket, 0); @fclose($this->socket); $this->socket = false; //-- return 0; //-- } //end if //-- if ($this->debug) { $this->log .= '[REPLY] \'' . $reply . '\'' . "\n"; } //end if //-- //-- apop banner $this->apop_banner = $this->parse_banner($reply); //-- //-- return 1; //-- }
public function connect($helo, $server, $port = 25, $sslversion = '') { //-- inits $this->socket = false; //-- //-- checks $helo = trim((string) $helo); $server = trim((string) $server); if (strlen($server) <= 0 or strlen($server) > 255) { $this->error = '[ERR] Invalid Server to Connect ! [' . $server . ']'; return 0; } //end if //-- $port = (int) $port; if ($port <= 0 or $port > 65535) { $this->error = '[ERR] Invalid Port to Connect ! [' . $port . ']'; return 0; } //end if //-- //-- $protocol = ''; $start_tls = false; //-- if (strlen($sslversion) > 0) { //-- if (!function_exists('openssl_open')) { $this->error = '[ERR] PHP OpenSSL Extension is required to perform SSL requests !'; return 0; } //end if //-- switch (strtolower($sslversion)) { case 'starttls': $start_tls = true; $protocol = ''; // reset because will connect in a different way break; case 'ssl': $protocol = 'ssl://'; break; case 'sslv3': $protocol = 'sslv3://'; break; case 'tls': default: $protocol = 'tls://'; } //end switch //-- } //end if else //-- //-- if ($this->debug) { $this->log .= '[INF] Connecting to Mail Server: ' . $protocol . $server . ':' . $port . "\n"; } //end if //-- //-- //$sock = @fsockopen($protocol.$server, $port, $errno, $errstr, $this->timeout); $stream_context = @stream_context_create(); if ((string) $protocol != '' or $start_tls === true) { 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 $sock = @stream_socket_client($protocol . $server . ':' . $port, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $stream_context); //-- if (!is_resource($sock)) { $this->error = '[ERR] Could not open connection. Error: ' . $errno . ' :: ' . $errstr; return 0; } //end if //-- $this->socket = $sock; unset($sock); //-- @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 //-- //-- $reply = $this->retry_data(); if (strlen($this->error) > 0) { //-- @fclose($this->socket); $this->socket = false; //-- return 0; //-- } //end if if ($this->debug) { $this->log .= '[REPLY] \'' . $reply . '\'' . "\n"; } //end if //-- $test = $this->answer_code($reply); if ((string) $test != '220') { //-- $this->error = '[ERR] Server Reply is NOT OK // ' . $test . ' // ' . $reply; //-- @fclose($this->socket); $this->socket = false; //-- return 0; //-- } //end if //-- //-- if (!$this->hello($helo)) { //-- $this->error = '[ERR] HELLO Command Failed // ' . $helo; //-- @fclose($this->socket); $this->socket = false; //-- return 0; //-- } //end if //-- //-- if ($start_tls === true) { //-- if ($this->starttls($stream_context) != '1') { //-- if ((string) $this->error == '') { $this->error = '[ERR] Connection CRYPTO ENABLE Failed ...'; } //end if //-- @fclose($this->socket); $this->socket = false; //-- return 0; // error message comes from above //-- } //end if //-- BugFix: Xmail fails after STARTTLS without sending again the HELO as 503 BAD Sequence of commands if (!$this->hello($helo)) { //-- $this->error = '[ERR] HELLO Command Failed // ' . $helo; //-- @fclose($this->socket); $this->socket = false; //-- return 0; //-- } //end if //-- } //end if //-- //-- $chk_crypto = (array) @stream_get_meta_data($this->socket); if ((string) $protocol != '' or $start_tls === true) { // avoid connect normally if SSL/TLS was explicit required if (!SmartUnicode::str_icontains($chk_crypto['stream_type'], '/ssl')) { // will return something like: tcp_socket/ssl //-- $this->error = '[ERR] Connection CRYPTO CHECK Failed ...'; //-- @fclose($this->socket); $this->socket = false; //-- return 0; //-- } //end if } //end if //-- //-- return 1; //-- }