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-2012 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 ********************************************************************************/
40 * Retrieve field data for a provided SugarBean.
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
49 function get_field_list($value, $translate=true){
52 if(!empty($value->field_defs)){
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;
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'])) {
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);
71 if(!empty($var['dbType']) && $var['type'] == 'bool') {
72 $options_ret[] = get_name_value('type', $var['dbType']);
76 $entry['name'] = $var['name'];
77 $entry['type'] = $var['type'];
79 $entry['label'] = isset($var['vname']) ? translate($var['vname'], $value->module_dir) : $var['name'];
81 $entry['label'] = isset($var['vname']) ? $var['vname'] : $var['name'];
83 $entry['required'] = $required;
84 $entry['options'] = $options_ret;
85 if(isset($var['default'])) {
86 $entry['default_value'] = $var['default'];
89 $list[$var['name']] = $entry;
93 if($value->module_dir == 'Bugs'){
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);
101 if(isset($list['fixed_in_release'])){
102 $list['fixed_in_release']['type'] = 'enum';
103 $list['fixed_in_release']['options'] = $options_ret;
105 if(isset($list['release'])){
106 $list['release']['type'] = 'enum';
107 $list['release']['options'] = $options_ret;
109 if(isset($list['release_name'])){
110 $list['release_name']['type'] = 'enum';
111 $list['release_name']['options'] = $options_ret;
114 if($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];
121 $entry['name'] = $var['name'];
122 $entry['type'] = $var['type'];
124 $entry['label'] = isset($var['vname']) ? translate($var['vname'], $value->module_dir) : $var['name'];
126 $entry['label'] = isset($var['vname']) ? $var['vname'] : $var['name'];
128 $entry['required'] = $required;
129 $entry['options'] = array();
130 if(isset($var['default'])) {
131 $entry['default_value'] = $var['default'];
134 $list[$var['name']] = $entry;
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';
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';
146 if(isset($list['created_by'])) {
147 $list['created_by_name'] = $list['created_by'];
148 $list['created_by_name']['name'] = 'created_by_name';
153 function new_get_field_list($value, $translate=true) {
154 $module_fields = array();
155 $link_fields = array();
157 if(!empty($value->field_defs)){
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')) {
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'])) {
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);
179 if(!empty($var['dbType']) && $var['type'] == 'bool') {
180 $options_ret[] = get_name_value('type', $var['dbType']);
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;
193 $entry['label'] = isset($var['vname']) ? translate($var['vname'], $value->module_dir) : $var['name'];
195 $entry['label'] = isset($var['vname']) ? $var['vname'] : $var['name'];
197 $entry['required'] = $required;
198 $entry['options'] = $options_ret;
199 if(isset($var['default'])) {
200 $entry['default_value'] = $var['default'];
202 $module_fields[$var['name']] = $entry;
207 if($value->module_dir == 'Bugs'){
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);
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;
219 if(isset($module_fields['release'])){
220 $module_fields['release']['type'] = 'enum';
221 $module_fields['release']['options'] = $options_ret;
223 if(isset($module_fields['release_name'])){
224 $module_fields['release_name']['type'] = 'enum';
225 $module_fields['release_name']['options'] = $options_ret;
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';
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';
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';
242 return array('module_fields' => $module_fields, 'link_fields' => $link_fields);
245 function setFaultObject($errorObject) {
246 global $soap_server_object;
247 $soap_server_object->fault($errorObject->getFaultCode(), $errorObject->getName(), '', $errorObject->getDescription());
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);
257 global $beanList, $beanFiles;
258 if (!empty($module_name)) {
259 if(empty($beanList[$module_name])){
260 $errorObject->set_error('no_module');
261 setFaultObject($errorObject);
264 global $current_user;
265 if(!check_modules_access($current_user, $module_name, $access_level)){
266 $errorObject->set_error('no_access');
267 setFaultObject($errorObject);
274 function checkACLAccess($bean, $viewType, $errorObject, $error_key) {
275 if(!$bean->ACLAccess($viewType)){
276 $errorObject->set_error($error_key);
277 setFaultObject($errorObject);
283 function get_name_value($field,$value){
284 return array('name'=>$field, 'value'=>$value);
287 function get_user_module_list($user){
288 global $app_list_strings, $current_language, $beanList, $beanFiles;
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;
295 foreach($modInvisList as $invis){
296 $modules[$invis] = 'read_only';
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]);
305 $modules[$key] = 'read_only';
312 //Remove all modules that don't have a beanFiles entry associated with it
313 foreach($modules as $module_name=>$module)
315 if ( isset($beanList[$module_name]) ) {
316 $class_name = $beanList[$module_name];
317 if(empty($beanFiles[$class_name])) {
318 unset($modules[$module_name]);
321 unset($modules[$module_name]);
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);
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;
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
347 function get_name_value_list($value, $returnDomValue = false){
348 global $app_list_strings;
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);
354 if(isset($value->modified_by_name)) {
355 $list['modified_by_name'] = get_name_value('modified_by_name', $value->modified_by_name);
357 if(isset($value->created_by_name)) {
358 $list['created_by_name'] = get_name_value('created_by_name', $value->created_by_name);
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')){
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'))) {
370 if(isset($value->$var['name'])){
371 $val = $value->$var['name'];
372 $type = $var['type'];
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];
380 $list[$var['name']] = get_name_value($var['name'], $val);
388 function filter_fields($value, $fields) {
389 global $invalid_contact_fields;
390 $filterFields = array();
391 foreach($fields as $field){
392 if (is_array($invalid_contact_fields)) {
393 if (in_array($field, $invalid_contact_fields)) {
397 if (isset($value->field_defs[$field])) {
398 $var = $value->field_defs[$field];
399 if(isset($var['source']) && ($var['source'] != 'db' && $var['source'] != 'custom_fields') && $var['name'] != 'email1' && $var['name'] != 'email2' && (!isset($var['type'])|| $var['type'] != 'relate')) {
404 // No valid field should be caught by this quoting.
405 $filterFields[] = getValidDBName($field);
407 return $filterFields;
410 function get_name_value_list_for_fields($value, $fields) {
411 global $app_list_strings;
412 global $invalid_contact_fields;
415 if(!empty($value->field_defs)){
416 if(isset($value->assigned_user_name) && in_array('assigned_user_name', $fields)) {
417 $list['assigned_user_name'] = get_name_value('assigned_user_name', $value->assigned_user_name);
419 if(isset($value->modified_by_name) && in_array('modified_by_name', $fields)) {
420 $list['modified_by_name'] = get_name_value('modified_by_name', $value->modified_by_name);
422 if(isset($value->created_by_name) && in_array('created_by_name', $fields)) {
423 $list['created_by_name'] = get_name_value('created_by_name', $value->created_by_name);
426 $filterFields = filter_fields($value, $fields);
427 foreach($filterFields as $field){
428 $var = $value->field_defs[$field];
429 if(isset($value->$var['name'])){
430 $val = $value->$var['name'];
431 $type = $var['type'];
433 if(strcmp($type, 'date') == 0){
434 $val = substr($val, 0, 10);
435 }elseif(strcmp($type, 'enum') == 0 && !empty($var['options'])){
436 $val = $app_list_strings[$var['options']][$val];
439 $list[$var['name']] = get_name_value($var['name'], $val);
448 function array_get_name_value_list($array){
450 foreach($array as $name=>$value){
452 $list[$name] = get_name_value($name, $value);
458 function array_get_name_value_lists($array){
460 foreach($array as $name=>$value){
462 if(is_array($value)){
463 $tmp_value = array();
464 foreach($value as $k=>$v){
465 $tmp_value[] = get_name_value($k, $v);
468 $list[] = get_name_value($name, $tmp_value);
473 function name_value_lists_get_array($list){
475 foreach($list as $key=>$value){
476 if(isset($value['value']) && isset($value['name'])){
477 if(is_array($value['value'])){
478 $array[$value['name']]=array();
479 foreach($value['value'] as $v){
480 $array[$value['name']][$v['name']]=$v['value'];
483 $array[$value['name']]=$value['value'];
490 function array_get_return_value($array, $module){
492 return Array('id'=>$array['id'],
493 'module_name'=> $module,
494 'name_value_list'=>array_get_name_value_list($array)
498 function get_return_value_for_fields($value, $module, $fields) {
499 global $module_name, $current_user;
500 $module_name = $module;
501 if($module == 'Users' && $value->id != $current_user->id){
502 $value->user_hash = '';
504 return Array('id'=>$value->id,
505 'module_name'=> $module,
506 'name_value_list'=>get_name_value_list_for_fields($value, $fields)
510 function getRelationshipResults($bean, $link_field_name, $link_module_fields) {
511 global $beanList, $beanFiles;
512 $bean->load_relationship($link_field_name);
513 if (isset($bean->$link_field_name)) {
514 // get the query object for this link field
515 $query_array = $bean->$link_field_name->getQuery(true,array(),0,'',true);
517 $params['joined_tables'] = $query_array['join_tables'];
519 // get the related module name and instantiate a bean for that.
520 $submodulename = $bean->$link_field_name->getRelatedModuleName();
521 $submoduleclass = $beanList[$submodulename];
522 require_once($beanFiles[$submoduleclass]);
524 $submodule = new $submoduleclass();
525 $filterFields = filter_fields($submodule, $link_module_fields);
526 $relFields = $bean->$link_field_name->getRelatedFields();
529 if(!empty($relFields)){
530 foreach($link_module_fields as $field){
531 if(!empty($relFields[$field])){
532 $roleSelect .= ', ' . $query_array['join_tables'][0] . '.'. $field;
537 $subquery = $submodule->create_new_list_query('','',$filterFields,$params, 0,'', true,$bean);
538 $query = $subquery['select'].$roleSelect . $subquery['from'].$query_array['join']. $subquery['where'];
540 $result = $submodule->db->query($query, true);
542 while($row = $submodule->db->fetchByAssoc($result)) {
545 return array('rows' => $list, 'fields_set_on_rows' => $filterFields);
552 function get_return_value_for_link_fields($bean, $module, $link_name_to_value_fields_array) {
553 global $module_name, $current_user;
554 $module_name = $module;
555 if($module == 'Users' && $bean->id != $current_user->id){
556 $bean->user_hash = '';
559 if (empty($link_name_to_value_fields_array) || !is_array($link_name_to_value_fields_array)) {
563 $link_output = array();
564 foreach($link_name_to_value_fields_array as $link_name_value_fields) {
565 if (!is_array($link_name_value_fields) || !isset($link_name_value_fields['name']) || !isset($link_name_value_fields['value'])) {
568 $link_field_name = $link_name_value_fields['name'];
569 $link_module_fields = $link_name_value_fields['value'];
570 if (is_array($link_module_fields) && !empty($link_module_fields)) {
571 $result = getRelationshipResults($bean, $link_field_name, $link_module_fields);
573 $link_output[] = array('name' => $link_field_name, 'records' => array());
576 $list = $result['rows'];
577 $filterFields = $result['fields_set_on_rows'];
580 foreach($list as $row) {
581 $nameValueArray = array();
582 foreach ($filterFields as $field) {
583 $nameValue = array();
584 if (isset($row[$field])) {
585 $nameValue['name'] = $field;
586 $nameValue['value'] = $row[$field];
587 $nameValueArray[] = $nameValue;
590 $rowArray[] = $nameValueArray;
592 $link_output[] = array('name' => $link_field_name, 'records' => $rowArray);
601 * @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).
602 * @param String $module_id -- The ID of the bean in the specified module
603 * @param String $link_field_name - The relationship name for which to create realtionships.
604 * @param Array $related_ids -- The array of ids for which we want to create relationships
605 * @return true on success, false on failure
607 function new_handle_set_relationship($module_name, $module_id, $link_field_name, $related_ids) {
608 global $beanList, $beanFiles;
610 if(empty($beanList[$module_name])) {
613 $class_name = $beanList[$module_name];
614 require_once($beanFiles[$class_name]);
615 $mod = new $class_name();
616 $mod->retrieve($module_id);
617 if(!$mod->ACLAccess('DetailView')){
621 foreach($related_ids as $ids) {
622 $GLOBALS['log']->debug("ids = " . $ids );
625 if ($mod->load_relationship($link_field_name)) {
626 $mod->$link_field_name->add($related_ids);
633 function new_handle_set_entries($module_name, $name_value_lists, $select_fields = FALSE) {
634 global $beanList, $beanFiles, $app_list_strings;
635 global $current_user;
637 $ret_values = array();
639 $class_name = $beanList[$module_name];
640 require_once($beanFiles[$class_name]);
643 $total = sizeof($name_value_lists);
644 foreach($name_value_lists as $name_value_list){
645 $seed = new $class_name();
647 $seed->update_vcal = false;
648 foreach($name_value_list as $value){
649 if($value['name'] == 'id'){
650 $seed->retrieve($value['value']);
655 foreach($name_value_list as $value) {
656 $val = $value['value'];
657 if($seed->field_name_map[$value['name']]['type'] == 'enum'){
658 $vardef = $seed->field_name_map[$value['name']];
659 if(isset($app_list_strings[$vardef['options']]) && !isset($app_list_strings[$vardef['options']][$value]) ) {
660 if ( in_array($val,$app_list_strings[$vardef['options']]) ){
661 $val = array_search($val,$app_list_strings[$vardef['options']]);
665 $seed->$value['name'] = $val;
668 if($count == $total){
669 $seed->update_vcal = false;
673 //Add the account to a contact
674 if($module_name == 'Contacts'){
675 $GLOBALS['log']->debug('Creating Contact Account');
676 add_create_account($seed);
677 $duplicate_id = check_for_duplicate_contacts($seed);
678 if($duplicate_id == null){
679 if($seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
681 if($seed->deleted == 1){
682 $seed->mark_deleted($seed->id);
688 //since we found a duplicate we should set the sync flag
689 if( $seed->ACLAccess('Save')){
690 $seed = new $class_name();
691 $seed->id = $duplicate_id;
692 $seed->contacts_users_id = $current_user->id;
694 $ids[] = $duplicate_id;//we have a conflict
698 else if($module_name == 'Meetings' || $module_name == 'Calls'){
699 //we are going to check if we have a meeting in the system
700 //with the same outlook_id. If we do find one then we will grab that
702 if( $seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
703 if(empty($seed->id) && !isset($seed->id)){
704 if(!empty($seed->outlook_id) && isset($seed->outlook_id)){
705 //at this point we have an object that does not have
706 //the id set, but does have the outlook_id set
707 //so we need to query the db to find if we already
708 //have an object with this outlook_id, if we do
709 //then we can set the id, otherwise this is a new object
711 $query = $seed->table_name.".outlook_id = '".$GLOBALS['db']->quote($seed->outlook_id)."'";
712 $response = $seed->get_list($order_by, $query, 0,-1,-1,0);
713 $list = $response['list'];
714 if(count($list) > 0){
715 foreach($list as $value)
717 $seed->id = $value->id;
729 if( $seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))){
735 // if somebody is calling set_entries_detail() and wants fields returned...
736 if ($select_fields !== FALSE) {
737 $ret_values[$count] = array();
739 foreach ($select_fields as $select_field) {
740 if (isset($seed->$select_field)) {
741 $ret_values[$count][] = get_name_value($select_field, $seed->$select_field);
747 // handle returns for set_entries_detail() and set_entries()
748 if ($select_fields !== FALSE) {
750 'name_value_lists' => $ret_values,
760 function get_return_value($value, $module, $returnDomValue = false){
761 global $module_name, $current_user;
762 $module_name = $module;
763 if($module == 'Users' && $value->id != $current_user->id){
764 $value->user_hash = '';
766 return Array('id'=>$value->id,
767 'module_name'=> $module,
768 'name_value_list'=>get_name_value_list($value, $returnDomValue)
773 function get_encoded_Value($value) {
775 $value = htmlspecialchars($value);
777 // bug 47683, special characters cause OPI parser to fail
778 // htmlspecialchars or htmlentities does not convert control characters
779 // so we have to convert them by ourselves.
780 // Per http://en.wikipedia.org/wiki/XML#Valid_characters
781 // 00 is not allowed in XML
782 // 09, 0A, 0D and 85 does not require escaping
783 // CDATA is also needed
784 $conv_table = array();
785 $conv_table["\x00"] = ""; // not allowed in XML so remove it
786 $conv_table["\x01"] = "";
787 $conv_table["\x02"] = "";
788 $conv_table["\x03"] = "";
789 $conv_table["\x04"] = "";
790 $conv_table["\x05"] = "";
791 $conv_table["\x06"] = "";
792 $conv_table["\x07"] = "";
793 $conv_table["\x08"] = "";
794 //$conv_table["\x09"] = "	";
795 //$conv_table["\x0A"] = "
";
796 $conv_table["\x0B"] = "";
797 $conv_table["\x0C"] = "";
798 //$conv_table["\x0D"] = "
";
799 $conv_table["\x0E"] = "";
800 $conv_table["\x0F"] = "";
801 $conv_table["\x10"] = "";
802 $conv_table["\x11"] = "";
803 $conv_table["\x12"] = "";
804 $conv_table["\x13"] = "";
805 $conv_table["\x14"] = "";
806 $conv_table["\x15"] = "";
807 $conv_table["\x16"] = "";
808 $conv_table["\x17"] = "";
809 $conv_table["\x18"] = "";
810 $conv_table["\x19"] = "";
811 $conv_table["\x1A"] = "";
812 $conv_table["\x1B"] = "";
813 $conv_table["\x1C"] = "";
814 $conv_table["\x1D"] = "";
815 $conv_table["\x1E"] = "";
816 $conv_table["\x1F"] = "";
817 $conv_table["\x80"] = "€";
818 $conv_table["\x81"] = "";
819 $conv_table["\x82"] = "‚";
820 $conv_table["\x83"] = "ƒ";
821 $conv_table["\x84"] = "„";
822 $conv_table["\x85"] = "…";
823 $conv_table["\x86"] = "†";
824 $conv_table["\x87"] = "‡";
825 $conv_table["\x88"] = "ˆ";
826 $conv_table["\x89"] = "‰";
827 $conv_table["\x8A"] = "Š";
828 $conv_table["\x8B"] = "‹";
829 $conv_table["\x8C"] = "Œ";
830 $conv_table["\x8D"] = "";
831 $conv_table["\x8E"] = "Ž";
832 $conv_table["\x8F"] = "";
833 $conv_table["\x90"] = "";
834 $conv_table["\x91"] = "‘";
835 $conv_table["\x92"] = "’";
836 $conv_table["\x93"] = "“";
837 $conv_table["\x94"] = "”";
838 $conv_table["\x95"] = "•";
839 $conv_table["\x96"] = "–";
840 $conv_table["\x97"] = "—";
841 $conv_table["\x98"] = "˜";
842 $conv_table["\x99"] = "™";
843 $conv_table["\x9A"] = "š";
844 $conv_table["\x9B"] = "›";
845 $conv_table["\x9C"] = "œ";
846 $conv_table["\x9D"] = "";
847 $conv_table["\x9E"] = "ž";
848 $conv_table["\x9F"] = "Ÿ";
851 $value = strtr($value, $conv_table);
853 // wrap it with CDATA
854 $value = '<value><![CDATA['.$value.']]></value>';
859 function get_name_value_xml($val, $module_name){
861 $xml .= '<id>'.$val['id'].'</id>';
862 $xml .= '<module>'.$module_name.'</module>';
863 $xml .= '<name_value_list>';
864 foreach($val['name_value_list'] as $name=>$nv){
865 $xml .= '<name_value>';
866 $xml .= '<name>'.htmlspecialchars($nv['name']).'</name>';
867 $xml .= get_encoded_Value($nv['value']);
868 $xml .= '</name_value>';
870 $xml .= '</name_value_list>';
875 function new_get_return_module_fields($value, $module, $translate=true){
877 $module_name = $module;
878 $result = new_get_field_list($value, $translate);
879 return Array('module_name'=>$module,
880 'module_fields'=> $result['module_fields'],
881 'link_fields'=> $result['link_fields'],
885 function get_return_module_fields($value, $module, $error, $translate=true){
887 $module_name = $module;
888 return Array('module_name'=>$module,
889 'module_fields'=> get_field_list($value, $translate),
890 'error'=>get_name_value_list($value)
894 function get_return_error_value($error_num, $error_name, $error_description){
895 return Array('number'=>$error_num,
896 'name'=> $error_name,
897 'description'=> $error_description
901 function filter_field_list(&$field_list, $select_fields, $module_name){
902 return filter_return_list($field_list, $select_fields, $module_name);
907 * Filter the results of a list query. Limit the fields returned.
909 * @param Array $output_list -- The array of list data
910 * @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.
911 * @param String $module_name -- The name of the module this being worked on
912 * @return The filtered array of list data.
914 function filter_return_list(&$output_list, $select_fields, $module_name){
916 for($sug = 0; $sug < sizeof($output_list) ; $sug++){
917 if($module_name == 'Contacts'){
918 global $invalid_contact_fields;
919 if(is_array($invalid_contact_fields)){
920 foreach($invalid_contact_fields as $name=>$val){
921 unset($output_list[$sug]['field_list'][$name]);
922 unset($output_list[$sug]['name_value_list'][$name]);
928 if( !empty($output_list[$sug]['name_value_list']) && is_array($output_list[$sug]['name_value_list']) && !empty($select_fields) && is_array($select_fields)){
929 foreach($output_list[$sug]['name_value_list'] as $name=>$value)
930 if(!in_array($value['name'], $select_fields)){
931 unset($output_list[$sug]['name_value_list'][$name]);
932 unset($output_list[$sug]['field_list'][$name]);
939 function login_success(){
940 global $current_language, $sugar_config, $app_strings, $app_list_strings;
941 $current_language = $sugar_config['default_language'];
942 $app_strings = return_application_language($current_language);
943 $app_list_strings = return_app_list_strings_language($current_language);
948 * Given an account_name, either create the account or assign to a contact.
950 function add_create_account($seed)
952 global $current_user;
953 $account_name = $seed->account_name;
954 $account_id = $seed->account_id;
955 $assigned_user_id = $current_user->id;
957 // check if it already exists
958 $focus = new Account();
959 if( $focus->ACLAccess('Save')){
960 $class = get_class($seed);
961 $temp = new $class();
962 $temp->retrieve($seed->id);
963 if ((! isset($account_name) || $account_name == ''))
967 if (!isset($seed->accounts)){
968 $seed->load_relationship('accounts');
971 if($seed->account_name == '' && isset($temp->account_id)){
972 $seed->accounts->delete($seed->id, $temp->account_id);
980 $query = "select id, deleted from {$focus->table_name} WHERE name='".$seed->db->quote($account_name)."'";
981 $query .=" ORDER BY deleted ASC";
982 $result = $seed->db->query($query, true);
984 $row = $seed->db->fetchByAssoc($result, false);
986 // we found a row with that id
987 if (isset($row['id']) && $row['id'] != -1)
989 // if it exists but was deleted, just remove it entirely
990 if ( isset($row['deleted']) && $row['deleted'] == 1)
992 $query2 = "delete from {$focus->table_name} WHERE id='". $seed->db->quote($row['id'])."'";
993 $result2 = $seed->db->query($query2, true);
995 // else just use this id to link the contact to the account
998 $focus->id = $row['id'];
1002 // if we didnt find the account, so create it
1003 if (! isset($focus->id) || $focus->id == '')
1005 $focus->name = $account_name;
1007 if ( isset($assigned_user_id))
1009 $focus->assigned_user_id = $assigned_user_id;
1010 $focus->modified_user_id = $assigned_user_id;
1015 if($seed->accounts != null && $temp->account_id != null && $temp->account_id != $focus->id){
1016 $seed->accounts->delete($seed->id, $temp->account_id);
1019 if(isset($focus->id) && $focus->id != ''){
1020 $seed->account_id = $focus->id;
1025 function check_for_duplicate_contacts($seed){
1027 if(isset($seed->id)){
1031 $trimmed_email = trim($seed->email1);
1032 $trimmed_email2 = trim($seed->email2);
1033 $trimmed_last = trim($seed->last_name);
1034 $trimmed_first = trim($seed->first_name);
1035 if(!empty($trimmed_email) || !empty($trimmed_email2)){
1037 //obtain a list of contacts which contain the same email address
1038 $contacts = $seed->emailAddress->getBeansByEmailAddress($trimmed_email);
1039 $contacts2 = $seed->emailAddress->getBeansByEmailAddress($trimmed_email2);
1040 $contacts = array_merge($contacts, $contacts2);
1041 if(count($contacts) == 0){
1044 foreach($contacts as $contact){
1045 if(!empty($trimmed_last) && strcmp($trimmed_last, $contact->last_name) == 0){
1046 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)){
1047 //bug: 39234 - check if the account names are the same
1048 //if the incoming contact's account_name is empty OR it is not empty and is the same
1049 //as an existing contact's account name, then find the match.
1050 $contact->load_relationship('accounts');
1051 if(empty($seed->account_name) || strcmp($seed->account_name, $contact->account_name) == 0){
1052 $GLOBALS['log']->info('End: SoapHelperWebServices->check_for_duplicate_contacts - duplicte found ' . $contact->id);
1053 return $contact->id;
1061 //This section of code is executed if no emails are supplied in the $seed instance
1063 //This query is looking for the id of Contact records that do not have a primary email address based on the matching
1064 //first and last name and the record being not deleted. If any such records are found we will take the first one and assume
1065 //that it is the duplicate record
1066 $query = "SELECT c.id as id FROM contacts c
1067 LEFT OUTER JOIN email_addr_bean_rel eabr ON eabr.bean_id = c.id
1068 WHERE c.first_name = '{$trimmed_first}' AND c.last_name = '{$trimmed_last}' AND c.deleted = 0 AND eabr.id IS NULL";
1070 //Apply the limit query filter to this since we only need the first record
1071 $result = $GLOBALS['db']->getOne($query);
1072 return !empty($result) ? $result : null;
1077 * Given a client version and a server version, determine if the right hand side(server version) is greater
1079 * @param left the client sugar version
1080 * @param right the server version
1082 * return true if the server version is greater or they are equal
1083 * false if the client version is greater
1085 function is_server_version_greater($left, $right){
1086 if(count($left) == 0 && count($right) == 0){
1089 else if(count($left) == 0 || count($right) == 0){
1092 else if($left[0] == $right[0]){
1094 array_shift($right);
1095 return is_server_version_greater($left, $right);
1097 else if($left[0] < $right[0]){
1104 function getFile( $zip_file, $file_in_zip ){
1105 $base_upgrade_dir = sugar_cached("/upgrades");
1106 $base_tmp_upgrade_dir = "$base_upgrade_dir/temp";
1107 $my_zip_dir = mk_temp_dir( $base_tmp_upgrade_dir );
1108 unzip_file( $zip_file, $file_in_zip, $my_zip_dir );
1109 return( "$my_zip_dir/$file_in_zip" );
1112 function getManifest( $zip_file ){
1113 ini_set("max_execution_time", "3600");
1114 return( getFile( $zip_file, "manifest.php" ) );
1117 if(!function_exists("get_encoded")){
1118 /*HELPER FUNCTIONS*/
1119 function get_encoded($object){
1120 return base64_encode(serialize($object));
1122 function get_decoded($object){
1123 return unserialize(base64_decode($object));
1128 * decrypt a string use the TripleDES algorithm. This meant to be
1129 * modified if the end user chooses a different algorithm
1131 * @param $string - the string to decrypt
1133 * @return a decrypted string if we can decrypt, the original string otherwise
1135 function decrypt_string($string){
1136 if(function_exists('mcrypt_cbc')){
1138 $focus = new Administration();
1139 $focus->retrieveSettings();
1141 if(!empty($focus->settings['ldap_enc_key'])){
1142 $key = $focus->settings['ldap_enc_key'];
1147 $key = substr(md5($key),0,24);
1149 return mcrypt_cbc(MCRYPT_3DES, $key, pack("H*", $buffer), MCRYPT_DECRYPT, $iv);
1157 function canViewPath( $path, $base ){
1158 $path = realpath( $path );
1159 $base = realpath( $base );
1160 return 0 !== strncmp( $path, $base, strlen( $base ) );
1167 * This function applies the given values to the bean object. If it is a first time sync
1168 * then empty values will not be copied over.
1170 * @param Mixed $seed Object representing SugarBean instance
1171 * @param Array $dataValues Array of fields/values to set on the SugarBean instance
1172 * @param boolean $firstSync Boolean indicating whether or not this is a first time sync
1174 function apply_values($seed, $dataValues, $firstSync)
1176 if(!$seed instanceof SugarBean || !is_array($dataValues))
1181 foreach($dataValues as $field=>$value)
1185 //If this is a first sync AND the value is not empty then we set it
1188 $seed->$field = $value;
1191 $seed->$field = $value;