| 2302 | | |
|---|
| 2303 | | /** |
|---|
| 2304 | | * cURL function to download files with a progress bar and echo output while downloading to the screen |
|---|
| 2305 | | * @global <type> $ch |
|---|
| 2306 | | * @global <type> $fout |
|---|
| 2307 | | * @global <type> $file_size |
|---|
| 2308 | | * @global <type> $downloaded |
|---|
| 2309 | | * @global <type> $pkg_interface |
|---|
| 2310 | | * @global <type> $progress_bar |
|---|
| 2311 | | * @param <type> $url_file |
|---|
| 2312 | | * @param <type> $destination_file |
|---|
| 2313 | | * @return <type> |
|---|
| 2314 | | */ |
|---|
| | 2305 | |
|---|
| | 2306 | function run_in_background($Command, $Priority = 0) { |
|---|
| | 2307 | if ($Priority) |
|---|
| | 2308 | $PID = shell_exec("nohup nice -n $Priority $Command 2> /dev/null & echo $!"); |
|---|
| | 2309 | else |
|---|
| | 2310 | $PID = shell_exec("nohup $Command > /dev/null 2> /dev/null & echo $!"); |
|---|
| | 2311 | return($PID); |
|---|
| | 2312 | } |
|---|
| | 2313 | |
|---|
| | 2314 | function is_process_running($PID) { |
|---|
| | 2315 | exec("ps $PID", $ProcessState); |
|---|
| | 2316 | return(count($ProcessState) >= 2); |
|---|
| | 2317 | } |
|---|
| | 2318 | |
|---|
| 2318 | | $progress_bar = 1; |
|---|
| 2319 | | $file_size = 1; |
|---|
| 2320 | | $downloaded = 1; |
|---|
| 2321 | | echo " "; |
|---|
| 2322 | | /* open destination file */ |
|---|
| 2323 | | $fout = fopen($destination_file, "wb"); |
|---|
| 2324 | | |
|---|
| 2325 | | /* |
|---|
| 2326 | | * Originally by Author: Keyvan Minoukadeh |
|---|
| 2327 | | * Modified by Scott Ullrich to return Content-Length size |
|---|
| 2328 | | */ |
|---|
| 2329 | | |
|---|
| 2330 | | $ch = curl_init(); |
|---|
| 2331 | | curl_setopt($ch, CURLOPT_URL, $url_file); |
|---|
| 2332 | | curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'endpointmanager_read_header'); |
|---|
| 2333 | | curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'endpointmanager_read_body'); |
|---|
| 2334 | | curl_setopt($ch, CURLOPT_NOPROGRESS, '1'); |
|---|
| 2335 | | curl_setopt($ch, CURLOPT_TIMEOUT, 120); |
|---|
| 2336 | | |
|---|
| 2337 | | curl_exec($ch); |
|---|
| 2338 | | $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|---|
| 2339 | | if($fout) |
|---|
| 2340 | | fclose($fout); |
|---|
| 2341 | | curl_close($ch); |
|---|
| 2342 | | return ($http_code == 200) ? true : $http_code; |
|---|
| | 2321 | $headers = get_headers($url_file, 1); |
|---|
| | 2322 | $size = $headers['Content-Length']; |
|---|
| | 2323 | |
|---|
| | 2324 | if(preg_match('/200/', $headers[0])) { |
|---|
| | 2325 | $pid = $this->run_in_background("wget ". $url_file . " -O " . $destination_file); |
|---|
| | 2326 | |
|---|
| | 2327 | while($this->is_process_running($pid)) { |
|---|
| | 2328 | |
|---|
| | 2329 | $out = 100 * round(filesize($destination_file) / $size,2); |
|---|
| | 2330 | echo '<script type="text/javascript">document.getElementById(\'DivExample\').innerHTML="%'.$out.'";</script>'; |
|---|
| | 2331 | |
|---|
| | 2332 | usleep('500'); |
|---|
| | 2333 | endpointman_flush_buffers(); |
|---|
| | 2334 | |
|---|
| | 2335 | clearstatcache(); // make sure PHP actually checks dest. file size |
|---|
| | 2336 | } |
|---|
| | 2337 | return true; |
|---|
| | 2338 | } else { |
|---|
| | 2339 | return false; |
|---|
| | 2340 | } |
|---|
| 2346 | | global $ch, $fout, $file_size, $downloaded, $pkg_interface, $progress_bar; |
|---|
| 2347 | | set_time_limit(0); |
|---|
| 2348 | | $progress_bar = 0; |
|---|
| 2349 | | $file_size = 1; |
|---|
| 2350 | | $downloaded = 1; |
|---|
| 2351 | | /* open destination file */ |
|---|
| 2352 | | $fout = fopen($destination_file, "wb"); |
|---|
| 2353 | | |
|---|
| 2354 | | /* |
|---|
| 2355 | | * Originally by Author: Keyvan Minoukadeh |
|---|
| 2356 | | * Modified by Scott Ullrich to return Content-Length size |
|---|
| 2357 | | */ |
|---|
| 2358 | | |
|---|
| 2359 | | $ch = curl_init(); |
|---|
| 2360 | | curl_setopt($ch, CURLOPT_URL, $url_file); |
|---|
| 2361 | | curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'endpointmanager_read_header'); |
|---|
| 2362 | | curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'endpointmanager_read_body'); |
|---|
| 2363 | | curl_setopt($ch, CURLOPT_NOPROGRESS, '1'); |
|---|
| 2364 | | curl_setopt($ch, CURLOPT_TIMEOUT, 120); |
|---|
| 2365 | | |
|---|
| 2366 | | curl_exec($ch); |
|---|
| 2367 | | $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|---|
| 2368 | | if($fout) |
|---|
| 2369 | | fclose($fout); |
|---|
| 2370 | | curl_close($ch); |
|---|
| 2371 | | return ($http_code == 200) ? true : $http_code; |
|---|
| | 2344 | $contents = file_get_contents($url_file); |
|---|
| | 2345 | if(preg_match('/200/', $http_response_header[0])) { |
|---|
| | 2346 | file_put_contents($destination_file,$contents); |
|---|
| | 2347 | //check file placement |
|---|
| | 2348 | if(!file_exists($destination_file)) { |
|---|
| | 2349 | return false; |
|---|
| | 2350 | } |
|---|
| | 2351 | return true; |
|---|
| | 2352 | } else { |
|---|
| | 2353 | return false; |
|---|
| | 2354 | } |
|---|
| 2375 | | global $ch, $fout, $file_size, $downloaded, $pkg_interface, $progress_bar; |
|---|
| 2376 | | set_time_limit(0); |
|---|
| 2377 | | $progress_bar = 0; |
|---|
| 2378 | | $file_size = 1; |
|---|
| 2379 | | $downloaded = 1; |
|---|
| 2380 | | /* open destination file */ |
|---|
| 2381 | | $fout = fopen($destination_file, "wb"); |
|---|
| 2382 | | |
|---|
| 2383 | | /* |
|---|
| 2384 | | * Originally by Author: Keyvan Minoukadeh |
|---|
| 2385 | | * Modified by Scott Ullrich to return Content-Length size |
|---|
| 2386 | | */ |
|---|
| 2387 | | |
|---|
| 2388 | | $ch = curl_init(); |
|---|
| 2389 | | curl_setopt($ch, CURLOPT_URL, $url_file); |
|---|
| 2390 | | curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'endpointmanager_read_header'); |
|---|
| 2391 | | curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'endpointmanager_read_body'); |
|---|
| 2392 | | curl_setopt($ch, CURLOPT_NOPROGRESS, '1'); |
|---|
| 2393 | | curl_setopt($ch, CURLOPT_TIMEOUT, 20); |
|---|
| 2394 | | |
|---|
| 2395 | | curl_exec($ch); |
|---|
| 2396 | | $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|---|
| 2397 | | if($fout) |
|---|
| 2398 | | fclose($fout); |
|---|
| 2399 | | curl_close($ch); |
|---|
| 2400 | | return ($http_code == 200) ? true : $http_code; |
|---|
| | 2358 | $contents = file_get_contents($url_file); |
|---|
| | 2359 | if(preg_match('/200/', $http_response_header[0])) { |
|---|
| | 2360 | file_put_contents($destination_file,$contents); |
|---|
| | 2361 | //check file placement |
|---|
| | 2362 | if(!file_exists($destination_file)) { |
|---|
| | 2363 | return false; |
|---|
| | 2364 | } |
|---|
| | 2365 | return true; |
|---|
| | 2366 | } else { |
|---|
| | 2367 | return false; |
|---|
| | 2368 | } |
|---|
| 2981 | | |
|---|
| 2982 | | function endpointman_update_progress_bar($out) { |
|---|
| 2983 | | echo '<script type="text/javascript">document.getElementById(\'DivExample\').innerHTML="%'.$out.'";</script>'; |
|---|
| 2984 | | } |
|---|
| 2985 | | |
|---|
| 2986 | | function endpointmanager_read_header($ch, $string) { |
|---|
| 2987 | | global $file_size, $fout; |
|---|
| 2988 | | $length = strlen($string); |
|---|
| 2989 | | $regs = ""; |
|---|
| 2990 | | preg_match("/(Content-Length:) (.*)/i", $string, $regs); |
|---|
| 2991 | | if((isset($regs[2])) AND ($regs[2] <> "")) { |
|---|
| 2992 | | $file_size = intval($regs[2]); |
|---|
| 2993 | | } |
|---|
| 2994 | | //ob_flush(); |
|---|
| 2995 | | endpointman_flush_buffers(); |
|---|
| 2996 | | return $length; |
|---|
| 2997 | | } |
|---|
| 2998 | | |
|---|
| 2999 | | function endpointmanager_read_body($ch, $string) { |
|---|
| 3000 | | global $fout, $file_size, $downloaded, $lastseen, $progress_bar; |
|---|
| 3001 | | $length = strlen($string); |
|---|
| 3002 | | $downloaded += intval($length); |
|---|
| 3003 | | $downloadProgress = round(100 * (1 - $downloaded / $file_size), 0); |
|---|
| 3004 | | $downloadProgress = 100 - $downloadProgress; |
|---|
| 3005 | | if($lastseen <> $downloadProgress and $downloadProgress < 101) { |
|---|
| 3006 | | if($progress_bar) { |
|---|
| 3007 | | endpointman_update_progress_bar($downloadProgress); |
|---|
| 3008 | | } |
|---|
| 3009 | | $lastseen = $downloadProgress; |
|---|
| 3010 | | } |
|---|
| 3011 | | if($fout) |
|---|
| 3012 | | fwrite($fout, $string); |
|---|
| 3013 | | //ob_flush(); |
|---|
| 3014 | | endpointman_flush_buffers(); |
|---|
| 3015 | | return $length; |
|---|
| 3016 | | } |
|---|