<?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"
	>

<channel>
	<title>Spertegaz</title>
	<atom:link href="http://blog.spertegaz.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.spertegaz.com</link>
	<description>Blogueando sobre programación</description>
	<pubDate>Thu, 29 Jan 2009 18:53:20 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.5</generator>
	<language>en</language>
			<item>
		<title>Seguridad en PHP al subir un archivo</title>
		<link>http://blog.spertegaz.com/2009/01/28/seguridad-en-php-al-subir-un-archivo/</link>
		<comments>http://blog.spertegaz.com/2009/01/28/seguridad-en-php-al-subir-un-archivo/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 20:21:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[php5]]></category>

		<category><![CDATA[php seguridad]]></category>

		<guid isPermaLink="false">http://blog.spertegaz.com/?p=23</guid>
		<description><![CDATA[Si en tu aplicación web desarrollada en php permites subir archivos al servidor, te puedes encontrar con algún problemilla de seguridad. A continuación describiré algunos agujeros de seguridad que se producen al subir un archivo y como implementar una solución segura.

Implementación básica
upload1.php
PLAIN TEXT
PHP:




&#60;?php


$uploaddir = 'uploads/'; // Directorio relativo al raiz web


$uploadfile = $uploaddir . basename&#40;$_FILES&#91;'userfile'&#93;&#91;'name'&#93;&#41;;


if [...]]]></description>
			<content:encoded><![CDATA[<p>Si en tu aplicación web desarrollada en php permites subir archivos al servidor, te puedes encontrar con algún problemilla de seguridad. A continuación describiré algunos agujeros de seguridad que se producen al subir un archivo y como implementar una solución segura.<br />
<span id="more-23"></span><br />
<strong>Implementación básica</strong></p>
<p><em>upload1.php</em></p>
<div class="igBar"><span id="lphp-7"><a href="#" onclick="javascript:showCodeTxt('php-7'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-7">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$uploaddir</span> = <span style="color:#FF0000;">'uploads/'</span>; <span style="color:#FF9933; font-style:italic;">// Directorio relativo al raiz web</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$uploadfile</span> = <span style="color:#0000FF;">$uploaddir</span> . <a href="http://www.php.net/basename" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">basename</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_FILES</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'userfile'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'name'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/move_uploaded_file" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">move_uploaded_file</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_FILES</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'userfile'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'tmp_name'</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#0000FF;">$uploadfile</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"Fichero subido correctamente.<span style="color:#000099; font-weight:bold;">\n</span>"</span>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"Error al subir el archivo.<span style="color:#000099; font-weight:bold;">\n</span>"</span>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Los usuarios podrán ver los archivos subidos navegando en http://www.midominio.com/uploads/filename.jpg</p>
<p>Y para subir los archivos les mostraremos un formulario como el siguiente:</p>
<div class="igBar"><span id="lphp-8"><a href="#" onclick="javascript:showCodeTxt('php-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-8">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;form name=<span style="color:#FF0000;">"upload"</span> action=<span style="color:#FF0000;">"upload1.php"</span> method=<span style="color:#FF0000;">"POST"</span> ENCTYPE=<span style="color:#FF0000;">"multipart/formdata"</span>&gt;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Select the <a href="http://www.php.net/file" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">file</span></a> to upload: &lt;input type=<span style="color:#FF0000;">"file"</span> name=<span style="color:#FF0000;">"userfile"</span>&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;input type=<span style="color:#FF0000;">"submit"</span> name=<span style="color:#FF0000;">"upload"</span> value=<span style="color:#FF0000;">"upload"</span>&gt;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;/form&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Un ataque no tiene porque usar el formulario anterior. Se puede escribir un script en Perl y subir archivos o usar un proxy para interceptar y modificar los datos subidos. El código anterior sufre un de un agujero mayor de seguridad. Permite a los usuarios subir ficheros arbitrariamente al directorio uploads/ a partir del raiz. Por lo tanto, se podria subir un fichero PHP y ejecutar comandos en el servidor. El siguiente script en PHP permite al usuario ejecutar comdandos de shell en el servidor.</p>
<div class="igBar"><span id="lphp-9"><a href="#" onclick="javascript:showCodeTxt('php-9'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-9">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/system" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">system</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_GET</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'command'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Si éste fichero se encuentra en nuestro servidor, cualquiera podría ejecutar comandos de shell navegando por ésta dirección http://midominio.com/shell.php?command=Unix_shell_command.</p>
<p>Puedes encontrar en internet muchos más scripts de PHP Shells más sofisticados que permiten incluso ejecutar sentencias SQL.</p>
<p>Por lo tanto debemos controlar que tipo de ficheros se pueden subir al servidor<br />
<strong>Comprobar el Content-type</strong></p>
<p><em>upload2.php</em></p>
<div class="igBar"><span id="lphp-10"><a href="#" onclick="javascript:showCodeTxt('php-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-10">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_FILES</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'userfile'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'type'</span><span style="color:#006600; font-weight:bold;">&#93;</span> != <span style="color:#FF0000;">"image/gif"</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"Sorry, we only allow uploading GIF images"</span>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/exit" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">exit</span></a>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$uploaddir</span> = <span style="color:#FF0000;">'uploads/'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$uploadfile</span> = <span style="color:#0000FF;">$uploaddir</span> . <a href="http://www.php.net/basename" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">basename</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_FILES</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'userfile'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'name'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/move_uploaded_file" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">move_uploaded_file</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_FILES</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'userfile'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'tmp_name'</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#0000FF;">$uploadfile</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"File is valid, and was successfully uploaded.<span style="color:#000099; font-weight:bold;">\n</span>"</span>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"File uploading failed.<span style="color:#000099; font-weight:bold;">\n</span>"</span>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>En éste caso, si un usuario intenta subir shell.php, se comprobará el MIME type en la petición de subida del archivo y se rechazará.</p>
<p>Hemos solucionado un problema, pero la comprobación del MIME type se puede saltar fácilmente, como muestra el siguiente script en PERL. </p>
<div class="igBar"><span id="lperl-11"><a href="#" onclick="javascript:showCodeTxt('perl-11'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PERL:</span>
<div id="perl-11">
<div class="perl">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">#!/usr/bin/perl</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">#</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">use</span> LWP;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">use</span> HTTP::<span style="color: #006600;">Request</span>::<span style="color: #006600;">Common</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">$ua</span> = <span style="color: #0000ff;">$ua</span> = LWP::<span style="color: #006600;">UserAgent</span>-&gt;<span style="color: #006600;">new</span>;;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">$res</span> = <span style="color: #0000ff;">$ua</span>-&gt;<span style="color: #006600;">request</span><span style="color: #66cc66;">&#40;</span>POST <span style="color: #ff0000;">'http://localhost/upload2.php'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Content_Type =&gt; <span style="color: #ff0000;">'form-data'</span>,</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Content =&gt; <span style="color: #66cc66;">&#91;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">userfile =&gt; <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">"shell.php"</span>, <span style="color: #ff0000;">"shell.php"</span>, <span style="color: #ff0000;">"Content-Type"</span> =&gt;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff0000;">"image/gif"</span><span style="color: #66cc66;">&#93;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#93;</span>,</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.perldoc.com/perl5.6/pod/func/print.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.perldoc.com');"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$res</span>-&gt;<span style="color: #006600;">as_string</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>El script en Perl anterior cambia el valor de la cabecera Content-type a image/gif, lo que permite que <em>upload2.php</em> acepte la subida del fichero, aunque sigue trantandose de un PHP shell script.</p>
<p><strong>Validar en contenido del fichero imagen</strong></p>
<p>Utilizaremos la función de PHP getimagesize(). Captura el nombre del fichero como un argumento y devuleve el tamaño y tipo de la imagen. </p>
<p><em>upload3.php</em></p>
<div class="igBar"><span id="lphp-12"><a href="#" onclick="javascript:showCodeTxt('php-12'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-12">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$imageinfo</span> = <a href="http://www.php.net/getimagesize" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">getimagesize</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_FILES</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'userfile'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'tmp_name'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$imageinfo</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'mime'</span><span style="color:#006600; font-weight:bold;">&#93;</span> != <span style="color:#FF0000;">'image/gif'</span> &amp;&amp; <span style="color:#0000FF;">$imageinfo</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'mime'</span><span style="color:#006600; font-weight:bold;">&#93;</span> != <span style="color:#FF0000;">'image/jpeg'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"Sorry, we only accept GIF and JPEG images<span style="color:#000099; font-weight:bold;">\n</span>"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/exit" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">exit</span></a>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$uploaddir</span> = <span style="color:#FF0000;">'uploads/'</span>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$uploadfile</span> = <span style="color:#0000FF;">$uploaddir</span> . <a href="http://www.php.net/basename" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">basename</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_FILES</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'userfile'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'name'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/move_uploaded_file" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">move_uploaded_file</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_FILES</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'userfile'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'tmp_name'</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#0000FF;">$uploadfile</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"File is valid, and was successfully uploaded.<span style="color:#000099; font-weight:bold;">\n</span>"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"File uploading failed.<span style="color:#000099; font-weight:bold;">\n</span>"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><code></code><!--more--></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.spertegaz.com/2009/01/28/seguridad-en-php-al-subir-un-archivo/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Cómo insertar código en un Post de Wordpress. iG:Syntax Hiliter</title>
		<link>http://blog.spertegaz.com/2009/01/15/como-insertar-codigo-en-un-post-de-wordpress-igsyntax-hiliter/</link>
		<comments>http://blog.spertegaz.com/2009/01/15/como-insertar-codigo-en-un-post-de-wordpress-igsyntax-hiliter/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 22:27:45 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[wordpress]]></category>

		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://blog.spertegaz.com/?p=17</guid>
		<description><![CDATA[Con éste plugin para Wordpress podremos insertar código en un post. Algo imprescindible si vas a postear sobre programación.

Decora el código en un cuadro, numera las líneas y lo colorea conforme al lenguaje escrito. Soporta ActionScript, ASP, C, C++, C#, CSS, Delphi, HTML, Java, JavaScript, MySQL, Perl, PHP, Python, Ruby, Smarty, SQL, Visual Basic, VB.NET, [...]]]></description>
			<content:encoded><![CDATA[<p>Con éste plugin para Wordpress podremos insertar código en un post. Algo imprescindible si vas a postear sobre programación.<br />
<span id="more-17"></span><br />
Decora el código en un cuadro, numera las líneas y lo colorea conforme al lenguaje escrito. Soporta <strong>ActionScript, ASP, C, C++, C#, CSS, Delphi, HTML, Java, JavaScript, MySQL, Perl, PHP, Python, Ruby, Smarty, SQL, Visual Basic, VB.NET, XML</strong> y todos los lenguajes soportados por GeSHi.</p>
<p>Se instala como cualquier otro plugin, subes la carpeta al directorio plugins y desde el administrador lo activas. Y al escribir tu post sólo tienes que escribir el código entre las etiquetas [lenguaje] ... [/lenguaje], donde lenguaje es el del script que vayas a incluir. El resultdo es el siguiente.</p>
<div class="igBar"><span id="lphp-14"><a href="#" onclick="javascript:showCodeTxt('php-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-14">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">'hola mundo'</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Debes tener instalada una vesión de wordpress 1.5 o superior y lo puedes descargar desde aquí</p>
<p><a href="http://blog.igeek.info/wp-plugins/igsyntax-hiliter/" onclick="javascript:pageTracker._trackPageview('/outbound/article/blog.igeek.info');">iG:Syntax Hilite</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.spertegaz.com/2009/01/15/como-insertar-codigo-en-un-post-de-wordpress-igsyntax-hiliter/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Auto carga de objetos utilizando la función __autoload de PHP5</title>
		<link>http://blog.spertegaz.com/2009/01/09/auto-carga-de-objetos-utilizando-la-funcion-__autoload-de-php5/</link>
		<comments>http://blog.spertegaz.com/2009/01/09/auto-carga-de-objetos-utilizando-la-funcion-__autoload-de-php5/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 12:42:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[php5]]></category>

		<guid isPermaLink="false">http://blog.spertegaz.com/?p=9</guid>
		<description><![CDATA[Cuando desarrollamos con PHP sobre el paradigma Orientado Objetos, cada clase es definida en un script php. Por lo que para poder utilizar dichas clases, se deberán hacer los includes necesarios al principio de cada script. Dependiendo de la complejidad de la aplicación, puede terminar siendo, una larga y tediosa lista de includes.

En PHP5 esto [...]]]></description>
			<content:encoded><![CDATA[<p>Cuando desarrollamos con PHP sobre el paradigma Orientado Objetos, cada clase es definida en un script php. Por lo que para poder utilizar dichas clases, se deberán hacer los includes necesarios al principio de cada script. Dependiendo de la complejidad de la aplicación, puede terminar siendo, una larga y tediosa lista de includes.<br />
<span id="more-9"></span><br />
En PHP5 esto se puede evitar. Sólo hay que definir una función llamada __autoload, la cual es llamada automáticamente en el caso de que se intente usar una clase que no haya sido incluida en el script. Es la última chance de cargar la clase antes de que PHP lance un error.</p>
<p>Otra de las ventajas de utilizar __autoload, es que evitamos se incluyan definiciones de clases que no utilizamos. Esto mejorará el rendimiento de nuestras aplicaciones ya que solo se cargan las clases que necesitamos. Por otro lado, se debe tener en cuenta de que la función __autoload será llamada recurrentemente, por lo que tendrá que ser definida lo más liviana posible.</p>
<div class="igBar"><span id="lphp-18"><a href="#" onclick="javascript:showCodeTxt('php-18'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-18">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;span <span style="color:#000000; font-weight:bold;">class</span>=<span style="color:#FF0000;">"codigo"</span>&gt;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">function</span> __autoload<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$className</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/file_exists" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">file_exists</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"./lib/$className.class.php"</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">include_once</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"./lib/$className.class.php"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span>&lt;/span&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$instancia</span>  = <span style="color:#000000; font-weight:bold;">new</span> unaClase<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>En el caso de que estemos desarrollando una aplicación web o escribiendo código que puede ser utilizado por terceros, no es buena idea usar la función __autoload, dado que podría generar eventualmente un conflicto con otra función __autoload definida por terceros. Esto sucede porque lógicamente no puede haber dos funciones con el mismo nombre registradas. Lo que conviene es o bien declararla dentro de una clase o llamarla con otro nombre y luego registrarla en ambos casos mediante la función <a class="wikiext" href="http://www.php.net/manual/en/function.spl-autoload-register.php" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');" target="new">spl_autoload_register</a></p>
<p><span class="codigo">
<div class="igBar"><span id="lphp-19"><a href="#" onclick="javascript:showCodeTxt('php-19'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-19">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">spl_autoload_register<span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/array" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'OP'</span>, <span style="color:#FF0000;">'__autoload'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;&lt;/span&gt;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">class</span> OP<span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">private <a href="http://www.php.net/static" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">static</span></a></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$dir_class</span>= <a href="http://www.php.net/array" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'util'</span>,</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF0000;">'controller'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF0000;">'model'</span>,</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF0000;">'session'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF0000;">'i18n'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">public <a href="http://www.php.net/static" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">static</span></a> <span style="color:#000000; font-weight:bold;">function</span> __autoload<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$class</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">foreach</span><span style="color:#006600; font-weight:bold;">&#40;</span>self::<span style="color:#0000FF;">$dir_class</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$dir</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/file_exists" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">file_exists</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"./lib/$dir/$class.class.php"</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">include_once</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"./lib/$dir/$class.class.php"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">function</span> miAutoload<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$class</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">include_once</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"./$class.php"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">true</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">spl_autoload_register<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'miAutoload'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//Registramos otra vez la misma función</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">spl_autoload_register<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'miAutoload'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//Mostramos las funciones registradas</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/var_dump" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">var_dump</span></a><span style="color:#006600; font-weight:bold;">&#40;</span>spl_autoload_functions<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
<!--fin de codigo --><br />
Podremos ver que la función miAutoload esta registrada solo una vez.</p>
<p><span class="codigo">
<div class="igBar"><span id="lphp-20"><a href="#" onclick="javascript:showCodeTxt('php-20'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-20">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;/span&gt;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;span <span style="color:#000000; font-weight:bold;">class</span>=<span style="color:#FF0000;">"codigo"</span>&gt;array<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>=&amp;gt; <a href="http://www.php.net/array" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>=&amp;gt; string<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#FF0000;">"OP"</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>=&amp;gt; string<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">8</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#FF0000;">"__autoload"</span> <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>=&amp;gt; string<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#FF0000;">"miAutoload"</span> <span style="color:#006600; font-weight:bold;">&#125;</span>&lt;/span&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.spertegaz.com/2009/01/09/auto-carga-de-objetos-utilizando-la-funcion-__autoload-de-php5/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Integrar en tu web marcadores de del.icio.us con cURL y PHP5</title>
		<link>http://blog.spertegaz.com/2009/01/09/integrar-en-tu-web-marcadores-de-delicious-con-curl-y-php5/</link>
		<comments>http://blog.spertegaz.com/2009/01/09/integrar-en-tu-web-marcadores-de-delicious-con-curl-y-php5/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 12:35:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[php5]]></category>

		<category><![CDATA[php5 curl delicious]]></category>

		<guid isPermaLink="false">http://blog.spertegaz.com/?p=6</guid>
		<description><![CDATA[Antes que nada explicaré brevemente que es del.icio.us. Es un servicio de gestión de marcadores al estilo red social que permite agregar los marcadores que usualmente se guardan en los navegadores, categorizarlos con el sistema de Tags, compartirlos con otros usuarios, acceder a ellos desde cualquier ordenador conectado a internet, etc. Y además dispone una [...]]]></description>
			<content:encoded><![CDATA[<p>Antes que nada explicaré brevemente que es del.icio.us. Es un servicio de gestión de marcadores al estilo red social que permite agregar los marcadores que usualmente se guardan en los navegadores, categorizarlos con el sistema de Tags, compartirlos con otros usuarios, acceder a ellos desde cualquier ordenador conectado a internet, etc. Y además dispone una potente pero sencilla API que utlizaremos para el propósito de éste artículo, integrar los marcadores almacenados en del.icio.us en nuestra web utilizando PHP y cURL. De ésta forma conseguiremos mantener la lista de enlaces de nuestra web al mismo tiempo que mantenemos nuestra lista de enlaces particular.<br />
<span id="more-6"></span><br />
¿Qué es cURL? PHP soporta libcurl, librería que permite conectar y establecer comunicación con diferentes tipos de servidores con diferentes protocolos como http, https, ftp, gopher, telnet, dict, file, ldap. Soporta certificados https, autenticación de usuarios, etc. En php dispone de una colección de funciones con el prefijo curl_* , así que vamos a utilizar ésta fantástica librería para comunicarnos con del.icio.us.</p>
<p>La API de del.icio.us es sencilla, consiste en peticiones HTTPS con autenticación HTTP-Auth. Utilizaremos el método POSTS (así denomina del.icio.us a los marcadores, así los denominaré a partir de ahora) que permite hacer diferentes acciones sobre éstos. La que nos interesa es la que obtiene el listado de posts, además con la opción de filtrarlos por un tag.</p>
<p>Está disponible en <a class="wikiext" href="http://del.icio.us/help/api/" onclick="javascript:pageTracker._trackPageview('/outbound/article/del.icio.us');" target="new">http://del.icio.us/help/api/</a>.</p>
<p>Ésta sería la petición:<br />
<span class="codigo">https://api.del.icio.us/v1/posts/get?tag=public<br />
</span><!--fin de codigo --><br />
Nos devolvería en formato XML los posts etiquetados con public, que serán los que queremos que se muestren en la web. Pues vamos a ver como realizar está petición con cURL.<br />
<span class="codigo">&lt;code&gt;</span></p>
<p><span class="codigo">$peticion = https://api.del.icio.us/v1/posts/get?tag=public;</span></p>
<p>if (function_exists('curl_init')) {<br />
$o_curl = curl_init($peticion);<br />
curl_setopt_array($o_curl, array(<br />
CURLOPT_RETURNTRANSFER =&gt; true,<br />
CURLOPT_USERAGENT =&gt; 'http://www.miweb.com',<br />
CURLOPT_CONNECTTIMEOUT =&gt; 5, //segundos<br />
CURLOPT_TIMEOUT =&gt;10 //segundos,<br />
CURLOPT_USERPWD =&gt; 'username:password'<br />
));<br />
if ($result = curl_exec($o_curl)) {<br />
switch (curl_getinfo($o_curl, CURLINFO_HTTP_CODE)) {<br />
case 200:<br />
$posts = $result;<br />
break;<br />
case 503:<br />
//lanzar error. Bloqueo de la petición temporalmente.<br />
default:<br />
//lanzar error de conexión a del.icio.us<br />
}<br />
curl_close($o_curl);<br />
}</p>
<p><span class="codigo">&lt;/code&gt;<br />
</span><!--fin de codigo --><br />
La función <em>curl_setopt_array()</em> setea las opciones de la petición.</p>
<ul>
<li> <strong>CURLOPT_RETURNTRANSFER</strong>. Lo seteamos a TRUE para devolver el resultado como una cadena de texto que contiene el valor devuelto por la función curl_exec(), en vez de mostrar la salida directamente en la ventana del navegador.</li>
<li> <strong>CURLOPT_USERAGENT</strong>.El contenido de la cabecera "User-Agent: " enviada en las peticiones HTTP. Es un requisito de la API de del.icio.us. Por ejemplo podemos poner la url de nuestra web.</li>
<li> <strong>CURLOPT_CONNECTTIMEOUT</strong>. El número de segundos que se pueden esperar como máximo intentando establecer la conexión.</li>
<li> <strong>CURLOPT_TIMEOUT</strong>. El número máximo de segundos que cURL espera a que se ejecuten las funciones.</li>
<li> <strong>CURLOPT_USERPWD</strong>. La API requiere autenticación. Aquí seteamos el nombre de usuario y contraseña con el formato "username:password".</li>
</ul>
<p>Con <em>curl_getinfo</em> obtenemos el codigo http para comprobar si ha habido algún error. Cabe destacar el error 503, devuelto cuando del.icio.us bloquea la petición por un tiempo si se realizan muchas en un corto intervalo de tiempo. Por lo que sería conveniente añadir un sistema de <em>cache</em> en un archivo de texto para limitar el número de peticiones. El resultado de la petición lo devuelve en formato XML, por lo que tendríamos que parsearlo para mostrarlo en nuestra web. Pero ésto ya es otro tema.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.spertegaz.com/2009/01/09/integrar-en-tu-web-marcadores-de-delicious-con-curl-y-php5/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Hola mundo!</title>
		<link>http://blog.spertegaz.com/2008/11/24/hello-world/</link>
		<comments>http://blog.spertegaz.com/2008/11/24/hello-world/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 23:11:23 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.spertegaz.com/?p=1</guid>
		<description><![CDATA[Bienvenidos a mi blog. Éste es mi primer post, así que nada mejor que empezar con un "hola mundo".
]]></description>
			<content:encoded><![CDATA[<p>Bienvenidos a mi blog. Éste es mi primer post, así que nada mejor que empezar con un "hola mundo".</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.spertegaz.com/2008/11/24/hello-world/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
