root/eaccelerator/tags/0.9.1/encoder.php

Revision 22, 14.9 kB (checked in by anonymous, 4 years ago)

This commit was manufactured by cvs2svn to create tag
'release-0-9-1'.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 $web_error = "";
3
4 function eaccelerator_encoder_usage() {
5   echo "Usage:\tphp -q encoder.php [options] source_file_name\n";
6   echo       "\tphp -q encoder.php [options] source_file_name...\n";
7   echo       "\tphp -q encoder.php [options] source_directory_name...\n\n";
8   echo "Options:\n";
9   echo "\t-s suffix\n\t\tencode files only with following suffix (default is \"php\")\n";
10   echo "\t-a\n\t\tencode all files (no by default)\n";
11   echo "\t-l\n\t\tfollow symbolic links (no by default)\n";
12   echo "\t-r\n\t\tencode directories recursively (no by default)\n";
13   echo "\t-c\n\t\tcopy files those shouldn't be encoded (no by default)\n";
14   echo "\t-f\n\t\toverwrite existing files (no by default)\n";
15   echo "\t-o target\n\t\tIf you encode only one script then 'target' specifyes an output\n";
16   echo               "\t\tfile name. If you encode directory or several files at once\n";
17   echo               "\t\tthen 'target' specifyes an output directory name.\n";
18   echo "\nExamples:\n";
19   echo "\tphp -q encoder.php some_file.php\n";
20   echo "\tphp -q encoder.php some_file.php -o some_encoded_file.php\n";
21   echo "\tphp -q encoder.php *.php -o some_dir\n";
22   echo "\tphp -q encoder.php ~/public_html/x -rcf -sphp -sinc -o ~/public_html/y\n";
23   echo "\n";
24   exit();
25 }
26
27 function eaccelerator_error($str, $web) {
28   if ($web) {
29     global $web_error;
30     $web_error = "ERROR: $str";
31   } else {
32     echo "eAccelerator Encoder ERROR: $str\n";
33   }
34 }
35
36 function eaccelerator_encode_file($src, $out, $f, $c, $web) {
37   if (empty($out)) {
38     echo "\n// $src\n";
39   }
40   $prefix = "";
41   $cmp = eaccelerator_encode($src, $prefix);
42   if (empty($cmp)) {
43     eaccelerator_error("Can't compile file \"$src\"",$web);
44     if ($f) {
45       if ($c && !empty($out)) {
46         if ($web) {
47           global $web_error;
48           if (!empty($web_error)) {
49             echo "<font color=\"#ff0000\">$web_error</font><br>\n"; flush();
50             $web_error = "";
51           }
52         }
53         eaccelerator_copy_file($src, $out, $f, $web);
54       }
55     } else {
56       if (!$web) {
57         exit();
58       }
59     }
60   } else {
61     $cmp = $prefix.'<?php if (!is_callable("eaccelerator_load") && !@dl((PHP_OS=="WINNT"||PHP_OS=="WIN32")?"TurckLoader.dll":"TurckLoader.so")) { die("This PHP script has been encoded with eAccelerator, to run it you must install <a href=\"http://eaccelerator.sourceforge.net/\">eAccelerator or Turck Loader</a>");} return eaccelerator_load(\''.$cmp."');?>\n";
62     if (!empty($out)) {
63       if (!$f && file_exists($out)) {
64         eaccelerator_error("Can't create output file \"$out\" (already exists)",$web);
65       } else {
66         $file = @fopen($out,"wb");
67         if (!$file) {
68           eaccelerator_error("Can't open output file \"$out\"",$web);
69         } else {
70           fwrite($file,$cmp);
71           unset($cmp);
72           fclose($file);
73           $stat = stat($src);
74           chmod($out, $stat['mode']);
75           if ($web) {
76             echo "<font color=\"#00aa00\">Encoding: \"$src\" -> \"$out\"</font><br>\n";
77           }
78         }
79       }
80     } else {
81       if ($web) {
82         echo "<pre>".htmlspecialchars($cmp)."</pre>\n";
83       } else {
84         echo $cmp;
85       }
86       unset($cmp);
87     }
88   }
89 }
90
91 function eaccelerator_mkdir($dir, $f, $web) {
92   if (!empty($dir)) {
93     if (!@mkdir($dir,0777)) {
94       if (!$f) {
95         $error = "Can't create destination directory \"$dir\"";
96         if (file_exists($dir)) {
97           $error .= " (already exists)";
98         }
99         eaccelerator_error($error, $web);
100         return 0;
101       }
102     }
103   }
104   return 1;
105 }
106
107 function eaccelerator_copy_dir($src, $dir, $f, $web) {
108   $stat = stat($src);
109   $old = umask(0);
110   $ret = eaccelerator_mkdir($dir, $f, $web);
111   umask($old);
112   return $ret;
113 }
114
115 function eaccelerator_copy_file($src, $out, $f, $web) {
116   $i = @fopen($src, "rb");
117   if (!$i) {
118     eaccelerator_error("Can't open file \"$src\" for copying",$web);
119     return;
120   }
121   if (!$f && file_exists($out)) {
122     eaccelerator_error("Can't create output file \"$out\" (already exists)");
123   } else {
124     $o = @fopen($out, "wb");
125     if (!$o) {
126       eaccelerator_error("Can't copy file into \"$out\"");
127       return;
128     }
129     while ($tmp = fread($i, 1024*32)) {
130       fwrite($o, $tmp);
131     }
132     fclose($i);
133     fclose($o);
134     $stat = stat($src);
135     chmod($out, $stat['mode']);
136     if ($web) {
137       echo "<font color=\"#00aa00\">Copying: \"$src\" -> \"$out\"</font><br>\n";
138     }
139   }
140 }
141
142 function eaccelerator_copy_link($src, $out, $f, $web) {
143   $link = readlink($src);
144   if (!@symlink($link,$out)) {
145     if ($f && file_exists($out)) {
146       unlink($out);
147       eaccelerator_copy_link($src, $out, false, $web);
148     } else if ($f && is_array(lstat($out))) {
149       unlink($out);
150       eaccelerator_copy_link($src, $out, false, $web);
151     } else {
152       eaccelerator_error("Can't create symlink \"$out\" -> \"$link\"");
153       return;
154     }
155   }
156 }
157
158 function eaccelerator_encode_dir($src, $out, $s, $r, $l, $c, $f, $web) {
159   if ($dir = @opendir($src)) {
160     while (($file = readdir($dir)) !== false) {
161       if ($file == "." || $file == "..") continue;
162       $i = "$src/$file";
163       $o = empty($out)?$out:"$out/$file";
164       if (is_link($i)) {
165         if ($c && !empty($o)) {
166           eaccelerator_copy_link($i, $o, $f, $web);
167           if ($web) {
168             global $web_error;
169             if (!empty($web_error)) {
170               echo "<font color=\"#ff0000\">$web_error</font><br>\n"; flush();
171               $web_error = "";
172             }
173           }
174           continue;
175         } else if (!$l) {
176           continue;
177         }
178       }
179       if (is_dir($i)) {
180         if ($r) {
181           if (eaccelerator_copy_dir($i, $o, $f, $web)) {
182             eaccelerator_encode_dir($i, $o, $s, $r, $l, $c, $f, $web);
183           }
184         }
185       } else if (is_file($i)) {
186         if (empty($s)) {
187           eaccelerator_encode_file($i, $o, $f, $c, $web);
188         } else if (is_string($s)) {
189           if (preg_match("/".preg_quote(".$s")."\$/i", $file)) {
190             eaccelerator_encode_file($i, $o, $f, $c, $web);
191           } else if (!empty($o) && $c) {
192             eaccelerator_copy_file($i, $o, $f, $web);
193           }
194         } else if (is_array($s)) {
195           $encoded = false;
196           foreach($s as $z) {
197             if (preg_match("/".preg_quote(".$z")."\$/i", $file)) {
198               eaccelerator_encode_file($i, $o, $f, $c, $web);
199               $encoded = true;
200               break;
201             }
202           }
203           if (!$encoded && !empty($o) && $c) {
204             eaccelerator_copy_file($i, $o, $f, $web);
205           }
206         }
207       }
208       if ($web) {
209         global $web_error;
210         if (!empty($web_error)) {
211           echo "<font color=\"#ff0000\">$web_error</font><br>\n"; flush();
212           $web_error = "";
213         }
214       }
215     }
216     closedir($dir);
217   } else {
218     eaccelerator_error("Can't open source directory \"$src\"", $web);
219   }
220 }
221
222 function eaccelerator_encoder_main() {
223   $argc = $_SERVER['argc'];
224   $argv = $_SERVER['argv'];
225
226   $src = array();
227   $out = null;
228   unset($s);
229   $r = false;
230   $l = false;
231   $a = false;
232   $c = false;
233   $f = false;
234
235   for ($i = 1; $i < $argc; $i++) {
236     $arg = $argv[$i];
237     if (!empty($arg)) {
238       if ($arg[0] == '-') {
239         if ($arg[1] == "o") {
240           if (!empty($out)) {
241             eaccelerator_encoder_usage();
242           }
243           if (strlen($arg) == 2) {
244             if ($argc <= $i || empty($argv[$i+1]) || $argv[$i+1][0] == "-") {
245               eaccelerator_encoder_usage();
246             }
247             $out = $argv[++$i];
248           } else {
249             $out = substr($arg,2);
250           }
251
252         } else if ($arg[1] == "s") {
253           if (strlen($arg) == 2) {
254             $s[] = $argv[++$i];
255           } else {
256             $s[] = substr($arg,2);
257           }
258         } else if (strlen($arg) == 2 && $arg[1] == "r") {
259           $r = true;
260         } else if (strlen($arg) == 2 && $arg[1] == "l") {
261           $l = true;
262         } else if (strlen($arg) == 2 && $arg[1] == "a") {
263           $a = true;
264         } else if (strlen($arg) == 2 && $arg[1] == "c") {
265           $c = true;
266         } else if (strlen($arg) == 2 && $arg[1] == "f") {
267           $f = true;
268         } else {
269           $len = strlen($arg);
270           if ($len > 1) {
271             $n   = 1;
272             while ($n < $len) {
273               if ($arg[$n] == "r") {
274                 $r = true;
275               } else if ($arg[$n] == "l") {
276                 $l = true;
277               } else if ($arg[$n] == "a") {
278                 $a = true;
279               } else if ($arg[$n] == "c") {
280                 $c = true;
281               } else if ($arg[$n] == "f") {
282                 $f = true;
283               } else {
284                 if ($arg[$n] != "o" && $arg[$n] != "s") {
285                   echo("eAccelerator Encoder ERROR: Unknown option \"-".$arg[$n]."\"\n\n");
286                 }
287                 eaccelerator_encoder_usage();
288               }
289               ++$n;
290             }
291           } else {
292             echo("eAccelerator Encoder ERROR: Unknown option \"$arg\"\n\n");
293             eaccelerator_encoder_usage();
294           }
295         }
296       } else {
297         $src[] = $arg;
298       }
299     }
300   }
301   if (isset($src) && is_array($src) && count($src) > 0) {
302     $cnt = count($src);
303     if ($a) {
304       $s = "";
305     } else if (!isset($s)) {
306       $s = "php";
307     }
308     if ($cnt > 1) {
309       if (!eaccelerator_mkdir($out, $f, 0)) {
310         return;
311       }
312     }
313     foreach($src as $file) {
314       if (!file_exists($file)) {
315         echo("eAccelerator Encoder ERROR: Source file \"$file\" doesn't exist.\n");
316       } else {
317         if (is_dir($file)) {
318           if ($cnt == 1) {
319             if (eaccelerator_mkdir($out, $f, 0)) {
320               eaccelerator_encode_dir($file, $out, $s, $r, $l, $c, $f, 0);
321             }
322           } else {
323             if (eaccelerator_copy_dir($file, $out."/".basename($file), $f, 0)) {
324               eaccelerator_encode_dir($file, $out."/".basename($file), $s, $r, $l, $c, $f, 0);
325             }
326           }
327         } else {
328           if ($cnt == 1) {
329             eaccelerator_encode_file($file, $out, $f, $c, 0);
330           } else {
331             if (empty($out)) {
332               eaccelerator_encode_file($file, $out, $f, $c, 0);
333             } else {
334               eaccelerator_encode_file($file, $out."/".basename($file), $f, $c, 0);
335             }
336           }
337         }
338       }
339     }
340   } else {
341     eaccelerator_encoder_usage();
342   }
343 }
344
345 function eaccelerator_encoder_web() {
346   echo "<html><head><title>eAccelerator Encoder</title></head>".
347        "<body><h1 align=\"center\">eAccelerator Encoder ".EACCELERATOR_VERSION."</h1>";
348   $error = "";
349   $source = "";
350   $target = "";
351   $s = "php";
352   $suffixies = "php";
353   if (isset($_POST["submit"])) {
354     if (isset($_POST["source"])) {
355       $source = $_POST["source"];
356     }
357     if (isset($_POST["target"])) {
358       $target = $_POST["target"];
359     }
360     if (isset($_POST["suffixies"])) {
361       $suffixies = $_POST["suffixies"];
362       if (strpos($suffixies,",") !== false) {
363         $s = explode(",",$suffixies);
364       } else {
365         $s = $suffixies;
366       }
367     }
368     $all = isset($_POST["all"])?$_POST["all"]:false;
369     $links = isset($_POST["links"])?$_POST["links"]:false;
370     $recursive = isset($_POST["recursive"])?$_POST["recursive"]:false;
371     $copy = isset($_POST["copy"])?$_POST["copy"]:false;
372     $force = isset($_POST["force"])?$_POST["force"]:false;
373     if (empty($source)) {
374       $error = "ERROR: Source is not specified!";
375     } else if (!file_exists($source)) {
376       $error = "ERROR: Source file \"$source\" doesn't exist.\n";
377     } else {
378       if (is_dir($source)) {
379         if (eaccelerator_mkdir($target, $force, 1)) {
380           if ($all) {
381             $s = "";
382           }
383           eaccelerator_encode_dir($source, $target, $s, $recursive, $links, $copy, $force, 1);
384         }
385       } else {
386         eaccelerator_encode_file($source, $target, $force, $copy, 1);
387       }
388       global $web_error;
389       if (empty($web_error)) {
390         echo "<br><b>DONE</b></html>";
391         return;
392       } else {
393         $error = $web_error;
394       }
395     }
396   }
397   echo "<h3 align=\"center\"><font color=\"#ff0000\">$error</font></h3>".
398        "<form method=\"POST\">".
399        "<table border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"600\" bgcolor=\"#000000\" align=\"center\">".
400        "<tr valign=\"baseline\" bgcolor=\"#cccccc\"><td width=\"50%\" bgcolor=\"#ccccff\"><b>Souce file or directory name:</b></td><td width=\"50%\"><input type=\"text\" name=\"source\" size=\"32\" value=\"$source\" style=\"width:100%\"></td></tr>".
401        "<tr valign=\"baseline\" bgcolor=\"#cccccc\"><td width=\"50%\" bgcolor=\"#ccccff\"><b>Target file or directory name:</b></td><td width=\"50%\"><input type=\"text\" name=\"target\" size=\"32\" value=\"$target\" style=\"width:100%\"></td></tr>".
402        "<tr valign=\"baseline\" bgcolor=\"#cccccc\"><td width=\"50%\" bgcolor=\"#ccccff\"><b>PHP suffixies <small>(comma separated list)</small>:</b></td><td width=\"50%\"><input type=\"text\" name=\"suffixies\" size=\"32\" value=\"$suffixies\" style=\"width:100%\"></td></tr>".
403
404        "<tr valign=\"baseline\" bgcolor=\"#cccccc\"><td width=\"50%\" bgcolor=\"#ccccff\"><b>Options:</b></td><td width=\"50%\">".
405        "<input type=\"checkbox\" id=\"all\" name=\"all\"".(empty($all)?"":" checked")."> - <label for=\"all\">encode all files</label><br>".
406        "<input type=\"checkbox\" id=\"links\" name=\"links\"".(empty($links)?"":" checked")."> - <label for=\"links\">follow symbolic links</label><br>".
407        "<input type=\"checkbox\" id=\"recursive\" name=\"recursive\"".(empty($recursive)?"":" checked")."> - <label for=\"recursive\">encode directories recursively</label><br>".
408        "<input type=\"checkbox\" id=\"copy\" name=\"copy\"".(empty($copy)?"":" checked")."> - <label for=\"copy\">copy files those shouldn't be encoded</label><br>".
409        "<input type=\"checkbox\" id=\"force\" name=\"force\"".(empty($force)?"":" checked")."> - <label for=\"force\">overwrite existing files</label><br>".
410        "</td></tr>".
411        "<tr><td colspan=\"2\" align=\"center\" bgcolor=\"#cccccc\"><input type=\"submit\" name=\"submit\" value=\"OK\" style=\"width:100px\"></td></tr>".
412        "</form></body></html>";
413 }
414
415 set_time_limit(0);
416
417 function is_cli() {
418   if (php_sapi_name() == "cli" || empty($_SERVER['PHP_SELF'])) {
419     return 1;
420   } else {
421     return 0;
422   }
423 }
424
425 if (is_cli()) {
426   if (!is_callable("eaccelerator_encode") && !(!extension_loaded("eAccelerator") && @dl((PHP_OS=="WINNT"||PHP_OS=="WIN32")?"eaccelerator.dll":"eaccelerator.so") && is_callable("eaccelerator_encode"))) {
427     die("ERROR: eAccelerator Encoder is not installed\n");
428   }
429   if (!isset($_SERVER['argc'])) {
430     die("ERROR: Set \"register_argc_argv = On\" in your php.ini or use a CLI version of PHP to run encoder.\n");
431   }
432   eaccelerator_encoder_main();
433 } else {
434   if (!is_callable("eaccelerator_encode") && !(!extension_loaded("eAccelerator") && @dl((PHP_OS=="WINNT"||PHP_OS=="WIN32")?"eaccelerator.dll":"eaccelerator.so") && is_callable("eaccelerator_encode"))) {
435     die("<html><head><title>eAccelerator Encoder</title></head><body><h1 align=\"center\">eAccelerator Encoder is not installed</h1></body></html>");
436   }
437   eaccelerator_encoder_web();
438 }
439 ?>
440
Note: See TracBrowser for help on using the browser.