home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 April / Chip_2003-04_cd1.bin / zkuste / phpgtk / download / samples / grabheader.php < prev    next >
Encoding:
PHP Script  |  2003-01-12  |  10.5 KB  |  350 lines

  1. #!/usr/local/bin/php -q
  2. <?php
  3. // header_grabber.php
  4. // Version : 1.0
  5. // Date : 23/03/02
  6. // Authors : franck tabary ftab@free.fr franck@exeprod.com
  7. //
  8. // URL : http://exeprod.com/phpgtk/
  9. //
  10. // TODO for next version :
  11. // - correct a visual bug when host select (combo)
  12. // - write \r\n when we are under windows, \n under *nix
  13. // - do nothing when entry combo is empty
  14. // - Learn english
  15. //
  16. // Next PHPGTK Project
  17. // - vulnerabilitie & attack scanner
  18. // - site mapper & url checker
  19.  
  20. ///// INITIALISATION
  21. if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {        dl('php_gtk.dll');} else {        dl('php_gtk.so');}
  22.  
  23. ///// CONSTANTS
  24. $item1=array(
  25.     'GET'            =>array('change_method','GET'),
  26.     'POST'            =>array('change_method','POST'),
  27.     'PUT'            =>array('change_method','PUT'),
  28.     'HEAD'            =>array('change_method','HEAD'),
  29.     'REPLY'            =>array('change_method','REPLY'),
  30.     'DELETE'        =>array('change_method','DELETE'),
  31.     'LINK'            =>array('change_method','LINK'),
  32.     'UNLINK'        =>array('change_method','UNLINK'),
  33.     'CHECKIN'        =>array('change_method','CHECKIN'),
  34.     'CHECKOUT'        =>array('change_method','CHECKOUT'),
  35.     'SHOWMETHOD'    =>array('change_method','SHOWMETHOD'),
  36.     'TEXTSEARCH'    =>array('change_method','TEXTSEARCH'),
  37.     'SPACEJUMP'        =>array('change_method','SPACEJUMP'),
  38.     'SEARCH'        =>array('change_method','SEARCH'),
  39.     );
  40.  
  41. $METHOD="GET";
  42. $CONF_FILE="./header_grabber.conf";
  43.  
  44. ///// FUNCTIONS DECLARATIONS
  45. function delete_event()                        { return false; }
  46. function destroy()                            { Gtk::main_quit(); }
  47. function quit()                                { global  $window; $window->destroy(); }
  48. function change_method($menu_item,$value)    {global $METHOD;$METHOD=$value;}
  49. function close_window($widget)  {$window = $widget->get_toplevel(); Gtk::grab_remove($window); $window->hide();}
  50. function clear_entry()        {  global $entry1;  $entry1->set_text("");}
  51. function clear_result() {global $text1;$text1->delete_text(0,-1);}
  52. function build_option_menu($items,$history=null) {
  53.     $omenu=&new GtkOptionMenu();
  54.     $menu=&new GtkMenu();
  55.     foreach ($items as $item_name => $callback_data) {
  56.         $menu_item=&new GtkMenuItem($item_name);
  57.         array_unshift($callback_data,'activate');
  58.         call_user_func_array(array(&$menu_item,'connect'),$callback_data);
  59.         $menu->append($menu_item);
  60.         $menu_item->show;
  61.     }
  62.     $omenu->set_menu($menu);
  63.     if ($history!==null) { $omenu->set_history($history);}
  64. return $omenu;
  65. }
  66.  
  67. function help_about() {
  68.  
  69.   $msg ="Php Header Grabber by  franck tabary ( ftab@free.fr,franck@exeprod.com )\n\n";
  70.   $msg.="version 1, ( free version ) \n\n";
  71.   $msg.="URL for this little project : http://exeprod.com/phpgtk/\n";
  72.   $msg.="PHP manual is at http://www.php.net\n";
  73.   $msg.="Get Latest PHPGtk at http://gtk.php.net";
  74.  
  75.   $about = &new gtkwindow();
  76.   Gtk::grab_add($about);
  77.   $about->set_border_width(10);
  78.   $about->set_usize(550,300);
  79.   $about->set_policy(FALSE,FALSE,FALSE);
  80.   $about->set_position(GTK_WIN_POS_CENTER);
  81.   $about->set_title("About");
  82.   $about->connect('destroy', destroy);
  83.   $about->connect('delete-event', delete_event);
  84.   $box1 = &new gtkvbox(FALSE,0);
  85.   $about->add($box1);
  86.   $box1->show();
  87.   $label1 = &new gtklabel($msg);
  88.   $label1->set_justify(GTK_JUSTIFY_FILL);
  89.   $box1->pack_start($label1,TRUE,TRUE,10);
  90.   $label1->show();
  91.   $bbox = &new gtkhbuttonbox();
  92.   $box1->pack_start($bbox,TRUE,TRUE,10);
  93.   $bbox->show();
  94.   $button = &new GtkButton('OK');
  95.   $bbox->pack_start($button,FALSE,FALSE,0);
  96.   $button->connect_object("clicked", "close_window", $about);
  97.   $button->show();
  98.   $about->show();
  99. }
  100.  
  101. function hit_enter($entry,$event) {
  102. $value=$event->keyval;
  103. global $quit;
  104. if ($value==GDK_KEY_Return) {
  105.     evaluate_entry();
  106.     } else {
  107.         if ($value=="65507") {
  108.             $quit=1;
  109.         } else {
  110.             if ($value=="88"&&$quit) {
  111.                 quit();
  112.             }
  113.         }
  114.     }
  115. }
  116.  
  117. function create_file_selection() {
  118.     global $windows;
  119.     if (!isset($windows['file_selection'])) {
  120.         $window = &new GtkFileSelection('File selection dialog');
  121.         $windows['file_selection'] = $window;
  122.         $window->hide_fileop_buttons();
  123.         $window->set_position(GTK_WIN_POS_MOUSE);
  124.         $window->connect('delete_event', 'delete_event');
  125.  
  126.         $button_ok = $window->ok_button;
  127.         $button_ok->connect('clicked', 'file_selection_ok',$window);
  128.  
  129.         $button_cancel = $window->cancel_button;
  130.         $button_cancel->connect('clicked', 'close_window');
  131.  
  132.         $action_area = $window->action_area;
  133. /*
  134.         $button = &new GtkButton('Hide Fileops');
  135.         $button->show();
  136.         $button->connect_object('clicked', create_function('$w', '$w->hide_fileop_buttons();'), $window);
  137.         $action_area->pack_start($button, false, false, 0);
  138.  
  139.         $button = &new GtkButton('Show Fileops');
  140.         $button->show();
  141.         $button->connect_object('clicked', create_function('$w', '$w->show_fileop_buttons();'), $window);
  142.         $action_area->pack_start($button, false, false, 0);
  143. */
  144.     }
  145.     if ($windows['file_selection']->flags() & GTK_VISIBLE)
  146.         $windows['file_selection']->hide();
  147.     else
  148.         $windows['file_selection']->show();
  149. }
  150.  
  151. function file_selection_ok($button,$window) {
  152. global $text1;
  153.     $file=$window->get_filename();
  154.     $string = $text1->get_chars(0,-1);
  155.     $fp=fopen($file,"w");
  156.     fwrite($fp,$string);
  157.     fclose($fp);
  158.     close_window($window);
  159. }
  160.  
  161. function evaluate_entry() {
  162.   global $text1;
  163.   global $entry1;
  164.   global $lenprev;
  165.   global $METHOD;
  166.   global $lentotal;
  167.   global $status;
  168.  
  169.   $status_context = $status->get_context_id("foo");
  170.   $status->push($status_context, "Contacting host $host ...");
  171.   while (gtk::events_pending())  gtk::main_iteration();
  172.   add_one();
  173.  
  174.   $host = $entry1->get_text();
  175.   if (ereg("https",$host)) {$port=443;} else {$port=80;}
  176.   $host=ereg_replace("https?://","",$host);
  177.   $position=0;
  178.   clear_result();
  179.   $url=strstr($host,"/");
  180.   if (!$url) {$url="/";} else {$host=ereg_replace($url,"",$host);}
  181.  
  182.   $fp=fsockopen($host,$port,$errno,$errstr,10);
  183.   if (!$fp) {
  184.     $header="$errstr ($errno)\n";
  185.     if (!$errno) { $header="Host $host not found\n";}
  186.   } else {
  187.     $entry1->set_sensitive(false);
  188.     $phrase="$METHOD $url HTTP/1.0";
  189.     $phrase.="\r\n";
  190.     $phrase.="Host: $host";
  191.     $phrase.="\r\n\r\n";
  192.     $phrase2="$METHOD $url HTTP/1.0";
  193.     $phrase2.="\n";
  194.     $phrase2.="Host: $host";
  195.     $phrase2.="\n\n";
  196.  
  197.     $text1->insert_text($phrase2,1);
  198.     $text1->thaw();
  199.     fputs($fp,$phrase);
  200.     $ok=0;
  201.     while (!feof($fp)&&!$ok) {
  202.         $l=fgets($fp,128);
  203.         $l=ereg_replace("\n","",$l);
  204.         $l=chop(trim($l));
  205.         if ($l=="") {
  206.             $ok=1;
  207.         } else {
  208.             $header.=$l."\n";
  209.         }
  210.     }
  211.     fclose($fp);
  212.   }
  213. if ($header) {
  214.     $text1->insert_text($header,strlen($phrase2));
  215.     $text1->thaw();
  216.     $text1->freeze();
  217. }
  218.   $entry1->set_sensitive(true);
  219.   $status->push($status_context, "Terminated.");
  220.   while (gtk::events_pending())  gtk::main_iteration();
  221. }
  222.  
  223.  
  224. function add_one() {
  225. global $strings,$entry1,$entry0,$CONF_FILE;
  226. $entered = $entry1->get_text();
  227. $entered=ereg_replace("\n","",$entered);
  228. $entered=ereg_replace("\r","",$entered);
  229. $entry0->disable_activate();
  230. if (!in_array($entered,$strings)) {
  231.     $strings[]= $entered;
  232.     $strings=array_unique($strings);
  233.     $max=count($strings);
  234.     $entry0->set_popdown_strings($strings);
  235.     $i=0;
  236.     $fp=fopen($CONF_FILE,"w");
  237.     while ($i<$max) {
  238.         $s=$strings[$i];
  239.         $s=ereg_replace("\n","",$s);
  240.         $s=ereg_replace("\r","",$s);
  241.         if ($s) {
  242.             fwrite($fp,$s."\r\n");
  243.             if ($s==$entered) {
  244.                 $sel=$i;
  245.             }
  246.         }
  247.         $i++;
  248.     }
  249.     fclose($fp);
  250.     //$entry0->set_value_in_list($sel,0);
  251. }
  252. $entry1->set_text($entered);
  253. $entry1->grab_focus();
  254. }
  255.  
  256. // APPLICATION CONSTRUCTION
  257. $window = &new GtkWindow();
  258. $window->connect('destroy', destroy);
  259. $window->connect('delete-event', delete_event);
  260.  
  261. $window->set_usize(700,400);
  262. $window->set_policy(TRUE,TRUE,FALSE);
  263. $window->set_title("Php Header Grabber");
  264. $window->set_position(GTK_WIN_POS_CENTER);
  265.  
  266. $box1 = &new GtkVBox(FALSE,0);
  267. $window->add($box1);
  268. $box1->show();
  269.  
  270. $ag = &new GtkAccelGroup();
  271. $itemf = &new GtkItemFactory(GtkMenuBar::get_type(), '<main>', $ag);
  272. $window->add_accel_group($ag);
  273. $itemf->create_items(array(
  274.         array('/_File',                '',                null,                    0,'<Branch>'),
  275.         array('/_File/_Save ...',    '',                'create_file_selection',2,''),
  276.         array('/_File/E_xit',        '<control>X',    'quit',                    2,''),
  277.         array('/_Help',                '',                null,                    0, '<LastBranch>'),
  278.         array('/_Help/_About',       'F1',          help_about,                0,''))
  279.         );
  280. $box1->pack_start($itemf->get_widget('<main>'),FALSE,FALSE,0);
  281.  
  282. $box2 = &new gtkhbox(FALSE,0);
  283. $box1->pack_start($box2,FALSE,FALSE,0);
  284. $box2->show();
  285.  
  286. $popup=build_option_menu($item1,0);
  287. $box2->pack_start($popup,TRUE,TRUE,1);
  288.  
  289. $s=file($CONF_FILE);
  290. $i=0;
  291. $max=count($s);
  292. while ($i<$max) {
  293.     $t=$s[$i];
  294.     $t=ereg_replace("\n","",$t);
  295.     $t=ereg_replace("\r","",$t);
  296.     if ($t) {
  297.         $strings[$i]=$t;
  298.     }
  299.     $i++;
  300. }
  301.  
  302.  
  303. $entry0 = &new GtkCombo();
  304. $entry0->set_usize(500,20);
  305. $entry0->set_popdown_strings($strings);
  306. $entry1= $entry0->entry;
  307. $entry1->select_region(0, -1);
  308. $entry1->set_text("");
  309. $box2->pack_start($entry0);
  310. //$list=$entry0->list;$list->connect('select-child', 'combo_select',$listed);
  311. $entry0->show();
  312.  
  313. $box2->pack_start($entry1,TRUE,TRUE,0);
  314. $entry1->show;
  315.  
  316. $button = &new GtkButton(' Get headers ');
  317. $button->connect('clicked', 'evaluate_entry');
  318.  
  319. $box2->pack_start($button,FALSE,FALSE,0);
  320. $button->show();
  321.  
  322. $box3 = &new GtkHbox(FALSE,0);
  323. $box1->pack_start($box3,TRUE,TRUE,0);
  324. $box3->show();
  325.  
  326. $text1 = &new gtktext();
  327. $text1->set_editable(TRUE);
  328. $box3->pack_start($text1,TRUE,TRUE,0);
  329. $text1->show();
  330.  
  331. $scroll = &new gtkvscrollbar($text1->vadj);
  332. $box3->pack_start($scroll,FALSE,TRUE,0);
  333. $scroll->show();
  334.  
  335. $box4 = &new GtkVbox(FALSE,0);
  336. $box1->pack_start($box4,FALSE,FALSE,0);
  337. $box4->show();
  338.  
  339. $status=&new GtkStatusBar();
  340. $box1->pack_start($status,FALSE,FALSE,0);
  341.  
  342. $window->show_all();
  343.  
  344. $entry1->grab_focus();
  345. $entry1->connect('key-press-event','hit_enter');
  346.  
  347. /* Run the main loop. */
  348. Gtk::main();
  349. ?> 
  350.