<?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>blog.autopsy.se &#187; PHP</title>
	<atom:link href="http://blog.autopsy.se/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.autopsy.se</link>
	<description>basically; it sucks. everything sucks. but.. in a good way..</description>
	<lastBuildDate>Wed, 23 Nov 2011 18:25:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>tv.autopsy.se sucks!</title>
		<link>http://blog.autopsy.se/2009/10/tv-autopsy-se-sucks/</link>
		<comments>http://blog.autopsy.se/2009/10/tv-autopsy-se-sucks/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 21:52:16 +0000</pubDate>
		<dc:creator>ube</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Code Poetry]]></category>
		<category><![CDATA[tv.autopsy.se]]></category>

		<guid isPermaLink="false">http://blog.autopsy.se/?p=98</guid>
		<description><![CDATA[Finally I got around to re-code  http://tv.autopsy.se again. Enjoy!]]></description>
			<content:encoded><![CDATA[<p>Finally I got around to re-code  <a title="tv.autopsy.se" href="http://tv.autopsy.se" target="_blank">http://tv.autopsy.se</a> again. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.autopsy.se/2009/10/tv-autopsy-se-sucks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reading config from database sucks!</title>
		<link>http://blog.autopsy.se/2009/01/reading-config-from-database-sucks/</link>
		<comments>http://blog.autopsy.se/2009/01/reading-config-from-database-sucks/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 19:16:24 +0000</pubDate>
		<dc:creator>ube</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Code Poetry]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.autopsy.se/?p=30</guid>
		<description><![CDATA[I'm working on a new site and I wanted an easy way to read config for a user from the database. Earlier I've stored all the config variables in the user table, but this leaves a problem when developing; adding a new parameter means that you'll have to add a new entry in the user [...]]]></description>
			<content:encoded><![CDATA[<p>I'm working on a new site and I wanted an easy way to read config for a user from the database. Earlier I've stored all the config variables in the user table, but this leaves a problem when developing; adding a new parameter means that you'll have to add a new entry in the user table. So, why not use one table for config and then have a default value stored in the database as user 0? <span id="more-30"></span></p>
<pre class="mysql"> <span style="color: #993333; font-weight: bold;">EXPLAIN</span> config;
+<span style="color: #808080; font-style: italic;">-------------+--------------+------+-----+---------+-------+</span>
| <span style="color: #993333; font-weight: bold;">FIELD</span>       | Type         | <span style="color: #aa3399; font-weight: bold;">NULL</span> | Key | <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> | Extra |
+<span style="color: #808080; font-style: italic;">-------------+--------------+------+-----+---------+-------+</span>
| configUser  | <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span>      | NO   |     | <span style="color: #cc66cc;">0</span>       |       |
| configVar   | <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> | NO   |     | <span style="color: #aa3399; font-weight: bold;">NULL</span>    |       |
| configValue | <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> | NO   |     | <span style="color: #aa3399; font-weight: bold;">NULL</span>    |       |
+<span style="color: #808080; font-style: italic;">-------------+--------------+------+-----+---------+-------+</span>
&nbsp;
 <span style="color: #993333; font-weight: bold;">SELECT</span> * <span style="color: #993333; font-weight: bold;">FROM</span> config;
+<span style="color: #808080; font-style: italic;">------------+----------------+-------------+</span>
| configUser | configVar      | configValue |
+<span style="color: #808080; font-style: italic;">------------+----------------+-------------+</span>
|          <span style="color: #cc66cc;">0</span> | streamsPerPage | <span style="color: #cc66cc;">30</span>          |
|          <span style="color: #cc66cc;">1</span> | streamsPerPage | <span style="color: #cc66cc;">10</span>          |
+<span style="color: #808080; font-style: italic;">------------+----------------+-------------+</span></pre>
<p>The problem this is when you're trying to get the config value from the database for a user and the value is set, if you don't want to run two queries that is:</p>
<pre class="mysql"><span style="color: #993333; font-weight: bold;">SELECT</span> * <span style="color: #993333; font-weight: bold;">FROM</span> config <span style="color: #993333; font-weight: bold;">WHERE</span> configUser = <span style="color: #cc66cc;">0</span>;
<span style="color: #993333; font-weight: bold;">SELECT</span> * <span style="color: #993333; font-weight: bold;">FROM</span> config <span style="color: #993333; font-weight: bold;">WHERE</span> configUser = <span style="color: #cc66cc;">4384</span>;</pre>
<pre class="php"><span style="color: #000000; font-weight: bold;">function</span> getConfig<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$uid</span> = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #0000ff;">$ids</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;0&quot;</span>, =&amp;gt; <span style="color: #cc66cc;">0</span>, <span style="color: #ff0000;">&quot;1&quot;</span>, <span style="color: #0000ff;">$uid</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #0000ff;">$ids</span> = <a href="http://www.php.net/array_unique"><span style="color: #000066;">array_unique</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ids</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$ids</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$id</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
   <span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">&quot;select * from config where configUser = $id&quot;</span>;
   <span style="color: #0000ff;">$data</span> = executeSql<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$sql</span><span style="color: #66cc66;">&#41;</span>;
   <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$q</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
     <span style="color: #0000ff;">$return</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$q</span><span style="color: #66cc66;">&#91;</span>configVar<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$q</span><span style="color: #66cc66;">&#91;</span>configValue<span style="color: #66cc66;">&#93;</span>;
   <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
 <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$return</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>Could be used, but you do want a more elegant way, right?</p>
<pre class="mysql"><span style="color: #993333; font-weight: bold;">SELECT</span>
  *
 <span style="color: #993333; font-weight: bold;">FROM</span>
  config
 <span style="color: #993333; font-weight: bold;">WHERE</span>
  configUser =
   <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span>
    max<span style="color: #66cc66;">&#40;</span>configUser<span style="color: #66cc66;">&#41;</span>
   <span style="color: #993333; font-weight: bold;">FROM</span>
    config
   <span style="color: #993333; font-weight: bold;">WHERE</span>
    configUser = <span style="color: #cc66cc;">4384</span>
   <span style="color: #993333; font-weight: bold;">OR</span>
    configUser = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>Could also work, but sub-queries are CPU hoggers. So what I came up with (with some help from my friends):</p>
<pre class="mysql"><span style="color: #993333; font-weight: bold;">SELECT</span>
 config.configVar,
 IFNULL<span style="color: #66cc66;">&#40;</span>userconfig.configvalue, config.configvalue<span style="color: #66cc66;">&#41;</span> AS configValue
<span style="color: #993333; font-weight: bold;">FROM</span>
 config <span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">JOIN</span> config as userconfig
    on
     <span style="color: #66cc66;">&#40;</span>userconfig.configVar = config.configVar <span style="color: #993333; font-weight: bold;">AND</span> userconfig.configuser=<span style="color: #cc66cc;">4384</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #993333; font-weight: bold;">WHERE</span>
    config.configuser=<span style="color: #cc66cc;">0</span><span style="color: #ff0000;">&quot;;</span></pre>
<p>This gives us the following code:</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">function</span> getConfig<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$uid</span> = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">&quot;select
                 config.configVar,
                 ifnull(userconfig.configvalue, config.configvalue) as configValue
          from
                 config
          left join
                 config as userconfig on (userconfig.configVar = config.configVar and userconfig.configuser=$uid)
          where
                 config.configuser=0&quot;</span>;
  <span style="color: #0000ff;">$data</span> = executeSql<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$sql</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$q</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$return</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$q</span><span style="color: #66cc66;">&#91;</span>configVar<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$q</span><span style="color: #66cc66;">&#91;</span>configValue<span style="color: #66cc66;">&#93;</span>;
  <span style="color: #66cc66;">&#125;</span>
 <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$return</span>;
<span style="color: #66cc66;">&#125;</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.autopsy.se/2009/01/reading-config-from-database-sucks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>validating user names sucks!</title>
		<link>http://blog.autopsy.se/2008/10/validating-user-names-sucks/</link>
		<comments>http://blog.autopsy.se/2008/10/validating-user-names-sucks/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 14:17:02 +0000</pubDate>
		<dc:creator>ube</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Code Poetry]]></category>
		<category><![CDATA[RegExp]]></category>

		<guid isPermaLink="false">http://blog.autopsy.se/?p=6</guid>
		<description><![CDATA[You know when you're writing any application, and you want to restrict the characters in the user name? Instead of checking for non allowed characters, look for allowed. Sounds strange? Not really, you'll understand: function checkname&#40;$string, $allowed&#41; &#123; $allowed = &#34;/[$allowed]/s&#34;; $string = preg_replace&#40;$allowed, &#34;&#34;, $string&#41;; if &#40; $string &#41; return FALSE; else return TRUE; [...]]]></description>
			<content:encoded><![CDATA[<p>You know when you're writing any application, and you want to restrict the characters in the user name? Instead of checking for non allowed characters, look for allowed. Sounds strange? Not really, you'll understand:</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">function</span> checkname<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$string</span>, <span style="color: #0000ff;">$allowed</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #0000ff;">$allowed</span> = <span style="color: #ff0000;">&quot;/[$allowed]/s&quot;</span>;
<span style="color: #0000ff;">$string</span> = <a href="http://www.php.net/preg_replace"><span style="color: #000066;">preg_replace</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$allowed</span>, <span style="color: #ff0000;">&quot;&quot;</span>, <span style="color: #0000ff;">$string</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$string</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">FALSE</span>;
<span style="color: #b1b100;">else</span>
<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">TRUE</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>So what you'll do is remove all allowed characters, and if there are any left in the string the user has chosen an invalid username. Ofcourse it's regexp that does the trick. Using the code:</p>
<pre class="php"><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> ! checkname<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$input</span>, <span style="color: #ff0000;">&quot;A-Za-z0-9_<span style="color: #000099; font-weight: bold;">\.</span>&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;Invalid username!&quot;</span>;
<span style="color: #b1b100;">else</span>
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;Ok, lets go!</span></pre>
<p>We're allowing A-Z, a-z, 0-9, underscore and dots in the username.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.autopsy.se/2008/10/validating-user-names-sucks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

