ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
ilLinkChecker Class Reference

class for checking external links in page objects Normally used in Cron jobs, but should be extensible for use in learning modules. More...

+ Collaboration diagram for ilLinkChecker:

Public Member Functions

 ilLinkChecker (&$db, $a_validate_all=true)
 setCheckPeriod ($a_period)
 getCheckPeriod ()
 setMailStatus ($a_status)
 getMailStatus ()
 __setType ($a_type)
 __getType ()
 setObjId ($a_page_id)
 getObjId ()
 getValidateAll ()
 getLogMessages ()
 getInvalidLinks ()
 getInvalidLinksFromDB ()
 getLastCheckTimestamp ()
 checkWebResourceLinks ()
 checkLinks ()
 checkPear ()
 __txt ($language, $key, $module= 'common')
 __fetchUserData ($a_usr_id)
 __getTitle ($a_lm_obj_id)
 __sendMail ()
 __getNotifyLinks ()
 __clearInvalidLinks ()
 __appendInvalidLink ($a_link)
 __appendLogMessage ($a_string)
 __clearLogMessages ()
 __getLinks ($a_page)
 __getWebResourceLinks ()
 __validateLinks ($a_links)
 __getObjIdByPageId ($a_page_id)
 __isInvalid ($a_page_id, $a_url)
 __saveInDB ()
 __checkNotify ()
 __clearDBData ()

Data Fields

 $db = null
 $log_messages = array()
 $invalid_links = array()
 $validate_all = true
 $mail_status = false
 $page_id = 0

Protected Member Functions

 createPermanentLink ($a_obj_id, $a_usr_id, $a_obj_type)
 creates a permanent link

Detailed Description

class for checking external links in page objects Normally used in Cron jobs, but should be extensible for use in learning modules.

In this case set second parameter of contructor = false, and use setPageObjectId()

Author
Stefan Meyer meyer.nosp@m.@lei.nosp@m.fos.c.nosp@m.om
Version
Id:
class.ilLinkChecker.php 54204 2014-10-14 09:40:44Z fwolf

Definition at line 12 of file class.ilLinkChecker.php.

Member Function Documentation

ilLinkChecker::__appendInvalidLink (   $a_link)

Definition at line 384 of file class.ilLinkChecker.php.

Referenced by checkLinks(), and checkWebResourceLinks().

{
$this->invalid_links[] = $a_link;
}

+ Here is the caller graph for this function:

ilLinkChecker::__appendLogMessage (   $a_string)

Definition at line 390 of file class.ilLinkChecker.php.

Referenced by __sendMail(), __validateLinks(), checkLinks(), and checkWebResourceLinks().

{
$this->log_messages[] = $a_string;
}

+ Here is the caller graph for this function:

ilLinkChecker::__checkNotify ( )

Definition at line 605 of file class.ilLinkChecker.php.

References $ilDB, $query, $res, and getInvalidLinks().

Referenced by __saveInDB().

{
global $ilDB;
foreach($this->getInvalidLinks() as $link)
{
$query = "SELECT * FROM link_check ".
"WHERE page_id = ".$ilDB->quote($link['page_id'],'integer')." ".
"AND url = ".$ilDB->quote(substr($link['complete'],0,255),'text')." ";
$res = $ilDB->query($query);
if(!$res->numRows())
{
$this->notify[$link["obj_id"]][] = array('page_id' => $link['page_id'],
'url' => $link['complete']);
}
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilLinkChecker::__clearDBData ( )

Definition at line 625 of file class.ilLinkChecker.php.

References $ilDB, $query, $res, getObjId(), and getValidateAll().

Referenced by __saveInDB().

{
global $ilDB;
if($this->getValidateAll())
{
$query = "DELETE FROM link_check";
}
else
{
$query = "DELETE FROM link_check ".
"WHERE obj_id = ".$ilDB->quote($this->getObjId(),'integer');
}
$res = $ilDB->manipulate($query);
return true;
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilLinkChecker::__clearInvalidLinks ( )

Definition at line 380 of file class.ilLinkChecker.php.

Referenced by checkLinks(), and checkWebResourceLinks().

{
$this->invalid_links = array();
}

+ Here is the caller graph for this function:

ilLinkChecker::__clearLogMessages ( )

Definition at line 394 of file class.ilLinkChecker.php.

Referenced by checkLinks(), and checkWebResourceLinks().

{
return $this->log_messages = array();
}

+ Here is the caller graph for this function:

ilLinkChecker::__fetchUserData (   $a_usr_id)

Definition at line 236 of file class.ilLinkChecker.php.

References $ilDB, $query, $row, and DB_FETCHMODE_OBJECT.

{
global $ilDB;
$query = "SELECT email FROM usr_data WHERE usr_id = ".$ilDB->quote($a_usr_id)."";
$row = $this->db->getRow($query,DB_FETCHMODE_OBJECT);
$data['email'] = $row->email;
$set = $ilDB->query("SELECT * FROM usr_pref ".
"WHERE usr_id = ".$ilDB->quote($a_usr_id, "integer")." ".
"AND keyword = ".$ilDB->quote('language', "text"));
$row = $ilDB->fetchObject($set);
$data['lang'] = $row->value;
return $data;
}
ilLinkChecker::__getLinks (   $a_page)

Definition at line 399 of file class.ilLinkChecker.php.

References __getObjIdByPageId().

Referenced by checkLinks().

{
$matches = array();
$pattern_complete = '/<ExtLink Href="([^"]*)">/';
if(preg_match_all($pattern_complete,$a_page['content'],$matches))
{
for($i = 0;$i < count($matches[0]); ++$i)
{
$url_data = @parse_url($matches[1][$i]);
// continue if mailto link
if($url_data['scheme'] == 'mailto')
{
continue;
}
// PUH, HTTP_REQUEST needs a beginning http://
if(!$url_data['scheme'])
{
$matches[1][$i] = 'http://'.$matches[1][$i];
}
$lm_id = $this->__getObjIdByPageId($a_page['page_id']);
$link[] = array('page_id' => $a_page['page_id'],
'obj_id' => $lm_id,
'type' => $a_page['type'],
'complete' => $matches[1][$i],
'scheme' => isset($url_data['scheme']) ? $url_data['scheme'] : 'http',
'host' => isset($url_data['host']) ? $url_data['host'] : $url_data['path']);
}
}
return $link ? $link : array();
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilLinkChecker::__getNotifyLinks ( )

Definition at line 374 of file class.ilLinkChecker.php.

Referenced by __sendMail().

{
return $this->notify ? $this->notify : array();
}

+ Here is the caller graph for this function:

ilLinkChecker::__getObjIdByPageId (   $a_page_id)

Definition at line 556 of file class.ilLinkChecker.php.

References $query, $row, and DB_FETCHMODE_OBJECT.

Referenced by __getLinks().

{
$query = "SELECT lm_id FROM lm_data ".
"WHERE obj_id = '".$a_page_id."'";
$row = $this->db->getRow($query,DB_FETCHMODE_OBJECT);
return $row->lm_id ? $row->lm_id : 0;
}

+ Here is the caller graph for this function:

ilLinkChecker::__getTitle (   $a_lm_obj_id)

Definition at line 257 of file class.ilLinkChecker.php.

References $ilDB, $query, $row, and DB_FETCHMODE_OBJECT.

Referenced by __sendMail().

{
global $ilDB;
$query = "SELECT title FROM object_data ".
"WHERE obj_id = ".$ilDB->quote($a_lm_obj_id ,'integer')." ";
$row = $this->db->getRow($query,DB_FETCHMODE_OBJECT);
return $row->title;
}

+ Here is the caller graph for this function:

ilLinkChecker::__getType ( )

Definition at line 60 of file class.ilLinkChecker.php.

Referenced by __sendMail().

{
return $this->type;
}

+ Here is the caller graph for this function:

ilLinkChecker::__getWebResourceLinks ( )

Definition at line 434 of file class.ilLinkChecker.php.

References getObjId().

Referenced by checkWebResourceLinks().

{
global $objDefinition;
include_once 'Modules/WebResource/classes/class.ilLinkResourceItems.php';
$link_res_obj = new ilLinkResourceItems($this->getObjId());
foreach($check_links = $link_res_obj->getCheckItems($this->getCheckPeriod()) as $item_data)
{
// #10091 - internal
if(strpos($item_data['target'], '|'))
{
$parts = explode('|', $item_data['target']);
if(sizeof($parts) == 2 &&
is_numeric($parts[1]) &&
$objDefinition->isAllowedInRepository($parts[0]))
{
$link[] = array('page_id' => $item_data['link_id'],
'obj_id' => $this->getObjId(),
'type' => 'webr',
'complete' => $item_data['target'],
'scheme' => 'internal',
'obj_type' => $parts[0],
'ref_id' => $parts[1]);
continue;
}
}
// external
$url_data = @parse_url($item_data['target']);
// PUH, HTTP_REQUEST needs a beginning http://
if(!$url_data['scheme'])
{
$item_data['target'] = 'http://'.$item_data['target'];
}
$link[] = array('page_id' => $item_data['link_id'],
'obj_id' => $this->getObjId(),
'type' => 'webr',
'complete' => $item_data['target'],
'scheme' => isset($url_data['scheme']) ? $url_data['scheme'] : 'http',
'host' => isset($url_data['host']) ? $url_data['host'] : $url_data['path']);
}
return $link ? $link : array();
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilLinkChecker::__isInvalid (   $a_page_id,
  $a_url 
)

Definition at line 566 of file class.ilLinkChecker.php.

References getInvalidLinks().

{
foreach($this->getInvalidLinks() as $link)
{
if($link['page_id'] == $a_page_id and
substr($link['complete'],0,255) == $a_url)
{
return true;
}
}
return false;
}

+ Here is the call graph for this function:

ilLinkChecker::__saveInDB ( )

Definition at line 579 of file class.ilLinkChecker.php.

References $ilDB, $query, $res, __checkNotify(), __clearDBData(), getInvalidLinks(), and getMailStatus().

Referenced by checkLinks(), and checkWebResourceLinks().

{
global $ilDB;
if($this->getMailStatus())
{
$this->__checkNotify();
}
$this->__clearDBData();
foreach($this->getInvalidLinks() as $link)
{
$query = "INSERT INTO link_check (obj_id,page_id,url,parent_type,http_status_code,last_check) ".
"VALUES ( ".
$ilDB->quote($link['obj_id'],'integer').", ".
$ilDB->quote($link['page_id'],'integer').", ".
$ilDB->quote(substr($link['complete'],0,255),'text').", ".
$ilDB->quote($link['type'],'text').", ".
$ilDB->quote($link['http_status_code'] ? $link['http_status_code'] : 0,'integer').", ".
$ilDB->quote(time(),'integer')." ".
")";
$res = $ilDB->manipulate($query);
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilLinkChecker::__sendMail ( )

Definition at line 269 of file class.ilLinkChecker.php.

References $ilUser, __appendLogMessage(), __getNotifyLinks(), __getTitle(), __getType(), __txt(), ilLinkCheckNotify\_getAllNotifiers(), createPermanentLink(), ilObjectFactory\getInstanceByObjId(), and getMailStatus().

Referenced by checkLinks(), and checkWebResourceLinks().

{
global $ilUser;
if(!count($notify = $this->__getNotifyLinks()))
{
// Nothing to do
return true;
}
if(!$this->getMailStatus())
{
return true;
}
include_once './Services/LinkChecker/classes/class.ilLinkCheckNotify.php';
$body = "";
$obj_name = "";
foreach(ilLinkCheckNotify::_getAllNotifiers($this->db) as $usr_id => $obj_ids)
{
if(!is_object($tmp_user =& ilObjectFactory::getInstanceByObjId($usr_id,false)))
{
$this->__appendLogMessage('LinkChecker: Cannot find user with id: '.$usr_id);
continue;
}
$counter = 0;
foreach($obj_ids as $obj_id)
{
if(!isset($notify[$obj_id]))
{
continue;
}
++$counter;
switch($this->__getType())
{
case 'webr':
$obj_name = $this->__txt($tmp_user->getLanguage(),'obj_webr');
break;
case 'lm':
default:
$obj_name = $this->__txt($tmp_user->getLanguage(),'lo');
break;
}
$body .= $obj_name.': '.$this->__getTitle($obj_id)."\r\n";
$body .= $this->__txt($tmp_user->getLanguage(),'link_check_perma_link', "mail"). ": " .
$this->createPermanentLink($obj_id, $usr_id, $this->__getType())." \r\n";
$body .= $this->__txt($tmp_user->getLanguage(),"link_check_affected_links", "mail"). ":\r\n";
// Print all invalid
foreach($notify[$obj_id] as $data)
{
$body .= $data['url']."\r\n";
}
$body .= "\r\n";
}
if($counter)
{
include_once "./Services/Notification/classes/class.ilSystemNotification.php";
$ntf = new ilSystemNotification();
$ntf->setLangModules(array("mail", "common"));
$ntf->setSubjectLangId("link_check_subject");
$ntf->setIntroductionLangId("link_check_introduction");
$ntf->setReasonLangId("link_check_reason");
$ntf->addAdditionalInfo("additional_info", $body,true);
$ntf->sendMail(array($tmp_user->getId()));
$this->__appendLogMessage('LinkChecker: Sent mail to '.$tmp_user->getEmail());
}
$body = "";
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilLinkChecker::__setType (   $a_type)

Definition at line 56 of file class.ilLinkChecker.php.

Referenced by checkLinks(), and checkWebResourceLinks().

{
$this->type = $a_type;
}

+ Here is the caller graph for this function:

ilLinkChecker::__txt (   $language,
  $key,
  $module = 'common' 
)

Definition at line 228 of file class.ilLinkChecker.php.

References $ilDB, and ilLanguage\_lookupEntry().

Referenced by __sendMail().

{
global $ilDB;
include_once './Services/Language/classes/class.ilLanguage.php';
return ilLanguage::_lookupEntry($language, $module, $key);
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilLinkChecker::__validateLinks (   $a_links)

Definition at line 484 of file class.ilLinkChecker.php.

References $options, __appendLogMessage(), ilProxySettings\_getInstance(), ilObject\_lookupObjId(), and ilObject\_lookupType().

Referenced by checkLinks(), and checkWebResourceLinks().

{
global $tree;
if(!@include_once('HTTP/Request.php'))
{
$this->__appendLogMessage('LinkChecker: Pear HTTP_Request is not installed. Aborting');
return array();
}
foreach($a_links as $link)
{
// #10091 - internal
if($link['scheme'] == 'internal')
{
$obj_id = ilObject::_lookupObjId($link['ref_id']);
if(!$obj_id ||
ilObject::_lookupType($obj_id) != $link['obj_type'] ||
$tree->isDeleted($link['ref_id']))
{
$invalid[] = $link;
}
}
// external
else
{
if(gethostbyname($link['host']) == $link['host'])
{
$invalid[] = $link;
continue;
}
if($link['scheme'] !== 'http' and $link['scheme'] !== 'https')
{
continue;
}
require_once './Services/Http/classes/class.ilProxySettings.php';
{
$options = array('proxy_host' => ilProxySettings::_getInstance()->getHost(),
'proxy_port' => ilProxySettings::_getInstance()->getPort());
}
else
{
$options = array();
}
$req = new HTTP_Request($link['complete'], $options);
$req->sendRequest();
switch($req->getResponseCode())
{
// EVERYTHING OK
case '200':
// In the moment 301 will be handled as ok
case '301':
case '302':
break;
default:
$link['http_status_code'] = $req->getResponseCode();
$invalid[] = $link;
break;
}
}
}
return $invalid ? $invalid : array();
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilLinkChecker::checkLinks ( )

Definition at line 151 of file class.ilLinkChecker.php.

References $ilDB, $query, $res, $row, __appendInvalidLink(), __appendLogMessage(), __clearInvalidLinks(), __clearLogMessages(), __getLinks(), __saveInDB(), __sendMail(), __setType(), __validateLinks(), DB_FETCHMODE_OBJECT, getInvalidLinks(), getObjId(), and getValidateAll().

{
global $ilDB;
$pages = array();
$this->__setType('lm');
$this->__appendLogMessage('LinkChecker: Start checkLinks()');
if(!$this->getValidateAll() and !$this->getObjId())
{
echo "ilLinkChecker::checkLinks() No Page id given";
return false;
}
elseif(!$this->getValidateAll() and $this->getObjId())
{
$query = "SELECT * FROM page_object ".
"WHERE parent_id = ".$ilDB->quote($this->getObjId())." ".
"AND parent_type = 'lm'";
$res = $this->db->query($query);
while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
{
$pages[] = array('page_id' => $row->page_id,
'content' => $row->content,
'type' => $row->parent_type);
}
}
elseif($this->getValidateAll())
{
$query = "SELECT * FROM page_object ".
"WHERE parent_type = 'lm'";
$res = $this->db->query($query);
while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
{
$pages[] = array('page_id' => $row->page_id,
'content' => $row->content,
'type' => $row->parent_type);
}
}
// VALIDATE
foreach($pages as $page)
{
if(count($invalid = $this->__validateLinks($this->__getLinks($page))))
{
foreach($invalid as $invalid_item)
{
$this->__appendLogMessage('LinkChecker: found invalid link: '.$invalid_item['complete']);
$this->__appendInvalidLink($invalid_item);
}
}
}
$this->__appendLogMessage('LinkChecker: End checkLinks()');
$this->__saveInDB();
$this->__sendMail();
return $this->getInvalidLinks();
}

+ Here is the call graph for this function:

ilLinkChecker::checkPear ( )

Definition at line 217 of file class.ilLinkChecker.php.

{
if(!@include_once('HTTP/Request.php'))
{
return false;
}
return true;
}
ilLinkChecker::checkWebResourceLinks ( )

Definition at line 125 of file class.ilLinkChecker.php.

References __appendInvalidLink(), __appendLogMessage(), __clearInvalidLinks(), __clearLogMessages(), __getWebResourceLinks(), __saveInDB(), __sendMail(), __setType(), __validateLinks(), and getInvalidLinks().

{
$pages = array();
$this->__setType('webr');
$this->__appendLogMessage('LinkChecker: Start checkLinks()');
if(count($invalid = $this->__validateLinks($this->__getWebResourceLinks())))
{
foreach($invalid as $invalid_item)
{
$this->__appendLogMessage('LinkChecker: found invalid link: '.$invalid_item['complete']);
$this->__appendInvalidLink($invalid_item);
}
}
$this->__appendLogMessage('LinkChecker: End checkLinks()');
$this->__saveInDB();
$this->__sendMail();
return $this->getInvalidLinks();
}

+ Here is the call graph for this function:

ilLinkChecker::createPermanentLink (   $a_obj_id,
  $a_usr_id,
  $a_obj_type 
)
protected

creates a permanent link

Parameters
$a_obj_id
$a_usr_id
$a_obj_type
Returns
string goto link

Definition at line 351 of file class.ilLinkChecker.php.

References $ref_id, ilObject\_getAllReferences(), and ilLink\_getLink().

Referenced by __sendMail().

{
global $ilAccess;
$ref_ids = ilObject::_getAllReferences($a_obj_id);
$ref_id = null;
foreach((array) $ref_ids as $id)
{
if($ilAccess->checkAccessOfUser($a_usr_id, "read", "", $id, $a_obj_type, $a_obj_id))
{
$ref_id = $id;
}
}
if($ref_id === null)
{
return false;
}
include_once './Services/Link/classes/class.ilLink.php';
return ilLink::_getLink($ref_id, $a_obj_type);
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilLinkChecker::getCheckPeriod ( )

Definition at line 42 of file class.ilLinkChecker.php.

{
return $this->period;
}
ilLinkChecker::getInvalidLinks ( )

Definition at line 84 of file class.ilLinkChecker.php.

Referenced by __checkNotify(), __isInvalid(), __saveInDB(), checkLinks(), and checkWebResourceLinks().

{
return $this->invalid_links ? $this->invalid_links : array();
}

+ Here is the caller graph for this function:

ilLinkChecker::getInvalidLinksFromDB ( )

Definition at line 89 of file class.ilLinkChecker.php.

References $ilDB, $query, $res, $row, DB_FETCHMODE_OBJECT, and getObjId().

{
global $ilDB;
$query = "SELECT * FROM link_check ".
"WHERE obj_id = ".$ilDB->quote($this->getObjId(),'integer')." ";
$res = $this->db->query($query);
while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
{
$invalid[] = array('page_id' => $row->page_id,
'url' => $row->url);
}
return $invalid ? $invalid : array();
}

+ Here is the call graph for this function:

ilLinkChecker::getLastCheckTimestamp ( )

Definition at line 106 of file class.ilLinkChecker.php.

References $ilDB, $query, $res, $row, getObjId(), and getValidateAll().

{
global $ilDB;
if($this->getValidateAll())
{
$query = "SELECT MAX(last_check) last_check FROM link_check ";
}
else
{
$query = "SELECT MAX(last_check) last_check FROM link_check ".
"WHERE obj_id = ".$ilDB->quote($this->getObjId(),'integer')." ";
}
$res = $ilDB->query($query);
$row = $ilDB->fetchObject($res);
return $row->last_check ? $row->last_check : 0;
}

+ Here is the call graph for this function:

ilLinkChecker::getLogMessages ( )

Definition at line 79 of file class.ilLinkChecker.php.

{
return $this->log_messages ? $this->log_messages : array();
}
ilLinkChecker::getMailStatus ( )

Definition at line 51 of file class.ilLinkChecker.php.

References $mail_status.

Referenced by __saveInDB(), and __sendMail().

{
return (bool) $this->mail_status;
}

+ Here is the caller graph for this function:

ilLinkChecker::getObjId ( )

Definition at line 69 of file class.ilLinkChecker.php.

References $page_id.

Referenced by __clearDBData(), __getWebResourceLinks(), checkLinks(), getInvalidLinksFromDB(), and getLastCheckTimestamp().

{
}

+ Here is the caller graph for this function:

ilLinkChecker::getValidateAll ( )

Definition at line 74 of file class.ilLinkChecker.php.

Referenced by __clearDBData(), checkLinks(), and getLastCheckTimestamp().

{
return $this->validate_all ? true : false;
}

+ Here is the caller graph for this function:

ilLinkChecker::ilLinkChecker ( $db,
  $a_validate_all = true 
)

Definition at line 23 of file class.ilLinkChecker.php.

References $db, and $ilDB.

{
global $ilDB;
define('DEBUG',1);
define('SOCKET_TIMEOUT',5);
$this->db =& $db;
// SET GLOBAL DB HANDLER FOR STATIC METHODS OTHER CLASSES
$ilDB =& $db;
$this->validate_all = $a_validate_all;
}
ilLinkChecker::setCheckPeriod (   $a_period)

Definition at line 38 of file class.ilLinkChecker.php.

{
$this->period = $a_period;
}
ilLinkChecker::setMailStatus (   $a_status)

Definition at line 47 of file class.ilLinkChecker.php.

{
$this->mail_status = (bool) $a_status;
}
ilLinkChecker::setObjId (   $a_page_id)

Definition at line 65 of file class.ilLinkChecker.php.

{
return $this->page_id = $a_page_id;
}

Field Documentation

ilLinkChecker::$db = null

Definition at line 14 of file class.ilLinkChecker.php.

Referenced by ilLinkChecker().

ilLinkChecker::$invalid_links = array()

Definition at line 16 of file class.ilLinkChecker.php.

ilLinkChecker::$log_messages = array()

Definition at line 15 of file class.ilLinkChecker.php.

ilLinkChecker::$mail_status = false

Definition at line 19 of file class.ilLinkChecker.php.

Referenced by getMailStatus().

ilLinkChecker::$page_id = 0

Definition at line 20 of file class.ilLinkChecker.php.

Referenced by getObjId().

ilLinkChecker::$validate_all = true

Definition at line 18 of file class.ilLinkChecker.php.


The documentation for this class was generated from the following file: