Subversion Repositories vnag

Rev

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

Rev Author Line No. Line
82 daniel-mar 1
#!/usr/bin/php
79 daniel-mar 2
<?php
3
 
4
/*
5
 * VNag - Nagios Framework for PHP
6
 * Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com>
7
 * Licensed under the terms of the Apache 2.0 license
8
 *
9
 * Revision 2023-10-14
10
 */
11
 
12
function vnag_make_phar($plugin) {
80 daniel-mar 13
        $filename = __DIR__.'/../bin/'.$plugin.'.phar';
79 daniel-mar 14
 
82 daniel-mar 15
        copy(__DIR__.'/plugins/'.$plugin.'/icinga2.conf', __DIR__.'/../bin/'.$plugin.'.conf');
79 daniel-mar 16
 
17
        # ---
18
 
19
        $input_files = [__DIR__.'/framework/vnag_framework.inc.php'];
20
        $input_files = array_merge($input_files, glob(__DIR__.'/plugins/'.$plugin.'/*.php*'));
21
 
22
        $main = '';
23
        $files_for_phar = [];
24
        foreach ($input_files as &$input_file) {
25
 
26
                if (strpos($input_file, 'index.php') !== false) continue;
27
 
28
                $input_file_short = substr($input_file, strlen(__DIR__)+1);
29
 
30
                if (strpos(file_get_contents($input_file),'->run()') !== false) {
31
                        $main = $input_file_short;
32
                }
33
 
34
                $files_for_phar[$input_file_short] = $input_file;
35
        }
36
 
37
        if (!$main) throw new Exception("Could not find VNag plugin main file for plugin $plugin");
38
 
39
        # ---
40
 
41
        $max_mtime = 0;
81 daniel-mar 42
        $algo = 'sha256';
43
        $checksums = $algo.'||';
44
        ksort($files_for_phar);
79 daniel-mar 45
        foreach ($files_for_phar as $input_file_short => $input_file) {
46
                $max_mtime = max($max_mtime, filemtime($input_file));
81 daniel-mar 47
                $checksums .= $input_file_short.'|'.hash_file($algo,$input_file).'||';
79 daniel-mar 48
        }
49
 
50
        if (file_exists($filename)) {
81 daniel-mar 51
                $phar = new Phar($filename);
52
                $metadata = $phar->getMetadata();
53
                if (($metadata['1.3.6.1.4.1.37476.3.0.2']??'') == $checksums) {
79 daniel-mar 54
                        echo "Plugin $filename already up-to-date\n";
81 daniel-mar 55
                        $phar = null;
79 daniel-mar 56
                        return;
57
                }
58
        }
59
 
60
        # ---
61
 
62
        if (file_exists($filename)) unlink($filename);
63
        $phar = new Phar($filename);
64
        echo "Generate $filename\n";
65
        foreach ($files_for_phar as $input_file_short => $input_file) {
66
                echo "\tAdd: $input_file_short\n";
67
                $phar->addFromString ($input_file_short, php_strip_whitespace ($input_file));
68
        }
69
 
83 daniel-mar 70
        $stub  = "#!/usr/bin/env php\n";
71
        $stub .= "<?php @ob_end_clean(); ?>"; // ob_end_clean() avoids that Shebang is sent to webserver daemon
72
        $stub .= $phar->createDefaultStub($main);
73
        $phar->setStub($stub);
79 daniel-mar 74
 
75
        #$private = openssl_get_privatekey(file_get_contents(__DIR__.'/private.pem'));
76
        #$pkey = '';
77
        #openssl_pkey_export($private, $pkey);
78
        #$phar->setSignatureAlgorithm(Phar::OPENSSL, $pkey);
79
        #copy(__DIR__.'/public.pem', $filename.'.pubkey');
80
        $phar->setSignatureAlgorithm(Phar::SHA512);
81
 
81 daniel-mar 82
        $metadata = [];
83
        $metadata['1.3.6.1.4.1.37476.3.0.2'] = $checksums;
84
        #$metadata['bootstrap'] = $main;
85
        $phar->setMetadata($metadata);
86
 
79 daniel-mar 87
        $phar = null; // save
88
 
89
        chmod($filename, fileperms($filename) | 0755);
90
 
91
        touch($filename, $max_mtime);
92
}
93
 
94
$plugins = glob(__DIR__.'/plugins/*');
95
foreach ($plugins as $plugin) {
96
        if (!is_dir($plugin)) continue;
97
        vnag_make_phar(basename($plugin));
98
}