-
Notifications
You must be signed in to change notification settings - Fork 1
/
file-browser.php
129 lines (101 loc) · 3.51 KB
/
file-browser.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
<?php
namespace Grav\Plugin;
use \Grav\Common\Plugin;
use \RocketTheme\Toolbox\Event\Event;
class FileBrowserPlugin extends Plugin
{
public static function getSubscribedEvents()
{
return [
'onGetPageTemplates' => ['onGetPageTemplates', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onPageInitialized' => ['getFolderStructure', 0]
];
}
/**
* Add the templates to the list in the Admin plugin
*/
public function onGetPageTemplates(Event $event)
{
//$event->types->scanBlueprints(__DIR__.'/blueprints');
$event->types->scanTemplates(__DIR__."/templates");
}
/**
* Add the templates to twig lookup paths (for rendering)
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* Discover the folder structure if the page template is "file-browser"
*/
public function getFolderStructure(Event $event)
{
$page = $event['page'];
// Exit and do nothing if it's not a file-browser page
if ($page->template() !== "file_browser_plugin") {
return;
}
// Get relevant configs
$pluginConfig = $this->grav['config']->get('plugins')["file-browser"];
$pageConfig = isset($page->header()->file_browser) ? $page->header()->file_browser : array("enabled"=>true);
// Combine them, preferring the page config
$config = array_merge($pluginConfig, $pageConfig);
// Load the header configuration with default
$filesDir = isset($config["source"]) ? $config["source"] : "user://files";
// Get the path using the resource locator (relative to the root directory)
$path = $this->grav['locator']->findResource($filesDir, $absolute = false);
// Get filters
$showHidden = isset($config["show_hidden_files"]) ? $config["show_hidden_files"] : false;
$filters = array("show_hidden"=>$showHidden);
// If the path doesn't exist, send an error
// We catch this error in the Twig template by testing iterability:
// If this routine is successful, the 'filebrowser' variable will be an array, but here we set a string.
if (!$path) {
$this->grav["twig"]->twig_vars["fileBrowserContent"] = "Path not found: ".$filesDir;
} else {
$this->grav["twig"]->twig_vars["fileBrowserContent"] = getFolderStructure($path, $filters);
}
}
}
/**
* getFolderStructure returns an array describing the structure
*
* Files are stored as an array: ["file name", "file location"]
* Folders are stored as an array: ["folder name", [folder contents]]
*/
function getFolderStructure($path, $filters) {
$structure = array();
$folderContents = scanFolder($path, $filters);
$folderList = $folderContents[0];
$fileList = $folderContents[1];
foreach ($folderList as $folder) {
array_push($structure, [$folder[0], getFolderStructure($folder[1], $filters)]);
}
foreach ($fileList as $file) {
array_push($structure, $file);
}
return $structure;
}
function scanFolder($path, $filters) {
$contents = scandir($path);
$fileList = array();
$folderList = array();
$showHidden = isset($filters["show_hidden"]) ? $filters["show_hidden"] : false;
foreach ($contents as $item) {
if ($item === "." or $item === "..") {
continue;
}
if ( (substr($item, 0, 1) === ".") && !($showHidden) ) {
continue;
}
$itemPath = $path . "/" . $item;
if (is_dir($itemPath)) {
array_push($folderList, [$item, $itemPath]);
} else {
array_push($fileList, [$item, $itemPath]);
}
}
return [$folderList, $fileList];
}