1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11: class Klutz_Comic_Search extends Klutz_Comic
12: {
13: 14: 15: 16: 17:
18: var $search = null;
19:
20: 21: 22: 23: 24: 25: 26:
27: function Klutz_Comic_search($comic)
28: {
29:
30:
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:
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: 55: 56: 57: 58: 59: 60: 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:
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: 126: 127: 128: 129: 130: 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: