_URL = $URL; $this->_socket_LinkId = $LinkId; $this->_socket_LinkValidationId = $LinkValId; // parse and prepare url if (!$this->prepare_url($this->_URL)) { $this->_socket_status = SOCKET_URL_ERROR; return void; } $this->_socket_blocking_status = $objConfig->Get("SocketBlockingMode")? TRUE : FALSE; // echo "BLOCKING MODE ".$this->_socket_blocking_status; // open new socket connection // $this->socket_connect($this->_socket); } ## Prepare URL function prepare_url($url) { // preg_match("|^([^:]+)://([^:/]+)(:[\d]+)*(.*)|", $url, $url_data); if(!ereg("^http://",$url) && !ereg("^https://",$url) || ereg("^ftp://",$url)) $url = "http://".$url; $url_data = parse_url($url); $this->_url_host = $url_data['host']; $this->_url_query = $url_data['query']; $this->_url_path = empty($url_data['path'])? "/" : $url_data['path']; $this->_url_user = $url_data['user']; $this->_url_password = $url_data['pass']; if (empty($url_data['schema']) || ($url_data['schema'] == "http")) { $this->_url_port = 80; $this->_url_method = HTTP_version; } elseif ($url_data['schema'] == "https") { $this->_url_port = 443; $this->_url_method = HTTPS_version; } $ret = (strlen($this->_url_host) && strlen($this->_url_path) && strlen($this->_url_port))? TRUE : FALSE; return $ret; } ## Prepare URL REQUEST function prepare_url_request() { if (strlen(trim($this->_url_query))) $request_path = $this->_url_path."?".$this->_url_query; else $request_path = $this->_url_path; $headers = HTTP_method." ".$request_path." ".$this->_url_method."\r\nHost: ".$this->_url_host."\r\nConnection: Close\r\n"; if(!empty($this->agent)) $headers.= "User-Agent: ".$this->agent."\r\n"; /* if(!empty($this->_url_host) && !isset($this->rawheaders['Host'])) $headers.= "Host: ".$this->_url_host."\r\n"; */ if(!empty($this->accept)) $headers.= "Accept: ".$this->accept."\r\n"; if(!empty($this->referer)) $headers.= "Referer: ".$this->referer."\r\n"; if(!empty($this->rawheaders)) { if(!is_array($this->rawheaders)) $this->rawheaders = (array)$this->rawheaders; while(list($headerKey,$headerVal) = each($this->rawheaders)) { $headers.= $headerKey.": ".$headerVal."\r\n"; } } if(!empty($this->_url_user) || !empty($this->_url_password)) $headers.= "Authorization: Basic ".base64_encode($this->_url_user.":" . $this->_url_password)."\r\n"; $headers.= "\r\n"; if (!empty($headers)) $this->_socket_request_line = $headers; return void; } ## Open connection to server through socket function socket_connect(&$fp) { $fp = @fsockopen($this->_url_host, $this->_url_port, $errno, $errstr, SOCKET_STREAM_TIMEOUT); $this->_socket_status = $fp? SOCKET_OPENED : SOCKET_OPEN_ERROR; if ($this->_socket_status == SOCKET_OPENED) $this->_socket_set_blocking($this->_socket, $this->_socket_blocking_status); return void; } ## Close opened connection function socket_disconnect() { if ($this->_socket) $this->_socket = @fclose($this->_socket); $this->_socket_status = SOCKET_CLOSED; return void; } ## Set socket connection type (blocking/non-blocking:deafult) function _socket_set_blocking(&$fp, $status) { @set_socket_blocking($fp, $status); return void; } function socket_read_all() { if ($this->_socket_status != SOCKET_URL_ERROR) { $this->socket_connect($this->_socket); if ($this->_socket_status == SOCKET_OPENED) { // Prepare URL request $this->prepare_url_request(); // Write to socket $this->_socket_status = SOCKET_WRITE; if (!$this->socket_write($this->_socket_request_line)) { $ret = ""; } // Read from socket $this->_socket_status = SOCKET_READ; $start_time = getmicrotime(); while(!feof($this->_socket)) { $this->socket_read(FALSE); $elapsed = getmicrotime() - $start_time; if($elapsed > 10000) { $ret = ""; break; } } $ret = $this->_socket_output; } } return $ret; } ## Read from socket function socket_read($NewLine = TRUE) { $data = @fread($this->_socket, MaxLinelen); if (strlen($data)) { $this->_socket_output_last_line = $data; $this->_socket_output.= $data; if($NewLine) $this->_socket_output .= "\n"; } else $this->_socket_output_last_line = FALSE; $this->_socket_read_status = !strlen($data)? FALSE : TRUE; return $this->_socket_output_last_line; } ## Write to socket function socket_write($data) { $ret = @fwrite($this->_socket, $data, strlen($data)); $this->_socket_write_status = $ret; return $ret; } ## Parse Output function parse_output($line, $type = 1) { if ($type == 1) // To get the page status { if(eregi("HTTP/1.(.) ([0-9]*) (.*)", $line, $parts)) { $result = ($parts[2] < "400")? 1 : 0; $this->_socket_return_code = $parts[2]; $this->_socket_status = SOCKET_COMPLETED; $this->_validation_status = $result? TRUE : FALSE; } } elseif ($type == 2) // To get the entire page { } return void; } } ?>