Route::post($segment . '/{id}/verifyconnection', $controller . '@run');
}
- public function setup() {
+ public function setup()
+ {
parent::setup();
$this->crud->setEditView('vendor.backpack.crud.edit-external-server');
}
- protected function run(Request $request, $id) {
- $validation = [
- 'password' => $request->protocol === "hosting" ? 'nullable' : 'required|string|min:8',
- 'username' => $request->protocol === "hosting" ? 'nullable' : 'required|string|max:255',
- 'host' => $request->protocol === "hosting" ? 'nullable' : 'required|string|max:255',
- 'port' => 'nullable|numeric',
- ];
- $validator = Validator::make([
- 'password' => $request->password,
- 'username' => $request->username,
- 'host' => $request->host,
- 'port' => $request->port], $validation);
-
- if ($validator->fails()) {
- $result = ['error' => true, 'message' => __('Des champs sont manquants')];
- throw new ValidationException($validator);
- }
-
- $validateData = $validator->validate();
-
- $result = FluidbookExternalInstallServer::verifyServerConnexion($id, $validateData);
-
+ protected function run(Request $request, $id)
+ {
+ $result = FluidbookExternalInstallServer::verifyServerConnexion($request->toArray());
return response()->json($result);
}
}
use Cubist\Backpack\Magic\Fields\Table;
use Cubist\Backpack\Magic\Fields\Textarea;
use Cubist\Backpack\Magic\Models\ExternalServer;
+use Cubist\Net\Transfer\IServer;
use Cubist\Net\Transfer\Local;
use Cubist\Util\Files\Files;
use Cubist\Util\Json;
use Cubist\Util\Text;
use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Validator;
// __('!! Serveurs externes')
class FluidbookExternalInstallServer extends ExternalServer
protected static $hostingBasePaths = [
'hosting' => '/mnt/hosting/',
- 'hosting2' => '/mnt/hosting2',
+ 'hosting2' => '/mnt/hosting2/',
'ushosting' => '/application/usstorage/hosting/'
];
public function setFields()
{
- $nothostingProtocols = array_diff(array_keys($this->getProtocols(), $this->getHostingProtocols()));
+ $hostingProtocols = static::getHostingProtocols();
+ $nothostingProtocols = array_diff(array_keys($this->getProtocols(), $hostingProtocols));
parent::setFields();
$this->getField('base_url')->setAttribute('when', ['protocol' => $nothostingProtocols]);
- $this->addField('subdomains', Textarea::class, __('Sous-domaines'), ['when' => ['protocol' => $this->getHostingProtocols()]]);
- $this->addField('php', Checkbox::class, __('Activer le support de PHP'), ['default' => false, 'when' => ['protocol' => $this->getHostingProtocols()]]);
+ $this->addField('subdomains', Textarea::class, __('Sous-domaines'), ['when' => ['protocol' => $hostingProtocols]]);
+ $this->addField('php', Checkbox::class, __('Activer le support de PHP'), ['default' => false, 'when' => ['protocol' => $hostingProtocols]]);
$this->addField('redirections', Table::class, __('Redirections'), ['entity_singular' => __('redirection'), 'columns' => ['from' => __('De'), 'to' => __('Vers')], 'when' => ['protocol' => 'hosting']]);
$this->addField('allows_root', Checkbox::class, __('Autoriser le chargement à la racine (sur le chemin de base)'), ['default' => false]);
}
protected function isHosting()
{
- return in_array($this->getProtocol(), $this->getHostingProtocols());
+ return static::isHostingProtocol($this->getProtocol());
}
public function getBaseURL()
return ['FTP' => __('FTP non sécurisé'), 'SFTP' => 'SFTP', 'hosting' => 'Hosting', 'hosting2' => 'Hosting #2', 'ushosting' => 'US Hosting'];
}
- protected function getHostingProtocols()
+ protected static function getHostingProtocols()
{
return array_keys(static::$hostingBasePaths);
}
protected function getHostingBasePath()
{
-
return static::$hostingBasePaths[$this->getProtocol()];
}
- public static function verifyServerConnexion($id, $data = [])
+ public static function verifyServerConnexion(array $data)
{
- $server = static::find($id);
- $data = [
- 'username' => $data['username'] ?? $server->username,
- 'password' => $data['password'] ?? $server->password,
- 'host' => $data['host'] ?? $server->host,
- 'port' => $data['port'] ?? $server->port,
+ $isHosting = static::isHostingProtocol($data['protocol']);
+
+ $validation = [
+ 'password' => $isHosting ? 'nullable' : 'required|string|min:8',
+ 'username' => $isHosting ? 'nullable' : 'required|string|max:255',
+ 'host' => $isHosting ? 'nullable' : 'required|string|max:255',
+ 'port' => 'nullable|numeric',
];
+ $validator = Validator::make([
+ 'password' => $data['password'],
+ 'username' => $data['username'],
+ 'host' => $data['host'],
+ 'port' => $data['port']], $validation);
+
+ if ($validator->fails()) {
+ return ['error' => true, 'message' => __('Un ou plusieurs champs sont invalides') . ': ' . implode(', ', array_keys($validator->errors()->toArray()))];
+ }
- $result = static::find($id)->getTransferDriver()->checkConnexion($data);
- if ($result === true) {
- $result_['success'] = true;
+ $server = new static();
+ $server->setRawAttributes($data);
+ $check = $server->getTransferDriver()->checkConnexion($data);
+
+ $res = [];
+
+ if ($check === true) {
+ $res['success'] = true;
+ $res['message'] = __("La connexion a été établie avec succès");
} else {
- $result_['error'] = true;
+ $res['error'] = true;
+ $res['message'] = static::messages()[$check];
}
- $result_['message'] = $result === true ? __("La connexion a été établie avec succès") : static::messages()[$result];
- return $result_;
+ return $res;
}
public static function getListFluidbook($id)
__("L'hôte ou le mot de passe est invalide"),
__("La connexion a échouée"),
__("Le dossier n'a pas été monté"),
- __("Le protocol est indisponible"),
+ __("Le protocole n'est pas supporté"),
];
}
+
+
+ /**
+ * @param $protocol
+ * @return bool
+ */
+ public static function isHostingProtocol($protocol)
+ {
+ return in_array($protocol, static::getHostingProtocols());
+ }
}