Overview

Packages

  • Horde
    • Block
  • Klutz
  • None

Classes

  • Klutz
  • Klutz_Comic
  • Klutz_Comic_Bysize
  • Klutz_Comic_Direct
  • Klutz_Comic_Search
  • Klutz_Driver
  • Klutz_Driver_File
  • Klutz_Driver_Sql
  • Klutz_Image
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Klutz_Comic_Search Class.
  4:  *
  5:  * This class takes uses $comic->url to get an image searchly.
  6:  * This is, of course, the most efficient as it takes one fetch.
  7:  *
  8:  * @author Marcus I. Ryan <marcus@riboflavin.net>
  9:  * @package Klutz
 10:  */
 11: class Klutz_Comic_Search extends Klutz_Comic
 12: {
 13:     /**
 14:      * Once set, an array of preg searches to perform to find the comic image
 15:      *
 16:      * @var array
 17:      */
 18:     var $search = null;
 19: 
 20:     /**
 21:      * Constructor - Create an object that can be used to retrieve a comic
 22:      * by using a preg_match to reliably get the comic URL from an html
 23:      * document.
 24:      *
 25:      * @param string $comic  Index for the comic
 26:      */
 27:     function Klutz_Comic_search($comic)
 28:     {
 29:         // call the parent constructor...this should leave $comic with just
 30:         // the parameters we need for fetching (if any are left)
 31: 
 32:         $par = get_parent_class($this);
 33:         $this->$par($comic);
 34: 
 35:         if (is_null($this->subs)) {
 36:             $this->subs = array('url');
 37:         }
 38: 
 39:         // Hopefully we have at least one search pattern
 40:         if (is_array($comic['search']) && count($comic['search']) > 0) {
 41:             $this->search = $comic['search'];
 42:             unset($comic['search']);
 43:         } elseif (is_string($comic['search']) && !empty($comic['search'])) {
 44:             $this->search = array($comic['search']);
 45:             unset($comic['search']);
 46:         } else {
 47:             return null;
 48:         }
 49: 
 50:         $this->search = $this->_prepareSearch($this->search);
 51:     }
 52: 
 53:     /**
 54:      * Do all that is necessary to get the final URL from which the comic
 55:      * will be fetched.  Instead of returning the comic, return the URL
 56:      * pointing to that comic.
 57:      *
 58:      * @param timestamp $date  Date of the comic to retrieve (default today)
 59:      *
 60:      * @return string  URL of the comic image
 61:      */
 62:     function fetchURL($date = null)
 63:     {
 64:         if (is_null($date)) {
 65:             $date = mktime(0, 0, 0);
 66:         }
 67:         $offset = $this->getOverride('offset', $date);
 68:         $d = getdate($date);
 69:         $date = mktime(0, 0, 0, $d['mon'], $d['mday'] - $offset, $d['year']);
 70: 
 71:         $url = $this->getOverride('url', $date);
 72:         if (in_array('url', $this->getOverride('subs', $date))) {
 73:             $url = $this->substitute($url, $date);
 74:         }
 75: 
 76:         $this->_initHTTP($date, $url);
 77: 
 78:         // loop through the array of searches to get a final URL
 79:         foreach ($this->getOverride('search', $date,
 80:                                     array($this, '_prepareSearch')) as $search) {
 81:             if (in_array('search', $this->getOverride('subs', $date))) {
 82:                 $search = $this->substitute($search, $date);
 83:             }
 84: 
 85:             $this->http->setURL($url);
 86:             $this->http->sendRequest();
 87:             if (is_array($search)) {
 88:                 $text = $this->http->getResponseBody();
 89:                 foreach ($search as $s) {
 90:                     $num_matches = preg_match($s, $text, $matches);
 91:                     if (isset($matches[1])) {
 92:                         $text = $matches[1];
 93:                     } elseif ($num_matches > 0) {
 94:                         $text = $matches[0];
 95:                     } else {
 96:                         break;
 97:                     }
 98:                 }
 99:             } else {
100:                 preg_match($search, $this->http->getResponseBody(), $matches);
101:             }
102:             if (!isset($matches[1]) && $this->days != 'random') {
103:                 $msg  = "URL: $url";
104:                 $msg .= "\nSEARCH: " . print_r($search, true);
105:                 $msg .= "\nHTML: " . $this->http->getResponseBody();
106:                 Horde::logMessage($msg, __FILE__, __LINE__, PEAR_LOG_DEBUG);
107:                 return false;
108:             }
109: 
110:             if (strstr($matches[1], '://')) {
111:                 $url = $matches[1];
112:             } elseif ($matches[1][0] == '/') {
113:                 $url = preg_replace("|^(http://.*?)/.*$|", '\\1', $url);
114:                 $url .= $matches[1];
115:             } else {
116:                 $url = preg_replace("|^(http://[^?]*/).*$|", '\\1', $url);
117:                 $url .= $matches[1];
118:             }
119:         }
120: 
121:         return $url;
122:     }
123: 
124:     /**
125:      * Fetch the actual image.
126:      *
127:      * @param timestamp $date  The date to retrieve the comic for (default
128:      *                         today).
129:      *
130:      * @return mixed  Klutz_Image on success, false otherwise.
131:      */
132:     function &fetchImage($date = null)
133:     {
134:         $url = $this->fetchURL($date);
135:         if ($url === false) {
136:             $false = false;
137:             return $false;
138:         }
139: 
140:         $this->_initHTTP($date, $url);
141:         $this->http->setURL($url);
142:         $this->http->sendRequest();
143: 
144:         $image = &new Klutz_Image($this->http->getResponseBody());
145:         if (is_null($image) || is_null($image->type)) {
146:             $image = false;
147:         }
148: 
149:         return $image;
150:     }
151: 
152: }
153: 
API documentation generated by ApiGen