ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilBookmarkImportExport.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
4
13{
19 function _parseFile($file)
20 {
21 if (file_exists($file) && is_file($file))
22 {
23 $fp = fopen($file, "r-");
24 while ($line=fgets($fp))
25 {
26 $line=trim($line);
27 if (preg_match('/<!DOCTYPE NETSCAPE-Bookmark-file-1>/i', $line)) {
29 }
30 }
31 }
32 return false;
33 }
40 function __parseNetscape(&$fp)
41 {
42 $result=array();
43 $parent=array();
44 $start_id=0;
45 $id=0;
46 $ok=false;
47 while ($line=fgets($fp))
48 {
49 $line=trim($line);
50 if (preg_match('/<DL>/i',$line))
51 {
52 $parent_id=$id;
53 }
54 elseif (preg_match('/<\/DL>/i',$line))
55 {
56 $parent_id=array_pop($parent);
57 }
58 elseif (preg_match('/<DD>(.+)$/i',$line,$match))
59 {
60 // extract description
61 $desc=ilBookmarkImportExport::_convertCharset(trim($match[1]),$charset);
63 $result[$parent_id][$id]['description']=strip_tags($desc);
64 }
65 elseif (preg_match('/<DT><H3[^>]*>(.*)<\/H3>/i',$line,$match))
66 {
67 //bookmark folder
68 array_push($parent,$parent_id);
69 $name=ilBookmarkImportExport::_convertCharset(trim($match[1]),$charset);
71 $id++;
72 $result[$parent_id][$id]=array(
73 'type'=>'bmf',
74 'title'=>strip_tags($name),
75 );
76 }
77 elseif (preg_match('/<DT><A HREF="([^"]*)[^>]*>(.*)<\/A>/i', $line, $match))
78 {
79 $id++;
80 // extract url and title
81 $url=ilBookmarkImportExport::_convertCharset(trim($match[1]),$charset);
83 $name=ilBookmarkImportExport::_convertCharset(trim($match[2]),$charset);
85 // extract dates
86 if (preg_match("/ADD_DATE=\"([^\"]*)/i", $line, $match)) $add_date = $match[1]; else $add_date=0;
87 if (preg_match("/LAST_VISIT=\"([^\"]*)/i", $line, $match)) $visited = $match[1]; else $visited=0;
88 if (preg_match("/LAST_MODIFIED=\"([^\"]*)/i", $line, $match)) $modified = $match[1]; else $modified=0;
89 $result[$parent_id][$id]=array(
90 'type'=>'bm',
91 'target'=>strip_tags($url),
92 'title'=>strip_tags($name),
93 'add_date'=>$add_date,
94 'visited'=>$visited,
95 'modified'=>$modified,
96 );
97 }
98 elseif (preg_match("/<META\s+HTTP-EQUIV=\"Content-Type\".+CONTENT=\"([^\"]*)\"/i", $line, $match))
99 {
100 preg_match("/charset=([^ ]+)/", $match[1], $match);
101 $charset=$match[1];
102 }
103 }
104 return $result;
105 }
113 function _exportBookmark ($obj_ids,$recursive = true,$title = '')
114 {
115 $htmlCont='<!DOCTYPE NETSCAPE-Bookmark-file-1>'."\n";
116 $htmlCont.='<!-- Created by ilias - www.ilias.de -->'."\n";
117 $htmlCont.='<!-- on '.date('r').' -->'."\n\n";
118 $htmlCont.='<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">'."\n";
119 $htmlCont.='<TITLE>'.ilUtil::prepareFormOutput($title).'</TITLE>'."\n";
120 $htmlCont.='<H1>'.ilUtil::prepareFormOutput($title).'</H1>'."\n\n";
121 $htmlCont.='<DL><p>'."\n";
122 foreach ($obj_ids as $obj_id) {
123 $object = ilBookmarkFolder::getObject($obj_id);
124 $htmlCont.=ilBookmarkImportExport::__parseExport($object,1,$recursive);
125 }
126 $htmlCont.='</DL><p>'."\n";
127//echo htmlentities($htmlCont); exit;
128 return $htmlCont;
129 }
138 function __parseExport ($object,$depth=1,$recursive = true)
139 {
140 switch ($object['type'])
141 {
142 case 'bm':
143 $result.=str_repeat("\t",$depth);
144 $result.='<DT><A HREF="'.ilUtil::prepareFormOutput($object['target']).'" ';
145 $result.='ADD_DATE="'.intval(0).'" ';
146 $result.='LAST_VISIT="'.intval(0).'" ';
147 $result.='LAST_MODIFIED="'.intval(0).'">';
148 $result.=ilUtil::prepareFormOutput($object['title']).'</A>'."\n";
149 if ($object['description']) $result.='<DD>'.
150 ilUtil::prepareFormOutput($object['description'])."\n";
151 break;
152 case 'bmf':
153 $result.=str_repeat("\t",$depth).'<DT><H3 ADD_DATE="0">'.
154 ilUtil::prepareFormOutput($object['title']).'</H3>'."\n";
155 if ($object['description']) $result.='<DD>'.
156 ilUtil::prepareFormOutput($object['description'])."\n";
157 $result.=str_repeat("\t",$depth).'<DL><p>'."\n";
158 if ($recursive)
159 {
160 $depth++;
161 $sub_objects=ilBookmarkFolder::getObjects($object['child']);
162 foreach ($sub_objects as $sub_object)
163 {
165 $depth,$recursive);
166 }
167 $depth--;
168 }
169 $result.=str_repeat("\t",$depth).'</DL><p>'."\n";
170 break;
171 }
172 return $result;
173 }
180 function _decodeEntities($string)
181 {
182 if (function_exists('html_entity_decode'))
183 {
184 $string= html_entity_decode($string,ENT_QUOTES,"ISO-8859-15"); #NOTE: UTF-8 does not work!
185 }
186 else
187 {
188 $trans_table = array_flip(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES));
189 $string = strtr($string, $trans_table );
190 }
191 $string= preg_replace_callback(
192 '/&#(\d+);/m',
193 function($hit){
194 return chr($hit[1]);
195 },
196 $string
197 ); #decimal notation
198 $string= preg_replace_callback(
199 '/&#x([a-f0-9]+);/mi',
200 function($hit) {
201 return chr(hexdec($hit[1]));
202 },
203 $string
204 ); #hex notation
205 return $string;
206 }
215 function _convertCharset($string,$from_charset='',$to_charset='UTF-8')
216 {
217 if (extension_loaded("mbstring"))
218 {
219 if (!$from_charset)
220 {
221 // try to detect charset
222 mb_detect_order("ASCII, JIS, UTF-8, EUC-JP, SJIS, ISO-8859-15, Windows-1252");
223 $from_charset=mb_detect_encoding ($string);
224 }
225 if (strtoupper($from_charset)!=$to_charset)
226 {
227 return @mb_convert_encoding($string,$to_charset,$from_charset);
228 }
229 }
230 return $string;
231 }
232}
233?>
$result
print $file
_decodeEntities($string)
decode html entities of given string
__parseNetscape(&$fp)
parse Netscape bookmark file
_parseFile($file)
parse Bookmark file static method returns 3 dimensional array of bookmarks and folders
_exportBookmark($obj_ids, $recursive=true, $title='')
export bookmarks static method return html string
_convertCharset($string, $from_charset='', $to_charset='UTF-8')
converts charset of given string
__parseExport($object, $depth=1, $recursive=true)
recursive methode generates bookmark output for export
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms @access public
$url
Definition: shib_logout.php:72