]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - json_server.php
Release 6.1.4
[Github/sugarcrm.git] / json_server.php
1 <?php if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
2 /*********************************************************************************
3  * SugarCRM is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37
38 require_once('soap/SoapHelperFunctions.php');
39 $GLOBALS['log']->debug("JSON_SERVER:");
40 $global_registry_var_name = 'GLOBAL_REGISTRY';
41
42 ///////////////////////////////////////////////////////////////////////////////
43 ////    SUPPORTED METHODS
44 /*
45  * ADD NEW METHODS TO THIS ARRAY:
46  * then create a function called "function json_$method($request_id, &$params)"
47  * where $method is the method name
48  */
49 $SUPPORTED_METHODS = array(
50         'retrieve',
51         'query',
52         'set_accept_status',
53         'get_user_array', 
54         'get_objects_from_module', 
55         'email', 
56         'get_full_list'
57 );
58
59 /**
60  * Generic retrieve for getting data from a sugarbean
61  */
62 function json_retrieve($request_id, $params) {
63         global $current_user;
64         global $beanFiles,$beanList;
65     $json = getJSONobj();
66     
67         $record = $params[0]['record'];
68
69         require_once($beanFiles[$beanList[$params[0]['module']]]);
70         $focus = new $beanList[$params[0]['module']];
71         $focus->retrieve($record);
72
73         // to get a simplified version of the sugarbean
74         $module_arr = populateBean($focus);
75
76         $response = array();
77         $response['id'] = $request_id;
78         $response['result'] = array("status"=>"success","record"=>$module_arr);
79         $json_response = $json->encode($response, true);
80         print $json_response;
81 }
82
83 function json_query($request_id, $params) {
84         global $response, $sugar_config;
85         global $beanFiles, $beanList;
86         $json = getJSONobj();
87     
88         if($sugar_config['list_max_entries_per_page'] < 31)     // override query limits
89                 $sugar_config['list_max_entries_per_page'] = 31;
90
91         $args = $params[0];
92         
93         //decode condition parameter values..
94         if(is_array($args['conditions'])) {
95                 foreach($args['conditions'] as $key=>$condition)        {               
96                         if(!empty($condition['value'])) {
97                                 $where = $json->decode(utf8_encode($condition['value']));
98                                 // cn: bug 12693 - API change due to CSRF security changes.
99                                 $where = empty($where) ? $condition['value'] : $where;
100                                 $args['conditions'][$key]['value'] = $where;
101                         }               
102                 }
103         }
104
105         $list_return = array();
106         
107         if(! empty($args['module'])) {
108                 $args['modules'] = array($args['module']);
109         }
110         
111         foreach($args['modules'] as $module) {
112                 require_once($beanFiles[$beanList[$module]]);
113                 $focus = new $beanList[$module];
114                 
115                 $query_orderby = '';
116                 if(!empty($args['order'])) {
117                         $query_orderby = $args['order'];
118                 }
119                 $query_limit = '';
120                 if(!empty($args['limit'])) {
121                         $query_limit = $args['limit'];
122                 }
123                 $query_where = construct_where($args, $focus->table_name,$module);
124                 $list_arr = array();
125                 if($focus->ACLAccess('ListView', true)) {
126                         $focus->ungreedy_count=false;
127                         $curlist = $focus->get_list($query_orderby, $query_where, 0, $query_limit, -1, 0);
128                         $list_return = array_merge($list_return,$curlist['list']);
129                 }
130         }
131         
132         $app_list_strings = null;
133
134         for($i = 0;$i < count($list_return);$i++) {
135                 if(isset($list_return[$i]->emailAddress) && is_object($list_return[$i]->emailAddress)) {
136                         $list_return[$i]->emailAddress->handleLegacyRetrieve($list_return[$i]);
137                 }
138                 
139                 $list_arr[$i]= array();
140                 $list_arr[$i]['fields']= array();
141                 $list_arr[$i]['module']= $list_return[$i]->object_name;
142                 
143                 foreach($args['field_list'] as $field) {
144                         // handle enums
145                         if(     (isset($list_return[$i]->field_name_map[$field]['type']) && $list_return[$i]->field_name_map[$field]['type'] == 'enum') || 
146                                 (isset($list_return[$i]->field_name_map[$field]['custom_type']) && $list_return[$i]->field_name_map[$field]['custom_type'] == 'enum')) {
147                                 
148                                 // get fields to match enum vals
149                                 if(empty($app_list_strings)) {
150                                         if(isset($_SESSION['authenticated_user_language']) && $_SESSION['authenticated_user_language'] != '') $current_language = $_SESSION['authenticated_user_language'];
151                                         else $current_language = $sugar_config['default_language'];
152                                         $app_list_strings = return_app_list_strings_language($current_language);
153                                 }
154                                 
155                                 // match enum vals to text vals in language pack for return
156                                 if(!empty($app_list_strings[$list_return[$i]->field_name_map[$field]['options']])) {
157                                         $list_return[$i]->$field = $app_list_strings[$list_return[$i]->field_name_map[$field]['options']][$list_return[$i]->$field];
158                                 }
159                         }
160
161                         $list_arr[$i]['fields'][$field] = $list_return[$i]->$field;
162                 }
163         }
164
165
166         $response['id'] = $request_id;
167         $response['result'] = array("list"=>$list_arr);
168     $json_response = $json->encode($response, true);
169         echo $json_response;
170 }
171
172
173 function json_set_accept_status($request_id, $params) {
174         global $current_user;
175         global $beanFiles,$beanList;
176     $json = getJSONobj();
177         require_once($beanFiles[$beanList[$params[0]['module']]]);
178         
179         $focus = new $beanList[$params[0]['module']];
180         $focus->id = $params[0]['record'];
181         
182         $test = $focus->set_accept_status($current_user,$params[0]['accept_status']);
183         
184         $response = array();
185         $response['id'] = $request_id;
186         $response['result'] = array("status"=>"success","record"=>$params[0]['record'],'accept_status'=>$params[0]['accept_status']);
187         $json_response = $json->encode($response, true);
188         print $json_response;
189 }
190
191
192 /**
193  * retrieves Users matching passed criteria
194  */
195 function json_get_user_array($request_id, $params) {
196         $json = getJSONobj();
197     $args = $params[0];
198         
199         //decode condition parameter values..
200         if(is_array($args['conditions'])) {
201                 foreach($args['conditions'] as $key=>$condition) {              
202                         if(!empty($condition['value'])) {
203                                 $args['conditions'][$key]['value']=$json->decode($condition['value']);
204                         }
205                 }
206         }
207         
208         $response = array();
209         $response['id'] = $request_id;
210         $response['result'] = array();
211         $response['result']['list'] = array();
212         
213         if(showFullName()) {
214                 $user_array = getUserArrayFromFullName($args['conditions'][0]['value']);
215         } else {
216                 $user_array = get_user_array(false, "Active", $focus->assigned_user_id, false, $args['conditions'][0]['value']);
217         }
218         
219         foreach($user_array as $id=>$name) {
220                 array_push($response['result']['list'], array('fields' => array('id' => $id, 'user_name' => $name), 'module' => 'Users'));
221         }
222
223         print $json->encode($response, true);
224 }
225
226 function json_get_objects_from_module($request_id, $params) {
227         global $beanList, $beanFiles, $current_user;
228     $json = getJSONobj();
229     
230         $module_name = $params[0]['module'];
231         $offset = intval($params[0]['offset']);
232         $where = $params[0]['where'];
233         $max = $params[0]['max'];
234         $order_by = $params[0]['order_by'];
235     $using_cp = false;
236     
237     if($module_name == 'CampaignProspects'){
238         $module_name = 'Prospects';   
239         $using_cp = true;
240     }
241
242         $class_name = $beanList[$module_name];
243         require_once($beanFiles[$class_name]);
244         $seed = new $class_name();
245         if($where == ''){
246                 $where = '';
247         }
248         if($offset == '' || $offset == -1){
249                 $offset = 0;
250         }
251         if($max == ''){
252                 $max = 10;
253         }
254
255         $deleted = '0';
256      if($using_cp){
257         $fields = array('id', 'first_name', 'last_name');
258        $response = $seed->retrieveTargetList($where, $fields, $offset,-1,$max,$deleted);
259     }else{
260           $response = $seed->get_list($order_by, $where, $offset,-1,$max,$deleted);
261     }
262      
263         $list = $response['list'];
264         $row_count = $response['row_count'];
265
266         $output_list = array();
267         foreach($list as $value)
268         {
269                 $output_list[] = get_return_value($value, $module_name);
270         }
271         $response = array();
272         $response['id'] = $request_id;
273    
274         $response['result'] = array('result_count'=>$row_count,'entry_list'=>$output_list);
275         $json_response = $json->encode($response, true);
276         print $json_response;
277 }
278
279
280
281
282 function json_email($request_id, $params) {
283         global $response, $sugar_config;
284         global $beanFiles,$beanList;
285     $json = getJSONobj();
286     
287         $args = $params[0];
288
289         if($sugar_config['list_max_entries_per_page'] < 50)     // override query limits
290                 $sugar_config['list_max_entries_per_page'] = 50;
291
292         $list_return = array();
293
294         if(! empty($args['module'])) {
295                 $args['modules'] = array($args['module']);
296         }
297
298         foreach($args['modules'] as $module) {
299                 require_once($beanFiles[$beanList[$module]]);
300                 $focus = new $beanList[$module];
301         
302                 $query_orderby = '';
303                 if(!empty($args['order'])) {
304                         $query_orderby = $args['order'];
305                 }
306                 $query_limit = '';
307                 if(!empty($args['limit'])) {
308                         $query_limit = $args['limit'];
309                 }
310                 $query_where = construct_where($args,$focus->table_name);
311                 $list_arr = array();
312         
313                 $curlist = $focus->get_list($query_orderby, $query_where, 0, $query_limit, -1, 0);
314                 $list_return = array_merge($list_return,$curlist['list']);
315         }
316
317         for($i = 0;$i < count($list_return);$i++) {
318                 $list_arr[$i]= array();
319                 $list_arr[$i]['fields']= array();
320                 $list_arr[$i]['module']= $list_return[$i]->object_name;
321         
322                 foreach($args['field_list'] as $field) {
323                         $list_arr[$i]['fields'][$field] = $list_return[$i]->$field;
324                 }
325         }
326
327         $response['id'] = $request_id;
328         $response['result'] = array("list"=>$list_arr);
329         $json_response = $json->encode($response, true);
330         echo $json_response;
331 }
332
333
334 function json_get_full_list($request_id, $params) {
335         global $beanFiles;
336         global $beanList;
337         $json = getJSONobj();
338     require_once($beanFiles[$beanList[$params[0]['module']]]);
339
340         $where = str_replace('\\','', rawurldecode($params[0]['where']));
341         $order = str_replace('\\','', rawurldecode($params[0]['order']));
342         $focus = new $beanList[$params[0]['module']];
343         
344         $fullList = $focus->get_full_list($order, $where, '');
345         $all_fields = array_merge($focus->column_fields,$focus->additional_column_fields);
346
347         $js_fields_arr = array();
348
349         if(isset($fullList) && !empty($fullList)) { // json error if this isn't defensive
350                 $i=0; 
351                 foreach($fullList as $note) {
352                         $js_fields_arr[$i] = array();
353                         
354                         foreach($all_fields as $field) {
355                                 if(isset($note->$field)) {
356                                         $note->$field = from_html($note->$field);
357                                         $note->$field = preg_replace('/\r\n/','<BR>',$note->$field);
358                                         $note->$field = preg_replace('/\n/','<BR>',$note->$field);
359                                         $js_fields_arr[$i][$field] = addslashes($note->$field);
360                                 }
361                         }
362                         $i++;
363                 }
364         }
365         
366         $fin['id'] = $request_id;
367         $fin['result'] = $js_fields_arr;
368         $out = $json->encode($fin, true);
369         
370         print($out);
371 }
372 ////    END SUPPORTED METHODS
373 ///////////////////////////////////////////////////////////////////////////////
374
375
376
377
378
379
380
381
382
383
384
385 // ONLY USED FOR MEETINGS
386 function meeting_retrieve($module,$record) {
387         global $response;
388         global $beanFiles,$beanList;
389         //header('Content-type: text/xml');
390         require_once($beanFiles[$beanList[$module]]);
391         $focus = new $beanList[$module];
392     $json = getJSONobj();
393     
394         if(empty($module) || empty($record))
395         {
396                 $response['error'] = array("error_msg"=>"method: retrieve: missing module or record as parameters");
397                 print $json->encode($response, true);
398                 
399         }
400
401         $focus->retrieve($record);
402         
403         $GLOBALS['log']->debug("JSON_SERVER:retrieved meeting:");
404         
405         $module_arr = populateBean($focus);
406
407         if($module == 'Meetings')
408         {
409                 $users = $focus->get_meeting_users();
410         } else if($module == 'Calls')
411         {
412                 $users = $focus->get_call_users();
413         }
414
415         $module_arr['users_arr'] = array();
416
417         foreach($users as $user)
418         {
419                 array_push($module_arr['users_arr'],    populateBean($user));
420         }
421         $module_arr['orig_users_arr_hash'] = array();
422         foreach($users as $user)
423         {
424         $module_arr['orig_users_arr_hash'][$user->id] = '1';
425         }
426
427         $module_arr['contacts_arr'] = array();
428
429         $focus->load_relationships('contacts');
430         $contacts=$focus->get_linked_beans('contacts','Contact');
431         foreach($contacts as $contact)
432         {
433                 array_push($module_arr['users_arr'], populateBean($contact));
434         }
435
436         return $module_arr;
437 }
438
439 // HAS MEETING SPECIFIC CODE:
440 function populateBean(&$focus) {
441         $all_fields = $focus->list_fields;
442         // MEETING SPECIFIC
443         $all_fields = array_merge($all_fields,array('required','accept_status','name')); // need name field for contacts and users
444         //$all_fields = array_merge($focus->column_fields,$focus->additional_column_fields);
445
446         $module_arr = array();
447
448         $module_arr['module'] = $focus->object_name;
449
450         $module_arr['fields'] = array();
451
452         foreach($all_fields as $field)
453         {
454                 if(isset($focus->$field))
455                 {
456                         $focus->$field =        from_html($focus->$field);
457                         $focus->$field =        preg_replace("/\r\n/","<BR>",$focus->$field);
458                         $focus->$field =        preg_replace("/\n/","<BR>",$focus->$field);
459                         $module_arr['fields'][$field] = $focus->$field;
460                 }
461         }
462 $GLOBALS['log']->debug("JSON_SERVER:populate bean:");
463         return $module_arr;
464 }
465
466
467
468
469
470
471
472
473
474
475
476
477 function getUserJSON() {
478 }
479
480
481 function getUserConfigJSON() {
482  require_once('include/TimeDate.php');
483  $td = new TimeDate();
484  global $current_user,$global_registry_var_name,$json,$_SESSION,$sugar_config;
485
486  if(isset($_SESSION['authenticated_user_theme']) && $_SESSION['authenticated_user_theme'] != '')
487  {
488         $theme = $_SESSION['authenticated_user_theme'];
489  }
490  else
491  {
492         $theme = $sugar_config['default_theme'];
493  }
494  $user_arr = array();
495  $user_arr['theme'] = $theme;
496  $user_arr['fields'] = array();
497  $user_arr['module'] = 'User';
498  $user_arr['fields']['id'] = $current_user->id;
499  $user_arr['fields']['user_name'] = $current_user->user_name;
500  $user_arr['fields']['first_name'] = $current_user->first_name;
501  $user_arr['fields']['last_name'] = $current_user->last_name;
502  $user_arr['fields']['email'] = $current_user->email1;
503  $userTz = $td->getUserTimeZone();
504  $dstRange = $td->getDSTRange(date('Y'), $userTz);
505  $user_arr['fields']['dst_start'] = $dstRange['start'];
506  $user_arr['fields']['dst_end'] = $dstRange['end'];
507  $user_arr['fields']['gmt_offset'] = $userTz['gmtOffset'];
508  $str = "\n".$global_registry_var_name.".current_user = ".$json->encode($user_arr, true).";\n";
509 return $str;
510
511 }
512
513
514
515
516
517
518 ///////////////////////////////////////////////////////////////////////////////
519 ////    UTILS
520 function authenticate() {
521         global $sugar_config;
522
523         $user_unique_key =(isset($_SESSION['unique_key'])) ? $_SESSION['unique_key'] : "";
524         $server_unique_key =(isset($sugar_config['unique_key'])) ? $sugar_config['unique_key'] : "";
525
526         if($user_unique_key != $server_unique_key) {
527                 $GLOBALS['log']->debug("JSON_SERVER: user_unique_key:".$user_unique_key."!=".$server_unique_key);
528                 session_destroy();
529                 return null;
530         }
531
532         if(!isset($_SESSION['authenticated_user_id'])) {
533                 $GLOBALS['log']->debug("JSON_SERVER: authenticated_user_id NOT SET. DESTROY");
534                 session_destroy();
535                 return null;
536         }
537
538         $current_user = new User();
539
540         $result = $current_user->retrieve($_SESSION['authenticated_user_id']);
541         $GLOBALS['log']->debug("JSON_SERVER: retrieved user from SESSION");
542
543
544         if($result == null) {
545                 $GLOBALS['log']->debug("JSON_SERVER: could get a user from SESSION. DESTROY");
546                 session_destroy();
547                 return null;
548         }
549
550         return $result;
551 }
552
553 function construct_where(&$query_obj, $table='',$module=null) {
554         if(! empty($table)) {
555                 $table .= ".";
556         }
557         $cond_arr = array();
558
559         if(! is_array($query_obj['conditions'])) {
560                 $query_obj['conditions'] = array();
561         }
562
563         foreach($query_obj['conditions'] as $condition) {
564
565                 if ($condition['name']=='email1' or $condition['name']=='email2') {
566
567                         $email1_value=strtoupper($condition['value']);
568                         $email1_condition = " {$table}id in ( SELECT  er.bean_id AS id FROM email_addr_bean_rel er, " .
569                          "email_addresses ea WHERE ea.id = er.email_address_id " .
570                          "AND ea.deleted = 0 AND er.deleted = 0 AND er.bean_module = '{$module}' AND email_address_caps LIKE '%{$email1_value}%' )";
571                      
572                  array_push($cond_arr,$email1_condition);
573                 }
574                 else {
575                         if($condition['op'] == 'contains') {
576                                 $cond_arr[] = $GLOBALS['db']->quote($table.$condition['name'])." like '%".$GLOBALS['db']->quote($condition['value'])."%'";
577                         }
578                         if($condition['op'] == 'like_custom') {
579                                 $like = '';
580                                 if(!empty($condition['begin'])) $like .= $GLOBALS['db']->quote($condition['begin']);
581                                 $like .= $GLOBALS['db']->quote($condition['value']);
582                                 if(!empty($condition['end'])) $like .= $GLOBALS['db']->quote($condition['end']);
583                                 $cond_arr[] = $GLOBALS['db']->quote($table.$condition['name'])." like '$like'";
584                         } else { // starts_with
585                                 $cond_arr[] = $GLOBALS['db']->quote($table.$condition['name'])." like '".$GLOBALS['db']->quote($condition['value'])."%'";
586                         }
587                 }
588         }
589         
590         if($table == 'users.') {
591                 $cond_arr[] = $table."status='Active'";
592         }
593         
594         return implode(" {$query_obj['group']} ",$cond_arr);
595 }
596
597 function getAppMetaJSON() {
598         global $global_registry_var_name, $sugar_config;
599     $json = getJSONobj();
600     
601         $str = "\nvar ".$global_registry_var_name." = new Object();\n";
602         $str .= "\n".$global_registry_var_name.".config = {\"site_url\":\"".getJavascriptSiteURL()."\"};\n";
603         
604         $str .= $global_registry_var_name.".meta = new Object();\n";
605         $str .= $global_registry_var_name.".meta.modules = new Object();\n";
606         $modules_arr = array('Meetings','Calls');
607         $meta_modules = array();
608         
609         global $beanFiles,$beanList;
610         //header('Content-type: text/xml');
611         foreach($modules_arr as $module) {
612                 require_once($beanFiles[$beanList[$module]]);
613                 $focus = new $beanList[$module];
614                 $meta_modules[$module] = array();
615                 $meta_modules[$module]['field_defs'] = $focus->field_defs;
616         }
617         
618         $str .= $global_registry_var_name.".meta.modules.Meetings = ". $json->encode($meta_modules['Meetings'], true)."\n";
619         $str .= $global_registry_var_name.".meta.modules.Calls = ". $json->encode($meta_modules['Calls'], true)."\n";
620         return $str;
621 }
622
623 function getFocusData() {
624         global $global_registry_var_name;
625     $json = getJSONobj();
626     
627         if(empty($_REQUEST['module']) )
628                 return '';
629         elseif(empty($_REQUEST['record'] ) )
630                 return "\n".$global_registry_var_name.'["focus"] = {"module":"'.$_REQUEST['module'].'",users_arr:[],fields:{"id":"-1"}}'."\n";
631
632         $module_arr = meeting_retrieve($_REQUEST['module'], $_REQUEST['record']);
633         return "\n".$global_registry_var_name."['focus'] = ". $json->encode($module_arr, true).";\n";
634 }
635
636 function getStringsJSON() {
637         //set module and application string arrays based upon selected language
638         global $current_language;
639         global $global_registry_var_name;
640         $json = getJSONobj();
641     
642         $currentModule = 'Calendar';
643         $mod_list_strings = return_mod_list_strings_language($current_language,$currentModule);
644         $str = "\n".$global_registry_var_name."['calendar_strings'] =   {\"dom_cal_month_long\":". $json->encode($mod_list_strings['dom_cal_month_long']).",\"dom_cal_weekdays_long\":". $json->encode($mod_list_strings['dom_cal_weekdays_long'])."}\n";
645         
646         if(empty($_REQUEST['module']))
647                 $_REQUEST['module'] = 'Home';
648         
649         $currentModule = $_REQUEST['module'];
650         $mod_strings = return_module_language($current_language,$currentModule);
651         return $str . "\n".$global_registry_var_name."['meeting_strings'] =     ". $json->encode($mod_strings, true)."\n";
652 }
653 ////    END UTILS
654 ///////////////////////////////////////////////////////////////////////////////
655
656
657
658 ///////////////////////////////////////////////////////////////////////////////
659 ////    JSON SERVER HANDLER LOGIC
660 //ignore notices
661 error_reporting(E_ALL ^ E_NOTICE);
662 ob_start();
663 insert_charset_header();
664
665 if(!empty($sugar_config['session_dir'])) {
666         session_save_path($sugar_config['session_dir']);
667         $GLOBALS['log']->debug("JSON_SERVER:session_save_path:".$sugar_config['session_dir']);
668 }
669
670 session_start();
671 $GLOBALS['log']->debug("JSON_SERVER:session started");
672
673 $current_language = 'en_us'; // defaulting - will be set by user, then sys prefs
674
675 // create json parser
676 $json = getJSONobj();
677
678 // if the language is not set yet, then set it to the default language.
679 if(isset($_SESSION['authenticated_user_language']) && $_SESSION['authenticated_user_language'] != '') {
680         $current_language = $_SESSION['authenticated_user_language'];
681 } else {
682         $current_language = $sugar_config['default_language'];
683 }
684
685 $locale = new Localization();
686
687 $GLOBALS['log']->debug("JSON_SERVER: current_language:".$current_language);
688
689 // if this is a get, than this is spitting out static javascript as if it was a file
690 // wp: DO NOT USE THIS. Include the javascript inline using include/json_config.php
691 // using <script src=json_server.php></script> does not cache properly on some browsers
692 // resulting in 2 or more server hits per page load. Very bad for SSL. 
693 if(strtolower($_SERVER['REQUEST_METHOD'])== 'get') {
694         echo "alert('DEPRECATED API\nPlease report as a bug.');";
695         /**
696          * Deprecated for security reasons.
697          * 
698          * DO NOT USE.
699          * 
700          * 
701         $current_user = authenticate();
702         if(empty($current_user)) {
703                 $GLOBALS['log']->debug("JSON_SERVER: current_user isn't set");
704                 print "";
705         }
706
707         $str = '';
708         $str .= getAppMetaJSON();
709         $GLOBALS['log']->debug("JSON_SERVER:getAppMetaJSON");
710         
711         if($_GET['module'] != '_configonly') {
712                 $str .= getFocusData();
713                 $GLOBALS['log']->debug("JSON_SERVER: getFocusData");
714                 $str .= getStringsJSON();
715                 $GLOBALS['log']->debug("JSON_SERVER:getStringsJSON");
716         }
717         
718         $str .= getUserConfigJSON();
719         $GLOBALS['log']->debug("JSON_SERVER:getUserConfigJSON");
720         print $str;
721          */
722 } else {
723         // else act as a JSON-RPC server for SugarCRM
724         // create result array
725         $response = array();
726         $response['result'] = null;
727         $response['id'] = "-1";
728
729         // authenticate user
730         $current_user = authenticate();
731
732         if(empty($current_user)) {
733                 $response['error'] = array("error_msg"=>"not logged in");
734                 print $json->encode($response, true);
735                 print "not logged in";
736         }
737
738         // extract request
739         if(isset($GLOBALS['HTTP_RAW_POST_DATA']))
740                 $request = $json->decode($GLOBALS['HTTP_RAW_POST_DATA'], true);
741         else
742                 $request = $json->decode(file_get_contents("php://input"), true);
743         
744         
745         if(!is_array($request)) {
746                 $response['error'] = array("error_msg"=>"malformed request");
747                 print $json->encode($response, true);
748         }
749
750         // make sure required RPC fields are set
751         if(empty($request['method']) || empty($request['id'])) {
752                 $response['error'] = array("error_msg"=>"missing parameters");
753                 print $json->encode($response, true);
754         }
755         
756         $response['id'] = $request['id'];
757
758         if(in_array($request['method'], $SUPPORTED_METHODS)) {
759                 call_user_func('json_'.$request['method'],$request['id'],$request['params']);
760         } else {
761                 $response['error'] = array("error_msg"=>"method:".$request["method"]." not supported");
762                 print $json->encode($response, true);
763         }
764 }
765
766 ob_end_flush();
767 sugar_cleanup();
768 exit();
769 ?>