profile.php ➔ get_api_profile()   F
last analyzed

Complexity

Conditions 30
Paths > 20000

Size

Total Lines 228

Duplication

Lines 36
Ratio 15.79 %

Code Coverage

Tests 0
CRAP Score 930

Importance

Changes 0
Metric Value
cc 30
nc 2097153
nop 1
dl 36
loc 228
ccs 0
cts 171
cp 0
crap 930
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
elgg_ws_expose_function(
3
	"profile.get",
4
	"get_api_profile",
5
	array("id" => array('type' => 'string')),
6
	'provide user GUID number and all profile information is returned',
7
	'GET',
8
	false,
9
	false
10
);
11
12
elgg_ws_expose_function(
13
	"get.profile",
14
	"get_api_profile",
15
	array("id" => array('type' => 'string')),
16
	'provide user GUID number and all profile information is returned',
17
	'GET',
18
	false,
19
	false
20
);
21
22
elgg_ws_expose_function(
23
	"get.profile.by.gcid",
24
	"get_api_profile_gcid",
25
	array("gcid" => array('type' => 'string')),
26
	'provide user GUID number and all profile information is returned',
27
	'GET',
28
	false,
29
	false
30
);
31
32
elgg_ws_expose_function(
33
	"profile.update",
34
	"profileUpdate",
35
	array("id" => array('type' => 'string'), "data" => array('type'=>'string')),
36
	'update a user profile based on id passed',
37
	'POST',
38
	true,
39
	false
40
);
41
42
elgg_ws_expose_function(
43
	"profile.create",
44
	"profileCreate",
45
	array("data" => array('type'=>'string')),
46
	'Create a new user profile, issue a password reset on the newly created profile and pre-populate profile fields based on data passed in. Returns guid of newly created user',
47
	'POST',
48
	true,
49
	false
50
);
51
function get_api_profile_gcid($gcid){
52
	if (!elgg_is_active_plugin('pleio')) {
53
		return "pleio mod is not active and there is no openid function";
54
	}
55
	$dbprefix = elgg_get_config("dbprefix");
56
57
    $result = get_data_row("SELECT * FROM {$dbprefix}users_entity WHERE pleio_guid = $gcid");
58
	
59
	if ($result)
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
60
		return get_api_profile($result->guid);
61
	else
62
		return "no user found";
63
}
64
function get_api_profile($id)
65
{
66
	$user_entity = getUserFromID($id);
67
	if (!$user_entity) {
68
		return "User was not found. Please try a different GUID, username, or email address";
69
	}
70
71
	$dbprefix = elgg_get_config("dbprefix");
72
73
    $result = get_data_row("SELECT * FROM {$dbprefix}users_entity WHERE guid = $user_entity->guid");
74
75
	$user['id'] = $user_entity->guid;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$user was never initialized. Although not strictly required by PHP, it is generally a good practice to add $user = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
76
77
	$user['pleioID'] = $result->pleio_guid;
78
79
	$user['username'] = $user_entity->username;
80
81
	//get and store user display name
82
	$user['displayName'] = $user_entity->name;
83
84
	$user['email'] = $user_entity->email;
85
86
	//get and store URL for profile
87
	$user['profileURL'] = $user_entity->getURL();
88
89
	//get and store URL of profile avatar
90
	$user['iconURL'] = $user_entity->geticon();
91
92
	$user['jobTitle'] = $user_entity->job;
93
94
	$user['department'] = $user_entity->department;
95
96
	$user['telephone'] = $user_entity->phone;
97
98
	$user['mobile'] = $user_entity->mobile;
99
100
	$user['Website'] = $user_entity->website;
101
102
	if ($user_entity->facebook) {
103
		$user['links']['facebook'] = "http://www.facebook.com/".$user_entity->facebook;
104
	}
105
	if ($user_entity->google) {
106
		$user['links']['google'] = "http://www.google.com/".$user_entity->google;
107
	}
108
	if ($user_entity->github) {
109
		$user['links']['github'] = "https://github.com/".$user_entity->github;
110
	}
111
	if ($user_entity->twitter) {
112
		$user['links']['twitter'] = "https://twitter.com/".$user_entity->twitter;
113
	}
114
	if ($user_entity->linkedin) {
115
		$user['links']['linkedin'] = "http://ca.linkedin.com/in/".$user_entity->linkedin;
116
	}
117
	if ($user_entity->pinterest) {
118
		$user['links']['pinterest'] = "http://www.pinterest.com/".$user_entity->pinterest;
119
	}
120
	if ($user_entity->tumblr) {
121
		$user['links']['tumblr'] = "https://www.tumblr.com/blog/".$user_entity->tumblr;
122
	}
123
	if ($user_entity->instagram) {
124
		$user['links']['instagram'] = "http://instagram.com/".$user_entity->instagram;
125
	}
126
	if ($user_entity->flickr) {
127
		$user['links']['flickr'] = "http://flickr.com/".$user_entity->flickr;
128
	}
129
	if ($user_entity->youtube) {
130
		$user['links']['youtube'] = "http://www.youtube.com/".$user_entity->youtube;
131
	}
132
133
	////////////////////////////////////////////////////////////////////////////////////
134
	//about me
135
	////////////////////////////////////////////////////////////////////////
136
	$aboutMeMetadata = elgg_get_metadata(array('guids'=>array($user['id']),'limit'=>0,'metadata_names'=>array('description')));
137
138
	if ($aboutMeMetadata[0]->access_id==2) {
139
		$user['about_me'] = $aboutMeMetadata[0]->value;
140
	}
141
142
	/////////////////////////////////////////////////////////////////////////////////
143
	//eductation
144
	//////////////////////////////////////////////////////////////////////
145
	$eductationEntity = elgg_get_entities(array(
146
		'owner_guid'=>$user['id'],
147
		'subtype'=>'education',
148
		'type' => 'object',
149
		'limit' => 0
150
		));
151
	$i=0;
152
	foreach ($eductationEntity as $school) {
153
		if ($school->access_id==2) {
154
			$user['education']['item_'.$i]['school_name'] = $school->school;
155
156
			$user['education']['item_'.$i]['start_date'] = buildDate($school->startdate, $school->startyear);
157
158 View Code Duplication
			if ($school->ongoing == "false") {
159
				$user['education']['item_'.$i]['end_date'] = buildDate($school->enddate, $school->endyear);
160
			} else {
161
				$user['education']['item_'.$i]['end_date'] = "present/actuel";
162
			}
163
			$user['education']['item_'.$i]['degree'] = $school->degree;
164
			$user['education']['item_'.$i]['field_of_study'] = $school->field;
165
			$i++;
166
		}
167
	}
168
	////////////////////////////////////////////////////////
169
	//experience
170
	//////////////////////////////////////
171
	$experienceEntity = elgg_get_entities(array(
172
		'owner_guid'=>$user['id'],
173
		'subtype'=>'experience',
174
		'type' => 'object',
175
		'limit' => 0
176
		));
177
	usort($experienceEntity, "sortDate");
178
	$i=0;
179
	foreach ($experienceEntity as $job) {
180
		if ($job->access_id == 2) {
181
			$user['experience']['item_'.$i]['job_title'] = $job->title;
182
			$user['experience']['item_'.$i]['organization'] = $job->organization;
183
			$user['experience']['item_'.$i]['start_date'] = buildDate($job->startdate, $job->startyear);
184 View Code Duplication
			if ($job->ongoing == "false") {
185
				$user['experience']['item_'.$i]['end_date'] = buildDate($job->enddate, $job->endyear);
186
			} else {
187
				$user['experience']['item_'.$i]['end_date'] = "present/actuel";
188
			}
189
			$user['experience']['item_'.$i]['responsibilities'] = $job->responsibilities;
190
191
			$j = 0;
192
			if (is_array($job->colleagues)) {
193
				foreach ($job->colleagues as $friend) {
194
					$friendEntity = get_user($friend);
195
					$user['experience']['item_'.$i]['colleagues']['colleague_'.$j]["id"] = $friendEntity->guid;
196
					$user['experience']['item_'.$i]['colleagues']['colleague_'.$j]["username"] = $friendEntity->username;
197
198
					//get and store user display name
199
					$user['experience']['item_'.$i]['colleagues']['colleague_'.$j]["displayName"] = $friendEntity->name;
200
201
					//get and store URL for profile
202
					$user['experience']['item_'.$i]['colleagues']['colleague_'.$j]["profileURL"] = $friendEntity->getURL();
203
204
					//get and store URL of profile avatar
205
					$user['experience']['item_'.$i]['colleagues']['colleague_'.$j]["iconURL"] = $friendEntity->geticon();
0 ignored issues
show
Deprecated Code introduced by
The method ElggEntity::getIcon() has been deprecated with message: 1.8 Use getIconURL()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
206
					$j++;
207
				}
208
			} elseif (!is_null($job->colleagues)) {
209
				$friendEntity = get_user($job->colleagues);
210
				$user['experience']['item_'.$i]['colleagues']['colleague_'.$j]["id"] = $friendEntity->guid;
211
				$user['experience']['item_'.$i]['colleagues']['colleague_'.$j]["username"] = $friendEntity->username;
212
213
				//get and store user display name
214
				$user['experience']['item_'.$i]['colleagues']['colleague_'.$j]["displayName"] = $friendEntity->name;
215
216
				//get and store URL for profile
217
				$user['experience']['item_'.$i]['colleagues']['colleague_'.$j]["profileURL"] = $friendEntity->getURL();
218
219
				//get and store URL of profile avatar
220
				$user['experience']['item_'.$i]['colleagues']['colleague_'.$j]["iconURL"] = $friendEntity->geticon();
0 ignored issues
show
Deprecated Code introduced by
The method ElggEntity::getIcon() has been deprecated with message: 1.8 Use getIconURL()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
221
			}
222
			$i++;
223
		}
224
	}
225
	/////////////////////////////////////////////////////////
226
	//Skills
227
	///////////////////////////////////////////////////////
228
	elgg_set_ignore_access(true);
229
	if ($user_entity->skill_access == ACCESS_PUBLIC) {
230
		$skillsEntity = elgg_get_entities(array(
231
			'owner_guid'=>$user['id'],
232
			'subtype'=>'MySkill',
233
			'type' => 'object',
234
			'limit' => 0
235
		));
236
	}
237
	$i=0;
238 View Code Duplication
	foreach ($skillsEntity as $skill) {
0 ignored issues
show
Bug introduced by
The variable $skillsEntity does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
239
		$user['skills']['item_'.$i]['skill'] = $skill->title;
240
		$j = 0;
241
		if (is_array($skill->endorsements)) {
242
			foreach ($skill->endorsements as $friend) {
243
				$friendEntity = get_user($friend);
244
				$user['skills']['item_'.$i]['endorsements']["user_".$j]["id"] = $friendEntity->guid;
245
				$user['skills']['item_'.$i]['endorsements']["user_".$j]["username"] = $friendEntity->username;
246
				$user['skills']['item_'.$i]['endorsements']["user_".$j]["displayName"] = $friendEntity->name;
247
				$user['skills']['item_'.$i]['endorsements']["user_".$j]["profileURL"] = $friendEntity->getURL();
248
				$user['skills']['item_'.$i]['endorsements']["user_".$j]["iconURL"] = $friendEntity->geticon();
0 ignored issues
show
Deprecated Code introduced by
The method ElggEntity::getIcon() has been deprecated with message: 1.8 Use getIconURL()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
249
				$j++;
250
			}
251
		} elseif (!is_null($skill->endorsements)) {
252
			$friendEntity = get_user($skill->endorsements);
253
			$user['skills']['item_'.$i]['endorsements']["user_".$j]["id"] = $friendEntity->guid;
254
			$user['skills']['item_'.$i]['endorsements']["user_".$j]["username"] = $friendEntity->username;
255
			$user['skills']['item_'.$i]['endorsements']["user_".$j]["displayName"] = $friendEntity->name;
256
			$user['skills']['item_'.$i]['endorsements']["user_".$j]["profileURL"] = $friendEntity->getURL();
257
			$user['skills']['item_'.$i]['endorsements']["user_".$j]["iconURL"] = $friendEntity->geticon();
0 ignored issues
show
Deprecated Code introduced by
The method ElggEntity::getIcon() has been deprecated with message: 1.8 Use getIconURL()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
258
		}
259
		$i++;
260
	}
261
	elgg_set_ignore_access(false);
262
263
	//////////////////////////////////////////////////////////////////////////////////////
264
	//portfolio
265
	///////////////////////////////////////////////////////////////////
266
	$portfolioEntity = elgg_get_entities(array(
267
		'owner_guid'=>$user['id'],
268
		'subtype'=>'portfolio',
269
		'type' => 'object',
270
		'limit' => 0
271
	));
272
	$i=0;
273
	foreach ($portfolioEntity as $portfolio) {
274
		if ($portfolio->access_id == 2) {
275
			$user['portfolio']['item_'.$i]['title'] = $portfolio->title;
276
			$user['portfolio']['item_'.$i]['link'] = $portfolio->link;
277 View Code Duplication
			if ($portfolio->datestamped == "on") {
278
				$user['portfolio']['item_'.$i]['date'] = $portfolio->publishdate;
279
			}
280
			$user['portfolio']['item_'.$i]['description'] = $portfolio->description;
281
		}
282
	}
283
284
	$user['dateJoined'] = date("Y-m-d H:i:s", $user_entity->time_created);
285
286
	$user['lastActivity'] = date("Y-m-d H:i:s", $user_entity->last_action);
287
288
	$user['lastLogin'] = date("Y-m-d H:i:s", $user_entity->last_login);
289
290
	return $user;
291
}
292
293
function profileUpdate($id, $data)
294
{
295
	global $CONFIG;
296
	$response['error'] = 0;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
297
	$user_entity = getUserFromID($id);
298
	if (!$user_entity) {
299
		$response['error'] = 1;
300
		$response['message'] = 'Invalid user id, username, or email';
301
		return $response;
302
	}
303
304 View Code Duplication
	if ($data == '') {
305
		$response['error'] = 2;
306
		$response['message'] = 'data must be a string representing a JSON object.';
307
		return $response;
308
	}
309
	$userDataObj = json_decode($data, true);
310 View Code Duplication
	if (json_last_error() !== 0) {
311
		$response['error'] = 2;
312
		$response['message'] = 'invalid JSON - data was unable to be parsed';
313
		return $response;
314
	}
315
316
	foreach ($userDataObj as $field => $value) {
317
		switch ($field) {
318
			case 'name':
319
			elgg_set_ignore_access(true);
320
321
				$nameData = json_decode(json_encode($value), true);
322 View Code Duplication
				if (!isset($nameData["firstName"])&&!isset($nameData["lastName"])) {
323
					$response['error'] = 4;
324
					$response['message'] = 'invalid data format - missing first and last name';
325
					return $response;
326
				}
327 View Code Duplication
				if (!isset($nameData["firstName"])||!isset($nameData["lastName"])) {
328
					$response['error'] = 4;
329
					$response['message'] = 'invalid data format - missing first or last name';
330
					return $response;
331
				}
332
333
				$name = $nameData["firstName"].' '.$nameData["lastName"];
334 View Code Duplication
				if (elgg_strlen($name) > 50) {
335
					register_error(elgg_echo('user:name:fail'));
336
				} elseif ($user_entity->name != $name) {
337
					$user_entity->name= $name;
338
					$user_entity->save();
339
				}
340
				elgg_set_ignore_access(false);
341
				break;
342
			case 'title':
343
344
				$titleData = json_decode(json_encode($value), true);
345
				if (!isset($titleData['fr'])&&!isset($titleData['en'])) {
346
					$response['error'] = 4;
347
					$response['message'] = 'invalid data format - missing french and english title';
348
					return $response;
349
				}
350
				if (!isset($titleData['fr'])||!isset($titleData['en'])) {
351
					$response['error'] = 4;
352
					$response['message'] = 'invalid data format - missing french or english title';
353
					return $response;
354
				}
355
356
				if ($user_entity->language === 'fr') {
357
					$user_entity->set('job', $titleData['fr'].' / '.$titleData['en']);
358
				} else {
359
					$user_entity->set('job', $titleData['en'].' / '.$titleData['fr']);
360
				}
361
362
				break;
363 View Code Duplication
			case 'classification':
364
				$classificationData = json_decode(json_encode($value), true);
365
				if (!isset($classificationData['group'])&&!isset($classificationData['level'])) {
366
					$response['error'] = 4;
367
					$response['message'] = 'invalid data format - missing classification group and level';
368
					return $response;
369
				}
370
				if (!isset($classificationData['group'])||!isset($classificationData['level'])) {
371
					$response['error'] = 4;
372
					$response['message'] = 'invalid data format - missing classification group or level';
373
					return $response;
374
				}
375
376
				$user_entity->set('classification', json_encode($value));
377
				break;
378
			case 'department':
379
				$deptData = json_decode(json_encode($value), true);
380
				if (!isset($deptData['fr'])&&!isset($deptData['en'])) {
381
					$response['error'] = 4;
382
					$response['message'] = 'invalid data format - department format';
383
					return $response;
384
				}
385
				if (!isset($deptData['fr'])||!isset($deptData['en'])) {
386
					$response['error'] = 4;
387
					$response['message'] = 'invalid data format - missing french or english department';
388
					return $response;
389
				}
390
391
				$obj = elgg_get_entities(array(
392
					'type' => 'object',
393
					'subtype' => 'dept_list',
394
					'owner_guid' => 0
395
				));
396
				$deptListEn = json_decode($obj[0]->deptsEn, true);
397
				$provinces = array();
398
				$provinces['pov-alb'] = 'Government of Alberta';
399
				$provinces['pov-bc'] = 'Government of British Columbia';
400
				$provinces['pov-man'] = 'Government of Manitoba';
401
				$provinces['pov-nb'] = 'Government of New Brunswick';
402
				$provinces['pov-nfl'] = 'Government of Newfoundland and Labrador';
403
				$provinces['pov-ns'] = 'Government of Nova Scotia';
404
				$provinces['pov-nwt'] = 'Government of Northwest Territories';
405
				$provinces['pov-nun'] = 'Government of Nunavut';
406
				$provinces['pov-ont'] = 'Government of Ontario';
407
				$provinces['pov-pei'] = 'Government of Prince Edward Island';
408
				$provinces['pov-que'] = 'Government of Quebec';
409
				$provinces['pov-sask'] = 'Government of Saskatchewan';
410
				$provinces['pov-yuk'] = 'Government of Yukon';
411
				$provinces['CIRNAC-RCAANC'] = 'Crown-Indigenous Relations and Northern Affairs Canada';
412
				$provinces['PPS-SPP'] = 'Parliamentary Protective Service';
413
				$deptAndProvincesEn = array_merge($deptListEn, $provinces);
414
				unset($deptAndProvincesEn['ou=INAC-AANC, o=GC, c=CA']);
415
				
416
417
				$deptListFr = json_decode($obj[0]->deptsFr, true);
418
				$provinces = array();
419
				$provinces['pov-alb'] = "Gouvernement de l'Alberta";
420
				$provinces['pov-bc'] = 'Gouvernement de la Colombie-Britannique';
421
				$provinces['pov-man'] = 'Gouvernement du Manitoba';
422
				$provinces['pov-nb'] = 'Gouvernement du Nouveau-Brunswick';
423
				$provinces['pov-nfl'] = 'Gouvernement de Terre-Neuve-et-Labrador';
424
				$provinces['pov-ns'] = 'Gouvernement de la Nouvelle-Écosse';
425
				$provinces['pov-nwt'] = 'Gouvernement du Territoires du Nord-Ouest';
426
				$provinces['pov-nun'] = 'Gouvernement du Nunavut';
427
				$provinces['pov-ont'] = "Gouvernement de l'Ontario";
428
				$provinces['pov-pei'] = "Gouvernement de l'Île-du-Prince-Édouard";
429
				$provinces['pov-que'] = 'Gouvernement du Québec';
430
				$provinces['pov-sask'] = 'Gouvernement de Saskatchewan';
431
				$provinces['pov-yuk'] = 'Gouvernement du Yukon';
432
				$provinces['CIRNAC-RCAANC'] = 'Relations Couronne-Autochtones et Affaires du Nord Canada';
433
				$provinces['PPS-SPP'] = 'Service de Protection Parlementaire';
434
				$deptAndProvincesFr = array_merge($deptListFr, $provinces);
435
				unset($deptAndProvincesFr['ou=INAC-AANC, o=GC, c=CA']);
436
437 View Code Duplication
				if (!in_array($deptData['en'], $deptAndProvincesEn)) {
438
					$response['error'] = 5;
439
					$response['message'] = 'invalid english department name. valid names: '.json_encode($deptAndProvincesEn);
440
					return $response;
441
				}
442
443 View Code Duplication
				if (!in_array($deptData['fr'], $deptAndProvincesFr)) {
444
					$response['error'] = 5;
445
					$response['message'] = 'invalid french department name. valid names: '.json_encode($deptAndProvincesFr);
446
					return $response;
447
				}
448
449
				if ($user_entity->language === 'fr') {
450
					$user_entity->set('department', $deptData['fr'].' / '.$deptData['en']);
451
				} else {
452
					$user_entity->set('department', $deptData['en'].' / '.$deptData['fr']);
453
				}
454
				break;
455 View Code Duplication
			case 'branch':
456
				$branchData = json_decode(json_encode($value), true);
457
				if (!isset($branchData['en'])&&!isset($branchData['fr'])) {
458
					$response['error'] = 4;
459
					$response['message'] = 'invalid data format - missing english and french branch name';
460
					return $response;
461
				}
462
				if (!isset($branchData['en'])||!isset($branchData['fr'])) {
463
					$response['error'] = 4;
464
					$response['message'] = 'invalid data format - missing english or french branch name';
465
					return $response;
466
				}
467
468
				$user_entity->set('branch', json_encode($value));
469
				break;
470 View Code Duplication
			case 'sector':
471
				$sectorData = json_decode(json_encode($value), true);
472
				if (!isset($sectorData['en'])&&!isset($sectorData['fr'])) {
473
					$response['error'] = 4;
474
					$response['message'] = 'invalid data format - missing english and french sector name';
475
					return $response;
476
				}
477
				if (!isset($sectorData['en'])||!isset($sectorData['fr'])) {
478
					$response['error'] = 4;
479
					$response['message'] = 'invalid data format - missing english or french sector name';
480
					return $response;
481
				}
482
483
				$user_entity->set('sector', json_encode($value));
484
				break;
485
			case 'location':
486 View Code Duplication
				if (!isset($value['en'])) {
487
					$response['error'] = 4;
488
					$response['message'] = 'missing english location data';
489
					return $response;
490
				}
491
				$locationData = json_decode(json_encode($value['en']), true);
492 View Code Duplication
				if (!isset($locationData['street'])&&!isset($locationData['city'])&&!isset($locationData['province'])&&!isset($locationData['postalCode'])&&!isset($locationData['country'])&&!isset($locationData['building'])&&!isset($locationData['floor'])&&!isset($locationData['officeNum'])) {
493
					$response['error'] = 4;
494
					$response['message'] = 'invalid location data';
495
					return $response;
496
				}
497 View Code Duplication
				if (!isset($locationData['street'])||!isset($locationData['city'])||!isset($locationData['province'])||!isset($locationData['postalCode'])||!isset($locationData['country'])||!isset($locationData['building'])||!isset($locationData['floor'])||!isset($locationData['officeNum'])) {
498
					$response['error'] = 4;
499
					$response['message'] = 'missing location data';
500
					return $response;
501
				}
502
503 View Code Duplication
				if (!isset($value['fr'])) {
504
					$response['error'] = 4;
505
					$response['message'] = 'missing french location data';
506
					return $response;
507
				}
508
				$locationData = json_decode(json_encode($value['fr']), true);
509 View Code Duplication
				if (!isset($locationData['street'])&&!isset($locationData['city'])&&!isset($locationData['province'])&&!isset($locationData['postalCode'])&&!isset($locationData['country'])&&!isset($locationData['building'])&&!isset($locationData['floor'])&&!isset($locationData['officeNum'])) {
510
					$response['error'] = 4;
511
					$response['message'] = 'invalid location data';
512
					return $response;
513
				}
514 View Code Duplication
				if (!isset($locationData['street'])||!isset($locationData['city'])||!isset($locationData['province'])||!isset($locationData['postalCode'])||!isset($locationData['country'])||!isset($locationData['building'])||!isset($locationData['floor'])||!isset($locationData['officeNum'])) {
515
					$response['error'] = 4;
516
					$response['message'] = 'missing location data';
517
					return $response;
518
				}
519
520
				$user_entity->set('addressString', json_encode($value["en"]));
521
				$user_entity->set('addressStringFr', json_encode($value["fr"]));
522
				break;
523
			case 'phone':
524
525
				$user_entity->set('phone', $value);
526
				break;
527
			case 'mobile':
528
529
				$user_entity->set('mobile', $value);
530
				break;
531
			case 'email':
532
533
				elgg_set_ignore_access(true);
534
				$connection = mysqli_connect($CONFIG->dbhost, $CONFIG->dbuser, $CONFIG->dbpass, $CONFIG->dbname)or die(mysqli_error($connection));
535
				mysqli_select_db($connection, $CONFIG->dbname);
536
				$emaildomain = explode('@', filter_var($value, FILTER_SANITIZE_EMAIL));
537
				$query = "SELECT count(*) AS num FROM email_extensions WHERE ext ='".$emaildomain[1]."'";
538
539
				$result = mysqli_query($connection, $query)or die(mysqli_error($connection));
540
				$result = mysqli_fetch_array($result);
541
542
				$emailgc = explode('.', $emaildomain[1]);
543
				$gcca = $emailgc[count($emailgc) - 2] .".".$emailgc[count($emailgc) - 1];
544
545
				mysqli_close($connection);
546
547
				$resulting_error = "";
548
549 View Code Duplication
				if ($result['num'][0] <= 0) {
550
					if ($gcca !== 'gc.ca') {
551
						$resulting_error .= elgg_echo('gcRegister:invalid_email');
552
					}
553
				}
554
555
556 View Code Duplication
				if ($resulting_error !== "") {
557
					$response['error'] = 3;
558
					$response['message'] = 'invalid email or email domain - must be a valid Government of Canada email address';
559
					return $response;
560
				}
561
				$user_entity->set('email', $value);
562
				$user_entity->save();
563
564
				elgg_set_ignore_access(false);
565
				break;
566
			case 'secondLanguage':
567
568
				$user_entity->set('english', $value["ENG"]);
569
				$user_entity->set('french', $value["FRA"]);
570
				$user_entity->set('officialLanguage', $value["firstLanguage"]);
571
572
				break;
573
		}
574
	}
575
576
	$user_entity->save();
577
	return 'success';
578
}
579
580
function profileCreate($data)
581
{
582
	global $CONFIG;
583
	// check email for duplicate
584
	// get email and create username
585
	// create account
586
	// send password reset email
587
	// fill in profile data
588 View Code Duplication
	if ($data == '') {
589
		$response['error'] = 2;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
590
		$response['message'] = 'data must be a string representing a JSON object.';
591
		return $response;
592
	}
593
	$userDataObj = json_decode($data, true);
594 View Code Duplication
	if (json_last_error() !== 0) {
595
		$response['error'] = 2;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
596
		$response['message'] = 'invalid JSON - data was unable to be parsed';
597
		return $response;
598
	}
599
600
	///////////////////////////////////////////////////////////////////
601
	//error check data field
602
	///////////////////////////////////////////////////////////////////
603
	foreach ($userDataObj as $field => $value) {
604
		switch ($field) {
605
			case 'name':
606
				$nameData = json_decode(json_encode($value), true);
607 View Code Duplication
				if (!isset($nameData["firstName"])&&!isset($nameData["lastName"])) {
608
					$response['error'] = 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
609
					$response['message'] = 'invalid data format - missing first and last name';
610
					return $response;
611
				}
612 View Code Duplication
				if (!isset($nameData["firstName"])||!isset($nameData["lastName"])) {
613
					$response['error'] = 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
614
					$response['message'] = 'invalid data format - missing first or last name';
615
					return $response;
616
				}
617
618
619
				$name = $nameData["firstName"].' '.$nameData["lastName"];
620
621
				break;
622 View Code Duplication
			case 'title':
623
624
				$titleData = json_decode(json_encode($value), true);
625
				if (!isset($titleData['fr'])&&!isset($titleData['en'])) {
626
					$response['error'] = 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
627
					$response['message'] = 'invalid data format - missing french and english title';
628
					return $response;
629
				}
630
				if (!isset($titleData['fr'])||!isset($titleData['en'])) {
631
					$response['error'] = 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
632
					$response['message'] = 'invalid data format - missing french or english title';
633
					return $response;
634
				}
635
				break;
636 View Code Duplication
			case 'classification':
637
				$classificationData = json_decode(json_encode($value), true);
638
				if (!isset($classificationData['group'])&&!isset($classificationData['level'])) {
639
					$response['error'] = 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
640
					$response['message'] = 'invalid data format - missing classification group and level';
641
					return $response;
642
				}
643
				if (!isset($classificationData['group'])||!isset($classificationData['level'])) {
644
					$response['error'] = 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
645
					$response['message'] = 'invalid data format - missing classification group or level';
646
					return $response;
647
				}
648
649
				break;
650
			case 'department':
651
				$deptData = json_decode(json_encode($value), true);
652
				if (!isset($deptData['fr'])&&!isset($deptData['en'])) {
653
					$response['error'] = 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
654
					$response['message'] = 'invalid data format - department format';
655
					return $response;
656
				}
657
				if (!isset($deptData['fr'])||!isset($deptData['en'])) {
658
					$response['error'] = 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
659
					$response['message'] = 'invalid data format - missing french or english department';
660
					return $response;
661
				}
662
663
				$obj = elgg_get_entities(array(
664
					'type' => 'object',
665
					'subtype' => 'dept_list',
666
					'owner_guid' => 0
667
				));
668
				$deptListEn = json_decode($obj[0]->deptsEn, true);
669
				$provinces = array();
670
				$provinces['pov-alb'] = 'Government of Alberta';
671
				$provinces['pov-bc'] = 'Government of British Columbia';
672
				$provinces['pov-man'] = 'Government of Manitoba';
673
				$provinces['pov-nb'] = 'Government of New Brunswick';
674
				$provinces['pov-nfl'] = 'Government of Newfoundland and Labrador';
675
				$provinces['pov-ns'] = 'Government of Nova Scotia';
676
				$provinces['pov-nwt'] = 'Government of Northwest Territories';
677
				$provinces['pov-nun'] = 'Government of Nunavut';
678
				$provinces['pov-ont'] = 'Government of Ontario';
679
				$provinces['pov-pei'] = 'Government of Prince Edward Island';
680
				$provinces['pov-que'] = 'Government of Quebec';
681
				$provinces['pov-sask'] = 'Government of Saskatchewan';
682
				$provinces['pov-yuk'] = 'Government of Yukon';
683
				$deptAndProvincesEn = array_merge($deptListEn, $provinces);
684
				unset($deptAndProvincesEn['ou=INAC-AANC, o=GC, c=CA']);
685
686
				$deptListFr = json_decode($obj[0]->deptsFr, true);
687
				$provinces = array();
688
				$provinces['pov-alb'] = "Gouvernement de l'Alberta";
689
				$provinces['pov-bc'] = 'Gouvernement de la Colombie-Britannique';
690
				$provinces['pov-man'] = 'Gouvernement du Manitoba';
691
				$provinces['pov-nb'] = 'Gouvernement du Nouveau-Brunswick';
692
				$provinces['pov-nfl'] = 'Gouvernement de Terre-Neuve-et-Labrador';
693
				$provinces['pov-ns'] = 'Gouvernement de la Nouvelle-Écosse';
694
				$provinces['pov-nwt'] = 'Gouvernement du Territoires du Nord-Ouest';
695
				$provinces['pov-nun'] = 'Gouvernement du Nunavut';
696
				$provinces['pov-ont'] = "Gouvernement de l'Ontario";
697
				$provinces['pov-pei'] = "Gouvernement de l'Île-du-Prince-Édouard";
698
				$provinces['pov-que'] = 'Gouvernement du Québec';
699
				$provinces['pov-sask'] = 'Gouvernement de Saskatchewan';
700
				$provinces['pov-yuk'] = 'Gouvernement du Yukon';
701
				$deptAndProvincesFr = array_merge($deptListFr, $provinces);
702
				unset($deptAndProvincesFr['ou=INAC-AANC, o=GC, c=CA']);
703
704
705 View Code Duplication
				if (!in_array($deptData['en'], $deptAndProvincesEn)) {
706
					$response['error'] = 5;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
707
					$response['message'] = 'invalid english department name. valid names: '.json_encode($deptAndProvincesEn);
708
					return $response;
709
				}
710
711 View Code Duplication
				if (!in_array($deptData['fr'], $deptAndProvincesFr)) {
712
					$response['error'] = 5;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
713
					$response['message'] = 'invalid french department name. valid names: '.json_encode($deptAndProvincesFr);
714
					return $response;
715
				}
716
				break;
717 View Code Duplication
			case 'branch':
718
				$branchData = json_decode(json_encode($value), true);
719
				if (!isset($branchData['en'])&&!isset($branchData['fr'])) {
720
					$response['error'] = 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
721
					$response['message'] = 'invalid data format - missing english and french branch name';
722
					return $response;
723
				}
724
				if (!isset($branchData['en'])||!isset($branchData['fr'])) {
725
					$response['error'] = 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
726
					$response['message'] = 'invalid data format - missing english or french branch name';
727
					return $response;
728
				}
729
				break;
730 View Code Duplication
			case 'sector':
731
				$sectorData = json_decode(json_encode($value), true);
732
				if (!isset($sectorData['en'])&&!isset($sectorData['fr'])) {
733
					$response['error'] = 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
734
					$response['message'] = 'invalid data format - missing english and french sector name';
735
					return $response;
736
				}
737
				if (!isset($sectorData['en'])||!isset($sectorData['fr'])) {
738
					$response['error'] = 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
739
					$response['message'] = 'invalid data format - missing english or french sector name';
740
					return $response;
741
				}
742
				break;
743
			case 'location':
744 View Code Duplication
				if (!isset($value['en'])) {
745
					$response['error'] = 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
746
					$response['message'] = 'missing english location data';
747
					return $response;
748
				}
749
				$locationData = json_decode(json_encode($value['en']), true);
750 View Code Duplication
				if (!isset($locationData['street'])&&!isset($locationData['city'])&&!isset($locationData['province'])&&!isset($locationData['postalCode'])&&!isset($locationData['country'])&&!isset($locationData['building'])&&!isset($locationData['floor'])&&!isset($locationData['officeNum'])) {
751
					$response['error'] = 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
752
					$response['message'] = 'invalid location data';
753
					return $response;
754
				}
755 View Code Duplication
				if (!isset($locationData['street'])||!isset($locationData['city'])||!isset($locationData['province'])||!isset($locationData['postalCode'])||!isset($locationData['country'])||!isset($locationData['building'])||!isset($locationData['floor'])||!isset($locationData['officeNum'])) {
756
					$response['error'] = 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
757
					$response['message'] = 'missing location data';
758
					return $response;
759
				}
760
761 View Code Duplication
				if (!isset($value['fr'])) {
762
					$response['error'] = 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
763
					$response['message'] = 'missing french location data';
764
					return $response;
765
				}
766
				$locationData = json_decode(json_encode($value['fr']), true);
767 View Code Duplication
				if (!isset($locationData['street'])&&!isset($locationData['city'])&&!isset($locationData['province'])&&!isset($locationData['postalCode'])&&!isset($locationData['country'])&&!isset($locationData['building'])&&!isset($locationData['floor'])&&!isset($locationData['officeNum'])) {
768
					$response['error'] = 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
769
					$response['message'] = 'invalid location data';
770
					return $response;
771
				}
772 View Code Duplication
				if (!isset($locationData['street'])||!isset($locationData['city'])||!isset($locationData['province'])||!isset($locationData['postalCode'])||!isset($locationData['country'])||!isset($locationData['building'])||!isset($locationData['floor'])||!isset($locationData['officeNum'])) {
773
					$response['error'] = 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
774
					$response['message'] = 'missing location data';
775
					return $response;
776
				}
777
				break;
778
			case 'email':
779
				$connection = mysqli_connect($CONFIG->dbhost, $CONFIG->dbuser, $CONFIG->dbpass, $CONFIG->dbname)or die(mysqli_error($connection));
780
				mysqli_select_db($connection, $CONFIG->dbname);
781
				$emaildomain = explode('@', filter_var($value, FILTER_SANITIZE_EMAIL));
782
783
				$query = "SELECT count(*) AS num FROM email_extensions WHERE ext ='".$emaildomain[1]."'";
784
785
				$result = mysqli_query($connection, $query)or die(mysqli_error($connection));
786
				$result = mysqli_fetch_array($result);
787
788
				$emailgc = explode('.', $emaildomain[1]);
789
				$gcca = $emailgc[count($emailgc) - 2] .".".$emailgc[count($emailgc) - 1];
790
791
				mysqli_close($connection);
792
793
				$resulting_error = "";
794
795
				// if domain doesn't exist in database, check if it's a gc.ca domain
796 View Code Duplication
				if ($result['num'][0] <= 0) {
797
					if ($gcca !== 'gc.ca') {
798
						$resulting_error .= elgg_echo('gcRegister:invalid_email');
799
					}
800
				}
801
802 View Code Duplication
				if ($resulting_error !== "") {
803
					$response['error'] = 3;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
804
					$response['message'] = 'invalid email or email domain - must be a valid Government of Canada email address';
805
					return $response;
806
				}
807
				break;
808
		}
809
	}
810
811
	//check for existing email
812
	$email = $userDataObj['email'];
813
	if (get_user_by_email($email)) {
814
		$response['error'] = 1;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
815
		$response['message'] = 'user with email already exists. please use profile.update call to update existing account';
816
		return $response;
817
	}
818
	//make usernaem based on email
819
	$username = strstr(strtolower($email), '@', true);
820
821
	$username = explode('.', $username);
822
	foreach ($username as $u=>$v) {
823
		$username[$u] = ucfirst($v);
824
	}
825
	$username = implode('.', $username);
826
827
	//check system for username. if is a username, append number or add number
828
	while (get_user_by_username($username)) {
829
		if (is_numeric(substr($username, -1))) {
830
			$num = substr($username, -1)+1;
831
			$username = substr($username, 0, strlen($username)-1).$num;
832
		} else {
833
			$username.='2';
834
		}
835
	}
836
	$tempPass = generateRandomString();
837
838
	//register user using data passed
839
	$userGUID = register_user($username, $tempPass, $name, $userDataObj['email']);
0 ignored issues
show
Bug introduced by
The variable $name does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
840 View Code Duplication
	if ($userGUID==false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $userGUID of type false|integer|null against false; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
841
		$response['error'] = 1;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
842
		$response['message'] = 'Failed creating account';
843
		return $response;
844
	}
845
846
	$user_entity = get_user($userGUID);
847
848
	foreach ($userDataObj as $field => $value) {
849
		switch ($field) {
850 View Code Duplication
			case 'title':
851
				$titleData = json_decode(json_encode($value), true);
852
853
				if ($user_entity->language === 'fr') {
854
					$user_entity->set('job', $titleData['fr'].' / '.$titleData['en']);
0 ignored issues
show
Deprecated Code introduced by
The method ElggEntity::set() has been deprecated with message: 1.9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
855
				} else {
856
					$user_entity->set('job', $titleData['en'].' / '.$titleData['fr']);
0 ignored issues
show
Deprecated Code introduced by
The method ElggEntity::set() has been deprecated with message: 1.9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
857
				}
858
859
				break;
860
			case 'classification':
861
				$classificationData = json_decode(json_encode($value), true);
862
863
				$user_entity->set('classification', json_encode($value));
0 ignored issues
show
Deprecated Code introduced by
The method ElggEntity::set() has been deprecated with message: 1.9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
864
				break;
865 View Code Duplication
			case 'department':
866
				$deptData = json_decode(json_encode($value), true);
867
868
				if ($user_entity->language === 'fr') {
869
					$user_entity->set('department', $deptData['fr'].' / '.$deptData['en']);
0 ignored issues
show
Deprecated Code introduced by
The method ElggEntity::set() has been deprecated with message: 1.9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
870
				} else {
871
					$user_entity->set('department', $deptData['en'].' / '.$deptData['fr']);
0 ignored issues
show
Deprecated Code introduced by
The method ElggEntity::set() has been deprecated with message: 1.9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
872
				}
873
				break;
874
			case 'branch':
875
				$branchData = json_decode(json_encode($value), true);
876
877
				$user_entity->set('branch', json_encode($value));
0 ignored issues
show
Deprecated Code introduced by
The method ElggEntity::set() has been deprecated with message: 1.9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
878
				break;
879
			case 'sector':
880
				$sectorData = json_decode(json_encode($value), true);
881
882
				$user_entity->set('sector', json_encode($value));
0 ignored issues
show
Deprecated Code introduced by
The method ElggEntity::set() has been deprecated with message: 1.9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
883
				break;
884
			case 'location':
885
886
				$user_entity->set('addressString', json_encode($value["en"]));
0 ignored issues
show
Deprecated Code introduced by
The method ElggEntity::set() has been deprecated with message: 1.9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
887
				$user_entity->set('addressStringFr', json_encode($value["fr"]));
0 ignored issues
show
Deprecated Code introduced by
The method ElggEntity::set() has been deprecated with message: 1.9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
888
				break;
889
			case 'phone':
890
891
				$user_entity->set('phone', $value);
0 ignored issues
show
Deprecated Code introduced by
The method ElggEntity::set() has been deprecated with message: 1.9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
892
				break;
893
			case 'mobile':
894
895
				$user_entity->set('mobile', $value);
0 ignored issues
show
Deprecated Code introduced by
The method ElggEntity::set() has been deprecated with message: 1.9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
896
				break;
897
		}
898
	}
899
	//save user
900
	$user_entity->save();
901
	//send password reset to user
902
	send_new_password_request($userGUID);
903
	return array(
904
		"guid"=> $userGUID,
905
		"message" => "user added"
906
	);
907
}
908
function generateRandomString($length = 10)
909
{
910
	return substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)))), 1, $length);
911
}
912
913
function getUserFromID($id)
914
{
915
	if (is_numeric($id)) {
916
		$user_entity = get_user($id);
917
	} else {
918
		if (strpos($id, '@')) {
919
			$user_entity = get_user_by_email($id);
920
			if (is_array($user_entity)) {
921
				if (count($user_entity)>1) {
922
					return "Found more than 1 user, please use username or GUID";
923
				} else {
924
					$user_entity = $user_entity[0];
925
				}
926
			}
927
		} else {
928
			$user_entity = get_user_by_username($id);
929
		}
930
	}
931
	return $user_entity;
932
}
933
934 View Code Duplication
function buildDate($month, $year)
935
{
936
	switch ($month) {
937
		case 1:
938
			$string = "01/";
939
			break;
940
		case 2:
941
			$string = "02/";
942
			break;
943
		case 3:
944
			$string = "03/";
945
			break;
946
		case 4:
947
			$string = "04/";
948
			break;
949
		case 5:
950
			$string = "05/";
951
			break;
952
		case 6:
953
			$string = "06/";
954
			break;
955
		case 7:
956
			$string = "07/";
957
			break;
958
		case 8:
959
			$string = "08/";
960
			break;
961
		case 9:
962
			$string = "09/";
963
			break;
964
		case 10:
965
			$string = "10/";
966
			break;
967
		case 11:
968
			$string = "11/";
969
			break;
970
		case 12:
971
			$string = "12/";
972
			break;
973
	}
974
	return $string.$year;
0 ignored issues
show
Bug introduced by
The variable $string does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
975
}
976