Files
slava.home/main_plugin/auth/func.auth.php

82 lines
3.3 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @file func.auth.php
* @brief Функции для регистрации пользователей и отправки уведомлений по email
*/
/**
* @brief Регистрирует нового пользователя
* @param array $params Массив с ключами 'login', 'pass', 'email'
* @return string "name_exists" если пользователь уже есть, "true" если регистрация прошла успешно
* @throws Exception Если произошла ошибка при создании запроса на регистрацию
*/
function registerUser($params) {
global $path, $config;
$exists = false;
$requestFile = simplexml_load_file($path . $config['usersRequest']);
foreach ($requestFile->users->user as $child) {
if ((string)$child['name'] === $params['login']) {
$exists = true;
break;
}
}
$usersFile = simplexml_load_file($path . $config['users']);
foreach ($usersFile->users->user as $child) {
if ((string)$child['name'] === $params['login']) {
$exists = true;
break;
}
}
if ($exists) {
return "name_exists";
} else {
$requestFile = simplexml_load_file($path . $config['usersRequest']);
$requestFilePath = $path . $config['usersRequest'];
$newUser = $requestFile->users->addChild('user');
$newUser->addAttribute('name', $params['login']);
$newUser->addAttribute('pass', $params['pass']);
$newUser->addAttribute('access', '');
$newUser->addAttribute('email', $params['email']);
$newUser->addAttribute('link', md5($params['login'].$params['pass']));
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($requestFile->asXML());
if ($dom->save($requestFilePath)) {
return sendRegistrationEmail($params);
} else {
throw new Exception("Error while creating user request", -32003);
}
}
}
/**
* @brief Отправляет уведомление о регистрации пользователю и администратору
* @param array $paramsEmail Массив с ключами 'login', 'pass', 'email'
* @return string "true" при успешной отправке
* @throws Exception Если произошла ошибка при отправке email
*/
function sendRegistrationEmail($paramsEmail) {
global $config;
$to = $config['emailAdmin'];
$subject = '=?UTF-8?B?'.base64_encode('Запрос на создание аккаунта на сайте ').'?=' . $_SERVER['HTTP_HOST'];
$message = 'Логин: ' . $paramsEmail['login'] . "\r\n";
$message .= 'Пароль: ' . $paramsEmail['pass'] . "\r\n";
$message .= 'Почта: ' . $paramsEmail['email'] . "\r\n";
$message .= 'Сайт: ' . $config['server'] . "\r\n";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
$headers .= "From: info@oblat.lv";
if (!mail($to, $subject, $message, $headers)) {
throw new Exception("Error sending email", -32002);
}
return "true";
}
?>