Assembla home | Assembla project page
 

Changeset 239

Show
Ignore:
Timestamp:
04/14/08 23:14:26 (7 months ago)
Author:
Slynderdale
Message:

Fixed up PHP a bit more to add error checking and such.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Phergie/Plugin/Php.php

    r238 r239  
    1717     */ 
    1818    protected $cache; 
     19 
     20    /** 
     21     * Set to true by the custom error handler if an HTTP error code has been received 
     22     * 
     23     * @var boolean 
     24     */ 
     25    protected $errorStatus = false; 
    1926 
    2027    /** 
     
    5461            $name = str_replace(array('_', ' '), '-', $function); 
    5562            $tmp = file_get_contents('http://php.net/manual/en/function.' . rtrim($name, '();') . '.php'); 
    56             if( strpos( $tmp, '<p class="refpurpose dc-title">' ) !== false ) { 
     63            if(!$this->errorStatus && strpos($tmp, '<p class="refpurpose dc-title">') !== false) { 
    5764               $contents = '[ '.$this->tinyUrl('http://php.net/'.$name).' ] '; 
    5865               if (preg_match( '/<div class="methodsynopsis dc-description">(.*?)<\/div>/mis', $tmp, $m)) { 
    59                   $contents .= $this->_clean($m[1]); 
     66                  $contents .= $this->decode($m[1]); 
    6067               } 
    6168               if (preg_match( '/<p class="refpurpose dc-title">(.*?)<\/p>/mis', $tmp, $m)) { 
    62                   $m = explode( '-', $this->_clean($m[1]), 2); 
     69                  $m = explode( '-', $this->decode($m[1]), 2); 
    6370                  $contents .= ' - '.$m[1]; 
    6471               } 
    6572               $contents = trim(str_replace(' ,', ',', $contents)); 
    66             } 
    67             else { 
    68                   $contents = 'Nothing found'; 
     73            } else { 
     74                $this->errorStatus = false; 
     75                $contents = 'Nothing found'; 
    6976            } 
    7077            $this->cache[$function] = $contents; 
     
    7279        $this->doPrivmsg($this->event->getSource(), $this->cache[$function]); 
    7380    } 
     81 
     82    /** 
     83     * Custom error handler meant to handle 404 errors and such 
     84     */ 
     85    public function onPhpError($errno, $errstr, $errfile, $errline) 
     86    { 
     87        if ($errno === E_WARNING) { 
     88            // Check to see if there was HTTP warning while connecting to the site 
     89            if (preg_match('{HTTP/1\.[01] ([0-9]{3})}i', $errstr, $m)) { 
     90                $this->errorStatus = true; 
     91                $this->debug('PHP Warning:  ' . $errstr . 'in ' . $errfile . ' on line ' . $errline); 
     92                return true; 
     93            // Safely ignore these SSL warnings so they don't appear in the log 
     94            } else if (stripos($errstr, 'SSL: fatal protocol error in') !== false || 
     95                       stripos($errstr, 'failed to open stream') !== false || 
     96                       stripos($errstr, 'HTTP request failed') !== false || 
     97                       stripos($errstr, 'SSL: An existing connection was forcibly closed by the remote host') !== false || 
     98                       stripos($errstr, 'Failed to enable crypto in') !== false || 
     99                       stripos($errstr, 'SSL: An established connection was aborted by the software in your host machine') !== false || 
     100                       stripos($errstr, 'SSL operation failed with code') !== false || 
     101                       stripos($errstr, 'unable to connect to') !== false) { 
     102                $this->errorStatus = true; 
     103                $this->debug('PHP Warning:  ' . $errstr . 'in ' . $errfile . ' on line ' . $errline); 
     104                return true; 
     105            } 
     106        } 
     107        return false; 
     108    } 
    74109}