Location: PHPKode > scripts > WGetAgent > wgetagent/wget.inc.php
<?

$WGET_ERROR_ALL_OK          = 0;
$WGET_ERROR_CANNOT_RUN      = 1; // cannot find exec function
$WGET_ERROR_WGET_NOT_FOUND  = 2; // wget executable not found in current path
$WGET_ERROR_DEST_NOT_FOUND  = 3; // destination folder not found
$WGET_ERROR_DEST_NO_RIGHTS  = 4; // no rights to write in destination folder
$WGET_ERROR_STORE_FUNC      = 5; // user defined store function not found
$WGET_ERROR_SCRIPT_ERROR    = 6; // script that runs after download not found or not executable
$WGET_ERROR_RM_NOT_FOUND    = 7; // script that runs after download not found or not executable

class wget_agent
{
    var $wget_url; // url to connect to
    var $wget_user, $wget_pass; // user/pass to connect to ftp server
    var $wget_prog; // location of wget binary executable...
    var $wget_destination; // location where wget will put the file
    var $wget_session; // Session id in which wget runs (unique string)
    var $wget_limit_rate; // max download rate for this wget session (default is in bytes, but u can specify Kb: 20K)
    var $wget_passive_transfer; // whether the transfer will be passive or not (default is passive)

    // log files prefix... During download wget and wget_agent class generates log files.
    // this variable holds log files prefix (default wga)
    var $wget_log_files_prefix;

    var $cleanup_logs; // wheather to clean all files created at destination after download (0/1)

    // Store function will get as parameters:
    // function my_wget_store( $session_id, $url, $dest_dir )
    // session_id: string that identifies the session id of current wget execution
    // url: from where did the download start
    // dest_dir: where control and download files are
    var $wget_store_function; // user definded function that will store session id into table/file
    var $wget_store_function_class; // useful when u want to run function from within other classes...

    var $wget_script; // a script that will be run AFTER download is complete
    var $wget_script_dir; // script directory. B4 running the script directory will be changed at this location
    
    // character that links parameters for given script
    // by default it is " " (CGI or normal linux executable), but if u use a PHP script and want parameters passed as a url query u will use & as separator
    var $wget_link_param_char;

    var $error_no, $error_msg;
    
    function wget_agent( $url, $destination, $limit_rate = "", $user = "", $pass = "" )
    {
        global $WGET_ERROR_CANNOT_RUN, $WGET_ERROR_WGET_NOT_FOUND, $WGET_ERROR_DEST_NOT_FOUND, $WGET_ERROR_DEST_NO_RIGHTS,
               $WGET_ERROR_ALL_OK;

        // testing exec function
        $arr = get_defined_functions();
        if( !isset( $arr["internal"] )
         or !@function_exists( "exec" )
         or !in_array( "exec", $arr["internal"] ) )
        {
            $this->error_no = $WGET_ERROR_CANNOT_RUN;
            $this->error_msg = "wget_agent::wget_agent [".__FILE__.":".__LINE__."] - Cannot run commands from within PHP. (exec function doesn't exist or is disabled)";
            return;
        }

        // testing if wget is installed and is set in path
        if( !strlen( ($this->wget_prog = @exec( "which wget" )) ) )
        {
            $this->error_no = $WGET_ERROR_WGET_NOT_FOUND;
            $this->error_msg = "wget_agent::wget_agent [".__FILE__.":".__LINE__."] - Cannot find wget executable. Be sure wget directory is in %PATH% environment variable or wget is properly installed.";
            return;
        }

        $this->wget_url = $url;
        
        $this->wget_limit_rate = $limit_rate;
        $this->wget_passive_transfer = 1;
        $this->wget_user = $user;
        $this->wget_pass = $pass;
        $this->wget_destination = $destination.(substr( $destination, -1, 1 )=="/"?"":"/");

        // script that will be run after download completes...
        $this->wget_script = "";
        $this->wget_script_dir = "";
        $this->wget_link_param_char = " ";

        // session ID... an unique string which identifies a wget download
        $this->wget_session = md5( uniqid( rand(), true ) );
        
        // store function
        $this->wget_store_function = "";
        $this->wget_store_function_class = "";
        $this->wget_store_parameter = ""; // this can be an object passed to store function or any extra parameter
        
        // by default, delete all logs created while downloading
        $this->cleanup_logs = true;

        // log files name prefix
        $this->wget_log_files_prefix = "wga";

        // testing if destination exists
        if( !@file_exists( $this->wget_destination ) )
        {
            $this->error_no = $WGET_ERROR_DEST_NOT_FOUND;
            $this->error_msg = "wget_agent::wget_agent [".__FILE__.":".__LINE__."] - Cannot find destination directory: ".$this->wget_destination.".";
            return;
        }
        
        // testing if we can write in destination dir
        $test_file = $this->wget_destination.$this->wget_session.".tst";
        if( !@touch( $test_file ) )
        {
            $this->error_no = $WGET_ERROR_DEST_NO_RIGHTS;
            $this->error_msg = "wget_agent::wget_agent [".__FILE__.":".__LINE__."] - Cannot write to destination directory: ".$this->wget_destination.".";
            return;
        } else
            @unlink( $test_file );

        // class inited ok
        $this->error_no = $WGET_ERROR_ALL_OK;
        $this->error_msg = "wget_agent::wget_agent [".__FILE__.":".__LINE__."] - Class successful inited. User didn't start the download.";
    }

    // user-control wget session store function
    // store function will be called after download is started in background
    function wget_store_function( $func, $class = "" )
    {
        global $WGET_ERROR_STORE_FUNC, $WGET_ERROR_ALL_OK;

        if( $this->error_no != $WGET_ERROR_ALL_OK )
            return;

        if( !is_string( $class ) )
        {
            $this->error_no = $WGET_ERROR_STORE_FUNC;
            $this->error_msg = "wget_agent::wget_store_function [".__FILE__.":".__LINE__."] - Second paramater must be a string with the name of class/object.";
            return;
        }

        $using_class = strlen( $class );

        // check if store function/method exists
        if( !$using_class and !@function_exists( $func ) )
        {
            $this->error_no = $WGET_ERROR_STORE_FUNC;
            $this->error_msg = "wget_agent::wget_store_function [".__FILE__.":".__LINE__."] - Store function ".$func." not defined.";
            return;
        } elseif( $using_class and !isset( $GLOBALS[$class] ) )
        {
            $this->error_no = $WGET_ERROR_STORE_FUNC;
            $this->error_msg = "wget_agent::wget_store_function [".__FILE__.":".__LINE__."] - Object ".$class." not defined.";
            return;
        } elseif( $using_class and !is_object( $GLOBALS[$class] ) )
        {
            $this->error_no = $WGET_ERROR_STORE_FUNC;
            $this->error_msg = "wget_agent::wget_store_function [".__FILE__.":".__LINE__."] - Variable ".$class." is not an object, it's type is: ".gettype( $GLOBALS[$class] ).".";
            return;
        } elseif( $using_class and !method_exists( $GLOBALS[$class], $func ) )
        {
            $this->error_no = $WGET_ERROR_STORE_FUNC;
            $this->error_msg = "wget_agent::wget_store_function [".__FILE__.":".__LINE__."] - Store function ".get_class($GLOBALS[$class])."::".$func." not defined.";
            return;
        }

        $this->wget_store_function = $func;
        $this->wget_store_function_class = $class;
    }

    // absolute path to a script must be given
    // this script will run after the download ends
    function wget_script( $script, $sep_char = " " )
    {
        global $WGET_ERROR_ALL_OK;

        if( $this->error_no != $WGET_ERROR_ALL_OK )
            return;

        $this->wget_script = $script;
        //$this->wget_script_dir = "";

        $this->wget_link_param_char = $sep_char;
    }

    // class will switch to this directory before running the script, but after download is complete
    function wget_script_dir( $dir = "" )
    {
        if( !strlen( $dir ) )
            return $this->wget_script_dir;
        else
        {
            if( !@file_exists( $dir ) )
            {
                global $WGET_ERROR_SCRIPT_ERROR;

                $this->error_no = $WGET_ERROR_SCRIPT_ERROR;
                $this->error_msg = "wget_agent::wget_script [".__FILE__.":".__LINE__."] - Directory ".$dir." not found... Path must be absolute.";
                return;
            }
            $this->wget_script_dir = $dir;
        }
    }

    // if there is a previous session started that must be continued use this method so class will use old logs
    function wget_session_id( $id = "" )
    {
        if( !strlen( $id ) )
            return $this->wget_session;
        else
            $this->wget_session = $id;
    }

    // 0/1 (true/false) whether to delete or not wget log file after download finishes
    function wget_cleanup_logs( $clean = -1 )
    {
        if( $clean == -1 )
            return $this->cleanup_logs;
        else
            $this->cleanup_logs = $clean;
    }

    // 0/1 (true/false) whether the transfer will be passive or not
    function wget_passive( $passive = -1 )
    {
        if( $passive == -1 )
            return $this->wget_passive_transfer;
        else
            $this->wget_passive_transfer = $passive;
    }

    // by default store function takes as parameters session id, url source and destination directory
    // if we want to pass an extra parameter (an object or an id) we pass it here
    function wget_extra_parameters( $parameter = "" )
    {
        if( !strlen( $parameter ) )
            return $this->wget_store_parameter;
        else
            $this->wget_store_parameter = $parameter;
    }
    
    // useful if we want to put log files in a diffrent directory in the destination directory
    function wget_log_prefix( $prefix = -1 )
    {
        if( $prefix == -1 )
            return $this->wget_log_files_prefix;
        else
            $this->wget_log_files_prefix = $prefix;
    }
    
    function wget_run()
    {
        global $WGET_ERROR_ALL_OK;

        if( $this->error_no != $WGET_ERROR_ALL_OK )
            return;
        
        $limit_str = "";
        if( strlen( $this->wget_limit_rate ) )
            $limit_str = " --limit-rate=".$this->wget_limit_rate;
        $passive_str = "";
        if( $this->wget_passive_transfer )
            $passive_str = "--passive-ftp ";

        $exec_file  = $this->wget_destination.$this->wget_log_files_prefix.$this->wget_session.".cmd";
        $cookie_jar = $this->wget_destination.$this->wget_log_files_prefix.$this->wget_session.".wgcookie";
        $log_file   = $this->wget_destination.$this->wget_log_files_prefix.$this->wget_session.".wglog";

        @touch( $cookie_jar );

        $cmd = $this->wget_prog." -o ".$log_file." -c -S".$limit_str." -nd -P ".$this->wget_destination.
               " --save-cookies ".$cookie_jar." --load-cookies ".$cookie_jar." ".$passive_str.
               "\"".$this->wget_url."\" 2>/dev/null >&- <&- >/dev/null\n";

        if( !strlen( ($rm_path = @exec( "which rm" )) ) )
        {
            global $WGET_ERROR_RM_NOT_FOUND;

            $this->error_no = $WGET_ERROR_RM_NOT_FOUND;
            $this->error_msg = "wget_agent::wget_run [".__FILE__.":".__LINE__."] - Cannot find rm executable. Be sure rm directory is in %PATH% environment variable.";
            return;
        }

        if( $this->wget_script )
        {
            if( strlen( $this->wget_script_dir ) )
                $cmd .= "cd ".$this->wget_script_dir."\n";
            $cmd .= $this->wget_script.$this->wget_link_param_char."wget_sessid=".$this->wget_session."\n";
        }
        
        if( $this->cleanup_logs )
        {
          $cmd .= $rm_path." -f ".$log_file."\n";
        }
        // we delete execution script and cookie file cuz they are junk anyway
        $cmd .= $rm_path." -f ".$exec_file."\n";
        $cmd .= $rm_path." -f ".$cookie_jar."\n";
        
        if( !($fil = @fopen( $exec_file, "w" )) )
        {
            global $WGET_ERROR_DEST_NO_RIGHTS;

            $this->error_no = $WGET_ERROR_DEST_NO_RIGHTS;
            $this->error_msg = "wget_agent::wget_run [".__FILE__.":".__LINE__."] - Couldn't create execution script: [".$exec_file."].";
            return;
        }

        fwrite( $fil, $cmd );
        fflush( $fil );
        fclose( $fil );

        chmod( $exec_file, 0775 );

        exec( $exec_file." 2>/dev/null >&- <&- >/dev/null &" );
        
        $this->error_no = $WGET_ERROR_ALL_OK;
        $this->error_msg = "wget_agent::wget_run [".__FILE__.":".__LINE__."] - Download started...";

        $ret = 0;
        // check if we have to run the store function
        if( strlen( $this->wget_store_function ) )
        {
            // if there is a store function it's return variable will be returned by wget_agent::wget_run
            if( strlen( $this->wget_store_function_class ) )
               $function_path = array( $GLOBALS[$this->wget_store_function_class], $this->wget_store_function );
            else
               $function_path = $this->wget_store_function;

            $ret = call_user_func( $function_path, $this->wget_session, $this->wget_url, $this->wget_destination, $this->wget_store_parameter );
        }

        return $ret;
    }

    function get_error()
    {
        return array( "error_msg" => $this->error_msg, "error_no" => $this->error_no );
    }
}
?>
Return current item: WGetAgent