www.pokeroconnor.com

SPL CSV File Iterator

August1

When using PHP’s SplFileObject for CSV file manipulation, as you will likely discover fgetcsv() also includes empty lines in the returned array.

The suggestion is to use SplFileObject::DROP_NEW_LINE or SplFileObject::SKIP_EMPTY to handle this.

However, I find I often need to use both to achieve the desired result of ignoring blank or empty lines. To achieve this, you need to use a bitmask in your setflags argument.

A value of ’7′ corresponds to both SplFileObject::DROP_NEW_LINE AND SplFileObject::SKIP_EMPTY, e.g.

$file = new SplFileObject($path);
$file->setFlags(7);

setFlags(15) would correspond to a bitmask for DROP_NEWLINE, SKIP_EMPTY, READ_CSV.

//
posted under php | No Comments »

phpmyadmin debian password protect

November1

Quick one – steps to password protect your phpmyadmin on Debian.

Good idea also to use a symlink to obfuscate the ‘phpmyadmin’ url too.

Eg. within the docroot of a site I wish to install phpmyadmin on, I would do:

cd /var/www/site

ln -s SECRET_PATH_TO_DB /usr/share/phpmyadmin/

Your phpmyadmin install is then available on site.com/SECRET_PATH_TO_DB

For the htaccess password protection, this is a very handy generator tool. The username and password you choose for the .htpasswd protection should be saved to /usr/share/phpmyadmin/.htpasswd (swap /usr/share for whatever install path you have).

//

xampp setup for mac

September15

For local development I use Xampp, which I find to be very handy. It’s really simple to and quick to setup, but there were just 2 gripes I had when setting it up that took an hour or so to fix – very annoying, so just recording them here for reference.

Firstly, the install is as easy as detailed on the official page, and the best guide I found was this excellent one.

There are a tonne of files in your /Applications/XAMPP folder, but the most usual thing is to setup a Vhost. Edit the /Applications/XAMPP/etc/extra/httpd-vhosts.conf file for that.

BUT, the first catch is you need to ensure that the following line is uncommented in the /Applications/XAMPP/etc/httpd.conf file:

Include etc/extra/httpd-xampp.conf

Gaaahhh!!!

Second gripe, your htaccess or mod_rewrite won’t work probably, so you need to make sure that AllowOverride None is changed to AllowOverride All in /Applications/XAMPP/etc/httpd.conf, e.g.

<Directory />
Options FollowSymLinks
AllowOverride All

Apart from those two minor annoyances, it was plain sailing!


//

WordPress site redirect

February13

I had the following problem lately: I added the typical rules to htaccess to redirect example.com/anything to www.example.com/anything, e.g.

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule (.*)$ http://www.example.com/$1 [L,NC,R=301]

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

But when I went to example.com I got infinite redirects…reason being I had the siteurl field in the wp_options table of the database set to http://example.com.

So, you need to change that to http://www.example.com, and also edit wp-config.php as follows:

define('WP_HOME','http://www.example.com');
define('WP_SITEURL','http://www.example.com');

That should be it - see here if not.

//

Using CURL to access certain IP of a Domain

July23

Something I needed to do recently was get the contents of a certain url of a certain domain name, for each of the IP addresses associated with that domain name. In other words, there was a domain we shall call www.test-domain.com, which was hosted on 5 different servers, with IPs 1.2.3.100 through 1.2.3.104. I wanted to automate the getting of the contents of www.test-domain.com/file-name.php on each of these servers. I could set my hosts file of course, and point at each server, blah blah blah, but this had to be automated, for sanity.

The php curl library is fully awesome, but personally I find this page to be the most useful. There are surprisingly few examples of this anywhere on the web…just try googling this topic and you get nothing at all relevant!

The solution is simple enough, but believe me it won’t be easy to find on the web – thanks to a certain work colleague for the expert advice on this! Basically you need to inject a header using CURLOPT_HTTPHEADER, and then use CURLOPT_URL with the actual IP address.

For example:

$ch = curl_init();
$headers = array("Host: www.test-domain.com");

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "http://1.2.3.100/file-name.php");

curl_exec($ch);

That’s the engine of it, a very neat little solution!

//

Zend Form Password

May16

When using Zend Form, and you have a password field and confirm password field, it is very handy to use Validation Context. It is described in the Zend documentation, just search for “My_Validate_PasswordConfirmation”. As is typical in Zend docs, the example is shit, they just give a class definition. So, here’s a brief example of I used it.

Basically what you need to do is attach the validator to both the password and the confirm password field. 

So firstly, add the class:

class My_Validate_PasswordConfirmation extends Zend_Validate_Abstract
{
const NOT_MATCH = 'notMatch';

protected $_messageTemplates = array(
self::NOT_MATCH => 'Password confirmation does not match'
);

public function isValid($value, $context = null)
{
$value = (string) $value;
$this->_setValue($value);

if (is_array($context)) {
if (isset($context['password_confirm'])
&& ($value == $context['password_confirm']))
{
return true;
}
} elseif (is_string($context) && ($value == $context)) {
return true;
}

 

 

$this->_error(self::NOT_MATCH);
return false;
}
}

Then, in your form file, use it something like follows:

class RegisterForm extends Zend_Dojo_Form

{

public function init()

{
$form = new Zend_Form();
....
....
....
$custom_pass = new MyValidatePasswordConfirmation();

...
...
$password = $form->createElement('password', 'password')
->addValidator('StringLength', false, array(1,24))
->setLabel('Choose your password:')
->addValidator('Alnum')
->addValidator($custom_pass)
->setRequired(true);

 

 

$password_confirm = $form->createElement('password', 'password_confirm')
->addValidator('StringLength', false, array(1,24))
->setLabel('Confirm your password:')
->addValidator('Alnum')
->addValidator($custom_pass)
->setRequired(true);

It’s fairly straightforward, but of course there’s very few examples I could find, this is probably the best.

//
posted under MySQL, php | 1 Comment »

Zend Form Validators

May1

I have another post with info on Zend validators, but here’s a very handy class to specify a maximum and minimum value to validate against. E.g. you have a form value that must be between 1 and 10 – Zend Validate doesn’t have a validator for that, but this class will allow you to. Just set $minimum and $maximum to your limits.


class MyValidNumericBetween extends Zend_Validate_Abstract
{
const MSG_NUMERIC = 'msgNumeric';
const MSG_MINIMUM = 'msgMinimum';
const MSG_MAXIMUM = 'msgMaximum';

public $minimum = 1;
public $maximum = 45;

protected $_messageVariables = array(
'min' => 'minimum',
'max' => 'maximum'
);

protected $_messageTemplates = array(
self::MSG_NUMERIC => "'%value%' is not numeric",
self::MSG_MINIMUM => "'%value%' must be at least '%min%'",
self::MSG_MAXIMUM => "'%value%' must be no more than '%max%'"
);

public function isValid($value)
{
$this->_setValue($value);

if (!is_numeric($value)) {
$this->_error(self::MSG_NUMERIC);
return false;
}

if ($value < $this->minimum) {
$this->_error(self::MSG_MINIMUM);
return false;
}

if ($value > $this->maximum) {
$this->_error(self::MSG_MAXIMUM);
return false;
}

 

 

 

return true;
}
}

And to use this class, assuming you have a normal form instantiated with $form = new Zend_Form(), just do:


$custom_valid8r = new MyValidNumericBetween();

$ball1 = $form->createElement('text', 'ball1')
->addValidator($custom_valid8r)
->setRequired(true);

And to set a custom message here are 2 links with good examples, some of them re-printed below.
Firstly:

$validator = new Zend_Validate_StringLength(8);

$validator->setMessage(
'The string \'%value%\' is too short; it must be at least %min% ' .
'characters',
Zend_Validate_StringLength::TOO_SHORT);

if (!$validator->isValid('word')) {
$messages = $validator->getMessages();
echo current($messages);

// "The string 'word' is too short; it must be at least 8 characters"
}


Secondly:

$validator = new Zend_Validate_StringLength(8, 12);

$validator->setMessages( array(
Zend_Validate_StringLength::TOO_SHORT =>
'The string \'%value%\' is too short',
Zend_Validate_StringLength::TOO_LONG =>
'The string \'%value%\' is too long'
));

$validator = new Zend_Validate_StringLength(8, 12);

 

 

if (!validator->isValid('word')) {
echo 'Word failed: '
. $validator->value
. '; its length is not between '
. $validator->min
. ' and '
. $validator->max
. "\n";
}

//
posted under php | 3 Comments »

Free SMS Gateways

April27

Will fill in more info later, but here’s the basics so far: got this amazing free sms Gateway, playSMS. Great install instructions, such as:

playSMS Web Interface:

1.  It is important to meet all minimum requiments above

2.  Setup a system user named ‘playsms’ to manage playSMS

    # adduser playsms

    # passwd playsms

    Note: on some Linux distributions adduser and passwd combined (Debian, Ubuntu and maybe others)

3.  On most Linux distributions actions (2) will create system user and group named ‘playsms’

    with home directory /home/playsms, but you will install playSMS in different directory

4.  Create playSMS web root, spool and log and set ownership to user www-data or web server user

    # mkdir -p /var/www/playsms

    # mkdir -p /var/spool/playsms

    # mkdir -p /var/log/playsms

    # chown -R www-data /var/www/playsms

    # chown -R www-data /var/spool/playsms

    # chown -R www-data /var/log/playsms

5.  Extract playSMS package somewhere (Usually in /usr/local/src)

    # tar -zxvf playsms-x.x.x.tar.gz -C /usr/local/src

    Note: x.x.x may vary according to the package name you’ve download

6.  Copy files and directories inside ‘web’ directory to playSMS web root and set ownership again to 

    user www-data or apache web server user

    # cd /usr/local/src/playsms-x.x.x/web

    # cp -rR * /var/www/playsms

    # chown -R www-data /var/www/playsms

    Note: assumed your web server user is www-data

7.  Setup database (import database)

    # mysqladmin -u root -p create playsms

    # mysql -u root -p playsms < /usr/local/src/playsms-x.x.x/db/playsms.sql

    Note: you dont need to use MySQL root access nor this method to setup playSMS

    database, but this is beyond our scope, you should read MySQL manual’s for custom

    installation method or howto insert SQL statements into existing database

8.  Copy config-dist.php to config.php and edit config.php

    # cd /var/www/playsms

    # cp config-dist.php config.php

    # mcedit config.php

    or 

    # vi config.php

    Note: please read and fill all required fields with coutious

9.  Enter bin directory, copy playsms, playsmsd, playsmsd.php, playsmsd_start to directory default

    # cd /usr/local/src/playsms-x.x.x/bin

    # cp playsmsd playsmsd.php playsmsd_start /usr/local/bin/

    # cp playsms /etc/default/

    Note: please note the different between playsms and playsmsd

10. Look for rc.local on /etc and its subdirectories (usualy /etc, /etc/init.d or /etc/rc.d/init.d)

    Edit rc.local and put: 

    ”/usr/local/bin/playsmsd_start” (without quotes)

    on the bottom of the file (before exit if theres exit command). This way playsmsd_start 

    will start automatically on boot. 

    Note: you need ‘root’ access to do this

11  Browse http://localhost/playsms/ and login using default administrator user

    username: admin

    password: admin

12. At this point you should be able to login to playSMS web interface and manage playSMS

There are quite a few requirements though, notably you must have PEAR DB installed, and also Lynx browser. More to follow…
To install wget, get it here, read this. For the lynx install, I used /usr/share/libtool/config.guess instead of /usr/libexec/config.guess. 
//
posted under MySQL, php | No Comments »

Server Timezones

April19

You can override your server’s timezone setting using php’s date_default_timezone_set. It is a really good idea, in my opinion, to use this at the start of your scripts that work with timestamps. Reason being, you can transfer scripts between servers and they will always work, which wouldn’t necessarily be the case if they depend on a servers timezone e.g if you are calculating a unixtime value in your script.

For example, I had an Ajax Calendar app that used Unixtime values to calculate which data to retrieve from a MySQL db. This worked fine on my dev server, whose server timezone was GMT, but when I transferred the code to my production server, which runs on E.T, it was not retrieving the correct data! This was a major pain to debug, as the code itself was fine of course, and was solved with a single line of code:

date_default_timezone_set(“GMT”);

Place this at the start of any script that has a dependency on the server timezone, and you can port your code to any server regardless of its timezone. If you don’t know your server’s timezone, date_default_timezone_get() will give it to you.

//
posted under MySQL, php | No Comments »

XPath and php starters

April17

Quick starting point on XPath and php would be the W3C Xpath pages. All the syntax info you really need to build queries are there. One other example i found useful was this one.

Then to use XPath with your php scripts, I use xpath(). A basic example would be the following (explained underneath) – say you have an <item> node which has multiple <title> children nodes, and you want to get a list of all the first children <title> nodes from that xml doc, you could do this:

<?php

$xml = simplexml_load_file(‘path/to/file/xml-file.xml’);

    $matches = $xml->xpath(“//item/title[1]“);

     foreach ($matches as $match){

echo $match[0] . “<br>”;

}

?>

So firstly we use simplexml_load_file to load our xml file. We then build the query, saying for every item element that appears anywhere in the xml file, select all title elements that are the first (the title elements that appear ‘top’ in the list) children of item elements.

//
posted under php | No Comments »
« Older Entries