File indexing completed on 2024-12-22 05:36:22

0001 <?php
0002 
0003 /**
0004  * Parses string hash files. File format is as such:
0005  *
0006  *      DefaultKeyValue
0007  *      KEY: Value
0008  *      KEY2: Value2
0009  *      --MULTILINE-KEY--
0010  *      Multiline
0011  *      value.
0012  *
0013  * Which would output something similar to:
0014  *
0015  *      array(
0016  *          'ID' => 'DefaultKeyValue',
0017  *          'KEY' => 'Value',
0018  *          'KEY2' => 'Value2',
0019  *          'MULTILINE-KEY' => "Multiline\nvalue.\n",
0020  *      )
0021  *
0022  * We use this as an easy to use file-format for configuration schema
0023  * files, but the class itself is usage agnostic.
0024  *
0025  * You can use ---- to forcibly terminate parsing of a single string-hash;
0026  * this marker is used in multi string-hashes to delimit boundaries.
0027  */
0028 class HTMLPurifier_StringHashParser
0029 {
0030 
0031     /**
0032      * @type string
0033      */
0034     public $default = 'ID';
0035 
0036     /**
0037      * Parses a file that contains a single string-hash.
0038      * @param string $file
0039      * @return array
0040      */
0041     public function parseFile($file)
0042     {
0043         if (!file_exists($file)) {
0044             return false;
0045         }
0046         $fh = fopen($file, 'r');
0047         if (!$fh) {
0048             return false;
0049         }
0050         $ret = $this->parseHandle($fh);
0051         fclose($fh);
0052         return $ret;
0053     }
0054 
0055     /**
0056      * Parses a file that contains multiple string-hashes delimited by '----'
0057      * @param string $file
0058      * @return array
0059      */
0060     public function parseMultiFile($file)
0061     {
0062         if (!file_exists($file)) {
0063             return false;
0064         }
0065         $ret = array();
0066         $fh = fopen($file, 'r');
0067         if (!$fh) {
0068             return false;
0069         }
0070         while (!feof($fh)) {
0071             $ret[] = $this->parseHandle($fh);
0072         }
0073         fclose($fh);
0074         return $ret;
0075     }
0076 
0077     /**
0078      * Internal parser that acepts a file handle.
0079      * @note While it's possible to simulate in-memory parsing by using
0080      *       custom stream wrappers, if such a use-case arises we should
0081      *       factor out the file handle into its own class.
0082      * @param resource $fh File handle with pointer at start of valid string-hash
0083      *            block.
0084      * @return array
0085      */
0086     protected function parseHandle($fh)
0087     {
0088         $state   = false;
0089         $single  = false;
0090         $ret     = array();
0091         do {
0092             $line = fgets($fh);
0093             if ($line === false) {
0094                 break;
0095             }
0096             $line = rtrim($line, "\n\r");
0097             if (!$state && $line === '') {
0098                 continue;
0099             }
0100             if ($line === '----') {
0101                 break;
0102             }
0103             if (strncmp('--#', $line, 3) === 0) {
0104                 // Comment
0105                 continue;
0106             } elseif (strncmp('--', $line, 2) === 0) {
0107                 // Multiline declaration
0108                 $state = trim($line, '- ');
0109                 if (!isset($ret[$state])) {
0110                     $ret[$state] = '';
0111                 }
0112                 continue;
0113             } elseif (!$state) {
0114                 $single = true;
0115                 if (strpos($line, ':') !== false) {
0116                     // Single-line declaration
0117                     list($state, $line) = explode(':', $line, 2);
0118                     $line = trim($line);
0119                 } else {
0120                     // Use default declaration
0121                     $state  = $this->default;
0122                 }
0123             }
0124             if ($single) {
0125                 $ret[$state] = $line;
0126                 $single = false;
0127                 $state  = false;
0128             } else {
0129                 $ret[$state] .= "$line\n";
0130             }
0131         } while (!feof($fh));
0132         return $ret;
0133     }
0134 }
0135 
0136 // vim: et sw=4 sts=4