-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
351 lines (287 loc) · 10.7 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
function tronque($chaine, $longueur = 120) {
if (empty ($chaine)) {
return "";
} elseif (strlen ($chaine) < $longueur) {
return $chaine;
} elseif (preg_match ("/(.{1,$longueur})\s./ms", $chaine, $match)) {
return $match [1] . "...";
} else {
return substr ($chaine, 0, $longueur) . "...";
}
}
function envoyer_email($destinataire, $objet, $message) {
$headers = 'From: "Chez le Pèr Pic"<[email protected]>'."\n";
$headers .= 'Reply-To: [email protected]'."\n";
$headers .= 'Content-Type: text/html; charset="iso-8859-1"'."\n";
$headers .= 'Content-Transfer-Encoding: 8bit';
if (mail($destinataire, $objet, $message, $headers)) {
return true;
} else {
return false;
}
}
function liste_clients($id = NULL) {
global $link, $prefix;
$liste = array();
if ($id != NULL) {
$req = 'SELECT * FROM '.$prefix.'client WHERE id = '.$id.' ORDER BY nom ASC, prenom ASC';
} else {
$req = 'SELECT * FROM '.$prefix.'client ORDER BY nom ASC, prenom ASC';
}
$res = mysqli_query($link,$req) OR die(mysqli_error($link));
$nb = mysqli_num_rows($res);
if ($nb == 0) { $liste['erreur'] = 'Aucun client.'; }
else {
while ($c = mysqli_fetch_array($res)) {
$liste[$c['id']]['id'] = $c['id'];
$liste[$c['id']]['nom'] = $c['nom'];
$liste[$c['id']]['prenom'] = $c['prenom'];
$liste[$c['id']]['email'] = $c['email'];
$liste[$c['id']]['tel'] = $c['tel'];
$liste[$c['id']]['tel2'] = $c['tel2'];
$liste[$c['id']]['adresse'] = $c['adresse'];
$liste[$c['id']]['code_postal'] = $c['code_postal'];
$liste[$c['id']]['ville'] = $c['ville'];
$liste[$c['id']]['pays'] = $c['pays'];
$liste[$c['id']]['password'] = $c['password'];
$liste[$c['id']]['pref_mail'] = $c['pref_mail'];
$liste[$c['id']]['pref_tel'] = $c['pref_tel'];
$liste[$c['id']]['newsletter'] = $c['newsletter'];
}
}
return $liste;
}
function envoi_mail ( $objet, $message, $destinataires = "[email protected], [email protected]") {
if (strtoupper(substr(PHP_OS,0,3)=='WIN')) { $eol="\r\n"; }
elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) { $eol="\r"; }
else { $eol="\n"; }
# To Email Address
$emailaddress = $destinataires;
# Message Subject
$emailsubject="[Chez le Per'Pic] ".$objet;
# Message Body
$body = $message;
# Common Headers
$headers = "";
$headers .= 'From: Chez le Per Pic <[email protected]>'.$eol;
$headers .= 'Reply-To: Chez le Per Pic <[email protected]>'.$eol;
$headers .= 'Return-Path: Chez le Per Pic <[email protected]>'.$eol; // these two to set reply address
$headers .= "Message-ID:<TheSystem@".$_SERVER['SERVER_NAME'].">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol; // These two to help avoid spam-filters
$msg = "";
# HTML Version
$headers .= "Content-Type: text/html; charset=utf-8".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= $body.$eol.$eol;
# SEND THE EMAIL
ini_set(sendmail_from,'[email protected]'); // the INI lines are to force the From Address to be used !
if ($_SERVER['SERVER_NAME'] != 'localhost') { mail($emailaddress, $emailsubject, $msg, $headers); }
ini_restore(sendmail_from);
}
function newChaine( $char = "") {
if( $char == "" ) $char = 8;
$list = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
mt_srand((double)microtime()*1000000);
$newstring="";
while( strlen( $newstring )< $char ) {
$newstring .= $list[mt_rand(0, strlen($list)-1)];
}
return $newstring;
}
function unzip_file($file, $destination_temp, $galerie) {
$erreur = "";
if ($file['size'] != 0) {
$extensions_valides = array('zip');
//print_r($extensions_valides);
// gestion des erreurs
if ($file['error'] > 0) $erreur .= "Erreur lors du transfert du fichier.<br />";
$extension_upload = strtolower( substr( strrchr($file['name'], '.') ,1) );
if ( !in_array($extension_upload,$extensions_valides) ) $erreur .= "Extension incorrecte : ".$extension_upload.".<br />";
} else $erreur .= "taille du fichier ?<br />";
$zip = new ZipArchive() ;
if ($zip->open($file['tmp_name']) !== true) {
$erreur .= "Impossible d'ouvrir l'archive";
}
$zip->extractTo($destination_temp);
$zip->close();
$dirname = 'photos/temp/';
$dir = opendir($dirname);
$succes = '<div class="info light good"><ul>';
$total = 0;
while($fichier = readdir($dir)) {
if($fichier != '.' && $fichier != '..' && !is_dir($dirname.$fichier) && $fichier != 'Thumbs.db')
{
// récupérer l'extension
$extension_photo = strtolower( substr( strrchr($fichier, '.') ,1) );
$ajout = ajouter_photo($fichier,$extension_photo);
if(is_array($ajout)) { $erreur .= $ajout['erreur']; }
else {
// on ajoute la photo en base
$nom = $ajout;
$req = 'INSERT INTO '.$prefix.'photos VALUES ("", "", "'.$nom.'", "'.$galerie.'", 0)';
if(mysqli_query($link,$req)) { $succes .= '<li>La photo '.$fichier.' a bien été ajoutée.</li>'; }
else $erreur .= mysqli_error($link);
}
$total++;
}
}
$succes .= '</ul><p>'.$total.' photos ajoutées.</p></div>';
closedir($dir);
if ($erreur != "") {
return $err = '<div class="info bad">'.$erreur.'</div>';
}
else return $succes;
}
function supprimer ($table, $id) {
global $prefix, $link;
$erreur = "";
$req = 'DELETE FROM '.$prefix.$table.' WHERE id = '.$id;
if(mysqli_query($link,$req)) {
$succes = '<div class="info good">La suppression a bien été effectuée.</div>';
} else $erreur .= mysqli_error($link);
if ($erreur != "") {
return $err = '<div class="info bad">'.$erreur.'<br />'.$req.'</div>';
}
else return $succes;
}
function suppr_doc($id,$nom) {
$req = 'DELETE FROM '.$prefix.'consultants_docs WHERE id = '.$id;
if(mysqli_query($link,$req)) {
unlink("admin/documents/".$nom);
$succes = '<div class="info good">Le document a bien été supprimé.</div>';
} else $erreur .= mysqli_error($link);
if ($erreur != "") {
return $err = '<div class="info bad">'.$erreur.'<br />'.$req.'</div>';
}
else return $succes;
}
function affich_photo($id) {
$req = 'SELECT id, titre, nom FROM '.$prefix.'photos WHERE id = '.$id.';';
$res = mysqli_query($link,$req) OR die(mysqli_error($link));
$p = mysqli_fetch_array($res);
$re = '<div class="info photo"><img src="photos/mini/'.$p['nom'].'" alt="'.$p['titre'].'" /><p>'.$p['titre'].'</p></div>';
return $re;
}
function suppr_photo($id,$nom) {
$req = 'DELETE FROM '.$prefix.'photos WHERE id = '.$id;
if(mysqli_query($link,$req)) {
unlink("photos/".$nom);
unlink("photos/mini/".$nom);
$succes = '<div class="info good">La photo a bien été supprimée.</div>';
}
if ($erreur != "") {
$err = array('erreur' => $erreur);
return $err;
}
else return $succes;
}
function ajouter_photo($photo, $ext) {
// traitement de l'image
$nom = "photo_".newChaine();
$nom_dossier = "photos/".$nom.".".$ext."";
$ancien_dossier = 'photos/temp/'.$photo;
$resultat = rename($ancien_dossier,$nom_dossier);
if ($resultat == false) { echo $ancien_dossier; }
// création de l'image miniature
// Définition de la largeur et de la hauteur maximale
$width = 800;
$height = 600;
$width2 = 90;
$height2 = 90;
// Content type
//header('Content-type: image/jpeg');
// Cacul des nouvelles dimensions
list($width_orig, $height_orig) = getimagesize($nom_dossier);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
if ($width2/$height2 > $ratio_orig) {
//$width2 = $height2*$ratio_orig;
$crop_v = $height2 - (($height2-$width2)/2);
$crop_h = 0;
} else {
//$height2 = $width2/$ratio_orig;
$crop_h = $width2 - (($width2-$height2)/2);
$crop_v = 0;
}
// Redimensionnement
$image_p = imagecreatetruecolor($width, $height);
$image_p2 = imagecreatetruecolor($width2, $height2);
$image = imagecreatefromjpeg($nom_dossier);
$image2 = imagecreatefromjpeg($nom_dossier);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagecopyresampled($image_p2, $image2, 0, 0, $crop_h, $crop_v, $width2, $height2, $width_orig-2*$crop_h, $height_orig-2*$crop_v);
// Affichage
imagejpeg($image_p, "photos/".$nom.".".$ext."");
imagejpeg($image_p2, "photos/mini/".$nom.".".$ext."");
if ($erreur != "") {
$err = array('erreur' => $erreur);
return $err;
}
else return $nom_image = $nom.".".$ext;
}
function ajout_photo($file) {
// traitement des erreurs de l'image
$erreur = "";
if ($file['size'] != 0) {
$maxsize = 1048576;
$extensions_valides = array('jpg','gif','png','jpeg','JPG');
//print_r($extensions_valides);
// gestion des erreurs
if ($file['error'] > 0) $erreur .= "Erreur lors du transfert du fichier.<br />";
if ($file['size'] > $maxsize) $erreur .= "Le fichier est trop gros.<br />";
$extension_upload = strtolower( substr( strrchr($file['name'], '.') ,1) );
if ( !in_array($extension_upload,$extensions_valides) ) $erreur .= "Extension incorrecte : ".$extension_upload.".<br />";
} else $erreur .= "taille du fichier ?";
if ($file['size'] != 0) {
// traitement de l'image
$nom = "photo_".newChaine();
$nom_dossier = "photos/".$nom.".".$extension_upload."";
$resultat = move_uploaded_file($file['tmp_name'],$nom_dossier);
// création de l'image miniature
// Définition de la largeur et de la hauteur maximale
$width = 800;
$height = 600;
$width2 = 90;
$height2 = 90;
// Content type
//header('Content-type: image/jpeg');
// Cacul des nouvelles dimensions
list($width_orig, $height_orig) = getimagesize($nom_dossier);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
if ($width2/$height2 > $ratio_orig) {
//$width2 = $height2*$ratio_orig;
$crop_v = $height2 - (($height2-$width2)/2);
$crop_h = 0;
} else {
//$height2 = $width2/$ratio_orig;
$crop_h = $width2 - (($width2-$height2)/2);
$crop_v = 0;
}
// Redimensionnement
$image_p = imagecreatetruecolor($width, $height);
$image_p2 = imagecreatetruecolor($width2, $height2);
$image = imagecreatefromjpeg($nom_dossier);
$image2 = imagecreatefromjpeg($nom_dossier);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagecopyresampled($image_p2, $image2, 0, 0, $crop_h, $crop_v, $width2, $height2, $width_orig-2*$crop_h, $height_orig-2*$crop_v);
// Affichage
imagejpeg($image_p, "photos/".$nom.".".$extension_upload."");
imagejpeg($image_p2, "photos/mini/".$nom.".".$extension_upload."");
}
if ($erreur != "") {
$err = array('erreur' => $erreur);
return $err;
}
else return $nom_image = $nom.".".$extension_upload;
}
?>