File: AlbumDB.php

package info (click to toggle)
gallery 1.5.4-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 26,712 kB
  • ctags: 6,567
  • sloc: php: 33,824; sh: 446; xml: 96; makefile: 88; perl: 61
file content (356 lines) | stat: -rw-r--r-- 10,993 bytes parent folder | download
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
/*
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2006 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * $Id: AlbumDB.php 13338 2006-03-27 15:32:14Z jenst $
*/
?>
<?php
class AlbumDB {
    var $albumList;
    var $albumOrder;

    function AlbumDB($loadphotos=TRUE) {
        global $gallery;
        $changed = 0;

        $dir = $gallery->app->albumDir;

        $tmp = getFile("$dir/albumdb.dat");
        if (strcmp($tmp, "")) {
            $this->albumOrder = unserialize($tmp);

            // albumdb.dat is corrupt, rebuild it
            if (empty($this->albumOrder)) {
                $this->albumOrder = array();
            }
            $changed = 1;
        } else {
            $this->albumOrder = array();
        }

        $this->albumList = array();
        $this->brokenAlbums = array();
        $this->outOfDateAlbums = array();
        $i = 0;
        while ($i < sizeof($this->albumOrder)) {
            $name = $this->albumOrder[$i];
            if (ereg("^\.", $name)) { // how did this get here??
            array_splice($this->albumOrder, $i, 1);
            $changed = 1;
            } else if (fs_is_dir("$dir/$name")) {
                $album = new Album;
                if ($album->load($name,$loadphotos)) {
                    array_push($this->albumList, $album);
                    if ($album->versionOutOfDate()) {
                        array_push($this->outOfDateAlbums, $name);
                    }
                } else if ($name != 'CVS') {
                    array_push($this->brokenAlbums, $name);
                }
                $i++;
            } else {
                /* Couldn't find the album -- delete it from order */
                array_splice($this->albumOrder, $i, 1);
                $changed = 1;
            }
        }

        if ($fd = fs_opendir($dir)) {
            while ($file = readdir($fd)) {
                if (!ereg("^\.", $file) &&
                fs_is_dir("$dir/$file") &&
                strcmp($file, "_vti_cnf") &&
                !in_array($file, $this->albumOrder)) {
                    $album = new Album;
                    $album->load($file,$loadphotos);
                    array_push($this->albumList, $album);
                    array_push($this->albumOrder, $file);
                    $changed = 1;
                }
            }
            closedir($fd);
        }

        if ($changed) {
            $this->save();
        }
    }

    function sortByField($fieldname = '', $order = 'desc') {
	$tmpOrder = array();

	if(!empty($fieldname)) {
	    if($fieldname == 'name') {
		if($order == 'asc') {
		    sort($this->albumOrder);
		}
		else {
		    rsort($this->albumOrder);
		}
	    }
	    else {
		array_sort_by_fields($this->albumList, $fieldname , $order, true, false, true);
		foreach ($this->albumList as $album) {
		    $tmpOrder[] = $album->fields["name"];
		}
		$this->albumOrder = $tmpOrder;
	    }
	    $this->save();
	}
    }

    function renameAlbum($oldName, $newName) {
        global $gallery;

        $dir = $gallery->app->albumDir;
        $galdir = dirname(dirname(__FILE__));

        if (fs_is_file("$galdir/$newName") || fs_is_dir("$galdir/$newName")) {
            return 0;
        }

        if (fs_is_dir("$dir/$newName")) {
            return 0;
        }

        if (fs_is_dir("$dir/$oldName")) {
            $success = fs_rename("$dir/$oldName", "$dir/$newName");
            if (!$success) {
                return 0;
            }
        }

        $album = new Album;
        $album->load($newName);
        $album->fields['name'] = $newName;
        $album->save();

        for ($i = 0; $i < sizeof($this->albumOrder); $i++) {
            if (!strcmp($this->albumOrder[$i], $oldName)) {
                $this->albumOrder[$i] = $newName;
            }
        }

        return 1;
    }

    function newAlbumName($name="album01") {
        global $gallery;

        if (!$name) {
            $name="album01";
        }
        $albumDir = $gallery->app->albumDir;
        while (fs_file_exists("$albumDir/$name")) {
            switch($name) {
                case 'album99':
                $name = 'album100';
                break;

                case 'album999':
                $name = 'album1000';
                break;

                case 'album9999':
                $name = 'album10000';
                break;

                default:
                if (!ereg('[0-9][0-9]$', $name)) {
                    $name.="00";
                }
                $name++;
            }
        }
        return $name;
    }

    function numAlbums($user) {
        return sizeof($this->getVisibleAlbums($user));
    }

    function numPhotos($user) {
        $numPhotos = 0;
        foreach ($this->albumList as $album) {
            if ($user->canWriteToAlbum($album)) {
                $numPhotos += $album->numPhotos(1);
            } else if ($user->canReadAlbum($album)) {
                $numPhotos += $album->numPhotos(0);
            }
        }

        return $numPhotos;
    }

    function getCachedNumPhotos($user) {
        $numPhotos = 0;
        foreach ($this->albumList as $album) {
            if ($user->canReadAlbum($album)) {
                $numPhotos += $album->fields["cached_photo_count"];
            }
        }
        return $numPhotos;
    }

    function getAlbum($user, $index) {
        global $gallery;
        $list = $this->getVisibleAlbums($user);
        $wantedAlbum = isset($list[$index-1]) ? $list[$index-1] : false;
        if ($wantedAlbum && (!isset($wantedAlbum->transient) || !$wantedAlbum->transient->photosloaded)) {
            $wantedAlbum->loadPhotos($gallery->app->albumDir . "/" . $wantedAlbum->fields["name"]);
        }
        return $wantedAlbum;
    }

    function getAlbumByName($name, $load=TRUE) {
        global $gallery;
        /* Look for an exact match */
        foreach ($this->albumList as $album) {
            set_time_limit($gallery->app->timeLimit);
            if ($album->fields["name"] == $name) {
                if ((!isset($album->transient) || !$album->transient->photosloaded) && $load) {
                    $album->loadPhotos($gallery->app->albumDir . "/$name");
                }
                return $album;
            }
        }

        /* Look for a match that is case insensitive */
        foreach ($this->albumList as $album) {
            set_time_limit($gallery->app->timeLimit);
            if (!strcasecmp($album->fields["name"], $name)) {
                if (!$album->transient->photosloaded && $load) {
                    $album->loadPhotos($gallery->app->albumDir . "/$name");
                }
                return $album;
            }
        }

        return 0;
    }

    function moveAlbum($user, $index, $newIndex) {

        // This is tricky.  The old and new indices are only relevant
        // within the list of albums that this user is able to see!
        // Find the location that the user desires and determine that it's
        // one of three cases:
        //	1. At the beginning of the album
        // 	2. At the end
        // 	3. After another album
        // Beginning and end are easy.  If it's after another album, then
        // figure out that album, find its absolute index and move it to
        // that spot +1
        //

        $visible = $this->getVisibleAlbums($user);
        $album1 = $visible[$index-1];
        $album2 = $visible[$newIndex-1];

        // Locate absolute indices of the target and destination
        for ($i = 0; $i < sizeof($this->albumList); $i++) {
            if ($this->albumList[$i]->fields['name'] == $album1->fields['name']) {
                $absIndex = $i;
            } else if ($this->albumList[$i]->fields['name'] == $album2->fields['name']) {
                $absNewIndex = $i;
            }
        }

        if ($newIndex == 1) {
            // Move to beginning
            $this->moveAlbumAbsolute($absIndex, 0);
        } else if ($newIndex == sizeof($visible)) {
            // Move to end
            $this->moveAlbumAbsolute($absIndex, sizeof($this->albumList)-1);
        } else {
            // Move to relative spot
            $this->moveAlbumAbsolute($absIndex, $absNewIndex);
        }

        return;
    }

    function moveAlbumAbsolute($index, $newIndex) {
        /* Pull album out */
        $name = array_splice($this->albumOrder, $index, 1);

        /* Add it back in */
        array_splice($this->albumOrder, $newIndex, 0, $name);
    }

    function getVisibleAlbums($user) {
        global $gallery;
        $list = array();
        foreach ($this->albumList as $album) {
            if ($user->canReadAlbum($album) && $album->isRoot()) {
                array_push($list, $album);
            }
        }

        return $list;
    }

    function save() {
        global $gallery;
        $success = 0;

        $dir = $gallery->app->albumDir;
        return safe_serialize($this->albumOrder, "$dir/albumdb.dat");
    }

    function numAccessibleAlbums($user) {
        $numAlbums = 0;
        foreach ($this->albumList as $album) {
            if ($user->canReadAlbum($album))
            $numAlbums++;
        }
        return $numAlbums;
    }

    function numAccessibleItems($user) {
        global $gallery;
        $numPhotos = $numAlbums = $numTopAlbums = 0;
        foreach ($this->albumList as $album) {
            if ($user->canReadAlbum($album)) {
                $numAlbums++;
                if ($album->isRoot()) {
                    $numTopAlbums++;
                }
                if (empty($gallery->app->slowPhotoCount) || $gallery->app->slowPhotoCount == "no") {
                    $numPhotos += $album->fields["cached_photo_count"];
                } else {
                    $album->load($album->fields['name']);
                    $numPhotos += $album->numPhotos(1,1);
                }
            }
        }
        return array($numPhotos, $numAlbums, $numTopAlbums);
    }

    function getAlbumsByRoot($rootAlbumName, $user=null) {
        $namedAlbum = $this->getAlbumByName($rootAlbumName);
        if ($namedAlbum) {
            $arr = $namedAlbum->getSubAlbums();
            array_push($arr, $namedAlbum);
            return ($arr);
        }
    }
}

?>