|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00014 class mnet_encxml_parser { 00020 function mnet_encxml_parser() { 00021 return $this->initialise(); 00022 } 00023 00029 function initialise() { 00030 $this->parser = xml_parser_create(); 00031 xml_set_object($this->parser, $this); 00032 00033 xml_set_element_handler($this->parser, "start_element", "end_element"); 00034 xml_set_character_data_handler($this->parser, "discard_data"); 00035 00036 $this->tag_number = 0; // Just a unique ID for each tag 00037 $this->digest = ''; 00038 $this->remote_timestamp = ''; 00039 $this->remote_wwwroot = ''; 00040 $this->signature = ''; 00041 $this->data_object = ''; 00042 $this->key_URI = ''; 00043 $this->payload_encrypted = false; 00044 $this->cipher = array(); 00045 $this->error = array(); 00046 $this->remoteerror = null; 00047 $this->errorstarted = false; 00048 return true; 00049 } 00050 00089 function parse($data) { 00090 $p = xml_parse($this->parser, $data); 00091 00092 if ($p == 0) { 00093 // Parse failed 00094 $errcode = xml_get_error_code($this->parser); 00095 $errstring = xml_error_string($errcode); 00096 $lineno = xml_get_current_line_number($this->parser); 00097 if ($lineno !== false) { 00098 $error = array('lineno' => $lineno); 00099 $lineno--; // Line numbering starts at 1. 00100 while ($lineno > 0) { 00101 $data = strstr($data, "\n"); 00102 $lineno--; 00103 } 00104 $data .= "\n"; // In case there's only one line (no newline) 00105 $line = substr($data, 0, strpos($data, "\n")); 00106 $error['code'] = $errcode; 00107 $error['string'] = $errstring; 00108 $error['line'] = $line; 00109 $this->error[] = $error; 00110 } else { 00111 $this->error[] = array('code' => $errcode, 'string' => $errstring); 00112 } 00113 } 00114 00115 if (!empty($this->remoteerror)) { 00116 return false; 00117 } 00118 00119 if (count($this->cipher) > 0) { 00120 $this->cipher = array_values($this->cipher); 00121 $this->payload_encrypted = true; 00122 } 00123 00124 return (bool)$p; 00125 } 00126 00130 function free_resource() { 00131 $free = xml_parser_free($this->parser); 00132 } 00133 00146 function start_element($parser, $name, $attrs) { 00147 $this->tag_number++; 00148 $handler = 'discard_data'; 00149 switch(strtoupper($name)) { 00150 case 'DIGESTVALUE': 00151 $handler = 'parse_digest'; 00152 break; 00153 case 'SIGNATUREVALUE': 00154 $handler = 'parse_signature'; 00155 break; 00156 case 'OBJECT': 00157 $handler = 'parse_object'; 00158 break; 00159 case 'RETRIEVALMETHOD': 00160 $this->key_URI = $attrs['URI']; 00161 break; 00162 case 'TIMESTAMP': 00163 $handler = 'parse_timestamp'; 00164 break; 00165 case 'WWWROOT': 00166 $handler = 'parse_wwwroot'; 00167 break; 00168 case 'CIPHERVALUE': 00169 $this->cipher[$this->tag_number] = ''; 00170 $handler = 'parse_cipher'; 00171 break; 00172 case 'FAULT': 00173 $handler = 'parse_fault'; 00174 default: 00175 break; 00176 } 00177 xml_set_character_data_handler($this->parser, $handler); 00178 return true; 00179 } 00180 00188 function parse_timestamp($parser, $data) { 00189 $this->remote_timestamp .= $data; 00190 return true; 00191 } 00192 00209 function parse_cipher($parser, $data) { 00210 $this->cipher[$this->tag_number] .= $data; 00211 return true; 00212 } 00213 00221 function parse_wwwroot($parser, $data) { 00222 $this->remote_wwwroot .= $data; 00223 return true; 00224 } 00225 00233 function parse_digest($parser, $data) { 00234 $this->digest .= $data; 00235 return true; 00236 } 00237 00245 function parse_signature($parser, $data) { 00246 $this->signature .= $data; 00247 return true; 00248 } 00249 00257 function parse_object($parser, $data) { 00258 $this->data_object .= $data; 00259 return true; 00260 } 00261 00271 function discard_data($parser, $data) { 00272 if (!$this->errorstarted) { 00273 // Not interested 00274 return true; 00275 } 00276 $data = trim($data); 00277 if (isset($this->errorstarted->faultstringstarted) && !empty($data)) { 00278 $this->remoteerror .= ', message: ' . $data; 00279 } else if (isset($this->errorstarted->faultcodestarted)) { 00280 $this->remoteerror = 'code: ' . $data; 00281 unset($this->errorstarted->faultcodestarted); 00282 } else if ($data == 'faultCode') { 00283 $this->errorstarted->faultcodestarted = true; 00284 } else if ($data == 'faultString') { 00285 $this->errorstarted->faultstringstarted = true; 00286 } 00287 return true; 00288 00289 } 00290 00291 function parse_fault($parser, $data) { 00292 $this->errorstarted = new StdClass; 00293 return true; 00294 } 00295 00303 function end_element($parser, $name) { 00304 $ok = xml_set_character_data_handler($this->parser, "discard_data"); 00305 return true; 00306 } 00307 }