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

<channel>
	<title>Chapter 42 &#187; php</title>
	<atom:link href="http://chapter42.whitewhale.net/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://chapter42.whitewhale.net</link>
	<description></description>
	<lastBuildDate>Thu, 30 Sep 2010 17:20:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Converting to Title Case</title>
		<link>http://chapter42.whitewhale.net/2008/10/28/converting-to-title-case/</link>
		<comments>http://chapter42.whitewhale.net/2008/10/28/converting-to-title-case/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 19:10:00 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Behind the scenes]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[livewhale]]></category>
		<category><![CDATA[lwblog]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Title Case]]></category>
		<category><![CDATA[whitewhale]]></category>

		<guid isPermaLink="false">http://chapter42.whaleblogs.net/?p=122</guid>
		<description><![CDATA[In the process of developing and refining our CMS, the question occasionally arises whether or not we should convert text inputs from format X to format Y.  These questions range from the innocuous and straightforward (should we convert curly quotes to straight quotes?  or vice versa?) to the more insidious (should we correct a misspelling?  should [...]]]></description>
			<content:encoded><![CDATA[<p>In the process of developing and refining <a title="LiveWhale" href="http://www.livewhale.com" target="_blank"><strong>our CMS</strong></a>, the question occasionally arises whether or not we should convert text inputs from format X to format Y.  These questions range from the innocuous and straightforward (should we convert curly quotes to straight quotes?  or vice versa?) to the more insidious (should we correct a misspelling?  should we move close quotes to outside a period?).  </p>
<p>On the one hand, it&#8217;s probably best to let users make mistakes, or format as they wish, and depend on human communication to clear things up— instead of trying to build in a bunch of extra structure designed to cover for mistakes.  It&#8217;s <a title="White Whale CMS conversation" href="http://www.whitewhale.net/content/cms.php" target="_blank">our view</a> that the latter approach leads to bloated, overbuilt CMS systems that discourage accountability.  But at the same time, consistency and coherence of communication across a Web site is a really, really important thing— it is what distinguishes sites that serve as good vehicles for an institution&#8217;s messaging from sites that are just decorated Web pages.</p>
<p>We&#8217;re thinking about this right now because we are considering automatically converting the titles of news items in LiveWhale to Title Case.  The following three news headlines, though identical in content, send very different messages:</p>
<p>1.  <strong>White Whale Web Services to Release New Content Management System, Revolutionize CMS Industry<br />
 </strong></p>
<p>2.  <strong>White Whale Web Services to release new content management system, revolutionize CMS industry<br />
 </strong></p>
<p>3.  <strong>WHITE WHALE WEB SERVICES TO RELEASE NEW CONTENT MANAGEMENT SYSTEM, REVOLUTIONIZE CMS INDUSTRY<br />
 </strong></p>
<p>Although there might be a justification for #3 in a particular Web design, it&#8217;s clear that you wouldn&#8217;t want a user to enter news headlines that way.  For one thing, caps are just hard to read in many contexts; but on top of that, it&#8217;s always easy to {text-transform:uppercase} if you need caps.  My general preference as a design snob would be for #2, but that&#8217;s clearly not common practice— as much as I enjoy headlines like <a href="http://www.timesonline.co.uk/tol/news/world/us_and_americas/us_elections/article5031835.ece" target="_blank">this</a> (or <a title="Why smaller is better" href="http://www.whitewhale.net/content/small.php" target="_blank">this</a>), that style is much more common in European news than American.</p>
<p>It&#8217;s also the case that styles #1 and #2 look terrible next to each other:</p>
<ul>
<li><strong>White Whale Web Services to Release New Content Management System, Revolutionize CMS Industry</strong></li>
<li><strong>Other CMS providers cower in fear</strong></li>
</ul>
<p>So.  Should we let users do what they will, and enter headlines according to any system they prefer?  Or should we legislate something?  It seems pretty clear from the above examples that— at least in the particular case of news headlines— legislating is the way to go.  And if you&#8217;re going to require a particular format for headlines, it seems pretty clear that Title Case Is Your Only Option.</p>
<p>So what&#8217;s the best way to do it?</p>
<p><span id="more-122"></span>You could, for example, simply ask the user to format the headline a particular way:</p>
<p>___________________________________________________<br />
<em><span style="color: #999999;">Please use Title Case for headlines.</span></em></p>
<p>Or, a bit more diplomatically, you could provide an example:</p>
<p>___________________________________________________<br />
<em><span style="color: #999999;">Example: Stock Market Soars on Rumors of Bailout</span></em></p>
<div>Either of those would be likely to get you good results a majority of the time. But this approach has its drawbacks.  Obviously, it doesn&#8217;t ensure that you&#8217;ll get the results you want; some people (including me, probably) would tend to ignore instructions of this sort and enter headlines the way we want to.  And in a more philosophical sense, it requires exposing a behind-the-scenes decision in the help text of the interface, which I don&#8217;t like&#8230; help text ought to help you fill out the form, not preach about how your content should be styled.  Ideally those decisions should be invisible, unless they&#8217;re important enough to require input from the user.  In other words— <em>we shouldn&#8217;t have to provide instructions about using Title Case unless we felt users might have legitimate reasons not to. </em>  Otherwise it looks like a limitation of the CMS.</div>
<p>Alternatively, we could let users enter any headlines they want, and control the front end display with CSS&#8217;s <a title="text-transform property" href="http://www.w3schools.com/Css/pr_text_text-transform.asp" target="_blank">text-transform property</a>.  But that&#8217;s not going to work either, because these are the only options we have:</p>
<table style="border-collapse:collapse;" border="1" cellspacing="0" cellpadding="4">
<tbody>
<tr>
<th align="left" valign="top">Value</th>
<th align="left" valign="top">Description</th>
</tr>
<tr>
<td valign="top">none</td>
<td valign="top">Default. Defines normal text, with lower case letters and capital letters</td>
</tr>
<tr>
<td valign="top">capitalize</td>
<td valign="top">Each word in a text starts with a capital letter</td>
</tr>
<tr>
<td valign="top">uppercase</td>
<td valign="top">Defines only capital letters</td>
</tr>
<tr>
<td valign="top">lowercase</td>
<td valign="top">Defines no capital letters, only lower case letters</td>
</tr>
</tbody>
</table>
<p>Title Case Is Great, But Not If You Capitalize Every Word In The Sentence.  Otherwise It Looks Hacky.  The text-transform property doesn&#8217;t have the kind of sensitivity you&#8217;d want in a good Title Case headline.  I am personally big on punctuation and capitalization styles that reflect spoken rhythms— that&#8217;s why, no matter what Douglas says, I always put a space after an em dash— and capitalizing &#8220;the,&#8221; &#8220;and,&#8221; etc. ruins the spoken rhythm of a headline.</p>
<p>So the only approach left— assuming you want to engage this issue at all!— is to automatically convert headlines into Title Case on the back end.  And that&#8217;s the whole reason I started this post in the first place.</p>
<p>It turns out that converting to Title Case is nowhere near as simple as you&#8217;d imagine.  Sure, we know that you don&#8217;t capitalize &#8220;the&#8221; or &#8220;a&#8221; or prepositions, etc.  But how about sentences like these?</p>
<p><strong>Read markdown_rules.txt to Find Out How _Underscores Around Words_ Will Be Interpreted</strong></p>
<p><strong>Notes and Observations Regarding Apple’s Announcements From ‘The Beat Goes On’ Special Event<br />
</strong></p>
<p>These sentences are from <a title="Title Case test suite" href="http://individed.com/code/to-title-case/tests.html" target="_blank">a suite of test cases</a> for a <a title="Daring Fireball: Title Case" href="http://daringfireball.net/2008/08/title_case_update" target="_blank">Title Case script</a> originally created by John Gruber of <a title="Daring Fireball" href="http://daringfireball.net/" target="_blank">Daring Fireball</a>.  We are planning to use <a title="Convert to Title Case (PHP)" href="http://files.nanovivid.com/wordpress/title-case.php" target="_blank">the PHP version of the script</a> to convert some of Southwestern&#8217;s old news headlines (originally entered with uppercase headlines) to a thoughtful Title Case, and are considering using it when headlines are created in LiveWhale in the first place.  In the process we discovered yet another odd, fascinating corner of the world of CMS development to geek out in.</p>
]]></content:encoded>
			<wfw:commentRss>http://chapter42.whitewhale.net/2008/10/28/converting-to-title-case/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Optimization in PHP</title>
		<link>http://chapter42.whitewhale.net/2008/08/20/optimization-in-php/</link>
		<comments>http://chapter42.whitewhale.net/2008/08/20/optimization-in-php/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 20:10:07 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Behind the scenes]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[cms]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[livewhale]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[speed]]></category>
		<category><![CDATA[whitewhale]]></category>

		<guid isPermaLink="false">http://chapter42.whaleblogs.net/?p=94</guid>
		<description><![CDATA[Optimization is one of the most enjoyable parts of software design, but unfortunately it does not claim a high percentage of development time. Generally speaking, it is not a task to consider until the time spent is justified, which is often toward the end of the development cycle (but not always!) Still, it is an [...]]]></description>
			<content:encoded><![CDATA[<p>Optimization is one of the most enjoyable parts of software design, but unfortunately it does not claim a high percentage of development time. Generally speaking, it is not a task to consider until the time spent is justified, which is often toward the end of the development cycle (but not always!) Still, it is an important step, especially with products like LiveWhale, which has to perform well under high traffic spikes. I&#8217;ve already talked about general page caching before, but fine-tuning a PHP application for speed when something is not cached is also important. Here are some thoughts on how to do just that.</p>
<p>At the code level, LiveWhale is a framework, which means the same codebase is hit for many different types of requests. The question is then: how to achieve high performance with a codebase that has to perform so many tasks and is therefore code heavy. It makes sense to divide code across a handful of files. The objective here is to only load libraries when you need them. A typical request will only use a tiny percentage of the entire codebase, so there&#8217;s no need to read a great deal of code from the filesystem and eat up RAM per PHP request. Also, with a modular system like LiveWhale, it is not explicitly known what modules exist that will need to be loaded. An important optimization is one where only the first request to the server has to perform logic to determine what to load. The results of this expensive operation are cached, and all subsequent LiveWhale requests enjoy dramatic savings in the module loader.<span id="more-94"></span></p>
<p>A common problem with application frameworks is that they perform two very expensive tasks with every single page load: 1) initializing a session and 2) connecting to a database. Neither of these two should be assumed. In LiveWhale, a session is started only when a component requires it. A database connection only occurs when/if the first query to it is performed. Making your application sufficiently &#8220;smart&#8221; about if and when these initializations take place will also lead to dramatic speed improvements.</p>
<p>Less is more. That means you should optimize out any hit to the file system that you can, or remove extraneous database queries. Also, be wary of OO (object oriented) slowdowns. If something doesn&#8217;t need to be a class, don&#8217;t use one. Remember that calls to an object&#8217;s method are more expensive than global function calls. The same holds true for accessing an object&#8217;s properties. An obvious optimization is done when dealing with data from a foreign server. Such content should be cached as much as possible, so that rapid hits to your site don&#8217;t rely on the performance (or availability) or an external data source. Another tip would be to use error suppression (@) sparingly. This is a known performance hit. There are a number of similar, simple tips available via a quick Google search for &#8220;php optimizations&#8221;, and there are slideshows available from PHP developers on the subject.</p>
<p>Analyze how your code operates. If you have a function that uses a lot of logic (if/then, switch), make sure you understand what the &#8220;common case&#8221; is. In other words, what&#8217;s the most likely condition that function will come across? Then determine if the function&#8217;s logic is conducive to the fastest possible result under the most common case. For example, I had a function that has to intercept a variety of XML structures and handle them in different ways. One particular XML structure (the simplest possible one) was far and away the most common case. Rather than parsing the XML with simplexml_load_string() as I do with all the chunks of XML I receive, I first do a preg_match() to determine if I have the common case. In this situation, I can immediately return a simple result and skip both the parsing of the XML and the lengthy logic that goes with performing operations with it.</p>
<p>Identify &#8220;hot&#8221; functions. Functions that are called many times, often within a loop, should get the most attention in terms of optimization. First, make sure the function should be called at all, as opposed to using inline code, otherwise you needlessly acquire function overhead. Second, move any redundant logic outside of the hot function. Perhaps the check can be performed just once before the hot code is executed. There are also situations where a function can potentially be called repeatedly and end up with the same result. It might make sense to declare a static variable inside the function, to locally cache content generated by the function. On a subsequent call, it can first check the static array to see if a result already exists, and simply return that if so. This sort of fine-grained optimization will only help when there is a significant bottleneck with a particular function, but it can make a huge difference if done wisely.</p>
<p>Lastly, use <a href="http://www.xdebug.org/" target="_blank">XDebug</a>! XDebug is your friend. Among the many things it can do, it will let you profile your PHP code and generate a report about how much time your script is taking overall and within particular functions. Here&#8217;s a mockup of the type of information it provides:</p>
<table border="1" cellspacing="0" cellpadding="4">
<tbody>
<tr>
<td>Function</td>
<td>Line(s)</td>
<td>Calls</td>
<td>Cycles</td>
<td>Time</td>
</tr>
<tr>
<td>LiveWhale-&gt;foo</td>
<td>128</td>
<td>1</td>
<td>1911</td>
<td>38.3%</td>
</tr>
<tr>
<td>LiveWhale-&gt;bar</td>
<td>40</td>
<td>1</td>
<td>854</td>
<td>17.1%</td>
</tr>
<tr>
<td>do_something</td>
<td>41</td>
<td>1</td>
<td>313</td>
<td>6.3%</td>
</tr>
<tr>
<td>do_something_else</td>
<td>218/218/218/218/218/218/218/21 &#8230;</td>
<td>15</td>
<td>252 (17)</td>
<td>5.1%</td>
</tr>
<tr>
<td>do_this</td>
<td>131</td>
<td>1</td>
<td>118</td>
<td>2.4%</td>
</tr>
<tr>
<td>do_that</td>
<td>1</td>
<td>1</td>
<td>72</td>
<td>1.4%</td>
</tr>
<tr>
<td>fast</td>
<td>886</td>
<td>1</td>
<td>58</td>
<td>1.2%</td>
</tr>
<tr>
<td>faster</td>
<td>124</td>
<td>1</td>
<td>47</td>
<td>0.9%</td>
</tr>
</tbody>
</table>
<p>In this example, the function LiveWhale-&gt;foo() clearly takes the majority of the time for this request. At 38.3% of request time for a single call, it is a good candidate for serious analysis. LiveWhale-&gt;bar() is also expensive compared to other operations, and there may be ways to make this function perform better too. The function do_something() takes 6.3% for a single call, on line 41. Another function, do_something_else(), takes nearly the same amount of time but due to the fact that it was called 15 times (at 17 CPU cycles each, so 252 cycles total). Knowing why each function takes the time that it does, and why it might be called so many times, will help you maximize the performance of your application.</p>
<p>Familiarity with what makes PHP code perform well will only lead to the best decisions being made early on, so that XDebug reveals fewer inefficiencies. In the long run, many of these tips become automatic and part of your coding style, but for the best quality, production ready code, a little bit of analysis and thought goes a long way.</p>
]]></content:encoded>
			<wfw:commentRss>http://chapter42.whitewhale.net/2008/08/20/optimization-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Open Source &amp; PHP</title>
		<link>http://chapter42.whitewhale.net/2008/07/30/open-source-php/</link>
		<comments>http://chapter42.whitewhale.net/2008/07/30/open-source-php/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 19:15:57 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Behind the scenes]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://chapter42.whaleblogs.net/?p=77</guid>
		<description><![CDATA[It&#8217;s hard to imagine how this industry would operate without open source software. The PHP language that I use to write software for the web is itself free software. Unfortunately, the quality of open source code written with PHP is relatively poor and inflexible compared to the quality of open source libraries that can be [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s hard to imagine how this industry would operate without open source software. The PHP language that I use to write software for the web is itself free software. Unfortunately, the quality of open source code written with PHP is relatively poor and inflexible compared to the quality of open source libraries that can be compiled into PHP as extensions. Consequentially my personal relationship to open source software is a hybrid scenario of, on the one hand, being deeply invested in open source tools that expand the language I program in, as well as having a commitment to turning out open source projects of my own to the community, and on the other hand, a healthy skepticism toward the bulk of open source software written in PHP. Part of this is due to the fact that, being the chief server side programming language of the web, freely available PHP code could well have been written by your grandmother (apologies to my GM, who happens to be a very savvy user).<span id="more-77"></span></p>
<p>I recently looked at five versions of a free class that provided some relatively trivial functionality that I wanted to include in our CMS, but didn&#8217;t want to spend too much time with. Some of them felt like they were the size of our entire core CMS library! I found plenty of frills that we don&#8217;t need (bloat which arises when a developer wants to pack their project with features to make it more than it needs to be), inefficient repetition of constants, and long procedures meant to do something that can be done in a few lines of smarter code (often meaning the developer missed some of the basics of the language). The fact that there&#8217;s a lot of garbage out there doesn&#8217;t imply that there&#8217;s nothing to gain by sharing open source code. In this case I had to study the scripts that I saw and develop my own (small) derivative, but if you are selective and critical when choosing to go with packaged code, there&#8217;s a lot of milage you can get out of other developers&#8217; work. Fortunately, projects like PEAR (<a href="http://pear.php.net/">http://pear.php.net/</a>) help to screen and collect useful code libraries and facilitate their distribution. Many other code archives just don&#8217;t fare so well.</p>
<p>I find that good PHP code is relatively fast and easy to write for any situation, because PHP comes with a wide range of libraries that automate most desired tasks, and the APIs are usually not very verbose. The places where I think open source software really becomes a feather in PHP&#8217;s cap are the multitude of extensions developed for the language as distinct open source projects (whether originally created for use within PHP or not). They are endless, and many of them are perfectly critical to writing a good web application. Nevertheless, there are areas where open sourced PHP code is also useful in the industry. I have felt that templating and caching are good examples of this, and created two open source projects for that. I aim to release another one that is designed to produce a powerful XML toolkit by adding a thin API on top of PHP&#8217;s XML foundation that vastly reduces the complexity of XML-generating code, making it more accessible to programmers at all goals and skill levels.</p>
<p>Some of the tools we have developed in-house and make freely available to whomever wants to experiment with them are available at <a href="http://technologies.whitewhale.net">http://technologies.whitewhale.net</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://chapter42.whitewhale.net/2008/07/30/open-source-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing XPHP</title>
		<link>http://chapter42.whitewhale.net/2008/06/30/introducing-xphp/</link>
		<comments>http://chapter42.whitewhale.net/2008/06/30/introducing-xphp/#comments</comments>
		<pubDate>Mon, 30 Jun 2008 19:23:27 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Behind the scenes]]></category>
		<category><![CDATA[Progressive Ideologies]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://chapter42.whaleblogs.net/?p=57</guid>
		<description><![CDATA[After four years of using XPHP successfully in a wide variety of applications for White Whale, we have released it to the public!
What is XPHP?
XPHP is a free class for PHP which allows for the embedding of dynamic content (web application variables, function output, etc.) in a web page, using a pure XML-based syntax. It [...]]]></description>
			<content:encoded><![CDATA[<p>After four years of using <a href="http://technologies.whitewhale.net/xphp/">XPHP</a> successfully in a wide variety of applications for White Whale, we have released it to the public!</p>
<p><strong>What is XPHP?</strong></p>
<p>XPHP is a free class for PHP which allows for the embedding of dynamic content (web application variables, function output, etc.) in a web page, using a pure XML-based syntax. It is designed to quickly bring application functionality to your web pages, while avoiding the complexity and overhead of a full CMS suite or application scripting framework. It has been deployed on many projects from small-scale sites as a basic templating engine, to powering mid-sized e-commerce applications, to running full-fledged intensive data applications on high profile web sites.</p>
<p><strong>Who&#8217;s it for?</strong></p>
<p>XPHP is designed for developers who want to improve the efficiency and structure of their applications without committing to a complicated, bulky framework, or involving disorganized, unsafe, and philosophically questionable pseudo-code in the delivery of dynamic content to a web page.</p>
<p><strong>Basic goals of XPHP development:</strong></p>
<ul>
<li>Embracing XML as a powerful programming tool and partner in PHP application development.</li>
<li>The avoidance of dispersing significant application logic, whether as PHP code or otherwise, throughout individual site files which take focus off the central web application itself.</li>
<li>Adhering to an extremely simple syntax, while allowing for maximum flexibility.</li>
<li>Ease of development and fast code execution.</li>
</ul>
<p><strong>Features of XPHP:</strong></p>
<ul>
<li>fast, seamless templating functionality</li>
<li>ability to execute application components with advanced parameter parsing</li>
<li>support for nesting/recursion, and output evaluation</li>
<li>advanced output control, including flexible caching support, basic if/else statements, and variable casting</li>
<li>fully aware of object-oriented programming techniques</li>
<li>support for PHP extensions such as XSL, and the popular APC opcode cache</li>
<li>support for pushing content</li>
<li>built-in routines for performing common actions</li>
<li>allows for the registration of user-defined tags and tag types</li>
<li>simple implementation via output buffering, or manual parsing</li>
<li>100% pure XML syntax</li>
</ul>
<p><strong>System requirements:</strong></p>
<ul>
<li>PHP 5.0+</li>
<li>SimpleXML extension for PHP (enabled by default)</li>
</ul>
<p>A full tutorial and download link are available at: <a href="http://technologies.whitewhale.net/xphp/">White Whale Technologies: XPHP</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://chapter42.whitewhale.net/2008/06/30/introducing-xphp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SFTPainless</title>
		<link>http://chapter42.whitewhale.net/2008/05/15/sftpainless/</link>
		<comments>http://chapter42.whitewhale.net/2008/05/15/sftpainless/#comments</comments>
		<pubDate>Thu, 15 May 2008 18:43:03 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Behind the scenes]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[ftp]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sftp]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://chapter42.whaleblogs.net/?p=22</guid>
		<description><![CDATA[At White Whale I maintain several libraries of code which involve performing tasks over FTP. PHP has an FTP extension that works pretty well for FTP-related code. The major flaw with it is that it only supports FTP and FTP+SSL. As we start working more and more with SFTP servers, many of these libraries needed [...]]]></description>
			<content:encoded><![CDATA[<p>At White Whale I maintain several libraries of code which involve performing tasks over FTP. PHP has an <a href="http://www.php.net/ftp">FTP</a> extension that works pretty well for FTP-related code. The major flaw with it is that it only supports FTP and FTP+SSL. As we start working more and more with SFTP servers, many of these libraries needed to add SFTP support.</p>
<p>There is an <a href="http://www.php.net/ssh2">SSH2</a> module that requires a little more effort to set up on your web server, but enables support for SFTP servers. The question is then, how can I integrate this extension and its features into an existing FTP codebase in an efficient manner? Furthermore, the SSH2 module&#8217;s syntax is not as straightforward for basic FTP tasks as the actual FTP extension is.</p>
<p>The answer is really pretty simple.<span id="more-22"></span> I created an FTP wrapper class which runs in one of three modes: FTP, SSL, or SFTP. Depending on the mode, the wrapper class reroutes its method calls to the appropriate extension. All the methods for this class exactly match the ones for the FTP extension. In the case of FTP and SSL, it is easy to map the methods from one to the other, because they&#8217;re exactly the same. In the case of SFTP, there&#8217;s a translation layer which converts FTP commands in the FTP extension&#8217;s syntax to the SSH2 extension&#8217;s equivalents.</p>
<p>The reason this approach is efficient is because all the existing code can be painlessly converted to the wrapper class, such that SFTP is instantly supported. No alternate SFTP code paths, no significant code rewrites, no new syntax to follow. What it boils down to is one extra step to specify the FTP mode, and then just prefixing each FTP function call with the wrapper object (i.e. ftp_put to $ftp-&gt;ftp_put). Done!</p>
]]></content:encoded>
			<wfw:commentRss>http://chapter42.whitewhale.net/2008/05/15/sftpainless/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

