PageRenderTime 38ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/plugin_orphanFiles/orphanFilesSrvr.php

https://bitbucket.org/mstetson/obiblio-10-wip
PHP | 298 lines | 275 code | 4 blank | 19 comment | 45 complexity | d27e753574fa004c22f0ec2084ba9b10 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /* This file is part of a copyrighted work; it is distributed with NO WARRANTY.
  3. * See the file COPYRIGHT.html for more details.
  4. */
  5. require_once("../shared/common.php");
  6. //print_r($_REQUEST);echo "<br />";
  7. /*
  8. if ($_REQUEST['verb'] == 'No')
  9. $verbose = false;
  10. else
  11. $verbose = true;
  12. if ($_REQUEST['detl'] == 'No')
  13. $detail = false;
  14. else
  15. $detail = true;
  16. */
  17. $verbose = ($_REQUEST['verb'] == 'No')?false:true;
  18. $detail = ($_REQUEST['detl'] == 'No')?false:true;
  19. if ($verbose == true) $detail = true;
  20. //var_dump($verbose); var_dump($detail); echo "<br />";
  21. function getDirList () {
  22. $dirs = [];
  23. $handl = opendir("..");
  24. while (false !== ($file = readdir($handl))) {
  25. if ($file != '.' && $file != '..') {
  26. if ((is_dir('../'.$file)) &&
  27. (substr($file,0,1) != '.') &&
  28. ## folowing names are main directories
  29. ($file != 'images') &&
  30. ($file != 'font') &&
  31. ($file != 'layouts') &&
  32. ($file != 'locale') &&
  33. ($file != 'themes') &&
  34. ($file != 'working') &&
  35. ($file != 'photos')
  36. ) {
  37. $dirs[] = $file;
  38. }
  39. }
  40. }
  41. closedir($handl);
  42. sort($dirs,SORT_LOCALE_STRING);
  43. return $dirs;
  44. };
  45. function array_flat($array) {
  46. $tmp = [];
  47. foreach($array as $a) {
  48. if(is_array($a)) {
  49. $tmp = array_merge($tmp, array_flat($a));
  50. } else {
  51. $tmp[] = $a;
  52. }
  53. }
  54. return $tmp;
  55. }
  56. function getFileList($dir) {
  57. $files = array();
  58. if ($handle = opendir($dir)) {
  59. while (false !== ($file = readdir($handle))) {
  60. $info = pathInfo($file);
  61. if (($file != ".") && ($file != "..") &&
  62. ## folowing names are sub-directories
  63. ($file != 'sql') &&
  64. ($file != 'legacy') &&
  65. ($file != 'jquery') &&
  66. ## folowing names are file extensions
  67. ($info['extension'] != 'tran') &&
  68. ## folowing are special filenames
  69. ($info['basename'] != 'custom_head.php')
  70. ) {
  71. if(is_dir($dir.'/'.$file)) {
  72. $dir2 = $dir.'/'.$file;
  73. $files[] = getFileList($dir2);
  74. } else {
  75. $aFile = $dir.'/'.$file;
  76. $files[] = $aFile;
  77. }
  78. }
  79. }
  80. closedir($handle);
  81. }
  82. return array_flat($files);
  83. }
  84. function getFnFmPhpReq($text, $dir) {
  85. if (stripos($text, 'required') >= 1) return '';
  86. $in = str_replace("'", "\"", $text);
  87. $in = str_replace('__, "', '__,"', $in);
  88. if($verbose) {echo "in===>";echo $in;echo"<br />";}
  89. preg_match('/((\.\.)?\/|_,\")(.*?)(\?|\"|\.js|\.css)/', $in, $out);
  90. if($verbose) {echo "out===>";print_r($out);echo"<br />";}
  91. $rslt = $out[3];
  92. $rslt = str_replace("(","",$rslt);
  93. $rslt = str_replace(")","",$rslt);
  94. $rslt = str_replace(";","",$rslt);
  95. $rslt = str_replace('"',"",$rslt);
  96. if ($out[1] == '../') $rslt = '../'.$rslt;
  97. if (!preg_match('/((\.|\.\.)?\/)/',$rslt)) $rslt = '../'.$dir.'/'.$rslt;
  98. return trim($rslt);
  99. }
  100. function getFnFmPhpHref($text, $dir) {
  101. $in = str_replace("'", "\"", $text);
  102. $in = str_replace('__, "', '__,"', $in);
  103. if($verbose) {echo "in===>";echo $in;echo"<br />";}
  104. preg_match('/(href=\")(.*?)(.php|.css)/', $in, $out);
  105. if($verbose) {echo "out===>";print_r($out);echo"<br />";}
  106. $rslt = $out[2].$out[3];
  107. $rslt = str_replace('"',"",$rslt);
  108. return trim($rslt);
  109. }
  110. function getFnFmPhpActn($text, $dir) {
  111. $in = str_replace("'", "\"", $text);
  112. if($verbose) {echo "in===>";echo $in;echo"<br />";}
  113. preg_match('/(action=\")(.*?)(.php)/', $in, $out);
  114. if($verbose) {echo "out===>";print_r($out);echo"<br />";}
  115. $rslt = $out[2].$out[3];
  116. $rslt = str_replace('"',"",$rslt);
  117. return trim($rslt);
  118. }
  119. function getFnFmPhpArray($text, $dir) {
  120. $in = str_replace("'", "\"", $text);
  121. if($verbose) {echo "in===>";echo $in;echo"<br />";}
  122. preg_match('/(\"action\"=>\")(.*?)(.php)/', $in, $out);
  123. if($verbose) {echo "out===>";print_r($out);echo"<br />";}
  124. $rslt = $out[2].$out[3];
  125. $rslt = str_replace('"',"",$rslt);
  126. return trim($rslt);
  127. }
  128. function getFnFmPhpLinkUrl($text, $dir) {
  129. $in = str_replace("'", "\"", $text);
  130. if($verbose) {echo "in===>";echo $in;echo"<br />";}
  131. preg_match('/(\.\.)?\/(.*?)(.php)/', $in, $out);
  132. if($verbose) {echo "out===>";print_r($out);echo"<br />";}
  133. $rslt = $out[1].$out[2].$out[3];
  134. $rslt = str_replace('"',"",$rslt);
  135. return trim($rslt);
  136. }
  137. function getFnFmPhpHdr($text, $dir) {
  138. if (stripos($text, 'age::header') >= 1) return '';
  139. $in = str_replace("'", "\"", $text);
  140. if($verbose) {echo "in===>";echo $in;echo"<br />";}
  141. preg_match('/((\.\.)?\/)(.*?)(.php)/', $in, $out);
  142. if($verbose) {echo "out===>";print_r($out);echo"<br />";}
  143. $rslt = $out[1].$out[3].$out[4];
  144. $rslt = str_replace('"',"",$rslt);
  145. return trim($rslt);
  146. }
  147. function getFnFmPhpSrc($text, $dir) {
  148. if (stripos($text, 'img src') >= 1) return '';
  149. $in = str_replace("'", "\"", $text);
  150. $in = str_replace('__, "', '__,"', $in);
  151. if($verbose) {echo "in===>";echo $in;echo"<br />";}
  152. preg_match('/(src=\")(.*?)(\.js)/', $in, $out);
  153. if($verbose) {echo "out===>";print_r($out);echo"<br />";}
  154. $rslt = $out[2].$out[3];
  155. $rslt = str_replace('"',"",$rslt);
  156. return trim($rslt);
  157. }
  158. function getFilenameFmJS($text, $dir) {
  159. $in = str_replace("'", "\"", $text);
  160. if($verbose) {echo "in===>";echo $in;echo"<br />";}
  161. preg_match('/(\")(.*?)(\.php|\.js)/', $in, $out);
  162. if($verbose) {echo "out===>";print_r($out);echo"<br />";}
  163. $rslt = $out[2];
  164. if (substr($rslt, 0,3) != '../') $rslt = '../'.$dir.'/'.$rslt;
  165. if (substr($rslt, -4,4) != '.php') $rslt = $rslt.$out[3];
  166. return trim($rslt);
  167. }
  168. function getFilenameFmMenu($text, $dir) {
  169. $in = str_replace("'", "\"", $text);
  170. if($verbose) {echo "in===>";echo $in;echo"<br />";}
  171. preg_match('/(\"\.\.\/)(.*?)(\.php)/', $in, $out);
  172. if($verbose) {echo "out===>";print_r($out);echo"<br />";}
  173. $rslt = $out[0];
  174. if ($rslt == '') return '';
  175. $rslt = str_replace('"','',$rslt);
  176. if (substr($rslt, 0,3) != '../') $rslt = '../'.$dir.'/'.$rslt;
  177. if (substr($rslt, -4,4) != '.php') $rslt = $rslt.'.php';
  178. return trim($rslt);
  179. }
  180. $files = [];
  181. switch ($_REQUEST['mode']) {
  182. case 'ck4Orfans':
  183. $found = [];
  184. $dirs = getDirList();
  185. foreach ($dirs as $dir) {
  186. if($detail)echo "<br /><br />---- $dir ----<br />";
  187. $dirFiles = getFileList('../'.$dir);
  188. //echo "dir files: ";print_r($dirFiles);echo "<br />";
  189. foreach ($dirFiles as $fileName) {
  190. // $fileName = '../'.$fileName;
  191. if (is_dir($fileName)) {
  192. if($detail) echo "--dir-- fn===> $fileName skipped <br />";
  193. break;
  194. }
  195. if($detail)echo '<p class="bold">'.$fileName."</p>";
  196. $allFiles[] = $fileName;
  197. $lines = file($fileName, FILE_SKIP_EMPTY_LINES);
  198. foreach ($lines as $line_num => $line) {
  199. $line = trim($line);
  200. if ($line != '') {
  201. if ( (substr($line,0,7) == "<script") ||
  202. (substr($line,0,5) == "<link") ||
  203. (substr($line,0,5) == "<form") ||
  204. (substr($line,0,3) == "<a ") ||
  205. (
  206. substr($line,0,2) != "/*" &&
  207. substr($line,0,2) != "*/" &&
  208. substr($line,0,2) != "//" &&
  209. substr($line,0,1) != "#" &&
  210. //substr($line,0,1) != "<" &&
  211. substr($line,0,1) != "?" &&
  212. substr($line,0,1) != "\n" &&
  213. substr($line,0,1) != "*" ) ){
  214. if (stripos($line, 'equire', 0) >= 1) {
  215. $fn = getFnFmPhpReq($line, $dir);
  216. } elseif (stripos($line, 'nclude', 0) >= 1) {
  217. $fn = getFnFmPhpReq($line, $dir);
  218. } elseif (stripos($line, 'href=', 0) >= 1) {
  219. $fn = getFnFmPhpHref($line, $dir);
  220. } elseif (stripos($line, "action'=>", 0) >= 1) {
  221. $fn = getFnFmPhpArray($line, $dir);
  222. } elseif (stripos($line, 'action=', 0) >= 1) {
  223. $fn = getFnFmPhpActn($line, $dir);
  224. } elseif (stripos($line, 'LinkUrl', 0) >= 1) {
  225. $fn = getFnFmPhpLinkUrl($line, $dir);
  226. } elseif (stripos($line, 'eader(', 0) >= 1) {
  227. $fn = getFnFmPhpHdr($line, $dir);
  228. } elseif (stripos($line, 'src=', 0) >= 1) {
  229. $fn = getFnFmPhpSrc($line, $dir);
  230. } elseif (stripos($line, "url = ", 0) >= 1) {
  231. $fn = getFilenameFmJS($line, $dir);
  232. } elseif (stripos($line, "av::node", 0) >= 1) {
  233. $fn = getFilenameFmMenu($line, $dir);
  234. } else {
  235. $fn = '';
  236. }
  237. $fn = trim($fn);
  238. if ($fn != '') {
  239. if (array_key_exists($fn, $found)) {
  240. if($detail)echo "--dupe-- fn===> $fn<br />";
  241. continue;
  242. } else {
  243. if($detail)echo "--added-- fn===> $fn<br />";
  244. $found[$fn] = 'OK';
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }
  251. }
  252. echo "There are ".count($allFiles)." files in ".count($dirs)." directories.<br />";
  253. echo count($found)." files are referenced.<br />";
  254. ksort($found);
  255. sort($allFiles);
  256. //var_dump($found);
  257. // if (count($allFiles) == count($found)) {
  258. // echo "All project files are in use.";
  259. // } elseif (count($allFiles) > count($found)) {
  260. echo "<p>The following files are not being used and may be removed.</p>";
  261. for ($i=0; $i<count($allFiles); $i++) {
  262. $file = trim($allFiles[$i]);
  263. if ((array_key_exists($file, $found)) && ($found[$file] == 'OK')) {
  264. continue;
  265. } else {
  266. $info = pathinfo($file);
  267. if ($info['extension'] != 'nav'){
  268. echo "unused===> $file <br />";
  269. }
  270. }
  271. }
  272. // } else {
  273. // echo (count($found)-count($allFiles))." scanned-in filenames are invalid.<br />";
  274. // for ($i=0; $i<count($found); $i++) {
  275. // $file = trim($found[$i]);
  276. // if (array_key_exists($file, $allFiles)) {
  277. // continue;
  278. // } else {
  279. // echo "invalid===> $file <br />";
  280. // }
  281. // }
  282. // }
  283. break;
  284. default:
  285. echo "<h4>".T("invalid mode").": &gt;".$_REQUEST['mode']."&lt;</h4><br />";
  286. break;
  287. }
  288. ?>