Skip to content

Commit c819b40

Browse files
committed
[Filemanager] Integrate Filegator filemanager in Hestia
Commit contains automated install script, config file and some minor changes to Session,Auth Adapter
1 parent 9655e5e commit c819b40

File tree

4 files changed

+301
-0
lines changed

4 files changed

+301
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FileGator package.
5+
*
6+
* (c) Milos Stojanovic <alcalbg@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE file
9+
*/
10+
11+
namespace Filegator\Services\Auth\Adapters;
12+
13+
use Filegator\Services\Auth\AuthInterface;
14+
use Filegator\Services\Auth\User;
15+
use Filegator\Services\Auth\UsersCollection;
16+
use Filegator\Services\Service;
17+
18+
/**
19+
* @codeCoverageIgnore
20+
*/
21+
class HestiaAuth implements Service, AuthInterface
22+
{
23+
24+
protected $permissions = [];
25+
26+
protected $private_repos = false;
27+
28+
protected $hestia_user = '';
29+
30+
public function init(array $config = [])
31+
{
32+
if (isset($_SESSION['user'])) {
33+
$v_user = $_SESSION['user'];
34+
}
35+
if (isset($_SESSION['look']) && $_SESSION['look'] != 'admin' && $v_user === 'admin') {
36+
$v_user = $_SESSION['look'];
37+
}
38+
$this->hestia_user = $v_user;
39+
$this->permissions = isset($config['permissions']) ? (array)$config['permissions'] : [];
40+
$this->private_repos = isset($config['private_repos']) ? (bool)$config['private_repos'] : false;
41+
}
42+
43+
public function user(): ?User
44+
{
45+
46+
$cmd="/usr/bin/sudo /usr/local/hestia/bin/v-list-user";
47+
exec ($cmd." ".escapeshellarg($this->hestia_user )." json", $output, $return_var);
48+
49+
if ($return_var == 0) {
50+
$data = json_decode(implode('', $output), true);
51+
$hestia_user_info = $data[$this->hestia_user];
52+
return $this->transformUser($hestia_user_info);
53+
}
54+
55+
return $this->getGuest();
56+
}
57+
58+
public function transformUser($hstuser): User
59+
{
60+
$user = new User();
61+
$user->setUsername($this->hestia_user);
62+
$user->setName($this->hestia_user . " (" . $hstuser['FNAME'] . " " . $hstuser['LNAME'] . ")");
63+
$user->setRole('user');
64+
$user->setPermissions($this->permissions);
65+
$user->setHomedir('/');
66+
return $user;
67+
}
68+
69+
public function authenticate($username, $password): bool
70+
{
71+
# Auth is handled by Hestia
72+
return false;
73+
}
74+
75+
public function forget()
76+
{
77+
// Logout return to Hestia
78+
return $this->getGuest();
79+
}
80+
81+
public function store(User $user)
82+
{
83+
return null; // not used
84+
}
85+
86+
public function update($username, User $user, $password = ''): User
87+
{
88+
// Password change is handled by Hestia
89+
return $this->user();
90+
}
91+
92+
public function add(User $user, $password): User
93+
{
94+
return new User(); // not used
95+
}
96+
97+
public function delete(User $user)
98+
{
99+
return true; // not used
100+
}
101+
102+
public function find($username): ?User
103+
{
104+
return null; // not used
105+
}
106+
107+
public function allUsers(): UsersCollection
108+
{
109+
return new UsersCollection(); // not used
110+
}
111+
112+
public function getGuest(): User
113+
{
114+
$guest = new User();
115+
116+
$guest->setUsername('guest');
117+
$guest->setName('Guest');
118+
$guest->setRole('guest');
119+
$guest->setHomedir('/');
120+
$guest->setPermissions([]);
121+
122+
return $guest;
123+
}
124+
125+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FileGator package.
5+
*
6+
* (c) Milos Stojanovic <alcalbg@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE file
9+
*/
10+
11+
namespace Filegator\Services\Session\Adapters;
12+
13+
use Filegator\Kernel\Request;
14+
use Filegator\Services\Service;
15+
use Filegator\Services\Session\Session;
16+
use Filegator\Services\Session\SessionStorageInterface;
17+
18+
class SessionStorage implements Service, SessionStorageInterface
19+
{
20+
protected $request;
21+
22+
protected $config;
23+
24+
public function __construct(Request $request)
25+
{
26+
$this->request = $request;
27+
}
28+
29+
public function init(array $config = [])
30+
{
31+
// we don't have a previous session attached
32+
if (! $this->getSession()) {
33+
$handler = $config['handler'];
34+
$session = new Session($handler());
35+
//$session->setName('filegator');
36+
$this->setSession($session);
37+
}
38+
}
39+
40+
public function save()
41+
{
42+
$this->getSession()->save();
43+
}
44+
45+
public function set(string $key, $data)
46+
{
47+
return $this->getSession()->set($key, $data);
48+
}
49+
50+
public function get(string $key, $default = null)
51+
{
52+
return $this->getSession() ? $this->getSession()->get($key, $default) : $default;
53+
}
54+
55+
public function invalidate()
56+
{
57+
if (! $this->getSession()->isStarted()) {
58+
$this->getSession()->start();
59+
}
60+
61+
$this->getSession()->invalidate();
62+
}
63+
64+
private function setSession(Session $session)
65+
{
66+
return $this->request->setSession($session);
67+
}
68+
69+
private function getSession(): ?Session
70+
{
71+
return $this->request->getSession();
72+
}
73+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
$dist_config = require __DIR__.'/configuration_sample.php';
4+
5+
$dist_config['frontend_config']['app_name'] = 'Hestia FM';
6+
$dist_config['frontend_config']['logo'] = 'https://raw.githubusercontent.com/filegator/filegator/master/dist/img/logo.png';
7+
$dist_config['frontend_config']['editable'] = ['.txt', '.css', '.js', '.ts', '.html', '.php', '.py' ];
8+
$dist_config['frontend_config']['guest_redirection'] = '/login/' ;
9+
10+
$dist_config['services']['Filegator\Services\Storage\Filesystem']['config']['adapter'] = function () {
11+
12+
if (isset($_SESSION['user'])) {
13+
$v_user = $_SESSION['user'];
14+
}
15+
if (isset($_SESSION['look']) && $_SESSION['look'] != 'admin' && $v_user === 'admin') {
16+
$v_user = $_SESSION['look'];
17+
}
18+
19+
return new \League\Flysystem\Sftp\SftpAdapter([
20+
'host' => '127.0.0.1',
21+
'port' => 22,
22+
'username' => basename($v_user),
23+
'privateKey' => '/home/'.basename($v_user).'/.ssh/hst-filemanager-key',
24+
'root' => '/',
25+
'timeout' => 10,
26+
]);
27+
};
28+
29+
$dist_config['services']['Filegator\Services\Auth\AuthInterface'] = [
30+
'handler' => '\Filegator\Services\Auth\Adapters\HestiaAuth',
31+
'config' => [
32+
'permissions' => ['read', 'write', 'upload', 'download', 'batchdownload', 'zip'],
33+
'private_repos' => false,
34+
],
35+
];
36+
37+
$dist_config['services']['Filegator\Services\View\ViewInterface']['config'] = [
38+
'add_to_head' => '',
39+
'add_to_body' => '',
40+
];
41+
42+
43+
return $dist_config;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
3+
if [ -z "$HESTIA" ]; then
4+
HESTIA="/usr/local/hestia"
5+
fi
6+
7+
# Hestia php-fpm pool user
8+
user='admin'
9+
10+
source $HESTIA/func/main.sh
11+
12+
FM_INSTALL_DIR="$HESTIA/web/fm"
13+
14+
FM_V="7.4.1"
15+
FM_FILE="filegator_v${FM_V}.zip"
16+
FM_URL="https://github.com/filegator/filegator/releases/download/v${FM_V}/${FM_FILE}"
17+
18+
19+
COMPOSER_DIR="$HOMEDIR/$user/.composer"
20+
COMPOSER_BIN="$COMPOSER_DIR/composer"
21+
22+
if [ ! -f "$COMPOSER_BIN" ]; then
23+
mkdir -p "$COMPOSER_DIR"
24+
chown $user: "$COMPOSER_DIR"
25+
26+
COMPOSER_SETUP_FILE=$(mktemp)
27+
check_result $? "Create temp file"
28+
29+
signature="$(curl https://composer.github.io/installer.sig)"
30+
check_result $? "Download signature"
31+
32+
user_exec wget --tries=3 --timeout=15 --read-timeout=15 --waitretry=3 --no-dns-cache https://getcomposer.org/installer --quiet -O "$COMPOSER_SETUP_FILE"
33+
check_result $? "Download composer installer"
34+
35+
[[ "$signature" = $(sha384sum $COMPOSER_SETUP_FILE | cut -f 1 -d " ") ]] || check_result $E_INVALID "Composer signature does not match"
36+
37+
COMPOSER_HOME="$HOMEDIR/$user/.config/composer" user_exec /usr/bin/php "$COMPOSER_SETUP_FILE" --install-dir="$COMPOSER_DIR" --filename=composer
38+
check_result $? "Composer instal failed"
39+
40+
[ -f "$COMPOSER_SETUP_FILE" ] && rm -f "$COMPOSER_SETUP_FILE"
41+
fi
42+
43+
mkdir -p "$FM_INSTALL_DIR"
44+
cd "$FM_INSTALL_DIR"
45+
46+
[ ! -f "${FM_INSTALL_DIR}/${FM_FILE}" ] &&
47+
wget "$FM_URL" --quiet -O "${FM_INSTALL_DIR}/${FM_FILE}"
48+
49+
unzip -qq "${FM_INSTALL_DIR}/${FM_FILE}"
50+
mv --force ${FM_INSTALL_DIR}/filegator/* "${FM_INSTALL_DIR}"
51+
rm --recursive --force ${FM_INSTALL_DIR}/filegator
52+
chown root: "${FM_INSTALL_DIR}"
53+
54+
cp --recursive --force ${HESTIA_INSTALL_DIR}/filemanager/filegator/* "${FM_INSTALL_DIR}"
55+
56+
COMPOSER_HOME="$HOMEDIR/$user/.config/composer" $COMPOSER_BIN require league/flysystem-sftp
57+
58+
chown $user: "${FM_INSTALL_DIR}/private"
59+
chown $user: "${FM_INSTALL_DIR}/private/logs"
60+
chown $user: "${FM_INSTALL_DIR}/repository"

0 commit comments

Comments
 (0)