00001 <?php 00002 /* 00003 +-----------------------------------------------------------------------------+ 00004 | ILIAS open source | 00005 +-----------------------------------------------------------------------------+ 00006 | Copyright (c) 1998-2001 ILIAS open source, University of Cologne | 00007 | | 00008 | This program is free software; you can redistribute it and/or | 00009 | modify it under the terms of the GNU General Public License | 00010 | as published by the Free Software Foundation; either version 2 | 00011 | of the License, or (at your option) any later version. | 00012 | | 00013 | This program is distributed in the hope that it will be useful, | 00014 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00015 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00016 | GNU General Public License for more details. | 00017 | | 00018 | You should have received a copy of the GNU General Public License | 00019 | along with this program; if not, write to the Free Software | 00020 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 00021 +-----------------------------------------------------------------------------+ 00022 */ 00023 00032 class ilLMMenuEditor 00033 { 00034 function ilLMMenuEditor() 00035 { 00036 global $ilDB; 00037 00038 $this->db =& $ilDB; 00039 $this->link_type = "extern"; 00040 $this->link_ref_id = null; 00041 } 00042 00043 function setObjId($a_obj_id) 00044 { 00045 $this->lm_id = $a_obj_id; 00046 } 00047 00048 function getObjId() 00049 { 00050 return $this->lm_id; 00051 } 00052 00053 function setEntryId($a_id) 00054 { 00055 $this->entry_id = $a_id; 00056 } 00057 00058 function getEntryId() 00059 { 00060 return $this->entry_id; 00061 } 00062 00063 function setLinkType($a_link_type) 00064 { 00065 $this->link_type = $a_link_type; 00066 } 00067 00068 function getLinkType() 00069 { 00070 return $this->link_type; 00071 } 00072 00073 function setTitle($a_title) 00074 { 00075 $this->title = $a_title; 00076 } 00077 00078 function getTitle() 00079 { 00080 return $this->title; 00081 } 00082 00083 function setTarget($a_target) 00084 { 00085 $this->target = $a_target; 00086 } 00087 00088 function getTarget() 00089 { 00090 return $this->target; 00091 } 00092 00093 function setLinkRefId($a_link_ref_id) 00094 { 00095 $this->link_ref_id = $a_link_ref_id; 00096 } 00097 00098 function getLinkRefId() 00099 { 00100 return $this->link_ref_id; 00101 } 00102 00103 function create() 00104 { 00105 $q = "INSERT INTO lm_menu (lm_id,link_type,title,target,link_ref_id) ". 00106 "VALUES ". 00107 "(". 00108 $this->db->quote($this->getObjId()).",". 00109 $this->db->quote($this->getLinkType()).",". 00110 $this->db->quote($this->getTitle()).",". 00111 $this->db->quote($this->getTarget()).",". 00112 $this->db->quote($this->getLinkRefId()).")"; 00113 $r = $this->db->query($q); 00114 00115 return true; 00116 } 00117 00118 function getMenuEntries($a_only_active = false) 00119 { 00120 global $ilDB; 00121 00122 $entries = array(); 00123 00124 if ($a_only_active === true) 00125 { 00126 $and = " AND active = 'y'"; 00127 } 00128 00129 $q = "SELECT * FROM lm_menu ". 00130 "WHERE lm_id = ".$ilDB->quote($this->lm_id). 00131 $and; 00132 00133 $r = $this->db->query($q); 00134 00135 while($row = $r->fetchRow(DB_FETCHMODE_OBJECT)) 00136 { 00137 $entries[] = array('id' => $row->id, 00138 'title' => $row->title, 00139 'link' => $row->target, 00140 'type' => $row->link_type, 00141 'ref_id' => $row->link_ref_id, 00142 'active' => $row->active 00143 ); 00144 } 00145 00146 return $entries; 00147 } 00148 00153 function delete($a_id) 00154 { 00155 if (!$a_id) 00156 { 00157 return false; 00158 } 00159 00160 $q = "DELETE FROM lm_menu WHERE id = ".$this->db->quote($a_id); 00161 $this->db->query($q); 00162 00163 return true; 00164 } 00165 00170 function update() 00171 { 00172 global $ilDB; 00173 00174 $q = "UPDATE lm_menu SET ". 00175 " link_type = ".$ilDB->quote($this->getLinkType()).",". 00176 " title = ".$ilDB->quote($this->getTitle()).",". 00177 " target = ".$ilDB->quote($this->getTarget()).",". 00178 " link_ref_id = ".$ilDB->quote($this->getLinkRefId()). 00179 " WHERE id = ".$ilDB->quote($this->getEntryId()); 00180 $r = $this->db->query($q); 00181 00182 return true; 00183 } 00184 00185 function readEntry($a_id) 00186 { 00187 if (!$a_id) 00188 { 00189 return false; 00190 } 00191 00192 $q = "SELECT * FROM lm_menu WHERE id = ".$this->db->quote($a_id); 00193 $r = $this->db->query($q); 00194 00195 $row = $this->db->getRow($q,DB_FETCHMODE_OBJECT); 00196 00197 $this->setTitle($row->title); 00198 $this->setTarget($row->target); 00199 $this->setLinkType($row->link_type); 00200 $this->setLinkRefId($row->link_ref_id); 00201 $this->setEntryid($a_id); 00202 } 00203 00209 function updateActiveStatus($a_entries) 00210 { 00211 if (!is_array($a_entries)) 00212 { 00213 return false; 00214 } 00215 00216 // update active status 00217 $q = "UPDATE lm_menu SET " . 00218 "active = CASE " . 00219 "WHEN id IN (".implode(',',$a_entries).") " . 00220 "THEN 'y' ". 00221 "ELSE 'n' ". 00222 "END " . 00223 "WHERE lm_id = ".$this->lm_id; 00224 $this->db->query($q); 00225 } 00226 00227 /* 00228 function getValidateAll() 00229 { 00230 return $this->validate_all ? true : false; 00231 } 00232 00233 function getLogMessages() 00234 { 00235 return $this->log_messages ? $this->log_messages : array(); 00236 } 00237 00238 function getInvalidLinks() 00239 { 00240 return $this->invalid_links ? $this->invalid_links : array(); 00241 } 00242 00243 function getInvalidLinksFromDB() 00244 { 00245 $query = "SELECT * FROM link_check ". 00246 "WHERE obj_id = '".$this->getObjId()."'"; 00247 00248 $res = $this->db->query($query); 00249 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT)) 00250 { 00251 $invalid[] = array('page_id' => $row->page_id, 00252 'url' => $row->url); 00253 } 00254 00255 return $invalid ? $invalid : array(); 00256 } 00257 00258 function getLastCheckTimestamp() 00259 { 00260 if($this->getValidateAll()) 00261 { 00262 $query = "SELECT MAX(last_check) as last_check FROM link_check "; 00263 } 00264 else 00265 { 00266 $query = "SELECT MAX(last_check) as last_check FROM link_check ". 00267 "WHERE obj_id = '".$this->getObjId()."'"; 00268 } 00269 $row = $this->db->getRow($query,DB_FETCHMODE_OBJECT); 00270 00271 return $row->last_check ? $row->last_check : 0; 00272 } 00273 00274 00275 function checkLinks() 00276 { 00277 $pages = array(); 00278 00279 $this->__clearLogMessages(); 00280 $this->__clearInvalidLinks(); 00281 $this->__appendLogMessage('LinkChecker: Start checkLinks()'); 00282 00283 if(!$this->getValidateAll() and !$this->getObjId()) 00284 { 00285 echo "ilLinkChecker::checkLinks() No Page id given"; 00286 00287 return false; 00288 } 00289 elseif(!$this->getValidateAll() and $this->getObjId()) 00290 { 00291 $query = "SELECT * FROM page_object ". 00292 "WHERE parent_id = '".$this->getObjId()."' ". 00293 "AND parent_type = 'lm'"; 00294 00295 $res = $this->db->query($query); 00296 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT)) 00297 { 00298 $pages[] = array('page_id' => $row->page_id, 00299 'content' => $row->content, 00300 'type' => $row->parent_type); 00301 } 00302 } 00303 elseif($this->getValidateAll()) 00304 { 00305 $query = "SELECT * FROM page_object ". 00306 "WHERE parent_type = 'lm'"; 00307 00308 $res = $this->db->query($query); 00309 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT)) 00310 { 00311 $pages[] = array('page_id' => $row->page_id, 00312 'content' => $row->content, 00313 'type' => $row->parent_type); 00314 } 00315 } 00316 00317 // VALIDATE 00318 foreach($pages as $page) 00319 { 00320 if(count($invalid = $this->__validateLinks($this->__getLinks($page)))) 00321 { 00322 foreach($invalid as $invalid_item) 00323 { 00324 $this->__appendLogMessage('LinkChecker: found invalid link: '.$invalid_item['complete']); 00325 $this->__appendInvalidLink($invalid_item); 00326 } 00327 } 00328 } 00329 00330 $this->__appendLogMessage('LinkChecker: End checkLinks()'); 00331 $this->__saveInDB(); 00332 00333 $this->__sendMail(); 00334 00335 return $this->getInvalidLinks(); 00336 } 00337 00338 function checkPear() 00339 { 00340 if(!@include_once('HTTP/Request.php')) 00341 { 00342 return false; 00343 } 00344 return true; 00345 } 00346 00347 00348 // PRIVATE 00349 function __txt($language,$key,$module = 'common') 00350 { 00351 $query = "SELECT value FROM lng_data ". 00352 "WHERE module = '".$module."' ". 00353 "AND identifier = '".$key."' ". 00354 "AND lang_key = '".$language."'"; 00355 00356 $res = $this->db->query($query); 00357 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT)) 00358 { 00359 $value = $row->value; 00360 } 00361 if(!$value) 00362 { 00363 $query = "SELECT value FROM lng_data ". 00364 "WHERE module = '".$module."' ". 00365 "AND identifier = '".$key."' ". 00366 "AND lang_key = 'en'"; 00367 00368 $res = $this->db->query($query); 00369 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT)) 00370 { 00371 $value = $row->value; 00372 } 00373 } 00374 return $value ? $value : '-'.$key.'-'; 00375 } 00376 00377 function __fetchUserData($a_usr_id) 00378 { 00379 $query = "SELECT email FROM usr_data WHERE usr_id = '".$a_usr_id."'"; 00380 00381 $row = $this->db->getRow($query,DB_FETCHMODE_OBJECT); 00382 00383 $data['email'] = $row->email; 00384 00385 $query = "SELECT * FROM usr_pref ". 00386 "WHERE usr_id = '".$a_usr_id."' ". 00387 "AND keyword = 'language'"; 00388 00389 $row = $this->db->getRow($query,DB_FETCHMODE_OBJECT); 00390 00391 $data['lang'] = $row->value; 00392 00393 return $data; 00394 } 00395 00396 function __getTitle($a_lm_obj_id) 00397 { 00398 $query = "SELECT title FROM object_data ". 00399 "WHERE obj_id = '".$a_lm_obj_id."'"; 00400 00401 $row = $this->db->getRow($query,DB_FETCHMODE_OBJECT); 00402 00403 return $row->title; 00404 } 00405 00406 function __sendMail() 00407 { 00408 if(!count($notify = $this->__getNotifyLinks())) 00409 { 00410 // Nothing to do 00411 return true; 00412 } 00413 if($this->getMailStatus()) 00414 { 00415 // get all users who want to be notified 00416 include_once '../classes/class.ilLinkCheckNotify.php'; 00417 00418 foreach(ilLinkCheckNotify::_getAllNotifiers($this->db) as $usr_id => $obj_ids) 00419 { 00420 // Get usr_data (default language, email) 00421 $usr_data = $this->__fetchUserData($usr_id); 00422 00423 include_once '../classes/class.ilMimeMail.php'; 00424 00425 $mail =& new ilMimeMail(); 00426 00427 $mail->From('noreply'); 00428 $mail->To($usr_data['email']); 00429 $mail->Subject($this->__txt($usr_data['lang'],'link_check_subject')); 00430 00431 $body = $this->__txt($usr_data['lang'],'link_check_body_top')."\r\n"; 00432 00433 $counter = 0; 00434 foreach($obj_ids as $obj_id) 00435 { 00436 if(!isset($notify[$obj_id])) 00437 { 00438 continue; 00439 } 00440 ++$counter; 00441 00442 $body .= $this->__txt($usr_data['lang'],'lo'); 00443 $body .= ': '; 00444 $body .= $this->__getTitle($obj_id)."\r\n"; 00445 00446 // Print all invalid 00447 foreach($notify[$obj_id] as $data) 00448 { 00449 $body .= $data['url']."\r\n"; 00450 } 00451 $body .= "\r\n"; 00452 } 00453 if($counter) 00454 { 00455 $mail->Body($body); 00456 $mail->Send(); 00457 $this->__appendLogMessage('LinkChecker: Sent mail to '.$usr_data['email']); 00458 00459 } 00460 } 00461 } 00462 } 00463 00464 function __getNotifyLinks() 00465 { 00466 return $this->notify ? $this->notify : array(); 00467 } 00468 00469 00470 function __clearInvalidLinks() 00471 { 00472 $this->invalid_links = array(); 00473 } 00474 function __appendInvalidLink($a_link) 00475 { 00476 $this->invalid_links[] = $a_link; 00477 } 00478 00479 00480 function __appendLogMessage($a_string) 00481 { 00482 $this->log_messages[] = $a_string; 00483 } 00484 function __clearLogMessages() 00485 { 00486 return $this->log_messages = array(); 00487 } 00488 00489 function __getLinks($a_page) 00490 { 00491 $matches = array(); 00492 00493 $pattern_complete = '/<ExtLink Href="([^"]*)">/'; 00494 if(preg_match_all($pattern_complete,$a_page['content'],$matches)) 00495 { 00496 for($i = 0;$i < count($matches[0]); ++$i) 00497 { 00498 $url_data = parse_url($matches[1][$i]); 00499 00500 // PUH, HTTP_REQUEST needs a beginning http:// 00501 if(!$url_data['scheme']) 00502 { 00503 $matches[1][$i] = 'http://'.$matches[1][$i]; 00504 } 00505 00506 $lm_id = $this->__getObjIdByPageId($a_page['page_id']); 00507 $link[] = array('page_id' => $a_page['page_id'], 00508 'obj_id' => $lm_id, 00509 'type' => $a_page['type'], 00510 'complete' => $matches[1][$i], 00511 'scheme' => isset($url_data['scheme']) ? $url_data['scheme'] : 'http', 00512 'host' => isset($url_data['host']) ? $url_data['host'] : $url_data['path']); 00513 } 00514 } 00515 00516 return $link ? $link : array(); 00517 } 00518 00519 function __validateLinks($a_links) 00520 { 00521 if(!@include_once('HTTP/Request.php')) 00522 { 00523 $this->__appendLogMessage('LinkChecker: Pear HTTP_Request is not installed. Aborting'); 00524 00525 return array(); 00526 } 00527 00528 foreach($a_links as $link) 00529 { 00530 if($link['scheme'] !== 'http' and $link['scheme'] !== 'https') 00531 { 00532 continue; 00533 } 00534 00535 $req =& new HTTP_Request($link['complete']); 00536 $req->sendRequest(); 00537 00538 switch($req->getResponseCode()) 00539 { 00540 // EVERYTHING OK 00541 case '200': 00542 // In the moment 301 will be handled as ok 00543 case '301': 00544 case '302': 00545 break; 00546 00547 default: 00548 $link['http_status_code'] = $req->getResponseCode(); 00549 $invalid[] = $link; 00550 break; 00551 } 00552 } 00553 return $invalid ? $invalid : array(); 00554 } 00555 00556 function __getObjIdByPageId($a_page_id) 00557 { 00558 $query = "SELECT lm_id FROM lm_data ". 00559 "WHERE obj_id = '".$a_page_id."'"; 00560 00561 $row = $this->db->getRow($query,DB_FETCHMODE_OBJECT); 00562 00563 return $row->lm_id ? $row->lm_id : 0; 00564 } 00565 00566 function __isInvalid($a_page_id, $a_url) 00567 { 00568 foreach($this->getInvalidLinks() as $link) 00569 { 00570 if($link['page_id'] == $a_page_id and 00571 substr($link['complete'],0,255) == $a_url) 00572 { 00573 return true; 00574 } 00575 } 00576 return false; 00577 } 00578 00579 function __saveInDB() 00580 { 00581 if($this->getMailStatus()) 00582 { 00583 $this->__checkNotify(); 00584 } 00585 $this->__clearDBData(); 00586 00587 00588 foreach($this->getInvalidLinks() as $link) 00589 { 00590 00591 $query = "INSERT INTO link_check ". 00592 "SET page_id = '".$link['page_id']."', ". 00593 "obj_id = '".$link['obj_id']."', ". 00594 "url = '".substr($link['complete'],0,255)."', ". 00595 "parent_type = '".$link['type']."', ". 00596 "http_status_code = '".$link['http_status_code']."', ". 00597 "last_check = '".time()."'"; 00598 00599 00600 $res = $this->db->query($query); 00601 } 00602 00603 // delete old values 00604 00605 } 00606 00607 function __checkNotify() 00608 { 00609 foreach($this->getInvalidLinks() as $link) 00610 { 00611 $query = "SELECT * FROM link_check ". 00612 "WHERE page_id = '".$link['page_id']."' ". 00613 "AND url = '".substr($link['complete'],0,255)."'"; 00614 $res = $this->db->query($query); 00615 00616 if(!$res->numRows()) 00617 { 00618 $this->notify["$link[obj_id]"][] = array('page_id' => $link['page_id'], 00619 'url' => $link['complete']); 00620 } 00621 } 00622 } 00623 00624 00625 function __clearDBData() 00626 { 00627 if($this->getValidateAll()) 00628 { 00629 $query = "DELETE FROM link_check"; 00630 } 00631 else 00632 { 00633 $query = "DELETE FROM link_check ". 00634 "WHERE obj_id = '".$this->getObjId()."'"; 00635 } 00636 00637 $this->db->query($query); 00638 00639 return true; 00640 } 00641 */ 00642 } 00643 ?>