Browsing articles in "PHP"

WordPress 3.2 upgrade, WYSIWYG editor no longer loads? Now requires JSON extension

An issue I encountered recently doing a WordPress upgrade from 3.1 to 3.2, is that afterwards everything appeared to work with the exception of the editor for Posts and Pages. After working through the checklist for the manual install and checking each plugin individually, I took a look at the PHP error log and noticed that PHP didn’t have the JSON extension bundled.

If you get an error mentioning that json_encode() is missing, it will be because PHP wasn’t compiled with it and WordPress now uses it. If you use SuSE (which this particular server is) you need to install the php5_json package using a command similar to:

zypper install php5_json

Otherwise you will need to see if your distro has a JSON-specific package for PHP, or you may have to compile it from source.

NOTE: PHP usually has the JSON extension compiled into it by default since 5.2, so this is an unusual case

Access the non-direct View Helper methods in Zend Framework

Something that was temporarily puzzling me so I thought I would post about it. As in most examples to access the direct() method of a View Helper, you simply call the class name on the View Object:

$view->baseUrl();

But if you want to access any of the methods other than direct() you’ve got to get the Helper first, as unlike most Action Helpers they don’t implement a fluent interface because they return bits of the view instead (usually HTML)

$this->getHelper('BaseUrl')->setBaseUrl();

Configuring Zend Server or Zend Server CE on Mac OS X

Unlike Windows which doesn’t come with Apache by default, or distributions of Linux which (usually) have their own package management systems, Mac OS X comes with an outdated version of Apache which isn’t handled by package management. This means it’s a pain to either update or remove it. Any Mac PHP developer will probably have tried MAMP which gives you a relatively pain-free installation, but for anything complex it can be a bit tricky due to the way it’s self-contained. As a Zend Framework developer I use Zend Server and Zend Server CE at work now and figured now that I’ve got to do some work at home I should look to get it installed and configured…

Just to check that everything is working OK to start with, go to http://localhost/ and you should see the default Mac OS X homepage which will be located in your Sites directory.

Installation

Download Zend Server CE from Zend and install as usual.

Configuration

Disable the default Apache installation

Make sure you stop the default Mac OS X version of Apache before you start the new Zend one, or it’ll conflict.

apachectl stop

Replace the default apachectl with Zend Server’s apachectl

Easiest way to do this (without uninstalling anything) is to replace the default apachectl with Zend Server’s apachectl, that way it will just appear to be launching Apache as per usual when OS X starts up.

Firstly move apachectl to a safe place, such as your home directory:

sudo mv /usr/sbin/apachectl /Users/yourusername/

Then create a symbolic link to Zend Server’s apachectl:

sudo ln -s /usr/local/zend/apache2/bin/apachectl /usr/sbin/

Start Zend Server’s Apache:

sudo apachectl start

You should then see:

/usr/sbin/apachectl start [OK]

This will indicate that you are running Zend Server’s Apache as opposed to Mac OS X’s Apache (as the latter doesn’t output to the terminal when you start/stop it) although if you go to http://localhost/ you will notice that you will get a 404.

Configure Apache to use port 80

I’m assuming to avoid any conflict with the default Apache installation that Zend Studio’s Apache comes preconfigured to use port 10088. To change this simply edit the configuration:

sudo vi /usr/local/zend/apache2/conf/httpd.conf

… or if you dont use Vi, use another text editor such as Nano:

sudo nano /usr/local/zend/apache2/conf/httpd.conf

Look for the line Listen 10088, then either comment it out and add a new line or edit the port to be port 80.

#Listen 10088
Listen 80

You may also need to change the DocumentRoot and first Directory parameters to your webroot (/Users/username/Sites for example)

Save httpd.conf (Ctrl + x in Nano to exit then y to save) and restart Apache:

sudo apachectl restart

If you go to http://localhost/ in your browser you should now see the default Mac OS X homepage, as httpd.conf should already be setup to point to your Sites directory by default.

If you get any complaints about the FQDN when you start Apache, such as:

httpd: Could not reliably determine the server's fully qualified domain name, \
using iMac.local for ServerName

Then you need to explicitely set the name of the domain to be used (in this case, localhost)

#ServerName www.example.com:10088
ServerName localhost:80

Configure Zend Framework

sudo ln -s /usr/local/zend/share/ZendFramework/bin/zf.sh ./zf

Configure Pear

sudo ln -s /usr/local/zend/bin/pear ./
sudo pear upgrade pear

Configure MySQL

You can create symbolic links for the typical MySQL commands if you wish (although you can change the include paths for the shell if you so desire)

sudo ln -s /usr/local/zend/mysql/bin/mysql ./
sudo ln -s /usr/local/zend/mysql/bin/mysqladmin ./
sudo vi /usr/local/zend/mysql/data/my.cnf

You will then need to change where it looks for the socket:

#socket = /usr/local/zend/mysql/tmp/mysql.sock
socket = /tmp/mysql.sock

If this doesn’t work (as it won’t for some programs) you will need to create a symbolic link for the socket:

sudo ln /usr/local/zend/mysql/tmp/mysql.sock /tmp/mysql.sock

If there isn’t a socket file there…

Configure Xdebug

First symlink PECL

sudo ln /usr/local/zend/bin/pecl /usr/sbin/pecl

Then you will need to install Xdebug with it

sudo pecl update-channels

sudo pecl install xdebug

Errors installing via PECL

You might get an error with autoconf (I have Xcode 4.3 installed which might have something to do with it)

Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

ERROR: `phpize’ failed

If so you’ll need to either compile it yourself, or grab a pre-compiled version. Luckily the nice folks at ActiveState provide pre-compiled extensions. Grab the latest version from their site and copy the extension in the directory ’5.3′ (as in PHP 5.3)

cp xdebug.so /usr/local/zend/lib/php_extensions/

Configure Xdebug as a Zend Extension

You cannot enable Xdebug as a normal PHP extension as it won’t work properly; you need to enable it as a Zend extension. This means you’ll need to add the following line to php.ini

zend_extension=/usr/local/zend/lib/php_extensions/xdebug.so

However this won’t work on it’s own as you will need to turn off the debugger that ships with Zend Server. The great instructions from the tailored installation instructions from Xdebug state “Open /usr/local/zend/etc/conf.d/debugger.ini and put a ; in front of the line that says zend_extension_manager.dir.debugger= so that it says ;zend_extension_manager.dir.debugger=”

If you have any problems configuring it, the Xdebug tailored instructions are a great help, just copy the HTML source of a phpinfo() script and it tells you what you need to do.

Configure phpMyAdmin

If you want to use Apache instead of Lighttp, you will have to create a Directory entry and Alias for phpMyAdmin:

sudo vi /usr/local/zend/apache2/conf/httpd.conf
Alias /phpMyAdmin /usr/local/zend/share/phpmyadmin
<Directory "/usr/local/zend/share/phpmyadmin">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

Install PHPUnit

sudo pear channel-discover pear.phpunit.de
sudo pear channel-discover components.ez.no
sudo pear channel-discover pear.symfony-project.com
sudo pear update-channels
sudo pear install --alldeps phpunit/PHPUnit
sudo ln -s /usr/local/zend/bin/phpunit ./
sudo pear install --alldeps phpunit/PHPUnit

Installing PHPUnit with Zend Server (CE) on Windows using PEAR

After a few hours of trying to figure out what was failing each time, I think I’ve finally found the solution (which is a combination of various other tidbits of information I’ve picked up from other blogs)

Firstly, assuming you’ve got a stock installation of Zend Server or Zend Server CE, it will be installed at:

C:\Program Files\Zend\ZendServer\

Therefore the PEAR setup batch file will be located at:

C:\Program Files\Zend\ZendServer\bin\go-pear.bat

If you run this from the command line (go to the bin directory then type “go-pear”) it will prompt you with the setup configuration for PEAR. The prompt will look something like this:

Are you installing a system-wide PEAR or a local copy?(system|local) [system] : system

Below is a suggested file layout for your new PEAR installation.  Tochange individual locations, type the number in front of thedirectory.  Type 'all' to change all of them or simply press Enter toaccept these locations.

The issue that was causing my installation in particular to fail (which is not an issue with say XAMPP or other bundles) is that Zend Server installs by default to your Program Files directory, which has a space in it. PEAR doesn’t play well with spaces, so you will need to change your Temporary directory for processing and Temporary directory for downloads to something without a space such as C:\Temp or C:\tmp

Type “all” to change them all and simply enter replacement paths for the temporary directories, then hit enter when re-prompted. This should install PEAR and create a Registry file for you to merge (it will say where it created it, which should be in the same directory you just ran go-pear.bat in)

Type “pear” at the command line to see if it installed properly. Once it’s working OK type the following:

pear channel-discover pear.phpunit.depear channel-discover components.ez.nopear channel-discover pear.symfony-project.compear update-channels

This will add the channels that contain the packages required by PHPUnit (you’ll need all 3) then updates all available channels

pear upgrade pear

This will upgrade PEAR in case it is not up to date.

pear install --alldeps phpunit/PHPUnit

This will install PHPUnit from the correct channel with all the dependencies it requires (which is what the Symfony and eZ Component channels are needed for)

The problems I encountered which aren’t covered elsewhere is the fact that PEAR will fail trying to download and install packages if you don’t change the temporary directories, and that without the additional channels (not just the PHPUnit channel) it doesn’t seem to be able to work out where the dependencies are even though it knows they are required.

Zend_Auth_Adapter_Ldap “Adapter options array not in array”

Whilst trying to setup LDAP authentication for the first time in Zend Framework, I can across this little quirk when passing parameters to Zend_Auth_Adapter_Ldap, namely that I wasn’t passing an array even though it was an array (which I checked using Zend_Debug)

Basically Zend Framework expects you to have multiple LDAP servers (even if you have alternative failover in place on a single subdomain, for example) so if you only have a single LDAP server specified in the array, it needs to be nested in an array.

$config = new Zend_Config_Ini(APPLICATION_PATH.’/configs/application.ini’, APPLICATION_ENV);
$options[] = $config->ldap->toArray();

http://forums.zend.com/viewtopic.php?f=69&t=5898

Configure Imagemagick with PHP on Windows

Since you can’t simply get Imagemagick from a respository and have the legwork done for you, this is a great guide to follow. Once I’ve got a spare minute I will revise this for Zend Server (CE) as you can skip a few steps with that.

http://www.elxsy.com/2009/07/installing-imagemagick-on-windows-and-using-with-php-imagick/

Get column names from a Zend_Db_Table object

If you need to get access to the column names of a Zend_Db_Table object (filtering a list of column names so that you discard any that don’t match up, for example) then as found on StackOverflow you can do the following:

$table->info(Zend_Db_Table_Abstract::COLS);

Which of course works from within the object itself, so you could write a method such as:

public function getCols() {
        return $this->info(Zend_Db_Table_Abstract::COLS);
}

However unlike the documentation says, this does not appear to work…

//return $this->_cols;

Drupal migration SQL fixes

If you are migrating from a single instance of Drupal to a multi-site installation, you may encounter a problem or two along the way in terms of certain things breaking as they aren’t where they used to be. Since Drupal is almost solely reliant upon the database during it’s bootstrap process, more often than not if something is misconfigured you can end up with the white screen of death (which neither PHP or Apache will be able to pick up)

Theme fixing… (http://drupal.org/node/200774)

UPDATE system SET status=1 WHERE name = ‘garland’;

UPDATE variable SET value=’s:7:”garland”‘ WHERE name = ‘theme_default’;
TRUNCATE cache;

Files fixing… (http://flevour.net/blog/drupal-changing-files-directory-configuration-setting)

UPDATE `files` SET `filepath` = REPLACE(`filepath`, “files/”, “sites/SITEDOMAIN/files/”);

Lack of PHP default timezone

It is not safe to rely on the system's timezone settings.

If you see this thrown at the E_STRICT level by PHP, then you need to let PHP know which timezone the server is currently running in, by either running the following function in PHP:

date_default_timezone_set('Europe/London');

… where Europe/London is a supported timezone or setting it in php.ini:

date.timezone = "Europe/London"
Pages:«12