PHP WinBinder 3: Introduction

From The Uniform Server Wiki
Revision as of 16:16, 17 October 2012 by Olajideolaolorun (talk | contribs) (Text replace - "projects/uniformserver" to "projects/miniserver")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

 

UniServer 5-Nano
PHP WinBinder 3.

WinBinder Part 3

Introduction

PHP is a powerful scripting language you can run its scripting engine from a command prompt. However when it comes to user input, you will quickly discover it is very primitive.

For a small amount of user input this may be adequate however it is an irritation, especially if a user makes a typing error. For example you may require a user to enter the full path to a specific file. This may be buried several folders deep and is certainly prone to typing errors.

This tutorial provides support material for scripts released with a range of new plugins for Uniform Server 5.6.2 these can be found under Applications at SourceForge.

Each Application has an associated script (application name_pro.exe) after extracting you will find the source code in folder docs.

Each script creates a restricted user account on the MySQL server. This requires a user to input a name and password. A small pop-up window created by WinBinder is used for this purpose. However prior to creating this pop-up window a user may be requested to select the full path to the associated application’s folder.

Significance of that last statement, for some scripting applications there may be no need to even create, a pop-up window. WinBinder for user input and output contains several standard dialog functions; these can be directly used within a script or used within a Widow application.

Top

Pop-up Window

Strange as it may seem there is no standard API for a name-password user input pop-up. This is not a serious issue just create a small Windows application using WinBinder.

Creating complex Windows applications WinBinder uses four include files; however smaller application do not require these. The trick here is to extract only the function you require and place them in your application script. Advantage of this, only a single file is required!

OK not strictly true! Apart from support files a batch file is required. What are these support files? Before starting the tutorial it’s worth looking at these files and structure.

Top

Tutorial Plugin

A plugin for this tutorial is downloadable from SourgeForge it is located in the applications folder.

The plugin follows same structure as plugins for each application, main difference being it contains minimal code hence is a template.

Top

Download and extract

  1. Download V56_Pro_demo_wb.exe
  2. Save file to folder UniServer
  3. Double click to extract files (Allow overwrite).
  4. To save space you can delete V56_Pro_demo_wb.exe

Top

Files extracted

  • UniServer\Pro_demo_wb.bat - Runs script Pro_demo_wb.php
  • UniServer\Pro_demo_wb.php - Script
  • UniServer\Read_me_Pro_demo.txt - This file
  • UniServer\usr\local\php\php_pro_wb.ini - Config
  • UniServer\usr\local\php\extensions\php_winbinder.dll - WinBinder

Reference (Archive)

  • UniServer\docs\SRC\pro_demo - Archive source code folder
  • UniServer\docs\SRC\pro_demo\Pro_demo_wb.bat - Batch file
  • UniServer\docs\SRC\pro_demo\Pro_demo_wb.php - Script
  • UniServer\docs\SRC\pro_demo\pro.ico - Image to use in exe

Top

Batch file

The batch file (Pro_demo_wb.bat) is effectively a single line of code that runs our script (Pro_demo_wb.php).

usr\local\php\php.exe -c usr\local\php\php_pro_demo_wb.ini Pro_demo_wb.php
  • usr\local\php\php.exe

PHP CLI scripts are independent of the server and require their own configuration file. PHP is forced to use our configuration file using the –c switch this is followed by a relative path (relative to the batch file) to the configuration file as follows:

  • usr\local\php\php_pro_demo_wb.ini

The last parameter is the script we wish to run. It requires not path since the first place PHP looks for it is the current working directory (where the batch file is run from) hence the last parameter is the file name:

  • Pro_demo_wb.php

Top

PHP Configuration file

Our scripts use the WinBinder extension hence we inform PHP to load this using a configuration file.

Note: To avoid overwriting existing configuration files avoid the following names:

  • Application plugins use a file named php_pro_wb.ini
  • Controller unicon uses php-cli.ini
  • Z-con uses php-wb.ini

Hence the demo avoids any conflicts by using a CLI configuration file named php_pro_demo_wb.ini

It containes the following lines:

[PHP]

extension=php_winbinder.dll
extension=php_curl.dll
extension=php_sqlite.dll
extension=php_mbstring.dll

extension=php_mysql.dll
extension_dir = "./extensions"
error_reporting = E_ALL | E_STRICT
date.timezone = "Europe/London"

For this demo if you wish you can delete all lines with the exception of these three:

  • [PHP]
  • extension=php_winbinder.dll -- Required for scripts
  • extension_dir = "./extensions" -- Where to find extensions

Top

Script (Template)

All application scripts have the following format:

<?php
/*
Text
*/

chdir(dirname(__FILE__));                  // Change wd to this files location

//-- Global variables ---------------------------------------------------------


//=== INIT ====================================================================

//=============================================================== END INIT ====

//== If our application requires user input the following section creates a
//   pop-up window to capture that input. Processing continues in Handler
//   Function see section 4)

//-- Constants ----------------------------------------------------------------
define('IDC_USER_NAME',        11010); // Text box New user name
define('IDC_USER_PASSWORD',    11020); // Text box New user password
define('IDC_USER_ENTER',       11030); // Button Enter new user
define('IDC_USER_HELP',        11040); // Button Help


//=== 1) Create main window ---------------------------------------------------
$mainwin = wb_create_window(NULL, PopupWindow, "Demo", WBC_CENTER, WBC_CENTER, 210, 150, 0);

//=== 2) Create controls for the main window ----------------------------------

wb_create_control($mainwin, Frame, 'Enter name password',  5,  5, 195, 105, 0, 0, 0, 0);
wb_create_control($mainwin, Label, 'User Name',           15, 25,  50,  15, 0, 0, 0, 0);
wb_create_control($mainwin, EditBox, '',                  75, 25, 115,  20, IDC_USER_NAME, 0, 0, 0);
wb_create_control($mainwin, Label, 'Password',            15, 50,  45,  15, 0, 0, 0, 0);
wb_create_control($mainwin, EditBox, '',                  75, 50, 115,  20, IDC_USER_PASSWORD, 0, 0, 0);
wb_create_control($mainwin, PushButton, 'Helpl',          15, 80,  50,  20, IDC_USER_HELP, 0, 0, 0);
wb_create_control($mainwin, PushButton, 'Enter',          75, 80, 115,  20, IDC_USER_ENTER, 0, 0, 0);

//=== 3) Assign handler function to the main window  --------------------------
wb_set_handler($mainwin, "process_main");       

//=== 5) Enter application loop -----------------------------------------------
wb_main_loop();                                  

//=== 4) Handler Function =====================================================
function process_main($window, $id){

  switch($id) { 

   //--- GET USER INPUT -------------------------------------------------------=
   case  IDC_USER_ENTER:

     //-- Read user input 
     $name  = wb_get_text(wb_get_control($window, IDC_USER_NAME));     // Get name
     $name  = trim($name);
     $pass  = wb_get_text(wb_get_control($window, IDC_USER_PASSWORD)); // Get pass
     $pass  = trim($pass);
   break; 

 //--- CLOSE ------------------------------------------------------------------
   case IDCLOSE:                        // Constant IDCLOSE (8) predefined 
    wb_destroy_window($window);         // Destroy the window
    break; 
  }
}
//==================================================== End Handler Function ===

//###== FROM WINBINDER MINIMUM REQUIRED FOR TEXT AND BUTTONS ==################

//--- CREATE CONTROL ----------------------------------------------------------
function wb_create_control($parent, $class, $caption="", $xpos=0, $ypos=0, $width=0, $height=0, $id=null, $style=0, $lparam=null, $ntab=0){
  return wbtemp_create_control($parent, $class, $caption, $xpos, $ypos, $width, $height, $id, $style, $lparam, $ntab);
}
//----------------------------------------------------- END CREATE CONTROL ----

//--- GET CONTROL TEXT --------------------------------------------------------
function wb_get_text($ctrl, $item=null, $subitem=null){
  if(!$ctrl) return null;
  return wbtemp_get_text($ctrl);
}
//---------------------------------------------------- END GET CONTROL TEXT ---

//###== END FROM WINBINDER MINIMUM REQUIRED FOR TEXT AND BUTTONS ==############
?>

Top section /*Text*/ allows you to enter a description of what the script does.

Change working directory is optional if you leave this line in current working directory is changed to the location of this script.

I have marked an area INIT this is where you place your standard PHP script code.

Remainder of the script (sections 1 to 5) creates a pop-up window allowing you to enter a name and password. You would continue processing your application in the Handler Function.

The above is a generic windows application where you would place any initialisation code in the INIT section and then create a windows application for further proceesing.

Alternative

Everyone has a different programming style and preferences. Perhaps you would like to place all your script code in the INIT section and create a pop-up Window function to capture name-password as required. This would returns the values entered and automatically close the window.


Top

Summary

  • Run the above, double click on Pro_demo_wb.bat
  • Code in the INIT section is executed (no code to execute)
  • This is followed by the Window creation code. Result on right

Note: You can convert the batch file into an executable

for details see Create an EXE file

 

Standard dialog functions are covered on the next page.

Top