lib_scanner.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. require_once('base.php'); // base lib
  3. class OC_GALLERY_SCANNER {
  4. public static function scan($root) {
  5. $albums = array();
  6. self::scanDir($root, $albums);
  7. return $albums;
  8. }
  9. public static function scanDir($path, &$albums) {
  10. $current_album = array('name'=> $path, 'imagesCount' => 0, 'images' => array());
  11. $current_album['name'] = str_replace('/', '.', str_replace(OC::$CONFIG_DATADIRECTORY, '', $current_album['name']));
  12. $current_album['name'] = ($current_album['name']==='')?'main':$current_album['name'];
  13. if ($dh = OC_Filesystem::opendir($path)) {
  14. while (($filename = readdir($dh)) !== false) {
  15. $filepath = $path.'/'.$filename;
  16. if (substr($filename, 0, 1) == '.') continue;
  17. if (OC_Filesystem::is_dir($filepath)) {
  18. self::scanDir($filepath, $albums);
  19. } elseif (self::isPhoto($path.'/'.$filename)) {
  20. $current_album['images'][] = $filepath;
  21. }
  22. }
  23. }
  24. $current_album['imagesCount'] = count($current_album['images']);
  25. $albums[] = $current_album;
  26. $stmt = OC_DB::prepare('SELECT * FROM *PREFIX*gallery_albums WHERE `uid_owner` = ? AND `album_name` = ?');
  27. $result = $stmt->execute(array(OC_User::getUser(), $current_album['name']));
  28. if ($result->numRows() == 0 && count($current_album['images'])) {
  29. $stmt = OC_DB::prepare('INSERT INTO *PREFIX*gallery_albums (`uid_owner`, `album_name`) VALUES (?, ?)');
  30. $stmt->execute(array(OC_User::getUser(), $current_album['name']));
  31. }
  32. $stmt = OC_DB::prepare('SELECT * FROM *PREFIX*gallery_albums WHERE `uid_owner` = ? AND `album_name` = ?');
  33. $result = $stmt->execute(array(OC_User::getUser(), $current_album['name']));
  34. $albumId = $result->fetchRow();
  35. $albumId = $albumId['album_id'];
  36. foreach ($current_album['images'] as $img) {
  37. $stmt = OC_DB::prepare('SELECT * FROM *PREFIX*gallery_photos WHERE `album_id` = ? AND `file_path` = ?');
  38. $result = $stmt->execute(array($albumId, $img));
  39. if ($result->numRows() == 0) {
  40. $stmt = OC_DB::prepare('INSERT INTO *PREFIX*gallery_photos (`album_id`, `file_path`) VALUES (?, ?)');
  41. $stmt->execute(array($albumId, $img));
  42. }
  43. }
  44. }
  45. public static function isPhoto($filename) {
  46. if (substr(OC_Filesystem::getMimeType($filename), 0, 6) == "image/")
  47. return 1;
  48. return 0;
  49. }
  50. }
  51. ?>