<?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>Security And Caffeine &#187; coding</title>
	<atom:link href="http://www.securityandcaffeine.com/category/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.securityandcaffeine.com</link>
	<description>something witty here</description>
	<lastBuildDate>Mon, 03 Oct 2011 21:45:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Adding Pages Dynamically within a jQuery Mobile Project in PhoneGap</title>
		<link>http://www.securityandcaffeine.com/2011/06/08/adding-pages-dynamically-within-a-jquery-mobile-project-in-phonegap/</link>
		<comments>http://www.securityandcaffeine.com/2011/06/08/adding-pages-dynamically-within-a-jquery-mobile-project-in-phonegap/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 00:25:12 +0000</pubDate>
		<dc:creator>Mack Staples</dc:creator>
				<category><![CDATA[coding]]></category>

		<guid isPermaLink="false">http://www.securityandcaffeine.com/?p=77</guid>
		<description><![CDATA[Code&#8217;s towards the bottom if you can&#8217;t be buggered to read my ramblings. The Challenge: Add new &#8220;pages&#8221; (page divs) during execution in a mobile app. This was part of a project using jQuery Mobile and PhoneGap, and the information that I wanted to display wouldn&#8217;t be available until the app ran and was subject [...]]]></description>
			<content:encoded><![CDATA[<p>Code&#8217;s towards the bottom if you can&#8217;t be buggered to read my ramblings.</p>
<p>The Challenge: Add new &#8220;pages&#8221; (page divs) during execution in a mobile app. This was part of a project using jQuery Mobile and PhoneGap, and the information that I wanted to display wouldn&#8217;t be available until the app ran and was subject to change. In this case, the app lists a number of companies, and each was supposed to have their own &#8220;page&#8221; within the app. Tap the company name, see the company info. Simple, right?</p>
<p>The Reason: The app I was working on had a basic layout that could be coded beforehand, but much of the data would be unknown until the app actually ran and pulled that data from a server. Since the various companies that the app listed could change at any time, this needed to be dynamic. While the approval time for apps on mobile devices is enough of a burden (up to three weeks or more), I had the additional hurdle that an update for every new company might degrade user confidence. Yes, updating your apps too frequently can erode confidence, because the users start wondering what, exactly, you&#8217;re screwing up so bad that you need to patch it every week or two.<br />
<span id="more-77"></span><br />
The Failed Approach: On an initial build of the app, I used a simple method to accomplish this: update a single page. Yep. When the user clicked the button to see a particular company, the app would have to dig through it&#8217;s little database, pull out the company name, logo, and other information, then shove it into containers for each piece of info. Sounds great. Well, it worked, but it was slow. Local storage on a device can be quite slow, especially when it comes to images (the logo, in this case). The speed gets worse if you&#8217;re pulling the images from the web. It was slow enough that users would sometimes see the logo of another company before the app had a chance to update the container with the new image. Nasty.</p>
<p>The Goal: Make a &#8220;page&#8221; (a page div) for each company as soon as the data comes in. Modern browsers, including the browser implementation within a PhoneGap project, lives and dies by the DOM. If you&#8217;re not familiar with it, long story short is that it is the &#8220;living&#8221; version of web pages and web data that your browser actually interacts with. My goal, then, was to find a good, reliable way to make new pages inside the DOM, as the data came in.</p>
<p>The Solution: I tried a lot of things that did not work. This included trying to create new page divs and append them to the page&#8217;s body element. No dice. By the way, as the title says, this was for a jQuery Mobile project on PhoneGap. PhoneHap is not necessary for this to work, but jQuery Mobile and jQuery are. Here&#8217;s the one method that did work:</p>
<p>1) Create a (just one) static &#8220;template&#8221; page in your html document. It can be as simple or as in-depth as you want. It can be empty, or you can structure the whole thing the way the rest of the pages will look. In my case, I could create a header area for the company name, an area for the logo, a text box for additional information about the company, and another text box for the phone number. Whatever you like. The below example is a simple one, but it doesn&#8217;t have to be. Important: assign the template div an id so it will be easily referenced later in code.</p>
<blockquote><p>&lt;!&#8211;// BASIC COMPANY VIEW PAGE //&#8211;&gt;<br />
&lt;div data-role=&#8221;page&#8221; id=&#8221;page_basic_company&#8221;&gt;</p>
<p>&lt;div data-role=&#8221;header&#8221;&gt;	&lt;h1&gt;&lt;/h1&gt;	&lt;/div&gt;<br />
&lt;div data-role=&#8221;content&#8221;&gt;	&lt;/div&gt;</p>
<p>&lt;/div&gt;<br />
&lt;!&#8211;// END BASIC COMPANY VIEW PAGE//&#8211;&gt;</p></blockquote>
<p>2) Clone the template div, change its id  and data-url values to the same thing, and then attach it to the body. Yes, all in one step. For editing simplicity, I&#8217;ve also assigned the new div to a jQuery variable. By the way, the [a] below is because I was iterating through an array of companies (called company_list). The array came in as JSON via AJAX, and here we go.</p>
<blockquote><p>var $new_company_page = $(&#8216;#page_basic_company&#8217;).clone().attr(&#8216;data-url&#8217;, company_list[a].business_id).attr(&#8216;id&#8217;, company_list[a].business_id).appendTo(&#8216;body&#8217;);</p></blockquote>
<p>3) Use the jQuery variable to reference the new page div and go to town. Add content. This can be anything from completely replacing the contents with new html to picking specific children elements to modify.</p>
<blockquote><p>$new_company_page.children(&#8216;.phone_box&#8217;).text(&#8217;555-555-5555&#8242;);</p>
<p>&nbsp;</p></blockquote>
<p>Hope this helps. If anyone figures out a better way, please do let me know. Enjoy.</p>
<p>&nbsp;</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.securityandcaffeine.com%2F2011%2F06%2F08%2Fadding-pages-dynamically-within-a-jquery-mobile-project-in-phonegap%2F&amp;title=Adding%20Pages%20Dynamically%20within%20a%20jQuery%20Mobile%20Project%20in%20PhoneGap" id="wpa2a_2"><img src="http://www.securityandcaffeine.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.securityandcaffeine.com/2011/06/08/adding-pages-dynamically-within-a-jquery-mobile-project-in-phonegap/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Better Form Processing</title>
		<link>http://www.securityandcaffeine.com/2008/06/16/better-form-processing/</link>
		<comments>http://www.securityandcaffeine.com/2008/06/16/better-form-processing/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 08:03:01 +0000</pubDate>
		<dc:creator>Mack Staples</dc:creator>
				<category><![CDATA[coding]]></category>

		<guid isPermaLink="false">http://securityandcaffeine.com/?p=22</guid>
		<description><![CDATA[This is a much condensed version of the original post. Usually, processing form data means getting either POST or GET data from a form, and trying to figure out, in code, what you have, and then do something with it.  This can be easy or complicated, depending on how much is being passed in.  Email [...]]]></description>
			<content:encoded><![CDATA[<p>This is a much condensed version of the original post.</p>
<p>Usually, processing form data means getting either POST or GET data from a form, and trying to figure out, in code, what you have, and then do something with it.  This can be easy or complicated, depending on how much is being passed in.  Email address only? Easy.  Checkboxes, optional fields, and so on, all together?  Pain.  Often, a lot of form processing is done with stacks of &#8220;if&#8221; statements.  This sucks.  Here is a better way:</p>
<p>From now on, I want you to name all of your &#8220;real&#8221; form elements (ones that have data that could change, so not buttons) using the name you would have given then, <em>plus an array name</em>, that they will all share.</p>
<p>So,</p>
<pre>&lt;input type="text" name="username" id="username" /&gt;</pre>
<p>becomes</p>
<pre>&lt;input type="text" name="formdata[username]" id="username" /&gt;</pre>
<p>Why?  Instead of having one array ($_POST), you&#8217;ll now have two ($_POST and &#8216;formdata&#8217;, within $_POST).  Your buttons and other &#8220;static&#8221; form elements will still live in $_POST, but everything containing data that needs handling will be in the &#8216;formdata&#8217; array, which you can access as $_POST[formdata].<br />
<span id="more-28"></span><br />
Why does this rock?  I find I have to go through all of the data in a form, even if I know what and how much is coming in, and that the amount wont change.  For example, it&#8217;s a good idea to clean all user input.  Since users can change any form value, this needs handled.  Why bother with a ton of &#8216;if&#8217; statements when we can just iterate through the &#8216;formdata&#8217; array?</p>
<pre>  foreach ($_POST['formdata'] as $key =&gt; $value)
    $_POST['formdata'][$key] = makesafe($value);</pre>
<p>Cautionary note: this definitely seems ideal for updating data in a database and saving time, doesn&#8217;t it?  Maybe something like (let&#8217;s assume &#8220;$user_id&#8221; is the current user&#8217;s account, and we determined that earlier somehow) :</p>
<pre>  foreach ($_POST['formdata'] as $key =&gt; $value)
    mysql_query("UPDATE info_table SET $key = '$value' WHERE index = $user_id");</pre>
<p>This is a bad thing.  Why?  SQL injection.  At the very least, clean <em>all</em> input, including the field names, if you think something like the above statement is a good idea.  If you don&#8217;t, don&#8217;t say I didn&#8217;t warn you if you get owned.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.securityandcaffeine.com%2F2008%2F06%2F16%2Fbetter-form-processing%2F&amp;title=Better%20Form%20Processing" id="wpa2a_4"><img src="http://www.securityandcaffeine.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.securityandcaffeine.com/2008/06/16/better-form-processing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>U@T Rogue Forums</title>
		<link>http://www.securityandcaffeine.com/2008/05/23/ut-rogue-forums/</link>
		<comments>http://www.securityandcaffeine.com/2008/05/23/ut-rogue-forums/#comments</comments>
		<pubDate>Fri, 23 May 2008 04:18:57 +0000</pubDate>
		<dc:creator>Mack Staples</dc:creator>
				<category><![CDATA[coding]]></category>

		<guid isPermaLink="false">http://securityandcaffeine.com/?p=11</guid>
		<description><![CDATA[During the Spring Semester of 2007, I worked as the lead developer on the UAT Rogue Forums.  The project was designed and lead by Jordan Womack, a UAT Master&#8217;s Student.  Under him, the Rogue Forums was created as an alternative to UAT&#8217;s intranet forums; general opinion of the intranet forums was quite low. We used and [...]]]></description>
			<content:encoded><![CDATA[<p>During the Spring Semester of 2007, I worked as the lead developer on the <a title="UAT Rogue Forums" href="http://op-server.0x58.com/forums/" target="_blank">UAT Rogue Forums</a>.  The project was designed and lead by Jordan Womack, a UAT Master&#8217;s Student.  Under him, the Rogue Forums was created as an alternative to <a title="University of Advancing Technology" href="http://www.uat.edu" target="_blank">UAT&#8217;s</a> intranet forums; general opinion of the intranet forums was quite low.</p>
<p>We used and modified <a title="phpBB Main Site" href="http://www.phpbb.com" target="_blank">phpBB.</a>  My portion of the project was to re-skin the bulletin board system, act as lead moderator, prevent non-UAT students from registering, and implement the post voting system.  phpBB version 3, RC5 was used and updated to subsequent release candidates.<br />
<span id="more-15"></span><br />
Two php files were added to the core of phpBB3, one core file was modified, and additional tables were created in the database.  Currently this project only works with phpBB3 on a MySQL database, but I hope to make it database-independent.  Below is a snippet of code from the new files.</p>
<pre>&lt;?php
	$sql = 'INSERT INTO ' . VOTES_TABLE . ' ' . $db-&gt;sql_build_array('INSERT', array(
		'post_id'		=&gt; (int) $post_id,
		'user_id'		=&gt; $user-&gt;data['user_id'],
		'adjust'		=&gt; 1,
		'vote_time'		=&gt; time(),
		'voter_ip'		=&gt; $user-&gt;ip)
	);
?&gt;</pre>
<p>A working example of these modifications in a live phpBB3 environment can be seen at <a title="UAT's Rogue Forums" href="http://op-server.0x58.com/forums/" target="_blank">The Rogue Forums</a>.  Please contact me for a login, as account creation is limited to UAT students only.  The new files associated with this project are linked below.</p>
<p><a title="Zipfile of phpBB3 voting system files" href="http://www.securityandcaffeine.com/projects/phpBB3.zip">pbpBB voting system files</a></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.securityandcaffeine.com%2F2008%2F05%2F23%2Fut-rogue-forums%2F&amp;title=U%40T%20Rogue%20Forums" id="wpa2a_6"><img src="http://www.securityandcaffeine.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.securityandcaffeine.com/2008/05/23/ut-rogue-forums/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CSS Rotate v2</title>
		<link>http://www.securityandcaffeine.com/2008/05/08/css-rotate-v2/</link>
		<comments>http://www.securityandcaffeine.com/2008/05/08/css-rotate-v2/#comments</comments>
		<pubDate>Thu, 08 May 2008 22:59:10 +0000</pubDate>
		<dc:creator>Mack Staples</dc:creator>
				<category><![CDATA[coding]]></category>

		<guid isPermaLink="false">http://securityandcaffeine.com/?p=7</guid>
		<description><![CDATA[Background: This projects began as a request from a friend.  His site, acting as a working demo, is at Fried Pope.  He wanted the ability to have different stylesheets govern the look of his site, and be chosen randomly for each visitor.  In short, he wanted to rotate his style sheets. Project: The result from that request, plus [...]]]></description>
			<content:encoded><![CDATA[<div>
<p>Background: This projects began as a request from a friend.  His site, acting as a working demo, is at <a title="Fried Pope" href="http://www.friedpope.com">Fried Pope</a>.  He wanted the ability to have different stylesheets govern the look of his site, and be chosen randomly for each visitor.  In short, he wanted to rotate his style sheets.</p>
<p>Project: The result from that request, plus a major recode, is linked below.  The system is designed to pick a stylesheet from the directory you specify, and redirect the user to it.  It will remember the stylesheet selected for the user for their entire visit, so your site doesn&#8217;t keep changing its look.  That would be confusing.</p>
<p>Compatibility: This project works in all known browsers.</p>
<p>Use: Using this tool is extremely simple.  You must have a web host that supports PHP.</p>
<ol>
<li>Uncompress the source code, and put &#8216;cssrotate2.php&#8217; in your website&#8217;s directory</li>
<li>Open &#8216;cssrotate2.php&#8217; with an HTML editor</li>
<li>If you need to, change the the line under the comments to specify where your styles are ($stylesdir = &#8220;./styles&#8221;;)</li>
<li>Save and close it</li>
<li>Open any pages in your site that you want to have use CSS Rotate</li>
<li>Add a link to it like you would any other stylesheet (&lt;link href=&#8221;./styles/cssrotate2.php&#8221; rel=&#8221;stylesheet&#8221; type=&#8221;text/css&#8221; /&gt;)</li>
<li>Save and close those pages</li>
</ol>
<p><a title="CSS Rotate 2 Download" href="http://www.securityandcaffeine.com/projects/cssrotate2.zip">download css rotate 2</a></div>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.securityandcaffeine.com%2F2008%2F05%2F08%2Fcss-rotate-v2%2F&amp;title=CSS%20Rotate%20v2" id="wpa2a_8"><img src="http://www.securityandcaffeine.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.securityandcaffeine.com/2008/05/08/css-rotate-v2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP, MySQL, and mysql_fetch_array()</title>
		<link>http://www.securityandcaffeine.com/2008/04/03/php-mysql-and-mysql_fetch_array/</link>
		<comments>http://www.securityandcaffeine.com/2008/04/03/php-mysql-and-mysql_fetch_array/#comments</comments>
		<pubDate>Fri, 04 Apr 2008 03:54:05 +0000</pubDate>
		<dc:creator>Mack Staples</dc:creator>
				<category><![CDATA[coding]]></category>

		<guid isPermaLink="false">http://securityandcaffeine.com/?p=5</guid>
		<description><![CDATA[Today, I spent a couple hours working on a customer management system for APS Web Design.  Over the course of that work, I discovered a very interesting behavior in a particular function, specifically mysql_fetch_array(). For those not familiar with it, but familiar with PHP, this function takes the results returned by mysql_query(), and turns it into [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I spent a couple hours working on a customer management system for <a title="APS Web Design" href="http://www.apswebdesign.com" target="_blank">APS Web Design</a>.  Over the course of that work, I discovered a very interesting behavior in a particular function, specifically <strong>mysql_fetch_array()</strong>.</p>
<p>For those not familiar with it, but familiar with PHP, this function takes the results returned by <strong>mysql_query()</strong>, and turns it into an array of values.  There are two other functions that do similar things: <strong>mysql_fetch_row()</strong> and <strong>mysql_fetch_assoc()</strong>.</p>
<p><strong>mysql_fetch_row()</strong> &#8221;fetches one row of data from the result associated with the specified result identifier.  The row is returned as an array.  Each result column is stored in an array offset, starting at offset 0&#8243;. <a title="PHP.net Reference for mysql_fetch_row" href="http://us.php.net/mysql_fetch_row" target="_blank"><em>source</em></a><br />
<span id="more-14"></span><br />
So, first value is <strong>$result[0]</strong>, second is <strong>$result[1]</strong>, and so on.</p>
<p><strong>mysql_fetch_assoc()</strong> &#8220;returns an asociative array of strings that corresponds to the fetched row&#8221;. <a title="PHP.net Reference for mysql_fetch_assoc" href="http://us3.php.net/mysql_fetch_assoc" target="_blank"><em>source</em>  </a>That is, each value that is returned is part of a pair: a &#8220;key&#8221; (index) value, and the actual value.  The &#8220;key&#8221; names correspond to the name of the field in the database.</p>
<p>So, first value is <strong>$result['name']</strong>, second is <strong>$result['email']</strong>, and so on.</p>
<p>When I&#8217;m coding, I like to have access to both the &#8220;key&#8221; values, and the numeric index.  Now, in all honesty, I almost always use &#8220;key&#8221; values, over numeric, but I still like having the option.  This is where <strong>mysql_fetch_array()</strong> comes in.</p>
<p><strong>mysql_fetch_array()</strong> will give you &#8220;an array with both associative and number indices.&#8221; <a title="PHP.net Reference for mysql_fetch_array" href="http://us3.php.net/manual/en/function.mysql-fetch-array.php" target="_blank"><em>source</em></a></p>
<p>Many other languages can do both associative (&#8220;key&#8221; index) and numeric indices by default.  Others are constrained to just numeric.  Here, PHP is giving us both.  </p>
<p>The question: <em>how?</em></p>
<p>The answer: <em>it cheats!</em></p>
<p>Seriously.  It cheats.  What do I mean?  I mean that the numeric indices are not truly referencing the position of the value.  One would expect that &#8220;<strong>$result[0]</strong>&#8221; would reference the value in the first position.  It does not.  This can easily be verified with a handy <strong>print_r()</strong>, or the following loop:</p>
<blockquote><p><strong>$result = mysql_fetch_array($result);<br />
foreach ($result as $key =&gt; $value)<br />
echo $key . &#8216;=&#8217; . $value . &#8216;&lt;br /&gt;&#8217;;</strong></p></blockquote>
<p>The result of that loop, on a data set I was working with, returned the following:</p>
<blockquote><p>0=mack<br />
username=mack<br />
1=1<br />
job_num=1<br />
2=Mack Staples<br />
name=Mack Staples<br />
3=5415551234<br />
phone_num= 5415551234<br />
4=test@example.com<br />
email=test@example.com</p></blockquote>
<p>What do you notice?  Every value is in there twice, and while the &#8217;0&#8242; value is indeed in the first position, the rest of the numbers don&#8217;t correlate.  So what is it doing?</p>
<p><strong>mysql_fetch_array()</strong> is building a larger array (twice the size of either of the other functions) and using the numbers not as true indexes, but as associative &#8220;key&#8221; values.  This means a couple of things.</p>
<p>1 &#8211; You are wasting speed if you reference the values, but don&#8217;t use quotes.  According to <a title="Reinhold Weber's Blog" href="http://reinholdweber.com/?p=3" target="_blank"><em>Reinhold Weber&#8217;s Blog</em></a>:  #17: &#8220;<span>$row[’id’] is 7 times faster than $row[id]&#8220;.  If you&#8217;re doing this a lot, and often&#8230; ouch.</span></p>
<p>2 &#8211; You&#8217;re wasting speed just using it.  From having to initially populate twice the data into an array, to a slowdown copying the array to other memory locations, this can cost you performance.</p>
<p>So, what can we do about it?</p>
<p>1 &#8211; Use quotes!  1/7th of the time to get <strong>$result['2']</strong> compared to <strong>$result[2]</strong>.  Simple speed fix.</p>
<p>2 &#8211; Using <strong>mysql_fetch_array()</strong> is up to you.  For what I usually use database results for, the difference is negligible.  I do small, infrequent queries, and I like having both numeric and associated indices.  To me, it&#8217;s worth the <em>minor</em> performance hit.</p>
<p>Code Safe. Code Smart.  <em>Be paranoid.</em></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.securityandcaffeine.com%2F2008%2F04%2F03%2Fphp-mysql-and-mysql_fetch_array%2F&amp;title=PHP%2C%20MySQL%2C%20and%20mysql_fetch_array%28%29" id="wpa2a_10"><img src="http://www.securityandcaffeine.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.securityandcaffeine.com/2008/04/03/php-mysql-and-mysql_fetch_array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
