File: RewriteHelper.class

package info (click to toggle)
gallery2 2.1.2-2.0.etch.1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 46,156 kB
  • ctags: 5,615
  • sloc: php: 75,481; xml: 1,683; sh: 731; perl: 479; sql: 202; makefile: 54
file content (315 lines) | stat: -rw-r--r-- 10,567 bytes parent folder | download | duplicates (2)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
/*
 * $RCSfile: RewriteHelper.class,v $
 *
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2006 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */
/**
 * @package Rewrite
 * @version $Revision: 1.33 $ $Date: 2006/01/10 04:42:16 $
 * @author Douglas Cau <douglas@cau.se>
 */

/* General status codes */
define('REWRITE_STATUS_OK', 0);
define('REWRITE_STATUS_BAD_KEYWORD', 1);
define('REWRITE_STATUS_MULTISITE', 2);
define('REWRITE_STATUS_DUPE_SHORT_URL', 3);
define('REWRITE_STATUS_INVALID_PATTERN', 4);
define('REWRITE_STATUS_EMPTY_VALUE', 5);

/**
 * A helper class for the URL Rewrite module
 *
 * @package Rewrite
 * @subpackage Classes
 */
class RewriteHelper {

    /**
     * Load and initialize the rewrite plugin. If no plugin has been configured yet
     * it returns a GalleryStatus ERROR_MISSING_VALUE.
     *
     * @param boolean true if we need a new instance
     * @return array object GalleryStatus a status code
     *               object RewritePlugin a loaded parser
     * @static
     */
    function getRewriteParser($new=false) {
	global $gallery;
	static $rewriteParser;
	$platform =& $gallery->getPlatform();

	if (!isset($rewriteParser) || $new) {
	    list ($ret, $rewriteParserId) = GalleryCoreApi::getPluginParameter(
		'module', 'rewrite', 'parserId');
	    if ($ret) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	    if (empty($rewriteParserId)) {
		return array(GalleryCoreApi::error(ERROR_MISSING_VALUE, __FILE__, __LINE__), null);
	    }

	    $path = 'modules/rewrite/classes/parsers/' . $rewriteParserId . '/parser.inc';
	    if ($platform->file_exists(GalleryCoreApi::getPluginBaseDir('module', 'rewrite') .
		    $path)) {
		GalleryCoreApi::requireOnce($path);
	    }
	    $class = $rewriteParserId . 'Parser';
	    $rewriteParser = new $class();
	}

	return array(null, $rewriteParser);
    }

    /**
     * Parse active rules into an array of regular expresions for parsing URLs during page
     * requests and build an array for the url generator to use when generating short URLs.
     *
     * @param array active rules by reference
     * @param object RewriteParser
     * @param object RewriteModule (optional) passed in during activate/upgrade
     * @param array optional defenition of default and mandator flags.
     * @return array object GalleryStatus a status code
     *               int rewrite status code (REWRITE_STATUS_OK on success)
     *               array parsed patterns translated to regular expressions
     *               array short urls which will used by to generate urls
     *               array string module (on error)
     *                     int rule id (on error)
     */
    function parseActiveRules(&$activeRules, $rewriteParser, $rewriteModule=null, $flags=null) {
	global $gallery;
	$urlGenerator =& $gallery->getUrlGenerator();

    	/* Get access list information */
	list ($ret, $accessList) = GalleryCoreApi::getPluginParameter(
	    'module', 'rewrite', 'accessList');
	if ($ret) {
	    return array($ret->wrap(__FILE__, __LINE__), null, null, null, null);
	}
	$accessList = unserialize($accessList);
	$ourHostNames[$urlGenerator->getHostName()] = true;
	$ourHostNames[$urlGenerator->getHostName(true)] = true;
	$accessList = array_merge($accessList, array_keys($ourHostNames));

	$regexRules = array();
	$shortUrls = array();
	foreach (array_keys($activeRules) as $moduleId) {
	    if ($moduleId == 'rewrite' && isset($rewriteModule)) {
		/* Avoid PLUGIN_VERSION_MISMATCH during upgrade by passing in module */
		$module = $rewriteModule;
	    } else {
		list($ret, $module) = GalleryCoreApi::loadPlugin('module', $moduleId);
		if ($ret) {
		    if ($ret->getErrorCode() & ERROR_PLUGIN_VERSION_MISMATCH) {
			/*
			 * Add CONFIGURATION_REQUIRED code to more gracefully abort upgrade
			 * if a dependent module for one of our rules also needs upgrading.
			 */
			$ret->addErrorCode(ERROR_CONFIGURATION_REQUIRED);
		    }
		    return array($ret->wrap(__FILE__, __LINE__), null, null, null, null);
		}
	    }

	    $rules = $module->getRewriteRules();
	    foreach (array_keys($activeRules[$moduleId]) as $ruleId) {
		/* Make sure this rule still exists, if not silently continue. */
		if (!isset($rules[$ruleId])) {
		    unset($activeRules[$moduleId][$ruleId]);
		    continue;
		}

		/* Make sure this parser supports this kind of rule */
		if (!$rewriteParser->isValidRule($rules[$ruleId],
			$activeRules[$moduleId][$ruleId])) {
		    return array(null, REWRITE_STATUS_INVALID_PATTERN,
			null, null, array($moduleId, $ruleId));
		}

	    	/* Save the pattern for the URL generator to use */
		if (isset($rules[$ruleId]['match'])) {
		    $match = join('|', $rules[$ruleId]['match']);
		    if (isset($shortUrls[$match])) {
			return array(null, REWRITE_STATUS_DUPE_SHORT_URL,
			    null, null, array($match));
		    }

		    $shortUrls[$match] = array(
			'pattern' => $activeRules[$moduleId][$ruleId]);

		    /* Get custom function information */
		    if (isset($rules[$ruleId]['keywords'])) {
			foreach ($rules[$ruleId]['keywords'] as $key => $info) {
			    if (isset($info['function'])) {
				$shortUrls[$match]['functions'][$key] = $info['function'];
			    }
			}
		    }

		    /* Save the onLoad handler for this rule */
		    if (isset($rules[$ruleId]['onLoad'])) {
			$shortUrls[$match]['onLoad'] = $rules[$ruleId]['onLoad'];
		    }
		}

		/* Parse the pattern and create regular expressions with conditions */
		$settings = array();
		$keywords = array('itemId' => array('pattern' => '([0-9]+)'));

		/* Add custom keywords to the list of allowed keywords */
		if (isset($rules[$ruleId]['keywords'])) {
		    $keywords = array_merge($keywords, $rules[$ruleId]['keywords']);
		}

		/* Restrict this rule to given query string conditions */
		if (!empty($rules[$ruleId]['restrict'])) {
		    foreach ($rules[$ruleId]['restrict'] as $key => $value) {
			$settings['restrict'][] = $key . '=' . $value;
		    }

		    $settings['exempt'] = array();
		    /* Exempt request from the following hosts */
		    if  (isset($rules[$ruleId]['exemptReferer'])) {
			$settings['exempt'] = $accessList;
		    }
		}

		/* Build the list of flags to apply to this rule. */
		$settings['flags'] = isset($flags) ? $flags['default'] : array();
		if (isset($rules[$ruleId]['flags']) && isset($flags)) {
		    $settings['flags'] = array_merge($rules[$ruleId]['flags'], $flags['mandatory']);
		}

		/* Make sure that there's no subrequest made when we match by the query string */
		if (isset($settings['restrict']) && !in_array('L', $settings['flags'])) {
		    $settings['flags'][] = 'L';
		}
		$settings['flags'] = join(',', $settings['flags']);

		/* Build the query string to map the request on to */
		$queryString = array();
		if (isset($rules[$ruleId]['match'])) {
		    $queryString = $rules[$ruleId]['match'];
		}
		if (!empty($rules[$ruleId]['queryString'])) {
		    $queryString = array_merge($queryString, $rules[$ruleId]['queryString']);
		}

		/* Parse the rule */
		list ($ret, $code) = RewriteHelper::_parseRule($activeRules[$moduleId][$ruleId],
		    $keywords, $queryString, $regexRules, $settings);
		if ($ret) {
		    return array($ret->wrap(__FILE__, __LINE__), null, null, null, null);
		}
		if ($code != REWRITE_STATUS_OK) {
		    return array(null, $code, null, null,
			array($moduleId, $ruleId));
		}
	    }
	}

	usort($regexRules, array('RewriteHelper', '_sortRules'));
	return array(null, REWRITE_STATUS_OK, $regexRules, $shortUrls, null);
    }

    /**
     * Replaces all keywords with an appropriate pattern and append to $regexRules.
     *
     * @param string url pattern
     * @param array of keywords => regular expresion for the htaccess file
     * @param array of query string params (key => value)
     * @param array of parsed rules
     * @param array of settings
     * @access private
     */
    function _parseRule($pattern, $keywords, $queryString, &$regexRules, $settings) {
	$reference = 1;
	preg_match_all('/\%([^\%]+)\%/', $pattern, $regs);

	foreach ($regs[1] as $keyword) {
	    if (!isset($keywords[$keyword]['pattern'])) {
		return array(null, REWRITE_STATUS_BAD_KEYWORD);
	    }

	    $pattern = str_replace('%' . $keyword . '%',
		$keywords[$keyword]['pattern'], $pattern);

	    if (!isset($keywords[$keyword]['ignore'])) {
		$queryString[$keyword] = '$' . $reference;
	    }
	    $reference++;
	}

	$args = array();
	foreach ($queryString as $key => $value) {
	    $args[] = GalleryUtilities::prefixFormVariable($key) . '=' . $value;
	}

	if (!empty($pattern)) {
	    $regexRules[] = array(
		    'pattern' => $pattern,
		    'queryString' => join('&', $args),
		    'settings' => $settings);
	}

	return array(null, REWRITE_STATUS_OK);
    }

    /**
     * Comparison function used to order rewrite rules.
     *
     * @access private
     */
    function _sortRules($a, $b) {
	if (isset($a['settings']['restrict']) || isset($b['settings']['restrict'])) {
	    if (isset($a['settings']['restrict']) && isset($b['settings']['restrict'])) {
		$diff = sizeof($a['settings']['restrict']) - sizeof($b['settings']['restrict']);
		return $diff;
	    }

	    if (isset($a['settings']['restrict'])) {
		return -1;
	    }

	    return 1;
	}

	/* Regular expression in both */
	if (strpos($a['pattern'], '(') !== false && strpos($b['pattern'], '(') !== false) {
	    if (strpos($a['pattern'], '(') == strpos($b['pattern'], '(')) {
		if (strlen($a['pattern']) == strlen($b['pattern'])) {
		    return 0;
		}

		return (strlen($a['pattern']) < strlen($b['pattern'])) ? 1 : -1;
	    }

	    return (strpos($a['pattern'], '(') < strpos($b['pattern'], '(')) ? 1 : -1;
	}
	if (strpos($a['pattern'], '(') !== false) {
	    return 1;
	}
	if (strpos($b['pattern'], '(') !== false) {
	    return -1;
	}

	return (strlen($a['pattern']) < strlen($b['pattern'])) ? 1 : -1;
    }
}
?>