<?php
namespace App\Controller;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class AccueilController extends AbstractController
{
public function __construct(
AdminUrlGenerator $adminUrlGenerator
) {
$this->adminUrlGenerator = $adminUrlGenerator;
}
/**
* @Route("/", name="app_home")
*/
public function index(): Response
{
return $this->redirectToRoute('admin');
/* return $this->render('accueil/index.html.twig', [
'controller_name' => 'AccueilController',
]); */
}
// get_dirs_content
public function htmlNiveauIndicator($dirname, $racine)
{
$res = $this->getParentDirs($dirname, $racine);
$html = "";
foreach ($res['name'] as $key => $value) {
$html .= '<button type="button" class="btn btn-secondary mx-1" onclick="getdirfiles(\'' . $res['path'][$key] . '\', \'valid\')">' . $value . '</button>';
if (($key + 1) < count($res['name'])) {
$html .= '<i class="fa fa-chevron-right" style="color: #cbc8c8;"></i>';
}
}
return $html;
}
public function htmlDirCRes($dirname, $selected, $racine)
{
$res = $this->getDirs($dirname, $racine);
$html = '<div class="my-2 p-2">';
$html .= '<div class="p-2">';
$html .= '<div class="card p-2">';
$html .= '<div class="d-flex flex-wrap">';
$html .= '<i class="bi bi-arrow-return-right"></i>';
foreach ($res["dir"] as $value) {
$html .= '<button type="button" class="btn btn-primary mx-1" onclick="getdirfiles(\'' . $res["filePath"] . $value . '/\', \'valid\')">' . $value . '</button>';
}
$html .= '</div>';
$html .= '</div>';
$html .= '';
$html .= '<i class="bi bi-arrow-bar-down"></i>';
$html .= '<div class="card p-2">';
$html .= '<div class="d-flex flex-wrap">';
//dump($selected);
foreach ($res["file"] as $value) {
//dump($value);
$html .= '<div class="m-2" style="width: 200px; height: fit-content;">';
$imgPath = $res["filePath"];
if ($racine != '') {
if (strpos($res["filePath"], $racine) == 0) {
$imgPath = substr_replace($res["filePath"], "", 0, strlen($racine));
}
}
if (strpos($imgPath, "/") != 0) {
$imgPath = "/" . $imgPath;
}
if ($this->getUser()->getProjet()->getSwitchEnLigne()) {
if ($selected == ("/" . $value)) {
$html .= '<img id="imagegellery" src="' . $this->getUser()->getProjet()->getImageHttpServerEnligne() . $imgPath . $value . '" data-racine="' . $this->getUser()->getProjet()->getImageHttpServerEnligne() . '" data-dir="' . $imgPath . $value . '" onclick="selectedimage(this)" class="img-thumbnail selectedFtpImg" style="width: 100%; height: fit-content; background-color: white; border: 1px solid #5656b5; border-color: #5656b5; background-color: #5656b5" loading="lazy">';
} else {
$html .= '<img id="imagegellery" src="' . $this->getUser()->getProjet()->getImageHttpServerEnligne() . $imgPath . $value . '" data-racine="' . $this->getUser()->getProjet()->getImageHttpServerEnligne() . '" data-dir="' . $imgPath . $value . '" onclick="selectedimage(this)" class="img-thumbnail selectedFtpImg" style="width: 100%; height: fit-content; background-color: white; border: 1px;" loading="lazy">';
}
} else {
if ($selected == ("/" . $value)) {
$html .= '<img id="imagegellery" src="' . $this->getUser()->getProjet()->getImageHttpServer() . $imgPath . $value . '" data-racine="' . $this->getUser()->getProjet()->getImageHttpServer() . '" data-dir="' . $imgPath . $value . '" onclick="selectedimage(this)" class="img-thumbnail selectedFtpImg" style="width: 100%; height: fit-content; background-color: white; border: 1px solid #5656b5; border-color: #5656b5; background-color: #5656b5" loading="lazy">';
} else {
$html .= '<img id="imagegellery" src="' . $this->getUser()->getProjet()->getImageHttpServer() . $imgPath . $value . '" data-racine="' . $this->getUser()->getProjet()->getImageHttpServer() . '" data-dir="' . $imgPath . $value . '" onclick="selectedimage(this)" class="img-thumbnail selectedFtpImg" style="width: 100%; height: fit-content; background-color: white; border: 1px;" loading="lazy">';
}
}
// $html .= '<img src="' . $this->getUser()->getProjet()->getImageHttpServer() . $res["filePath"] . $value . '" data-racine="' . $this->getUser()->getProjet()->getImageHttpServer() . '" data-dir="' . $res["filePath"] . $value . '" onclick="selectedimage(this)" class="img-thumbnail selectedFtpImg" style="width: fit-content; height: fit-content; background-color: white; border: 1px;" loading="lazy">';
$html .= '</div>';
}
$html .= '</div>';
$html .= '</div>';
$html .= '</div>';
$html .= '<script>checkImageGalleyExt()</script>';
return $html;
}
public function getParentDirs($dirname, $racine)
{
$parentsName = explode("/", $dirname);
$parents = [];
$paths = [];
$path = "";
$parents[] = '<i class="bi bi-house-door-fill"></i>';
$paths[] = $racine . "/";
$npaths = [];
$nparents = [];
foreach ($parentsName as $value) {
if ($value != '') {
array_push($parents, $value);
$path .= $value . "/";
array_push($paths, $path);
}
}
if ($racine != '') {
foreach ($paths as $key => $value) {
if ($key != 1) {
array_push($npaths, $value);
array_push($nparents, $parents[$key]);
}
}
} else {
$npaths = $paths;
$nparents = $parents;
}
$parentsName['name'] = $nparents;
$parentsName['path'] = $npaths;
return $parentsName;
}
public function getDirs($dirName, $racine)
{
if ($this->getUser()->getProjet()->getSwitchEnLigne()) {
$ftp_server = $this->getUser()->getProjet()->getImageFtpServerEnligne();
$ftp_username = $this->getUser()->getProjet()->getImageFtpLoginEnligne();
$ftp_userpass = $this->getUser()->getProjet()->getImageFtpMdpEnligne();
} else {
$ftp_server = $this->getUser()->getProjet()->getImageFtpServer();
$ftp_username = $this->getUser()->getProjet()->getImageFtpLogin();
$ftp_userpass = $this->getUser()->getProjet()->getImageFtpMdp();
}
if (str_contains($ftp_server, '/')) {
$pos = strpos($ftp_server, '/');
$racine = substr($ftp_server, $pos);
$ftp_server = str_replace($racine, "", $ftp_server);
}
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
ftp_set_option($ftp_conn, FTP_USEPASVADDRESS, false); // set ftp option
ftp_pasv($ftp_conn, true); //make connection to passive mode
$filesPath = "";
$dirs = $this->ftp_mlsd_dir($ftp_conn, $dirName);
$files = $this->ftp_mlsd_file($ftp_conn, $dirName);
$filesPath = $dirName;
/* if (strpos($dirName, $racine) == 0) {
$filesPath = substr_replace($dirName, "", 0, strlen($racine));
} */
$dirlst = [];
$dirlst['dir'] = $dirs;
$dirlst['file'] = $files;
$dirlst['filePath'] = $filesPath;
// close connection
ftp_close($ftp_conn);
return $dirlst;
}
public function ftp_mlsd_dir($ftp_stream, $directory)
{
$result = [];
$files = ftp_mlsd($ftp_stream, $directory);
if ($files === false) {
die("Cannot list $directory");
}
if (count($files) != 0) {
foreach ($files as $file) {
if ($file["type"] == "dir") {
$name = $file["name"];
array_push($result, $name);
}
}
}
return $result;
}
public function ftp_mlsd_file($ftp_stream, $directory)
{
$result = [];
$files = ftp_mlsd($ftp_stream, $directory);
if ($files === false) {
die("Cannot list $directory");
}
if (count($files) != 0) {
foreach ($files as $file) {
$name = $file["name"];
if ($file["type"] == "file") {
array_push($result, $name);
}
}
}
return $result;
}
/**
* @Route("/get/img/upload", name="get_image_upload")
*/
public function uploadImageFtp(Request $request)
{
$file = $request->get('file');
$fileName = $request->get('name');
if ($this->getUser()->getProjet()->getSwitchEnLigne()) {
$ftp_server = $this->getUser()->getProjet()->getImageFtpServerEnligne();
$ftp_username = $this->getUser()->getProjet()->getImageFtpLoginEnligne();
$ftp_userpass = $this->getUser()->getProjet()->getImageFtpMdpEnligne();
} else {
$ftp_server = $this->getUser()->getProjet()->getImageFtpServer();
$ftp_username = $this->getUser()->getProjet()->getImageFtpLogin();
$ftp_userpass = $this->getUser()->getProjet()->getImageFtpMdp();
}
//var_dump($this->csvService->importPage('../csv/2022-05-18-16-49 devil app pro/page.csv', $projet));
$racine = "";
if (str_contains($ftp_server, '/')) {
$pos = strpos($ftp_server, '/');
$racine = substr($ftp_server, $pos);
$ftp_server = str_replace($racine, "", $ftp_server);
}
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
ftp_set_option($ftp_conn, FTP_USEPASVADDRESS, false); // set ftp option
ftp_pasv($ftp_conn, true); //make connection to passive mode
// $file = 'newimg/' . $fileName;
$data = explode(',', $file);
$fp = fopen($fileName, "w+");
fwrite($fp, base64_decode($data[1]));
fclose($fp);
if (ftp_nlist($ftp_conn, $racine . "/upload") == false) {
ftp_mkdir($ftp_conn, $racine . "/upload");
}
$fp2 = fopen($fileName, "r");
if (ftp_fput($ftp_conn, $racine . "/upload/" . $fileName, $fp2)) {
$returnstat = $this->json(['msg' => 'success']);
} else {
$returnstat = $this->json(['msg' => 'non']);
}
ftp_close($ftp_conn);
fclose($fp2);
unlink($fileName);
return $returnstat;
}
/**
* @Route("/get/dirs/projet/logo/old", name="get_dirs_content_projet_logoold")
*/
public function getDirsContentProjetLogo(Request $request)
{
$dirname = $request->get('dirname');
$selected = $request->get('selected');
$inputv = $request->get('validinput');
if ($this->getUser()->getProjet()->getSwitchEnLigne()) {
$imageFtpServer = $this->getUser()->getProjet()->getImageFtpServerEnligne();
} else {
$imageFtpServer = $this->getUser()->getProjet()->getImageFtpServer();
}
$racine = "";
if (str_contains($imageFtpServer, '/')) {
$pos = strpos($imageFtpServer, '/');
$racine = substr($imageFtpServer, $pos);
}
if ($inputv == 'nnvalid' && $dirname != "") {
$dirname = $racine . $dirname;
} else if ($dirname == "") {
$dirname = $racine . "/";
}
/* if ($dirname == '') {
$dirname = '/loue-ton-box/';
} */
$content = $this->htmlDirCResProjetLogo($dirname, $selected, $racine);
$niveauindicator = $this->htmlNiveauIndicatorProjetLogo($dirname, $racine);
return $this->json(['content' => $content, 'niveauindicator' => $niveauindicator]);
}
public function htmlNiveauIndicatorProjetLogo($dirname, $racine)
{
$res = $this->getParentDirs($dirname, $racine);
$html = "";
foreach ($res['name'] as $key => $value) {
$html .= '<button type="button" class="btn btn-secondary mx-1" onclick="getdirfilesprojetlogo(\'' . $res['path'][$key] . '\', \'valid\')">' . $value . '</button>';
if (($key + 1) < count($res['name'])) {
$html .= '<i class="fa fa-chevron-right" style="color: #cbc8c8;"></i>';
}
}
return $html;
}
public function htmlDirCResProjetLogo($dirname, $selected, $racine)
{
$res = $this->getDirs($dirname, $racine);
$html = '<div class="my-2 p-2">';
$html .= '<div class="p-2">';
$html .= '<div class="card p-2">';
$html .= '<div class="d-flex flex-wrap">';
$html .= '<i class="bi bi-arrow-return-right"></i>';
foreach ($res["dir"] as $value) {
$html .= '<button type="button" class="btn btn-primary mx-1" onclick="getdirfilesprojetlogo(\'' . $res["filePath"] . $value . '/\', \'valid\')">' . $value . '</button>';
}
$html .= '</div>';
$html .= '</div>';
$html .= '';
$html .= '<i class="bi bi-arrow-bar-down"></i>';
$html .= '<div class="card p-2">';
$html .= '<div class="d-flex flex-wrap">';
//dump($selected);
foreach ($res["file"] as $value) {
//dump($value);
$html .= '<div class="m-2" style="width: 200px; height: fit-content;">';
$imgPath = $res["filePath"];
if ($racine != '') {
if (strpos($res["filePath"], $racine) == 0) {
$imgPath = substr_replace($res["filePath"], "", 0, strlen($racine));
}
}
if (strpos($imgPath, "/") != 0) {
$imgPath = "/" . $imgPath;
}
if ($this->getUser()->getProjet()->getSwitchEnLigne()) {
if ($selected == ("/" . $value)) {
$html .= '<img id="imagegelleryprojectlogo" src="' . $this->getUser()->getProjet()->getImageHttpServerEnligne() . $imgPath . $value . '" data-racine="' . $this->getUser()->getProjet()->getImageHttpServerEnligne() . '" data-dir="' . $imgPath . $value . '" onclick="selectedimageprojetlogo(this)" class="img-thumbnail selectedFtpImg" style="width: 100%; height: fit-content; background-color: white; border: 1px solid #5656b5; border-color: #5656b5; background-color: #5656b5" loading="lazy">';
} else {
$html .= '<img id="imagegelleryprojectlogo" src="' . $this->getUser()->getProjet()->getImageHttpServerEnligne() . $imgPath . $value . '" data-racine="' . $this->getUser()->getProjet()->getImageHttpServerEnligne() . '" data-dir="' . $imgPath . $value . '" onclick="selectedimageprojetlogo(this)" class="img-thumbnail selectedFtpImg" style="width: 100%; height: fit-content; background-color: white; border: 1px;" loading="lazy">';
}
} else {
if ($selected == ("/" . $value)) {
$html .= '<img id="imagegelleryprojectlogo" src="' . $this->getUser()->getProjet()->getImageHttpServer() . $imgPath . $value . '" data-racine="' . $this->getUser()->getProjet()->getImageHttpServer() . '" data-dir="' . $imgPath . $value . '" onclick="selectedimageprojetlogo(this)" class="img-thumbnail selectedFtpImg" style="width: 100%; height: fit-content; background-color: white; border: 1px solid #5656b5; border-color: #5656b5; background-color: #5656b5" loading="lazy">';
} else {
$html .= '<img id="imagegelleryprojectlogo" src="' . $this->getUser()->getProjet()->getImageHttpServer() . $imgPath . $value . '" data-racine="' . $this->getUser()->getProjet()->getImageHttpServer() . '" data-dir="' . $imgPath . $value . '" onclick="selectedimageprojetlogo(this)" class="img-thumbnail selectedFtpImg" style="width: 100%; height: fit-content; background-color: white; border: 1px;" loading="lazy">';
}
}
// $html .= '<img src="' . $this->getUser()->getProjet()->getImageHttpServer() . $res["filePath"] . $value . '" data-racine="' . $this->getUser()->getProjet()->getImageHttpServer() . '" data-dir="' . $res["filePath"] . $value . '" onclick="selectedimage(this)" class="img-thumbnail selectedFtpImg" style="width: fit-content; height: fit-content; background-color: white; border: 1px;" loading="lazy">';
$html .= '</div>';
}
$html .= '</div>';
$html .= '</div>';
$html .= '</div>';
$html .= '<script>checkImageGalleyExtProjectLogo()</script>';
return $html;
}
}