summaryrefslogtreecommitdiffstats
path: root/apps/gallery/lib_scanner.php
blob: 5d88a08158a92d269293f511341ff5d82627469d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?

require_once('base.php'); // base lib

class OC_GALLERY_SCANNER {

  public static function scan($root) {
    $albums = array();
    self::scanDir($root, $albums);
    return $albums;
  }

  public static function scanDir($path, &$albums) {
    $current_album = array('name'=> $path, 'imagesCount' => 0, 'images' => array());
    $current_album['name'] = str_replace('/', '.', str_replace(OC::$CONFIG_DATADIRECTORY, '', $current_album['name']));
    $current_album['name'] = ($current_album['name']==='')?'main':$current_album['name'];

    if ($dh = OC_Filesystem::opendir($path)) {
      while (($filename = readdir($dh)) !== false) {
        $filepath = $path.'/'.$filename;
        if (substr($filename, 0, 1) == '.') continue;
        if (OC_Filesystem::is_dir($filepath)) {
          self::scanDir($filepath, $albums);
        } elseif (self::isPhoto($path.'/'.$filename)) {
          $current_album['images'][] = $filepath;
        }
      } 
    }
    $current_album['imagesCount'] = count($current_album['images']);
    $albums[] = $current_album;
    $stmt = OC_DB::prepare('SELECT * FROM *PREFIX*gallery_albums WHERE "uid_owner" = ? AND "album_name" = ?');
    $result = $stmt->execute(array(OC_User::getUser(), $current_album['name']));
    if ($result->numRows() == 0) {
      $stmt = OC_DB::prepare('INSERT OR REPLACE INTO *PREFIX*gallery_albums ("uid_owner", "album_name") VALUES (?, ?)');
      $stmt->execute(array(OC_User::getUser(), $current_album['name']));
    }
    $stmt = OC_DB::prepare('SELECT * FROM *PREFIX*gallery_albums WHERE "uid_owner" = ? AND "album_name" = ?');
    $result = $stmt->execute(array(OC_User::getUser(), $current_album['name']));
    $albumId = $result->fetchRow();
    $albumId = $albumId['album_id'];
    foreach ($current_album['images'] as $img) {
      $stmt = OC_DB::prepare('SELECT * FROM *PREFIX*gallery_photos WHERE "album_id" = ? AND "file_path" = ?');
      $result = $stmt->execute(array($albumId, $img));
      if ($result->numRows() == 0) {
        $stmt = OC_DB::prepare('INSERT OR REPLACE INTO *PREFIX*gallery_photos ("album_id", "file_path") VALUES (?, ?)');
        $stmt->execute(array($albumId, $img));
      }
    }
  }

  public static function isPhoto($filename) {
    if (substr(OC_Filesystem::getMimeType($filename), 0, 6) == "image/")
      return 1;
    return 0;
  }
}
?>