This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
forked from osclass/Osclass
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathitem.php
executable file
·558 lines (480 loc) · 28 KB
/
item.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
<?php if ( ! defined('ABS_PATH')) exit('ABS_PATH is not loaded. Direct access is not allowed.');
/**
* OSClass – software for creating and publishing online classified advertising platforms
*
* Copyright (C) 2010 OSCLASS
*
* This program is free software: you can redistribute it and/or modify it under the terms
* of the GNU Affero General Public License as published by the Free Software Foundation,
* either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class CWebItem extends BaseModel
{
private $itemManager;
private $user;
private $userId;
function __construct()
{
parent::__construct() ;
$this->itemManager = Item::newInstance();
// here allways userId == ''
if( osc_is_web_user_logged_in() ) {
$this->userId = osc_logged_user_id();
$this->user = User::newInstance()->findByPrimaryKey($this->userId);
} else {
$this->userId = null;
$this->user = null;
}
}
//Business Layer...
function doModel()
{
//calling the view...
$locales = OSCLocale::newInstance()->listAllEnabled() ;
$this->_exportVariableToView('locales', $locales) ;
switch( $this->action ) {
case 'item_add': // post
if( osc_reg_user_post() && $this->user == null ) {
osc_add_flash_warning_message( _m('Only registered users are allowed to post listings') ) ;
$this->redirectTo(osc_user_login_url()) ;
}
$countries = Country::newInstance()->listAll();
$regions = array();
if( isset($this->user['fk_c_country_code']) && $this->user['fk_c_country_code']!='' ) {
$regions = Region::newInstance()->findByCountry($this->user['fk_c_country_code']);
} else if( count($countries) > 0 ) {
$regions = Region::newInstance()->findByCountry($countries[0]['pk_c_code']);
}
$cities = array();
if( isset($this->user['fk_i_region_id']) && $this->user['fk_i_region_id']!='' ) {
$cities = City::newInstance()->findByRegion($this->user['fk_i_region_id']) ;
} else if( count($regions) > 0 ) {
$cities = City::newInstance()->findByRegion($regions[0]['pk_i_id']) ;
}
$this->_exportVariableToView('countries',$countries ) ;
$this->_exportVariableToView('regions', $regions) ;
$this->_exportVariableToView('cities', $cities) ;
$form = count(Session::newInstance()->_getForm());
$keepForm = count(Session::newInstance()->_getKeepForm());
if($form==0 || $form==$keepForm) {
Session::newInstance()->_dropKeepForm();
}
if( Session::newInstance()->_getForm('countryId') != "" ) {
$countryId = Session::newInstance()->_getForm('countryId') ;
$regions = Region::newInstance()->findByCountry($countryId) ;
$this->_exportVariableToView('regions', $regions) ;
if(Session::newInstance()->_getForm('regionId') != "" ) {
$regionId = Session::newInstance()->_getForm('regionId') ;
$cities = City::newInstance()->findByRegion($regionId ) ;
$this->_exportVariableToView('cities', $cities ) ;
}
}
$this->_exportVariableToView('user', $this->user) ;
osc_run_hook('post_item');
$this->doView('item-post.php');
break;
case 'item_add_post': //post_item
if( osc_reg_user_post() && $this->user == null ) {
osc_add_flash_warning_message( _m('Only registered users are allowed to post listings') ) ;
$this->redirectTo( osc_base_url(true) ) ;
}
$mItems = new ItemActions(false);
// prepare data for ADD ITEM
$mItems->prepareData(true);
// set all parameters into session
foreach( $mItems->data as $key => $value ) {
Session::newInstance()->_setForm($key,$value);
}
$meta = Params::getParam('meta');
if(is_array($meta)) {
foreach( $meta as $key => $value ) {
Session::newInstance()->_setForm('meta_'.$key, $value);
Session::newInstance()->_keepForm('meta_'.$key);
}
}
if ((osc_recaptcha_private_key() != '') && Params::existParam("recaptcha_challenge_field")) {
if(!osc_check_recaptcha()) {
osc_add_flash_error_message( _m('The Recaptcha code is wrong') ) ;
$this->redirectTo( osc_item_post_url() );
return false; // BREAK THE PROCESS, THE RECAPTCHA IS WRONG
}
}
if(!osc_is_web_user_logged_in()) {
$user = User::newInstance()->findByEmail($mItems->data['contactEmail']);
// The user exists but it's not logged
if(isset($user['pk_i_id'])) {
foreach( $mItems->data as $key => $value ) {
Session::newInstance()->_keepForm($key);
}
osc_add_flash_error_message( _m('A user with that email address already exists, if it is you, please log in'));
$this->redirectTo(osc_user_login_url());
}
}
// POST ITEM ( ADD ITEM )
$success = $mItems->add();
if($success!=1 && $success!=2) {
osc_add_flash_error_message( $success) ;
$this->redirectTo( osc_item_post_url() );
} else {
Session::newInstance()->_dropkeepForm('meta_'.$key);
if($success==1) {
osc_add_flash_ok_message( _m('Check your inbox to validate your listing') ) ;
} else {
osc_add_flash_ok_message( _m('Your listing has been published') ) ;
}
$itemId = Params::getParam('itemId');
$item = $this->itemManager->findByPrimaryKey($itemId);
osc_run_hook('posted_item', $item);
$category = Category::newInstance()->findByPrimaryKey(Params::getParam('catId'));
View::newInstance()->_exportVariableToView('category', $category);
$this->redirectTo(osc_search_category_url());
}
break;
case 'item_edit': // edit item
$secret = Params::getParam('secret');
$id = Params::getParam('id');
$item = $this->itemManager->listWhere("i.pk_i_id = '%s' AND ((i.s_secret = '%s' AND i.fk_i_user_id IS NULL) OR (i.fk_i_user_id = '%d'))", addslashes($id), addslashes($secret), addslashes($this->userId));
if (count($item) == 1) {
$item = Item::newInstance()->findByPrimaryKey($id);
$form = count(Session::newInstance()->_getForm());
$keepForm = count(Session::newInstance()->_getKeepForm());
if($form == 0 || $form == $keepForm) {
Session::newInstance()->_dropKeepForm();
}
$this->_exportVariableToView('item', $item);
osc_run_hook("before_item_edit", $item);
$this->doView('item-edit.php');
} else {
// add a flash message [ITEM NO EXISTE]
osc_add_flash_error_message( _m("Sorry, we don't have any listings with that ID") ) ;
if($this->user != null) {
$this->redirectTo( osc_user_list_items_url() );
} else {
$this->redirectTo( osc_base_url() ) ;
}
}
break;
case 'item_edit_post':
// recoger el secret y el
$secret = Params::getParam('secret');
$id = Params::getParam('id');
$item = $this->itemManager->listWhere("i.pk_i_id = '%s' AND ((i.s_secret = '%s' AND i.fk_i_user_id IS NULL) OR (i.fk_i_user_id = '%d'))", addslashes($id), addslashes($secret), addslashes($this->userId)) ;
if (count($item) == 1) {
$this->_exportVariableToView('item', $item[0]) ;
$mItems = new ItemActions(false);
// prepare data for ADD ITEM
$mItems->prepareData(false);
// set all parameters into session
foreach( $mItems->data as $key => $value ) {
Session::newInstance()->_setForm($key,$value);
}
$meta = Params::getParam('meta');
if(is_array($meta)) {
foreach( $meta as $key => $value ) {
Session::newInstance()->_setForm('meta_'.$key, $value);
Session::newInstance()->_keepForm('meta_'.$key);
}
}
if( (osc_recaptcha_private_key() != '') && Params::existParam("recaptcha_challenge_field") ) {
if( !osc_check_recaptcha() ) {
osc_add_flash_error_message( _m('The Recaptcha code is wrong') ) ;
$this->redirectTo( osc_item_edit_url() ) ;
return false ; // BREAK THE PROCESS, THE RECAPTCHA IS WRONG
}
}
$success = $mItems->edit();
osc_run_hook('edited_item', Item::newInstance()->findByPrimaryKey($id));
if($success==1) {
osc_add_flash_ok_message( _m("Great! We've just updated your listing") ) ;
View::newInstance()->_exportVariableToView("item", Item::newInstance()->findByPrimaryKey($id));
$this->redirectTo( osc_item_url() ) ;
} else {
osc_add_flash_error_message( $success) ;
$this->redirectTo( osc_item_edit_url($secret) ) ;
}
}
break;
case 'activate':
$secret = Params::getParam('secret');
$id = Params::getParam('id');
$item = $this->itemManager->listWhere("i.pk_i_id = '%s' AND ((i.s_secret = '%s') OR (i.fk_i_user_id = '%d'))", addslashes($id), addslashes($secret), addslashes($this->userId)) ;
// item doesn't exist
if( count($item) == 0 ) {
$this->do404() ;
return ;
}
View::newInstance()->_exportVariableToView('item', $item[0]);
if( $item[0]['b_active'] == 0 ) {
// ACTIVETE ITEM
$mItems = new ItemActions(false) ;
$success = $mItems->activate( $item[0]['pk_i_id'], $item[0]['s_secret'] );
if( $success ) {
osc_add_flash_ok_message( _m('The listing has been validated') ) ;
}else{
osc_add_flash_error_message( _m("The listing can't be validated") ) ;
}
} else {
osc_add_flash_warning_message( _m('The listing has already been validated') ) ;
}
$this->redirectTo( osc_item_url( ) );
break;
case 'item_delete':
$secret = Params::getParam('secret');
$id = Params::getParam('id');
$item = $this->itemManager->listWhere("i.pk_i_id = '%s' AND ((i.s_secret = '%s') OR (i.fk_i_user_id = '%d'))", addslashes($id), addslashes($secret), addslashes($this->userId)) ;
if (count($item) == 1) {
$mItems = new ItemActions(false);
$success = $mItems->delete($item[0]['s_secret'], $item[0]['pk_i_id']);
if($success) {
osc_add_flash_ok_message( _m('Your listing has been deleted') ) ;
} else {
osc_add_flash_error_message( _m("The listing you are trying to delete couldn't be deleted") ) ;
}
if($this->user!=null) {
$this->redirectTo(osc_user_list_items_url());
} else {
$this->redirectTo( osc_base_url() ) ;
}
}else{
osc_add_flash_error_message( _m("The listing you are trying to delete couldn't be deleted") ) ;
$this->redirectTo( osc_base_url() ) ;
}
break;
case 'mark':
$id = Params::getParam('id') ;
$as = Params::getParam('as') ;
$item = Item::newInstance()->findByPrimaryKey($id);
View::newInstance()->_exportVariableToView('item', $item);
require_once(osc_lib_path() . 'osclass/user-agents.php');
foreach($user_agents as $ua) {
if(preg_match('|'.$ua.'|', @$_SERVER['HTTP_USER_AGENT'])) {
// mark item if it's not a bot
$mItem = new ItemActions(false) ;
$mItem->mark($id, $as) ;
break;
}
}
osc_add_flash_ok_message( _m("Thanks! That's very helpful") ) ;
$this->redirectTo( osc_item_url( ) );
break;
case 'send_friend':
$item = $this->itemManager->findByPrimaryKey( Params::getParam('id') );
$this->_exportVariableToView('item', $item) ;
$this->doView('item-send-friend.php');
break;
case 'send_friend_post':
$item = $this->itemManager->findByPrimaryKey( Params::getParam('id') );
$this->_exportVariableToView('item', $item) ;
Session::newInstance()->_setForm("yourEmail", Params::getParam('yourEmail'));
Session::newInstance()->_setForm("yourName", Params::getParam('yourName'));
Session::newInstance()->_setForm("friendName", Params::getParam('friendName'));
Session::newInstance()->_setForm("friendEmail", Params::getParam('friendEmail'));
Session::newInstance()->_setForm("message_body",Params::getParam('message'));
if ((osc_recaptcha_private_key() != '') && Params::existParam("recaptcha_challenge_field")) {
if(!osc_check_recaptcha()) {
osc_add_flash_error_message( _m('The Recaptcha code is wrong') ) ;
$this->redirectTo(osc_item_send_friend_url() );
return false; // BREAK THE PROCESS, THE RECAPTCHA IS WRONG
}
}
$mItem = new ItemActions(false);
$success = $mItem->send_friend();
if($success) {
Session::newInstance()->_clearVariables();
$this->redirectTo( osc_item_url() );
} else {
$this->redirectTo(osc_item_send_friend_url() );
}
break;
case 'contact':
$item = $this->itemManager->findByPrimaryKey( Params::getParam('id') ) ;
if( empty($item) ){
osc_add_flash_error_message( _m("This listing doesn't exist") );
$this->redirectTo( osc_base_url(true) );
} else {
$this->_exportVariableToView('item', $item) ;
if( osc_item_is_expired () ) {
osc_add_flash_error_message( _m("We're sorry, but the listing has expired. You can't contact the seller") ) ;
$this->redirectTo( osc_item_url() );
}
if( osc_reg_user_can_contact() && osc_is_web_user_logged_in() || !osc_reg_user_can_contact() ){
$this->doView('item-contact.php');
} else {
osc_add_flash_error_message( _m("You can't contact the seller, only registered users can") ) ;
$this->redirectTo( osc_item_url() );
}
}
break;
case 'contact_post':
$item = $this->itemManager->findByPrimaryKey( Params::getParam('id') ) ;
$this->_exportVariableToView('item', $item) ;
if ((osc_recaptcha_private_key() != '') && Params::existParam("recaptcha_challenge_field")) {
if(!osc_check_recaptcha()) {
osc_add_flash_error_message( _m('The Recaptcha code is wrong') ) ;
Session::newInstance()->_setForm("yourEmail", Params::getParam('yourEmail'));
Session::newInstance()->_setForm("yourName", Params::getParam('yourName'));
Session::newInstance()->_setForm("phoneNumber", Params::getParam('phoneNumber'));
Session::newInstance()->_setForm("message_body",Params::getParam('message'));
$this->redirectTo( osc_item_url( ) );
return false; // BREAK THE PROCESS, THE RECAPTCHA IS WRONG
}
}
if( osc_isExpired($item['dt_expiration']) ) {
osc_add_flash_error_message( _m("We're sorry, but the listing has expired. You can't contact the seller") ) ;
$this->redirectTo(osc_item_url());
}
$mItem = new ItemActions(false);
$result = $mItem->contact();
if(is_string($result)){
osc_add_flash_error_message( $result ) ;
} else {
osc_add_flash_ok_message( _m("We've just sent an e-mail to the seller") ) ;
}
$this->redirectTo( osc_item_url( ) );
break;
case 'add_comment':
$mItem = new ItemActions(false) ;
$status = $mItem->add_comment() ;
switch ($status) {
case -1: $msg = _m('Sorry, we could not save your comment. Try again later') ;
osc_add_flash_error_message($msg) ;
break ;
case 1: $msg = _m('Your comment is awaiting moderation') ;
osc_add_flash_info_message($msg) ;
break ;
case 2: $msg = _m('Your comment has been approved') ;
osc_add_flash_ok_message($msg) ;
break ;
case 3: $msg = _m('Please fill the required field (email)') ;
osc_add_flash_warning_message($msg) ;
break ;
case 4: $msg = _m('Please type a comment') ;
osc_add_flash_warning_message($msg) ;
break ;
case 5: $msg = _m('Your comment has been marked as spam') ;
osc_add_flash_error_message($msg) ;
break ;
}
$this->redirectTo( osc_item_url() ) ;
break ;
case 'delete_comment':
$mItem = new ItemActions(false);
$status = $mItem->add_comment();
$itemId = Params::getParam('id');
$commentId = Params::getParam('comment');
$item = Item::newInstance()->findByPrimaryKey($itemId);
if( count($item) == 0 ) {
osc_add_flash_error_message( _m("This listing doesn't exist") );
$this->redirectTo( osc_base_url(true) );
}
View::newInstance()->_exportVariableToView('item', $item);
if($this->userId == null) {
osc_add_flash_error_message(_m('You must be logged in to delete a comment') );
$this->redirectTo( osc_item_url() );
}
$commentManager = ItemComment::newInstance();
$aComment = $commentManager->findByPrimaryKey($commentId);
if( count($aComment) == 0 ) {
osc_add_flash_error_message( _m("The comment doesn't exist") );
$this->redirectTo( osc_item_url() );
}
if( $aComment['b_active'] != 1 ) {
osc_add_flash_error_message( _m('The comment is not active, you cannot delete it') );
$this->redirectTo( osc_item_url() );
}
if($aComment['fk_i_user_id'] != $this->userId) {
osc_add_flash_error_message( _m('The comment was not added by you, you cannot delete it') );
$this->redirectTo( osc_item_url() );
}
$commentManager->deleteByPrimaryKey($commentId);
osc_add_flash_ok_message( _m('The comment has been deleted' ) ) ;
$this->redirectTo( osc_item_url() );
break;
default:
// if there isn't ID, show an error 404
if( Params::getParam('id') == '') {
$this->do404() ;
return ;
}
if( Params::getParam('lang') != '' ) {
Session::newInstance()->_set('userLocale', Params::getParam('lang')) ;
}
$item = $this->itemManager->findByPrimaryKey( Params::getParam('id') );
// if item doesn't exist show an error 404
if( count($item) == 0 ) {
$this->do404() ;
return ;
}
if ($item['b_active'] != 1) {
if( $this->userId == $item['fk_i_user_id'] ) {
osc_add_flash_warning_message( _m("The listing hasn't been validated. Please validate it in order to make it public") );
} else {
osc_add_flash_warning_message( _m("This listing hasn't been validated") );
$this->redirectTo( osc_base_url(true) );
}
} else if ($item['b_enabled'] == 0) {
osc_add_flash_warning_message( _m('The listing has been suspended') );
$this->redirectTo( osc_base_url(true) );
}
if(!osc_is_admin_user_logged_in()) {
require_once(osc_lib_path() . 'osclass/user-agents.php');
foreach($user_agents as $ua) {
if(preg_match('|'.$ua.'|', @$_SERVER['HTTP_USER_AGENT'])) {
$mStats = new ItemStats();
$mStats->increase('i_num_views', $item['pk_i_id']);
break;
}
}
}
foreach($item['locale'] as $k => $v) {
$item['locale'][$k]['s_title'] = osc_apply_filter('item_title',$v['s_title']);
$item['locale'][$k]['s_description'] = nl2br(osc_apply_filter('item_description',$v['s_description']));
}
if( $item['fk_i_user_id'] != '' ) {
$user = User::newInstance()->findByPrimaryKey($item['fk_i_user_id']);
$this->_exportVariableToView('user', $user);
}
$this->_exportVariableToView('item', $item);
osc_run_hook('show_item', $item) ;
// redirect to the correct url just in case it has changed
$itemURI = str_replace(osc_base_url(), '', osc_item_url());
$URI = preg_replace('|^' . REL_WEB_URL . '|', '', $_SERVER['REQUEST_URI']);
// do not clean QUERY_STRING if permalink is not enabled
if( osc_rewrite_enabled () ) {
$URI = str_replace('?' . $_SERVER['QUERY_STRING'], '', $URI);
} else {
$params_keep = array('page', 'id');
$params = array();
foreach( Params::getParamsAsArray('get') as $k => $v ) {
if( in_array($k, $params_keep) ) {
$params[] = "$k=$v";
}
}
$URI = 'index.php?' . implode('&', $params);
}
// redirect to the correct url
if( $itemURI != $URI ) {
$this->redirectTo(osc_base_url() . $itemURI);
}
$this->doView('item.php') ;
break;
}
}
//hopefully generic...
function doView($file)
{
osc_run_hook("before_html");
osc_current_web_theme_path($file) ;
Session::newInstance()->_clearVariables();
osc_run_hook("after_html");
}
}
/* file end: ./item.php */
?>