forked from osclass/Osclass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax.php
executable file
·249 lines (223 loc) · 11.2 KB
/
ajax.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
<?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/>.
*/
define('IS_AJAX', true) ;
class CWebAjax extends BaseModel
{
function __construct()
{
parent::__construct() ;
$this->ajax = true ;
}
//Business Layer...
function doModel()
{
//specific things for this class
switch ($this->action)
{
case 'bulk_actions':
break;
case 'regions': //Return regions given a countryId
$regions = Region::newInstance()->findByCountry(Params::getParam("countryId"));
echo json_encode($regions);
break;
case 'cities': //Returns cities given a regionId
$cities = City::newInstance()->findByRegion(Params::getParam("regionId"));
echo json_encode($cities);
break;
case 'location': // This is the autocomplete AJAX
$cities = City::newInstance()->ajax(Params::getParam("term"));
foreach($cities as $k => $city) {
$cities[$k]['label'] = $city['label']." (".$city['region'].")";
}
echo json_encode($cities);
break;
case 'location_countries': // This is the autocomplete AJAX
$countries = Country::newInstance()->ajax(Params::getParam("term"));
echo json_encode($countries);
break;
case 'location_regions': // This is the autocomplete AJAX
$regions = Region::newInstance()->ajax(Params::getParam("term"), Params::getParam("country"));
echo json_encode($regions);
break;
case 'location_cities': // This is the autocomplete AJAX
$cities = City::newInstance()->ajax(Params::getParam("term"), Params::getParam("region"));
echo json_encode($cities);
break;
case 'delete_image': // Delete images via AJAX
$id = Params::getParam('id') ;
$item = Params::getParam('item') ;
$code = Params::getParam('code') ;
$secret = Params::getParam('secret') ;
$json = array();
if( Session::newInstance()->_get('userId') != '' ){
$userId = Session::newInstance()->_get('userId');
$user = User::newInstance()->findByPrimaryKey($userId);
}else{
$userId = null;
$user = null;
}
// Check for required fields
if ( !( is_numeric($id) && is_numeric($item) && preg_match('/^([a-z0-9]+)$/i', $code) ) ) {
$json['success'] = false;
$json['msg'] = _m("The selected photo couldn't be deleted, the url doesn't exist");
echo json_encode($json);
return false;
}
$aItem = Item::newInstance()->findByPrimaryKey($item);
// Check if the item exists
if(count($aItem) == 0) {
$json['success'] = false;
$json['msg'] = _m("The listing doesn't exist");
echo json_encode($json);
return false;
}
if(!osc_is_admin_user_logged_in()) {
// Check if the item belong to the user
if($userId != null && $userId != $aItem['fk_i_user_id']) {
$json['success'] = false;
$json['msg'] = _m("The listing doesn't belong to you");
echo json_encode($json);
return false;
}
// Check if the secret passphrase match with the item
if($userId == null && $aItem['fk_i_user_id']==null && $secret != $aItem['s_secret']) {
$json['success'] = false;
$json['msg'] = _m("The listing doesn't belong to you");
echo json_encode($json);
return false;
}
}
// Does id & code combination exist?
$result = ItemResource::newInstance()->existResource($id, $code) ;
if ($result > 0) {
$resource = ItemResource::newInstance()->findByPrimaryKey($id);
if($resource['fk_i_item_id']==$item) {
// Delete: file, db table entry
if(defined(OC_ADMIN)) {
osc_deleteResource($id, true);
Log::newInstance()->insertLog('ajax', 'deleteimage', $id, $id, 'admin', osc_logged_admin_id()) ;
} else {
osc_deleteResource($id, false);
Log::newInstance()->insertLog('ajax', 'deleteimage', $id, $id, 'user', osc_logged_user_id()) ;
}
ItemResource::newInstance()->delete(array('pk_i_id' => $id, 'fk_i_item_id' => $item, 's_name' => $code) );
$json['msg'] = _m('The selected photo has been successfully deleted') ;
$json['success'] = 'true';
} else {
$json['msg'] = _m("The selected photo does not belong to you") ;
$json['success'] = 'false';
}
} else {
$json['msg'] = _m("The selected photo couldn't be deleted") ;
$json['success'] = 'false';
}
echo json_encode($json);
return true;
break;
case 'alerts': // Allow to register to an alert given (not sure it's used on admin)
$alert = Params::getParam("alert");
$email = Params::getParam("email");
$userid = Params::getParam("userid");
if($alert!='' && $email!='') {
if( preg_match("/^[_a-z0-9-+]+(\.[_a-z0-9-+]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/",$email) ) {
$secret = osc_genRandomPassword();
if( Alerts::newInstance()->createAlert($userid, $email, $alert, $secret) ) {
if( (int)$userid > 0 ) {
$user = User::newInstance()->findByPrimaryKey($userid);
if($user['b_active']==1 && $user['b_enabled']==1) {
Alerts::newInstance()->activate($email, $secret);
echo '1';
return true;
} else {
echo '-1';
return false;
}
} else {
osc_run_hook('hook_email_alert_validation', $alert, $email, $secret);
}
echo "1";
} else {
echo "0";
}
return true;
} else {
echo '-1';
return false;
}
}
echo '0';
return false;
break;
case 'runhook': // run hooks
$hook = Params::getParam('hook');
if($hook == '') {
echo json_encode(array('error' => 'hook parameter not defined')) ;
break;
}
switch($hook) {
case 'item_form':
osc_run_hook('item_form', Params::getParam('catId'));
break;
case 'item_edit':
$catId = Params::getParam("catId");
$itemId = Params::getParam("itemId");
osc_run_hook("item_edit", $catId, $itemId);
break;
default:
osc_run_hook('ajax_' . $hook);
break;
}
break;
case 'custom': // Execute via AJAX custom file
$ajaxFile = Params::getParam("ajaxfile");
if($ajaxFile == '') {
echo json_encode(array('error' => 'no action defined'));
break ;
}
// valid file?
if( stripos($ajaxFile, '../') !== false ) {
echo json_encode(array('error' => 'no valid ajaxFile'));
break ;
}
if( !file_exists(osc_plugins_path() . $ajaxFile) ) {
echo json_encode(array('error' => "ajaxFile doesn't exist"));
break;
}
require_once osc_plugins_path() . $ajaxFile ;
break;
default:
echo json_encode(array('error' => __('no action defined')));
break;
}
// clear all keep variables into session
Session::newInstance()->_dropKeepForm();
Session::newInstance()->_clearVariables();
}
//hopefully generic...
function doView($file)
{
osc_run_hook("before_html");
osc_current_web_theme_path($file) ;
osc_run_hook("after_html");
}
}
/* file end: ./ajax.php */
?>