View file application/system/library/class.usersettings.php

File size: 4.63Kb
<?PHP
	class UserSettings {
		public $uploadMaxSize = '5M';
		
		public $allowedMimeTypes = [
			/* Картинки */
			'image/jpeg',																			/* JPEG / JPG */
			'image/png',																			/* PNG */
			'image/gif',																			/* GIF */
			'image/webp'																			/* WEBP */
		];
		
		/*
			Загрузка и изменение Аватара
		*/
		public function changeImage(array $File = []) {
			try {
				$allowedMimeTypes = $this->allowedMimeTypes;
				$uploadMaxSizeBytes = return_bytes($this->uploadMaxSize);
				$uploadedFileType = mime_content_type($File['image']['tmp_name']);
				
				if(!in_array($uploadedFileType, $allowedMimeTypes)) {
					throw new Exception(getLang('settings_error_expansion'));
				}
				
				if($File['image']['size'] > $uploadMaxSizeBytes) {
					$endMessage = getLang('settings_error_size', [$this->uploadMaxSize]);
				}
				
				$uploadDirectory = '/public/images/avatars/' . $_SESSION['id'] . '/';
				
				if(!is_dir($_SERVER['DOCUMENT_ROOT'] . $uploadDirectory)) {
					mkdir($_SERVER['DOCUMENT_ROOT'] . $uploadDirectory);
				}
					
				$fileExtension = pathinfo($File['image']['name'], PATHINFO_EXTENSION);
				$uniqueFileName = uniqid() . '_' . rand(1000, 99999) . '.' . $fileExtension;
				$fullPath = $uploadDirectory . $uniqueFileName;
				
				if(move_uploaded_file($File['image']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . $fullPath)) {
					$sth = pdo()->prepare('UPDATE `users` SET `image`=:image WHERE `id`=:userid LIMIT 1');
					$sth->execute([
						':image' => $_SESSION['id'] . '/' . $uniqueFileName,
						':userid' => $_SESSION['id']
					]);
					
					return $fullPath;
				}
			}
			catch(Exception $e) {
				throw new Exception($e->getMessage());
			}
		}
		
		/*
			Изменение имени и фамилии
		*/
		public function editName($_ARRAY = []) {
			try {
				/* Проверка имени */
				$_ARRAY['first_name'] = TextClear($_ARRAY['first_name']);
					
				if(empty($_ARRAY['first_name'])) {
					throw new Exception(getLang('settings_error_firstname'));
				}
					
				$_ARRAY['first_name'] = trim($_ARRAY['first_name']);
					
				/* Проверка фамилии */
				$_ARRAY['last_name'] = TextClear($_ARRAY['last_name']);
					
				if(empty($_ARRAY['last_name'])) {
					throw new Exception(getLang('settings_error_lastname'));
				}
					
				$_ARRAY['last_name'] = trim($_ARRAY['last_name']);
				
				$sth = pdo()->prepare('UPDATE `users` SET `first_name`=:first_name, `last_name`=:last_name WHERE `id`=:id LIMIT 1');
				$sth->execute([
					':id' => $_SESSION['id'],
					':first_name' => $_ARRAY['first_name'],
					':last_name' => $_ARRAY['last_name']
				]);
				
				return true;
			}
			catch(Exception $e) {
				throw new Exception($e->getMessage());
			}
		}
		
		/*
			Изменение языка
		*/
		public function changeLanguage($_LANG) {
			try {
				$sth = pdo()->prepare('UPDATE `users` SET `language`=:language WHERE `id`=:userid LIMIT 1');
				$sth->execute([
					':language' => $_LANG,
					':userid' => $_SESSION['id']
				]);
			}
			catch(Exception $e) {
				throw new Exception($e->getMessage());
			}
		}
		
		/*
			Изменение пароля
		*/
		public function editPassword($_ARRAY = []) {
			try {
				if($_ARRAY['new_password'] != $_ARRAY['confirm_password']) {
					throw new Exception(getLang('settings_error_password'));
				}
				
				if($_ARRAY['new_password'] == $_ARRAY['last_password']) {
					throw new Exception(getLang('settings_error_new_password'));
				}
				
				$dataUser = users()->Get($_SESSION['id']);
				
				if(!password_verify($_ARRAY['last_password'], $dataUser->password)) {
					throw new Exception(getLang('settings_error_last_password'));
				}
				
				$sth = pdo()->prepare('UPDATE `users` SET `password`=:password, `date_password_change`=:date_password_change WHERE `id`=:id LIMIT 1');
				$sth->execute([
					':password' => password_hash($_ARRAY['new_password'], PASSWORD_DEFAULT),
					':date_password_change' => time(),
					':id' => $_SESSION['id']
				]);
				
				return true;
			}
			catch(Exception $e) {
				throw new Exception($e->getMessage());
			}
		}
		
		/*
			Список разрешенного формата
		*/
		public function AcceptList() {
			$iLast = count($this->allowedMimeTypes) - 1;
			$lists = '';
			
			for($i = 0; $i < count($this->allowedMimeTypes); $i++) {
				if($i == $iLast) {
					$lists .= $this->allowedMimeTypes[$i];
				}
				else {
					$lists .= $this->allowedMimeTypes[$i] . ', ';
				}
			}
			
			return $lists;
		}
	}