-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
191 lines (184 loc) · 7.55 KB
/
functions.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
_strip_slashes(); # used if server has get_magic_quotes_gpc() enabled ( autodetected )
if(isset($_POST['getCaptcha'])){ # generate captcha
die(newCaptcha(1));
}
function _inputHTML($in,$id){
global $config;
$placeholder = (!isset($in['placeholder'])?$config['defaults'][$in['type']]['placeholder']:$in['placeholder']);
$style = (!isset($in['style'])?$config['defaults'][$in['type']]['style']:$in['style']);
$icon = (($config['icons'])?'<span class="'.(isset($in['icon'])?$in['icon']:$config['defaults'][$in['type']]['icon']).' cnts-before"></span>':'');
$required = @( (isset($in['required'])?$in['required']:$config['defaults'][$in['type']]['required']) ? ' required-data="'.$in['type'].'"' : '' );
switch ($in['type']) {
case 'name':
echo '<div class="form-group">'
.$icon.'<input type="text"'.$required.' class="form-control input-sm cnts-input" id="cnts-name-'.$id.'" placeholder="'.$placeholder.'" style="'.$style.'">'
.'<span class="glyphicon form-control-feedback"> </span>'
.'</div>';
break;
case 'custom':
echo '<div class="form-group">'
.$icon.'<input type="text"'.$required.' class="form-control input-sm cnts-input" id="cnts-custom-'.$id.'" placeholder="'.$placeholder.'" style="'.$style.'">'
.'<span class="glyphicon form-control-feedback"> </span>'
.'</div>';
break;
case 'mail':
echo '<div class="form-group">'
.$icon.'<input type="text"'.$required.' class="form-control input-sm cnts-input" id="cnts-mail-'.$id.'" placeholder="'.$placeholder.'" style="'.$style.'">'
.'<span class="glyphicon form-control-feedback"> </span>'
.'</div>';
break;
case 'phone':
echo '<div class="form-group">'
.$icon.'<input type="text"'.$required.' class="form-control input-sm cnts-input" id="cnts-phone-'.$id.'" placeholder="'.$placeholder.'" style="'.$style.'">'
.'<span class="glyphicon form-control-feedback"> </span>'
.'</div>';
break;
case 'textarea':
echo '<div class="form-group">'
.$icon.'<textarea class="form-control cnts-input"'.$required.' id="cnts-textarea-'.$id.'" placeholder="'.$placeholder.'" style="'.$style.'"></textarea>'
.'</div>';
break;
case 'checkbox':
echo '<div class="form-group">'
.$icon.'<span class="cnts-input" type="checkbox" id="cnts-checkbox-'.$id.'" style="'.$style.'">'
.$placeholder.'<span><input type="checkbox"> '.implode('</span> <span><input type="checkbox"> ', $in['checkboxes']).'</span>'
.'</span>'
.'</div>';
break;
case 'number':
echo '<div class="form-group">'
.$icon.'<input type="text"'.$required.' class="form-control input-sm cnts-input" id="cnts-number-'.$id.'" placeholder="'.$placeholder.'" style="'.$style.'">'
.'<span class="glyphicon form-control-feedback"> </span>'
.'</div>';
break;
}
}
function _init_notifications(){
if(!isset($_POST['cnts'])){return false;} # initialize only on post contact data
header('Content-Type: text/plain'); # set output as text only
_check_captcha(); #check captcha error
global $config;
if(strlen(json_encode($_POST['cnts']))>intval($config['max_post_data'])){die('max size extended');}
$cnts = json_decode($_POST['cnts'],true);if($cnts===null||empty($cnts)){die('error json');}
$inputs = _init_validations($cnts);
foreach ($config['notifications'] as $c) {
switch ($c['type']) {
case 'email': # $c['config'] = array(email,subject,from,reply)
$from = $c['config']['from'] ;
$message = '
<table border="0" align="left" cellspacing="0" cellpadding="5" style="font-size:11pt;">
<thead>
<tr>
<th style="width:100px"></th>
<th></th>
</tr>
</thead>
<tbody>
';
foreach ($inputs as $v) {
if(preg_match('/(mail)/i', $v[0])){ $from = $v[1]; }
$message .= '<tr><td style="border-top:1px solid black;"><b>'.$v[0].'</b></td><td style="border-top:1px solid black;">'.$v[1].'</td></tr>';
}
$message .= '</tbody></table>';
$headers = "From: ".$from."\r\n".
"Reply-To: ".$from."\r\n".
(($c['config']['bcc'])?"Bcc: ".$c['config']['bcc']."\r\n":'').
(($c['config']['cc'])?"Cc: ".$c['config']['cc']."\r\n":'').
"MIME-Version: 1.0\r\n".
"Content-Type: text/html; charset=ISO-8859-1\r\n";
mail( $c['config']['email'], $c['config']['subject'], $message, $headers );
break;
case 'database': # $c['config'] = array( connection=>array(connInfo,user,pass), show_errors, table, column )
try{
$conn = new PDO($c['config']['connection'][0],$c['config']['connection'][1],$c['config']['connection'][2]);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$st=$conn->prepare('INSERT INTO `'.$c['config']['table'].'` (`'.$c['config']['column'].'`) VALUES (?)');
$conn->beginTransaction();
$st->execute(array(json_encode($inputs)));
$conn->commit();
}catch(PDOException $e){
// $conn->rollBack();
if($c['config']['show_errors']){print_r($e->getMessage());}
die('db error');
}
break;
case 'file':
file_put_contents( $c['config']['file'] , json_encode($inputs)."\n" , FILE_APPEND );
break;
}
}
die('ok');
}
function _init_validations($cnts){
global $config;
foreach ($cnts as $key => $value) {
$reg = '#^(cnts-('.implode('|', array_keys($config['defaults']) ).')-([0-9]*))$#i';
preg_match($reg, $key, $t);
if(empty($t)){die('error on keys');}
list($id, $type) = array(intval($t[3]),$t[2]);
if(!isset($config['inputs'][ $id ])){print_r($id);print_r($type);die('error on id');}
$required = (isset($config['inputs'][ $id ]['required'])) ? $config['inputs'][ $id ]['required'] : ((isset($config['defaults'][ $type ]['required']))?$config['defaults'][ $type ]['required']:false);
$regex = (isset($config['inputs'][ $id ]['regex'])) ? $config['inputs'][ $id ]['regex'] : @$config['defaults'][ $type ]['regex'];
if( $required && !preg_match($regex,$value) ){
header("Content-Type: text/plain");die('validation error');
}else{
$inKey = (isset($config['inputs'][ $id ]['name'])) ? $config['inputs'][ $id ]['name'] : $type.'-'.$id;
$r[] = array( $inKey, htmlspecialchars($value) );
}
}
if(!isset($r)){die('err, no inputs!');}
return $r;
}
function _check_captcha(){
global $config;
if( $config['captcha']
&& isset($_POST['captcha'])
&& ( strtolower(trim($_POST['captcha'])) !== strtolower($_SESSION['captcha']['code']) )
){
die('captcha error');
}else{
newCaptcha(false);
}
}
function newCaptcha($return=true){
include("inc/captcha/captcha.php");
$_SESSION['captcha'] = simple_php_captcha( array(
'min_length' => 3,
'max_length' => 3,
'fonts' => array('fonts/1.TTF','fonts/2.TTF','fonts/3.TTF','fonts/4.TTF'),
'characters' => 'ABCDEFGHJKLMNPRSTUVWXYZabcdefghjkmnprstuvwxyz23456789',
'min_font_size' => 30,
'max_font_size' => 30,
'color' => '#104D82',
'angle_min' => 0,
'angle_max' => 40,
'shadow' => true,
'shadow_color' => '#FFF',
'shadow_offset_x' => -1,
'shadow_offset_y' => 1
) );
return ($return)?$_SESSION['captcha']['image_src']:false;
}
function _strip_slashes(){
if (get_magic_quotes_gpc()) {
function stripslashes_array(&$arr) {
foreach ($arr as $k => &$v) {
$nk = stripslashes($k);
if ($nk != $k) {
$arr[$nk] = &$v;
unset($arr[$k]);
}
if (is_array($v)) {
stripslashes_array($v);
} else {
$arr[$nk] = stripslashes($v);
}
}
}
stripslashes_array($_POST);
stripslashes_array($_GET);
stripslashes_array($_REQUEST);
stripslashes_array($_COOKIE);
}
}