load(__DIR__.'/'.'uploader/uploader.html'); $html->set('filename', $filename); $html->set('host', $GLOBALS['wfpl_uploader_host']); $html->set('port', $GLOBALS['wfpl_uploader_port']); $html->show('main'); $html = $html->get('main'); $css = read_whole_file(__DIR__.'/'.'uploader/uploader.css'); $javascript = new tem(); $javascript->load(__DIR__.'/'.'uploader/progress.js'); $javascript->set('url', $progress_url); $javascript = $javascript->run(); uploader_daemon_start($GLOBALS['wfpl_uploader_port']); return array($html, $css, $javascript, $filename); } function uploader_move($tmp_filename, $filename) { $tmp_path = $GLOBALS['wfpl_uploader_path'] . '/partial/' . $tmp_filename; $out_path = $GLOBALS['wfpl_uploader_path'] . '/' . $filename; unlink($GLOBALS['wfpl_uploader_path'] . '/progress/' . $tmp_filename); rename($tmp_path, $out_path); } # start a daemon to accept file uploads and give progress indicators # if the port is used (eg if the daemon is already running) this will do nothing. function uploader_daemon_start($port) { exec(path_to('tcpserver') . " -q -R -H -llocalhost 0 $port " . path_to('perl') . ' lib/wfpl/uploader/daemon.pl ' . $GLOBALS['wfpl_uploader_path'] . ' >/dev/null 2>/dev/null < /dev/null &'); } /* call this to respond to the javascript async request for progress on the upload */ function wfpl_uploader_progress() { if(!isset($_REQUEST['wfpl_upload_progress'])) { return; } # allow this script to run for 8 hours set_time_limit(28800); $file = $_REQUEST['wfpl_upload_progress']; $file = strtolower($file); $file = preg_replace('|[^a-z0-9.-]|', '_', $file); $file = preg_replace('|^[.-]|', '_', $file); $file = $GLOBALS['wfpl_uploader_path'] . "/progress/$file"; $waited = 0; while(!file_exists($file)) { usleep(500000); ++$waited; if($waited > 100) { return; } } $progress_sent = 0; while(true) { clearstatcache(); $stats = stat($file); if($stats !== false) { $progress = $stats['size']; if($progress > $progress_sent) { print(substr('............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................', 0, $progress - $progress_sent)); flush(); $progress_sent = $progress; if($progress == 1000) { return; } } } usleep(500000); # wait half a second } }