| [ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 <?php 2 /**************************************************************************\ 3 * eGroupWare - Bookmarks * 4 * http://www.egroupware.org * 5 * Based on Bookmarker Copyright (C) 1998 Padraic Renaghan * 6 * http://www.renaghan.com/bookmarker * 7 * Ported to phpgroupware by Joseph Engo * 8 * Ported to three-layered design by Michael Totschnig * 9 * -------------------------------------------- * 10 * This program is free software; you can redistribute it and/or modify it * 11 * under the terms of the GNU General Public License as published by the * 12 * Free Software Foundation; either version 2 of the License, or (at your * 13 * option) any later version. * 14 \**************************************************************************/ 15 16 /* $Id: class.bo.inc.php 20014 2005-11-27 01:17:28Z milosch $ */ 17 18 class bo 19 { 20 var $so; 21 var $grants; 22 var $url_format_check; 23 var $validate; 24 var $categories; 25 //following two are used by the export function 26 var $type; 27 var $expanded; 28 var $error_msg; 29 var $msg; 30 31 function bo() 32 { 33 $this->so =& CreateObject('bookmarks.so'); 34 $this->grants = $GLOBALS['egw']->acl->get_grants('bookmarks'); 35 $this->categories =& CreateObject('phpgwapi.categories','','bookmarks'); 36 $GLOBALS['egw']->config =& CreateObject('phpgwapi.config'); 37 $GLOBALS['egw']->config->read_repository(); 38 $this->config = $GLOBALS['egw']->config->config_data; 39 $this->url_format_check = True; 40 $this->validate =& CreateObject('phpgwapi.validator'); 41 42 $this->translation = &$GLOBALS['egw']->translation; 43 $this->charset = $this->translation->charset(); 44 } 45 46 function grab_form_values($returnto,$returnto2,$bookmark) 47 { 48 $location_info = array( 49 'returnto' => $returnto, 50 'returnto2' => $returnto2, 51 'bookmark' => array( 52 'url' => $bookmark['url'], 53 'name' => $bookmark['name'], 54 'desc' => $bookmark['desc'], 55 'keywords' => $bookmark['keywords'], 56 'category' => $bookmark['category'], 57 'rating' => $bookmark['rating'], 58 'access' => $bookmark['access'] 59 ) 60 ); 61 $this->save_session_data($location_info); 62 } 63 64 function date_information(&$tpl, $raw_string) 65 { 66 $ts = explode(',',$raw_string); 67 68 $tpl->set_var('added_value',$GLOBALS['egw']->common->show_date($ts[0])); 69 $tpl->set_var('visited_value',($ts[1]?$GLOBALS['egw']->common->show_date($ts[1]):lang('Never'))); 70 $tpl->set_var('updated_value',($ts[2]?$GLOBALS['egw']->common->show_date($ts[2]):lang('Never'))); 71 } 72 73 function _list($cat_id,$start=False,$where_clause=False,$subcatsalso=True) 74 { 75 $cat_list = $cat_id ? 76 ($subcatsalso ? $this->getcatnested($cat_id,True,True) : array($cat_id)): 77 False; 78 return $this->so->_list($cat_list,$this->get_user_grant_list(),$start,$where_clause); 79 } 80 81 function read($id,$do_htmlspecialchars=True) 82 { 83 $bookmark = $this->so->read($id,$do_htmlspecialchars); 84 foreach(array(EGW_ACL_READ,EGW_ACL_EDIT,EGW_ACL_DELETE) as $required) 85 { 86 $bookmark[$required] = $this->check_perms2($bookmark['owner'],$bookmark['access'],$required); 87 } 88 return $bookmark; 89 } 90 91 function get_user_grant_list() 92 { 93 if (is_array($this->grants)) 94 { 95 reset($this->grants); 96 while (list($user) = each($this->grants)) 97 { 98 $public_user_list[] = $user; 99 } 100 return $public_user_list; 101 } 102 else 103 { 104 return False; 105 } 106 } 107 108 function check_perms2($owner,$access,$required) 109 { 110 return ($owner == $GLOBALS['egw_info']['user']['account_id']) || 111 ($access == 'public' && ($this->grants[$owner] & $required)); 112 } 113 114 function check_perms($id, $required) 115 { 116 if (!($bookmark = $this->so->read($id))) 117 { 118 return False; 119 } 120 else 121 { 122 return $this->check_perms2($bookmark['owner'],$bookmark['access'],$required); 123 } 124 } 125 126 function categories_list($selected_id,$multiple=False) 127 { 128 $option_list = $this->getcatnested(0); 129 $s = ''; 130 foreach($option_list as $option) 131 { 132 $s .= '<option value="' . $option['value'] . '"' . 133 (($option['value']==$selected_id) ? ' selected' : '') . 134 '>' . $option['display'] . '</option>' . "\n"; 135 } 136 return '<select name="bookmark[category]' . 137 ($multiple ? '[]" multiple="multiple" ' : '" ') . 138 'size="5">' . $s . '</select>'; 139 } 140 141 function getcatnested($cat_id,$idsonly=False,$parentalso=False) 142 { 143 $retval = $parentalso ? array($cat_id) : array(); 144 $root_list = $this->categories->return_array($cat_id ? 'all' : 'mains',0,False,'','cat_name','',True,$cat_id,-1,$idsonly ? 'id' : ''); 145 146 if (is_array($root_list)) 147 { 148 foreach($root_list as $cat) 149 { 150 $padding = str_pad('',12*$cat['level'],' '); 151 $retval[] = $idsonly ? $cat['id'] : array('value'=>$cat['id'], 'display'=>$padding.$cat['name']); 152 $sublist = $this->getcatnested($cat['id'],$idsonly); 153 if (is_array($sublist) && count($sublist)>0) 154 { 155 $retval = array_merge($retval,$sublist); 156 } 157 } 158 } 159 return $retval; 160 } 161 162 function add($values) 163 { 164 if ($this->validate($values)) 165 { 166 if ($this->so->exists($values['url'])) 167 { 168 $this->error_msg .= sprintf('<br>URL <B>%s</B> already exists!', $values['url']); 169 return False; 170 } 171 $bm_id = $this->so->add($values); 172 if ($bm_id) 173 { 174 $this->msg .= lang('Bookmark created successfully.'); 175 return $bm_id; 176 } 177 } 178 else 179 { 180 return false; 181 } 182 } 183 184 function update($id, $values) 185 { 186 #echo "bo::update<pre>".htmlspecialchars(print_r($values,True))."</pre>\n"; 187 if ($this->validate($values) && $this->check_perms($id,EGW_ACL_EDIT)) 188 { 189 if ($this->so->update($id,$values)) 190 { 191 $this->msg .= lang('Bookmark changed sucessfully'); 192 return True; 193 } 194 } 195 else 196 { 197 return false; 198 } 199 } 200 201 function updatetimestamp($id,$timestamp) 202 { 203 $this->so->updatetimestamp($id,$timestamp); 204 } 205 206 function delete($id) 207 { 208 if ($this->check_perms($id,EGW_ACL_DELETE)) 209 { 210 if ($this->so->delete($id)) 211 { 212 $this->msg .= lang('bookmark deleted successfully.'); 213 return True; 214 } 215 } 216 else 217 { 218 return false; 219 } 220 } 221 222 function validate($values) 223 { 224 $result = True; 225 if (! $values['name']) 226 { 227 $this->error_msg .= '<br>' . lang('Name is required'); 228 $result = False; 229 } 230 231 if (! $values['category']) 232 { 233 $this->error_msg .= '<br>' . lang('You must select a category'); 234 $result = False; 235 } 236 237 if (! $values['url'] || $values['url'] == 'http://') 238 { 239 $this->error_msg .= '<br>' . lang('URL is required.'); 240 $result = False; 241 } 242 // does the admin want us to check URL format 243 elseif ($this->url_format_check) 244 { 245 if (! $this->validate->is_url($values['url'])) 246 { 247 $this->error_msg = '<br>URL invalid. Format must be <strong>http://</strong> or 248 <strong>ftp://</strong> followed by a valid hostname and 249 URL!<br><small>' . $this->validate->ERROR . '</small>'; 250 $result = False; 251 } 252 } 253 return $result; 254 } 255 256 function save_session_data($data) 257 { 258 $GLOBALS['egw']->session->appsession('session_data','bookmarks',$data); 259 } 260 261 function read_session_data() 262 { 263 return $GLOBALS['egw']->session->appsession('session_data','bookmarks'); 264 } 265 266 function get_category($catname,$parent) 267 { 268 $this->_debug('<br>Testing for category: ' . $catname . ' with parent: \'' . $parent . '\''); 269 270 $catid = $this->categories->exists($parent?'subs':'mains',$catname,0,$parent); 271 if ($catid) 272 { 273 $this->_debug(' - ' . $catname . ' already exists - id: ' . $catid); 274 } 275 else 276 { 277 $catid = $this->categories->add(array( 278 'name' => $catname, 279 'descr' => '', 280 'parent' => $parent, 281 'access' => '', 282 'data' => '' 283 )); 284 $this->_debug(' - ' . $Catname . ' does not exist - new id: ' . $catid); 285 } 286 return $catid; 287 } 288 289 function import($bkfile,$parent) 290 { 291 $this->_debug('<p><b>DEBUG OUTPUT:</b>'); 292 $this->_debug('<br>file_name: ' . $bkfile['name']); 293 $this->_debug('<br>file_size: ' . $bkfile['size']); 294 $this->_debug('<br>file_type: ' . $bkfile['type'] . '<p><b>URLs:</b>'); 295 $this->_debug('<table border="1" width="100%">'); 296 $this->_debug('<tr><td>cat id</td> <td>sub id</td> <td>name</td> <td>url</td> <td>add date</td> <td>change date</td> <td>vist date</td></tr>'); 297 298 if (!$bkfile['name'] || $bkfile['name'] == 'none' || @$bkfile['error']) 299 { 300 $this->error_msg .= '<br>'.lang('Netscape bookmark filename is required!'); 301 } 302 elseif (!$parent) 303 { 304 $this->error_msg .= '<br>'.lang('You need to select a category!'); 305 } 306 else 307 { 308 $fd = @fopen($bkfile['tmp_name'],'r'); 309 if ($fd) 310 { 311 $default_rating = 0; 312 $inserts = 0; 313 $folderstack = array($parent); 314 315 $utf8flag = False; 316 317 //In the bookmark import file, description for site is allowd to be empty 318 //The description of folders in the bookmark will be skiped. 319 $have_desc = True; 320 $dir_desc = False; 321 322 while ($line = @fgets($fd, 2048)) 323 { 324 $from_charset = False; 325 if (preg_match('/<META HTTP-EQUIV="Content-Type" CONTENT="text\\/html; charset=([^"]*)/',$line,$matches)) 326 { 327 $from_charset = $matches[1]; 328 } 329 // URLs are recognized by A HREF tags in the NS file. 330 elseif (eregi('<A HREF="([^"]*)[^>]*>(.*)</A>', $line, $match)) 331 { 332 if(!$have_desc) 333 { 334 unset($values['desc']); 335 if ($this->add($values)) 336 { 337 $inserts++; 338 } 339 } 340 $have_desc = False; 341 $dir_desc = False; 342 343 $url_parts = @parse_url($match[1]); 344 if 345 ( 346 $url_parts[scheme] == 'http' || $url_parts[scheme] == 'https' || 347 $url_parts[scheme] == 'ftp' || $url_parts[scheme] == 'news' 348 ) 349 { 350 $values['category'] = end($folderstack); 351 $values['url'] = $match[1]; 352 353 $values['name'] = str_replace(array('&','<','>','"e;','''), 354 array('&','<','>','"',"'"),$this->translation->convert($match[2],$from_charset)); 355 $values['rating'] = $default_rating; 356 357 eregi('ADD_DATE="([^"]*)"',$line,$add_info); 358 eregi('LAST_VISIT="([^"]*)"',$line,$vist_info); 359 eregi('LAST_MODIFIED="([^"]*)"',$line,$change_info); 360 361 $values['timestamps'] = sprintf('%s,%s,%s',$add_info[1],$vist_info[1],$change_info[1]); 362 363 unset($keywords); 364 eregi('SHORTCUTURL="([^"]*)"',$line,$keywords); 365 $values['keywords']=$keywords[1]; 366 367 $this->_debug(sprintf("<tr><td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> </tr>",$cid,$scid,$match[2],$match[1],$add_info[1],$change_info[1],$vist_info[1])); 368 } 369 } 370 // folders start with the folder name inside an <H3> tag, 371 // and end with the close </DL> tag. 372 // we use a stack to keep track of where we are in the 373 // folder hierarchy. 374 elseif (eregi('<H3[^>]*>(.*)</H3>', $line, $match)) 375 { 376 $folder_name = $this->translation->convert($match[1],$from_charset); 377 $current_cat_id = $this->get_category($folder_name,end($folderstack)); 378 $dir_desc = True; 379 array_push($folderstack,$current_cat_id); 380 } 381 // description start with tag <DD> and the description for folder 382 // will be skiped 383 elseif (eregi('<DD>(.*)',$line,$desc)) 384 { 385 if($dir_desc) 386 { 387 continue; 388 } 389 else 390 { 391 $values['desc'] = str_replace(array('&','<','>','"','''),array('&','<','>','"',"'"),$this->translation->convert($desc[1],$from_charset)); 392 if ($this->add($values)) 393 { 394 $inserts++; 395 } 396 $have_desc = True; 397 } 398 } 399 elseif (eregi('</DL>', $line)) 400 { 401 array_pop($folderstack); 402 } 403 } 404 405 if(!$have_desc) 406 { 407 unset($values['desc']); 408 if ($this->add($values)) 409 { 410 $inserts++; 411 } 412 } 413 414 @fclose($fd); 415 $this->_debug('</table>'); 416 $this->msg = '<br>'.lang("%1 bookmarks imported from %2 successfully.", $inserts, $bkfile['name']); 417 } 418 else 419 { 420 $this->error_msg .= '<br>'.lang('Unable to open temp file %1 for import.',$bkfile['name']); 421 } 422 } 423 } 424 425 function export($catlist,$type,$expanded=array()) 426 { 427 $this->type = $type; 428 $this->expanded = $expanded; 429 430 $t =& CreateObject('phpgwapi.Template',EGW_INCLUDE_ROOT . '/bookmarks/templates/export'); 431 $t->set_file('export','export_' . $this->type . '.tpl'); 432 $t->set_block('export','catlist','categs'); 433 if (is_array($catlist)) 434 { 435 foreach ($catlist as $catid) 436 { 437 $t->set_var('categ',$this->gencat($catid)); 438 $t->fp('categs','catlist',True); 439 } 440 } 441 return $t->fp('out','export'); 442 } 443 444 function gencat($catid) 445 { 446 $t =& new Template(EGW_INCLUDE_ROOT . '/bookmarks/templates/export'); 447 $t->set_file('categ','export_' . $this->type . '_catlist.tpl'); 448 $t->set_block('categ','subcatlist','subcats'); 449 $t->set_block('categ','urllist','urls'); 450 $subcats = $this->categories->return_array('subs',0,False,'','cat_name','',True,$catid); 451 452 if ($subcats) 453 { 454 foreach($subcats as $subcat) 455 { 456 $t->set_var('subcat',$this->gencat($subcat['id'])); 457 $t->fp('subcats','subcatlist',True); 458 } 459 } 460 461 $t->set_var(array( 462 'catname' => $this->translation->convert($GLOBALS['egw']->strip_html($this->categories->id2name($catid)),$this->charset,'utf-8'), 463 'catid' => $catid, 464 'folded' => (in_array($catid,$this->expanded) ? 'no' : 'yes') 465 )); 466 467 $bm_list = $this->_list($catid,False,False,False); 468 469 while(list($bm_id,$bookmark) = @each($bm_list)) 470 { 471 $t->set_var(array( 472 'url' => $bookmark['url'], 473 'name' => $this->translation->convert($bookmark['name'],$this->charset,'utf-8'), 474 'desc' => $this->translation->convert($bookmark['desc'],$this->charset,'utf-8') 475 )); 476 $t->fp('urls','urllist',True); 477 } 478 return $t->fp('out','categ'); 479 } 480 481 function _debug($s) 482 { 483 //echo $s; 484 } 485 } 486 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |