If you are using the imagettfbbox() PHP function and come across this error:
imagettfbbox() [function.imagettfbbox]: Could not find/open font
The solution is to use neither relative nor absolute path to the font but to use a path like this:
$font = './arial.ttf';
So Matthew Weier O’Phinney and the rest of the Zend Framework team have started to convert all code to use ACRONYMCase casing. However, they’re not done yet and they have decided to let people decide whether to use MixedCase or ACRONYMCase. If you’d like to have your say in this matter simply visit this website and vote in the survey.
The poll will probably get closed in few days so hurry.
Personally, I have voted for ZF classes to enforce MixedCasing. I find it much more readable than ACRONYMCasing. XmlHttpRequest is prettier and more readable than XMLHTTPRequest, don’t you think?
Even though most designers write nice and valid XHTML it usually gets served to browser as text/html. In other words, what browser sees is a chaotic tag soup instead of XML (remember that while HTML was based on SGML, XHTML is based on XML or it is a reformulation of HTML in XML). The major reason why XHTML pages are getting served as text/html is infamous Internet Explorer which does not support serving XHTML the correct way (as XML). But why should your website suffer because of one browser? You can serve the tag soup to IE and serve XML to other browsers. This is called content negotiation and I’m going to show you a simple Zend Fraemwork controller plugin that will negotiate different content to different browsers:
-
class My_Controller_Plugin_NegotiateContent extends Zend_Controller_Plugin_Abstract
-
{
-
public function preDispatch(Zend_Controller_Request_Abstract $request)
-
{
-
$viewRenderer = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer');
-
$viewRenderer->initView();
-
$view = $viewRenderer->view;
-
-
// content negotiation
-
header('Vary: Accept');
-
if (false === stristr($_SERVER['HTTP_ACCEPT'], 'application/xhtml+xml')) {
-
-
header('Content-Type: text/html; charset=utf-8');
-
$view->doctype('HTML4_STRICT');
-
$view->headMeta()->appendHttpEquiv('Content-Type', 'text/html; charset=UTF-8');
-
$view->xhtmlAllowed = false;
-
-
} else {
-
-
header('Content-Type: application/xhtml+xml; charset=utf-8');
-
$view->doctype('XHTML1_STRICT');
-
$view->headMeta()->appendHttpEquiv('Content-Type', 'application/xhtml+xml; charset=UTF-8');
-
$view->xhtmlAllowed = true;
-
}
-
}
-
}
This plugin will serve HTML 4.01 Strict as text/html to IE and XHTML 1.0 Strict as application/xhtml+xml to other browsers.
The only thing you need to do is to register the plugin to the front controller during bootstrapping:
-
$frontController->registerPlugin(new My_Controller_Plugin_NegotiateContent());
And it will take care of content negotiation. The plugin will also set a boolean view variable $view->xhtmlAllowed so you can use different markups in layouts and views if needed (<img> in HTML and <img /> in XHTML).
I’ve been learning Java lately (by reading the official Java tutorials which are fantastic) and I have decided to write a simple tutorial for others who like to jump head first to developing with Java without having to read lots of theory on its architecture, OOP principles and so on.
You will need few things in order to develop Java applications:
You might also need to edit your PATH and CLASSPATH environment variables but more about that later.
Java source code is saved in files with a *.java extension. Once you compile a java file, a compiled *.class file is created. Java compiler outputs platform-neutral Java bytecode which is a bit different than *.exe executable files you are most likely used to. To run a class file you need Java Virtual Machine (JVM) installed on your machine. You should already have that if you downloaded and installed the JDK linked above.
Let’s write your first Java application then.
Open Eclipse and go to File -> New -> Java Project. Call it “Hello World”, for example. Once you have created a project, right click on “src” folder in the left part of Eclipse called Navigator and select New -> Class. Call it “HelloWorld” as well and write this code in the editor that appears next to the Navigator:
-
public class HelloWorld {
-
-
public static void main(String[] args) {
-
System.out.println("Hello World");
-
}
-
-
}
That’s it. The above Java application will simple print “Hello World” and do nothing more but you have to start somewhere
To compile and run the application, select Run -> Run As -> Java Application. The program output will appear in the bottom part of Eclipse under Console tab.
PATH & CLASSPATH
If you want to be able to compile and run Java applications from Windows command line, you will need to add/edit PATH and CLASSPATH environment variables first. In Windows 7/Vista, right click on Computer and select Properties. Go to Advanced system settings and click on Environment Variables button in bottom left corner. A window similar to the picture bellow should appear.
You need to add (or change if it already exists) CLASSPATH to your user variables and PATH to both user and system variables. Set PATH variable to a bin directory of your JDK installation (i.e. C:\Program Files (x86)\Java\jdk1.6.0_20\bin) and set CLASSPATH variable to a directory where you store compiled class files of your Java applications (you can see in the picture I set it to D:\java-projects\HelloWorld\bin). You can set multiple paths separated by semicolon. You might also want to set a system variable JAVA_HOME to a JDK installation directory (i.e. C:\Program Files (x86)\Java\jdk1.6.0_20).
Now you should be able to compile and run Java applications from Windows command line. To compile a java file, type:
javac D:\java-projects\HelloWorld\src\HelloWorld.java
To run the compiled class file, type:
java HelloWorld
If you haven’t set the CLASSPATH variable, you could use an optional cp parameter to specify it in cmd:
java -cp D:\java-projects\HelloWorld\bin HelloWorld
You can find more about PATH and CLASSPATH variables here.
You might need to integrate one of your future Zend Framework applications with phpBB forum software. The most important aspect of such integration is a dual sign up process. In other words, you want to avoid having users sign up at both the application and phpBB forum. The simplest solution is to have a single sign up form in your ZF application which upon submission will register user details in both your application and the phpBB forum. I will show you how to do that assuming both applications are on the same server.
The sign up form should have at least these fields:
- username
- password
-
$data = $form->getValues();
-
-
//
-
// here you would use the data to register user in the ZF application
-
//
-
-
// once the user has been registered at the application level,
-
// let's use the same data to register him/her at the phpBB level
-
-
$db = Zend_Registry::get('dbAdapter');
-
$phpbbUserIp = '127.0.0.1';
-
// if IP is valid include it in the INSERT query
-
$validator = new Zend_Validate_Ip();
-
if ($validator->isValid($_SERVER['REMOTE_ADDR'])) {
-
$phpbbUserIp = $_SERVER['REMOTE_ADDR'];
-
}
-
-
$sql = 'INSERT INTO phpbb_users (
-
user_type,
-
group_id,
-
user_permissions,
-
username,
-
username_clean,
-
user_password,
-
user_email,
-
user_email_hash,
-
user_timezone,
-
user_lang,
-
user_ip,
-
user_regdate,
-
user_dateformat)
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
-
$stmt = new Zend_Db_Statement_Pdo($db,
-
$sql);
-
$stmt->execute(array(0,
-
2,
-
'',
-
$data['username'],
-
$data['username'],
-
$this->_helper->PhpbbHash($data['password']),
-
$data['email'],
-
sprintf('%u', crc32(strtolower($data['email']))) . strlen($data['email']),
-
1.00,
-
'sk',
-
$phpbbUserIp,
-
time(),
-
'D M d, Y g:i a'));
-
-
$stmt = new Zend_Db_Statement_Pdo($db,
-
'SELECT user_id FROM phpbb_users WHERE user_email = ? LIMIT 1');
-
$stmt->execute(array($data['email']));
-
$userId = $stmt->fetchColumn(0);
-
-
$stmt = new Zend_Db_Statement_Pdo($db,
-
'INSERT INTO phpbb_user_group (
-
group_id,
-
user_id,
-
group_leader,
-
user_pending)
-
VALUES (?, ?, ?, ?)');
-
$stmt->execute(array(2, $userId, 0, 0));
-
$stmt->execute(array(7, $userId, 0, 0))
There are few tiny things you will probably need to modify according to your application. I chose Slovak language (‘sk’) and GMT+1 time zone (’1.00′) which is a time for central Europe. I also used a controller action helper PhpbbHash above. The helper is here:
-
<?php
-
/**
-
* PhpbbHash
-
* Just a phpbb hashing algorithm rewritten in a form of controller action helper.
-
* The reason for this is because we want users to be registered simultaneously
-
* at both the main website and the phpbb forum so we will need to add a row to
-
* phpbb_users table during registration at the main website.
-
*
-
* @author PHPBB
-
*/
-
class My_Controller_Action_Helper_PhpbbHash extends Zend_Controller_Action_Helper_Abstract
-
{
-
public function direct($password)
-
{
-
$itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
-
-
$random_state = '123456789012';
-
$random = '';
-
$count = 6;
-
-
if (($fh = @fopen('/dev/urandom', 'rb')))
-
{
-
$random = fread($fh, $count);
-
fclose($fh);
-
}
-
-
if (strlen($random) < $count)
-
{
-
$random = '';
-
-
for ($i = 0; $i < $count; $i += 16)
-
{
-
$random_state = md5('123456789012' . $random_state);
-
$random .= pack('H*', md5($random_state));
-
}
-
$random = substr($random, 0, $count);
-
}
-
-
$hash = $this->_hash_crypt_private($password, $this->_hash_gensalt_private($random, $itoa64), $itoa64);
-
-
if (strlen($hash) == 34)
-
{
-
return $hash;
-
}
-
-
return md5($password);
-
}
-
-
public function _hash_gensalt_private($input, &$itoa64, $iteration_count_log2 = 6)
-
{
-
if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31)
-
{
-
$iteration_count_log2 = 8;
-
}
-
-
$output = '$H$';
-
$output .= $itoa64[min($iteration_count_log2 + ((PHP_VERSION >= 5) ? 5 : 3), 30)];
-
$output .= $this->_hash_encode64($input, 6, $itoa64);
-
-
return $output;
-
}
-
-
public function _hash_encode64($input, $count, &$itoa64)
-
{
-
$output = '';
-
$i = 0;
-
-
do
-
{
-
$value = ord($input[$i++]);
-
$output .= $itoa64[$value & 0x3f];
-
-
if ($i < $count)
-
{
-
$value |= ord($input[$i]) << 8;
-
}
-
-
$output .= $itoa64[($value >> 6) & 0x3f];
-
-
if ($i++ >= $count)
-
{
-
break;
-
}
-
-
if ($i < $count)
-
{
-
$value |= ord($input[$i]) << 16;
-
}
-
-
$output .= $itoa64[($value >> 12) & 0x3f];
-
-
if ($i++ >= $count)
-
{
-
break;
-
}
-
-
$output .= $itoa64[($value >> 18) & 0x3f];
-
}
-
while ($i < $count);
-
-
return $output;
-
}
-
-
public function _hash_crypt_private($password, $setting, &$itoa64)
-
{
-
$output = '*';
-
-
// Check for correct hash
-
if (substr($setting, 0, 3) != '$H$')
-
{
-
return $output;
-
}
-
-
$count_log2 = strpos($itoa64, $setting[3]);
-
-
if ($count_log2 < 7 || $count_log2 > 30)
-
{
-
return $output;
-
}
-
-
$count = 1 << $count_log2;
-
$salt = substr($setting, 4, 8);
-
-
if (strlen($salt) != 8)
-
{
-
return $output;
-
}
-
-
/**
-
* We're kind of forced to use MD5 here since it's the only
-
* cryptographic primitive available in all versions of PHP
-
* currently in use. To implement our own low-level crypto
-
* in PHP would result in much worse performance and
-
* consequently in lower iteration counts and hashes that are
-
* quicker to crack (by non-PHP code).
-
*/
-
if (PHP_VERSION >= 5)
-
{
-
$hash = md5($salt . $password, true);
-
do
-
{
-
$hash = md5($hash . $password, true);
-
}
-
while (–$count);
-
}
-
else
-
{
-
$hash = pack('H*', md5($salt . $password));
-
do
-
{
-
$hash = pack('H*', md5($hash . $password));
-
}
-
while (–$count);
-
}
-
-
$output = substr($setting, 0, 12);
-
$output .= $this->_hash_encode64($hash, 16, $itoa64);
-
-
return $output;
-
}
-
}
Few comments:
- The above code is taken directly from the phpBB codebase, I only made a couple of necessary changes to make it work with Zend Framework.
- For those of you asking why didn’t I just use phpBB API and call its native functions, there is a reason for it. The phpBB code is written to be available with PHP 4. Zend Framework requires at least PHP 5.2.4. There are lots of globals and other scary stuff in the phpBB codebase which makes it impossible for its functions to be called from ZF controllers.
- If you think I should have done something differently or I completely forgot to do something, please let me know.
One of the most common questions beginner PHP programmers ask is “How to create a page in PHP?”. What they mean by that is how can they create a website where pages have URIs like these:
http://www.example.com/home http://www.example.com/about http://www.example.com/contact
Instead of URIs like these:
http://www.example.com/index.php?page=home http://www.example.com/index.php?page=about http://www.example.com/index.php?page=contact
It’s actually a trivial problem but surprisingly people still keep asking it. I guess there are not enough examples around the Web so bellow I will show you probably the most primitive front controller implementation.
What I have described above is called a front controller. To put it differently – a central script accepting all traffic and displaying different content based on GET parameters. Let’s get started!
First, I have created a simple directory structure for my front controller:
/path/to/root/
.htaccess
404.php
footer.php
header.php
index.php
/path/to/root/pages/
about.phtml
contact.phtml
home.phtml
.htaccess
RewriteEngine on RewriteRule ^([A-Za-z\-]+)/?$ index.php?page=$1 [L]
What it does is it will redirect you from http://www.example.com/page to http://www.example.com/index.php?page=page silently. This is not necessary but URIs with index.php are ugly and have no semantic meaning.
404.php
<h1>Error 404</h1>
This is just a template for a 404 Not Found page. You can put any HTML you like there.
footer.php
</body> </html>
All pages will finish with the same HTML written down in the footer.php file.
header.php
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-
<head>
-
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
-
<meta http-equiv="Content-Language" content="en-us" />
-
-
<title><?php echo $title; ?></title>
-
</head>
-
-
<body>
Similarly, all pages share the same header.
Notice the <?php echo $title; ?>. We will set a dynamic title based on a current page in the front controller.
index.php
The most important file. This is the already mentioned front controller:
-
<?php
-
-
// first we define the current working directory as a constant
-
define('BASE_PATH', getcwd());
-
-
// in case no page is defined, assume the home page
-
// otherwise, urldecode the page and make it lowercase
-
$page = (isset($_GET['page']))
-
? strtolower(urldecode($_GET['page']))
-
: 'home';
-
-
// absolute path to a page template
-
$path = BASE_PATH
-
. '/pages/'
-
. $page
-
. '.phtml';
-
-
// if a page template exists in the pages directory
-
if (false === file_exists($path)) {
-
-
// send 404 Not Found HTTP status code
-
header("HTTP/1.0 404 Not Found");
-
// set title
-
$title = 'Error 404';
-
// set template to be included
-
$page = '404.php';
-
-
} else {
-
-
// send 200 OK HTTP status code
-
header("HTTP/1.0 200 OK");
-
// set title (replace "-" chars with " " and make the first char uppercase)
-
$title = ucfirst(str_replace('-', ' ', $page));
-
// set template to be included
-
$page = $path;
-
-
}
-
-
// include the header template
-
include BASE_PATH . '/header.php';
-
-
// include the page template
-
include $page;
-
-
// and finally, include the footer template
-
include BASE_PATH . '/footer.php';
I have added some crucial comments above so you ca understand what’s going on there.
about.phtml
<h1>About</h1>
A template for http://www.example.com/about page.
contact.phtml
<h1>Contact</h1>
A template for http://www.example.com/contact page.
home.phtml
<h1>Home</h1>
A template for the home page: http://www.example.com/home or just http://www.example.com/.
Final thoughts
You can add as many new pages as you like by adding new templates to the pages directory. Just remember only alphabetical characters and the “-” character are allowed in their names.
I often hear developers from another camps such as Java or ASP.NET say that PHP is good for small or medium sized websites but it’s no good for larger web applications where more professional language should be used. I’m starting to get pretty tired of it so here is a list of some huge websites powered by PHP:
- Wikipedia
- Flickr
- Yahoo! Answers
- Yahoo! Bookmarks
- Delicious
- Digg
- Friendster
- SourceForge
- Photobucket
There are several other big websites powered by Drupal (see Drupal sites link) or other PHP based scripts/content management systems. IGN uses Zend Framework for some of its subdomains as well. Symfony powers lots of websites (some of them pretty large) as well.
I am not saying that PHP is the better option compared to other languages used for web development. All I wanted to prove by this post is that PHP can be successfully used for huge and scalable websites. How reliable, maintainable, secure or fast a website will be is mostly language-agnostic and depends on how much thought is put into design and whether developers adhere to best practices.
You can append a conditional stylesheet in a controller action like this:
-
$this->view->headLink()->appendStylesheet('/path/to/some/styles.css', 'screen', 'IE 8');
Which would produce this markup:
<!--[if IE 8]> <link href="/path/to/some/styles.css" media="screen" rel="stylesheet" type="text/css" /><![endif]-->
Adding a conditional JavaScript file is similar:
-
$this->view->headScript()->appendFile('/path/to/some/script.js', 'text/javascript', array('conditional' => 'IE'));
And that would produce markup like this:
<!--[if IE]> <script type="text/javascript" src="/path/to/some/script.js"></script><![endif]-->
It can pretty useful when you need to use IE conditional comments just for a single controller action. Otherwise, if it applies site-wide, it’s better to just put conditional stylesheets and/or scripts in a layout file.
Zend_Search_Lucene is a PHP port of a popular Java search engine Apache Lucene. It is also an important part of Zend Framework. Some say that it is too sluggish to be used in robust web applications and recommend faster alternatives such as Sphinx but that is not today’s topic. In this post I will show you a basic implementation of Zend_Search_Lucene that has worked well so far for medium websites I have worked on. There are two main tasks you will have to take care of:
- Creating an index and updating it regularly.
- Searching the index with a powerful query language.
First, let’s create a fresh search index. I know it’s already tiresome but I will use a simple blog application for my example implementation. To simplify it even further, it will only be possible to search blog posts. The posts schema looks like this:
CREATE TABLE posts ( id INT NOT NULL AUTO_INCREMENT, title VARCHAR(255) NOT NULL, body TEXT NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, user_id INT NOT NULL, INDEX (created_at), INDEX (user_id), FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE, PRIMARY KEY (id) ) ENGINE = INNODB;
Creating the search index is easy:
-
Zend_Search_Lucene::setDefaultSearchField('contents');
-
-
// create blog posts index located in /data/posts_index
-
// make sure the folder is writable
-
$index = Zend_Search_Lucene::create('data/posts_index');
-
-
// $this->_getTable() is a method that returns a model
-
// get() method of the model returns all posts from the database
-
$posts = $this->_getTable('Posts')->get();
-
// iterate through posts and build the index
-
foreach ($posts as $p) {
-
$doc = new Zend_Search_Lucene_Document();
-
$doc->addField(Zend_Search_Lucene_Field::UnIndexed('entry_id', $p->id));
-
$doc->addField(Zend_Search_Lucene_Field::Keyword('title', $p->title));
-
$doc->addField(Zend_Search_Lucene_Field::UnStored('contents', $p->body));
-
$index->addDocument($doc);
-
}
-
// commit the index
-
$index->commit();
Pretty straightforward. You can see I have used three different static methods for adding fields to the document:
- UnIndexed: unindexed and unstored (therefor unsearchable) but they are returned with search results. Unindexed fields usually store primary keys, timestamps or file paths.
- Text: indexed, stored and tokenized. Text fields are searchable and are returned with search hits. Titles, first and last names, cities and states, post codes and street names are all good candidates for keyword fields.
- UnStored: indexed and unstored – ideal for large texts.
There are more types of fields you can use (keyword, binary) but you can read about them in the documentation.
Next thing you need to do is update the index every once in a while so the search hits return up-to-date information. There are two ways to get around this problem. The most obvious is to update the index every time a new post is published or an existing post is edited. Another approach would be to set up a cron job to run every now and then and rebuild the index. Which way you choose depends on many variables such as expected index size (a very large index can have few GBs in size).
Secondly, the index is already taken care of, so let’s search it:
-
Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8());
-
-
Zend_Search_Lucene::setResultSetLimit(10);
-
-
// explode the search query to individual words
-
$words = explode(' ', urldecode($request->getParam('search_for')));
-
// start a search query and add a term for each word to it
-
$query = new Zend_Search_Lucene_Search_Query_MultiTerm();
-
foreach ($words as $w) {
-
$query->addTerm(new Zend_Search_Lucene_Index_Term($w), true);
-
}
-
-
// open and query the index
-
$index = Zend_Search_Lucene::open('data/posts_index');
-
$results = $index->find($query); // the search results
That was possibly the simplest possible example of a Lucene search query. You can, however, create very complex queries with the powerful Lucene query language. You can either build queries manually in PHP or you can use Zend_Search_Lucene methods to build them. It’s so easy a baby could do it.
To search for posts with words ‘hello’ and ‘word’ in the contents field you would write this query:
hello
To search for a post that must contain ‘hello’ and may contain ‘world’:
+hello world
To search for a post that must contain ‘hello’ in the contents field and may contain ‘world’ in the title field:
+hello title:"world"
And those were just basics. You can use boolean operators, wildcards, ranges and even perform a fuzzy search.

