Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Generator Tokenów
Balon
post
Post #1





Grupa: Zarejestrowani
Postów: 422
Pomógł: 0
Dołączył: 14.12.2005
Skąd: Wałbrzych

Ostrzeżenie: (0%)
-----


  1. <?php
  2. /**
  3.  * License
  4.  *
  5.  * This library is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU Lesser General Public
  7.  * License as published by the Free Software Foundation; either
  8.  * version 2.1 of the License, or (at your option) any later version.
  9.  *
  10.  * This library is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13.  * Lesser General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Lesser General Public
  16.  * License along with this library; if not, write to the Free Software
  17.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18.  **/
  19. /**
  20.  * @author Krzysztof (Balon) Jagiełło <balonyo@gmail.com>
  21.  * @copyright 2007 Krzysztof Jagiełło.
  22.  * @version 1.0
  23.  */
  24.  
  25. class Token {
  26. /** Kod na tokenie */
  27. var $_tokenText;
  28.  
  29. /** Adres folderu czcionek */
  30. var $_fontDir = '';
  31.  
  32. /** Nazwy czcionek */
  33. var $_fontFiles = array(
  34. 'georgia.TTF'
  35. );
  36.  
  37. /** Typ obrazka */
  38. var $_imgType = 'gif';
  39.  
  40. /** Długość kodu na tokenie */
  41. var $_length;
  42.  
  43. /** Kolor tła */
  44. var $_backColor = '#ffffff';
  45.  
  46. /** Kolor czcionki */
  47. var $_fontColor = '#466900';
  48.  
  49. /** Kolor elementów tłą */
  50. var $_elemsColor = '#E1E1E1';
  51.  
  52. /** Szerokość tokena */
  53. var $_width;
  54.  
  55. /** Wyskość tokena */
  56. var $_height = 25;
  57.  
  58. /** Obiekt tła */
  59. var $_bgObject;
  60.  
  61. /** Obrazek tokena */
  62. var $_image;
  63.  
  64. /** Nazwa klucza sesji z tokenem */
  65. var $_tokenId = 'token';
  66.  
  67. /** Dozwolone rozszerzenia */
  68. var $_allowedTypes = array( 
  69. 'gif', 'jpg', 'png' 
  70. );
  71.  
  72. /** Komunikaty błędów */
  73. var $_errorMsg = array(
  74. 'wrong_color'  => 'Format koloru (%s) jest nieprawidłowy.',
  75. 'wrong_object' => 'Podany obiekt nie istnieje.',
  76. 'wrong_img_type'=> 'Podany typ ("%s") jest nieprawidłowy.',
  77. 'wrong_file' => 'Czcionka "%s" nie istnieje .' 
  78. );
  79.  
  80. /**
  81.  * Konstruktor klasy
  82.  *
  83.  * @param integer $length - długość tekstu na tokenie 
  84.  */
  85. function Token( $length, $bgObject ){
  86. $this->_length = $length;
  87. $this->_tokenText = substr(md5(uniqid(time())), 0 - $length);
  88. $this->_width = $length * 15 + 10;
  89. $this->_image = imagecreate( $this->_width, $this->_height);
  90. if( !is_object( $bgObject ) )
  91. trigger_error( $this->_errorMsg['wrong_object'], E_USER_WARNING );
  92. $this->_bgObject = $bgObject;
  93. }
  94.  
  95. /**
  96.  * Tworzy token, zapisuje go w sesji i wyświetla.
  97.  */
  98. function getToken(){
  99. // kolorki
  100. $this->_backColor = $this->MakeColor( $this->_image, $this->_backColor );
  101. $this->_fontColor = $this->MakeColor( $this->_image, $this->_fontColor );
  102. $this->_elemsColor = $this->MakeColor( $this->_image, $this->_elemsColor );
  103.  
  104. $this->Background();
  105. $this->Text();
  106.  
  107. $_SESSION[$this->_tokenId] = $this->_tokenText;
  108.  
  109. if( !in_array( $this->_imgType, $this->_allowedTypes ) )
  110. trigger_error( sprintf( $this->_errorMsg['wrong_img_type'], $this->_imgType ), E_USER_ERROR );
  111. header( 'Content-type: image/' . $this->_imgType );
  112. $this->Display();
  113. }
  114.  
  115. /**
  116.  * Tworzy tekst na tokenie
  117.  */
  118. function Text(){
  119. for($i = 0; $i < strlen($this->_tokenText); $i++){
  120.  imagettftext( $this->_image, rand(14, 16), rand(-10, 10), rand(3, 5) + $i * 15, 18 + rand(-3, 3), $this->_fontColor, $this->getFontDir() . $this->_fontFiles[rand(0,count($this->_fontFiles)-1)], $this->_tokenText{$i});
  121. }
  122. return true;
  123. }
  124.  
  125. /**
  126.  * Tworzy tło korzystając z podanej klasy tła
  127.  * 
  128.  * @return true
  129.  */
  130. function Background(){
  131. $this->_bgObject->process( &$this->_image, get_object_vars( $this ) );
  132. return true;
  133. }
  134.  
  135. /**
  136.  * Wyświetla token w wybranym formacie
  137.  */
  138. function Display(){
  139. if( !in_array( $this->_imgType, $this->_allowedTypes ) )
  140. trigger_error( sprintf( $this->_errorMsg['wrong_img_type'], $this->_imgType ), E_USER_ERROR );
  141. call_user_func( 'image' . $this->_imgType, $this->_image );
  142. }
  143.  
  144. /**
  145.  * Dodaje czcionki to tablicy. Są one potem losowo używane w tokenie.
  146.  */
  147. function assignFonts(){
  148. $fonts = func_get_args();
  149. foreach( $fonts as $font ){
  150. if( !file_exists( $this->getFontDir() . $font ) )
  151. trigger_error( sprintf( $this->_errorMsg['wrong_file'], $font ), E_USER_ERROR );
  152. else
  153. $this->_fontsFiles[] = $font;
  154. }
  155. }
  156.  
  157. /**
  158.  * Zwraca katalog z czcionkami
  159.  */
  160. function getFontDir(){
  161. return $this->_fontDir == '' ? '' : $this->_fontDir . '/';
  162. }
  163.  
  164. /**
  165.  * Zwraca kolor dla obrazka
  166.  * 
  167.  * @param resource $img
  168.  * @param string $color - kolor w formacie hexagonalny
  169.  * @return integer
  170.  */
  171. function MakeColor( $img, $color ){
  172. if( !preg_match('/^[#]{0,1}[a-f0-9]{6}$/i', $color ) )
  173. trigger_error( sprintf( $this->_errorMsg['wrong_color'], $color ), E_USER_ERROR );
  174.  
  175. $color = str_replace('#', '', $color);
  176. $return = array(
  177. 'r' => hexdec(substr($color, 0, 2)),
  178. 'g' => hexdec(substr($color, 2, 2)),
  179. 'b' => hexdec(substr($color, 4, 2))
  180. );
  181. return imagecolorallocate( $img, $return['r'], $return['g'], $return['b']);
  182. }
  183. }
  184.  
  185. class TokenBackground_Dots {
  186. function process( $image, $params ){
  187. $pts = array();
  188. for($i = 0; $i < round($params['_width'] / 1.5); $i++) {
  189. $x = rand(0, $params['_width'] );
  190. $y = rand(0, $params['_height'] );  
  191.  
  192. if( !in_array( $x.'_'.$y, $pts ) ){
  193. imageellipse( $image, $x, $y, rand(2, 7), rand(3, 6), $params['_elemsColor'] );
  194. $pts[] = $x.'_'.$y;
  195. }
  196. else {
  197. $i--;
  198. }
  199. }
  200. }
  201. }
  202.  
  203. class TokenBackground_Slashes {
  204. function process( $image, $params ){
  205. $step = 5;
  206. for($i = 0; $i < round($params['_width']) + ($step*10); $i+= $step) {
  207. imageline( $image, $i, 0, 0, $i, $params['_elemsColor'] );
  208. }
  209. }
  210. }
  211.  
  212. class TokenBackground_Grid {
  213. function process( $image, $params ){
  214. for($i = 0; $i < round($params['_width']); $i+= 5) {
  215. // poziome linie
  216. imageline( $image, 0, $i, $params['_width'], $i, $params['_elemsColor'] );
  217. // pionowe linie
  218. imageline( $image, $i, 0, $i, $params['_height'], $params['_elemsColor'] );
  219. }
  220. }
  221. }
  222.  
  223. $token = new Token(32, new TokenBackground_Dots );
  224. $token->getToken();
  225. ?>


Klasa tokena.

Najważniejsze cechy:
- Proste tworzenie nowych efektów tła,
- duża konfigurowalność działania klasy.

Przykładowe efekty tła:
  1. <?php
  2. /** Ładne kółeczka - zgapiłem z jakiegoś innego tokena ;) */
  3. class TokenBackground_Dots {
  4. function process( $image, $params ){
  5. $pts = array();
  6. for($i = 0; $i < round($params['_width'] / 1.5); $i++) {
  7. $x = rand(0, $params['_width'] );
  8. $y = rand(0, $params['_height'] );  
  9.  
  10. if( !in_array( $x.'_'.$y, $pts ) ){
  11. imageellipse( $image, $x, $y, rand(2, 7), rand(3, 6), $params['_elemsColor'] );
  12. $pts[] = $x.'_'.$y;
  13. }
  14. else {
  15. $i--;
  16. }
  17. }
  18. }
  19. }
  20. /** Ukośne paski */
  21. class TokenBackground_Slashes {
  22. function process( $image, $params ){
  23. $step = 5;
  24. for($i = 0; $i < round($params['_width']) + ($step*10); $i+= $step) {
  25. imageline( $image, $i, 0, 0, $i, $params['_elemsColor'] );
  26. }
  27. }
  28. }
  29.  
  30. /** Najzwyklejsza kratka */
  31. class TokenBackground_Grid {
  32. function process( $image, $params ){
  33. for($i = 0; $i < round($params['_width']); $i+= 5) {
  34. // poziome linie
  35. imageline( $image, 0, $i, $params['_width'], $i, $params['_elemsColor'] );
  36. // pionowe linie
  37. imageline( $image, $i, 0, $i, $params['_height'], $params['_elemsColor'] );
  38. }
  39. }
  40. }
  41. ?>


Sposób użycia:
Kod
<?php
// ... tutaj klasa tokena
// ... tutaj klasa efektu

$token = new Token(20, new TokenBackground_Dots );
$token->getToken();
?>

.

Ten post edytował kwiateusz 19.05.2007, 16:19:57
Go to the top of the page
+Quote Post

Posty w temacie
- Balon   Generator Tokenów   19.05.2007, 14:19:07
- - hwao   Kod wygląda super:) Ale prosił bym o dodanie np j...   19.05.2007, 22:56:01
- - grzesio   Jak to uruchomić ? nie działa mi ten skrypt ! ...   20.05.2007, 23:26:06
- - my salsa   ciekawa klasa, ale mi brakuje dwoch rzeczy: - nie ...   20.05.2007, 23:53:51
|- - Balon   Cytat(my salsa @ 20.05.2007, 22:53:51...   23.05.2007, 09:45:56
- - ChowiX   Prosze napisac tak dokładniej ja uży...   28.06.2007, 14:22:08
- - Spirit86   musisz jeszcze skopiować obiekt tła: [PHP] pobier...   30.06.2007, 18:53:14
- - Balon   Cytatdo pliku i wiem ze cos zle zrobilem bo nie dz...   2.07.2007, 15:20:07
- - graft   Kurczeeeee, fajnie, tylko że klasy nigdy nie były ...   30.07.2007, 22:29:53
- - Balon   Standardowo poprzez $_SESSION['token...   31.08.2007, 09:33:49
- - Sedziwoj   Na pewno wywalił bym ustawienie długości do set...   1.09.2007, 21:23:02
- - ActivePlayer   [PHP] pobierz, plaintext <?phpclass TokenChecke...   16.12.2007, 22:13:58
- - Taifun   czemu jak dam [PHP] pobierz, plaintext <?php...   8.03.2008, 15:13:25
- - MiFlo   Nie wyświetla Ci tego co wypisałeś bo nagłówki zos...   19.05.2008, 19:47:41
- - kacpereczek   Mam takie pytanie: Czy moglibyście pokazać jak wst...   15.08.2008, 17:21:51
|- - .radex   Cytat(kacpereczek @ 15.08.2008, 18:21...   15.08.2008, 21:28:26
|- - kacpereczek   Cytat(.radex @ 15.08.2008, 22:28:26 )...   16.08.2008, 13:58:04
- - .radex   hmmm... jak masz powiedzmy obrazek o nazwie token...   16.08.2008, 14:42:55
- - kacpereczek   Bardzo poproszę o przykładowy kod, ale nie na bazi...   18.08.2008, 11:06:54
- - hinduseek   Nie polecam tego zabezpieczenia na boty. Przechodz...   19.10.2008, 23:10:46
- - DonJeday   Dobra klasa elegancko działa, ale powiedzcie mi ja...   26.10.2008, 10:35:48
- - hinduseek   [PHP] pobierz, plaintext <?php$token = new Toke...   27.10.2008, 22:56:19
- - crackcomm   Kod<?php /** * License * * This library is ...   3.07.2009, 18:34:37
- - smarcz   Nie wiem dlaczego ale nie zapisuje mi się do SESJI...   19.07.2009, 22:02:18
- - skowron-line   @smarcz pokaż kod może czegoś ci brakuje np. [PHP...   19.07.2009, 22:06:11


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 3.10.2025 - 21:27