-
Notifications
You must be signed in to change notification settings - Fork 3
/
contact.php
executable file
·107 lines (90 loc) · 2.75 KB
/
contact.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
require_once("db.php"); /* Database Class */
require_once('utils/is_email.php'); /* Email Validation Script */
/* Handle Ajax Request */
if(isset($_POST['newcontact'])){
$contact = new Contact();
unset($contact);
}
else{
header('Location: /');
}
/* Class Contact */
class Contact{
private $db; /* the database obj */
private $errors = array(); /* holds error messages */
private $num_errors; /* number of errors in submitted form */
public function __construct(){
$this->db = new DB();
if(isset($_POST['newcontact']))
$this->processNewMessage();
else
header("Location: /");
}
public function processNewMessage(){
$email = $_POST['email'];
$name = $_POST['name'];
$url = $_POST['url'];
$message = $_POST['message'];
/* Server Side Data Validation */
/* Email Validation */
if(!$email || mb_strlen($email = trim($email)) == 0)
$this->setError('email','required field');
else{
if(!is_email($email))
$this->setError('email', 'invalid email');
else if(mb_strlen($email) > 120)
$this->setError('email', 'too long! 120');
}
/* Name Validation */
if(!$name || mb_strlen($name = trim($name)) == 0)
$this->setError('name', 'required field');
else if(mb_strlen(trim($name)) > 120)
$this->setError('name', 'too long! 120 characters');
/* Message Validation */
if(!$message || mb_strlen($message = trim($message)) == 0)
$this->setError('message','required field');
elseif(mb_strlen($message) > 300)
$this->setError('message', 'too long! 300 characters');
/* Errors exist */
if($this->countErrors() > 0){
$json = array(
'result' => -1,
'errors' => array(
array('name' => 'email' ,'value' => $this->error_value('email')),
array('name' => 'name' ,'value' => $this->error_value('name')),
array('name' => 'message' ,'value' => $this->error_value('message'))
)
);
$encoded = json_encode($json);
echo $encoded;
unset($encoded);
}
/* No errors, insert in db*/
else
{
if(($ret = $this->db->dbNewMessage($email,$name,$message,$url)) > 0){
$json = array('result' => 1);
}
else
$json = array('result' => -2); /* something went wrong in database insertion */
$encoded = json_encode($json);
echo $encoded;
unset($encoded);
}
}
public function setError($field, $errmsg){
$this->errors[$field] = $errmsg;
$this->num_errors = count($this->errors); //hitung error
}
public function error_value($field){
if(array_key_exists($field,$this->errors))
return $this->errors[$field];
else
return '';
}
public function countErrors(){
return $this->num_errors;
}
};
?>