Completed
Push — master ( df8ec4...96358d )
by Nazar
04:25
created

profile   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 16
lcom 0
cbo 5
dl 0
loc 74
rs 10
c 1
b 1
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A profile___get() 0 17 2
C profile___patch() 0 51 13
A profile_contacts_get() 0 4 1
1
<?php
2
/**
3
 * @package    CleverStyle CMS
4
 * @subpackage System module
5
 * @category   modules
6
 * @author     Nazar Mokrynskyi <[email protected]>
7
 * @copyright  Copyright (c) 2015, Nazar Mokrynskyi
8
 * @license    MIT License, see license.txt
9
 */
10
namespace cs\modules\System\api\Controller;
11
use
12
	cs\Config,
13
	cs\ExitException,
14
	cs\Language,
15
	cs\Page,
16
	cs\User;
17
trait profile {
18
	static function profile___get () {
19
		$User = User::instance();
20
		if ($User->guest()) {
21
			throw new ExitException(403);
22
		}
23
		$fields = [
24
			'id',
25
			'login',
26
			'username',
27
			'language',
28
			'timezone',
29
			'avatar'
30
		];
31
		Page::instance()->json(
32
			$User->get($fields, $User->id)
33
		);
34
	}
35
	static function profile___patch () {
36
		if (
37
			!isset($_POST['login'], $_POST['username'], $_POST['language'], $_POST['timezone'], $_POST['avatar']) ||
38
			!$_POST['login']
39
		) {
40
			throw new ExitException(400);
41
		}
42
		$Config = Config::instance();
43
		$L      = Language::instance();
44
		$User   = User::instance();
45
		if ($User->guest()) {
46
			throw new ExitException(403);
47
		}
48
		$user_data = [
49
			'login'    => $_POST['login'],
50
			'username' => $_POST['username'],
51
			'language' => $_POST['language'],
52
			'timezone' => $_POST['timezone'],
53
			'avatar'   => $_POST['avatar']
54
		];
55
		$user_data = xap($user_data, false);
56
		if (
57
			(
58
				!in_array($user_data['timezone'], get_timezones_list()) &&
59
				$user_data['timezone'] !== ''
60
			) ||
61
			(
62
				!in_array($user_data['language'], $Config->core['active_languages']) &&
63
				$user_data['language'] !== ''
64
			)
65
		) {
66
			throw new ExitException(400);
67
		}
68
		$user_data['login'] = mb_strtolower($user_data['login']);
69
		/**
70
		 * Check for changing login to new one and whether it is available
71
		 */
72
		if (
73
			$user_data['login'] != $User->login &&
74
			$user_data['login'] != $User->email &&
75
			(
76
				filter_var($user_data['login'], FILTER_VALIDATE_EMAIL) ||
77
				$User->get_id(hash('sha224', $user_data['login'])) !== false
78
			)
79
		) {
80
			throw new ExitException($L->login_occupied, 400);
81
		}
82
		if (!$User->set($user_data)) {
83
			throw new ExitException(500);
84
		}
85
	}
86
	static function profile_contacts_get () {
87
		$User = User::instance();
88
		Page::instance()->json($User->get_contacts());
89
	}
90
}
91