source: extensions/CryptograPHP/cryptographp/cryptographp.inc.php @ 10837

Last change on this file since 10837 was 10837, checked in by mistic100, 13 years ago

first version of CryptograPHP

File size: 9.7 KB
Line 
1<?php
2
3// -----------------------------------------------
4// Cryptographp v1.4
5// (c) 2006-2007 Sylvain BRISON
6//
7// www.cryptographp.com
8// cryptographp@alphpa.com
9//
10// Licence CeCILL modifiée
11// => Voir fichier Licence_CeCILL_V2-fr.txt)
12// -----------------------------------------------
13
14define('PHPWG_ROOT_PATH','../../../');
15include_once( PHPWG_ROOT_PATH.'include/common.inc.php' );
16
17
18error_reporting(E_ALL ^ E_NOTICE);
19srand((double)microtime()*1000000); 
20
21if  ((pwg_get_cookie_var('cryptcookietest') == null) and ($_GET[$_GET['sn']]==""))
22    {
23    header("Content-type: image/png");
24    readfile('images/erreur3.png'); 
25    exit;
26    }
27
28if ($_GET[$_GET['sn']]=="") unset ($_GET['sn']); 
29// session_start();
30
31
32// N'accepte que les fichiers de config du meme répertoire
33if (is_file($_GET['cfg']) and dirname($_GET['cfg'])=='.' ) pwg_set_session_var('configfile', $_GET['cfg']); 
34   else  pwg_set_session_var('configfile', "cryptographp.cfg.php");
35
36include(pwg_get_session_var('configfile')); 
37
38
39// Vérifie si l'utilisateur a le droit de (re)générer un cryptogramme
40if (pwg_get_session_var('cryptcptuse')>=$cryptusemax) {
41   header("Content-type: image/png");
42   readfile('images/erreur1.png'); 
43   exit;
44   }
45
46$delai = time()-pwg_get_session_var('crypttime');
47if ($delai < $cryptusetimer) { 
48   switch ($cryptusertimererror) {
49          case 2  : header("Content-type: image/png");
50                    readfile('images/erreur2.png'); 
51                    exit;
52          case 3  : sleep ($cryptusetimer-$delai);
53                    break; // Fait une pause
54          case 1  :         
55          default : exit;  // Quitte le script sans rien faire
56          }
57   }
58
59// Création du cryptogramme temporaire
60$imgtmp = imagecreatetruecolor($cryptwidth,$cryptheight);
61$blank  = imagecolorallocate($imgtmp,255,255,255);
62$black   = imagecolorallocate($imgtmp,0,0,0);
63imagefill($imgtmp,0,0,$blank);
64
65
66$word ='';
67$x = 10; 
68$pair = rand(0,1);
69$charnb = rand($charnbmin,$charnbmax);
70for ($i=1;$i<= $charnb;$i++) {             
71     $tword[$i]['font'] =  $tfont[array_rand($tfont,1)];
72     $tword[$i]['angle'] = (rand(1,2)==1)?rand(0,$charanglemax):rand(360-$charanglemax,360);
73     
74     if ($crypteasy) $tword[$i]['element'] =(!$pair)?$charelc{rand(0,strlen($charelc)-1)}:$charelv{rand(0,strlen($charelv)-1)};
75        else $tword[$i]['element'] = $charel{rand(0,strlen($charel)-1)};
76
77     $pair=!$pair;
78     $tword[$i]['size'] = rand($charsizemin,$charsizemax);
79     $tword[$i]['y'] = ($charup?($cryptheight/2)+rand(0,($cryptheight/5)):($cryptheight/1.5));
80     $word .=$tword[$i]['element'];
81     
82     $lafont="fonts/".$tword[$i]['font'];
83     imagettftext($imgtmp,$tword[$i]['size'],$tword[$i]['angle'],$x,$tword[$i]['y'],$black,$lafont,$tword[$i]['element']);
84
85     $x +=$charspace;
86     } 
87
88// Calcul du racadrage horizontal du cryptogramme temporaire
89$xbegin=0;
90$x=0;
91while (($x<$cryptwidth)and(!$xbegin)) {
92     $y=0;
93     while (($y<$cryptheight)and(!$xbegin)) {
94           if (imagecolorat($imgtmp,$x,$y) != $blank) $xbegin = $x;
95           $y++;
96           }
97     $x++;
98     } 
99   
100$xend=0;
101$x=$cryptwidth-1;
102while (($x>0)and(!$xend)) {
103     $y=0;
104     while (($y<$cryptheight)and(!$xend)) {
105           if (imagecolorat($imgtmp,$x,$y) != $blank) $xend = $x;
106           $y++;
107           }
108     $x--;
109     } 
110     
111$xvariation = round(($cryptwidth/2)-(($xend-$xbegin)/2));
112imagedestroy ($imgtmp);
113
114
115// Création du cryptogramme définitif
116// Création du fond
117$img = imagecreatetruecolor($cryptwidth,$cryptheight); 
118
119if ($bgimg and is_dir($bgimg)) {
120                    $dh  = opendir($bgimg);
121                    while (false !== ($filename = readdir($dh))) 
122                          if(eregi(".[gif|jpg|png]$", $filename))  $files[] = $filename;
123                    closedir($dh);
124                    $bgimg = $bgimg.'/'.$files[array_rand($files,1)];
125                    }
126if ($bgimg) {
127            list($getwidth, $getheight, $gettype, $getattr) = getimagesize($bgimg);
128            switch ($gettype) {
129                   case "1": $imgread = imagecreatefromgif($bgimg); break;
130                                     case "2": $imgread = imagecreatefromjpeg($bgimg); break;
131                                     case "3": $imgread = imagecreatefrompng($bgimg); break;
132                   }
133                  imagecopyresized ($img, $imgread, 0,0,0,0,$cryptwidth,$cryptheight,$getwidth,$getheight);
134                        imagedestroy ($imgread);
135            }
136            else {
137                 $bg = imagecolorallocate($img,$bgR,$bgG,$bgB);
138                 imagefill($img,0,0,$bg);
139                 if ($bgclear) imagecolortransparent($img,$bg);
140                 }
141
142
143function ecriture()
144{
145// Création de l'écriture
146global  $img, $ink, $charR, $charG, $charB, $charclear, $xvariation, $charnb, $charcolorrnd, $charcolorrndlevel, $tword, $charspace;
147if (function_exists ('imagecolorallocatealpha')) $ink = imagecolorallocatealpha($img,$charR,$charG,$charB,$charclear);
148   else $ink = imagecolorallocate ($img,$charR,$charG,$charB);
149
150$x = $xvariation;
151for ($i=1;$i<=$charnb;$i++) {       
152       
153    if ($charcolorrnd){   // Choisit des couleurs au hasard
154       $ok = false;
155       do {
156          $rndR = rand(0,255); $rndG = rand(0,255); $rndB = rand(0,255);
157          $rndcolor = $rndR+$rndG+$rndB;
158          switch ($charcolorrndlevel) {
159                 case 1  : if ($rndcolor<200) $ok=true; break; // tres sombre
160                 case 2  : if ($rndcolor<400) $ok=true; break; // sombre
161                 case 3  : if ($rndcolor>500) $ok=true; break; // claires
162                 case 4  : if ($rndcolor>650) $ok=true; break; // très claires
163                 default : $ok=true;               
164                 }
165          } while (!$ok);
166         
167      if (function_exists ('imagecolorallocatealpha')) $rndink = imagecolorallocatealpha($img,$rndR,$rndG,$rndB,$charclear);
168          else $rndink = imagecolorallocate ($img,$rndR,$rndG,$rndB);         
169         } 
170         
171    $lafont="fonts/".$tword[$i]['font'];
172    imagettftext($img,$tword[$i]['size'],$tword[$i]['angle'],$x,$tword[$i]['y'],$charcolorrnd?$rndink:$ink,$lafont,$tword[$i]['element']);
173
174    $x +=$charspace;
175    } 
176}
177
178
179function noisecolor()
180// Fonction permettant de déterminer la couleur du bruit et la forme du pinceau
181 {
182 global $img, $noisecolorchar, $ink, $bg, $brushsize;
183 switch ($noisecolorchar) {
184         case 1  : $noisecol=$ink; break;
185         case 2  : $noisecol=$bg; break;
186         case 3  : 
187         default : $noisecol=imagecolorallocate ($img,rand(0,255),rand(0,255),rand(0,255)); break;               
188         }
189 if ($brushsize and $brushsize>1 and function_exists('imagesetbrush')) {
190    $brush = imagecreatetruecolor($brushsize,$brushsize);
191    imagefill($brush,0,0,$noisecol);
192    imagesetbrush($img,$brush);
193    $noisecol=IMG_COLOR_BRUSHED;
194    }
195 return $noisecol;   
196}
197
198
199function bruit()
200// Ajout de bruits: point, lignes et cercles aléatoires
201{
202global $noisepxmin, $noisepxmax, $noiselinemin, $noiselinemax, $nbcirclemin, $nbcirclemax,$img, $cryptwidth, $cryptheight;
203$nbpx = rand($noisepxmin,$noisepxmax);
204$nbline = rand($noiselinemin,$noiselinemax);
205$nbcircle = rand($nbcirclemin,$nbcirclemax);
206for ($i=1;$i<$nbpx;$i++) imagesetpixel ($img,rand(0,$cryptwidth-1),rand(0,$cryptheight-1),noisecolor());
207for ($i=1;$i<=$nbline;$i++) imageline($img,rand(0,$cryptwidth-1),rand(0,$cryptheight-1),rand(0,$cryptwidth-1),rand(0,$cryptheight-1),noisecolor());
208for ($i=1;$i<=$nbcircle;$i++) imagearc($img,rand(0,$cryptwidth-1),rand(0,$cryptheight-1),$rayon=rand(5,$cryptwidth/3),$rayon,0,360,noisecolor());
209} 
210
211
212if ($noiseup) {
213   ecriture();
214   bruit();
215   } else {
216          bruit();
217          ecriture();
218          }
219
220
221// Création du cadre
222if ($bgframe) {
223   $framecol = imagecolorallocate($img,($bgR*3+$charR)/4,($bgG*3+$charG)/4,($bgB*3+$charB)/4);
224   imagerectangle($img,0,0,$cryptwidth-1,$cryptheight-1,$framecol);
225   }
226 
227           
228// Transformations supplémentaires: Grayscale et Brouillage
229// Vérifie si la fonction existe dans la version PHP installée
230if (function_exists('imagefilter')) {
231   if ($cryptgrayscal) imagefilter ( $img,IMG_FILTER_GRAYSCALE);
232   if ($cryptgaussianblur) imagefilter ( $img,IMG_FILTER_GAUSSIAN_BLUR);
233   }
234
235
236// Conversion du cryptogramme en Majuscule si insensibilité à la casse
237$word = ($difuplow?$word:strtoupper($word));
238
239
240// Retourne 2 informations dans la session:
241// - Le code du cryptogramme (crypté ou pas)
242// - La Date/Heure de la création du cryptogramme au format integer "TimeStamp"
243switch (strtoupper($cryptsecure)) {   
244       case "MD5"  : pwg_set_session_var('cryptcode', md5($word)); break;
245       case "SHA1" : pwg_set_session_var('cryptcode', sha1($word)); break;
246       default     : pwg_set_session_var('cryptcode', $word); break;
247       }
248pwg_set_session_var('crypttime', time());
249pwg_set_session_var('cryptcptuse', pwg_get_session_var('cryptcptuse') +1);       
250 
251
252// Envoi de l'image finale au navigateur
253switch (strtoupper($cryptformat)) { 
254       case "JPG"  :
255             case "JPEG" : if (imagetypes() & IMG_JPG)  {
256                        header("Content-type: image/jpeg");
257                        imagejpeg($img, "", 80);
258                        }
259                     break;
260             case "GIF"  : if (imagetypes() & IMG_GIF)  {
261                        header("Content-type: image/gif");
262                        imagegif($img);
263                        }
264                     break;
265             case "PNG"  : 
266             default     : if (imagetypes() & IMG_PNG)  {
267                        header("Content-type: image/png");
268                        imagepng($img);
269                        }
270       }
271
272imagedestroy ($img);
273unset ($word,$tword);
274pwg_unset_session_var('cryptreload'); 
275?>
Note: See TracBrowser for help on using the repository browser.