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

« back to all changes in this revision

Viewing changes to modules/webcam/module.inc

  • 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: module.inc,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
21
21
 */
22
22
 
23
23
/**
24
 
 * @version $Revision: 1.31 $ $Date: 2005/09/10 20:28:16 $
 
24
 * @version $Revision: 1.37 $ $Date: 2006/03/22 03:52:37 $
25
25
 * @package WebCam
26
26
 * @author Alan Harder <alan.harder@sun.com>
27
27
 */
32
32
        $this->setId('webcam');
33
33
        $this->setName($gallery->i18n('WebCam'));
34
34
        $this->setDescription($gallery->i18n('Support periodically updated images like a webcam'));
35
 
        $this->setVersion('1.0.0'); /* Update upgrade() function below too */
36
 
        $this->setGroup('import', $this->translate('Import'));
 
35
        $this->setVersion('1.0.3'); /* Update upgrade() function below too */
 
36
        $this->setGroup('import', $gallery->i18n('Import'));
37
37
        $this->setCallbacks('getSiteAdminViews');
38
 
        $this->setRequiredCoreApi(array(6, 0));
39
 
        $this->setRequiredModuleApi(array(2, 0));
 
38
        $this->setRequiredCoreApi(array(7, 0));
 
39
        $this->setRequiredModuleApi(array(3, 0));
40
40
    }
41
41
 
42
42
    /**
46
46
        global $gallery;
47
47
 
48
48
        list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'webcam');
49
 
        if ($ret->isError()) {
 
49
        if ($ret) {
50
50
            return $ret->wrap(__FILE__, __LINE__);
51
51
        }
52
52
        foreach (array('duration' => 600 /* 10 minutes */) as $key => $value) {
53
53
            if (!isset($params[$key])) {
54
54
                $ret = $this->setParameter($key, $value);
55
 
                if ($ret->isError()) {
 
55
                if ($ret) {
56
56
                    return $ret->wrap(__FILE__, __LINE__);
57
57
                }
58
58
            }
74
74
                  WHERE [GalleryEntity::entityType] = \'GalleryLiveImage\'
75
75
                    AND [GalleryEntity::onLoadHandlers] IS NULL';
76
76
                $ret = $storage->execute($query);
77
 
                if ($ret->isError()) {
 
77
                if ($ret) {
78
78
                    return $ret->wrap(__FILE__, __LINE__);
79
79
                }
80
80
                list ($ret, $handlers) = $storage->getFunctionSql('CONCAT',
81
81
                    array('[GalleryEntity::onLoadHandlers]', "'WebCam|'"));
82
 
                if ($ret->isError()) {
 
82
                if ($ret) {
83
83
                    return $ret->wrap(__FILE__, __LINE__);
84
84
                }
85
85
                $query = 'UPDATE [GalleryEntity]
88
88
                  WHERE [GalleryEntity::entityType] = \'GalleryLiveImage\'
89
89
                    AND [GalleryEntity::onLoadHandlers] IS NOT NULL';
90
90
                $ret = $storage->execute($query);
91
 
                if ($ret->isError()) {
 
91
                if ($ret) {
92
92
                    return $ret->wrap(__FILE__, __LINE__);
93
93
                }
94
94
                break;
102
102
            case '0.9.6':
103
103
            case '0.9.7':
104
104
            case '0.9.8':
 
105
            case '1.0.0':
 
106
            case '1.0.1':
 
107
            case '1.0.2':
105
108
 
106
109
            case 'end of upgrade path':
107
110
                break;
108
111
 
109
112
            default:
110
 
                return GalleryStatus::error(ERROR_BAD_PLUGIN, __FILE__, __LINE__,
111
 
                                            sprintf('Unknown module version %s', $currentVersion));
 
113
                return GalleryCoreApi::error(ERROR_BAD_PLUGIN, __FILE__, __LINE__,
 
114
                                             sprintf('Unknown module version %s', $currentVersion));
112
115
            }
113
116
        }
114
117
 
115
 
        return GalleryStatus::success();
 
118
        return null;
116
119
    }
117
120
 
118
121
    /**
122
125
        $ret = GalleryCoreApi::registerFactoryImplementation(
123
126
            'GalleryOnLoadHandler', 'WebCamModule', 'WebCam',
124
127
            'modules/webcam/module.inc', 'webcam', null);
125
 
        if ($ret->isError()) {
 
128
        if ($ret) {
126
129
            return $ret->wrap(__FILE__, __LINE__);
127
130
        }
128
131
 
129
132
        $ret = GalleryCoreApi::registerFactoryImplementation(
130
133
            'ItemAddPlugin', 'ItemAddWebCam', 'ItemAddWebCam',
131
134
            'modules/webcam/ItemAddWebCam.inc', 'webcam', null);
132
 
        if ($ret->isError()) {
 
135
        if ($ret) {
133
136
            return $ret->wrap(__FILE__, __LINE__);
134
137
        }
135
138
 
136
 
        return GalleryStatus::success();
 
139
        return null;
137
140
    }
138
141
 
139
142
    /**
144
147
    }
145
148
 
146
149
    /**
147
 
     * @see GalleryModule::isRecommendedDuringInstall()
148
 
     */
149
 
    function isRecommendedDuringInstall() {
150
 
        return false;
151
 
    }
152
 
 
153
 
    /**
154
 
     * @see GalleryModule::autoConfigure()
155
 
     */
156
 
    function autoConfigure() {
157
 
        /* We don't require any special configuration */
158
 
        return array(GalleryStatus::success(), true);
159
 
    }
160
 
 
161
 
    /**
162
150
     * @see GalleryModule::getSiteAdminViews()
163
151
     */
164
152
    function getSiteAdminViews() {
165
 
        return array(GalleryStatus::success(),
 
153
        return array(null,
166
154
                     array(array('name' => $this->translate('WebCam'),
167
155
                                 'view' => 'webcam.WebCamSiteAdmin')));
168
156
    }
171
159
     * Check age and refresh from external source if needed..
172
160
     */
173
161
    function onLoad(&$item, $duringUpgrade) {
174
 
        static $loop = array();
175
162
        $old = false;
176
163
        $id = $item->getId();
 
164
        $loop = GalleryDataCache::containsKey("webcam:load-$id");
177
165
        /* We don't want to refresh the external source during an upgrade */
178
166
        if ($duringUpgrade) {
179
 
            return GalleryStatus::success();
 
167
            return null;
180
168
        }
181
169
        list ($ret, $duration) = $this->getParameter('duration');
182
 
        if ($ret->isError()) {
 
170
        if ($ret) {
183
171
            return $ret->wrap(__FILE__, __LINE__);
184
172
        }
185
 
        if (time() - $item->getModificationTimestamp() > $duration && !isset($loop[$id])) {
 
173
        if (time() - $item->getModificationTimestamp() > $duration && !$loop) {
186
174
            /* Lock and refresh to ensure another request didn't just update image.. */
187
175
            list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($id);
188
 
            if ($ret->isError()) {
 
176
            if ($ret) {
189
177
                return $ret->wrap(__FILE__, __LINE__);
190
178
            }
191
 
            $loop[$id] = true;
 
179
            GalleryDataCache::put("webcam:load-$id", true);
192
180
            list ($ret, $refresh) = $item->refresh();
193
 
            if ($ret->isError()) {
 
181
            if ($ret) {
194
182
                GalleryCoreApi::releaseLocks($lockId);
195
183
                return $ret->wrap(__FILE__, __LINE__);
196
184
            }
200
188
        }
201
189
        if ($old) {
202
190
            global $gallery;
203
 
            $platform = $gallery->getPlatform();
 
191
            $platform =& $gallery->getPlatform();
204
192
            $tmpDir = $gallery->getConfig('data.gallery.tmp');
205
193
            $tmpFile = $platform->tempnam($tmpDir, 'webcam');
206
194
            list ($ret, $url) = $this->getParameter('imageUrl', $id);
207
 
            if ($ret->isError()) {
 
195
            if ($ret) {
208
196
                GalleryCoreApi::releaseLocks($lockId);
209
197
                return $ret->wrap(__FILE__, __LINE__);
210
198
            }
211
199
 
212
200
            list ($successfullyCopied, $response, $headers) =
213
201
                GalleryCoreApi::fetchWebFile($url, $tmpFile);
214
 
            if (!$successfullyCopied || strpos($response, '404 Not Found') !== false) {
 
202
            if (!$successfullyCopied) {
215
203
                /* Can't get the current image.. just keep what we have now */
216
204
            } else {
217
205
                list ($ret, $path) = $item->fetchPath();
218
 
                if ($ret->isError()) {
 
206
                if ($ret) {
219
207
                    GalleryCoreApi::releaseLocks($lockId);
220
208
                    return $ret->wrap(__FILE__, __LINE__);
221
209
                }
 
210
                /* Backup previous file */
 
211
                $ret = $platform->copy($path, $tmpFile . '.bak');
 
212
                if (!$ret) {
 
213
                    GalleryCoreApi::releaseLocks($lockId);
 
214
                    return GalleryCoreApi::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__);
 
215
                }
222
216
                $ret = $platform->copy($tmpFile, $path);
223
217
                if (!$ret) {
224
218
                    GalleryCoreApi::releaseLocks($lockId);
225
 
                    return GalleryStatus::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__);
 
219
                    return GalleryCoreApi::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__);
226
220
                }
227
221
                $ret = $item->rescan();
228
 
                if ($ret->isError()) {
229
 
                    GalleryCoreApi::releaseLocks($lockId);
230
 
                    return $ret->wrap(__FILE__, __LINE__);
231
 
                }
232
 
                $item->setOriginationTimestamp(time());
233
 
                $ret = $item->save();
234
 
                if ($ret->isError()) {
235
 
                    GalleryCoreApi::releaseLocks($lockId);
236
 
                    return $ret->wrap(__FILE__, __LINE__);
237
 
                }
238
 
                $ret = GalleryCoreApi::expireDerivativeTreeBySourceIds(array($id));
239
 
                if ($ret->isError()) {
240
 
                    GalleryCoreApi::releaseLocks($lockId);
241
 
                    return $ret->wrap(__FILE__, __LINE__);
242
 
                }
 
222
                if ($ret) {
 
223
                    if (!($ret->getErrorCode() & ERROR_BAD_DATA_TYPE)) {
 
224
                        GalleryCoreApi::releaseLocks($lockId);
 
225
                        return $ret->wrap(__FILE__, __LINE__);
 
226
                    }
 
227
                    /* Problem with new file (maybe html response, not data file); rollback */
 
228
                    $platform->copy($tmpFile . '.bak', $path);
 
229
                } else {
 
230
                    $item->setOriginationTimestamp(time());
 
231
                    $ret = $item->save();
 
232
                    if ($ret) {
 
233
                        GalleryCoreApi::releaseLocks($lockId);
 
234
                        return $ret->wrap(__FILE__, __LINE__);
 
235
                    }
 
236
                    $ret = GalleryCoreApi::expireDerivativeTreeBySourceIds(array($id));
 
237
                    if ($ret) {
 
238
                        GalleryCoreApi::releaseLocks($lockId);
 
239
                        return $ret->wrap(__FILE__, __LINE__);
 
240
                    }
 
241
                }
 
242
                @$platform->unlink($tmpFile . '.bak');
243
243
            }
244
244
            @$platform->unlink($tmpFile);
245
245
        }
246
246
        if (isset($lockId)) {
247
247
            $ret = GalleryCoreApi::releaseLocks($lockId);
248
 
            if ($ret->isError()) {
 
248
            if ($ret) {
249
249
                return $ret->wrap(__FILE__, __LINE__);
250
250
            }
251
251
        }
252
252
 
253
 
        return GalleryStatus::success();
 
253
        return null;
254
254
    }
255
255
}
256
256
?>