Passed
Push — master ( 0561d0...39cfaa )
by Justin
04:49 queued 34s
created

SendMailPage   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 3
Metric Value
wmc 16
c 4
b 1
f 3
dl 0
loc 91
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAdditionalHeaderCode() 0 8 1
C getContent() 0 63 14
A getFooterScripts() 0 7 1
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: 24.04.2018
25
 * Time: 21:25
26
 */
27
28
class SendMailPage extends PageType {
29
30
	public function getAdditionalHeaderCode(): string {
31
		$base_url = DomainUtils::getBaseURL() . "/";
32
33
		return "<!-- iCheck -->
34
  				<link rel=\"stylesheet\" href=\"" . $base_url . "styles/admin/plugins/iCheck/flat/blue.css\">
35
		
36
				<!-- bootstrap wysihtml5 - text editor -->
37
  				<link rel=\"stylesheet\" href=\"" . $base_url . "styles/admin/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css\">";
38
	}
39
40
	public function getFooterScripts(): string {
41
		$base_url = DomainUtils::getBaseURL() . "/";
42
43
		return "<!-- iCheck -->
44
				<script src=\"" . $base_url . "styles/admin/plugins/iCheck/icheck.min.js\"></script>
45
				<!-- Bootstrap WYSIHTML5 -->
46
				<script src=\"" . $base_url . "styles/admin/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js\"></script>
47
				<!-- Page Script -->
48
				<script>
49
				  $(function () {
50
					//Add text editor
51
					$(\"#compose-textarea\").wysihtml5();
52
				  });
53
				</script>";
54
	}
55
56
	public function getContent(): string {
57
		$template = new DwooTemplate("pages/sendmail");
58
59
		$template->assign("action_url", DomainUtils::generateURL("pages/sendmail"));
60
61
		if (isset($_REQUEST['submit'])) {
62
			//first, check csrf token
63
			if (!Security::checkCSRFToken()) {
64
				$template->assign("error_message", "Wrong CSRF token!");
65
66
				if (isset($_POST['content'])) {
67
					$template->assign("content", $_POST['content']);
68
				}
69
			} else {
70
				$required_fields = array("to_mail", "subject", "content");
71
72
				foreach ($required_fields as $field) {
73
					if (!isset($_POST[$field]) || empty($_POST['field'])) {
74
						$template->assign("error_message", "Please complete form!");
75
76
						if (isset($_POST['content'])) {
77
							$template->assign("content", $_POST['content']);
78
						}
79
80
						return $template->getCode();
81
					}
82
				}
83
84
				//form is complete
85
				$to_mail = $_POST['to_mail'];
86
				$subject = $_POST['subject'];
87
				$content = $_POST['content'];
88
89
				//check, if mail is valide
90
				if (!(new Validator_Mail())->isValide($to_mail)) {
91
					$template->assign("error_message", "Mail is not valide!");
92
93
					if (isset($_POST['content'])) {
94
						$template->assign("content", $_POST['content']);
95
					}
96
				} else if (!(new Validator_String())->isValide($subject)) {
97
					$template->assign("error_message", "Subject is not valide!");
98
99
					if (isset($_POST['content'])) {
100
						$template->assign("content", $_POST['content']);
101
					}
102
				} else {
103
					//parameters are valide, send mail
104
105
					if (MailApi::sendHTMLMail($to_mail, $subject, $content)) {
106
						$template->assign("success_message", "Mail sended successfully!");
107
					} else {
108
						$template->assign("error_message", "Sending of mail failed!");
109
110
						if (isset($_POST['content'])) {
111
							$template->assign("content", $_POST['content']);
112
						}
113
					}
114
				}
115
			}
116
		}
117
118
		return $template->getCode();
119
	}
120
121
}
122
123
?>
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...
124