-
Notifications
You must be signed in to change notification settings - Fork 17
/
extension.driver.php
74 lines (58 loc) · 2.47 KB
/
extension.driver.php
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
<?php
class extension_rest_api extends Extension{
public function getSubscribedDelegates() {
return array(
array(
'page' => '/frontend/',
'delegate' => 'FrontendPageResolved',
'callback' => 'manipulateResolvedPage'
),
array(
'page' => '/frontend/',
'delegate' => 'FrontendOutputPreGenerate',
'callback' => 'frontendOutputPreGenerate'
)
);
}
public function manipulateResolvedPage($context) {
if(!class_exists('REST_API') || (class_exists('REST_API') && !REST_API::isFrontendPageRequest())) return;
// get the page data from context
$page = $context['page_data'];
if (REST_API::getHTTPMethod() == 'get') {
$page['data_sources'] = 'rest_api_entries';
$page['events'] = null;
}
if(REST_API::getHTTPMethod() == 'post'){
$page['events'] = 'rest_api_entries';
}
$context['page_data'] = $page;
}
public function frontendOutputPreGenerate($context) {
if(class_exists('REST_API') && class_exists('REST_Entries') && REST_API::isFrontendPageRequest()) {
REST_Entries::sendOutput($context['xml']);
exit;
}
}
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());
$rule = "
### START API RULES
RewriteRule ^symphony\/api(\/(.*\/?))?$ 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);
}
}