|
- <?php
-
- namespace Controller;
-
- class Admin {
- private $fff;
- public $classes = ['test'];
- public $edit_url = "";
-
- function __construct() {
- $f3 = \Base::instance();
- $this->fff = new \Modules\FilesInFolders(
- $f3->get('current_page_folder')
- );
- }
-
- function menu() {
- return '<a class="action" href="#">clear cache</a>';
- }
- function receive_edit() {
- $f3 = \Base::instance();
- $f3->reroute("?admin=1");
- }
- function index() {
- $f3 = \Base::instance();
- $mods = [];
- if ($edit_type = $f3->get("GET.edit")) {
- $this->classes[] = 'visible';
- switch ($edit_type) {
- case 'txt':
- $file = $f3->get("GET.file");
- $mods[] = $this->text_editor($file);
- break;
- }
- }
- $mods[] = $this->list_content_folder();
- return implode("",$mods);
- //return $this->clear_cache();
- }
-
- function clear_cache() {
- $CI = new \Modules\CachedImage("rsc/img/default.png");
- $cache_dir = $CI->cache_dir;
-
- $ls = scandir($cache_dir);
- $count = 0;
- foreach($ls as $file) {
- if(!strncmp(".",$file,1)) continue;
- unlink($cache_dir."".$file);
- $count++;
- }
- return "erased $count files<br>";
- }
-
- function get_type($file) {
- if (is_dir($file)) {
- return 'dir';
- } else if (is_file($file)) {
- $ex = array_pop(explode(".",$file));
- foreach ($this->fff->EXT as $type => $array) {
- if (in_array(strtolower($ex),$array)) {
- return $type;
- }
- }
- return 'unkown';
- }
-
-
-
- }
-
- function text_editor($file) {
- if (is_file($file)) {
- return sprintf('<form method="post" action="%s">'
- .'<textarea class="texteditor" cols="80" rows="20">%s</textarea>'
- .'<button type="submit">save</button>'
- .'</form>',
- $this->edit_url . "?".http_build_query([
- 'admin'=>1
- ]),
- file_get_contents($file)
- );
- }
- return "nope";
- }
-
- function list_content_folder() {
- $f3 = \Base::instance();
- $folder = $f3->get("current_page_folder");
- $out = "";
-
- $ls = [];
- foreach (scandir($folder) as $file) {
-
- $ls[] = (object)[
- 'name' => $file,
- 'type' => self::get_type($folder.$file),
- 'path' => $folder.$file
- ];
- }
-
- $out .= '<div class="filebrowser"><ul>';
- foreach ($ls as $file) {
- $edit_link = "?".http_build_query([
- "admin"=>1,
- "edit"=>$file->type,
- "file"=>$file->path
- ]);
- $out .= sprintf('<li class="filebrowser type-%s"><a href="%s">%s</a></li>',
- $file->type,
- $edit_link,
- $file->name
- );
- }
- $out .= "</ul></div>";
-
- return $out;
- }
- }
|