<?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:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Web Technology: A Revolution</title>
	<atom:link href="http://webtechnology.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://webtechnology.wordpress.com</link>
	<description>Developed by Aseef Ahmed</description>
	<pubDate>Thu, 03 Jul 2008 06:57:06 +0000</pubDate>
	<generator>http://wordpress.org/?v=MU</generator>
	<language>en</language>
			<item>
		<title>Managing Software with yum</title>
		<link>http://webtechnology.wordpress.com/2008/07/03/managing-software-with-yum/</link>
		<comments>http://webtechnology.wordpress.com/2008/07/03/managing-software-with-yum/#comments</comments>
		<pubDate>Thu, 03 Jul 2008 06:57:06 +0000</pubDate>
		<dc:creator>aseefahmed</dc:creator>
		
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://webtechnology.wordpress.com/?p=45</guid>
		<description><![CDATA[About Packages
Each package         is a compressed archive containing product information, program         files, icons, documentation and management scripts. Management         applications use these files to safely locate, install, update     [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h3 class="title">About Packages</h3>
<p>Each package         is a compressed archive containing product information, program         files, icons, documentation and management scripts. Management         applications use these files to safely locate, install, update         and remove software.</p>
<p>Packages also include a digital signature to prove their source.         Software management utilities verify this digital signature by         using a GPG <em>public key</em>. The         <code>yum</code> and <code>rpm</code> utilities         share a common <em>keyring</em> that stores all of         the public keys for approved package sources. The system         administrator configures these approved package sources.</p>
<h3 class="title">About Repositories</h3>
<p>A <em>repository</em> is a prepared directory or         web site that contains software packages and index files.         Software management utilities such as <code>yum</code> automatically locate and obtain the correct RPM packages from         these repositories. This method frees you from having to         manually find and install new applications or updates. You may         use a single command to update all system software, or search         for new software by specifying criteria.</p>
<h3 class="title">About Dependencies</h3>
<p>Some of the files installed on a distribution are         <em>libraries</em> which may provide functions to         multiple applications. When an application requires a specific         library, the package which contains that library is a         <em>dependency</em>. To properly install a         package. The         dependency information for a RPM package is stored within the         RPM file.</p>
<p>The <code>yum</code> utility uses package dependency data         to ensure that all of requirements for an application are met         during installation. It automatically installs the packages for         any dependencies not already present on your system. If a new         application has requirements that conflict with existing         software, <code>yum</code> aborts without making any         changes to your system.</p>
<h3 class="title">Understanding Package Names</h3>
<p>Each package file has a long name that indicates several key         pieces of information. This is the full name of the         <code>tsclient</code> package supplied with CentOS:</p>
<pre><code>tsclient-0.132-4.i386.rpm</code></pre>
<p>Management utilities commonly refer to packages with one of         three formats:</p>
<div class="itemizedlist">
<ul type="disc">
<li>Package name: <code>tsclient</code></li>
<li>Package name with version and release numbers:             <code>tsclient-0.132-4</code></li>
<li>Package name with hardware architecture:             <code>tsclient.i386</code></li>
</ul>
</div>
<p>For clarity, <code>yum</code> lists packages in the format         <code>name.architecture</code>. Repositories also         commonly store packages in separate directories by architecture.         In each case, the hardware architecture specified for the         package is the <span class="emphasis"><em>minimum</em></span> type of machine         required to use the package.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/webtechnology.wordpress.com/45/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/webtechnology.wordpress.com/45/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webtechnology.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webtechnology.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webtechnology.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webtechnology.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webtechnology.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webtechnology.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webtechnology.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webtechnology.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webtechnology.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webtechnology.wordpress.com/45/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&blog=2417486&post=45&subd=webtechnology&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://webtechnology.wordpress.com/2008/07/03/managing-software-with-yum/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/aseefahmed-128.jpg" medium="image">
			<media:title type="html">aseefahmed</media:title>
		</media:content>
	</item>
		<item>
		<title>Create Your Own Mail Script With PHP and IMAP</title>
		<link>http://webtechnology.wordpress.com/2008/05/26/create-your-own-mail-script-with-php-and-imap/</link>
		<comments>http://webtechnology.wordpress.com/2008/05/26/create-your-own-mail-script-with-php-and-imap/#comments</comments>
		<pubDate>Mon, 26 May 2008 08:31:14 +0000</pubDate>
		<dc:creator>aseefahmed</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://webtechnology.wordpress.com/?p=44</guid>
		<description><![CDATA[Thanks to PHP, we can check our email account remotely using PHP and its imap_xxx functions, which allow us to communicate with mail servers via IMAP, POP3 or NNTP protocols. In this article Mitchell shows us how to create a completely web-based email checking script which can also delete, send and reply to emails&#8230; all [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><span class="txtplain1">Thanks to PHP, we can check our email account remotely using PHP and its imap_xxx functions, which allow us to communicate with mail servers via IMAP, POP3 or NNTP protocols. In this article Mitchell shows us how to create a completely web-based email checking script which can also delete, send and reply to emails&#8230; all using only PHP&#8217;s IMAP functions.How many times have you been away from your home PC and wished that you could check your IMAP/POP3 mail account? If you&#8217;re anything like me then you&#8217;re always on the move and your laptop is your best friend.</span></p>
<p>Thanks to PHP, we can check our email account remotely using PHP and its imap_xxx functions, which allow us to communicate with mail servers via IMAP (Internet Message Access Protocol), POP3 (Post Office Protocol) and/or NNTP (Network News Transfer Protocol).</p>
<p>In this article we&#8217;re going to build a mail script that will accept an email account, password and optional IMAP port number. The script will then login to that mail account and display the users inbox in a similar fashion to the way Hotmail does: all using only PHP and HTML. From the inbox screen, we have the option to compose a new email, view our emails and even reply to them.</p>
<p>To test the source code and examples in this article you should be running either a Windows/Linux server with IIS/Apache and PHP 4+ installed. You should also have the IMAP libraries installed, which we will look at shortly.</p>
<p><span class="txtplain1"><span class="contentpagetitle"><strong>Create Your Own Mail Script With PHP and IMAP - What is IMAP?</strong></span><br />
(Page 2 of 6 )</span></p>
<p>IMAP is a method of accessing email messages stored on a remote mail server. The reason IMAP is so popular is because it allows clients to connect to and interact with their mail server without the need to download their emails and files locally. In other words, IMAP allows us to treat a remote mail server as is if it was on our local machine, checking our accounts, manipulating emails, replying to emails, etc.</p>
<p>IMAP shouldn&#8217;t be confused with the POP3 protocol, however. If you&#8217;ve ever used Outlook or Eudora or any other client mail access program, then you&#8217;ll know that you can &#8220;check&#8221; your email, disconnect from the Internet and your mail will be stored on your local machine, saving you the hassle of being on the &#8216;net to read your email. This is POP&#8217;s biggest advantage: offline access to your email. POP downloads all new messages off the server onto your local PC and then deletes them from that server. IMAP on the other hand queries the server, leaving all emails intact unless you specifically tell it to delete them.</p>
<p>POP was designed for use with client side email applications such as Outlook, whereas IMAP was designed for people on the move, wanting to check their email but not necessarily retrieve it.</p>
<p>The key goals for IMAP are:</p>
<ul>
<li>Be fully compatible with Internet messaging standards, e.g. MIME.</li>
<li>Allow message access and management from more than one computer.</li>
<li>Allow access without reliance on less efficient file access protocols.</li>
<li>Provide support for online, offline and disconnected access modes.</li>
<li>Support for concurrent access to shared mailboxes.</li>
<li>Client software needs no knowledge about the server&#8217;s file store format.</li>
</ul>
<p>Let&#8217;s take the last point into configuration: &#8220;Client software needs no knowledge about the server&#8217;s file store format&#8221;. In my opinion this is one of IMAP&#8217;s best features. The client simply queries the mail server using IMAP specific commands and that server returns the data the client&#8217;s after.</p>
<p>It doesn&#8217;t matter how the server stores the actual email messages (whether it be in a flat file, a MySQL database, an MSSQL server database or any other format) as long as the server returns the data that the client requests, then the storage method and location are irrelevant.</p>
<p><strong>Bringing PHP into the picture</strong></p>
<p>If you&#8217;ve worked with PHP extensions then you&#8217;ll know that it supports IMAP through an extension library, which isn&#8217;t installed by default. Before we can play around with PHP and IMAP, we have to install the necessary IMAP libraries first.</p>
<p><strong>Configuring PHP on Windows with IMAP support</strong></p>
<p>It&#8217;s easy to configure your Windows server to work with IMAP. Firstly, grab the php_imap.dll file from your PHP download (it should be in the extensions directory). If you can&#8217;t find it then download the PHP zip package <a rel="nofollow" href="http://www.php.net/downloads.php" target="_blank">here</a>.</p>
<p>Next, copy php_imap.dll to your PHP extension directory, which you can find in your php.ini file as the extension_dir variable. Lastly, open php.ini and look for the line starting &#8220;;extension=php_imap.dll&#8221;. Remove the semicolon from the front, save the file and restart your web server.</p>
<p><strong>Configuring PHP on Linux with IMAP support</strong></p>
<p>To enable IMAP support on your PHP installation you need to compile PHP with the &#8211;with-imap argument. You also need to download the IMAP library <a rel="nofollow" href="ftp://ftp.cac.washington.edu/imap/" target="_blank">here</a>. Once you&#8217;ve downloaded it, copy c-client/c-client.a to /usr/local/lib/libc-client.a. Also copy c-client/rfc822.h, mail.h and linkage.h to /usr/local/include or some other directory in your include path.</p>
<p><strong>Testing for IMAP support</strong></p>
<p>To make sure IMAP support is configured correctly, create a new file called phpinfo.php and save it into a directory that your web server can process. Add the following code to it and run it in your web browser:</p>
<p><span class="bodyCode">&lt;?php echo phpinfo(); ?&gt;</span></p>
<p>Scroll down the page about half way and look for the IMAP section. You should see something like this:</p>
<p><img src="http://www.devarticles.com/images/phpimap_1.gif" alt="Making sure IMAP was configured OK" align="middle" /></p>
<p>Now that we&#8217;ve setup IMAP support, it&#8217;s time to take a look at the various IMAP functions that we will be using later in this article to create our email script.</p>
<p><span class="txtplain1"><span class="contentpagetitle"><strong>Create Your Own Mail Script With PHP and IMAP - IMAP functions</strong></span><br />
(Page 3 of 6 )</span></p>
<p>The IMAP library has 61 functions. Luckily we only need to use a handful of these to create a fully functional email script. The functions we will be using in this article are shown and detailed below:</p>
<ul>
<li><strong>imap_open:</strong> Opens an IMAP stream to a mailbox.</li>
<li><strong>imap_headers:</strong> Returns headers for all emails in a mailbox.</li>
<li><strong>imap_headerinfo:</strong> Reads the header of an email.</li>
<li><strong>imap_delete:</strong> Mark an email for deletion from current mailbox.</li>
<li><strong>imap_expunge:</strong> Delete all emails marked for deletion.</li>
<li><strong>imap_mail:</strong> Sends an email message to a recipient.</li>
<li><strong>imap_body:</strong> Reads the body of an email from a mailbox.</li>
</ul>
<p>As mentioned earlier, we&#8217;re going to create a script that allows us to check our email account on the move using IMAP. Before we create the script however, let&#8217;s start with the basics. Create a new file called testimap.php and enter the following code into it:</p>
<p><span class="bodyCode">&lt;?php</span></p>
<p>$server = &#8220;mail.com&#8221;;</p>
<p>$user = &#8220;john&#8221;;</p>
<p>$pass = &#8220;mypass&#8221;;</p>
<p>$conn = @imap_open(&#8221;\{$server/imap}INBOX&#8221;, $user, $pass)</p>
<p>or die(&#8221;Connection to server failed&#8221;);</p>
<p>?&gt;</p>
<p>Replace the $server variable with the host name of your mail server. Replace $user with your mailbox account name and also replace $pass with the password for your mailbox. To connect to our mail server we use the imap_open function, passing in our mailbox, user ID and password. The format of the mailbox parameter looks like this:</p>
<p>{host_name[:port number]/imap}INBOX</p>
<p>The port number part of the mailbox parameter is optional. If we refer back to the testimap.php file above, our mailbox parameter looks like this:</p>
<p>{mail.com/imap}MAILBOX</p>
<p>In our example we&#8217;ve left out the port number. We could&#8217;ve optionally specified the port number like this:</p>
<p>{mail.com:25/imap}INBOX</p>
<p>The imap_open function returns an IMAP stream on success and FALSE on failure. We use this returned IMAP stream with various other imap_xxx function calls to manipulate our mailbox, send emails, etc.</p>
<p>To retrieve a list of emails in our mailbox, we can use the following code:</p>
<p><span class="bodyCode">$headers = @imap_headers($conn)</span></p>
<p>or die(&#8221;Couldn&#8217;t get emails&#8221;);</p>
<p>$numEmails = sizeof($headers);</p>
<p>echo &#8220;You have $numEmails in your mailbox&#8221;;</p>
<p>In the example above I&#8217;ve called the imap_headers function, passing in our IMAP stream as the only parameter. The imap_headers function returns an array of headers. Each of these headers contains the details of one email in our mailbox, and we can use the values of these headers to show who these emails have come from, their subjects, date received, etc. These headers do not however contain the body of each email message, and we must use the imap_body function to get them.</p>
<p>Now that we&#8217;ve got an array of headers representing each email in our mailbox, we can loop through them to display the details of each message with the imap_headerinfo function, like this:</p>
<p><span class="bodyCode">for($i = 1; $i &lt; $numEmails+1; $i++)</span></p>
<p>{</p>
<p>$mailHeader = @imap_headerinfo($conn, $i);</p>
<p>$from = $mailHeader-&gt;fromaddress;</p>
<p>$subject = strip_tags($mailHeader-&gt;subject);</p>
<p>$date = $mailHeader-&gt;date;</p>
<p>echo &#8220;Email from $from, subject $subject, date $date&lt;br&gt;&#8221;;</p>
<p>}</p>
<p>As you can see from the code above, we use the imap_headerinfo function to get the header details for each message. We extract the from address, subject and date for each message and use the echo command to output them to the browser.</p>
<p>Now that we&#8217;ve displayed each message in our mailbox, we can use the imap_close function to close the IMAP stream we created earlier with imap_open:</p>
<p><span class="bodyCode">imap_close($conn);</span></p>
<p>On this page we&#8217;ve taken a look at the basic imap_xxx functions that are required to open an IMAP stream, retrieve headers and display the details of each email from these headers. On the next page we&#8217;re going to put everything we&#8217;ve learnt so far to good use and create the email script I&#8217;ve been mentioning.</p>
<p><span class="contentpagetitle"><strong>Create Your Own Mail Script With PHP and IMAP - Creating our email script</strong></span><br />
(Page 4 of 6 )</p>
<p>There are a huge number of email applications on the market, however most of them expose the same core functionality:</p>
<ul>
<li>You can check your mailbox on any mail server.</li>
<li>You can create and send emails.</li>
<li>You can reply to emails.</li>
<li>You can delete emails from your mailbox.</li>
</ul>
<p>The script we&#8217;re about to discuss implements each of the bullet points shown above. We will run through the code step by step, describing various function calls where necessary.</p>
<p>Our script contains several functions and a switch statement. The switch statement works with the $what variable, which can be either a query string or form posted variable. Each time our script is called, a different function is executed, depending on the value of $what:</p>
<p><span class="bodyCode">switch($what)</span></p>
<p>{</p>
<p>case &#8220;login&#8221;:</p>
<p>ProcessLogin();</p>
<p>break;</p>
<p>case &#8220;inbox&#8221;:</p>
<p>ShowInbox();</p>
<p>break;</p>
<p>case &#8220;delete&#8221;:</p>
<p>DeleteEmails();</p>
<p>break;</p>
<p>case &#8220;compose&#8221;:</p>
<p>ShowComposeForm();</p>
<p>break;</p>
<p>case &#8220;send&#8221;:</p>
<p>SendMessage($to, $subject, $message, $cc, $bcc);</p>
<p>break;</p>
<p>case &#8220;read&#8221;:</p>
<p>ShowEmail();</p>
<p>break;</p>
<p>case &#8220;reply&#8221;:</p>
<p>ShowComposeForm($to, $subject, $message);</p>
<p>break;</p>
<p>default:</p>
<p>ShowLoginForm();</p>
<p>}</p>
<p>As you can see, the default option calls the ShowLoginForm function, which allows us to enter our email address and password. The ShowLoginForm function simply displays a HTML form which includes a hidden form variable, $what, which is set to &#8220;login&#8221;.</p>
<p>The ProcessLogin function is called when $what is equal to &#8220;login&#8221;. It validates the email address from the login form using the ereg function and creates some session variables containing our mailbox, username, host and password:</p>
<p><span class="bodyCode">$validEmail = ereg(&#8221;^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$&#8221;, $email);</span></p>
<p>$validPass = ($password == &#8220;&#8221; ? false: true);</p>
<p>$portString = (is_numeric($port) == true ? &#8220;:$port&#8221; : &#8220;&#8221;);</p>
<p>if(!$validEmail || !$validPass)</p>
<p>{</p>
<p>// Email or password are bad</p>
<p>die(&#8221;&lt;h2&gt;Invalid Login Credentials&lt;/h2&gt;&#8221;);</p>
<p>}</p>
<p>else</p>
<p>{</p>
<p>// Email and password are fine, get the user and hosts</p>
<p>$arr = split(&#8221;@&#8221;, $email);</p>
<p>$user = $arr[0];</p>
<p>$host = $arr[1];</p>
<p>$mailbox = &#8220;\{$host$portString/imap}INBOX&#8221;;</p>
<p>// Set the login credentials as session variables</p>
<p>setcookie(&#8221;mailbox&#8221;, $mailbox);</p>
<p>setcookie(&#8221;user&#8221;, $user);</p>
<p>setcookie(&#8221;host&#8221;, $host);</p>
<p>setcookie(&#8221;password&#8221;, $password);</p>
<p>header(&#8221;Location: $PHP_SELF?what=inbox&#8221;);</p>
<p>}</p>
<p>As you can see, we use the split function to separate the users email address into the $user and $host variables. We also create the $mailbox variable from the host name and port number, which may or may not be specified in the login form. The user is then redirected to the same scripting using $PHP_SELF, passing &#8220;inbox&#8221; as the $what query string variable. This results in the ShowInbox function being called.</p>
<p>The ShowInbox function connects to our mail server using imap_open (which we saw in our first example) and retrieves the headers for each email message in our mailbox. These headers are then extracted into variables and a HTML table is created that lists the details of each email. Each email is also hyper linked to the script, setting the query variables $what to &#8220;read&#8221;, and $id to the ID of the email, which is the index of that particular email in the array returned from a call to imap_headers:</p>
<p><span class="bodyCode">&lt;td width=&#8221;50%&#8221;&gt;</span></p>
<p>&lt;a href=&#8221;&lt;?php echo $PHP_SELF; ?&gt;?what=read&amp;id=&lt;?php echo $i; ?&gt;&#8221;&gt;</p>
<p>&lt;?php echo $subject; ?&gt;</p>
<p>&lt;/a&gt;</p>
<p>&lt;/td&gt;</p>
<p>When $what is &#8220;read&#8221;, the ShowEmail function is called.</p>
<p><span class="txtplain1"><span class="contentpagetitle"><strong>Create Your Own Mail Script With PHP and IMAP - The ShowEmail function and more</strong></span><br />
(Page 5 of 6 )</p>
<p>ShowEmail connects to our IMAP server using imap_open and retrieves both the header and body details of one specific email. As we saw earlier, imap_headerinfo returns the header details of an email as an array, like this:</p>
<p><span class="bodyCode">$mailHeader = @imap_headerinfo($conn, $id);</p>
<p>&#8230;</p>
<p>$from = $mailHeader-&gt;fromaddress;</p>
<p>$subject = strip_tags($mailHeader-&gt;subject);</p>
<p>$date = $mailHeader-&gt;date;</span></p>
<p>The body of the email is then retrieved with a call to imap_body. Because our email script only displays plain text emails, we use strip_tags to remove all HTML formatting from the email:</p>
<p><span class="bodyCode">$body = nl2br(strip_tags(imap_body($conn, $id)));</span></p>
<p>We then output all of these details to a form, including a button that lets us reply to this specific email:</p>
<p><span class="bodyCode">&lt;form name=&#8221;frmLogin&#8221; action=&#8221;&lt;?php echo $PHP_SELF; ?&gt;&#8221; method=&#8221;post&#8221;&gt;</p>
<p>&lt;input type=&#8221;hidden&#8221; name=&#8221;what&#8221; value=&#8221;reply&#8221;&gt;</p>
<p>&lt;input type=&#8221;hidden&#8221; name=&#8221;to&#8221; value=&#8221;&lt;?php echo $from; ?&gt;&#8221;&gt;</p>
<p>&lt;input type=&#8221;hidden&#8221; name=&#8221;subject&#8221; value=&#8221;Re: &lt;?php echo $subject; ?&gt;&#8221;&gt;</p>
<p>&lt;input type=&#8221;hidden&#8221; name=&#8221;message&#8221; value=&#8221;&lt;?php echo str_replace(&#8221;&lt;br /&gt;&#8221;, &#8220;&#8221;, str_replace(&#8221;\r\n&#8221;, &#8220;\r\n&gt;&gt;&#8221;, $body)) ?&gt;&#8221;&gt;</p>
<p>&lt;h2&gt;&lt;?php echo $subject; ?&gt;&lt;/h2&gt;</p>
<p>&lt;hr noshade size=&#8221;1&#8243; color=&#8221;#20158F&#8221;&gt;</p>
<p>&lt;b&gt;From:&lt;/b&gt; &lt;?php echo str_replace(&#8221;&lt;&#8221;, &#8220;&amp;lt;&#8221;, $from); ?&gt;&lt;br&gt;</p>
<p>&lt;b&gt;Date:&lt;/b&gt; &lt;?php echo $date; ?&gt;&lt;br&gt;</p>
<p>&lt;hr noshade size=&#8221;1&#8243; color=&#8221;#20158F&#8221;&gt;</p>
<p>&lt;?php echo $body; ?&gt;</p>
<p>&lt;hr noshade size=&#8221;1&#8243; color=&#8221;#20158F&#8221;&gt;</p>
<p>&lt;input type=&#8221;submit&#8221; name=&#8221;submit&#8221; value=&#8221;Reply &gt;&gt;&#8221;&gt;</p>
<p>&lt;/form&gt;</span></p>
<p>As you can see, this form passes the value &#8220;reply&#8221; for the hidden form variable &#8220;what&#8221;. The to, subject and message fields are also passed as hidden form variables, which in turn calls the ShowComposeForm function, which displays a HTML form in exactly the same was as the ShowEmail function.</p>
<p>Although this might sound a little confusing, the ShowComposeForm function is also used to compose a new email. The parameters passed to the function are the values for each text box in the form: if we don&#8217;t pass any parameters then we&#8217;re creating a new form. If we do, then we&#8217;re replying to a message. Here&#8217;s the function signature of ShowComposeForm:</p>
<p><span class="bodyCode">function ShowComposeForm($to = &#8220;&#8221;, $subject = &#8220;&#8221;, $message = &#8220;&#8221;)</span></p>
<p>The ShowComposeForm function includes a hidden form variable, what, which is set to &#8220;send&#8221;, triggering a call to the SendMessage function when the form is submitted.</p>
<p>The function signature of SendMessage looks like this:</p>
<p><span class="bodyCode">SendMessage($to, $subject, $message, $cc, $bcc);</span></p>
<p>SendMessage uses the imap_open function to connect to our mail server. If the connection was successful, it then uses imap_mail to send an email message to the intended recipient, like this:</p>
<p><span class="bodyCode">imap_mail($to, $subject, $message, &#8220;From: $user@$host&#8221;, $cc, $bcc);</span></p>
<p>One last function that I will mention is our DeleteEmails function. This function calls imap_delete as well as imap_explunge to remove email messages from our mailbox based on their ID. The ID&#8217;s for the DeleteEmails function are passed as an array from the form generated by the ShowInbox function:</p>
<p><span class="bodyCode">for($i = 1; $i &lt; $numEmails+1; $i++)</p>
<p>&#8230;</p>
<p>&lt;td width=&#8221;5%&#8221;"&gt;</p>
<p>&lt;input type=&#8221;checkbox&#8221; name=&#8221;emails[]&#8221; value=&#8221;&lt;?php echo $i; ?&gt;&#8221;&gt;</p>
<p>&lt;/td&gt;</span></p>
<p>The imap_delete function accepts two parameters: an IMAP stream object and the ID of the email to delete:</p>
<p><span class="bodyCode">for($i = 0; $i &lt; sizeof($emails); $i++)</p>
<p>{</p>
<p>@imap_delete($conn, (int)$emails[$i]);</p>
<p>}</span></p>
<p>Once we&#8217;ve called imap_delete for each email, we must call the imap_expuge function to actually remove the messages from the server:</p>
<p><span class="bodyCode">@imap_expunge($conn);</span></span></p>
<p class="pagenavbar">
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/webtechnology.wordpress.com/44/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/webtechnology.wordpress.com/44/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webtechnology.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webtechnology.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webtechnology.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webtechnology.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webtechnology.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webtechnology.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webtechnology.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webtechnology.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webtechnology.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webtechnology.wordpress.com/44/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&blog=2417486&post=44&subd=webtechnology&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://webtechnology.wordpress.com/2008/05/26/create-your-own-mail-script-with-php-and-imap/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/aseefahmed-128.jpg" medium="image">
			<media:title type="html">aseefahmed</media:title>
		</media:content>

		<media:content url="http://www.devarticles.com/images/phpimap_1.gif" medium="image">
			<media:title type="html">Making sure IMAP was configured OK</media:title>
		</media:content>
	</item>
		<item>
		<title>Retrieving data in CakePHP</title>
		<link>http://webtechnology.wordpress.com/2008/04/23/retrieving-data-in-cakephp/</link>
		<comments>http://webtechnology.wordpress.com/2008/04/23/retrieving-data-in-cakephp/#comments</comments>
		<pubDate>Wed, 23 Apr 2008 06:51:24 +0000</pubDate>
		<dc:creator>aseefahmed</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://webtechnology.wordpress.com/?p=41</guid>
		<description><![CDATA[
A function to retrieve data in CakePHP  is the findAll() model function.
It has the following syntax:
PLAIN TEXT
PHP:




findAll($conditions, $fields, $order, $limit, $page, $recursive);


string $conditions;


array $fields;


string $order;


int $limit;


int $page;


int $recursive;





For example to retrive all the data from a table you will use following:
PLAIN TEXT
PHP:




$this-&#62;set($var, $this-&#62;Modelname-&#62;findAll());





With the “set function” the $var value will be send to the [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><div class="post-content">
<p>A function to retrieve data in CakePHP  is the findAll() model function.<br />
It has the following syntax:</p>
<div class="igBar"><span><a href="http://www.bogdan-net.com/archives/16#">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span></p>
<div id="php-1">
<div class="php">
<ol>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;">findAll<span style="color:#006600;font-weight:bold;">(</span><span style="color:#0000ff;">$conditions</span>, <span style="color:#0000ff;">$fields</span>, <span style="color:#0000ff;">$order</span>, <span style="color:#0000ff;">$limit</span>, <span style="color:#0000ff;">$page</span>, <span style="color:#0000ff;">$recursive</span><span style="color:#006600;font-weight:bold;">)</span>;</div>
</li>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;">string <span style="color:#0000ff;">$conditions</span>;</div>
</li>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;"><a href="http://www.php.net/array"><span style="color:#000066;">array</span></a> <span style="color:#0000ff;">$fields</span>;</div>
</li>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;">string <span style="color:#0000ff;">$order</span>;</div>
</li>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;">int <span style="color:#0000ff;">$limit</span>;</div>
</li>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;">int <span style="color:#0000ff;">$page</span>;</div>
</li>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;">int <span style="color:#0000ff;">$recursive</span>;</div>
</li>
</ol>
</div>
</div>
</div>
<p>For example to retrive all the data from a table you will use following:</p>
<div class="igBar"><span><a href="http://www.bogdan-net.com/archives/16#">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span></p>
<div id="php-2">
<div class="php">
<ol>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;"><span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">set</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#0000ff;">$var</span>, <span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">Modelname</span>-&gt;<span style="color:#006600;">findAll</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#006600;font-weight:bold;">)</span><span style="color:#006600;font-weight:bold;">)</span>;</div>
</li>
</ol>
</div>
</div>
</div>
<p>With the “set function” the $var value will be send to the view. In the case above all the fields of the table will be retrieve.</p>
<p>Examples using findAll()</p>
<p>To sort the data by a certain field we can use:</p>
<div class="igBar"><span><a href="http://www.bogdan-net.com/archives/16#">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span></p>
<div id="php-3">
<div class="php">
<ol>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;"><span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">set</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#0000ff;">$var</span>, <span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">Modelname</span>-&gt;<span style="color:#006600;">findAll</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#000000;font-weight:bold;">null</span>, <span style="color:#000000;font-weight:bold;">null</span>, ‘Modelname.field’<span style="color:#006600;font-weight:bold;">)</span><span style="color:#006600;font-weight:bold;">)</span>;</div>
</li>
</ol>
</div>
</div>
</div>
<p>For ascending:</p>
<div class="igBar"><span><a href="http://www.bogdan-net.com/archives/16#">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span></p>
<div id="php-4">
<div class="php">
<ol>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;"><span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">set</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#0000ff;">$var</span>, <span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">Modelname</span>-&gt;<span style="color:#006600;">findAll</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#000000;font-weight:bold;">null</span>, <span style="color:#000000;font-weight:bold;">null</span>, ‘Modelname.field ASC’<span style="color:#006600;font-weight:bold;">)</span><span style="color:#006600;font-weight:bold;">)</span>;</div>
</li>
</ol>
</div>
</div>
</div>
<p>Descending:</p>
<div class="igBar"><span><a href="http://www.bogdan-net.com/archives/16#">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span></p>
<div id="php-5">
<div class="php">
<ol>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;"><span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">set</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#0000ff;">$var</span>, <span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">Modelname</span>-&gt;<span style="color:#006600;">findAll</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#000000;font-weight:bold;">null</span>, <span style="color:#000000;font-weight:bold;">null</span>, ‘Modelname.field DESC’<span style="color:#006600;font-weight:bold;">)</span><span style="color:#006600;font-weight:bold;">)</span>;</div>
</li>
</ol>
</div>
</div>
</div>
<p>Only first 10 results:</p>
<div class="igBar"><span><a href="http://www.bogdan-net.com/archives/16#">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span></p>
<div id="php-6">
<div class="php">
<ol>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;"><span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">set</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#0000ff;">$var</span>, <span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">Modelname</span>-&gt;<span style="color:#006600;">findAll</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#000000;font-weight:bold;">null</span>, <span style="color:#000000;font-weight:bold;">null</span>, ‘Modelname.field DESC’, <span style="color:#800000;">10</span><span style="color:#006600;font-weight:bold;">)</span><span style="color:#006600;font-weight:bold;">)</span>;</div>
</li>
</ol>
</div>
</div>
</div>
<p>The second results page:</p>
<div class="igBar"><span><a href="http://www.bogdan-net.com/archives/16#">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span></p>
<div id="php-7">
<div class="php">
<ol>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;"><span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">set</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#0000ff;">$var</span>, <span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">Modelname</span>-&gt;<span style="color:#006600;">findAll</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#000000;font-weight:bold;">null</span>, <span style="color:#000000;font-weight:bold;">null</span>, ‘Modelname.field DESC’, <span style="color:#800000;">10</span>, <span style="color:#800000;">2</span><span style="color:#006600;font-weight:bold;">)</span><span style="color:#006600;font-weight:bold;">)</span>;</div>
</li>
</ol>
</div>
</div>
</div>
<p>Putting some conditions in the query:</p>
<div class="igBar"><span><a href="http://www.bogdan-net.com/archives/16#">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span></p>
<div id="php-8">
<div class="php">
<ol>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;"><span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">set</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#0000ff;">$var</span>, <span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">Modelname</span>-&gt;<span style="color:#006600;">findAll</span><span style="color:#006600;font-weight:bold;">(</span>‘Modelname.id&gt; <span style="color:#800000;">1</span>’, <span style="color:#000000;font-weight:bold;">null</span>, ‘Modelname.field DESC’, <span style="color:#800000;">10</span> <span style="color:#006600;font-weight:bold;">)</span><span style="color:#006600;font-weight:bold;">)</span>;</div>
</li>
</ol>
</div>
</div>
</div>
</div>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/webtechnology.wordpress.com/41/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/webtechnology.wordpress.com/41/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webtechnology.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webtechnology.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webtechnology.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webtechnology.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webtechnology.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webtechnology.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webtechnology.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webtechnology.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webtechnology.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webtechnology.wordpress.com/41/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&blog=2417486&post=41&subd=webtechnology&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://webtechnology.wordpress.com/2008/04/23/retrieving-data-in-cakephp/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/aseefahmed-128.jpg" medium="image">
			<media:title type="html">aseefahmed</media:title>
		</media:content>
	</item>
		<item>
		<title>Transaction behavior</title>
		<link>http://webtechnology.wordpress.com/2008/04/08/transaction-behavior/</link>
		<comments>http://webtechnology.wordpress.com/2008/04/08/transaction-behavior/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 10:44:59 +0000</pubDate>
		<dc:creator>aseefahmed</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://webtechnology.wordpress.com/?p=40</guid>
		<description><![CDATA[Sample model - note the $actsAs
To change default settings do:
Download code
var $actsAs = array(&#8217;transaction&#8217; =&#62; array(&#8217;keyName&#8217; =&#62; &#8216;value&#8217;) );
 
Model Class:
Download code &#60;?php
class Order extends AppModel
{
var $name = &#8216;Order&#8217;;
var $actsAs = array(&#8216;transaction&#8217;);
var $hasMany = array(&#8216;OrderDetail&#8217;);
?&#62;  
Controller Class:
Download code &#60;?php
class OrderController extends AppController {
var $name = &#8216;Order&#8217;;
var $uses = array(&#8216;Order&#8217;);
function checkout() {
if(!empty($this-&#62;data)) {
$this-&#62;Order-&#62;begin(); // Start [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Sample model - note the $actsAs</p>
<p>To change default settings do:<br />
<a href="http://bakery.cakephp.org/articles/download_code/228/block/4">Download code</a><code><span style="color:#000000;"><br />
var $actsAs = array(&#8217;transaction&#8217; =&gt; array(&#8217;keyName&#8217; =&gt; &#8216;value&#8217;) );<br />
</span> </code></p>
<h4><strong>Model Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/228/block/1">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">Order </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">AppModel<br />
</span><span style="color:#007700;">{<br />
var </span><span style="color:#0000bb;">$name </span><span style="color:#007700;">= </span><span style="color:#dd0000;">&#8216;Order&#8217;</span><span style="color:#007700;">;<br />
var </span><span style="color:#0000bb;">$actsAs </span><span style="color:#007700;">= array(</span><span style="color:#dd0000;">&#8216;transaction&#8217;</span><span style="color:#007700;">);<br />
var </span><span style="color:#0000bb;">$hasMany </span><span style="color:#007700;">= array(</span><span style="color:#dd0000;">&#8216;OrderDetail&#8217;</span><span style="color:#007700;">);<br />
</span><span style="color:#0000bb;">?&gt;</span> </span> </code></p>
<h4><strong>Controller Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/228/block/3">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">OrderController </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">AppController </span><span style="color:#007700;">{<br />
var </span><span style="color:#0000bb;">$name </span><span style="color:#007700;">= </span><span style="color:#dd0000;">&#8216;Order&#8217;</span><span style="color:#007700;">;<br />
var </span><span style="color:#0000bb;">$uses </span><span style="color:#007700;">= array(</span><span style="color:#dd0000;">&#8216;Order&#8217;</span><span style="color:#007700;">);</span></span></code></p>
<p>function <span style="color:#0000bb;">checkout</span><span style="color:#007700;">() {<br />
if(!empty(</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">data</span><span style="color:#007700;">)) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">Order</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">begin</span><span style="color:#007700;">(); </span><span style="color:#ff8000;">// Start our transaction<br />
</span><span style="color:#007700;">if( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">Order</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">save</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">data </span><span style="color:#007700;">) ) {<br />
if( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">Order</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">OrderDetail</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">save</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">data </span><span style="color:#007700;">) ) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">Order</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">commit</span><span style="color:#007700;">(); </span><span style="color:#ff8000;">// Persist the data<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">redirect</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;/shop/order/thanks&#8217;</span><span style="color:#007700;">);<br />
exit;<br />
} else {<br />
</span><span style="color:#ff8000;">// Couldnt save the products to the order<br />
// since the products to the order couldnt be stored, dont store anything at all<br />
// rollback all changes to the database sine &#8216;begin&#8217; was called<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">Order</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">rollback</span><span style="color:#007700;">();<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">redirect</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;/shop/order/error&#8217;</span><span style="color:#007700;">);<br />
exit;<br />
}<br />
} else {<br />
</span><span style="color:#ff8000;">// Didnt save<br />
</span><span style="color:#007700;">}<br />
}<br />
}<br />
}<br />
</span><span style="color:#0000bb;">?&gt;</span></p>
<h4><strong>Model Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/228/block/2">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#ff8000;">/**<br />
* Transaction support for models in 1.2<br />
*<br />
* @version 0.2<br />
* @author Christian &#8216;Jippi&#8217; Winther<br />
*/<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">TransactionBehavior </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">ModelBehavior </span><span style="color:#007700;">{<br />
</span><span style="color:#ff8000;">/**<br />
* Settings: (false will disable them)<br />
*<br />
* - autocommit_on_start [ false | 0 | 1 ]<br />
*      Disable auto-commit on start?<br />
*<br />
* - autocommit_after_commit [ false | 0 | 1 ]<br />
*      Set auto-commit after successfull commit<br />
*<br />
* - autocommit_after_rollback [ false | 0 | 1 ]<br />
*      Set auto-commit after successfull rollback<br />
*<br />
* - throw_error [ false | true ]<br />
*      Should we call user_error if something is wrong?<br />
*<br />
* - error_level [ E_USER_* constants ]<br />
*      What level should the user_error use?<br />
*<br />
* - configure_key [ string ]<br />
*       What key should the transaction state be stored as<br />
*<br />
* @version 0.1<br />
* @since   0.1<br />
* @access  private<br />
* @var     array<br />
*/<br />
</span><span style="color:#007700;">var </span><span style="color:#0000bb;">$settings </span><span style="color:#007700;">= array(<br />
</span><span style="color:#dd0000;">&#8216;autocommit_on_start&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">false</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">&#8216;autocommit_after_commit&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">1</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">&#8216;autocommit_after_rollback&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">1</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">&#8216;throw_error&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">false</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">&#8216;error_level&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">E_USER_NOTICE</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">&#8216;configure_key&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">&#8216;inTransaction&#8217;<br />
</span><span style="color:#007700;">);</span></span></code></p>
<p><span style="color:#ff8000;">/**<br />
* List of legal &#8216;locks&#8217;<br />
*<br />
* @version 0.1<br />
* @since   0.1<br />
* @access  private<br />
* @var     array<br />
*/<br />
</span><span style="color:#007700;">var </span><span style="color:#0000bb;">$validLocks </span><span style="color:#007700;">= array(</span><span style="color:#dd0000;">&#8216;read&#8217;</span><span style="color:#007700;">,</span><span style="color:#dd0000;">&#8216;write&#8217;</span><span style="color:#007700;">);</span></p>
<p><span style="color:#ff8000;">/**<br />
* Startup hook from the model<br />
*<br />
* @version 0.1<br />
* @since   0.1<br />
* @access  public<br />
* @param   AppModel $model<br />
* @param   array $config<br />
*/<br />
</span><span style="color:#007700;">function </span><span style="color:#0000bb;">setup</span><span style="color:#007700;">(&amp;</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$config </span><span style="color:#007700;">= array()) {<br />
</span><span style="color:#ff8000;">/**<br />
* Check if there has been set a transaction state in another model<br />
* Configure::read returns null if no value is found, so its safe to<br />
* set the default value for our transaction state ( false )<br />
*/<br />
</span><span style="color:#007700;">if( </span><span style="color:#0000bb;">is_null</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_getTransactionState</span><span style="color:#007700;">() ) ) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_setTransactionState</span><span style="color:#007700;">( </span><span style="color:#0000bb;">false </span><span style="color:#007700;">);<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* If there was given any config settings to this behavior, merge them<br />
* with our default settings array<br />
*/<br />
</span><span style="color:#007700;">if( !</span><span style="color:#0000bb;">is_null</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$config </span><span style="color:#007700;">) &amp;&amp; </span><span style="color:#0000bb;">is_array</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$config </span><span style="color:#007700;">) ) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">settings </span><span style="color:#007700;">= </span><span style="color:#0000bb;">array_merge</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">settings</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$config </span><span style="color:#007700;">);<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* If we wish to start an transaction at load time do so<br />
*/<br />
</span><span style="color:#007700;">if( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">settings</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'autocommit_on_start'</span><span style="color:#007700;">] !== </span><span style="color:#0000bb;">false </span><span style="color:#007700;">) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">begin</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$model </span><span style="color:#007700;">);<br />
}<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* Begin a database transaction<br />
*<br />
* @version 0.1<br />
* @since   0.1<br />
* @access  public<br />
* @param   AppModel $model<br />
* @param   int $autocmmit<br />
* @return  boolean<br />
*/<br />
</span><span style="color:#007700;">function </span><span style="color:#0000bb;">begin</span><span style="color:#007700;">( &amp;</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$args </span><span style="color:#007700;">= array()) {<br />
if( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_getTransactionState</span><span style="color:#007700;">() === </span><span style="color:#0000bb;">false </span><span style="color:#007700;">) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_setTransactionState</span><span style="color:#007700;">( </span><span style="color:#0000bb;">true </span><span style="color:#007700;">);<br />
</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">query</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;SET autocommit=0&#8242;</span><span style="color:#007700;">);<br />
</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">query</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;begin&#8217;</span><span style="color:#007700;">);<br />
return </span><span style="color:#0000bb;">true</span><span style="color:#007700;">;<br />
}<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_error</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;Transaction has already started&#8217;</span><span style="color:#007700;">);<br />
return </span><span style="color:#0000bb;">false</span><span style="color:#007700;">;<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* Commits a database transaction<br />
*<br />
* @version 0.1<br />
* @since   0.1<br />
* @access  public<br />
* @param   AppModel $model<br />
* @param   integer $autocommit<br />
* @return  boolean<br />
*/<br />
</span><span style="color:#007700;">function </span><span style="color:#0000bb;">commit</span><span style="color:#007700;">( &amp;</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$args </span><span style="color:#007700;">= array() ) {<br />
if( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_getTransactionState</span><span style="color:#007700;">() === </span><span style="color:#0000bb;">true </span><span style="color:#007700;">) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_setTransactionState</span><span style="color:#007700;">( </span><span style="color:#0000bb;">false </span><span style="color:#007700;">);</span></p>
<p><span style="color:#0000bb;">$autoCommit </span><span style="color:#007700;">= empty(</span><span style="color:#0000bb;">$args</span><span style="color:#007700;">[</span><span style="color:#0000bb;">0</span><span style="color:#007700;">]) ? </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">settings</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'autocommit_after_commit'</span><span style="color:#007700;">] : </span><span style="color:#0000bb;">$args</span><span style="color:#007700;">[</span><span style="color:#0000bb;">0</span><span style="color:#007700;">];<br />
</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">query</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;commit&#8217;</span><span style="color:#007700;">);<br />
if( </span><span style="color:#0000bb;">$autoCommit </span><span style="color:#007700;">!== </span><span style="color:#0000bb;">false </span><span style="color:#007700;">) {<br />
</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">query</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;SET autocommit=&#8217;</span><span style="color:#007700;">. </span><span style="color:#0000bb;">$autoCommit</span><span style="color:#007700;">);<br />
}<br />
return </span><span style="color:#0000bb;">true</span><span style="color:#007700;">;<br />
}<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_error</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;No transaction active&#8217;</span><span style="color:#007700;">);<br />
return </span><span style="color:#0000bb;">false</span><span style="color:#007700;">;<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* Rollback a transaction<br />
*<br />
* @version 0.1<br />
* @since   0.1<br />
* @access  public<br />
* @param   AppModel $model<br />
* @param   integer $autocommit<br />
*/<br />
</span><span style="color:#007700;">function </span><span style="color:#0000bb;">rollback</span><span style="color:#007700;">( &amp;</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$args </span><span style="color:#007700;">) {<br />
if( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_getTransactionState </span><span style="color:#007700;">=== </span><span style="color:#0000bb;">true </span><span style="color:#007700;">) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_setTransactionState</span><span style="color:#007700;">( </span><span style="color:#0000bb;">false </span><span style="color:#007700;">);<br />
</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">query</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;rollback&#8217;</span><span style="color:#007700;">);<br />
</span><span style="color:#0000bb;">$autoCommit </span><span style="color:#007700;">= empty(</span><span style="color:#0000bb;">$args</span><span style="color:#007700;">[</span><span style="color:#0000bb;">0</span><span style="color:#007700;">]) ? </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">settings</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'autocommit_after_rollback'</span><span style="color:#007700;">] : </span><span style="color:#0000bb;">$args</span><span style="color:#007700;">[</span><span style="color:#0000bb;">0</span><span style="color:#007700;">];<br />
if( </span><span style="color:#0000bb;">$autoCommit </span><span style="color:#007700;">!== </span><span style="color:#0000bb;">false </span><span style="color:#007700;">) {<br />
</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">query</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;SET autocommit=&#8217;</span><span style="color:#007700;">. </span><span style="color:#0000bb;">$autoCommit</span><span style="color:#007700;">);<br />
}<br />
return </span><span style="color:#0000bb;">true</span><span style="color:#007700;">;<br />
}<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_error</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;No transaction active&#8217;</span><span style="color:#007700;">);<br />
return </span><span style="color:#0000bb;">false</span><span style="color:#007700;">;<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* Locks the model table for either READ or WRITE<br />
*<br />
* - Arg[0] is either &#8216;READ&#8217; or &#8216;WRITE&#8217;<br />
* - Arg[1]:<br />
*     If null     : Its going to lock the model table<br />
*     If string   : Locking the given table<br />
*     If array    : Going to lock all tables specified<br />
*                   NOTE: You have to specify model table aswell!<br />
*<br />
* @version 0.2<br />
* @since   0.1<br />
* @access  public<br />
* @param   AppModel $model<br />
* @param   array $args<br />
*/<br />
</span><span style="color:#007700;">function </span><span style="color:#0000bb;">lockTable</span><span style="color:#007700;">( &amp;</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$args </span><span style="color:#007700;">= array( ) ) {<br />
</span><span style="color:#0000bb;">$type </span><span style="color:#007700;">= </span><span style="color:#0000bb;">strtolower</span><span style="color:#007700;">( empty( </span><span style="color:#0000bb;">$args</span><span style="color:#007700;">[</span><span style="color:#0000bb;">0</span><span style="color:#007700;">] ) ? </span><span style="color:#0000bb;">null </span><span style="color:#007700;">: </span><span style="color:#0000bb;">$args</span><span style="color:#007700;">[</span><span style="color:#0000bb;">0</span><span style="color:#007700;">] );<br />
</span><span style="color:#0000bb;">$table </span><span style="color:#007700;">= </span><span style="color:#0000bb;">strtolower</span><span style="color:#007700;">( empty( </span><span style="color:#0000bb;">$args</span><span style="color:#007700;">[</span><span style="color:#0000bb;">1</span><span style="color:#007700;">] ) ? </span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">table </span><span style="color:#007700;">: </span><span style="color:#0000bb;">$args</span><span style="color:#007700;">[</span><span style="color:#0000bb;">1</span><span style="color:#007700;">] );<br />
if( </span><span style="color:#0000bb;">is_null</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$type </span><span style="color:#007700;">) ) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_error</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;Missing parameter for lockTable(), either READ or WRITE&#8217;</span><span style="color:#007700;">);<br />
return </span><span style="color:#0000bb;">false</span><span style="color:#007700;">;<br />
}<br />
if( </span><span style="color:#0000bb;">array_search</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$type</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">validLocks </span><span style="color:#007700;">) !== </span><span style="color:#0000bb;">false </span><span style="color:#007700;">) {<br />
if(</span><span style="color:#0000bb;">is_array</span><span style="color:#007700;">(</span><span style="color:#0000bb;">$table</span><span style="color:#007700;">)) {<br />
</span><span style="color:#0000bb;">$res </span><span style="color:#007700;">= array();<br />
foreach ( </span><span style="color:#0000bb;">$table </span><span style="color:#007700;">AS </span><span style="color:#0000bb;">$tableName </span><span style="color:#007700;">) {<br />
</span><span style="color:#0000bb;">$res</span><span style="color:#007700;">[ </span><span style="color:#0000bb;">$tableName </span><span style="color:#007700;">] = </span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">query</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;LOCK TABLE &#8216;</span><span style="color:#007700;">.</span><span style="color:#0000bb;">$table</span><span style="color:#007700;">.</span><span style="color:#dd0000;">&#8216; &#8216;</span><span style="color:#007700;">. </span><span style="color:#0000bb;">strtoupper</span><span style="color:#007700;">(</span><span style="color:#0000bb;">$type</span><span style="color:#007700;">));<br />
}<br />
return </span><span style="color:#0000bb;">$res</span><span style="color:#007700;">;<br />
} else {<br />
</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">query</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;LOCK TABLE &#8216;</span><span style="color:#007700;">.</span><span style="color:#0000bb;">$table</span><span style="color:#007700;">.</span><span style="color:#dd0000;">&#8216; &#8216;</span><span style="color:#007700;">. </span><span style="color:#0000bb;">strtoupper</span><span style="color:#007700;">(</span><span style="color:#0000bb;">$type</span><span style="color:#007700;">));<br />
return </span><span style="color:#0000bb;">true</span><span style="color:#007700;">;<br />
}<br />
}<br />
return </span><span style="color:#0000bb;">false</span><span style="color:#007700;">;<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* Remove all locks made on tables<br />
*<br />
* @version 0.1<br />
* @since   0.1<br />
* @access  public<br />
* @param   AppModel $model<br />
* @param   array $args<br />
*/<br />
</span><span style="color:#007700;">function </span><span style="color:#0000bb;">unlockTable</span><span style="color:#007700;">( &amp;</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$args </span><span style="color:#007700;">= array() ) {<br />
</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">query</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;UNLOCK TABLES&#8217;</span><span style="color:#007700;">);<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* Show an error to screen (When debug &gt; 0 )<br />
*<br />
* @version 0.1<br />
* @since 0.1<br />
* @access private<br />
* @param string $string<br />
*/<br />
</span><span style="color:#007700;">function </span><span style="color:#0000bb;">_error</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$string </span><span style="color:#007700;">) {<br />
if( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">settings</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'throw_error'</span><span style="color:#007700;">] ) {<br />
</span><span style="color:#0000bb;">trigger_error</span><span style="color:#007700;">(</span><span style="color:#0000bb;">$string</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">settings</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'error_level'</span><span style="color:#007700;">] );<br />
}<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* Get the current transaction state<br />
*<br />
* @version 0.1<br />
* @since 0.1<br />
* @access private<br />
* @return boolean<br />
*/<br />
</span><span style="color:#007700;">function </span><span style="color:#0000bb;">_getTransactionState</span><span style="color:#007700;">() {<br />
return </span><span style="color:#0000bb;">Configure</span><span style="color:#007700;">::</span><span style="color:#0000bb;">read</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">settings</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'configure_key'</span><span style="color:#007700;">] );<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* Write a new transaction state<br />
*<br />
* @version 0.1<br />
* @since 0.1<br />
* @access private<br />
* @param boolean $value<br />
*/<br />
</span><span style="color:#007700;">function </span><span style="color:#0000bb;">_setTransactionState</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$value </span><span style="color:#007700;">) {<br />
if( </span><span style="color:#0000bb;">is_bool</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$value </span><span style="color:#007700;">) ) {<br />
</span><span style="color:#0000bb;">Configure</span><span style="color:#007700;">::</span><span style="color:#0000bb;">write</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">settings</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'configure_key'</span><span style="color:#007700;">], </span><span style="color:#0000bb;">$value </span><span style="color:#007700;">);<br />
}<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_error</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;Not valid transaction state&#8217;</span><span style="color:#007700;">);<br />
}<br />
}<br />
</span><span style="color:#0000bb;">?&gt;</span><!-- comments --></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/webtechnology.wordpress.com/40/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/webtechnology.wordpress.com/40/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webtechnology.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webtechnology.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webtechnology.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webtechnology.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webtechnology.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webtechnology.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webtechnology.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webtechnology.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webtechnology.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webtechnology.wordpress.com/40/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&blog=2417486&post=40&subd=webtechnology&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://webtechnology.wordpress.com/2008/04/08/transaction-behavior/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/aseefahmed-128.jpg" medium="image">
			<media:title type="html">aseefahmed</media:title>
		</media:content>
	</item>
		<item>
		<title>Creating Reusable Elements with requestAction</title>
		<link>http://webtechnology.wordpress.com/2008/04/08/creating-reusable-elements-with-requestaction/</link>
		<comments>http://webtechnology.wordpress.com/2008/04/08/creating-reusable-elements-with-requestaction/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 04:12:11 +0000</pubDate>
		<dc:creator>aseefahmed</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://webtechnology.wordpress.com/?p=39</guid>
		<description><![CDATA[Creating reusable elements with requestAction is very simple. At the end, we can even cache the element using the new feature in 1.2.
Start of with a simple controller.
Controller Class:
Download code &#60;?php
class PostsController extends AppController {
var $name = &#8216;Posts&#8217;;
function index() {
$posts = $this-&#62;Post-&#62;findAll();
if(isset($this-&#62;params['requested'])) {
return $posts;
}
$this-&#62;set(&#8216;posts&#8217;, $posts);
}
}
?&#62;  
So, we created the Posts controller and we gave it an [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Creating reusable elements with requestAction is very simple. At the end, we can even cache the element using the new feature in 1.2.</p>
<p>Start of with a simple controller.</p>
<h4><strong>Controller Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/342/block/5">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">PostsController </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">AppController </span><span style="color:#007700;">{<br />
var </span><span style="color:#0000bb;">$name </span><span style="color:#007700;">= </span><span style="color:#dd0000;">&#8216;Posts&#8217;</span><span style="color:#007700;">;</p>
<p>function </span><span style="color:#0000bb;">index</span><span style="color:#007700;">() {<br />
</span><span style="color:#0000bb;">$posts </span><span style="color:#007700;">= </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">Post</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">findAll</span><span style="color:#007700;">();<br />
if(isset(</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">params</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'requested'</span><span style="color:#007700;">])) {<br />
return </span><span style="color:#0000bb;">$posts</span><span style="color:#007700;">;<br />
}<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">set</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;posts&#8217;</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$posts</span><span style="color:#007700;">);<br />
}<br />
}<br />
</span><span style="color:#0000bb;">?&gt;</span> </span> </code><br />
So, we created the Posts controller and we gave it an index action. Then we found all the posts, then if requestAction is used it will return the posts, otherwise it will set the posts for the view.</p>
<p>Now we can create a reusable element that will use requestAction. We create a file in /app/elements/posts.thtml, then add the code below.</p>
<h4><strong>View Template:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/342/block/1">Download code</a><code><span style="color:#000000;"><br />
$posts = $this-&gt;requestAction(&#8217;posts/index&#8217;);<br />
foreach($posts as $post):<br />
echo $post['Post']['title'];<br />
endforeach;<br />
</span> </code></p>
<p>Now we have the element we can include it in the layout, another element or a view template.</p>
<h4><strong>View Template:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/342/block/2">Download code</a><code><span style="color:#000000;"><br />
<span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">renderElement</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;posts&#8217;</span><span style="color:#007700;">);</span><span style="color:#0000bb;">?&gt;<br />
</span> </span> </code></p>
<p>The code above is for 1.1. If you want to get into the goodness of Cake 1.2 you can look at the updated code below.</p>
<h4><strong>Controller Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/342/block/6">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">PostsController </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">AppController </span><span style="color:#007700;">{<br />
var </span><span style="color:#0000bb;">$name </span><span style="color:#007700;">= </span><span style="color:#dd0000;">&#8216;Posts&#8217;</span><span style="color:#007700;">;</p>
<p>function </span><span style="color:#0000bb;">index</span><span style="color:#007700;">() {<br />
</span><span style="color:#0000bb;">$posts </span><span style="color:#007700;">= </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">paginate</span><span style="color:#007700;">();<br />
if(isset(</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">params</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'requested'</span><span style="color:#007700;">])) {<br />
return </span><span style="color:#0000bb;">$posts</span><span style="color:#007700;">;<br />
}<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">set</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;posts&#8217;</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$posts</span><span style="color:#007700;">);<br />
}<br />
}<br />
</span><span style="color:#0000bb;">?&gt;</span> </span> </code><br />
Notice we changed the index action to use paginate instead.<br />
In the view we can use paginate params to control what is returned.</p>
<h4><strong>View Template:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/342/block/3">Download code</a><code><span style="color:#000000;"><br />
$posts = $this-&gt;requestAction(&#8217;posts/index/sort:created/direction:desc/limit:10&#8242;);<br />
foreach($posts as $post):<br />
echo $post['Post']['title'];<br />
endforeach;<br />
</span> </code><br />
Now we are only grabbing the latest 10 posts. Whoa, we just created a latest posts element without having to change our index action or create another action.</p>
<p>Lets call this one latest posts and cache it for an hour.<br />
create the file /app/elements/latest_posts.ctp.</p>
<h4><strong>View Template:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/342/block/4">Download code</a><code><span style="color:#000000;"><br />
<span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">element</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;latest_posts&#8217;</span><span style="color:#007700;">, array(</span><span style="color:#dd0000;">&#8216;cache&#8217;</span><span style="color:#007700;">=&gt;</span><span style="color:#dd0000;">&#8216;+1 hour&#8217;</span><span style="color:#007700;">);</span><span style="color:#0000bb;">?&gt;<br />
</span> </span> </code><br />
We can still use this element in any other view file.</p>
<p>There are other ways to use requestAction, but if you are looking to create reusable elements for your site, then this is the way to go.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/webtechnology.wordpress.com/39/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/webtechnology.wordpress.com/39/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webtechnology.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webtechnology.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webtechnology.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webtechnology.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webtechnology.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webtechnology.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webtechnology.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webtechnology.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webtechnology.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webtechnology.wordpress.com/39/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&blog=2417486&post=39&subd=webtechnology&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://webtechnology.wordpress.com/2008/04/08/creating-reusable-elements-with-requestaction/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/aseefahmed-128.jpg" medium="image">
			<media:title type="html">aseefahmed</media:title>
		</media:content>
	</item>
		<item>
		<title>Multiple rules of validation per field in CakePHP 1.2</title>
		<link>http://webtechnology.wordpress.com/2008/04/07/multiple-rules-of-validation-per-field-in-cakephp-12/</link>
		<comments>http://webtechnology.wordpress.com/2008/04/07/multiple-rules-of-validation-per-field-in-cakephp-12/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 11:46:46 +0000</pubDate>
		<dc:creator>aseefahmed</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://webtechnology.wordpress.com/?p=38</guid>
		<description><![CDATA[On its 1.1 release, CakePHP only allowed us to define one rule of validation per field. If we needed to specify more than one rule, we either had to use some handy extensions to CakePHP that are available, or achieve multiple validation by using the callback method beforeValidate() in our models. CakePHP 1.2 brings us [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>On its 1.1 release, CakePHP only allowed us to define one rule of validation per field. If we needed to specify more than one rule, we either had to use some handy extensions to CakePHP that are available, or achieve multiple validation by using the callback method beforeValidate() in our models. CakePHP 1.2 brings us an improved technique to specify multiple rules per field, and best of all, it&#8217;s backwards compatible, so our existing rules will continue to work.</p>
<p>What better way to start that with an example, specifically the typical Article CRUD example. We start by creating a simple controller that provide us with an add form:</p>
<h4><strong>Controller Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/369/block/9">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">ArticlesController </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">AppController </span><span style="color:#007700;">{<br />
var </span><span style="color:#0000bb;">$name </span><span style="color:#007700;">= </span><span style="color:#dd0000;">&#8216;Articles&#8217;</span><span style="color:#007700;">;<br />
var </span><span style="color:#0000bb;">$helpers </span><span style="color:#007700;">= array(</span><span style="color:#dd0000;">&#8216;Form&#8217;</span><span style="color:#007700;">);</span></span></code></p>
<p>function <span style="color:#0000bb;">add</span><span style="color:#007700;">() {<br />
if (!empty(</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">data</span><span style="color:#007700;">)) {<br />
</span><span style="color:#ff8000;">// We don&#8217;t do any real saving, we just validate the model</span></p>
<p><span style="color:#007700;">if (</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">Article</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">create</span><span style="color:#007700;">(</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">data</span><span style="color:#007700;">) &amp;&amp; </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">Article</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">validates</span><span style="color:#007700;">()) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">set</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;valid&#8217;</span><span style="color:#007700;">, </span><span style="color:#0000bb;">true</span><span style="color:#007700;">);<br />
}<br />
}<br />
}<br />
}<br />
</span><span style="color:#0000bb;">?&gt;</span></p>
<p>and its matching articles/add.ctp view:</p>
<h4><strong>View Template:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/369/block/5">Download code</a><code><span style="color:#000000;"><br />
<span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">create</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;Article&#8217;</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
</span> <span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">input</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;title&#8217;</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
</span> <span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">input</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;body&#8217;</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">end</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;Add Article&#8217;</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
</span> </span> </code></p>
<p>Let&#8217;s take the classic Article model, and add only one validation to some fields:</p>
<h4><strong>Model Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/369/block/1">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">Article </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">AppModel </span><span style="color:#007700;">{<br />
var </span><span style="color:#0000bb;">$name </span><span style="color:#007700;">= </span><span style="color:#dd0000;">&#8216;Article&#8217;</span><span style="color:#007700;">;<br />
var </span><span style="color:#0000bb;">$validate </span><span style="color:#007700;">= array(<br />
</span><span style="color:#dd0000;">&#8216;title&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">VALID_NOT_EMPTY</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">&#8216;body&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">VALID_NOT_EMPTY<br />
</span><span style="color:#007700;">);<br />
}<br />
</span><span style="color:#0000bb;">?&gt;</span> </span> </code></p>
<p>Nothing new here, except that as you can see we&#8217;re not defining the error messages. We are going to do so in the view. So we go back and change the add.ctp view so it now looks like this:</p>
<h4><strong>View Template:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/369/block/6">Download code</a><code><span style="color:#000000;"><br />
<span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">create</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;Article&#8217;</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
</span> <span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">input</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;title&#8217;</span><span style="color:#007700;">, array(</span><span style="color:#dd0000;">&#8216;error&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">&#8216;Please specify a valid title&#8217;</span><span style="color:#007700;">)); </span><span style="color:#0000bb;">?&gt;<br />
</span> <span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">input</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;body&#8217;</span><span style="color:#007700;">, array(</span><span style="color:#dd0000;">&#8216;error&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">&#8216;Please specify a valid body&#8217;</span><span style="color:#007700;">)); </span><span style="color:#0000bb;">?&gt;<br />
&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">end</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;Add Article&#8217;</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
</span> </span> </code></p>
<p>If we try the add action and submit the form with both fields empty, we&#8217;ll get our newly defined error messages. What if we also wanted to add another rule of validation for the title field, where we should check that the title is at the most 100 characters long? Let&#8217;s review the model, and now change it to look like this:</p>
<h4><strong>Model Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/369/block/2">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">Article </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">AppModel </span><span style="color:#007700;">{<br />
var </span><span style="color:#0000bb;">$name </span><span style="color:#007700;">= </span><span style="color:#dd0000;">&#8216;Article&#8217;</span><span style="color:#007700;">;<br />
var </span><span style="color:#0000bb;">$validate </span><span style="color:#007700;">= array(<br />
</span><span style="color:#dd0000;">&#8216;title&#8217; </span><span style="color:#007700;">=&gt; array(<br />
</span><span style="color:#0000bb;">VALID_NOT_EMPTY</span><span style="color:#007700;">,<br />
array(<br />
</span><span style="color:#dd0000;">&#8216;rule&#8217; </span><span style="color:#007700;">=&gt; array(</span><span style="color:#dd0000;">&#8216;maxLength&#8217;</span><span style="color:#007700;">, </span><span style="color:#0000bb;">100</span><span style="color:#007700;">)<br />
)<br />
),<br />
</span><span style="color:#dd0000;">&#8216;body&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">VALID_NOT_EMPTY<br />
</span><span style="color:#007700;">);<br />
}<br />
</span><span style="color:#0000bb;">?&gt;</span> </span> </code></p>
<p>We have just converted the &#8216;title&#8217; field to consist of an array of rules. The first rule is a CakePHP provided regular expression that checks for a specified value, and the second rule is a method existing in CakePHP&#8217;s built in Validation class called &#8216;maxLength&#8217;. Since this method takes an extra parameter (the number of maximum characters the field can contain), we add it on an array.</p>
<p>So we have now two conditions that can generate an error for the field title: an empty value, or a value with more than 100 characters. How do we differentiate the error messages on the view? Let&#8217;s change the view so it now looks like:</p>
<h4><strong>View Template:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/369/block/7">Download code</a><code><span style="color:#000000;"><br />
<span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">create</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;Article&#8217;</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
</span> <span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">input</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;title&#8217;</span><span style="color:#007700;">, array(</span><span style="color:#dd0000;">&#8216;error&#8217; </span><span style="color:#007700;">=&gt; array(<br />
</span><span style="color:#0000bb;">0 </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">&#8216;Please specify a valid title&#8217;</span><span style="color:#007700;">,<br />
</span><span style="color:#0000bb;">1 </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">&#8216;The title must have no more than 100 characters&#8217;<br />
</span><span style="color:#007700;">))); </span><span style="color:#0000bb;">?&gt;<br />
</span> <span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">input</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;body&#8217;</span><span style="color:#007700;">, array(</span><span style="color:#dd0000;">&#8216;error&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">&#8216;Please specify a valid body&#8217;</span><span style="color:#007700;">)); </span><span style="color:#0000bb;">?&gt;<br />
&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">end</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;Add Article&#8217;</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
</span> </span> </code></p>
<p>As you can see we&#8217;re setting an error message per rule. 0 corresponds to the first rule, 1 to the second, and so on. If we wanted more flexibility (such as having the option to change the order of the rules and still have the same error message assignment) and needed more readability, we can then use the string index approach. Change the model so it now looks like:</p>
<h4><strong>Model Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/369/block/3">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">Article </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">AppModel </span><span style="color:#007700;">{<br />
var </span><span style="color:#0000bb;">$name </span><span style="color:#007700;">= </span><span style="color:#dd0000;">&#8216;Article&#8217;</span><span style="color:#007700;">;<br />
var </span><span style="color:#0000bb;">$validate </span><span style="color:#007700;">= array(<br />
</span><span style="color:#dd0000;">&#8216;title&#8217; </span><span style="color:#007700;">=&gt; array(<br />
</span><span style="color:#dd0000;">&#8216;required&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">VALID_NOT_EMPTY</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">&#8216;length&#8217; </span><span style="color:#007700;">=&gt; array( </span><span style="color:#dd0000;">&#8216;rule&#8217; </span><span style="color:#007700;">=&gt; array(</span><span style="color:#dd0000;">&#8216;maxLength&#8217;</span><span style="color:#007700;">, </span><span style="color:#0000bb;">100</span><span style="color:#007700;">) )<br />
),<br />
</span><span style="color:#dd0000;">&#8216;body&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">VALID_NOT_EMPTY<br />
</span><span style="color:#007700;">);<br />
}<br />
</span><span style="color:#0000bb;">?&gt;</span> </span> </code></p>
<p>and change the view so it now looks like:</p>
<h4><strong>View Template:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/369/block/8">Download code</a><code><span style="color:#000000;"><br />
<span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">create</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;Article&#8217;</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
</span> <span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">input</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;title&#8217;</span><span style="color:#007700;">, array(</span><span style="color:#dd0000;">&#8216;error&#8217; </span><span style="color:#007700;">=&gt; array(<br />
</span><span style="color:#dd0000;">&#8216;required&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">&#8216;Please specify a valid title&#8217;</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">&#8216;length&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">&#8216;The title must have no more than 100 characters&#8217;<br />
</span><span style="color:#007700;">))); </span><span style="color:#0000bb;">?&gt;<br />
</span> <span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">input</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;body&#8217;</span><span style="color:#007700;">, array(</span><span style="color:#dd0000;">&#8216;error&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">&#8216;Please specify a valid body&#8217;</span><span style="color:#007700;">)); </span><span style="color:#0000bb;">?&gt;<br />
&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">end</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;Add Article&#8217;</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
</span> </span> </code></p>
<h3>Custom Validation</h3>
<p>What about custom validation? What if we needed more rules than those provided by CakePHP&#8217;s Validation class? Don&#8217;t sweat, it comes very easy! All you need to do is set up your own validation functions on either your model or your AppModel class (if you wish to share them across your models.) For example, we&#8217;re going to add a new validation rule to allow us to specify a minimum and a maximum length for our title. I know, what&#8217;s the point when we have both minLength and maxLength in CakePHP&#8217;s Validation class? Well, to show how it can be done <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Edit the model and change it so it now looks like this:</p>
<h4><strong>Model Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/369/block/4">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">Article </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">AppModel </span><span style="color:#007700;">{<br />
var </span><span style="color:#0000bb;">$name </span><span style="color:#007700;">= </span><span style="color:#dd0000;">&#8216;Article&#8217;</span><span style="color:#007700;">;<br />
var </span><span style="color:#0000bb;">$validate </span><span style="color:#007700;">= array(<br />
</span><span style="color:#dd0000;">&#8216;title&#8217; </span><span style="color:#007700;">=&gt; array(<br />
</span><span style="color:#dd0000;">&#8216;required&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">VALID_NOT_EMPTY</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">&#8216;length&#8217; </span><span style="color:#007700;">=&gt; array( </span><span style="color:#dd0000;">&#8216;rule&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">&#8216;validateLength&#8217;</span><span style="color:#007700;">, </span><span style="color:#dd0000;">&#8216;min&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">5</span><span style="color:#007700;">, </span><span style="color:#dd0000;">&#8216;max&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">100 </span><span style="color:#007700;">)<br />
),<br />
</span><span style="color:#dd0000;">&#8216;body&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">VALID_NOT_EMPTY<br />
</span><span style="color:#007700;">);</span></span></code></p>
<p>function <span style="color:#0000bb;">validateLength</span><span style="color:#007700;">(</span><span style="color:#0000bb;">$value</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$params </span><span style="color:#007700;">= array()) {<br />
</span><span style="color:#0000bb;">$valid </span><span style="color:#007700;">= </span><span style="color:#0000bb;">false</span><span style="color:#007700;">;</span></p>
<p><span style="color:#0000bb;">$params </span><span style="color:#007700;">= </span><span style="color:#0000bb;">am</span><span style="color:#007700;">(array(<br />
</span><span style="color:#dd0000;">&#8216;min&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">null</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">&#8216;max&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">null</span><span style="color:#007700;">,<br />
), </span><span style="color:#0000bb;">$params</span><span style="color:#007700;">);</span></p>
<p>if (empty(<span style="color:#0000bb;">$params</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'min'</span><span style="color:#007700;">]) || empty(</span><span style="color:#0000bb;">$params</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'max'</span><span style="color:#007700;">])) {<br />
</span><span style="color:#0000bb;">$valid </span><span style="color:#007700;">= </span><span style="color:#0000bb;">false</span><span style="color:#007700;">;<br />
} else if (</span><span style="color:#0000bb;">strlen</span><span style="color:#007700;">(</span><span style="color:#0000bb;">$value</span><span style="color:#007700;">) &gt;= </span><span style="color:#0000bb;">$params</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'min'</span><span style="color:#007700;">] &amp;&amp; </span><span style="color:#0000bb;">strlen</span><span style="color:#007700;">(</span><span style="color:#0000bb;">$value</span><span style="color:#007700;">) &lt;= </span><span style="color:#0000bb;">$params</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'max'</span><span style="color:#007700;">]) {<br />
</span><span style="color:#0000bb;">$valid </span><span style="color:#007700;">= </span><span style="color:#0000bb;">true</span><span style="color:#007700;">;<br />
}</span></p>
<p>return <span style="color:#0000bb;">$valid</span><span style="color:#007700;">;<br />
}<br />
}<br />
</span><span style="color:#0000bb;">?&gt;</span></p>
<p>A custom validation function takes one mandatory first parameter: the value to validate, and must return a boolean value of true when the value validates, or false when it doesn&#8217;t. Extra parameters will be sent to the validation function as an array through its second parameter, and the values in the array are those values specified in the validation rule that do not correspond to CakePHP&#8217;s internal values (such as rule or allowEmpty.)</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/webtechnology.wordpress.com/38/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/webtechnology.wordpress.com/38/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webtechnology.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webtechnology.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webtechnology.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webtechnology.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webtechnology.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webtechnology.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webtechnology.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webtechnology.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webtechnology.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webtechnology.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&blog=2417486&post=38&subd=webtechnology&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://webtechnology.wordpress.com/2008/04/07/multiple-rules-of-validation-per-field-in-cakephp-12/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/aseefahmed-128.jpg" medium="image">
			<media:title type="html">aseefahmed</media:title>
		</media:content>
	</item>
		<item>
		<title>Browse The File System</title>
		<link>http://webtechnology.wordpress.com/2008/03/28/browse-the-file-system/</link>
		<comments>http://webtechnology.wordpress.com/2008/03/28/browse-the-file-system/#comments</comments>
		<pubDate>Fri, 28 Mar 2008 11:22:47 +0000</pubDate>
		<dc:creator>aseefahmed</dc:creator>
		
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://webtechnology.wordpress.com/?p=36</guid>
		<description><![CDATA[cd changes derectories

cd 

To your home directory




cd ..

To your home directory


cd -

To your previous working directory



Listing Directory Contents

ls -a 

include hidden files




ls -l

long listing style


ls -R

Recurse through directories



Copying Files &#38; Directories


cp -i source destination

interactive(ask before overwriting file)




cp -R source destination

recursive (recursively copy an entire directory tree)



Creating &#38; Removing Files


touch 

create empty file or update filetimestamps




rm -i 

interactive(ask before removing [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><font color="#993300"><b>cd changes derectories</b></font></p>
<ul>
<li><font color="#339966">cd </font>
<ul>
<li>To your home directory</li>
</ul>
</li>
</ul>
<ul>
<li><font color="#339966">cd ..</font>
<ul>
<li>To your home directory</li>
</ul>
</li>
<li><font color="#339966">cd -</font>
<ul>
<li>To your previous working directory</li>
</ul>
</li>
</ul>
<p><font color="#993300"><b>Listing Directory Contents</b></font></p>
<ul>
<li><font color="#339966">ls -a </font>
<ul>
<li>include hidden files</li>
</ul>
</li>
</ul>
<ul>
<li><font color="#339966">ls -l</font>
<ul>
<li>long listing style</li>
</ul>
</li>
<li><font color="#339966">ls -R</font>
<ul>
<li>Recurse through directories</li>
</ul>
</li>
</ul>
<p><font color="#993300"><b>Copying Files &amp; Directories<br />
</b></font></p>
<ul>
<li><font color="#339966">cp -i source destination</font>
<ul>
<li>interactive(ask before overwriting file)</li>
</ul>
</li>
</ul>
<ul>
<li><font color="#339966">c</font><font color="#339966">p -R source destination</font>
<ul>
<li>recursive (recursively copy an entire directory tree)</li>
</ul>
</li>
</ul>
<p><font color="#993300"><b>Creating &amp; Removing Files<br />
</b></font></p>
<ul>
<li><font color="#339966">touch </font>
<ul>
<li>create empty file or update filetimestamps</li>
</ul>
</li>
</ul>
<ul>
<li><font color="#339966">rm -i </font><font color="#339966"></font>
<ul>
<li>interactive(ask before removing file)</li>
</ul>
</li>
<li><font color="#339966">rm -R</font>
<ul>
<li>recursively (for removing an entire directory)</li>
</ul>
</li>
<li><font color="#339966">rm -f</font>
<ul>
<li>remove by forcing</li>
</ul>
</li>
<li><font color="#339966">mkdir -p</font>
<ul>
<li>make parent directories if needed</li>
</ul>
</li>
</ul>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/webtechnology.wordpress.com/36/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/webtechnology.wordpress.com/36/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webtechnology.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webtechnology.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webtechnology.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webtechnology.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webtechnology.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webtechnology.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webtechnology.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webtechnology.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webtechnology.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webtechnology.wordpress.com/36/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&blog=2417486&post=36&subd=webtechnology&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://webtechnology.wordpress.com/2008/03/28/browse-the-file-system/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/aseefahmed-128.jpg" medium="image">
			<media:title type="html">aseefahmed</media:title>
		</media:content>
	</item>
		<item>
		<title>Linux File Permission</title>
		<link>http://webtechnology.wordpress.com/2008/03/12/linux-file-permission/</link>
		<comments>http://webtechnology.wordpress.com/2008/03/12/linux-file-permission/#comments</comments>
		<pubDate>Wed, 12 Mar 2008 03:34:50 +0000</pubDate>
		<dc:creator>aseefahmed</dc:creator>
		
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://webtechnology.wordpress.com/?p=35</guid>
		<description><![CDATA[What are permissions?
On a UNIX web server, every single file and folder stored on the hard drive has a set of permissions associated with it, which says who is allowed to do what with the file. Every file (and folder) also has an &#8220;owner&#8221; and a &#8220;group&#8221; associated with it. If you created the file, [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h2>What are permissions?</h2>
<p>On a UNIX web server, every single file and folder stored on the hard drive has a set of permissions associated with it, which says who is allowed to do what with the file. Every file (and folder) also has an &#8220;owner&#8221; and a &#8220;group&#8221; associated with it. If you created the file, then you are usually the owner of that file, and your group, or the group associated with the folder you created the file in, will usually be associated with that file.</p>
<h2>Who can do stuff?</h2>
<p>There are three types of people that can do stuff to files - the <b>Owner</b> of the file, anyone in the <b>Group</b> that the file belongs to, and <b>Others</b> (everyone else).  In UNIX, these 3 types of people are referred to using the letters <b>U</b> (for Owner, or User in UNIX-speak!), <b>G</b> (for Group), and <b>O</b> (for Others).</p>
<h2>What stuff can you do?</h2>
<p>There are three basic things that can be done to files or folders:</p>
<ul>
<li>You can <b>read</b> the file. For folders, this means listing the contents of the folder.</li>
<li>You can <b>write</b> to (change) the file. For folders, this means creating and deleting files in the folder.</li>
<li>You can <b>execute</b> (run) the file, if it&#8217;s a program or script. For folders, this means accessing files in the folder.</li>
</ul>
<h2>What do all these funny letters and numbers mean?!</h2>
<p>That&#8217;s the basics of permissions covered. As you can see, there&#8217;s not much to them really!</p>
<p>The confusion often occurs when you have to start actually setting permissions on your file server. CGI scripts will tell you to do things like &#8220;chmod 755&#8243; or &#8220;Check that the file is executable&#8221;. Also, when you use FTP or SSH, you&#8217;ll see lots of funny letters next to the files (such as <code>rwxrw-rw-</code>).  We&#8217;ll now explain what all these hieroglyphics mean!</p>
<p>When you FTP to your web server, you&#8217;ll probably see something like this next to every file and folder:</p>
<div class="articleImageWrap"><img src="http://www.elated.com/res/Image/articles/management/unix/understanding-permissions/attr.gif" alt="Attributes list" class="articleImage" style="max-width:398px;" /></div>
<p>This string of letters, <code>drwxrwxrwx</code>, represents the permissions that are set for this folder. (Note that these are often called <i>attributes</i> by FTP programs.) Let&#8217;s explain what each of these letters means:</p>
<table class="shaded">
<tr>
<th>d</th>
<th>r</th>
<th>w</th>
<th>x</th>
<th>r</th>
<th>w</th>
<th>x</th>
<th>r</th>
<th>w</th>
<th>x</th>
</tr>
<tr>
<td style="text-align:center;">&nbsp;</td>
<td style="text-align:center;" colspan="3">Owner</td>
<td style="text-align:center;" colspan="3">Group</td>
<td style="text-align:center;" colspan="3">Other</td>
</tr>
<tr>
<td>Directory</td>
<td>Read</td>
<td>Write</td>
<td>Execute</td>
<td>Read</td>
<td>Write</td>
<td>Execute</td>
<td>Read</td>
<td>Write</td>
<td>Execute</td>
</tr>
</table>
<p>As you can see, the string of letters breaks down into 3 sections of 3 letters each, representing each of the types of users (the owner, members of the group, and everyone else). There is also a &#8220;d&#8221; attribute on the left, which tells us if this is a file or a directory (folder).</p>
<p>If any of these letters is replaced with a hyphen (-), it means that permission is <i>not</i> granted. For example:</p>
<dl>
<dt><code>drwxr-xr-x</code></dt>
<dd>A folder which has read, write and execute permissions for the owner, but only read and execute permissions for the group and for other users.</dd>
<dt><code>-rw-rw-rw-</code></dt>
<dd>A file that can be read and written by anyone, but not executed at all.</dd>
<dt><code>-rw-r--r--</code></dt>
<dd>A file that can be read and written by the user, but only read by the group and everyone else.</dd>
</dl>
<h3>Using numbers instead of letters</h3>
<p>As we said earlier, you&#8217;ll often be asked to do things using numbers, such as &#8220;set 755 permissions&#8221;. What do those numbers mean?</p>
<p>Well, each of the three numbers corresponds to each of the three sections of letters we referred to earlier. In other words, the first number determines the <i>owner</i> permissions, the second number determines the <i>group</i> permissions, and the third number determines the <i>other</i> permissions.</p>
<p>Each number can have one of eight values ranging from 0 to 7. Each value corresponds to a certain setting of the read, write and execute permissions, as explained in this table:</p>
<p>Read: 4<br />
Write: 2<br />
Execute: 1</p>
<p>So, for example:</p>
<div><b>777</b> is the same as <code>rwxrwxrwx</code><b>755</b> is the same as <code>rwxr-xr-x</code><b>666</b> is the same as <code>rw-rw-rw-</code></p>
<p><b>744</b> is the same as <code>rwxr--r--</code></p>
<h3>Setting permissions using number notation</h3>
<p>To set permissions with numbers, use the following syntax:</p>
<p><kbd>chmod <var>nnn</var> <var>filename</var></kbd></p>
<p style="margin-top:20px;">where <var>nnn</var> is the 3-digit number representing the permissions, and <var>filename</var> is the file you want to change. For example:</p>
<p><kbd>chmod 755 formmail.cgi</kbd></p>
<p style="margin-top:20px;">will assign read, write and execute permission to the owner, and just read and execute permission to everyone else, on the script called <code>formmail.cgi</code>.</p>
<h3>Setting permissions using letter notation</h3>
<p>You can use the letters <code>u</code> (owner/user), <code>g</code> (group) and <code>o</code> (other) to set permissions for each of the user types, and <code>r</code> (read), <code>w</code> (write) and <code>x</code> (execute) to represent the permissions to set.</p>
<p>You can also use <code>a</code> instead of u, g, and o, to mean all users (u,g,o).</p>
<p>You assign permissions using either the plus sign (<code>+</code>), which means &#8220;add these permissions&#8221;, the minus sign (<code>-</code>), which means &#8220;remove these permissions&#8221;, or the equals sign (<code>=</code>), which means &#8220;change the permissions to exactly these&#8221;.</p>
<p>For example:</p>
<p><kbd>chmod a+x formmail.cgi</kbd> adds execute permissions for all users to the file <code>formmail.cgi</code> (in other words, makes the file executable).</p>
<p><kbd>chmod u=rwx formmail.cgi</kbd> sets read, write and execute permission just for the owner (the permissions for the group and for others remain unchanged).</p>
<p><kbd>chmod go-w formmail.cgi</kbd> removes write permission for the group and for others, leaving the permissions for the owner unchanged.</p>
<h4>Checking your permissions</h4>
<p>You can check the permissions on all files and folders in the current directory by using the command:</p>
<p><kbd>ls -l</kbd></p>
<p style="margin-top:20px;">This will show you the permissions for every file and folder, in the same way as your FTP program does.</p>
<h4>chmod by the number</h4>
<h1></h1>
<p>Up to this point, we’ve been setting the mode with letters. It turns out that you can also set the mode numerically.  Here’s how it works:</p>
<ol>
<li>Write the permissions you want the file to have. To make your life easier, write the permissions grouped into sets of three letters. For example, let’s say you want file <code>info.sh</code> to have these permissions
<pre>- rwx r-x r-- info.sh</pre>
</li>
<li>Under each letter, write a digit 1; under each dash write a digit zero. Ignore the dash at the very beginning that tells you whether it’s a file or directory. This gives you three  binary numbers.
<pre>- rwx r-x r-- info.sh
  111 101 100</pre>
</li>
<li>Now convert each set of three digits to a single digit using this table:<br />
<table>
<tr>
<td>
<table border="1" cellpadding="3">
<thead>
<th>Binary</th>
<th>Becomes</th>
<td><code>000</code></td>
<td><code>0</code></td>
</tr>
<tr>
<td><code>001</code></td>
<td><code>1</code></td>
</tr>
<tr>
<td><code>010</code></td>
<td><code>2</code></td>
</tr>
<tr>
<td><code>011</code></td>
<td><code>3</code></td>
</tr>
</table>
</td>
<td>
<table border="1" cellpadding="3">
<thead>
<th>Binary</th>
<th>Becomes</th>
<td><code>100</code></td>
<td><code>4</code></td>
</tr>
<tr>
<td><code>101</code></td>
<td><code>5</code></td>
</tr>
<tr>
<td><code>110</code></td>
<td><code>6</code></td>
</tr>
<tr>
<td><code>111</code></td>
<td><code>7</code></td>
</tr>
</table>
</td>
</tr>
</table>
<p>From our example, the <code>111 101 100</code> translates to the number <code>754</code>.</li>
<li>Now use that number in a <code>chmod</code> command to set your desired permissions on the file:
<pre>chmod 754 info.sh</pre>
</li>
</ol>
<p style="margin-top:20px;">&nbsp;</p>
<p style="margin-top:20px;">&nbsp;</p>
</div>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/webtechnology.wordpress.com/35/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/webtechnology.wordpress.com/35/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webtechnology.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webtechnology.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webtechnology.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webtechnology.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webtechnology.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webtechnology.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webtechnology.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webtechnology.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webtechnology.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webtechnology.wordpress.com/35/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&blog=2417486&post=35&subd=webtechnology&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://webtechnology.wordpress.com/2008/03/12/linux-file-permission/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/aseefahmed-128.jpg" medium="image">
			<media:title type="html">aseefahmed</media:title>
		</media:content>

		<media:content url="http://www.elated.com/res/Image/articles/management/unix/understanding-permissions/attr.gif" medium="image">
			<media:title type="html">Attributes list</media:title>
		</media:content>
	</item>
		<item>
		<title>CakePHP: Data Validation</title>
		<link>http://webtechnology.wordpress.com/2008/03/11/cakephp-data-validation/</link>
		<comments>http://webtechnology.wordpress.com/2008/03/11/cakephp-data-validation/#comments</comments>
		<pubDate>Tue, 11 Mar 2008 10:49:55 +0000</pubDate>
		<dc:creator>aseefahmed</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://webtechnology.wordpress.com/2008/03/11/cakephp-data-validation/</guid>
		<description><![CDATA[
Data Validation
Creating custom validation rules can help to make sure the data in a     Model conforms to the business rules of the application, such as passwords     can only be eight characters long, user names can only have letters,     etc.
The first step to data [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><div class="text">
<h3>Data Validation</h3>
<p>Creating custom validation rules can help to make sure the data in a     Model conforms to the business rules of the application, such as passwords     can only be eight characters long, user names can only have letters,     etc.</p>
<p>The first step to data validation is creating the validation rules     in the Model. To do that, use the Model::validate array in the Model     definition, for example:</p>
<p><span></span></p>
<h3>/app/models/user.php</h3>
<div style="font-family:monospace;">&lt;?php<br />
class User extends AppModel<br />
{<br />
var $name = &#8216;User&#8217;;var $validate = array(<br />
&#8216;login&#8217; =&gt; &#8216;/[a-z0-9\_\-]{3,}$/i&#8217;,<br />
&#8216;password&#8217; =&gt; VALID_NOT_EMPTY,<br />
&#8216;email&#8217; =&gt; VALID_EMAIL,<br />
&#8216;born&#8217; =&gt; VALID_NUMBER<br />
);<br />
}<br />
?&gt;</div>
<p>Validations are defined using Perl-compatibile regular expressions,     some of which are pre-defined in /libs/validators.php. These are:</p>
<ul>
<li>VALID_NOT_EMPTY</li>
<li>VALID_NUMBER</li>
<li>VALID_EMAIL</li>
<li>VALID_YEAR</li>
</ul>
<p>If there are any validations present in the model definition (i.e.     in the $validate array), they will be parsed and checked during saves     (i.e. in the Model::save() method). To validate the data directly use the     Model::validates() (returns false if data is incorrect) and     Model::invalidFields() (which returns an array of error messages).</p>
<p>But usually the data is implicit in the controller code. The     following example demonstrates how to create a form-handling     action:</p>
<p><span></span></p>
<h3>Form-handling Action in         /app/controllers/blog_controller.php</h3>
<div style="font-family:monospace;">&lt;?php<br />
class BlogController extends AppController {var $uses = array(&#8217;Post&#8217;);</p>
<p>function add ()<br />
{<br />
if (empty($this-&gt;data))<br />
{<br />
$this-&gt;render();<br />
}<br />
else<br />
{<br />
if($this-&gt;Post-&gt;save($this-&gt;data))<br />
{<br />
//ok cool, the stuff is valid<br />
}<br />
else<br />
{<br />
//Danger, Will Robinson. Validation errors.<br />
$this-&gt;set(&#8217;errorMessage&#8217;, &#8216;Please correct errors below.&#8217;);<br />
$this-&gt;render();<br />
}<br />
}<br />
}<br />
}<br />
?&gt;</p></div>
<p>The view used by this action can look like this:</p>
<p><span></span></p>
<h3>The add form view in /app/views/blog/add.thtml</h3>
<div style="font-family:monospace;">&lt;h2&gt;Add post to blog&lt;/h2&gt;<br />
&lt;form action=&#8221;&lt;?php echo $html-&gt;url(&#8217;/blog/add&#8217;)?&gt;&#8221; method=&#8221;post&#8221;&gt;<br />
&lt;div class=&#8221;blog_add&#8221;&gt;<br />
&lt;p&gt;Title:<br />
&lt;?php echo $html-&gt;input(&#8217;Post/title&#8217;, array(&#8217;size&#8217;=&gt;&#8217;40&#8242;))?&gt;<br />
&lt;?php echo $html-&gt;tagErrorMsg(&#8217;Post/title&#8217;, &#8216;Title is required.&#8217;)?&gt;<br />
&lt;/p&gt;<br />
&lt;p&gt;Body<br />
&lt;?php echo $html-&gt;textarea(&#8217;Post/body&#8217;) ?&gt;<br />
&lt;?php echo $html-&gt;tagErrorMsg(&#8217;Post/body&#8217;, &#8216;Body is required.&#8217;)?&gt;<br />
&lt;/p&gt;<br />
&lt;p&gt;&lt;?=$html-&gt;submit(&#8217;Save&#8217;)?&gt;&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/form&gt;</div>
<p>The Controller::validates($model[, $model...]) is used to check any     custom validation added in the model. The Controller::validation