From fb96b353d9977c6659b65ade04f05c6544d58cd2 Mon Sep 17 00:00:00 2001 From: schapron Date: Fri, 6 Dec 2024 15:55:21 +0100 Subject: [PATCH] test revert - do not merge --- .../options/media/images/listImg.ihtml | 4 +- .../include/options/media/images/listImg.php | 13 +- .../include/options/media/images/syncDir.php | 292 ++++++------------ 3 files changed, 107 insertions(+), 202 deletions(-) diff --git a/centreon/www/include/options/media/images/listImg.ihtml b/centreon/www/include/options/media/images/listImg.ihtml index 91f0d54383..dc0d3a1d12 100644 --- a/centreon/www/include/options/media/images/listImg.ihtml +++ b/centreon/www/include/options/media/images/listImg.ihtml @@ -18,7 +18,7 @@ {$form.o1.html} {$msg.addT} - {$syncDir} + {$syncDir} @@ -67,7 +67,7 @@ {$form.o2.html} {$msg.addT} - {$syncDir} + {$syncDir} {$availiableSpace} {$Available} diff --git a/centreon/www/include/options/media/images/listImg.php b/centreon/www/include/options/media/images/listImg.php index ed91d9ac5c..6842a714c3 100644 --- a/centreon/www/include/options/media/images/listImg.php +++ b/centreon/www/include/options/media/images/listImg.php @@ -150,16 +150,7 @@ ); ?> - + _("More actions"), IMAGE_DELETE => _("Delete"), IMAGE_MOVE => _("Move images")]; $form->addElement('select', 'o1', null, $actions, ['onchange' => "javascript:submitO('o1');"]); diff --git a/centreon/www/include/options/media/images/syncDir.php b/centreon/www/include/options/media/images/syncDir.php index 3e0b44bc99..9073f0a3d0 100644 --- a/centreon/www/include/options/media/images/syncDir.php +++ b/centreon/www/include/options/media/images/syncDir.php @@ -33,13 +33,15 @@ * */ -require_once realpath(__DIR__ . '/../../../../../config/centreon.config.php'); +require_once realpath(__DIR__ . "/../../../../../config/centreon.config.php"); -require_once './class/centreonDB.class.php'; -require_once './class/centreonLog.class.php'; +require_once("./class/centreonDB.class.php"); +/** + * + * DB Connection + */ $pearDB = new CentreonDB(); -$mediaLogInstance = CentreonLog::create(); /** * Counters @@ -47,21 +49,21 @@ global $regCounter, $gdCounter, $fileRemoved, $dirCreated; $sid = session_id(); -if ($sid === false) { +if (!isset($sid)) { exit; } if (isset($sid)) { $DBRESULT = $pearDB->query("SELECT * FROM session WHERE session_id = '" . $pearDB->escape($sid) . "'"); - if ($DBRESULT->rowCount() === 0) { + if ($DBRESULT->rowCount() == 0) { exit(); } } -$dir = './img/media/'; +$dir = "./img/media/"; -$rejectedDir = ['.' => 1, '..' => 1]; -$allowedExt = ['jpg' => 1, 'jpeg' => 1, 'png' => 1, 'gif' => 1, 'gd2' => 1]; +$rejectedDir = ["." => 1, ".." => 1]; +$allowedExt = ["jpg" => 1, "jpeg" => 1, "png" => 1, "gif" => 1, "gd2" => 1]; $dirCreated = 0; $regCounter = 0; @@ -70,31 +72,12 @@ if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($subdir = readdir($dh)) !== false) { - if (! isset($rejectedDir[$subdir]) && filetype($dir . $subdir) === 'dir') { - try { - $dir_id = checkDirectory($subdir, $pearDB); - } catch (Exception $ex) { - $mediaLogInstance->error( - CentreonLog::TYPE_BUSINESS_LOG, - $ex->getMessage(), - [], - $ex - ); - continue; - } + if (!isset($rejectedDir[$subdir]) && filetype($dir . $subdir) == "dir") { + $dir_id = checkDirectory($subdir, $pearDB); if ($dh2 = opendir($dir . $subdir)) { while (($picture = readdir($dh2)) !== false) { - if (! isset($rejectedDir[$picture])) { - try { - checkPicture($picture, $dir . $subdir, $dir_id, $pearDB); - } catch (Exception $ex) { - $mediaLogInstance->error( - CentreonLog::TYPE_BUSINESS_LOG, - $ex->getMessage(), - [], - $ex - ); - } + if (!isset($rejectedDir[$picture])) { + checkPicture($picture, $dir . $subdir, $dir_id, $pearDB); } } closedir($dh2); @@ -105,31 +88,26 @@ } } -$fileRemoved = deleteOldPictures($pearDB); +$fileRemoved = DeleteOldPictures($pearDB); -// Display Stats +/* + * Display Stats + */ ?> -
  ' . _('Media Detection') . ''; +print "  " . _("Media Detection") . ""; ?>

"; - echo _('New directory added :') . " {$dirCreated}
"; - echo _('New images added :') . " {$regCounter}
"; - echo _('Convert gd2 -> png :') . " {$gdCounter}


"; + print _("Bad picture alias detected :") . " $fileRemoved
"; + print _("New directory added :") . " $dirCreated
"; + print _("New images added :") . " $regCounter
"; + print _("Convert gd2 -> png :") . " $gdCounter


"; ?>




@@ -140,181 +118,117 @@ function reloadAndClose() {
prepareQuery( - 'SELECT dir_id FROM view_img_dir WHERE dir_alias = :directory' + $DBRESULT = $pearDB->query( + "SELECT dir_id FROM view_img_dir WHERE dir_alias = '" . $dir . "'" ); - $db->executePreparedQuery($statement, [':directory' => $directory]); - $directoryId = $db->fetchColumn($statement); - if ($directoryId === false) { - $statement = $db->prepareQuery(<<<'SQL' - INSERT INTO view_img_dir (`dir_name`, `dir_alias`) - VALUES (:directory, :directory) - SQL + if (!$DBRESULT->rowCount()) { + $DBRESULT = $pearDB->query( + "INSERT INTO view_img_dir (`dir_name`, `dir_alias`) VALUES ('" . $dir . "', '" . $dir . "')" ); - $db->executePreparedQuery($statement, [':directory' => $directory]); - $directoryId = $db->lastInsertId(); - @mkdir("./img/media/{$directory}"); + @mkdir("./img/media/$dir"); + $DBRESULT = $pearDB->query("SELECT dir_id FROM view_img_dir WHERE dir_alias = '" . $dir . "'"); + $data = $DBRESULT->fetchRow(); $dirCreated++; + return $data["dir_id"]; + } else { + $data = $DBRESULT->fetchRow(); + return $data["dir_id"]; } - - return $directoryId; } - /** - * Inserts $dir_id/$picture into DB if not registered yet - * - * @param string $imagePath - * @param string $directoryPath - * @param int $directoryId - * @param CentreonDB $pearDB - * - * @throws CentreonDbException - * @return void + /* + * inserts $dir_id/$picture into DB if not registered yet */ - function checkPicture( - string $imagePath, - string $directoryPath, - int $directoryId, - CentreonDB $pearDB - ): void { - global $allowedExt, $regCounter, $gdCounter; - [$filename, $extension] = extractFileInfo($imagePath); - if ($filename === '') { - return; - } + function checkPicture($picture, $dirpath, $dir_id, $pearDB) + { + global $allowedExt; + global $regCounter, $gdCounter, $fileRemoved, $dirCreated; - if (! isset($allowedExt[$extension])) { - return; - } + $img_info = pathinfo($picture); + $img_ext = $img_info["extension"]; - if ($extension === 'gd2' && ! is_file($filename . '.png')) { - convertGd2ToPng($directoryPath, $imagePath); - $gdCounter++; + if (!isset($allowedExt[$img_ext])) { + return 0; } - $statement = $pearDB->prepareQuery(<<<'SQL' - SELECT 1 - FROM view_img - INNER JOIN view_img_dir_relation idr - ON idr.img_img_id = img_id - WHERE img_path = :img_path - AND idr.dir_dir_parent_id = :parent_id - SQL - ); - $pearDB->executePreparedQuery( - $statement, - [ - ':img_path' => $filename . '.' . $extension, - ':parent_id' => $directoryId, - ] - ); - - if (! $statement->rowCount()) { - $pearDB->beginTransaction(); - try { - $pearDB->executePreparedQuery( - $pearDB->prepareQuery( - 'INSERT INTO view_img (`img_name`, `img_path`) VALUES (:img_name, :img_path)' - ), [ - ':img_name' => $filename, - ':img_path' => $imagePath, - ] - ); - - $imageId = $pearDB->lastInsertId(); - $statement = $pearDB->prepareQuery( - <<<'SQL' - INSERT INTO view_img_dir_relation (`dir_dir_parent_id`, `img_img_id`) - VALUES (:parent_id, :img_id) - SQL - ); - $pearDB->executePreparedQuery( - $statement, [ - ':parent_id' => $directoryId, - ':img_id' => $imageId, - ] - ); - $pearDB->commit(); - $regCounter++; - } catch (Exception $ex) { - $pearDB->rollBack(); - throw $ex; - } + if (!isset($img_info["filename"])) { + $img_parts = explode(".", $img_info["basename"]); + $img_info["filename"] = $img_parts[0]; } - } - - /** - * Extracts file info from a picture - * - * @param string $picture - * @return array{0: string, 1: string} [filename, extension] - */ - function extractFileInfo(string $picture): array - { - $fileInfo = pathinfo($picture); - if (! isset($fileInfo['filename'])) { - $basenameDetails = explode('.', $fileInfo['basename']); - $fileInfo['filename'] = $basenameDetails[0]; + if ($img_info["extension"] == 'gd2' && !is_file($img_info["filename"] . ".png")) { + $im = imagecreatefromgd2($dirpath . "/" . $picture); + if (!$im) { + return 0; + } + //unlink($picture); + $picture = $img_info["filename"] . ".png"; + imagepng($im, $dirpath . "/" . $picture); + imagedestroy($im); + $gdCounter++; } - return [ - $fileInfo['filename'], - $fileInfo['extension'], - ]; - } - - function convertGd2ToPng(string $directoryPath, string $picture): void - { - $gdImage = imagecreatefromgd2($directoryPath . '/' . $picture); - if (! $gdImage) { - return; + $statement = $pearDB->prepare( + "SELECT img_id " . + "FROM view_img, view_img_dir_relation vidh " . + "WHERE img_path = :img_path " . + "AND vidh.dir_dir_parent_id = :dir_dir_parent_id " . + "AND vidh.img_img_id = img_id" + ); + $statement->bindValue(':img_path', $picture, \PDO::PARAM_STR); + $statement->bindValue(':dir_dir_parent_id', (int) $dir_id, \PDO::PARAM_INT); + $statement->execute(); + if (!$statement->rowCount()) { + $DBRESULT = $pearDB->query( + "INSERT INTO view_img (`img_name`, `img_path`) VALUES ('" + . $img_info["filename"] . "', '" . $picture . "')" + ); + $DBRESULT = $pearDB->query( + "SELECT img_id FROM view_img WHERE `img_name` = '" + . $img_info["filename"] . "' AND `img_path` = '" . $picture . "'" + ); + $data = $DBRESULT->fetchRow(); + $regCounter++; + $statement = $pearDB->prepare( + "INSERT INTO view_img_dir_relation (`dir_dir_parent_id`, `img_img_id`) + VALUES (:dir_dir_parent_id, :img_img_id)" + ); + $statement->bindValue(':dir_dir_parent_id', (int) $dir_id, \PDO::PARAM_INT); + $statement->bindValue(':img_img_id', (int) $data['img_id'], \PDO::PARAM_INT); + $statement->execute(); + return $data['img_id']; + } else { + $data = $statement->fetchRow(\PDO::FETCH_ASSOC); + return 0; } - [$filename] = extractFileInfo($picture); - imagepng($gdImage, $directoryPath . '/' . $filename . '.png'); - imagedestroy($gdImage); } - /** - * Removes obsolete files from DB if not on filesystem. - * - * @param CentreonDB $pearDB - * - * @return int Number of files removed + /* + * removes obsolete files from DB if not on filesystem */ - function deleteOldPictures(CentreonDB $pearDB): int + function DeleteOldPictures($pearDB) { $fileRemoved = 0; $DBRESULT = $pearDB->query( - 'SELECT img_id, img_path, dir_alias FROM view_img vi, ' - . 'view_img_dir vid, view_img_dir_relation vidr ' - . 'WHERE vidr.img_img_id = vi.img_id AND vid.dir_id = vidr.dir_dir_parent_id' + "SELECT img_id, img_path, dir_alias FROM view_img vi, " + . "view_img_dir vid, view_img_dir_relation vidr " + . "WHERE vidr.img_img_id = vi.img_id AND vid.dir_id = vidr.dir_dir_parent_id" ); - $statement = $pearDB->prepare('DELETE FROM view_img WHERE img_id = :img_id'); + $statement = $pearDB->prepare("DELETE FROM view_img WHERE img_id = :img_id"); while ($row2 = $DBRESULT->fetchRow()) { - if (! file_exists('./img/media/' . $row2['dir_alias'] . '/' . $row2['img_path'])) { - $statement->bindValue(':img_id', (int) $row2['img_id'], PDO::PARAM_INT); + if (!file_exists("./img/media/" . $row2["dir_alias"] . "/" . $row2["img_path"])) { + $statement->bindValue(':img_id', (int) $row2["img_id"], \PDO::PARAM_INT); $statement->execute(); $fileRemoved++; } } $DBRESULT->closeCursor(); - return $fileRemoved; } ?> -