#!/usr/bin/php date("y")) { $year = "19" . $year; } else { $year = "20" . $year; } $yd = dirfix($target . $year); cdir($yd); $md = dirfix($yd . $month); cdir($md); $dd = dirfix($md . $day); cdir($dd); $o = $dd . $n; $s = filesize($movie); $size = formatsize($s); $cont = true; $ow = false; if(file_exists($o)) { if(filesize($o) == $s) { $m = "File {$o} already exists and has the same size as {$movie} ({$size}), shall we override it anyways ? [y/n]: "; } else { $m = "File {$o} already exists, shall we override it ? ({$size}) [y/n]: "; } echo colorize ? "\033[33m{$m}\033[0m": $m; p_log("Prompting if we shall override {$o} with {$movie}..."); $i = fopen("php://stdin","r"); $l = fgets($i); fclose($i); $l = substr($l,0,-1); $l = strtolower($l); p_log("User response: {$l}"); if($l != "y") { $cont = false; info("Not overwriting."); } else { $ow = true; } } if($cont) { if($ow) { $msg = "Overriding {$o} with {$movie}"; } else { $msg = "Copying {$n} to {$o}..."; } echo colorize ? "\033[33m{$msg} ({$size})\033[0m\r": $msg . " ({$size})\r"; p_log($msg); $h = fopen($movie,"r"); $oh = fopen($o,"w"); $pos = 0; $pchunk = $s / 100; $t1 = time(); while(!feof($h)) { echo $blank . "\r"; if($pos == 0) { $m = "{$msg} (0 Byte of {$size}, 0%)"; } else { $percentage = round($pos / $pchunk,2); $copied = formatsize($pos); $tss = time()-$t1; // time since start $speed = $tss == 0 ? "0 Bytes" : formatsize($pos/$tss); $m = "{$msg} ({$copied} of {$size}, {$speed}/s, {$percentage}%)"; } echo colorize ? "\033[33m{$m}\033[0m\r": $m . "\r"; fwrite($oh,fread($h,block)); $pos += block; } $t2 = time(); fclose($h); fclose($oh); echo $blank . "\r"; if(file_exists($o)) { $s1 = filesize($o); $s2 = filesize($movie); $tss = $t2-$t1; if($s1 != $s2) warn("Size missmatch between files {$o} and {$movie} ({$s1} != {$s2})! The target file may be corrupt."); succ("Successfully copied {$movie} to {$o}, {$size} in " . $tss . " seconds."); } else { warn("Something went wrong that {$n} couldn't be copied :("); } } } succ("All files were successfully copied. Exiting..."); // exit error(NULL,1); // functions function dirfix($dir) { $a = substr($dir,-1) == DIRECTORY_SEPARATOR ? $dir : $dir . DIRECTORY_SEPARATOR; return $a; } // log functions function info($msg, $newline = PHP_EOL) { p_log($msg); echo colorize ? "\033[36m{$msg}\033[0m" . $newline : $msg . $newline; } function succ($msg, $newline = PHP_EOL) { p_log($msg); echo colorize ? "\033[32m{$msg}\033[0m" . $newline : $msg . $newline; } function warn($msg, $newline = PHP_EOL) { p_log($msg); echo colorize ? "\033[33mWarning: {$msg}\033[0m" . $newline : $msg . $newline; } function cdir($dir) // checks if a directory exists and creates it if it doesn't { if(!is_dir($dir)) { mkdir($dir); info("Created directory {$dir}"); } } function error($msg,$q = false) { $bt = debug_backtrace(); $caller = array_shift($bt); $line = $caller['line']; if(!$q) { p_log("function error() called from line {$line}. Exiting with message: {$msg}",$msg); } else { p_log("Exiting."); } if(!$q) echo(colorize ? "\033[31mERROR: {$msg}\033[0m" . PHP_EOL : $msg . PHP_EOL); if(!empty($GLOBALS['mpoint'])) { info("Unmounting CD if mounted myself due to coming exit."); $p = $GLOBALS['mountpoint']; shell("umount {$p}"); rmdir($p); } fclose($GLOBALS['log']); exit; } function p_log($txt) { fwrite($GLOBALS['log'],date("Y-m-d H:i:s") . " " . $txt . PHP_EOL); } function shell($cmd) { $cmd = $cmd . " 2>&1"; p_log("SHELL {$cmd}" . PHP_EOL); $sh = popen($cmd,"r"); while(!feof($sh)) { $buff = fread($sh,1024); echo $buff; fwrite($GLOBALS['log'],$buff); } pclose($sh); p_log("Shell exited."); } function mnts() // mount list { $out = Array(); $mounts = file("/proc/mounts"); foreach($mounts as $mount) { $mount = substr($mount,0,-1); $m = explode(" ",$mount); list($blk,$point,$type) = $m; $out[] = Array('block_device' => $blk, 'mountpoint' => $point, 'type' => $type); } return $out; } function formatsize($size) { if($size < 1) return false; if($size > 1024 * 1024 * 1024) { return round($size / 1024 / 1024 / 1024,2) . " GiB"; } elseif($size > 1024 * 1024) { return round($size / 1024 / 1024,2) . " MiB"; } elseif($size > 1024) { return round($size / 1024,2) . " KiB"; } elseif($size < 1024) { return "{$size} Bytes"; } } function ext($file,$split = false) { $extension = explode(".",$file); $asize = sizeof($extension); $ext = strtolower($extension[$asize-1]); if(!$split) { return $ext; } else { unset($extension[$asize-1]); return array($ext,implode(".",$extension)); } }