Subversion Repositories vnag

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

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