00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00032 class ilBookmarkImportExport
00033 {
00039 function _parseFile($file)
00040 {
00041 if (file_exists($file) && is_file($file))
00042 {
00043 $fp = fopen($file, "r-");
00044 while ($line=fgets($fp))
00045 {
00046 $line=trim($line);
00047 if (preg_match('/<!DOCTYPE NETSCAPE-Bookmark-file-1>/i', $line)) {
00048 return ilBookmarkImportExport::__parseNetscape($fp);
00049 }
00050 }
00051 }
00052 return false;
00053 }
00060 function __parseNetscape(&$fp)
00061 {
00062 $result=array();
00063 $parent=array();
00064 $start_id=0;
00065 $id=0;
00066 $ok=false;
00067 while ($line=fgets($fp))
00068 {
00069 $line=trim($line);
00070 if (preg_match('/<DL>/i',$line))
00071 {
00072 $parent_id=$id;
00073 }
00074 elseif (preg_match('/<\/DL>/i',$line))
00075 {
00076 $parent_id=array_pop($parent);
00077 }
00078 elseif (preg_match('/<DD>(.+)$/i',$line,$match))
00079 {
00080
00081 $desc=ilBookmarkImportExport::_convertCharset(trim($match[1]),$charset);
00082 $desc=ilBookmarkImportExport::_decodeEntities($desc);
00083 $result[$parent_id][$id]['description']=$desc;
00084 }
00085 elseif (preg_match('/<DT><H3[^>]*>(.*)<\/H3>/i',$line,$match))
00086 {
00087
00088 array_push($parent,$parent_id);
00089 $name=ilBookmarkImportExport::_convertCharset(trim($match[1]),$charset);
00090 $name=ilBookmarkImportExport::_decodeEntities($name);
00091 $id++;
00092 $result[$parent_id][$id]=array(
00093 'type'=>'bmf',
00094 'title'=>$name,
00095 );
00096 }
00097 elseif (preg_match('/<DT><A HREF="([^"]*)[^>]*>(.*)<\/A>/i', $line, $match))
00098 {
00099 $id++;
00100
00101 $url=ilBookmarkImportExport::_convertCharset(trim($match[1]),$charset);
00102 $url=ilBookmarkImportExport::_decodeEntities($url);
00103 $name=ilBookmarkImportExport::_convertCharset(trim($match[2]),$charset);
00104 $name=ilBookmarkImportExport::_decodeEntities($name);
00105
00106 if (preg_match("/ADD_DATE=\"([^\"]*)/i", $line, $match)) $add_date = $match[1]; else $add_date=0;
00107 if (preg_match("/LAST_VISIT=\"([^\"]*)/i", $line, $match)) $visited = $match[1]; else $visited=0;
00108 if (preg_match("/LAST_MODIFIED=\"([^\"]*)/i", $line, $match)) $modified = $match[1]; else $modified=0;
00109 $result[$parent_id][$id]=array(
00110 'type'=>'bm',
00111 'target'=>$url,
00112 'title'=>$name,
00113 'add_date'=>$add_date,
00114 'visited'=>$visited,
00115 'modified'=>$modified,
00116 );
00117 }
00118 elseif (preg_match("/<META\s+HTTP-EQUIV=\"Content-Type\".+CONTENT=\"([^\"]*)\"/i", $line, $match))
00119 {
00120 preg_match("/charset=([^ ]+)/", $match[1], $match);
00121 $charset=$match[1];
00122 }
00123 }
00124 return $result;
00125 }
00133 function _exportBookmark ($obj_ids,$recursive = true,$title = '')
00134 {
00135 $htmlCont='<!DOCTYPE NETSCAPE-Bookmark-file-1>'."\n";
00136 $htmlCont.='<!-- Created by ilias - www.ilias.de -->'."\n";
00137 $htmlCont.='<!-- on '.date('r').' -->'."\n\n";
00138 $htmlCont.='<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">'."\n";
00139 $htmlCont.='<TITLE>'.ilUtil::prepareFormOutput($title).'</TITLE>'."\n";
00140 $htmlCont.='<H1>'.ilUtil::prepareFormOutput($title).'</H1>'."\n\n";
00141 $htmlCont.='<DL><p>'."\n";
00142 foreach ($obj_ids as $obj_id) {
00143 $object=ilBookmarkFolder::getObject($obj_id);
00144 $htmlCont.=ilBookmarkImportExport::__parseExport($object,1,$recursive);
00145 }
00146 $htmlCont.='</DL><p>'."\n";
00147 return $htmlCont;
00148 }
00157 function __parseExport ($object,$depth=1,$recursive = true)
00158 {
00159 switch ($object['type'])
00160 {
00161 case 'bm':
00162 $result.=str_repeat("\t",$depth);
00163 $result.='<DT><A HREF="'.ilUtil::prepareFormOutput($object['target']).'" ';
00164 $result.='ADD_DATE="'.intval(0).'" ';
00165 $result.='LAST_VISIT="'.intval(0).'" ';
00166 $result.='LAST_MODIFIED="'.intval(0).'">';
00167 $result.=ilUtil::prepareFormOutput($object['title']).'</A>'."\n";
00168 if ($object['description']) $result.='<DD>'.
00169 ilUtil::prepareFormOutput($object['description'])."\n";
00170 break;
00171 case 'bmf':
00172 $result.=str_repeat("\t",$depth).'<DT><H3 ADD_DATE="0">'.
00173 ilUtil::prepareFormOutput($object['title']).'</H3>'."\n";
00174 if ($object['description']) $result.='<DD>'.
00175 ilUtil::prepareFormOutput($object['description'])."\n";
00176 $result.=str_repeat("\t",$depth).'<DL><p>'."\n";
00177 if ($recursive)
00178 {
00179 $depth++;
00180 $sub_objects=ilBookmarkFolder::getObjects($object['child']);
00181 foreach ($sub_objects as $sub_object)
00182 {
00183 $result.=ilBookmarkImportExport::__parseExport($sub_object,
00184 $depth,$recursive);
00185 }
00186 $depth--;
00187 }
00188 $result.=str_repeat("\t",$depth).'</DL><p>'."\n";
00189 break;
00190 }
00191 return $result;
00192 }
00199 function _decodeEntities($string)
00200 {
00201 if (function_exists('html_entity_decode'))
00202 {
00203 $string= html_entity_decode($string,ENT_QUOTES,"ISO-8859-15"); #NOTE: UTF-8 does not work!
00204 }
00205 else
00206 {
00207 $trans_table = array_flip(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES));
00208 $string = strtr($string, $trans_table );
00209 }
00210 $string= preg_replace('/&#(\d+);/me',"chr(\\1)",$string); #decimal notation
00211 $string= preg_replace('/&#x([a-f0-9]+);/mei',"chr(0x\\1)",$string); #hex notation
00212 return $string;
00213 }
00222 function _convertCharset($string,$from_charset='',$to_charset='UTF-8')
00223 {
00224 if (extension_loaded("mbstring"))
00225 {
00226 if (!$from_charset)
00227 {
00228
00229 mb_detect_order("ASCII, JIS, UTF-8, EUC-JP, SJIS, ISO-8859-15, Windows-1252");
00230 $from_charset=mb_detect_encoding ($string);
00231 }
00232 if (strtoupper($from_charset)!=$to_charset)
00233 {
00234 return @mb_convert_encoding($string,$to_charset,$from_charset);
00235 }
00236 }
00237 return $string;
00238 }
00239 }
00240 ?>