File Management Functions | > Script written by Matt Mecham | > Date started: Tuesday 22nd February 2005 (16:55) I have cold feet | +--------------------------------------------------------------------------- */ /** * Class File Management * * Wrapper for getting file contents * * @package IPS_KERNEL * @author Matt Mecham * @version 2.1 */ if ( ! defined( 'IPS_CLASSES_PATH' ) ) { /** * Define classes path */ define( 'IPS_CLASSES_PATH', dirname(__FILE__) ); } /** * Class File Management * * Wrapper for getting file contents * * @package IPS_KERNEL * @author Matt Mecham * @version 2.1 */ class class_file_management { /** * Use sockets flag * * @var integer */ var $use_sockets = 0; /** * Error array * * @var array */ var $errors = array(); /**#@+ * Set Authentication * * @var strings */ var $auth_req = 0; var $auth_user; var $auth_pass; /**#@-*/ /*-------------------------------------------------------------------------*/ // Constructor /*-------------------------------------------------------------------------*/ function class_file_management() { } /*-------------------------------------------------------------------------*/ // Get file contents (accepts URL or path) // file_get_contents() has caching issues /*-------------------------------------------------------------------------*/ /** * Get file contents (accepts URL or path) * * file_get_contents() has caching issues * @return string File data */ function get_file_contents( $file_location ) { //------------------------------- // INIT //------------------------------- $contents = ""; $file_location = str_replace( '&', '&', $file_location ); //------------------------------- // Hello //------------------------------- if ( ! $file_location ) { return FALSE; } if ( ! stristr( $file_location, 'http://' ) ) { //------------------------------- // It's a path! //------------------------------- if ( ! file_exists( $file_location ) ) { $this->errors[] = "Ôàéë '$file_location' íå ñóùåñòâóåò, ïîæàëóéñòà ïðîâåðüòå íàñòðîéêè ññûëêè íà øàáëîíû."; return; } $contents = $this->get_contents_with_fopen( $file_location ); } else { //------------------------------- // Is URL - using GET? //------------------------------- if ( $this->use_sockets ) { $contents = $this->get_contents_with_socket( $file_location ); } else { $contents = $this->get_contents_with_fopen( $file_location ); } } return $contents; } /*-------------------------------------------------------------------------*/ // USE FOPEN TO GET TEXT /*-------------------------------------------------------------------------*/ /** * Get file contents (with PHP's fopen) * * @return string File data */ function get_contents_with_fopen( $file_location ) { //------------------------------- // INIT //------------------------------- $buffer = ""; @clearstatcache(); if ( $FILE = fopen( $file_location, "r") ) { while ( ! feof( $FILE ) ) { $buffer .= fgets($FILE, 4096); } fclose($FILE); } return $buffer; } /*-------------------------------------------------------------------------*/ // USE SOCKET TO GET TEXT /*-------------------------------------------------------------------------*/ /** * Get file contents (with sockets) * * @return string File data */ function get_contents_with_socket( $file_location ) { //------------------------------- // INIT //------------------------------- $data = null; $fsocket_timeout = 10; //------------------------------- // Parse URL //------------------------------- $url_parts = parse_url($file_location); if ( ! $url_parts['host'] ) { $this->errors[] = "No host found in the URL '$file_location'!"; return FALSE; } //------------------------------- // Finalize //------------------------------- $host = $url_parts['host']; $port = ( isset($url_parts['port']) ) ? $url_parts['port'] : 80; //------------------------------- // Tidy up path //------------------------------- if ( !empty( $url_parts["path"] ) ) { $path = $url_parts["path"]; } else { $path = "/"; } if ( !empty( $url_parts["query"] ) ) { $path .= "?" . $url_parts["query"]; } //------------------------------- // Open connection //------------------------------- if ( ! $fp = @fsockopen( $host, $port, $errno, $errstr, $fsocket_timeout ) ) { $this->errors[] = "CONNECTION REFUSED FROM $host"; return FALSE; } else { $final_carriage = ""; if( ! $this->auth_req ) { $final_carriage = "\r\n"; } if ( ! fputs( $fp, "GET $path HTTP/1.0\r\nHost:$host\r\nConnection: Keep-Alive\r\n{$final_carriage}" ) ) { $this->errors[] = "Unable to send request to $host!"; return FALSE; } if( $this->auth_req ) { if( $this->auth_user && $this->auth_pass ) { $header = "Authorization: Basic ".base64_encode("{$this->auth_user}:{$this->auth_pass}")."\r\n\r\n"; if ( ! fputs( $fp, $header ) ) { $this->errors[] = "Authorization Failed!"; return FALSE; } } } } @stream_set_timeout($fp, $fsocket_timeout); $status = @socket_get_status($fp); while( ! feof($fp) && ! $status['timed_out'] ) { $data .= fgets ($fp,8192); $status = socket_get_status($fp); } fclose ($fp); //------------------------------- // Strip headers //------------------------------- $tmp = split("\r\n\r\n", $data, 2); $data = $tmp[1]; return $data; } /*-------------------------------------------------------------------------*/ // USE cURL TO GET TEXT /*-------------------------------------------------------------------------*/ /** * Get file contents (with cURL) * * @return string File data */ function get_contents_with_curl( $file_location ) { if ( function_exists( 'curl_init' ) ) { $ch = curl_init( $file_location ); curl_setopt( $ch, CURLOPT_HEADER , 0); curl_setopt( $ch, CURLOPT_TIMEOUT , 15 ); curl_setopt( $ch, CURLOPT_POST , 0 ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER , 1 ); $data = curl_exec($ch); curl_close($ch); return $data ? $data : FALSE; } else { return FALSE; } } } ?>