~ubuntu-branches/ubuntu/hardy/gallery2/hardy-security

« back to all changes in this revision

Viewing changes to modules/thumbnail/classes/ThumbnailHelper.class

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2006-04-16 16:42:35 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060416164235-8uy0u4bfjdxpge2o
Tags: 2.1.1-1
* New upstream release (Closes: #362936)
  + Bugfixes for Postgres7 (Closes: #359000, #362152)

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 * $RCSfile: ThumbnailHelper.class,v $
4
4
 *
5
5
 * Gallery - a web based photo album viewer and editor
6
 
 * Copyright (C) 2000-2005 Bharat Mediratta
 
6
 * Copyright (C) 2000-2006 Bharat Mediratta
7
7
 *
8
8
 * This program is free software; you can redistribute it and/or modify
9
9
 * it under the terms of the GNU General Public License as published by
20
20
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
21
21
 */
22
22
/**
23
 
 * @version $Revision: 1.17 $ $Date: 2005/08/23 03:49:56 $
 
23
 * @version $Revision: 1.21 $ $Date: 2006/02/06 20:41:20 $
24
24
 * @package Thumbnail
25
25
 * @author Alan Harder <alan.harder@sun.com>
26
26
 */
68
68
 
69
69
        list ($ret, $searchResults) =
70
70
            $gallery->search($query, $data,
71
 
                             array('limit' => array('count' => $count,
72
 
                                                    'offset' => $offset)));
73
 
        if ($ret->isError()) {
 
71
                             array('limit' => array('count' => (int)$count,
 
72
                                                    'offset' => (int)$offset)));
 
73
        if ($ret) {
74
74
            return array($ret->wrap(__FILE__, __LINE__), null);
75
75
        }
76
76
 
77
77
        $items = array();
78
78
        while ($result = $searchResults->nextResult()) {
79
 
            $items[$result[0]] = $result[1];
 
79
            $items[(int)$result[0]] = $result[1];
80
80
        }
81
81
 
82
 
        return array(GalleryStatus::success(), $items);
 
82
        return array(null, $items);
83
83
    }
84
84
 
85
85
    /**
96
96
        list ($ret, $searchResults) = $gallery->search(
97
97
            'SELECT [ThumbnailImage::id] FROM [ThumbnailImage], [ChildEntity]
98
98
             WHERE [ThumbnailImage::id] = [ChildEntity::id] AND [ChildEntity::parentId] = ?',
99
 
            array($itemId));
100
 
        if ($ret->isError()) {
 
99
            array((int)$itemId));
 
100
        if ($ret) {
101
101
            return array($ret->wrap(__FILE__, __LINE__), null);
102
102
        }
103
103
 
104
104
        $thumbnailImage = null;
105
105
        if ($result = $searchResults->nextResult()) {
106
106
            list ($ret, $thumbnailImage) = GalleryCoreApi::loadEntitiesById((int)$result[0]);
107
 
            if ($ret->isError()) {
 
107
            if ($ret) {
108
108
                return array($ret->wrap(__FILE__, __LINE__), null);
109
109
            }
110
110
        }
111
111
 
112
 
        return array(GalleryStatus::success(), $thumbnailImage);
 
112
        return array(null, $thumbnailImage);
113
113
    }
114
114
 
115
115
    /**
124
124
        list ($ret, $searchResults) = $gallery->search(
125
125
            'SELECT [ThumbnailImage::id], [ThumbnailImage::itemMimeTypes]
126
126
             FROM [ThumbnailImage] WHERE [ThumbnailImage::itemMimeTypes] IS NOT NULL');
127
 
        if ($ret->isError()) {
 
127
        if ($ret) {
128
128
            return array($ret->wrap(__FILE__, __LINE__), null);
129
129
        }
130
130
        $data = array();
131
131
        while ($result = $searchResults->nextResult()) {
132
132
            foreach (explode('|', $result[1]) as $mimeType) {
133
 
                $data[$mimeType] = $result[0];
 
133
                $data[$mimeType] = (int)$result[0];
134
134
            }
135
135
        }
136
 
        return array(GalleryStatus::success(), $data);
 
136
        return array(null, $data);
137
137
    }
138
138
 
139
139
    /**
149
149
     */
150
150
    function fetchToolkitSupport($byMimeType=true) {
151
151
        list ($ret, $tList) = GalleryCoreApi::getToolkitOperationMimeTypes('thumbnail');
152
 
        if ($ret->isError()) {
 
152
        if ($ret) {
153
153
            return array($ret->wrap(__FILE__, __LINE__), null, null);
154
154
        }
155
155
        list ($ret, $cList) = GalleryCoreApi::getToolkitOperationMimeTypes('convert-to-image/jpeg');
156
 
        if ($ret->isError()) {
 
156
        if ($ret) {
157
157
            return array($ret->wrap(__FILE__, __LINE__), null, null);
158
158
        }
159
159
        $data = array_merge_recursive($tList, $cList);
160
160
        if ($byMimeType) {
161
 
            return array(GalleryStatus::success(), $data, null);
 
161
            return array(null, $data, null);
162
162
        }
163
163
        $badMime = array();
164
164
        $toolkitMime = array();
172
172
                $badMime[] = $mime;
173
173
            }
174
174
        }
175
 
        return array(GalleryStatus::success(), $toolkitMime, $badMime);
 
175
        return array(null, $toolkitMime, $badMime);
176
176
    }
177
177
 
178
178
    /**
202
202
                $fileMimeType == 'application/unknown' || empty($mimeExtensions)) {
203
203
            $extension = GalleryUtilities::getFileExtension($filename);
204
204
            list ($ret, $fileMimeType) = GalleryCoreApi::convertExtensionToMime($extension);
205
 
            if ($ret->isError()) {
 
205
            if ($ret) {
206
206
                $fileMimeType = 'application/unknown';
207
207
            }
208
208
        }
209
209
        if ($fileMimeType != 'image/jpeg') {
210
 
            return array(GalleryStatus::error(ERROR_UNSUPPORTED_FILE_TYPE, __FILE__, __LINE__),
 
210
            return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_FILE_TYPE, __FILE__, __LINE__),
211
211
                         null);
212
212
        }
213
213
 
214
214
        if (!isset($item)) {
215
215
            list ($ret, $item) =
216
216
                GalleryCoreApi::newFactoryInstance('GalleryEntity', 'ThumbnailImage');
217
 
            if ($ret->isError()) {
 
217
            if ($ret) {
218
218
                return array($ret->wrap(__FILE__, __LINE__), null);
219
219
            }
220
220
            if (!isset($item)) {
221
 
                return array(GalleryStatus::error(ERROR_MISSING_OBJECT, __FILE__, __LINE__), null);
 
221
                return array(GalleryCoreApi::error(ERROR_MISSING_OBJECT, __FILE__, __LINE__), null);
222
222
            }
223
223
        }
224
224
 
225
225
        $ret = $item->create($tmpfile, $fileMimeType, $filename);
226
 
        if ($ret->isError()) {
 
226
        if ($ret) {
227
227
            return array($ret->wrap(__FILE__, __LINE__), null);
228
228
        }
229
229
        if (isset($mimeType)) {
238
238
            $item->setHeight($knownSize['height']);
239
239
        }
240
240
        $ret = $item->save();
241
 
        if ($ret->isError()) {
 
241
        if ($ret) {
242
242
            return array($ret->wrap(__FILE__, __LINE__), null);
243
243
        }
244
244
 
245
245
        list ($ret, $adminGroupId) =
246
246
            GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup');
247
 
        if ($ret->isError()) {
 
247
        if ($ret) {
248
248
            return array($ret->wrap(__FILE__, __LINE__), null);
249
249
        }
250
250
        $ret = GalleryCoreApi::addGroupPermission($item->getId(), $adminGroupId, 'core.viewSource');
251
 
        if ($ret->isError()) {
 
251
        if ($ret) {
252
252
            return array($ret->wrap(__FILE__, __LINE__), null);
253
253
        }
254
254
 
256
256
            $ret = GalleryCoreApi::registerToolkitOperation('Thumbnail',
257
257
                                   array($mimeType), 'convert-to-image/jpeg', array(),
258
258
                                   $gallery->i18n('Convert to a JPEG'), 'image/jpeg', 50);
259
 
            if ($ret->isError()) {
 
259
            if ($ret) {
260
260
                return array($ret->wrap(__FILE__, __LINE__), null);
261
261
            }
262
262
        }
263
263
 
264
 
        return array(GalleryStatus::success(), $item->getId());
 
264
        return array(null, $item->getId());
265
265
    }
266
266
 
267
267
    /**
275
275
     */
276
276
    function updateItem($itemId, $mimeType, $isAdd=true) {
277
277
        list ($ret, $item) = GalleryCoreApi::loadEntitiesById($itemId);
278
 
        if ($ret->isError()) {
 
278
        if ($ret) {
279
279
            return $ret->wrap(__FILE__, __LINE__);
280
280
        }
281
281
        list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($itemId);
282
 
        if ($ret->isError()) {
 
282
        if ($ret) {
283
283
            return $ret->wrap(__FILE__, __LINE__);
284
284
        }
285
285
        $mimeTypes = $item->getItemMimeTypesList();
296
296
            $item->setItemMimeTypesList($newList);
297
297
        }
298
298
        $ret = $item->save();
299
 
        if ($ret->isError()) {
 
299
        if ($ret) {
300
300
            GalleryCoreApi::releaseLocks($lockId);
301
301
            return $ret->wrap(__FILE__, __LINE__);
302
302
        }
303
303
        $ret = GalleryCoreApi::releaseLocks($lockId);
304
 
        if ($ret->isError()) {
 
304
        if ($ret) {
305
305
            return $ret->wrap(__FILE__, __LINE__);
306
306
        }
307
307
        if ($isAdd) {
309
309
            $ret = GalleryCoreApi::registerToolkitOperation('Thumbnail',
310
310
                                   array($mimeType), 'convert-to-image/jpeg', array(),
311
311
                                   $gallery->i18n('Convert to a JPEG'), 'image/jpeg', 50);
312
 
            if ($ret->isError()) {
 
312
            if ($ret) {
313
313
                return $ret->wrap(__FILE__, __LINE__);
314
314
            }
315
315
        } else {
316
316
            $ret = GalleryCoreApi::unregisterToolkitOperation('Thumbnail',
317
317
                                   'convert-to-image/jpeg', array($mimeType));
318
 
            if ($ret->isError()) {
 
318
            if ($ret) {
319
319
                return $ret->wrap(__FILE__, __LINE__);
320
320
            }
321
321
        }
322
 
        return GalleryStatus::success();
 
322
        return null;
323
323
    }
324
324
 
325
325
    /**
332
332
     */
333
333
    function applyThumbnail(&$item, $thumbnailId) {
334
334
        list ($ret, $derivative) = GalleryCoreApi::fetchThumbnailsByItemIds(array($item->getId()));
335
 
        if ($ret->isError()) {
 
335
        if ($ret) {
336
336
            return $ret->wrap(__FILE__, __LINE__);
337
337
        }
338
338
        if (!empty($derivative)) {
339
339
            $derivative = array_shift($derivative);
340
340
            list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($derivative->getId());
341
 
            if ($ret->isError()) {
 
341
            if ($ret) {
342
342
                return $ret->wrap(__FILE__, __LINE__);
343
343
            }
344
 
            /* Make sure operations just contains thumbnail|{size} */
 
344
            /* Make sure operations contains only thumbnail and composite */
345
345
            $derivative->setDerivativeOperations(
346
 
                preg_replace('/^(?:.*;)?(thumbnail\|\d+)(?:;.*)?$/', '$1',
347
 
                             $derivative->getDerivativeOperations()));
 
346
                rtrim(preg_replace('/(^|;)((?!thumbnail\||composite\|).*?(;|$))+/', '$1',
 
347
                                   $derivative->getDerivativeOperations()), ';'));
348
348
        } else {
349
349
            list ($ret, $derivative) =
350
350
                GalleryCoreApi::newFactoryInstanceByHint('GalleryDerivative', 'ThumbnailImage');
351
 
            if ($ret->isError()) {
 
351
            if ($ret) {
352
352
                return $ret->wrap(__FILE__, __LINE__);
353
353
            }
354
354
            if (!isset($derivative)) {
355
 
                return GalleryStatus::error(ERROR_MISSING_OBJECT, __FILE__, __LINE__);
 
355
                return GalleryCoreApi::error(ERROR_MISSING_OBJECT, __FILE__, __LINE__);
356
356
            }
357
357
            $ret = $derivative->create($item->getId(), DERIVATIVE_TYPE_IMAGE_THUMBNAIL);
358
 
            if ($ret->isError()) {
 
358
            if ($ret) {
359
359
                return $ret->wrap(__FILE__, __LINE__);
360
360
            }
361
361
            list ($ret, $prefs) =
362
362
                GalleryCoreApi::fetchDerivativePreferencesForItem($item->getParentId());
363
 
            if ($ret->isError()) {
 
363
            if ($ret) {
364
364
                return $ret->wrap(__FILE__, __LINE__);
365
365
            }
366
366
            $op = 'thumbnail|150';
373
373
            $derivative->setDerivativeOperations($op);
374
374
        }
375
375
 
 
376
        list ($ret, $mimeList) = GalleryCoreApi::getToolkitOperationMimeTypes('thumbnail');
 
377
        if ($ret) {
 
378
            return $ret->wrap(__FILE__, __LINE__);
 
379
        }
 
380
        if (!isset($mimeList['image/jpeg'])) {
 
381
            /*
 
382
             * No toolkit for jpeg thumbnails.
 
383
             * Omit thumbnail operation to just use custom thumbnail in its original size.
 
384
             */
 
385
            $derivative->setDerivativeOperations(
 
386
                rtrim(preg_replace('/(^|;)thumbnail\|.*?(;|$)/', '$1',
 
387
                                   $derivative->getDerivativeOperations()), ';'));
 
388
        }
 
389
 
376
390
        $derivative->setDerivativeSourceId($thumbnailId);
377
391
        $derivative->setMimeType('image/jpeg');
378
392
        $ret = $derivative->save();
379
 
        if ($ret->isError()) {
 
393
        if ($ret) {
380
394
            if (isset($lockId)) {
381
395
                GalleryCoreApi::releaseLocks($lockId);
382
396
            }
384
398
        }
385
399
        if (isset($lockId)) {
386
400
            $ret = GalleryCoreApi::releaseLocks($lockId);
387
 
            if ($ret->isError()) {
 
401
            if ($ret) {
388
402
                return $ret->wrap(__FILE__, __LINE__);
389
403
            }
390
404
        }
391
405
 
392
 
        return GalleryStatus::success();
 
406
        return null;
393
407
    }
394
408
 
395
409
    /**
402
416
     */
403
417
    function restoreThumbnail(&$item) {
404
418
        list ($ret, $derivative) = GalleryCoreApi::fetchThumbnailsByItemIds(array($item->getId()));
405
 
        if ($ret->isError()) {
 
419
        if ($ret) {
406
420
            return $ret->wrap(__FILE__, __LINE__);
407
421
        }
408
422
        if (empty($derivative)) {
409
 
            return GalleryStatus::error(ERROR_MISSING_OBJECT, __FILE__, __LINE__);
 
423
            return GalleryCoreApi::error(ERROR_MISSING_OBJECT, __FILE__, __LINE__);
410
424
        }
411
425
        $derivative = array_shift($derivative);
412
426
 
413
427
        if (GalleryUtilities::isA($item, 'GalleryAlbumItem')) {
414
428
            list ($ret, $success) = GalleryCoreApi::guaranteeAlbumHasThumbnail($item->getId());
415
 
            if ($ret->isError()) {
 
429
            if ($ret) {
416
430
                return $ret->wrap(__FILE__, __LINE__);
417
431
            }
418
 
            return GalleryStatus::success();
 
432
            return null;
419
433
        }
420
434
 
421
 
        /* Make sure operations just contains thumbnail|{size} */
422
 
        $operations = preg_replace('/^(?:.*;)?(thumbnail\|\d+)(?:;.*)?$/', '$1',
423
 
                                   $derivative->getDerivativeOperations());
 
435
        /* Make sure operations contains only thumbnail and composite */
 
436
        $operations = rtrim(preg_replace('/(^|;)((?!thumbnail\||composite\|).*?(;|$))+/', '$1',
 
437
                                         $derivative->getDerivativeOperations()), ';');
424
438
 
425
439
        list ($ret, $source) = GalleryCoreApi::fetchPreferredSource($item);
426
 
        if ($ret->isError()) {
 
440
        if ($ret) {
427
441
            return $ret->wrap(__FILE__, __LINE__);
428
442
        }
429
443
 
430
444
        list ($ret, $operations, $outputMimeType) =
431
445
            GalleryCoreApi::makeSupportedViewableOperationSequence(
432
446
                                         $source->getMimeType(), $operations, true);
433
 
        if ($ret->isError()) {
 
447
        if ($ret) {
434
448
            return $ret->wrap(__FILE__, __LINE__);
435
449
        }
436
450
 
437
451
        if (empty($operations)) {
438
452
            /* Operations not supported.. remove the thumbnail */
439
453
            $ret = GalleryCoreApi::deleteEntityById($derivative->getId());
440
 
            if ($ret->isError()) {
 
454
            if ($ret) {
441
455
                return $ret->wrap(__FILE__, __LINE__);
442
456
            }
443
457
        } else {
444
458
            list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($derivative->getId());
445
 
            if ($ret->isError()) {
 
459
            if ($ret) {
446
460
                return $ret->wrap(__FILE__, __LINE__);
447
461
            }
448
462
 
451
465
            $derivative->setDerivativeSourceId($source->getId());
452
466
 
453
467
            $ret = $derivative->save();
454
 
            if ($ret->isError()) {
 
468
            if ($ret) {
455
469
                return $ret->wrap(__FILE__, __LINE__);
456
470
            }
457
471
 
458
472
            $ret = GalleryCoreApi::releaseLocks($lockId);
459
 
            if ($ret->isError()) {
 
473
            if ($ret) {
460
474
                return $ret->wrap(__FILE__, __LINE__);
461
475
            }
462
476
        }
463
477
 
464
 
        return GalleryStatus::success();
 
478
        return null;
465
479
    }
466
480
 
467
481
    /**
480
494
            ORDER BY [GalleryMimeTypeMap::mimeType]
481
495
        ';
482
496
        list ($ret, $searchResults) = $gallery->search($query);
483
 
        if ($ret->isError()) {
 
497
        if ($ret) {
484
498
            return array($ret->wrap(__FILE__, __LINE__), null);
485
499
        }
486
500
        while ($result = $searchResults->nextResult()) {
490
504
        foreach(array_keys($typeMap) as $mimeType) {
491
505
            $typeMap[$mimeType] = implode(' ', $typeMap[$mimeType]);
492
506
        }
493
 
        return array(GalleryStatus::success(), $typeMap);
 
507
        return array(null, $typeMap);
494
508
    }
495
509
}
496
510
?>