• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

classes/class.ilLinkChecker.php

Go to the documentation of this file.
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 
00033 class ilLinkChecker
00034 {
00035         var $db = null;
00036         var $log_messages = array();
00037         var $invalid_links = array();
00038 
00039         var $validate_all = true;
00040         var $mail_status = false;
00041         var $page_id = 0;
00042 
00043 
00044         function ilLinkChecker(&$db,$a_validate_all = true)
00045         {
00046                 global $ilDB;
00047 
00048                 define('DEBUG',1);
00049                 define('SOCKET_TIMEOUT',5);
00050 
00051                 $this->db =& $db;
00052 
00053                 // SET GLOBAL DB HANDLER FOR STATIC METHODS OTHER CLASSES
00054                 $ilDB =& $db;
00055 
00056                 $this->validate_all = $a_validate_all;
00057         }
00058 
00059         function setMailStatus($a_status)
00060         {
00061                 $this->mail_status = (bool) $a_status;
00062         }
00063         function getMailStatus()
00064         {
00065                 return (bool) $this->mail_status;
00066         }
00067 
00068 
00069         function setObjId($a_page_id)
00070         {
00071                 return $this->page_id = $a_page_id;
00072         }
00073         function getObjId()
00074         {
00075                 return $this->page_id;
00076         }
00077 
00078         function getValidateAll()
00079         {
00080                 return $this->validate_all ? true : false;
00081         }
00082 
00083         function getLogMessages()
00084         {
00085                 return $this->log_messages ? $this->log_messages : array();
00086         }
00087 
00088         function getInvalidLinks()
00089         {
00090                 return $this->invalid_links ? $this->invalid_links : array();
00091         }
00092 
00093         function getInvalidLinksFromDB()
00094         {
00095                 $query = "SELECT * FROM link_check ".
00096                         "WHERE obj_id = '".$this->getObjId()."'";
00097 
00098                 $res = $this->db->query($query);
00099                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00100                 {
00101                         $invalid[] = array('page_id' => $row->page_id,
00102                                                            'url'         => $row->url);
00103                 }
00104 
00105                 return $invalid ? $invalid : array();
00106         }
00107 
00108         function getLastCheckTimestamp()
00109         {
00110                 if($this->getValidateAll())
00111                 {
00112                         $query = "SELECT MAX(last_check) as last_check FROM link_check ";
00113                 }
00114                 else
00115                 {
00116                         $query = "SELECT MAX(last_check) as last_check FROM link_check ".
00117                                 "WHERE obj_id = '".$this->getObjId()."'";
00118                 }
00119                 $row = $this->db->getRow($query,DB_FETCHMODE_OBJECT);
00120 
00121                 return $row->last_check ? $row->last_check : 0;
00122         }
00123 
00124         
00125         function checkLinks()
00126         {
00127                 $pages = array();
00128 
00129                 $this->__clearLogMessages();
00130                 $this->__clearInvalidLinks();
00131                 $this->__appendLogMessage('LinkChecker: Start checkLinks()');
00132 
00133                 if(!$this->getValidateAll() and !$this->getObjId())
00134                 {
00135                         echo "ilLinkChecker::checkLinks() No Page id given";
00136 
00137                         return false;
00138                 }
00139                 elseif(!$this->getValidateAll() and $this->getObjId())
00140                 {
00141                         $query = "SELECT * FROM page_object ".
00142                                 "WHERE parent_id = '".$this->getObjId()."' ".
00143                                 "AND parent_type = 'lm'";
00144 
00145                         $res = $this->db->query($query);
00146                         while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00147                         {
00148                                 $pages[] = array('page_id' => $row->page_id,
00149                                                                  'content' => $row->content,
00150                                                                  'type'  => $row->parent_type);
00151                         }
00152                 }
00153                 elseif($this->getValidateAll())
00154                 {
00155                         $query = "SELECT * FROM page_object ".
00156                                 "WHERE parent_type = 'lm'";
00157 
00158                         $res = $this->db->query($query);
00159                         while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00160                         {
00161                                 $pages[] = array('page_id' => $row->page_id,
00162                                                                  'content' => $row->content,
00163                                                                  'type'  => $row->parent_type);
00164                         }
00165                 }
00166 
00167                 // VALIDATE
00168                 foreach($pages as $page)
00169                 {
00170                         if(count($invalid = $this->__validateLinks($this->__getLinks($page))))
00171                         {
00172                                 foreach($invalid as $invalid_item)
00173                                 {
00174                                         $this->__appendLogMessage('LinkChecker: found invalid link: '.$invalid_item['complete']);
00175                                         $this->__appendInvalidLink($invalid_item);
00176                                 }
00177                         }
00178                 }
00179                 
00180                 $this->__appendLogMessage('LinkChecker: End checkLinks()');
00181                 $this->__saveInDB();
00182 
00183                 $this->__sendMail();
00184 
00185                 return $this->getInvalidLinks();
00186         }
00187 
00188         function checkPear()
00189         {
00190                 if(!@include_once('HTTP/Request.php'))
00191                 {
00192                         return false;
00193                 }
00194                 return true;
00195         }
00196                 
00197 
00198         // PRIVATE
00199         function __txt($language,$key,$module = 'common')
00200         {
00201                 $query = "SELECT value FROM lng_data ".
00202                         "WHERE module = '".$module."' ".
00203                         "AND identifier = '".$key."' ".
00204                         "AND lang_key = '".$language."'";
00205 
00206                 $res = $this->db->query($query);
00207                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00208                 {
00209                         $value = $row->value;
00210                 }
00211                 if(!$value)
00212                 {
00213                         $query = "SELECT value FROM lng_data ".
00214                                 "WHERE module = '".$module."' ".
00215                                 "AND identifier = '".$key."' ".
00216                                 "AND lang_key = 'en'";
00217                         
00218                         $res = $this->db->query($query);
00219                         while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00220                         {
00221                                 $value = $row->value;
00222                         }
00223                 }
00224                 return $value ? $value : '-'.$key.'-';
00225         }
00226 
00227         function __fetchUserData($a_usr_id)
00228         {
00229                 $query = "SELECT email FROM usr_data WHERE usr_id = '".$a_usr_id."'";
00230 
00231                 $row = $this->db->getRow($query,DB_FETCHMODE_OBJECT);
00232 
00233                 $data['email'] = $row->email;
00234 
00235                 $query = "SELECT * FROM usr_pref ".
00236                         "WHERE usr_id = '".$a_usr_id."' ".
00237                         "AND keyword = 'language'";
00238 
00239                 $row = $this->db->getRow($query,DB_FETCHMODE_OBJECT);
00240 
00241                 $data['lang'] = $row->value;
00242 
00243                 return $data;
00244         }
00245 
00246         function __getTitle($a_lm_obj_id)
00247         {
00248                 $query = "SELECT title FROM object_data ".
00249                         "WHERE obj_id = '".$a_lm_obj_id."'";
00250 
00251                 $row = $this->db->getRow($query,DB_FETCHMODE_OBJECT);
00252 
00253                 return $row->title;
00254         }
00255 
00256         function __sendMail()
00257         {
00258                 if(!count($notify = $this->__getNotifyLinks()))
00259                 {
00260                         // Nothing to do
00261                         return true;
00262                 }
00263                 if($this->getMailStatus())
00264                 {
00265                         // get all users who want to be notified
00266                         include_once '../classes/class.ilLinkCheckNotify.php';
00267                         
00268                         foreach(ilLinkCheckNotify::_getAllNotifiers($this->db) as $usr_id => $obj_ids)
00269                         {
00270                                 // Get usr_data (default language, email)
00271                                 $usr_data = $this->__fetchUserData($usr_id);
00272 
00273                                 include_once '../classes/class.ilMimeMail.php';
00274                                 
00275                                 $mail =& new ilMimeMail();
00276                                 
00277                                 $mail->From('noreply');
00278                                 $mail->To($usr_data['email']);
00279                                 $mail->Subject($this->__txt($usr_data['lang'],'link_check_subject'));
00280 
00281                                 $body = $this->__txt($usr_data['lang'],'link_check_body_top')."\r\n";
00282 
00283                                 $counter = 0;
00284                                 foreach($obj_ids as $obj_id)
00285                                 {
00286                                         if(!isset($notify[$obj_id]))
00287                                         {
00288                                                 continue;
00289                                         }
00290                                         ++$counter;
00291 
00292                                         $body .= $this->__txt($usr_data['lang'],'lo');
00293                                         $body .= ': ';
00294                                         $body .= $this->__getTitle($obj_id)."\r\n";
00295 
00296                                         // Print all invalid
00297                                         foreach($notify[$obj_id] as $data)
00298                                         {
00299                                                 $body .= $data['url']."\r\n";
00300                                         }
00301                                         $body .= "\r\n";
00302                                 }
00303                                 if($counter)
00304                                 {
00305                                         $mail->Body($body);
00306                                         $mail->Send();
00307                                         $this->__appendLogMessage('LinkChecker: Sent mail to '.$usr_data['email']);
00308 
00309                                 }
00310                         }
00311                 }
00312         }
00313 
00314         function __getNotifyLinks()
00315         {
00316                 return $this->notify ? $this->notify : array();
00317         }
00318 
00319 
00320         function __clearInvalidLinks()
00321         {
00322                 $this->invalid_links = array();
00323         }
00324         function __appendInvalidLink($a_link)
00325         {
00326                 $this->invalid_links[] = $a_link;
00327         }
00328                                         
00329 
00330         function __appendLogMessage($a_string)
00331         {
00332                 $this->log_messages[] = $a_string;
00333         }
00334         function __clearLogMessages()
00335         {
00336                 return $this->log_messages = array();
00337         }
00338 
00339         function __getLinks($a_page)
00340         {
00341                 $matches = array();
00342 
00343                 $pattern_complete = '/<ExtLink Href="([^"]*)">/';
00344                 if(preg_match_all($pattern_complete,$a_page['content'],$matches))
00345                 {
00346                         for($i = 0;$i < count($matches[0]); ++$i)
00347                         {
00348                                 $url_data = parse_url($matches[1][$i]);
00349                                 
00350                                 // PUH, HTTP_REQUEST needs a beginning http://
00351                                 if(!$url_data['scheme'])
00352                                 {
00353                                         $matches[1][$i] = 'http://'.$matches[1][$i];
00354                                 }
00355 
00356                                 $lm_id = $this->__getObjIdByPageId($a_page['page_id']);
00357                                 $link[] = array('page_id'  => $a_page['page_id'],
00358                                                                 'obj_id'   => $lm_id,
00359                                                                 'type'     => $a_page['type'],
00360                                                                 'complete' => $matches[1][$i],
00361                                                                 'scheme'   => isset($url_data['scheme']) ? $url_data['scheme'] : 'http',
00362                                                                 'host'     => isset($url_data['host']) ? $url_data['host'] : $url_data['path']);
00363                         }
00364                 }
00365 
00366                 return $link ? $link : array();
00367         }
00368 
00369         function __validateLinks($a_links)
00370         {
00371                 if(!@include_once('HTTP/Request.php'))
00372                 {
00373                         $this->__appendLogMessage('LinkChecker: Pear HTTP_Request is not installed. Aborting');
00374 
00375                         return array();
00376                 }
00377 
00378                 foreach($a_links as $link)
00379                 {
00380                         if($link['scheme'] !== 'http' and $link['scheme'] !== 'https')
00381                         {
00382                                 continue;
00383                         }
00384 
00385                         $req =& new HTTP_Request($link['complete']);
00386                         $req->sendRequest();
00387 
00388                         switch($req->getResponseCode())
00389                         {
00390                                 // EVERYTHING OK
00391                                 case '200':
00392                                         // In the moment 301 will be handled as ok
00393                                 case '301':
00394                                 case '302':
00395                                         break;
00396 
00397                                 default:
00398                                         $link['http_status_code'] = $req->getResponseCode();
00399                                         $invalid[] = $link;
00400                                         break;
00401                         }
00402                 }
00403                 return $invalid ? $invalid : array();
00404         }
00405 
00406         function __getObjIdByPageId($a_page_id)
00407         {
00408                 $query = "SELECT lm_id FROM lm_data ".
00409                         "WHERE obj_id = '".$a_page_id."'";
00410 
00411                 $row = $this->db->getRow($query,DB_FETCHMODE_OBJECT);
00412 
00413                 return $row->lm_id ? $row->lm_id : 0;
00414         }
00415 
00416         function __isInvalid($a_page_id, $a_url)
00417         {
00418                 foreach($this->getInvalidLinks() as $link)
00419                 {
00420                         if($link['page_id'] == $a_page_id and
00421                            substr($link['complete'],0,255) == $a_url)
00422                         {
00423                                 return true;
00424                         }
00425                 }
00426                 return false;
00427         }
00428 
00429         function __saveInDB()
00430         {
00431                 if($this->getMailStatus())
00432                 {
00433                         $this->__checkNotify();
00434                 }
00435                 $this->__clearDBData();
00436 
00437 
00438                 foreach($this->getInvalidLinks() as $link)
00439                 {
00440 
00441                         $query = "INSERT INTO link_check ".
00442                                 "SET page_id = '".$link['page_id']."', ".
00443                                 "obj_id = '".$link['obj_id']."', ".
00444                                 "url = '".substr($link['complete'],0,255)."', ".
00445                                 "parent_type = '".$link['type']."', ".
00446                                 "http_status_code = '".$link['http_status_code']."', ".
00447                                 "last_check = '".time()."'";
00448 
00449 
00450                         $res = $this->db->query($query);
00451                 }
00452 
00453                 // delete old values
00454                 
00455         }
00456 
00457         function __checkNotify()
00458         {
00459                 foreach($this->getInvalidLinks() as $link)
00460                 {
00461                         $query = "SELECT * FROM link_check ".
00462                                 "WHERE page_id = '".$link['page_id']."' ".
00463                                 "AND url = '".substr($link['complete'],0,255)."'";
00464                         $res = $this->db->query($query);
00465                         
00466                         if(!$res->numRows())
00467                         {
00468                                 $this->notify["$link[obj_id]"][] = array('page_id' => $link['page_id'],
00469                                                                                                                  'url'     => $link['complete']);
00470                         }
00471                 }
00472         }
00473 
00474 
00475         function __clearDBData()
00476         {
00477                 if($this->getValidateAll())
00478                 {
00479                         $query = "DELETE FROM link_check";
00480                 }
00481                 else
00482                 {
00483                         $query = "DELETE FROM link_check ".
00484                                 "WHERE obj_id = '".$this->getObjId()."'";
00485                 }
00486 
00487                 $this->db->query($query);
00488 
00489                 return true;
00490         }
00491 }
00492 ?>

Generated on Fri Dec 13 2013 08:00:14 for ILIAS Release_3_3_x_branch .rev 46803 by  doxygen 1.7.1