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

« back to all changes in this revision

Viewing changes to modules/comment/AddComment.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: AddComment.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
19
19
 * along with this program; if not, write to the Free Software
20
20
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
21
21
 */
22
 
 
23
22
/**
24
 
 * @version $Revision: 1.37 $ $Date: 2005/08/23 03:49:01 $
 
23
 * @version $Revision: 1.42 $ $Date: 2006/01/30 22:52:14 $
25
24
 * @package Comment
26
25
 * @subpackage UserInterface
27
26
 * @author Bharat Mediratta <bharat@menalto.com>
35
34
 *
36
35
 */
37
36
class AddCommentController extends GalleryController {
 
37
    /**
 
38
     * ValidationPlugin instances to use when handling this request.  Only used by test code.
 
39
     *
 
40
     * @var array $_plugins (array of GalleryValidationPlugin)
 
41
     * @access private
 
42
     */
 
43
    var $_pluginInstances;
38
44
 
39
45
    /**
40
46
     * @see GalleryController::handleRequest()
46
52
 
47
53
        /* Make sure we have permission to add a comment */
48
54
        $ret = GalleryCoreApi::assertHasItemPermission($itemId, 'comment.add');
49
 
        if ($ret->isError()) {
 
55
        if ($ret) {
50
56
            return array($ret->wrap(__FILE__, __LINE__), null);
51
57
        }
52
58
 
53
 
        $redirect = array();
54
 
        $status = array();
55
 
        $error = array();
 
59
        $redirect = $status = $error = array();
56
60
        if (isset($form['action']['add'])) {
 
61
            GalleryCoreApi::requireOnce(
 
62
                'modules/comment/classes/GalleryCommentHelper.class');
57
63
 
58
64
            if (empty($form['subject'])) {
59
65
                $error[] = 'form[error][subject][missing]';
60
66
            }
61
 
 
62
67
            if (empty($form['comment'])) {
63
68
                $error[] = 'form[error][comment][missing]';
64
69
            }
65
70
 
 
71
            list ($ret, $useValidationPlugins) = GalleryCommentHelper::useValidationPlugins();
 
72
            if ($ret) {
 
73
                return array($ret->wrap(__FILE__, __LINE__), null);
 
74
            }
 
75
            if (empty($error) && $useValidationPlugins) {
 
76
                if (isset($this->_pluginInstances)) {
 
77
                    $pluginInstances = $this->_pluginInstances;
 
78
                } else {
 
79
                    list ($ret, $pluginInstances) =
 
80
                        GalleryCoreApi::getAllFactoryImplementationIds('GalleryValidationPlugin');
 
81
                    if ($ret) {
 
82
                        return array($ret->wrap(__FILE__, __LINE__), null);
 
83
                    }
 
84
 
 
85
                    foreach (array_keys($pluginInstances) as $pluginId) {
 
86
                        list ($ret, $pluginInstances[$pluginId]) =
 
87
                            GalleryCoreApi::newFactoryInstanceById('GalleryValidationPlugin',
 
88
                                                                   $pluginId);
 
89
                        if ($ret) {
 
90
                            return array($ret->wrap(__FILE__, __LINE__), null);
 
91
                        }
 
92
                    }
 
93
                }
 
94
 
 
95
                /* Let each plugin do its verification */
 
96
                foreach ($pluginInstances as $plugin) {
 
97
                    list ($ret, $pluginErrors, $continue) = $plugin->performValidation($form);
 
98
                    if ($ret) {
 
99
                        return array($ret->wrap(__FILE__, __LINE__), null);
 
100
                    }
 
101
 
 
102
                    $error = array_merge($error, $pluginErrors);
 
103
                    if (!$continue) {
 
104
                        break;
 
105
                    }
 
106
                }
 
107
            }
 
108
 
66
109
            if (empty($error)) {
67
110
                /* Add the comment */
68
111
                list ($ret, $comment) =
69
112
                    GalleryCoreApi::newFactoryInstance('GalleryEntity', 'GalleryComment');
70
 
                if ($ret->isError()) {
 
113
                if ($ret) {
71
114
                    return array($ret->wrap(__FILE__, __LINE__), null);
72
115
                }
73
116
 
74
117
                if (!isset($comment)) {
75
 
                    return array(GalleryStatus::error(ERROR_MISSING_OBJECT, __FILE__, __LINE__),
 
118
                    return array(GalleryCoreApi::error(ERROR_MISSING_OBJECT, __FILE__, __LINE__),
76
119
                                 null);
77
120
                }
78
121
 
79
122
                $ret = $comment->create($itemId);
80
 
                if ($ret->isError()) {
81
 
                    return array($ret->wrap(__FILE__, __LINE__), null);
 
123
                if ($ret) {
 
124
                    return array($ret->wrap(__FILE__, __LINE__), null);
 
125
                }
 
126
 
 
127
                list ($ret, $markup) =
 
128
                    GalleryCoreApi::getPluginParameter('module', 'core', 'misc.markup');
 
129
                if ($ret) {
 
130
                    return array($ret->wrap(__FILE__, __LINE__), null);
 
131
                }
 
132
                if ($markup == 'html') {
 
133
                    /* Strip malicious content if html markup allowed */
 
134
                    $form['subject'] = GalleryUtilities::htmlSafe($form['subject'], true);
 
135
                    $form['comment'] = GalleryUtilities::htmlSafe($form['comment'], true);
82
136
                }
83
137
 
84
138
                $comment->setCommenterId($gallery->getActiveUserId());
88
142
                $comment->setDate(time());
89
143
 
90
144
                $ret = $comment->save();
91
 
                if ($ret->isError()) {
 
145
                if ($ret) {
92
146
                    return array($ret->wrap(__FILE__, __LINE__), null);
93
147
                }
94
148
 
123
177
        $results['status'] = $status;
124
178
        $results['error'] = $error;
125
179
 
126
 
        return array(GalleryStatus::success(), $results);
 
180
        return array(null, $results);
127
181
    }
128
182
}
129
183
 
140
194
     * @see GalleryView::loadTemplate
141
195
     */
142
196
    function loadTemplate(&$template, &$form) {
 
197
        GalleryCoreApi::requireOnce('modules/comment/classes/GalleryCommentHelper.class');
 
198
 
143
199
        /* Load our item */
144
200
        list ($ret, $item) = $this->_getItem();
145
 
        if ($ret->isError()) {
 
201
        if ($ret) {
146
202
            return array($ret->wrap(__FILE__, __LINE__), null);
147
203
        }
148
204
 
149
205
        /* Make sure we have permission to add a comment */
150
206
        $ret = GalleryCoreApi::assertHasItemPermission($item->getId(), 'comment.add');
151
 
        if ($ret->isError()) {
 
207
        if ($ret) {
152
208
            return array($ret->wrap(__FILE__, __LINE__), null);
153
209
        }
154
210
 
155
211
        if ($form['formName'] != 'AddComment') {
156
212
            $form['formName'] = 'AddComment';
157
 
            $form['subject'] = '';
158
 
            $form['comment'] = '';
 
213
            $form['subject'] = $form['comment'] = '';
159
214
        } else {
160
215
            foreach (array('subject', 'comment') as $key) {
161
216
                if (empty($form[$key])) {
164
219
            }
165
220
        }
166
221
 
167
 
        $AddComment = array();
168
 
        $AddComment['host'] = GalleryUtilities::getRemoteHostAddress();
169
 
        $AddComment['itemId'] = $item->getId();
170
 
 
171
 
        $template->setVariable('AddComment', $AddComment);
 
222
        /* Check if we should use validation plugins for new comments */
 
223
        $plugins = array();
 
224
        list ($ret, $useValidationPlugins) = GalleryCommentHelper::useValidationPlugins();
 
225
        if ($ret) {
 
226
            return array($ret->wrap(__FILE__, __LINE__), null);
 
227
        }
 
228
        if ($useValidationPlugins) {
 
229
            list ($ret, $allPluginIds) =
 
230
                GalleryCoreApi::getAllFactoryImplementationIds('GalleryValidationPlugin');
 
231
            if ( $ret) {
 
232
                return array($ret->wrap(__FILE__, __LINE__), null);
 
233
            }
 
234
 
 
235
            /* Let each plugin load its template data */
 
236
            foreach (array_keys($allPluginIds) as $pluginId) {
 
237
                list ($ret, $plugin) =
 
238
                    GalleryCoreApi::newFactoryInstanceById('GalleryValidationPlugin', $pluginId);
 
239
                if ($ret) {
 
240
                    return array($ret->wrap(__FILE__, __LINE__), null);
 
241
                }
 
242
                list ($ret, $data['file'], $data['l10Domain']) = $plugin->loadTemplate($form);
 
243
                if ($ret) {
 
244
                    return array($ret->wrap(__FILE__, __LINE__), null);
 
245
                }
 
246
                if (isset($data['file'])) {
 
247
                    $plugins[] = $data;
 
248
                }
 
249
            }
 
250
        }
 
251
 
 
252
        $template->setVariable('AddComment',
 
253
            array('itemId' => $item->getId(),
 
254
                  'host' => GalleryUtilities::getRemoteHostAddress(),
 
255
                  'plugins' => $plugins));
172
256
        $template->setVariable('controller', 'comment.AddComment');
173
257
 
174
258
        list($ret, $module) = GalleryCoreApi::loadPlugin('module', 'comment');
175
 
        if ($ret->isError()) {
 
259
        if ($ret) {
176
260
            return array($ret->wrap(__FILE__, __LINE__), null);
177
261
        }
178
262
 
179
263
        $template->title($module->translate('Add Comment'));
180
264
 
181
 
        return array(GalleryStatus::success(),
 
265
        return array(null,
182
266
                     array('body' => 'modules/comment/templates/AddComment.tpl'));
183
267
    }
184
268
 
187
271
     */
188
272
    function getViewDescription() {
189
273
        list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'comment');
190
 
        if ($ret->isError()) {
 
274
        if ($ret) {
191
275
            return array($ret->wrap(__FILE__, __LINE__), null);
192
276
        }
193
277
 
194
 
        return array(GalleryStatus::success(), $module->translate('add comment'));
 
278
        return array(null, $module->translate('add comment'));
195
279
    }
196
280
}
197
281
?>