-
Notifications
You must be signed in to change notification settings - Fork 10
/
AppInstanceController.php
347 lines (304 loc) · 13.2 KB
/
AppInstanceController.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
namespace App\Http\Controllers;
use App\AppInstance;
use App\App;
use App\AppVersion;
use App\Endpoint;
use App\Group;
use App\UserPreference;
use App\User;
use App\Page;
use App\ResourceCache;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Libraries\HTTPHelper;
use App\Libraries\Templater;
class AppInstanceController extends Controller
{
public function __construct() {
// $this->middleware('auth')->except('run','fetch', 'get_data');
}
public function list_all_app_instances(Request $request) {
if (Auth::user()->site_admin) {
$app_instances = AppInstance::select('id','app_id','group_id','app_version_id','name','slug','icon','order','device','unlisted','public')
->with('app')
->whereHas('group', function($q){
$q->where('site_id','=',config('app.site')->id);
})->orderBy('group_id','order')->get();
} else {
$app_instances = AppInstance::select('id','app_id','group_id','app_version_id','name','slug','icon','order','device','unlisted','public')
->with('app')
->whereHas('group', function($q){
$q->where('site_id','=',config('app.site')->id)->whereIn('id',Auth::user()->apps_admin_groups);
})->orderBy('group_id','order')->get();
}
return $app_instances;
}
public function admin(AppInstance $app_instance) {
return view('admin', ['resource'=>'app_instance','id'=>$app_instance->id]);
}
public function show(AppInstance $app_instance) {
$myApp = AppInstance::with('app')->with('group')->where('id', '=', $app_instance->id)->first();
$myApp->findVersion();
return $myApp;
}
public function pages(AppInstance $app_instance)
{
$pages = Page::with('group')->where('group_id', '=', $app_instance->group_id)->get();
$page_list = [];
foreach($pages as $page){
$sections = $page->content;
foreach($sections->sections as $section){
foreach($section as $widget){
if($widget->widgetType === 'uApp'){
if($widget->app_id == $app_instance->id){
unset($page->content);
$page_list[] = $page;
break 2;
}
}
}
}
}
return $page_list;
}
public function create(Request $request) {
$this->validate($request,['name'=>['required']]);
$app_instance = new AppInstance($request->all());
$app_instance->app_version_id = AppVersion::where('app_id','=',$request->get('app_id'))->orderBy('id','desc')->pluck('id')->first();
$app_instance->app_id = $request->get('app_id');
$app_instance->group_id = $request->get('group_id');
$app_instance->save();
return $app_instance;
}
public function update(Request $request, AppInstance $app_instance) {
$data = $request->all();
if($request->app_version_id == -1){$data['app_version_id'] = null;}
if(isset($data['groups'])){
$data['groups'] = json_decode($data['groups']);
}
$app_instance->update($data);
return AppInstance::with('app')->where('id', '=',$app_instance->id)->first();
}
public function destroy(AppInstance $app_instance){
if ($app_instance->delete()) {
return 1;
}
}
public function save_user_options(AppInstance $app_instance, Request $request)
{
return $app_instance->set_user_options(Auth::user(),$request->get('options'));
}
public function run($group, $slug, Request $request) {
if(!is_numeric($group)) {
$groupObj = Group::with('composites')->where('slug','=',$group)->first();
$group = $groupObj->id;
}else{
$groupObj = Group::with('composites')->where('id','=',$group)->first();
}
if (Auth::check()) { /* User is Authenticated */
$current_user = Auth::user();
$myApp = AppInstance::with('app')->with(['user_options'=>function($query){
$query->where('user_id','=', Auth::user()->id);
}])->where('group_id','=', $group)->where('slug', '=', $slug)->with('app')->first();
if($myApp->public == 0) {
$this->authorize('fetch' ,$myApp);
}
$links = Group::AppsPages()->where('unlisted','=',0)->orderBy('order')->get();
} else { /* User is not Authenticated */
$current_user = new User;
$myApp = AppInstance::with('app')->where('group_id','=', $group)->where('slug', '=', $slug)->where('public','=',true)->first();
if (is_null($myApp)) { abort(403); }
$links = Group::publicAppsPages()->where('unlisted','=',0)->orderBy('order')->get();
}
$myApp->findVersion();
if($myApp != null) {
$template = new Templater();
return $template->render([
'apps_pages'=>$links,
'name'=>$myApp->name,
'slug'=>$myApp->slug,
'id'=>$myApp->id,
'uapp'=>$myApp,
'data'=>array(),
'config'=>json_decode('{"sections":[[{"title":"'.$myApp->name.'","app_id":'.$myApp->id.',"widgetType":"uApp"}]],"layout":4}'),
'group'=>$groupObj
]);
}
abort(404,'App not found');
}
public function fetch($ai_id, Request $request) {
session_write_close();
if (Auth::check()) { /* User is Authenticated */
$current_user = Auth::user();
$myApp = AppInstance::with(['user_options'=>function($query){
$query->where('user_id','=',Auth::user()->id);
}])->where('id', '=', $ai_id)->first();
$this->authorize('fetch' ,$myApp);
} else { /* User is not Authenticated */
$current_user = new User;
$myApp = AppInstance::where('id', '=', $ai_id)->first();
if (is_null($myApp)) { abort(403); }
}
$myApp->findVersion();
if($myApp != null){
$data = [
'user'=>$current_user,
'options'=>$myApp->options
];
if(!is_null($myApp->user_options)) {
$data['user']->options = $myApp->user_options->options;
if(is_null($data['user']->options)){
$data['user']->options = is_null($myApp->user_options_default)?[]:$myApp->user_options_default;
}
}else{
$data['user']->options = is_null($myApp->user_options_default)?[]:$myApp->user_options_default;
}
if($myApp->app->code && isset($myApp->app->code->resources) ){
foreach($myApp->app->code->resources as $source){
if ($source->fetch === 'true' || $source->fetch === true) {
$data[$source->name] = $this->get_data($myApp, $source->name, $request);
}
}
}
return $data;
}
abort(404,'App not found');
}
private function http_endpoint(Endpoint $endpoint, $resource_info, $verb, $all_data, $app_instance_id) {
// Derive and Map URL with Mustache
$m = new \Mustache_Engine;
$url = $m->render($endpoint->config->url . $resource_info->path, $all_data);
if ($resource_info->cache === true || $resource_info->cache === 'true') {
$cache = ResourceCache::where('app_instance_id', '=', $app_instance_id)
->where('url','=',$url)
->whereRaw('created_at >= (NOW() - INTERVAL 10 MINUTE)')
->first();
if (!is_null($cache)) {
$data = unserialize($cache->content);
}
}
if (!isset($data)) {
// Fetch Data based on Endpoint Type
$httpHelper = new HTTPHelper();
if ($endpoint->type == 'http_no_auth') {
$data = $httpHelper->http_fetch($url,$verb,$all_data['request']);
} else if ($endpoint->type == 'http_basic_auth') {
$data = $httpHelper->http_fetch($url,$verb,$all_data['request'],
$endpoint->config->username, $endpoint->getSecret());
} else {
abort(505,'Authentication Type Not Supported');
}
if ($resource_info->cache === true || $resource_info->cache === 'true') {
// Delete existing cache for that app_instance, as well as any other stale cache
ResourceCache::where('app_instance_id', '=', $app_instance_id)->where('url','=',$url)->delete();
ResourceCache::whereRaw('created_at < (NOW() - INTERVAL 10 MINUTE)')->delete();
$cache = ResourceCache::create([
'app_instance_id' => $app_instance_id,
'url' => $url,
'content' => serialize($data),
]);
}
}
// Convert Data based on Modifier
if ($resource_info->modifier == 'csv') {
$data = array_map("str_getcsv", explode("\n", $data));
} else if ($resource_info->modifier == 'xml') {
$data = json_decode(json_encode(simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA)),true);
} else {
try{
if(is_string($data)){
$try_decode = json_decode($data);
if($try_decode){
$data = $try_decode;
}
}
}catch(Exception $e){}
}
return $data;
}
private function google_endpoint(Endpoint $endpoint, $resource_info, $verb, $all_data) {
$googleClient = new \PulkitJalan\Google\Client(config('google'));
$client = $googleClient->getClient();
$client->setAccessToken($endpoint->getSecret());
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$endpoint->config->secret = $client->getAccessToken();
$endpoint->save();
}
$service = new \Google_Service_Sheets($client);
$sheets = new \GoogleSheets\Sheets();
$sheets->setService($service);
if ($resource_info->path == '' ) {
abort(505,'The path endpoint must specifiy a valid worksheet name');
}
$values = $sheets->spreadsheet($endpoint->config->sheet_id)->sheet($resource_info->path)->all();
return $values;
}
public function get_data(AppInstance $app_instance, $endpoint_name, Request $request) {
session_write_close(); // Don't keep waiting
if(!isset($app_instance->app->code)){
$app_instance->findVersion();
}
if (!$app_instance->public) {
$this->authorize('get_data', $app_instance);
}
$options = $app_instance->options;
$user_options_default = $app_instance->user_options_default;
$resources = $app_instance->resources;
$verb = 'GET'; // Default Verb
if ($request->has('verb')) {
$verb = $request->input('verb');
}
if ($request->has('request') && !is_array($request->input('request'))) {
abort(505,'request must be an array');
}
if(!count($resources)){
return [];
}
// Lookup Resource by Name
$endpoint_found = false;
foreach($resources as $resource_endpoint) {
if ($endpoint_name == $resource_endpoint->name) {
$endpoint_found = true; break;
}
}
if (!$endpoint_found) {
abort(505,'Endpoint Not Found');
}
// Lookup Endpoint
$endpoint = Endpoint::find((int)$resource_endpoint->endpoint);
// Merge App Options with User Preferences, User Info, and `request` data
if (Auth::check()) { /* User is Authenticated */
$current_user = Auth::user();
} else { /* User is not Authenticated */
$current_user = new User;
}
$all_data = ['options'=>$options,
'user'=>$current_user->toArray(),
'request'=>$request->has('request')?$request->input('request'):[]];
$user_prefs = $app_instance->user_options()->where('user_id','=',$current_user->id)->first();
if(isset($user_prefs['options'])){
$all_data['user']['options'] = $user_prefs['options'];
}else{
$all_data['user']['options'] = $user_options_default;
}
$resource_found = false;
foreach($app_instance->app->code->resources as $resource_app) {
if ($endpoint_name == $resource_app->name) {
$resource_found = true; break;
}
}
if (!$resource_found) {
abort(505,'Resource Not Found');
}
$resource_app->endpoint = $resource_endpoint->endpoint;
if ($endpoint->type == 'http_no_auth' || $endpoint->type == 'http_basic_auth') {
$data = $this->http_endpoint($endpoint, $resource_app, $verb, $all_data, $app_instance->id);
}
// else if ($endpoint->type == 'google_sheets') {
// $data = $this->google_endpoint($endpoint, $resource_app, $verb, $all_data);
// }
return $data;
}
}