Site de la semaine d'accueil 2023
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

photos.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. ob_start(); // Start reading html
  3. //header('Location: construction.php');
  4. define("urlParam", "path");
  5. define("photoRoot", "assets/img/com_photo/thumbs");
  6. define("photoRootHQ", "assets/img/com_photo/full_q");
  7. /**
  8. * Get active path from url and prevent from seeing folders before 'photos/'
  9. * @return string current path
  10. */
  11. function getActivePath()
  12. {
  13. $dir = '';
  14. if (isset($_GET[urlParam]))
  15. $dir = $_GET[urlParam];
  16. $folders = explode(DIRECTORY_SEPARATOR, $dir);
  17. $currentPath = "";
  18. foreach ($folders as $value) {
  19. if ($value != ".." && $value != "." && $value != "") {
  20. $currentPath .= DIRECTORY_SEPARATOR . $value;
  21. }
  22. }
  23. return $currentPath;
  24. }
  25. /**
  26. * Get active folder from the active path
  27. * @param string $path path representing the active folder
  28. * @return string active folder name
  29. */
  30. function GetActiveFolder($path)
  31. {
  32. $dir = explode(DIRECTORY_SEPARATOR, $path);
  33. return ".".DIRECTORY_SEPARATOR.$dir[sizeof($dir) - 1]; // Last item after /
  34. }
  35. /**
  36. * Check whether the current album is available for download as a .zip file
  37. * @param string $path path to search the album in
  38. * @return bool True if an album is available, false otherwise
  39. */
  40. function isAlbumAvailable($path)
  41. {
  42. $dir = photoRoot . $path;
  43. $files = scandir($dir);
  44. $valid = false;
  45. foreach ($files as $key => $value) {
  46. $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
  47. if (!is_dir($path)) {
  48. $valid = pathinfo($path, PATHINFO_EXTENSION) == "zip";
  49. if ($valid)
  50. break;
  51. }
  52. }
  53. return $valid;
  54. }
  55. /**
  56. * Get all directories in the specified path and creates them on the page
  57. * @param string $path path to search directories in
  58. */
  59. function createDirectories($path)
  60. {
  61. $path = photoRoot . $path;
  62. $displayedItems = 0;
  63. $folders = getDirectories($path);
  64. foreach ($folders as $key => $value) {
  65. $folderTitle = $value;
  66. $photos = getTotalPhotoCount($path . DIRECTORY_SEPARATOR . $value);
  67. $albums = getTotalAlbumCount($path . DIRECTORY_SEPARATOR . $value);
  68. $folderLink = "?" . urlParam . "=" . getActivePath() . DIRECTORY_SEPARATOR . $value;
  69. ?>
  70. <a href="<?= $folderLink ?>">
  71. <li class="directory-li">
  72. <span id="folderTitle"><?= str_replace("_", " ", $folderTitle) ?></span>
  73. <span id="folderPhotos">
  74. <?php if ($photos > 1) {
  75. echo " - ".$photos." photos";
  76. } elseif ($photos == 1) {
  77. echo " - ".$photos ." photo";
  78. } else {
  79. echo " - "."vide";
  80. } ?>
  81. </span>
  82. <span id="folderAlbums">
  83. <?php if ($albums > 1) {
  84. echo " - ".$albums." albums";
  85. } elseif ($albums == 1) {
  86. echo " - ".$albums." album";
  87. } ?>
  88. </span>
  89. </li>
  90. </a>
  91. <?php
  92. $displayedItems++;
  93. }
  94. }
  95. /**
  96. * Get all photos in the specified path and creates them on the page
  97. * @param string $path path to search photos in
  98. */
  99. function createPhotos($path_in)
  100. {
  101. $path = photoRoot . $path_in;
  102. $path_no_thumb = photoRootHQ . $path_in;
  103. $files = scandir($path);
  104. $displayedItems = 0;
  105. foreach ($files as $key => $value) {
  106. $realPath = realpath($path . DIRECTORY_SEPARATOR . $value);
  107. $realPath_no_thumb = realpath($path_no_thumb . DIRECTORY_SEPARATOR . $value);
  108. if (isValidImage($realPath) AND isValidImage($realPath_no_thumb)) {
  109. $imageSrc = $path . DIRECTORY_SEPARATOR . $value;
  110. $img_no_thumb = $path_no_thumb . DIRECTORY_SEPARATOR . $value;
  111. $imageId = "photo-" . $displayedItems;
  112. ?><a download="" href="<?= $img_no_thumb ?>"><img src="<?= $imageSrc ?>" class="photo" id="<?= $imageId ?>" alt=""/></a><?php
  113. $displayedItems++;
  114. }
  115. }
  116. }
  117. /**
  118. * get folders in the given path
  119. * @param string $path path to search folders in
  120. * @return array array of folders
  121. */
  122. function getDirectories($path)
  123. {
  124. $files = scandir($path);
  125. $folders = [];
  126. foreach ($files as $key => $value) {
  127. $realPath = realpath($path . DIRECTORY_SEPARATOR . $value);
  128. if (isValidDirectory($realPath, $value)) {
  129. array_push($folders, $value);
  130. }
  131. }
  132. return $folders;
  133. }
  134. /**
  135. * Counts directories in the specified folder
  136. * @param string $path path to search directories in
  137. * @return int directories count
  138. */
  139. function getDirectoriesCount($path)
  140. {
  141. $files = scandir($path);
  142. $dirCount = 0;
  143. foreach ($files as $key => $value) {
  144. $realPath = realpath($path . DIRECTORY_SEPARATOR . $value);
  145. if (isValidDirectory($realPath, $value)) {
  146. $dirCount++;
  147. }
  148. }
  149. return $dirCount;
  150. }
  151. /**
  152. * Counts images in the specified folder
  153. * @param string $path path to search photos in
  154. * @return int photo count
  155. */
  156. function getPhotoCount($path)
  157. {
  158. $files = scandir($path);
  159. $fileCount = 0;
  160. foreach ($files as $key => $value) {
  161. $realPath = realpath($path . DIRECTORY_SEPARATOR . $value);
  162. if (isValidImage($realPath)) {
  163. $fileCount++;
  164. }
  165. }
  166. return $fileCount;
  167. }
  168. /**
  169. * Get the count of all directories, recursively from the path specified
  170. * @param string $path root for search
  171. * @return int total number of directories
  172. */
  173. function getTotalAlbumCount($path)
  174. {
  175. $folders = getDirectories($path);
  176. $total = sizeof($folders);
  177. foreach ($folders as $key => $value) {
  178. $total += getTotalAlbumCount($path . DIRECTORY_SEPARATOR . $value);
  179. }
  180. return $total;
  181. }
  182. function getTotalPhotoCount($path)
  183. {
  184. $folders = getDirectories($path);
  185. $total = getPhotoCount($path);
  186. foreach ($folders as $key => $value) {
  187. $total += getTotalPhotoCount($path . DIRECTORY_SEPARATOR . $value);
  188. }
  189. return $total;
  190. }
  191. /**
  192. * Check if the given image is valid
  193. * @param string $imagePath absolute path of the image
  194. * @return bool True if the file is a jpg, jpeg or png, false otherwise
  195. */
  196. function isValidImage($imagePath)
  197. {
  198. $ext = pathinfo($imagePath, PATHINFO_EXTENSION);
  199. return !is_dir($imagePath) && ($ext == "jpg" || $ext == "JPG" || $ext == "jpeg" || $ext == "JPEG" || $ext == "png" || $ext == "PNG");
  200. }
  201. /**
  202. * Check if the given folder is valid (is not '.' or '..')
  203. * @param string $directoryPath directory path
  204. * @param string $directory directory name
  205. * @return bool True if the directory is valid, false otherwise
  206. */
  207. function isValidDirectory($directoryPath, $directory)
  208. {
  209. return is_dir($directoryPath) && $directory != "." && $directory != ".." && substr($directory, 0, 1) !== ".";
  210. }
  211. /**
  212. * Creates buttons representing the actual path for easier navigation
  213. * @param string $path Actual Path
  214. */
  215. function generatePath($path)
  216. {
  217. $folders = explode(DIRECTORY_SEPARATOR, $path);
  218. $currentPath = "";
  219. $pathTitle = "Menu";
  220. $pathLink = "?" . urlParam . "=";
  221. echo '<li><a href="'.$pathLink.'" class="link">'.str_replace("_", " ", $pathTitle).'</a></li>';
  222. foreach ($folders as $value) {
  223. if ($value != "") {
  224. $pathTitle = $value;
  225. $currentPath .= DIRECTORY_SEPARATOR . $value;
  226. $pathLink = "?" . urlParam . "=" . $currentPath;
  227. echo ' > <li><a href="'.$pathLink.'" class="link">'.str_replace("_", " ", $pathTitle).'</a></li>';
  228. }
  229. }
  230. }
  231. //CAS
  232. require_once("phpCAS-1.3.6/CAS.php");
  233. phpCAS::client(CAS_VERSION_2_0, "cas.insa-toulouse.fr", 443, 'cas', true);
  234. phpCAS::setNoCasServerValidation();
  235. // phpCAS::setCasServerCACert($cas_server_ca_cert_path);
  236. phpCAS::forceAuthentication();
  237. if (isset($_REQUEST['logout'])) {
  238. phpCAS::logout();
  239. }
  240. ?>
  241. <div class="inner">
  242. <div class="zone_txt">
  243. <font color="red">
  244. <h1>Vous êtes connecté !</h1>
  245. <p>Connecté en tant que : <b><?php echo phpCAS::getUser(); ?></b>.</p>
  246. <p>Vous pouvez vous déconnecter en cliquant ici :<a href="?logout=" class="link">Se déconnecter</a></p>
  247. </font><br><br>
  248. <p>Clique sur le dossier de ton choix pour afficher les photos. Il faut que tu sois inscrit à l'INSA pour pouvoir
  249. les regarder (et oui, pas de spoiler).</p>
  250. <p>Il te suffit ensuite de cliquer sur la photo que tu veux télécharger.</p>
  251. <p>Si tu ne peux pas voir les photos (la fenêtre pour entrer le mot de passe ne s'affiche pas), ouvre cette page avec un autre navigateur.</p>
  252. </div>
  253. <div class="zone_txt" align="left">
  254. <ul class="photos-path">
  255. <li>Chemin : </li>
  256. <?php
  257. generatePath(getActivePath());
  258. ?>
  259. </ul>
  260. <?php if (isAlbumAvailable(getActivePath())): ?>
  261. <a download="" href="assets/com_photo/"<?php echo getActivePath().DIRECTORY_SEPARATOR.GetActiveFolder(getActivePath()); ?>.zip" id="downloadAlbum">
  262. <span id="downloadText">Télécharger</span>
  263. <span id="albumPhotoCount"><?php echo getTotalPhotoCount(photoRoot . getActivePath()) ?> photos</span>
  264. </a>
  265. <?php endif; ?>
  266. <ul class="directory-ul">
  267. <?php if (getDirectoriesCount(photoRoot . getActivePath()) > 0): ?>
  268. <div class="photos-folder-container">
  269. <?php
  270. createDirectories(getActivePath());
  271. ?>
  272. </div>
  273. <?php endif; ?>
  274. </ul>
  275. <?php if (getPhotoCount(photoRoot . getActivePath()) > 0): ?>
  276. <div class="photos">
  277. <?php
  278. createPhotos(getActivePath());
  279. ?>
  280. </div>
  281. <?php endif; ?>
  282. </div class="zone_txt">
  283. </div>
  284. <br>
  285. <?php
  286. $infopage = ["", "Photos", ob_get_clean(), "", "photos","Les photos"]; //relativepath, pagetitle, pagecontent, pagescript, pagename | cf structure/template.php ligne 2 à 6
  287. include("structure/template.php");
  288. ?>