Issues (4069)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

soap/SoapRelationshipHelper.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
4
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
5
/*********************************************************************************
6
 * SugarCRM Community Edition is a customer relationship management program developed by
7
 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
8
9
 * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
10
 * Copyright (C) 2011 - 2014 Salesagility Ltd.
11
 *
12
 * This program is free software; you can redistribute it and/or modify it under
13
 * the terms of the GNU Affero General Public License version 3 as published by the
14
 * Free Software Foundation with the addition of the following permission added
15
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
16
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
17
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
18
 *
19
 * This program is distributed in the hope that it will be useful, but WITHOUT
20
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
22
 * details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License along with
25
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
26
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27
 * 02110-1301 USA.
28
 *
29
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
30
 * SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
31
 *
32
 * The interactive user interfaces in modified source and object code versions
33
 * of this program must display Appropriate Legal Notices, as required under
34
 * Section 5 of the GNU Affero General Public License version 3.
35
 *
36
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
37
 * these Appropriate Legal Notices must retain the display of the "Powered by
38
 * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
39
 * reasonably feasible for  technical reasons, the Appropriate Legal Notices must
40
 * display the words  "Powered by SugarCRM" and "Supercharged by SuiteCRM".
41
 ********************************************************************************/
42
43
require_once('soap/SoapError.php');
44
45
function check_for_relationship($relationships, $module){
46
	foreach($relationships as $table=>$rel){
47
		if( $rel['rhs_key'] == $module){
48
			return $table;
49
50
		}
51
	}
52
	return false;
53
}
54
55
/*
56
 * takes in two modules and returns the relationship information about them
57
 *
58
 */
59
60
function retrieve_relationships_properties($module_1, $module_2, $relationship_name = ""){
61
62
	$rs = new Relationship();
63
	$query =  "SELECT * FROM $rs->table_name WHERE ((lhs_module = '".$rs->db->quote($module_1)."' AND rhs_module='".$rs->db->quote($module_2)."') OR (lhs_module = '".$rs->db->quote($module_2)."' AND rhs_module='".$rs->db->quote($module_1)."'))";
64
	if(!empty($relationship_name) && isset($relationship_name)){
65
		$query .= " AND relationship_name = '".$rs->db->quote($relationship_name)."'";
66
	}
67
	$result = $rs->db->query($query);
68
69
	return $rs->db->fetchByAssoc($result);
70
}
71
72
73
74
75
/*
76
 * retireves relationships between two modules
77
 * This will return all viewable relationships between two modules
78
 * module_query is a filter on the first module
79
 * related_module_query is a filter on the second module
80
 * relationship_query is a filter on the relationship between them
81
 * show_deleted is if deleted items should be shown or not
82
 *
83
 */
84
function retrieve_relationships($module_name,  $related_module, $relationship_query, $show_deleted, $offset, $max_results){
85
	global  $beanList, $beanFiles, $dictionary, $current_user;
86
87
	$error = new SoapError();
88
	$result_list = array();
89
	if(empty($beanList[$module_name]) || empty($beanList[$related_module])){
90
91
		$error->set_error('no_module');
92
		return array('result'=>$result_list, 'error'=>$error->get_soap_array());
93
	}
94
95
	$result = retrieve_relationship_query($module_name,  $related_module, $relationship_query, $show_deleted, $offset, $max_results);
96
97
	if(empty($result['module_1'])){
98
99
		$error->set_error('no_relationship_support');
100
		return array('result'=>$result_list, 'error'=>$error->get_soap_array());
101
	}
102
	$query = $result['query'];
103
	$module_1 = $result['module_1'];
104
	$table = $result['join_table'];
105
106
	$class_name = $beanList[$module_1];
107
	require_once($beanFiles[$class_name]);
108
	$mod = new $class_name();
109
110
	$count_query = str_replace('rt.*', 'count(*)', $query);
111
	$result = $mod->db->query($count_query);
112
	$row = $mod->db->fetchByAssoc($result);
113
	$total_count = $row['count(*)'];
114
115
	if($max_results != '-99'){
116
		$result = $mod->db->limitQuery($query, $offset, $max_results);
117
	}else{
118
		$result = $mod->db->query($query);
119
	}
120
	while($row = $mod->db->fetchByAssoc($result)){
121
122
		$result_list[] = $row;
123
	}
124
125
	return array('table_name'=>$table, 'result'=>$result_list, 'total_count'=>$total_count, 'error'=>$error->get_soap_array());
126
}
127
128
/*
129
 * retrieve_modified_relationships
130
 *
131
 * This method retrieves modified relationships between two modules
132
 * This will return all viewable relationships between two modules
133
 *
134
 * @param $module_name String value of the module on the left hand side of relationship
135
 * @param related_module String value of the module on the right hand side of relationship
136
 * @param relationship_query SQL String used to query for the relationships
137
 * @show_deleted boolean value indicating whether or not deleted items should be shown (IGNORED)
138
 * @offset integer value indicating the starting offset of results to return
139
 * @max_results integer value indicating the maximum number of results to return
140
 * @select_fields Mixed Array indicating the select fields used in the query to return in results
141
 * @relationship_name String value of the relationship name as defined in the relationships table to be used in retrieving the relationship information
142
 * @return Mixed Array of results with the following delta/value information:
143
 *         table_name String value of the table name queried for the results
144
 *         result Mixed Array of the results.  Each entry in the Array contains an Array of key/value pairs from the select_fields parameter
145
 *         total_count integer value indicating the total count of results from the query
146
 *         error Mixed Array containing the SOAP errors if found, empty otherwise
147
 *
148
 */
149
function retrieve_modified_relationships($module_name, $related_module, $relationship_query, $show_deleted, $offset, $max_results, $select_fields = array(), $relationship_name = ''){
150
151
    global  $beanList, $beanFiles, $dictionary, $current_user;
152
	$error = new SoapError();
153
	$result_list = array();
154
	if(empty($beanList[$module_name]) || empty($beanList[$related_module])){
155
156
		$error->set_error('no_module');
157
		return array('result'=>$result_list, 'error'=>$error->get_soap_array());
158
	}
159
160
	$row = retrieve_relationships_properties($module_name, $related_module, $relationship_name);
161
162
	if(empty($row)){
163
164
		$error->set_error('no_relationship_support');
165
		return array('result'=>$result_list, 'error'=>$error->get_soap_array());
166
	}
167
168
	$table = $row['join_table'];
169
	$has_join = true;
170
	if(empty($table)){
171
		//return array('table_name'=>$table, 'result'=>$result_list, 'error'=>$error->get_soap_array());
172
		$table = $row['rhs_table'];
173
		$module_1 = $row['lhs_module'];
174
		$mod_key = $row['lhs_key'];
175
		$module_2 = $row['rhs_module'];
176
		$mod2_key = $row['rhs_key'];
177
		$has_join = false;
178
	}
179
	else{
180
		$module_1 = $row['lhs_module'];
181
		$mod_key = $row['join_key_lhs'];
182
		$module_2 = $row['rhs_module'];
183
		$mod2_key = $row['join_key_rhs'];
184
	}
185
186
187
188
	$class_name = $beanList[$module_1];
189
	require_once($beanFiles[$class_name]);
190
	$mod = new $class_name();
191
192
	$mod2_name = $beanList[$module_2];
193
	require_once($beanFiles[$mod2_name]);
194
	$mod2 = new $mod2_name();
195
	$table_alias = 'rt';
196
	if($has_join == false){
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
197
		$table_alias = 'm1';
198
	}
199
200
	if(isset($select_fields) && !empty($select_fields)){
201
		$index = 0;
202
		$field_select ='';
203
204
		foreach($select_fields as $field){
205
			if($field == "id"){
206
				$field_select .= "DISTINCT m1.id";
207
			} else {
208
			    $parts = explode(' ', $field);
209
			    $alias = '';
210
			    if(count($parts) > 1) {
211
			        // have aliases: something like "blah.blah blah"
212
                    $alias = array_pop($parts);
213
                    $field = array_pop($parts); // will check for . further down
214
			    }
215
			    if($alias == "email1") {
216
                    // special case for primary emails
217
                    $field_select .= "(SELECT email_addresses.email_address FROM {$mod->table_name}
218
                    	LEFT JOIN  email_addr_bean_rel ON {$mod->table_name}.id = email_addr_bean_rel.bean_id
219
                    		AND email_addr_bean_rel.bean_module='{$mod->module_dir}'
220
                    		AND email_addr_bean_rel.deleted=0 AND email_addr_bean_rel.primary_address=1
221
                    	LEFT JOIN email_addresses ON email_addresses.id = email_addr_bean_rel.email_address_id Where {$mod->table_name}.id = m1.ID) email1";
222
			    } elseif($alias == "email2") {
223
                    // special case for non-primary emails
224
                    // FIXME: This is not a DB-safe code. Does not work on SQL Server & Oracle.
225
                    // Using dirty hack here.
226
                    $field_select .= "(SELECT email_addresses.email_address FROM {$mod->table_name}
227
                    	LEFT JOIN  email_addr_bean_rel on {$mod->table_name}.id = email_addr_bean_rel.bean_id
228
                    		AND email_addr_bean_rel.bean_module='{$mod->module_dir}' AND email_addr_bean_rel.deleted=0
229
                    		AND email_addr_bean_rel.primary_address!=1
230
                    	LEFT JOIN email_addresses ON email_addresses.id = email_addr_bean_rel.email_address_id Where {$mod->table_name}.id = m1.ID limit 1) email2";
231
			    } else {
232
                    if(strpos($field, ".") == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($field, '.') of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
233
                        // no dot - field for m1
234
                        $fieldname = "m1.".$mod->db->getValidDBName($field);
235
                    } else {
236
                        // There is a dot in here somewhere.
237
                        list($table_part,$field_part) = explode('.',$field);
238
                        $fieldname = $mod->db->getValidDBName($table_part).".".$mod->db->getValidDBName($field_part);
239
    			    }
240
    			    $field_select .= $fieldname;
241
    			    if(!empty($alias)) {
242
    			        $field_select .= " ".$mod->db->getValidDBName($alias);
243
    			    }
244
			    }
245
			}
246
			if($index < (count($select_fields) - 1))
247
			{
248
				$field_select .= ",";
249
				$index++;
250
			}
251
		}//end foreach
252
		$query = "SELECT $field_select FROM $table $table_alias ";
253
	}
254
	else{
255
		$query = "SELECT rt.* FROM  $table $table_alias ";
256
	}
257
258
	if($has_join == false){
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
259
		$query .= " inner join $mod->table_name m2 on $table_alias.$mod2_key = m2.id AND m2.id = '$current_user->id'";
260
	}
261
	else{
262
		$query .= " inner join $mod->table_name m1 on rt.$mod_key = m1.id ";
263
		$query .= " inner join $mod2->table_name m2 on rt.$mod2_key = m2.id AND m2.id = '$current_user->id'";
264
	}
265
266
	if(!empty($relationship_query)){
267
		$query .= ' WHERE ' . string_format($relationship_query, array($table_alias));
268
	}
269
270
	if($max_results != '-99'){
271
		$result = $mod->db->limitQuery($query, $offset, $max_results);
272
	}else{
273
		$result = $mod->db->query($query);
274
	}
275
	while($row = $mod->db->fetchByAssoc($result)){
276
		$result_list[] = $row;
277
	}
278
279
    $total_count = !empty($result_list) ? count($result_list) : 0;
280
	return array('table_name'=>$table, 'result'=>$result_list, 'total_count'=>$total_count, 'error'=>$error->get_soap_array());
281
}
282
283
function server_save_relationships($list, $from_date, $to_date){
284
	require_once('include/utils/db_utils.php');
285
	global  $beanList, $beanFiles;
286
	$from_date = db_convert("'".$GLOBALS['db']->quote($from_date)."'", 'datetime');
0 ignored issues
show
Deprecated Code introduced by
The function db_convert() has been deprecated with message: use DBManager::convert() instead.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
287
	$to_date = db_convert("'".$GLOBALS['db']->quote($to_date)."'", 'datetime');
0 ignored issues
show
Deprecated Code introduced by
The function db_convert() has been deprecated with message: use DBManager::convert() instead.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
288
	global $sugar_config;
289
	$db = DBManagerFactory::getInstance();
290
291
	$ids = array();
292
	$add = 0;
293
	$modify = 0;
294
	$deleted = 0;
295
296
	foreach($list as $record)
297
	{
298
		$insert = '';
299
		$insert_values = '';
300
		$update = '';
301
		$select_values	= '';
302
		$args = array();
303
304
		$id = $record['id'];
305
306
		$table_name = $record['module_name'];
307
		$resolve = 1;
308
309
		foreach($record['name_value_list'] as $name_value){
310
			$name = $GLOBALS['db']->quote($name_value['name']);
311
312
			if($name == 'date_modified'){
313
                $value = $to_date;
314
			}else{
315
                $value = db_convert("'".$GLOBALS['db']->quote($name_value['value'])."'", 'varchar');
0 ignored issues
show
Deprecated Code introduced by
The function db_convert() has been deprecated with message: use DBManager::convert() instead.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
316
			}
317
			if($name != 'resolve'){
318
			if(empty($insert)){
319
				$insert = '('	.$name;
320
				$insert_values = '('	.$value;
321
				if($name != 'date_modified' && $name != 'id' ){
322
					$select_values = $name ."=$value";
323
				}
324
				if($name != 'id'){
325
					$update = $name ."=$value";
326
				}
327
			}else{
328
				$insert .= ', '	.$name;
329
				$insert_values .= ', '	.$value;
330
				if(empty($update)){
331
					$update .= $name."=$value";
332
				}else{
333
					$update .= ','.$name."=$value";
334
				}
335
336
				if($name != 'date_modified' && $name != 'id' ){
337
					if(empty($select_values)){
338
						$select_values = $name ."=$value";
339
					}else{
340
						$select_values .= ' AND '.$name ."=$value";
341
					}
342
				}
343
			}
344
			}else{
345
				$resolve = $value;
346
			}
347
348
349
350
351
		}
352
		//ignore resolve for now server always wins
353
		$resolve = 1;
354
		$insert = "INSERT INTO $table_name $insert) VALUES $insert_values)";
355
		$update = "UPDATE $table_name SET $update WHERE id=";
356
		$delete = "DELETE FROM $table_name WHERE id=";
357
		$select_by_id_date = "SELECT id FROM $table_name WHERE id ='".$GLOBALS['db']->quote($id)."' AND date_modified > $from_date AND date_modified<= $to_date";
358
		$select_by_id = "SELECT id FROM $table_name WHERE id ='".$GLOBALS['db']->quote($id)."'";
359
		$select_by_values = "SELECT id FROM $table_name WHERE $select_values";
360
		$updated = false;
361
362
363
		$result = $db->query($select_by_id_date);
364
		//see if we have a matching id in the date_range
365
		if(!($row = $db->fetchByAssoc($result))){
366
			//if not lets check if we have one that matches the values
367
368
			$result = $db->query($select_by_values);
369
			if(!($row = $db->fetchByAssoc($result))){
370
371
				$result = $db->query($select_by_id);
372
				if($row = $db->fetchByAssoc($result)){
373
374
					$db->query($update ."'".$GLOBALS['db']->quote($row['id'])."'" );
375
					$ids[] = $row['id'];
376
					$modify++;
377
				}else{
378
					$db->query($insert);
379
					$add++;
380
					$ids[] = $row['id'];
381
				}
382
			}
383
	}
384
385
	}
386
	return array('add'=>$add, 'modify'=>$modify, 'ids'=>$ids);
387
}
388
389
/*
390
 *
391
 * gets the from statement from a query without the order by and without the select
392
 *
393
 */
394
function get_from_statement($query){
395
	$query = explode('FROM', $query);
396
	if(sizeof($query) == 1){
397
		$query = explode('from', $query[0]);
398
	}
399
	$query = explode( 'ORDER BY',$query[1]);
400
401
	return ' FROM ' . $query[0];
402
403
}
404
405
function retrieve_relationship_query($module_name,  $related_module, $relationship_query, $show_deleted, $offset, $max_results){
406
	global  $beanList, $beanFiles, $dictionary, $current_user;
407
	$error = new SoapError();
408
	$result_list = array();
409
	if(empty($beanList[$module_name]) || empty($beanList[$related_module])){
410
411
		$error->set_error('no_module');
412
		return array('query' =>"", 'module_1'=>"", 'join_table' =>"", 'error'=>$error->get_soap_array());
413
	}
414
415
	$row = retrieve_relationships_properties($module_name, $related_module);
416
	if(empty($row)){
417
418
		$error->set_error('no_relationship_support');
419
		return array('query' =>"", 'module_1'=>"", 'join_table' =>"", 'error'=>$error->get_soap_array());
420
	}
421
422
	$module_1 = $row['lhs_module'];
423
	$mod_key = $row['join_key_lhs'];
424
	$module_2 = $row['rhs_module'];
425
	$mod2_key = $row['join_key_rhs'];
426
427
	$table = $row['join_table'];
428
	if(empty($table)){
429
		return array('query' =>"", 'module_1'=>"", 'join_table' =>"", 'error'=>$error->get_soap_array());
430
	}
431
	$class_name = $beanList[$module_1];
432
	require_once($beanFiles[$class_name]);
433
	$mod = new $class_name();
434
435
	$mod2_name = $beanList[$module_2];
436
	require_once($beanFiles[$mod2_name]);
437
	$mod2 = new $mod2_name();
438
	$query = "SELECT rt.* FROM  $table rt ";
439
	$query .= " inner join $mod->table_name m1 on rt.$mod_key = m1.id ";
440
	$query .= " inner join $mod2->table_name m2 on rt.$mod2_key = m2.id  ";
441
442
443
	if(!empty($relationship_query)){
444
		$query .= ' WHERE ' . $relationship_query;
445
	}
446
447
	return array('query' =>$query, 'module_1'=>$module_1, 'join_table' => $table, 'error'=>$error->get_soap_array());
448
}
449
450
// Returns name of 'link' field between two given modules
451
function get_module_link_field($module_1, $module_2) {
452
	global $beanList, $beanFiles;
453
454
	// check to make sure both modules exist
455
	if (empty($beanList[$module_1]) || empty($beanList[$module_2])) {
456
		return FALSE;
457
	}
458
459
	$class_1 = $beanList[$module_1];
460
	require_once($beanFiles[$class_1]);
461
462
	$obj_1 = new $class_1();
463
464
	// loop through link fields of $module_1, checking for a link to $module_2
465
	foreach ($obj_1->get_linked_fields() as $linked_field) {
466
		$obj_1->load_relationship($linked_field['name']);
467
		$field = $linked_field['name'];
468
469
		if (empty($obj_1->$field)) {
470
			continue;
471
		}
472
473
		if ($obj_1->$field->getRelatedModuleName() == $module_2) {
474
			return $field;
475
		}
476
	}
477
478
	return FALSE;
479
}
480
481
// Retrieves array of ids for records of $get_module linked to $from_module by $get_id
482
// Example: to retrieve list of Contacts associated to Account X: $return = get_linked_records("Contacts", "Accounts", "X");
483
function get_linked_records($get_module, $from_module, $get_id) {
484
	global $beanList, $beanFiles;
485
486
	// instantiate and retrieve $from_module
487
	$from_class = $beanList[$from_module];
488
	require_once($beanFiles[$from_class]);
489
	$from_mod = new $from_class();
490
	$from_mod->retrieve($get_id);
491
492
	$field = get_module_link_field($from_module, $get_module);
493
	if ($field === FALSE) {
494
		return FALSE;
495
	}
496
497
	$from_mod->load_relationship($field);
498
	$id_arr = $from_mod->$field->get();
499
500
	//bug: 38065
501
	if ($get_module == 'EmailAddresses') {
502
		$emails = $from_mod->emailAddress->addresses;
503
		$email_arr = array();
504
		foreach ($emails as $email) {
505
			$email_arr[] = $email['email_address_id'];
506
		}
507
		return $email_arr;
508
	}
509
510
	return $id_arr;
511
}
512
513
?>