Ejemplo n.º 1
0
function write_file($data, $file)
{
    global $path;
    backup_file($file);
    $open = fopen($path . $file, "w") or die("can't open file");
    fwrite($open, $data);
    fclose($open);
}
Ejemplo n.º 2
0
function curl($opts, $pipe, $cmd = __FUNCTION__, $default_protocol = null)
{
    # set up internal (which by default comes from there being a default protocol)
    if ($default_protocol) {
        $internal = true;
    } else {
        $internal = false;
    }
    # merge the opts
    $opts = merge_opts($opts, $pipe);
    # get prefix opt
    $prefix = get_opt('curl', $opts, 'prefix', 'curl');
    if (!check_opt_set_type($cmd, $prefix, 'prefix', 'string')) {
        return false;
    }
    # get execute opt
    $execute = get_opt_config_value($prefix, $opts, 'execute', true);
    if (!check_opt_set_type($cmd, $execute, 'execute', 'boolean')) {
        return false;
    }
    # check if we should execute or not
    if (!$execute) {
        debug_echo($cmd, "not executing {$prefix} request");
        return;
    }
    # get debug opt
    $debug = get_opt_config_value($prefix, $opts, 'debug', false);
    if (!check_opt_set_type($cmd, $debug, 'debug', 'boolean')) {
        return false;
    }
    ## saved files ##
    # get save_to_file opt
    $save_to_file = get_opt_config_value($prefix, $opts, 'save_to_file');
    if (!check_opt_if_set_type($cmd, $save_to_file, 'save_to_file', 'string')) {
        return false;
    }
    # get use saved file opt
    $use_saved_file = get_opt_config_value($prefix, $opts, 'use_saved_file', false);
    if (!check_opt_set_type($cmd, $use_saved_file, 'use_saved_file', 'boolean')) {
        return false;
    }
    # get backup old saved files
    $backup_old_saved_files = get_opt_config_value($prefix, $opts, 'backup_old_saved_files', false);
    if (!check_opt_set_type($cmd, $backup_old_saved_files, 'backup_old_saved_files', 'boolean')) {
        return false;
    }
    # check if file exists and back up if necessary
    if (file_exists($save_to_file) && $use_saved_file) {
        debug_echo($cmd, "using saved file instead of executing request : {$save_to_file}");
        return array('file' => $save_to_file, 'response_file' => $save_to_file);
    }
    # get curlopts opt
    $curlopts = get_opt($prefix, $opts, 'curlopts');
    if (!check_opt_if_set_type($cmd, $curlopts, 'curlopts', 'array')) {
        return false;
    }
    if (is_array($curlopts)) {
        $curlopts =& $opts['curlopts'];
    } else {
        $curlopts = array();
        $opts['curlopts'] =& $curlopts;
    }
    # get finalize_curlopts opt
    $finalize_curlopts = get_opt($prefix, $opts, 'finalize_curlopts');
    if (!check_opt_if_set_type($cmd, $finalize_curlopts, 'finalize_curlopts', 'array')) {
        return false;
    }
    # set up response
    $res = array();
    # call pre functions if internal and set
    if ($internal) {
        # get pre functions opt
        $init_functions = get_opt($prefix, $opts, 'init_functions');
        if (!check_opt_if_set_type($cmd, $init_functions, 'init_functions', 'array_of_strings')) {
            return false;
        }
        # get pre exec functions opt
        $pre_exec_functions = get_opt($prefix, $opts, 'pre_exec_functions');
        if (!check_opt_if_set_type($cmd, $pre_exec_functions, 'pre_exec_functions', 'array_of_strings')) {
            return false;
        }
        # get post exec functions opt
        $post_exec_functions = get_opt($prefix, $opts, 'post_exec_functions');
        if (!check_opt_if_set_type($cmd, $post_exec_functions, 'post_exec_functions', 'array_of_strings')) {
            return false;
        }
        # get finalize functions opt
        $finalize_functions = get_opt($prefix, $opts, 'finalize_functions');
        if (!check_opt_if_set_type($cmd, $finalize_functions, 'finalize_functions', 'array_of_strings')) {
            return false;
        }
        # set array of arrays to pass to hooked functions
        $func_args = array('curlopts' => &$curlopts, 'opts' => &$opts, 'res' => &$res);
        # call the pre functions
        if ($init_functions) {
            foreach ($init_functions as $function) {
                $r = call_user_func($function, $prefix, $func_args, $cmd, $debug);
                if ($r === false) {
                    return false;
                }
            }
        }
    }
    # get download progress update opt
    $download_progress_update = get_opt_config_value($prefix, $opts, 'download_progress_update', true);
    if (!check_opt_set_type($cmd, $download_progress_update, 'download_progress_update', 'boolean')) {
        return false;
    }
    # add download process update to curlopts if necessary
    if ($download_progress_update) {
        $curlopts['noprogress'] = false;
        if ($curlopts['progressfunction'] = 'curl_progress_function') {
        }
    } else {
        $curlopts['noprogress'] = true;
    }
    # get url opt
    do {
        # get url opt (from curlopts)
        $url = get_opt($prefix, $curlopts, 'url');
        if (!check_opt_if_set_type($cmd, $url, 'url', 'string')) {
            return false;
        }
        if ($url) {
            break;
        }
        # get url opt (from opts)
        $url = get_opt($prefix, $opts, 'url');
        if (!check_opt_if_set_type($cmd, $url, 'url', 'string')) {
            return false;
        }
        if ($url) {
            break;
        }
        # get server opt
        $tries = array('server', 'svr');
        foreach ($tries as $try) {
            $server = get_opt($prefix, $opts, $try);
            if (!check_opt_if_set_type($cmd, $server, $try, 'string')) {
                return false;
            }
            if ($server) {
                break;
            }
        }
        # get path opt
        $path = get_opt($prefix, $opts, 'path');
        if (!check_opt_if_set_type($cmd, $path, 'path', 'string')) {
            return false;
        }
        # build path from dir / file if necessary
        if (!$path) {
            # get dir opt
            $dir = get_opt($prefix, $opts, 'dir');
            if (!check_opt_if_set_type($cmd, $dir, 'dir', 'string')) {
                return false;
            }
            # get file opt
            $file = get_opt($prefix, $opts, 'file');
            if (!check_opt_if_set_type($cmd, $file, 'file', 'string')) {
                return false;
            }
            # build path from dir/file
            $path = '';
            if ($dir) {
                if (substr($dir, -1) != '/') {
                    $dir .= '/';
                }
                $path .= $dir;
            }
            if ($file) {
                $path .= $file;
            }
            if (!$path) {
                $path = '/';
            } elseif ($path[0] != '/') {
                $path = "/{$path}";
            }
        }
        # build url from server and path
        if ($server) {
            if (substr($server, -1) == '/') {
                $server = substr($server, 0, -1);
            }
            $url = "{$server}{$path}";
            break;
        }
        return error($cmd, "no 'url' option set, and cannot be constructed from a 'server' and an optional 'path' option");
    } while (false);
    # get protocol opt
    $protocol = get_opt($prefix, $opts, 'protocol');
    if (!check_opt_if_set_type($cmd, $protocol, 'protocol', 'enum:ftp|ftps|http|https')) {
        return false;
    }
    # check the protocol is valid / accepted
    if (!$protocol) {
        $substr_6 = substr($url, 0, 6);
        $substr_7 = substr($url, 0, 7);
        $substr_8 = substr($url, 0, 8);
        if ($substr_6 == 'ftp://') {
            $protocol = 'ftp';
        } elseif ($substr_7 == 'ftps://') {
            $protocol = 'ftps';
        } elseif ($substr_7 == 'http://') {
            $protocol = 'http';
        } elseif ($substr_8 == 'https://') {
            $protocol = 'https';
        } elseif ($default_protocol) {
            $protocol = $default_protocol;
        } else {
            return error($cmd, "protocol '{$protocol}' is not supported");
        }
        $opts['protocol'] = $protocol;
    }
    # check if the url has the protocol included
    if (substr($url, 0, strlen($protocol) + 3) != "{$protocol}://") {
        $url = "{$protocol}://{$url}";
    }
    # check if the path is a directory, and add a slash if so
    if (substr($url, -1) != '/') {
        # get path is dir opt
        $path_is_dir = get_opt($prefix, $opts, 'path_is_dir', false);
        if (!check_opt_set_type($cmd, $path_is_dir, 'path_is_dir', 'boolean')) {
            return false;
        }
        # add slash to end if necessary
        if ($path_is_dir) {
            $url .= '/';
        }
    }
    # set the url
    $curlopts['url'] = $url;
    # get port opt
    $port = get_opt($prefix, $opts, 'port');
    if (!check_opt_if_set_type($cmd, $port, 'port', 'integer')) {
        return false;
    }
    # set the port curlopt
    if (!$port) {
        $port = $GLOBALS['curl_default_ports'][$protocol];
    }
    $curlopts['port'] = $port;
    # get user opt
    $tries = array('user', 'usr');
    foreach ($tries as $try) {
        $user = get_opt($prefix, $opts, $try);
        if (!check_opt_if_set_type($cmd, $user, $try, 'string')) {
            return false;
        }
        if ($user) {
            break;
        }
    }
    # get password opt
    $tries = array('password', 'pwd');
    foreach ($tries as $try) {
        $password = get_opt($prefix, $opts, $try);
        if (!check_opt_if_set_type($cmd, $password, $try, 'string')) {
            return false;
        }
        if ($password) {
            break;
        }
    }
    # set the authentication
    if ($user && $password) {
        $curlopts['userpwd'] = "{$user}:{$password}";
    }
    # get curlinfo opt
    $curlinfo = get_opt($prefix, $opts, 'curlinfo');
    if (!check_opt_if_set_type($cmd, $curlinfo, 'curlinfo', 'array_of_strings')) {
        return false;
    }
    # get request retries opt
    $request_retries = get_opt_config_value($prefix, $opts, 'request_retries', 3);
    if (!check_opt_set_type($cmd, $request_retries, 'request_retries', 'integer')) {
        # change to having max 10
        return false;
    }
    ## messages ##
    # get exec msg opt
    $exec_msg = get_opt($prefix, $opts, 'exec_msg', "executing curl request to url : {$url}");
    if (!check_opt_set_type($cmd, $exec_msg, 'exec_msg', 'string,boolean')) {
        return false;
    }
    # get exec retry msg opt
    $exec_retry_msg = get_opt($prefix, $opts, 'exec_retry_msg', "previous request failed - trying again");
    if (!check_opt_set_type($cmd, $exec_retry_msg, 'exec_retry_msg', 'string,boolean')) {
        return false;
    }
    # get exec retry msg opt
    $success_msg = get_opt($prefix, $opts, 'success_msg', "request successful");
    if (!check_opt_set_type($cmd, $success_msg, 'success_msg', 'string,boolean')) {
        return false;
    }
    # get exec retry msg opt
    $fail_msg = get_opt($prefix, $opts, 'fail_msg', "request failed");
    if (!check_opt_set_type($cmd, $fail_msg, 'fail_msg', 'string,boolean')) {
        return false;
    }
    ## save to locations ##
    # get save request head to file opt
    $request_head_file = get_opt($prefix, $opts, 'save_request_head_to_file');
    if (!check_opt_if_set_type($cmd, $request_head_file, 'save_request_head_to_file', 'string')) {
        return false;
    }
    # get save request body to file opt
    $request_body_file = get_opt($prefix, $opts, 'save_request_body_to_file');
    if (!check_opt_if_set_type($cmd, $request_body_file, 'save_request_body_to_file', 'string')) {
        return false;
    }
    # get save head to file opt
    $response_head_file = get_opt($prefix, $opts, 'save_head_to_file');
    if (!check_opt_if_set_type($cmd, $response_head_file, 'save_head_to_file', 'string')) {
        return false;
    }
    # get save body to file opt
    $response_body_file = get_opt($prefix, $opts, 'save_body_to_file');
    if (!check_opt_if_set_type($cmd, $response_body_file, 'save_body_to_file', 'string')) {
        return false;
    }
    # get max saved body size opt
    $max_saved_body_size = get_opt_config_value($prefix, $opts, 'max_saved_body_size', 512 * 1024 * 1024);
    if (!check_opt_if_set_type($cmd, $max_saved_body_size, 'max_saved_body_size', 'integer')) {
        return false;
    }
    # get stderr opt
    $stderr = get_opt_config_value($prefix, $opts, 'stderr');
    if (!check_opt_if_set_type($cmd, $stderr, 'stderr', 'string')) {
        return false;
    }
    # set stderr curlopt if necessary
    if ($stderr) {
        if (!make_dir_for_saving_file($stderr, null, $cmd)) {
            return false;
        }
        $stderr_handle = fopen($stderr, 'w');
        $curlopts['stderr'] = $stderr_handle;
    }
    # get the connection
    #$conn = curl_get_conn ($prefix, $opts, $cmd);
    #if (!$conn)
    #  return  false;
    #$opts['conn'] = $conn;
    #$c = $conn['handle'];
    /*
    $c = curl_init ();
    
    if (!$c)
      return  error ($cmd, "could not create curl resource");
    */
    # execute pre-exec functions
    if ($internal && $pre_exec_functions) {
        foreach ($pre_exec_functions as $function) {
            $r = call_user_func($function, $prefix, $func_args, $cmd, $debug);
            if ($r === false) {
                return false;
            }
        }
    }
    # set up the options
    if (!curl_set_curlopts($prefix, $protocol, $curlopts, $cmd, $c, $internal, $debug)) {
        return false;
    }
    if ($save_to_file) {
        # backup old response file if set and exists
        if (is_file($save_to_file) && $backup_old_saved_files && !backup_file($save_to_file, null, $cmd)) {
            return error($cmd, "could not backup previously saved file : {$save_to_file}");
        }
        # create parent directory of response file
        if (!make_dir(dirname($save_to_file), $opts, $cmd)) {
            return false;
        }
        /*
        # create file handler for response file
          
        $save_to_file_handle = @fopen ($save_to_file, "w");
          
        if (!$save_to_file_handle)
          return  error ($cmd, "could not open response file : $save_to_file");
        */
        # add response file handler to request
        curl_setopt($c, CURLOPT_FILE, $save_to_file_handle);
        # display message and add to $res
        debug_echo($cmd, "saving response to file : {$save_to_file}");
        $res['response_file'] = $save_to_file;
        $res['file'] = $save_to_file;
    }
    # display exec message
    if ($exec_msg) {
        debug_echo($cmd, "{$exec_msg} ...");
    }
    # execute request
    $success = false;
    for ($i = 0; $i < $request_retries; $i++) {
        $r = curl_exec($c);
        if ($r) {
            $success = true;
            break;
        } elseif ($exec_retry_msg) {
            debug_echo($cmd, "{$exec_retry_msg} ...");
        }
    }
    # close file handles
    if (@$save_to_file_handle) {
        fclose($save_to_file_handle);
    }
    if (@$stderr_handle) {
        fclose($stderr_handle);
    }
    # display message about result (we add space to cover up any download monitor)
    if ($success) {
        if ($success_msg) {
            debug_echo($cmd, "{$success_msg}                     ");
        }
    } elseif ($fail_msg) {
        $curl_error = strtolower(curl_error($c));
        $fail_msg .= ", {$curl_error}";
        return error($cmd, "{$fail_msg}                        ");
    }
    # execute post-exec functions
    if ($internal && $post_exec_functions) {
        foreach ($post_exec_functions as $function) {
            $r = call_user_func($function, $prefix, $func_args, $cmd, $debug);
            if ($r === false) {
                return false;
            }
        }
    }
    # split the response into header and body
    $request_head = curl_getinfo($c, CURLINFO_HEADER_OUT);
    $response_head_size = curl_getinfo($c, CURLINFO_HEADER_SIZE);
    if ($save_to_file) {
        # get response body size
        $handle = fopen($save_to_file, 'r');
        $response_head = $response_head_size > 0 ? trim(fread($handle, $response_head_size)) : '';
        $stat = fstat($handle);
        $response_body_size = $stat['size'] - $response_head_size;
        # check max size of response body file before putting in separate file
        if ($response_body_size > $max_saved_body_size) {
            warning($cmd, "response body size is greater than max, so is not set : {$response_body_size} > {$max_saved_body_size}");
            $response_body = "[{$save_to_file}]";
            $no_reset_response_body_size = true;
        } else {
            $response_body = $response_body_size > 0 ? trim(fread($handle, $response_body_size)) : '';
        }
        fclose($handle);
    } else {
        $response_head = trim(substr($r, 0, $response_head_size));
        $response_body = trim(substr($r, $response_head_size));
    }
    $response_head_size = strlen($response_head);
    if (!@$no_reset_response_body_size) {
        $response_body_size = strlen($response_body);
    }
    # add main request and response variables to $res
    $res['request_url'] = $url;
    $res['request_head_size'] = strlen($request_head);
    $res['response_head_size'] = $response_head_size;
    $res['response_body_size'] = $response_body_size;
    # save data to files if set and save in $res
    $save_to_files = array('request_head' => array($request_head_file, $request_head), 'response_head' => array($response_head_file, $response_head), 'response_body' => array($response_body_file, $response_body));
    # save data to files if set and save in $res
    foreach ($save_to_files as $name => $vars) {
        $file = $vars[0];
        $data = $vars[1];
        if ($file) {
            $report_name = str_replace('_', ' ', $name);
            if (is_file($file) && $backup_old_saved_files && !backup_file($file, null, $cmd)) {
                return error($cmd, "could not backup previously saved {$report_name} file : {$file}");
            }
            if (strlen($data) == 0) {
                warning($cmd, "{$report_name} is of zero size, so shall not be saved to file : {$file}");
            } else {
                if (!file_put_contents($file, $data)) {
                    return error($cmd, "could not save {$report_name} to file : {$file}");
                }
                debug_echo($cmd, "{$report_name} saved to file : {$file}");
                $res["{$name}_file"] = $file;
            }
        }
        $res[$name] = $data;
    }
    # finalize curlopts
    if ($finalize_curlopts) {
        if (!curl_set_curlopts($prefix, $protocol, $finalize_curlopts, $cmd, $c, $internal, $debug, true)) {
            return false;
        }
    }
    # execute finalize functions
    if ($internal && $finalize_functions) {
        foreach ($finalize_functions as $function) {
            $r = call_user_func($function, $prefix, $func_args, $cmd, $debug);
            if ($r === false) {
                return false;
            }
        }
    }
    # sort $res, display and return
    ksort($res);
    curl_close($c);
    return $res;
}
Ejemplo n.º 3
0
function http_request($opts, $pipe = null, $cmd = __FUNCTION__)
{
    # set prefix
    $prefix = 'http';
    # merge opts
    $opts = merge_opts($opts, $pipe, 'url');
    # get execute opt
    $execute = get_opt_config_value($prefix, $opts, 'execute', true);
    if (!check_opt_set_type($cmd, $execute, 'execute', 'boolean')) {
        return false;
    }
    # check if we should execute or not
    if (!$execute) {
        return;
    }
    # get debug opt
    $debug = get_opt_config_value($prefix, $opts, 'debug', false);
    if (!check_opt_set_type($cmd, $debug, 'debug', 'boolean')) {
        return false;
    }
    # get request method opt
    $request_method = get_opt_config_value($prefix, $opts, 'request_method', 'get');
    echo "{$request_method}\\[1]\n";
    if (!check_opt_if_set_type($cmd, $request_method, 'request_method', 'http_request_method')) {
        return false;
    }
    # get post args opt
    $post_args = get_opt($prefix, $opts, 'post_args');
    if (!check_opt_if_set_type($cmd, $post_args, 'post_args', 'array')) {
        return false;
    }
    # get post form opt
    $form_name = get_opt($prefix, $opts, 'form_name');
    if (!check_opt_if_set_type($cmd, $form_name, 'form_name', 'string')) {
        return false;
    }
    # get response body opt
    $response_body = get_opt($prefix, $opts, 'response_body');
    if (!check_opt_if_set_type($cmd, $response_body, 'response_body', 'string')) {
        return false;
    }
    # check to see if we're posting
    if ($post_args || $form_name) {
        $request_method = 'post';
    }
    if ($request_method == 'post' && $response_body) {
        debug_echo($cmd, "performing a post request and there was a previous request, so will search for action, method and inputs");
        $opts = http_parse_form($opts, $form_name, $cmd);
        if ($opts === false) {
            return false;
        }
        $post_args = $opts['post_args'];
    }
    echo "{$request_method}\\[2\\]\n";
    # get base url from url|response_location|request opt
    $base_url_opts = array('url', 'response_location', 'form_action', 'response_form_action', 'request_url');
    foreach ($base_url_opts as $opt) {
        $base_url = get_opt($prefix, $opts, $opt);
        if (!check_opt_if_set_type($cmd, $base_url, $opt, 'string')) {
            return false;
        }
        if ($base_url) {
            break;
        }
    }
    if (!$base_url) {
        return opt_not_set_msg($cmd, implode(',', $base_url_opts));
    }
    # get get args opt
    $get_args = get_opt($prefix, $opts, 'get_args');
    if (!check_opt_if_set_type($cmd, $get_args, 'get_args', 'array,string')) {
        return false;
    }
    # get headers opt
    $request_headers = get_opt($prefix, $opts, 'headers');
    if (!check_opt_if_set_type($cmd, $request_headers, 'headers', 'array_of_strings')) {
        return false;
    }
    # get auto redirect opt
    $auto_redirect = get_opt($prefix, $opts, 'auto_redirect', true);
    if (!check_opt_set_type($cmd, $auto_redirect, 'auto_redirect', 'boolean')) {
        return false;
    }
    # get authorization opt
    $authorization = get_opt($prefix, $opts, 'authorization');
    if (!check_opt_if_set_type($cmd, $authorization, 'authorization', 'array')) {
        return false;
    }
    # set authorization if necessary
    if ($authorization) {
        # get authorization:usr opt
        $usr = get_opt($prefix, $authorization, 'usr');
        if (!check_opt_set_type($cmd, $usr, 'authorization:usr', 'string,integer')) {
            return false;
        }
        $usr = (string) $usr;
        # get authorization:pwd opt
        $pwd = get_opt($prefix, $authorization, 'pwd');
        if (!check_opt_set_type($cmd, $pwd, 'authorization:pwd', 'string,integer')) {
            return false;
        }
        $pwd = (string) $pwd;
        # get authorization:type opt
        $type = get_opt($prefix, $authorization, 'type', 'Basic');
        if (!check_opt_set_type($cmd, $type, 'authorization:type', 'string')) {
            return false;
        }
        # add the authorization header to the list of headers
        $auth_header = "Authorization: {$type} " . base64_encode("{$usr}:{$pwd}");
        if (!$request_headers) {
            $request_headers = array();
        }
        $request_headers[] = $auth_header;
    }
    # get request retries opt
    $request_retries = get_opt_config_value($prefix, $opts, 'request_retries', 3);
    if (!check_opt_set_type($cmd, $request_retries, 'request_retries', 'integer')) {
        return false;
    }
    # get download msg opt
    $download_msg = get_opt($prefix, $opts, 'download_msg');
    if (!check_opt_if_set_type($cmd, $download_msg, 'download_msg', 'string')) {
        return false;
    }
    # get use downloaded file opt
    $use_saved_file = get_opt_config_value($prefix, $opts, 'use_saved_file', true);
    if (!check_opt_set_type($cmd, $use_saved_file, 'use_saved_file', 'boolean')) {
        return false;
    }
    # get backup old downloaded files
    $backup_old_saved_files = get_opt_config_value($prefix, $opts, 'backup_old_saved_files', true);
    if (!check_opt_set_type($cmd, $backup_old_saved_files, 'backup_old_saved_files', 'boolean')) {
        return false;
    }
    # get checked downloaded file opt
    $checked_saved_file = get_config_value('http_checked_saved_file');
    if (!check_opt_if_set_type($cmd, $checked_saved_file, 'http_checked_saved_file', 'string')) {
        return false;
    }
    # get download to file opt
    $save_to_file = get_opt($prefix, $opts, 'save_to_file');
    if (!check_opt_if_set_type($cmd, $save_to_file, 'save_to_file', 'string')) {
        return false;
    }
    # get download progress update opt
    $download_progress_update = get_opt_config_value($prefix, $opts, 'download_progress_update', true);
    if (!check_opt_set_type($cmd, $download_progress_update, 'download_progress_update', 'boolean')) {
        return false;
    }
    # get max response body size opt
    $max_response_body_size = get_opt_config_value($prefix, $opts, 'max_response_body_size', 32 * 1024 * 1024);
    if (!check_opt_if_set_type($cmd, $max_response_body_size, 'max_response_body_size', 'integer')) {
        return false;
    }
    # get save request head to file opt
    $request_head_file = get_opt($prefix, $opts, 'save_request_head_to_file');
    if (!check_opt_if_set_type($cmd, $request_head_file, 'save_request_head_to_file', 'string')) {
        return false;
    }
    # get save request body to file opt
    $request_body_file = get_opt($prefix, $opts, 'save_request_body_to_file');
    if (!check_opt_if_set_type($cmd, $request_body_file, 'save_request_body_to_file', 'string')) {
        return false;
    }
    # get save head to file opt
    $response_head_file = get_opt($prefix, $opts, 'save_head_to_file');
    if (!check_opt_if_set_type($cmd, $response_head_file, 'save_head_to_file', 'string')) {
        return false;
    }
    # get save body to file opt
    $response_body_file = get_opt($prefix, $opts, 'save_body_to_file');
    if (!check_opt_if_set_type($cmd, $response_body_file, 'save_body_to_file', 'string')) {
        return false;
    }
    # get parse response form opt
    $parse_response_form = get_opt_config_value($prefix, $opts, 'parse_response_form');
    if (!check_opt_if_set_type($cmd, $parse_response_form, 'parse_response_form', 'string')) {
        return false;
    }
    # get parse response hidden inputs opt
    $parse_response_form_args = get_opt_config_value($prefix, $opts, 'parse_response_form_args', false);
    if (!check_opt_set_type($cmd, $parse_response_form_args, 'parse_response_form_args', 'boolean')) {
        return false;
    }
    # check to see if we should use a downloaded file
    if ($use_saved_file) {
        # check if response body file exists
        if (file_exists($response_body_file)) {
            debug_echo($cmd, "using saved response body file instead of executing the HTTP request : {$response_body_file}");
            $res['response_body_file'] = $response_body_file;
            $res['response_file'] = $response_body_file;
            $res['file'] = $response_body_file;
            return $res;
        }
        # check if the save to file exists
        if (file_exists($save_to_file)) {
            debug_echo($cmd, "using saved file instead of executing the HTTP request : {$save_to_file}");
            $res['response_file'] = $save_to_file;
            $res['file'] = $save_to_file;
            return $res;
        }
        # return immediately if there's a checked downloaded file
        if ($checked_saved_file) {
            $res['response_file'] = $checked_saved_file;
            $res['file'] = $checked_saved_file;
            return $res;
        }
    }
    # initiate request and $res
    $c = curl_init();
    if (!$c) {
        return error($cmd, "could not create HTTP request");
    }
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($c, CURLOPT_HEADER, true);
    curl_setopt($c, CURLINFO_HEADER_OUT, true);
    $res = array();
    # set request url
    if (is_array($get_args)) {
        $get_args = http_build_query($get_args);
    }
    if (is_string($get_args)) {
        $url = "{$base_url}?{$get_args}";
    } else {
        $url = $base_url;
    }
    curl_setopt($c, CURLOPT_URL, $url);
    if ($debug) {
        debug_echo($cmd, "initiating HTTP request : {$url}");
    }
    # add headers to request
    if ($request_headers) {
        curl_setopt($c, CURLOPT_HTTPHEADER, $request_headers);
        if ($debug) {
            debug_echo($cmd, "setting HTTP request headers :");
            debug_dump_yaml($request_headers, true);
        }
    }
    # add body to request
    $request_body = '';
    if ($post_args) {
        foreach ($post_args as $key => $value) {
            $request_body .= urlencode($key) . '=' . urlencode($value) . '&';
        }
        $request_body = rtrim($request_body, '&');
        curl_setopt($c, CURLOPT_POST, strlen($request_body));
        curl_setopt($c, CURLOPT_POSTFIELDS, $request_body);
        $request_method = 'post';
        if ($debug) {
            debug_echo($cmd, "HTTP post args :");
            debug_dump_yaml($post_args, true);
        }
    } else {
        switch ($request_method) {
            case 'post':
                curl_setopt($c, CURLOPT_POST, 1);
        }
    }
    # set up downloading to file if set
    if ($save_to_file) {
        # backup old response file if set and exists
        if (is_file($save_to_file) && $backup_old_saved_files && !backup_file($save_to_file, null, $cmd)) {
            return error($cmd, "could not backup previously saved file : {$save_to_file}");
        }
        # create parent directory of response file
        if (!make_dir($opts, dirname($save_to_file), $cmd)) {
            return false;
        }
        # create file handler for response file
        $save_to_file_handle = @fopen($save_to_file, "w");
        if (!$save_to_file_handle) {
            return error($cmd, "could not open response file : {$save_to_file}");
        }
        # add response file handler to request
        curl_setopt($c, CURLOPT_FILE, $save_to_file_handle);
        # display message and add to $res
        debug_echo($cmd, "saving response to file : {$save_to_file}");
        $res['response_file'] = $save_to_file;
        $res['file'] = $save_to_file;
    }
    # add download progress update function if set
    if ($download_progress_update) {
        curl_setopt($c, CURLOPT_PROGRESSFUNCTION, 'curl_transfer_progress');
        curl_setopt($c, CURLOPT_NOPROGRESS, false);
    }
    # display download message
    if (!$download_msg) {
        $download_msg = "executing HTTP request : {$url}";
    }
    debug_echo($cmd, "{$download_msg} ...");
    # execute request
    for ($i = 0; $i < $request_retries; $i++) {
        $r = curl_exec($c);
        if ($r) {
            break;
        }
    }
    if (!$r) {
        return error($cmd, "could not execute HTTP request : {$url}");
    }
    # split the response into header and body
    $request_head = curl_getinfo($c, CURLINFO_HEADER_OUT);
    $response_head_size = curl_getinfo($c, CURLINFO_HEADER_SIZE);
    if ($save_to_file) {
        $handle = fopen($save_to_file, 'r');
        $response_head = trim(fread($handle, $response_head_size));
        $stat = fstat($handle);
        $response_body_size = $stat['size'] - $response_head_size;
        if ($response_body_size > $max_response_body_size) {
            warning($cmd, "response body size is greater than max, so is not set : {$response_body_size} > {$max_response_body_size}");
            $response_body = "[{$save_to_file}]";
            $no_reset_response_body_size = true;
        } else {
            $response_body = trim(fread($handle, $response_body_size));
        }
        fclose($handle);
    } else {
        $response_head = trim(substr($r, 0, $response_head_size));
        $response_body = trim(substr($r, $response_head_size));
    }
    $response_head_size = strlen($response_head);
    if (!@$no_reset_response_body_size) {
        $response_body_size = strlen($response_body);
    }
    # setup response header parsing
    $headers = explode("\n", $response_head);
    $response_headers = array();
    $response_cookies = array();
    $pipe_headers = array();
    # parse response headers
    foreach ($headers as $header) {
        # clean and add response header
        $header = trim($header);
        if ($header != '') {
            $response_headers[] = $header;
        }
        # parse cookies
        if (substr($header, 0, 11) == 'Set-Cookie:') {
            # add cookie to pipe headers
            $full_cookie = trim(substr($header, 11));
            $pipe_headers[] = "Cookie: {$full_cookie}";
            # add cookie to cookies
            $cookie = strstr($full_cookie, ';', true);
            $name = urldecode(strstr($cookie, '=', true));
            $val = urldecode(substr(strstr($cookie, '='), 1));
            $response_cookies[$name] = $val;
        }
        # parse location
        if (substr($header, 0, 9) == 'Location:') {
            $res['response_location'] = trim(substr($header, 9));
        }
    }
    # add main request and response variables to $res
    $res['headers'] = $pipe_headers;
    $res['request_url'] = $base_url;
    $res['request_get_args'] = $get_args;
    $res['request_full_url'] = $url;
    $res['request_method'] = $request_method;
    $res['request_post_args'] = $post_args;
    $res['request_headers'] = $request_headers;
    $res['response_headers'] = $response_headers;
    $res['response_cookies'] = $response_cookies;
    $res['response_head_size'] = $response_head_size;
    $res['response_body_size'] = $response_body_size;
    # parse response body for JSON
    $parsed_body = @json_decode($response_body, true);
    if (!is_null($parsed_body)) {
        $res['response_body_json_decoded'] = $parsed_body;
    }
    # save data to files if set and save in $res
    $save_to_files = array('request_head' => array($request_head_file, $request_head), 'request_body' => array($request_body_file, $request_body), 'response_head' => array($response_head_file, $response_head), 'response_body' => array($response_body_file, $response_body));
    foreach ($save_to_files as $name => $vars) {
        $file = $vars[0];
        $data = $vars[1];
        if ($file) {
            $report_name = str_replace('_', ' ', $name);
            if (is_file($file) && $backup_old_saved_files && !backup_file($file, null, $cmd)) {
                return error($cmd, "could not backup previously saved {$report_name} file : {$file}");
            }
            if (!file_put_contents($file, $data)) {
                return error($cmd, "could not save {$report_name} to file : {$file}");
            }
            debug_echo($cmd, "{$report_name} saved to file : {$file}");
            $res["{$name}_file"] = $file;
        }
        $res[$name] = $data;
    }
    # parse response hidden inputs if set
    if ($parse_response_form_args) {
        // we don't pass the $opts as it does not work properly, and $pipe needs to be second
        debug_echo($cmd, "parsing response for post action url and hidden args");
        $res = http_parse_form($parse_response_form, $res, $cmd, true);
    }
    # close handlers
    curl_close($c);
    if (@$save_to_file_handle) {
        fclose($save_to_file_handle);
    }
    # sort $res, display and return
    ksort($res);
    if ($debug) {
        debug_echo($cmd, "HTTP response:");
        @debug_dump_yaml($res, true);
    }
    # check for automatic redirection
    $response_location = @$res['response_location'];
    if ($response_location && $auto_redirect) {
        debug_echo($cmd, "automatically redirecting to url : {$response_location}");
        $res = merge_opts_for_output($res, $opts);
        # avoid infinite loops because the url opt is checked before the response_location opt
        unset($res['form_name']);
        unset($res['post_args']);
        unset($res['request_method']);
        unset($res['url']);
        return http_request($res);
    }
    return $res;
}
Ejemplo n.º 4
0
function curl($opts, $pipe, $cmd = __FUNCTION__, $default_protocol = null)
{
    # set up internal (which by default comes from there being a default protocol)
    if ($default_protocol) {
        $internal = true;
    } else {
        $internal = false;
    }
    # merge the opts
    $opts = merge_opts($opts, $pipe);
    # set prefix opt
    $prefix = $default_protocol ? $default_protocol : 'curl';
    # get execute opt
    $execute = get_opt_config_value($prefix, $opts, 'execute', true);
    if (!check_opt_set_type($cmd, $execute, 'execute', 'boolean')) {
        return false;
    }
    # check if we should execute or not
    if (!$execute) {
        debug_echo($cmd, "not executing {$prefix} request");
        return;
    }
    # get debug opt
    $debug = get_opt_config_value($prefix, $opts, 'debug', false);
    if (!check_opt_set_type($cmd, $debug, 'debug', 'boolean')) {
        return false;
    }
    ## saved files ##
    # get save_to_file opt
    $save_to_file = get_opt_config_value($prefix, $opts, 'save_to_file');
    if (!check_opt_if_set_type($cmd, $save_to_file, 'save_to_file', 'string')) {
        return false;
    }
    # get use saved file opt
    $use_saved_file = get_opt_config_value($prefix, $opts, 'use_saved_file', false);
    if (!check_opt_set_type($cmd, $use_saved_file, 'use_saved_file', 'boolean')) {
        return false;
    }
    # get backup old saved files
    $backup_old_saved_files = get_opt_config_value($prefix, $opts, 'backup_old_saved_files', false);
    if (!check_opt_set_type($cmd, $backup_old_saved_files, 'backup_old_saved_files', 'boolean')) {
        return false;
    }
    # check if file exists and back up if necessary
    if (file_exists($save_to_file) && $use_saved_file) {
        debug_echo($cmd, "using saved file instead of executing request : {$save_to_file}");
        return array('file' => $save_to_file, 'response_file' => $save_to_file);
    }
    # get curlopts opt
    $curlopts = get_opt($prefix, $opts, 'curlopts');
    if (!check_opt_if_set_type($cmd, $curlopts, 'curlopts', 'array')) {
        return false;
    }
    if (is_array($curlopts)) {
        $curlopts =& $opts['curlopts'];
    } else {
        $curlopts = array();
        $opts['curlopts'] =& $curlopts;
    }
    # get finalize_curlopts opt
    $finalize_curlopts = get_opt($prefix, $opts, 'finalize_curlopts');
    if (!check_opt_if_set_type($cmd, $finalize_curlopts, 'finalize_curlopts', 'array')) {
        return false;
    }
    # set up response
    $res = array();
    # call pre functions if internal and set
    if ($internal) {
        # get pre functions opt
        $init_functions = get_opt($prefix, $opts, 'init_functions');
        if (!check_opt_if_set_type($cmd, $init_functions, 'init_functions', 'array_of_strings')) {
            return false;
        }
        # get pre exec functions opt
        $pre_exec_functions = get_opt($prefix, $opts, 'pre_exec_functions');
        if (!check_opt_if_set_type($cmd, $pre_exec_functions, 'pre_exec_functions', 'array_of_strings')) {
            return false;
        }
        # get post exec functions opt
        $post_exec_functions = get_opt($prefix, $opts, 'post_exec_functions');
        if (!check_opt_if_set_type($cmd, $post_exec_functions, 'post_exec_functions', 'array_of_strings')) {
            return false;
        }
        # get finalize functions opt
        $finalize_functions = get_opt($prefix, $opts, 'finalize_functions');
        if (!check_opt_if_set_type($cmd, $finalize_functions, 'finalize_functions', 'array_of_strings')) {
            return false;
        }
        # set array of arrays to pass to hooked functions
        $func_args = array('curlopts' => &$curlopts, 'opts' => &$opts, 'res' => &$res);
        # call the pre functions
        if ($init_functions) {
            foreach ($init_functions as $function) {
                $r = call_user_func($function, $prefix, $func_args, $cmd, $debug);
                if ($r === false) {
                    return false;
                }
            }
        }
    }
    # get url opt
    do {
        # get url opt (from curlopts)
        $url = get_opt($prefix, $curlopts, 'url');
        if (!check_opt_if_set_type($cmd, $url, 'url', 'string')) {
            return false;
        }
        if ($url) {
            break;
        }
        # get url opt (from opts)
        $url = get_opt($prefix, $opts, 'url');
        if (!check_opt_if_set_type($cmd, $url, 'url', 'string')) {
            return false;
        }
        if ($url) {
            break;
        }
        # get server opt
        $tries = array('server', 'svr');
        foreach ($tries as $try) {
            $server = get_opt($prefix, $opts, $try);
            if (!check_opt_if_set_type($cmd, $server, $try, 'string')) {
                return false;
            }
            if ($server) {
                break;
            }
        }
        # get path opt
        $path = get_opt($prefix, $opts, 'path');
        if (!check_opt_if_set_type($cmd, $path, 'path', 'string')) {
            return false;
        }
        # build path from dir / file if necessary
        if (!$path) {
            # get dir opt
            $dir = get_opt($prefix, $opts, 'dir');
            if (!check_opt_if_set_type($cmd, $dir, 'dir', 'string')) {
                return false;
            }
            # get file opt
            $file = get_opt($prefix, $opts, 'file');
            if (!check_opt_if_set_type($cmd, $file, 'file', 'string')) {
                return false;
            }
            # build path from dir/file
            $path = '';
            if ($dir) {
                if (substr($dir, -1) != '/') {
                    $dir .= '/';
                }
                $path .= $dir;
            }
            if ($file) {
                $path .= $file;
            }
            if (!$path) {
                $path = '/';
            } elseif ($path[0] != '/') {
                $path = "/{$path}";
            }
        }
        # build url from server and path
        if ($server) {
            if (substr($server, -1) == '/') {
                $server = substr($server, 0, -1);
            }
            $url = "{$server}{$path}";
            break;
        }
        return error($cmd, "no 'url' option set, and cannot be constructed from a 'server' and an optional 'path' option");
    } while (false);
    # get protocol opt
    $protocol = get_opt($prefix, $opts, 'protocol');
    if (!check_opt_if_set_type($cmd, $protocol, 'protocol', 'enum:ftp|ftps|http|https|imap|imaps|smtp|smtps')) {
        return false;
    }
    # check the protocol is valid / accepted
    if (!$protocol) {
        $substr_6 = substr($url, 0, 6);
        $substr_7 = substr($url, 0, 7);
        $substr_8 = substr($url, 0, 8);
        if ($substr_6 == 'ftp://') {
            $protocol = 'ftp';
        } elseif ($substr_7 == 'ftps://') {
            $protocol = 'ftps';
        } elseif ($substr_7 == 'http://') {
            $protocol = 'http';
        } elseif ($substr_8 == 'https://') {
            $protocol = 'https';
        } elseif ($substr_7 == 'imap://') {
            $protocol = 'imap';
        } elseif ($substr_8 == 'imaps://') {
            $protocol = 'imaps';
        } elseif ($substr_7 == 'smtp://') {
            $protocol = 'smtp';
        } elseif ($substr_8 == 'smtps://') {
            $protocol = 'smtps';
        } elseif ($default_protocol) {
            $protocol = $default_protocol;
        } else {
            return error($cmd, "protocol '{$protocol}' is not supported");
        }
        $opts['protocol'] = $protocol;
    }
    # check if the url has the protocol included
    if (substr($url, 0, strlen($protocol) + 3) != "{$protocol}://") {
        $url = "{$protocol}://{$url}";
    }
    # get port opt
    $port = get_opt($prefix, $opts, 'port');
    if (!check_opt_if_set_type($cmd, $port, 'port', 'integer')) {
        return false;
    }
    # set the port curlopt
    if ($port) {
        $url .= ":{$port}";
    }
    # check if the path is a directory, and add a slash if so
    if (substr($url, -1) != '/') {
        # get path is dir opt
        $path_is_dir = get_opt($prefix, $opts, 'path_is_dir', false);
        if (!check_opt_set_type($cmd, $path_is_dir, 'path_is_dir', 'boolean')) {
            return false;
        }
        # add slash to end if necessary
        if ($path_is_dir) {
            $url .= '/';
        }
    }
    # set the url
    $curlopts['url'] = $url;
    # get user opt
    $tries = array('user', 'usr');
    foreach ($tries as $try) {
        $user = get_opt($prefix, $opts, $try);
        if (!check_opt_if_set_type($cmd, $user, $try, 'string')) {
            return false;
        }
        if ($user) {
            break;
        }
    }
    # get password opt
    $tries = array('password', 'pwd');
    foreach ($tries as $try) {
        $password = get_opt($prefix, $opts, $try);
        if (!check_opt_if_set_type($cmd, $password, $try, 'string')) {
            return false;
        }
        if ($password) {
            break;
        }
    }
    # set the authentication
    if ($user && $password) {
        $curlopts['user'] = "******";
    }
    # get curlinfo opt
    $curlinfo = get_opt($prefix, $opts, 'curlinfo');
    if (!check_opt_if_set_type($cmd, $curlinfo, 'curlinfo', 'array_of_strings')) {
        return false;
    }
    # get request retries opt
    $request_retries = get_opt_config_value($prefix, $opts, 'request_retries', 3);
    if (!check_opt_set_type($cmd, $request_retries, 'request_retries', 'integer')) {
        # change to having max 10
        return false;
    }
    $curlopts['retry'] = $request_retries;
    ## messages ##
    # get exec msg opt
    $exec_msg = get_opt($prefix, $opts, 'exec_msg', "executing curl request to url : {$url}");
    if (!check_opt_set_type($cmd, $exec_msg, 'exec_msg', 'string,boolean')) {
        return false;
    }
    # get exec retry msg opt
    $exec_retry_msg = get_opt($prefix, $opts, 'exec_retry_msg', "previous request failed - trying again");
    if (!check_opt_set_type($cmd, $exec_retry_msg, 'exec_retry_msg', 'string,boolean')) {
        return false;
    }
    # get exec retry msg opt
    $success_msg = get_opt($prefix, $opts, 'success_msg', "request successful");
    if (!check_opt_set_type($cmd, $success_msg, 'success_msg', 'string,boolean')) {
        return false;
    }
    # get exec retry msg opt
    $fail_msg = get_opt($prefix, $opts, 'fail_msg', "request failed");
    if (!check_opt_set_type($cmd, $fail_msg, 'fail_msg', 'string,boolean')) {
        return false;
    }
    ## save to locations ##
    # get save request head to file opt
    $request_head_file = get_opt($prefix, $opts, 'save_request_head_to_file');
    if (!check_opt_if_set_type($cmd, $request_head_file, 'save_request_head_to_file', 'string')) {
        return false;
    }
    # get save request body to file opt
    $request_body_file = get_opt($prefix, $opts, 'save_request_body_to_file');
    if (!check_opt_if_set_type($cmd, $request_body_file, 'save_request_body_to_file', 'string')) {
        return false;
    }
    # get save head to file opt
    $response_head_file = get_opt($prefix, $opts, 'save_head_to_file');
    if (!check_opt_if_set_type($cmd, $response_head_file, 'save_head_to_file', 'string')) {
        return false;
    }
    # get save body to file opt
    $response_body_file = get_opt($prefix, $opts, 'save_body_to_file');
    if (!check_opt_if_set_type($cmd, $response_body_file, 'save_body_to_file', 'string')) {
        return false;
    }
    # get max saved body size opt
    $max_saved_body_size = get_opt_config_value($prefix, $opts, 'max_saved_body_size', 512 * 1024 * 1024);
    if (!check_opt_if_set_type($cmd, $max_saved_body_size, 'max_saved_body_size', 'integer')) {
        return false;
    }
    # get stderr opt
    $stderr = get_opt_config_value($prefix, $opts, 'stderr');
    if (!check_opt_if_set_type($cmd, $stderr, 'stderr', 'string')) {
        return false;
    }
    # set stderr curlopt if necessary
    if ($stderr) {
        if (!make_dir_for_saving_file($stderr, null, $cmd)) {
            return false;
        }
        $stderr_handle = fopen($stderr, 'w');
        $curlopts['stderr'] = $stderr_handle;
    }
    # execute pre-exec functions
    if ($internal && $pre_exec_functions) {
        foreach ($pre_exec_functions as $function) {
            $r = call_user_func($function, $prefix, $func_args, $cmd, $debug);
            if ($r === false) {
                return false;
            }
        }
    }
    # save to file
    if ($save_to_file) {
        # backup old response file if set and exists
        if (is_file($save_to_file) && $backup_old_saved_files && !backup_file($save_to_file, null, $cmd)) {
            return error($cmd, "could not backup previously saved file : {$save_to_file}");
        }
        # create parent directory of response file
        if (!make_dir(dirname($save_to_file), $opts, $cmd)) {
            return false;
        }
        # add response file handler to request
        $curlopts['output'] = $save_to_file;
        # display message and add to $res
        debug_echo($cmd, "saving response to file : {$save_to_file}");
        $response_body_file = $save_to_file;
        $res['file'] = $save_to_file;
    } else {
        $response_body_file = null;
    }
    # add headers file
    $response_heads_file = tempnam('/tmp', "mawk-curl-response-heads-");
    $curlopts['dump-header'] = $response_heads_file;
    # set up the options
    $request_cmd = curl_build_request($prefix, $protocol, $curlopts, $cmd, $internal, $debug);
    if (!$request_cmd) {
        return false;
    }
    $res['request_cmd'] = $request_cmd;
    # display exec message
    if ($exec_msg) {
        debug_echo($cmd, "{$exec_msg} ...");
    }
    if ($debug) {
        debug_echo($cmd, "curl exec : {$request_cmd}");
    }
    # execute request
    $response_last_line = exec($request_cmd, $response_body, $rv);
    # merge the response body
    $response_body = implode('', $response_body);
    # display message about result (we add space to cover up any download monitor)
    if ($rv === 0) {
        if ($success_msg) {
            debug_echo($cmd, "{$success_msg}                     ");
        }
    } else {
        return error($cmd, "{$fail_msg}                        ");
    }
    # execute post-exec functions
    if ($internal && $post_exec_functions) {
        foreach ($post_exec_functions as $function) {
            $r = call_user_func($function, $prefix, $func_args, $cmd, $debug);
            if ($r === false) {
                return false;
            }
        }
    }
    # add main request and response variables to $res
    $res['request_url'] = $url;
    # get the response heads
    $response_heads = file_get_contents($response_heads_file);
    unlink($response_heads_file);
    switch ($protocol) {
        case 'http':
        case 'https':
            # get the last response heads
            $response_heads = explode("\r\n\r\n", $response_heads);
            foreach ($response_heads as $i => $headers) {
                if ($headers == '') {
                    unset($response_heads[$i]);
                }
            }
            $res['response_heads'] = $response_heads;
            # get response head
            $response_head = $response_heads[count($response_heads) - 1];
            break;
        default:
            $response_head = $response_heads;
            break;
    }
    # save data to files if set and save in $res
    $save_to_files = array('response_head' => array($response_head_file, $response_head, true), 'response_body' => array($response_body_file, $response_body));
    # save data to files if set and save in $res
    foreach ($save_to_files as $name => $vars) {
        $file = $vars[0];
        $data = $vars[1];
        $set = @$vars[2];
        if ($file) {
            if ($set) {
                $report_name = str_replace('_', ' ', $name);
                if (is_file($file) && $backup_old_saved_files && !backup_file($file, null, $cmd)) {
                    return error($cmd, "could not backup previously saved {$report_name} file : {$file}");
                }
                if (strlen($data) == 0) {
                    warning($cmd, "{$report_name} is of zero size, so shall not be saved to file : {$file}");
                } else {
                    if (!file_put_contents($file, $data)) {
                        return error($cmd, "could not save {$report_name} to file : {$file}");
                    }
                    debug_echo($cmd, "{$report_name} saved to file : {$file}");
                }
            } else {
                $data = file_get_contents($file);
            }
            $res["{$name}_file"] = $file;
        }
        $res[$name] = $data;
    }
    # execute finalize functions
    if ($internal && $finalize_functions) {
        foreach ($finalize_functions as $function) {
            $r = call_user_func($function, $prefix, $func_args, $cmd, $debug);
            if ($r === false) {
                return false;
            }
        }
    }
    # sort $res, display and return
    ksort($res);
    return $res;
}
Ejemplo n.º 5
0
function ftp_get_file($opts, $pipe = false, $cmd = __FUNCTION__)
{
    # set prefix
    $prefix = 'ftp';
    # merge opts
    $opts = merge_opts($opts, $pipe, 'remote_file');
    # get execute opt
    $execute = get_opt_config_value($prefix, $opts, 'execute', true);
    if (!check_opt_set_type($cmd, $execute, 'execute', 'boolean')) {
        return false;
    }
    # check if we should execute or not
    if (!$execute) {
        return true;
    }
    # get remote file opt
    $remote_file = get_opt($prefix, $opts, 'remote_file');
    if (!check_opt_set_type($cmd, $remote_file, 'remote_file', 'string')) {
        return false;
    }
    # get local file opt
    $local_file = get_opt($prefix, $opts, 'local_file', $remote_file);
    if (!check_opt_set_type($cmd, $local_file, 'local_file', 'string')) {
        return false;
    }
    # get get retries opt
    $get_retries = get_opt_config_value($prefix, $opts, 'get_retries', 3);
    if (!check_opt_set_type($cmd, $get_retries, 'get_retries', 'integer')) {
        return false;
    }
    # get download progress update opt
    $download_progress_update = get_opt_config_value($prefix, $opts, 'download_progress_update', true);
    if (!check_opt_set_type($cmd, $download_progress_update, 'download_progress_update', 'boolean')) {
        return false;
    }
    # check to see if we shall re-use the file if it already exists
    if (is_file($local_file)) {
        $use_saved_file = get_opt_config_value($prefix, $opts, 'use_saved_file', true);
        if (!check_opt_set_type($cmd, $use_saved_file, 'use_saved_file', 'boolean')) {
            return false;
        }
        # get backup old downloaded files
        $backup_old_saved_files = get_opt_config_value($prefix, $opts, 'backup_old_saved_files', true);
        if (!check_opt_set_type($cmd, $backup_old_saved_files, 'backup_old_saved_files', 'boolean')) {
            return false;
        }
        if ($use_saved_file) {
            debug_echo($cmd, "using saved file instead of executing the FTP request : {$local_file}");
            return array('file' => $local_file);
        } elseif ($backup_old_saved_files && !backup_file($local_file)) {
            return error($cmd, "could not backup previously downloaded file : {$local_file}");
        }
    }
    # setup connection
    $conn = ftp_get_conn($opts, $cmd);
    if (!$conn) {
        return false;
    }
    # get connection values
    $ftp_handle = $conn['handle'];
    $svr = $conn['svr'];
    # make the parent dir of local file
    if (!make_dir(dirname($local_file), $opts, $cmd)) {
        return false;
    }
    # get the size of the remote file
    $size = ftp_size($ftp_handle, $remote_file);
    if ($size == -1) {
        debug_echo($cmd, "fetching file from FTP server : {$remote_file} => {$local_file} (unknown size) ...");
    } else {
        $size_str = human_filesize($size);
        debug_echo($cmd, "fetching file from FTP server : {$remote_file} => {$local_file} (size: {$size_str}) ...");
    }
    # get the file
    if ($download_progress_update) {
        # get the file with a progress monitor (a little slower)
        for ($j = 0; $j < $get_retries; $j++) {
            $ret = ftp_nb_get($ftp_handle, $local_file, $remote_file, FTP_BINARY);
            while ($ret == FTP_MOREDATA) {
                # get the size of the downloaded file (note: we need to use file handlers otherwise it doesn't work)
                $fh = fopen($local_file, 'r');
                $stat = fstat($fh);
                $dl_size = $stat['size'];
                fclose($fh);
                # print progress
                download_progress($size, $dl_size);
                # continue downloading
                $ret = ftp_nb_continue($ftp_handle);
            }
            if ($ret == FTP_FINISHED) {
                return array('file' => $local_file);
            }
        }
    } else {
        # get local handle
        $local_file_handle = fopen($local_file, 'w');
        if (!$local_file_handle) {
            return error($cmd, "could not open local file : {$local_file}");
        }
        # get the file without a progress monitor (a bit quicker)
        for ($j = 0; $j < $get_retries; $j++) {
            if (@ftp_fget($ftp_handle, $local_file_handle, $remote_file, FTP_BINARY)) {
                return array('file' => $local_file);
            }
        }
        fclose($local_file_handle);
    }
    return error($cmd, "could not fetch file from FTP server : {$remote_file}");
}
Ejemplo n.º 6
0
function do_cp_update()
{
    global $CP;
    if (checkprivilege_action($CP[login_id], g_view_lastestcp)) {
        $sgcp = new CP_Update();
        $version = cp_current_version();
        $cp_version = $sgcp->version_data($version, 0);
        $cp_release = $sgcp->version_data($version, 1);
        $list_version = cp_list_version();
        if (!is_dir("tmp_update")) {
            if (!mkdir("tmp_update")) {
                exit;
            }
        }
        $count_files = $sgcp->count_list_update_file($cp_version, $cp_release, $list_version) * 2;
        $date = date("Y-m-d_H_i_s");
        $count_success = 0;
        cp_update_set_msg(0, 0);
        cp_update_set_msg(3, 0);
        for ($i = 0; $i < count($list_version); $i++) {
            $load_version = $sgcp->version_data($list_version[$i], 0);
            $load_release = $sgcp->version_data($list_version[$i], 1);
            if ($load_version && cp_check_version($cp_version, $cp_release, $load_version, $load_release)) {
                $list_update_file = file_get_result("bin/" . $list_version[$i] . "/list.sgcp", 1);
                $list_update_file = $sgcp->true_list_update_file($list_update_file);
                $list_download_file = $sgcp->true_list_download_file($list_update_file, $list_version[$i]);
                $tmp_list_update_file[$i] = $list_update_file;
                for ($j = 0; $j < count($list_download_file); $j++) {
                    cp_update_set_msg(1, $list_download_file[$j]);
                    if (!($tmp_update_result[$i][$j] = file_get_result($list_download_file[$j]))) {
                        cp_update_set_msg(4, $list_download_file[$j]);
                        exit;
                    } else {
                        $count_success++;
                        $percent = $count_success / $count_files * 100;
                        $percent = (int) $percent;
                        cp_update_set_msg(3, $percent);
                    }
                }
            }
        }
        for ($i = 0; $i < count($list_version); $i++) {
            $load_version = $sgcp->version_data($list_version[$i], 0);
            $load_release = $sgcp->version_data($list_version[$i], 1);
            if ($load_version && cp_check_version($cp_version, $cp_release, $load_version, $load_release)) {
                $list_update_file = $tmp_list_update_file[$i];
                $list_download_file = $sgcp->true_list_download_file($list_update_file, $list_version[$i]);
                for ($j = 0; $j < count($list_download_file); $j++) {
                    // Check & Create dirs
                    $sgcp->list_create_dir($list_update_file[$j]);
                    // Backup files
                    backup_file($list_update_file[$j], $date);
                    if (!($handle = fopen($list_update_file[$j], 'w+'))) {
                        cp_update_set_msg(5, $list_update_file[$j]);
                        exit;
                    } else {
                        cp_update_set_msg(2, $list_update_file[$j]);
                        if (fwrite($handle, $tmp_update_result[$i][$j]) === FALSE) {
                            cp_update_set_msg(5, $list_update_file[$j]);
                            exit;
                        } else {
                            $count_success++;
                            $percent = $count_success / $count_files * 100;
                            $percent = (int) $percent;
                            cp_update_set_msg(3, $percent);
                        }
                        fclose($handle);
                    }
                }
            }
        }
        cp_update_set_msg(0, 3);
    }
}