forked from bkilgore/sugarcrm_dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_server.php
370 lines (302 loc) · 12.6 KB
/
json_server.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
<?php if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/*********************************************************************************
* SugarCRM Community Edition is a customer relationship management program developed by
* SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by the
* Free Software Foundation with the addition of the following permission added
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
* IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
* OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* 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 or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
* You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
* SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* SugarCRM" logo. If the display of the logo is not reasonably feasible for
* technical reasons, the Appropriate Legal Notices must display the words
* "Powered by SugarCRM".
********************************************************************************/
require_once('soap/SoapHelperFunctions.php');
$GLOBALS['log']->debug("JSON_SERVER:");
$global_registry_var_name = 'GLOBAL_REGISTRY';
///////////////////////////////////////////////////////////////////////////////
//// SUPPORTED METHODS
/*
* ADD NEW METHODS TO THIS ARRAY:
* then create a function called "function json_$method($request_id, &$params)"
* where $method is the method name
*/
$SUPPORTED_METHODS = array(
'retrieve',
'query',
);
/**
* Generic retrieve for getting data from a sugarbean
*/
function json_retrieve($request_id, $params) {
global $current_user;
global $beanFiles,$beanList;
$json = getJSONobj();
$record = $params[0]['record'];
require_once($beanFiles[$beanList[$params[0]['module']]]);
$focus = new $beanList[$params[0]['module']];
$focus->retrieve($record);
// to get a simplified version of the sugarbean
$module_arr = populateBean($focus);
$response = array();
$response['id'] = $request_id;
$response['result'] = array("status"=>"success","record"=>$module_arr);
$json_response = $json->encode($response, true);
print $json_response;
}
function json_query($request_id, $params) {
global $response, $sugar_config;
global $beanFiles, $beanList;
$json = getJSONobj();
if($sugar_config['list_max_entries_per_page'] < 31) // override query limits
$sugar_config['list_max_entries_per_page'] = 31;
$args = $params[0];
//decode condition parameter values..
if(is_array($args['conditions'])) {
foreach($args['conditions'] as $key=>$condition) {
if(!empty($condition['value'])) {
$where = $json->decode(utf8_encode($condition['value']));
// cn: bug 12693 - API change due to CSRF security changes.
$where = empty($where) ? $condition['value'] : $where;
$args['conditions'][$key]['value'] = $where;
}
}
}
$list_return = array();
if(! empty($args['module'])) {
$args['modules'] = array($args['module']);
}
foreach($args['modules'] as $module) {
require_once($beanFiles[$beanList[$module]]);
$focus = new $beanList[$module];
$query_orderby = '';
if(!empty($args['order'])) {
$query_orderby = preg_replace('/[^\w_.-]+/i', '', $args['order']['by']);
if(!empty($args['order']['desc'])) {
$query_orderby .= " DESC";
} else {
$query_orderby .= " ASC";
}
}
$query_limit = '';
if(!empty($args['limit'])) {
$query_limit = (int)$args['limit'];
}
$query_where = construct_where($args, $focus->table_name,$module);
$list_arr = array();
if($focus->ACLAccess('ListView', true)) {
$focus->ungreedy_count=false;
$curlist = $focus->get_list($query_orderby, $query_where, 0, $query_limit, -1, 0);
$list_return = array_merge($list_return,$curlist['list']);
}
}
$app_list_strings = null;
for($i = 0;$i < count($list_return);$i++) {
if(isset($list_return[$i]->emailAddress) && is_object($list_return[$i]->emailAddress)) {
$list_return[$i]->emailAddress->handleLegacyRetrieve($list_return[$i]);
}
$list_arr[$i]= array();
$list_arr[$i]['fields']= array();
$list_arr[$i]['module']= $list_return[$i]->object_name;
foreach($args['field_list'] as $field) {
// handle enums
if( (isset($list_return[$i]->field_name_map[$field]['type']) && $list_return[$i]->field_name_map[$field]['type'] == 'enum') ||
(isset($list_return[$i]->field_name_map[$field]['custom_type']) && $list_return[$i]->field_name_map[$field]['custom_type'] == 'enum')) {
// get fields to match enum vals
if(empty($app_list_strings)) {
if(isset($_SESSION['authenticated_user_language']) && $_SESSION['authenticated_user_language'] != '') $current_language = $_SESSION['authenticated_user_language'];
else $current_language = $sugar_config['default_language'];
$app_list_strings = return_app_list_strings_language($current_language);
}
// match enum vals to text vals in language pack for return
if(!empty($app_list_strings[$list_return[$i]->field_name_map[$field]['options']])) {
$list_return[$i]->$field = $app_list_strings[$list_return[$i]->field_name_map[$field]['options']][$list_return[$i]->$field];
}
}
$list_arr[$i]['fields'][$field] = $list_return[$i]->$field;
}
}
$response['id'] = $request_id;
$response['result'] = array("list"=>$list_arr);
$json_response = $json->encode($response, true);
echo $json_response;
}
//// END SUPPORTED METHODS
///////////////////////////////////////////////////////////////////////////////
// ONLY USED FOR MEETINGS
// HAS MEETING SPECIFIC CODE:
function populateBean(&$focus) {
$all_fields = $focus->column_fields;
// MEETING SPECIFIC
$all_fields = array_merge($all_fields,array('required','accept_status','name')); // need name field for contacts and users
//$all_fields = array_merge($focus->column_fields,$focus->additional_column_fields);
$module_arr = array();
$module_arr['module'] = $focus->object_name;
$module_arr['fields'] = array();
foreach($all_fields as $field)
{
if(isset($focus->$field) && !is_object($focus->$field))
{
$focus->$field = from_html($focus->$field);
$focus->$field = preg_replace("/\r\n/","<BR>",$focus->$field);
$focus->$field = preg_replace("/\n/","<BR>",$focus->$field);
$module_arr['fields'][$field] = $focus->$field;
}
}
$GLOBALS['log']->debug("JSON_SERVER:populate bean:");
return $module_arr;
}
///////////////////////////////////////////////////////////////////////////////
//// UTILS
function authenticate() {
global $sugar_config;
$user_unique_key =(isset($_SESSION['unique_key'])) ? $_SESSION['unique_key'] : "";
$server_unique_key =(isset($sugar_config['unique_key'])) ? $sugar_config['unique_key'] : "";
if($user_unique_key != $server_unique_key) {
$GLOBALS['log']->debug("JSON_SERVER: user_unique_key:".$user_unique_key."!=".$server_unique_key);
session_destroy();
return null;
}
if(!isset($_SESSION['authenticated_user_id'])) {
$GLOBALS['log']->debug("JSON_SERVER: authenticated_user_id NOT SET. DESTROY");
session_destroy();
return null;
}
$current_user = new User();
$result = $current_user->retrieve($_SESSION['authenticated_user_id']);
$GLOBALS['log']->debug("JSON_SERVER: retrieved user from SESSION");
if($result == null) {
$GLOBALS['log']->debug("JSON_SERVER: could get a user from SESSION. DESTROY");
session_destroy();
return null;
}
return $result;
}
function construct_where(&$query_obj, $table='',$module=null) {
if(! empty($table)) {
$table .= ".";
}
$cond_arr = array();
if(! is_array($query_obj['conditions'])) {
$query_obj['conditions'] = array();
}
foreach($query_obj['conditions'] as $condition) {
if ($condition['name']=='email1' or $condition['name']=='email2') {
$email1_value=strtoupper($condition['value']);
$email1_condition = " {$table}id in ( SELECT er.bean_id AS id FROM email_addr_bean_rel er, " .
"email_addresses ea WHERE ea.id = er.email_address_id " .
"AND ea.deleted = 0 AND er.deleted = 0 AND er.bean_module = '{$module}' AND email_address_caps LIKE '%{$email1_value}%' )";
array_push($cond_arr,$email1_condition);
}
else {
if($condition['op'] == 'contains') {
$cond_arr[] = $GLOBALS['db']->quote($table.$condition['name'])." like '%".$GLOBALS['db']->quote($condition['value'])."%'";
}
if($condition['op'] == 'like_custom') {
$like = '';
if(!empty($condition['begin'])) $like .= $GLOBALS['db']->quote($condition['begin']);
$like .= $GLOBALS['db']->quote($condition['value']);
if(!empty($condition['end'])) $like .= $GLOBALS['db']->quote($condition['end']);
$cond_arr[] = $GLOBALS['db']->quote($table.$condition['name'])." like '$like'";
} else { // starts_with
$cond_arr[] = $GLOBALS['db']->quote($table.$condition['name'])." like '".$GLOBALS['db']->quote($condition['value'])."%'";
}
}
}
if($table == 'users.') {
$cond_arr[] = $table."status='Active'";
}
return implode(" {$query_obj['group']} ",$cond_arr);
}
//// END UTILS
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//// JSON SERVER HANDLER LOGIC
//ignore notices
error_reporting(E_ALL & ~E_NOTICE);
ob_start();
insert_charset_header();
global $sugar_config;
if(!empty($sugar_config['session_dir'])) {
session_save_path($sugar_config['session_dir']);
$GLOBALS['log']->debug("JSON_SERVER:session_save_path:".$sugar_config['session_dir']);
}
session_start();
$GLOBALS['log']->debug("JSON_SERVER:session started");
$current_language = 'en_us'; // defaulting - will be set by user, then sys prefs
// create json parser
$json = getJSONobj();
// if the language is not set yet, then set it to the default language.
if(isset($_SESSION['authenticated_user_language']) && $_SESSION['authenticated_user_language'] != '') {
$current_language = $_SESSION['authenticated_user_language'];
} else {
$current_language = $sugar_config['default_language'];
}
$locale = new Localization();
$GLOBALS['log']->debug("JSON_SERVER: current_language:".$current_language);
// if this is a get, than this is spitting out static javascript as if it was a file
// wp: DO NOT USE THIS. Include the javascript inline using include/json_config.php
// using <script src=json_server.php></script> does not cache properly on some browsers
// resulting in 2 or more server hits per page load. Very bad for SSL.
if(strtolower($_SERVER['REQUEST_METHOD'])== 'get') {
echo "alert('DEPRECATED API\nPlease report as a bug.');";
} else {
// else act as a JSON-RPC server for SugarCRM
// create result array
$response = array();
$response['result'] = null;
$response['id'] = "-1";
// authenticate user
$current_user = authenticate();
if(empty($current_user)) {
$response['error'] = array("error_msg"=>"not logged in");
print $json->encode($response, true);
print "not logged in";
}
// extract request
if(isset($GLOBALS['HTTP_RAW_POST_DATA']))
$request = $json->decode($GLOBALS['HTTP_RAW_POST_DATA'], true);
else
$request = $json->decode(file_get_contents("php://input"), true);
if(!is_array($request)) {
$response['error'] = array("error_msg"=>"malformed request");
print $json->encode($response, true);
}
// make sure required RPC fields are set
if(empty($request['method']) || empty($request['id'])) {
$response['error'] = array("error_msg"=>"missing parameters");
print $json->encode($response, true);
}
$response['id'] = $request['id'];
if(in_array($request['method'], $SUPPORTED_METHODS)) {
call_user_func('json_'.$request['method'],$request['id'],$request['params']);
} else {
$response['error'] = array("error_msg"=>"method:".$request["method"]." not supported");
print $json->encode($response, true);
}
}
ob_end_flush();
sugar_cleanup();
exit();