Test Failed
Push — master ( 7333bd...9261fc )
by Justin
04:07
created

Mail_Verification::sendMail()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 28
rs 8.8571
cc 1
eloc 14
nc 1
nop 3
1
<?php
2
3
/**
4
 * Copyright (c) 2018 Justin Kuenzel (jukusoft.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
20
/**
21
 * Project: JuKuCMS
22
 * License: Apache 2.0 license
23
 * User: Justin
24
 * Date: 06.04.2018
25
 * Time: 15:25
26
 */
27
28
class Mail_Verification {
29
30
	public static function sendMail (int $userID, string $username, string $mail) {
31
		//generate token
32
		$token = PHPUtils::randomString(64);
33
34
		Database::getInstance()->execute("INSERT INTO `{praefix}register_mail_verification` (
35
			`userID`, `token`
36
		) VALUES (
37
			:userID, :token
38
		) ON DUPLICATE KEY UPDATE `token` = :token; ", array(
39
			'userID' => $userID,
40
			'token' => $token
41
		));
42
43
		//send mail
44
		$template = new DwooTemplate(STORE_PATH . "templates/mail/verify_mail.tpl");
45
46
		//assign variables
47
		$template->assign("token", $token);
48
		$template->assign("userID", $userID);
49
		$template->assign("username", $username);
50
		$template->assign("verify_url", DomainUtils::generateURL("user/verify_mail", array('token' => $token)));
51
		$template->assign("base_url", DomainUtils::getBaseURL());
52
		$template->assign("mail", $mail);
53
54
		$message = $template->getCode();
55
56
		//send mail
57
		MailApi::sendHTMLMail($mail, "Mail Verification " . Settings::get("website_name", ""), $message);
58
	}
59
60
	public static function checkToken (string $token) : bool {
61
		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}register_mail_verification` WHERE `token` = :token; ", array('token' => $token));
62
63
		if ($row === false) {
64
			return false;
65
		} else {
66
			//activate user
67
			Database::getInstance()->execute("UPDATE `{praefix}user` SET `activated` = '1' WHERE `userID` = :userID AND `activated` = '2'; ", array('userID' => $row['userID']));
68
69
			//remove token
70
			self::removeToken($token);
71
72
			return true;
73
		}
74
	}
75
76
	public static function removeToken (string $token) {
77
		Database::getInstance()->execute("DELETE FROM `{praefix}register_mail_verification` WHERE `token` = :token; ", array('token' => $token));
78
	}
79
80
}
81
82
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
83