Subversion Repositories vnag

Rev

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