home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / procwatch-lint < prev    next >
Text File  |  2008-07-02  |  5KB  |  201 lines

  1. #!H:\release167\NEU\xampp\php\.\php.exe
  2. <?php
  3. // +----------------------------------------------------------------------+
  4. // | procwatch-lint                                                       |
  5. // +----------------------------------------------------------------------+
  6. // | This source file is subject to version 3.0 of the PHP license,       |
  7. // | that is available at http://www.php.net/license/3_0.txt              |
  8. // | If you did not receive a copy of the PHP license and are unable      |
  9. // | to obtain it through the world-wide-web, please send a note to       |
  10. // | license@php.net so we can mail you a copy immediately.               |
  11. // +----------------------------------------------------------------------+
  12. // | Copyright (c) 2003-2004 Michael Wallner <mike@iworks.at>             |
  13. // +----------------------------------------------------------------------+
  14. //
  15. // $Id: system-procwatch_lint.php,v 1.5 2004/03/21 17:21:41 mike Exp $
  16.  
  17. /**
  18. * Shell script for validation of System_ProcWatch' config files
  19. * @author       Michael Wallner <mike@php.net>
  20. * @link         http://pear.php.net/package/System_ProcWatch
  21. * @package      System_ProcWatch
  22. * @category     System
  23. * @version      $Revision: 1.5 $
  24. */
  25.  
  26. /**
  27. * Requires XML::DTD::XmlValidator
  28. */
  29. require_once('XML/DTD/XmlValidator.php');
  30.  
  31. /**
  32. * Requires Console::Getopt
  33. */
  34. require_once('Console/Getopt.php');
  35.  
  36. /**
  37. * Set error handling
  38. */
  39. PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, '_show_usage');
  40.  
  41. /**
  42. * Command line options
  43. */
  44. $options = array(
  45.     'c:'    => 'conf=',
  46.     'd:'    => 'dtd=',
  47.     'v'     => 'verbose',
  48.     'h'     => 'help'
  49. );
  50.  
  51. $arglist = Console_Getopt::readPHPArgv();
  52. $srcname = array_shift($arglist);
  53.  
  54. /**
  55. * Get command line arguments
  56. */
  57. $cl_args = _prepare_args(
  58.     array_shift(
  59.         Console_Getopt::getopt(
  60.             $arglist,                           // ARGV
  61.             implode('', array_keys($options)),  // short options
  62.             array_values($options)              // long options
  63.         )
  64.     )
  65. );
  66.  
  67. /**
  68. * Show usage if:
  69. *   o requested with -h or --help
  70. *   o -c or --conf was not specified
  71. */
  72. if (
  73.     isset($cl_args['h'])    ||
  74.     isset($cl_args['help']) ||
  75.     (!isset($cl_args['c'])  && !isset($cl_args['conf']))
  76. )
  77. {
  78.     _show_usage();
  79. }
  80.  
  81. /**
  82. * Get path to config file
  83. */
  84. $xmlfile = (isset($cl_args['c']) ? $cl_args['c'] : $cl_args['conf']);
  85.  
  86. /**
  87. * Show usage with error message if path to config file is invalid
  88. * PEAR::raiseError() will call _show_usage() and exit
  89. */
  90. if (!is_file($xmlfile)) {
  91.     PEAR::raiseError("File '$xmlfile' doesn't exist");
  92. }
  93.  
  94. /**
  95. * Get path to dtd file if set
  96. */
  97. $dtdfile =  (isset($cl_args['d'])       ? $cl_args['d'] :
  98.             (isset($cl_args['dtd'])     ? $cl_args['dtd'] : 
  99.             'C:\php5\pear\data/System_ProcWatch/procwatch-1_0.dtd'));
  100.  
  101. /**
  102. * Show usage with error message if path to dtd file is invalid
  103. * PEAR::raiseError() will call _show_usage() and exit
  104. */
  105. if (!is_file($dtdfile)) {
  106.     PEAR::raiseError("File '$dtdfile' doesn't exist");
  107. }
  108.  
  109.  
  110. /**
  111. * Init XML::DTD::XmlValidator
  112. */
  113. $xmlvalid = &new XML_DTD_XmlValidator;
  114.  
  115. /**
  116. * Validate the supplied XML configuration file
  117. */
  118. if ($xmlvalid->isValid($dtdfile, $xmlfile)) {
  119.  
  120.     $str =  "\nCONGRATS!\n\nConfiguration from $xmlfile seems " .
  121.             "valid according to $dtdfile\n\n";
  122.  
  123. } else {
  124.  
  125.     // echo errors to STDERR if (-v|--verbose) isset
  126.     if (isset($cl_args['v']) || isset($cl_args['verbose'])) {
  127.         fputs(STDERR, $xmlvalid->getMessage());
  128.     }
  129.  
  130.     $str =  "\nERRRRR...\n\nConfiguration from $xmlfile seems ".
  131.             "NOT valid according to $dtdfile\n\n";
  132.  
  133. }
  134.  
  135. echo wordwrap($str, 79);
  136.  
  137.  
  138. // ------------------ script end ------------------ //
  139.  
  140. /**
  141. * Prepare args array from Console_Getopt::getopt()
  142. * @return   array
  143. * @param    array
  144. */
  145. function _prepare_args($array)
  146. {
  147.     $args = array();
  148.     foreach ($array as $option) {
  149.         $key        = preg_replace('/^-+/', '', $option[0]);
  150.         $args[$key] = empty($option[1]) ?  1 : $option[1];
  151.     }
  152.     return $args;
  153. }
  154.  
  155. /**
  156. * Function which shows usage of this script
  157. * @return   void
  158. * @param    object PEAR_Error
  159. */
  160. function _show_usage($error = null)
  161. {
  162.     if (isset($error)) {
  163.  
  164.         fputs(STDERR, "\nError: " . $error->getMessage() . "\n\n");
  165.         exit(-1);
  166.  
  167.     } else {
  168.  
  169.         echo <<<_SHOW_USAGE
  170. #
  171. # procwatch-lint v0.4.2, 2004-06-12 by Michael Wallner <mike@php.net>
  172. # For further information visit http://pear.php.net/package/System_ProcWatch
  173. #
  174.  
  175. USAGE:
  176. \$ {$GLOBALS['srcname']} -c <file> [-d <dtd>] [-v]
  177.  
  178. OPTIONS:
  179.     -c | --conf=        path to XML configuration file
  180.     -d | --dtd=         path to DTD
  181.     
  182.     -v | --verbose      verbose output on errors
  183.  
  184.     -h | --help         this help message
  185.  
  186. EXAMPLE:
  187.     \$ {$GLOBALS['srcname']} -c /etc/procwatch.xml
  188.  
  189.     This command will validate the configuration file '/etc/procwatch.xml'
  190.     using the DTD at 'C:\php5\pear\data/System_ProcWatch/procwatch-1.0.dtd'
  191.  
  192. _SHOW_USAGE;
  193.     
  194.         exit(0);
  195.  
  196.     }
  197. }
  198. ?>