PhpBrew

Share your advanced PureBasic knowledge/code with the community.
User avatar
griz
Enthusiast
Enthusiast
Posts: 167
Joined: Sun Jun 29, 2003 7:32 pm
Location: Canada

PhpBrew

Post by griz »

Code updated for 5.20+

Hi guys

For PHP programmers, this may be of some intrest to you. This simple Pure Basic code creates an EXE which you can drag and drop PHP files to. They will then be processed and the results automatically sent to your web browser. Optionally, if "index.php" is found in the same directory as the EXE, it is automatically launched when the EXE starts.

For Pure Basic beginners, it shows how to run other programs and access the command line. Of course you will need PHP installed on your system and can get it here if you don't already have it : http://php.net/

Code: Select all

; ----------------------------------------
; | PhpBrew version 0.3 Feb 2005 by Griz |
; ----------------------------------------
; | Initialize globals.                  |
; ----------------------------------------
Global php$
Global browser$
Global outfile$      
Global fname$  
Global extension$     
; ----------------------------------------
; | WinAPI. Returns directory of EXE.    |
; ----------------------------------------
Procedure.s ExePath() 
  ExePath$ = Space(1000) 
  GetModuleFileName_(0,@ExePath$,1000) 
  ProcedureReturn GetPathPart(ExePath$) 
EndProcedure 
; ----------------------------------------
; | Read settings from "prefs.txt" if    |
; | the file exists, otherwise specify   |
; | your settings here before compiling. |
; ----------------------------------------
Procedure readprefs()
  If OpenPreferences(ExePath()+"prefs.txt")
    php$ = ReadPreferenceString("php", "c:\php\php-cgi.exe")
    browser$ = ReadPreferenceString("browser", "C:\Program Files\Mozilla Firefox\firefox.exe")
    extension$ = ReadPreferenceString("fileext", "htm")
    ClosePreferences()
  Else
    MessageRequester("Error: No prefs.txt","php=? and browser=?",#PB_MessageRequester_Ok)
    End

  EndIf
EndProcedure
; ----------------------------------------
; | Change filename's extension.         |
; ----------------------------------------
Procedure.s NameOutputFile(sourcefile$)
  output$=""
  pc$ = GetFilePart(sourcefile$)
  l=FindString(pc$,".",1)
  If l>0
    output$=Mid(pc$,1,l-1)
  Else
    output$=pc$
  EndIf
  output$+"."+extension$
  ProcedureReturn GetPathPart(sourcefile$)+output$
EndProcedure
; ----------------------------------------
; | MAIN PROGRAM                         |
; ----------------------------------------
; | Initialize variables                 |
; ----------------------------------------
Readprefs()
fname$ = ProgramParameter()
; ----------------------------------------
; | If no file specified by command line | 
; | look for presence of "index.php"and  |
; | use if found, otherwise end program. |
; ----------------------------------------
If fname$ = ""
  fname$ = exepath() + "index.php"
  If FileSize(fname$)=-1
    MessageRequester("Error: No file specified","Drag and drop a PHP file or place 'index.php' in same directory.",#PB_MessageRequester_Ok)
    End
  EndIf
EndIf
; ----------------------------------------
; | Outfile$ will be named the same as   |
; | the source PHP file, but with a      |
; | different extension. For example,    |
; | "C:\Hello.php" -> "C:\Hello.Htm".    |
; ----------------------------------------
outfile$ = NameOutputFile(fname$)
; ----------------------------------------
; | Launch PHP and generate output file  |
; | as specified above.                  |
; ----------------------------------------
If RunProgram("cmd.exe","/c "+php$+" -q "+Chr(34)+fname$+Chr(34)+" >"+Chr(34)+outfile$+Chr(34),"",1|2) = 0
  MessageRequester("Error","Could not launch PHP - check prefs.txt file settings.",#PB_MessageRequester_Ok)
  End
EndIf
; ----------------------------------------
; | Launch web browser and pass the file |
; | (outfile$) as a parameter, in order  |
; | to view the generated results.       |
; ----------------------------------------
If RunProgram(browser$, Chr(34)+outfile$+Chr(34), "") = 0
  MessageRequester("Error","Could not launch Web Browser : "+browser$,#PB_MessageRequester_Ok)
EndIf
End
Here are some php samples you can use for testing :

hello.php

Code: Select all

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?>
</body>
</html>
phpinfo.php

Code: Select all

<html>
 <head>
  <title>PHP Info</title>
 </head>
 <body>
<? phpinfo() ?>
</body>
</html>
sqlitetest.php - requires PHP5 with Sqlite. Isn't it great that Pure Basic and PHP can both access SQLite!?

Code: Select all

<?php

//create table
if(file_exists("test.db")) {
	$weNeedToCreateTheDatabase = false;
} else {
	$weNeedToCreateTheDatabase = true;
	
}

//create or open database
$db = sqlite_open("test.db") or die("failed to open/create the database");

//create table
if($weNeedToCreateTheDatabase) {
	sqlite_query($db, "CREATE TABLE Members(FirstName,LastName)");
}

//add info
sqlite_query($db,"INSERT INTO Members VALUES ('Pure', 'Basic')");

//get info
$dt = sqlite_query($db, "SELECT * FROM Members");
while ($row = sqlite_fetch_array($dt)) {
echo "row: $row[FirstName] $row[LastName]<br/>";
}

//close database
sqlite_close($db);
?>
Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Post by Beach »

This is really cool, thanks for sharing.
-Beach
User avatar
griz
Enthusiast
Enthusiast
Posts: 167
Joined: Sun Jun 29, 2003 7:32 pm
Location: Canada

Post by griz »

This is really cool, thanks for sharing.
No problem, thank you Beach.
Post Reply