A Simple PHPGTK Application

   

This next application uses GTK widgets to create a small application that encodes passwords. You enter a username and password into their respective fields, then click the "Generate…" button to create an encrypted username and password combination that can be used in some of the previous scripts in this book. The username and password are displayed in a text box in the middle of the application. When you have created as many passwords as needed, you can click the "Write To File" button to write the data to a text file. See Figure 12-8 for example output.

Script 12-6 password_gen.php
  1.  <?php  2.    if (!class_exists('gtk')) {  3.    if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')  4.      dl('php_gtk.dll');  5.    else  6.      dl('php_gtk.so');  7.  }  8.  function destroy(){  9.    Gtk::main_quit(); 10.  } 11.  function generate(){ 12.    global $user_in, $pass_in, $output; 13.    $text = $user_in->get_text() . ":" . encoded($pass_in->get_text()) . "\n"; 14.    $output->insert(null, null, null, $text); 15.    $user_in->set_text(''); 16.    $pass_in->set_text(''); 17.  } 18.  function encoded($text) { 19.    return md5($text); 20.  } 21.  function write(){ 22.    global $output; 23.    $text = $output->get_chars(0,-1); 24.    $file = fopen("pass.txt", "w") or die("CANNOT OPEN FILE"); 25.    fwrite($file, $text); 26.    fclose($file); 27.    $output->delete_text(0,-1); 28.    $complete = "Passwords written to file!\n"; 29.    $output->insert(null, null, null, $complete); 30.  } 31.  $window = &new GtkWindow(); 32.  $window->set_default_size(300, 300); 33.  $window->connect('destroy', 'destroy'); 34.  $user_label = &new GtkLabel("Username: "); 35.  $user_in = &new GtkEntry(); 36.  $user_box = &new GtkHBox(); 37.  $user_box->pack_start($user_label, false, false, 0); 38.  $user_box->pack_start($user_in, false, false, 0); 39.  $pass_label = &new GtkLabel("Password: "); 40.  $pass_in = &new GtkEntry(); 41.  $pass_box = &new GtkHBox(); 42.  $pass_box->pack_start($pass_label, false, false, 0); 43.  $pass_box->pack_start($pass_in, false, false, 0); 44.  $button1 = &new GtkButton('Generate Username:Password!'); 45.  $button1->connect('clicked', 'generate'); 46.  $output = &new GtkText(); 47.  $button2 = &new GtkButton('Write To File'); 48.  $button2->connect('clicked', 'write'); 49.  $vbox = &new GtkVBox(); 50.  $vbox->pack_start($user_box); 51.  $vbox->pack_start($pass_box); 52.  $vbox->pack_start($button1); 53.  $vbox->pack_start($output); 54.  $vbox->pack_start($button2); 55.  $window->add($vbox); 56.  $window->show_all(); 57.  Gtk::main(); 58.  ?> 
Figure 12-8. password_gen.php

graphics/12fig08.jpg

Script 12-6. password_gen.php Line-by-Line Explanation

LINE

DESCRIPTION

2

If the GTK class has not been defined, then execute lines 3 7. If the GTK class has been defined, then the script needs to do nothing.

3

Check the first three characters of the PHP_OS environment variable.

4

If it is WIN, then the system is running Windows. Load the PHPGTK library for Windows.

5 7

If the PHP_OS environment variable is not WIN, then assume that PHP is running on a Linux system and load the Linux PHPGTK library.

8 10

Define a function, destroy(), that stops the script when called.

9

Execute the Gtk::main_quit() method to stop the script and close the GTK window.

10

End the function declaration.

11 17

Define a function, generate(), that encrypts a username/password combination entered by the user and prints it to a text box inside the application.

12

Assign the $user_in, $pass_in, and $output variables as global so that they can be used inside this function.

13

Assign the user-entered username and password to the $text variable.

14

Insert the username and encoded password into the text box using the insert() method for the text object. insert() takes four arguments:

  • Font A GDK Font object.

  • Color A GDK color object.

  • Size The number of characters you wish to insert.

  • String The text to be inserted.

For simplicity, I set the first three arguments as null so that the default values are used.

15

Clear the user-entered text in the Username field by inserting a blank string into the entry box.

16

Clear the user-entered text in the Password field by inserting a blank string into the entry box.

17

End the function declaration.

18 20

Define a function, $encode, that encodes the entered password.

19

Encode the password using the md5() function and return it to the calling function. You can change the encryption method to suit your needs (for example by using the crypt() function).

20

End the function declaration.

21 30

Define a function, write(), that writes the encoded username/passwords to a file.

22

Assign the $output variable as global so that it can be used inside this function.

23

Get the entire text from the text object using the get_chars() method and place it into the $text variable. The get_chars() method takes two arguments:

  • The start position of the text (0 is the first position).

  • The end position of the text.

Using 0 and -1 as arguments causes the entire text in the text object to be collected.

24

Open a file for writing. Note that if you have an existing file with the same name, it will be written over with the new username/password combos. You must also set permissions on the file so that it can be written to by PHP.

25

Write the text to the file.

26

Close the file.

27

Delete the text in the text area.

28

Create a string of text notifying the user that the text has been written.

29

Write the string to the text area. Note that you will need to close and restart the application if you wish to create passwords again.

30

End the function declaration.

31

Create a new window object called $window.

32

Set the size of the window to be 300 x 300 pixels.

33

Connect the 'destroy' method of the window to the destroy() function that you defined earlier. The 'destroy' method is called when the user clicks the "X" on the top right of the window.

34

Create a new label object for the username label.

35

Create a new text-entry object for the username.

36

Create a new hbox in which to place the username label and text-entry objects.

37

Pack the label object into the hbox.

38

Pack the text-entry object into the hbox.

39

Create a new label object for the password label.

40

Create a new text-entry object for the password.

41

Create a new hbox in which to place the password label and text-entry objects.

42

Pack the label object into the hbox.

43

Pack the text-entry object into the hbox.

44

Create a new button object for the "Generate…" button.

45

Connect the "Generate…" button to the generate() function.

46

Create a new text object to display the output.

47

Create a new button object for the "Write To File" button.

48

Connect the "Write To File" button to the write() function.

49

Create a new vbox to hold the other boxes and buttons.

50

Pack the hbox containing the Username label and entry into the vbox.

51

Pack the hbox containing the Password label and entry into the vbox.

52

Pack the "Generate…" button into the vbox.

53

Pack the text object into the vbox.

54

Pack the "Write To File" button into the vbox.

55

Add the vbox to the window.

56

Show all of the contents of the window.

57

Execute the script.


   
Top


Advanced PHP for Web Professionals
Advanced PHP for Web Professionals
ISBN: 0130085391
EAN: 2147483647
Year: 2005
Pages: 92

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net