2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM 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) {
45 $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_field_list');
46 $module_fields = array();
47 $link_fields = array();
48 if(!empty($value->field_defs)){
50 foreach($value->field_defs as $var){
51 if(!empty($fields) && !in_array( $var['name'], $fields))continue;
52 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;
53 if ($var['source'] == 'non_db' && (isset($var['type']) && $var['type'] != 'link')) {
57 $options_dom = array();
58 $options_ret = array();
59 // Apparently the only purpose of this check is to make sure we only return fields
60 // when we've read a record. Otherwise this function is identical to get_module_field_list
61 if( isset($var['required']) && $var['required'] && $var['required'] !== 'false' ){
64 if(isset($var['options'])){
65 $options_dom = translate($var['options'], $value->module_dir);
66 if(!is_array($options_dom)) $options_dom = array();
67 foreach($options_dom as $key=>$oneOption)
68 $options_ret[$key] = $this->get_name_value($key,$oneOption);
71 if(!empty($var['dbType']) && $var['type'] == 'bool') {
72 $options_ret['type'] = $this->get_name_value('type', $var['dbType']);
76 $entry['name'] = $var['name'];
77 $entry['type'] = $var['type'];
78 if ($var['type'] == 'link') {
79 $entry['relationship'] = (isset($var['relationship']) ? $var['relationship'] : '');
80 $entry['module'] = (isset($var['module']) ? $var['module'] : '');
81 $entry['bean_name'] = (isset($var['bean_name']) ? $var['bean_name'] : '');
82 $link_fields[$var['name']] = $entry;
85 $entry['label'] = isset($var['vname']) ? translate($var['vname'], $value->module_dir) : $var['name'];
87 $entry['label'] = isset($var['vname']) ? $var['vname'] : $var['name'];
89 $entry['required'] = $required;
90 $entry['options'] = $options_ret;
91 if(isset($var['default'])) {
92 $entry['default_value'] = $var['default'];
94 $module_fields[$var['name']] = $entry;
99 if($value->module_dir == 'Bugs'){
100 require_once('modules/Releases/Release.php');
101 $seedRelease = new Release();
102 $options = $seedRelease->get_releases(TRUE, "Active");
103 $options_ret = array();
104 foreach($options as $name=>$value){
105 $options_ret[] = array('name'=> $name , 'value'=>$value);
107 if(isset($module_fields['fixed_in_release'])){
108 $module_fields['fixed_in_release']['type'] = 'enum';
109 $module_fields['fixed_in_release']['options'] = $options_ret;
111 if(isset($module_fields['release'])){
112 $module_fields['release']['type'] = 'enum';
113 $module_fields['release']['options'] = $options_ret;
115 if(isset($module_fields['release_name'])){
116 $module_fields['release_name']['type'] = 'enum';
117 $module_fields['release_name']['options'] = $options_ret;
121 if(isset($value->assigned_user_name) && isset($module_fields['assigned_user_id'])) {
122 $module_fields['assigned_user_name'] = $module_fields['assigned_user_id'];
123 $module_fields['assigned_user_name']['name'] = 'assigned_user_name';
125 if(isset($module_fields['modified_user_id'])) {
126 $module_fields['modified_by_name'] = $module_fields['modified_user_id'];
127 $module_fields['modified_by_name']['name'] = 'modified_by_name';
129 if(isset($module_fields['created_by'])) {
130 $module_fields['created_by_name'] = $module_fields['created_by'];
131 $module_fields['created_by_name']['name'] = 'created_by_name';
134 $GLOBALS['log']->info('End: SoapHelperWebServices->get_field_list');
135 return array('module_fields' => $module_fields, 'link_fields' => $link_fields);
138 function setFaultObject($errorObject) {
139 if ($this->isLogLevelDebug()) {
140 $GLOBALS['log']->debug('SoapHelperWebServices->setFaultObject - ' . var_export($errorObject, true));
142 global $service_object;
143 $service_object->error($errorObject);
147 * Validate the user session based on user name and password hash.
149 * @param string $user_name -- The user name to create a session for
150 * @param string $password -- The MD5 sum of the user's password
151 * @return true -- If the session is created
152 * @return false -- If the session is not created
154 function validate_user($user_name, $password){
155 $GLOBALS['log']->info('Begin: SoapHelperWebServices->validate_user');
156 global $server, $current_user, $sugar_config, $system_config;
158 $user->user_name = $user_name;
159 $system_config = new Administration();
160 $system_config->retrieveSettings('system');
161 $authController = new AuthenticationController((!empty($sugar_config['authenticationClass'])? $sugar_config['authenticationClass'] : 'SugarAuthenticate'));
162 // Check to see if the user name and password are consistent.
163 if($user->authenticate_user($password)){
164 // we also need to set the current_user.
165 $user->retrieve($user->id);
166 $current_user = $user;
168 $GLOBALS['log']->info('End: SoapHelperWebServices->validate_user - validation passed');
170 }else if(function_exists('mcrypt_cbc')){
171 $password = $this->decrypt_string($password);
172 if($authController->login($user_name, $password) && isset($_SESSION['authenticated_user_id'])){
173 $user->retrieve($_SESSION['authenticated_user_id']);
174 $current_user = $user;
175 $GLOBALS['log']->info('End: SoapHelperWebServices->validate_user - validation passed');
179 $GLOBALS['log']->fatal("SECURITY: failed attempted login for $user_name using SOAP api");
180 $server->setError("Invalid username and/or password");
187 * Validate the provided session information is correct and current. Load the session.
189 * @param String $session_id -- The session ID that was returned by a call to login.
190 * @return true -- If the session is valid and loaded.
191 * @return false -- if the session is not valid.
193 function validate_authenticated($session_id){
194 $GLOBALS['log']->info('Begin: SoapHelperWebServices->validate_authenticated');
195 if(!empty($session_id)){
196 session_id($session_id);
198 if(!empty($_SESSION['is_valid_session']) && $this->is_valid_ip_address('ip_address') && $_SESSION['type'] == 'user'){
200 global $current_user;
201 require_once('modules/Users/User.php');
202 $current_user = new User();
203 $current_user->retrieve($_SESSION['user_id']);
204 $this->login_success();
205 $GLOBALS['log']->info('Begin: SoapHelperWebServices->validate_authenticated - passed');
206 $GLOBALS['log']->info('End: SoapHelperWebServices->validate_authenticated');
210 $GLOBALS['log']->debug("calling destroy");
213 LogicHook::initialize();
214 $GLOBALS['logic_hook']->call_custom_logic('Users', 'login_failed');
215 $GLOBALS['log']->info('End: SoapHelperWebServices->validate_authenticated - validation failed');
220 * Use the same logic as in SugarAuthenticate to validate the ip address
222 * @param string $session_var
223 * @return bool - true if the ip address is valid, false otherwise.
225 function is_valid_ip_address($session_var){
226 global $sugar_config;
227 // grab client ip address
228 $clientIP = query_client_ip();
230 // check to see if config entry is present, if not, verify client ip
231 if (!isset ($sugar_config['verify_client_ip']) || $sugar_config['verify_client_ip'] == true) {
232 // check to see if we've got a current ip address in $_SESSION
233 // and check to see if the session has been hijacked by a foreign ip
234 if (isset ($_SESSION[$session_var])) {
235 $session_parts = explode(".", $_SESSION[$session_var]);
236 $client_parts = explode(".", $clientIP);
237 if(count($session_parts) < 4) {
240 // match class C IP addresses
241 for ($i = 0; $i < 3; $i ++) {
242 if ($session_parts[$i] == $client_parts[$i]) {
251 // we have a different IP address
252 if ($_SESSION[$session_var] != $clientIP && empty ($classCheck)) {
253 $GLOBALS['log']->fatal("IP Address mismatch: SESSION IP: {$_SESSION[$session_var]} CLIENT IP: {$clientIP}");
263 function checkSessionAndModuleAccess($session, $login_error_key, $module_name, $access_level, $module_access_level_error_key, $errorObject) {
264 $GLOBALS['log']->info('Begin: SoapHelperWebServices->checkSessionAndModuleAccess');
265 if(!$this->validate_authenticated($session)){
266 $GLOBALS['log']->info('SoapHelperWebServices->checkSessionAndModuleAccess - validate_authenticated failed');
267 $errorObject->set_error('invalid_session');
268 $this->setFaultObject($errorObject);
269 $GLOBALS['log']->info('End: SoapHelperWebServices->checkSessionAndModuleAccess');
273 global $beanList, $beanFiles;
274 if (!empty($module_name)) {
275 if(empty($beanList[$module_name])){
276 $GLOBALS['log']->info('SoapHelperWebServices->checkSessionAndModuleAccess - module does not exists - ' . $module_name);
277 $errorObject->set_error('no_module');
278 $this->setFaultObject($errorObject);
279 $GLOBALS['log']->info('End: SoapHelperWebServices->checkSessionAndModuleAccess');
282 global $current_user;
283 if(!$this->check_modules_access($current_user, $module_name, $access_level)){
284 $GLOBALS['log']->info('SoapHelperWebServices->checkSessionAndModuleAccess - no module access - ' . $module_name);
285 $errorObject->set_error('no_access');
286 $this->setFaultObject($errorObject);
287 $GLOBALS['log']->info('End: SoapHelperWebServices->checkSessionAndModuleAccess');
291 $GLOBALS['log']->info('End: SoapHelperWebServices->checkSessionAndModuleAccess');
295 function checkACLAccess($bean, $viewType, $errorObject, $error_key) {
296 $GLOBALS['log']->info('Begin: SoapHelperWebServices->checkACLAccess');
297 if(!$bean->ACLAccess($viewType)) {
298 $GLOBALS['log']->info('SoapHelperWebServices->checkACLAccess - no ACLAccess');
299 $errorObject->set_error($error_key);
300 $this->setFaultObject($errorObject);
301 $GLOBALS['log']->info('End: SoapHelperWebServices->checkACLAccess');
304 $GLOBALS['log']->info('End: SoapHelperWebServices->checkACLAccess');
308 function get_name_value($field,$value){
309 return array('name'=>$field, 'value'=>$value);
312 function get_user_module_list($user){
313 $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_user_module_list');
314 global $app_list_strings, $current_language;
315 $app_list_strings = return_app_list_strings_language($current_language);
316 $modules = query_module_access_list($user);
317 ACLController :: filterModuleList($modules, false);
318 global $modInvisList, $modInvisListActivities;
320 foreach($modInvisList as $invis){
321 $modules[$invis] = 'read_only';
324 if(isset($modules['Calendar']) || $modules['Activities']){
325 foreach($modInvisListActivities as $invis){
326 $modules[$invis] = $invis;
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');
357 $GLOBALS['log']->info('End: SoapHelperWebServices->check_modules_access');
360 $GLOBALS['log']->info('End: SoapHelperWebServices->check_modules_access');
363 $GLOBALS['log']->info('End: SoapHelperWebServices->check_modules_access');
368 function get_name_value_list(&$value){
369 $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_name_value_list');
370 global $app_list_strings;
372 if(!empty($value->field_defs)){
373 if(isset($value->assigned_user_name)) {
374 $list['assigned_user_name'] = $this->get_name_value('assigned_user_name', $value->assigned_user_name);
376 if(isset($value->modified_by_name)) {
377 $list['modified_by_name'] = $this->get_name_value('modified_by_name', $value->modified_by_name);
379 if(isset($value->created_by_name)) {
380 $list['created_by_name'] = $this->get_name_value('created_by_name', $value->created_by_name);
382 foreach($value->field_defs as $var){
383 if(isset($var['source']) && ($var['source'] != 'db' && $var['source'] != 'custom_fields') && $var['name'] != 'email1' && $var['name'] != 'email2' && (!isset($var['type'])|| $var['type'] != 'relate'))continue;
385 if(isset($value->$var['name'])){
386 $val = $value->$var['name'];
387 $type = $var['type'];
389 if(strcmp($type, 'date') == 0){
390 $val = substr($val, 0, 10);
391 }elseif(strcmp($type, 'enum') == 0 && !empty($var['options'])){
392 //$val = $app_list_strings[$var['options']][$val];
395 $list[$var['name']] = $this->get_name_value($var['name'], $val);
399 $GLOBALS['log']->info('End: SoapHelperWebServices->get_name_value_list');
404 function filter_fields($value, $fields) {
405 $GLOBALS['log']->info('Begin: SoapHelperWebServices->filter_fields');
406 global $invalid_contact_fields;
407 $filterFields = array();
408 foreach($fields as $field){
409 if (is_array($invalid_contact_fields)) {
410 if (in_array($field, $invalid_contact_fields)) {
414 if (isset($value->field_defs[$field])) {
415 $var = $value->field_defs[$field];
416 if(isset($var['source']) && ($var['source'] != 'db' && $var['source'] != 'custom_fields') && $var['name'] != 'email1' && $var['name'] != 'email2' && (!isset($var['type'])|| $var['type'] != 'relate')) {
418 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'))) {
425 $filterFields[] = $field;
427 $GLOBALS['log']->info('End: SoapHelperWebServices->filter_fields');
428 return $filterFields;
431 function get_name_value_list_for_fields($value, $fields) {
432 $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_name_value_list_for_fields');
433 global $app_list_strings;
434 global $invalid_contact_fields;
437 if(!empty($value->field_defs)){
438 if(empty($fields))$fields = array_keys($value->field_defs);
439 if(isset($value->assigned_user_name) && in_array('assigned_user_name', $fields)) {
440 $list['assigned_user_name'] = $this->get_name_value('assigned_user_name', $value->assigned_user_name);
442 if(isset($value->modified_by_name) && in_array('modified_by_name', $fields)) {
443 $list['modified_by_name'] = $this->get_name_value('modified_by_name', $value->modified_by_name);
445 if(isset($value->created_by_name) && in_array('created_by_name', $fields)) {
446 $list['created_by_name'] = $this->get_name_value('created_by_name', $value->created_by_name);
449 $filterFields = $this->filter_fields($value, $fields);
450 foreach($filterFields as $field){
451 $var = $value->field_defs[$field];
452 if(isset($value->$var['name'])){
453 $val = $value->$var['name'];
454 $type = $var['type'];
456 if(strcmp($type, 'date') == 0){
457 $val = substr($val, 0, 10);
458 }elseif(strcmp($type, 'enum') == 0 && !empty($var['options'])){
459 //$val = $app_list_strings[$var['options']][$val];
462 $list[$var['name']] = $this->get_name_value($var['name'], $val);
466 $GLOBALS['log']->info('End: SoapHelperWebServices->get_name_value_list_for_fields');
467 if ($this->isLogLevelDebug()) {
468 $GLOBALS['log']->debug('SoapHelperWebServices->get_name_value_list_for_fields - return data = ' . var_export($list, true));
474 function array_get_name_value_list($array){
475 $GLOBALS['log']->info('Begin: SoapHelperWebServices->array_get_name_value_list');
477 foreach($array as $name=>$value){
478 $list[$name] = $this->get_name_value($name, $value);
480 $GLOBALS['log']->info('End: SoapHelperWebServices->array_get_name_value_list');
485 function array_get_name_value_lists($array){
486 $GLOBALS['log']->info('Begin: SoapHelperWebServices->array_get_name_value_lists');
488 foreach($array as $name=>$value){
490 if(is_array($value)){
491 $tmp_value = array();
492 foreach($value as $k=>$v){
493 $tmp_value[$k] = $this->get_name_value($k, $v);
496 $list[$name] = $this->get_name_value($name, $tmp_value);
498 $GLOBALS['log']->info('End: SoapHelperWebServices->array_get_name_value_lists');
502 function name_value_lists_get_array($list){
503 $GLOBALS['log']->info('Begin: SoapHelperWebServices->name_value_lists_get_array');
505 foreach($list as $key=>$value){
506 if(isset($value['value']) && isset($value['name'])){
507 if(is_array($value['value'])){
508 $array[$value['name']]=array();
509 foreach($value['value'] as $v){
510 $array[$value['name']][$v['name']]=$v['value'];
513 $array[$value['name']]=$value['value'];
517 $GLOBALS['log']->info('End: SoapHelperWebServices->name_value_lists_get_array');
521 function array_get_return_value($array, $module){
523 $GLOBALS['log']->info('Begin/End: SoapHelperWebServices->array_get_return_value');
524 return Array('id'=>$array['id'],
525 'module_name'=> $module,
526 'name_value_list'=>$this->array_get_name_value_list($array)
530 function get_return_value_for_fields($value, $module, $fields) {
531 $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_return_value_for_fields');
532 global $module_name, $current_user;
533 $module_name = $module;
534 if($module == 'Users' && $value->id != $current_user->id){
535 $value->user_hash = '';
537 $GLOBALS['log']->info('End: SoapHelperWebServices->get_return_value_for_fields');
538 return Array('id'=>$value->id,
539 'module_name'=> $module,
540 'name_value_list'=>$this->get_name_value_list_for_fields($value, $fields)
544 function getRelationshipResults($bean, $link_field_name, $link_module_fields, $optional_where = '') {
545 $GLOBALS['log']->info('Begin: SoapHelperWebServices->getRelationshipResults');
546 require_once('include/TimeDate.php');
547 global $beanList, $beanFiles, $current_user;
548 global $disable_date_format;
550 $bean->load_relationship($link_field_name);
551 if (isset($bean->$link_field_name)) {
552 // get the query object for this link field
553 $query_array = $bean->$link_field_name->getQuery(true,array(),0,'',true);
554 if (isset($query_array['where'])) {
555 $query_array['where'] = str_ireplace("where", "", $query_array['where']);
556 if (!empty($optional_where)) {
557 $optional_where = $query_array['where'] . " and " . $optional_where;
559 $optional_where = $query_array['where'];
564 $params['joined_tables'] = $query_array['join_tables'];
566 // get the related module name and instantiate a bean for that.
567 $submodulename = $bean->$link_field_name->getRelatedModuleName();
568 $submoduleclass = $beanList[$submodulename];
569 require_once($beanFiles[$submoduleclass]);
570 $submodule = new $submoduleclass();
571 $filterFields = $this->filter_fields($submodule, $link_module_fields);
572 $relFields = $bean->$link_field_name->getRelatedFields();
575 $idSetInSubModule = false;
576 if($submodulename == 'Users' && !in_array('id', $link_module_fields)){
577 $link_module_fields[] = 'id';
578 $idSetInSubModule = true;
581 if(!empty($relFields)){
582 foreach($link_module_fields as $field){
583 if(!empty($relFields[$field])){
584 $roleSelect .= ', ' . $query_array['join_tables'][0] . '.'. $field;
589 $subquery = $submodule->create_new_list_query('',$optional_where ,$filterFields,$params, 0,'', true,$bean);
590 $query = $subquery['select'].$roleSelect . $subquery['from'].$query_array['join']. $subquery['where'];
591 $GLOBALS['log']->info('SoapHelperWebServices->getRelationshipResults query = ' . $query);
593 $result = $submodule->db->query($query, true);
595 while($row = $submodule->db->fetchByAssoc($result)) {
596 if (!$disable_date_format) {
597 foreach ($filterFields as $field) {
598 if (isset($submodule->field_defs[$field]) &&
599 isset($submodule->field_defs[$field]['type']) &&
600 isset($row[$field])) {
602 if ($submodule->field_defs[$field]['type'] == 'date') {
604 $row[$field] = $timedate->to_display_date_time($row[$field]);
606 if ($submodule->field_defs[$field]['type'] == 'currency') {
607 // TODO: convert data from db to user preferred format absed on the community input
613 if($submodulename == 'Users' && $current_user->id != $row['id']) {
614 $row['user_hash'] = "";
616 if ($idSetInSubModule) {
621 $GLOBALS['log']->info('End: SoapHelperWebServices->getRelationshipResults');
622 return array('rows' => $list, 'fields_set_on_rows' => $filterFields);
624 $GLOBALS['log']->info('End: SoapHelperWebServices->getRelationshipResults - ' . $link_field_name . ' relationship does not exists');
630 function get_return_value_for_link_fields($bean, $module, $link_name_to_value_fields_array) {
631 $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_return_value_for_link_fields');
632 global $module_name, $current_user;
633 $module_name = $module;
634 if($module == 'Users' && $bean->id != $current_user->id){
635 $bean->user_hash = '';
638 if (empty($link_name_to_value_fields_array) || !is_array($link_name_to_value_fields_array)) {
639 $GLOBALS['log']->debug('End: SoapHelperWebServices->get_return_value_for_link_fields - Invalid link information passed ');
643 if ($this->isLogLevelDebug()) {
644 $GLOBALS['log']->debug('SoapHelperWebServices->get_return_value_for_link_fields - link info = ' . var_export($link_name_to_value_fields_array, true));
646 $link_output = array();
647 foreach($link_name_to_value_fields_array as $link_name_value_fields) {
648 if (!is_array($link_name_value_fields) || !isset($link_name_value_fields['name']) || !isset($link_name_value_fields['value'])) {
651 $link_field_name = $link_name_value_fields['name'];
652 $link_module_fields = $link_name_value_fields['value'];
653 if (is_array($link_module_fields) && !empty($link_module_fields)) {
654 $result = $this->getRelationshipResults($bean, $link_field_name, $link_module_fields);
656 $link_output[] = array('name' => $link_field_name, 'records' => array());
659 $list = $result['rows'];
660 $filterFields = $result['fields_set_on_rows'];
663 foreach($list as $row) {
664 $nameValueArray = array();
665 foreach ($filterFields as $field) {
666 $nameValue = array();
667 if (isset($row[$field])) {
668 $nameValueArray[$field] = $this->get_name_value($field, $row[$field]);
671 $rowArray[] = $nameValueArray;
673 $link_output[] = array('name' => $link_field_name, 'records' => $rowArray);
677 $GLOBALS['log']->debug('End: SoapHelperWebServices->get_return_value_for_link_fields');
678 if ($this->isLogLevelDebug()) {
679 $GLOBALS['log']->debug('SoapHelperWebServices->get_return_value_for_link_fields - output = ' . var_export($link_output, true));
686 * @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).
687 * @param String $module_id -- The ID of the bean in the specified module
688 * @param String $link_field_name - The relationship name for which to create realtionships.
689 * @param Array $related_ids -- The array of ids for which we want to create relationships
690 * @param Array $name_value_list -- The array of name value pair of additional attributes to be set when adding this relationship
691 * @param int delete -- If 0 then add relationship else delete this relationship data
692 * @return true on success, false on failure
694 function new_handle_set_relationship($module_name, $module_id, $link_field_name, $related_ids, $name_value_list, $delete) {
695 $GLOBALS['log']->info('Begin: SoapHelperWebServices->new_handle_set_relationship');
696 global $beanList, $beanFiles;
698 if(empty($beanList[$module_name])) {
699 $GLOBALS['log']->debug('SoapHelperWebServices->new_handle_set_relationship - module ' . $module_name . ' does not exists' );
700 $GLOBALS['log']->info('End: SoapHelperWebServices->new_handle_set_relationship');
703 $class_name = $beanList[$module_name];
704 require_once($beanFiles[$class_name]);
705 $mod = new $class_name();
706 $mod->retrieve($module_id);
707 if(!$mod->ACLAccess('DetailView')){
708 $GLOBALS['log']->info('End: SoapHelperWebServices->new_handle_set_relationship');
712 if ($mod->load_relationship($link_field_name)) {
714 $name_value_pair = array();
715 if (!empty($name_value_list)) {
716 $relFields = $mod->$link_field_name->getRelatedFields();
717 if(!empty($relFields)){
718 $relFieldsKeys = array_keys($relFields);
719 foreach($name_value_list as $key => $value) {
720 if (in_array($value['name'], $relFieldsKeys)) {
721 $name_value_pair[$value['name']] = $value['value'];
726 $mod->$link_field_name->add($related_ids, $name_value_pair);
728 foreach($related_ids as $id) {
729 $mod->$link_field_name->delete($module_id, $id);
732 $GLOBALS['log']->info('End: SoapHelperWebServices->new_handle_set_relationship');
735 $GLOBALS['log']->info('End: SoapHelperWebServices->new_handle_set_relationship');
740 function new_handle_set_entries($module_name, $name_value_lists, $select_fields = FALSE) {
741 $GLOBALS['log']->info('Begin: SoapHelperWebServices->new_handle_set_entries');
742 global $beanList, $beanFiles, $current_user;
744 $ret_values = array();
746 global $current_user;
747 $class_name = $beanList[$module_name];
748 require_once($beanFiles[$class_name]);
751 $total = sizeof($name_value_lists);
752 foreach($name_value_lists as $name_value_list){
753 $seed = new $class_name();
755 $seed->update_vcal = false;
756 foreach($name_value_list as $value){
757 if($value['name'] == 'id'){
758 $seed->retrieve($value['value']);
763 foreach($name_value_list as $value) {
764 $val = $value['value'];
765 if($seed->field_name_map[$value['name']]['type'] == 'enum'){
766 $vardef = $seed->field_name_map[$value['name']];
767 if(isset($app_list_strings[$vardef['options']]) && !isset($app_list_strings[$vardef['options']][$value]) ) {
768 if ( in_array($val,$app_list_strings[$vardef['options']]) ){
769 $val = array_search($val,$app_list_strings[$vardef['options']]);
773 if($module_name == 'Users' && !empty($seed->id) && ($seed->id != $current_user->id) && $value['name'] == 'user_hash'){
777 $seed->$value['name'] = $val;
780 if($count == $total){
781 $seed->update_vcal = false;
785 //Add the account to a contact
786 if($module_name == 'Contacts'){
787 $GLOBALS['log']->debug('Creating Contact Account');
788 $this->add_create_account($seed);
789 $duplicate_id = $this->check_for_duplicate_contacts($seed);
790 if($duplicate_id == null){
791 if($seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
793 if($seed->deleted == 1){
794 $seed->mark_deleted($seed->id);
800 //since we found a duplicate we should set the sync flag
801 if( $seed->ACLAccess('Save')){
802 $seed = new $class_name();
803 $seed->id = $duplicate_id;
804 $seed->contacts_users_id = $current_user->id;
806 $ids[] = $duplicate_id;//we have a conflict
810 else if($module_name == 'Meetings' || $module_name == 'Calls'){
811 //we are going to check if we have a meeting in the system
812 //with the same outlook_id. If we do find one then we will grab that
814 if( $seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
815 if(empty($seed->id) && !isset($seed->id)){
816 if(!empty($seed->outlook_id) && isset($seed->outlook_id)){
817 //at this point we have an object that does not have
818 //the id set, but does have the outlook_id set
819 //so we need to query the db to find if we already
820 //have an object with this outlook_id, if we do
821 //then we can set the id, otherwise this is a new object
823 $query = $seed->table_name.".outlook_id = '".$seed->outlook_id."'";
824 $response = $seed->get_list($order_by, $query, 0,-1,-1,0);
825 $list = $response['list'];
826 if(count($list) > 0){
827 foreach($list as $value)
829 $seed->id = $value->id;
835 if (empty($seed->reminder_time)) {
836 $seed->reminder_time = -1;
838 if($seed->reminder_time == -1){
839 $defaultRemindrTime = $current_user->getPreference('reminder_time');
840 if ($defaultRemindrTime != -1){
841 $seed->reminder_checked = '1';
842 $seed->reminder_time = $defaultRemindrTime;
851 if( $seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
857 // if somebody is calling set_entries_detail() and wants fields returned...
858 if ($select_fields !== FALSE) {
859 $ret_values[$count] = array();
861 foreach ($select_fields as $select_field) {
862 if (isset($seed->$select_field)) {
863 $ret_values[$count][$select_field] = $this->get_name_value($select_field, $seed->$select_field);
869 // handle returns for set_entries_detail() and set_entries()
870 if ($select_fields !== FALSE) {
871 $GLOBALS['log']->info('End: SoapHelperWebServices->new_handle_set_entries');
873 'name_value_lists' => $ret_values,
877 $GLOBALS['log']->info('End: SoapHelperWebServices->new_handle_set_entries');
884 function get_return_value(&$value, $module){
885 $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_return_value');
886 global $module_name, $current_user;
887 $module_name = $module;
888 if($module == 'Users' && $value->id != $current_user->id){
889 $value->user_hash = '';
891 $GLOBALS['log']->info('End: SoapHelperWebServices->new_handle_set_entries');
892 return Array('id'=>$value->id,
893 'module_name'=> $module,
894 'name_value_list'=>$this->get_name_value_list($value)
899 function get_return_module_fields($value, $module,$fields, $translate=true){
900 $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_return_module_fields');
902 $module_name = $module;
903 $result = $this->get_field_list($value,$fields, $translate);
904 $GLOBALS['log']->info('End: SoapHelperWebServices->get_return_module_fields');
905 return Array('module_name'=>$module,
906 'module_fields'=> $result['module_fields'],
907 'link_fields'=> $result['link_fields'],
911 function login_success($name_value_list = array()){
912 $GLOBALS['log']->info('Begin: SoapHelperWebServices->login_success');
913 global $current_language, $sugar_config, $app_strings, $app_list_strings;
914 $current_language = $sugar_config['default_language'];
915 if (is_array($name_value_list) && !empty($name_value_list)) {
916 foreach($name_value_list as $key => $value) {
917 if (isset($value['name']) && ($value['name'] == 'language')) {
918 $language = $value['value'];
919 $supportedLanguages = $sugar_config['languages'];
920 if (array_key_exists($language, $supportedLanguages)) {
921 $current_language = $language;
924 if (isset($value['name']) && ($value['name'] == 'notifyonsave')) {
925 if ($value['value']) {
926 $_SESSION['notifyonsave'] = true;
931 if (isset($_SESSION['user_language'])) {
932 $current_language = $_SESSION['user_language'];
935 $GLOBALS['log']->info("Users language is = " . $current_language);
936 $app_strings = return_application_language($current_language);
937 $app_list_strings = return_app_list_strings_language($current_language);
938 $GLOBALS['log']->info('End: SoapHelperWebServices->login_success');
942 function checkSaveOnNotify() {
943 $notifyonsave = false;
944 if (isset($_SESSION['notifyonsave']) && $_SESSION['notifyonsave'] == true) {
945 $notifyonsave = true;
947 return $notifyonsave;
950 * Given an account_name, either create the account or assign to a contact.
952 function add_create_account($seed) {
953 $GLOBALS['log']->info('Begin: SoapHelperWebServices->add_create_account');
954 global $current_user;
955 $account_name = $seed->account_name;
956 $account_id = $seed->account_id;
957 $assigned_user_id = $current_user->id;
959 // check if it already exists
960 $focus = new Account();
961 if($focus->ACLAccess('Save')) {
962 $class = get_class($seed);
963 $temp = new $class();
964 $temp->retrieve($seed->id);
965 if ((!isset($account_name) || $account_name == '')) {
968 if (!isset($seed->accounts)){
969 $seed->load_relationship('accounts');
972 if($seed->account_name = '' && isset($temp->account_id)){
973 $seed->accounts->delete($seed->id, $temp->account_id);
974 $GLOBALS['log']->info('End: SoapHelperWebServices->add_create_account');
979 $query = "select id, deleted from {$focus->table_name} WHERE name='".$seed->db->quote($account_name)."'";
980 $result = $seed->db->query($query) or sugar_die("Error selecting sugarbean: ".mysql_error());
982 $row = $seed->db->fetchByAssoc($result, -1, false);
984 // we found a row with that id
985 if (isset($row['id']) && $row['id'] != -1)
987 // if it exists but was deleted, just remove it entirely
988 if ( isset($row['deleted']) && $row['deleted'] == 1)
990 $query2 = "delete from {$focus->table_name} WHERE id='". $seed->db->quote($row['id'])."'";
991 $result2 = $seed->db->query($query2) or sugar_die("Error deleting existing sugarbean: ".mysql_error());
993 // else just use this id to link the contact to the account
996 $focus->id = $row['id'];
1000 // if we didnt find the account, so create it
1001 if (! isset($focus->id) || $focus->id == '')
1003 $focus->name = $account_name;
1005 if ( isset($assigned_user_id))
1007 $focus->assigned_user_id = $assigned_user_id;
1008 $focus->modified_user_id = $assigned_user_id;
1013 if($seed->accounts != null && $temp->account_id != null && $temp->account_id != $focus->id){
1014 $seed->accounts->delete($seed->id, $temp->account_id);
1017 if(isset($focus->id) && $focus->id != ''){
1018 $seed->account_id = $focus->id;
1020 $GLOBALS['log']->info('End: SoapHelperWebServices->add_create_account');
1023 $GLOBALS['log']->info('End: SoapHelperWebServices->add_create_account - Insufficient ACLAccess');
1027 function check_for_duplicate_contacts(&$seed){
1028 $GLOBALS['log']->info('Begin: SoapHelperWebServices->check_for_duplicate_contacts');
1029 require_once('modules/Contacts/Contact.php');
1031 if(isset($seed->id)){
1032 $GLOBALS['log']->info('End: SoapHelperWebServices->check_for_duplicate_contacts - no duplicte found');
1038 $trimmed_email = trim($seed->email1);
1039 $trimmed_last = trim($seed->last_name);
1040 $trimmed_first = trim($seed->first_name);
1041 if(!empty($trimmed_email)){
1043 //obtain a list of contacts which contain the same email address
1044 $contacts = $seed->emailAddress->getBeansByEmailAddress($trimmed_email);
1045 if(count($contacts) == 0){
1046 $GLOBALS['log']->info('End: SoapHelperWebServices->check_for_duplicate_contacts - no duplicte found');
1049 foreach($contacts as $contact){
1050 if(!empty($trimmed_last) && strcmp($trimmed_last, $contact->last_name) == 0){
1051 if(!empty($trimmed_email) && strcmp($trimmed_email, $contact->email1) == 0){
1052 if(!empty($trimmed_email)){
1053 if(strcmp($trimmed_email, $contact->email1) == 0){
1054 //bug: 39234 - check if the account names are the same
1055 //if the incoming contact's account_name is empty OR it is not empty and is the same
1056 //as an existing contact's account name, then find the match.
1057 $contact->load_relationship('accounts');
1058 if(empty($seed->account_name) || strcmp($seed->account_name, $contact->account_name) == 0){
1059 $GLOBALS['log']->info('End: SoapHelperWebServices->check_for_duplicate_contacts - duplicte found ' . $contact->id);
1060 return $contact->id;
1064 $GLOBALS['log']->info('End: SoapHelperWebServices->check_for_duplicate_contacts - duplicte found' . $contact->id);
1065 return $contact->id;
1070 $GLOBALS['log']->info('End: SoapHelperWebServices->check_for_duplicate_contacts - no duplicte found');
1074 $GLOBALS['log']->info('End: SoapHelperWebServices->check_for_duplicate_contacts - no duplicte found');
1080 * decrypt a string use the TripleDES algorithm. This meant to be
1081 * modified if the end user chooses a different algorithm
1083 * @param $string - the string to decrypt
1085 * @return a decrypted string if we can decrypt, the original string otherwise
1087 function decrypt_string($string){
1088 $GLOBALS['log']->info('Begin: SoapHelperWebServices->decrypt_string');
1089 if(function_exists('mcrypt_cbc')){
1090 require_once('modules/Administration/Administration.php');
1091 $focus = new Administration();
1092 $focus->retrieveSettings();
1094 if(!empty($focus->settings['ldap_enc_key'])){
1095 $key = $focus->settings['ldap_enc_key'];
1098 $GLOBALS['log']->info('End: SoapHelperWebServices->decrypt_string - empty key');
1102 $key = substr(md5($key),0,24);
1104 $GLOBALS['log']->info('End: SoapHelperWebServices->decrypt_string');
1105 return mcrypt_cbc(MCRYPT_3DES, $key, pack("H*", $buffer), MCRYPT_DECRYPT, $iv);
1107 $GLOBALS['log']->info('End: SoapHelperWebServices->decrypt_string');
1112 function isLogLevelDebug() {
1113 if (isset($GLOBALS['sugar_config']['logger'])) {
1114 if (isset($GLOBALS['sugar_config']['logger']['level'])) {
1115 return ($GLOBALS['sugar_config']['logger']['level'] == 'debug');