#!/usr/local/bin/php pecl install channel://pecl.php.net/bcompiler-0.7\n\n"; exit; } $suffix = 'php|html|htm'; $output = $srcdir = ''; $infiles = array(); $verbose = true; $follow_symlink = $force_overwrite = $copy_rest = $recursive = false; for( $i=1 ; $i < $_SERVER['argc']; $i++ ) { if( $_SERVER['argv'][$i] == '-h' ) { print_usage(); } elseif( $_SERVER['argv'][$i] == '-f' ) { $force_overwrite = true; } elseif( $_SERVER['argv'][$i] == '-o' ) { if( ++$i < $_SERVER['argc'] ) $output = $_SERVER['argv'][$i]; } elseif( $_SERVER['argv'][$i] == '-s' || $_SERVER['argv'][$i] == '-a' ) { if( ++$i < $_SERVER['argc'] ) $srcdir = $_SERVER['argv'][$i]; } elseif( $_SERVER['argv'][$i] == '-e' ) { if( ++$i < $_SERVER['argc'] ) $suffix = $_SERVER['argv'][$i]; } elseif( $_SERVER['argv'][$i] == '-c' ) { $copy_rest = true; } elseif( $_SERVER['argv'][$i] == '-r' ) { $recursive = true; } elseif( $_SERVER['argv'][$i] == '-l' ) { $follow_symlink = true; } elseif( $_SERVER['argv'][$i] == '-q' ) { $verbose = false; } elseif( preg_match( '/^-o(.+)$/', $_SERVER['argv'][$i], $match ) ) { $output = $match[1]; } else { $infiles[] = $_SERVER['argv'][$i]; } } $numfiles = count($infiles); $outdir = ''; if( $numfiles > 0 || $srcdir != '' ) $outdir = $output; if( $srcdir == '' ) { if( $numfiles == 0 ) print_usage(); if( $numfiles > 1 && $outdir == '' ) error_exit("You should use `-o DIR' to specify the output directory"); } else { if( $numfiles > 0 ) error_exit("You can not encode files and specify `-a DIR' at the same times"); if( $outdir == '' ) error_exit("You should use `-o DIR' to specify the output directory"); } if( $outdir != '' ) { if( file_exists( $outdir ) ) { if( !is_dir($outdir) ) error_exit("$outdir already exists and is not a directory"); if( !$force_overwrite ) error_exit("The directory [$outdir] already exists, use -f to force overwriting"); } else mkdir( $output, 0755 ); } if( $verbose ) print "$title\n\n"; if( $srcdir != '' ) { process_dir( $srcdir, $output ); } elseif( $numfiles == 1 ) { if( $output == '' ) { $ext = substr( strrchr( $infiles[0], '.' ), 1 ); if( $ext ) $output = basename( $infiles[0], ".$ext" ) . "-encoded.$ext"; else $output = $infiles[0] . '-encoded'; } elseif( is_dir($output) ) $output = $outdir.'/'.$infiles[0]; bencode( $infiles[0], $output ); } else { foreach( $infiles as $infile ) { bencode( $infile, "$outdir/$infile" ); } } exit(0); function process_dir( $srcdir, $outdir ) { global $follow_symlink, $copy_rest, $suffix, $recursive, $force_overwrite; $dh = opendir($srcdir); while (($file = readdir($dh)) !== false) { if( $file == '.' || $file == '..' ) continue; $srcpath = "$srcdir/$file"; $outpath = "$outdir/$file"; if( is_link($srcpath) ) { $real = readlink($srcpath); $realpath = ($real[0]=='/')?$real:"$srcdir/$real"; if( $follow_symlink ) { $srcpath = $realpath; } else { if( $recursive || (!is_dir($realpath) && $copy_rest) ) { if( file_exists($outpath) && $force_overwrite ) @unlink($outpath); symlink( $real, $outpath ); show_verbose("symlink: $outpath"); } else show_verbose("skipped: $outpath"); continue; } } if( is_dir($srcpath) ) { if( $recursive ) { if( !file_exists( $outpath ) ) { show_verbose(" mkdir: $outpath"); mkdir($outpath,0755); } elseif( !is_dir($outpath) ) error_exit("$outpath is not a directory"); process_dir( $srcpath, $outpath ); } } elseif( is_readable($srcpath) ) { if( file_exists($outpath) && !$force_overwrite ) show_verbose("skipped: $outpath"); elseif( preg_match( "/\.($suffix)\$/", $file ) ) bencode( $srcpath, $outpath ); elseif( $copy_rest ) { if( @copy( $srcpath, $outpath ) ) show_verbose(" copied: $outpath"); else print " copy: $outpath (failed)\n"; } continue; } else print " failed: $outpath (un-readable)\n"; } closedir($dh); } function bencode( $infile, $outfile ) { $fh = fopen( $outfile, 'w'); bcompiler_write_header($fh); bcompiler_write_file($fh, $infile); bcompiler_write_footer($fh); fclose($fh); show_verbose("encoded: $outfile"); return(1); } function error_exit( $err ) { global $title; print "$title\n\n"; print "ERROR: $err\n\n"; exit(1); } function print_usage() { global $help; print $help; exit(0); } function show_verbose( $msg ) { global $verbose; if( $verbose ) print "$msg\n"; } ?>