root/eaccelerator/tags/0.9.5-beta2/encoder.php

Revision 75, 15.5 kB (checked in by zoeloelip, 4 years ago)

User who supplied patch seemed to use the version with oloader. Fixing again.

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