-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.php
360 lines (313 loc) · 9.66 KB
/
files.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
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
/**
* @package hubzero-cms
* @copyright Copyright (c) 2005-2020 The Regents of the University of California.
* @license http://opensource.org/licenses/MIT MIT
*/
// No direct access
defined('_HZEXEC_') or die();
/**
* Groups Plugin class for group members
*/
class plgGroupsFiles extends \Hubzero\Plugin\Plugin
{
/**
* Affects constructor behavior. If true, language files will be loaded automatically.
*
* @var boolean
*/
protected $_autoloadLanguage = true;
/**
* Loads the plugin language file
*
* @param string $extension The extension for which a language file should be loaded
* @param string $basePath The basepath to use
* @return boolean True, if the file has successfully loaded.
*/
public function loadLanguage($extension = '', $basePath = PATH_APP)
{
if (empty($extension))
{
$extension = 'plg_' . $this->_type . '_' . $this->_name;
}
$group = \Hubzero\User\Group::getInstance(Request::getCmd('cn'));
if ($group && $group->isSuperGroup())
{
$basePath = PATH_APP . DS . 'site' . DS . 'groups' . DS . $group->get('gidNumber');
}
$lang = \App::get('language');
return $lang->load(strtolower($extension), $basePath, null, false, true)
|| $lang->load(strtolower($extension), PATH_APP . DS . 'plugins' . DS . $this->_type . DS . $this->_name, null, false, true)
|| $lang->load(strtolower($extension), PATH_APP . DS . 'plugins' . DS . $this->_type . DS . $this->_name, null, false, true)
|| $lang->load(strtolower($extension), PATH_CORE . DS . 'plugins' . DS . $this->_type . DS . $this->_name, null, false, true);
}
/**
* Return the alias and name for this category of content
*
* @return array
*/
public function &onGroupAreas()
{
$area = array(
'name' => 'files',
'title' => Lang::txt('PLG_GROUPS_FILES'),
'default_access' => $this->params->get('plugin_access', 'members'),
'display_menu_tab' => $this->params->get('display_tab', 1),
'icon' => 'f0c5'
);
return $area;
}
/**
* Return data on a group view (this will be some form of HTML)
*
* @param object $group Current group
* @param string $option Name of the component
* @param string $authorized User's authorization level
* @param integer $limit Number of records to pull
* @param integer $limitstart Start of records to pull
* @param string $action Action to perform
* @param array $access What can be accessed
* @param array $areas Active area(s)
* @return array
*/
public function onGroup($group, $option, $authorized, $limit=0, $limitstart=0, $action='', $access, $areas=null)
{
$returnhtml = true;
$active = 'files';
// The output array we're returning
$arr = array(
'html' => '',
'metadata' => array()
);
//get this area details
$this_area = $this->onGroupAreas();
// Check if our area is in the array of areas we want to return results for
if (is_array($areas) && $limit)
{
if (!in_array($this_area['name'], $areas))
{
$returnhtml = false;
}
}
// Set some variables so other functions have access
$this->authorized = $authorized;
$this->action = $action;
$this->_option = $option;
$this->group = $group;
$this->name = substr($option, 4, strlen($option));
// Only perform the following if this is the active tab/plugin
if ($returnhtml)
{
//set group members plugin access level
$group_plugin_acl = $access[$active];
// Get the group members and managers
$members = $group->get('members');
$managers = $group->get('managers');
//if set to nobody make sure cant access
if ($group_plugin_acl == 'nobody')
{
$arr['html'] = '<p class="info">' . Lang::txt('GROUPS_PLUGIN_OFF', ucfirst($active)) . '</p>';
return $arr;
}
//check if guest and force login if plugin access is registered or members
if (User::isGuest()
&& ($group_plugin_acl == 'registered' || $group_plugin_acl == 'members' || $group_plugin_acl == 'managers'))
{
$url = Route::url('index.php?option=com_groups&cn='.$group->get('cn').'&active='.$active, false, true);
App::redirect(
Route::url('index.php?option=com_users&view=login&return=' . base64_encode($url)),
Lang::txt('GROUPS_PLUGIN_REGISTERED', ucfirst($active)),
'warning'
);
return;
}
// Plugin was made public - Disallow uploading or editing
if (User::isGuest() && $group_plugin_acl == 'anyone')
{
$this->authorized = false;
}
//check to see if user is member and plugin access requires members
if (!in_array(User::get('id'), $members)
&& $group_plugin_acl == 'members'
&& $authorized != 'admin')
{
$arr['html'] = '<p class="info">' . Lang::txt('GROUPS_PLUGIN_REQUIRES_MEMBER', ucfirst($active)) . '</p>';
return $arr;
}
// Check to see if user is manager and plugin access requires managers
if (!in_array(User::get('id'), $managers)
&& $group_plugin_acl == 'managers'
&& $authorized != 'admin')
{
$arr['html'] = '<p class="info">' . Lang::txt('GROUPS_PLUGIN_REQUIRES_MANAGER', ucfirst($active)) . '</p>';
return $arr;
}
// Append to document the title
Document::setTitle(Document::getTitle() . ': ' . Lang::txt('PLG_GROUPS_FILES'));
$this->path = PATH_APP . DS . trim($this->params->get('uploadpath', '/site/groups'), DS) . DS . $this->group->get('gidNumber');
$arr['html'] = $this->_browse();
}
// Return the output
return $arr;
}
/**
* Display a list of latest blog entries
*
* @return string
*/
private function _browse()
{
$view = $this->view('filebrowser')
->set('option', $this->option)
->set('group', $this->group)
->set('config', $this->params)
->set('task', $this->action)
->set('authorized', $this->authorized)
->set('notifications', array());
//get rel path to start
$view->activeFolder = Request::getString('path', '/');
// make sure we have an active folder
if ($view->activeFolder == '')
{
$view->activeFolder = '/uploads';
}
$view->activeFolder = '/' . trim($view->activeFolder, '/');
if (preg_match('/^\/uploads(?!.*\/\.\.).*$/', $view->activeFolder) == 0)
{
$view->activeFolder = '/uploads';
}
// make sure we have a path
$this->_createGroupFolder($this->path);
// get list of folders
$folders = Filesystem::directoryTree($this->path, '.', 10);
foreach ($folders as $i => $folder)
{
if ($folder['parent'] || (!$folder['parent'] && $folder['name'] == 'uploads'))
{
continue;
}
unset($folders[$i]);
}
// build recursive folder trees
$folderTree = $this->_buildFolderTree($folders);
$view->folderTree = $this->_buildFolderTreeHtml($folderTree);
$view->folderList = $this->_buildFolderTreeSelect($folderTree);
foreach ($this->getErrors() as $error)
{
$view->setError($error);
}
return $view->loadTemplate();
}
/**
* Create group folder id doesnt exist
*
* @param string $path
* @return void
*/
private function _createGroupFolder($path)
{
// create base group folder
if (!Filesystem::exists($path))
{
Filesystem::makeDirectory($path);
}
// create uploads file
if (!Filesystem::exists($path . DS . 'uploads'))
{
Filesystem::makeDirectory($path . DS . 'uploads');
}
}
/**
* Build Folder tree based on path
*
* @param array $folders
* @param integer $parent_id
* @return array
*/
private function _buildFolderTree($folders, $parent_id = 0)
{
$branch = array();
foreach ($folders as $folder)
{
if ($folder['parent'] == $parent_id)
{
$children = $this->_buildFolderTree($folders, $folder['id']);
if ($children)
{
$folder['children'] = $children;
}
$branch[] = $folder;
}
}
return $branch;
}
/**
* Build Folder tree in html ul list form
*
* @param array $tree
* @return string
*/
private function _buildFolderTreeHtml($tree)
{
$base = substr(PATH_APP, strlen(PATH_ROOT));
$html = '<ul>';
foreach ($tree as $treeLevel)
{
$folder = str_replace($base . '/site/groups/' . $this->group->get('gidNumber'), '', $treeLevel['relname']);
$nodeToggle = '<span class="tree-folder-toggle-spacer"></span>';
$childrenHtml = '';
if (@is_array($treeLevel['children']))
{
$nodeToggle = '<a class="tree-folder-toggle" href="javascript:void(0);"></a>';
$childrenHtml = $this->_buildFolderTreeHtml($treeLevel['children']);
}
$html .= '<li>';
$html .= $nodeToggle . '<a data-folder="'.$folder.'" href="javascript:void(0);" class="tree-folder">' . $treeLevel['name'].'</a>';
$html .= $childrenHtml;
$html .= '</li>';
}
$html .= '</ul>';
return $html;
}
/**
* Build Folder tree in select list form
*
* @param array $tree
* @return string
*/
private function _buildFolderTreeSelect($tree)
{
$html = '<select class="" name="folder">';
if ($this->group->get('type') == 3)
{
$html .= '<option value="/">(root)</option>';
}
$html .= $this->_buildFolderTreeSelectOptionList($tree);
$html .= '</select>';
return $html;
}
/**
* Recursive function to create options for select list
*
* @param array $tree
* @return string
*/
private function _buildFolderTreeSelectOptionList($tree)
{
$base = substr(PATH_APP, strlen(PATH_ROOT));
$options = '';
foreach ($tree as $treeLevel)
{
$value = str_replace($base . '/site/groups/' . $this->group->get('gidNumber'), '', $treeLevel['relname']);
$text = str_repeat('⌊', substr_count($value, '/'));
$parts = explode('/', $value);
$text .= ' ' . array_pop($parts);
$options .= '<option value="'.$value.'">' . $text.'</option>';
if (@is_array($treeLevel['children']))
{
$options .= $this->_buildFolderTreeSelectOptionList($treeLevel['children']);
}
}
return $options;
}
}