HTMLPurifier 4.4.0
/home/ezyang/Dev/htmlpurifier/library/HTMLPurifier/StringHashParser.php
Go to the documentation of this file.
00001 <?php
00002 
00028 class HTMLPurifier_StringHashParser
00029 {
00030 
00031     public $default = 'ID';
00032 
00036     public function parseFile($file) {
00037         if (!file_exists($file)) return false;
00038         $fh = fopen($file, 'r');
00039         if (!$fh) return false;
00040         $ret = $this->parseHandle($fh);
00041         fclose($fh);
00042         return $ret;
00043     }
00044 
00048     public function parseMultiFile($file) {
00049         if (!file_exists($file)) return false;
00050         $ret = array();
00051         $fh = fopen($file, 'r');
00052         if (!$fh) return false;
00053         while (!feof($fh)) {
00054             $ret[] = $this->parseHandle($fh);
00055         }
00056         fclose($fh);
00057         return $ret;
00058     }
00059 
00068     protected function parseHandle($fh) {
00069         $state   = false;
00070         $single  = false;
00071         $ret     = array();
00072         do {
00073             $line = fgets($fh);
00074             if ($line === false) break;
00075             $line = rtrim($line, "\n\r");
00076             if (!$state && $line === '') continue;
00077             if ($line === '----') break;
00078             if (strncmp('--#', $line, 3) === 0) {
00079                 // Comment
00080                 continue;
00081             } elseif (strncmp('--', $line, 2) === 0) {
00082                 // Multiline declaration
00083                 $state = trim($line, '- ');
00084                 if (!isset($ret[$state])) $ret[$state] = '';
00085                 continue;
00086             } elseif (!$state) {
00087                 $single = true;
00088                 if (strpos($line, ':') !== false) {
00089                     // Single-line declaration
00090                     list($state, $line) = explode(':', $line, 2);
00091                     $line = trim($line);
00092                 } else {
00093                     // Use default declaration
00094                     $state  = $this->default;
00095                 }
00096             }
00097             if ($single) {
00098                 $ret[$state] = $line;
00099                 $single = false;
00100                 $state  = false;
00101             } else {
00102                 $ret[$state] .= "$line\n";
00103             }
00104         } while (!feof($fh));
00105         return $ret;
00106     }
00107 
00108 }
00109 
00110 // vim: et sw=4 sts=4