]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - soap/SoapHelperFunctions.php
Release 6.5.16
[Github/sugarcrm.git] / soap / SoapHelperFunctions.php
1 <?php
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-2013 SugarCRM Inc.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
38
39 /**
40  * Retrieve field data for a provided SugarBean.
41  *
42  * @param SugarBean $value -- The bean to retrieve the field information for.
43  * @return Array -- 'field'=>   'name' -- the name of the field
44  *                              'type' -- the data type of the field
45  *                              'label' -- the translation key for the label of the field
46  *                              'required' -- Is the field required?
47  *                              'options' -- Possible values for a drop down field
48  */
49 function get_field_list($value, $translate=true){
50         $list = array();
51
52         if(!empty($value->field_defs)){
53
54                 foreach($value->field_defs as $var){
55                         if(isset($var['source']) && ($var['source'] != 'db' && $var['source'] != 'custom_fields') && $var['name'] != 'email1' && $var['name'] != 'email2' && (!isset($var['type'])|| $var['type'] != 'relate'))continue;
56                         $required = 0;
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(!empty($var['required'])) {
62                                 $required = 1;
63                         }
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[] = get_name_value($key,$oneOption);
69                         }
70
71             if(!empty($var['dbType']) && $var['type'] == 'bool') {
72                 $options_ret[] = get_name_value('type', $var['dbType']);
73             }
74
75             $entry = array();
76             $entry['name'] = $var['name'];
77             $entry['type'] = $var['type'];
78             if($translate) {
79             $entry['label'] = isset($var['vname']) ? translate($var['vname'], $value->module_dir) : $var['name'];
80             } else {
81             $entry['label'] = isset($var['vname']) ? $var['vname'] : $var['name'];
82             }
83             $entry['required'] = $required;
84             $entry['options'] = $options_ret;
85                         if(isset($var['default'])) {
86                            $entry['default_value'] = $var['default'];
87                         }
88
89                         $list[$var['name']] = $entry;
90                 } //foreach
91         } //if
92
93     if (isset($value->module_dir) && $value->module_dir == 'Bugs') {
94
95                 $seedRelease = new Release();
96                 $options = $seedRelease->get_releases(TRUE, "Active");
97                 $options_ret = array();
98                 foreach($options as $name=>$value){
99                         $options_ret[] =  array('name'=> $name , 'value'=>$value);
100                 }
101                 if(isset($list['fixed_in_release'])){
102                         $list['fixed_in_release']['type'] = 'enum';
103                         $list['fixed_in_release']['options'] = $options_ret;
104                 }
105                 if(isset($list['release'])){
106                         $list['release']['type'] = 'enum';
107                         $list['release']['options'] = $options_ret;
108                 }
109                 if(isset($list['release_name'])){
110                         $list['release_name']['type'] = 'enum';
111                         $list['release_name']['options'] = $options_ret;
112                 }
113         }
114     if (isset($value->module_dir) && $value->module_dir == 'Emails') {
115         $fields = array('from_addr_name', 'reply_to_addr', 'to_addrs_names', 'cc_addrs_names', 'bcc_addrs_names');
116         foreach($fields as $field){
117             $var = $value->field_defs[$field];
118
119             $required = 0;
120             $entry = array();
121             $entry['name'] = $var['name'];
122             $entry['type'] = $var['type'];
123             if($translate) {
124             $entry['label'] = isset($var['vname']) ? translate($var['vname'], $value->module_dir) : $var['name'];
125             } else {
126             $entry['label'] = isset($var['vname']) ? $var['vname'] : $var['name'];
127             }
128             $entry['required'] = $required;
129             $entry['options'] = array();
130                         if(isset($var['default'])) {
131                            $entry['default_value'] = $var['default'];
132                         }
133
134                         $list[$var['name']] = $entry;
135         }
136     }
137
138         if(isset($value->assigned_user_name) && isset($list['assigned_user_id'])) {
139                 $list['assigned_user_name'] = $list['assigned_user_id'];
140                 $list['assigned_user_name']['name'] = 'assigned_user_name';
141         }
142         if(isset($list['modified_user_id'])) {
143                 $list['modified_by_name'] = $list['modified_user_id'];
144                 $list['modified_by_name']['name'] = 'modified_by_name';
145         }
146         if(isset($list['created_by'])) {
147                 $list['created_by_name'] = $list['created_by'];
148                 $list['created_by_name']['name'] = 'created_by_name';
149         }
150         return $list;
151 }
152
153 function new_get_field_list($value, $translate=true) {
154         $module_fields = array();
155         $link_fields = array();
156
157         if(!empty($value->field_defs)){
158
159                 foreach($value->field_defs as $var){
160                         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;
161                         if ($var['source'] == 'non_db' && (isset($var['type']) && $var['type'] != 'link')) {
162                                 continue;
163                         }
164                         $required = 0;
165                         $options_dom = array();
166                         $options_ret = array();
167                         // Apparently the only purpose of this check is to make sure we only return fields
168                         //   when we've read a record.  Otherwise this function is identical to get_module_field_list
169                         if(!empty($var['required'])) {
170                                 $required = 1;
171                         }
172                         if(isset($var['options'])){
173                                 $options_dom = translate($var['options'], $value->module_dir);
174                                 if(!is_array($options_dom)) $options_dom = array();
175                                 foreach($options_dom as $key=>$oneOption)
176                                         $options_ret[] = get_name_value($key,$oneOption);
177                         }
178
179             if(!empty($var['dbType']) && $var['type'] == 'bool') {
180                 $options_ret[] = get_name_value('type', $var['dbType']);
181             }
182
183             $entry = array();
184             $entry['name'] = $var['name'];
185             $entry['type'] = $var['type'];
186             if ($var['type'] == 'link') {
187                     $entry['relationship'] = (isset($var['relationship']) ? $var['relationship'] : '');
188                     $entry['module'] = (isset($var['module']) ? $var['module'] : '');
189                     $entry['bean_name'] = (isset($var['bean_name']) ? $var['bean_name'] : '');
190                                 $link_fields[$var['name']] = $entry;
191             } else {
192                     if($translate) {
193                         $entry['label'] = isset($var['vname']) ? translate($var['vname'], $value->module_dir) : $var['name'];
194                     } else {
195                         $entry['label'] = isset($var['vname']) ? $var['vname'] : $var['name'];
196                     }
197                     $entry['required'] = $required;
198                     $entry['options'] = $options_ret;
199                                 if(isset($var['default'])) {
200                                    $entry['default_value'] = $var['default'];
201                                 }
202                                 $module_fields[$var['name']] = $entry;
203             } // else
204                 } //foreach
205         } //if
206
207         if($value->module_dir == 'Bugs'){
208
209                 $seedRelease = new Release();
210                 $options = $seedRelease->get_releases(TRUE, "Active");
211                 $options_ret = array();
212                 foreach($options as $name=>$value){
213                         $options_ret[] =  array('name'=> $name , 'value'=>$value);
214                 }
215                 if(isset($module_fields['fixed_in_release'])){
216                         $module_fields['fixed_in_release']['type'] = 'enum';
217                         $module_fields['fixed_in_release']['options'] = $options_ret;
218                 }
219                 if(isset($module_fields['release'])){
220                         $module_fields['release']['type'] = 'enum';
221                         $module_fields['release']['options'] = $options_ret;
222                 }
223                 if(isset($module_fields['release_name'])){
224                         $module_fields['release_name']['type'] = 'enum';
225                         $module_fields['release_name']['options'] = $options_ret;
226                 }
227         }
228
229         if(isset($value->assigned_user_name) && isset($module_fields['assigned_user_id'])) {
230                 $module_fields['assigned_user_name'] = $module_fields['assigned_user_id'];
231                 $module_fields['assigned_user_name']['name'] = 'assigned_user_name';
232         }
233         if(isset($module_fields['modified_user_id'])) {
234                 $module_fields['modified_by_name'] = $module_fields['modified_user_id'];
235                 $module_fields['modified_by_name']['name'] = 'modified_by_name';
236         }
237         if(isset($module_fields['created_by'])) {
238                 $module_fields['created_by_name'] = $module_fields['created_by'];
239                 $module_fields['created_by_name']['name'] = 'created_by_name';
240         }
241
242         return array('module_fields' => $module_fields, 'link_fields' => $link_fields);
243 } // fn
244
245 function setFaultObject($errorObject) {
246         global $soap_server_object;
247         $soap_server_object->fault($errorObject->getFaultCode(), $errorObject->getName(), '', $errorObject->getDescription());
248 } // fn
249
250 function checkSessionAndModuleAccess($session, $login_error_key, $module_name, $access_level, $module_access_level_error_key, $errorObject) {
251         if(!validate_authenticated($session)){
252                 $errorObject->set_error('invalid_login');
253                 setFaultObject($errorObject);
254                 return false;
255         } // if
256
257         global  $beanList, $beanFiles;
258         if (!empty($module_name)) {
259                 if(empty($beanList[$module_name])){
260                         $errorObject->set_error('no_module');
261                         setFaultObject($errorObject);
262                         return false;
263                 } // if
264                 global $current_user;
265                 if(!check_modules_access($current_user, $module_name, $access_level)){
266                         $errorObject->set_error('no_access');
267                         setFaultObject($errorObject);
268                         return false;
269                 }
270         } // if
271         return true;
272 } // fn
273
274 function checkACLAccess($bean, $viewType, $errorObject, $error_key) {
275         if(!$bean->ACLAccess($viewType)){
276                 $errorObject->set_error($error_key);
277                 setFaultObject($errorObject);
278                 return false;
279         } // if
280         return true;
281 } // fn
282
283 function get_name_value($field,$value){
284         return array('name'=>$field, 'value'=>$value);
285 }
286
287 function get_user_module_list($user){
288         global $app_list_strings, $current_language, $beanList, $beanFiles;
289
290         $app_list_strings = return_app_list_strings_language($current_language);
291         $modules = query_module_access_list($user);
292         ACLController :: filterModuleList($modules, false);
293         global $modInvisList;
294
295         foreach($modInvisList as $invis){
296                 $modules[$invis] = 'read_only';
297         }
298
299         $actions = ACLAction::getUserActions($user->id,true);
300         foreach($actions as $key=>$value){
301                 if(isset($value['module']) && $value['module']['access']['aclaccess'] < ACL_ALLOW_ENABLED){
302                         if ($value['module']['access']['aclaccess'] == ACL_ALLOW_DISABLED) {
303                                 unset($modules[$key]);
304                         } else {
305                                 $modules[$key] = 'read_only';
306                         } // else
307                 } else {
308                         $modules[$key] = '';
309                 } // else
310         } // foreach
311
312         //Remove all modules that don't have a beanFiles entry associated with it
313         foreach($modules as $module_name=>$module)
314         {
315         if ( isset($beanList[$module_name]) ) {
316             $class_name = $beanList[$module_name];
317             if(empty($beanFiles[$class_name])) {
318                 unset($modules[$module_name]);
319             }
320         } else {
321             unset($modules[$module_name]);
322         }
323         }
324
325         return $modules;
326
327 }
328
329 function check_modules_access($user, $module_name, $action='write'){
330         if(!isset($_SESSION['avail_modules'])){
331                 $_SESSION['avail_modules'] = get_user_module_list($user);
332         }
333     if(isset($_SESSION['avail_modules'][$module_name])){
334                 if($action == 'write' && $_SESSION['avail_modules'][$module_name] == 'read_only'){
335                         if(is_admin($user))return true;
336                         return false;
337                 }elseif($action == 'write' && strcmp(strtolower($module_name), 'users') == 0 && !$user->isAdminForModule($module_name)){
338             //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
339             return false;
340         }
341                 return true;
342         }
343         return false;
344
345 }
346
347 function get_name_value_list($value, $returnDomValue = false){
348         global $app_list_strings;
349         $list = array();
350         if(!empty($value->field_defs)){
351                 if(isset($value->assigned_user_name)) {
352                         $list['assigned_user_name'] = get_name_value('assigned_user_name', $value->assigned_user_name);
353                 }
354                 if(isset($value->modified_by_name)) {
355                         $list['modified_by_name'] = get_name_value('modified_by_name', $value->modified_by_name);
356                 }
357                 if(isset($value->created_by_name)) {
358                         $list['created_by_name'] = get_name_value('created_by_name', $value->created_by_name);
359                 }
360                 foreach($value->field_defs as $var){
361                         if(isset($var['source']) && ($var['source'] != 'db' && $var['source'] != 'custom_fields') && $var['name'] != 'email1' && $var['name'] != 'email2' && (!isset($var['type'])|| $var['type'] != 'relate')){
362
363                                         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'))) {
364
365                                         } else {
366                                                 continue;
367                                         }
368                                 }
369
370                         if(isset($value->$var['name'])){
371                                 $val = $value->$var['name'];
372                                 $type = $var['type'];
373
374                                 if(strcmp($type, 'date') == 0){
375                                         $val = substr($val, 0, 10);
376                                 }elseif(strcmp($type, 'enum') == 0 && !empty($var['options']) && $returnDomValue){
377                                         $val = $app_list_strings[$var['options']][$val];
378                                 }
379                                 elseif(strcmp($type, 'currency') == 0){
380                                         $params = array( 'currency_symbol' => false );
381                                         $val = currency_format_number($val, $params);
382                                 }
383
384                                 $list[$var['name']] = get_name_value($var['name'], $val);
385                         }
386                 }
387         }
388         return $list;
389
390 }
391
392 function filter_fields($value, $fields) {
393         global $invalid_contact_fields;
394         $filterFields = array();
395         foreach($fields as $field){
396                 if (is_array($invalid_contact_fields)) {
397                         if (in_array($field, $invalid_contact_fields)) {
398                                 continue;
399                         } // if
400                 } // if
401                 if (isset($value->field_defs[$field])) {
402                         $var = $value->field_defs[$field];
403                         if(isset($var['source']) && ($var['source'] != 'db' && $var['source'] != 'custom_fields') && $var['name'] != 'email1' && $var['name'] != 'email2' && (!isset($var['type'])|| $var['type'] != 'relate')) {
404
405                                 continue;
406                         }
407                 } // if
408         // No valid field should be caught by this quoting.
409                 $filterFields[] = getValidDBName($field);
410         } // foreach
411         return $filterFields;
412 } // fn
413
414 function get_name_value_list_for_fields($value, $fields) {
415         global $app_list_strings;
416         global $invalid_contact_fields;
417
418         $list = array();
419         if(!empty($value->field_defs)){
420                 if(isset($value->assigned_user_name) && in_array('assigned_user_name', $fields)) {
421                         $list['assigned_user_name'] = get_name_value('assigned_user_name', $value->assigned_user_name);
422                 }
423                 if(isset($value->modified_by_name) && in_array('modified_by_name', $fields)) {
424                         $list['modified_by_name'] = get_name_value('modified_by_name', $value->modified_by_name);
425                 }
426                 if(isset($value->created_by_name) && in_array('created_by_name', $fields)) {
427                         $list['created_by_name'] = get_name_value('created_by_name', $value->created_by_name);
428                 }
429
430                 $filterFields = filter_fields($value, $fields);
431                 foreach($filterFields as $field){
432                         $var = $value->field_defs[$field];
433                         if(isset($value->$var['name'])){
434                                 $val = $value->$var['name'];
435                                 $type = $var['type'];
436
437                                 if(strcmp($type, 'date') == 0){
438                                         $val = substr($val, 0, 10);
439                                 }elseif(strcmp($type, 'enum') == 0 && !empty($var['options'])){
440                                         $val = $app_list_strings[$var['options']][$val];
441                                 }
442
443                                 $list[$var['name']] = get_name_value($var['name'], $val);
444                         } // if
445                 } // foreach
446         } // if
447         return $list;
448
449 } // fn
450
451
452 function array_get_name_value_list($array){
453         $list = array();
454         foreach($array as $name=>$value){
455
456                                 $list[$name] = get_name_value($name, $value);
457         }
458         return $list;
459
460 }
461
462 function array_get_name_value_lists($array){
463     $list = array();
464     foreach($array as $name=>$value){
465         $tmp_value=$value;
466         if(is_array($value)){
467             $tmp_value = array();
468             foreach($value as $k=>$v){
469                 $tmp_value[] = get_name_value($k, $v);
470             }
471         }
472         $list[] = get_name_value($name, $tmp_value);
473     }
474     return $list;
475 }
476
477 function name_value_lists_get_array($list){
478     $array = array();
479     foreach($list as $key=>$value){
480         if(isset($value['value']) && isset($value['name'])){
481             if(is_array($value['value'])){
482                 $array[$value['name']]=array();
483                 foreach($value['value'] as $v){
484                     $array[$value['name']][$v['name']]=$v['value'];
485                 }
486             }else{
487                 $array[$value['name']]=$value['value'];
488             }
489         }
490     }
491     return $array;
492 }
493
494 function array_get_return_value($array, $module){
495
496         return Array('id'=>$array['id'],
497                                 'module_name'=> $module,
498                                 'name_value_list'=>array_get_name_value_list($array)
499                                 );
500 }
501
502 function get_return_value_for_fields($value, $module, $fields) {
503         global $module_name, $current_user;
504         $module_name = $module;
505         if($module == 'Users' && $value->id != $current_user->id){
506                 $value->user_hash = '';
507         }
508         $value = clean_sensitive_data($value->field_defs, $value);
509         return Array('id'=>$value->id,
510                                 'module_name'=> $module,
511                                 'name_value_list'=>get_name_value_list_for_fields($value, $fields)
512                                 );
513 }
514
515 function getRelationshipResults($bean, $link_field_name, $link_module_fields) {
516         global  $beanList, $beanFiles;
517         $bean->load_relationship($link_field_name);
518         if (isset($bean->$link_field_name)) {
519                 // get the query object for this link field
520                 $query_array = $bean->$link_field_name->getQuery(true,array(),0,'',true);
521                 $params = array();
522                 $params['joined_tables'] = $query_array['join_tables'];
523
524                 // get the related module name and instantiate a bean for that.
525                 $submodulename = $bean->$link_field_name->getRelatedModuleName();
526                 $submoduleclass = $beanList[$submodulename];
527                 require_once($beanFiles[$submoduleclass]);
528
529                 $submodule = new $submoduleclass();
530                 $filterFields = filter_fields($submodule, $link_module_fields);
531                 $relFields = $bean->$link_field_name->getRelatedFields();
532                 $roleSelect = '';
533
534                 if(!empty($relFields)){
535                         foreach($link_module_fields as $field){
536                                 if(!empty($relFields[$field])){
537                                         $roleSelect .= ', ' . $query_array['join_tables'][0] . '.'. $field;
538                                 }
539                         }
540                 }
541                 // create a query
542                 $subquery = $submodule->create_new_list_query('','',$filterFields,$params, 0,'', true,$bean);
543                 $query =  $subquery['select'].$roleSelect .   $subquery['from'].$query_array['join']. $subquery['where'];
544
545                 $result = $submodule->db->query($query, true);
546                 $list = array();
547                 while($row = $submodule->db->fetchByAssoc($result)) {
548                         $list[] = $row;
549                 }
550                 return array('rows' => $list, 'fields_set_on_rows' => $filterFields);
551         } else {
552                 return false;
553         } // else
554
555 } // fn
556
557 function get_return_value_for_link_fields($bean, $module, $link_name_to_value_fields_array) {
558         global $module_name, $current_user;
559         $module_name = $module;
560         if($module == 'Users' && $bean->id != $current_user->id){
561                 $bean->user_hash = '';
562         }
563         $bean = clean_sensitive_data($value->field_defs, $bean);
564
565         if (empty($link_name_to_value_fields_array) || !is_array($link_name_to_value_fields_array)) {
566                 return array();
567         }
568
569         $link_output = array();
570         foreach($link_name_to_value_fields_array as $link_name_value_fields) {
571                 if (!is_array($link_name_value_fields) || !isset($link_name_value_fields['name']) || !isset($link_name_value_fields['value'])) {
572                         continue;
573                 }
574                 $link_field_name = $link_name_value_fields['name'];
575                 $link_module_fields = $link_name_value_fields['value'];
576                 if (is_array($link_module_fields) && !empty($link_module_fields)) {
577                         $result = getRelationshipResults($bean, $link_field_name, $link_module_fields);
578                         if (!$result) {
579                                 $link_output[] = array('name' => $link_field_name, 'records' => array());
580                                 continue;
581                         }
582                         $list = $result['rows'];
583                         $filterFields = $result['fields_set_on_rows'];
584                         if ($list) {
585                                 $rowArray = array();
586                                 foreach($list as $row) {
587                                         $nameValueArray = array();
588                                         foreach ($filterFields as $field) {
589                                                 $nameValue = array();
590                                                 if (isset($row[$field])) {
591                                                         $nameValue['name'] = $field;
592                                                         $nameValue['value'] = $row[$field];
593                                                         $nameValueArray[] = $nameValue;
594                                                 } // if
595                                         } // foreach
596                                         $rowArray[] = $nameValueArray;
597                                 } // foreach
598                                 $link_output[] = array('name' => $link_field_name, 'records' => $rowArray);
599                         } // if
600                 } // if
601         } // foreach
602         return $link_output;
603 } // fn
604
605 /**
606  *
607  * @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).
608  * @param String $module_id -- The ID of the bean in the specified module
609  * @param String $link_field_name - The relationship name for which to create realtionships.
610  * @param Array $related_ids -- The array of ids for which we want to create relationships
611  * @return true on success, false on failure
612  */
613 function new_handle_set_relationship($module_name, $module_id, $link_field_name, $related_ids) {
614     global  $beanList, $beanFiles;
615
616     if(empty($beanList[$module_name])) {
617         return false;
618     } // if
619     $class_name = $beanList[$module_name];
620     require_once($beanFiles[$class_name]);
621     $mod = new $class_name();
622     $mod->retrieve($module_id);
623         if(!$mod->ACLAccess('DetailView')){
624                 return false;
625         }
626
627     foreach($related_ids as $ids) {
628         $GLOBALS['log']->debug("ids = " . $ids );
629     }
630
631         if ($mod->load_relationship($link_field_name)) {
632                 $mod->$link_field_name->add($related_ids);
633                 return true;
634         } else {
635                 return false;
636         }
637 }
638
639 function new_handle_set_entries($module_name, $name_value_lists, $select_fields = FALSE) {
640         global $beanList, $beanFiles, $app_list_strings;
641         global $current_user;
642
643         $ret_values = array();
644
645         $class_name = $beanList[$module_name];
646         require_once($beanFiles[$class_name]);
647         $ids = array();
648         $count = 1;
649         $total = sizeof($name_value_lists);
650         foreach($name_value_lists as $name_value_list){
651                 $seed = new $class_name();
652
653                 $seed->update_vcal = false;
654                 foreach($name_value_list as $value){
655                         if($value['name'] == 'id'){
656                                 $seed->retrieve($value['value']);
657                                 break;
658                         }
659                 }
660
661                 foreach($name_value_list as $value) {
662                         $val = $value['value'];
663                         if($seed->field_name_map[$value['name']]['type'] == 'enum'){
664                                 $vardef = $seed->field_name_map[$value['name']];
665                                 if(isset($app_list_strings[$vardef['options']]) && !isset($app_list_strings[$vardef['options']][$value]) ) {
666                             if ( in_array($val,$app_list_strings[$vardef['options']]) ){
667                                 $val = array_search($val,$app_list_strings[$vardef['options']]);
668                             }
669                         }
670                         }
671                         $seed->$value['name'] = $val;
672                 }
673
674                 if($count == $total){
675                         $seed->update_vcal = false;
676                 }
677                 $count++;
678
679                 //Add the account to a contact
680                 if($module_name == 'Contacts'){
681                         $GLOBALS['log']->debug('Creating Contact Account');
682                         add_create_account($seed);
683                         $duplicate_id = check_for_duplicate_contacts($seed);
684                         if($duplicate_id == null){
685                                 if($seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
686                                         $seed->save();
687                                         if($seed->deleted == 1){
688                                                 $seed->mark_deleted($seed->id);
689                                         }
690                                         $ids[] = $seed->id;
691                                 }
692                         }
693                         else{
694                                 //since we found a duplicate we should set the sync flag
695                                 if( $seed->ACLAccess('Save')){
696                                         $seed = new $class_name();
697                                         $seed->id = $duplicate_id;
698                                         $seed->contacts_users_id = $current_user->id;
699                                         $seed->save();
700                                         $ids[] = $duplicate_id;//we have a conflict
701                                 }
702                         }
703                 }
704                 else if($module_name == 'Meetings' || $module_name == 'Calls'){
705                         //we are going to check if we have a meeting in the system
706                         //with the same outlook_id. If we do find one then we will grab that
707                         //id and save it
708                         if( $seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
709                                 if(empty($seed->id) && !isset($seed->id)){
710                                         if(!empty($seed->outlook_id) && isset($seed->outlook_id)){
711                                                 //at this point we have an object that does not have
712                                                 //the id set, but does have the outlook_id set
713                                                 //so we need to query the db to find if we already
714                                                 //have an object with this outlook_id, if we do
715                                                 //then we can set the id, otherwise this is a new object
716                                                 $order_by = "";
717                                                 $query = $seed->table_name.".outlook_id = '".$GLOBALS['db']->quote($seed->outlook_id)."'";
718                                                 $response = $seed->get_list($order_by, $query, 0,-1,-1,0);
719                                                 $list = $response['list'];
720                                                 if(count($list) > 0){
721                                                         foreach($list as $value)
722                                                         {
723                                                                 $seed->id = $value->id;
724                                                                 break;
725                                                         }
726                                                 }//fi
727                                         }//fi
728                                 }//fi
729                                 $seed->save();
730                 if($seed->deleted == 1){
731                     $seed->mark_deleted($seed->id);
732                 }
733                                 $ids[] = $seed->id;
734                         }//fi
735                 }
736                 else
737                 {
738                         if( $seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
739                                 $seed->save();
740                                 $ids[] = $seed->id;
741                         }
742                 }
743
744                 // if somebody is calling set_entries_detail() and wants fields returned...
745                 if ($select_fields !== FALSE) {
746                         $ret_values[$count] = array();
747
748                         foreach ($select_fields as $select_field) {
749                                 if (isset($seed->$select_field)) {
750                                         $ret_values[$count][] = get_name_value($select_field, $seed->$select_field);
751                                 }
752                         }
753                 }
754         }
755
756         // handle returns for set_entries_detail() and set_entries()
757         if ($select_fields !== FALSE) {
758                 return array(
759                         'name_value_lists' => $ret_values,
760                 );
761         }
762         else {
763                 return array(
764                         'ids' => $ids,
765                 );
766         }
767 }
768
769 function get_return_value($value, $module, $returnDomValue = false){
770         global $module_name, $current_user;
771         $module_name = $module;
772         if($module == 'Users' && $value->id != $current_user->id){
773                 $value->user_hash = '';
774         }
775         $value = clean_sensitive_data($value->field_defs, $value);
776         return Array('id'=>$value->id,
777                                 'module_name'=> $module,
778                                 'name_value_list'=>get_name_value_list($value, $returnDomValue)
779                                 );
780 }
781
782
783 function get_encoded_Value($value) {
784
785     // XML 1.0 doesn't allow those...
786     $value = preg_replace("/([\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F])/", '', $value);
787     $value = htmlspecialchars($value, ENT_NOQUOTES, "utf-8");
788     return "<value>$value</value>";
789 }
790
791 function get_name_value_xml($val, $module_name){
792         $xml = '<item>';
793                         $xml .= '<id>'.$val['id'].'</id>';
794                         $xml .= '<module>'.$module_name.'</module>';
795                         $xml .= '<name_value_list>';
796                         foreach($val['name_value_list'] as $name=>$nv){
797                                 $xml .= '<name_value>';
798                                 $xml .= '<name>'.htmlspecialchars($nv['name']).'</name>';
799                 $xml .= get_encoded_Value($nv['value']);
800                                 $xml .= '</name_value>';
801                         }
802                         $xml .= '</name_value_list>';
803                         $xml .= '</item>';
804                         return $xml;
805 }
806
807 function new_get_return_module_fields($value, $module, $translate=true){
808         global $module_name;
809         $module_name = $module;
810         $result = new_get_field_list($value, $translate);
811         return Array('module_name'=>$module,
812                                 'module_fields'=> $result['module_fields'],
813                                 'link_fields'=> $result['link_fields'],
814                                 );
815 }
816
817 function get_return_module_fields($value, $module, $error, $translate=true){
818         global $module_name;
819         $module_name = $module;
820         return Array('module_name'=>$module,
821                                 'module_fields'=> get_field_list($value, $translate),
822                                 'error'=>get_name_value_list($value)
823                                 );
824 }
825
826 function get_return_error_value($error_num, $error_name, $error_description){
827         return Array('number'=>$error_num,
828                                 'name'=> $error_name,
829                                 'description'=> $error_description
830                                 );
831 }
832
833 function filter_field_list(&$field_list, $select_fields, $module_name){
834         return filter_return_list($field_list, $select_fields, $module_name);
835 }
836
837
838 /**
839  * Filter the results of a list query.  Limit the fields returned.
840  *
841  * @param Array $output_list -- The array of list data
842  * @param Array $select_fields -- The list of fields that should be returned.  If this array is specfied, only the fields in the array will be returned.
843  * @param String $module_name -- The name of the module this being worked on
844  * @return The filtered array of list data.
845  */
846 function filter_return_list(&$output_list, $select_fields, $module_name){
847
848         for($sug = 0; $sug < sizeof($output_list) ; $sug++){
849                 if($module_name == 'Contacts'){
850                         global $invalid_contact_fields;
851                         if(is_array($invalid_contact_fields)){
852                                 foreach($invalid_contact_fields as $name=>$val){
853                                         unset($output_list[$sug]['field_list'][$name]);
854                                         unset($output_list[$sug]['name_value_list'][$name]);
855
856                                 }
857                         }
858                 }
859
860                 if( !empty($output_list[$sug]['name_value_list']) && is_array($output_list[$sug]['name_value_list']) && !empty($select_fields) && is_array($select_fields)){
861                         foreach($output_list[$sug]['name_value_list'] as $name=>$value)
862                                         if(!in_array($value['name'], $select_fields)){
863                                                 unset($output_list[$sug]['name_value_list'][$name]);
864                                                 unset($output_list[$sug]['field_list'][$name]);
865                                         }
866                 }
867         }
868         return $output_list;
869 }
870
871 function login_success(){
872         global $current_language, $sugar_config, $app_strings, $app_list_strings;
873         $current_language = $sugar_config['default_language'];
874         $app_strings = return_application_language($current_language);
875         $app_list_strings = return_app_list_strings_language($current_language);
876 }
877
878
879 /*
880  *      Given an account_name, either create the account or assign to a contact.
881  */
882 function add_create_account($seed)
883 {
884         global $current_user;
885         $account_name = $seed->account_name;
886         $account_id = $seed->account_id;
887         $assigned_user_id = $current_user->id;
888
889         // check if it already exists
890     $focus = new Account();
891     if( $focus->ACLAccess('Save')){
892                 $class = get_class($seed);
893                 $temp = new $class();
894                 $temp->retrieve($seed->id);
895                 if ((! isset($account_name) || $account_name == ''))
896                 {
897                         return;
898                 }
899                 if (!isset($seed->accounts)){
900                         $seed->load_relationship('accounts');
901                 }
902
903                 if($seed->account_name == '' && isset($temp->account_id)){
904                         $seed->accounts->delete($seed->id, $temp->account_id);
905                         return;
906                 }
907
908         // attempt to find by id first
909         $ret = $focus->retrieve($account_id, true, false);
910
911         // if it doesn't exist by id, attempt to find by name (non-deleted)
912         if (empty($ret))
913         {
914             $query = "select {$focus->table_name}.id, {$focus->table_name}.deleted from {$focus->table_name} ";
915             $query .= " WHERE name='".$seed->db->quote($account_name)."'";
916             $query .=" ORDER BY deleted ASC";
917             $result = $seed->db->query($query, true);
918
919             $row = $seed->db->fetchByAssoc($result, false);
920
921             if (!empty($row['id']))
922             {
923                 $focus->retrieve($row['id']);
924             }
925         }
926         // if it exists by id but was deleted, just remove it entirely
927         else if ($focus->deleted)
928         {
929             $query2 = "delete from {$focus->table_name} WHERE id='". $seed->db->quote($focus->id) ."'";
930             $seed->db->query($query2, true);
931             // it was deleted, create new
932             $focus = BeanFactory::newBean('Accounts');
933         }
934
935                 // if we didnt find the account, so create it
936             if (empty($focus->id))
937             {
938                 $focus->name = $account_name;
939
940                         if ( isset($assigned_user_id))
941                         {
942                    $focus->assigned_user_id = $assigned_user_id;
943                    $focus->modified_user_id = $assigned_user_id;
944                         }
945                 $focus->save();
946             }
947
948             if($seed->accounts != null && $temp->account_id != null && $temp->account_id != $focus->id){
949                 $seed->accounts->delete($seed->id, $temp->account_id);
950             }
951
952             if(isset($focus->id) && $focus->id != ''){
953                         $seed->account_id = $focus->id;
954                 }
955     }
956 }
957
958 function check_for_duplicate_contacts($seed){
959
960         if(isset($seed->id)){
961                 return null;
962         }
963
964         $trimmed_email = trim($seed->email1);
965     $trimmed_email2 = trim($seed->email2);
966         $trimmed_last = trim($seed->last_name);
967         $trimmed_first = trim($seed->first_name);
968         if(!empty($trimmed_email) || !empty($trimmed_email2)){
969
970                 //obtain a list of contacts which contain the same email address
971                 $contacts = $seed->emailAddress->getBeansByEmailAddress($trimmed_email);
972         $contacts2 = $seed->emailAddress->getBeansByEmailAddress($trimmed_email2);
973         $contacts = array_merge($contacts, $contacts2);
974                 if(count($contacts) == 0){
975                         return null;
976                 }else{
977             foreach($contacts as $contact){
978                                 if(!empty($trimmed_last) && strcmp($trimmed_last, $contact->last_name) == 0){
979                     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)){
980                                                 //bug: 39234 - check if the account names are the same
981                                                 //if the incoming contact's account_name is empty OR it is not empty and is the same
982                                                 //as an existing contact's account name, then find the match.
983                         $contact->load_relationship('accounts');
984                                                 if(empty($seed->account_name) || strcmp($seed->account_name, $contact->account_name) == 0){
985                                                     $GLOBALS['log']->info('End: SoapHelperWebServices->check_for_duplicate_contacts - duplicte found ' . $contact->id);
986                                                         return $contact->id;
987                                                 }
988                                         }
989                                 }
990                         }
991                         return null;
992                 }
993         } else {
994         //This section of code is executed if no emails are supplied in the $seed instance
995
996         //This query is looking for the id of Contact records that do not have a primary email address based on the matching
997         //first and last name and the record being not deleted.  If any such records are found we will take the first one and assume
998         //that it is the duplicate record
999             $query = "SELECT c.id as id FROM contacts c
1000 LEFT OUTER JOIN email_addr_bean_rel eabr ON eabr.bean_id = c.id
1001 WHERE c.first_name = '{$trimmed_first}' AND c.last_name = '{$trimmed_last}' AND c.deleted = 0 AND eabr.id IS NULL";
1002
1003         //Apply the limit query filter to this since we only need the first record
1004         $result = $GLOBALS['db']->getOne($query);
1005         return !empty($result) ? $result : null;
1006     }
1007 }
1008
1009 /*
1010  * Given a client version and a server version, determine if the right hand side(server version) is greater
1011  *
1012  * @param left           the client sugar version
1013  * @param right          the server version
1014  *
1015  * return               true if the server version is greater or they are equal
1016  *                      false if the client version is greater
1017  */
1018 function is_server_version_greater($left, $right){
1019     if(count($left) == 0 && count($right) == 0){
1020         return false;
1021     }
1022     else if(count($left) == 0 || count($right) == 0){
1023         return true;
1024     }
1025     else if($left[0] == $right[0]){
1026         array_shift($left);
1027         array_shift($right);
1028         return is_server_version_greater($left, $right);
1029     }
1030     else if($left[0] < $right[0]){
1031        return true;
1032     }
1033     else
1034         return false;
1035 }
1036
1037 function getFile( $zip_file, $file_in_zip ){
1038     $base_upgrade_dir = sugar_cached("/upgrades");
1039     $base_tmp_upgrade_dir   = "$base_upgrade_dir/temp";
1040     $my_zip_dir = mk_temp_dir( $base_tmp_upgrade_dir );
1041     unzip_file( $zip_file, $file_in_zip, $my_zip_dir );
1042     return( "$my_zip_dir/$file_in_zip" );
1043 }
1044
1045 function getManifest( $zip_file ){
1046     ini_set("max_execution_time", "3600");
1047     return( getFile( $zip_file, "manifest.php" ) );
1048 }
1049
1050 if(!function_exists("get_encoded")){
1051 /*HELPER FUNCTIONS*/
1052 function get_encoded($object){
1053                 return  base64_encode(serialize($object));
1054 }
1055 function get_decoded($object){
1056                 return  unserialize(base64_decode($object));
1057
1058 }
1059
1060 /**
1061  * decrypt a string use the TripleDES algorithm. This meant to be
1062  * modified if the end user chooses a different algorithm
1063  *
1064  * @param $string - the string to decrypt
1065  *
1066  * @return a decrypted string if we can decrypt, the original string otherwise
1067  */
1068 function decrypt_string($string){
1069         if(function_exists('mcrypt_cbc')){
1070
1071                 $focus = new Administration();
1072                 $focus->retrieveSettings();
1073                 $key = '';
1074                 if(!empty($focus->settings['ldap_enc_key'])){
1075                         $key = $focus->settings['ldap_enc_key'];
1076                 }
1077                 if(empty($key))
1078                         return $string;
1079                 $buffer = $string;
1080                 $key = substr(md5($key),0,24);
1081             $iv = "password";
1082             return mcrypt_cbc(MCRYPT_3DES, $key, pack("H*", $buffer), MCRYPT_DECRYPT, $iv);
1083         }else{
1084                 return $string;
1085         }
1086 }
1087
1088 }
1089
1090 function canViewPath( $path, $base ){
1091   $path = realpath( $path );
1092   $base = realpath( $base );
1093   return 0 !== strncmp( $path, $base, strlen( $base ) );
1094 }
1095
1096
1097 /**
1098  * apply_values
1099  *
1100  * This function applies the given values to the bean object.  If it is a first time sync
1101  * then empty values will not be copied over.
1102  *
1103  * @param Mixed $seed Object representing SugarBean instance
1104  * @param Array $dataValues Array of fields/values to set on the SugarBean instance
1105  * @param boolean $firstSync Boolean indicating whether or not this is a first time sync
1106  */
1107 function apply_values($seed, $dataValues, $firstSync)
1108 {
1109     if(!$seed instanceof SugarBean || !is_array($dataValues))
1110     {
1111         return;
1112     }
1113
1114     foreach($dataValues as $field=>$value)
1115     {
1116         if($firstSync)
1117         {
1118             //If this is a first sync AND the value is not empty then we set it
1119             if(!empty($value))
1120             {
1121                 $seed->$field = $value;
1122             }
1123         } else {
1124             $seed->$field = $value;
1125         }
1126     }
1127 }
1128
1129 /*END HELPER*/
1130
1131 ?>