1: <?php
2: /**
3: * Klutz_Comic_Direct Class.
4: *
5: * This class takes uses $comic->url to get an image directly.
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_Direct extends Klutz_Comic
12: {
13: /**
14: * Constructor - Create an object that can be used to retrieve a comic
15: * directly from a URL passed in (using substitutions as necessary).
16: *
17: * @param string $comic Index for the comic
18: */
19: function Klutz_Comic_direct($comic)
20: {
21: // Call the parent constructor...this should leave $comic with
22: // just the parameters we need for fetching (if any are left).
23: $par = get_parent_class($this);
24: $this->$par($comic);
25:
26: if (is_null($this->subs)) {
27: $this->subs = array("url");
28: }
29:
30: // assuming $this->url is set, so are we...
31: }
32:
33: /**
34: * Do all that is necessary to get the final URL from which the comic
35: * will be fetched. Instead of returning the comic, return the URL
36: * pointing to that comic.
37: *
38: * @param timestamp $date Date of the comic to retrieve (default today)
39: *
40: * @return string URL of the comic image
41: */
42: function fetchURL($date = null)
43: {
44: if (is_null($date)) {
45: $date = mktime(0, 0, 0);
46: }
47: $offset = $this->getOverride('offset', $date);
48: $d = getdate($date);
49: $date = mktime(0, 0, 0, $d['mon'], $d['mday'] - $offset, $d['year']);
50:
51: $url = $this->getOverride('url', $date);
52: if (in_array('url', $this->getOverride('subs', $date))) {
53: $url = $this->substitute($url, $date);
54: }
55:
56: return $url;
57: }
58:
59: /**
60: * Fetch the actual image
61: *
62: * @param timestamp $date The date to retrieve the comic for (default
63: * today).
64: *
65: * @return mixed Klutz_Image on success, false otherwise.
66: */
67: function &fetchImage($date = null)
68: {
69: if (is_null($date)) {
70: $date = mktime(0, 0, 0);
71: }
72: $offset = $this->getOverride('offset', $date);
73: $d = getdate($date);
74: $date = mktime(0, 0, 0, $d['mon'], $d['mday'] - $offset, $d['year']);
75:
76: $url = $this->getOverride('url', $date);
77: if (in_array('url', $this->getOverride('subs', $date))) {
78: $url = $this->substitute($url, $date);
79: }
80:
81: $this->_initHTTP($date, $url);
82: $this->http->setURL($url);
83: $this->http->sendRequest();
84:
85: $image = &new Klutz_Image($this->http->getResponseBody());
86: if (is_null($image) || is_null($image->type)) {
87: $image = false;
88: }
89:
90: return $image;
91: }
92:
93: }
94: