Subversion Repositories vnag

Rev

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

  1. #!/usr/bin/php
  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) {
  13.         $filename = __DIR__.'/../bin/'.$plugin.'.phar';
  14.  
  15.         copy(__DIR__.'/plugins/'.$plugin.'/icinga2.conf', __DIR__.'/../bin/'.$plugin.'.conf');
  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;
  42.         $algo = 'sha256';
  43.         $checksums = $algo.'||';
  44.         ksort($files_for_phar);
  45.         foreach ($files_for_phar as $input_file_short => $input_file) {
  46.                 $max_mtime = max($max_mtime, filemtime($input_file));
  47.                 $checksums .= $input_file_short.'|'.hash_file($algo,$input_file).'||';
  48.         }
  49.  
  50.         if (file_exists($filename)) {
  51.                 $phar = new Phar($filename);
  52.                 $metadata = $phar->getMetadata();
  53.                 if (($metadata['1.3.6.1.4.1.37476.3.0.2']??'') == $checksums) {
  54.                         echo "Plugin $filename already up-to-date\n";
  55.                         $phar = null;
  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.  
  70.         $shebang = '#!/usr/bin/env php';
  71.         $phar->setStub(($shebang ? $shebang . PHP_EOL : "") . $phar->createDefaultStub($main));
  72.  
  73.         #$private = openssl_get_privatekey(file_get_contents(__DIR__.'/private.pem'));
  74.         #$pkey = '';
  75.         #openssl_pkey_export($private, $pkey);
  76.         #$phar->setSignatureAlgorithm(Phar::OPENSSL, $pkey);
  77.         #copy(__DIR__.'/public.pem', $filename.'.pubkey');
  78.         $phar->setSignatureAlgorithm(Phar::SHA512);
  79.  
  80.         $metadata = [];
  81.         $metadata['1.3.6.1.4.1.37476.3.0.2'] = $checksums;
  82.         #$metadata['bootstrap'] = $main;
  83.         $phar->setMetadata($metadata);
  84.  
  85.         $phar = null; // save
  86.  
  87.         chmod($filename, fileperms($filename) | 0755);
  88.  
  89.         touch($filename, $max_mtime);
  90. }
  91.  
  92. $plugins = glob(__DIR__.'/plugins/*');
  93. foreach ($plugins as $plugin) {
  94.         if (!is_dir($plugin)) continue;
  95.         vnag_make_phar(basename($plugin));
  96. }
  97.