Completed
Push — sgdt-style-layout ( f4ca52...97ec33 )
by Ilia
10:18 queued 10s
created

profile.php ➔ get_api_profile_gcid()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 13
ccs 0
cts 10
cp 0
crap 12
rs 9.8333
c 0
b 0
f 0
1
<?php
2
elgg_ws_expose_function(
3
	"get.profile",
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.by.gcid",
14
	"get_api_profile_gcid",
15
	array("gcid" => 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
	"profile.update",
24
	"profileUpdate",
25
	array("id" => array('type' => 'string'), "data" => array('type'=>'string')),
26
	'update a user profile based on id passed',
27
	'POST',
28
	true,
29
	false
30
);
31
32
elgg_ws_expose_function(
33
	"profile.create",
34
	"profileCreate",
35
	array("data" => array('type'=>'string')),
36
	'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',
37
	'POST',
38
	true,
39
	false
40
);
41
function get_api_profile_gcid($gcid){
42
	if (!elgg_is_active_plugin('pleio')) {
43
		return "pleio mod is not active and there is no openid function";
44
	}
45
	$dbprefix = elgg_get_config("dbprefix");
46
47
    $result = get_data_row("SELECT * FROM {$dbprefix}users_entity WHERE pleio_guid = $gcid");
48
	
49
	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...
50
		return get_api_profile($result->guid);
51
	else
52
		return "no user found";
53
}
54
function get_api_profile($id)
55
{
56
	$user_entity = getUserFromID($id);
57
	if (!$user_entity) {
58
		return "User was not found. Please try a different GUID, username, or email address";
59
	}
60
61
	$dbprefix = elgg_get_config("dbprefix");
62
63
    $result = get_data_row("SELECT * FROM {$dbprefix}users_entity WHERE guid = $user_entity->guid");
64
65
	$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...
66
67
	$user['pleioID'] = $result->pleio_guid;
68
69
	$user['username'] = $user_entity->username;
70
71
	//get and store user display name
72
	$user['displayName'] = $user_entity->name;
73
74
	$user['email'] = $user_entity->email;
75
76
	//get and store URL for profile
77
	$user['profileURL'] = $user_entity->getURL();
78
79
	//get and store URL of profile avatar
80
	$user['iconURL'] = $user_entity->geticon();
81
82
	$user['jobTitle'] = $user_entity->job;
83
84
	$user['department'] = $user_entity->department;
85
86
	$user['telephone'] = $user_entity->phone;
87
88
	$user['mobile'] = $user_entity->mobile;
89
90
	$user['Website'] = $user_entity->website;
91
92
	if ($user_entity->facebook) {
93
		$user['links']['facebook'] = "http://www.facebook.com/".$user_entity->facebook;
94
	}
95
	if ($user_entity->google) {
96
		$user['links']['google'] = "http://www.google.com/".$user_entity->google;
97
	}
98
	if ($user_entity->github) {
99
		$user['links']['github'] = "https://github.com/".$user_entity->github;
100
	}
101
	if ($user_entity->twitter) {
102
		$user['links']['twitter'] = "https://twitter.com/".$user_entity->twitter;
103
	}
104
	if ($user_entity->linkedin) {
105
		$user['links']['linkedin'] = "http://ca.linkedin.com/in/".$user_entity->linkedin;
106
	}
107
	if ($user_entity->pinterest) {
108
		$user['links']['pinterest'] = "http://www.pinterest.com/".$user_entity->pinterest;
109
	}
110
	if ($user_entity->tumblr) {
111
		$user['links']['tumblr'] = "https://www.tumblr.com/blog/".$user_entity->tumblr;
112
	}
113
	if ($user_entity->instagram) {
114
		$user['links']['instagram'] = "http://instagram.com/".$user_entity->instagram;
115
	}
116
	if ($user_entity->flickr) {
117
		$user['links']['flickr'] = "http://flickr.com/".$user_entity->flickr;
118
	}
119
	if ($user_entity->youtube) {
120
		$user['links']['youtube'] = "http://www.youtube.com/".$user_entity->youtube;
121
	}
122
123
	////////////////////////////////////////////////////////////////////////////////////
124
	//about me
125
	////////////////////////////////////////////////////////////////////////
126
	$aboutMeMetadata = elgg_get_metadata(array('guids'=>array($user['id']),'limit'=>0,'metadata_names'=>array('description')));
127
128
	if ($aboutMeMetadata[0]->access_id==2) {
129
		$user['about_me'] = $aboutMeMetadata[0]->value;
130
	}
131
132
	/////////////////////////////////////////////////////////////////////////////////
133
	//eductation
134
	//////////////////////////////////////////////////////////////////////
135
	$eductationEntity = elgg_get_entities(array(
136
		'owner_guid'=>$user['id'],
137
		'subtype'=>'education',
138
		'type' => 'object',
139
		'limit' => 0
140
		));
141
	$i=0;
142
	foreach ($eductationEntity as $school) {
143
		if ($school->access_id==2) {
144
			$user['education']['item_'.$i]['school_name'] = $school->school;
145
146
			$user['education']['item_'.$i]['start_date'] = buildDate($school->startdate, $school->startyear);
147
148 View Code Duplication
			if ($school->ongoing == "false") {
149
				$user['education']['item_'.$i]['end_date'] = buildDate($school->enddate, $school->endyear);
150
			} else {
151
				$user['education']['item_'.$i]['end_date'] = "present/actuel";
152
			}
153
			$user['education']['item_'.$i]['degree'] = $school->degree;
154
			$user['education']['item_'.$i]['field_of_study'] = $school->field;
155
			$i++;
156
		}
157
	}
158
	////////////////////////////////////////////////////////
159
	//experience
160
	//////////////////////////////////////
161
	$experienceEntity = elgg_get_entities(array(
162
		'owner_guid'=>$user['id'],
163
		'subtype'=>'experience',
164
		'type' => 'object',
165
		'limit' => 0
166
		));
167
	usort($experienceEntity, "sortDate");
168
	$i=0;
169
	foreach ($experienceEntity as $job) {
170
		if ($job->access_id == 2) {
171
			$user['experience']['item_'.$i]['job_title'] = $job->title;
172
			$user['experience']['item_'.$i]['organization'] = $job->organization;
173
			$user['experience']['item_'.$i]['start_date'] = buildDate($job->startdate, $job->startyear);
174 View Code Duplication
			if ($job->ongoing == "false") {
175
				$user['experience']['item_'.$i]['end_date'] = buildDate($job->enddate, $job->endyear);
176
			} else {
177
				$user['experience']['item_'.$i]['end_date'] = "present/actuel";
178
			}
179
			$user['experience']['item_'.$i]['responsibilities'] = $job->responsibilities;
180
181
			$j = 0;
182
			if (is_array($job->colleagues)) {
183
				foreach ($job->colleagues as $friend) {
184
					$friendEntity = get_user($friend);
185
					$user['experience']['item_'.$i]['colleagues']['colleague_'.$j]["id"] = $friendEntity->guid;
186
					$user['experience']['item_'.$i]['colleagues']['colleague_'.$j]["username"] = $friendEntity->username;
187
188
					//get and store user display name
189
					$user['experience']['item_'.$i]['colleagues']['colleague_'.$j]["displayName"] = $friendEntity->name;
190
191
					//get and store URL for profile
192
					$user['experience']['item_'.$i]['colleagues']['colleague_'.$j]["profileURL"] = $friendEntity->getURL();
193
194
					//get and store URL of profile avatar
195
					$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...
196
					$j++;
197
				}
198
			} elseif (!is_null($job->colleagues)) {
199
				$friendEntity = get_user($job->colleagues);
200
				$user['experience']['item_'.$i]['colleagues']['colleague_'.$j]["id"] = $friendEntity->guid;
201
				$user['experience']['item_'.$i]['colleagues']['colleague_'.$j]["username"] = $friendEntity->username;
202
203
				//get and store user display name
204
				$user['experience']['item_'.$i]['colleagues']['colleague_'.$j]["displayName"] = $friendEntity->name;
205
206
				//get and store URL for profile
207
				$user['experience']['item_'.$i]['colleagues']['colleague_'.$j]["profileURL"] = $friendEntity->getURL();
208
209
				//get and store URL of profile avatar
210
				$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...
211
			}
212
			$i++;
213
		}
214
	}
215
	/////////////////////////////////////////////////////////
216
	//Skills
217
	///////////////////////////////////////////////////////
218
	elgg_set_ignore_access(true);
219
	if ($user_entity->skill_access == ACCESS_PUBLIC) {
220
		$skillsEntity = elgg_get_entities(array(
221
			'owner_guid'=>$user['id'],
222
			'subtype'=>'MySkill',
223
			'type' => 'object',
224
			'limit' => 0
225
		));
226
	}
227
	$i=0;
228 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...
229
		$user['skills']['item_'.$i]['skill'] = $skill->title;
230
		$j = 0;
231
		if (is_array($skill->endorsements)) {
232
			foreach ($skill->endorsements as $friend) {
233
				$friendEntity = get_user($friend);
234
				$user['skills']['item_'.$i]['endorsements']["user_".$j]["id"] = $friendEntity->guid;
235
				$user['skills']['item_'.$i]['endorsements']["user_".$j]["username"] = $friendEntity->username;
236
				$user['skills']['item_'.$i]['endorsements']["user_".$j]["displayName"] = $friendEntity->name;
237
				$user['skills']['item_'.$i]['endorsements']["user_".$j]["profileURL"] = $friendEntity->getURL();
238
				$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...
239
				$j++;
240
			}
241
		} elseif (!is_null($skill->endorsements)) {
242
			$friendEntity = get_user($skill->endorsements);
243
			$user['skills']['item_'.$i]['endorsements']["user_".$j]["id"] = $friendEntity->guid;
244
			$user['skills']['item_'.$i]['endorsements']["user_".$j]["username"] = $friendEntity->username;
245
			$user['skills']['item_'.$i]['endorsements']["user_".$j]["displayName"] = $friendEntity->name;
246
			$user['skills']['item_'.$i]['endorsements']["user_".$j]["profileURL"] = $friendEntity->getURL();
247
			$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...
248
		}
249
		$i++;
250
	}
251
	elgg_set_ignore_access(false);
252
253
	//////////////////////////////////////////////////////////////////////////////////////
254
	//portfolio
255
	///////////////////////////////////////////////////////////////////
256
	$portfolioEntity = elgg_get_entities(array(
257
		'owner_guid'=>$user['id'],
258
		'subtype'=>'portfolio',
259
		'type' => 'object',
260
		'limit' => 0
261
	));
262
	$i=0;
263
	foreach ($portfolioEntity as $portfolio) {
264
		if ($portfolio->access_id == 2) {
265
			$user['portfolio']['item_'.$i]['title'] = $portfolio->title;
266
			$user['portfolio']['item_'.$i]['link'] = $portfolio->link;
267 View Code Duplication
			if ($portfolio->datestamped == "on") {
268
				$user['portfolio']['item_'.$i]['date'] = $portfolio->publishdate;
269
			}
270
			$user['portfolio']['item_'.$i]['description'] = $portfolio->description;
271
		}
272
	}
273
274
	$user['dateJoined'] = date("Y-m-d H:i:s", $user_entity->time_created);
275
276
	$user['lastActivity'] = date("Y-m-d H:i:s", $user_entity->last_action);
277
278
	$user['lastLogin'] = date("Y-m-d H:i:s", $user_entity->last_login);
279
280
	return $user;
281
}
282
283
function profileUpdate($id, $data)
284
{
285
	global $CONFIG;
286
	$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...
287
	$user_entity = getUserFromID($id);
288
	if (!$user_entity) {
289
		$response['error'] = 1;
290
		$response['message'] = 'Invalid user id, username, or email';
291
		return $response;
292
	}
293
294 View Code Duplication
	if ($data == '') {
295
		$response['error'] = 2;
296
		$response['message'] = 'data must be a string representing a JSON object.';
297
		return $response;
298
	}
299
	$userDataObj = json_decode($data, true);
300 View Code Duplication
	if (json_last_error() !== 0) {
301
		$response['error'] = 2;
302
		$response['message'] = 'invalid JSON - data was unable to be parsed';
303
		return $response;
304
	}
305
306
	foreach ($userDataObj as $field => $value) {
307
		switch ($field) {
308
			case 'name':
309
			elgg_set_ignore_access(true);
310
311
				$nameData = json_decode(json_encode($value), true);
312 View Code Duplication
				if (!isset($nameData["firstName"])&&!isset($nameData["lastName"])) {
313
					$response['error'] = 4;
314
					$response['message'] = 'invalid data format - missing first and last name';
315
					return $response;
316
				}
317 View Code Duplication
				if (!isset($nameData["firstName"])||!isset($nameData["lastName"])) {
318
					$response['error'] = 4;
319
					$response['message'] = 'invalid data format - missing first or last name';
320
					return $response;
321
				}
322
323
				$name = $nameData["firstName"].' '.$nameData["lastName"];
324 View Code Duplication
				if (elgg_strlen($name) > 50) {
325
					register_error(elgg_echo('user:name:fail'));
326
				} elseif ($user_entity->name != $name) {
327
					$user_entity->name= $name;
328
					$user_entity->save();
329
				}
330
				elgg_set_ignore_access(false);
331
				break;
332
			case 'title':
333
334
				$titleData = json_decode(json_encode($value), true);
335
				if (!isset($titleData['fr'])&&!isset($titleData['en'])) {
336
					$response['error'] = 4;
337
					$response['message'] = 'invalid data format - missing french and english title';
338
					return $response;
339
				}
340
				if (!isset($titleData['fr'])||!isset($titleData['en'])) {
341
					$response['error'] = 4;
342
					$response['message'] = 'invalid data format - missing french or english title';
343
					return $response;
344
				}
345
346
				if ($user_entity->language === 'fr') {
347
					$user_entity->set('job', $titleData['fr'].' / '.$titleData['en']);
348
				} else {
349
					$user_entity->set('job', $titleData['en'].' / '.$titleData['fr']);
350
				}
351
352
				break;
353 View Code Duplication
			case 'classification':
354
				$classificationData = json_decode(json_encode($value), true);
355
				if (!isset($classificationData['group'])&&!isset($classificationData['level'])) {
356
					$response['error'] = 4;
357
					$response['message'] = 'invalid data format - missing classification group and level';
358
					return $response;
359
				}
360
				if (!isset($classificationData['group'])||!isset($classificationData['level'])) {
361
					$response['error'] = 4;
362
					$response['message'] = 'invalid data format - missing classification group or level';
363
					return $response;
364
				}
365
366
				$user_entity->set('classification', json_encode($value));
367
				break;
368
			case 'department':
369
				$deptData = json_decode(json_encode($value), true);
370
				if (!isset($deptData['fr'])&&!isset($deptData['en'])) {
371
					$response['error'] = 4;
372
					$response['message'] = 'invalid data format - department format';
373
					return $response;
374
				}
375
				if (!isset($deptData['fr'])||!isset($deptData['en'])) {
376
					$response['error'] = 4;
377
					$response['message'] = 'invalid data format - missing french or english department';
378
					return $response;
379
				}
380
381
				$obj = elgg_get_entities(array(
382
					'type' => 'object',
383
					'subtype' => 'dept_list',
384
					'owner_guid' => 0
385
				));
386
				$deptListEn = json_decode($obj[0]->deptsEn, true);
387
				$provinces = array();
388
				$provinces['pov-alb'] = 'Government of Alberta';
389
				$provinces['pov-bc'] = 'Government of British Columbia';
390
				$provinces['pov-man'] = 'Government of Manitoba';
391
				$provinces['pov-nb'] = 'Government of New Brunswick';
392
				$provinces['pov-nfl'] = 'Government of Newfoundland and Labrador';
393
				$provinces['pov-ns'] = 'Government of Nova Scotia';
394
				$provinces['pov-nwt'] = 'Government of Northwest Territories';
395
				$provinces['pov-nun'] = 'Government of Nunavut';
396
				$provinces['pov-ont'] = 'Government of Ontario';
397
				$provinces['pov-pei'] = 'Government of Prince Edward Island';
398
				$provinces['pov-que'] = 'Government of Quebec';
399
				$provinces['pov-sask'] = 'Government of Saskatchewan';
400
				$provinces['pov-yuk'] = 'Government of Yukon';
401
				$provinces['CIRNAC-RCAANC'] = 'Crown-Indigenous Relations and Northern Affairs Canada';
402
				$provinces['PPS-SPP'] = 'Parliamentary Protective Service';
403
				$deptAndProvincesEn = array_merge($deptListEn, $provinces);
404
				unset($deptAndProvincesEn['ou=INAC-AANC, o=GC, c=CA']);
405
				
406
407
				$deptListFr = json_decode($obj[0]->deptsFr, true);
408
				$provinces = array();
409
				$provinces['pov-alb'] = "Gouvernement de l'Alberta";
410
				$provinces['pov-bc'] = 'Gouvernement de la Colombie-Britannique';
411
				$provinces['pov-man'] = 'Gouvernement du Manitoba';
412
				$provinces['pov-nb'] = 'Gouvernement du Nouveau-Brunswick';
413
				$provinces['pov-nfl'] = 'Gouvernement de Terre-Neuve-et-Labrador';
414
				$provinces['pov-ns'] = 'Gouvernement de la Nouvelle-Écosse';
415
				$provinces['pov-nwt'] = 'Gouvernement du Territoires du Nord-Ouest';
416
				$provinces['pov-nun'] = 'Gouvernement du Nunavut';
417
				$provinces['pov-ont'] = "Gouvernement de l'Ontario";
418
				$provinces['pov-pei'] = "Gouvernement de l'Île-du-Prince-Édouard";
419
				$provinces['pov-que'] = 'Gouvernement du Québec';
420
				$provinces['pov-sask'] = 'Gouvernement de Saskatchewan';
421
				$provinces['pov-yuk'] = 'Gouvernement du Yukon';
422
				$provinces['CIRNAC-RCAANC'] = 'Relations Couronne-Autochtones et Affaires du Nord Canada';
423
				$provinces['PPS-SPP'] = 'Service de Protection Parlementaire';
424
				$deptAndProvincesFr = array_merge($deptListFr, $provinces);
425
				unset($deptAndProvincesFr['ou=INAC-AANC, o=GC, c=CA']);
426
427 View Code Duplication
				if (!in_array($deptData['en'], $deptAndProvincesEn)) {
428
					$response['error'] = 5;
429
					$response['message'] = 'invalid english department name. valid names: '.json_encode($deptAndProvincesEn);
430
					return $response;
431
				}
432
433 View Code Duplication
				if (!in_array($deptData['fr'], $deptAndProvincesFr)) {
434
					$response['error'] = 5;
435
					$response['message'] = 'invalid french department name. valid names: '.json_encode($deptAndProvincesFr);
436
					return $response;
437
				}
438
439
				if ($user_entity->language === 'fr') {
440
					$user_entity->set('department', $deptData['fr'].' / '.$deptData['en']);
441
				} else {
442
					$user_entity->set('department', $deptData['en'].' / '.$deptData['fr']);
443
				}
444
				break;
445 View Code Duplication
			case 'branch':
446
				$branchData = json_decode(json_encode($value), true);
447
				if (!isset($branchData['en'])&&!isset($branchData['fr'])) {
448
					$response['error'] = 4;
449
					$response['message'] = 'invalid data format - missing english and french branch name';
450
					return $response;
451
				}
452
				if (!isset($branchData['en'])||!isset($branchData['fr'])) {
453
					$response['error'] = 4;
454
					$response['message'] = 'invalid data format - missing english or french branch name';
455
					return $response;
456
				}
457
458
				$user_entity->set('branch', json_encode($value));
459
				break;
460 View Code Duplication
			case 'sector':
461
				$sectorData = json_decode(json_encode($value), true);
462
				if (!isset($sectorData['en'])&&!isset($sectorData['fr'])) {
463
					$response['error'] = 4;
464
					$response['message'] = 'invalid data format - missing english and french sector name';
465
					return $response;
466
				}
467
				if (!isset($sectorData['en'])||!isset($sectorData['fr'])) {
468
					$response['error'] = 4;
469
					$response['message'] = 'invalid data format - missing english or french sector name';
470
					return $response;
471
				}
472
473
				$user_entity->set('sector', json_encode($value));
474
				break;
475
			case 'location':
476 View Code Duplication
				if (!isset($value['en'])) {
477
					$response['error'] = 4;
478
					$response['message'] = 'missing english location data';
479
					return $response;
480
				}
481
				$locationData = json_decode(json_encode($value['en']), true);
482 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'])) {
483
					$response['error'] = 4;
484
					$response['message'] = 'invalid location data';
485
					return $response;
486
				}
487 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'])) {
488
					$response['error'] = 4;
489
					$response['message'] = 'missing location data';
490
					return $response;
491
				}
492
493 View Code Duplication
				if (!isset($value['fr'])) {
494
					$response['error'] = 4;
495
					$response['message'] = 'missing french location data';
496
					return $response;
497
				}
498
				$locationData = json_decode(json_encode($value['fr']), true);
499 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'])) {
500
					$response['error'] = 4;
501
					$response['message'] = 'invalid location data';
502
					return $response;
503
				}
504 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'])) {
505
					$response['error'] = 4;
506
					$response['message'] = 'missing location data';
507
					return $response;
508
				}
509
510
				$user_entity->set('addressString', json_encode($value["en"]));
511
				$user_entity->set('addressStringFr', json_encode($value["fr"]));
512
				break;
513
			case 'phone':
514
515
				$user_entity->set('phone', $value);
516
				break;
517
			case 'mobile':
518
519
				$user_entity->set('mobile', $value);
520
				break;
521
			case 'email':
522
523
				elgg_set_ignore_access(true);
524
				$connection = mysqli_connect($CONFIG->dbhost, $CONFIG->dbuser, $CONFIG->dbpass, $CONFIG->dbname)or die(mysqli_error($connection));
525
				mysqli_select_db($connection, $CONFIG->dbname);
526
				$emaildomain = explode('@', filter_var($value, FILTER_SANITIZE_EMAIL));
527
				$query = "SELECT count(*) AS num FROM email_extensions WHERE ext ='".$emaildomain[1]."'";
528
529
				$result = mysqli_query($connection, $query)or die(mysqli_error($connection));
530
				$result = mysqli_fetch_array($result);
531
532
				$emailgc = explode('.', $emaildomain[1]);
533
				$gcca = $emailgc[count($emailgc) - 2] .".".$emailgc[count($emailgc) - 1];
534
535
				mysqli_close($connection);
536
537
				$resulting_error = "";
538
539 View Code Duplication
				if ($result['num'][0] <= 0) {
540
					if ($gcca !== 'gc.ca') {
541
						$resulting_error .= elgg_echo('gcRegister:invalid_email');
542
					}
543
				}
544
545
546 View Code Duplication
				if ($resulting_error !== "") {
547
					$response['error'] = 3;
548
					$response['message'] = 'invalid email or email domain - must be a valid Government of Canada email address';
549
					return $response;
550
				}
551
				$user_entity->set('email', $value);
552
				$user_entity->save();
553
554
				elgg_set_ignore_access(false);
555
				break;
556
			case 'secondLanguage':
557
558
				$user_entity->set('english', $value["ENG"]);
559
				$user_entity->set('french', $value["FRA"]);
560
				$user_entity->set('officialLanguage', $value["firstLanguage"]);
561
562
				break;
563
		}
564
	}
565
566
	$user_entity->save();
567
	return 'success';
568
}
569
570
function profileCreate($data)
571
{
572
	global $CONFIG;
573
	// check email for duplicate
574
	// get email and create username
575
	// create account
576
	// send password reset email
577
	// fill in profile data
578 View Code Duplication
	if ($data == '') {
579
		$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...
580
		$response['message'] = 'data must be a string representing a JSON object.';
581
		return $response;
582
	}
583
	$userDataObj = json_decode($data, true);
584 View Code Duplication
	if (json_last_error() !== 0) {
585
		$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...
586
		$response['message'] = 'invalid JSON - data was unable to be parsed';
587
		return $response;
588
	}
589
590
	///////////////////////////////////////////////////////////////////
591
	//error check data field
592
	///////////////////////////////////////////////////////////////////
593
	foreach ($userDataObj as $field => $value) {
594
		switch ($field) {
595
			case 'name':
596
				$nameData = json_decode(json_encode($value), true);
597 View Code Duplication
				if (!isset($nameData["firstName"])&&!isset($nameData["lastName"])) {
598
					$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...
599
					$response['message'] = 'invalid data format - missing first and last name';
600
					return $response;
601
				}
602 View Code Duplication
				if (!isset($nameData["firstName"])||!isset($nameData["lastName"])) {
603
					$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...
604
					$response['message'] = 'invalid data format - missing first or last name';
605
					return $response;
606
				}
607
608
609
				$name = $nameData["firstName"].' '.$nameData["lastName"];
610
611
				break;
612 View Code Duplication
			case 'title':
613
614
				$titleData = json_decode(json_encode($value), true);
615
				if (!isset($titleData['fr'])&&!isset($titleData['en'])) {
616
					$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...
617
					$response['message'] = 'invalid data format - missing french and english title';
618
					return $response;
619
				}
620
				if (!isset($titleData['fr'])||!isset($titleData['en'])) {
621
					$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...
622
					$response['message'] = 'invalid data format - missing french or english title';
623
					return $response;
624
				}
625
				break;
626 View Code Duplication
			case 'classification':
627
				$classificationData = json_decode(json_encode($value), true);
628
				if (!isset($classificationData['group'])&&!isset($classificationData['level'])) {
629
					$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...
630
					$response['message'] = 'invalid data format - missing classification group and level';
631
					return $response;
632
				}
633
				if (!isset($classificationData['group'])||!isset($classificationData['level'])) {
634
					$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...
635
					$response['message'] = 'invalid data format - missing classification group or level';
636
					return $response;
637
				}
638
639
				break;
640
			case 'department':
641
				$deptData = json_decode(json_encode($value), true);
642
				if (!isset($deptData['fr'])&&!isset($deptData['en'])) {
643
					$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...
644
					$response['message'] = 'invalid data format - department format';
645
					return $response;
646
				}
647
				if (!isset($deptData['fr'])||!isset($deptData['en'])) {
648
					$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...
649
					$response['message'] = 'invalid data format - missing french or english department';
650
					return $response;
651
				}
652
653
				$obj = elgg_get_entities(array(
654
					'type' => 'object',
655
					'subtype' => 'dept_list',
656
					'owner_guid' => 0
657
				));
658
				$deptListEn = json_decode($obj[0]->deptsEn, true);
659
				$provinces = array();
660
				$provinces['pov-alb'] = 'Government of Alberta';
661
				$provinces['pov-bc'] = 'Government of British Columbia';
662
				$provinces['pov-man'] = 'Government of Manitoba';
663
				$provinces['pov-nb'] = 'Government of New Brunswick';
664
				$provinces['pov-nfl'] = 'Government of Newfoundland and Labrador';
665
				$provinces['pov-ns'] = 'Government of Nova Scotia';
666
				$provinces['pov-nwt'] = 'Government of Northwest Territories';
667
				$provinces['pov-nun'] = 'Government of Nunavut';
668
				$provinces['pov-ont'] = 'Government of Ontario';
669
				$provinces['pov-pei'] = 'Government of Prince Edward Island';
670
				$provinces['pov-que'] = 'Government of Quebec';
671
				$provinces['pov-sask'] = 'Government of Saskatchewan';
672
				$provinces['pov-yuk'] = 'Government of Yukon';
673
				$deptAndProvincesEn = array_merge($deptListEn, $provinces);
674
				unset($deptAndProvincesEn['ou=INAC-AANC, o=GC, c=CA']);
675
676
				$deptListFr = json_decode($obj[0]->deptsFr, true);
677
				$provinces = array();
678
				$provinces['pov-alb'] = "Gouvernement de l'Alberta";
679
				$provinces['pov-bc'] = 'Gouvernement de la Colombie-Britannique';
680
				$provinces['pov-man'] = 'Gouvernement du Manitoba';
681
				$provinces['pov-nb'] = 'Gouvernement du Nouveau-Brunswick';
682
				$provinces['pov-nfl'] = 'Gouvernement de Terre-Neuve-et-Labrador';
683
				$provinces['pov-ns'] = 'Gouvernement de la Nouvelle-Écosse';
684
				$provinces['pov-nwt'] = 'Gouvernement du Territoires du Nord-Ouest';
685
				$provinces['pov-nun'] = 'Gouvernement du Nunavut';
686
				$provinces['pov-ont'] = "Gouvernement de l'Ontario";
687
				$provinces['pov-pei'] = "Gouvernement de l'Île-du-Prince-Édouard";
688
				$provinces['pov-que'] = 'Gouvernement du Québec';
689
				$provinces['pov-sask'] = 'Gouvernement de Saskatchewan';
690
				$provinces['pov-yuk'] = 'Gouvernement du Yukon';
691
				$deptAndProvincesFr = array_merge($deptListFr, $provinces);
692
				unset($deptAndProvincesFr['ou=INAC-AANC, o=GC, c=CA']);
693
694
695 View Code Duplication
				if (!in_array($deptData['en'], $deptAndProvincesEn)) {
696
					$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...
697
					$response['message'] = 'invalid english department name. valid names: '.json_encode($deptAndProvincesEn);
698
					return $response;
699
				}
700
701 View Code Duplication
				if (!in_array($deptData['fr'], $deptAndProvincesFr)) {
702
					$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...
703
					$response['message'] = 'invalid french department name. valid names: '.json_encode($deptAndProvincesFr);
704
					return $response;
705
				}
706
				break;
707 View Code Duplication
			case 'branch':
708
				$branchData = json_decode(json_encode($value), true);
709
				if (!isset($branchData['en'])&&!isset($branchData['fr'])) {
710
					$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...
711
					$response['message'] = 'invalid data format - missing english and french branch name';
712
					return $response;
713
				}
714
				if (!isset($branchData['en'])||!isset($branchData['fr'])) {
715
					$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...
716
					$response['message'] = 'invalid data format - missing english or french branch name';
717
					return $response;
718
				}
719
				break;
720 View Code Duplication
			case 'sector':
721
				$sectorData = json_decode(json_encode($value), true);
722
				if (!isset($sectorData['en'])&&!isset($sectorData['fr'])) {
723
					$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...
724
					$response['message'] = 'invalid data format - missing english and french sector name';
725
					return $response;
726
				}
727
				if (!isset($sectorData['en'])||!isset($sectorData['fr'])) {
728
					$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...
729
					$response['message'] = 'invalid data format - missing english or french sector name';
730
					return $response;
731
				}
732
				break;
733
			case 'location':
734 View Code Duplication
				if (!isset($value['en'])) {
735
					$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...
736
					$response['message'] = 'missing english location data';
737
					return $response;
738
				}
739
				$locationData = json_decode(json_encode($value['en']), true);
740 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'])) {
741
					$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...
742
					$response['message'] = 'invalid location data';
743
					return $response;
744
				}
745 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'])) {
746
					$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...
747
					$response['message'] = 'missing location data';
748
					return $response;
749
				}
750
751 View Code Duplication
				if (!isset($value['fr'])) {
752
					$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...
753
					$response['message'] = 'missing french location data';
754
					return $response;
755
				}
756
				$locationData = json_decode(json_encode($value['fr']), true);
757 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'])) {
758
					$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...
759
					$response['message'] = 'invalid location data';
760
					return $response;
761
				}
762 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'])) {
763
					$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...
764
					$response['message'] = 'missing location data';
765
					return $response;
766
				}
767
				break;
768
			case 'email':
769
				$connection = mysqli_connect($CONFIG->dbhost, $CONFIG->dbuser, $CONFIG->dbpass, $CONFIG->dbname)or die(mysqli_error($connection));
770
				mysqli_select_db($connection, $CONFIG->dbname);
771
				$emaildomain = explode('@', filter_var($value, FILTER_SANITIZE_EMAIL));
772
773
				$query = "SELECT count(*) AS num FROM email_extensions WHERE ext ='".$emaildomain[1]."'";
774
775
				$result = mysqli_query($connection, $query)or die(mysqli_error($connection));
776
				$result = mysqli_fetch_array($result);
777
778
				$emailgc = explode('.', $emaildomain[1]);
779
				$gcca = $emailgc[count($emailgc) - 2] .".".$emailgc[count($emailgc) - 1];
780
781
				mysqli_close($connection);
782
783
				$resulting_error = "";
784
785
				// if domain doesn't exist in database, check if it's a gc.ca domain
786 View Code Duplication
				if ($result['num'][0] <= 0) {
787
					if ($gcca !== 'gc.ca') {
788
						$resulting_error .= elgg_echo('gcRegister:invalid_email');
789
					}
790
				}
791
792 View Code Duplication
				if ($resulting_error !== "") {
793
					$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...
794
					$response['message'] = 'invalid email or email domain - must be a valid Government of Canada email address';
795
					return $response;
796
				}
797
				break;
798
		}
799
	}
800
801
	//check for existing email
802
	$email = $userDataObj['email'];
803
	if (get_user_by_email($email)) {
804
		$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...
805
		$response['message'] = 'user with email already exists. please use profile.update call to update existing account';
806
		return $response;
807
	}
808
	//make usernaem based on email
809
	$username = strstr(strtolower($email), '@', true);
810
811
	$username = explode('.', $username);
812
	foreach ($username as $u=>$v) {
813
		$username[$u] = ucfirst($v);
814
	}
815
	$username = implode('.', $username);
816
817
	//check system for username. if is a username, append number or add number
818
	while (get_user_by_username($username)) {
819
		if (is_numeric(substr($username, -1))) {
820
			$num = substr($username, -1)+1;
821
			$username = substr($username, 0, strlen($username)-1).$num;
822
		} else {
823
			$username.='2';
824
		}
825
	}
826
	$tempPass = generateRandomString();
827
828
	//register user using data passed
829
	$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...
830 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...
831
		$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...
832
		$response['message'] = 'Failed creating account';
833
		return $response;
834
	}
835
836
	$user_entity = get_user($userGUID);
837
838
	foreach ($userDataObj as $field => $value) {
839
		switch ($field) {
840 View Code Duplication
			case 'title':
841
				$titleData = json_decode(json_encode($value), true);
842
843
				if ($user_entity->language === 'fr') {
844
					$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...
845
				} else {
846
					$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...
847
				}
848
849
				break;
850
			case 'classification':
851
				$classificationData = json_decode(json_encode($value), true);
852
853
				$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...
854
				break;
855 View Code Duplication
			case 'department':
856
				$deptData = json_decode(json_encode($value), true);
857
858
				if ($user_entity->language === 'fr') {
859
					$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...
860
				} else {
861
					$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...
862
				}
863
				break;
864
			case 'branch':
865
				$branchData = json_decode(json_encode($value), true);
866
867
				$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...
868
				break;
869
			case 'sector':
870
				$sectorData = json_decode(json_encode($value), true);
871
872
				$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...
873
				break;
874
			case 'location':
875
876
				$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...
877
				$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...
878
				break;
879
			case 'phone':
880
881
				$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...
882
				break;
883
			case 'mobile':
884
885
				$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...
886
				break;
887
		}
888
	}
889
	//save user
890
	$user_entity->save();
891
	//send password reset to user
892
	send_new_password_request($userGUID);
893
	return array(
894
		"guid"=> $userGUID,
895
		"message" => "user added"
896
	);
897
}
898
function generateRandomString($length = 10)
899
{
900
	return substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)))), 1, $length);
901
}
902
903
function getUserFromID($id)
904
{
905
	if (is_numeric($id)) {
906
		$user_entity = get_user($id);
907
	} else {
908
		if (strpos($id, '@')) {
909
			$user_entity = get_user_by_email($id);
910
			if (is_array($user_entity)) {
911
				if (count($user_entity)>1) {
912
					return "Found more than 1 user, please use username or GUID";
913
				} else {
914
					$user_entity = $user_entity[0];
915
				}
916
			}
917
		} else {
918
			$user_entity = get_user_by_username($id);
919
		}
920
	}
921
	return $user_entity;
922
}
923
924 View Code Duplication
function buildDate($month, $year)
925
{
926
	switch ($month) {
927
		case 1:
928
			$string = "01/";
929
			break;
930
		case 2:
931
			$string = "02/";
932
			break;
933
		case 3:
934
			$string = "03/";
935
			break;
936
		case 4:
937
			$string = "04/";
938
			break;
939
		case 5:
940
			$string = "05/";
941
			break;
942
		case 6:
943
			$string = "06/";
944
			break;
945
		case 7:
946
			$string = "07/";
947
			break;
948
		case 8:
949
			$string = "08/";
950
			break;
951
		case 9:
952
			$string = "09/";
953
			break;
954
		case 10:
955
			$string = "10/";
956
			break;
957
		case 11:
958
			$string = "11/";
959
			break;
960
		case 12:
961
			$string = "12/";
962
			break;
963
	}
964
	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...
965
}
966