2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
37 require_once('service/v3_1/SugarWebServiceUtilv3_1.php');
39 class SugarWebServiceUtilv4 extends SugarWebServiceUtilv3_1
41 function get_module_view_defs($moduleName, $type, $view)
43 require_once('include/MVC/View/SugarView.php');
46 if( empty($moduleName) )
49 $view = strtolower($view);
50 switch (strtolower($type)){
53 require_once('include/SugarWireless/SugarWirelessListView.php');
54 $GLOBALS['module'] = $moduleName; //WirelessView keys off global variable not instance variable...
55 $v = new SugarWirelessListView();
56 $results = $v->getMetaDataFile();
57 $results = self::formatWirelessListViewResultsToArray($results);
60 elseif ($view == 'subpanel')
61 $results = $this->get_subpanel_defs($moduleName, $type);
63 require_once('include/SugarWireless/SugarWirelessView.php');
64 $v = new SugarWirelessView();
65 $v->module = $moduleName;
66 $fullView = ucfirst($view) . 'View';
67 $meta = $v->getMetaDataFile('Wireless' . $fullView);
68 $metadataFile = $meta['filename'];
69 require($metadataFile);
70 //Wireless detail metadata may actually be just edit metadata.
71 $results = isset($viewdefs[$meta['module_name']][$fullView] ) ? $viewdefs[$meta['module_name']][$fullView] : $viewdefs[$meta['module_name']]['EditView'];
77 if ($view == 'subpanel')
78 $results = $this->get_subpanel_defs($moduleName, $type);
81 $v = new SugarView(null,array());
82 $v->module = $moduleName;
84 $fullView = ucfirst($view) . 'View';
85 $metadataFile = $v->getMetaDataFile();
86 require_once($metadataFile);
88 $results = $listViewDefs[$moduleName];
90 $results = $viewdefs[$moduleName][$fullView];
94 //Add field level acls.
95 $results = $this->addFieldLevelACLs($moduleName,$type, $view, $results);
101 * Format the results for wirless list view metadata from an associative array to a
102 * numerically indexed array. This conversion will ensure that consumers of the metadata
103 * can eval the json response and iterative over the results with the order of the fields
106 * @param array $fields
109 function formatWirelessListViewResultsToArray($fields)
112 foreach($fields as $key => $defs)
114 $defs['name'] = $key;
122 * Equivalent of get_list function within SugarBean but allows the possibility to pass in an indicator
123 * if the list should filter for favorites. Should eventually update the SugarBean function as well.
126 function get_data_list($seed, $order_by = "", $where = "", $row_offset = 0, $limit=-1, $max=-1, $show_deleted = 0, $favorites = false, $singleSelect=false)
128 $GLOBALS['log']->debug("get_list: order_by = '$order_by' and where = '$where' and limit = '$limit'");
129 if(isset($_SESSION['show_deleted']))
133 $order_by=$seed->process_order_by($order_by, null);
135 if($seed->bean_implements('ACL') && ACLController::requireOwner($seed->module_dir, 'list') )
137 global $current_user;
138 $owner_where = $seed->getOwnerWhere($current_user->id);
139 if(!empty($owner_where)){
141 $where = $owner_where;
143 $where .= ' AND '. $owner_where;
148 if($favorites === TRUE )
149 $params['favorites'] = true;
151 $query = $seed->create_new_list_query($order_by, $where,array(),$params, $show_deleted,'',false,null,$singleSelect);
152 return $seed->process_list_query($query, $row_offset, $limit, $max, $where);
156 * Convert modules list to Web services result
158 * @param array $list List of module candidates (only keys are used)
159 * @param array $availModules List of module availability from Session
161 public function getModulesFromList($list, $availModules)
163 global $app_list_strings;
164 $enabled_modules = array();
165 $availModulesKey = array_flip($availModules);
166 foreach ($list as $key=>$value)
168 if( isset($availModulesKey[$key]) )
170 $label = !empty( $app_list_strings['moduleList'][$key] ) ? $app_list_strings['moduleList'][$key] : '';
171 $acl = $this->checkModuleRoleAccess($key);
172 $fav = $this->is_favorites_enabled($key);
173 $enabled_modules[] = array('module_key' => $key,'module_label' => $label, 'favorite_enabled' => $fav, 'acls' => $acl);
176 return $enabled_modules;
180 * Return a boolean indicating if the bean name is favorites enabled.
182 * @param string The module name
183 * @return bool true indicating bean is favorites enabled
185 function is_favorites_enabled($module_name)
187 global $beanList, $beanFiles;
194 * Parse wireless editview metadata and add ACL values.
196 * @param String $module_name
197 * @param array $metadata
198 * @return array Metadata with acls added
200 function metdataAclParserWirelessEdit($module_name, $metadata)
202 global $beanList, $beanFiles;
203 $class_name = $beanList[$module_name];
204 require_once($beanFiles[$class_name]);
205 $seed = new $class_name();
208 $results['templateMeta'] = $metadata['templateMeta'];
210 //Wireless metadata only has a single panel definition.
211 foreach ($metadata['panels'] as $row)
214 foreach ($row as $field)
217 if( is_string($field) )
218 $aclField['name'] = $field;
222 if($seed->bean_implements('ACL'))
223 $aclField['acl'] = $this->getFieldLevelACLValue($seed->module_dir, $aclField['name']);
225 $aclField['acl'] = ACL_FIELD_DEFAULT;
227 $aclRow[] = $aclField;
229 $aclRows[] = $aclRow;
232 $results['panels'] = $aclRows;
237 * Parse wireless detailview metadata and add ACL values.
239 * @param String $module_name
240 * @param array $metadata
241 * @return array Metadata with acls added
243 function metdataAclParserWirelessDetail($module_name, $metadata)
245 return self::metdataAclParserWirelessEdit($module_name, $metadata);
249 * Parse wireless listview metadata and add ACL values.
251 * @param String $module_name
252 * @param array $metadata
253 * @return array Metadata with acls added
255 function metdataAclParserWirelessList($module_name, $metadata)
257 global $beanList, $beanFiles;
258 $class_name = $beanList[$module_name];
259 require_once($beanFiles[$class_name]);
260 $seed = new $class_name();
263 foreach ($metadata as $entry)
265 $field_name = $entry['name'];
266 if($seed->bean_implements('ACL'))
267 $entry['acl'] = $this->getFieldLevelACLValue($seed->module_dir, strtolower($field_name));
278 * Processes the filter_fields attribute to use with SugarBean::create_new_list_query()
280 * @param object $value SugarBean
281 * @param array $fields
284 protected function filter_fields_for_query(SugarBean $value, array $fields)
286 $GLOBALS['log']->info('Begin: SoapHelperWebServices->filter_fields_for_query');
287 $filterFields = array();
288 foreach($fields as $field)
290 if (isset($value->field_defs[$field]))
292 $filterFields[$field] = $value->field_defs[$field];
295 $GLOBALS['log']->info('End: SoapHelperWebServices->filter_fields_for_query');
296 return $filterFields;
299 function get_field_list($value,$fields, $translate=true) {
301 $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_field_list');
302 $module_fields = array();
303 $link_fields = array();
304 if(!empty($value->field_defs)){
306 foreach($value->field_defs as $var){
307 if(!empty($fields) && !in_array( $var['name'], $fields))continue;
308 if(isset($var['source']) && ($var['source'] != 'db' && $var['source'] != 'non-db' &&$var['source'] != 'custom_fields') && $var['name'] != 'email1' && $var['name'] != 'email2' && (!isset($var['type'])|| $var['type'] != 'relate'))continue;
309 if ((isset($var['source']) && $var['source'] == 'non_db') && (isset($var['type']) && $var['type'] != 'link')) {
313 $options_dom = array();
314 $options_ret = array();
315 // Apparently the only purpose of this check is to make sure we only return fields
316 // when we've read a record. Otherwise this function is identical to get_module_field_list
317 if( isset($var['required']) && ($var['required'] || $var['required'] == 'true' ) ){
321 if($var['type'] == 'bool')
322 $var['options'] = 'checkbox_dom';
324 if(isset($var['options'])){
325 $options_dom = translate($var['options'], $value->module_dir);
326 if(!is_array($options_dom)) $options_dom = array();
327 foreach($options_dom as $key=>$oneOption)
328 $options_ret[$key] = $this->get_name_value($key,$oneOption);
331 if(!empty($var['dbType']) && $var['type'] == 'bool') {
332 $options_ret['type'] = $this->get_name_value('type', $var['dbType']);
336 $entry['name'] = $var['name'];
337 $entry['type'] = $var['type'];
338 $entry['group'] = isset($var['group']) ? $var['group'] : '';
339 $entry['id_name'] = isset($var['id_name']) ? $var['id_name'] : '';
341 if ($var['type'] == 'link') {
342 $entry['relationship'] = (isset($var['relationship']) ? $var['relationship'] : '');
343 $entry['module'] = (isset($var['module']) ? $var['module'] : '');
344 $entry['bean_name'] = (isset($var['bean_name']) ? $var['bean_name'] : '');
345 $link_fields[$var['name']] = $entry;
348 $entry['label'] = isset($var['vname']) ? translate($var['vname'], $value->module_dir) : $var['name'];
350 $entry['label'] = isset($var['vname']) ? $var['vname'] : $var['name'];
352 $entry['required'] = $required;
353 $entry['options'] = $options_ret;
354 $entry['related_module'] = (isset($var['id_name']) && isset($var['module'])) ? $var['module'] : '';
355 $entry['calculated'] = (isset($var['calculated']) && $var['calculated']) ? true : false;
356 $entry['len'] = isset($var['len']) ? $var['len'] : '';
358 if(isset($var['default'])) {
359 $entry['default_value'] = $var['default'];
361 if( $var['type'] == 'parent' && isset($var['type_name']) )
362 $entry['type_name'] = $var['type_name'];
364 $module_fields[$var['name']] = $entry;
369 if($value->module_dir == 'Meetings' || $value->module_dir == 'Calls')
371 if( isset($module_fields['duration_minutes']) && isset($GLOBALS['app_list_strings']['duration_intervals']))
373 $options_dom = $GLOBALS['app_list_strings']['duration_intervals'];
374 $options_ret = array();
375 foreach($options_dom as $key=>$oneOption)
376 $options_ret[$key] = $this->get_name_value($key,$oneOption);
378 $module_fields['duration_minutes']['options'] = $options_ret;
382 if($value->module_dir == 'Bugs'){
383 require_once('modules/Releases/Release.php');
384 $seedRelease = new Release();
385 $options = $seedRelease->get_releases(TRUE, "Active");
386 $options_ret = array();
387 foreach($options as $name=>$value){
388 $options_ret[] = array('name'=> $name , 'value'=>$value);
390 if(isset($module_fields['fixed_in_release'])){
391 $module_fields['fixed_in_release']['type'] = 'enum';
392 $module_fields['fixed_in_release']['options'] = $options_ret;
394 if(isset($module_fields['found_in_release'])){
395 $module_fields['found_in_release']['type'] = 'enum';
396 $module_fields['found_in_release']['options'] = $options_ret;
398 if(isset($module_fields['release'])){
399 $module_fields['release']['type'] = 'enum';
400 $module_fields['release']['options'] = $options_ret;
402 if(isset($module_fields['release_name'])){
403 $module_fields['release_name']['type'] = 'enum';
404 $module_fields['release_name']['options'] = $options_ret;
408 if(isset($value->assigned_user_name) && isset($module_fields['assigned_user_id'])) {
409 $module_fields['assigned_user_name'] = $module_fields['assigned_user_id'];
410 $module_fields['assigned_user_name']['name'] = 'assigned_user_name';
412 if(isset($value->assigned_name) && isset($module_fields['team_id'])) {
413 $module_fields['team_name'] = $module_fields['team_id'];
414 $module_fields['team_name']['name'] = 'team_name';
416 if(isset($module_fields['modified_user_id'])) {
417 $module_fields['modified_by_name'] = $module_fields['modified_user_id'];
418 $module_fields['modified_by_name']['name'] = 'modified_by_name';
420 if(isset($module_fields['created_by'])) {
421 $module_fields['created_by_name'] = $module_fields['created_by'];
422 $module_fields['created_by_name']['name'] = 'created_by_name';
425 $GLOBALS['log']->info('End: SoapHelperWebServices->get_field_list');
426 return array('module_fields' => $module_fields, 'link_fields' => $link_fields);
430 function new_handle_set_entries($module_name, $name_value_lists, $select_fields = FALSE) {
431 $GLOBALS['log']->info('Begin: SoapHelperWebServices->new_handle_set_entries');
432 global $beanList, $beanFiles, $current_user, $app_list_strings;
434 $ret_values = array();
436 $class_name = $beanList[$module_name];
437 require_once($beanFiles[$class_name]);
440 $total = sizeof($name_value_lists);
441 foreach($name_value_lists as $name_value_list){
442 $seed = new $class_name();
444 $seed->update_vcal = false;
445 foreach($name_value_list as $name => $value){
446 if(is_array($value) && $value['name'] == 'id'){
447 $seed->retrieve($value['value']);
450 else if($name === 'id' ){
451 $seed->retrieve($value);
455 foreach($name_value_list as $name => $value) {
456 //Normalize the input
457 if(!is_array($value)){
462 $field_name = $value['name'];
463 $val = $value['value'];
466 if($seed->field_name_map[$field_name]['type'] == 'enum'){
467 $vardef = $seed->field_name_map[$field_name];
468 if(isset($app_list_strings[$vardef['options']]) && !isset($app_list_strings[$vardef['options']][$val]) ) {
469 if ( in_array($val,$app_list_strings[$vardef['options']]) ){
470 $val = array_search($val,$app_list_strings[$vardef['options']]);
474 if($module_name == 'Users' && !empty($seed->id) && ($seed->id != $current_user->id) && $field_name == 'user_hash'){
478 $seed->$field_name = $val;
481 if($count == $total){
482 $seed->update_vcal = false;
486 //Add the account to a contact
487 if($module_name == 'Contacts'){
488 $GLOBALS['log']->debug('Creating Contact Account');
489 $this->add_create_account($seed);
490 $duplicate_id = $this->check_for_duplicate_contacts($seed);
491 if($duplicate_id == null){
492 if($seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
494 if($seed->deleted == 1){
495 $seed->mark_deleted($seed->id);
501 //since we found a duplicate we should set the sync flag
502 if( $seed->ACLAccess('Save')){
503 $seed = new $class_name();
504 $seed->id = $duplicate_id;
505 $seed->contacts_users_id = $current_user->id;
507 $ids[] = $duplicate_id;//we have a conflict
511 else if($module_name == 'Meetings' || $module_name == 'Calls'){
512 //we are going to check if we have a meeting in the system
513 //with the same outlook_id. If we do find one then we will grab that
515 if( $seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
516 if(empty($seed->id) && !isset($seed->id)){
517 if(!empty($seed->outlook_id) && isset($seed->outlook_id)){
518 //at this point we have an object that does not have
519 //the id set, but does have the outlook_id set
520 //so we need to query the db to find if we already
521 //have an object with this outlook_id, if we do
522 //then we can set the id, otherwise this is a new object
524 $query = $seed->table_name.".outlook_id = '".$seed->outlook_id."'";
525 $response = $seed->get_list($order_by, $query, 0,-1,-1,0);
526 $list = $response['list'];
527 if(count($list) > 0){
528 foreach($list as $value)
530 $seed->id = $value->id;
536 if (empty($seed->reminder_time)) {
537 $seed->reminder_time = -1;
539 if($seed->reminder_time == -1){
540 $defaultRemindrTime = $current_user->getPreference('reminder_time');
541 if ($defaultRemindrTime != -1){
542 $seed->reminder_checked = '1';
543 $seed->reminder_time = $defaultRemindrTime;
552 if( $seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
558 // if somebody is calling set_entries_detail() and wants fields returned...
559 if ($select_fields !== FALSE) {
560 $ret_values[$count] = array();
562 foreach ($select_fields as $select_field) {
563 if (isset($seed->$select_field)) {
564 $ret_values[$count][$select_field] = $this->get_name_value($select_field, $seed->$select_field);
570 // handle returns for set_entries_detail() and set_entries()
571 if ($select_fields !== FALSE) {
572 $GLOBALS['log']->info('End: SoapHelperWebServices->new_handle_set_entries');
574 'name_value_lists' => $ret_values,
578 $GLOBALS['log']->info('End: SoapHelperWebServices->new_handle_set_entries');
586 function checkSessionAndModuleAccess($session, $login_error_key, $module_name, $access_level, $module_access_level_error_key, $errorObject)
588 if(isset($_REQUEST['oauth_token'])) {
589 $session = $this->checkOAuthAccess($errorObject);
591 if(!$session) return false;
592 return parent::checkSessionAndModuleAccess($session, $login_error_key, $module_name, $access_level, $module_access_level_error_key, $errorObject);
595 public function checkOAuthAccess($errorObject)
597 require_once "include/SugarOAuthServer.php";
599 $oauth = new SugarOAuthServer();
600 $token = $oauth->authorizedToken();
601 if(empty($token) || empty($token->assigned_user_id)) {
604 } catch(OAuthException $e) {
605 $GLOBALS['log']->debug("OAUTH Exception: $e");
606 $errorObject->set_error('invalid_login');
607 $this->setFaultObject($errorObject);
612 $user->retrieve($token->assigned_user_id);
613 if(empty($user->id)) {
616 global $current_user;
617 $current_user = $user;
618 ini_set("session.use_cookies", 0); // disable cookies to prevent session ID from going out
620 session_regenerate_id();
621 $_SESSION['oauth'] = $oauth->authorization();
622 $_SESSION['avail_modules'] = $this->get_user_module_list($user);
625 $_SESSION['is_valid_session']= true;
626 $_SESSION['ip_address'] = query_client_ip();
627 $_SESSION['user_id'] = $current_user->id;
628 $_SESSION['type'] = 'user';
629 $_SESSION['authenticated_user_id'] = $current_user->id;