ep_function.php 12.2 KB
Newer Older
David's avatar
David committed
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
<?php

//////////////////
// Function 
//////////////////

// Verification des emails
function VerifMXemail($email) {
    if (CHECKMX) {
    	$domaine=explode('@', $email);
    	$r = new Net_DNS2_Resolver(array('nameservers' => array(NS1, NS2)));
    	try {
            $result = $r->query($domaine[1], 'MX');
    	} catch(Net_DNS2_Exception $e) {
    		return false;
    	}
    	if ($result->answer) {
    		return true;
    	} else {
    		return false;
    	}
    } else {
        return true;
    }
}

// postmap command
function UpdateVirtualDB() {
	global $dbco;
	try {
		$selectcmd = $dbco->prepare("SELECT status, alias, email
									FROM ".DBTABLEPREFIX."alias 
									WHERE status > 0
34
									ORDER BY alias ASC");
David's avatar
David committed
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
		$selectcmd->execute();
	} catch ( PDOException $e ) {
		echo "DB error :  ", $e->getMessage();
		die();
	}
	$file_content=null;
	while($alias_db = $selectcmd->fetch()) {
		if ($alias_db['status'] == 5) {
			$file_content .= $alias_db['alias'].' '.$alias_db['email']."\n";
		} else if ($alias_db['status'] == 3) {
			$file_content .= $alias_db['alias']." devnull\n";
		}
	}
	$alias_file=fopen(FICHIERALIAS,'w'); 
	fputs($alias_file, $file_content);
	fclose($alias_file);
	exec(BIN_POSTMAP.' '.FICHIERALIAS,$output,$return);
}

// add new alias
function AjouterAlias($status, $alias,$email, $life, $comment) {
	global $dbco;
	$dateCreat=date('Y-m-d H:i:s');
	if ($life == 0) {
		$dateExpir=NULL;
	} else {
		$dateExpir=date('Y-m-d H:i:s', time()+$life);
	}
	try {
		$insertcmd = $dbco->prepare("INSERT INTO ".DBTABLEPREFIX."alias (status, alias, email, dateCreat, dateExpir, comment) 
										VALUES (:status, :alias, :email, :dateCreat, :dateExpir, :comment)");
		$insertcmd->bindParam('status', $status, PDO::PARAM_INT);
		$insertcmd->bindParam('alias', $alias, PDO::PARAM_STR);
		$insertcmd->bindParam('email', $email, PDO::PARAM_STR);
		$insertcmd->bindParam('dateCreat', $dateCreat, PDO::PARAM_STR);
		$insertcmd->bindParam('dateExpir', $dateExpir, PDO::PARAM_STR);
		$insertcmd->bindParam('comment', $comment, PDO::PARAM_STR);
		$insertcmd->execute();
	} catch ( PDOException $e ) {
		echo "DB error :  ", $e->getMessage();
		die();
	}
	UpdateVirtualDB();
	return $dbco->lastInsertId();
}

// delete alias
function DeleteAlias($id, $alias_full) {
	global $dbco;
	if ($dbco->query("SELECT COUNT(*) FROM ".DBTABLEPREFIX."alias WHERE alias = '".$alias_full."' AND id = ".$id)->fetchColumn() != 0) {
		try {
			$deletecmd = $dbco->prepare("DELETE FROM ".DBTABLEPREFIX."alias WHERE id = :id AND alias =  :alias_full");
			$deletecmd->bindParam('id', $id, PDO::PARAM_INT);
			$deletecmd->bindParam('alias_full', $alias_full, PDO::PARAM_STR);
			$deletecmd->execute();
			echo '<div class="highlight-3">l\'email <b>'.$alias_full.'</b> a bien été supprimé</div>';
		} catch ( PDOException $e ) {
			echo "DB error :  ", $e->getMessage();
			die();
		}	
	} else {
		echo '<div class="highlight-1">Erreur : email poubelle inconnu</div>';
	}
	UpdateVirtualDB();
}

// enable alias
function EnableAlias($id, $alias_full, $email) {
	global $dbco;
	if ($id == null) {
		$selectcmd = $dbco->prepare("SELECT id,status FROM ".DBTABLEPREFIX."alias WHERE email = :email AND alias = :alias_full");
		$selectcmd->bindParam('email', $email, PDO::PARAM_STR);
	} else {
		$selectcmd = $dbco->prepare("SELECT id,status FROM ".DBTABLEPREFIX."alias WHERE id = :id AND alias = :alias_full");
		$selectcmd->bindParam('id', $id, PDO::PARAM_INT);
	}
	$selectcmd->bindParam('alias_full', $alias_full, PDO::PARAM_STR);
	$selectcmd->execute();
	$alias_fetch = $selectcmd->fetch();
	if (! $alias_fetch) {
		echo '<div class="highlight-1">Erreur : impossible de trouver cet email poubelle</div>';
	} else if ($alias_fetch['status'] == 3) {
		UpdateStatusAlias($alias_fetch['id'], $alias_full, 5);
		echo '<div class="highlight-3">La réception sur <b>'.$alias_full.'</b> est de nouveau active.</div>';
	} else if ($alias_fetch['status'] == 5) {
		echo '<div class="highlight-2">La réception sur <b>'.$alias_full.'</b> est déjà active.</div>';
	} else if ($alias_fetch['status'] == 0) {
		echo '<div class="highlight-1">La réception sur <b>'.$alias_full.'</b n\'à pas été confirmé par email.</div>';
	} else {
		echo '<div class="highlight-1">Erreur : status inconnu</div>';
	}
	UpdateVirtualDB();
}

// disable alias
function DisableAlias($id, $alias_full, $email) {
	global $dbco;
	if ($id == null) {
		$selectcmd = $dbco->prepare("SELECT id,status FROM ".DBTABLEPREFIX."alias WHERE email = :email AND alias = :alias_full");
		$selectcmd->bindParam('email', $email, PDO::PARAM_STR);
	} else {
		$selectcmd = $dbco->prepare("SELECT id,status FROM ".DBTABLEPREFIX."alias WHERE id = :id AND alias = :alias_full");
		$selectcmd->bindParam('id', $id, PDO::PARAM_INT);
	}
	$selectcmd->bindParam('alias_full', $alias_full, PDO::PARAM_STR);
	$selectcmd->execute();
	$alias_fetch = $selectcmd->fetch();
	if (! $alias_fetch) {
		echo '<div class="highlight-1">Erreur : impossible de trouver cet email poubelle</div>';
	} else if ($alias_fetch['status'] == 5) {
		UpdateStatusAlias($alias_fetch['id'], $alias_full, 3);
		echo '<div class="highlight-3">La réception sur <b>'.$alias_full.'</b> est maintenant suspendu.</div>';
	} else if ($alias_fetch['status'] == 3) {
		echo '<div class="highlight-2">La réception sur <b>'.$alias_full.'</b> est déjà suspendu.</div>';
	} else if ($alias_fetch['status'] == 0) {
		echo '<div class="highlight-1">La réception sur <b>'.$alias_full.'</b> ne peut être suspendu car elle n\'a pas encore été activé.</div>';
	} else {
		echo '<div class="highlight-1">Erreur : status inconnu</div>';
	}
	UpdateVirtualDB();
}

// update alias status
function UpdateStatusAlias($id, $alias_full, $status) {
	global $dbco;
	try {
		$updatecmd = $dbco->prepare("UPDATE ".DBTABLEPREFIX."alias SET status = $status WHERE id = :id AND alias = :alias_full");
		$updatecmd->bindParam('id', $id, PDO::PARAM_INT);
		$updatecmd->bindParam('alias_full', $alias_full, PDO::PARAM_STR);
		$updatecmd->execute();
	} catch ( PDOException $e ) {
		echo "DB error :  ", $e->getMessage();
		die();
	}
	UpdateVirtualDB();
}

// parse file for blacklist and aliasdeny
function parseFileRegex($file, $chaine) {
    $return=false;
    $handle = fopen($file, 'r');
    while (!feof($handle)) {
        $buffer = fgets($handle);
        $buffer = str_replace("\n", "", $buffer);
        if ($buffer) {
            if (!preg_match('/^(#|$|;)/', $buffer) && preg_match($buffer, $chaine)) {
                $return=true;
                break;
            }
        }
    }
    fclose($handle);
    return $return;
}

// check blacklistemail
function BlacklistEmail($email) {
    if (defined('BLACKLIST')) {
        return parseFileRegex(BLACKLIST, $email);
    } else {
        return false;
    }
}

// check aliasdeny
function AliasDeny($alias) {
    if (defined('ALIASDENY')) {
        return parseFileRegex(ALIASDENY, $alias);
    } else {
        return false;
    }
}

// list alias 
function ListeAlias($email) {
	global $dbco;
	try {
		$selectcmd = $dbco->prepare("SELECT id, status, alias, dateCreat, dateExpir, comment 
									FROM ".DBTABLEPREFIX."alias 
									WHERE email = :email AND status > 0
									ORDER BY status DESC");
		$selectcmd->bindParam('email', $email, PDO::PARAM_STR);
		$selectcmd->execute();
	} catch ( PDOException $e ) {
		echo "DB error :  ", $e->getMessage();
		die();
	}
	$nb_alias=0;
	$nb_alias_disable=0;
	$message= "## Liste de vos redirections poubelles active : \n\n";
	while($alias_db = $selectcmd->fetch()) {
226
		if ($alias_db['status'] == 3 && $nb_alias_disable == 0) {
David's avatar
David committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
			$message.= "## Liste de vos redirections poubelles désactivé : \n\n";
		} 
		$message.=" * ".$alias_db['alias']." Créé le ".$alias_db['dateCreat'];
		if ($alias_db['dateExpir']) {
			$message.=" et expire le ".$alias_db['dateExpir'];
		}
		$message.="\n";
		if ($alias_db['comment']) {
			$message.="\tCommentaire : ".$alias_db['comment']."\n";
		}
		if ($alias_db['status'] == 5) {
			$message.="\tDésactiver : ".urlGen('disable',$alias_db['id'],$alias_db['alias'])."\n";
			$nb_alias++;
		} else {
			$message.="\tActiver : ".urlGen('enable',$alias_db['id'],$alias_db['alias'])."\n";
242
			$nb_alias_disable++;
David's avatar
David committed
243
244
245
246
247
248
249
		}
		$message.="\tSupprimer : ".urlGen('delete',$alias_db['id'],$alias_db['alias'])."\n\n";
	}
	$nb_alias_total = $nb_alias + $nb_alias_disable;
	if ($nb_alias_total == 0) {
		return false;
	} else {
250
		SendEmail($email,'Liste de vos '.$nb_alias_total.' redirection(s) poubelle(s)',$message);
David's avatar
David committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
		return true;
	}
}

function SendEmail($recipient, $sujet, $message) {
	$header = "From: ".EMAILFROM."\n";
	$header.= "MIME-Version: 1.0\n";
	if (preg_match('#^[a-z0-9._-]+@(hotmail|live|msn).[a-z]{2,4}$#', $recipient)) {
		$header = str_replace("\n", "\r\n", $header);
		$message = str_replace("\n", "\r\n", $header);
	}
	$message="Bonjour,\n\n".$message."\n\n".
	mail($recipient,EMAILTAGSUJET.' '.$sujet,$message,$header);
}

function urlGen($act,$id,$alias_full) {
	$idUrl=base64_encode($id.';'.$alias_full);
David's avatar
David committed
268
269
	if (URLREWRITE_START && URLREWRITE_MIDDLE && URLREWRITE_END) {
		return URLREWRITE_START.$act.URLREWRITE_MIDDLE.$idUrl.URLREWRITE_END;
David's avatar
David committed
270
271
272
273
274
275
276
277
278
279
280
	} else {
		return URLPAGE."?act=".$act."&value=".$idUrl;
	}
}
function urlUnGen($get_value) {
	$explode_get_value = explode(';', base64_decode($get_value));
	$return['id']=$explode_get_value[0];
	$return['alias_full']=$explode_get_value[1];
	return $return;
}

David's avatar
David committed
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Source http://css-tricks.com/serious-form-security/
function StripCleanToHtml($s){
		// Restores the added slashes (ie.: " I\'m John " for security in output, and escapes them in htmlentities(ie.:  &quot; etc.)
		// Also strips any <html> tags it may encouter
		// Use: Anything that shouldn't contain html (pretty much everything that is not a textarea)
		return htmlentities(trim(strip_tags(stripslashes($s))), ENT_NOQUOTES, "UTF-8");
}
function CleanToHtml($s){
		// Restores the added slashes (ie.: " I\'m John " for security in output, and escapes them in htmlentities(ie.:  &quot; etc.)
		// It preserves any <html> tags in that they are encoded aswell (like &lt;html&gt;)
		// As an extra security, if people would try to inject tags that would become tags after stripping away bad characters,
		// we do still strip tags but only after htmlentities, so any genuine code examples will stay
		// Use: For input fields that may contain html, like a textarea
		return strip_tags(htmlentities(trim(stripslashes($s))), ENT_NOQUOTES, "UTF-8");
}

David's avatar
David committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//////////////////
// Admin function
//////////////////

function CheckUpdate() {
	if (CHECKUPDATE) {
		if (! is_file(DATA.'/checkupdate') || filemtime(DATA.'/checkupdate') + CHECKUPDATE < time()) {
			$ep_get_version = @file_get_contents('http://poubelle.zici.fr/ep_checkupdate');
			$ep_version_file=fopen(DATA.'/checkupdate','w'); 
			fputs($ep_version_file, $ep_get_version);
			fclose($ep_version_file);
			if (DEBUG) { echo 'ep_checkupdate_downloaded : '.file_get_contents(DATA.'/checkupdate').'\n'; }
		} 
		$file_current_version = trim(file_get_contents(DATA.'/checkupdate'));
		if ($file_current_version != '' && $file_current_version != VERSION) {
David's avatar
David committed
312
			return '<p>Upgrade note : Votre version est en '.VERSION.' alors que la version en cours est en '.$file_current_version.'</p>';
David's avatar
David committed
313
314
315
316
317
		} else {
			return false;
		}
	}
}
David's avatar
David committed
318
319
320
321
322
323
324
325
326

function LifeExpire() {
	global $dbco;
	try {
		$deletecmd = $dbco->prepare("DELETE FROM ".DBTABLEPREFIX."alias WHERE dateExpir IS NOT NULL AND dateExpir < '".date('Y-m-d H:i:s')."'");
		$deletecmd->execute();
	} catch ( PDOException $e ) {
		echo "DB error :  ", $e->getMessage();
		die();
327
	}
David's avatar
David committed
328
}
David's avatar
David committed
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363

// Vérifie que le domaine de l'alias est bien dans la configuration
function domainePresent($postDom) {
	$domains = explode(';', DOMAIN);
	$return=true;
	if (count($domains) == 1) {
		if (!preg_match('#'.$postDom.'#',DOMAIN)) {
			$return=false;
		}
	} else {
		foreach ($domains as $one_domain)  {
			if (!preg_match('#'.$postDom.'#',$one_domain)) {
				$return=false;
			}
		}
	}
	return $return;
}
// Vérifie que l'email n'est pas un alias avec un domain "poubelle" (éviter boucle forward)
function emailIsAlias($postemail) {
	$domains = explode(';', DOMAIN);
	$return=false;
	if (count($domains) == 1) {
		if (preg_match('#'.DOMAIN.'$#',$postemail)) {
			$return=true;
		}
	} else {
		foreach ($domains as $one_domain)  {
			if (preg_match('#'.$one_domain.'$#',$postemail)) {
				$return=true;
			}
		}
	}
	return $return;
}
David's avatar
David committed
364
?>