<?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>Carlos André Ferrari &#187; classes</title>
	<atom:link href="http://ferrari.eti.br/tag/classes/feed/" rel="self" type="application/rss+xml" />
	<link>http://ferrari.eti.br</link>
	<description>php, python, xhtml, html5, css, javascript, jquery, webstandards, linux, nginx</description>
	<lastBuildDate>Thu, 29 Jul 2010 10:05:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Classe, WebService e RSS para rastreamento de encomendas nos Correios em PHP</title>
		<link>http://ferrari.eti.br/classe-api-webservice-para-rastreamento-de-encomendas-nos-correios-em-php/</link>
		<comments>http://ferrari.eti.br/classe-api-webservice-para-rastreamento-de-encomendas-nos-correios-em-php/#comments</comments>
		<pubDate>Sat, 10 Apr 2010 10:25:32 +0000</pubDate>
		<dc:creator>caferrari</dc:creator>
				<category><![CDATA[github]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[opensource]]></category>

		<guid isPermaLink="false">http://ferrari.eti.br/?p=117</guid>
		<description><![CDATA[Olá, depois de meses sem escrever estou voltando com uma classe muito útil e simples que nunca vi em lugar nenhum. É uma classe para rastrear encomendas nos correios que eu escrevi em PHP, que retorna um vetor com cada modificação de status lá.
Vamos direto ao ponto, confira o exemplo:
http://ferrari.eti.br/correios/samples/class.php
E para deixar a brincadeira mais [...]]]></description>
			<content:encoded><![CDATA[<p>Olá, depois de meses sem escrever estou voltando com uma <strong>classe</strong> muito útil e simples que nunca vi em lugar nenhum. É uma classe para rastrear encomendas nos <strong>correios</strong> que eu escrevi em <strong>PHP</strong>, que retorna um vetor com cada modificação de status lá.</p>
<p>Vamos direto ao ponto, confira o exemplo:</p>
<p><a href="http://ferrari.eti.br/correios/samples/class.php">http://ferrari.eti.br/correios/samples/class.php</a></p>
<p>E para deixar a brincadeira mais divertida, fiz também um <strong>webservice</strong> com suporte a <strong>Json</strong> (padrão), <strong>phpserialize</strong>, <strong>XML</strong> e um modo dump, para visualização:</p>
<p><a href="http://ferrari.eti.br/correios/webservice/?q=PB151832535BR">http://ferrari.eti.br/correios/webservice/?q=PB151832535BR</a><br />
<a href="http://ferrari.eti.br/correios/webservice/?q=PB151832535BR&amp;f=dump">http://ferrari.eti.br/correios/webservice/?q=PB151832535BR&amp;f=dump</a><br />
<a href="http://ferrari.eti.br/correios/webservice/?q=PB151832535BR&amp;f=serial">http://ferrari.eti.br/correios/webservice/?q=PB151832535BR&amp;f=serial</a><br />
<a href="http://ferrari.eti.br/correios/webservice/?q=PB151832535BR&amp;f=xml">http://ferrari.eti.br/correios/webservice/?q=PB151832535BR&amp;f=xml</a><br />
e claro, para quem usa <strong>Ajax</strong>:<br />
<a href="http://ferrari.eti.br/correios/webservice/?q=PB151832535BR&amp;callback=minhaFuncJs">http://ferrari.eti.br/correios/webservice/?q=PB151832535BR&amp;callback=minhaFuncJs</a></p>
<p>Disponibilizei tanto a classe quanto o código do webservice (que tem suporte a <strong>caching</strong>!) &#8220;de grátis&#8221; e com licença BSD no <strong>github</strong>:</p>
<p><a href="http://github.com/caferrari/correios/">http://github.com/caferrari/correios/</a></p>
<p><strong>*UPDATE* </strong>Adicionado um gerador de RSS, assim é possível usar o proprio agregador para fazer o tracking!<strong></strong></p>
<p><a href="http://ferrari.eti.br/correios/rss/?PB151832535BR">http://ferrari.eti.br/correios/rss/?PB151832535BR</a></p>
<p>e em homenagem aos atendentes de telemarketing: Estarei esperando forks e colaborações para a classe, além de comentários!.</p>
<p>Até a próxima.</p>
]]></content:encoded>
			<wfw:commentRss>http://ferrari.eti.br/classe-api-webservice-para-rastreamento-de-encomendas-nos-correios-em-php/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Autenticação de Usuários no Vórtice PHP</title>
		<link>http://ferrari.eti.br/autenticacao-de-usuarios-no-vortice-php/</link>
		<comments>http://ferrari.eti.br/autenticacao-de-usuarios-no-vortice-php/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 19:02:57 +0000</pubDate>
		<dc:creator>caferrari</dc:creator>
				<category><![CDATA[vortice]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://ferrari.eti.br/?p=71</guid>
		<description><![CDATA[Nesse post vou explicar como adicionar autenticação de usuários no @vorticephp, como base para o sistema vamos utilizar o exemplo que já vem com o framework.
Script de BD:

CREATE TABLE IF NOT EXISTS orgaos &#40;
id int&#40;11&#41; NOT NULL AUTO_INCREMENT,
sigla varchar&#40;50&#41; collate utf8_unicode_ci NOT NULL,
nome varchar&#40;150&#41; collate utf8_unicode_ci NOT NULL,
PRIMARY KEY  &#40;id&#41;
&#41; ENGINE=MyISAM  DEFAULT CHARSET=utf8
&#160;
CREATE TABLE IF [...]]]></description>
			<content:encoded><![CDATA[<p>Nesse post vou explicar como adicionar autenticação de usuários no <a href="http://twitter.com/vorticephp">@vorticephp</a>, como base para o sistema vamos utilizar o exemplo que já vem com o framework.</p>
<p>Script de BD:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> orgaos <span style="color: #66cc66;">&#40;</span>
id int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
sigla varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> collate utf8_unicode_ci <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
nome varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">150</span><span style="color: #66cc66;">&#41;</span> collate utf8_unicode_ci <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM  <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> usuarios <span style="color: #66cc66;">&#40;</span>
id int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
nome varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
email varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
senha char<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> usuarios <span style="color: #66cc66;">&#40;</span>nome<span style="color: #66cc66;">,</span> email<span style="color: #66cc66;">,</span> senha<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Administrador'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'adm@ferrari.eti.br'</span><span style="color: #66cc66;">,</span> md5<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'123'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>eu criei essa tabela com esse usuario e senha no database que já vem configurado no framework para vocês testarem localmente. Você pode configurar a conexão com o seu próprio banco de dados no arquivo app/app.php</p>
<p>Para iniciar vamos criar o objeto do usuário em: app/model/Usuario.php e codificar:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> Usuario<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$nome</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$email</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$senha</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nome</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$nome</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">email</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$email</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">senha</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$senha</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>agora que temos o objeto, vamos criar um DAO para realizar operações, crie o arquivo app/model/UsuarioDAO.php e segue o código:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> UsuarioDAO<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getByEmail<span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM usuarios WHERE email='<span style="color: #006699; font-weight: bold;">$email</span>'&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> Database<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">queryOne</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</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></div></div>

<p>vamos para o controller de usuários, crie o arquivo app/controller/UsuarioController.php e segue o codigo inicial:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> UsuarioController <span style="color: #000000; font-weight: bold;">extends</span> Controller<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> login<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Dai a gente cria o nosso formulário de login, edite app/view/usuario/login.php:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;h2&gt;Login&lt;/h2&gt;
&lt;fieldset&gt;
	&lt;form method=&quot;post&quot;&gt;
		&lt;p&gt;&lt;label&gt;E-mail:&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;email&quot; /&gt;&lt;/p&gt;
		&lt;p&gt;&lt;label&gt;Senha:&lt;/label&gt;&lt;input type=&quot;password&quot; name=&quot;senha&quot; /&gt;&lt;/p&gt;
		&lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Logar!&quot; /&gt;&lt;/p&gt;
	&lt;/form&gt;
&lt;/fieldset&gt;</pre></div></div>

<p>Agora com o form criado, vamos editar a action login do controller Usuario, volte e edite o arquivo app/controller/UsuarioController.php:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> UsuarioController <span style="color: #000000; font-weight: bold;">extends</span> Controller<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> login<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;">// se um formulario foi postado</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>post<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// pega as variáveis do post</span>
			<span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #990000;">addslashes</span><span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$senha</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'senha'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// seleciona o usuario, armazena na variavel $u</span>
			<span style="color: #666666; font-style: italic;">// e ainda verifica se o usuario existe</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$u</span> <span style="color: #339933;">=</span> UsuarioDAO<span style="color: #339933;">::</span><span style="color: #004000;">getByEmail</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</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;">// verifica se a senha digitada combina com</span>
				<span style="color: #666666; font-style: italic;">// a que está no BD</span>
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$u</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">senha</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$senha</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #666666; font-style: italic;">// remove a senha do objeto</span>
					<span style="color: #000088;">$u</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">senha</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
					<span style="color: #666666; font-style: italic;">// grava a session</span>
					Session<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;u&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$u</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #666666; font-style: italic;">// Cria mensagem de sucesso e redireciona para o index</span>
					Post<span style="color: #339933;">::</span><span style="color: #004000;">setSucesso</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Olá <span style="color: #006699; font-weight: bold;">{$u-&gt;nome}</span>, você está logado&quot;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #990000;">Link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</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: #b1b100;">else</span>
					<span style="color: #666666; font-style: italic;">// Retorna erro</span>
					Post<span style="color: #339933;">::</span><span style="color: #004000;">setErros</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Senha incorreta!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span>
				<span style="color: #666666; font-style: italic;">// Retorna erro</span>
				Post<span style="color: #339933;">::</span><span style="color: #004000;">setErros</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;e-mail não encontrado!&quot;</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></pre></div></div>

<p>Para ficar bonita a url vamos criar um route para esta action, edite o arquivo app/routes.php e adicione:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Route<span style="color: #339933;">::</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;^login$&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;usuario:login&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Agora o formulário de login já está funcionando, vamos fazer o sistema obrigar o usuario estar logado para fazer qualquer coisa no sistema, para isso edite o arquivo app/controller/MasterController.php, como queremos que para todo o sistema sejá necessário o login, vamos adicionar um construtor nessa classe com o seguinte código:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> __construct<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;">// se a session &quot;u&quot; não existir, criar uma com um usuario vazio</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Session<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;u&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> Session<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;u&quot;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">new</span> Usuario<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Se a session u tiver um objeto vazio..</span>
	<span style="color: #666666; font-style: italic;">// e o controller/action forem diferentes dos de login</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>Session<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;u&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">&amp;&amp;</span> controller <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'usuario'</span> <span style="color: #339933;">&amp;&amp;</span> action <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'login'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// redireciona para a tela de login</span>
		redirect<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #990000;">Link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;login&quot;</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></div></div>

<p>Bom, com isso já deve estar tudo funcionando de forma eficiente e simples. Qualquer dúvida ou sugestão mande sem hexitar.</p>
]]></content:encoded>
			<wfw:commentRss>http://ferrari.eti.br/autenticacao-de-usuarios-no-vortice-php/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Criando Tweets pelo PHP</title>
		<link>http://ferrari.eti.br/criando-tweets-pelo-php/</link>
		<comments>http://ferrari.eti.br/criando-tweets-pelo-php/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 15:32:03 +0000</pubDate>
		<dc:creator>caferrari</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[tweet]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://ferrari.eti.br/?p=33</guid>
		<description><![CDATA[Sou responsável por um site de notícias e hoje precisei criar um meio de que quando uma notícia for inserida no site, ser criado automaticamente um tweet no twitter lincando para ela, a solução foi essa classe abaixo:

class Tweet&#123;
	var $user;
	var $pass;
	var $json = false;
&#160;
	function __construct&#40;$user, $pass, $texto=false&#41;&#123;
		$this-&#38;gt;user = $user;
		$this-&#38;gt;pass = $pass;
		if &#40;$texto&#41; $this-&#38;gt;send&#40;$texto&#41;;
	&#125;
&#160;
	private function testTweet&#40;$texto, [...]]]></description>
			<content:encoded><![CDATA[<p>Sou responsável por um site de notícias e hoje precisei criar um meio de que quando uma notícia for inserida no site, ser criado automaticamente um tweet no <a href="http://twitter.com">twitter</a> lincando para ela, a solução foi essa classe abaixo:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Tweet<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$user</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$pass</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$json</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pass</span><span style="color: #339933;">,</span> <span style="color: #000088;">$texto</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>user <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>pass <span style="color: #339933;">=</span> <span style="color: #000088;">$pass</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$texto</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>send<span style="color: #009900;">&#40;</span><span style="color: #000088;">$texto</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> testTweet<span style="color: #009900;">&#40;</span><span style="color: #000088;">$texto</span><span style="color: #339933;">,</span> <span style="color: #000088;">$match</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;">$match</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$m</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$m</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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">ereg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;^www&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://<span style="color: #006699; font-weight: bold;">$url</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">19</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_repeat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;x&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">19</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$texto</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$m</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;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$texto</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$texto</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ereg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://(x{19,})&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>1&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$texto</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$texto</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">140</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> makeUrl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</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: #990000;">ereg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;^www&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://<span style="color: #006699; font-weight: bold;">$url</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span> <span style="color: #cc66cc;">19</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$url</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://migre.me/atalho/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">curl_setopt_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			CURLOPT_URL <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$url</span><span style="color: #339933;">,</span>
			CURLOPT_RETURNTRANSFER <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #009900; font-weight: bold;">true</span>
		<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$o</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">ereg</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value=<span style="color: #000099; font-weight: bold;">\&quot;</span>(http://migre.me/[a-zA-Z0-9/:]+)<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$o</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mat</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$mat</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>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> send<span style="color: #009900;">&#40;</span><span style="color: #000088;">$texto</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: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;curl_init&quot;</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;">// Localiza as URLs</span>
			<span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/((https?:\/\/)|(www))([a-zA-Z0-9\/:%&amp;amp;\?\.=\-\+]+)/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$texto</span><span style="color: #339933;">,</span> <span style="color: #000088;">$match</span><span style="color: #339933;">,</span> PREG_SET_ORDER <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// Testa se o tamanho do twitter é aceitavel</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;">-&amp;</span>gt<span style="color: #339933;">;</span>testTweet<span style="color: #009900;">&#40;</span><span style="color: #000088;">$texto</span><span style="color: #339933;">,</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Postagem muito longa!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// Cria as URLs finais</span>
			<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$match</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$m</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$texto</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$m</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;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>makeUrl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$texto</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$texto</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://http://&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;http://&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$texto</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// Testa novamente pelo comprimento do tweet</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$texto</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">140</span><span style="color: #009900;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Postagem muito longa!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// Envia o tweet</span>
			<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #990000;">curl_setopt_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				CURLOPT_URL <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">&quot;http://twitter.com/statuses/update.json&quot;</span><span style="color: #339933;">,</span>
				CURLOPT_HTTPHEADER <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Expect:'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				CURLOPT_POST <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
				CURLOPT_POSTFIELDS <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;status&quot;</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$texto}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				CURLOPT_HTTPAUTH <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> CURLAUTH_ANY<span style="color: #339933;">,</span>
				CURLOPT_RETURNTRANSFER <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
				CURLOPT_USERPWD <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">&quot;{<span style="color: #006699; font-weight: bold;">$this</span>-&amp;gt;user}:{<span style="color: #006699; font-weight: bold;">$this</span>-&amp;gt;pass}&quot;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>json <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>json <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Tweet<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$tmp</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>send<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Eu não gosto das URLs do site da Microsoft: http://search.microsoft.com/results.aspx?form=MSHOME&amp;amp;setlang=en-us&amp;amp;q=office+templates&amp;amp;mkt=en-US&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Ela faz a análise do seu texto e converte as URLs, caso necessário, para um formato resumido usando o <a href="http://migre.me">migre.me</a> e caso passe os 140 caracteres uma exception é jogada.</p>
]]></content:encoded>
			<wfw:commentRss>http://ferrari.eti.br/criando-tweets-pelo-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Operador XOR no PHP</title>
		<link>http://ferrari.eti.br/operador-xor-no-php/</link>
		<comments>http://ferrari.eti.br/operador-xor-no-php/#comments</comments>
		<pubDate>Wed, 21 May 2008 19:00:59 +0000</pubDate>
		<dc:creator>caferrari</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[criptografia]]></category>

		<guid isPermaLink="false">http://ferrari.eti.br/?p=6</guid>
		<description><![CDATA[Há alguns meses estava precisando criar uma classe de criptografia e quando eu fui desenvolver eu não encontrei nenhuma criptografia veloz o suficiente, até que o Luan me mostrou uma excelente utilização para o XOR. Este operador é muito parecido com o OR, porém a sua leitura é: &#8220;OU um OU outro&#8221; e não &#8220;UM [...]]]></description>
			<content:encoded><![CDATA[<p>Há alguns meses estava precisando criar uma classe de criptografia e quando eu fui desenvolver eu não encontrei nenhuma criptografia veloz o suficiente, até que o <a href="http://luan.eti.br">Luan</a> me mostrou uma excelente utilização para o <a href="http://en.wikipedia.org/wiki/Exclusive_or">XOR</a>. Este operador é muito parecido com o <a href="http://en.wikipedia.org/wiki/Or">OR</a>, porém a sua leitura é: &#8220;OU um OU outro&#8221; e não &#8220;UM ou OUTRO&#8221; como o <a href="http://en.wikipedia.org/wiki/Or">OR</a>. Confira a tabela abaixo exemplificando:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900; font-weight: bold;">false</span> ^ <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span>
<span style="color: #009900; font-weight: bold;">false</span> ^ <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span>
<span style="color: #009900; font-weight: bold;">true</span> ^ <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span>
<span style="color: #009900; font-weight: bold;">true</span> ^ <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span></pre></div></div>

<p>Legal não? só é verdadeiro se os valores forem diferentes.</p>
<p>Como exemplo vamos pegar 2 valores binários, como sendo uma chave e um valor:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">valor<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;11010110&quot;</span>
chave<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;10011101&quot;</span></pre></div></div>

<p>Vamos operar cada bit usando o <a href="http://en.wikipedia.org/wiki/Exclusive_or">XOR</a>, isso irá resultar o binário &#8220;encriptado&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #cc66cc;">11010110</span> ^ <span style="color: #009900;">&#40;</span>valor<span style="color: #009900;">&#41;</span>
<span style="color: #cc66cc;">10011101</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>chave<span style="color: #009900;">&#41;</span>
<span style="color: #208080;">01001011</span>    <span style="color: #009900;">&#40;</span>valor encriptado<span style="color: #009900;">&#41;</span></pre></div></div>

<p>Na terceira linha obtivemos o valor encriptado, só para demonstrar, vamos usar esse valor encriptado e dar um <a href="http://en.wikipedia.org/wiki/Exclusive_or">XOR</a> com a chave novamente.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #208080;">01001011</span> ^ <span style="color: #009900;">&#40;</span>valor encriptado<span style="color: #009900;">&#41;</span>
<span style="color: #cc66cc;">10011101</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>chave<span style="color: #009900;">&#41;</span>
<span style="color: #cc66cc;">11010110</span>   <span style="color: #009900;">&#40;</span>valor novamente<span style="color: #009900;">&#41;</span></pre></div></div>

<p>Não precisa de mais exemplos certo? é uma forma fraca, porém muito veloz de gerar criptografia, e perfeita para usarmos em coisas simples, como a criptografia de URL&#8217;s e <a href="http://en.wikipedia.org/wiki/Salt_%28cryptography%29">Salts</a></p>
<p>Mas não pense que programar isso é complexo, o <a href="http://br.php.net">PHP</a> permite que façamos esse processo usando dois caracteres.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #0000ff;">&quot;a&quot;</span> ^ <span style="color: #0000ff;">&quot;b&quot;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;?&quot;</span><span style="color: #339933;">,</span> logo
<span style="color: #0000ff;">&quot;?&quot;</span> ^ <span style="color: #0000ff;">&quot;b&quot;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;a&quot;</span></pre></div></div>

<p>Com isso vou mostrar uma classe de criptografia extremamente simples:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> <span style="color: #990000;">Crypt</span> <span style="color: #009900;">&#123;</span>
    static <span style="color: #000088;">$chave</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;xy&quot;</span><span style="color: #339933;">;</span>
    static <span style="color: #000000; font-weight: bold;">function</span> StringXor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #000088;">$b</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;">$a</span><span style="color: #339933;">==</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$retorno</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">do</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$retorno</span> <span style="color: #339933;">.=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#125;</span> ^ <span style="color: #000088;">$b</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">%</span> <span style="color: #000088;">$j</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">strrev</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$retorno</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    static <span style="color: #000000; font-weight: bold;">function</span> Encrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">base64_encode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">Crypt</span><span style="color: #339933;">::</span><span style="color: #004000;">StringXor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #990000;">Crypt</span><span style="color: #339933;">::</span><span style="color: #000088;">$chave</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    static <span style="color: #000000; font-weight: bold;">function</span> Decrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">Crypt</span><span style="color: #339933;">::</span><span style="color: #004000;">StringXor</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">base64_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">Crypt</span><span style="color: #339933;">::</span><span style="color: #000088;">$chave</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></div></div>

<p>O que essa classe faz basicamente é percorrer as strings da chave e do valor passado, calculando para cada caractere o seu valor encriptado, e ao final é codificado usando-se base64 para que não fique com caracteres inválidos na string.</p>
<p>Um exemplo:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">texto <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;andre&quot;</span>
chave <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;xy&quot;</span>
resultado<span style="color: #339933;">:</span>
a ^ x
n ^ y
d ^ x
r ^ y
e ^ x</pre></div></div>

<p>Código php:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">Crypt</span><span style="color: #339933;">::</span><span style="color: #004000;">Encrypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;andre&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Encriptado:&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #990000;">Crypt</span><span style="color: #339933;">::</span><span style="color: #004000;">Decrypt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Decriptado: <span style="color: #006699; font-weight: bold;">$b</span>&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Saída:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Encriptado<span style="color: #339933;">:</span> GRccCx0<span style="color: #339933;">=</span>
Decriptado<span style="color: #339933;">:</span> andre</pre></div></div>

<p>é isso, testem, entendam e qualquer coisa pergunte.</p>
]]></content:encoded>
			<wfw:commentRss>http://ferrari.eti.br/operador-xor-no-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
