forked from glFusion/classifieds
/
edit.php
414 lines (359 loc) · 14.5 KB
/
edit.php
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<?php
/**
* Provide forms ad update functions for editing and submitting ads
*
* @author Lee Garner <lee@leegarner.com>
* @copyright Copyright (c) 2009 Lee Garner <lee@leegarner.com>
* @package classifieds
* @version 1.0.0
* @license http://opensource.org/licenses/gpl-2.0.php
* GNU Public License v2 or later
* @filesource
*/
/**
* Provide a form to edit a new or existing ad.
* @param array $A Array of ad data for edit form
* @param string $mode Edit mode
* @param boolean $admin True for administrator edit, false for normal
* @return string HTML for ad edit form
*/
function adEdit($A, $mode = 'edit', $admin=false)
{
global $_TABLES, $LANG_ADVT, $_CONF, $_CONF_ADVT, $LANG_ADMIN,
$_USER, $LANG_ACCESS, $_GROUPS, $LANG12, $LANG24, $MESSAGE,
$LANG_postmodes;
USES_classifieds_class_adtype();
// Determine if this user is an admin. Deprecates the $admin parameter.
$admin = SEC_hasRights($_CONF_ADVT['pi_name'] . '.admin') ? 1 : 0;
// only valid users allowed
if (COM_isAnonUser() ||
($_CONF_ADVT['usercanedit'] == 0 && !$admin)) {
return CLASSIFIEDS_errorMsg($LANG_ADVT['no_permission'], 'alert',
$LANG_ADVT['access_denied']);
}
// We know that we need to have categories, so make sure some exist
// before even trying to display the form. The category dropdown is
// created later since it needs the existing cat_id, if any.
if (DB_count($_TABLES['ad_category']) < 1) {
return CLASSIFIEDS_errorMsg($LANG_ADVT['no_categories'], 'info');
}
$time = time(); // used to compare now with expiration date
if ($admin) {
$T = new Template(CLASSIFIEDS_PI_PATH . '/templates/admin');
$T->set_file('adedit', "adminedit.thtml");
$action_url = CLASSIFIEDS_ADMIN_URL . '/index.php';
} else {
$T = new Template(CLASSIFIEDS_PI_PATH . '/templates');
$T->set_file('adedit', "submitform.thtml");
$action_url = CLASSIFIEDS_URL . '/index.php';
}
// Set up the wysiwyg editor, if available
switch (PLG_getEditorType()) {
case 'ckeditor':
$T->set_var('show_htmleditor', true);
PLG_requestEditor('classifieds','classifieds_entry','ckeditor_classifieds.thtml');
PLG_templateSetVars('classifieds_entry', $T);
break;
case 'tinymce' :
$T->set_var('show_htmleditor',true);
PLG_requestEditor('classifieds','classifieds_entry','tinymce_classifieds.thtml');
PLG_templateSetVars('classifieds_entry', $T);
break;
default :
// don't support others right now
$T->set_var('show_htmleditor', false);
break;
}
switch ($mode) {
case 'editsubmission':
case 'moderate':
$savemode = 'savesubmission';
$delete_img = 'delsubimg';
$delete_ad = 'deletesubmission';
$type = 'moderate';
$saveoption = $LANG_ADMIN['moderate'];
$cancel_url = $_CONF['site_admin_url'] . '/moderation.php';
break;
case 'edit':
$savemode = 'savesubmission';
$delete_img = 'delsubimg';
$delete_ad = 'deletesubmission';
$saveoption = $LANG_ADMIN['save'];
$type = 'submission';
$cancel_url = $action_url;
break;
case 'update_ad':
default:
$savemode = 'update_ad';
$delete_img = 'delete_img';
$delete_ad = 'delete_ad';
$saveoption = $LANG_ADMIN['save'];
$type = '';
$cancel_url = $action_url;
break;
}
// Admins (only) use this form for submissions as well as edits,
// so we need to expect an empty array.
if (empty($A['ad_id'])) {
if (!$admin) {
return CLASSIFIEDS_errorMsg($LANG_ADVT['no_permission'], 'alert',
$LANG_ADVT['access_denied']);
}
$A['ad_id'] = COM_makeSid();
$A['subject'] = '';
$A['descript'] = '';
$A['price'] = '';
$A['url'] = '';
$A['exp_date'] = '';
$A['add_date'] = time();
$A['ad_type'] = 0;
$A['perm_owner'] = $_CONF_ADVT['default_permissions'][0];
$A['perm_group'] = $_CONF_ADVT['default_permissions'][1];
$A['perm_members'] = $_CONF_ADVT['default_permissions'][2];
$A['perm_anon'] = $_CONF_ADVT['default_permissions'][3];
$A['uid'] = $_USER['uid'];
if (isset($_REQUEST['cat'])) {
$A['cat_id'] = intval($_REQUEST['cat']);
} else {
$A['cat_id'] = 0;
}
$catsql = "SELECT cat_id,perm_anon,keywords
FROM {$_TABLES['ad_category']} ";
if ($A['cat_id'] > 0) {
$catsql .= "WHERE cat_id = {$A['cat_id']} ";
} else {
$catsql .= "ORDER BY cat_name ASC ";
}
$catsql .= "LIMIT 1";
$r = DB_query($catsql, 1);
if ($r && DB_numRows($r) > 0) {
$row = DB_fetchArray($r, false);
$A['cat_id'] = $row['cat_id'];
$A['keywords'] = trim($row['keywords']);
} else {
$A['cat_id'] = 0;
$A['keywords'] = '';
}
$A['owner_id'] = $_USER['uid']; // Set ad owner to current user for new ads
$A['group_id'] = isset($_GROUPS['classifieds Admin']) ?
$_GROUPS['classifieds Admin'] :
SEC_getFeatureGroup('classifieds.edit');
$A['exp_sent'] = 0;
// set expiration & duration info for a new ad
$T->set_var('expiration_date', $LANG_ADVT['runfor']); // "run for: X days"
$comments_enabled = $_CONF_ADVT['commentsupport'] == 1 ? 0 : 1;
$T->set_var("sel_{$comments_enabled}", 'selected');
if ($_CONF_ADVT['purchase_enabled']) {
USES_classifieds_class_userinfo();
$User = new adUserInfo();
$T->set_var('days',
min($_CONF_ADVT['default_duration'], $User->getMaxDays()));
} else {
$T->set_var('days', $_CONF_ADVT['default_duration']);
}
$photocount = 0; // No photos yet with a new ad
} else {
// This is an existing ad with values already in $A
$T->set_var('expiration_date', $LANG_ADVT['expiration']);
$T->set_var('days', '0');
// Disable the perm_anon checkbox if it's disabled by the category.
if (!$admin && DB_getItem($_TABLES['ad_category'],
'perm_anon', "cat_id='{$A['cat_id']}'") == '0') {
$T->set_var('vis_disabled', 'disabled');
}
// get the photo information
$sql = "SELECT photo_id, filename
FROM {$_TABLES['ad_photo']}
WHERE ad_id='{$A['ad_id']}'";
$photo = DB_query($sql, 1);
// save the count of photos for later use
if ($photo)
$photocount = DB_numRows($photo);
else
$photocount = 0;
$comments_enabled = (int)$A['comments_enabled'];
$T->set_var("sel_{$comments_enabled}", 'selected');
}
// Get the max image size in MB and set the message
$img_max = $_CONF['max_image_size'] / 1048576; // Show in MB
// Sanitize entries from the database
$A['subject'] = htmlspecialchars($A['subject']);
$A['descript'] = htmlspecialchars($A['descript']);
$A['keywords'] = htmlspecialchars($A['keywords']);
$A['price'] = htmlspecialchars($A['price']);
$A['url'] = htmlspecialchars($A['url']);
$A['ad_type'] = (int)$A['ad_type'];
// set expiration & duration based on existing info
if ($A['exp_date'] == '') {
$T->set_var('row_exp_date', '');
} else if ($A['exp_date'] < $time) {
$T->set_var('already_expired', $LANG_ADVT['already_expired']);
} else {
$T->set_var('row_exp_date', date("d M Y", $A['exp_date']));
}
$T->set_var(array(
'post_options' => $post_options,
'change_editormode' => 'onchange="change_editmode(this);"',
'glfusionStyleBasePath' => $_CONF['site_url']. '/fckeditor',
'gltoken_name' => CSRF_TOKEN,
'gltoken' => SEC_createToken(),
'has_delbtn' => 'true',
'txt_photo' => "{$LANG_ADVT['photo']}<br />" .
sprintf($LANG_ADVT['image_max'], $img_max),
'type' => $type,
'action_url' => $action_url,
'max_file_size' => $_CONF['max_image_size'],
'row_cat_id' => $A['cat_id'],
'row_ad_id' => $A['ad_id'],
'row_subject' => $A['subject'],
'row_descript' => $A['descript'],
'row_price' => $A['price'],
'row_url' => $A['url'],
'keywords' => $A['keywords'],
'exp_date' => $A['exp_date'],
'add_date' => $A['add_date'],
'ad_type_selection' => AdType::makeSelection($A['ad_type']),
'sel_list_catid' => CLASSIFIEDS_buildCatSelection($A['cat_id']),
'saveoption' => $saveoption,
'cancel_url' => $cancel_url,
) );
// set expiration & duration based on existing info
if ($A['exp_date'] == '') {
$T->set_var('row_exp_date', '');
} else if ($A['exp_date'] < $time) {
$T->set_var('already_expired', $LANG_ADVT['already_expired']);
} else {
$T->set_var('row_exp_date', date("d M Y", $A['exp_date']));
}
// Set up permission editor on the admin template if needed.
// Otherwise, set hidden values with existing permissions
if ($admin) {
// Set up owner selection
$T->set_var(array(
'ownerselect' => CLASSIFIEDS_userDropdown($A['owner_id']),
'permissions_editor' => SEC_getPermissionsHTML(
$A['perm_owner'],$A['perm_group'],
$A['perm_members'],$A['perm_anon']),
'group_dropdown' => SEC_getGroupDropdown($A['group_id'], 3),
) );
} else {
$ownername = COM_getDisplayName($A['owner_id']);
$T->set_var(array(
'owner_id' => $A['owner_id'],
'ownername' => $ownername,
'perm_owner' => $A['perm_owner'],
'perm_group' => $A['perm_group'],
'perm_members' => $A['perm_members'],
'perm_anon' => $A['perm_anon'],
'group_id' => $A['group_id'],
) );
if ($A['perm_anon'] == 2)
$T->set_var('perm_anon_chk', 'checked');
}
// Set up the photo fields. Use $photocount defined above.
// If there are photos, read the $photo result. Otherwise,
// or if this is a new ad, just clear the photo area
$T->set_block('adedit', 'PhotoRow', 'PRow');
$i = 0;
if ($photocount > 0) {
while ($prow = DB_fetchArray($photo, false)) {
$i++;
$T->set_var(array(
'img_url' => LGLIB_ImageUrl(CLASSIFIEDS_IMGPATH . '/' . $prow['filename'],
$_CONF_ADVT['img_max_width'], $_CONF_ADVT['img_max_height']),
'thumb_url' => LGLIB_ImageUrl(CLASSIFIEDS_IMGPATH . '/' . $prow['filename'],
$_CONF_ADVT['thumb_max_size'], $_CONF_ADVT['thumb_max_size']),
'seq_no' => $i,
'ad_id' => $A['ad_id'],
'del_img_url' => $action_url .
"?mode=$delete_img&mid={$prow['photo_id']}" .
"&id={$A['ad_id']}",
) );
$T->parse('PRow', 'PhotoRow', true);
}
} else {
$T->parse('PRow', '');
}
// add upload fields for unused images
$T->set_block('adedit', 'UploadFld', 'UFLD');
for ($j = $i; $j < $_CONF_ADVT['imagecount']; $j++) {
$T->parse('UFLD', 'UploadFld', true);
}
$T->parse('output','adedit');
return $T->finish($T->get_var('output'));
} // adEdit()
/**
* Delete a single image from an ad
* @param boolean $admin True if this is an admin, false if not
* @param string $adTable Table ID, either production or submission
* @return string Error message, if any
*/
function imgDelete($admin=false, $adTable = 'ad_ads')
{
global $_TABLES, $_USER, $_CONF_ADVT;
$mid = isset($_REQUEST['mid']) ? intval($_REQUEST['mid']) : 0;
if (!$mid)
return "No image selected";
if ($adTable != 'ad_ads' && $adTable != 'ad_submission') {
return "Invalid ad table specified";
}
// find the ad corresponding to this image. Don't trust the input.
$sql = "SELECT ad_id, filename
FROM {$_TABLES['ad_photo']}
WHERE photo_id=$mid";
$result = DB_query($sql, 1);
if (!$result || DB_numrows($result) < 1)
return '';
$row = DB_fetchArray($result, false);
// now get the ad id. We're really just checking that the current
// user is the ad's owner.
$sql = "
SELECT
ad_id
FROM
{$_TABLES[$adTable]}
WHERE
ad_id={$row['ad_id']}
";
// Set up base url for links here while we're checking admin status
if (!$admin) {
$base_url = $_CONF['site_url'] . '/' .
$_CONF_ADVT['pi_name'] . '/index.php';
$sql .= "AND uid='{$_USER['uid']}'";
} else {
$base_url = $_CONF['site_admin_url'] . '/plugins';
}
$result = DB_query($sql, 1);
if (DB_numrows($result) < 1) {
return "Unauthorized Access";
}
// Otherwise, this is the right owner for this ad, so delete the image
// and thumbnail from the filesystem and database
if (file_exists(CLASSIFIEDS_IMGPATH . '/' . $row['filename']))
unlink(CLASSIFIEDS_IMGPATH . '/' . $row['filename']);
DB_delete($_TABLES['ad_photo'], 'photo_id', $mid);
// No text returned unless there was an error
return '';
} // imgDelete();
/**
* Returns the <option></option> portion to be used
* within a <select></select> block to choose users from a dropdown list
* @param string $sel ID of selected value
* @return string HTML output containing options
*/
function CLASSIFIEDS_userDropdown($selId = '')
{
global $_TABLES;
$retval = '';
// Find users, excluding anonymous
$sql = "SELECT uid FROM {$_TABLES['users']}
WHERE uid > 1";
$result = DB_query($sql, 1);
while ($row = DB_fetchArray($result, false)) {
$name = COM_getDisplayName($row['uid']);
$sel = $row['uid'] == $selId ? 'selected' : '';
$retval .= "<option value=\"{$row['uid']}\" $sel>$name</option>\n";
}
return $retval;
} // function CLASSIFIEDS_userDropdown()
?>