| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | if (!defined('DEBUG_MODE')) { die(); } |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | class Hm_Gmail_Contact_XML { |
| 15: | private $collect = false; |
| 16: | private $all_fields = false; |
| 17: | private $misc_tag_name = false; |
| 18: | private $results = array(); |
| 19: | private $xml_parser = false; |
| 20: | private $current; |
| 21: | private $xml = false; |
| 22: | private $index = 0; |
| 23: | private $xml_support = false; |
| 24: | |
| 25: | public function __construct($xml) { |
| 26: | $this->xml = $xml; |
| 27: | if (!Hm_Functions::function_exists('xml_parser_create')) { |
| 28: | Hm_Debug::add('Gmail contacts enabled, but no PHP XML support found', 'warning'); |
| 29: | return; |
| 30: | } |
| 31: | $this->xml_support = true; |
| 32: | $this->xml_parser = xml_parser_create('UTF-8'); |
| 33: | xml_set_object($this->xml_parser, $this); |
| 34: | xml_set_element_handler($this->xml_parser, 'xml_start_element', 'xml_end_element'); |
| 35: | xml_set_character_data_handler($this->xml_parser, 'xml_character_data'); |
| 36: | } |
| 37: | public function parse() { |
| 38: | if ($this->xml_support) { |
| 39: | xml_parse($this->xml_parser, $this->xml); |
| 40: | } |
| 41: | return $this->results; |
| 42: | } |
| 43: | public function xml_start_element($parser, $tagname, $attrs) { |
| 44: | $this->all_fields = false; |
| 45: | $this->current = false; |
| 46: | if ($tagname == 'ENTRY') { |
| 47: | if (!array_key_exists($this->index, $this->results)) { |
| 48: | $this->results[$this->index] = array(); |
| 49: | } |
| 50: | } |
| 51: | if ($tagname == 'GD:EMAIL') { |
| 52: | $this->results[$this->index]['email_address'] = $attrs['ADDRESS']; |
| 53: | } |
| 54: | if ($tagname == 'GD:FULLNAME') { |
| 55: | $this->collect = true; |
| 56: | } |
| 57: | if ($tagname == 'GD:PHONENUMBER') { |
| 58: | if (array_key_exists('URI', $attrs)) { |
| 59: | $this->results[$this->index]['phone_number'] = mb_substr($attrs['URI'], 5); |
| 60: | } |
| 61: | } |
| 62: | else { |
| 63: | $this->all_fields = true; |
| 64: | $this->misc_tag_name = $tagname; |
| 65: | } |
| 66: | } |
| 67: | public function xml_end_element($parser, $tagname) { |
| 68: | if ($tagname == 'ENTRY') { |
| 69: | $this->index++; |
| 70: | } |
| 71: | $this->current = false; |
| 72: | $this->collect = false; |
| 73: | } |
| 74: | public function xml_character_data($parser, $data) { |
| 75: | if ($this->collect) { |
| 76: | if ($this->current) { |
| 77: | $this->results[$this->index]['display_name'] .= $data; |
| 78: | } |
| 79: | else { |
| 80: | $this->results[$this->index]['display_name'] = $data; |
| 81: | } |
| 82: | } |
| 83: | elseif ($this->all_fields && trim($data)) { |
| 84: | $this->results[$this->index]['all_fields'][$this->misc_tag_name] = $data; |
| 85: | } |
| 86: | $this->current = true; |
| 87: | } |
| 88: | } |
| 89: | |