PageRenderTime 72ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/soap/SoapHelperFunctions.php

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