Debian Codegraph @

src/scanner.php

raw ◀ ▶
1 Debian patch touch this file
1 #!/usr/bin/php
2 <?php
3
4 // scan a root folder and all its subfolders for softwares.
5 // each known software has its own function that we launch to detect it.
6 // the function returns the detected version of the software.
7 // (then we could check, using the software-checksum-service, that this software version's files are unchanged.)
8
9 if (!isset($argv[1])) {
10 echo "Usage: scanner.php <root path>\n";
11 exit();
12 }
13
14 $root=realpath($argv[1]);
15
16 if (!$root || !is_dir($root)) {
17 echo "FATAL: $root is not a directory or can't be opened\n";
18 }
19
20 $t=stat($root);
21 $fs=$t["dev"]; // we don't cross filesystems.
22
•23 $d=@opendir('/usr/share/php/CmsScanner');
24 if (!$d) die("FATAL: Can't open /usr/share/php/CmsScanner folder.");
25 while (($c=readdir($d))!==false) {
26 if (substr($c,-4)==".php") {
•27 require_once('/usr/share/php/CmsScanner/'.$c);
28 $classname=substr($c,0,-4);
29 $classes[strtolower($classname)]=new $classname();
30 }
31 }
32 closedir($d);
33
34 scanner($root);
35
36
37 function scanner($root,$level=0) {
38 global $fs,$classes;
39 foreach($classes as $class=>$object) {
40 if ($version = $object->search($root)) {
41 echo "$class $version $root\n";
42 }
43 }
44 $d=@opendir($root);
45 if (!$d) {
46 echo "ERROR: Can't open folder $root (deleted or permission denied)\n";
47 return;
48 }
49 $root=rtrim($root,"/");
50 while (($c=readdir($d))!==false) {
51 if ($c=="." || $c=="..") continue;
52 if (is_link($root."/".$c)) continue;
53 if (is_dir($root."/".$c)) {
54 $t=stat($root."/".$c);
55 if ($t["dev"]!=$fs) continue; // we skip subfolders in OTHER filesystems (nfs, crossmounts ...)
56 scanner($root."/".$c,$level+1); // recursive call.
57 }
58 }
59 closedir($d);
60 }
61