PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/www/include/options/media/images/syncDir.php

https://gitlab.com/florianocomercial/centreon
PHP | 201 lines | 125 code | 20 blank | 56 comment | 25 complexity | 1f09a6e925646931fcd1c14c77ba521a MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2005-2015 Centreon
  4. * Centreon is developped by : Julien Mathis and Romain Le Merlus under
  5. * GPL Licence 2.0.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it under
  8. * the terms of the GNU General Public License as published by the Free Software
  9. * Foundation ; either version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT ANY
  12. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  13. * PARTICULAR PURPOSE. See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, see <http://www.gnu.org/licenses>.
  17. *
  18. * Linking this program statically or dynamically with other modules is making a
  19. * combined work based on this program. Thus, the terms and conditions of the GNU
  20. * General Public License cover the whole combination.
  21. *
  22. * As a special exception, the copyright holders of this program give Centreon
  23. * permission to link this program with independent modules to produce an executable,
  24. * regardless of the license terms of these independent modules, and to copy and
  25. * distribute the resulting executable under terms of Centreon choice, provided that
  26. * Centreon also meet, for each linked independent module, the terms and conditions
  27. * of the license of that module. An independent module is a module which is not
  28. * derived from this program. If you modify this program, you may extend this
  29. * exception to your version of the program, but you are not obliged to do so. If you
  30. * do not wish to do so, delete this exception statement from your version.
  31. *
  32. * For more information : contact@centreon.com
  33. *
  34. * SVN : $URL$
  35. * SVN : $Id$
  36. *
  37. */
  38. require_once realpath(dirname(__FILE__) . "/../../../../../config/centreon.config.php");
  39. require_once ("./class/centreonDB.class.php");
  40. /**
  41. *
  42. * DB Connection
  43. */
  44. $pearDB = new CentreonDB();
  45. /**
  46. * Counters
  47. */
  48. global $regCounter, $gdCounter, $fileRemoved, $dirCreated;
  49. session_start();
  50. $sid = session_id();
  51. if (!isset($sid))
  52. exit ;
  53. if (isset($sid)) {
  54. $DBRESULT = $pearDB->query("SELECT * FROM session WHERE session_id = '".$pearDB->escape($sid)."'");
  55. if ($DBRESULT->numRows() == 0)
  56. exit();
  57. }
  58. $dir = "./img/media/";
  59. $rejectedDir = array("." => 1, ".." => 1);
  60. $allowedExt = array("jpg" => 1, "jpeg" => 1, "png" => 1, "gif" => 1, "gd2" => 1);
  61. $dirCreated = 0;
  62. $regCounter = 0;
  63. $gdCounter = 0;
  64. if (is_dir($dir)) {
  65. if ($dh = opendir($dir)) {
  66. while (($subdir = readdir($dh)) !== false) {
  67. if (!isset($rejectedDir[$subdir]) && filetype($dir . $subdir) == "dir") {
  68. $dir_id = checkDirectory($subdir, $pearDB);
  69. if ($dh2 = opendir($dir.$subdir)) {
  70. while (($picture = readdir($dh2)) !== false) {
  71. if (!isset($rejectedDir[$picture])) {
  72. checkPicture($picture, $dir.$subdir, $dir_id, $pearDB);
  73. }
  74. }
  75. closedir($dh2);
  76. }
  77. }
  78. }
  79. closedir($dh);
  80. }
  81. }
  82. $fileRemoved = DeleteOldPictures($pearDB);
  83. /*
  84. * Display Stats
  85. */
  86. ?>
  87. <br>
  88. <?php print "<b>&nbsp;&nbsp;"._("Media Detection")."</b>"; ?>
  89. <br><br>
  90. <div style="width:250px;height:50px;margin-left:5px;padding:20px;background-color:#FFFFFF;border:1px #CDCDCD solid;-moz-border-radius:4px;">
  91. <div style='float:left;width:270px;text-align:left;'>
  92. <p>
  93. <?php
  94. print _("Bad picture alias detected :") . " $fileRemoved<br>";
  95. print _("New directory added :") . " $dirCreated<br>";
  96. print _("New images added :") . " $regCounter<br>";
  97. print _("Convert gd2 -> png :") . " $gdCounter<br><br><br>";
  98. ?>
  99. </p>
  100. <br><br><br>
  101. <center><a href='javascript:window.opener.location.reload();javascript:window.close();'><?php print _("Close"); ?></a></center>
  102. </div>
  103. <br>
  104. <?php
  105. /*
  106. * recreates local centreon directories as defined in DB
  107. */
  108. function checkDirectory($dir, $pearDB) {
  109. global $dirCreated;
  110. $DBRESULT = $pearDB->query("SELECT dir_id FROM view_img_dir WHERE dir_alias = '".$dir."'");
  111. if (!$DBRESULT->numRows()) {
  112. $DBRESULT = $pearDB->query("INSERT INTO view_img_dir (`dir_name`, `dir_alias`) VALUES ('".$dir."', '".$dir."')");
  113. @mkdir("./img/media/$dir");
  114. $DBRESULT = $pearDB->query("SELECT dir_id FROM view_img_dir WHERE dir_alias = '".$dir."'");
  115. $data = $DBRESULT->fetchRow();
  116. $dirCreated++;
  117. return $data["dir_id"];
  118. } else {
  119. $data = $DBRESULT->fetchRow();
  120. return $data["dir_id"];
  121. }
  122. }
  123. /*
  124. * inserts $dir_id/$picture into DB if not registered yet
  125. */
  126. function checkPicture($picture, $dirpath, $dir_id, $pearDB) {
  127. global $allowedExt;
  128. global $regCounter, $gdCounter, $fileRemoved, $dirCreated;
  129. $img_info = pathinfo($picture);
  130. $img_ext = $img_info["extension"];
  131. if (!isset($allowedExt[$img_ext])) {
  132. return 0;
  133. }
  134. if (!isset($img_info["filename"])) {
  135. $img_parts = explode(".", $img_info["basename"]);
  136. $img_info["filename"] = $img_parts[0];
  137. }
  138. if ($img_info["extension"] == 'gd2' && !is_file($img_info["filename"] . ".png") ) {
  139. $im = imagecreatefromgd2($dirpath ."/". $picture);
  140. if (!$im) {
  141. return 0;
  142. }
  143. //unlink($picture);
  144. $picture = $img_info["filename"] . ".png";
  145. imagepng($im, $dirpath ."/". $picture);
  146. imagedestroy($im);
  147. $gdCounter++;
  148. }
  149. $DBRESULT = $pearDB->query("SELECT img_id " .
  150. "FROM view_img, view_img_dir_relation vidh " .
  151. "WHERE img_path = '".$picture."' " .
  152. " AND vidh.dir_dir_parent_id = '".$dir_id."'" .
  153. " AND vidh.img_img_id = img_id");
  154. if (!$DBRESULT->numRows()) {
  155. $DBRESULT = $pearDB->query("INSERT INTO view_img (`img_name`, `img_path`) VALUES ('".$img_info["filename"]."', '".$picture."')");
  156. $DBRESULT = $pearDB->query("SELECT img_id FROM view_img WHERE `img_name` = '".$img_info["filename"]."' AND `img_path` = '".$picture."'");
  157. $data = $DBRESULT->fetchRow();
  158. $regCounter++;
  159. $DBRESULT = $pearDB->query("INSERT INTO view_img_dir_relation (`dir_dir_parent_id`, `img_img_id`) VALUES ('".$dir_id."', '".$data['img_id']."')");
  160. return $data['img_id'];
  161. } else {
  162. $data = $DBRESULT->fetchRow();
  163. return 0;
  164. }
  165. }
  166. /*
  167. * removes obsolete files from DB if not on filesystem
  168. */
  169. function DeleteOldPictures($pearDB) {
  170. $fileRemoved = 0;
  171. $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");
  172. while ($row2 = $DBRESULT->fetchRow()) {
  173. if (!file_exists("./img/media/".$row2["dir_alias"]."/".$row2["img_path"])) {
  174. $pearDB->query("DELETE FROM view_img WHERE img_id = '".$row2["img_id"]."'");
  175. $fileRemoved++;
  176. }
  177. }
  178. $DBRESULT->free();
  179. return $fileRemoved;
  180. }
  181. ?>