<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Luca's forge &#187; php</title>
	<atom:link href="http://lucasforge.2bopen.org/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://lucasforge.2bopen.org</link>
	<description>Experiments from the land of open source</description>
	<lastBuildDate>Tue, 20 Jul 2010 15:24:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How enable the PHP Tidy extension for MAMP</title>
		<link>http://lucasforge.2bopen.org/2010/07/how-enable-the-php-tidy-extension-for-mamp/</link>
		<comments>http://lucasforge.2bopen.org/2010/07/how-enable-the-php-tidy-extension-for-mamp/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 15:21:14 +0000</pubDate>
		<dc:creator>Luca Corbo</dc:creator>
				<category><![CDATA[mac]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[mamp]]></category>
		<category><![CDATA[tidy]]></category>

		<guid isPermaLink="false">http://lucasforge.2bopen.org/?p=126</guid>
		<description><![CDATA[Download the PHP source in according with your environment: $ php -v PHP 5.2.11 (cli) (built: Dec 12 2009 13:19:08) Extract in a temporary folder $ cd ~ &#38;&#38; tar zxvf php-5.2.11.tar.gz &#38;&#38; cd php-5.2.11 Patch the ext/iconv/iconv.c file remove the lib on #define iconv libiconv so that the code reads like this: #ifdef HAVE_LIBICONV [...]]]></description>
			<content:encoded><![CDATA[<ol>
<li>Download the PHP source in according with your environment:
<pre>$ php -v
PHP 5.2.11 (cli) (built: Dec 12 2009 13:19:08)
</pre>
</li>
<li>Extract in a temporary folder
<pre>$ cd ~ &amp;&amp; tar zxvf php-5.2.11.tar.gz &amp;&amp; cd php-5.2.11
</pre>
</li>
<li>Patch the <code><em>ext/iconv/iconv.c</em> file </code>remove the <em><code>lib</code></em> on <em><code>#define iconv libiconv</code></em> so that the code reads like this:
<pre><code>#ifdef HAVE_LIBICONV
#define iconv iconv
#endif</code></pre>
</li>
<li>Patch the ext/tidy/tidy.c file moving the line 34: #include &#8220;tidy.h&#8221; to line 24 of tidy.c so that the code reads like this:
<pre>#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "tidy.h"
#include "php.h"
#include "php_tidy.h"

#if HAVE_TIDY

#include "php_ini.h"
</pre>
</li>
<li>Instruct the system to build universal binaries, that will work on both 32 and 64 bit systems by entering the following commands in the terminal console:
<pre>$ MACOSX_DEPLOYMENT_TARGET=10.6
$ CFLAGS="-arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp"
$ CCFLAGS="-arch i386 -arch x86_64 -g -Os -pipe"CXXFLAGS="-arch i386 -arch x86_64 -g -Os -pipe"
$ LDFLAGS="-arch i386 -arch x86_64 -bind_at_load"
$ export CFLAGS CXXFLAGS LDFLAGS CCFLAGS MACOSX_DEPLOYMENT_TARGET</pre>
</li>
<li>Configure &amp; make
<pre>$ LIBS=-lresolv ./configure --with-tidy=shared &amp;&amp; make</pre>
</li>
<li>Copy the module in MAMP
<pre>$ cp ./modules/tidy.so /Applications/MAMP/bin/php5/lib/php/extensions/no-debug-non-zts-20060613/</pre>
</li>
<li>Enable the module in the /Applications/MAMP/conf/php5/php.ini adding the following line in the extension section:
<pre>extension=tidy.so</pre>
</li>
<li>Restart the webserver</li>
<li>Check with php_info() if the tidy extension is loaded correctly <img src='http://lucasforge.2bopen.org/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </li>
</ol>
<p>Reference Links:</p>
<ul>
<li><a title="Installing PHP 5.2.10 on OS X 10.6 Snow Leopard" href="http://www.firewing1.com/node/31">Installing PHP 5.2.10 on OS X 10.6 Snow Leopard</a></li>
<li><a title="How to compile PHP with '--enable-zip' on Mac OS-X SnowLeopard (10.6)" href="http://symphony-cms.com/discuss/thread/35577/#position-14">How to compile PHP with &#8216;&#8211;enable-zip&#8217; on Mac OS-X SnowLeopard (10.6)</a></li>
<li><a href="http://forum.mamp.info/viewtopic.php?f=4&amp;t=6569">http://forum.mamp.info/viewtopic.php?f=4&amp;t=6569</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://lucasforge.2bopen.org/2010/07/how-enable-the-php-tidy-extension-for-mamp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ortro – Scp Transfer plugin v1.0.3 released</title>
		<link>http://lucasforge.2bopen.org/2009/07/ortro-%e2%80%93-scp-transfer-plugin-v1-0-3-released/</link>
		<comments>http://lucasforge.2bopen.org/2009/07/ortro-%e2%80%93-scp-transfer-plugin-v1-0-3-released/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 16:53:50 +0000</pubDate>
		<dc:creator>Luca Corbo</dc:creator>
				<category><![CDATA[ortro]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[scp]]></category>

		<guid isPermaLink="false">http://lucasforge.2bopen.org/?p=77</guid>
		<description><![CDATA[The Scp Transfer plugin allows Ortro to transfer files between remote hosts in a secure way. This release add the capability to use compression during transfer and enable the recursive copy of files and folders. Download and enjoy]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://dev.ortro.net/wiki/ortro/PluginFileScpTransfer">Scp Transfer</a> plugin allows <a href="http://www.ortro.net">Ortro</a> to transfer files between remote hosts in a secure way.</p>
<p>This release add the capability to use compression during transfer and enable the recursive copy of files and folders. <a href="http://dev.ortro.net/attachments/download/48/file_scp-1.0.3.tar.gz"><br />
Download</a> and enjoy <img src='http://lucasforge.2bopen.org/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://lucasforge.2bopen.org/2009/07/ortro-%e2%80%93-scp-transfer-plugin-v1-0-3-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ortro &#8211; Scp Transfer plugin v1.0.2 released</title>
		<link>http://lucasforge.2bopen.org/2009/07/ortro-scp-transfer-plugin-v1-0-2-released/</link>
		<comments>http://lucasforge.2bopen.org/2009/07/ortro-scp-transfer-plugin-v1-0-2-released/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 13:18:13 +0000</pubDate>
		<dc:creator>Luca Corbo</dc:creator>
				<category><![CDATA[ortro]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[scp]]></category>

		<guid isPermaLink="false">http://lucasforge.2bopen.org/?p=74</guid>
		<description><![CDATA[The Scp Transfer plugin allows Ortro to transfer files between remote hosts in a secure way. This release add the capability to filter the copy using whitelist or blacklist based on the host:path string and the saving of transfer time. Download and enjoy]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://dev.ortro.net/wiki/ortro/PluginFileScpTransfer">Scp Transfer</a> plugin allows <a href="http://www.ortro.net">Ortro</a> to transfer files between remote hosts in a secure way.</p>
<p>This release add the capability to filter the copy using whitelist or blacklist based on the host:path string and the saving of transfer time. <a href="http://dev.ortro.net/attachments/download/47/file_scp-1.0.2.tar.gz"><br />
Download</a> and enjoy <img src='http://lucasforge.2bopen.org/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://lucasforge.2bopen.org/2009/07/ortro-scp-transfer-plugin-v1-0-2-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Pear::Auth as Zend_Auth adapter</title>
		<link>http://lucasforge.2bopen.org/2009/07/using-pear-auth-as-zend-auth-adapter/</link>
		<comments>http://lucasforge.2bopen.org/2009/07/using-pear-auth-as-zend-auth-adapter/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 13:57:04 +0000</pubDate>
		<dc:creator>Luca Corbo</dc:creator>
				<category><![CDATA[pear]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[authentication]]></category>

		<guid isPermaLink="false">http://lucasforge.2bopen.org/?p=63</guid>
		<description><![CDATA[If you use both Pear and Zend framework you may want to extend the <a href="http://framework.zend.com/manual/en/zend.auth.html">Zend_Auth</a> adapters to use all the containers shipped with the <a href="http://pear.php.net/package/Auth">Pear::Auth</a> package.

Let's go to create the class implements the Zend_Auth_Adapter_Interface starting from the Zend_Auth_Adapter_Ldap class.]]></description>
			<content:encoded><![CDATA[<p>If you use both Pear and Zend framework you may want to extend the <a href="http://framework.zend.com/manual/en/zend.auth.html">Zend_Auth</a> adapters to use all the containers shipped with the <a href="http://pear.php.net/package/Auth">Pear::Auth</a> package.</p>
<p>Let&#8217;s go to create the class implements the Zend_Auth_Adapter_Interface starting from the Zend_Auth_Adapter_Ldap class.</p>
]]></content:encoded>
			<wfw:commentRss>http://lucasforge.2bopen.org/2009/07/using-pear-auth-as-zend-auth-adapter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Minerva (Ldap Password Changer) 0.5beta released</title>
		<link>http://lucasforge.2bopen.org/2009/07/minerva-ldap-password-changer-0-5beta-released/</link>
		<comments>http://lucasforge.2bopen.org/2009/07/minerva-ldap-password-changer-0-5beta-released/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 14:03:27 +0000</pubDate>
		<dc:creator>Luca Corbo</dc:creator>
				<category><![CDATA[Linux/Unix]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[ldap]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[utility]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://lucasforge.2bopen.org/?p=61</guid>
		<description><![CDATA[The first beta version of Minerva (Ldap Password Changer) at version 0.5 has been released! Minerva is a very little and simple PHP application with only one scope: permit at your openldap accounts to simply change its password without use other application (such as a webmail) that you can have or not. Download it and [...]]]></description>
			<content:encoded><![CDATA[<p>The first beta version of Minerva (Ldap Password Changer) at version 0.5 has been released!</p>
<p><a href="http://sourceforge.net/projects/ldapminerva/">Minerva</a> is a very little and simple PHP application with only one scope: permit at your openldap accounts to simply change its password without use other application (such as a webmail) that you can have or not.</p>
<p><a href="http://sourceforge.net/projects/ldapminerva/files/">Download</a> it and tell us what you think about it!</p>
]]></content:encoded>
			<wfw:commentRss>http://lucasforge.2bopen.org/2009/07/minerva-ldap-password-changer-0-5beta-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ReCaptcha with Zend Form</title>
		<link>http://lucasforge.2bopen.org/2009/04/recaptcha-with-zend-form/</link>
		<comments>http://lucasforge.2bopen.org/2009/04/recaptcha-with-zend-form/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 13:43:03 +0000</pubDate>
		<dc:creator>Luca Corbo</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[Form]]></category>
		<category><![CDATA[ReCaptcha]]></category>

		<guid isPermaLink="false">http://lucasforge.2bopen.org/?p=38</guid>
		<description><![CDATA[Requirements Zend Framework (1.7.8 at writing time) Valid ReCaptcha keys (public &#38; private) Simple ReCaptcha Zend Form Class source: application/forms/ReCaptcha.php &#60;?php class Form_ReCaptcha extends Zend_Form { public function init() { $this-&#62;setMethod(&#039;post&#039;); //Add your elements here... $recaptcha = new Zend_Service_ReCaptcha($publickey, $privatekey); $captcha = new Zend_Form_Element_Captcha(&#039;challenge&#039;, array(&#039;captcha&#039; =&#62; &#039;ReCaptcha&#039;, &#039;captchaOptions&#039; =&#62; array(&#039;captcha&#039; =&#62; &#039;ReCaptcha&#039;, &#039;service&#039; =&#62; $recaptcha))); [...]]]></description>
			<content:encoded><![CDATA[<h3>Requirements</h3>
<ul>
<li><a href="http://framework.zend.com/download/" target="_blank">Zend Framework</a> (1.7.8 at writing time)</li>
<li>Valid <a href="http://recaptcha.net/" target="_blank">ReCaptcha</a> keys (public &amp; private)</li>
</ul>
<h3>Simple ReCaptcha</h3>
<h4>Zend Form Class</h4>
<p>source: <em>application/forms/ReCaptcha.php</em></p>
<pre class="brush: php">
&lt;?php
class Form_ReCaptcha extends Zend_Form
{
    public function init()
    {
        $this-&gt;setMethod(&#039;post&#039;);
        //Add your elements here...

        $recaptcha = new Zend_Service_ReCaptcha($publickey, $privatekey);

        $captcha = new Zend_Form_Element_Captcha(&#039;challenge&#039;,
              array(&#039;captcha&#039;        =&gt; &#039;ReCaptcha&#039;,
                    &#039;captchaOptions&#039; =&gt; array(&#039;captcha&#039; =&gt; &#039;ReCaptcha&#039;, &#039;service&#039; =&gt; $recaptcha)));

        $this-&gt;addElement($captcha);

        // Add the submit button
        $this-&gt;addElement(&#039;submit&#039;, &#039;submit&#039;, array(&#039;label&#039; =&gt; &#039;Submit&#039;));
    }
}
?&gt;
</pre>
<h4>Zend Controller Class</h4>
<p>source: <em>application/controller/ReCaptchaController.php</em></p>
<pre class="brush: php">
&lt;?php
class ReCaptchaController extends Zend_Controller_Action
{
    public function indexAction()
    {
        require_once APPLICATION_PATH . &#039;/forms/Contact.php&#039;;

        $form = new Form_ReCaptcha();

        if ($this-&gt;_request-&gt;isPost()) {
            $formData = $this-&gt;_request-&gt;getPost();
            if ($form-&gt;isValid($formData)) {
                $recaptcha = new Zend_Service_ReCaptcha($publickey, $privatekey);

                $result = $recaptcha-&gt;verify($this-&gt;_getParam(&#039;recaptcha_challenge_field&#039;),
                                             $this-&gt;_getParam(&#039;recaptcha_response_field&#039;));
                if (!$result-&gt;isValid()) {
                    //ReCaptcha validation error
                    //Your action here...
               }
            }
        }
        $this-&gt;view-&gt;form = $form;
    }
}
?&gt;
</pre>
<h3>Customized ReCaptcha</h3>
<p>You may also want to internationalizing or change colors to ReCaptcha, to do it you need to specify some options for the  Zend_Service_ReCaptcha object.<br />
See the <a href="http://wiki.recaptcha.net/index.php/Main_Page" target="_blank">ReCaptcha wiki</a> for a complete list of available options. </p>
<h4>Zend Form Class</h4>
<p>source: <em>application/forms/ReCaptcha.php</em></p>
<pre class="brush: php">
&lt;?php
class Form_ReCaptcha extends Zend_Form
{
    public function init()
    {
        $this-&gt;setMethod(&#039;post&#039;);
        //Add your elements here...
        $recaptcha = new Zend_Service_ReCaptcha($publickey, $privatekey);

        //Translate in your language
        $recaptcha_it_translation =
            array(&#039;visual_challenge&#039; =&gt; &quot;Verifica video&quot;,
                  &#039;audio_challenge&#039; =&gt; &quot;Verifica audio&quot;,
                  &#039;refresh_btn&#039; =&gt; &quot;Effettua una nuova verifica&quot;,
                  &#039;instructions_visual&#039; =&gt; &quot;Scrivi le due parole&quot;,
                  &#039;instructions_audio&#039; =&gt; &quot;Scrivi quello che ascolti&quot;,
                  &#039;help_btn&#039; =&gt; &quot;Aiuto&quot;,
                  &#039;play_again&#039; =&gt; &quot;Riascolto di nuovo l&#039;audio&quot;,
                  &#039;cant_hear_this&#039; =&gt; &quot;Scarica l&#039;audio come MP3&quot;,
                  &#039;incorrect_try_again&#039; =&gt; &quot;Incorretto. Prova ancora.&quot;);

        $recaptcha-&gt;setOption(&#039;custom_translations&#039;, $recaptcha_it_translation);
        //Change theme
        $recaptcha-&gt;setOption(&#039;theme&#039;, &#039;clean&#039;);

        $captcha = new Zend_Form_Element_Captcha(&#039;challenge&#039;,
              array(&#039;captcha&#039;        =&gt; &#039;ReCaptcha&#039;,
                    &#039;captchaOptions&#039; =&gt; array(&#039;captcha&#039; =&gt; &#039;ReCaptcha&#039;,
                                             &#039;service&#039; =&gt; $recaptcha)));

        $this-&gt;addElement($captcha);

        // Add the submit button
        $this-&gt;addElement(&#039;submit&#039;, &#039;submit&#039;, array(&#039;label&#039; =&gt; &#039;Submit&#039;));
    }
}
?&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://lucasforge.2bopen.org/2009/04/recaptcha-with-zend-form/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Discovery hosts and services with PHP and Nmap</title>
		<link>http://lucasforge.2bopen.org/2008/06/discovery-hosts-and-services-with-php-and-nmap/</link>
		<comments>http://lucasforge.2bopen.org/2008/06/discovery-hosts-and-services-with-php-and-nmap/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 21:39:38 +0000</pubDate>
		<dc:creator>Luca Corbo</dc:creator>
				<category><![CDATA[Net_Nmap]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[nmap]]></category>
		<category><![CDATA[pear]]></category>

		<guid isPermaLink="false">http://lucasforge.2bopen.org/?p=8</guid>
		<description><![CDATA[Requirements: - PHP5 - PEAR::Net_Nmap - nmap &#60;?php /** * Scan network to retrieve hosts and services information. */ require_once &#039;Net/Nmap.php&#039;; //Define the target to scan $target = array(&#039;127.0.0.1&#039;,&#039;www.yourserver.com&#039;); $options = array(&#039;nmap_binary&#039; =&#62; &#039;/usr/local/bin/nmap&#039;); try { $nmap = new Net_Nmap($options); //Enable nmap options $nmap_options = array(&#039;os_detection&#039; =&#62; true, &#039;service_info&#039; =&#62; true, &#039;port_ranges&#039; =&#62; &#039;U:53,111,137,T:21-25,80,139,8080&#039;,//to scan [...]]]></description>
			<content:encoded><![CDATA[<p>Requirements:</p>
<p>- <a title="PHP.net" href="http://www.php.net" target="_blank">PHP5</a><br />
- <a title="Net_Nmap" href="http://pear.php.net/package/Net_Nmap" target="_self">PEAR::Net_Nmap</a><br />
- <a title="nmap" href="http://nmap.org/" target="_blank">nmap</a></p>
<pre class="brush: php">
&lt;?php

/**
 * Scan network to retrieve hosts and services information.
 */

require_once &#039;Net/Nmap.php&#039;;

//Define the target to scan
$target = array(&#039;127.0.0.1&#039;,&#039;www.yourserver.com&#039;);

$options = array(&#039;nmap_binary&#039; =&gt; &#039;/usr/local/bin/nmap&#039;);

try {
    $nmap = new Net_Nmap($options);

    //Enable nmap options
    $nmap_options = array(&#039;os_detection&#039; =&gt; true,
                          &#039;service_info&#039; =&gt; true,
                          &#039;port_ranges&#039; =&gt; &#039;U:53,111,137,T:21-25,80,139,8080&#039;,//to scan only specified ports
                          );

    $nmap-&gt;enableOptions($nmap_options);

    //Scan target
    $res = $nmap-&gt;scan($target);

    //Get failed hosts
    $failed_to_resolve = $nmap-&gt;getFailedToResolveHosts();

    if (count($failed_to_resolve) &gt; 0) {
        echo &#039;Failed to resolve given hostname/IP: &#039; .
             implode (&#039;, &#039;, $failed_to_resolve) .
             &quot;\n&quot;;
    }

    //Parse XML Output to retrieve Hosts Object
    $hosts = $nmap-&gt;parseXMLOutput();

    //Print results
    foreach ($hosts as $key =&gt; $host) {
        echo &#039;Hostname: &#039; . $host-&gt;getHostname() . &quot;\n&quot;;
        echo &#039;Address: &#039; . $host-&gt;getAddress() . &quot;\n&quot;;
        echo &#039;OS: &#039; . $host-&gt;getOS() . &quot;\n&quot;;
        echo &#039;Status: &#039; . $host-&gt;getStatus . &quot;\n&quot;;
        $services = $host-&gt;getServices();
        echo &#039;Number of discovered services: &#039; . count($services) . &quot;\n&quot;;
        foreach ($services as $key =&gt; $service) {
            echo &quot;\n&quot;;
            echo &#039;Service Name: &#039; . $service-&gt;name . &quot;\n&quot;;
            echo &#039;Port: &#039; . $service-&gt;port . &quot;\n&quot;;
            echo &#039;Protocol: &#039; . $service-&gt;protocol . &quot;\n&quot;;
            echo &#039;Product information: &#039; . $service-&gt;product . &quot;\n&quot;;
            echo &#039;Product version: &#039; . $service-&gt;version . &quot;\n&quot;;
            echo &#039;Product additional info: &#039; . $service-&gt;extrainfo . &quot;\n&quot;;
        }
    }
} catch (Net_Nmap_Exception $ne) {
    echo $ne-&gt;getMessage();
}
?&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://lucasforge.2bopen.org/2008/06/discovery-hosts-and-services-with-php-and-nmap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Execute SQL script from a file using PHP</title>
		<link>http://lucasforge.2bopen.org/2008/06/execute-sql-script-from-a-file-using-php/</link>
		<comments>http://lucasforge.2bopen.org/2008/06/execute-sql-script-from-a-file-using-php/#comments</comments>
		<pubDate>Wed, 04 Jun 2008 17:45:39 +0000</pubDate>
		<dc:creator>Luca Corbo</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://lucasforge.2bopen.org/?p=7</guid>
		<description><![CDATA[The code below allows to retrieve and execute all SQL statements defined in a SQL script file removing all comments. &#60;?php $sql_file = &#039;test.sql&#039;; $contents = file_get_contents($sql_file); // Remove C style and inline comments $comment_patterns = array(&#039;/\/\*.*(\n)*.*(\*\/)?/&#039;, //C comments &#039;/\s*--.*\n/&#039;, //inline comments start with -- &#039;/\s*#.*\n/&#039;, //inline comments start with # ); $contents = preg_replace($comment_patterns, [...]]]></description>
			<content:encoded><![CDATA[<p>The code below allows to retrieve and execute all SQL statements defined in a SQL script file removing all comments.</p>
<pre class="brush: php">
&lt;?php
$sql_file = &#039;test.sql&#039;;

$contents = file_get_contents($sql_file);

// Remove C style and inline comments
$comment_patterns = array(&#039;/\/\*.*(\n)*.*(\*\/)?/&#039;, //C comments
                          &#039;/\s*--.*\n/&#039;, //inline comments start with --
                          &#039;/\s*#.*\n/&#039;, //inline comments start with #
                          );
$contents = preg_replace($comment_patterns, &quot;\n&quot;, $contents);

//Retrieve sql statements
$statements = explode(&quot;;\n&quot;, $contents);
$statements = preg_replace(&quot;/\s/&quot;, &#039; &#039;, $statements);

require_once &#039;MDB2.php&#039;;

$mdb2 =&amp; MDB2::connect(&#039;mysql://usr:pw@localhost/dbnam&#039;);

foreach ($statements as $query) {
    if (trim($query) != &#039;&#039;) {
        echo &#039;Executing query: &#039; . $query . &quot;\n&quot;;
        $res = $mdb2-&gt;exec($query);

        if (PEAR::isError($res)) {
            die($res-&gt;getMessage());
        }
    }
}
?&gt;
</pre>
<p>I have used <a href="http://pear.php.net/package/MDB2">Pear::MDB2</a> abstraction layer to interact with the database but the code above should work with any other db abstraction layer or PHP built-in functions.</p>
]]></content:encoded>
			<wfw:commentRss>http://lucasforge.2bopen.org/2008/06/execute-sql-script-from-a-file-using-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
