senses-designertravelv3 /application/helpers/users_helper.php

Language PHP Lines 556
MD5 Hash 6d1b4a6947fb61815ba5cda0f42d8551 Estimated Cost $8,677 (why?)
Repository https://bitbucket.org/matyhaty/senses-designertravelv3 View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
<?php

/**
 * User helper function for seeing if the user is logged in
 */
if (!function_exists('isLoggedIn'))
{

	function isLoggedIn($session)
	{
		$user = $session -> all_userdata();

		if (!isset($user['user_data']['username']) || !isset($user['user_data']['email']))
			return false;
		return $user['user_data'];

	}

}

/* Function checks whether the user has permission to access a function
 */

function check_permissions(DataMapper $user = NULL, $uri = null)
{

	static $allowedURIS;

	if (isset($allowedURIS[$uri]))
	{
		// this uri has allowed been allowed! cool trick aye
		return true;
	}

	// 0 non registered
	// 1 registered
	// 2 special guest
	// 3 some other account
	// 9 admin account

	//$perms['user/profile/index']	= array( 'user_type' => array( 1=>1 , 3=>3 ) , 'fail_hook'=>$failHook = array(), 'success'=>$successHook = array() , 'logged_in' => 1 );
	//$perms['user/account/index']	= array( 'user_type' => array( 1=>1 , 3=>3 ) , 'fail_hook'=>$failHook = array(), 'success'=>$successHook = array() , 'logged_in' => 1 );
	//$perms['event/events/create']	= array( 'user_type' => array( 1=>1 , 3=>3 ) , 'fail_hook'=>$failHook = array(), 'success'=>$successHook = array() , 'logged_in' => 1 );
	$perms['admin/login'] = array(
		'user_type' => array(0 => 0),
		'fail_hook' => $failHook = array(),
		'success' => $successHook = array(),
		'logged_in' => 2
	);
	
	$perms['admin/home'] = array(
		'user_type' => array(1 => 1),
		'fail_hook' => $failHook = array(),
		'success' => $successHook = array(),
		'logged_in' => 1
	);

	$perms['admin/destinations/list'] = array(
		'user_type' => array(1 => 1),
		'fail_hook' => $failHook = array(),
		'success' => $successHook = array(),
		'logged_in' => 1
	);
	$perms['admin/itinerary/list'] = array(
		'user_type' => array(1 => 1),
		'fail_hook' => $failHook = array(),
		'success' => $successHook = array(),
		'logged_in' => 1
	);
	$perms['admin/user/view'] = array(
		'user_type' => array(1 => 1),
		'fail_hook' => $failHook = array(),
		'success' => $successHook = array(),
		'logged_in' => 1
	);

	$perms['admin/communications/list'] = array(
		'user_type' => array(1 => 1),
		'fail_hook' => $failHook = array(),
		'success' => $successHook = array(),
		'logged_in' => 1
	);

	//$perms['user/account']           = array( 'user_type' => array( 1=>1 ) , 'fail_hook'=>$failHook = array(), 'success'=>$successHook = array() , 'logged_in' => 1 );

	// could probably be moved to its own seperate config file!

	// perm usertype, key and val should always be the the same
	$uri = $uri ? $uri : uri_string(current_url());
	$uriArr = explode('/', $uri, 4);
	unset($uriArr[3]);
	// we are only interested in the first 3 segments
	$uri = implode('/', $uriArr);

	if (isset($perms[$uri]))
	{

		// they must be logged in first
		if ($perms[$uri]['logged_in'] == 1 && !$user -> id)
		{

			return 'must_be_logged_in';

		}

		// they cant be logged in to view this page, useful for the login page for example
		if ($perms[$uri]['logged_in'] == 2 && $user -> id)
		{

			return 'must_be_logged_in_out';
		}

		// check if the current uri has a permission, use isset for speed
		// admin overrules all
		if (isset($perms[$uri]['user_type'][(int)$user -> user_type]) || $user -> user_type == ADMIN_USER_TYPE)
		{

			// looks complicated but quite simple
			// check the $perms array against user_type array against the key of the actual users user_type
			// if it return true the have permission
			// run success hook
			$allowedURIS[$uri] = 1;
			return true;
		}
		else
		{
			//print_r($perms[$uri]);
			//echo '<hr><pre>';
			//print_r( $user -> all_to_array());
			return false;
			//run fail hook
		}// no acces
	}
	else
	{
		// we are only interested in the first 2 segments and wildcards
		unset($uriArr[2]);
		$uriWildcard = implode('/', $uriArr);
		$uriWildcard .= '/any';

		if (isset($perms[$uriWildcard]))
		{
			//$CI = &get_instance();
			//$CI -> firephp -> error("Wildcard match: " . $uriWildcard);
			// they must be logged in first
			if ($perms[$uriWildcard]['logged_in'] == 1 && !$user -> id)
			{

				return 'must_be_logged_in';

			}

			// they cant be logged in to view this page, useful for the login page for example
			if ($perms[$uriWildcard]['logged_in'] == 2 && $user -> id)
			{

				return 'must_be_logged_in_out';
			}

			// check if the current uri has a permission, use isset for speed
			// admin overrules all
			if (isset($perms[$uriWildcard]['user_type'][(int)$user -> user_type]) || $user -> user_type == ADMIN_USER_TYPE)
			{

				// looks complicated but quite simple
				// check the $perms array against user_type array against the key of the actual users user_type
				// if it return true the have permission
				// run success hook
				$allowedURIS[$uriWildcard] = 1;
				return true;
			}
			else
			{
				return false;
				//run fail hook
			}// no acces
		}
		else
		{
			$CI = &get_instance();
			$CI -> firephp -> error("No Permission settings for URL: " . $uri . " or " . $uriWildcard);
			$allowedURIS[$uri] = 1;
			// if not set has no perm association so return true
			//return true;
			
				return true;
			
		}
	}

}

/**
 * All links should use this function to utilise the basic ACL
 *
 * @param DataMapper $user
 * @paramString $uri
 * @param Boolean $returnURI
 * @param String $permURI
 * @return varies
 */
function checkURIPerm(DataMapper $user, $uri, $returnURI = true, $permURI = '')
{

	//if(!$uri) throw new Exception('URI must be sent to this checkURIPerm function');

	$uri = $permURI != '' ? $permURI : $uri;
	// add flexibilty for uri which have ids in them
	//$CI = &get_instance();
	//		$CI -> firephp -> error("not allowed: ".$uri);

	if (check_permissions($user, $uri))
	{
		if ($returnURI)
		{
			return site_url($uri);
		}
		else
		{
			return true;
		}
	}

	if ($returnURI)
	{
		//	return site_url('noaccess');
	}
	else
	{
		return false;
	}

}

function slir($path, $image, $width, $height, array $crop = array(), $attri = '', $uriOnly = false)
{
	$slircode = '';
	$slircode .= 'w' . $width . '-';
	$slircode .= 'h' . $height;

	if (isset($crop['width']) && isset($crop['height']))
		$slircode .= '-c' . $crop['width'] . ':' . $crop['height'];

	$imageRel = $path . $image;
	$imageAbs = FRONT_END_PATH . '/uploads/' . $path . $image;
	$imageAbsBackup = FRONT_END_PATH . '/uploads/' . $path . 'profile-image.png';
	$imageAbsFullback = FRONT_END_PATH . '/uploads/fallback-image.png';
	// the absolute path
	if (file_exists($imageAbs) && $image != "")
	{
		if ($uriOnly)
			return SLIR_URL . $slircode . $imageRel;
		$base = '<img src="' . SLIR_URL . $slircode . '/uploads' . $imageRel . '" ' . $attri . ' />';
	}
	
else
	if (file_exists($imageAbsBackup) && $image != "")
	{
		//echo $imageAbs;
		if ($uriOnly)
			return SLIR_URL . $slircode . $path . 'profile-image.png';
		$base = '<img src="' . SLIR_URL . $slircode . '/uploads' . $path . 'profile-image.png" alt="File not found: ' . $path . $image . '" />';
	}
	else
	{
		if ($uriOnly)
			return SLIR_URL . $slircode . 'fallback-image.png';
		$base = '<img src="' . SLIR_URL . $slircode . '/uploads/fallback-image.png" alt="File not found: ' . $path . $image . '" />';
	}

	return $base;
}

function slirPath($image, $slircode, $data = NULL)
{
	$imageAbs = FRONT_END_PATH . '/uploads/' . $image;
	//echo SLIR_URL . '?' . $slircode . '&i=' . UPLOAD_URL_ . $image;
	if (file_exists($imageAbs) && !is_dir($imageAbs))
	{
		$base = SLIR_URL . '?' . $slircode . '&i=' . UPLOAD_URL . $image;
	}
	else
	{
		$base = SLIR_URL . '?' . $slircode . '&i=' . UPLOAD_URL . 'placeholder.jpg';
	}

	return $base;
}

/**
 * All links should pass through this function
 *
 * @param DataMapper $user
 * @param unknown_type $uri
 * @param unknown_type $text
 * @param unknown_type $attribs
 * @return unknown
 */
function permAnchor(DataMapper $user, $uri, $text, $attribs = array())
{
	return anchor(checkURIPerm($user, $uri), $text, $attribs);
}

/**
 * Truncate string
 *
 * @param String $string
 * @param Int $your_desired_width
 * @return String
 */
function tokenTruncate($string, $your_desired_width)
{
	$parts = preg_split('/([\s\n\r]+)/', $string, null, PREG_SPLIT_DELIM_CAPTURE);
	$parts_count = count($parts);

	$length = 0;
	$last_part = 0;
	for (; $last_part < $parts_count; ++$last_part)
	{
		$length += strlen($parts[$last_part]);
		if ($length > $your_desired_width)
		{
			break;
		}
	}

	return implode(array_slice($parts, 0, $last_part));
}

/**
 * Get a language string
 *
 * @param String $langStr
 * @return String
 */
function l($langStr)
{
	static $CI;
	if ($CI)
		return $CI -> lang -> line($langStr);
	$CI = get_instance();
	return $CI -> lang -> line($langStr);
}

/**
 * Returns the number of images for the supplied user
 *
 */
function get_user_image_count($userid = 0)
{
	if ($userid == 0)
		return 0;
}

/**
 * Returns the number of classes for the supplied user
 *
 */
function get_user_class_count($userid = 0)
{
	if ($userid == 0)
		return 0;

	$user = new User();
	$user -> where("id", $userid) -> get();
	$user -> lecture -> get();
	return ($user -> lecture -> count() != 0 ? $user -> lecture -> count() : 0);
}

/**
 * Returns the number of events for the supplied user
 *
 */
function get_user_event_count($userid = 0)
{
	if ($userid == 0)
		return 0;

	$user = new User();
	$user -> where("id", $userid) -> get();
	$user -> event -> get();
	return ($user -> event -> count() != 0 ? $user -> event -> count() : 0);
}

/**
 * Returns the number of jobs for the supplied user
 *
 */
function get_user_job_count($userid = 0)
{
	if ($userid == 0)
		return 0;

	$user = new User();
	$user -> where("id", $userid) -> get();
	$user -> job -> get();
	return ($user -> job -> count() != 0 ? $user -> job -> count() : 0);
}

/**
 * Returns the number of groups for the supplied user
 *
 */
function get_user_group_count($userid = 0)
{
	if ($userid == 0)
		return 0;

	$user = new User();
	$user -> where("id", $userid) -> get();
	$user -> group -> get();
	return ($user -> group -> count() != 0 ? $user -> group -> count() : 0);
}

function isAdmin($id)
{
	$u = new User();
	$obj = $u -> getObject('user', $id, null, false, array('state' => 'Active'), '', '') -> dm_object;
	if ($obj -> user_admin)
	{
		return true;
	}
	else
	{
		return false;
	}
}

/**
 * Returns the number of questions for the supplied user
 *
 */
function get_user_question_count($userid = 0)
{
	if ($userid == 0)
		return 0;

	$user = new User();
	$user -> where("id", $userid) -> get();
	$user -> group -> get();
	return ($user -> question -> count() != 0 ? $user -> question -> count() : 0);
}

/**
 * Get the user's fullname
 *
 */
function get_user_fullname($userid = 0)
{
	$user = new User();
	$user -> where("id", $userid) -> get();

	return $user -> firstname . " " . $user -> surname;
}

/**
 * The time since something was done by a user, or object
 *
 * @param UNIX time stamp
 * @return STRING
 */
function time_since($original)
{
	// array of time period chunks
	$chunks = array(
		array(
			60 * 60 * 24 * 365,
			'year'
		),
		array(
			60 * 60 * 24 * 30,
			'month'
		),
		array(
			60 * 60 * 24 * 7,
			'week'
		),
		array(
			60 * 60 * 24,
			'day'
		),
		array(
			60 * 60,
			'hour'
		),
		array(
			60,
			'minute'
		),
	);

	$today = time();
	/* Current unix time  */
	$since = $today - $original;

	if ($since > 604800)
	{
		$print = date("M jS", $original);

		if ($since > 31536000)
		{
			$print .= ", " . date("Y", $original);
		}

		return $print;

	}

	// $j saves performing the count function each time around the loop
	for ($i = 0, $j = count($chunks); $i < $j; $i++)
	{

		$seconds = $chunks[$i][0];
		$name = $chunks[$i][1];

		// finding the biggest chunk (if the chunk fits, break)
		if (($count = floor($since / $seconds)) != 0)
		{
			// DEBUG print "<!-- It's $name -->\n";
			break;
		}
	}

	$print = ($count == 1) ? '1 ' . $name : "$count {$name}s";

	return $print . " ago";

}

/**
 * Get the number of inbox messages for the user
 *
 */
function get_user_pms_count($uid, $state = "active", $created_by = null, $userid_to = null)
{
	$user = new User();

	// Get the users messages
	$user -> where("id", $uid);
	$user -> get();

	// Get the messages
	$messages = new Message();
	$messages -> where('state', $state);

	// handle user id to field
	if ($userid_to != 0)
		$messages -> where('userid_to', $userid_to);

	// handle created by field
	if ($created_by != 0)
		$messages -> where('created_by', $created_by);

	return $messages -> count();
}
Back to Top