File: class.dc.store.reader.php

package info (click to toggle)
dotclear 2.6.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 8,420 kB
  • sloc: php: 54,270; sql: 1,290; sh: 213; xml: 173; makefile: 158
file content (273 lines) | stat: -rw-r--r-- 6,172 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
# -- BEGIN LICENSE BLOCK ---------------------------------------
#
# This file is part of Dotclear 2.
#
# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
# Licensed under the GPL version 2.0 license.
# See LICENSE file or
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# -- END LICENSE BLOCK -----------------------------------------
if (!defined('DC_RC_PATH')) { return; }

/**
@ingroup DC_CORE
@brief Repository modules XML feed reader
@since 2.6

Provides an object to parse XML feed of modules from repository.
This class extends clearbricks netHttp class.
*/
class dcStoreReader extends netHttp
{
	/** @var	string	User agent used to query repository */
	protected $user_agent = 'DotClear.org RepoBrowser/0.1';
	/** @var	integer	User agent used to query repository */
	protected $timeout = 5;
	/** @var	array	 HTTP Cache validators */
	protected $validators = null;
	/** @var	string	Cache temporary directory */
	protected $cache_dir = null;
	/** @var	string	Cache file prefix */
	protected $cache_file_prefix = 'dcrepo';
	/** @var	integer	Cache TTL */
	protected $cache_ttl = '-30 minutes';
	/** @var	boolean	'Cache' TTL on server failed */
	protected $cache_touch_on_fail = true;
	/** @var	boolean	Force query server */
	protected $force = false;

	/**
	 * Constructor.
	 *
	 * Bypass first argument of clearbricks netHttp constructor.
	 */
	public function __construct()
	{
		parent::__construct('');
		$this->setUserAgent(sprintf('Dotclear/%s)', DC_VERSION));
	}

	/**
	 * Parse modules feed.
	 *
	 * @param	string	$url		XML feed URL
	 * @return	object	dcStore instance
	 */
	public function parse($url)
	{
		$this->validators = array();

		if ($this->cache_dir) {
			return $this->withCache($url);
		}
		elseif (!$this->getModulesXML($url) || $this->getStatus() != '200') {
			return false;
		}

		return new dcStoreParser($this->getContent());
	}

	/**
	 * Quick parse modules feed.
	 *
	 * @param	string	$url		XML feed URL
	 * @param	string	$cache_dir	Cache directoy or null for no cache
	 * @param	boolean	$force		Force query repository
	 * @return	object	Self instance
	 */
	public static function quickParse($url, $cache_dir=null, $force=false)
	{
		$parser = new self();
		if ($cache_dir) {
			$parser->setCacheDir($cache_dir);
		}
		if ($force) {
			$parser->setForce($force);
		}

		return $parser->parse($url);
	}

	/**
	 * Set cache directory.
	 *
	 * @param	string	$dir		Cache directory
	 * @return	boolean	True if cache dierctory is useable
	 */
	public function setCacheDir($dir)
	{
		$this->cache_dir = null;

		if (!empty($dir) && is_dir($dir) && is_writeable($dir)) {
			$this->cache_dir = $dir;
			return true;
		}

		return false;
	}

	/**
	 * Set cache TTL.
	 *
	 * @param	string	$str		Cache TTL
	 */
	public function setCacheTTL($str)
	{
		$str = trim($str);

		if (!empty($str)) {
			$this->cache_ttl = substr($str, 0, 1) == '-' ? $str : '-'.$str;
		}
	}

	/**
	 * Set force query repository.
	 *
	 * @param	boolean	$force	True to force query
	 */
	public function setForce($force)
	{
		$this->force = $force;
	}

	/**
	 * Get repository XML feed URL content.
	 *
	 * @param	string	$url		XML feed URL
	 * @return	string	Feed content
	 */
	protected function getModulesXML($url)
	{
		if (!self::readURL($url, $ssl, $host, $port, $path, $user, $pass)) {
			return false;
		}
		$this->setHost($host, $port);
		$this->useSSL($ssl);
		$this->setAuthorization($user, $pass);

		try {
			return $this->get($path);
		}
		catch(Exception $e) {
			// @todo Log error when repository query fail
			return false;
		}
	}

	/**
	 * Get repository modules list using cache.
	 *
	 * @param	string	$url		XML feed URL
	 * @return	array	Feed content or False on fail
	 */
	protected function withCache($url)
	{
		$url_md5 = md5($url);
		$cached_file = sprintf('%s/%s/%s/%s/%s.ser',
			$this->cache_dir,
			$this->cache_file_prefix,
			substr($url_md5,0,2),
			substr($url_md5,2,2),
			$url_md5
		);

		$may_use_cached = false;

		# Use cache file ?
		if (@file_exists($cached_file) && !$this->force) {
			$may_use_cached = true;
			$ts = @filemtime($cached_file);
			if ($ts > strtotime($this->cache_ttl)) {
				# Direct cache
				return unserialize(file_get_contents($cached_file));
			}
			$this->setValidator('IfModifiedSince', $ts);
		}

		# Query repository
		if (!$this->getModulesXML($url)) {
			if ($may_use_cached) {
				# Touch cache TTL even if query failed ?
				if ($this->cache_touch_on_fail) {
					@files::touch($cached_file);
				}
				# Connection failed - fetched from cache
				return unserialize(file_get_contents($cached_file));
			}
			return false;
		}

		# Parse response
		switch ($this->getStatus())
		{
			# Not modified, use cache
			case '304':
				@files::touch($cached_file);
				return unserialize(file_get_contents($cached_file));
			# Ok, parse feed
			case '200':
				if ($modules = new dcStoreParser($this->getContent())) {
					try {
						files::makeDir(dirname($cached_file), true);
					}
					catch (Exception $e) {
						return $modules;
					}

					if (($fp = @fopen($cached_file, 'wb'))) {
						fwrite($fp, serialize($modules));
						fclose($fp);
						files::inheritChmod($cached_file);
					}
					return $modules;
				}
		}

		return false;
	}

	/**
	 * Prepare query.
	 *
	 * @return	array	Query headers
	 */
	protected function buildRequest()
	{
		$headers = parent::buildRequest();

		# Cache validators
		if (!empty($this->validators)) {
			if (isset($this->validators['IfModifiedSince'])) {
				$headers[] = 'If-Modified-Since: '.$this->validators['IfModifiedSince'];
			}
			if (isset($this->validators['IfNoneMatch'])) {
				if (is_array($this->validators['IfNoneMatch'])) {
					$etags = implode(',', $this->validators['IfNoneMatch']);
				}
				else {
					$etags = $this->validators['IfNoneMatch'];
				}
				$headers[] = '';
			}
		}

		return $headers;
	}

	/**
	 * Tweak query cache validator.
	 *
	 * @param	string	$key		Validator key
	 * @param	string	$value		Validator value
	 */
	private function setValidator($key, $value)
	{
		if ($key == 'IfModifiedSince') {
			$value = gmdate('D, d M Y H:i:s', $value).' GMT';
		}

		$this->validators[$key] = $value;
	}
}