-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontroller.php
More file actions
285 lines (234 loc) · 8.63 KB
/
controller.php
File metadata and controls
285 lines (234 loc) · 8.63 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
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
<?php defined('C5_EXECUTE') or die("Access Denied.");
/**
* concrete5 API
* This is the basic package class extended into the api package
*
* @category Api
* @package ApiCore
* @author Michael Krasnow <mnkras@gmail.com>
* @author Lucas Anderson <lucas@lucasanderson.com>
* @copyright 2011-2012 Michael Krasnow and Lucas Anderson
* @license See License.txt
* @link http://c5api.com
*/
class ApiPackage extends Package {
/**
* @var string The handle of the package
*/
protected $pkgHandle = 'api';
/**
* @var string Minimum version of concrete5 required
*/
protected $appVersionRequired = '5.6.0';
/**
* @var string Version of the package
*/
protected $pkgVersion = '0.91';
/**
* Returns the package name
*
* @return string
*/
public function getPackageName() {
return t("Api");
}
/**
* Returns the package description
*
* @return string
*/
public function getPackageDescription() {
return t("Provides an external API for remote management of your site. Allows integration into 3rd party applications.");
}
/**
* Fired by concrete5
*
* The constant "BASE_API_PATH" is defined here and is by default set to "-/api".
* We then use events and call the parseRequest method of the ApiRequest model
*
* @return void
*/
public function on_start() {
if(!defined('BASE_API_PATH')) {
define('BASE_API_PATH', '-/api'); // -/api
}
if(!defined('C5_API_DEBUG')) {
Config::getandDefine('C5_API_DEBUG', true);
}
define('C5_API_DIRNAME_AUTH', 'api/auth');
define('C5_API_DIRNAME_FORMATS', 'api/formats');
define('C5_API_DIRNAME_ROUTES', 'api/routes');
define('C5_API_DEFAULT_FORMAT', 'json');
define('C5_API_DEFAULT_AUTH', 'none');
define('C5_API_DEFAULT_KEY_LENGTH', 40);
define('C5_API_KEY_TIMEOUT', 120);//in seconds
define('C5_API_FILENAME_ROUTES_CONTROLLER', 'controller.php');
define('C5_API_HANDLE', 'api');
self::registerAutoload();
Events::extend('on_start', 'ApiRouter', 'get', DIR_PACKAGES.'/'.$this->pkgHandle.'/'.DIRNAME_MODELS.'/api/router.php');
}
/**
* Called by concrete5 to install the package
*
* We generate the auth key (24 chars long).
* We add all dashboard singlepages
*
* @return void
*/
public function install() {
$this->on_start();
$pkg = parent::install();
Loader::model('single_page');
$p = SinglePage::add('/dashboard/api',$pkg);
$p->update(array('cName'=>t('concrete5 API'), 'cDescription'=>t('Remote management of your site.')));
$p->setAttribute('icon_dashboard', 'icon-home');
$p1 = SinglePage::add('/dashboard/api/core/',$pkg);
$p1->update(array('cName'=>t('Core API')));
$p1->setAttribute('icon_dashboard', 'icon-folder-open');
$p2 = SinglePage::add('/dashboard/api/core/manage_routes',$pkg);
$p2->update(array('cName'=>t('Manage Routes'), 'cDescription'=>t('Managed installed API routes.')));
$p2->setAttribute('icon_dashboard', 'icon-list-alt');
$p3 = SinglePage::add('/dashboard/api/core/on_off',$pkg);
$p3->update(array('cName'=>t('Enable & Disable the API')));
$p3->setAttribute('icon_dashboard', 'icon-off');
$p4 = SinglePage::add('/dashboard/api/core/formats',$pkg);
$p4->update(array('cName'=>t('Enable & Disable the API Formats')));
$p4->setAttribute('icon_dashboard', 'icon-cog');
$p5 = SinglePage::add('/dashboard/api/core/auth',$pkg);
$p5->update(array('cName'=>t('Manage Authentication Types')));
$p5->setAttribute('icon_dashboard', 'icon-lock');
$p6 = SinglePage::add('/dashboard/api/auth',$pkg);
$p6->update(array('cName'=>t('Authentication')));
$p6->setAttribute('icon_dashboard', 'icon-folder-open');
$p7 = SinglePage::add('/dashboard/api/auth/key',$pkg);
$p7->update(array('cName'=>t('Api Keys')));
$p7->setAttribute('icon_dashboard', 'icon-th-list');
$p8 = SinglePage::add('/dashboard/api/formats',$pkg);
$p8->update(array('cName'=>t('Formats')));
$p8->setAttribute('icon_dashboard', 'icon-folder-open');
$p9 = SinglePage::add('/dashboard/api/settings',$pkg);
$p9->update(array('cName'=>t('Route Settings')));
$p9->setAttribute('icon_dashboard', 'icon-folder-open');
$pkg->saveConfig('ENABLED', 1);
$u = new User();
Loader::helper('concrete/dashboard');
$qn = ConcreteDashboardMenu::getMine();
if(!$qn->contains($p)) {
$qn->add($p);
$u->saveConfig('QUICK_NAV_BOOKMARKS', serialize($qn));
}
$this->installRoutes();
$this->installFormats();
$this->installAuth();
}
/**
* Called by concrete5 to uninstall the package
*
* We check if any other packages are installed that are using the api, if not uninstall.
*
* @return void
*/
public function uninstall() {
$force = $_POST['force'];
if($force != t('remove')) {
$force = false;
}
if(!$force) {
$pkgs = ApiRouteList::getPackagesList();
if(count($pkgs) > 0) {
throw new Exception(t('Please uninstall all addons that register routes with the API, before uninstalling this addon.'));
}
}
$db = Loader::db();
$sql = array();
$sql[] = 'DROP TABLE IF EXISTS ApiRouteRegistry';
$sql[] = 'DROP TABLE IF EXISTS ApiFormats';
$sql[] = 'DROP TABLE IF EXISTS ApiAuth';
$sql[] = 'DROP TABLE IF EXISTS ApiAuthKey';
foreach($sql as $s) {
$db->Execute($s);
}
$pkg = parent::uninstall();
}
public function installRoutes() {
$pkg = Package::getByHandle(C5_API_HANDLE);
ApiRoute::add('bad_request', t('Bad Request'), $pkg, true, false, true);
ApiRoute::add('unauthorized', t('Unauthorized'), $pkg, true, false, true);
ApiRoute::add('forbidden', t('Forbidden'), $pkg, true, false, true);
ApiRoute::add('server_error', t('Server Error'), $pkg, true, false, true);
}
public function installFormats() {
$pkg = Package::getByHandle(C5_API_HANDLE);
ApiFormatModel::add('json', $pkg, true)->setDefault();
}
public function installAuth() {
$pkg = Package::getByHandle(C5_API_HANDLE);
ApiAuthModel::add('none', t('None'), $pkg)->setEnabled();
ApiAuthModel::add('key', t('Key'), $pkg);
}
public static function registerAutoload() {
$classes = array();
$classes['ApiRouter'] = array('model', 'api/router', C5_API_HANDLE);
$classes['ApiRoute,ApiRouteList'] = array('model', 'api/route/route', C5_API_HANDLE);
$classes['ApiRouteController'] = array('model', 'api/route/controller', C5_API_HANDLE);
$classes['ApiResponse'] = array('model', 'api/response', C5_API_HANDLE);
$classes['ApiFormatModel'] = array('model', 'api/format/model', C5_API_HANDLE);
$classes['ApiFormatList'] = array('model', 'api/format/list', C5_API_HANDLE);
$classes['ApiAuthModel'] = array('model', 'api/auth/model', C5_API_HANDLE);
$classes['ApiAuthList'] = array('model', 'api/auth/list', C5_API_HANDLE);
$classes['ApiAuthKeyModel,ApiAuthKeyList'] = array('model', 'api/auth/key', C5_API_HANDLE);
//$classes['NoneApiAuth'] = array('apiAuth', 'none', C5_API_HANDLE);
//$classes['KeyApiAuth'] = array('apiAuth', 'key', C5_API_HANDLE);
//$classes['JsonApiFormat'] = array('apiFormat', 'json', C5_API_HANDLE);
$classes['BadRequestApiRouteController'] = array('apiRoute', 'bad_request');
$classes['ForbiddenApiRouteController'] = array('apiRoute', 'forbidden');
$classes['ServerErrorApiRouteController'] = array('apiRoute', 'server_error');
ApiLoader::registerAutoload($classes);
spl_autoload_register(array('ApiLoader', 'autoload'));
}
}
class ApiLoader extends Loader {
static $ApiClasses = array();
public static function autoload($class) {
$classes = self::$ApiClasses;
$cl = $classes[$class];
if ($cl) {
if(is_callable(array('ApiLoader', $cl[0]))) {
call_user_func_array(array('ApiLoader', $cl[0]), array($cl[1], $cl[2]));
}
} else {
parent::autoload($class);
}
}
public static function registerAutoload($classes) {
foreach($classes as $class => $data) {
if (strpos($class, ',') > -1) {
$subclasses = explode(',', $class);
foreach($subclasses as $subclass) {
self::$ApiClasses[$subclass] = $data;
}
} else {
self::$ApiClasses[$class] = $data;
}
}
}
public static function apiAuth($path, $pkg) {
$env = Environment::get();
require_once($env->getPath(C5_API_DIRNAME_AUTH . '/' . $path . '.php', $pkg));
}
public static function apiFormat($path, $pkg) {
$env = Environment::get();
require_once($env->getPath(C5_API_DIRNAME_FORMATS . '/' . $path . '.php', $pkg));
}
public static function apiRoute($route) {
if(!is_object($route)) {
$routee = ApiRouteList::getRouteByPath($route);
if(!$routee) {
throw new Exception(t('Invalid route: %s', $route));
}
$route = $routee;
}
$env = Environment::get();
require_once($env->getPath(C5_API_DIRNAME_ROUTES.'/'.$route->file, Package::getByID($route->pkgID)));
}
}