1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
12 | if (!defined('DC_RC_PATH')) { return; } |
---|
13 | |
---|
14 | /** |
---|
15 | @ingroup DC_CORE |
---|
16 | @brief Repository modules feed reader |
---|
17 | |
---|
18 | Provides an object to parse feed of modules from repository. |
---|
19 | */ |
---|
20 | class dcRepositoryReader extends netHttp |
---|
21 | { |
---|
22 | protected $user_agent = 'DotClear.org RepoBrowser/0.1'; |
---|
23 | protected $timeout = 5; |
---|
24 | protected $validators = null; ///< <b>array</b> HTTP Cache validators |
---|
25 | protected $cache_dir = null; ///< <b>string</b> Cache temporary directory |
---|
26 | protected $cache_file_prefix = 'dcrepo'; ///< <b>string</b> Cache file prefix |
---|
27 | protected $cache_ttl = '-30 minutes'; ///< <b>string</b> Cache TTL |
---|
28 | protected $cache_touch_on_fail = true; |
---|
29 | protected $force = false; |
---|
30 | |
---|
31 | public function __construct() |
---|
32 | { |
---|
33 | parent::__construct(''); |
---|
34 | $this->setUserAgent(sprintf('Dotclear/%s)', DC_VERSION)); |
---|
35 | } |
---|
36 | |
---|
37 | public function parse($url) |
---|
38 | { |
---|
39 | $this->validators = array(); |
---|
40 | if ($this->cache_dir) |
---|
41 | { |
---|
42 | return $this->withCache($url); |
---|
43 | } |
---|
44 | else |
---|
45 | { |
---|
46 | if (!$this->getModulesXML($url)) { |
---|
47 | return false; |
---|
48 | } |
---|
49 | |
---|
50 | if ($this->getStatus() != '200') { |
---|
51 | return false; |
---|
52 | } |
---|
53 | |
---|
54 | return new dcRepositoryParser($this->getContent()); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | public static function quickParse($url, $cache_dir=null, $force=false) |
---|
59 | { |
---|
60 | $parser = new self(); |
---|
61 | if ($cache_dir) { |
---|
62 | $parser->setCacheDir($cache_dir); |
---|
63 | } |
---|
64 | if ($force) { |
---|
65 | $parser->setForce($force); |
---|
66 | } |
---|
67 | |
---|
68 | return $parser->parse($url); |
---|
69 | } |
---|
70 | |
---|
71 | public function setCacheDir($dir) |
---|
72 | { |
---|
73 | $this->cache_dir = null; |
---|
74 | |
---|
75 | if (!empty($dir) && is_dir($dir) && is_writeable($dir)) |
---|
76 | { |
---|
77 | $this->cache_dir = $dir; |
---|
78 | return true; |
---|
79 | } |
---|
80 | |
---|
81 | return false; |
---|
82 | } |
---|
83 | |
---|
84 | public function setCacheTTL($str) |
---|
85 | { |
---|
86 | $str = trim($str); |
---|
87 | if (!empty($str)) |
---|
88 | { |
---|
89 | if (substr($str, 0, 1) != '-') { |
---|
90 | $str = '-'.$str; |
---|
91 | } |
---|
92 | $this->cache_ttl = $str; |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | public function setForce($force) |
---|
97 | { |
---|
98 | $this->force = $force; |
---|
99 | } |
---|
100 | |
---|
101 | protected function getModulesXML($url) |
---|
102 | { |
---|
103 | if (!self::readURL($url, $ssl, $host, $port, $path, $user, $pass)) { |
---|
104 | return false; |
---|
105 | } |
---|
106 | $this->setHost($host, $port); |
---|
107 | $this->useSSL($ssl); |
---|
108 | $this->setAuthorization($user, $pass); |
---|
109 | |
---|
110 | return $this->get($path); |
---|
111 | } |
---|
112 | |
---|
113 | protected function withCache($url) |
---|
114 | { |
---|
115 | $url_md5 = md5($url); |
---|
116 | $cached_file = sprintf('%s/%s/%s/%s/%s.ser', |
---|
117 | $this->cache_dir, |
---|
118 | $this->cache_file_prefix, |
---|
119 | substr($url_md5,0,2), |
---|
120 | substr($url_md5,2,2), |
---|
121 | $url_md5 |
---|
122 | ); |
---|
123 | |
---|
124 | $may_use_cached = false; |
---|
125 | |
---|
126 | if (@file_exists($cached_file) && !$this->force) |
---|
127 | { |
---|
128 | $may_use_cached = true; |
---|
129 | $ts = @filemtime($cached_file); |
---|
130 | if ($ts > strtotime($this->cache_ttl)) |
---|
131 | { |
---|
132 | # Direct cache |
---|
133 | return unserialize(file_get_contents($cached_file)); |
---|
134 | } |
---|
135 | $this->setValidator('IfModifiedSince', $ts); |
---|
136 | } |
---|
137 | |
---|
138 | if (!$this->getModulesXML($url)) |
---|
139 | { |
---|
140 | if ($may_use_cached) |
---|
141 | { |
---|
142 | if ($this->cache_touch_on_fail) { |
---|
143 | @files::touch($cached_file); |
---|
144 | } |
---|
145 | # connection failed - fetched from cache |
---|
146 | return unserialize(file_get_contents($cached_file)); |
---|
147 | } |
---|
148 | return false; |
---|
149 | } |
---|
150 | |
---|
151 | switch ($this->getStatus()) |
---|
152 | { |
---|
153 | case '304': |
---|
154 | @files::touch($cached_file); |
---|
155 | return unserialize(file_get_contents($cached_file)); |
---|
156 | case '200': |
---|
157 | if ($modules = new dcRepositoryParser($this->getContent())) |
---|
158 | { |
---|
159 | try { |
---|
160 | files::makeDir(dirname($cached_file), true); |
---|
161 | } catch (Exception $e) { |
---|
162 | return $modules; |
---|
163 | } |
---|
164 | |
---|
165 | if (($fp = @fopen($cached_file, 'wb'))) |
---|
166 | { |
---|
167 | fwrite($fp, serialize($modules)); |
---|
168 | fclose($fp); |
---|
169 | files::inheritChmod($cached_file); |
---|
170 | } |
---|
171 | return $modules; |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | return false; |
---|
176 | } |
---|
177 | |
---|
178 | protected function buildRequest() |
---|
179 | { |
---|
180 | $headers = parent::buildRequest(); |
---|
181 | |
---|
182 | # Cache validators |
---|
183 | if (!empty($this->validators)) |
---|
184 | { |
---|
185 | if (isset($this->validators['IfModifiedSince'])) { |
---|
186 | $headers[] = 'If-Modified-Since: '.$this->validators['IfModifiedSince']; |
---|
187 | } |
---|
188 | if (isset($this->validators['IfNoneMatch'])) { |
---|
189 | if (is_array($this->validators['IfNoneMatch'])) { |
---|
190 | $etags = implode(',', $this->validators['IfNoneMatch']); |
---|
191 | } else { |
---|
192 | $etags = $this->validators['IfNoneMatch']; |
---|
193 | } |
---|
194 | $headers[] = ''; |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | return $headers; |
---|
199 | } |
---|
200 | |
---|
201 | private function setValidator($key, $value) |
---|
202 | { |
---|
203 | if ($key == 'IfModifiedSince') { |
---|
204 | $value = gmdate('D, d M Y H:i:s', $value).' GMT'; |
---|
205 | } |
---|
206 | |
---|
207 | $this->validators[$key] = $value; |
---|
208 | } |
---|
209 | } |
---|