What Version of Cake Does the Book Use?
I’ve been asked this question many times. So, let me put your mind at ease if you’ve wondered this same thing. Beginning CakePHP: From Novice to Professional uses version 1.2 RC1.
I did my best to keep the book as up-to-date as possible, and some changes were happening during the final stages of the book (for one, version 1.2 RC2 was released), but I felt that regardless of when the book was to be published, it had to deal with 1.2 from start to finish. When the final proofs came back, I was able to remove
1 | flay() |
, for instance, since this is on the way out. And I was pleased that the Containable behavior made its way into the core in time for me to include a section on it in Chapter 14.
Obfuscate CakePHP Helper
Sometimes you may need to obfuscate your HTML markup. I know for one of my projects, I needed to set up a PayPal redirect page and didn’t want the data to be manipulated by any users, so I opted to obfuscate the markup using JavaScript functions. Obfuscation is by no means a perfect method for encrypting HTML markup, but it certainly makes hacking your stuff a pain, and generally gets the job done.
A CakePHP Helper
To simplify obfuscation methods for Cake, I put together the Obfuscate helper, which you can see below. Using it is easy. Copy the contents of the helper into the
1 | app/views/helpers/obfuscate.php |
file, then include the helper in your controller, like so:
1 var $helpers = array('Obfuscate');
Then in the view, whatever you want to obfuscate, simply run:
1 $obfuscate->string('Markup to obfuscate');
The Helper’s Code
Here’s the code you’ll need to copy to
1 | app/views/helpers/obfuscate.php |
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 <?php
/* Based on Ian Willis' iScramble PHP script: z-host.com/php/iscramble */
class ObfuscateHelper extends Helper {
/**
* Obfuscate::_rot13()
* ++ performs ROT13 enconding on a given string
* @param string $str The string to encode
* @access private
*/
function _rot13($str) {
$from = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$to = 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM';
return strtr($str, $from, $to);
}
/* Perform the equivalent of the JavaScript escape function */
/**
* Obfuscate::_escape()
* ++ Equivalent of the JavaScript escape function
* @param string $plain The string to escape
* @access private
*/
function _escape($plain) {
$escaped = "";
$passChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789*@-_+./";
for ($i = 0; $i < strlen($plain); $i++) {
$char = $plain{$i};
if (strpos($passChars, $char) === false) {
$escaped .= sprintf("%%%02X", ord($char));
} else {
$escaped .= $char;
}
}
return $escaped;
}
/**
* Obfuscate::string()
* ++ Obfuscates a given string
* @param string $plain The string to obfuscate
* @param bool $longPwn Whether to use more JavaScript code for better obfuscation
* @param bool $rot13 Whether to use ROT13 encoding; takes longer to decode, not recommended for long strings
* @param string $sorry Markup to display if the user doesn't have JavaScript enabled in the browser
* @return Obfuscated JavaScript code
* @access public
*/
function string($plain, $longPwd=false, $rot13=false, $sorry="<I>[Please Enable JavaScript]</I>") {
$escaped = $this->_escape($plain);
if ($rot13) {
$escaped = $this->_rot13($escaped);
}
$numberOfColumns = 10;
$numberOfRows = ceil(strlen($escaped) / $numberOfColumns);
$scrambled = "";
$escaped = str_pad($escaped, $numberOfColumns * $numberOfRows);
$password = "";
srand(time());
for ($j = 0; $j < ($longPwd ? $numberOfRows : 1); $j++) {
$availChars = substr("0123456789", 0, $numberOfColumns);
for ($i = 0 ; $i < $numberOfColumns; $i++) {
$char = $availChars{ rand(0, strlen($availChars)-1) };
$password .= $char;
$availChars = str_replace($char, "", $availChars);
}
}
$scramblePassword = str_repeat($password, $longPwd ? 1 : $numberOfRows);
$scrambled = str_repeat(" ", $numberOfColumns * $numberOfRows);
$k = 0;
for ($i = 0; $i < $numberOfRows; $i++) {
for($j = 0; $j < $numberOfColumns; $j++ ) {
$scrambled{(((int)$scramblePassword{$k}) * $numberOfRows) + $i} = $escaped{$k};
$k++;
}
}
$javascript = "<SCRIPT>\n<!--\n";
$javascript .= "var a='';var b='$scrambled';var c='$password';";
if ($rot13) {
$javascript .= "var d='';";
}
$javascript .= "for(var i=0;i<$numberOfRows;i++) for(var j=0;j<$numberOfColumns;j++) ";
if ($rot13) {
$javascript .= "{d=b.charCodeAt(";
} else {
$javascript .= "a+=b.charAt(";
}
if ($longPwd) {
$javascript .= "(parseInt(c.charAt(i*$numberOfColumns+j))*$numberOfRows)+i); ";
} else {
$javascript .= "(parseInt(c.charAt(j))*$numberOfRows)+i);";
}
if ($rot13) {
$javascript .= "if ((d>=65 && d<78) || (d>=97 && d<110)) d+=13; else if ((d>=78 && d<91) || (d>=110 && d<123)) d-=13;a+=String.fromCharCode(d);}";
}
$javascript .= "document.writeln(unescape(a));\n";
$javascript .= "-->\n</SCRIPT>\n";
$javascript .= "<NOSCRIPT>\n$sorry\n</NOSCRIPT>\n";
return $javascript;
}
}
?>
Table of Contents
I’ve been asked about my upcoming CakePHP book’s table of contents. Here’s a list of the chapters with a short description about each one. As always, you can order the book which is now available online and at a bookstore near you.
Chapter 1: Introduction
Discusses many of the main advantages for using Cake and explains the direction I will go in teaching you how to use it. Briefly explains the model-view-controller (MVC) paradigm for software development as well as the concept of inversion of control for frameworks. Key benefits of using Cake over other frameworks are also discussed.
Chapter 2: Installing and Running CakePHP
This chapter explains how to install Cake and launch a bare-bones application in your web browser. This book uses CakePHP 1.2 RC1, so the installation procedures get you running in 1.2 quickly. More detailed instructions for running Cake on a localhost setup are explained in Appendix A.
Chapter 3: Creating a To-Do List Application
All about using Cake’s scaffolding feature to create a to-do list application. Explains in more detail how MVC works in Cake.
Chapter 4: Naming Files and Designing the Database
Explains the naming conventions in Cake, best practices when naming files and resources, and how to set up the database to work with Cake. Discusses database normalization, as well as Cake’s table association parameters that manage one-to-one, one-to-many, and many-to-many data relationships. How to use models is discussed, and each of Cake’s table association methods (belongsTo, hasOne, hasMany, and hasAndBelongsToMany) are explained in detail.
Chapter 5: Creating Simple Views and Baking in the Console
Explains various methods for creating views and layouts and how to use the Bake shell script to create views and functions automatically. Gives instructions on how to get Bake working in the console as well.
Chapter 6: Customizing Views
Discusses different user interaction sequences in Cake, how to handle form submissions, and how to customize views following previously baked code.
Chapter 7: Working with Controllers and Models
The main tutorial of building a more extensive blog application begins in this chapter. Explains how to customize controllers and models to handle various methods. Model functions, like
1 | find() |
and
1 | read() |
, as well as data validation methods are explained in detail. How to build controller actions is also discussed.
Chapter 8: Implementing Ajax Features
All about the Ajax helper and how to use Ajax in Cake to building a voting mechanism into the blog’s comments section. Also explains how to do file uploads using the File utility class and jQuery.
Chapter 9: Helpers
This chapter goes into detail about using helpers, how to build custom helpers, and what’s available in Cake’s built-in helpers. Each function in the HTML and Form helpers is discussed, and all other built-in helpers are outlined as well.
Chapter 10: Routes
Building routes with arguments, reverse routing, custom expressions, magic variables, and more is explained here. Also explains how to parse files with extensions other than
1 | .php |
and how to dynamically render an RSS feed in Cake, with a
1 | .rss |
extension in the URL.
Chapter 11: Components and Utilities
Explains how to use components in Cake and discusses some built-in components. The Auth, Session, Cookie, and Email components are explained in more detail, and the ACL, RequestHandler, and Security components are outlined. Cake’s utility classes, like Configure, File, Folder, HTTP Socket are explained as well as how to use I18n and L10n classes in your application.
Chapter 12: Vendors
About how to use third-party scripts in your Cake app. Also how to use Zend Framework in Cake; the Akismet and PDF Zend components are specifically mentioned and built into the blog tutorial application.
Chapter 13: Plugins
About creating Cake plugins. This chapter explains how to build a calendar plugin for use in the blog.
Chapter 14: DataSources and Behaviors
Explains how to build custom DataSources and gives a walkthrough for building an XML DataSource. Also explains building behaviors, and gives a detailed walkthrough of the Tree and Containable behaviors. Outlines other built-in behaviors and explains how to build a custom behavior.
Chapter 15: Wrapping Up the Application
Discusses the final routines when completing a Cake project, like building landing pages, using the Pages controller, generating dynamic navigation, customizing the overall design, debugging the application, and running a Cake app on a remote host.
Appendix A: Installation Issues
Explains how to set up a localhost to run Cake on a Mac and PC, and how to run MySQL.
Appendix B: How CakePHP Compares with Other Frameworks
Compares Cake with other PHP frameworks: Zend Framework, CodeIgniter, and Symfony. Discusses why Cake is better :)

