PageRenderTime 29ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 0ms

/class/class_digicode.php

http://malleo-cms.googlecode.com/
PHP | 261 lines | 187 code | 18 blank | 56 comment | 28 complexity | f8caf835d73e205e4afa854042779f06 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. |------------------------------------------------------------------------------------------------------------
  4. | Software: Malleo ( CMS )
  5. | Contact: SP - http://www.malleo-cms.com
  6. | Support: http://www.malleo-cms.com?module=forum
  7. | Documentation : Support: http://www.malleo-cms.com?module=wiki
  8. |------------------------------------------------------------------------------------------------------------
  9. | Author: Stephane RAJALU
  10. | Copyright (c) 2008-2009, Stephane RAJALU All Rights Reserved
  11. |------------------------------------------------------------------------------------------------------------
  12. | License: Distributed under the CECILL V2 License
  13. | This program is distributed in the hope that it will be useful - WITHOUT
  14. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. | FITNESS FOR A PARTICULAR PURPOSE.
  16. |
  17. | Please read Licence_CeCILL_V2-en.txt
  18. | SVP lisez Licence_CeCILL_V2-fr.txt
  19. |------------------------------------------------------------------------------------------------------------
  20. */
  21. if ( !defined('PROTECT_ADMIN') )
  22. {
  23. die("Tentative de Hacking");
  24. }
  25. require_once($root.'class/class_image.php');
  26. class digicode extends image
  27. {
  28. // Combien de cases pour former le digicode
  29. var $nbre_cases = 25;
  30. // Combien de colonnes dans le damier
  31. var $nbre_colonnes = 5;
  32. // Caracteres possibles
  33. var $caracteres_autorises = array(0,1,2,3,4,5,6,7,8,9);
  34. // Longueur clef par caractere
  35. var $longueur_clef = 5;
  36. // Liste des clefs pour cette tentative de connexion
  37. var $clefs = array();
  38. // Repertoire de cache
  39. var $url_cache='cache/digicode/';
  40. var $format = 'png';
  41. var $taille_texte = 14;
  42. // Largeur des cases
  43. var $largeur_cases = 30;
  44. var $police = array();
  45. var $couleur_texte = '#000000';
  46. var $couleur_fond = '#FFFFFF';
  47. //
  48. // Init
  49. function digicode(){
  50. global $cf,$root;
  51. // Polices dispos
  52. $this->police[] = $root.'data/fonts/verdana.ttf';
  53. $this->police[] = $root.'data/fonts/Alanden_.ttf';
  54. $this->code_acces_za = preg_replace('/[^0-9]/','',$cf->config['digicode_acces_zone_admin']);
  55. if ($this->code_acces_za == '')$this->code_acces_za='0000';
  56. }
  57. //
  58. // Cree une image avec l'adresse en texte.
  59. function generer_image_case_digicode($texte,$url){
  60. $this->image = imagecreatetruecolor($this->largeur_cases,$this->largeur_cases);
  61. $rgb = $this->html2rgb($this->couleur_fond);
  62. $background = ImageColorAllocate ($this->image, $rgb[0],$rgb[1],$rgb[2]);
  63. imagefill($this->image,0,0,$background);
  64. // Parasites
  65. $this->genere_parasites($texte);
  66. // Texte
  67. $this->position_x=4;
  68. $this->position_y=20;
  69. shuffle($this->police);
  70. $this->inserer_texte($texte,$this->taille_texte,$this->couleur_texte,$this->police[0]);
  71. // On enregistre
  72. $this->extension = $this->format;
  73. $this->save_image($url);
  74. }
  75. //
  76. // Cree des symboles parasites pour que la taille des images ne soit pas constante
  77. function genere_parasites($texte){
  78. // Rectangle
  79. $rgb = $this->html2rgb('#d2ee95');
  80. $couleur_motifs = ImageColorAllocate($this->image, $rgb[0],$rgb[1],$rgb[2]);
  81. $position = mt_rand(0,$this->largeur_cases);
  82. imagefilledrectangle($this->image, $position, $position, ($position+20), ($position+10), $couleur_motifs);
  83. // elypse
  84. $rgb = $this->html2rgb('#cfe2f0');
  85. $couleur_motifs = ImageColorAllocate($this->image, $rgb[0],$rgb[1],$rgb[2]);
  86. $position = mt_rand(0,$this->largeur_cases);
  87. imagefilledellipse($this->image, $position, $position, $position, $position, $couleur_motifs);
  88. // Si il n'y a pas de texte on rajoute un peu de couleur noire
  89. $texte=trim($texte);
  90. if (empty($texte)){
  91. $rgb = $this->html2rgb($this->couleur_texte);
  92. $couleur_motifs = ImageColorAllocate($this->image, $rgb[0],$rgb[1],$rgb[2]);
  93. imageline($this->image, mt_rand(0,$this->largeur_cases), mt_rand(0,$this->largeur_cases), mt_rand(0,$this->largeur_cases), mt_rand(0,$this->largeur_cases), $couleur_motifs);
  94. $couleur_motifs = ImageColorAllocate($this->image, $rgb[0],$rgb[1],$rgb[2]);
  95. imageline($this->image, mt_rand(0,$this->largeur_cases), mt_rand(0,$this->largeur_cases), mt_rand(0,$this->largeur_cases), mt_rand(0,$this->largeur_cases), $couleur_motifs);
  96. $couleur_motifs = ImageColorAllocate($this->image, $rgb[0],$rgb[1],$rgb[2]);
  97. imageline($this->image, mt_rand(0,$this->largeur_cases), mt_rand(0,$this->largeur_cases), mt_rand(0,$this->largeur_cases), mt_rand(0,$this->largeur_cases), $couleur_motifs);
  98. }
  99. }
  100. //
  101. // Genere une image pour la case du digicode
  102. function appel_image_case_digicode($numero,$clef){
  103. if (empty($numero) || empty($clef)) return false;
  104. global $root;
  105. if (!is_dir($root.$this->url_cache)) $this->creer_dossier_image($root.$this->url_cache);
  106. $url = $root.$this->url_cache.md5($clef).'.'.$this->format;
  107. if (!file_exists($url)){
  108. $this->generer_image_case_digicode($numero,$url);
  109. }
  110. return $url;
  111. }
  112. //
  113. // Affiche le digicode
  114. function afficher_digicode(){
  115. global $tpl,$root,$lang;
  116. $this->purger_cache_digicode();
  117. $tpl->set_filenames(array(
  118. 'body_admin' => $root.'html/admin_digicode.html'
  119. ));
  120. $liste_cases = array_pad($this->caracteres_autorises,$this->nbre_cases,' ');
  121. shuffle($liste_cases);
  122. include_once($root.'fonctions/fct_maths.php');
  123. $i = 0;
  124. foreach($liste_cases AS $numero){
  125. $clef = generate_key($this->longueur_clef);
  126. $this->clefs[$numero] = $clef;
  127. if ($i%$this->nbre_colonnes ==0) $tpl->assign_block_vars('tr', array());
  128. $tpl->assign_block_vars('tr.case', array(
  129. 'IMAGE' => $this->appel_image_case_digicode(' '.$numero,$clef),
  130. 'CLEF' => $clef,
  131. ));
  132. $i++;
  133. }
  134. unset($liste_cases);
  135. $this->encoder_code_access();
  136. $this->declarer_clefs_langue();
  137. $nbre_tentatives = 0;
  138. if (isset($_SESSION['digicode_nbre_tentatives']) && $_SESSION['digicode_nbre_tentatives']>0){
  139. $tpl->assign_block_vars('alerte_tentatives', array());
  140. $nbre_tentatives = $_SESSION['digicode_nbre_tentatives'];
  141. }
  142. $tpl->assign_vars(array(
  143. 'LARGEUR_DIGICODE' => ($this->largeur_cases*$this->nbre_colonnes+35),
  144. 'NBRE_COLONNES' => $this->nbre_colonnes,
  145. 'L_ALERTE_TENTATIVES' => sprintf($lang['L_ALERTE_TENTATIVES'],$nbre_tentatives)
  146. ));
  147. }
  148. //
  149. // Encode la clef de la zone admin pour la stocker en session
  150. function encoder_code_access(){
  151. $caracteres = str_split($this->code_acces_za,1);
  152. $code_crypte = '';
  153. // Parcours du code secret
  154. foreach($caracteres AS $c){ $code_crypte .= $this->clefs[$c]; }
  155. $_SESSION['digicode_clef_acceptee'] = $code_crypte;
  156. }
  157. //
  158. // Verifie que le code saisi corresponde bien au code definis par les fondateurs
  159. function verifier_code($vars){
  160. $this->purger_cache_digicode();
  161. if (!isset($vars['code'])
  162. || empty($vars['code'])
  163. || !isset($_SESSION['digicode_clef_acceptee'])
  164. || empty($_SESSION['digicode_clef_acceptee']))
  165. {
  166. $this->redirect_echec();
  167. }else{
  168. $code_saisis = preg_replace('/[^a-z0-9]/i','',$vars['code']);
  169. if ($code_saisis != $_SESSION['digicode_clef_acceptee']){
  170. $this->redirect_echec();
  171. }else{
  172. $_SESSION['digicode_TTL'] = time();
  173. $_SESSION['digicode_clef_acceptee'] = null;
  174. $_SESSION['digicode_nbre_tentatives'] = 0;
  175. header('location: '.$this->retour);
  176. exit;
  177. }
  178. }
  179. }
  180. //
  181. // Redirection apres tentative ratee
  182. function redirect_echec(){
  183. $_SESSION['digicode_clef_acceptee'] = null;
  184. $_SESSION['digicode_nbre_tentatives'] = (isset($_SESSION['digicode_nbre_tentatives']))?$_SESSION['digicode_nbre_tentatives']+1:1;
  185. if ($_SESSION['digicode_nbre_tentatives']>2){
  186. global $droits,$session,$lang,$user;
  187. $this->alerte_mail_fondateurs();
  188. $droits->ban_ip($session->ip,time(),sprintf($lang['L_DIGICODE_HACK'],$user['pseudo']));
  189. header('location: ./index.php');
  190. }
  191. $this->afficher_digicode();
  192. }
  193. //
  194. // Mail pour prevenir les fondateurs
  195. function alerte_mail_fondateurs(){
  196. global $root,$cf,$c,$lang,$user,$session;
  197. load_lang('emails');
  198. require_once($root.'class/class_mail.php');
  199. $email = new mail();
  200. $email->Subject = $lang['L_DIGICODE_ALERTE_HACKING'];
  201. $email->titre_message = $lang['L_DIGICODE_ALERTE_HACKING_DETAIL'];
  202. $email->message_explain = $lang['L_DIGICODE_ALERTE_HACKING_EXPLAIN'];
  203. $email->formate_html(sprintf($lang['L_DIGICODE_ALERTE_HACKING_MSG'], date('d/m/Y H\hi'), $user['pseudo'], $session->ip, gethostbyaddr($session->ip)));
  204. $sql = 'SELECT email, pseudo FROM '.TABLE_USERS.' WHERE level>8';
  205. if (!$resultat = $c->sql_query($sql))message_die(E_ERROR,1300,__FILE__,__LINE__,$sql);
  206. while($row = $c->sql_fetchrow($resultat)){
  207. $email->AddAddress($row['email'],$row['pseudo']);
  208. }
  209. $email->Send();
  210. }
  211. //
  212. // Purger le dossier des images
  213. function purger_cache_digicode(){
  214. global $root;
  215. $this->url_cache = $root.$this->url_cache;
  216. if (!is_dir($this->url_cache)) return true;
  217. $ch = @opendir($this->url_cache);
  218. while ($fichier = @readdir($ch))
  219. {
  220. if ($fichier != '.' && $fichier != '..'
  221. && $fichier != '.htaccess' && !is_dir($this->url_cache.$fichier))
  222. {
  223. @unlink($this->url_cache.$fichier);
  224. }
  225. }
  226. @closedir($ch);
  227. return true;
  228. }
  229. //
  230. // Declaration des clefs de langue
  231. function declarer_clefs_langue(){
  232. global $tpl,$lang,$img;
  233. $tpl->assign_vars(array(
  234. 'L_SAISISSEZ_VOTRE_CODE' => $lang['L_SAISISSEZ_VOTRE_CODE'],
  235. 'L_ENTRER' => $lang['L_ENTRER'],
  236. 'L_EFFACER' => $lang['L_EFFACER'],
  237. 'L_ADMIN_DIGICODE' => $lang['L_ADMIN_DIGICODE'],
  238. 'L_ADMIN_DIGICODE_EXPLAIN' => $lang['L_ADMIN_DIGICODE_EXPLAIN'],
  239. 'I_DIGICODE' => $img['digicode'],
  240. ));
  241. }
  242. }
  243. ?>