[libgda] Web provider: PHP scripts and documentation improvements



commit d2a80f33204189570e6ae0c246b943ee5294f06b
Author: Vivien Malerba <malerba gnome-db org>
Date:   Sun Aug 21 18:45:13 2011 +0200

    Web provider: PHP scripts and documentation improvements
    
    - define connections in gda-secure-config.php file
    - possibily enable logging
    - gda-tester.php now outputs HTML

 providers/web/Makefile.am               |    1 +
 providers/web/README                    |    7 ++++
 providers/web/php/README                |   40 ++++++++++++++++++++------
 providers/web/php/gda-config.php        |   40 +++++++++++++++-----------
 providers/web/php/gda-front.php         |   10 ++++++-
 providers/web/php/gda-secure-config.php |   33 +++++++++++++++++++++
 providers/web/php/gda-tester.php        |   38 +++++++++++++++++-------
 providers/web/php/gda-utils.php         |   47 ++++++++++++++++++++++++++++++-
 providers/web/php/gda-worker.php        |    4 ++
 9 files changed, 181 insertions(+), 39 deletions(-)
---
diff --git a/providers/web/Makefile.am b/providers/web/Makefile.am
index 932cb45..8de002d 100644
--- a/providers/web/Makefile.am
+++ b/providers/web/Makefile.am
@@ -54,6 +54,7 @@ php_DATA = \
 	php/README \
 	php/gda-clean.php \
 	php/gda-config.php \
+	php/gda-secure-config.php \
 	php/gda-exception.php \
 	php/gda-front.php \
 	php/gda-meta.php \
diff --git a/providers/web/README b/providers/web/README
index 86d6437..8c9935b 100644
--- a/providers/web/README
+++ b/providers/web/README
@@ -61,3 +61,10 @@ for:
 * the password MyPass1 (note that the gda-sql command requires a username when a password is specified which is
   the reason for the dummy "none" user name here to avoid any user name prompting, or use the -p command line option
   otherwise)
+
+
+Installation
+------------
+
+See php/README for more information.
+
diff --git a/providers/web/php/README b/providers/web/php/README
index c3979fd..b802f6b 100644
--- a/providers/web/php/README
+++ b/providers/web/php/README
@@ -15,21 +15,43 @@ for which the current implementation is provided under the PEAR_MDB2/
 directory.
 
 Once the scripts are installed, some configuration needs to be done
-in the gda-config.php script.
+in the gda-config.php and gda-secure-config.php files:
+- the gda-config.php defines where the PEAR MDB2 extension should be
+  available, and where the gda-secure-config.php can be found.
+- the gda-secure-config.php defines the actual connection(s) available
+  through the Web interface. For security reasons this file should be moved
+  to a location outside the Web server's PHP scripts (such as /etc)
 
-Note that for better security, this script should not be accessible
-from the outside (ie it should be put outside ot the web server's
-root directory, and the gda-front.php and gda-worker.php scripts should be
-modified to point to its real location).
+Web server setup
+----------------
+You need a functionnal web server (tested with Apache and Lighthttpd but should
+work fine with others) whih can interpret PHP scripts.
 
+For example using Apache, you can either use an existing virtual host or create a
+one. In any case all the PHP scripts (except the gda-secure-config.php) should be
+copied in a single directory (this directory, relative to the DOCUMENT ROOT directory
+should then specified as the PATH argument in a Libgda's connection string).
 
-Note about gda-tester.php
--------------------------
+Connections setup
+-----------------
+Edit the gda-config.php file to:
+- define where the PEAR MDB2 extension is located
+- define where the gda-secure-config.php file is located
+
+Move the gda-secure-config.php to a directory outside the DOCUMENT ROOT directory and
+edit it to define connections available through the Web proxy.
+
+Setup testing
+-------------
 The gda-tester.php script can be executed to diagnose any setup problem,
 it reports errors if the MDB2 PEAR of SimpleXML extensions are missing,
 and, for each configured connection, tests if it can be opened.
 
-Note that by default the gda-tester.php will _not_ display any
-configured connection information, this needs to be enabled in the
+To use this scrip, simply point your favorite web browser to the URL:
+http://<your server>/path/to/script/gda-tester.php
+
+
+By default the gda-tester.php will _not_ test configured connection,
+this needs to be enabled in the
 script's source. Better yet is to remove that file once not necessary
 anymore as it could leak useful information.
diff --git a/providers/web/php/gda-config.php b/providers/web/php/gda-config.php
index 8acd867..cbd2c94 100644
--- a/providers/web/php/gda-config.php
+++ b/providers/web/php/gda-config.php
@@ -1,28 +1,34 @@
 <?php
 
 /*
- * initial shared secret: will have to be passed as the SECRET argument when opening
- * the connection from Libgda
+ * Add this directive to use the implementation of PEAR MDB2 provided
+ * in Libgda
  */
-$init_shared = "MySecret";
+//set_include_path (get_include_path().":PEAR_MDB2");
 
 /*
- * declared connections: for each connection which can be opened by Libgda, the
- * the connection's password and the real connection's DSN need to be added respectively
- * to the $cnc and $dsn arrays, using the connection name as a key. The connection name
- * and password have no significance outside of the Libgda's context and be arbitrary.
- * However the real connection's DSN need to be valid for the PEAR's MDB2 module, as
- * per http://pear.php.net/manual/en/package.database.mdb2.intro-dsn.php
- *
+ * Add the directory to which the gda-secure-config.php has been copied (here /etc for example)
  */
+set_include_path (get_include_path().":/etc");
 
-/* sample connection cnc1 */
-$cnc["cnc1"] = "MyPass1";
-//$dsn["cnc1"] = "pgsql://vivien unix(/var/run/postgresql)/sales";
-$dsn["cnc1"] = "pgsql://unix(/tmp)/sales";
 
-/* sample connection cnc2 */
-$cnc["cnc2"] = "MyPass2";
-$dsn["cnc2"] = "mysql://user unix(/path/to/socket)/sales";
+
+
+
+/*
+ * NO MODIFICATION BELOW
+ */
+$found = false;
+$array = explode (":", get_include_path ());
+foreach ($array as $index => $path) {
+	if (file_exists ($path.DIRECTORY_SEPARATOR."gda-secure-config.php")) {
+		include_once ($path.DIRECTORY_SEPARATOR."gda-secure-config.php");
+		$found = true;
+	}
+}
+if (! $found) {
+	echo "Missing gda-secure-config.php file!";
+	exit (1);
+}
 
 ?>
diff --git a/providers/web/php/gda-front.php b/providers/web/php/gda-front.php
index 1de3f76..28070fd 100644
--- a/providers/web/php/gda-front.php
+++ b/providers/web/php/gda-front.php
@@ -28,6 +28,9 @@ try {
 	if (file_put_contents ($datafile, $text) == false)
 		throw new Exception ("Can't create command file ".$cmdfile);
 	
+	if (isset ($log))
+		$log->lwrite ("COMMAND: [$text]");
+
 	$file = fopen ($cmdfile, "w");
 	if (fwrite ($file, $datafile) == false) // block until there is a reader
 		throw new Exception ("Can't send command");
@@ -48,8 +51,13 @@ try {
 
 	$file = fopen ($datafile, 'rb');
 	fpassthru ($file);
+	if (isset ($log)) {
+		$tmp = file_get_contents ($datafile);
+		$log->lwrite ("RESPONSE: [$tmp]");
+	}
+
 	@unlink ($datafile);
-	
+
 	echo $text;
 }
 catch (Exception $e) {
diff --git a/providers/web/php/gda-secure-config.php b/providers/web/php/gda-secure-config.php
new file mode 100644
index 0000000..29cc8cc
--- /dev/null
+++ b/providers/web/php/gda-secure-config.php
@@ -0,0 +1,33 @@
+<?php
+/*
+ * This file should be put outside the server's ROOT directory to avoid
+ * malicious accessing, for example in /etc.
+ *
+ * It is read only by the gda-config.php script.
+ */
+
+/*
+ * initial shared secret: will have to be passed as the SECRET argument when opening
+ * the connection from Libgda
+ */
+$init_shared = "MySecret";
+
+/*
+ * declared connections: for each connection which can be opened by Libgda, the
+ * the connection's password and the real connection's DSN need to be added respectively
+ * to the $cnc and $dsn arrays, using the connection name as a key. The connection name
+ * and password have no significance outside of the Libgda's context and be arbitrary.
+ * However the real connection's DSN need to be valid for the PEAR's MDB2 module, as
+ * per http://pear.php.net/manual/en/package.database.mdb2.intro-dsn.php
+ *
+ */
+
+/* sample connection cnc1 */
+$cnc["cnc1"] = "MyPass1";
+$dsn["cnc1"] = "pgsql://gdauser:GdaUser 127 0 0 1/test";
+
+/* sample connection cnc2 */
+$cnc["cnc2"] = "MyPass2";
+$dsn["cnc2"] = "mysql://gdauser:GdaUser unix(/var/run/mysqld/mysqld.sock)/test1";
+
+?>
diff --git a/providers/web/php/gda-tester.php b/providers/web/php/gda-tester.php
index 7303087..1c22e56 100644
--- a/providers/web/php/gda-tester.php
+++ b/providers/web/php/gda-tester.php
@@ -1,6 +1,7 @@
 <?php
 session_cache_limiter('nocache');
-header('Content-type: text/plain; charset=UTF-8');
+header('Content-type: text/html; charset=UTF-8');
+header('Cache-Control: public, no-cache');
 
 include_once "gda-utils.php";
 include_once "gda-exception.php";
@@ -8,43 +9,58 @@ include_once "gda-config.php";
 
 $test_connections = false; // set to true to enable each connection testing
 
-echo "Gda connections tester\n----------------------\n\n";
+echo "<h1>Gda connections tester</h1>\n\n";
 
 if (! try_include ("MDB2.php", true)) {
-	echo "ERROR: The PEAR MDB2 extension is required\n";
+	echo "<b>ERROR:</b> The PEAR MDB2 extension is required\n";
+	echo "<p>If you want to use the implementation provided in Libgda, then add a directive as following to the <tt>gda-config.php</tt> file:</p>";
+	echo "<p><tt>set_include_path (get_include_path().\":PEAR_MDB2\");</tt></p>";
+	echo "<p>Note that you still need to install the PEAR package (named 'php-pear' on a Debian system).</p>";
 	exit (1);
 }
 
 if (! extension_loaded ("SimpleXML")) {
-	echo "ERROR: The SimpleXML extension is required\n";
+	echo "<b>ERROR</b>: The SimpleXML extension is required\n";
+	echo "<p>You need to configure PHP to include this extension.</p>";
 	exit (1);
 }
 
 function handle_pear_error ($res)
 {
 	if (PEAR::isError($res)) {
-		$cause = "\tStandard Message [".$res->getMessage()."]\n". 
-			"\tUser Information [".$res->getUserInfo()."]\n".
-			"\tDebug Information [".$res->getDebugInfo()."]";
+		$cause = "<p><b>Standard Message:</b> ".$res->getMessage()."</p>\n". 
+			"<p><b>User Information:</b> ".$res->getUserInfo()."</p>\n".
+			"<p><b>Debug Information:</b> ".$res->getDebugInfo()."</p>";
 		throw new GdaException($cause, false);
 	}
 }
 
 echo "\n";
+
+if (! isset ($cnc, $dsn)) {
+	echo "<p><b>ERROR:</b> No connection defined!</p>\n";
+	exit (1);
+}
+
 if ($test_connections) {
 	foreach ($cnc as $dbname => $dbpass) {
-		echo "Connection ".$dbname;
+		echo "<h2>Connection '".$dbname."'</h2>";
 		try {
 			$mdb2 = MDB2::connect($dsn[$dbname]);
 			handle_pear_error ($mdb2);
-			echo " ==> OK\n";
+			echo "OK.\n";
 		}
 		catch (GdaException $e) {
-			echo " ==> FAILED:\n".$e->getMessage()."\n";
+			echo "FAILED!\n".$e->getMessage()."\n";
 		}
 	}
 }
 else {
-	echo "Connections are not tested, set \$test_connections to true to enable connection testing\n";
+	echo "<p>Connections listed below but not tested (set <tt>\$test_connections</tt> to <tt>true</tt> in the <tt>gda-config.php</tt> file to change):</p>\n";
+	echo "<ul>\n";
+	foreach ($cnc as $dbname => $dbpass) {
+		echo "<li>Connection '".$dbname."'</li>\n";
+	}
+	echo "</ul>\n";
 }
 ?>
diff --git a/providers/web/php/gda-utils.php b/providers/web/php/gda-utils.php
index 212b27d..20bee78 100644
--- a/providers/web/php/gda-utils.php
+++ b/providers/web/php/gda-utils.php
@@ -19,7 +19,7 @@ function try_include ($file, $debug=false)
 		if (file_exists ($path.DIRECTORY_SEPARATOR.$file)) {
 			include_once ($path.DIRECTORY_SEPARATOR.$file);
 			if ($debug)
-				echo "Using: ".$path.DIRECTORY_SEPARATOR.$file."\n";
+				echo "<b>Using PEAR MDB2:</b> ".$path.DIRECTORY_SEPARATOR.$file."\n";
 			return true;
 		}
 	}
@@ -104,4 +104,49 @@ function mdb2_type_to_gtype ($mdb2type)
 	}
 }
 
+/**
+ * Logging class:
+ * - contains lfile, lopen and lwrite methods
+ * - lfile sets path and name of log file
+ * - lwrite will write message to the log file
+ * - first call of the lwrite will open log file implicitly
+ * - message is written with the following format: hh:mm:ss (script name) message
+ */
+class Logging{
+    // define default log file
+    private $log_file = '/tmp/gdalog';
+    // define file pointer
+    private $fp = null;
+    // set log file (path and name)
+    public function lfile($path) {
+        $this->log_file = $path;
+    }
+    // write message to the log file
+    public function lwrite($message){
+        // if file pointer doesn't exist, then open log file
+        if (!$this->fp) $this->lopen();
+        // define script name
+        $script_name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
+        // define current time
+        $time = date('H:i:s');
+        // write current time, script name and message to the log file
+        fwrite($this->fp, "$time ($script_name) $message\n");
+    }
+    // open log file
+    private function lopen(){
+        // define log file path and name
+        $lfile = $this->log_file;
+        // define the current date (it will be appended to the log file name)
+        $today = date('Y-m-d');
+        // open log file for writing only; place the file pointer at the end of the file
+        // if the file does not exist, attempt to create it
+        $this->fp = fopen($lfile . '_' . $today, 'a') or exit("Can't open $lfile!");
+    }
+}
+
+/*
+ * Define this variable to enable logging
+ */
+//$log = new Logging();
+
 ?>
\ No newline at end of file
diff --git a/providers/web/php/gda-worker.php b/providers/web/php/gda-worker.php
index 4e96dd7..75a02e5 100644
--- a/providers/web/php/gda-worker.php
+++ b/providers/web/php/gda-worker.php
@@ -46,11 +46,15 @@ else {
 		$_SESSION ["counter"] ++;
 	else
 		$_SESSION ["counter"] = 1;
+	if (isset ($log))
+		$log->lwrite ("WORKER started");
 	gda_worker_main ();
 }
 
 function shutdown ()
 {
+	if (isset ($log))
+		$log->lwrite ("SHUTDOWN");
 	session_write_close ();
 	flush ();
 	ob_flush();



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]