| 1 |
<?php
|
| 2 |
// $Id: xbview_parser.inc,v 1.1.2.1 2007/05/02 13:43:05 profix898 Exp $
|
| 3 |
|
| 4 |
function _xbview_parse() {
|
| 5 |
if (!($bookmark_path = _xbview_xmlpath())) {
|
| 6 |
return FALSE;
|
| 7 |
}
|
| 8 |
$xbview_parser = new xbview_parser();
|
| 9 |
|
| 10 |
return $xbview_parser->parse($bookmark_path);
|
| 11 |
}
|
| 12 |
|
| 13 |
/**
|
| 14 |
* CLASS: xbview_parser
|
| 15 |
*/
|
| 16 |
class xbview_parser {
|
| 17 |
|
| 18 |
var $parser = NULL;
|
| 19 |
var $title = FALSE;
|
| 20 |
var $element = array();
|
| 21 |
var $stack = array();
|
| 22 |
var $count = 1;
|
| 23 |
|
| 24 |
function parse($xmlfile) {
|
| 25 |
$xmldata = file_get_contents($xmlfile);
|
| 26 |
if (!$xmldata) {
|
| 27 |
drupal_set_message(t('Unable to open bookmark file!'), 'error');
|
| 28 |
return FALSE;
|
| 29 |
}
|
| 30 |
|
| 31 |
$xmldata = preg_replace('@<title[^>]*>(.*?)</title>@i', '<title><![CDATA[\1]]></title>', $xmldata);
|
| 32 |
|
| 33 |
$this->parser = xml_parser_create('UTF-8');
|
| 34 |
xml_set_object($this->parser, $this);
|
| 35 |
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 1);
|
| 36 |
xml_set_element_handler($this->parser, 'xbel_tag_open', 'xbel_tag_close');
|
| 37 |
xml_set_character_data_handler($this->parser, 'xbel_tag_data');
|
| 38 |
|
| 39 |
if (!xml_parse($this->parser, $xmldata)) {
|
| 40 |
drupal_set_message('XML error: '.
|
| 41 |
xml_error_string(xml_get_error_code($this->parser)) .' at line '.
|
| 42 |
xml_get_current_line_number($this->parser)
|
| 43 |
);
|
| 44 |
return FALSE;
|
| 45 |
}
|
| 46 |
|
| 47 |
xml_parser_free($this->parser);
|
| 48 |
|
| 49 |
drupal_set_message(t('XBEL file successfully parsed.'));
|
| 50 |
return TRUE;
|
| 51 |
}
|
| 52 |
|
| 53 |
function xbel_tag_open($parser, $name, $attrs) {
|
| 54 |
switch ($name) {
|
| 55 |
case 'XBEL':
|
| 56 |
db_lock_table('xbookmark');
|
| 57 |
db_query("DELETE FROM {xbookmark} ");
|
| 58 |
db_unlock_tables();
|
| 59 |
$this->element = array('type' => XBVIEW_NONE, 'id' => 0);
|
| 60 |
break;
|
| 61 |
case 'TITLE':
|
| 62 |
$this->title = TRUE;
|
| 63 |
break;
|
| 64 |
case 'METADATA':
|
| 65 |
$this->element['icon'] = isset($attrs['ICON']) ? $attrs['ICON'] : '';
|
| 66 |
break;
|
| 67 |
case 'FOLDER':
|
| 68 |
array_push($this->stack, $this->element);
|
| 69 |
$this->element['type'] = XBVIEW_FOLDER;
|
| 70 |
$this->element['href'] = '';
|
| 71 |
$this->element['icon'] = '';
|
| 72 |
$this->element['parent'] = $this->element['id'];
|
| 73 |
//$this->element['id'] = $attrs['ID'] ? crc32($attrs['ID']) : $this->count++;
|
| 74 |
$this->element['id'] = $this->count++;
|
| 75 |
break;
|
| 76 |
case 'BOOKMARK':
|
| 77 |
array_push($this->stack, $this->element);
|
| 78 |
$this->element['type'] = XBVIEW_BOOKMARK;
|
| 79 |
$this->element['href'] = $attrs['HREF'];
|
| 80 |
$this->element['icon'] = '';
|
| 81 |
$this->element['parent'] = $this->element['id'];
|
| 82 |
//$this->element['id'] = $attrs['ID'] ? crc32($attrs['ID']) : $this->count++;
|
| 83 |
$this->element['id'] = $this->count++;
|
| 84 |
break;
|
| 85 |
}
|
| 86 |
}
|
| 87 |
|
| 88 |
function xbel_tag_data($parser, $data) {
|
| 89 |
if (trim($data) && $this->title) {
|
| 90 |
$this->element['title'] = htmlspecialchars($data);
|
| 91 |
}
|
| 92 |
}
|
| 93 |
|
| 94 |
function xbel_tag_close($parser, $name) {
|
| 95 |
switch ($name) {
|
| 96 |
case 'XBEL':
|
| 97 |
variable_set('xbview_title', $this->element['title']);
|
| 98 |
$this->element = array('type' => XBVIEW_NONE);
|
| 99 |
break;
|
| 100 |
case 'TITLE':
|
| 101 |
$this->title = FALSE;
|
| 102 |
break;
|
| 103 |
case 'FOLDER':
|
| 104 |
case 'BOOKMARK':
|
| 105 |
db_lock_table('xbookmark');
|
| 106 |
db_query("INSERT INTO {xbookmark} (id, title, href, icon, type, parent) VALUES ('%d', '%s', '%s', '%s', '%s', '%d')",
|
| 107 |
$this->element['id'], $this->element['title'], $this->element['href'], $this->element['icon'], $this->element['type'], $this->element['parent']);
|
| 108 |
db_unlock_tables();
|
| 109 |
$this->element = array_pop($this->stack);
|
| 110 |
break;
|
| 111 |
}
|
| 112 |
}
|
| 113 |
}
|