ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilBookmarkImportExport.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
32 {
38  function _parseFile($file)
39  {
40  if (file_exists($file) && is_file($file))
41  {
42  $fp = fopen($file, "r-");
43  while ($line=fgets($fp))
44  {
45  $line=trim($line);
46  if (preg_match('/<!DOCTYPE NETSCAPE-Bookmark-file-1>/i', $line)) {
48  }
49  }
50  }
51  return false;
52  }
59  function __parseNetscape(&$fp)
60  {
61  $result=array();
62  $parent=array();
63  $start_id=0;
64  $id=0;
65  $ok=false;
66  while ($line=fgets($fp))
67  {
68  $line=trim($line);
69  if (preg_match('/<DL>/i',$line))
70  {
71  $parent_id=$id;
72  }
73  elseif (preg_match('/<\/DL>/i',$line))
74  {
75  $parent_id=array_pop($parent);
76  }
77  elseif (preg_match('/<DD>(.+)$/i',$line,$match))
78  {
79  // extract description
80  $desc=ilBookmarkImportExport::_convertCharset(trim($match[1]),$charset);
82  $result[$parent_id][$id]['description']=$desc;
83  }
84  elseif (preg_match('/<DT><H3[^>]*>(.*)<\/H3>/i',$line,$match))
85  {
86  //bookmark folder
87  array_push($parent,$parent_id);
88  $name=ilBookmarkImportExport::_convertCharset(trim($match[1]),$charset);
90  $id++;
91  $result[$parent_id][$id]=array(
92  'type'=>'bmf',
93  'title'=>$name,
94  );
95  }
96  elseif (preg_match('/<DT><A HREF="([^"]*)[^>]*>(.*)<\/A>/i', $line, $match))
97  {
98  $id++;
99  // extract url and title
100  $url=ilBookmarkImportExport::_convertCharset(trim($match[1]),$charset);
102  $name=ilBookmarkImportExport::_convertCharset(trim($match[2]),$charset);
104  // extract dates
105  if (preg_match("/ADD_DATE=\"([^\"]*)/i", $line, $match)) $add_date = $match[1]; else $add_date=0;
106  if (preg_match("/LAST_VISIT=\"([^\"]*)/i", $line, $match)) $visited = $match[1]; else $visited=0;
107  if (preg_match("/LAST_MODIFIED=\"([^\"]*)/i", $line, $match)) $modified = $match[1]; else $modified=0;
108  $result[$parent_id][$id]=array(
109  'type'=>'bm',
110  'target'=>$url,
111  'title'=>$name,
112  'add_date'=>$add_date,
113  'visited'=>$visited,
114  'modified'=>$modified,
115  );
116  }
117  elseif (preg_match("/<META\s+HTTP-EQUIV=\"Content-Type\".+CONTENT=\"([^\"]*)\"/i", $line, $match))
118  {
119  preg_match("/charset=([^ ]+)/", $match[1], $match);
120  $charset=$match[1];
121  }
122  }
123  return $result;
124  }
132  function _exportBookmark ($obj_ids,$recursive = true,$title = '')
133  {
134  $htmlCont='<!DOCTYPE NETSCAPE-Bookmark-file-1>'."\n";
135  $htmlCont.='<!-- Created by ilias - www.ilias.de -->'."\n";
136  $htmlCont.='<!-- on '.date('r').' -->'."\n\n";
137  $htmlCont.='<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">'."\n";
138  $htmlCont.='<TITLE>'.ilUtil::prepareFormOutput($title).'</TITLE>'."\n";
139  $htmlCont.='<H1>'.ilUtil::prepareFormOutput($title).'</H1>'."\n\n";
140  $htmlCont.='<DL><p>'."\n";
141  foreach ($obj_ids as $obj_id) {
142  $object = ilBookmarkFolder::getObject($obj_id);
143  $htmlCont.=ilBookmarkImportExport::__parseExport($object,1,$recursive);
144  }
145  $htmlCont.='</DL><p>'."\n";
146 //echo htmlentities($htmlCont); exit;
147  return $htmlCont;
148  }
157  function __parseExport ($object,$depth=1,$recursive = true)
158  {
159  switch ($object['type'])
160  {
161  case 'bm':
162  $result.=str_repeat("\t",$depth);
163  $result.='<DT><A HREF="'.ilUtil::prepareFormOutput($object['target']).'" ';
164  $result.='ADD_DATE="'.intval(0).'" ';
165  $result.='LAST_VISIT="'.intval(0).'" ';
166  $result.='LAST_MODIFIED="'.intval(0).'">';
167  $result.=ilUtil::prepareFormOutput($object['title']).'</A>'."\n";
168  if ($object['description']) $result.='<DD>'.
169  ilUtil::prepareFormOutput($object['description'])."\n";
170  break;
171  case 'bmf':
172  $result.=str_repeat("\t",$depth).'<DT><H3 ADD_DATE="0">'.
173  ilUtil::prepareFormOutput($object['title']).'</H3>'."\n";
174  if ($object['description']) $result.='<DD>'.
175  ilUtil::prepareFormOutput($object['description'])."\n";
176  $result.=str_repeat("\t",$depth).'<DL><p>'."\n";
177  if ($recursive)
178  {
179  $depth++;
180  $sub_objects=ilBookmarkFolder::getObjects($object['child']);
181  foreach ($sub_objects as $sub_object)
182  {
183  $result.=ilBookmarkImportExport::__parseExport($sub_object,
184  $depth,$recursive);
185  }
186  $depth--;
187  }
188  $result.=str_repeat("\t",$depth).'</DL><p>'."\n";
189  break;
190  }
191  return $result;
192  }
199  function _decodeEntities($string)
200  {
201  if (function_exists('html_entity_decode'))
202  {
203  $string= html_entity_decode($string,ENT_QUOTES,"ISO-8859-15"); #NOTE: UTF-8 does not work!
204  }
205  else
206  {
207  $trans_table = array_flip(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES));
208  $string = strtr($string, $trans_table );
209  }
210  $string= preg_replace('/&#(\d+);/me',"chr(\\1)",$string); #decimal notation
211  $string= preg_replace('/&#x([a-f0-9]+);/mei',"chr(0x\\1)",$string); #hex notation
212  return $string;
213  }
222  function _convertCharset($string,$from_charset='',$to_charset='UTF-8')
223  {
224  if (extension_loaded("mbstring"))
225  {
226  if (!$from_charset)
227  {
228  // try to detect charset
229  mb_detect_order("ASCII, JIS, UTF-8, EUC-JP, SJIS, ISO-8859-15, Windows-1252");
230  $from_charset=mb_detect_encoding ($string);
231  }
232  if (strtoupper($from_charset)!=$to_charset)
233  {
234  return @mb_convert_encoding($string,$to_charset,$from_charset);
235  }
236  }
237  return $string;
238  }
239 }
240 ?>