TYPO3 API  SVNRelease
class.tx_linkvalidator_linktype_external.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2010 - 2009 Jochen Rieger (j.rieger@connecta.ag) 
00006  *  (c) 2010 - 2011 Michael Miousse (michael.miousse@infoglobe.ca)
00007  *  All rights reserved
00008  *
00009  *  This script is part of the TYPO3 project. The TYPO3 project is
00010  *  free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  The GNU General Public License can be found at
00016  *  http://www.gnu.org/copyleft/gpl.html.
00017  *
00018  *  This script is distributed in the hope that it will be useful,
00019  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  *  GNU General Public License for more details.
00022  *
00023  *  This copyright notice MUST APPEAR in all copies of the script!
00024  ***************************************************************/
00025 /**
00026  * This class provides Check External Links plugin implementation.
00027  *
00028  * @author Dimitri König <dk@cabag.ch>
00029  * @author Michael Miousse <michael.miousse@infoglobe.ca>
00030  * @package TYPO3
00031  * @subpackage linkvalidator
00032  */
00033 class tx_linkvalidator_linktype_External extends tx_linkvalidator_linktype_Abstract {
00034 
00035     /**
00036      * Cached list of the URLs, which were already checked for the current processing.
00037      *
00038      * @var array
00039      */
00040     protected $urlReports = array();
00041 
00042     /**
00043      * Cached list of all error parameters of the URLs, which were already checked for the current processing.
00044      *
00045      * @var array
00046      */
00047     protected $urlErrorParams = array();
00048 
00049     /**
00050      * Checks a given URL + /path/filename.ext for validity
00051      *
00052      * @param   string      $url: url to check
00053      * @param    array     $softRefEntry: the softref entry which builds the context of that url
00054      * @param   object      $reference:  parent instance of tx_linkvalidator_Processor
00055      * @return  string      TRUE on success or FALSE on error
00056      */
00057     public function checkLink($url, $softRefEntry, $reference) {
00058         $errorParams = array();
00059         if (isset($this->urlReports[$url])) {
00060             if(!$this->urlReports[$url]) {
00061                 if(is_array($this->urlErrorParams[$url])) {
00062                     $this->setErrorParams($this->urlErrorParams[$url]);
00063                 }
00064             }
00065             return $this->urlReports[$url];
00066         }
00067 
00068             // remove possible anchor from the url
00069         if (strrpos($url, '#') !== FALSE) {
00070             $url = substr($url, 0, strrpos($url, '#'));
00071         }
00072 
00073             // try to fetch the content of the URL (headers only)
00074         $report = array();
00075 
00076             // try fetching the content of the URL (just fetching the headers does not work correctly)
00077         $content = '';
00078         $content = t3lib_div::getURL($url, 1, FALSE, $report);
00079 
00080         $tries = 0;
00081         while (($report['http_code'] == 301 || $report['http_code'] == 302
00082             || $report['http_code'] == 303 || $report['http_code'] == 307)
00083             && ($tries < 5)) {
00084                 $isCodeRedirect = preg_match('/Location: (.*)/', $content, $location);
00085                 if (isset($location[1])) {
00086                     $content = t3lib_div::getURL($location[1], 2, FALSE, $report);
00087                 }
00088                 $tries++;
00089         }
00090 
00091         $response = TRUE;
00092 
00093             // analyze the response
00094         if ($report['error']) {
00095                 // More cURL error codes can be found here:
00096                 // http://curl.haxx.se/libcurl/c/libcurl-errors.html
00097             if ($report['lib'] === 'cURL' && $report['error'] === 28) {
00098                 $errorParams['errorType'] = 'cURL28';
00099             } elseif ($report['lib'] === 'cURL' && $report['error'] === 22) {
00100                 if (strstr($report['message'], '404')) {
00101                     $errorParams['errorType'] = 404;
00102                 } elseif(strstr($report['message'], '403')) {
00103                     $errorParams['errorType'] = 403;
00104                 } elseif(strstr($report['message'], '500')) {
00105                     $errorParams['errorType'] = 500;
00106                 }
00107             } elseif ($report['lib'] === 'cURL' && $report['error'] === 6) {
00108                 $errorParams['errorType'] = 'cURL6';
00109             } elseif ($report['lib'] === 'cURL' && $report['error'] === 56) {
00110                 $errorParams['errorType'] = 'cURL56';
00111             }
00112 
00113             $response = FALSE;
00114         }
00115 
00116 
00117             // special handling for more information
00118         if (($report['http_code'] == 301) || ($report['http_code'] == 302)
00119             || ($report['http_code'] == 303) || ($report['http_code'] == 307)) {
00120                 $errorParams['errorType'] = $report['http_code'];
00121                 $errorParams['location'] = $location[1];
00122                 $response = FALSE;
00123         }
00124 
00125         if ($report['http_code'] == 404 || $report['http_code'] == 403) {
00126             $errorParams['errorType'] = $report['http_code'];
00127             $response = FALSE;
00128         }
00129 
00130         if ($report['http_code'] >= 300 && $response) {
00131             $errorParams['errorType'] = $report['http_code'];
00132             $response = FALSE;
00133         }
00134 
00135         if(!$response) {
00136             $this->setErrorParams($errorParams);
00137         }
00138 
00139         $this->urlReports[$url] = $response;
00140         $this->urlErrorParams[$url] = $errorParams;
00141 
00142         return $response;
00143     }
00144 
00145     /**
00146      * Generate the localized error message from the error params saved from the parsing.
00147      *
00148      * @param   array    all parameters needed for the rendering of the error message
00149      * @return  string    validation error message
00150      */
00151     public function getErrorMessage($errorParams) {
00152         $errorType = $errorParams['errorType'];
00153         switch ($errorType) {
00154             case 300:
00155                 $response = sprintf($GLOBALS['LANG']->getLL('list.report.externalerror'), $errorType);
00156                 break;
00157 
00158             case 301:
00159             case 302:
00160             case 303:
00161             case 307:
00162                 $response = sprintf($GLOBALS['LANG']->getLL('list.report.redirectloop'), $errorType, $errorParams['location']);
00163                 break;
00164 
00165             case 404:
00166                 $response = $GLOBALS['LANG']->getLL('list.report.pagenotfound404');
00167                 break;
00168 
00169             case 403:
00170                 $response = $GLOBALS['LANG']->getLL('list.report.pageforbidden403');
00171                 break;
00172 
00173             case 500:
00174                 $response = $GLOBALS['LANG']->getLL('list.report.internalerror500');
00175                 break;
00176 
00177             case 'cURL6':
00178                 $response = $GLOBALS['LANG']->getLL('list.report.couldnotresolvehost');
00179                 break;
00180 
00181             case 'cURL28':
00182                 $response = $GLOBALS['LANG']->getLL('list.report.timeout');
00183                 break;
00184 
00185             case 'cURL56':
00186                 $response = $GLOBALS['LANG']->getLL('list.report.errornetworkdata');
00187                 break;
00188 
00189             default:
00190                 $response = $GLOBALS['LANG']->getLL('list.report.noresponse');
00191         }
00192 
00193         return $response;
00194     }
00195 
00196     /**
00197      * get the external type from the softRefParserObj result.
00198      *
00199      * @param   array     $value: reference properties
00200      * @param   string   $type: current type
00201      * @param   string   $key: validator hook name
00202      * @return  string   fetched type
00203      */
00204     public function fetchType($value, $type, $key) {
00205         preg_match_all('/((?:http|https|ftp|ftps))(?::\/\/)(?:[^\s<>]+)/i', $value['tokenValue'], $urls, PREG_PATTERN_ORDER);
00206 
00207         if (!empty($urls[0][0])) {
00208             $type = "external";
00209         }
00210 
00211         return $type;
00212     }
00213 
00214 }
00215 
00216 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/linkvalidator/classes/linktypes/class.tx_linkvalidator_linktypes_external.php'])) {
00217     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/linkvalidator/classes/linktypes/class.tx_linkvalidator_linktypes_external.php']);
00218 }
00219 
00220 ?>