home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / System / ProcWatch / Config.php next >
Encoding:
PHP Script  |  2008-07-02  |  5.8 KB  |  188 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: System :: ProcWatch :: Config                                |
  4. // +----------------------------------------------------------------------+
  5. // | This source file is subject to version 3.0 of the PHP license,       |
  6. // | that is available at http://www.php.net/license/3_0.txt              |
  7. // | If you did not receive a copy of the PHP license and are unable      |
  8. // | to obtain it through the world-wide-web, please send a note to       |
  9. // | license@php.net so we can mail you a copy immediately.               |
  10. // +----------------------------------------------------------------------+
  11. // | Copyright (c) 2003-2004 Michael Wallner <mike@iworks.at>             |
  12. // +----------------------------------------------------------------------+
  13. //
  14. // $Id: Config.php,v 1.4 2004/03/14 14:31:37 mike Exp $
  15.  
  16. /**
  17. * Requires PEAR.
  18. */
  19. require_once 'PEAR.php';
  20.  
  21. /** 
  22. * System_ProcWatch_Config
  23. * Build a configuration array for System_ProcWatch
  24. * Usage:
  25. * <code>
  26. * $cf = System_ProcWatch_Config::fromXmlFile('/etc/procwatch.xml');
  27. * $pw = &new System_ProcWatch($cf);
  28. * </code>
  29. * @author       Michael Wallner <mike@php.net>
  30. * @package      System_ProcWatch
  31. * @category     System
  32. * @version      $Revision: 1.4 $
  33. * @access       public
  34. */
  35. class System_ProcWatch_Config
  36. {
  37.     /**
  38.     * Get config array from XML file
  39.     *
  40.     * @throws   PEAR_Error
  41.     * @static
  42.     * @access   public
  43.     * @return   mixed
  44.     * @param    string  $file   path to XML file
  45.     */
  46.     function fromXmlFile($file)
  47.     {
  48.         if (!is_file($file)) {
  49.             return PEAR::raiseError("File '$file' doesn't exist");
  50.         }
  51.         
  52.         return System_ProcWatch_Config::fromXml(implode('', file($file)));
  53.     }
  54.  
  55.     /**
  56.     * Get config array from XML string
  57.     *
  58.     * @throws   PEAR_Error
  59.     * @static
  60.     * @access   public
  61.     * @return   mixed
  62.     * @param    string  $xml    XML string
  63.     */
  64.     function fromXml($xml)
  65.     {
  66.         include_once('System/ProcWatch/Config/Parser.php');
  67.         
  68.         $config = &new System_ProcWatch_Config_Parser();
  69.         $result = $config->getConf($xml);
  70.  
  71.         unset($config);
  72.  
  73.         return  $result;
  74.     }
  75.     
  76.     /**
  77.     * Get config array from INI file
  78.     *
  79.     * @throws   PEAR_Error
  80.     * @static
  81.     * @access   public
  82.     * @return   mixed
  83.     * @param    string  $file   path to INI file
  84.     */
  85.     function fromIniFile($file)
  86.     {
  87.         if (!is_file($file)) {
  88.             return PEAR::raiseError("File '$file' doesn't exist");
  89.         }
  90.         
  91.         $jobs   = parse_ini_file($file, true);
  92.         $result = array();
  93.         
  94.         foreach ($jobs as $job => $c) {
  95.  
  96.             $result[$job] = array();
  97.             
  98.             $result[$job]['pattern'][$c['match']] = $c['pattern'];
  99.             $result[$job]['condition'][$c['condition']] = array();
  100.             
  101.             // set a refernce for easier use
  102.             if ($c['condition'] == 'attr') {
  103.                 $result[$job]['condition']['attr'] = array();
  104.                 $reference = &$result[$job]['condition']['attr'][$c['attr']];
  105.             } elseif($c['condition'] == 'presence') {
  106.                 $reference = &$result[$job]['condition'][$c['condition']];
  107.             } else {
  108.                 continue;
  109.             }
  110.             
  111.             // (min|max|sum|is|isnot) conditions
  112.             if (isset($c['min'])) {
  113.                 $reference['min'] = $c['min'];
  114.             }
  115.             if (isset($c['max'])) {
  116.                 $reference['max'] = $c['max'];
  117.             }
  118.             if (isset($c['sum'])) {
  119.                 $reference['sum'] = $c['sum'];
  120.             }
  121.             if (isset($c['is'])) {
  122.                 $reference['is'] = $c['is'];
  123.             }
  124.             if (isset($c['isnot'])) {
  125.                 $reference['isnot'] = $c['isnot'];
  126.             }
  127.             
  128.             $result[$job]['execute'] = array('shell' => array($c['execute']));
  129.         }
  130.         unset($jobs);
  131.         
  132.         return $result;
  133.     }
  134.     
  135.     /**
  136.     * Get config array from an array :)
  137.     *
  138.     * This method in fact does a sanity check on the supplied config array
  139.     * and should only be used for testing purposes.
  140.     * 
  141.     * @throws   PEAR_Error
  142.     * @static
  143.     * @access   public
  144.     * @return   mixed
  145.     * @param    array   $array  config array to check
  146.     */
  147.     function fromArray($array)
  148.     {
  149.         foreach ($array as $job) {
  150.             if (
  151.                 !isset($job['pattern'])                         OR
  152.                 !isset($job['condition'])                       OR
  153.                 !isset($job['execute'])                         OR
  154.                 
  155.                 !is_array($job['pattern'])                      OR
  156.                 !is_array($job['condition'])                    OR
  157.                 !is_array($job['execute'])                      OR
  158.                 
  159.                 (   (
  160.                     !isset($job['execute']['shell'])            AND
  161.                     !isset($job['execute']['php'])
  162.                     )                                           OR
  163.                     (
  164.                     !is_array(@$job['execute']['shell'])        AND
  165.                     !is_array(@$job['execute']['php'])
  166.                     )
  167.                 )                                               OR
  168.                 (   (
  169.                     !isset($job['condition']['presence'])       AND
  170.                     !isset($job['condition']['attr'])
  171.                     )                                           OR
  172.                     (
  173.                     !is_array(@$job['condition']['presence'])   AND
  174.                     !is_array(@$job['condition']['attr'])
  175.                     )
  176.                 )
  177.             ) {
  178.                 return PEAR::raiseError('Invalid configuration array supplied');
  179.             }
  180.         }
  181.         return $array;
  182.     }
  183. }
  184. ?>