Subversion Repositories oidplus

Rev

Rev 597 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #!/usr/bin/env php
  2. <?php
  3.  
  4. /*
  5.  * OIDplus 2.0
  6.  * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
  7.  *
  8.  * Licensed under the Apache License, Version 2.0 (the "License");
  9.  * you may not use this file except in compliance with the License.
  10.  * You may obtain a copy of the License at
  11.  *
  12.  *     http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing, software
  15.  * distributed under the License is distributed on an "AS IS" BASIS,
  16.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17.  * See the License for the specific language governing permissions and
  18.  * limitations under the License.
  19.  */
  20.  
  21. $dir = __DIR__ . '/../../';
  22.  
  23. // ---
  24.  
  25. $all_cont  = "<?php\n";
  26. $all_cont .= "__halt_compiler();\n"; // avoid that the FastPHP code explorer runs crazy
  27. $all_cont .= "?>\n";
  28.  
  29. $it = new RecursiveDirectoryIterator($dir);
  30. foreach(new RecursiveIteratorIterator($it) as $file) {
  31.         if (strpos(str_replace('\\','/',realpath($file)),'/vendor/') !== false) continue; // ignore third-party-code
  32.         if (strpos(str_replace('\\','/',realpath($file)),'/dev/') !== false) continue; // ignore development utilities
  33.         if (basename($file) == 'ipv4_functions.inc.php') continue; // 3P
  34.         if (basename($file) == 'ipv6_functions.inc.php') continue; // 3P
  35.         if (basename($file) == 'gmp_supplement.inc.php') continue; // 3P
  36.         if (basename($file) == 'xml_utils.inc.php') continue; // 3P
  37.         if (basename($file) == 'uuid_utils.inc.php') continue; // 3P
  38.         if (basename($file) == 'oid_utils.inc.php') continue; // 3P
  39.         if (basename($file) == 'oidinfo_api.inc.php') continue; // 3P
  40.         if (basename($file) == 'anti_xss.inc.php') continue; // 3P
  41.         if ($file->getExtension() == 'js') {
  42.  
  43.                 $cont = file_get_contents($file);
  44.  
  45.                 $ary = explode("\n",$cont);
  46.                 $cont = '';
  47.                 foreach ($ary as $a) {
  48.                         if (stripos($a,'do not translate') === false) {
  49.                                 $cont .= "$a\n";
  50.                         }
  51.                 }
  52.  
  53.                 $cont = preg_replace('@_L\\((\'|")(.*)\\1@smU', '_L(...', $cont);
  54.  
  55.                 $ary = explode("\n",$cont);
  56.                 $cont = '';
  57.                 foreach ($ary as $a) {
  58.                         $cont .= wordwrap(trim($a), 80)."\n";
  59.                 }
  60.  
  61.                 $all_cont .= "=============== ".realpath($file)." ===============\n";
  62.                 $all_cont .= $cont."\n\n";
  63.  
  64.         }
  65.         if ($file->getExtension() == 'php') {
  66.                 $cont = file_get_contents($file);
  67.  
  68.                 $ary = explode("\n",$cont);
  69.                 $cont = '';
  70.                 foreach ($ary as $a) {
  71.                         if (stripos($a,'do not translate') === false) {
  72.                                 $cont .= "$a\n";
  73.                         }
  74.                 }
  75.  
  76.                 $cont = phpRemoveComments($cont);
  77.  
  78.                 $cont = str_replace('<li>', '', $cont);
  79.                 $cont = str_replace('</li>', '', $cont);
  80.                 $cont = str_replace('<ul>', '', $cont);
  81.                 $cont = str_replace('</ul>', '', $cont);
  82.                 $cont = str_replace('<th>', '', $cont);
  83.                 $cont = str_replace('</th>', '', $cont);
  84.                 $cont = str_replace('<pre>', '', $cont);
  85.                 $cont = str_replace('</pre>', '', $cont);
  86.                 $cont = str_replace('</a>', '', $cont);
  87.                 $cont = str_replace('<br>', '', $cont);
  88.                 $cont = str_replace('<br/>', '', $cont);
  89.                 $cont = str_replace('<br />', '', $cont);
  90.                 $cont = str_replace('<h1>', '', $cont);
  91.                 $cont = str_replace('</h1>', '', $cont);
  92.                 $cont = str_replace('<h2>', '', $cont);
  93.                 $cont = str_replace('</h2>', '', $cont);
  94.                 $cont = str_replace('<h3>', '', $cont);
  95.                 $cont = str_replace('</h3>', '', $cont);
  96.                 $cont = str_replace('<p>', '', $cont);
  97.                 $cont = str_replace('</p>', '', $cont);
  98.                 $cont = str_replace('<th width="25%">', '', $cont);
  99.                 $cont = str_replace('<th width="50%">', '', $cont);
  100.                 $cont = str_replace('</th>', '', $cont);
  101.                 $cont = str_replace('<td>', '', $cont);
  102.                 $cont = str_replace('</td>', '', $cont);
  103.                 $cont = str_replace('<tr>', '', $cont);
  104.                 $cont = str_replace('</tr>', '', $cont);
  105.                 $cont = str_replace('<b>', '', $cont);
  106.                 $cont = str_replace('</b>', '', $cont);
  107.                 $cont = str_replace('<i>', '', $cont);
  108.                 $cont = str_replace('</i>', '', $cont);
  109.                 $cont = str_replace('<u>', '', $cont);
  110.                 $cont = str_replace('</u>', '', $cont);
  111.                 $cont = preg_replace('@<form.+>@ismU', '', $cont);
  112.                 $cont = preg_replace('@<table.+>@ismU', '', $cont);
  113.                 $cont = str_replace('</form>', '', $cont);
  114.                 $cont = str_replace('</table>', '', $cont);
  115.  
  116.                 $cont = str_replace('\\\\', chr(1), $cont);
  117.                 $cont = str_replace('\\"', chr(2), $cont);
  118.                 $cont = str_replace("\\'", chr(3), $cont);
  119.  
  120.                 $cont = preg_replace_callback('@("|\').*\\1@ismU', function($treffer) {
  121.                         $x = $treffer[0];
  122.  
  123.                         if (strpos($x,'_L(') !== false) {
  124.                                 echo "Attention: '_L(' inside a string?! at <pre>'.htmlentities($x).'</pre>\n";
  125.                         }
  126.  
  127.                         $x = str_replace('(', chr(4), $x);
  128.                         $x = str_replace(')', chr(5), $x);
  129.  
  130.                         /*
  131.                         if (strpos($x,"\n") !== false) echo 'DEBUG: <pre>'.htmlentities($x)."</pre>\n\n\n\n\n";
  132.                         */
  133.  
  134.                         return $x;
  135.                 }, $cont);
  136.  
  137.                 $cont = preg_replace('@_L\\((\'|")(.*)\\1@smU', '_L(...', $cont);
  138.  
  139.                 $cont = preg_replace('@logger\\(\\)\\->log\\([^\n]*\\)@smU', 'logger()->log(...)', $cont);
  140.                 $cont = preg_replace('@config\\(\\)\\->getValue\\([^\n]*\\)@smU', 'config()->getValue(...)', $cont);
  141.                 $cont = preg_replace('@config\\(\\)\\->setValue\\([^\n]*\\)@smU', 'config()->setValue(...)', $cont);
  142.                 $cont = preg_replace('@prepareConfigKey\\([^\n]*\\)@smU', 'prepareConfigKey(...)', $cont);
  143.                 $cont = preg_replace('@baseConfig\\(\\)\\->getValue\\([^\n]*\\)@smU', 'baseConfig()->getValue(...)', $cont);
  144.                 $cont = preg_replace('@query\\([^\n]*\\)@smU', 'query(...)', $cont);
  145.                 $cont = preg_replace('@query\\([^\n]*\\)@smU', 'query(...)', $cont);
  146.                 $cont = preg_replace('@registerAllPlugins\\([^\n]*\\);@smU', 'registerAllPlugins(...)', $cont);
  147.  
  148.                 $cont = preg_replace('@\\[(\'|")([^\n]*)\\1\\]@smU', '[...]', $cont);
  149.                 $cont = preg_replace('@header\\([^\n]*\\);@smU', 'header(...)', $cont);
  150.                 $cont = preg_replace('@setcookie\\([^\n]*\\);@smU', 'header(...)', $cont);
  151.                 $cont = preg_replace('@file_exists\\((\'|")([^\n]*)\\1\\)@smU', 'file_exists(...)', $cont);
  152.                 $cont = preg_replace('@is_dir\\((\'|")([^\n]*)\\1\\)@smU', 'is_dir(...)', $cont);
  153.                 $cont = preg_replace('@ini_get\\((\'|")([^\n]*)\\1\\)@smU', 'ini_get(...)', $cont);
  154.                 $cont = preg_replace('@realpath\\((\'|")([^\n]*)\\1\\)@smU', 'realpath(...)', $cont);
  155.  
  156.                 $cont = preg_replace('@include_once(.+);@ismU', '', $cont);
  157.                 $cont = preg_replace('@require_once(.+);@ismU', '', $cont);
  158.                 $cont = preg_replace('@==\\s*(\'|").*\\1@ismU', '==...', $cont);
  159.                 $cont = preg_replace('@!=\\s*(\'|").*\\1@ismU', '!=...', $cont);
  160.  
  161.                 $cont = str_replace(chr(1), "\\\\", $cont);
  162.                 $cont = str_replace(chr(2), '\\"', $cont);
  163.                 $cont = str_replace(chr(3), "\\'", $cont);
  164.                 $cont = str_replace(chr(4), "(", $cont);
  165.                 $cont = str_replace(chr(5), ")", $cont);
  166.  
  167.                 $cont = str_replace("\t", "", $cont);
  168.  
  169.                 $cont = str_replace("''", '', $cont);
  170.                 $cont = str_replace('""', '', $cont);
  171.  
  172.                 $cont = str_replace("'.'", '', $cont);
  173.                 $cont = str_replace('"."', '', $cont);
  174.  
  175.                 $cont = str_replace("':'", '', $cont);
  176.                 $cont = str_replace('":"', '', $cont);
  177.  
  178.                 $cont = str_replace("';'", '', $cont);
  179.                 $cont = str_replace('";"', '', $cont);
  180.  
  181.                 $cont = str_replace('"\\n"', '', $cont);
  182.  
  183.                 $cont = str_replace('"\\r\\n"', '', $cont);
  184.  
  185.                 $ary = explode("\n",$cont);
  186.                 $cont = '';
  187.                 foreach ($ary as $a) {
  188.                         $cont .= wordwrap($a, 80)."\n";
  189.                 }
  190.  
  191.                 $all_cont .= "=============== ".realpath($file)." ===============\n";
  192.                 $all_cont .= $cont."\n\n";
  193.                 $all_cont .= "?>\n";
  194.         }
  195. }
  196.  
  197. $outfile = __DIR__.'/.nontranslated.tmp';
  198. $fastphp = 'C:\\Program Files (x86)\\FastPHP\\FastPHPEditor.exe';
  199.  
  200. file_put_contents($outfile, $all_cont);
  201.  
  202. if (file_exists($fastphp)) {
  203.         system('"'.$fastphp.'" "'.$outfile.'"');
  204.         unlink($outfile);
  205. } else {
  206.         echo "Generated file $outfile\n";
  207. }
  208.  
  209. # ---
  210.  
  211. function phpRemoveComments($fileStr) {
  212.  
  213.         // https://stackoverflow.com/questions/503871/best-way-to-automatically-remove-comments-from-php-code
  214.  
  215.         $newStr  = '';
  216.  
  217.         $commentTokens = array(T_COMMENT);
  218.  
  219.         if (defined('T_DOC_COMMENT')) $commentTokens[] = T_DOC_COMMENT; // PHP 5
  220.         if (defined('T_ML_COMMENT'))  $commentTokens[] = T_ML_COMMENT;  // PHP 4
  221.  
  222.         $tokens = token_get_all($fileStr);
  223.  
  224.         foreach ($tokens as $token) {
  225.                 if (is_array($token)) {
  226.                         if (in_array($token[0], $commentTokens)) continue;
  227.                         $token = $token[1];
  228.                 }
  229.                 $newStr .= $token;
  230.         }
  231.  
  232.         return $newStr;
  233.  
  234. }
  235.