2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
39 global $disable_date_format;
40 $disable_date_format = true;
42 class SoapHelperWebServices {
44 function get_field_list($value, $fields, $translate=true)
46 $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_field_list('.print_r($value, true).', '.print_r($fields, true).", $translate");
47 $module_fields = array();
48 $link_fields = array();
49 if(!empty($value->field_defs)){
51 foreach($value->field_defs as $var){
52 if(!empty($fields) && !in_array( $var['name'], $fields))continue;
53 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;
54 if ($var['source'] == 'non_db' && (isset($var['type']) && $var['type'] != 'link')) {
58 $options_dom = array();
59 $options_ret = array();
60 // Apparently the only purpose of this check is to make sure we only return fields
61 // when we've read a record. Otherwise this function is identical to get_module_field_list
63 if( isset($var['required']) && $var['required'] && $var['required'] !== 'false' ){
66 if(isset($var['options'])){
67 $options_dom = translate($var['options'], $value->module_dir);
68 if(!is_array($options_dom)) $options_dom = array();
69 foreach($options_dom as $key=>$oneOption)
70 $options_ret[$key] = $this->get_name_value($key,$oneOption);
73 if(!empty($var['dbType']) && $var['type'] == 'bool') {
74 $options_ret['type'] = $this->get_name_value('type', $var['dbType']);
78 $entry['name'] = $var['name'];
79 $entry['type'] = $var['type'];
80 if ($var['type'] == 'link') {
81 $entry['relationship'] = (isset($var['relationship']) ? $var['relationship'] : '');
82 $entry['module'] = (isset($var['module']) ? $var['module'] : '');
83 $entry['bean_name'] = (isset($var['bean_name']) ? $var['bean_name'] : '');
84 $link_fields[$var['name']] = $entry;
87 $entry['label'] = isset($var['vname']) ? translate($var['vname'], $value->module_dir) : $var['name'];
89 $entry['label'] = isset($var['vname']) ? $var['vname'] : $var['name'];
91 $entry['required'] = $required;
92 $entry['options'] = $options_ret;
93 if(isset($var['default'])) {
94 $entry['default_value'] = $var['default'];
96 $module_fields[$var['name']] = $entry;
101 if($value->module_dir == 'Bugs'){
102 require_once('modules/Releases/Release.php');
103 $seedRelease = new Release();
104 $options = $seedRelease->get_releases(TRUE, "Active");
105 $options_ret = array();
106 foreach($options as $name=>$value){
107 $options_ret[] = array('name'=> $name , 'value'=>$value);
109 if(isset($module_fields['fixed_in_release'])){
110 $module_fields['fixed_in_release']['type'] = 'enum';
111 $module_fields['fixed_in_release']['options'] = $options_ret;
113 if(isset($module_fields['release'])){
114 $module_fields['release']['type'] = 'enum';
115 $module_fields['release']['options'] = $options_ret;
117 if(isset($module_fields['release_name'])){
118 $module_fields['release_name']['type'] = 'enum';
119 $module_fields['release_name']['options'] = $options_ret;
123 if(isset($value->assigned_user_name) && isset($module_fields['assigned_user_id'])) {
124 $module_fields['assigned_user_name'] = $module_fields['assigned_user_id'];
125 $module_fields['assigned_user_name']['name'] = 'assigned_user_name';
127 if(isset($module_fields['modified_user_id'])) {
128 $module_fields['modified_by_name'] = $module_fields['modified_user_id'];
129 $module_fields['modified_by_name']['name'] = 'modified_by_name';
131 if(isset($module_fields['created_by'])) {
132 $module_fields['created_by_name'] = $module_fields['created_by'];
133 $module_fields['created_by_name']['name'] = 'created_by_name';
136 $return = array('module_fields' => $module_fields, 'link_fields' => $link_fields);
137 $GLOBALS['log']->info('End: SoapHelperWebServices->get_field_list ->> '.print_r($return, true));
141 function setFaultObject($errorObject) {
142 if ($this->isLogLevelDebug()) {
143 $GLOBALS['log']->debug('SoapHelperWebServices->setFaultObject - ' . var_export($errorObject, true));
145 global $service_object;
146 $service_object->error($errorObject);
150 * Validate the user session based on user name and password hash.
152 * @param string $user_name -- The user name to create a session for
153 * @param string $password -- The MD5 sum of the user's password
154 * @return true -- If the session is created
155 * @return false -- If the session is not created
157 function validate_user($user_name, $password){
158 $GLOBALS['log']->info('Begin: SoapHelperWebServices->validate_user');
159 global $server, $current_user, $sugar_config, $system_config;
161 $user->user_name = $user_name;
162 $system_config = new Administration();
163 $system_config->retrieveSettings('system');
164 $authController = new AuthenticationController((!empty($sugar_config['authenticationClass'])? $sugar_config['authenticationClass'] : 'SugarAuthenticate'));
165 // Check to see if the user name and password are consistent.
166 if($user->authenticate_user($password)){
167 // we also need to set the current_user.
168 $user->retrieve($user->id);
169 $current_user = $user;
171 $GLOBALS['log']->info('End: SoapHelperWebServices->validate_user - validation passed');
173 }else if(function_exists('mcrypt_cbc')){
174 $password = $this->decrypt_string($password);
175 if($authController->login($user_name, $password) && isset($_SESSION['authenticated_user_id'])){
176 $user->retrieve($_SESSION['authenticated_user_id']);
177 $current_user = $user;
178 $GLOBALS['log']->info('End: SoapHelperWebServices->validate_user - validation passed');
182 $GLOBALS['log']->fatal("SECURITY: failed attempted login for $user_name using SOAP api");
183 $server->setError("Invalid username and/or password");
190 * Validate the provided session information is correct and current. Load the session.
192 * @param String $session_id -- The session ID that was returned by a call to login.
193 * @return true -- If the session is valid and loaded.
194 * @return false -- if the session is not valid.
196 function validate_authenticated($session_id){
197 $GLOBALS['log']->info('Begin: SoapHelperWebServices->validate_authenticated');
198 if(!empty($session_id)){
199 session_id($session_id);
200 if(empty($_SESSION)) {
203 if(!empty($_SESSION['is_valid_session']) && $this->is_valid_ip_address('ip_address') && $_SESSION['type'] == 'user'){
205 global $current_user;
206 require_once('modules/Users/User.php');
207 $current_user = new User();
208 $current_user->retrieve($_SESSION['user_id']);
209 $this->login_success();
210 $GLOBALS['log']->info('Begin: SoapHelperWebServices->validate_authenticated - passed');
211 $GLOBALS['log']->info('End: SoapHelperWebServices->validate_authenticated');
215 $GLOBALS['log']->debug("calling destroy");
218 LogicHook::initialize();
219 $GLOBALS['logic_hook']->call_custom_logic('Users', 'login_failed');
220 $GLOBALS['log']->info('End: SoapHelperWebServices->validate_authenticated - validation failed');
225 * Use the same logic as in SugarAuthenticate to validate the ip address
227 * @param string $session_var
228 * @return bool - true if the ip address is valid, false otherwise.
230 function is_valid_ip_address($session_var){
231 global $sugar_config;
232 // grab client ip address
233 $clientIP = query_client_ip();
235 // check to see if config entry is present, if not, verify client ip
236 if (!isset ($sugar_config['verify_client_ip']) || $sugar_config['verify_client_ip'] == true) {
237 // check to see if we've got a current ip address in $_SESSION
238 // and check to see if the session has been hijacked by a foreign ip
239 if (isset ($_SESSION[$session_var])) {
240 $session_parts = explode(".", $_SESSION[$session_var]);
241 $client_parts = explode(".", $clientIP);
242 if(count($session_parts) < 4) {
245 // match class C IP addresses
246 for ($i = 0; $i < 3; $i ++) {
247 if ($session_parts[$i] == $client_parts[$i]) {
256 // we have a different IP address
257 if ($_SESSION[$session_var] != $clientIP && empty ($classCheck)) {
258 $GLOBALS['log']->fatal("IP Address mismatch: SESSION IP: {$_SESSION[$session_var]} CLIENT IP: {$clientIP}");
268 function checkSessionAndModuleAccess($session, $login_error_key, $module_name, $access_level, $module_access_level_error_key, $errorObject) {
269 $GLOBALS['log']->info('Begin: SoapHelperWebServices->checkSessionAndModuleAccess - ' . $module_name);
270 if(!$this->validate_authenticated($session)){
271 $GLOBALS['log']->error('SoapHelperWebServices->checkSessionAndModuleAccess - validate_authenticated failed - ' . $module_name);
272 $errorObject->set_error('invalid_session');
273 $this->setFaultObject($errorObject);
274 $GLOBALS['log']->info('End: SoapHelperWebServices->checkSessionAndModuleAccess -' . $module_name);
278 global $beanList, $beanFiles;
279 if (!empty($module_name)) {
280 if(empty($beanList[$module_name])){
281 $GLOBALS['log']->error('SoapHelperWebServices->checkSessionAndModuleAccess - module does not exists - ' . $module_name);
282 $errorObject->set_error('no_module');
283 $this->setFaultObject($errorObject);
284 $GLOBALS['log']->info('End: SoapHelperWebServices->checkSessionAndModuleAccess -' . $module_name);
287 global $current_user;
288 if(!$this->check_modules_access($current_user, $module_name, $access_level)){
289 $GLOBALS['log']->error('SoapHelperWebServices->checkSessionAndModuleAccess - no module access - ' . $module_name);
290 $errorObject->set_error('no_access');
291 $this->setFaultObject($errorObject);
292 $GLOBALS['log']->info('End: SoapHelperWebServices->checkSessionAndModuleAccess - ' . $module_name);
296 $GLOBALS['log']->info('End: SoapHelperWebServices->checkSessionAndModuleAccess - ' . $module_name);
300 function checkACLAccess($bean, $viewType, $errorObject, $error_key) {
301 $GLOBALS['log']->info('Begin: SoapHelperWebServices->checkACLAccess');
302 if(!$bean->ACLAccess($viewType)) {
303 $GLOBALS['log']->error('SoapHelperWebServices->checkACLAccess - no ACLAccess');
304 $errorObject->set_error($error_key);
305 $this->setFaultObject($errorObject);
306 $GLOBALS['log']->info('End: SoapHelperWebServices->checkACLAccess');
309 $GLOBALS['log']->info('End: SoapHelperWebServices->checkACLAccess');
313 function get_name_value($field,$value){
314 return array('name'=>$field, 'value'=>$value);
317 function get_user_module_list($user){
318 $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_user_module_list');
319 global $app_list_strings, $current_language;
320 $app_list_strings = return_app_list_strings_language($current_language);
321 $modules = query_module_access_list($user);
322 ACLController :: filterModuleList($modules, false);
323 global $modInvisList;
325 foreach($modInvisList as $invis){
326 $modules[$invis] = 'read_only';
329 $actions = ACLAction::getUserActions($user->id,true);
330 foreach($actions as $key=>$value){
331 if(isset($value['module']) && $value['module']['access']['aclaccess'] < ACL_ALLOW_ENABLED){
332 if ($value['module']['access']['aclaccess'] == ACL_ALLOW_DISABLED) {
333 unset($modules[$key]);
335 $modules[$key] = 'read_only';
341 $GLOBALS['log']->info('End: SoapHelperWebServices->get_user_module_list');
346 function check_modules_access($user, $module_name, $action='write'){
347 $GLOBALS['log']->info('Begin: SoapHelperWebServices->check_modules_access');
348 if(!isset($_SESSION['avail_modules'])){
349 $_SESSION['avail_modules'] = $this->get_user_module_list($user);
351 if(isset($_SESSION['avail_modules'][$module_name])){
352 if($action == 'write' && $_SESSION['avail_modules'][$module_name] == 'read_only'){
353 if(is_admin($user)) {
354 $GLOBALS['log']->info('End: SoapHelperWebServices->check_modules_access - SUCCESS: Admin can even write to read_only module');
357 $GLOBALS['log']->info('End: SoapHelperWebServices->check_modules_access - FAILED: write action on read_only module only available to admins');
359 }elseif($action == 'write' && strcmp(strtolower($module_name), 'users') == 0 && !$user->isAdminForModule($module_name)){
360 //rrs bug: 46000 - If the client is trying to write to the Users module and is not an admin then we need to stop them
363 $GLOBALS['log']->info('End: SoapHelperWebServices->check_modules_access - SUCCESS');
366 $GLOBALS['log']->info('End: SoapHelperWebServices->check_modules_access - FAILED: Module info not available in $_SESSION');
372 function get_name_value_list($value){
373 $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_name_value_list');
374 global $app_list_strings;
376 if(!empty($value->field_defs)){
377 if(isset($value->assigned_user_name)) {
378 $list['assigned_user_name'] = $this->get_name_value('assigned_user_name', $value->assigned_user_name);
380 if(isset($value->modified_by_name)) {
381 $list['modified_by_name'] = $this->get_name_value('modified_by_name', $value->modified_by_name);
383 if(isset($value->created_by_name)) {
384 $list['created_by_name'] = $this->get_name_value('created_by_name', $value->created_by_name);
386 foreach($value->field_defs as $var){
387 if(isset($var['source']) && ($var['source'] != 'db' && $var['source'] != 'custom_fields') && $var['name'] != 'email1' && $var['name'] != 'email2' && (!isset($var['type'])|| $var['type'] != 'relate'))continue;
389 if(isset($value->$var['name'])){
390 $val = $value->$var['name'];
391 $type = $var['type'];
393 if(strcmp($type, 'date') == 0){
394 $val = substr($val, 0, 10);
395 }elseif(strcmp($type, 'enum') == 0 && !empty($var['options'])){
396 //$val = $app_list_strings[$var['options']][$val];
399 $list[$var['name']] = $this->get_name_value($var['name'], $val);
403 $GLOBALS['log']->info('End: SoapHelperWebServices->get_name_value_list');
408 function filter_fields($value, $fields) {
409 $GLOBALS['log']->info('Begin: SoapHelperWebServices->filter_fields');
410 global $invalid_contact_fields;
411 $filterFields = array();
412 foreach($fields as $field){
413 if (is_array($invalid_contact_fields)) {
414 if (in_array($field, $invalid_contact_fields)) {
418 if (isset($value->field_defs[$field])) {
419 $var = $value->field_defs[$field];
420 if(isset($var['source']) && ($var['source'] != 'db' && $var['source'] != 'custom_fields') && $var['name'] != 'email1' && $var['name'] != 'email2' && (!isset($var['type'])|| $var['type'] != 'relate')) {
422 if($value->module_dir == 'Emails' && (($var['name'] == 'description') || ($var['name'] == 'description_html') || ($var['name'] == 'from_addr_name') || ($var['name'] == 'reply_to_addr') || ($var['name'] == 'to_addrs_names') || ($var['name'] == 'cc_addrs_names') || ($var['name'] == 'bcc_addrs_names') || ($var['name'] == 'raw_source'))) {
429 $filterFields[] = $field;
431 $GLOBALS['log']->info('End: SoapHelperWebServices->filter_fields');
432 return $filterFields;
435 function get_name_value_list_for_fields($value, $fields) {
436 $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_name_value_list_for_fields');
437 global $app_list_strings;
438 global $invalid_contact_fields;
441 if(!empty($value->field_defs)){
442 if(empty($fields))$fields = array_keys($value->field_defs);
443 if(isset($value->assigned_user_name) && in_array('assigned_user_name', $fields)) {
444 $list['assigned_user_name'] = $this->get_name_value('assigned_user_name', $value->assigned_user_name);
446 if(isset($value->modified_by_name) && in_array('modified_by_name', $fields)) {
447 $list['modified_by_name'] = $this->get_name_value('modified_by_name', $value->modified_by_name);
449 if(isset($value->created_by_name) && in_array('created_by_name', $fields)) {
450 $list['created_by_name'] = $this->get_name_value('created_by_name', $value->created_by_name);
453 $filterFields = $this->filter_fields($value, $fields);
454 foreach($filterFields as $field){
455 $var = $value->field_defs[$field];
456 if(isset($value->$var['name'])){
457 $val = $value->$var['name'];
458 $type = $var['type'];
460 if(strcmp($type, 'date') == 0){
461 $val = substr($val, 0, 10);
462 }elseif(strcmp($type, 'enum') == 0 && !empty($var['options'])){
463 //$val = $app_list_strings[$var['options']][$val];
466 $list[$var['name']] = $this->get_name_value($var['name'], $val);
470 $GLOBALS['log']->info('End: SoapHelperWebServices->get_name_value_list_for_fields');
471 if ($this->isLogLevelDebug()) {
472 $GLOBALS['log']->debug('SoapHelperWebServices->get_name_value_list_for_fields - return data = ' . var_export($list, true));
478 function array_get_name_value_list($array){
479 $GLOBALS['log']->info('Begin: SoapHelperWebServices->array_get_name_value_list');
481 foreach($array as $name=>$value){
482 $list[$name] = $this->get_name_value($name, $value);
484 $GLOBALS['log']->info('End: SoapHelperWebServices->array_get_name_value_list');
489 function array_get_name_value_lists($array){
490 $GLOBALS['log']->info('Begin: SoapHelperWebServices->array_get_name_value_lists');
492 foreach($array as $name=>$value){
494 if(is_array($value)){
495 $tmp_value = array();
496 foreach($value as $k=>$v){
497 $tmp_value[$k] = $this->get_name_value($k, $v);
500 $list[$name] = $this->get_name_value($name, $tmp_value);
502 $GLOBALS['log']->info('End: SoapHelperWebServices->array_get_name_value_lists');
506 function name_value_lists_get_array($list){
507 $GLOBALS['log']->info('Begin: SoapHelperWebServices->name_value_lists_get_array');
509 foreach($list as $key=>$value){
510 if(isset($value['value']) && isset($value['name'])){
511 if(is_array($value['value'])){
512 $array[$value['name']]=array();
513 foreach($value['value'] as $v){
514 $array[$value['name']][$v['name']]=$v['value'];
517 $array[$value['name']]=$value['value'];
521 $GLOBALS['log']->info('End: SoapHelperWebServices->name_value_lists_get_array');
525 function array_get_return_value($array, $module){
527 $GLOBALS['log']->info('Begin/End: SoapHelperWebServices->array_get_return_value');
528 return Array('id'=>$array['id'],
529 'module_name'=> $module,
530 'name_value_list'=>$this->array_get_name_value_list($array)
534 function get_return_value_for_fields($value, $module, $fields) {
535 $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_return_value_for_fields');
536 global $module_name, $current_user;
537 $module_name = $module;
538 if($module == 'Users' && $value->id != $current_user->id){
539 $value->user_hash = '';
541 $GLOBALS['log']->info('End: SoapHelperWebServices->get_return_value_for_fields');
542 return Array('id'=>$value->id,
543 'module_name'=> $module,
544 'name_value_list'=>$this->get_name_value_list_for_fields($value, $fields)
548 function getRelationshipResults($bean, $link_field_name, $link_module_fields, $optional_where = '') {
549 $GLOBALS['log']->info('Begin: SoapHelperWebServices->getRelationshipResults');
550 require_once('include/TimeDate.php');
551 global $current_user, $disable_date_format, $timedate;
553 $bean->load_relationship($link_field_name);
554 if (isset($bean->$link_field_name)) {
555 //First get all the related beans
556 $related_beans = $bean->$link_field_name->getBeans();
557 $filterFields = $this->filter_fields($submodule, $link_module_fields);
558 //Create a list of field/value rows based on $link_module_fields
560 foreach($related_beans as $id => $bean)
563 foreach ($filterFields as $field) {
564 if (isset($bean->$field))
566 if (isset($bean->field_defs[$field]['type']) && $bean->field_defs[$field]['type'] == 'date') {
567 $row[$field] = $timedate->to_display_date_time($bean->$field);
569 $row[$field] = $bean->$field;
576 //Users can't see other user's hashes
577 if(is_a($bean, 'User') && $current_user->id != $bean->id && isset($row['user_hash'])) {
578 $row['user_hash'] = "";
582 $GLOBALS['log']->info('End: SoapHelperWebServices->getRelationshipResults');
583 return array('rows' => $list, 'fields_set_on_rows' => $filterFields);
585 $GLOBALS['log']->info('End: SoapHelperWebServices->getRelationshipResults - ' . $link_field_name . ' relationship does not exists');
591 function get_return_value_for_link_fields($bean, $module, $link_name_to_value_fields_array) {
592 $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_return_value_for_link_fields');
593 global $module_name, $current_user;
594 $module_name = $module;
595 if($module == 'Users' && $bean->id != $current_user->id){
596 $bean->user_hash = '';
599 if (empty($link_name_to_value_fields_array) || !is_array($link_name_to_value_fields_array)) {
600 $GLOBALS['log']->debug('End: SoapHelperWebServices->get_return_value_for_link_fields - Invalid link information passed ');
604 if ($this->isLogLevelDebug()) {
605 $GLOBALS['log']->debug('SoapHelperWebServices->get_return_value_for_link_fields - link info = ' . var_export($link_name_to_value_fields_array, true));
607 $link_output = array();
608 foreach($link_name_to_value_fields_array as $link_name_value_fields) {
609 if (!is_array($link_name_value_fields) || !isset($link_name_value_fields['name']) || !isset($link_name_value_fields['value'])) {
612 $link_field_name = $link_name_value_fields['name'];
613 $link_module_fields = $link_name_value_fields['value'];
614 if (is_array($link_module_fields) && !empty($link_module_fields)) {
615 $result = $this->getRelationshipResults($bean, $link_field_name, $link_module_fields);
617 $link_output[] = array('name' => $link_field_name, 'records' => array());
620 $list = $result['rows'];
621 $filterFields = $result['fields_set_on_rows'];
624 foreach($list as $row) {
625 $nameValueArray = array();
626 foreach ($filterFields as $field) {
627 $nameValue = array();
628 if (isset($row[$field])) {
629 $nameValueArray[$field] = $this->get_name_value($field, $row[$field]);
632 $rowArray[] = $nameValueArray;
634 $link_output[] = array('name' => $link_field_name, 'records' => $rowArray);
638 $GLOBALS['log']->debug('End: SoapHelperWebServices->get_return_value_for_link_fields');
639 if ($this->isLogLevelDebug()) {
640 $GLOBALS['log']->debug('SoapHelperWebServices->get_return_value_for_link_fields - output = ' . var_export($link_output, true));
647 * @param String $module_name -- The name of the module that the primary record is from. This name should be the name the module was developed under (changing a tab name is studio does not affect the name that should be passed into this method).
648 * @param String $module_id -- The ID of the bean in the specified module
649 * @param String $link_field_name - The relationship name for which to create realtionships.
650 * @param Array $related_ids -- The array of ids for which we want to create relationships
651 * @param Array $name_value_list -- The array of name value pair of additional attributes to be set when adding this relationship
652 * @param int delete -- If 0 then add relationship else delete this relationship data
653 * @return true on success, false on failure
655 function new_handle_set_relationship($module_name, $module_id, $link_field_name, $related_ids, $name_value_list, $delete) {
656 $GLOBALS['log']->info('Begin: SoapHelperWebServices->new_handle_set_relationship');
657 global $beanList, $beanFiles;
659 if(empty($beanList[$module_name])) {
660 $GLOBALS['log']->debug('SoapHelperWebServices->new_handle_set_relationship - module ' . $module_name . ' does not exists' );
661 $GLOBALS['log']->info('End: SoapHelperWebServices->new_handle_set_relationship');
664 $class_name = $beanList[$module_name];
665 require_once($beanFiles[$class_name]);
666 $mod = new $class_name();
667 $mod->retrieve($module_id);
668 if(!$mod->ACLAccess('DetailView')){
669 $GLOBALS['log']->info('End: SoapHelperWebServices->new_handle_set_relationship');
673 if ($mod->load_relationship($link_field_name)) {
675 $name_value_pair = array();
676 if (!empty($name_value_list)) {
677 $relFields = $mod->$link_field_name->getRelatedFields();
678 if(!empty($relFields)){
679 $relFieldsKeys = array_keys($relFields);
680 foreach($name_value_list as $key => $value) {
681 if (in_array($value['name'], $relFieldsKeys)) {
682 $name_value_pair[$value['name']] = $value['value'];
687 $mod->$link_field_name->add($related_ids, $name_value_pair);
689 foreach($related_ids as $id) {
690 $mod->$link_field_name->delete($module_id, $id);
693 $GLOBALS['log']->info('End: SoapHelperWebServices->new_handle_set_relationship');
696 $GLOBALS['log']->info('End: SoapHelperWebServices->new_handle_set_relationship');
701 function new_handle_set_entries($module_name, $name_value_lists, $select_fields = FALSE) {
702 $GLOBALS['log']->info('Begin: SoapHelperWebServices->new_handle_set_entries');
703 global $beanList, $beanFiles, $current_user, $app_list_strings;
705 $ret_values = array();
707 $class_name = $beanList[$module_name];
708 require_once($beanFiles[$class_name]);
711 $total = sizeof($name_value_lists);
712 foreach($name_value_lists as $name_value_list){
713 $seed = new $class_name();
715 $seed->update_vcal = false;
716 foreach($name_value_list as $value){
717 if($value['name'] == 'id'){
718 $seed->retrieve($value['value']);
723 foreach($name_value_list as $value) {
724 $val = $value['value'];
725 if($seed->field_name_map[$value['name']]['type'] == 'enum'){
726 $vardef = $seed->field_name_map[$value['name']];
727 if(isset($app_list_strings[$vardef['options']]) && !isset($app_list_strings[$vardef['options']][$value]) ) {
728 if ( in_array($val,$app_list_strings[$vardef['options']]) ){
729 $val = array_search($val,$app_list_strings[$vardef['options']]);
733 if($module_name == 'Users' && !empty($seed->id) && ($seed->id != $current_user->id) && $value['name'] == 'user_hash'){
737 $seed->$value['name'] = $val;
740 if($count == $total){
741 $seed->update_vcal = false;
745 //Add the account to a contact
746 if($module_name == 'Contacts'){
747 $GLOBALS['log']->debug('Creating Contact Account');
748 $this->add_create_account($seed);
749 $duplicate_id = $this->check_for_duplicate_contacts($seed);
750 if($duplicate_id == null){
751 if($seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
753 if($seed->deleted == 1){
754 $seed->mark_deleted($seed->id);
760 //since we found a duplicate we should set the sync flag
761 if( $seed->ACLAccess('Save')){
762 $seed = new $class_name();
763 $seed->id = $duplicate_id;
764 $seed->contacts_users_id = $current_user->id;
766 $ids[] = $duplicate_id;//we have a conflict
770 else if($module_name == 'Meetings' || $module_name == 'Calls'){
771 //we are going to check if we have a meeting in the system
772 //with the same outlook_id. If we do find one then we will grab that
774 if( $seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
775 if(empty($seed->id) && !isset($seed->id)){
776 if(!empty($seed->outlook_id) && isset($seed->outlook_id)){
777 //at this point we have an object that does not have
778 //the id set, but does have the outlook_id set
779 //so we need to query the db to find if we already
780 //have an object with this outlook_id, if we do
781 //then we can set the id, otherwise this is a new object
783 $query = $seed->table_name.".outlook_id = '".$seed->outlook_id."'";
784 $response = $seed->get_list($order_by, $query, 0,-1,-1,0);
785 $list = $response['list'];
786 if(count($list) > 0){
787 foreach($list as $value)
789 $seed->id = $value->id;
795 if (empty($seed->reminder_time)) {
796 $seed->reminder_time = -1;
798 if($seed->reminder_time == -1){
799 $defaultRemindrTime = $current_user->getPreference('reminder_time');
800 if ($defaultRemindrTime != -1){
801 $seed->reminder_checked = '1';
802 $seed->reminder_time = $defaultRemindrTime;
811 if( $seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
817 // if somebody is calling set_entries_detail() and wants fields returned...
818 if ($select_fields !== FALSE) {
819 $ret_values[$count] = array();
821 foreach ($select_fields as $select_field) {
822 if (isset($seed->$select_field)) {
823 $ret_values[$count][$select_field] = $this->get_name_value($select_field, $seed->$select_field);
829 // handle returns for set_entries_detail() and set_entries()
830 if ($select_fields !== FALSE) {
831 $GLOBALS['log']->info('End: SoapHelperWebServices->new_handle_set_entries');
833 'name_value_lists' => $ret_values,
837 $GLOBALS['log']->info('End: SoapHelperWebServices->new_handle_set_entries');
844 function get_return_value($value, $module){
845 $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_return_value');
846 global $module_name, $current_user;
847 $module_name = $module;
848 if($module == 'Users' && $value->id != $current_user->id){
849 $value->user_hash = '';
851 $GLOBALS['log']->info('End: SoapHelperWebServices->new_handle_set_entries');
852 return Array('id'=>$value->id,
853 'module_name'=> $module,
854 'name_value_list'=>$this->get_name_value_list($value)
859 function get_return_module_fields($value, $module,$fields, $translate=true){
860 $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_return_module_fields');
862 $module_name = $module;
863 $result = $this->get_field_list($value,$fields, $translate);
864 $GLOBALS['log']->info('End: SoapHelperWebServices->get_return_module_fields');
865 return Array('module_name'=>$module,
866 'module_fields'=> $result['module_fields'],
867 'link_fields'=> $result['link_fields'],
871 function login_success($name_value_list = array()){
872 $GLOBALS['log']->info('Begin: SoapHelperWebServices->login_success');
873 global $current_language, $sugar_config, $app_strings, $app_list_strings;
874 $current_language = $sugar_config['default_language'];
875 if (is_array($name_value_list) && !empty($name_value_list)) {
876 foreach($name_value_list as $key => $value) {
877 if (isset($value['name']) && ($value['name'] == 'language')) {
878 $language = $value['value'];
879 $supportedLanguages = $sugar_config['languages'];
880 if (array_key_exists($language, $supportedLanguages)) {
881 $current_language = $language;
884 if (isset($value['name']) && ($value['name'] == 'notifyonsave')) {
885 if ($value['value']) {
886 $_SESSION['notifyonsave'] = true;
891 if (isset($_SESSION['user_language'])) {
892 $current_language = $_SESSION['user_language'];
895 $GLOBALS['log']->info("Users language is = " . $current_language);
896 $app_strings = return_application_language($current_language);
897 $app_list_strings = return_app_list_strings_language($current_language);
898 $GLOBALS['log']->info('End: SoapHelperWebServices->login_success');
902 function checkSaveOnNotify() {
903 $notifyonsave = false;
904 if (isset($_SESSION['notifyonsave']) && $_SESSION['notifyonsave'] == true) {
905 $notifyonsave = true;
907 return $notifyonsave;
910 * Given an account_name, either create the account or assign to a contact.
912 function add_create_account($seed) {
913 $GLOBALS['log']->info('Begin: SoapHelperWebServices->add_create_account');
914 global $current_user;
915 $account_name = $seed->account_name;
916 $account_id = $seed->account_id;
917 $assigned_user_id = $current_user->id;
919 // check if it already exists
920 $focus = new Account();
921 if($focus->ACLAccess('Save')) {
922 $class = get_class($seed);
923 $temp = new $class();
924 $temp->retrieve($seed->id);
925 if ( empty($account_name) && empty($account_id)) {
928 if (!isset($seed->accounts)){
929 $seed->load_relationship('accounts');
932 if($seed->account_name == '' && isset($temp->account_id)){
933 $seed->accounts->delete($seed->id, $temp->account_id);
934 $GLOBALS['log']->info('End: SoapHelperWebServices->add_create_account');
939 if(!empty($account_id)) // bug # 44280
941 $query = "select id, deleted from {$focus->table_name} WHERE id='".$seed->db->quote($account_id)."'";
945 $query = "select id, deleted from {$focus->table_name} WHERE name='".$seed->db->quote($account_name)."'";
947 $result = $seed->db->query($query, true);
949 $row = $seed->db->fetchByAssoc($result, false);
951 // we found a row with that id
952 if (isset($row['id']) && $row['id'] != -1)
954 // if it exists but was deleted, just remove it entirely
955 if ( isset($row['deleted']) && $row['deleted'] == 1)
957 $query2 = "delete from {$focus->table_name} WHERE id='". $seed->db->quote($row['id'])."'";
958 $result2 = $seed->db->query($query2, true);
960 // else just use this id to link the contact to the account
963 $focus->id = $row['id'];
967 // if we didnt find the account, so create it
968 if (! isset($focus->id) || $focus->id == '')
970 $focus->name = $account_name;
972 if ( isset($assigned_user_id))
974 $focus->assigned_user_id = $assigned_user_id;
975 $focus->modified_user_id = $assigned_user_id;
980 if($seed->accounts != null && $temp->account_id != null && $temp->account_id != $focus->id){
981 $seed->accounts->delete($seed->id, $temp->account_id);
984 if(isset($focus->id) && $focus->id != ''){
985 $seed->account_id = $focus->id;
987 $GLOBALS['log']->info('End: SoapHelperWebServices->add_create_account');
990 $GLOBALS['log']->info('End: SoapHelperWebServices->add_create_account - Insufficient ACLAccess');
994 function check_for_duplicate_contacts($seed){
995 $GLOBALS['log']->info('Begin: SoapHelperWebServices->check_for_duplicate_contacts');
996 require_once('modules/Contacts/Contact.php');
998 if(isset($seed->id)){
999 $GLOBALS['log']->info('End: SoapHelperWebServices->check_for_duplicate_contacts - no duplicte found');
1005 $trimmed_email = trim($seed->email1);
1006 $trimmed_email2 = trim($seed->email2);
1007 $trimmed_last = trim($seed->last_name);
1008 $trimmed_first = trim($seed->first_name);
1009 if(!empty($trimmed_email) || !empty($trimmed_email2)){
1011 //obtain a list of contacts which contain the same email address
1012 $contacts = $seed->emailAddress->getBeansByEmailAddress($trimmed_email);
1013 $contacts2 = $seed->emailAddress->getBeansByEmailAddress($trimmed_email2);
1014 $contacts = array_merge($contacts, $contacts2);
1015 if(count($contacts) == 0){
1016 $GLOBALS['log']->info('End: SoapHelperWebServices->check_for_duplicate_contacts - no duplicte found');
1019 foreach($contacts as $contact){
1020 if(!empty($trimmed_last) && strcmp($trimmed_last, $contact->last_name) == 0){
1021 if((!empty($trimmed_email) || !empty($trimmed_email2)) && (strcmp($trimmed_email, $contact->email1) == 0 || strcmp($trimmed_email, $contact->email2) == 0 || strcmp($trimmed_email2, $contact->email) == 0 || strcmp($trimmed_email2, $contact->email2) == 0)){
1022 $contact->load_relationship('accounts');
1023 if(empty($seed->account_name) || strcmp($seed->account_name, $contact->account_name) == 0){
1024 $GLOBALS['log']->info('End: SoapHelperWebServices->check_for_duplicate_contacts - duplicte found ' . $contact->id);
1025 return $contact->id;
1030 $GLOBALS['log']->info('End: SoapHelperWebServices->check_for_duplicate_contacts - no duplicte found');
1034 $GLOBALS['log']->info('End: SoapHelperWebServices->check_for_duplicate_contacts - no duplicte found');
1040 * decrypt a string use the TripleDES algorithm. This meant to be
1041 * modified if the end user chooses a different algorithm
1043 * @param $string - the string to decrypt
1045 * @return a decrypted string if we can decrypt, the original string otherwise
1047 function decrypt_string($string){
1048 $GLOBALS['log']->info('Begin: SoapHelperWebServices->decrypt_string');
1049 if(function_exists('mcrypt_cbc')){
1050 require_once('modules/Administration/Administration.php');
1051 $focus = new Administration();
1052 $focus->retrieveSettings();
1054 if(!empty($focus->settings['ldap_enc_key'])){
1055 $key = $focus->settings['ldap_enc_key'];
1058 $GLOBALS['log']->info('End: SoapHelperWebServices->decrypt_string - empty key');
1062 $key = substr(md5($key),0,24);
1064 $GLOBALS['log']->info('End: SoapHelperWebServices->decrypt_string');
1065 return mcrypt_cbc(MCRYPT_3DES, $key, pack("H*", $buffer), MCRYPT_DECRYPT, $iv);
1067 $GLOBALS['log']->info('End: SoapHelperWebServices->decrypt_string');
1072 function isLogLevelDebug() {
1073 if (isset($GLOBALS['sugar_config']['logger'])) {
1074 if (isset($GLOBALS['sugar_config']['logger']['level'])) {
1075 return ($GLOBALS['sugar_config']['logger']['level'] == 'debug');