1: <?php
2: /**
3: * Klutz_Driver:: defines an API for storing and retrieving the comic images
4: *
5: * @author Marcus I. Ryan <marcus@riboflavin.net>
6: * @since Klutz 0.1
7: * @package Klutz
8: */
9: class Klutz_Driver
10: {
11: /**
12: * Gets a concrete Klutz_Driver instance.
13: *
14: * @param string $driver The type of concrete Klutz_Driver subclass to
15: * return. The code for the driver is dynamically
16: * included.
17: *
18: * @param array $params A hash containing any additional configuration or
19: * connection parameters a subclass might need
20: *
21: * @return object Klutz_Driver The newly created concrete instance, or
22: * false on error.
23: */
24: function factory($driver = null, $params = null)
25: {
26: if (is_null($driver)) {
27: $driver = $GLOBALS['conf']['storage']['driver'];
28: }
29: $driver = ucfirst(basename($driver));
30:
31: if (is_null($params)) {
32: $params = Horde::getDriverConfig('storage', $driver);
33: }
34:
35: $class = 'Klutz_Driver_' . $driver;
36: if (class_exists($class)) {
37: return new $class($params);
38: }
39: return new Klutz_Driver($params);
40: }
41:
42: /**
43: * Gets a list of the dates for which we have comics between $oldest and
44: * $newest. In the default driver (no backend) this is just a list of
45: * all dates between $oldest and $newest.
46: *
47: * @param timestamp $date The reference date (default today)
48: * @param timestamp $oldest The earliest possible date to return (default
49: * first of the month)
50: * @param timestamp $newest The latest possible date to return (default
51: * last date of the month)
52: *
53: * @return array timestamps Dates between $oldest and $newest we have
54: * comics for
55: */
56: function listDates($date = null, $oldest = null, $newest = null)
57: {
58: if (is_null($date)) {
59: $date = mktime(0, 0, 0);
60: }
61:
62: $dateparts = getdate($date);
63:
64: // Default to showing only the month specified.
65: if (is_null($oldest)) {
66: $oldest = mktime(0, 0, 0, $dateparts['mon'], 1, $dateparts['year']);
67: }
68: if (is_null($newest)) {
69: $newest = mktime(0, 0, 0, $dateparts['mon'] + 1, 0, $dateparts['year']);
70: $newest = min($newest, mktime(0, 0, 0));
71: }
72:
73: $return = array();
74: $i = date('j', $oldest);
75: $loopMonth = date('n', $oldest);
76: $loopYear = date('Y', $oldest);
77: $loopStamp = mktime(0, 0, 0, $loopMonth, $i, $loopYear);
78: while ($loopStamp <= $newest) {
79: $return[] = $loopStamp;
80: $loopStamp = mktime(0, 0, 0, $loopMonth, ++$i, $loopYear);
81: }
82:
83: return $return;
84: }
85:
86: /**
87: * Get the image dimensions for the requested image.
88: *
89: * The image is not stored locally so this function returns an
90: * empty string. Performance hit is too expensive to make this
91: * worth it.
92: *
93: * @param string $index The index of the comic to check
94: * @param timestamp $date The date of the comic to check (default today)
95: *
96: * @return string Attributes for an <img> tag giving height and width
97: */
98: function imageSize($index, $date = null)
99: {
100: // Getting the image size is too expensive for the benefit
101: // when using this driver.
102: return '';
103: }
104:
105: /**
106: * Find out if we already have a local copy of this image.
107: *
108: * Even though we never actually store a local copy, pretend.
109: *
110: * @param string $index The index of the comic to check
111: * @param timestamp $date The date of the comic to check (default today)
112: *
113: * @return boolean True
114: */
115: function imageExists($index, $date = null)
116: {
117: return true;
118: }
119:
120: /**
121: * Store an image for later retrieval
122: *
123: * Even though we never actually store a local copy, pretend.
124: *
125: * @param string $index The index of the comic to retrieve
126: * @param string $image Raw (binary) image data to store
127: * @param timestamp $data Date to store it under (default today)
128: *
129: * @return boolean True
130: */
131: function storeImage($index, $image, $date = null)
132: {
133: return true;
134: }
135:
136: /**
137: * Retrieve an image from storage. Since there is no local storage
138: * this will actually call for the fetching.
139: *
140: * @param string $index The index of the comic to retrieve
141: * @param timestamp $date The date for which we want $comic
142: *
143: * @return mixed If the image exists locally, return a Klutz_Image object.
144: * If it doesn't, return a string with the URL pointing to
145: * the comic.
146: */
147: function retrieveImage($index, $date = null)
148: {
149: if (is_null($date)) {
150: $date = mktime(0, 0, 0);
151: }
152:
153: // For this driver, we grab the image on the fly
154: $comic = $GLOBALS['klutz']->comicObject($index);
155: var_dump($comic);
156: if (is_null($comic->referer) &&
157: is_null($comic->agent) &&
158: is_null($comic->user) &&
159: is_null($comic->pass) &&
160: count($comic->cookies) == 0 &&
161: count($comic->headers) == 0) {
162: return $comic->fetchURL($date);
163: } else {
164: return $comic->fetchImage($date);
165: }
166: }
167: }
168: