<?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>caioariede.com weblog</title>
	<atom:link href="http://caioariede.com/feed" rel="self" type="application/rss+xml" />
	<link>http://caioariede.com</link>
	<description>— coding for great good</description>
	<lastBuildDate>Mon, 08 Mar 2010 01:03:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Bitwise Operators</title>
		<link>http://caioariede.com/2009/bitwise-operators</link>
		<comments>http://caioariede.com/2009/bitwise-operators#comments</comments>
		<pubDate>Mon, 09 Nov 2009 01:53:11 +0000</pubDate>
		<dc:creator>Caio Ariede</dc:creator>
				<category><![CDATA[Desenvolvimento]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://caioariede.com/?p=578</guid>
		<description><![CDATA[Uma forma muito elegante de armazenar uma série de regras, utilizando um só valor, é utilizando os Bitwise Operators.
O PHP faz isso a um bom tempo já, e um bom exemplo é a função error_reporting, onde podemos utilizar uma série de regras para denominar quais os erros devem ser reportados.
error_reporting&#40;E_ERROR &#124; E_WARNING&#41;;
No exemplo acima, foi [...]]]></description>
			<content:encoded><![CDATA[<p>Uma forma muito elegante de armazenar uma série de regras, utilizando um só valor, é utilizando os Bitwise Operators.</p>
<p>O PHP faz isso a um bom tempo já, e um bom exemplo é a função <a href="http://br2.php.net/manual/en/function.error-reporting.php">error_reporting</a>, onde podemos utilizar uma série de regras para denominar quais os erros devem ser reportados.</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://www.php.net/error_reporting"><span style="color: #990000;">error_reporting</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ERROR</span> <span style="color: #339933;">|</span> <span style="color: #009900; font-weight: bold;">E_WARNING</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>No exemplo acima, foi denominado que somente os tipos de erro E_ERROR e E_WARNING devem ser reportados. Se quiséssemos que o tipo de erro E_PARSE fosse reportado também, bastariamos adiciona-lo na regra:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://www.php.net/error_reporting"><span style="color: #990000;">error_reporting</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ERROR</span> <span style="color: #339933;">|</span> <span style="color: #009900; font-weight: bold;">E_WARNING</span> <span style="color: #339933;">|</span> <span style="color: #009900; font-weight: bold;">E_PARSE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>Mas como isso funciona?</h3>
<p>Utilizando como exemplo, essas contantes do PHP contém os seguintes valores:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">E_ERROR &nbsp; &nbsp; 1<br />
E_WARNING &nbsp; 2<br />
E_PARSE &nbsp; &nbsp; 4</div></div>
<p>O operador | é uma operação &#8220;Or&#8221; inclusiva, o que indica que, <em>todo bit que estiver presente em A <strong>ou</strong> B, será utilizado</em>.</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">0001 ou<br />
0010<br />
=<br />
0011</div></div>
<p>Podemos verificar o valor em binário de cada uma das constantes, utilizando a função <a href="http://br2.php.net/manual/en/function.printf.php">printf</a>:</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ php <span style="color: #660033;">-r</span> <span style="color: #ff0000;">'printf(&quot;%04b&quot;, E_ERROR);'</span><br />
0001<br />
$ php <span style="color: #660033;">-r</span> <span style="color: #ff0000;">'printf(&quot;%04b&quot;, E_WARNING);'</span><br />
0010<br />
$ php <span style="color: #660033;">-r</span> <span style="color: #ff0000;">'printf(&quot;%04b&quot;, E_PARSE);'</span><br />
0100</div></div>
<p>Portanto, voltando ao exemplo inicial da função error_reporting, temos:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900; font-weight: bold;">E_ERROR</span> <span style="color: #339933;">|</span> <span style="color: #009900; font-weight: bold;">E_WARNING</span></div></div>
<p>E se fizermos a operação, obteremos 0011, vejamos:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ php <span style="color: #339933;">-</span>r <span style="color: #0000ff;">'printf(&quot;%04b&quot;, E_ERROR | E_WARNING);'</span><br />
<span style="color: #208080;">0011</span></div></div>
<p>E como verificamos se <code>E_ERROR</code> esta contido em <code>E_ERROR | E_WARNING</code>?</p>
<p>Simples. Basta utilizar o operador &amp;, que realiza uma operação &#8220;And&#8221;, onde <em>somente são utilizados os bits presentes em A <strong>e</strong> B</em>.</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ERROR</span> <span style="color: #339933;">|</span> <span style="color: #009900; font-weight: bold;">E_WARNING</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> <span style="color: #009900; font-weight: bold;">E_ERROR</span></div></div>
<p>Traduzindo em bits, ficaria:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">0011 e<br />
0001<br />
=<br />
0001</div></div>
<p>Veja que o resultado tem o mesmo valor de B, e é assim que funciona, basta comparar <code>E_ERROR</code> com o resultado de <code>(E_ERROR | E_WARNING) &amp; E_ERROR</code>.</p>
<p>Agora em PHP, já que um exemplo vale mais que mil palavras:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ERROR</span> <span style="color: #339933;">==</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ERROR</span> <span style="color: #339933;">|</span> <span style="color: #009900; font-weight: bold;">E_WARNING</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> <span style="color: #009900; font-weight: bold;">E_ERROR</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'E_ERROR presente nas regras'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>Criando a sua própria série de regras</h3>
<p>Imagine uma situação, onde você possui um sistema com vários usuários, e cada usuário pode conter as permissões P_READ (leitura), P_WRITE (escrita), P_REMOVE (remoção).</p>
<p>Criamos então, uma permissão &#8220;base&#8221;, chamada P_NONE (nada).</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'P_NONE'</span><span style="color: #339933;">,</span>&nbsp; &nbsp; 100<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>E em seguida, as outras permissões:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'P_READ'</span><span style="color: #339933;">,</span>&nbsp; &nbsp; P_NONE <span style="color: #339933;">&lt;&lt;</span> 1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'P_WRITE'</span><span style="color: #339933;">,</span> &nbsp; P_NONE <span style="color: #339933;">&lt;&lt;</span> 2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'P_REMOVE'</span><span style="color: #339933;">,</span>&nbsp; P_NONE <span style="color: #339933;">&lt;&lt;</span> 3<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Logo, podemos criar uma regra para um usuário:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$mode</span> <span style="color: #339933;">=</span> P_READ <span style="color: #339933;">|</span> P_WRITE<span style="color: #339933;">;</span></div></div>
<p>E armazena-la em um banco de dados. Aqui utilizei um campo <code>int</code>, em um banco de dados MySQL.</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;update users set mode = <span style="color: #006699; font-weight: bold;">$mode</span> where id = <span style="color: #006699; font-weight: bold;">$id</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Posteriormente, você pode listar os usuários que tem permissão para escrita:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$mode</span> <span style="color: #339933;">=</span> P_WRITE<span style="color: #339933;">;</span><br />
<span style="color: #000088;">$users</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select * from users where (mode &amp; <span style="color: #006699; font-weight: bold;">$mode</span>) = <span style="color: #006699; font-weight: bold;">$mode</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Para verificar com PHP:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>P_READ <span style="color: #339933;">==</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mode</span> <span style="color: #339933;">&amp;</span> P_READ<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Este usuário possui permissão para leitura.'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>Conclusão</h3>
<p>Você pode especificar várias e várias regras, utilizando apenas um valor, uma variável ou um campo no banco de dados. Basta utilizar alguns bits e operações lógicas.</p>
<p>Para maiores informações sobre os Bitwise Operators do PHP, basta acessar a URL:</p>
<p><a href="http://br2.php.net/language.operators.bitwise">http://br2.php.net/language.operators.bitwise</a></p>
]]></content:encoded>
			<wfw:commentRss>http://caioariede.com/2009/bitwise-operators/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Usando o Git para deploy de sites</title>
		<link>http://caioariede.com/2009/usando-git-para-deploy-sites</link>
		<comments>http://caioariede.com/2009/usando-git-para-deploy-sites#comments</comments>
		<pubDate>Mon, 02 Nov 2009 16:48:40 +0000</pubDate>
		<dc:creator>Caio Ariede</dc:creator>
				<category><![CDATA[Desenvolvimento]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://caioariede.com/?p=568</guid>
		<description><![CDATA[Neste artigo, assume-se que você tenha um site hospedado em um servidor, que esteja rodando Git e SSH.
Repositório local
Crie um repositório local normalmente, caso já tenha, pule esta etapa.
$ mkdir meusite.com.br
$ cd meusite.com.br
$ git init
Repositório remoto
Neste exemplo, vou utilizar a estrutura que o Plesk utiliza para aos domínios.
Precisamos de uma pasta que fique fora de [...]]]></description>
			<content:encoded><![CDATA[<p>Neste artigo, assume-se que você tenha um site hospedado em um servidor, que esteja rodando Git e SSH.</p>
<h2>Repositório local</h2>
<p>Crie um repositório local normalmente, caso já tenha, pule esta etapa.</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> meusite.com.br<br />
$ <span style="color: #7a0874; font-weight: bold;">cd</span> meusite.com.br</div></div>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ git init</div></div>
<h2>Repositório remoto</h2>
<p>Neste exemplo, vou utilizar a estrutura que o Plesk utiliza para aos domínios.</p>
<p>Precisamos de uma pasta que fique fora de visibilidade da web, portanto, utilizarei a pasta private, que fica na raiz do domínio.</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>vhosts<span style="color: #000000; font-weight: bold;">/</span>meusite.com.br<span style="color: #000000; font-weight: bold;">/</span>private</div></div>
<p>Criamos a pasta do repositório:</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> meusite.git<span style="color: #000000; font-weight: bold;">/</span><br />
$ <span style="color: #7a0874; font-weight: bold;">cd</span> meusite.git<span style="color: #000000; font-weight: bold;">/</span></div></div>
<p>Iniciamos um repositório &#8220;crú&#8221; no mesmo local:</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ git <span style="color: #660033;">--bare</span> init</div></div>
<p>E então configuramos o Git com as seguintes opções:</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ git config core.worktree <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>vhosts<span style="color: #000000; font-weight: bold;">/</span>meusite.com.br<span style="color: #000000; font-weight: bold;">/</span>httpdocs<br />
$ git config core.bare <span style="color: #c20cb9; font-weight: bold;">false</span><br />
$ git config receive.denycurrentbranch ignore</div></div>
<p>Configuramos também o Hook que será executado após toda atualização:</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">&gt;</span> hooks<span style="color: #000000; font-weight: bold;">/</span>post-receive<br />
<span style="color: #666666; font-style: italic;">#!/bin/bash</span><br />
<span style="color: #007800;">SITE_PATH</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>vhosts<span style="color: #000000; font-weight: bold;">/</span>meusite.com.br<br />
<br />
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #007800;">$SITE_PATH</span><span style="color: #000000; font-weight: bold;">/</span>httpdocs<br />
git <span style="color: #660033;">--work-tree</span>=. <span style="color: #660033;">--git-dir</span>=<span style="color: #007800;">$SITE_PATH</span><span style="color: #000000; font-weight: bold;">/</span>private<span style="color: #000000; font-weight: bold;">/</span>meusite.git checkout <span style="color: #660033;">-f</span></div></div>
<p>E colocamos permissão para execução:</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">chmod</span> +x hooks<span style="color: #000000; font-weight: bold;">/</span>post-receive</div></div>
<h2>Processo de atualização</h2>
<p>O processo para atualização é bem simples. Primeiro criamos outro &#8220;master&#8221; branch, chamado &#8220;web&#8221;.</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ git remote add web <span style="color: #c20cb9; font-weight: bold;">ssh</span>:<span style="color: #000000; font-weight: bold;">//</span>meusite<span style="color: #000000; font-weight: bold;">@</span>servidor.com.br<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>vhosts<span style="color: #000000; font-weight: bold;">/</span>meusite.com.br<span style="color: #000000; font-weight: bold;">/</span>private<span style="color: #000000; font-weight: bold;">/</span>meusite.git</div></div>
<p>Em seguida, realize qualquer alteração:</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;funcionou!&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> teste.html<br />
$ git commit <span style="color: #660033;">-am</span> <span style="color: #ff0000;">'teste'</span></div></div>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ git push web +master:refs<span style="color: #000000; font-weight: bold;">/</span>heads<span style="color: #000000; font-weight: bold;">/</span>master</div></div>
<p>Tente acessar: http://meusite.com.br/teste.html</p>
<p>Após isso, você pode atualizar somente com &#8220;git push web&#8221;, configurando:</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ git config push.default current</div></div>
<p>Quaisquer dúvidas sobre o Git, podem ser retiradas através do canal #git na irc.freenode.net.</p>
<p>Este material foi baseado no <a href="http://toroid.org/ams/git-website-howto">Using Git to manage a web site</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://caioariede.com/2009/usando-git-para-deploy-sites/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Criando o seu próprio encurtador de URL&#8217;s usando a API do 307.to</title>
		<link>http://caioariede.com/2009/criando-o-seu-proprio-encurtador-de-urls-usando-php-e-a-api-do-307to</link>
		<comments>http://caioariede.com/2009/criando-o-seu-proprio-encurtador-de-urls-usando-php-e-a-api-do-307to#comments</comments>
		<pubDate>Fri, 21 Aug 2009 18:51:45 +0000</pubDate>
		<dc:creator>Caio Ariede</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://caioariede.com/?p=547</guid>
		<description><![CDATA[Demonstrar de forma simples objetiva, que é totalmente possível criar o seu próprio encurtador de URL&#8217;s de maneira simples, sem se preocupar com banco de dados, estatísticas, etc&#8230;
A explicação demonstra como requisitar links, como criar uma página de erro personalizada, e como utilizar o seu domínio para acessa-los.
Porém não há limites. Basta usar o seu [...]]]></description>
			<content:encoded><![CDATA[<p>Demonstrar de forma simples objetiva, que é totalmente possível criar o seu próprio encurtador de URL&#8217;s de maneira simples, sem se preocupar com banco de dados, estatísticas, etc&#8230;</p>
<p>A explicação demonstra como requisitar links, como criar uma página de erro personalizada, e como utilizar o seu domínio para acessa-los.</p>
<p>Porém não há limites. Basta usar o seu conhecimento junto da sua criatividade para criar o seu próprio encurtador, sem maiores preocupações.</p>
<p><a href="http://caioariede.com/arquivos/my-shortener.zip">Faça o download do código completo.</a></p>
<h3>Vamos tomar como exemplo o seguinte cenário:</h3>
<p>Você possui um blog ou uma agência digital, e quer espalhar os seus links através de um domínio próprio, no qual os seus clientes confiem meramente, e que não tenham medo de acessar achando que é algum vírus.</p>
<h3>Neste tutorial, teremos 4 arquivos:</h3>
<p><strong>.htaccess</strong></p>
<p><em>Para melhor o &#8220;visual&#8221; das URL&#8217;s</em></p>
<p><strong>307.class.php</strong><br />
<em>Classe para manipulação de links do 307.to</em></p>
<p><strong>shorturl.php</strong><br />
<em>Onde utilizamos a classe do 307.to</em></p>
<p><strong>404.php</strong><br />
<em>Página de erro personalizada</em></p>
<h3>A classe do 307</h3>
<p>A classe <em>class307</em> possui os seguintes métodos:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> set_format<span style="color: #009900;">&#40;</span><span style="color: #000088;">$format</span><span style="color: #009900;">&#41;</span></div></div>
<p>Específica o formato do retorno, podendo ser json ou text.</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> set_account_key<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span></div></div>
<p>Específica a chave da sua conta.</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$u</span><span style="color: #009900;">&#41;</span></div></div>
<p>Requisita um link.</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> post<span style="color: #009900;">&#40;</span><span style="color: #000088;">$u</span><span style="color: #009900;">&#41;</span></div></div>
<p>Cria um link, se <em>account_key</em> for específicado o link será anexado a sua conta.</p>
<h3>Utilizando a classe</h3>
<p>Esse é o script principal do sistema, onde é utilizada a classe do 307 para requisitar os links.</p>
<p>Como você pode ver abaixo, o método <em>set_account_key</em> foi comentado para fins de demonstração, porém ele é extremamente importante, pois permita que somente os links criados por você possam ser acessados através do seu domínio.</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;height:300px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<br />
<span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'307.class.php'</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$class307</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> class307<span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">// optional</span><br />
<span style="color: #666666; font-style: italic;">//$class307-&gt;set_account_key('&lt;YOUR ACCOUNT KEY&gt;');</span><br />
<br />
try<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_array"><span style="color: #990000;">is_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$class307</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//header('Location: ' . $url, TRUE, 307);</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/header"><span style="color: #990000;">header</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location: http://307.to/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$link</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$error</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'404.php'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Indo logo para a parte mais importante do código, verificamos se a variável &#8220;link&#8221; é do tipo Array ou se esta vazia. Caso um dos dois casos ocorra, geramos uma exceção.</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_array"><span style="color: #990000;">is_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #339933;">...</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Logo depois, requisitamos o endereço através do método <strong>get</strong>:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$class307</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Caso o link não exista, o método <strong>get</strong> gerará uma exceção, caso contrário continuará a execução.</p>
<p>Veja que ocorrendo alguma exceção, o arquivo <strong>404.php</strong> será chamado e logo em seguida a execução terminará.</p>
<h3>Utilizando a URL retornada</h3>
<p>Se o método <strong>get</strong> conseguir encontrar a URL, ela será armazenada na variável <strong>$url</strong>, no script de exemplo, eu somente utilizei o método <em>get</em> para verificar se o link existe ou não, e então redirecionei para a página do próprio 307.to onde serão geradas estatísticas de acesso e tudo mais. Mas nada impede que você faça a sua própria coleta de estatísticas e então redirecione o usuário para a página final.</p>
<h3>Testando o script</h3>
<p>Para testar o script, basta realizar as seguintes requisições:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">http://seusite.com.br/shorturl.php?link=rG</div></div>
<p>e</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">http://seusite.com.br/shorturl.php?link=ESSE_LINK_NAO_EXISTE</div></div>
<p>Veja que a primeira requisição irá redirecionar para http://307.to/rG, enquanto a segunda exibirá uma página de erro (404.php).</p>
<h3>Personalizando a URL (Escondendo o shorturl.php)</h3>
<p>Para encurtar o link, você pode usar algumas regras no .htaccess, que permitem que você esconda o nome do arquivo, no caso &#8220;shorturl.php&#8221;.</p>
<p>.htaccess</p>
<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #00007f;">RewriteEngine</span> <span style="color: #0000ff;">On</span><br />
<br />
<span style="color: #00007f;">RewriteCond</span> %{REQUEST_FILENAME} !-d<br />
<span style="color: #00007f;">RewriteCond</span> %{REQUEST_FILENAME} !-f<br />
<br />
<span style="color: #00007f;">RewriteRule</span> ^(.*)$ shorturl.php?link=$<span style="color: #ff0000;">1</span></div></div>
<p>Agora para acessar o seu link, basta requisitar o endereço:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">http://seusite.com.br/rG</div></div>
<h3>Personalizando a página de erro (404.php)</h3>
<p>Para personalizar a página de erro, basta ter um pouco de conhecimento de HTML:</p>
<p>404.php</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html;charset=UTF-8&quot; /&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
<br />
&lt;h1&gt;Erro - Link não encontrado&lt;/h1&gt;<br />
<br />
&lt;em&gt;<br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<a href="http://www.php.net/printf"><span style="color: #990000;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'%d - %s'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$error</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$error</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&lt;/em&gt;<br />
<br />
&lt;/body&gt;<br />
&lt;/html&gt;</div></div>
<h3>Download do código</h3>
<p>Para fazer o download do código completo citado, basta clicar no link abaixo:</p>
<p><a href="http://caioariede.com/arquivos/my-shortener.zip">Download<br />
</a></p>
]]></content:encoded>
			<wfw:commentRss>http://caioariede.com/2009/criando-o-seu-proprio-encurtador-de-urls-usando-php-e-a-api-do-307to/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Instalando o emesene no Gentoo Linux</title>
		<link>http://caioariede.com/2009/instalando-o-emesene-no-gentoo-linux</link>
		<comments>http://caioariede.com/2009/instalando-o-emesene-no-gentoo-linux#comments</comments>
		<pubDate>Sun, 26 Jul 2009 17:51:41 +0000</pubDate>
		<dc:creator>Caio Ariede</dc:creator>
				<category><![CDATA[Dicas Rápidas]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://caioariede.com/?p=533</guid>
		<description><![CDATA[O emesene (emesene.org), não esta disponível oficialmente nos repositórios do Gentoo, porém pode ser instalado através de um Overlay.
Um Overlay é uma árvore separada da árvore principal de pacotes do Gentoo, mantida por terceiros.
O Overlay onde o emesene esta disponível, chama-se dottout, e requer a instalação do git.
Portanto, primeiramente instale o git:
# emerge dev-util/git
* Vá [...]]]></description>
			<content:encoded><![CDATA[<p>O <strong>emesene</strong> (<a href="http://emesene.org/">emesene.org</a>), não esta disponível oficialmente nos repositórios do Gentoo, porém pode ser instalado através de um Overlay.</p>
<p>Um <strong>Overlay</strong> é uma árvore separada da árvore principal de pacotes do Gentoo, mantida por terceiros.</p>
<p>O Overlay onde o emesene esta disponível, chama-se <strong>dottout</strong>, e requer a instalação do <strong>git</strong>.</p>
<p>Portanto, primeiramente instale o <strong>git</strong>:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"># emerge dev-util/git</div></div>
<p>* Vá tomar um cafézinho, pois demora um pouco!</p>
<p>Depois do café, adicione o Overlay, através do <strong>layman</strong>:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"># layman -a dottout</div></div>
<p>Depois disso, antes de instalar o emesene, é preciso satisfazer uma dependência, que é a <strong>libmimic</strong>.</p>
<p>Crie o arquivo /etc/portage/package.keywords/libmimic, especificando a arquitetura da sua máquina, no meu caso &#8220;~x86&#8243;:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"># cat &gt;/etc/portage/package.keywords/libmimic<br />
media-libs/libmimic ~x86</div></div>
<p>E em seguida dê emerge:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"># emerge media-libs/libmimic</div></div>
<p>Após a instalação, especifique também a arquitetura da sua máquina para a instalação do emesene:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"># cat &gt;/etc/portage/package.keywords/emesene<br />
net-im/emesene ~x86</div></div>
<p>E então, dê emerge:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"># emerge emesene</div></div>
<p>Vá até o diretório do emesene, normalmente localizado em /usr/share/emesene, e execute o <strong>setup.py</strong>:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"># cd /usr/share/emesene<br />
# python setup.py build_ext -i</div></div>
<p>emesene instalado!</p>
<p>* Lembre-se, o emesene não roda como usuário <strong>root</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://caioariede.com/2009/instalando-o-emesene-no-gentoo-linux/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ajudando a traduzir o site do Kohana PHP</title>
		<link>http://caioariede.com/2009/ajudando-a-traduzir-o-site-do-kohana-php</link>
		<comments>http://caioariede.com/2009/ajudando-a-traduzir-o-site-do-kohana-php#comments</comments>
		<pubDate>Thu, 18 Jun 2009 23:01:25 +0000</pubDate>
		<dc:creator>Caio Ariede</dc:creator>
				<category><![CDATA[Kohana]]></category>

		<guid isPermaLink="false">http://caioariede.com/?p=530</guid>
		<description><![CDATA[Esclarecerei aqui, como ajudar na tradução do site do projeto Kohana PHP.
Primeiramente, baixe e instale o Subversion.
Caso você utilize Ubuntu/Linux, Debian, ou alguma distribuição parecida, você pode instalar através do terminal, usando o comando:
sudo apt-get install subversion
Caso você utilize outra distribuição, ou Windows, mais informações para download estão disponíveis em http://subversion.tigris.org/getting.html#binary-packages
Após a instalação
Após instalar o [...]]]></description>
			<content:encoded><![CDATA[<p>Esclarecerei aqui, como ajudar na tradução do site do projeto Kohana PHP.</p>
<p>Primeiramente, baixe e instale o <a href="http://pt.wikipedia.org/wiki/Subversion">Subversion</a>.</p>
<p>Caso você utilize Ubuntu/Linux, Debian, ou alguma distribuição parecida, você pode instalar através do terminal, usando o comando:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">sudo apt-get install subversion</div></div>
<p>Caso você utilize outra distribuição, ou Windows, mais informações para download estão disponíveis em <a href="http://subversion.tigris.org/getting.html#binary-packages">http://subversion.tigris.org/getting.html#binary-packages</a></p>
<h3>Após a instalação</h3>
<p>Após instalar o Subversion, caso esteja no Linux, abra o terminal.</p>
<p>Caso esteja no Windows, vá em Iniciar &gt; Executar, digite &#8220;cmd&#8221; e execute.</p>
<p>Crie uma pasta, no lugar que desejar. Aqui eu optei por ~/kohana.</p>
<p>Esta é a pasta onde você baixará os arquivos do site do Kohana, para fazer isso, vá até a pasta e execute o comando abaixo:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">svn co http://source.kohanaphp.com/branches/website/application kohana-website</div></div>
<p>Esse comando baixará somente a pasta <em>application</em>, pois fora dela, não há conteúdo a ser traduzido.</p>
<p>Após o término, você terá os arquivos em sua máquina.</p>
<h3>Realizando o primeiro Patch</h3>
<p>O Patch, ou Correção, nada mais é que a diferença entre o arquivo antigo e o novo (modificado).</p>
<p>Portanto, você irá modificar o arquivo em questão, e realizar o patch através do SVN mesmo. Ele possui uma ferramenta que faz isso.</p>
<p>Para montar esse Mini How To, eu realizarei um patch, no arquivo: <em>i18n/pt_BR/layout.php</em></p>
<p>Realizei algumas alterações no arquivo, comparando com a versão atual em inglês  <em>i18n/en_US/layout.php</em>, adicionei duas traduções que faltavam: <em>menu_redmine</em> e <em>menu_projects</em>.</p>
<p>Após isso, é bastante simples. Somente fui até a pasta, através do terminal e gerei o patch, com o comando:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">svn diff i18n/pt_BR/layout.php &gt; patch_i18n_pt_BR_layout.patch</div></div>
<p>Eu acho importante manter uma padronização no nome dos patches.</p>
<h3>Finalizando, envio do Patch</h3>
<p>Foi criado um Ticket no site do Kohana, onde devem ser adicionados os patches, para isso basta se cadastrar no site, ir até o Ticket e enviar o arquivo.</p>
<p>O Ticket criado para isso, foi o <a href="http://dev.kohanaphp.com/issues/1799">#1799</a>.</p>
<p>Espero que esse documento seja de bastante utilidade, e que todos consigam compreender bem.</p>
]]></content:encoded>
			<wfw:commentRss>http://caioariede.com/2009/ajudando-a-traduzir-o-site-do-kohana-php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kohana PHP: Brincando com os Hooks</title>
		<link>http://caioariede.com/2009/kohana-php-brincando-com-os-hooks</link>
		<comments>http://caioariede.com/2009/kohana-php-brincando-com-os-hooks#comments</comments>
		<pubDate>Tue, 26 May 2009 13:59:11 +0000</pubDate>
		<dc:creator>Caio Ariede</dc:creator>
				<category><![CDATA[Kohana]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://caioariede.com/?p=525</guid>
		<description><![CDATA[Os Hooks do Kohana PHP são realmente úteis. Demonstrarei como utiliza-los, exemplificando a sua utilização em uma situação real.
O que são Hooks?
Hooks são arquivos que são carregados na inicialização do Kohana, permitindo você adicionar ou modificar funções, chamadas callbacks, que são disparadas pelo Kohana.
Essas funções (ou callbacks) são colocados em uma lista dentro do seu [...]]]></description>
			<content:encoded><![CDATA[<p>Os Hooks do <a href="http://kohanaphp.com/">Kohana PHP</a> são realmente úteis. Demonstrarei como utiliza-los, exemplificando a sua utilização em uma situação real.</p>
<h3>O que são Hooks?</h3>
<p>Hooks são arquivos que são carregados na inicialização do Kohana, permitindo você adicionar ou modificar funções, chamadas <em>callbacks</em>, que são disparadas pelo Kohana.</p>
<p>Essas funções (ou <em>callbacks</em>) são colocados em uma lista dentro do seu evento específico, chamada Event Queue.</p>
<h3>O que são Eventos?</h3>
<p>Durante a inicialização, e até mesmo durante a execução, em alguns casos, o núcleo do Kohana dispara vários eventos. Eventos que são definidos pelo próprio núcleo.</p>
<p>Você pode ver a lista completa de eventos disparados pelo Kohana, na documentação: <a href="http://docs.kohanaphp.com/general/events">http://docs.kohanaphp.com/general/events</a></p>
<h3>Habilitando os Hooks no Kohana</h3>
<p>Para começar a brincar com os Hooks, é necessário habilitá-los. Para isso, basta configurar a opção <em>enable_hooks</em> no <em>config/config.php</em>, para <em>TRUE</em>:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'enable_hooks'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span></div></div>
<h3>Exemplo de utilização de Hooks</h3>
<p>Suponha que tenhamos um site com sistema de autenticação.</p>
<p>Quando um visitante chega, ele é logo direcionado ao <em>controller</em> padrão, definido em <em>config/routes.php</em>, pela seguinte linha:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'_default'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'site'</span><span style="color: #339933;">;</span></div></div>
<p style="text-align: justify;">O <em>controller</em> site, contém os métodos (páginas): Home (com formulário de autenticação), Quem Somos, Ajuda&#8230;</p>
<p>Agora, o que fazer quando este visitante já estava autenticado anteriormente, e retorna ao site?</p>
<p>A solução mais simples a se fazer, é criar uma verificação no <em>controller</em> site, e caso o usuário já esteja autenticado ele será redirecionado à outro <em>controller</em>, chamado <span style="text-decoration: underline;">painel</span>.</p>
<h4>Problema</h4>
<p>A cada redirecionamento do <em>controller</em> site ao controller <em>painel</em>, seria realizado outra inicialização do sistema completo.</p>
<h4>Solução</h4>
<p>Uma solução elegante e correta para isso, é utilizar um Hook para informar ao <em>Router</em>, o <em>controller</em> correto a ser chamado.</p>
<p>O <em>Router</em> é quem define através da URL, qual <em>controller</em> deverá ser chamado, portanto a única coisa que terá de ser feita, é dizer à ele qual <em>controller</em> ele deverá chamar.</p>
<p>Criamos então, o arquivo <em>hooks/site.php</em>, o nome não importa, a não ser por fácil identificação posteriormente.</p>
<p>No arquivo, é especificado que no momento do evento <em>system.routing</em>, uma função (leia-se <em>callback</em>) será chamada, antes do <em>Router::setup</em>.</p>
<p>Esse <em>callback</em> definirá o <em>controller</em> correto à ser carregado.</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <a href="http://www.php.net/defined"><span style="color: #990000;">defined</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SYSPATH'</span><span style="color: #009900;">&#41;</span> OR <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No direct access allowed.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> hook_define_controller_padrao<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span>Router<span style="color: #339933;">::</span><span style="color: #000088;">$current_uri</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>Session<span style="color: #339933;">::</span><span style="color: #004000;">instance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// usuário logado</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Router<span style="color: #339933;">::</span><span style="color: #000088;">$current_uri</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'painel'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Você pode também impossibilitar o acesso à página de autenticação, quando o usuário já estiver autenticado, modificação a função <em>callback</em> acima, da seguinte forma:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> hook_define_controller_padrao<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$logado</span> <span style="color: #339933;">=</span> Session<span style="color: #339933;">::</span><span style="color: #004000;">instance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span>Router<span style="color: #339933;">::</span><span style="color: #000088;">$current_uri</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$logado</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// usuário autenticado</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Router<span style="color: #339933;">::</span><span style="color: #000088;">$current_uri</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'painel'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span>Router<span style="color: #339933;">::</span><span style="color: #000088;">$current_uri</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'site/login'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$logado</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// exibir &quot;página não encontrada&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Event<span style="color: #339933;">::</span><span style="color: #004000;">run</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'system.404'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Criada a função, é necessário que seja adicionada à lista de eventos a serem realizados, isso pode ser feito logo abaixo da função, ou no final do arquivo Hook.</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Event<span style="color: #339933;">::</span><span style="color: #004000;">add_before</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'system.routing'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Router'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'setup'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'hook_define_controller_padrao'</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Existe também como definir novos eventos, mas isso fica para outro artigo, pois este é somente uma introdução sobre o assunto.</p>
<p>Utilize os Hooks sempre que possível, eles poderão reduzir inúmeras linhas no seu código, tornando-o mais enxuto, legível e organizado.</p>
]]></content:encoded>
			<wfw:commentRss>http://caioariede.com/2009/kohana-php-brincando-com-os-hooks/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TTYtter: Twitter no console</title>
		<link>http://caioariede.com/2009/ttytter-twitter-no-console</link>
		<comments>http://caioariede.com/2009/ttytter-twitter-no-console#comments</comments>
		<pubDate>Sat, 23 May 2009 19:17:35 +0000</pubDate>
		<dc:creator>Caio Ariede</dc:creator>
				<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://caioariede.com/?p=514</guid>
		<description><![CDATA[Aplicativos modo-texto são realmente muito úteis, é claro que, para quem sabe como utilizá-los de forma adequada, e digamos que produtiva.
Tenho visto em muitos blogs, pessoas ensinando como usar o Twitter através da linha de comando, com o cURL. Apesar de ser uma forma interessante, não é algo prático, o que torna a utilização no [...]]]></description>
			<content:encoded><![CDATA[<p>Aplicativos modo-texto são realmente muito úteis, é claro que, para quem sabe como utilizá-los de forma adequada, e digamos que produtiva.</p>
<p>Tenho visto em muitos blogs, pessoas ensinando como usar o Twitter através da linha de comando, com o cURL. Apesar de ser uma forma interessante, não é algo prático, o que torna a utilização no dia-a-dia inviável, principalmente para pessoas que utilizam muito o Twitter.</p>
<p>Assim como já escrevi sobre o cmus, <a href="http://caioariede.com/2008/music-player-command-line">um tocador de músicas com playlist, modo texto</a>, resolvi escrever um pouco sobre TTYtter para demonstrar que é possível utilizar aplicativos na linha de comando, de uma forma viável.</p>
<h3>TTYtter</h3>
<p>O TTytter é um cliente para Twitter interativo e multi-funcional, que nada mais é que um script Perl.</p>
<p>A sua página oficial é <a href="http://www.floodgap.com/software/ttytter/">http://www.floodgap.com/software/ttytter/</a></p>
<h3>Instalação</h3>
<p>Na verdade não é bem uma instalação, basicamente você terá de baixar o script, e só. Por ser um script Perl, não há o processo de compilação, bastando somente você ter o Perl instalado (o que é muito provável que já esteja).</p>
<p>A instalação tem como base o sistema operacional Linux, podendo ser aplicada também a sistemas Unix-like.</p>
<p>Você pode baixa-lo com o <em>wget</em> direto para o seu diretório de programas ou <em>/usr/local/bin</em>, da seguinte forma:</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>www.floodgap.com<span style="color: #000000; font-weight: bold;">/</span>software<span style="color: #000000; font-weight: bold;">/</span>ttytter<span style="color: #000000; font-weight: bold;">/</span>ttytter.txt <span style="color: #660033;">-O</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>ttytter</div></div>
<p>A opção <em>-O</em> utilizada no <em>wget</em> especifica o arquivo onde será salvo.</p>
<p>Após isso, dê permissão para execução ao ttytter:</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chmod</span> +x <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>ttytter</div></div>
<p>Retire o <em>sudo</em>, caso não seja necessário na distribuição que esteja usando.</p>
<h3>Utilização</h3>
<p>Você pode iniciar o TTYtter manualmente, digitando:</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ ttytter <span style="color: #660033;">-user</span>=seuusuario:suasenha <span style="color: #660033;">-ansi</span></div></div>
<p>A opção <em>-ansi</em> é para habilitar as cores.</p>
<p>Ou então, para facilitar, pode-se criar um script:</p>
<p>Crie um arquivo com um nome qualquer e coloque o conteúdo abaixo dentro dele.</p>
<p>Aqui eu criei um arquivo chamado <em>ttytter</em>, dentro da pasta <em>app</em>.</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#!/bin/bash</span><br />
<span style="color: #007800;">usuario</span>=caioariede<br />
<span style="color: #c20cb9; font-weight: bold;">read</span> <span style="color: #660033;">-s</span> <span style="color: #660033;">-p</span> <span style="color: #ff0000;">&quot;Senha: &quot;</span> senha<br />
ttytter <span style="color: #660033;">-user</span>=<span style="color: #007800;">$usuario</span>:<span style="color: #007800;">$senha</span> <span style="color: #660033;">-ansi</span></div></div>
<p>Dê ao arquivo permissão para execução:</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">chmod</span> +x app<span style="color: #000000; font-weight: bold;">/</span>ttytter</div></div>
<p>Configure o script com os seus dados e execute-o:</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ .<span style="color: #000000; font-weight: bold;">/</span>app<span style="color: #000000; font-weight: bold;">/</span>ttytter</div></div>
<h3>Comandos</h3>
<p><strong>/refresh</strong></p>
<p>Atualização, busca por novos tweets.</p>
<p><strong>/whois</strong></p>
<p>Exibe informações do perfil do usuário.</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">TTYtter&gt; /whois caioariede<br />
caio (caioariede) (f:116/176) (u:3014)<br />
&quot;php programmer, python and linux enthusiast, workaholic,<br />
learning erlang, using ion3 and vim&quot;<br />
Location:   Bauru<br />
URL:        ...<br />
Picture:    ...</div></div>
<p><strong>/replies</strong></p>
<p>Exibe seus últimos &#8220;replies&#8221; e menções (tweets referindo o seu usuário)</p>
<p><strong>/dmagain</strong></p>
<p>Exibe suas últimas &#8220;direct messages&#8221;</p>
<p><strong>/reply</strong></p>
<p>Responde a um tweet.</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">g4&gt; &lt;foo&gt; mensagem<br />
TTYtter&gt; /reply g4 mensagem de resposta</div></div>
<p>A mensagem enviada será um <em>reply</em>:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@foo mensagem de resposta</div></div>
<p><strong>/delete</strong></p>
<p>Remove um tweet, se for seu é claro.</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">g5&gt; &lt;foo&gt; mensagem teste<br />
/delete g5</div></div>
<p><strong>/help</strong></p>
<p>Use para ver outros comandos e informações sobre o TTYtter.</p>
]]></content:encoded>
			<wfw:commentRss>http://caioariede.com/2009/ttytter-twitter-no-console/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Kohana PHP: Boas práticas de desenvolvimento</title>
		<link>http://caioariede.com/2009/kohanaphp-boas-praticas-de-desenvolvimento</link>
		<comments>http://caioariede.com/2009/kohanaphp-boas-praticas-de-desenvolvimento#comments</comments>
		<pubDate>Wed, 20 May 2009 13:03:19 +0000</pubDate>
		<dc:creator>Caio Ariede</dc:creator>
				<category><![CDATA[Kohana]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://caioariede.com/?p=453</guid>
		<description><![CDATA[Este artigo tem como objetivo relembrar algumas das funcionalidades disponibilizadas pelo excelente framework Kohana PHP. Funcionalidades estas que podem facilitar, e muito, na hora de desenvolver ou refatorar uma aplicação.
Serve também para aqueles que estão começando a se familiarizar com o framework e querem aprender um pouco mais sobre as possibilidades que ele oferece.
Singleton
Singleton é [...]]]></description>
			<content:encoded><![CDATA[<p>Este artigo tem como objetivo relembrar algumas das funcionalidades disponibilizadas pelo excelente framework <a href="http://kohanaphp.com/">Kohana PHP</a>. Funcionalidades estas que podem facilitar, e muito, na hora de desenvolver ou refatorar uma aplicação.</p>
<p>Serve também para aqueles que estão começando a se familiarizar com o framework e querem aprender um pouco mais sobre as possibilidades que ele oferece.</p>
<h3>Singleton</h3>
<p>Singleton é um padrão (Design Pattern) que garante que suas classes serão instânciadas somente uma vez, podendo ser acessadas de forma global, evitando a criação de várias instâncias desnecessarias da mesma.</p>
<p>No KohanaPHP isso pode ser aplicado nas bibliotecas, pois todas as bibliotecas tem suporte a Singleton.</p>
<p>Ao invés de criar uma nova instância:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">session</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Session<span style="color: #339933;">;</span></div></div>
<p>Você utiliza o método estático <em>instance</em>:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">session</span> <span style="color: #339933;">=</span> Session<span style="color: #339933;">::</span><span style="color: #004000;">instance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>O que o método <em>instance</em> cria uma nova instância, armazena-a em uma variável, e a retorna. Na próxima chamada é feita uma verificação, caso uma variável com a instância já exista, ao invés de criar uma nova, ele retorna a instância contida na variável.</p>
<h3>Utilize o Query Builder</h3>
<p>Muita gente odeia Query Builders por serem um tanto limitados, porém se você quiser que a sua aplicação seja independente de banco de dados, use-o. Deixe para escrever SQL&#8217;s somente quando for realmente necessários.</p>
<p>O Query Builder suporta <em>method chaining</em>, o que pode deixar o código mais elegante.</p>
<p><em>Exemplos de Method Chaining com Query Builder</em></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id, nome'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tabela'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'categoria'</span> <span style="color: #339933;">=&gt;</span> 1<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">-&gt;</span><span style="color: #004000;">limit</span><span style="color: #009900;">&#40;</span>10<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">-&gt;</span><span style="color: #004000;">getwhere</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tabela'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'categoria'</span> <span style="color: #339933;">=&gt;</span> 2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p><em>Contando total de registros em uma tabela</em></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$total_usuarios</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count_records</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'usuarios'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>Utilize segmentos, de preferência nomeados</h3>
<p>Ao invés de usar variáveis <em>$_GET</em>, dê preferência aos segmentos. Além de mais elegantes, são mais fáceis de serem manipulados.</p>
<p>http://meusite/clientes/busca/<strong>cidade</strong>/Bauru/<strong>estado</strong>/SP</p>
<p>Onde <em>clientes</em> é o controller, <em>busca</em> é o método, e <em>cidade</em> e <em>estado</em> são segmentos.</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$cidade</span> <span style="color: #339933;">=</span> uri<span style="color: #339933;">::</span><span style="color: #004000;">segment</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cidade'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$estado</span> <span style="color: #339933;">=</span> uri<span style="color: #339933;">::</span><span style="color: #004000;">segment</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'estado'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>Nunca modifique os arquivos do diretório <em>system</em></h3>
<p>Os recursos disponíveis pelo Kohana, são carregados no formato de cascata, ou seja, primeiro ele verifica se o arquivo existe no diretório <em>application</em>, caso não exista, então procura nos diretórios de cada módulo, em seguida no diretório <em>system</em>.</p>
<p>Um bom exemplo são os arquivos de configuração, localizados no diretório <em>system/config/</em>.</p>
<p>O caminho percorrido até ele chegar em qualquer arquivo dentro deste diretório, é o seguinte:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">application/config/auth.php<br />
--&gt; modules/archive/config/auth.php<br />
--&gt; modules/flot/config/auth.php<br />
--&gt; modules/auth/config/auth.php (encontrado)</div></div>
<p>Caso não fosse encontrado, ele continuaria a pesquisar, terminando no diretório <em>system</em>:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">system/config/auth.php</div></div>
<p>Por isso é muito importante transferir todos os arquivos necessários para o diretório <em>application</em>, e nunca modifica-los nos seus diretórios de origem.</p>
<h3>Use os helpers e bibliotecas disponíveis</h3>
<p>O Kohana disponibiliza uma série de helpers e bibliotecas que podem facilitar e muito o desenvolvimento. Não invente a roda à todo momento.</p>
<p><em>Exemplo de utilização do helper Upload</em></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'arquivo'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$tmp_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'arquivo'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tmp_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$filename_path</span> <span style="color: #339933;">=</span> upload<span style="color: #339933;">::</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><br />
<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$filename</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'tmp_name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$tmp_name</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/basename"><span style="color: #990000;">basename</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename_path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p><em>Exemplo de utilização da biblioteca Pagination</em></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pagination</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Pagination<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><br />
<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'base_url'</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'noticias/page/'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'uri_segment'</span> &nbsp; &nbsp;<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'page'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'total_items'</span> &nbsp; &nbsp;<span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$total_num_rows</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'items_per_page'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$rows</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'style'</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'digg'</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pagination</span><span style="color: #339933;">;</span></div></div>
<p>No exemplo acima, será impressa uma paginação no estilo Digg.</p>
<p>Existem outros estilos de paginação que podem ser conferidos através da documentação:</p>
<p>http://docs.kohanaphp.com/libraries/pagination</p>
<h3>Estenda os helpers, sempre que necessário</h3>
<p>Uma prática muito encorajada no Kohana é a extensão dos helpers para se adequar as suas necessidades.</p>
<p>Por exemplo, podemos estender o helper <em>format</em>, para formatar também o tamanho de arquivos, criando o arquivo <em>MY_format.php</em> dentro do diretório helpers da sua apliação, geralmente <em>application/helpers</em>.</p>
<p><em>applications/helpers/MY_format.php</em></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <a href="http://www.php.net/defined"><span style="color: #990000;">defined</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SYSPATH'</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No direct script access.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> format <span style="color: #000000; font-weight: bold;">extends</span> format_Core<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> <a href="http://www.php.net/filesize"><span style="color: #990000;">filesize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$sizes</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'B'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'kB'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'MB'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'GB'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'TB'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'PB'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'EB'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ZB'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'YB'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sizes</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$size</span> <span style="color: #339933;">&gt;</span> 1024 <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$size</span> <span style="color: #339933;">/=</span> <span style="color: #cc66cc;">1024</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <a href="http://www.php.net/round"><span style="color: #990000;">round</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #339933;">,</span> 2<span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$sizes</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">echo</span> format<span style="color: #339933;">::</span><a href="http://www.php.net/filesize"><span style="color: #990000;">filesize</span></a><span style="color: #009900;">&#40;</span>1024<span style="color: #339933;">*</span>4<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 4kB</span></div></div>
<p>Lembre-se, todo arquivo de extensão deve ser iniciado por <em>MY_</em>, é um padrão definido pelo Kohana.</p>
<h3>Internacionalização</h3>
<p>Criar uma aplicação multi-idiomas com o Kohana é relativamente simples.</p>
<p>Primeiro crie o diretório <em>i18n</em> dentro de <em>applications</em>, e em seguida crie o diretório <em>pt_BR</em>.</p>
<p><em>applications/i18n/pt_BR/</em></p>
<p>Após isso, as strings são divididas entre arquivos, dentro desse diretório, por exemplo:</p>
<p><em>applications/i18n/pt_BR/caption.php</em></p>
<p>Dentro de cada arquivo, são especificadas as strings:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <a href="http://www.php.net/defined"><span style="color: #990000;">defined</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SYSPATH'</span><span style="color: #009900;">&#41;</span> OR <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No direct access allowed.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$lang</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><br />
<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'submit'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Enviar'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'cancel'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Cancelar'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'confirm'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Confirmar'</span><span style="color: #339933;">,</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">echo</span> Kohana<span style="color: #339933;">::</span><span style="color: #004000;">lang</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'caption.submit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Enviar</span></div></div>
<p>Também é permitido a utilização de Arrays:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">'confirm'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><br />
<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'edit?'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Editar texto?'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'remove?'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Remover texto?'</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">echo</span> Kohana<span style="color: #339933;">::</span><span style="color: #004000;">lang</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'message.confirm.edit?'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Editar texto?</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://caioariede.com/2009/kohanaphp-boas-praticas-de-desenvolvimento/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>PHP para Web Designers</title>
		<link>http://caioariede.com/2009/php-para-web-designers</link>
		<comments>http://caioariede.com/2009/php-para-web-designers#comments</comments>
		<pubDate>Sat, 16 May 2009 18:16:23 +0000</pubDate>
		<dc:creator>Caio Ariede</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://caioariede.com/?p=440</guid>
		<description><![CDATA[Objetivo
O objetivo desse material, é disponibilizar um caminho para os Web Designers aprenderem um pouco mais sobre PHP, explicando algumas instruções básicas (se-então-senão, include, por exemplo), e algumas funções que geralmente são úteis, e facilitam o desenvolvimento básico de sites e layouts.
O manual do PHP
Tudo o que será demonstrado nesse artigo, esta documentado no manual [...]]]></description>
			<content:encoded><![CDATA[<h3>Objetivo</h3>
<p>O objetivo desse material, é disponibilizar um caminho para os Web Designers aprenderem um pouco mais sobre PHP, explicando algumas instruções básicas (se-então-senão, include, por exemplo), e algumas funções que geralmente são úteis, e facilitam o desenvolvimento básico de sites e layouts.</p>
<h3>O manual do PHP</h3>
<p>Tudo o que será demonstrado nesse artigo, esta documentado no manual oficial do PHP, onde inclusive existem exemplos e comentários de outras pessoas.</p>
<p>O manual do PHP é uma ótima fonte de referência. Lá você tem uma explicação detalhada para a utilização de praticamente todas as funções, e pode ser encontrado aqui: <a href="http://www.php.net/manual/pt_BR/index.php">http://www.php.net/manual/pt_BR/index.php<br />
</a></p>
<p>Uma forma rápida de acesso ao manual, é ir direto na página da função desejada, por exemplo:</p>
<p><a href="http://php.net/include">http://php.net/include</a><br />
<a href="http://php.net/echo">http://php.net/echo</a><br />
<a href="http://php.net/date">http://php.net/date</a><br />
<a href="http://php.net/string">http://php.net/string</a></p>
<h3>Os tipos de dados</h3>
<p>Os principais tipos de dados em PHP, são: Booleanos, Inteiros, Números de ponto flutuante e Strings.</p>
<p>Não abordarei os demais tipos de dados, pois não serão comentados neste material introdutório.</p>
<p><strong>Booleano</strong>, é o tipo de dado Sim / Não, Verdadeiro / Falso, 1 / 0.</p>
<p>Em PHP são descritos como <strong>true</strong> e <strong>false</strong>, ou <strong>1</strong> e <strong>0</strong>. Onde 1 é true, e 0 é false.</p>
<p><strong>Inteiros</strong> (<strong>Z</strong>), os que você aprendeu no ensino fundamental: &#8230;, -2, -1, 0, 1, 2, &#8230;</p>
<p><strong>Números de Ponto Flutuante</strong>, são números reais: -1.15, 0.333333, 1, 1.5, 400.20, &#8230;</p>
<p>e finalmente, uma <strong>String</strong> é uma cadeia de caracteres (texto, frase, palavra&#8230;):</p>
<p>Uma string em PHP é descrita dentre &#8216; (aspas simples) ou &#8221; (aspas duplas).</p>
<p>A diferença entre uma string entre aspas simples, e outra entre aspas duplas, é que a string entre aspas duplas, sempre espera por receber uma variável (dentro dela), como será visto mais além, durante a leitura.</p>
<p>&#8220;String entre aspas duplas&#8221;</p>
<p>&#8216;String entre aspas simples&#8217;</p>
<h3>Comentários</h3>
<p>Assim como no HTML, o PHP também possui comentários, vejamos:</p>
<p>Em HTML:</p>
<div class="codecolorer-container xml twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">&lt;!-- Comentário no meio de um arquivo HTML --&gt;</span></div></div>
<p>em PHP:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Comentário no meio de um arquivo PHP (somente uma linha)</span><br />
<br />
<span style="color: #666666; font-style: italic;">/*<br />
Comentário para<br />
várias linhas, em PHP<br />
(várias linhas)<br />
*/</span><br />
<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<h3><span id="more-440"></span></h3>
<h3>As condições If-then-else (Se-então-senão)</h3>
<p>Não se assute com essa parte do material, pois é só um pré-requisito para o melhor entendimento do material por um todo. Você conseguirá entender melhor isso conforme o andamento da leitura, vendo a aplicação disso na prática.</p>
<p>O <strong>if-then-else</strong> trabalha em cima de condições, condições que são geralmente são avaliadas através de Operadores Comparativos e Operadores Lógicos.</p>
<p>Os principais <strong>Operadores Comparativos</strong>, são: == (igual), != (diferente), &lt; (menor), &gt; (maior), &lt;= (menor ou igual), &gt;= (maior ou igual).</p>
<p>Os principais <strong>Operadores Lógicos</strong>, são: &amp;&amp; (e), || (ou) e ! (negação).</p>
<p>Na prática, isso tudo é muito simples:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #cc66cc;">1</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> é igual à <span style="color: #cc66cc;">1</span><br />
<span style="color: #cc66cc;">1</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> é diferente de <span style="color: #cc66cc;">2</span><br />
<br />
<span style="color: #cc66cc;">1</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> é menor que <span style="color: #cc66cc;">2</span><br />
<span style="color: #cc66cc;">2</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span> é maior que <span style="color: #cc66cc;">1</span><br />
<br />
<span style="color: #cc66cc;">1</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> é menor ou igual à <span style="color: #cc66cc;">2</span><br />
<span style="color: #cc66cc;">2</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span> é maior ou igual à <span style="color: #cc66cc;">2</span></div></div>
<p>Agora em conjunto com os <strong>Operadores Lógicos</strong>:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #cc66cc;">1</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> é igual à <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> E<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> é menor que <span style="color: #cc66cc;">2</span><span style="color: #339933;">.</span><br />
<span style="color: #cc66cc;">2</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span> é maior que <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> E<span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span> é maior ou igual à <span style="color: #cc66cc;">2</span><span style="color: #339933;">.</span></div></div>
<p>A sintaxe do <strong>if-then-else</strong> é mais simples ainda:</p>
<p><strong>If</strong></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">se <span style="color: #009900;">&#40;</span>verdade<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> então<br />
&nbsp; &nbsp; faz isso<br />
fim</div></div>
<p><strong>If, Else</strong></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">se <span style="color: #009900;">&#40;</span>verdade<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> então<span style="color: #339933;">:</span><br />
&nbsp; &nbsp; faz isso<br />
senão<br />
&nbsp; &nbsp; faz isso <span style="color: #cc66cc;">2</span><br />
fim</div></div>
<p>Agora, traduzindo para o PHP, seria:</p>
<p><strong>If</strong></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>condicao <span style="color: #b1b100;">for</span> verdade<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'faz isso'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><strong>If, Else</strong></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>condicao <span style="color: #b1b100;">for</span> verdade<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'faz isso'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #b1b100;">else</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'faz isso 2'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>O PHP também suporta o <strong>Else-If</strong> (Senão-se):</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>numero <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Numéro negativo</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span>numero <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Número menor que 5</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span>numero <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Número menor ou igual a 10</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Um número qualquer, acima de 10</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>Existe também outra forma de se fazer um If, que é chamado de Operador Ternário:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CONDICAO ? ENTAO <span style="color: #339933;">:</span> SENAO</div></div>
<p>em PHP:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 1 <span style="color: #339933;">==</span> 1 ? 1 <span style="color: #339933;">:</span> O<span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>Explicando: <strong>Se</strong> 1 é igual a 1, <strong>então</strong> 1, <strong>senão</strong> 0.</p>
<h3>O include</h3>
<p>A instrução <strong>include</strong> pode facilitar muito na criação do layout, pois possibilita separar o layout em arquivos, como no exemplo abaixo:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'cabecalho.php'</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
<br />
Olá Mundo!<br />
<br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'rodape.php'</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p><strong>Notas</strong></p>
<ul>
<li>O arquivo utilizado no <em>include</em> não precisa necessáriamente ser PHP, pode ser qualquer tipo de arquivo de texto plano, ou seja, nada de imagens, claro.</li>
<li>O que o <em>include</em> faz, basicamente, é embutir o arquivo especificado formando um único arquivo.</li>
<li>Um <em>include</em> pode também conter outros <em>include</em>&#8217;s. Você pode ter um include chamado menu.php dentro do seu cabecalho.php.</li>
</ul>
<h3>Utilização de Variáveis</h3>
<p>Suponha que o arquivo <strong>cabecalho.php</strong> contenha o seguinte trecho de código:</p>
<div class="codecolorer-container xml twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Título da Página<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>Veja que se você utilizar o arquivo <strong>cabecalho.php</strong>, em todas as suas páginas, todas as páginas terão o mesmo título, pois ele esta definido de forma estática, imutável.</p>
<p>Caso queira que cada página contenha um título diferente, por exemplo: Início, Blog e Contato.</p>
<p>Você deve especificar que o arquivo <strong>cabecalho.php</strong> contém uma área de conteúdo variável, sujeita a modificações, mutável. Isso é feito através da utilização de variáveis.</p>
<p>Como exemplo, um arquivo chamado <strong>contato.php</strong>, conteria o seguinte trecho de código:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$titulo_da_pagina</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Contato'</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'cabecalho.php'</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
<br />
Entre em contato conosco... Telefone: (99) 9999-9999<br />
<br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'rodape.php'</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>E no arquivo <strong>cabecalho.php</strong> seria especificada a variável, no lugar do título:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;html&gt;<br />
&lt;head&gt;<br />
&nbsp; &nbsp; &lt;title&gt;Meu Site | <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$titulo_da_pagina</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/title&gt;<br />
&lt;/head&gt;</div></div>
<p>A instrução <strong>echo</strong>, basicamente imprime o que é passado de parâmetro. No caso acima, uma variável, que foi pré-definida com o conteúdo: Contato</p>
<p>A sintaxe da variável é simples. Para a criação, utiliza-se:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$nomedavariavel</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Conteudo da variavel'</span><span style="color: #339933;">;</span></div></div>
<p>O $ (sifrão) é o que identifica uma variável. O nome das variáveis podem conter somente letras, números e _ (underline), mas nunca devem começar com número. O conteúdo delas pode variar:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$se_for_um_texto</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Utilize aspas simples...'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$se_for_um_texto</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ou aspas duplas&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$se_for_um_numero</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">15</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$se_for_um_valor</span> <span style="color: #339933;">=</span> <span style="color:#800080;">99.99</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>Para imprimir uma variável é mais simples:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$nomedavariavel</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<h3>Utilizando uma variável para especificar um include</h3>
<p>Como nós vimos, a sintaxe para um include é a seguinte:</p>
<p style="padding-left: 30px;">include &lt;STRING&gt;;</p>
<p>Para você poder inserir uma variável dentro da string, a string deverá iniciar e terminar com &#8221; (aspas duplas). Pois uma string com aspas duplas, indica que ela poderá conter variáveis, então tratará o $ (sifrão) de forma diferente, como se fosse uma variável.</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$pagina</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'historia'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// não haverá nenhuma variável no meio da string</span><br />
<br />
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;includes/<span style="color: #006699; font-weight: bold;">$pagina</span>.php&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// existe uma variável no meio da string</span></div></div>
<p>Lembre-se, que se você utilizar aspas simples em uma string que contém uma variável, o sifrão será impresso como se fosse um sifrão mesmo:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'minha variável $teste'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// imprimirá: minha variável $teste</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p><strong>Notas</strong></p>
<ul>
<li>Lembre-se de utilizar o &#8220;<strong>;</strong>&#8220;, caso contrário terá problemas.</li>
<li>Prefira usar <strong>&#8216;</strong> na maioria das vezes, use <strong>&#8220;</strong> apenas quando for necessário colocar uma variável dentro da string.</li>
<li>Em alguns códigos, você pode encontrar a função &#8220;<strong>print</strong>&#8221; no lugar do &#8220;<strong>echo</strong>&#8220;, as duas fazem praticamente a mesma coisa, mas dê sempre preferência ao <strong>echo</strong>.</li>
</ul>
<h3>Verificação de tipos de variáveis</h3>
<p>Você pode verificar se uma variável é do tipo que você espera, exemplos:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_int"><span style="color: #990000;">is_int</span></a><span style="color: #009900;">&#40;</span>1<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'verdadeiro'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_numeric"><span style="color: #990000;">is_numeric</span></a><span style="color: #009900;">&#40;</span>1<span style="color: #339933;">.</span>5<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'verdadeiro'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_string"><span style="color: #990000;">is_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'foobar'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'verdadeiro'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_string"><span style="color: #990000;">is_string</span></a><span style="color: #009900;">&#40;</span>100<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'falso'</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<h3>Variáveis Globais: $_GET e $_POST</h3>
<p>Supondo que você tenha uma página chamada <strong>teste.php</strong>, vamos fazer alguns testes com essas variáveis globais.</p>
<p>A variável global <strong>$_GET</strong>, serve para transportar uma variável da URL para dentro do seu código, por exemplo, tendo o <strong>teste.php</strong> com o seguinte conteúdo:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pagina'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>Ao chamarmos: http://seusite/teste.php?pagina=exemplo, você terá:</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">exemplo</div></div>
<p>E basicamente é isso. Porém também temos que trabalhar visando que essa variável pode não existir, então é necessário trata-la, para evitar erros, então usamos a condição <strong>if-then-else</strong> junto à função <strong>empty</strong>.</p>
<p>A função <strong>empty</strong> retorna <strong>TRUE</strong>, caso a variável:</p>
<ol>
<li>Não exista</li>
<li>Seja 0 (zero)</li>
<li>Seja false (booleano)</li>
<li>Esteja em branco</li>
</ol>
<p>Caso contrário, retorna <strong>true</strong>.</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pagina'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'a variável página não existe, ou esta vazia'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pagina'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>As variáveis <strong>$_GET</strong> são comumente utilizadas em <strong>includes</strong>, porém deve haver um <strong>cuidado muito grande</strong>.</p>
<p>Sempre verifique qual o conteúdo da variável antes de coloca-la no include, por exemplo:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pagina'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$pagina</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">else</span> <span style="color: #000088;">$pagina</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pagina'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pagina</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'historia'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'historia.php'</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pagina</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'contato'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'contato.php'</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pagina</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'noticias'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'noticias.php'</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'default.php'</span><span style="color: #339933;">;</span></div></div>
<p><strong>NUNCA utilize:</strong></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">include</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pagina'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span></div></div>
<p>Pois isso poderá ocasionar uma invasão no seu site, mas isso não é o foco do artigo, apenas não faça-o, tenha total controle do que será incluso.</p>
<p>A variável global <strong>$_POST,</strong> é geralmente utilizada em formulários, de login por exemplo, onde não é recomendado óbviamente mostrar a senha na URL.</p>
<p>Variáveis $_POST são criadas quando um formulário é especificado com <em>method=&#8221;POST&#8221;</em>, por exemplo:</p>
<p><strong>form.php</strong></p>
<div class="codecolorer-container xml twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;POST&quot;</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;teste.php&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; Nome: <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;meunome&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; Idade: <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;idade&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Enviar&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>Para recuperar o valor desse campo no PHP, você teria de fazer:</p>
<p><strong>teste.php</strong></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;height:300px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// verificando se o método é mesmo POST</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_METHOD'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'meunome'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$meunome</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// nenhum nome especificado</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$meunome</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'meunome'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'idade'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$idade</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$idade</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'idade'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// função is_int(), utilizada pra verificação do tipo Inteiro</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_int"><span style="color: #990000;">is_int</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$idade</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$idade</span> <span style="color: #339933;">&gt;=</span> 18<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Bem-vindo <span style="color: #006699; font-weight: bold;">$nome</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// imprimindo saudação</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Conteúdo proibido para menores&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>Simples não?</p>
<h3>Trabalhando com Datas</h3>
<p>Você pode automatizar aquele famoso &#8220;Copyright&#8221; no rodapé da página, utilizando a função <strong>date()</strong> do PHP:</p>
<p><strong>rodape.php</strong></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;div class=&quot;rodape&quot;&gt;<br />
Todos os direitos reservados &amp;copy 2008-<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/date"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&lt;/div&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</div></div>
<p>O que vai &#8220;dentro&#8221; de uma função, é chamado de argumento. No exemplo acima, foi passado apenas um argumento, do tipo string: &#8216;Y&#8217;, o que diz à função date que queremos o ano (<strong>Y</strong>ear). Então, será impresso:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Todos direitos reservados <span style="color: #339933;">&amp;</span>copy <span style="color: #cc66cc;">2008</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">2009</span></div></div>
<p>Exibindo a data de hoje:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/date"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p><strong>Verificando se é Manhã, Tarde ou Noite (Comentado)<br />
</strong></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Hora</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$hora</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/date"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'H'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// se Hora for maior que 18 ou menor que 5</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// de 18:01 até 23:59 ou 0 até 04:59</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$hora</span> <span style="color: #339933;">&gt;</span> 18 <span style="color: #339933;">||</span> <span style="color: #000088;">$hora</span> <span style="color: #339933;">&lt;</span> 5<span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Boa Noite'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// se Hora for menor que 12</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// de 05:00 até 11:59</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$hora</span> <span style="color: #339933;">&lt;</span> 12<span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Bom dia'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// senão... é de tarde</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// de 12:00 até 18:00</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Boa tarde'</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>Uma lista com todos os formatos para a função &#8220;date&#8221; pode ser encontrada no manual do PHP, em: http://www.php.net/manual/en/function.date.php</p>
<h3>Tratamento de Strings</h3>
<p><strong>strtolower()</strong></p>
<p>A função strtolower() transforma todos caracteres em letras minúsculas.</p>
<p><span style="text-decoration: underline;">Exemplo</span></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/strtolower"><span style="color: #990000;">strtolower</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FooBar'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p><span style="text-decoration: underline;">Imprimirá</span></p>
<p>foobar</p>
<p><span style="text-decoration: underline;">Manual</span></p>
<p><a href="http://br2.php.net/strtolower">http://br2.php.net/strtolower</a></p>
<p><strong>strtoupper()</strong></p>
<p>A função strtoupper() é o inverso da strtolower(), transforma todos caracteres em letras maiúsculas.</p>
<p><span style="text-decoration: underline;">Exemplo</span></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/strtoupper"><span style="color: #990000;">strtoupper</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'foobar'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p><span style="text-decoration: underline;">Imprimirá</span></p>
<p>FOOBAR</p>
<p><span style="text-decoration: underline;">Manual</span></p>
<p><a href="http://br2.php.net/strtoupper">http://br2.php.net/strtoupper</a></p>
<p><strong>number_format()</strong></p>
<p>A função number_format() formata números, valores por exemplo.</p>
<p><span style="text-decoration: underline;">Exemplo</span></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">R$ <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/number_format"><span style="color: #990000;">number_format</span></a><span style="color: #009900;">&#40;</span>1300<span style="color: #339933;">.</span>40<span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p><span style="text-decoration: underline;">Imprimirá</span></p>
<p>R$ 1.300,40</p>
<p><span style="text-decoration: underline;">Manual</span></p>
<p><a href="http://br2.php.net/number_format">http://br2.php.net/number_format</a></p>
<p><strong>trim()</strong></p>
<p>A função trim() remove espaços no começo e no fim da string.</p>
<p><span style="text-decoration: underline;">Exemplo</span></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' &nbsp; Texto'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>É muito útil também para fazer validações:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$nome</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Nome em branco'</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p><span style="text-decoration: underline;">Imprimirá</span></p>
<p>Texto (sem nenhum espaço no começo)</p>
<p><span style="text-decoration: underline;">Manual</span></p>
<p><a href="http://br2.php.net/trim">http://br2.php.net/trim</a></p>
<p><strong>wordwrap()</strong></p>
<p>A função wordwrap() quebra um texto em partes, a cada N caracteres.</p>
<p><span style="text-decoration: underline;">Exemplo</span></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/wordwrap"><span style="color: #990000;">wordwrap</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'um texto muito, mas muito longo.'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p><span style="text-decoration: underline;">Imprimirá</span></p>
<div class="codecolorer-container xml twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">um texto<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp;muito, <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
mas muit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
o longo.</div></div>
<p><span style="text-decoration: underline;">Manual</span></p>
<p><a href="http://br2.php.net/wordwrap">http://br2.php.net/wordwrap</a></p>
<p><strong>nl2br()</strong></p>
<p>A função nl2br() transforma quebras de linha em &lt;br /&gt; (HTML). Muito útil também quando retornando um campo texto, de um banco de dados.</p>
<p><span style="text-decoration: underline;">Exemplo</span></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/nl2br"><span style="color: #990000;">nl2br</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'um<br />
outro texto<br />
só para exemplo...'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p><span style="text-decoration: underline;">Imprimirá</span></p>
<div class="codecolorer-container xml twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">um<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
outro texto<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
só para exemplo...</div></div>
<p><span style="text-decoration: underline;">Manual</span></p>
<p><a href="http://br2.php.net/nl2br">http://br2.php.net/nl2br</a></p>
<p><strong>strlen()</strong></p>
<p>A função strlen() retorna quantos caracteres a string possui.</p>
<p><span style="text-decoration: underline;">Exemplo</span></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/strlen"><span style="color: #990000;">strlen</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cinco'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p><span style="text-decoration: underline;">Imprimirá</span></p>
<p>5</p>
<p><span style="text-decoration: underline;">Manual</span></p>
<p><a href="http://br2.php.net/strlen">http://br2.php.net/strlen</a></p>
<p><strong>htmlentities()</strong></p>
<p>A função htmlentities() converte caracteres que podem ocasionar problemas no seu HTML, como &lt;, &gt; e &amp; em entidades correspondentes, como: &amp;lt;, &amp;gt; e &amp;amp.</p>
<p><span style="text-decoration: underline;">Exemplo</span></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/strlen"><span style="color: #990000;">strlen</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;foo&gt;bar&amp;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p><span style="text-decoration: underline;">Imprimirá</span></p>
<p>&amp;lt;foo&amp;gt;bar&amp;amp;</p>
<p><span style="text-decoration: underline;">Manual</span></p>
<p><a href="http://br2.php.net/htmlentities">http://br2.php.net/htmlentities</a></p>
<p><strong>urlencode()</strong></p>
<p>A função urlencode() serve para você codificar uma string de uma forma que ela fique de acordo com a especificação, do que é permitido utilizar em uma URL.</p>
<p><span style="text-decoration: underline;">Exemplo</span></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$titulo</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Primeira noticia, teste'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'http://meusite/noticia.php?id=10&amp;titulo='</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/urlencode"><span style="color: #990000;">urlencode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$titulo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p><span style="text-decoration: underline;">Imprimirá</span></p>
<p>http://meusite/noticia.php?id=10&amp;titulo=Primeira+noticia%2C+teste</p>
<p><span style="text-decoration: underline;">Manual</span></p>
<p><a href="http://br2.php.net/urlencode">http://br2.php.net/urlencode</a></p>
<p><strong>urldecode()</strong></p>
<p>A função urldecode() é o inverso da urlencode().</p>
<p><span style="text-decoration: underline;">Exemplo</span></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/urldecode"><span style="color: #990000;">urldecode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Primeira+noticia%2C+teste'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p><span style="text-decoration: underline;">Imprimirá</span></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Primeira noticia<span style="color: #339933;">,</span> teste</div></div>
<p><span style="text-decoration: underline;">Manual</span></p>
<p><a href="http://br2.php.net/urldecode">http://br2.php.net/urldecode</a></p>
]]></content:encoded>
			<wfw:commentRss>http://caioariede.com/2009/php-para-web-designers/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Montando um SFTP em um diretório local com SSHFS</title>
		<link>http://caioariede.com/2009/montando-ftp-diretorio-local-sshfs</link>
		<comments>http://caioariede.com/2009/montando-ftp-diretorio-local-sshfs#comments</comments>
		<pubDate>Mon, 04 May 2009 03:56:25 +0000</pubDate>
		<dc:creator>Caio Ariede</dc:creator>
				<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://caioariede.com/?p=435</guid>
		<description><![CDATA[Montar um FTP como se fosse um diretório local na maquina pode facilitar a vida de muita gente. Principalmente pessoas que trabalham com desenvolvimento WEB e precisam fazer atualizações direto no FTP. Mesmo que não recomendado, às vezes é necessário, até mesmo para enviar arquivos.
Vou exemplificar aqui utilizando o Ubuntu, porém mesmo em outras distribuições [...]]]></description>
			<content:encoded><![CDATA[<p>Montar um FTP como se fosse um diretório local na maquina pode facilitar a vida de muita gente. Principalmente pessoas que trabalham com desenvolvimento WEB e precisam fazer atualizações direto no FTP. Mesmo que não recomendado, às vezes é necessário, até mesmo para enviar arquivos.</p>
<p>Vou exemplificar aqui utilizando o Ubuntu, porém mesmo em outras distribuições Linux, a ideia pode ser reutilizada.</p>
<p>Supondo que você tenha um FTP no endereço example.org, vamos criar uma estrutura como essa abaixo, onde possibilitará posteriormente a adição de novos endereços (FTP&#8217;s).</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>seuusuario<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">ftp</span><br />
<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>seuusuario<span style="color: #000000; font-weight: bold;">/</span>ftp<span style="color: #000000; font-weight: bold;">/</span>example.org<br />
<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>seuusuario<span style="color: #000000; font-weight: bold;">/</span>ftp<span style="color: #000000; font-weight: bold;">/</span>example.org<span style="color: #000000; font-weight: bold;">/</span>sftpmount <span style="color: #7a0874; font-weight: bold;">&#40;</span>script para montagem<span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>seuusuario<span style="color: #000000; font-weight: bold;">/</span>ftp<span style="color: #000000; font-weight: bold;">/</span>example.org<span style="color: #000000; font-weight: bold;">/</span>www <span style="color: #7a0874; font-weight: bold;">&#40;</span>arquivos no servidor<span style="color: #7a0874; font-weight: bold;">&#41;</span></div></div>
<p>Então vamos lá.</p>
<ol>
<li>Primeiro baixe o SSHFS
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> sshfs</div></div>
</li>
<li>Vamos criar o diretório para o nosso FTP
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> ~<span style="color: #000000; font-weight: bold;">/</span>ftp<span style="color: #000000; font-weight: bold;">/</span>example.org<span style="color: #000000; font-weight: bold;">/</span>www<br />
<span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>ftp<span style="color: #000000; font-weight: bold;">/</span>example.org</div></div>
<p>A opção <strong>-p</strong> no mkdir, faz com que ele crie todos os diretórios antecedentes ao <em>www</em>, <em>ftp</em> e <em>example.org</em>.</li>
<li>Agora crie um script para montar o FTP mais facilmente
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">gedit sftpmount</div></div>
<p>Coloque a linha abaixo no arquivo, modificando às suas necessidades:</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #7a0874; font-weight: bold;">echo</span> senha <span style="color: #000000; font-weight: bold;">|</span> sshfs <span style="color: #660033;">-o</span> password_stdin usuario<span style="color: #000000; font-weight: bold;">@</span>example.org:public_html www</div></div>
<p>Lembre-se de alterar também onde esta <strong>public_html</strong> para o seu diretório remoto.</p>
<p>A senha é opcional, você pode omiti-la retirando o <strong>echo</strong> e a opção <strong>-o password_stdin</strong>.</li>
<li>Dê permissão de execução para o script
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">chmod</span> +x sftpmount</div></div>
</li>
<li>Execute o script
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">.<span style="color: #000000; font-weight: bold;">/</span>sftpmount</div></div>
</li>
<li>Se tudo ocorrer normalmente, você verá os arquivos listando o diretório www:
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">ls</span> www<span style="color: #000000; font-weight: bold;">/</span></div></div>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://caioariede.com/2009/montando-ftp-diretorio-local-sshfs/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
