Subversion Repositories vnag

Rev

Rev 83 | 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
                $input_file_short = substr($input_file, strlen(__DIR__)+1);
27
 
28
                if (strpos(file_get_contents($input_file),'->run()') !== false) {
29
                        $main = $input_file_short;
30
                }
31
 
32
                $files_for_phar[$input_file_short] = $input_file;
33
        }
34
 
35
        if (!$main) throw new Exception("Could not find VNag plugin main file for plugin $plugin");
36
 
37
        # ---
38
 
39
        $max_mtime = 0;
81 daniel-mar 40
        $algo = 'sha256';
41
        $checksums = $algo.'||';
84 daniel-mar 42
        $checksums .= '<builder>|'.hash_file($algo,__FILE__).'||';
81 daniel-mar 43
        ksort($files_for_phar);
79 daniel-mar 44
        foreach ($files_for_phar as $input_file_short => $input_file) {
45
                $max_mtime = max($max_mtime, filemtime($input_file));
81 daniel-mar 46
                $checksums .= $input_file_short.'|'.hash_file($algo,$input_file).'||';
79 daniel-mar 47
        }
48
 
49
        if (file_exists($filename)) {
81 daniel-mar 50
                $phar = new Phar($filename);
51
                $metadata = $phar->getMetadata();
52
                if (($metadata['1.3.6.1.4.1.37476.3.0.2']??'') == $checksums) {
79 daniel-mar 53
                        echo "Plugin $filename already up-to-date\n";
81 daniel-mar 54
                        $phar = null;
79 daniel-mar 55
                        return;
56
                }
57
        }
58
 
59
        # ---
60
 
61
        if (file_exists($filename)) unlink($filename);
62
        $phar = new Phar($filename);
63
        echo "Generate $filename\n";
64
        foreach ($files_for_phar as $input_file_short => $input_file) {
65
                echo "\tAdd: $input_file_short\n";
66
                $phar->addFromString ($input_file_short, php_strip_whitespace ($input_file));
67
        }
68
 
83 daniel-mar 69
        $stub  = "#!/usr/bin/env php\n";
70
        $stub .= "<?php @ob_end_clean(); ?>"; // ob_end_clean() avoids that Shebang is sent to webserver daemon
71
        $stub .= $phar->createDefaultStub($main);
72
        $phar->setStub($stub);
79 daniel-mar 73
 
74
        #$private = openssl_get_privatekey(file_get_contents(__DIR__.'/private.pem'));
75
        #$pkey = '';
76
        #openssl_pkey_export($private, $pkey);
77
        #$phar->setSignatureAlgorithm(Phar::OPENSSL, $pkey);
78
        #copy(__DIR__.'/public.pem', $filename.'.pubkey');
79
        $phar->setSignatureAlgorithm(Phar::SHA512);
80
 
81 daniel-mar 81
        $metadata = [];
82
        $metadata['1.3.6.1.4.1.37476.3.0.2'] = $checksums;
83
        #$metadata['bootstrap'] = $main;
84
        $phar->setMetadata($metadata);
85
 
79 daniel-mar 86
        $phar = null; // save
87
 
88
        chmod($filename, fileperms($filename) | 0755);
89
 
90
        touch($filename, $max_mtime);
91
}
92
 
93
$plugins = glob(__DIR__.'/plugins/*');
94
foreach ($plugins as $plugin) {
95
        if (!is_dir($plugin)) continue;
96
        vnag_make_phar(basename($plugin));
97
}