forked from symphonists/rest_api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.driver.php
More file actions
54 lines (39 loc) · 1.54 KB
/
extension.driver.php
File metadata and controls
54 lines (39 loc) · 1.54 KB
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
<?php
Class extension_rest_api extends Extension{
public function about(){
return array('name' => 'REST API',
'version' => '1.1.0',
'release-date' => '2011-03-28',
'author' => array(
'name' => 'Nick Dunn',
'website' => 'http://nick-dunn.co.uk')
);
}
public function uninstall(){
$htaccess = @file_get_contents(DOCROOT . '/.htaccess');
if($htaccess === FALSE) return FALSE;
$htaccess = self::__removeAPIRules($htaccess);
return @file_put_contents(DOCROOT . '/.htaccess', $htaccess);
}
public function install(){
$htaccess = @file_get_contents(DOCROOT . '/.htaccess');
if($htaccess === FALSE) return FALSE;
$token = md5(time());
// Find out if the rewrite base is another other than /
$rewrite_base = NULL;
if(preg_match('/RewriteBase\s+([^\s]+)/i', $htaccess, $match)){
$rewrite_base = trim($match[1], '/') . '/';
}
$rule = "
### START API RULES
RewriteRule ^symphony\/api(\/(.*\/?))?$ {$rewrite_base}extensions/rest_api/handler.php?url={$token}&%{QUERY_STRING} [NC,L]
### END API RULES\n\n";
$htaccess = self::__removeAPIRules($htaccess);
$htaccess = preg_replace('/RewriteRule .\* - \[S=14\]\s*/i', "RewriteRule .* - [S=14]\n{$rule}\t", $htaccess);
$htaccess = str_replace($token, '$1', $htaccess);
return @file_put_contents(DOCROOT . '/.htaccess', $htaccess);
}
private static function __removeAPIRules($htaccess){
return preg_replace('/### START API RULES(.)+### END API RULES[\n]/is', NULL, $htaccess);
}
}