<?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>Adventures in Japanese Programming &#187; Uncategorized</title>
	<atom:link href="http://blog.japanesetesting.com/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.japanesetesting.com</link>
	<description>Design and Programming of a mISV Site</description>
	<lastBuildDate>Fri, 23 Jul 2010 13:29:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to limit user access in CakePHP: findMy</title>
		<link>http://blog.japanesetesting.com/2010/05/07/how-to-limit-user-access-in-cakephp-findmy/</link>
		<comments>http://blog.japanesetesting.com/2010/05/07/how-to-limit-user-access-in-cakephp-findmy/#comments</comments>
		<pubDate>Fri, 07 May 2010 03:19:25 +0000</pubDate>
		<dc:creator>Harisenbon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[system design]]></category>

		<guid isPermaLink="false">http://blog.japanesetesting.com/?p=170</guid>
		<description><![CDATA[Perhaps like many people starting a CakePHP project, I created a site where users could log in and create/modify their own files (in my case Japanese flashcards and tests) while not being able to screw with other people&#8217;s stuff.
One would think that you could solve this with some nifty ACL and Auth work, but if [...]]]></description>
			<content:encoded><![CDATA[<p>Perhaps like many people starting a CakePHP project, I created a <a title="Ippatsu! Japanese Testing" href="http://japanesetesting.com">site where users could log in and create/modify their own files</a> (in my case Japanese flashcards and tests) while not being able to screw with other people&#8217;s stuff.</p>
<p>One would think that you could solve this with some nifty ACL and Auth work, but if you thought that, then you would be <strong>wrong</strong>.</p>
<p>Unforunately ACL only lets you determine what <strong>actions</strong> a user is allowed to perform, not on which items they&#8217;re allowed to perform it.</p>
<p>And Auth only checks to make sure a person is logged in, not who they are or what they&#8217;re doing.</p>
<h2>The Traditional Way</h2>
<p>However, thanks to the glory of cake, it&#8217;s not that hard to limit a search by user! Just replace your find with the following function.</p>
<pre class="brush: php;">
$my_file = $this-&gt;SomeModel-&gt;findAllByUserId($this&gt;Auth-&gt;user(id);
</pre>
<p>And we&#8217;re done!</p>
<h3>Oops! Not so fast!</h3>
<p>We just got all of the SomeModels! Well <strong>crap</strong>, I guess we&#8217;ll have to make an option array</p>
<pre class="brush: php;">
$opt = array(
   'conditions' =&gt; array(
      'SomeModel.user_id' =&gt; $this-&gt;Auth-&gt;user('id'),
   ),
   'order' =&gt; 'SomeModel.date DESC',
);
$my_file = $this-&gt;SomeModel-&gt;find('first', $opt);
</pre>
<p>That&#8217;s a lot of work for just getting my latest SomeModel. Even more work if I have to replace<strong> every</strong> instance of a simple find with something like that. I also need to do the same thing for every <strong>save</strong> and <strong>delete</strong> as well to make sure that they&#8217;re not saving over someone else&#8217;s data.</p>
<p>Sure, I could put that code throughout all my Controllers, but that&#8217;s messy &#8212; and if I forget to put it somewhere <strong>someone gets their data deleted.</strong></p>
<h2>So what can we do?</h2>
<p>Well, I don&#8217;t know about you, but I put a function in my AppModel file that lets me easily make sure that whoever&#8217;s touching the file has permission to do so.</p>
<pre class="brush: php;">
function findMy($type, $options=array())
{
   if($this-&gt;hasField('user_id') &amp;&amp; !empty($_SESSION['Auth']['User']['id'])){
      $options['conditions'][$this-&gt;alias.'.user_id'] = $_SESSION['Auth']['User']['id'];
      return parent::find($type, $options);
   }
   else{
      return parent::find($type, $options);
   }
}
</pre>
<h3>What this does</h3>
<ol>
<li>Make sure that the model has an &#8216;user_id&#8217; field</li>
<li>Make sure that the user is logged in</li>
<li>Add the user_id condition to the find options
<ul>
<li>Be sure to use the $this-&gt;alias in case you&#8217;re using an alias for the Model in a BelongsTo or HasMany relationship.</li>
</ul>
</li>
<li>Find the data</li>
</ol>
<p>This is a pretty simple function, and can form the base for any logic that you want to do.</p>
<p>For example, if you want to allow Admins to view anything, just add an <strong>$_SESSION['Auth']['User']['role'] == &#8216;admin&#8217;</strong> to the mix.</p>
<p>If you want to allow users access to any &#8216;public&#8217; items, add a condition for <strong>&#8216;OR is_public == true&#8217;</strong></p>
<h2>Extending the function</h2>
<p>This function is set in the AppModel, so it can be overridden in any of your defined models if you need to do any custom filtering on a per-model basis.</p>
<p>Also, it is possible to extend this to <strong>save</strong> and <strong>delete</strong> functions, to make sure that users are only saving or deleting their own files. If they try to delete a file that doesn&#8217;t belong to them, return <strong>false</strong> and alert the user that that file could not be found.</p>
<pre class="brush: php;">
function deleteMy($id = null, $cascade = true)
{
   if (!empty($id)) {
      $this-&gt;id = $id;
   }
   $id = $this-&gt;id;

   if($this-&gt;hasField('user_id') &amp;&amp; !empty($_SESSION['Auth']['User']['id'])){
      $opt = array(
         'conditions' =&gt; array(
            $this-&gt;alias.'.user_id' =&gt; $_SESSION['Auth']['User']['id'],
            $this-&gt;alias.'.id' =&gt; $id,
            ),
         );
      if($this-&gt;find('count', $opt) &gt; 0){
         return parent::delete($id, $cascade);
      }
      else{
         return false;
      }

   }
   else
      return parent::delete($id, $cascade);
}
</pre>
<h2>Conclusion</h2>
<p>The power of CakePHP comes from it&#8217;s infinite extensibility, and the fact that at it&#8217;s core, it&#8217;s still <strong>just a PHP program</strong>.</p>
<p>While I recommend following MVC practices, and to use the built-in CakePHP functions as much as possible, there are times when <a href="http://blog.japanesetesting.com/2009/11/24/the-programmers-folly-simple-is-best/">you just need to do it the simple way</a>.</p>
<p>Also, for those who balk at my use of $_SESSION, I talked with one of the CakePHP core developers at a conference a while ago, and was asking him about this problem.</p>
<p>I asked,</p>
<blockquote><p>&#8220;Why is Auth only available in controllers? It would be much more useful if we could use it everywhere. Is it a design decision?&#8221;</p></blockquote>
<p>He replied.</p>
<blockquote><p>&#8220;Because it&#8217;s a component. That&#8217;s the only reason.&#8221;</p></blockquote>
<p>Remember: <strong>the framework is there to help you</strong>. You are not its slave.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.japanesetesting.com/2010/05/07/how-to-limit-user-access-in-cakephp-findmy/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Widgity! Multi-element designs with CakePHP</title>
		<link>http://blog.japanesetesting.com/2010/04/27/widgity-multi-element-designs-with-cakephp/</link>
		<comments>http://blog.japanesetesting.com/2010/04/27/widgity-multi-element-designs-with-cakephp/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 09:54:01 +0000</pubDate>
		<dc:creator>Harisenbon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.japanesetesting.com/?p=143</guid>
		<description><![CDATA[Since I started programming with CakePHP almost two years ago, I&#8217;ve had a bunch of widgets on the right side of my page ( http://www.JapaneseTesting.com ).
Which pages each of these widgets appear on is guided by the controller, action, user role, or any combination thereof.
I used to deal with this in a Controller-Model combination, called [...]]]></description>
			<content:encoded><![CDATA[<p>Since I started programming with <a href="http://cakephp.org" target="_blank">CakePHP</a> almost two years ago, I&#8217;ve had a bunch of widgets on the right side of my page ( <a href="http://japanesetesting.com" target="_blank">http://www.JapaneseTesting.com</a> ).</p>
<p>Which pages each of these widgets appear on is guided by the controller, action, user role, or any combination thereof.</p>
<p>I used to deal with this in a Controller-Model combination, called with resource-heavy Request Actions. Then I found a better way. A <strong>Simple </strong>way. A way with <strong>Branching Elements</strong>.</p>
<p>Read on to see how to dynamically put customizable element blocks on any of your pages.</p>
<h3><img class="alignright size-full wp-image-164" style="margin: 5px 0 5px 5px;" title="JapaneseTesting.com  Widgets" src="http://blog.japanesetesting.com/wp-content/uploads/2010/04/jp_widgets.png" alt="JapaneseTesting.com Widgets" width="202" height="435" />The Layout</h3>
<p>On my site, each of the widgets is a separate element, and the way I used to deal with deciding which page to display them on was by running a RequestAction on my Widgets Controller, which in turn grabbed the widget data from the DB, checked which widgets needed to be displayed when, grabbed the elements, and then displayed each of them. Overall a very <strong>thorough </strong>way of managing and displaying the widgets, but <strong>not </strong>very nice on my load-time (especially when <a href="http://blog.japanesetesting.com/2009/11/12/cakephp-debugkit-take-back-your-debugging/">I started using DebugKit</a>).</p>
<p>Eventually I started using a widgeting component of my own design that took an array of widget types, their caching statuses, various data, etc and threw it all into a controller variable which in turn threw it into a helper and then finally displayed the widgets.</p>
<p>This might have actually been <strong>slower</strong> than the previous DB calls, and let code to be placed in either controllers or views, which defeated the whole purpose of having a single <em>consolidated</em> place to insert the code.</p>
<p>But then I found a better way:</p>
<h3>Enter the $this-&gt;name and $this-&gt;action variables!</h3>
<p>Every view <strong>(and every element in that view)</strong> has the <strong>$this-&gt;name</strong> (the controller name) and the <strong>$this-&gt;action</strong> (the controller&#8217;s action name) available to them. That means that in any view, we know exactly what method has been called to get there (and thus, what variables, data, etc are available).</p>
<p>We&#8217;re going to use these variables, along with some Auth information in order to create widgets that obey a set of rules to display a bunch of widgets.</p>
<h1>Our five widget elements</h1>
<p>We&#8217;re going to have 5 elements that are going to be displayed to the user, depending on what page they&#8217;re looking at, and if they&#8217;re logged in or not.</p>
<ul>
<li>who&#8217;s logged in Display  [ Shown to users who are logged in ]</li>
<li>latest Articles Display    [ Always Shown ]</li>
<li>latest Posts Display         [ Only available on Forum pages ]</li>
<li>Upgrade now Advert      [ Available on every page except the Forum  pages ]</li>
<li>Handy Post editing          [ Shown only on the Forums/edit page ]</li>
</ul>
<p>We&#8217;re going to store these in our elements folder with the following names:</p>
<ul>
<li>/elements/widgets/whos_online.ctp</li>
<li>/elements/widgets/latest_articles.ctp</li>
<li>/elements/widgets/latest_posts.ctp</li>
<li>/elements/widgets/upgrade_now.ctp</li>
<li>/elements/widgets/post_edit.ctp</li>
</ul>
<h2>Controller Widgets</h2>
<p>Then, we&#8217;re going to create a couple of &#8220;controller&#8221; widgets that allow us to break up the show/don&#8217;t show logic a little bit. These are each going to be named after the controller that will display them.</p>
<p><strong>It&#8217;s up to you whether or not you want to use these,</strong> or just stick them in the main controller (see below) but I like the distribution of data, and it keeps me from having a huge main controller loaded on every page load.</p>
<p>Also, because I might have multiple widget bars with different contents, I&#8217;m going to put these in a different elements folder, named &#8220;sidebar_rt&#8221;</p>
<ul>
<li>Controls all the Widgets that are Available only on the <strong>Forms</strong> pages [ /elements/sidebar_rt/forms.ctp ]</li>
</ul>
<h2>The Main Controller</h2>
<p>Finally, we&#8217;re going to make a main controller element that will call all the widgets from a single location so we don&#8217;t have a lot of unnecessary code in our layout.</p>
<ul>
<li>Main Widget Controller [ /elements/sidebar_rt/index.ctp ]</li>
</ul>
<h1>Putting it all together</h1>
<p>So, now that we have the files created, let&#8217;s see what goes in them.</p>
<p>First, in your <strong>/layouts/default.ctp</strong> file, we put the main sidebar controller into the layout.</p>
<pre class="brush: php;">
// /layouts/default.ctp
&lt;div class=&quot;sidebar right&quot;&gt;
     &lt;?= $this-&gt;element('sidebar_rt/index'); ?&gt;
&lt;/div&gt;
</pre>
<p>Then, set the logic for the sidebar widgets:</p>
<pre class="brush: php;">
// /elements/sidebar_rt/index.ctp

// Include the widget that will always be shown
echo $this-&gt;element('widgets/latest_articles');

// Include the widgets that are shown only if the user is logged in
if( !empty( $_user_info ) ){
   echo $this-&gt;element('widgets/whos_online');
}

// Include the widget that's shown everywhere except the forums
if( $this-&gt;name != 'forums' ){
   echo $this-&gt;element('widgets/upgrade_now');
}

// Finally, include the Controller widget, which is named after the controller name
echo $this-&gt;element('sidebar_rt/'.$this-&gt;name);
</pre>
<p>Now we&#8217;ve got three of our five widgets down.</p>
<p>Next, we just need to set up the controllers for the forums and users widgets</p>
<pre class="brush: php;">
// /elements/sidebar_rt/forums.ctp

// These widgets will be displayed on every page in the forums controller:
echo $this-&gt;element('widgets/latest_posts');

// These widgets will only be shown for the corresponding action
switch($this-&gt;action){
   case 'edit':
      echo $this-&gt;element('widgets/post_edit');
      break;
}
</pre>
<p>And we&#8217;re done!</p>
<h1>Using variables in Elements</h1>
<p>Remember earlier when we said that we can use our view variables in Elements? Well, since we know when each widget will be used (controller/action), we know what variables will be available to them.</p>
<p>So, let&#8217;s look at the &#8216;post_edit&#8217; widget up above. We want to have buttons to edit the current post, but we need to be able to access the current element&#8217;s id and other data in order to make a good edit box.</p>
<p>Luckily, we have all the data from the main view available in the widget as well!</p>
<pre class="brush: php;">
// /elements/widgets/post_edit.ctp

&lt;div class=&quot;actions&quot;&gt;
   &lt;a href=&quot;/posts/publish/&lt;?=$this-&gt;data['Post']['id']?&gt;&quot;&gt;Publish this post&lt;/a&gt;
   &lt;a href=&quot;/posts/preview/&lt;?=$this-&gt;data['Post']['id']?&gt;&quot;&gt;Preview this post&lt;/a&gt;
   &lt;a href=&quot;/posts/delete/&lt;?=$this-&gt;data['Post']['id']?&gt;&quot;&gt;Delete this post&lt;/a&gt;
&lt;/div&gt;

&lt;div class=&quot;tags&quot;&gt;
   &lt;? foreach($this-&gt;data['Tags'] as $tag ):?&gt;
      &lt;a href=&quot;/tags/delete/&lt;?=$tag['id']?&gt;&quot;&gt;&lt;?=$tag['name']?&gt;&lt;/a&gt;
   &lt;?endforeach;?&gt;
   &lt;a href=&quot;/tags/add/post_id:&lt;?=$this-&gt;data['Post']['id']?&gt;&quot;&gt;Add a tag&lt;/a&gt;
&lt;/div&gt;
</pre>
<h1>Conclusion: Really not that hard</h1>
<p>Really, getting a good widget system working is fairly simple, and doesn&#8217;t require a lot of backend programming, but you need to have a solid idea of what you want to display in your sidebar, and how you want to organize the data.</p>
<p>Remember that your widget data is usually <strong>supplemental to your page&#8217;s message</strong>, and you shouldn&#8217;t waste precious processor cycles or memory with a high-overhead system when a simple switch block will do the trick.</p>
<h2>Bonus Chatter: Doing it all in one stroke</h2>
<p>So, let&#8217;s say you don&#8217;t like having all those file calls and separation of files: if you want, you can do it all in <strong>one &#8220;controller&#8221; element</strong>.</p>
<p>So, like before, we have the same element setup</p>
<ul>
<li>/elements
<ul>
<li>/widgets
<ul>
<li>whos_online.ctp</li>
<li>latest_articles.ctp</li>
<li>latest_posts.ctp</li>
<li>upgrade_now.ctp</li>
<li>post_edit.ctp</li>
</ul>
</li>
<li>sidebar_rt.ctp</li>
</ul>
</li>
</ul>
<p>And in the layout, we&#8217;re just going to call the simple one liner:</p>
<pre class="brush: php;">
// /layouts/default.ctp
&lt;div class=&quot;sidebar right&quot;&gt;
     &lt;?= $this-&gt;element('sidebar_rt'); ?&gt;
&lt;/div&gt;
</pre>
<p>And then in the <strong>sidebar_rt.ctp</strong> file, we include a long switch statement</p>
<pre class="brush: php;">
// /elements/sidebar_rt.ctp

// Include the widget that will always be shown
echo $this-&gt;element('widgets/latest_articles');

// Include the widgets that are shown only if the user is logged in
if( !empty( $_user_info ) ){
   echo $this-&gt;element('widgets/whos_online');
}

// Include the widgets for each controller / action page
switch( $this-&gt; name ){
   case 'users':
      echo $this-&gt;element('widgets/upgrade_now');
      break;
   case 'forums':
      echo $this-&gt;element('widgets/latest_posts');
      break;
   case 'posts':
      switch( $this-&gt;action ){
         case 'edit':
            echo $this-&gt;element('widgets/post_edit');
            break;
      }
      echo $this-&gt;element('widgets/upgrade_now');
      break;
   default':
      echo $this-&gt;element('widgets/upgrade_now');
      break;

}
</pre>
<p>It&#8217;s a little more unwieldy than the separated version, but if you only have a few controllers that have special cases it might be easier to have all your widget data in one place.</p>
<p>Have any comments? Know of a better way? I&#8217;d love to hear them!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.japanesetesting.com/2010/04/27/widgity-multi-element-designs-with-cakephp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Things I never knew about CSS</title>
		<link>http://blog.japanesetesting.com/2009/10/06/things-i-never-knew-about-css/</link>
		<comments>http://blog.japanesetesting.com/2009/10/06/things-i-never-knew-about-css/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 08:20:07 +0000</pubDate>
		<dc:creator>Harisenbon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.japanesetesting.com/?p=99</guid>
		<description><![CDATA[Smashing Magazine, for all their &#8220;Top 10&#8243; and &#8220;Best 50&#8243; articles, continues to occasionally post very helpful and mind-bending articles about Design and Production.
Their newest article about CSS is aimed at the CSS beginner, but has a few tricks that I wasn&#8217;t even aware of.
Floating and Overflow
The first, I want to point out is something [...]]]></description>
			<content:encoded><![CDATA[<p>Smashing Magazine, for all their &#8220;Top 10&#8243; and &#8220;Best 50&#8243; articles, continues to occasionally post very helpful and mind-bending articles about Design and Production.</p>
<p>Their newest article about CSS is aimed at the CSS beginner, but has a few tricks that I wasn&#8217;t even aware of.</p>
<h2>Floating and Overflow</h2>
<p>The first, I want to point out is something that has always bugged me: the fact that a floated image is not completely contained within its parent container. Apparently this can be fixed simply by adding overflow:hidden to the div (autoflow:auto also works).</p>
<p><a href="http://www.sohtanaka.com/web-design/examples/css-back-to-basics/overflow-hidden-trick.htm"><img class="alignnone size-full wp-image-103" title="Floats and Images" src="http://blog.japanesetesting.com/wp-content/uploads/2009/10/6_b.jpg" alt="Floats and Images" width="500" height="254" /></a></p>
<h2><a href="http://www.sohtanaka.com/web-design/examples/css-back-to-basics/overflow-hidden-trick.htm" target="_blank"></a>Floating and Double Margins (ie6 Bug)</h2>
<p>Additionally, Internet Explorer 6 seems to <strong>double</strong> the margin of floated elements. So your 5px margin becomes 10px in IE6, and is the cause of all of my perfectly aligned floating divs crashing to the ground when I test the page in IE6 <strong>(grumble grumble hate hate)</strong></p>
<p>This is also easy to fix: just add <strong>display: inline</strong> to your floated element.</p>
<pre class="brush: css;">
.floated_element {
     float: left;
     width: 200px;
     margin: 5px;
     display: inline; /*--IE6 workaround--*/
     }
</pre>
<h2>Deserves a once-over</h2>
<p>There were alot of other great tips and points that even seasoned CSS-ers should look at &#8212; you never know when you might find something new, or an easy way to do something you had been futzing with. If you get a chance, it&#8217;s <a title="Smashing Magazine" href="http://www.smashingmagazine.com/2009/10/05/mastering-css-coding-getting-started/" target="_blank">definitely worth a look.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.japanesetesting.com/2009/10/06/things-i-never-knew-about-css/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Delete Cached Models in CakePHP</title>
		<link>http://blog.japanesetesting.com/2009/09/15/delete-cached-models-in-cakephp/</link>
		<comments>http://blog.japanesetesting.com/2009/09/15/delete-cached-models-in-cakephp/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 09:25:45 +0000</pubDate>
		<dc:creator>Harisenbon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.japanesetesting.com/?p=78</guid>
		<description><![CDATA[Did you know that CakePHP automatically caches a lot of your data so that you don&#8217;t have to call (for example) model references over and over again? Isn&#8217;t that nice?
Did you know that if you make a change to your database (for example: adding a column) and the caches haven&#8217;t been updated, it will neither [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Did you know</strong> that CakePHP automatically caches a lot of your data so that you don&#8217;t have to call (for example) model references over and over again? Isn&#8217;t that nice?</p>
<p><strong>Did you know </strong>that if you make a change to your database (for example: adding a column) and the caches haven&#8217;t been updated, it will neither <strong>SAVE </strong>nor <strong>RETRIEVE </strong>any data relating to those changes?</p>
<p><strong>Did you know</strong> that cake won&#8217;t tell you that your models might be out of date, and that you can easily waste 2-3 <strong>HOURS</strong> of your life on something stupid like that?</p>
<p>Do yourself a favor: When you change your database, delete <strong>EVERYTHING </strong>in <strong>app\tmp\cache\models</strong></p>
<p>And thankyou for the heads up, <a href="http://snook.ca/archives/cakephp/delete_cached_models/" target="_blank">Snook.ca</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.japanesetesting.com/2009/09/15/delete-cached-models-in-cakephp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wordpress and Syntax Highlighting</title>
		<link>http://blog.japanesetesting.com/2009/09/12/wordpress-and-syntax-highlighting/</link>
		<comments>http://blog.japanesetesting.com/2009/09/12/wordpress-and-syntax-highlighting/#comments</comments>
		<pubDate>Sat, 12 Sep 2009 00:00:01 +0000</pubDate>
		<dc:creator>Harisenbon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.japanesetesting.com/?p=73</guid>
		<description><![CDATA[Just a quicky:
I was looking for some good syntax-highlighting plugins for WordPress, but so many of them were hard to use, required configuration or just plain mangled my code.
So then I found Syntax Highlighter Evolved. Set it up in 5 minutes, easy as pie to use, and no more code mangling. Definitely recommended.
Syntax Highlighter Evolved [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quicky:</p>
<p>I was looking for some good syntax-highlighting plugins for WordPress, but so many of them were hard to use, required configuration or just plain mangled my code.</p>
<p>So then I found <a href="http://wordpress.org/extend/plugins/syntaxhighlighter/" target="_blank">Syntax Highlighter Evolved</a>. Set it up in 5 minutes, easy as pie to use, and no more code mangling. Definitely recommended.</p>
<h3>Syntax Highlighter Evolved is recommended by some guy in Japan!</h3>
<p>Now that&#8217;s a tagline that will sell products if I ever heard one!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.japanesetesting.com/2009/09/12/wordpress-and-syntax-highlighting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CakePHP and RememberMe &#8212; AutoLogins for the soul</title>
		<link>http://blog.japanesetesting.com/2009/09/11/cakephp-and-rememberme-autologins-for-the-soul/</link>
		<comments>http://blog.japanesetesting.com/2009/09/11/cakephp-and-rememberme-autologins-for-the-soul/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 23:45:01 +0000</pubDate>
		<dc:creator>Harisenbon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.japanesetesting.com/?p=48</guid>
		<description><![CDATA[So, as I as spent half the day today working on getting my &#8220;Remember Me&#8221; function to work, I figured it would be a good thing to share with the rest of the world, so that they didn&#8217;t have to ever EVER put up with the kind of stuff I went through today.
So, first of [...]]]></description>
			<content:encoded><![CDATA[<p>So, as I as spent half the day today working on getting my &#8220;Remember Me&#8221; function to work, I figured it would be a good thing to share with the rest of the world, so that they didn&#8217;t have to ever EVER put up with the kind of stuff I went through today.</p>
<p>So, first of all, we all know what a remember me function is, right?</p>
<div id="attachment_52" class="wp-caption alignnone" style="width: 343px"><img class="size-full wp-image-52 " title="rememberme_1" src="http://blog.japanesetesting.com/wp-content/uploads/2009/09/rememberme_1.jpg" alt="Yup, that's it (Wordpress)" width="333" height="347" /><p class="wp-caption-text">Yup, that&#39;s it (Wordpress)</p></div>
<div id="attachment_50" class="wp-caption alignnone" style="width: 319px"><img class="size-full wp-image-50 " title="remember_me2" src="http://blog.japanesetesting.com/wp-content/uploads/2009/09/remember_me2.jpg" alt="Yup, that's it (google)" width="309" height="221" /><p class="wp-caption-text">And there it is again (google)</p></div>
<p>The &#8220;Remember me&#8221; function is a little checkbox that the user checks when they log in so that they don&#8217;t have to bother logging in again as long as they keep visiting the site every XX days where XX is the number of days that the site will remember them for.</p>
<h1>Sounds easy! Let&#8217;s do it in cake!</h1>
<p>Great idea! Cake should make it super easy! And if you search for cake and remember me functions, a lot (or rather a lot of variations based around 2 or 3 guys&#8217; code) of sites will pop up. I personally recommend the <a href="http://dsi.vozibrale.com/articles/view/rememberme-component-the-final-word" target="_blank">RememberMe component from the Neutrino CMS</a>, as it&#8217;s small, lightweight and doesn&#8217;t take much configuring. (And don&#8217;t believe the <a href="http://www.webdevelopment2.com/cakephp-auth-component-tutorial-3/" target="_blank" rel="nofollow">page that comes up when you enter &#8220;CakePHP rememberme&#8221; into google</a>. That&#8217;s what got me off on the wrong foot)</p>
<p>And if you plan on something really simple with your site (i.e. logged in or not logged in) then it&#8217;s really easy to set up.</p>
<ol>
<li>Get the component that you&#8217;re going to use (I used RememberMe)</li>
<li>Plop it in the components.</li>
<li>Set your references to it in your users/login function, and</li>
<li>(The most important part) <strong>attach the component&#8217;s cookie checking function to the app_controller&#8217;s beforeRender function.</strong></li>
</ol>
<p>Now, why&#8217;s the last step the most important? Because the &#8220;remember me&#8221; cookie needs to be checked for every time a page is visited, otherwise it can&#8217;t do it&#8217;s job. The first version of the remember me code I had used when I first started CakePHP had the cookie management in the users/login function. Like this:</p>
<pre class="brush: php;">
function login() {
//-- code inside this function will execute only when autoRedirect was set to false (i.e. in a beforeFilter).
if ($this-&gt;Auth-&gt;user()) {
if (!empty($this-&gt;data) &amp;amp;&amp;amp; $this-&gt;data['User']['remember_me']) {
$cookie = array();
$cookie['username'] = $this-&gt;data['User']['username'];
$cookie['password'] = $this-&gt;data['User']['password'];
$this-&gt;Cookie-&gt;write('Auth.User', $cookie, true, '+2 weeks');
unset($this-&gt;data['User']['remember_me']);
}
$this-&gt;redirect($this-&gt;Auth-&gt;redirect());
}
if (empty($this-&gt;data)) {
$cookie = $this-&gt;Cookie-&gt;read('Auth.User');
if (!is_null($cookie)) {
if ($this-&gt;Auth-&gt;login($cookie)) {
//  Clear auth message, just in case we use it.
$this-&gt;Session-&gt;del('Message.auth');
$this-&gt;redirect($this-&gt;Auth-&gt;redirect());
} else { // Delete invalid Cookie
$this-&gt;Cookie-&gt;del('Auth.User');
}
}
}
}
</pre>
<p>Makes perfect sense, right?</p>
<h1>NO</h1>
<p>The problem is that users/login only gets called when there&#8217;s a login to be processed (which is not explained very well in the documentation). This becomes a problem when you are using the Auth-&gt;allow() array to let people access parts of the site, and not others. (Even more of a problem when the same page has different data for logged in and non-logged in users)</p>
<p>What happens is that since the action is allowed, Cake decides that there&#8217;s no reason to check for a login, so even if the login cookie exists, it doesn&#8217;t get checked because the Users/Login function never gets called.</p>
<p>So, in most cases, this is not a problem &#8212; just put the RememberMe-&gt;check() function into the AppController&#8217;s beforeRender &#8212; but what if you want do something more after a user logs in? Something like increasing their login count, setting login time, getting info about them, etc etc.</p>
<h1>Custom Functions and Remember Me</h1>
<p>Most RememberMe components and solutions have a provision for callbacks after a successful cookie login (much like isAuthorized) however (also like isAuthorized) the function has to be in either:</p>
<ul>
<li>The AppController</li>
<li>The Current Controller</li>
</ul>
<p>Because the component only has access to the controller that&#8217;s currently calling it, you&#8217;re confined to either the code in the <strong>AppController </strong>or the currently viewed controller. And if your current controller is Articles (because your user is looking at an article) then we can&#8217;t directly access the users controller to run our little _after_login function. <span style="color: #808080;">(With <strong>Models </strong>you can get to from almost anywhere in your code through skillful use of relations, but <strong>Controllers </strong>are a bit trickier)</span></p>
<p>So, what do we do? We take a little hint from the cake library, and use the wonderful <strong>App:import</strong> function to forcibly bring the Controller to us. Now this is an <strong><span style="text-decoration: underline;">expensive </span></strong>call, because it&#8217;s essentially ringing up an entire new controller with all its associated models, et al. <span style="color: #ff0000;">So we need to be sure to do this only when the user is logging in through a cookie. <span style="color: #000000;">That way, it&#8217;ll only happen once for the user&#8217;s session, and we can quickly get rid of its memory-eating load on the next page view.</span></span></p>
<p><span style="color: #ff0000;"><span style="color: #000000;">So, let&#8217;s look at some code:</span></span></p>
<h1><span style="color: #ff0000;"><span style="color: #000000;">The Original RememberMe component</span></span></h1>
<pre class="brush: php;">
&lt;?php
class RememberMeComponent extends Object
{
var $components = array('Auth', 'Cookie');
var $controller = null;

/**
* Cookie retention period.
*
* @var string
*/
var $period = '+2 weeks';
var $cookieName = 'User';

function startup(&amp;$controller)
{
$this-&gt;controller =&amp; $controller;
}

function remember($username, $password)
{
$cookie = array();
$cookie[$this-&gt;Auth-&gt;fields['username']] = $username;
$cookie[$this-&gt;Auth-&gt;fields['password']] = $password;
$this-&gt;Cookie-&gt;write($this-&gt;cookieName, $cookie, true, $this-&gt;period);
}

function check()
{
$cookie = $this-&gt;Cookie-&gt;read($this-&gt;cookieName);

if (!is_array($cookie) || $this-&gt;Auth-&gt;user())
return;

if ($this-&gt;Auth-&gt;login($cookie))
{
$this-&gt;Cookie-&gt;write($this-&gt;cookieName, $cookie, true, $this-&gt;period);
}
else
{
$this-&gt;delete();
}
}

function delete()
{
$this-&gt;Cookie-&gt;del($this-&gt;cookieName);
}
}

?&gt;
</pre>
<h1>So, this check function has got to go</h1>
<p>We need to be able to load the <strong>UsersController</strong> so that we can get to our <strong>_post_login()</strong> function to do all our login magic like checking how the user&#8217;s subscription is going, how long many times they&#8217;ve logged in, etc.</p>
<pre class="brush: php;">
function check()
{
// If you want to change the cookie name, change it here
$this-&gt;Cookie-&gt;name = 'rememberme';
$cookie = $this-&gt;Cookie-&gt;read($this-&gt;cookieName);

if (!is_array($cookie) || $this-&gt;Auth-&gt;user())
return;

if ($this-&gt;Auth-&gt;login($cookie))
{
$this-&gt;Cookie-&gt;write($this-&gt;cookieName, $cookie, true, $this-&gt;period);
if (!App::import('Controller', 'Users')) {
return false;
}
$className = 'UsersController';
$Ctrl =&amp; new $className();
$Ctrl-&gt;constructClasses();
$Ctrl-&gt;_post_login($this-&gt;Auth-&gt;user('id'));
}
else
{
$this-&gt;delete();
}
}
</pre>
<p>So, here&#8217;s my code as it stands now. What I&#8217;ve done is if there is a cookie, and you can successfully login with said cookie (Through <strong>$this-&gt;Auth-&gt;login($cookie)</strong> ) then we&#8217;re going to spin up the <strong>Controller </strong>called <strong>Users </strong>through the <strong>App:import</strong> function. If it can&#8217;t load, then we return false, but if it <strong>DOES </strong>load, then we create a new Controller and construct it so that we can then call our post_login script with the <strong>Auth-&gt;user</strong> Info. Everything else is set up just how the instructions on the <a href="http://dsi.vozibrale.com/articles/view/rememberme-component-the-final-word" target="_blank">RememberMe Component </a>Page say.</p>
<h1>Conclusion</h1>
<p>Getting a RememberMe function working in CakePHP really isn&#8217;t that difficult, but the problem (like a lot of CakePHP) is that the signal-to-noise ratio of those who know what they&#8217;re doing and those that don&#8217;t is incredibly high. I don&#8217;t profess to have all the answers, but hopefully just some of the problems that I&#8217;ve had will help others who are struggling with the same issues.</p>
<p>Till next time!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.japanesetesting.com/2009/09/11/cakephp-and-rememberme-autologins-for-the-soul/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Harisenbon Speaks: Welcome to the blog!</title>
		<link>http://blog.japanesetesting.com/2009/08/19/hello-world/</link>
		<comments>http://blog.japanesetesting.com/2009/08/19/hello-world/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 00:29:21 +0000</pubDate>
		<dc:creator>Harisenbon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.japanesetesting.com/?p=1</guid>
		<description><![CDATA[In which Harisenbon lays out the purpose for this blog, gives some shoutouts to those who have helped him figure out how to get a site up and running, and is generally rather long-winded.]]></description>
			<content:encoded><![CDATA[<p>So here I am starting my first foray into the world of blogging.</p>
<p>I&#8217;ve never been much into blogging, even though I have to my credit: a livejournal page, a shared facebook account, a mixi account, a blogger account, and whatever microsoft was using for their blog system 2 years ago.</p>
<p>Suffice it to say none of these have ever been really updated with anything resembling regularity.</p>
<p>But I&#8217;m hoping that this blog will be a bit different. I&#8217;ve been inspired (or beaten around the head, whichever way you want to look at it) by my friend who runs <a href="http://bingocardcreator.com" target="_blank">BingoCardcreator.com</a> and is also the owner of the increasingly popular small business blog <a href="http://www.kalzumeus.com/" target="_blank">MicroISV on a Shoestring</a> which in fact was so popular that he even got interviewed for a book called <em>Blog Blazers</em> (Which I have a copy of, but still have never read).</p>
<p>In any case, he started out with the dream of making bingo cards for English Teachers in Japan, and got sucked in to the wonderful world of website optimizations and internet marketing. He&#8217;s surprisingly done very well for himself almost to the point that he makes about as much as I do at my day job by selling bingocards to school teachers.  Part of me is always glad that he chose to focus his diabolic marketing energy into something like bingocards instead of say, world domination, otherwise we&#8217;d all by singing hail to the Patrick.</p>
<p>So, back to the main point, I took his idea and decided to try my hand at some marketing / site-design and try to learn some new systems that I never knew before. The experience has completely changed the way that I view programming and design. Bingocard Creator is run off of RubyOnRails, the newest-and-greatest technology to ever hit the web (apparently), but as I am a dyed-in-the-wool PHP programmer I decided to try and find some sort of framework that could match the &#8220;it just works&#8221; methodology of RubyOnRails while not leaving my comfortable niche of PHP.</p>
<p>After checking out a lot of frameworks, I finally settled on <a href="http://cakephp.org" target="_blank">CakePHP</a> which is a framework for PHP based on RubyOnRails. It took a lot of getting used to, but a few months later, I feel that I am fully confident to actually use CakePHP for projects in the future, and hopefully erase some of the evil code that plagues my main site ( <a href="http://japanesetesting.com" target="_blank">Ippatsu @ JapaneseTesting.com</a> ) in future upgrades.</p>
<p>So, after a long and windy introduction, I lay out the purpose of this blog:</p>
<p>To share with anyone else who is thinking of making their own CMS-based site, the trials, tribulations and resources that helped me to get my site up and running.</p>
<p>Essentially I&#8217;ll be sharing programming tricks that I&#8217;ve found, marketing ideas, design resources, etc. Pretty much anything that I think that&#8217;s useful to people trying to make their own site.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.japanesetesting.com/2009/08/19/hello-world/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
