tag:blogger.com,1999:blog-10008696198669573012024-02-08T20:23:05.211+00:00Rob's Area for PHP on WindowsProgramming used to be fun. Thanks to PHP's support for Windows builds, you can run very efficient web sites on Windows and have a good time making them. This blog is a small collection of the additional things I've needed to help me build a few sites.Robhttp://www.blogger.com/profile/06996218238493777139noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-1000869619866957301.post-43317691255491644722014-06-06T14:40:00.001+01:002014-08-30T04:14:16.054+01:00PHP 5.5 Builds<h2>
<span style="font-size: large;">
PHP 5.5</span></h2>
<div>
A little bit late but I've updated to PHP 5.5. There are a few improvements that I like, and a great opcache extension, all covered in detail here so no need to go on about it: <a href="http://www.php.net/manual/migration55.new-features.php" target="_blank">http://www.php.net/manual/migration55.new-features.php</a>.</div>
<h2>
<span style="font-size: large;"><br />New PHP extension builds</span></h2>
New builds of php_sqlsrv and php_spidermonkey are available including PHP 5.5 supporting 32 and 64 bits (64-bit builds are in the x64 folder of the zip file). <br />
<br />
<a name='more'></a><br />
<br />
<h4>
<a href="https://onedrive.live.com/redir?resid=669EE24817961774%21685" target="_blank">Download all_php_builds.zip</a> - new builds of various 3rd party extensions, updated 19 June 2014:</h4>
<ul>
<li>Microsoft SQL Server Driver for PHP 5.3, 5.4 and 5.5 (php_sqlsrv and php_pdo_sqlsrv). 64 bit drivers are in the <b>x64</b> folder of the zip file. <b><i>This is version 3.0.2.2 built 19 June 2014, a quick rebuild to fix a bug I missed a few days ago, reported by Marius Serban - many thanks Marius.</i></b></li>
<li>php_spidermonkey for PHP 5.3, 5.4 and 5.5. This extension needs updating to work with Mozilla's newest version of Spidermonkey. PHP 5.5 64-bit build is included in the x64 folder of the zip file.</li>
</ul>
<h3>
<br />Notes </h3>
I have rebuilt all the extensions I use on Windows and put them all in
one big zip file. Some notes on sqlsrv are below. I have not tested
all versions, just rebuilt. There is a 64-bit build of Spidermonkey in the x64 folder of the zip file as of 19 June 2014. Everything is built on either the VC9 (PHP 5.3, 5.4) or VC11 (PHP 5.5) compiler.<br />
<h4>
<br />Sqlsrv changes</h4>
<div>
Update 30 Aug 2014: sqlsrv for PHP 5.6 is included on this page: <a href="http://robsphp.blogspot.co.uk/2012/06/unofficial-microsoft-sql-server-driver.html">Unofficial Microsoft SQL Server Driver for PHP</a></div>
<div>
<br /></div>
The sqlsrv driver has a fix to a bug found by Charles Durrant (<a href="http://sqlsrvphp.codeplex.com/workitem/22427">http://sqlsrvphp.codeplex.com/workitem/22427</a>) which I too recently experienced when running PHP-32 on a 64 bit server. I noted Charles's post when he first reported the bug, but tried my best to avoid working on the source code until I had to... Charles saved me a huge amount of time, so thank you to him. My fix is different, I have repaired all of Microsoft's dodgy auto pointers and completely rewritten the horrible function that caused the crash. If anyone uses these new builds and spots memory leaks or bugs due to these changes, please let me know and I'll fix them within a day or two.<br />
<h2>
<span style="font-size: large;"><br />Other stuff </span></h2>
<h3>
Netbeans - very cool</h3>
Some time ago I switched from Eclipse to
Netbeans for PHP development because Eclipse was having trouble handling
very large source files (such as <a href="http://www.tcpdf.org/" target="_blank">TCPDF</a>) and it was a little bit generic. I have never looked back, Netbeans just
gets better and better and is a joy to work with - <a href="http://www.netbeans.org/">http://www.netbeans.org</a>. It is free and a perfect platform for PHP development, with support for PHP, HTML, CSS and Javascript.<br />
<h3>
Stop using Wincache</h3>
Wincache does not speed up PHP on IIS 7+, and
causes it to seize up at random times. If you need an op-code cache
use the distributed one with PHP 5.5, or just make do. IIS 7.5 and
later are so quick and reliable, wincache is not going to make a
significant difference and might actually slow things down. Which leads
to...<br />
<h3>
IIS 7.5 and later - very cool</h3>
I've been extremely impressed by
IIS over the past few years. Ever since 7.5 it's been a real monster of a web server,
incredibly fast and easy to look after and set up. Well done Microsoft - and this is a very fine web site too: <a href="http://www.iis.net/">http://www.iis.net/</a>. It has plenty of support for running PHP on IIS.Robhttp://www.blogger.com/profile/06996218238493777139noreply@blogger.com4tag:blogger.com,1999:blog-1000869619866957301.post-39218918381215525322012-09-07T13:52:00.001+01:002014-09-23T11:03:17.104+01:00How to install Microsoft's SQL Server Driver for PHP<br />
There are questions on <a href="http://social.msdn.microsoft.com/Forums/en-US/sqldriverforphp/" target="_blank">Microsoft's SQL Server driver for PHP forum</a> from people that cannot work out how to install the PHP driver.<br />
<br />
PHP offers some help on choosing extensions here: <a href="http://php.net/manual/en/install.pecl.windows.php" target="_blank">http://php.net/manual/en/install.pecl.windows.php</a>, and here: <a href="http://php.net/manual/en/install.windows.extensions.php" target="_blank">http://php.net/manual/en/install.windows.extensions.php</a>.<br />
<br />
It's still a bit complicated, so here are some explicit instructions which I hope can help new users of PHP. After a while all of this will become common sense. <br />
<br />
<a name='more'></a><br />
<br />
Instruction notes: <br />
<ol>
<li>Microsoft's PHP driver for SQL Server only runs with PHP 32-bit builds (as standard from <a href="http://windows.php.net/download" target="_blank">http://windows.php.net/download</a>).</li>
<li>These instructions are only for PHP 5.2, 5.3 or 5.4. <span style="color: #e69138;">(5.5 and 5.6 supported with custom build.)</span></li>
<li>I am pretending that PHP is installed into folder <span style="color: blue;">C:\PHP</span>. Replace the term C:\PHP in the instructions with whatever folder your PHP is installed into.</li>
<li><span style="color: #e69138;">See orange text for PHP 5.5 and 5.6 support in 32 and 64 bits. If you do not know whether you have 64-bit PHP 5.5/5.6, start the instructions at step 5, note the orange comment in 5.3, then return to step 1 of the instructions below.</span></li>
</ol>
Instructions: <br />
<ol>
<li>Download Microsoft's PHP drivers. Open page: <a href="http://www.microsoft.com/en-us/download/details.aspx?id=20098" target="_blank">http://www.microsoft.com/en-us/download/details.aspx?id=20098</a><ol>
<li>If you have Windows Vista, Server 2008 or above, download SQLSRV30.EXE. </li>
<li>If you have Server 2003/Windows XP or below, download SQLSRV20.EXE</li>
<li><i><span style="color: #e69138;">If you have PHP 5.5/5.6, or you have Windows XP/2003 and you want to install driver 3.0...</span></i><ol>
<li><i><span style="color: #e69138;">See this post: <a href="http://robsphp.blogspot.com/2012/06/unofficial-microsoft-sql-server-driver.html" target="_blank">Unofficial php_sqlsrv 3.0</a> and download the archive</span></i></li>
<li><i><span style="color: #e69138;">Note the text in orange in step (2.) below - you don't need to extract the 'source code' folder. Also...</span></i></li>
<ol>
<li><i><span style="color: #e69138;">If you have 64-bit PHP, you need to extract <u>only</u> the files in sub-folder <b>x64</b> of the zip file, and can ignore everything else.</span></i></li>
<li><i><span style="color: #e69138;">If you have 32-bit PHP, you should ignore the folder <b>x64</b> in the zip file.</span></i></li>
</ol>
<li><i><i><span style="color: #e69138;">Assume for step (6.) below that you downloaded <b>SQLSRV30.EXE</b></span></i></i></li>
<li><i><i><span style="color: #e69138;">Remember that PHP 5.5+ does not work on Windows XP/2003 - for this OS you can only go up to PHP 5.4.</span><br /><span style="color: #b45f06;"> </span></i></i></li>
</ol>
</li>
</ol>
</li>
<li>Extract the entire contents of the file downloaded in (1.) to your PHP extensions folder. This is usually a sub-folder of the location where php is installed, named 'ext', e.g. <span style="color: blue;">C:\PHP</span>\ext.<br /><i>Note that you can use <a href="http://www.rarlab.com/download.htm" target="_blank">WinRAR</a> to open the .exe file and extract the files, if you want.<br /><br /><span style="color: #e69138;">For users of the <i><i><a href="http://robsphp.blogspot.com/2012/06/unofficial-microsoft-sql-server-driver.html" target="_blank">Unofficial php_sqlsrv 3.0</a></i></i> zip file:</span></i></li>
<ol>
<li><i><span style="color: #e69138;">If you have 64-bit PHP, please extract only the contents of folder <b>x64 </b>of the zip file.</span></i></li>
<li><i><span style="color: #e69138;">If you have 32-bit PHP, extract the files in the root of the zip and ignore any folders. </span><br /> </i></li>
</ol>
<li>Download the SQL Server Native Client (SNAC), the PHP driver depends on it:<ol>
<li>For SQLSRV30.EXE (Windows Vista and above), go here: <a href="http://www.microsoft.com/en-us/download/details.aspx?id=29065#SNAC" target="_blank">http://www.microsoft.com/en-us/download/details.aspx?id=29065#SNAC</a>. These are <b>direct links</b>:</li>
<ol>
<li><a href="http://go.microsoft.com/fwlink/?LinkID=239648&clcid=0x409" target="_blank">SQL Server 2012 Native Client for 64 Bit Windows</a> (x64)</li>
<li><a href="http://go.microsoft.com/fwlink/?LinkID=239647&clcid=0x409" target="_blank">SQL Server 2012 Native Client for 32 Bit Windows</a> (x86)<br /> </li>
</ol>
<li>For SQLSRV20.EXE (Windows XP/2003), go here: <a href="http://www.microsoft.com/en-us/download/details.aspx?id=16978#SNAC" target="_blank">http://www.microsoft.com/en-us/download/details.aspx?id=16978#SNAC</a>. Here are the <b>direct links</b>:</li>
<ol>
<li><a href="http://go.microsoft.com/fwlink/?LinkID=188401&clcid=0x409" target="_blank">SQL Server 2008r2 Native Client for 64 Bit Windows</a> (x64)</li>
<li><a href="http://go.microsoft.com/fwlink/?LinkID=188400&clcid=0x409" target="_blank">SQL Server 2008r2 Native Client for 32 Bit Windows</a> (x86)<br /> </li>
</ol>
</ol>
</li>
<li>
Install the SQL Server Native Client downloaded from the previous step (3.) on the same computer that runs PHP.<br /> </li>
<li>Find out which version of PHP you have (if you already know it, go to step 6):<br /><br /><ol>
<li>Open a command window (run cmd.exe)</li>
<li>Using your PHP folder, run command: <span style="font-family: "Courier New",Courier,monospace;">"</span><span style="color: blue; font-family: "Courier New",Courier,monospace;">C:\PHP</span><span style="font-family: "Courier New",Courier,monospace;">\php.exe" -i | more</span><br /><i>'more' loads the information page by page.</i></li>
<li>The top line will show <i>PHP Version => 5.x.x...</i> Make a note of the version. It will start with 5.6, 5.5, 5.4, 5.3 or 5.2. The final (3rd) number is not important.<span style="color: #e69138; display: block; margin: 0.25em 0px 0.25em 1em;">PHP 5.5+ users can also tell if they have a 64-bit or 32-bit version of PHP by looking at the reported <i><b>Architecture</b></i>: <b>x86</b> = <b>32-bit</b> and <b>x64</b> = <b>64-bit</b>.</span><i>For the next steps you may need to scroll to the 2nd information page in the console window, by pressing the space bar...</i></li>
<li><b><i>For version 5.6, 5.5, 5.4 and 5.3</i></b>, a little further down is setting <i>PHP Extension Build => API...</i> It will end with either ...TS,VCn or ...NTS,VCn. Make a note of whether it says TS or NTS. Also make a note of whether the final part is VC11, VC9 or VC6 (VC6 is still possible with PHP 5.3 and SQLSRV20.EXE).</li>
<li><b><i>For PHP version 5.2 only</i></b>: note the setting that says "Thread Safety enabled" or "Thread Safety disabled". <br />"Thread Safety enabled" means TS,VC6<br />"Thread Safety disabled" means NTS,VC6.<br /> </li>
</ol>
</li>
<li>Edit your php configuration file, php.ini. Go to the end of the file and add these lines, depending on your earlier choices. You need to know your PHP version number (5.2, 5.3, 5.4, 5.5 or 5.6), and your TS/NTS setting, and whether you have a VC11, VC9 or VC6 build, all noted from step 5. above.<br /><br />You can calculate the files you need by looking at the file name and comparing to your PHP version, and remembering whether you downloaded SQLSRV20.exe or SQLSRV30.exe. Here are all of the combinations that are possible:<br /><span style="color: #b45f06;"><br />
</span><ul>
<li style="color: #e69138;">
For the custom drivers, PHP 5.6, NTS, VC11 add lines:
<div style="border: 1px solid silver; font-family: monospace; margin-bottom: 1em; margin-left: 1em; padding: 2px; width: 30em;">
extension=php_sqlsrv_56_nts.dll<br />
extension=php_pdo_sqlsrv_56_nts.dll
</div>
</li>
<li style="color: #e69138;">
For the custom drivers, PHP 5.6, TS, VC11 add lines:
<div style="border: 1px solid silver; font-family: monospace; margin-bottom: 1em; margin-left: 1em; padding: 2px; width: 30em;">
extension=php_sqlsrv_56_ts.dll<br />
extension=php_pdo_sqlsrv_56_ts.dll
</div>
</li>
<li style="color: #e69138;">
For the custom drivers, PHP 5.5, NTS, VC11 add lines:
<div style="border: 1px solid silver; font-family: monospace; margin-bottom: 1em; margin-left: 1em; padding: 2px; width: 30em;">
extension=php_sqlsrv_55_nts.dll<br />
extension=php_pdo_sqlsrv_55_nts.dll
</div>
</li>
<li style="color: #e69138;">
For the custom drivers, PHP 5.5, TS, VC11 add lines:
<div style="border: 1px solid silver; font-family: monospace; margin-bottom: 1em; margin-left: 1em; padding: 2px; width: 30em;">
extension=php_sqlsrv_55_ts.dll<br />
extension=php_pdo_sqlsrv_55_ts.dll
</div>
</li>
<li>
For SQLSRV30.EXE <span style="color: #e69138;">(or the custom drivers)</span>, PHP 5.4, NTS, VC9 add lines:
<div style="border: 1px solid silver; font-family: monospace; margin-bottom: 1em; margin-left: 1em; padding: 2px; width: 30em;">
extension=php_sqlsrv_54_nts.dll<br />
extension=php_pdo_sqlsrv_54_nts.dll
</div>
</li>
<li>
For SQLSRV30.EXE <span style="color: #e69138;">(or the custom drivers)</span>, PHP 5.4, TS, VC9 add lines:
<div style="border: 1px solid silver; font-family: monospace; margin-bottom: 1em; margin-left: 1em; padding: 2px; width: 30em;">
extension=php_sqlsrv_54_ts.dll<br />
extension=php_pdo_sqlsrv_54_ts.dll
</div>
</li>
<li>
For SQLSRV30.EXE <span style="color: #e69138;">(or the custom drivers)</span>, PHP 5.3, NTS, VC9 add lines:
<div style="border: 1px solid silver; font-family: monospace; margin-bottom: 1em; margin-left: 1em; padding: 2px; width: 30em;">
extension=php_sqlsrv_53_nts.dll<br />
extension=php_pdo_sqlsrv_53_nts.dll
</div>
</li>
<li>
For SQLSRV30.EXE <span style="color: #e69138;">(or the custom drivers)</span>, PHP 5.3, TS, VC9 add lines:
<div style="border: 1px solid silver; font-family: monospace; margin-bottom: 1em; margin-left: 1em; padding: 2px; width: 30em;">
extension=php_sqlsrv_53_ts.dll<br />
extension=php_pdo_sqlsrv_53_ts.dll
</div>
</li>
<li>
For SQLSRV20.EXE, PHP 5.3, NTS, VC9 add lines:
<div style="border: 1px solid silver; font-family: monospace; margin-bottom: 1em; margin-left: 1em; padding: 2px; width: 30em;">
extension=php_sqlsrv_53_nts_vc9.dll<br />
extension=php_pdo_sqlsrv_53_nts_vc9.dll
</div>
</li>
<li>
For SQLSRV20.EXE, PHP 5.3, NTS, VC6 add lines:
<div style="border: 1px solid silver; font-family: monospace; margin-bottom: 1em; margin-left: 1em; padding: 2px; width: 30em;">
extension=php_sqlsrv_53_nts_vc6.dll<br />
extension=php_pdo_sqlsrv_53_nts_vc6.dll
</div>
</li>
<li>
For SQLSRV20.EXE, PHP 5.3, TS, VC9 add lines:
<div style="border: 1px solid silver; font-family: monospace; margin-bottom: 1em; margin-left: 1em; padding: 2px; width: 30em;">
extension=php_sqlsrv_53_ts_vc9.dll<br />
extension=php_pdo_sqlsrv_53_ts_vc9.dll
</div>
</li>
<li>
For SQLSRV20.EXE, PHP 5.3, TS, VC6 add lines:
<div style="border: 1px solid silver; font-family: monospace; margin-bottom: 1em; margin-left: 1em; padding: 2px; width: 30em;">
extension=php_sqlsrv_53_ts_vc6.dll<br />
extension=php_pdo_sqlsrv_53_ts_vc6.dll
</div>
</li>
<li>
For SQLSRV20.EXE, PHP 5.2, NTS, VC6 add lines:
<div style="border: 1px solid silver; font-family: monospace; margin-bottom: 1em; margin-left: 1em; padding: 2px; width: 30em;">
extension=php_sqlsrv_52_nts_vc6.dll<br />
extension=php_pdo_sqlsrv_52_nts_vc6.dll
</div>
</li>
<li>
For SQLSRV20.EXE, PHP 5.2, TS, VC6 add lines:
<div style="border: 1px solid silver; font-family: monospace; margin-bottom: 1em; margin-left: 1em; padding: 2px; width: 30em;">
extension=php_sqlsrv_52_ts_vc6.dll<br />
extension=php_pdo_sqlsrv_52_ts_vc6.dll
</div>
</li>
</ul>
</li>
<li>Restart your web server for the change to take effect. That means stop and start IIS, Apache, or whatever you use, not the whole computer.</li>
</ol>
Robhttp://www.blogger.com/profile/06996218238493777139noreply@blogger.com210tag:blogger.com,1999:blog-1000869619866957301.post-60889575779883003462012-07-12T23:20:00.002+01:002014-06-11T11:20:13.335+01:00PHP_Wincache - Wincache for PHP 5.4<span style="font-size: large;"><b>** Update May 2014 **</b></span><br />
Don't use wincache on IIS 7+. I found that PHP on IIS runs faster without wincache, and when wincache is used it always causes php to seize up after a large number of requests and your server will stop responding. PHP 5.5 has a built in op-code cache extension, so it's probably not worth using wincache any more.<br />
<br />
Microsoft's official release of Wincache 1.3 for PHP 5.4 and 5.5 is now available on the Wincache home page: <a href="http://www.iis.net/download/wincacheforphp">http://www.iis.net/download/wincacheforphp</a><br />
<br />
Links:<br />
<ul>
<li><a href="http://www.iis.net/download/wincacheforphp" target="_blank">Wincache official homepage</a> from the fantastic <a href="http://www.iis.net/" target="_blank">IIS site</a>, one of my favourites, well worth exploring.</li>
<li>Microsoft's own pre-release builds for Wincache: <a href="http://sourceforge.net/projects/wincache/files/development/">http://sourceforge.net/projects/wincache/files/development/</a></li>
</ul>
<ul>
</ul>
Robhttp://www.blogger.com/profile/06996218238493777139noreply@blogger.com0tag:blogger.com,1999:blog-1000869619866957301.post-75410030808079948512012-06-13T15:30:00.000+01:002015-06-05T20:49:13.164+01:00FindProxyForURL - Parse a PAC (proxy auto config) file in PHPProxy servers are used in most companies as a gateway to the internet, that do other things like cache results and check web requests and responses. A PAC file is a Javascript file evaluated by web browsers to determine which proxy server to use for certain URL's. For example, some URL's can go straight to the internet, or a local server, and others must be sent to one of the proxy servers.<br />
<br />
One day I needed to run this javascript from php on the web server, to find the proxy in the same way; then php could also follow the same proxy rules as desktop computers in my office.<br />
<br />
Here's a working example of using the php_spidermonkey extension to run a proxy auto config script (javascript) in PHP.<br />
<br />
<a name='more'></a><br />
<br />
All you need is a PAC file to test it. You can write your own following instructions across the web, here is a guide: <a href="http://findproxyforurl.com/pac-functions/">http://findproxyforurl.com/pac-functions/</a><br />
<br />
There is more information and Windows builds of php_spidermonkey in this article: <a href="http://robsphp.blogspot.co.uk/2012/06/javascript-in-php-on-windows-with.html">Javascript in PHP on Windows with php_spidermonkey</a>. Install the extension dll in PHP in the usual way.<br />
<br />
Here is a snippet of code that uses the class 'CProxy' (defined underneath) which executes the Javascript PAC function FindProxyForURL:<br />
<br />
<br />
<div style="text-align: right;">
<span style="font-size: xx-small;">Code formatted by <a href="http://hilite.me/">http://hilite.me/</a></span></div>
<div style="background-color: #f4f8f8; border: solid gray 1px; color: black; margin: 0.5em; overflow: auto; padding: .5em; width: auto;">
<pre style="line-height: 1.2em; margin: 0px;"><span style="color: green; font-weight: bold;">use</span> DEMO\PAC\CProxy; <span style="color: #408080; font-style: italic;">// namespace, if required.</span></pre>
<pre style="line-height: 1.2em; margin: 0px;"><span style="color: #19177c;"> </span></pre>
<pre style="line-height: 1.2em; margin: 0px;"><span style="color: #19177c;">$proxy</span> <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">new</span> CProxy();
<span style="color: #19177c;">$proxy</span><span style="color: #666666;">-></span><span style="color: #7d9029;">LoadPAC</span>(<span style="color: #ba2121;">'C:\proxy.pac'</span>); <span style="color: #408080; font-style: italic;">// pac file is usually a URL</span>
<span style="color: #19177c;">$test</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$proxy</span><span style="color: #666666;">-></span><span style="color: #7d9029;">FindProxyForAddress</span>(<span style="color: #ba2121;">'http://www.google.com/'</span>, <span style="color: green; font-weight: bold;">true</span>);
<span style="color: green; font-weight: bold;">echo</span> <span style="color: #19177c;">$test</span>;
<span style="color: #408080; font-style: italic;">/* </span>
<span style="color: #408080; font-style: italic;">Assuming your PAC file contained something like this:</span>
<span style="color: #408080; font-style: italic;">function FindProxyForURL(url, host)</span>
<span style="color: #408080; font-style: italic;">{</span>
<span style="color: #408080; font-style: italic;"> if ( shExpMatch(host, "*.google.*") )</span>
<span style="color: #408080; font-style: italic;"> {</span>
<span style="color: #408080; font-style: italic;"> return "PROXY 10.20.30.40:8000; DIRECT";</span>
<span style="color: #408080; font-style: italic;"> }</span>
<span style="color: #408080; font-style: italic;"> return "DIRECT";</span>
<span style="color: #408080; font-style: italic;">}</span>
<span style="color: #408080; font-style: italic;">this snippet would print:</span>
<span style="color: #408080; font-style: italic;"> PROXY 10.20.30.40:8000; DIRECT</span>
<span style="color: #408080; font-style: italic;">*/</span>
</pre>
</div>
<br />
Definition of class CProxy - it utilises and demonstrates some of the following: <br />
<ol>
<li><b>PHP 5.4 </b>- uses <b>namespaces</b> to show how they work in this scenario, and <b>traits</b>.</li>
<li><b>php_spidermonkey</b> javascript interpreter</li>
<li>PAC (<b>proxy-auto-config</b>) functions </li>
</ol>
<br />
<div style="background-color: #f4f4f4; border: solid gray 1px; color: black; margin: 0.5em; overflow: auto; padding: .5em; width: auto;">
<pre style="line-height: 1.2em; margin: 0; width: 1200px;"><span style="color: #bc7a00;"><?php</span>
<span style="color: #408080; font-style: italic;">/* This example uses a namespace, just to show how it works inside namespaces. </span>
<span style="color: #408080; font-style: italic;">Namespace can be removed. */</span>
<span style="color: green; font-weight: bold;">namespace</span> DEMO\PAC;
<span style="color: green; font-weight: bold;">use</span> \JSContext, \Exception;
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span><span style="color: green;">defined</span>(<span style="color: #ba2121;">'JSVERSION_DEFAULT'</span>) )
{
<span style="color: green;">error_log</span>(<span style="color: green; font-weight: bold;">__FILE__</span> <span style="color: #666666;">.</span> <span style="color: #ba2121;">'CProxy not loaded, php_spidermonkey extension not detected!'</span>);
<span style="color: green; font-weight: bold;">return</span>;
}</pre>
<pre style="line-height: 1.2em; margin: 0pt; width: 1200px;"> </pre>
<pre style="line-height: 1.2em; margin: 0pt; width: 1200px;"><span style="color: #408080; font-style: italic;">// global function to escape javascript strings:</span> </pre>
<pre style="line-height: 1.2em; margin: 0pt; width: 1200px;"><span style="color: green; font-weight: bold;">function</span> <span style="color: blue;">js</span>(<span style="color: #19177c;">$str</span>, <span style="color: #19177c;">$quotes</span> <span style="color: #666666;">=</span> <span style="color: #ba2121;">'\'"'</span>)
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: green;">addcslashes</span>(<span style="color: #19177c;">$str</span>,<span style="color: #ba2121;">"</span><span style="color: #bb6622; font-weight: bold;">\\</span><span style="color: #bb6688; font-weight: bold;">$quotes</span><span style="color: #bb6622; font-weight: bold;">\n\r</span><span style="color: #ba2121;"><>"</span>); <span style="color: #408080; font-style: italic;"></span>
} </pre>
<pre style="line-height: 1.2em; margin: 0pt; width: 1200px;"> </pre>
<pre style="line-height: 1.2em; margin: 0pt; width: 1200px;"><span style="color: green; font-weight: bold;">class</span> <span style="color: blue; font-weight: bold;">CProxy</span>
{
<span style="color: green; font-weight: bold;">use</span> PACFunctions; <span style="color: #408080; font-style: italic;">// include traits in this class, defined below.</span>
<span style="color: green; font-weight: bold;">private</span> <span style="color: #19177c;">$pac</span>, <span style="color: #19177c;">$js</span>, <span style="color: #19177c;">$proxyconfig</span>;
<span style="color: green; font-weight: bold;">function</span> <span style="color: blue;">__construct</span>(<span style="color: #19177c;">$pacfile</span><span style="color: #666666;">=</span><span style="color: green; font-weight: bold;">NULL</span>, <span style="color: #19177c;">$strictJavascript</span><span style="color: #666666;">=</span><span style="color: green; font-weight: bold;">false</span>)
{
<span style="color: #408080; font-style: italic;">// requires php_spidermonkey.dll extension.</span>
<span style="color: #19177c;">$js</span> <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">new</span> JSContext();
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$strictJavascript</span> )
{
<span style="color: #19177c;">$opts</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$js</span><span style="color: #666666;">-></span><span style="color: #7d9029;">getOptions</span>() <span style="color: #666666;">|</span> JSOPTION_STRICT <span style="color: #666666;">|</span> JSOPTION_WERROR;
<span style="color: #19177c;">$prev</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$js</span><span style="color: #666666;">-></span><span style="color: #7d9029;">setOptions</span>(<span style="color: #19177c;">$opts</span>);
}
<span style="color: #19177c;">$js</span><span style="color: #666666;">-></span><span style="color: #7d9029;">registerFunction</span>(__NAMESPACE__<span style="color: #666666;">.</span><span style="color: #ba2121;">'\CProxy::myIpAddress'</span>, <span style="color: #ba2121;">'myIpAddress'</span>);
<span style="color: #19177c;">$js</span><span style="color: #666666;">-></span><span style="color: #7d9029;">registerFunction</span>(__NAMESPACE__<span style="color: #666666;">.</span><span style="color: #ba2121;">'\CProxy::isPlainHostName'</span>, <span style="color: #ba2121;">'isPlainHostName'</span>);
<span style="color: #19177c;">$js</span><span style="color: #666666;">-></span><span style="color: #7d9029;">registerFunction</span>(__NAMESPACE__<span style="color: #666666;">.</span><span style="color: #ba2121;">'\CProxy::dnsDomainIs'</span>, <span style="color: #ba2121;">'dnsDomainIs'</span>);
<span style="color: #19177c;">$js</span><span style="color: #666666;">-></span><span style="color: #7d9029;">registerFunction</span>(__NAMESPACE__<span style="color: #666666;">.</span><span style="color: #ba2121;">'\CProxy::localHostOrDomainIs'</span>, <span style="color: #ba2121;">'localHostOrDomainIs'</span>);
<span style="color: #19177c;">$js</span><span style="color: #666666;">-></span><span style="color: #7d9029;">registerFunction</span>(__NAMESPACE__<span style="color: #666666;">.</span><span style="color: #ba2121;">'\CProxy::isResolvable'</span>, <span style="color: #ba2121;">'isResolvable'</span>);
<span style="color: #19177c;">$js</span><span style="color: #666666;">-></span><span style="color: #7d9029;">registerFunction</span>(__NAMESPACE__<span style="color: #666666;">.</span><span style="color: #ba2121;">'\CProxy::isInNet'</span>, <span style="color: #ba2121;">'isInNet'</span>);
<span style="color: #19177c;">$js</span><span style="color: #666666;">-></span><span style="color: #7d9029;">registerFunction</span>(__NAMESPACE__<span style="color: #666666;">.</span><span style="color: #ba2121;">'\CProxy::dnsResolve'</span>, <span style="color: #ba2121;">'dnsResolve'</span>);
<span style="color: #19177c;">$js</span><span style="color: #666666;">-></span><span style="color: #7d9029;">registerFunction</span>(__NAMESPACE__<span style="color: #666666;">.</span><span style="color: #ba2121;">'\CProxy::dnsDomainLevels'</span>, <span style="color: #ba2121;">'dnsDomainLevels'</span>);
<span style="color: #19177c;">$js</span><span style="color: #666666;">-></span><span style="color: #7d9029;">registerFunction</span>(__NAMESPACE__<span style="color: #666666;">.</span><span style="color: #ba2121;">'\CProxy::shExpMatch'</span>, <span style="color: #ba2121;">'shExpMatch'</span>);
<span style="color: #19177c;">$js</span><span style="color: #666666;">-></span><span style="color: #7d9029;">registerFunction</span>(__NAMESPACE__<span style="color: #666666;">.</span><span style="color: #ba2121;">'\CProxy::weekdayRange'</span>, <span style="color: #ba2121;">'weekdayRange'</span>);
<span style="color: #19177c;">$js</span><span style="color: #666666;">-></span><span style="color: #7d9029;">registerFunction</span>(__NAMESPACE__<span style="color: #666666;">.</span><span style="color: #ba2121;">'\CProxy::dateRange'</span>, <span style="color: #ba2121;">'dateRange'</span>);
<span style="color: #19177c;">$js</span><span style="color: #666666;">-></span><span style="color: #7d9029;">registerFunction</span>(__NAMESPACE__<span style="color: #666666;">.</span><span style="color: #ba2121;">'\CProxy::timeRange'</span>, <span style="color: #ba2121;">'timeRange'</span>);
<span style="color: #408080; font-style: italic;">// I don't know what ProxyConfig is used for:</span>
<span style="color: #19177c;">$this</span><span style="color: #666666;">-></span><span style="color: #7d9029;">proxyconfig</span> <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">new</span> \stdClass();
<span style="color: #19177c;">$this</span><span style="color: #666666;">-></span><span style="color: #7d9029;">proxyconfig</span><span style="color: #666666;">-></span><span style="color: #7d9029;">bindings</span> <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">array</span>();
<span style="color: #19177c;">$js</span><span style="color: #666666;">-></span><span style="color: #7d9029;">assign</span>(<span style="color: #ba2121;">'ProxyConfig'</span>, <span style="color: #19177c;">$this</span><span style="color: #666666;">-></span><span style="color: #7d9029;">proxyconfig</span>);
<span style="color: #19177c;">$this</span><span style="color: #666666;">-></span><span style="color: #7d9029;">js</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$js</span>;
<span style="color: #19177c;">$this</span><span style="color: #666666;">-></span><span style="color: #7d9029;">LoadPAC</span>(<span style="color: #19177c;">$pacfile</span>);
}
<span style="color: green; font-weight: bold;">function</span> <span style="color: blue;">FindProxyForAddress</span>(<span style="color: #19177c;">$address</span>, <span style="color: #19177c;">$raw</span><span style="color: #666666;">=</span><span style="color: green; font-weight: bold;">false</span>)
{
<span style="color: #408080; font-style: italic;">/* returns an array of proxy servers </span>
<span style="color: #408080; font-style: italic;"> (from preg_match_all),</span>
<span style="color: #408080; font-style: italic;"> or, if $raw is true, returns</span>
<span style="color: #408080; font-style: italic;"> the original string returned by </span>
<span style="color: #408080; font-style: italic;"> FindProxyForURL function. */</span>
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span><span style="color: #19177c;">$this</span><span style="color: #666666;">-></span><span style="color: #7d9029;">pac</span> )
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">false</span>;
<span style="color: #19177c;">$parts</span> <span style="color: #666666;">=</span> <span style="color: green;">parse_url</span>(<span style="color: #19177c;">$address</span>);
<span style="color: #19177c;">$url</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$address</span>;
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span><span style="color: green;">isset</span>(<span style="color: #19177c;">$parts</span>[<span style="color: #ba2121;">'port'</span>]) )
{
<span style="color: #19177c;">$port</span> <span style="color: #666666;">=</span> <span style="color: #ba2121;">''</span>;
}
<span style="color: green; font-weight: bold;">else</span> <span style="color: green; font-weight: bold;">if</span> ( (<span style="color: #19177c;">$port</span><span style="color: #666666;">=</span><span style="color: #19177c;">$parts</span>[<span style="color: #ba2121;">'port'</span>]) )
{
<span style="color: #19177c;">$port</span> <span style="color: #666666;">=</span> <span style="color: #ba2121;">':'</span> <span style="color: #666666;">.</span> <span style="color: #19177c;">$port</span>;
}
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span><span style="color: green;">isset</span>(<span style="color: #19177c;">$parts</span>[<span style="color: #ba2121;">'host'</span>]) )
{
<span style="color: #408080; font-style: italic;">/* $address is badly formed, not sure if this is right, but </span>
<span style="color: #408080; font-style: italic;"> * return it from the beginning to the first slash. */</span>
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span><span style="color: green;">isset</span>(<span style="color: #19177c;">$parts</span>[<span style="color: #ba2121;">'path'</span>]) )
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">false</span>;
<span style="color: #19177c;">$parts</span>[<span style="color: #ba2121;">'host'</span>] <span style="color: #666666;">=</span> <span style="color: green;">preg_replace</span>(<span style="color: #ba2121;">'/(.*?)(?>\/.*$|$)/'</span>, <span style="color: #ba2121;">'$1'</span>, <span style="color: #19177c;">$parts</span>[<span style="color: #ba2121;">'path'</span>]);
}
<span style="color: #19177c;">$host</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$parts</span>[<span style="color: #ba2121;">'host'</span>] <span style="color: #666666;">.</span> <span style="color: #19177c;">$port</span>;
<span style="color: green; font-weight: bold;">try</span>
{
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span>(<span style="color: #19177c;">$rv</span><span style="color: #666666;">=</span><span style="color: #19177c;">$this</span><span style="color: #666666;">-></span><span style="color: #7d9029;">js</span><span style="color: #666666;">-></span><span style="color: #7d9029;">evaluateScript</span>(<span style="color: #ba2121;">'FindProxyForURL(\''</span> <span style="color: #666666;">.</span> js(<span style="color: #19177c;">$url</span>) <span style="color: #666666;">.</span> <span style="color: #ba2121;">'\', \''</span> <span style="color: #666666;">.</span> js(<span style="color: #19177c;">$host</span>) <span style="color: #666666;">.</span> <span style="color: #ba2121;">'\');'</span>, <span style="color: #ba2121;">'FindProxyForURL'</span>)) <span style="color: #666666;">||</span> <span style="color: #19177c;">$raw</span> )
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: #19177c;">$rv</span>;
}
}
<span style="color: green; font-weight: bold;">catch</span> ( Exception <span style="color: #19177c;">$ex</span> )
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">false</span>;
}
<span style="color: green; font-weight: bold;">return</span> self<span style="color: #666666;">::</span><span style="color: #7d9029;">ReadPacResult</span>(<span style="color: #19177c;">$rv</span>);
}
<span style="color: green; font-weight: bold;">function</span> <span style="color: blue;">LoadPAC</span>(<span style="color: #19177c;">$pac</span>)
{
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$this</span><span style="color: #666666;">-></span><span style="color: #7d9029;">pac</span> )
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">NULL</span>; <span style="color: #408080; font-style: italic;">// already loaded</span>
}
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$pac</span> <span style="color: #666666;">===</span> <span style="color: green; font-weight: bold;">NULL</span> <span style="color: #666666;">||</span> <span style="color: #666666;">!</span><span style="color: #19177c;">$this</span><span style="color: #666666;">-></span><span style="color: #7d9029;">js</span> )
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">false</span>; <span style="color: #408080; font-style: italic;">// param error or not initialised properly</span>
}
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span>(<span style="color: #19177c;">$script</span> <span style="color: #666666;">=</span> <span style="color: green;">file_get_contents</span>(<span style="color: #19177c;">$pac</span>)) )
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">false</span>; <span style="color: #408080; font-style: italic;">// cannot read PAC file</span>
}
<span style="color: green; font-weight: bold;">try</span>
{
<span style="color: #19177c;">$rv</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$this</span><span style="color: #666666;">-></span><span style="color: #7d9029;">js</span><span style="color: #666666;">-></span><span style="color: #7d9029;">evaluateScript</span>(<span style="color: #19177c;">$script</span>, <span style="color: #19177c;">$pac</span>);
}
<span style="color: green; font-weight: bold;">catch</span> ( Exception <span style="color: #19177c;">$ex</span> )
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">false</span>;
}
<span style="color: #19177c;">$this</span><span style="color: #666666;">-></span><span style="color: #7d9029;">pac</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$script</span>;
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">true</span>;
}
<span style="color: green; font-weight: bold;">public</span> <span style="color: green; font-weight: bold;">static</span> <span style="color: green; font-weight: bold;">function</span> <span style="color: blue;">ReadPacResult</span>(<span style="color: #19177c;">$pacres</span>)
{
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span><span style="color: #19177c;">$pacres</span> )
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">false</span>;
<span style="color: #408080; font-style: italic;">// $pacres should be something like</span>
<span style="color: #408080; font-style: italic;">// PROXY 1.2.3.4:8080; PROXY 1.2.3.5:8080; DIRECT</span>
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span><span style="color: green;">preg_match_all</span>(<span style="color: #ba2121;">'/(PROXY|SOCKS|DIRECT)\s*([^;]*|.*?$)/ui'</span>, <span style="color: #19177c;">$pacres</span>, <span style="color: #19177c;">$matches</span>, PREG_SET_ORDER) )
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">false</span>;
}
<span style="color: green; font-weight: bold;">return</span> <span style="color: #19177c;">$matches</span>;
}
}
<span style="color: #bc7a00;">?></span>
</pre>
</div>
<br />
<ol>
</ol>
PAC functions written as a trait class (remember - not tested very much!):<br />
<br />
<div style="text-align: right;">
<span style="font-size: xx-small;">Code formatted by <a href="http://hilite.me/">http://hilite.me/</a></span></div>
<div style="background-color: #f4f4f4; border: solid gray 1px; color: black; margin: 0.5em; overflow: auto; padding: .5em; width: auto;">
<pre style="line-height: 1.2em; margin: 0px; width: 1200px;"><span style="color: #bc7a00;"><?php</span>
<span style="color: #408080; font-style: italic;">/* Define the PAC traits used in class CProxy, above.</span><span style="color: #408080; font-style: italic;"> */</span>
<span style="color: green; font-weight: bold;">namespace</span> DEMO\PAC;
<span style="color: green; font-weight: bold;">use</span> \DateTime, \DateTimeZone, \Exception;
trait PACFunctions
{
<span style="color: green; font-weight: bold;">private</span> <span style="color: green; font-weight: bold;">static</span> <span style="color: #19177c;">$s_myIP</span>;
<span style="color: #408080; font-style: italic;">/* </span>
<span style="color: #408080; font-style: italic;"> * PAC functions:</span>
<span style="color: #408080; font-style: italic;"> * 'global' functions to replicate javascript PAC environment... </span>
<span style="color: #408080; font-style: italic;"> */</span>
<span style="color: green; font-weight: bold;">static</span> <span style="color: green; font-weight: bold;">function</span> <span style="color: blue;">myIpAddress</span>()
{
<span style="color: green; font-weight: bold;">if</span> ( self<span style="color: #666666;">::</span><span style="color: #19177c;">$s_myIP</span> )
{
<span style="color: green; font-weight: bold;">return</span> self<span style="color: #666666;">::</span><span style="color: #19177c;">$s_myIP</span>;
}
<span style="color: #19177c;">$vars</span> <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">array</span>(<span style="color: #ba2121;">'SERVER_ADDR'</span>, <span style="color: #ba2121;">'LOCAL_ADDR'</span>);
<span style="color: #19177c;">$ip</span> <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">NULL</span>;
<span style="color: #19177c;">$bestip</span> <span style="color: #666666;">=</span> <span style="color: #ba2121;">'0.0.0.0'</span>;
<span style="color: green; font-weight: bold;">foreach</span> ( <span style="color: #19177c;">$vars</span> <span style="color: green; font-weight: bold;">as</span> <span style="color: #19177c;">$var</span> )
{
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: green;">isset</span>(<span style="color: #19177c;">$_SERVER</span>[<span style="color: #19177c;">$var</span>]) )
{
<span style="color: #19177c;">$ip</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$_SERVER</span>[<span style="color: #19177c;">$var</span>];
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span><span style="color: green; font-weight: bold;">empty</span>(<span style="color: #19177c;">$ip</span>) )
{
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$ip</span> <span style="color: #666666;">!=</span> <span style="color: #ba2121;">'127.0.0.1'</span> )
{
<span style="color: green; font-weight: bold;">return</span> (self<span style="color: #666666;">::</span><span style="color: #19177c;">$s_myIP</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$ip</span>);
}
<span style="color: #19177c;">$bestip</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$ip</span>;
}
}
}
<span style="color: green; font-weight: bold;">if</span> ( (<span style="color: #19177c;">$hn</span> <span style="color: #666666;">=</span> gethostname()) <span style="color: #666666;">&&</span> (<span style="color: #19177c;">$ip</span> <span style="color: #666666;">=</span> <span style="color: green;">gethostbyname</span>(<span style="color: #19177c;">$hn</span>)) <span style="color: #666666;">&&</span> <span style="color: #19177c;">$ip</span> <span style="color: #666666;">!==</span> <span style="color: #19177c;">$hn</span> )
{
<span style="color: green; font-weight: bold;">return</span> (self<span style="color: #666666;">::</span><span style="color: #19177c;">$s_myIP</span><span style="color: #666666;">=</span><span style="color: #19177c;">$ip</span>);
}
<span style="color: green; font-weight: bold;">return</span> (self<span style="color: #666666;">::</span><span style="color: #19177c;">$s_myIP</span><span style="color: #666666;">=</span><span style="color: #19177c;">$bestip</span>);
}
<span style="color: green; font-weight: bold;">static</span> <span style="color: green; font-weight: bold;">function</span> <span style="color: blue;">isPlainHostName</span>( <span style="color: #19177c;">$host</span> )
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: green;">strpos</span>(<span style="color: #19177c;">$host</span>, <span style="color: #ba2121;">'.'</span>)<span style="color: #666666;">===</span><span style="color: green; font-weight: bold;">false</span>;
}
<span style="color: green; font-weight: bold;">static</span> <span style="color: green; font-weight: bold;">function</span> <span style="color: blue;">shExpMatch</span>(<span style="color: #19177c;">$host</span>, <span style="color: #19177c;">$pattern</span>)
{
<span style="color: #19177c;">$parts</span> <span style="color: #666666;">=</span> <span style="color: green;">explode</span>(<span style="color: #ba2121;">'*'</span>, <span style="color: #19177c;">$pattern</span>);
<span style="color: #19177c;">$pattern</span> <span style="color: #666666;">=</span> <span style="color: #ba2121;">''</span>;
<span style="color: green; font-weight: bold;">foreach</span>( <span style="color: #19177c;">$parts</span> <span style="color: green; font-weight: bold;">as</span> <span style="color: #19177c;">$part</span> )
{
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$part</span> <span style="color: #666666;">===</span> <span style="color: #ba2121;">''</span> )
{
<span style="color: #19177c;">$pattern</span> <span style="color: #666666;">.=</span> <span style="color: #ba2121;">'.*?'</span>;
}
<span style="color: green; font-weight: bold;">else</span>
{
<span style="color: #19177c;">$pattern</span> <span style="color: #666666;">.=</span> <span style="color: green;">preg_quote</span>(<span style="color: #19177c;">$part</span>, <span style="color: #ba2121;">'/'</span>);
}
}
<span style="color: green; font-weight: bold;">return</span> <span style="color: green;">preg_match</span>(<span style="color: #ba2121;">'/^'</span><span style="color: #666666;">.</span><span style="color: #19177c;">$pattern</span><span style="color: #666666;">.</span><span style="color: #ba2121;">'$/ui'</span>, <span style="color: #19177c;">$host</span>)<span style="color: #666666;">===1</span>;
}
<span style="color: green; font-weight: bold;">static</span> <span style="color: green; font-weight: bold;">function</span> <span style="color: blue;">isResolvable</span>(<span style="color: #19177c;">$host</span>)
{
<span style="color: green; font-weight: bold;">if</span> ( ip2long(<span style="color: #19177c;">$host</span>) <span style="color: #666666;">!==</span> <span style="color: green; font-weight: bold;">false</span> )
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">true</span>;
}
<span style="color: #19177c;">$test</span> <span style="color: #666666;">=</span> <span style="color: green;">gethostbyname</span>(<span style="color: #19177c;">$host</span>);
<span style="color: green; font-weight: bold;">return</span> <span style="color: #19177c;">$test</span> <span style="color: #666666;">&&</span> ip2long(<span style="color: #19177c;">$test</span>) <span style="color: #666666;">!==</span> <span style="color: green; font-weight: bold;">false</span>;
}
<span style="color: green; font-weight: bold;">static</span> <span style="color: green; font-weight: bold;">function</span> <span style="color: blue;">isInNet</span>(<span style="color: #19177c;">$host</span>, <span style="color: #19177c;">$ip</span>, <span style="color: #19177c;">$subnet</span>)
{
<span style="color: green; font-weight: bold;">if</span> ( (<span style="color: #19177c;">$longhost</span> <span style="color: #666666;">=</span> ip2long(<span style="color: #19177c;">$host</span>)) <span style="color: #666666;">===</span> <span style="color: green; font-weight: bold;">false</span> )
{
<span style="color: #19177c;">$host</span> <span style="color: #666666;">=</span> <span style="color: green;">gethostbyname</span>(<span style="color: #19177c;">$host</span>);
<span style="color: green; font-weight: bold;">if</span> ( (<span style="color: #19177c;">$longhost</span> <span style="color: #666666;">=</span> ip2long(<span style="color: #19177c;">$host</span>))<span style="color: #666666;">===</span><span style="color: green; font-weight: bold;">false</span> )
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">false</span>;
}
}
<span style="color: #19177c;">$longip</span> <span style="color: #666666;">=</span> ip2long(<span style="color: #19177c;">$ip</span>);
<span style="color: #19177c;">$longsub</span> <span style="color: #666666;">=</span> ip2long(<span style="color: #19177c;">$subnet</span>);
<span style="color: green; font-weight: bold;">return</span> (<span style="color: #19177c;">$longhost</span><span style="color: #666666;">&</span><span style="color: #19177c;">$longsub</span>) <span style="color: #666666;">==</span> <span style="color: #19177c;">$longip</span>;
}
<span style="color: green; font-weight: bold;">static</span> <span style="color: green; font-weight: bold;">function</span> <span style="color: blue;">dnsDomainIs</span>(<span style="color: #19177c;">$host</span>, <span style="color: #19177c;">$domain</span>)
{
<span style="color: #19177c;">$len</span> <span style="color: #666666;">=</span> <span style="color: green;">strlen</span>(<span style="color: #19177c;">$domain</span>);
<span style="color: green; font-weight: bold;">return</span> <span style="color: #19177c;">$len</span> <span style="color: #666666;"><=</span> <span style="color: green;">strlen</span>(<span style="color: #19177c;">$host</span>) <span style="color: #666666;">&&</span> substr(<span style="color: #19177c;">$host</span>, <span style="color: #666666;">-</span><span style="color: #19177c;">$len</span>)<span style="color: #666666;">==</span><span style="color: #19177c;">$domain</span>;
}
<span style="color: green; font-weight: bold;">static</span> <span style="color: green; font-weight: bold;">function</span> <span style="color: blue;">localHostOrDomainIs</span>(<span style="color: #19177c;">$host</span>, <span style="color: #19177c;">$domain</span>)
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: green;">strcasecmp</span>(<span style="color: #19177c;">$domain</span>, substr(<span style="color: #19177c;">$host</span>, <span style="color: #666666;">0</span>, <span style="color: green;">strlen</span>(<span style="color: #19177c;">$domain</span>)))<span style="color: #666666;">===0</span>;
}
<span style="color: green; font-weight: bold;">static</span> <span style="color: green; font-weight: bold;">function</span> <span style="color: blue;">dnsResolve</span>(<span style="color: #19177c;">$host</span>)
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: green;">gethostbyname</span>(<span style="color: #19177c;">$host</span>);
}
<span style="color: green; font-weight: bold;">static</span> <span style="color: green; font-weight: bold;">function</span> <span style="color: blue;">dnsDomainLevels</span>(<span style="color: #19177c;">$host</span>)
{
<span style="color: green; font-weight: bold;">return</span> substr_count(<span style="color: #19177c;">$host</span>, <span style="color: #ba2121;">'.'</span>);
}
<span style="color: green; font-weight: bold;">static</span> <span style="color: green; font-weight: bold;">function</span> <span style="color: blue;">weekdayRange</span>(<span style="color: #19177c;">$fr</span>, <span style="color: #19177c;">$to</span><span style="color: #666666;">=</span><span style="color: green; font-weight: bold;">NULL</span>, <span style="color: #19177c;">$gmt</span><span style="color: #666666;">=</span><span style="color: green; font-weight: bold;">NULL</span>)
{
<span style="color: #19177c;">$days</span> <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">array</span>(<span style="color: #ba2121;">'mon'</span>, <span style="color: #ba2121;">'tue'</span>, <span style="color: #ba2121;">'wed'</span>, <span style="color: #ba2121;">'thu'</span>, <span style="color: #ba2121;">'fri'</span>, <span style="color: #ba2121;">'sat'</span>, <span style="color: #ba2121;">'sun'</span>);
<span style="color: #19177c;">$fr</span> <span style="color: #666666;">=</span> strtolower(substr(<span style="color: #19177c;">$fr</span>,<span style="color: #666666;">0</span>,<span style="color: #666666;">3</span>));
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$gmt</span> )
<span style="color: #19177c;">$gmt</span> <span style="color: #666666;">=</span> strtoupper(<span style="color: #19177c;">$gmt</span>);
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$to</span> <span style="color: #666666;">===</span> <span style="color: green; font-weight: bold;">NULL</span> )
<span style="color: #19177c;">$to</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$fr</span>;
<span style="color: green; font-weight: bold;">else</span> <span style="color: green; font-weight: bold;">if</span> ( <span style="color: green;">strcasecmp</span>(<span style="color: #19177c;">$to</span>, <span style="color: #ba2121;">'GMT'</span>)<span style="color: #666666;">==0</span> )
{
<span style="color: #19177c;">$to</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$fr</span>;
<span style="color: #19177c;">$gmt</span> <span style="color: #666666;">=</span> <span style="color: #ba2121;">'GMT'</span>;
}
<span style="color: green; font-weight: bold;">else</span>
<span style="color: #19177c;">$to</span> <span style="color: #666666;">=</span> strtolower(substr(<span style="color: #19177c;">$to</span>,<span style="color: #666666;">0</span>,<span style="color: #666666;">3</span>));
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$gmt</span> <span style="color: #666666;">==</span> <span style="color: #ba2121;">'GMT'</span> )
{
<span style="color: #19177c;">$tzn</span> <span style="color: #666666;">=</span> <span style="color: #ba2121;">'UTC'</span>;
}
<span style="color: green; font-weight: bold;">else</span> <span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span>(<span style="color: #19177c;">$tzn</span> <span style="color: #666666;">=</span> <span style="color: green;">ini_get</span>(<span style="color: #ba2121;">'date.timezone'</span>)) )
{
<span style="color: #19177c;">$tzn</span> <span style="color: #666666;">=</span> <span style="color: #ba2121;">'Europe/London'</span>;
}
<span style="color: #19177c;">$tz</span> <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">new</span> DateTimeZone(<span style="color: #19177c;">$tzn</span>);
<span style="color: #19177c;">$today</span> <span style="color: #666666;">=</span> strtolower( (<span style="color: green; font-weight: bold;">new</span> DateTime(<span style="color: #ba2121;">'now'</span>,<span style="color: #19177c;">$tz</span>))<span style="color: #666666;">-></span><span style="color: #7d9029;">format</span>(<span style="color: #ba2121;">'D'</span>) );
<span style="color: green; font-weight: bold;">foreach</span> ( <span style="color: #19177c;">$days</span> <span style="color: green; font-weight: bold;">as</span> <span style="color: #19177c;">$wd</span><span style="color: #666666;">=></span><span style="color: #19177c;">$day</span> )
{
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$fr</span> <span style="color: #666666;">==</span> <span style="color: #19177c;">$day</span> )
<span style="color: #19177c;">$fr</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$wd</span><span style="color: #666666;">+1</span>;
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$to</span> <span style="color: #666666;">==</span> <span style="color: #19177c;">$day</span> )
<span style="color: #19177c;">$to</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$wd</span><span style="color: #666666;">+1</span>;
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$today</span> <span style="color: #666666;">==</span> <span style="color: #19177c;">$day</span> )
<span style="color: #19177c;">$today</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$wd</span><span style="color: #666666;">+1</span>;
}
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span>(int)<span style="color: #19177c;">$today</span> <span style="color: #666666;">||</span> <span style="color: #666666;">!</span>(int)<span style="color: #19177c;">$fr</span> <span style="color: #666666;">||</span> <span style="color: #666666;">!</span>(int)<span style="color: #19177c;">$to</span> )
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">false</span>;
<span style="color: #408080; font-style: italic;">// FRI = 5</span>
<span style="color: #408080; font-style: italic;">// MON = 1</span>
<span style="color: #408080; font-style: italic;">// THU = 4</span>
<span style="color: #408080; font-style: italic;">// is THU in FRI - MON ??</span>
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$fr</span> <span style="color: #666666;">></span> <span style="color: #19177c;">$to</span> )
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: #19177c;">$today</span> <span style="color: #666666;">>=</span> <span style="color: #19177c;">$fr</span> <span style="color: #666666;">||</span> <span style="color: #19177c;">$today</span> <span style="color: #666666;"><=</span> <span style="color: #19177c;">$to</span>;
}
<span style="color: green; font-weight: bold;">return</span> <span style="color: #19177c;">$today</span> <span style="color: #666666;">>=</span> <span style="color: #19177c;">$fr</span> <span style="color: #666666;">&&</span> <span style="color: #19177c;">$today</span> <span style="color: #666666;"><=</span> <span style="color: #19177c;">$to</span>;
}
<span style="color: green; font-weight: bold;">static</span> <span style="color: green; font-weight: bold;">function</span> <span style="color: blue;">dateRange</span>(<span style="color: #19177c;">$day1</span>, <span style="color: #19177c;">$month1</span><span style="color: #666666;">=</span><span style="color: green; font-weight: bold;">NULL</span>, <span style="color: #19177c;">$year1</span><span style="color: #666666;">=</span><span style="color: green; font-weight: bold;">NULL</span>, <span style="color: #19177c;">$day2</span><span style="color: #666666;">=</span><span style="color: green; font-weight: bold;">NULL</span>, <span style="color: #19177c;">$month2</span><span style="color: #666666;">=</span><span style="color: green; font-weight: bold;">NULL</span>, <span style="color: #19177c;">$year2</span><span style="color: #666666;">=</span><span style="color: green; font-weight: bold;">NULL</span>, <span style="color: #19177c;">$gmt</span><span style="color: #666666;">=</span><span style="color: green; font-weight: bold;">NULL</span>)
{
<span style="color: #19177c;">$args</span> <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">array</span>(<span style="color: #19177c;">$day1</span>, <span style="color: #19177c;">$month1</span>, <span style="color: #19177c;">$year1</span>, <span style="color: #19177c;">$day2</span>, <span style="color: #19177c;">$month2</span>, <span style="color: #19177c;">$year2</span>, <span style="color: #19177c;">$gmt</span>);
<span style="color: #19177c;">$RationaliseArgs</span> <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">function</span>(<span style="color: #666666;">&</span><span style="color: #19177c;">$arg</span>)
{
<span style="color: #19177c;">$isgmt</span> <span style="color: #666666;">=</span> <span style="color: #666666;">0</span>;
<span style="color: green; font-weight: bold;">for</span> ( <span style="color: #19177c;">$ct</span><span style="color: #666666;">=0</span> ; <span style="color: #19177c;">$ct</span> <span style="color: #666666;"><</span> <span style="color: #666666;">6</span> ; <span style="color: #19177c;">$ct</span> <span style="color: #666666;">+=</span> <span style="color: #666666;">3</span> )
{
<span style="color: green; font-weight: bold;">if</span> ( (int)<span style="color: #19177c;">$arg</span>[<span style="color: #19177c;">$ct</span>] <span style="color: #666666;">></span> <span style="color: #666666;">31</span> )
{
<span style="color: #408080; font-style: italic;">// 1st arg is a year, insert blank day and month</span>
<span style="color: green;">array_splice</span>(<span style="color: #19177c;">$arg</span>, <span style="color: #19177c;">$ct</span>, <span style="color: #666666;">0</span>, <span style="color: green; font-weight: bold;">array</span>(<span style="color: #666666;">0</span>,<span style="color: #ba2121;">''</span>));
}
<span style="color: green; font-weight: bold;">else</span> <span style="color: green; font-weight: bold;">if</span> ( (int)<span style="color: #19177c;">$arg</span>[<span style="color: #19177c;">$ct</span>] )
{
<span style="color: #408080; font-style: italic;">// 1st arg is day</span>
<span style="color: green; font-weight: bold;">if</span> ( (int)<span style="color: #19177c;">$arg</span>[<span style="color: #19177c;">$ct</span><span style="color: #666666;">+1</span>] )
{
<span style="color: #408080; font-style: italic;">// 2nd arg must be a day. 2nd arg can't be year if 1st arg is a day.</span>
<span style="color: green;">array_splice</span>(<span style="color: #19177c;">$arg</span>, <span style="color: #19177c;">$ct</span><span style="color: #666666;">+1</span>, <span style="color: #666666;">0</span>, <span style="color: green; font-weight: bold;">array</span>(<span style="color: #ba2121;">''</span>,<span style="color: #666666;">0</span>)); <span style="color: #408080; font-style: italic;">// insert blank month and year</span>
}
<span style="color: green; font-weight: bold;">else</span> <span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$arg</span>[<span style="color: #19177c;">$ct</span><span style="color: #666666;">+1</span>]<span style="color: #666666;">===</span><span style="color: green; font-weight: bold;">NULL</span> <span style="color: #666666;">||</span> <span style="color: green;">strcasecmp</span>(<span style="color: #19177c;">$arg</span>[<span style="color: #19177c;">$ct</span><span style="color: #666666;">+1</span>], <span style="color: #ba2121;">'GMT'</span>)<span style="color: #666666;">==0</span> )
{
<span style="color: #408080; font-style: italic;">// nothing else after the day.</span>
<span style="color: green;">array_splice</span>(<span style="color: #19177c;">$arg</span>, <span style="color: #19177c;">$ct</span><span style="color: #666666;">+1</span>, <span style="color: #666666;">0</span>, <span style="color: green; font-weight: bold;">array</span>(<span style="color: #ba2121;">''</span>,<span style="color: #666666;">0</span>)); <span style="color: #408080; font-style: italic;">// insert blank month and year.</span>
}
<span style="color: green; font-weight: bold;">else</span> <span style="color: green; font-weight: bold;">if</span> ( (int)<span style="color: #19177c;">$arg</span>[<span style="color: #19177c;">$ct</span><span style="color: #666666;">+2</span>] <span style="color: #666666;">&&</span> (int)<span style="color: #19177c;">$arg</span>[<span style="color: #19177c;">$ct</span><span style="color: #666666;">+2</span>] <span style="color: #666666;"><=</span> <span style="color: #666666;">31</span> ) <span style="color: #408080; font-style: italic;">// month supplied, test what's after month</span>
{
<span style="color: #408080; font-style: italic;">// month followed by day... insert blank year...</span>
<span style="color: green;">array_splice</span>(<span style="color: #19177c;">$arg</span>, <span style="color: #19177c;">$ct</span><span style="color: #666666;">+2</span>, <span style="color: #666666;">0</span>, <span style="color: green; font-weight: bold;">array</span>(<span style="color: #666666;">0</span>)); <span style="color: #408080; font-style: italic;">// insert empty year1 and shift arg2 to 2nd date.</span>
}
}
<span style="color: green; font-weight: bold;">else</span> <span style="color: green; font-weight: bold;">if</span> ( <span style="color: green;">is_string</span>(<span style="color: #19177c;">$arg</span>[<span style="color: #19177c;">$ct</span>]) )
{
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: green;">strcasecmp</span>(<span style="color: #19177c;">$arg</span>[<span style="color: #19177c;">$ct</span>], <span style="color: #ba2121;">'GMT'</span>)<span style="color: #666666;">==0</span> )
{
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span><span style="color: #19177c;">$ct</span> )
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">false</span>; <span style="color: #408080; font-style: italic;">// error, 1st arg cannot be 'GMT'.</span>
}
<span style="color: green; font-weight: bold;">break</span>;
}
<span style="color: #408080; font-style: italic;">// 1st arg is month</span>
<span style="color: green;">array_splice</span>(<span style="color: #19177c;">$arg</span>, <span style="color: #19177c;">$ct</span>, <span style="color: #666666;">0</span>, <span style="color: green; font-weight: bold;">array</span>(<span style="color: #666666;">0</span>)); <span style="color: #408080; font-style: italic;">// insert a blank day.</span>
<span style="color: green; font-weight: bold;">if</span> ( (int)<span style="color: #19177c;">$arg</span>[<span style="color: #19177c;">$ct</span><span style="color: #666666;">+2</span>] <span style="color: #666666;"><=</span> <span style="color: #666666;">31</span> ) <span style="color: #408080; font-style: italic;">// month not followed by a year</span>
{
<span style="color: #408080; font-style: italic;">// insert a blank year...</span>
<span style="color: green;">array_splice</span>(<span style="color: #19177c;">$arg</span>, <span style="color: #19177c;">$ct</span><span style="color: #666666;">+2</span>, <span style="color: #666666;">0</span>, <span style="color: green; font-weight: bold;">array</span>(<span style="color: #666666;">0</span>));
}
}
<span style="color: green; font-weight: bold;">else</span>
{
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$ct</span> <span style="color: #666666;">&&</span> <span style="color: #19177c;">$arg</span>[<span style="color: #19177c;">$ct</span>] <span style="color: #666666;">===</span> <span style="color: green; font-weight: bold;">NULL</span> ) <span style="color: #408080; font-style: italic;">// only 1 date supplied. ok.</span>
<span style="color: green; font-weight: bold;">return</span> <span style="color: #666666;">1</span>;
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">false</span>; <span style="color: #408080; font-style: italic;">// error</span>
}
}
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: green;">strcasecmp</span>(<span style="color: #19177c;">$arg</span>[<span style="color: #19177c;">$ct</span>], <span style="color: #ba2121;">'GMT'</span>)<span style="color: #666666;">==0</span> )
{
<span style="color: #19177c;">$isgmt</span> <span style="color: #666666;">=</span> <span style="color: #666666;">1</span>;
<span style="color: #19177c;">$arg</span>[<span style="color: #19177c;">$ct</span>] <span style="color: #666666;">=</span> <span style="color: #ba2121;">'GMT'</span>;
}
<span style="color: green;">array_splice</span>(<span style="color: #19177c;">$arg</span>, <span style="color: #666666;">7</span>);
<span style="color: green; font-weight: bold;">return</span> <span style="color: #19177c;">$ct</span><span style="color: #666666;">/3</span>;
};
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span>(<span style="color: #19177c;">$cd</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$RationaliseArgs</span>(<span style="color: #19177c;">$args</span>, <span style="color: #19177c;">$gmt</span>)) )
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">false</span>;
}
<span style="color: green; font-weight: bold;">list</span>(<span style="color: #19177c;">$day1</span>, <span style="color: #19177c;">$month1</span>, <span style="color: #19177c;">$year1</span>, <span style="color: #19177c;">$day2</span>, <span style="color: #19177c;">$month2</span>, <span style="color: #19177c;">$year2</span>, <span style="color: #19177c;">$gmt</span>) <span style="color: #666666;">=</span> <span style="color: #19177c;">$args</span>;
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$gmt</span> <span style="color: #666666;">==</span> <span style="color: #ba2121;">'GMT'</span> )
{
<span style="color: #19177c;">$tzn</span> <span style="color: #666666;">=</span> <span style="color: #ba2121;">'UTC'</span>;
}
<span style="color: green; font-weight: bold;">else</span> <span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span>(<span style="color: #19177c;">$tzn</span> <span style="color: #666666;">=</span> <span style="color: green;">ini_get</span>(<span style="color: #ba2121;">'date.timezone'</span>)) )
{
<span style="color: #19177c;">$tzn</span> <span style="color: #666666;">=</span> <span style="color: #ba2121;">'Europe/London'</span>;
}
<span style="color: #19177c;">$tz</span> <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">new</span> DateTimeZone(<span style="color: #19177c;">$tzn</span>);
<span style="color: #19177c;">$now</span> <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">new</span> DateTime(<span style="color: #ba2121;">'now'</span>,<span style="color: #19177c;">$tz</span>);
<span style="color: #19177c;">$thisyear</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$now</span><span style="color: #666666;">-></span><span style="color: #7d9029;">format</span>(<span style="color: #ba2121;">'Y'</span>);
<span style="color: #19177c;">$thismonth</span> <span style="color: #666666;">=</span> <span style="color: #ba2121;">'Jan'</span>; <span style="color: #408080; font-style: italic;">// a month with 31 days.</span>
<span style="color: #19177c;">$thisday</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$now</span><span style="color: #666666;">-></span><span style="color: #7d9029;">format</span>(<span style="color: #ba2121;">'d'</span>);
<span style="color: green; font-weight: bold;">try</span>
{
<span style="color: #19177c;">$from</span> <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">new</span> \DateTime((<span style="color: #19177c;">$year1</span> <span style="color: #666666;">?</span> <span style="color: #19177c;">$year1</span> <span style="color: #666666;">:</span> <span style="color: #19177c;">$thisyear</span>) <span style="color: #666666;">.</span> <span style="color: #ba2121;">'-'</span> <span style="color: #666666;">.</span> (<span style="color: #19177c;">$month1</span> <span style="color: #666666;">?</span> <span style="color: #19177c;">$month1</span> <span style="color: #666666;">:</span> <span style="color: #19177c;">$thismonth</span>) <span style="color: #666666;">.</span> <span style="color: #ba2121;">'-'</span> <span style="color: #666666;">.</span> (<span style="color: #19177c;">$day1</span> <span style="color: #666666;">?</span> <span style="color: #19177c;">$day1</span> <span style="color: #666666;">:</span> <span style="color: #19177c;">$thisday</span>), <span style="color: #19177c;">$tz</span>);
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span><span style="color: #19177c;">$day1</span> )
<span style="color: #19177c;">$day2</span> <span style="color: #666666;">=</span> <span style="color: #666666;">0</span>;
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span><span style="color: #19177c;">$month1</span> )
<span style="color: #19177c;">$month2</span> <span style="color: #666666;">=</span> <span style="color: #ba2121;">''</span>;
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span><span style="color: #19177c;">$year1</span> )
<span style="color: #19177c;">$year2</span> <span style="color: #666666;">=</span> <span style="color: #666666;">0</span>;
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$day2</span> <span style="color: #666666;">||</span> <span style="color: #19177c;">$month2</span> <span style="color: #666666;">||</span> <span style="color: #19177c;">$year2</span> )
{
<span style="color: #19177c;">$to</span> <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">new</span> \DateTime((<span style="color: #19177c;">$year2</span> <span style="color: #666666;">?</span> <span style="color: #19177c;">$year2</span> <span style="color: #666666;">:</span> <span style="color: #19177c;">$thisyear</span>) <span style="color: #666666;">.</span> <span style="color: #ba2121;">'-'</span> <span style="color: #666666;">.</span> (<span style="color: #19177c;">$month2</span> <span style="color: #666666;">?</span> <span style="color: #19177c;">$month2</span> <span style="color: #666666;">:</span> <span style="color: #19177c;">$thismonth</span>) <span style="color: #666666;">.</span> <span style="color: #ba2121;">'-'</span> <span style="color: #666666;">.</span> (<span style="color: #19177c;">$day2</span> <span style="color: #666666;">?</span> <span style="color: #19177c;">$day2</span> <span style="color: #666666;">:</span> <span style="color: #19177c;">$thisday</span>), <span style="color: #19177c;">$tz</span>);
}
<span style="color: green; font-weight: bold;">else</span>
<span style="color: #19177c;">$to</span> <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">NULL</span>;
}
<span style="color: green; font-weight: bold;">catch</span> ( Exception <span style="color: #19177c;">$ex</span> )
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">false</span>; <span style="color: #408080; font-style: italic;">// data error, an arg is incorrect</span>
}
<span style="color: #19177c;">$dateformat</span> <span style="color: #666666;">=</span> (<span style="color: #19177c;">$year1</span> <span style="color: #666666;">?</span> <span style="color: #ba2121;">'Y'</span> <span style="color: #666666;">:</span> <span style="color: #ba2121;">''</span>) <span style="color: #666666;">.</span> (<span style="color: #19177c;">$month1</span> <span style="color: #666666;">?</span> <span style="color: #ba2121;">'m'</span> <span style="color: #666666;">:</span> <span style="color: #ba2121;">''</span>) <span style="color: #666666;">.</span> (<span style="color: #19177c;">$day1</span> <span style="color: #666666;">?</span> <span style="color: #ba2121;">'d'</span> <span style="color: #666666;">:</span> <span style="color: #ba2121;">''</span>);
<span style="color: #19177c;">$test</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$now</span><span style="color: #666666;">-></span><span style="color: #7d9029;">format</span>(<span style="color: #19177c;">$dateformat</span>);
<span style="color: #19177c;">$lhs</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$from</span><span style="color: #666666;">-></span><span style="color: #7d9029;">format</span>(<span style="color: #19177c;">$dateformat</span>);
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span><span style="color: #19177c;">$to</span> )
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: #19177c;">$test</span> <span style="color: #666666;">==</span> <span style="color: #19177c;">$lhs</span>;
}
<span style="color: #19177c;">$rhs</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$to</span><span style="color: #666666;">-></span><span style="color: #7d9029;">format</span>(<span style="color: #19177c;">$dateformat</span>);
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$rhs</span> <span style="color: #666666;"><</span> <span style="color: #19177c;">$lhs</span> <span style="color: #666666;">&&</span> <span style="color: #19177c;">$dateformat</span>{<span style="color: #666666;">0</span>} <span style="color: #666666;">!=</span> <span style="color: #ba2121;">'Y'</span> )
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: #19177c;">$test</span> <span style="color: #666666;">>=</span> <span style="color: #19177c;">$lhs</span> <span style="color: #666666;">||</span> <span style="color: #19177c;">$test</span> <span style="color: #666666;"><=</span> <span style="color: #19177c;">$rhs</span>;
}
<span style="color: green; font-weight: bold;">return</span> <span style="color: #19177c;">$test</span> <span style="color: #666666;">>=</span> <span style="color: #19177c;">$lhs</span> <span style="color: #666666;">&&</span> <span style="color: #19177c;">$test</span> <span style="color: #666666;"><=</span> <span style="color: #19177c;">$rhs</span>;
}
<span style="color: green; font-weight: bold;">static</span> <span style="color: green; font-weight: bold;">function</span> <span style="color: blue;">timeRange</span>(<span style="color: #19177c;">$hour1</span>, <span style="color: #19177c;">$min1</span><span style="color: #666666;">=</span><span style="color: green; font-weight: bold;">NULL</span>, <span style="color: #19177c;">$sec1</span><span style="color: #666666;">=</span><span style="color: green; font-weight: bold;">NULL</span>, <span style="color: #19177c;">$hour2</span><span style="color: #666666;">=</span><span style="color: green; font-weight: bold;">NULL</span>, <span style="color: #19177c;">$min2</span><span style="color: #666666;">=</span><span style="color: green; font-weight: bold;">NULL</span>, <span style="color: #19177c;">$sec2</span><span style="color: #666666;">=</span><span style="color: green; font-weight: bold;">NULL</span>, <span style="color: #19177c;">$gmt</span><span style="color: #666666;">=</span><span style="color: green; font-weight: bold;">NULL</span>)
{
<span style="color: #19177c;">$args</span> <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">array</span>(<span style="color: #19177c;">$hour1</span>, <span style="color: #19177c;">$min1</span>, <span style="color: #19177c;">$sec1</span>, <span style="color: #19177c;">$hour2</span>, <span style="color: #19177c;">$min2</span>, <span style="color: #19177c;">$sec2</span>, <span style="color: #19177c;">$gmt</span>);
<span style="color: green; font-weight: bold;">for</span> ( <span style="color: #19177c;">$ct</span><span style="color: #666666;">=0</span> ; <span style="color: #19177c;">$ct</span> <span style="color: #666666;"><</span> <span style="color: green;">count</span>(<span style="color: #19177c;">$args</span>) <span style="color: #666666;">&&</span> <span style="color: #19177c;">$args</span>[<span style="color: #19177c;">$ct</span>] <span style="color: #666666;">!==</span> <span style="color: green; font-weight: bold;">NULL</span> <span style="color: #666666;">&&</span> <span style="color: green;">strcasecmp</span>(<span style="color: #19177c;">$args</span>[<span style="color: #19177c;">$ct</span>], <span style="color: #ba2121;">'GMT'</span>) ; <span style="color: #666666;">++</span><span style="color: #19177c;">$ct</span> ) ;
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span><span style="color: #19177c;">$ct</span> )
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">false</span>; <span style="color: #408080; font-style: italic;">// arg error</span>
<span style="color: green; font-weight: bold;">if</span> ( (<span style="color: #19177c;">$gmt</span> <span style="color: #666666;">=</span> (<span style="color: green;">strcasecmp</span>(<span style="color: #19177c;">$args</span>[<span style="color: #19177c;">$ct</span>], <span style="color: #ba2121;">'GMT'</span>)<span style="color: #666666;">==0</span>)) )
{
<span style="color: #19177c;">$tzn</span> <span style="color: #666666;">=</span> <span style="color: #ba2121;">'UTC'</span>;
}
<span style="color: green; font-weight: bold;">else</span> <span style="color: green; font-weight: bold;">if</span> ( <span style="color: #666666;">!</span>(<span style="color: #19177c;">$tzn</span> <span style="color: #666666;">=</span> <span style="color: green;">ini_get</span>(<span style="color: #ba2121;">'date.timezone'</span>)) )
{
<span style="color: #19177c;">$tzn</span> <span style="color: #666666;">=</span> <span style="color: #ba2121;">'Europe/London'</span>;
}
<span style="color: #19177c;">$tz</span> <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">new</span> DateTimeZone(<span style="color: #19177c;">$tzn</span>);
<span style="color: #19177c;">$now</span> <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">new</span> DateTime(<span style="color: #ba2121;">'now'</span>,<span style="color: #19177c;">$tz</span>);
<span style="color: #408080; font-style: italic;">/* $ct is the number of args (excluding any 'GMT' value).</span>
<span style="color: #408080; font-style: italic;"> * if 6 args supplied, full time specified, else... */</span>
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$ct</span> <span style="color: #666666;">==</span> <span style="color: #666666;">4</span> ) <span style="color: #408080; font-style: italic;">// range of hours+minutes</span>
{
<span style="color: #408080; font-style: italic;">// hour and minute supplied</span>
<span style="color: #19177c;">$min2</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$hour2</span>;
<span style="color: #19177c;">$hour2</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$sec1</span>;
<span style="color: #19177c;">$sec1</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$sec2</span> <span style="color: #666666;">=</span> <span style="color: #666666;">0</span>;
<span style="color: #19177c;">$fmt</span> <span style="color: #666666;">=</span> <span style="color: #ba2121;">'Hi'</span>;
}
<span style="color: green; font-weight: bold;">else</span> <span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$ct</span> <span style="color: #666666;"><</span> <span style="color: #666666;">3</span> ) <span style="color: #408080; font-style: italic;">// range of hours (1 or 2)</span>
{
<span style="color: #19177c;">$hour2</span> <span style="color: #666666;">=</span> (<span style="color: #19177c;">$ct</span><span style="color: #666666;">==2</span> <span style="color: #666666;">?</span> <span style="color: #19177c;">$min1</span> <span style="color: #666666;">:</span> <span style="color: #19177c;">$hour1</span>);
<span style="color: #19177c;">$min1</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$sec1</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$min2</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$sec2</span> <span style="color: #666666;">=</span> <span style="color: #666666;">0</span>;
<span style="color: #19177c;">$fmt</span> <span style="color: #666666;">=</span> <span style="color: #ba2121;">'H'</span>;
}
<span style="color: green; font-weight: bold;">else</span> <span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$ct</span> <span style="color: #666666;">==</span> <span style="color: #666666;">6</span> )
{
<span style="color: #19177c;">$fmt</span> <span style="color: #666666;">=</span> <span style="color: #ba2121;">'His'</span>;
}
<span style="color: green; font-weight: bold;">else</span>
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">false</span>; <span style="color: #408080; font-style: italic;">// parameters not specified properly</span>
}
<span style="color: green; font-weight: bold;">try</span>
{
<span style="color: #19177c;">$lhs</span> <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">new</span> \DateTime( (<span style="color: #19177c;">$dt</span><span style="color: #666666;">=</span><span style="color: #19177c;">$now</span><span style="color: #666666;">-></span><span style="color: #7d9029;">format</span>(<span style="color: #ba2121;">'Y-m-d'</span>)) <span style="color: #666666;">.</span> <span style="color: #ba2121;">' '</span> <span style="color: #666666;">.</span> (int)<span style="color: #19177c;">$hour1</span> <span style="color: #666666;">.</span> <span style="color: #ba2121;">':'</span> <span style="color: #666666;">.</span> (int)<span style="color: #19177c;">$min1</span> <span style="color: #666666;">.</span> <span style="color: #ba2121;">':'</span> <span style="color: #666666;">.</span> <span style="color: #19177c;">$sec1</span>, <span style="color: #19177c;">$tz</span>);
<span style="color: #19177c;">$rhs</span> <span style="color: #666666;">=</span> (( <span style="color: #19177c;">$ct</span> <span style="color: #666666;">==</span> <span style="color: #666666;">1</span> ) <span style="color: #666666;">?</span> <span style="color: green; font-weight: bold;">NULL</span> <span style="color: #666666;">:</span> <span style="color: green; font-weight: bold;">new</span> \DateTime( <span style="color: #19177c;">$dt</span> <span style="color: #666666;">.</span> <span style="color: #ba2121;">' '</span> <span style="color: #666666;">.</span> (int)<span style="color: #19177c;">$hour2</span> <span style="color: #666666;">.</span> <span style="color: #ba2121;">':'</span> <span style="color: #666666;">.</span> (int)<span style="color: #19177c;">$min2</span> <span style="color: #666666;">.</span> <span style="color: #ba2121;">':'</span> <span style="color: #666666;">.</span> <span style="color: #19177c;">$sec2</span>, <span style="color: #19177c;">$tz</span>));
}
<span style="color: green; font-weight: bold;">catch</span> ( Exception <span style="color: #19177c;">$ex</span> )
{
<span style="color: #408080; font-style: italic;">// datetime not recognised, so parameter probably out of range</span>
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">false</span>;
}
<span style="color: #19177c;">$nowtime</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$now</span><span style="color: #666666;">-></span><span style="color: #7d9029;">format</span>(<span style="color: #19177c;">$fmt</span>);
<span style="color: #19177c;">$ltime</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$lhs</span><span style="color: #666666;">-></span><span style="color: #7d9029;">format</span>(<span style="color: #19177c;">$fmt</span>);
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$rhs</span> <span style="color: #666666;">===</span> <span style="color: green; font-weight: bold;">NULL</span> )
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: #19177c;">$nowtime</span> <span style="color: #666666;">==</span> <span style="color: #19177c;">$ltime</span>;
}
<span style="color: #19177c;">$rtime</span> <span style="color: #666666;">=</span> <span style="color: #19177c;">$rhs</span><span style="color: #666666;">-></span><span style="color: #7d9029;">format</span>(<span style="color: #19177c;">$fmt</span>);
<span style="color: green; font-weight: bold;">if</span> ( <span style="color: #19177c;">$ltime</span> <span style="color: #666666;">></span> <span style="color: #19177c;">$rtime</span> )
{
<span style="color: green; font-weight: bold;">return</span> <span style="color: #19177c;">$nowtime</span> <span style="color: #666666;">>=</span> <span style="color: #19177c;">$ltime</span> <span style="color: #666666;">||</span> <span style="color: #19177c;">$nowtime</span> <span style="color: #666666;"><=</span> <span style="color: #19177c;">$rtime</span>;
}
<span style="color: green; font-weight: bold;">return</span> <span style="color: #19177c;">$nowtime</span> <span style="color: #666666;">>=</span> <span style="color: #19177c;">$ltime</span> <span style="color: #666666;">&&</span> <span style="color: #19177c;">$nowtime</span> <span style="color: #666666;"><=</span> <span style="color: #19177c;">$rtime</span>;
}
}
</pre>
<pre style="line-height: 1.2em; margin: 0px; width: 1200px;"><span style="color: #bc7a00;">?></span></pre>
</div>
Robhttp://www.blogger.com/profile/06996218238493777139noreply@blogger.com2tag:blogger.com,1999:blog-1000869619866957301.post-15945240979987632092012-06-13T00:56:00.001+01:002012-06-15T11:22:27.105+01:00Javascript Return StatementAfter 20+ years of programming I learnt something new today, while messing about with the <a href="http://robsphp.blogspot.co.uk/2012/06/javascript-in-php-on-windows-with.html">php_spidermonkey extension</a>.<br />
<br />
If you place a carriage return after a 'return' statement in Javascript, and put some code on the line underneath, the function returns 'undefined', as if the statement was simply: <code style="color: blue;">return; </code><br />
<br />
Here's an example of a function that returns 'undefined':<br />
<div style="background: #f8f8f8; background: white; border: solid gray 1px; color: black; margin: 0.5em; overflow: auto; padding: .5em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: green; font-weight: bold;">function</span> whatelse()
{
<span style="color: green; font-weight: bold;">var</span> ok <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">false</span>;
<span style="color: green; font-weight: bold;">return</span> <span style="color: #408080; font-style: italic;">// function ends here</span>
ok <span style="color: #666666;">?</span> <span style="color: #ba2121;">'OK (true)'</span> <span style="color: #666666;">:</span> <span style="color: #ba2121;">'OK (false)'</span>; <span style="color: #408080; font-style: italic;">// this line is never executed!</span>
}
</pre>
</div>Robhttp://www.blogger.com/profile/06996218238493777139noreply@blogger.com0tag:blogger.com,1999:blog-1000869619866957301.post-639150396690856872012-06-13T00:31:00.001+01:002015-06-05T20:47:55.832+01:00Javascript in PHP on Windows with php_spidermonkey<div>
<i><b>Update 19 June 2014</b> - the latest builds of php_spidermonkey for Windows are contained in this zip: <a href="https://onedrive.live.com/redir?resid=669EE24817961774%21685" style="font-family: sans-serif;" target="_blank">all_php_builds.zip</a>. I reported a few days ago that the PHP 5.5 TS build did not work - as of 19 June 2014 it works. PHP 5.5 includes 64-bit versions in the x64 folder of the zip file.</i></div>
<div>
<br /></div>
I recently had a problem to solve: run some Javascript from inside PHP on a web server.<br />
<br />
I needed to evaluate a web browser's PAC (proxy auto config) file exactly the way a browser would, by executing the Javascript function FindProxyForURL (<a href="http://findproxyforurl.com/pac-functions/" target="_blank" title="FindProxyFroURL PAC information">see here for a good guide</a>). The browser uses its Javascript engine, so to simulate it, I wanted to run Javascript inside PHP (<a href="http://robsphp.blogspot.co.uk/2012/06/running-browsers-findproxyforurl.html">an example is here in another post</a>). <br />
<br />
<a name='more'></a><br />
<br />
On the web there were some solutions for Javascript engines, written in PHP, such as J4P5 (<a href="http://j4p5.sourceforge.net/" target="_blank" title="J4P5 Javascript solution">http://j4p5.sourceforge.net/</a>) and PHPJS (<a href="http://include-once.org/p/phpjs/" target="_blank" title="phpjs Javascript solution">http://include-once.org/p/phpjs/</a>); the problem is that these solutions, however impressive, are not the real thing. There will be nagging worries about reliability and speed.<br />
<br />
So then I looked at Mozilla and wondered if their Spidermonkey engine could be used. I didn't have to look much further to see that there was already a fantastic PHP PECL package written by Christophe Robin that wraps the Mozilla Spidermonkey engine. Theoretically a perfect solution, giving speed, efficiency and reliablity - the only hurdle was to get it built on Windows - it wasn't a piece of cake, but worth the effort, and nothing compared to the effort of the programmers who wrote the code.<br />
<br />
I am posting the binaries here so that others can get up and running instantly. The instructions on how to build these follow:<br />
<br />
<a href="https://www.blogger.com/null" name="more"></a><br />
<ul>
<li><b><span style="font-size: large;">Download</span>:</b></li>
<ul>
<li><a href="https://onedrive.live.com/redir?resid=669EE24817961774%21685" style="font-family: sans-serif;" target="_blank"><b>all_php_builds.zip</b></a> contains php_spidermonkey.dll for Windows, for PHP 5.5 (32 and 64-bit), 5.4 and 5.3.<br /> </li>
</ul>
<li>Useful links:</li>
<ul>
<li><a href="http://devzone.zend.com/1480/using-javascript-in-php-with-pecl-and-spidermonkey/" target="_blank" title="Zend DevZone - php_spidermonkey help">Zend DevZone notes on how to use php_spidermonkey</a></li>
<li><a href="http://pecl.php.net/package/spidermonkey/1.0.0" target="_blank" title="PECL Spidermonkey">Spidermonkey PECL extension page </a>
<a href="https://github.com/christopherobin/php-spidermonkey" target="_blank" title="php_spidermonkey source">(Github)</a></li>
<li><a href="https://developer.mozilla.org/En/SpiderMonkey" target="_blank" title="Mozilla Spidermonkey">Mozilla Spidermonkey Javascript engine</a></li>
</ul>
</ul>
<div style="padding: 1em; text-align: left;">
</div>
Steps to build - you don't need to build anything, the dll's for Windows are above - these are to help me remember what to do next time around, and maybe will be useful to someone:<br />
<ol>
<li>For builds to do with PHP, I use this VC9 SDK from Microsoft: <a href="http://www.microsoft.com/en-us/download/details.aspx?id=3138" target="_blank" title="Windows 7 .Net 3.5 (VC9) platform SDK">http://www.microsoft.com/en-us/download/details.aspx?id=3138</a></li>
<li>Get and build Spidermonkey 1.8.5 from here: <a href="https://developer.mozilla.org/En/SpiderMonkey/Build_Documentation" target="_blank" title="Mozilla Spidermonkey Instructions">https://developer.mozilla.org/En/SpiderMonkey/Build_Documentation</a>.
<ol>
<li>Get the source code - near the top of the page is a link to it, or you can get it from here: <a href="http://ftp.mozilla.org/pub/mozilla.org/js/" target="_blank" title="Spidermonkey source code">http://ftp.mozilla.org/pub/mozilla.org/js/</a>.</li>
<li>Get the build tools for Windows - near the top of the page is a list of platforms - click on Windows, then about half way down the page is a link to the Mozilla build tools entitled "<a href="http://ftp.mozilla.org/pub/mozilla.org/mozilla/libraries/win32/MozillaBuildSetup-Latest.exe" rel="external" target="_blank" title="http://ftp.mozilla.org/pub/mozilla.org/mozilla/libraries/win32/MozillaBuildSetup-Latest.exe">Download the current MozillaBuild package</a>".</li>
<li>Once the build tools are installed, run "<code>start-msvc9.bat</code>", as documented on the Windows page. It opens a Mozilla command window (a unix bash simulation).</li>
<li>Go back to the <a href="https://developer.mozilla.org/En/SpiderMonkey/Build_Documentation" target="_blank" title="Build documentation">Build Documentation</a> and follow the instructions for "Easy Build".</li>
</ol>
I had no problems building, everything worked perfectly and I ended up with a static JS library, a DLL and js.exe. I chose to use the static library to build php_spidermonkey so that it could be installed as a stand-alone extension in PHP, with no other dependencies.</li>
<li>Building php_spidermonkey is a bit more tricky. Get the original source code here: <a href="http://pecl.php.net/package/spidermonkey/1.0.0" target="_blank" title="PECL php_spidermonkey page">http://pecl.php.net/package/spidermonkey/1.0.0</a>. It does not build on Windows VC9 at all without adjustment, but the code is sound - it's just minor technicalities and looks worse than it is.You need a working PHP build environment to build the extension. This won't make any sense unless you know how to build PHP extensions - these instructions work with my modified source code...:<br /><br />
<ol>
<li>Create a spidermonkey folder in php extensions, and copy this (modified) source code into it: <a href="http://sdrv.ms/K38jzT" target="_blank" title="php_spidermonkey source">php_spidermonkey source</a> (submitted to https://github.com/christopherobin/php-spidermonkey)</li>
<li>Get the recently built javascript static library "js_static.lib" located in "js\src\dist\lib", copy it to PHP build environment folder "php54dev\vc9\x86\deps\include\lib", and rename it "<b>js32.lib</b>"</li>
<li>Create sub-folder "php54dev\vc9\x86\deps\include\<b>js</b>", and copy all of the .h files from the spidermonkey api folder "js\src" into it.</li>
<li>Add "--with-spidermonkey=shared" to your PHP configure command.</li>
<li>Build PHP.</li>
</ol>
</li>
</ol>
Robhttp://www.blogger.com/profile/06996218238493777139noreply@blogger.com9tag:blogger.com,1999:blog-1000869619866957301.post-23666716629558825822012-06-13T00:09:00.002+01:002014-08-30T04:11:01.915+01:00Unofficial Microsoft SQL Server Driver for PHP (sqlsrv)Here are unofficial modified builds of Microsoft's sqlsrv and pdo_sqlsrv drivers for PHP, version 3.0, for:<br />
<br />
<ul>
<li>PHP 5.6 for Windows Vista/2008 and above (32 and 64 bit) </li>
<li>PHP 5.5 for Windows Vista/2008 and above (32 and 64 bit) </li>
<li>PHP 5.3 and 5.4 for Windows XP/2003 and above (32 bit)<br />Microsoft's version 3.0 driver does not run on XP/2003 because it has a hard-coded dependency on the SQL Server 2012 Native Client (ODBC driver), which will not install on XP. To fix the problem I just had to allow the PHP driver to use an older version of the ODBC driver supported by XP.</li>
</ul>
<br />
<br />
<a name='more'></a><br />
<h3>
Downloads</h3>
<ul>
<li><a href="https://onedrive.live.com/redir?resid=669EE24817961774%21720" target="_blank"><b>php_sqlsrv_3.0.2.2 for PHP 5.6, 5.5, 5.4 and 5.3</b></a> (19 June 2014, PHP 5.6 added 30 August 2014)<br />64 bit builds are in the zip folder '<b>x64</b>'.</li>
</ul>
<ul>
<li>Previous versions are in a folder on my OneDrive: <a href="https://onedrive.live.com/redir?resid=669EE24817961774%21687" target="_blank">Previous Versions</a></li>
</ul>
<ul>
</ul>
<br />
The latest drivers are exactly the same as Microsoft's, except:<br />
<ol>
<li>All known memory leak and memory exception bugs have been fixed *</li>
<li>Column names can contain unicode characters</li>
<li>The newest of any of these ODBC drivers is selected, depending on what's installed:</li>
<ol>
<li>SQL Server Native Client 12.0 <i>(hasn't been invented as of June 2014!)</i></li>
<li>SQL Server Native Client 11.0 <i>(SQL Server 2012 ODBC driver)</i></li>
<li>SQL Server Native Client 10.0 <i>(SQL Server 2008/r2 ODBC driver)</i></li>
<li>SQL Native Client <i>(SQL Server 2005 ODBC driver)</i></li>
</ol>
<li>64 bit versions included for PHP 5.5 and 5.6</li>
<ol>
</ol>
</ol>
<br />
<br />
Builds for PHP 5.3 and PHP 5.4 work on XP and above.<br />
<br />
Builds for PHP 5.5+ only work on Windows Vista/2008 and above - PHP 5.5+ is built with the 2012 Visual C++ 11 compiler and it generates code that works on Windows Vista and above. I am comfortable with that - XP has been superseded a long time ago, Windows 8.1 is absolutely great, and don't forget that upgrading Windows also upgrades to a far better web server in IIS 7.5 or 8.5.<br />
<div>
<br />
It's never a good idea to look at the source code of things you like - since I had to look, my new list of things to do includes writing a new version of this driver. It is a wrapper around the ODBC API, and has no business making strict dependencies on any version of the SQL Server Native Client. It should even work with SQL Server 6, because there is an ODBC driver for that too. Furthermore, it's a pretty ugly bit of code and every update is likely to introduce new bugs. It's still a better choice than using the PHP ODBC driver, as it's much faster in returning data, it is multilingual (nchar and nvarchar), it supports the majority of data types, and includes parameters.</div>
<br />
<span style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 12px; line-height: 16.940000534057617px;">* Sqlsrv changes</span><br />
<span style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 12px; line-height: 16.940000534057617px;">The sqlsrv driver has a fix to a bug found by Charles Durrant (</span><a href="http://sqlsrvphp.codeplex.com/workitem/22427" style="background-color: white; color: #888888; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 12px; line-height: 16.940000534057617px; text-decoration: none;">http://sqlsrvphp.codeplex.com/workitem/22427</a><span style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 12px; line-height: 16.940000534057617px;">) which I too recently experienced when running PHP-32 on a 64 bit server. I noted Charles's post when he first reported the bug, but tried my best to avoid working on the source code until I had to... Charles saved me a huge amount of time, so thank you to him. My fix is different, I have repaired all of Microsoft's dodgy auto pointers and completely rewritten the horrible function that caused the crash. If anyone uses these new builds and spots memory leaks or bugs due to these changes, please let me know and I'll fix them within a day or two.</span><br />
<br />
Microsoft's official downloads are here:<br />
<ul>
<li><a href="http://www.microsoft.com/en-us/download/details.aspx?id=20098">http://www.microsoft.com/en-us/download/details.aspx?id=20098</a></li>
</ul>
<br />
SQL Server Native Clients are here:<br />
<ul>
<li>SQL 2012<br /> <a href="http://www.microsoft.com/download/en/details.aspx?id=29065">http://www.microsoft.com/download/en/details.aspx?id=29065</a></li>
<li>SQL 2008 R2<br /> <a href="http://www.microsoft.com/download/en/details.aspx?id=16978">http://www.microsoft.com/download/en/details.aspx?id=16978</a></li>
<li>SQL 2005<br /> <a href="http://www.microsoft.com/download/en/details.aspx?id=20101">http://www.microsoft.com/download/en/details.aspx?id=20101</a> </li>
</ul>
Robhttp://www.blogger.com/profile/06996218238493777139noreply@blogger.com123