Network Merchants API Script
Have just completed a Network Merchants API class. This class utilizes the Network Merchants credit card and electronic check processing API. It also includes the customer vault API which allows merchants to securely store their customer’s credit card and bank account information in Network Merchant’s secure customer vault.
Google chart over HTTPS/SSL
The google charts API does not support the https protocol. If your website is being delivered through a secure connection, the chart will cause a SSL error. Here's a quick way to deliver google chart images over ssl.
To start off with, the chart image must be delivered from a secure connection. Google doesn't allow this plain and simple, so we need to figure out how to host it from our own site. We accomplish this by fetching the image from google using the standard API, writing it to a file, and then calling it on our own script. We basically make a image handling proxy.
Let's take a simple google chart to experiment with.
-
$chart_image = 'http://chart.apis.google.com/chart?chs=500x50&chf=bg,s,ffffff&cht=ls&chd=t:23.52,20.58,26.47,23.52,23.52,23.52,100.00,0.00,23.52,23.52,27.94,20.58,23.52&chco=0066ff';
Next we need to make a function to fetch and save the google chart locally. It will check the chart against the local copy and save it if the chart doesn't exist, or the image has changed. This way we aren't re-writing the same chart on every request, but if the chart changes, it will be updated appropriately.
-
{
-
}
-
-
return $file_name;
-
}
Lastly we tie it all together so that it is usable in our application. Im using this within a class, but this could just be used as a function as well. Your image directory will need to be writable for this to work.
-
public function doSomething()
-
{
-
-
$local_image_path = '/path/to/images/charts/';
-
$image_name = 'some_chart_image.png';
-
$chart_url = 'http://chart.apis.google.com/chart?chs=500x50&chf=bg,s,ffffff&cht=ls&chd=t:23.52,20.58,26.47,23.52,23.52,23.52,100.00,0.00,23.52,23.52,27.94,20.58,23.52&chco=0066ff';
-
-
$image = self::saveImage($chart_url ,$local_image_path,$image_name);
-
-
}
You'll need to implement your own error handling, and adjust this to meet the paths and specifics of your server, but the image can now be called from:
<img src="/images/charts/some_chart_image.png" alt="" />
If you need help creating your base chart image, this tool is a great place to start.
Firefox 3 broken images breaking SSL
After battling a SSL error: "Warning contains unauthenticated content" in Firefox 3.5 for the past few days, I finally figured out what the problem was.
Unlike Internet Explorer and older Firefox versions, Firefox 3.5 gives this error if there are any missing images on a webpage. Meaning, that if an image link or css reference is to a non-existent image, Firefox warns that the page is not secure. To my knowledge, Firefox versions previous to 3.something, did not behave like this. Internet explorer, Google Chrome, Safari, and every other non-Firefox browser that I tested don't care about broken images. Not to say that Firefox is wrong, but this made the problem more difficult to diagnose because it couldn't be reproduced in another program.
The difficulty in diagnosing this, is that background images that don't load also don't show up in the page info section on Firefox. Firebug didn't provide any immediately useful information, and there was no documentation that I could find regarding this situation.

After several hours of searching, I realized that there was a small broken background image. Deleting or correcting the image path immediately corrected the certificate error.
Anyway, if anyone has been pulling out their hair trying to diagnose a mystery ssl error, and this is a possibility, definitely look into your image paths.
Symfony 1.2 redirect specific modules and actions to HTTPS (SSL)
Post Symfony 1.1, the sfSslRequirementPlugin will no longer work.
Having needed a way to force a SSL connection for certain pages, I modified a few scripts that I found online, and created a very simple filter to handle this. This was inspired by this script, and the unacceptably poor example in the Symfony 1.2 book.
To start off with, we need to modify our app.yml file to specify what modules and/or actions need to be secure. Leave the action completely blank if you want the entire module secure. Also change ignore_non_secure to true if you don't care if non specified pages are server over a ssl connection. Basically, from the app.yml below, setting this to false, will redirect any module/action to the non-secure version if it is not specifically defined under secure_actions. Setting it to true will allow a user to request any page over https, even if it is not listed in app.yml. Let me know if this is confusing in any way.
-
//app.yml
-
all:
-
ssl:
-
ignore_non_secure: false
-
secure_actions:
-
- { module: shopping_cart}
-
- { module: services action: apply}
Next we add this filter. Save this under MyProject/apps/MyApp/lib/sfSslFilter.php
-
<?php
-
-
class sslFilter extends sfFilter
-
{
-
/**
-
* Execute filter
-
*
-
* @param FilterChain $filterChain The symfony filter chain
-
*/
-
public function execute ($filterChain)
-
{
-
-
$context = $this->getContext();
-
$request = $context->getRequest();
-
-
$ssl_actions = sfConfig::get('app_ssl_secure_actions');
-
$allow_ssl = sfConfig::get('app_ssl_ignore_non_secure');
-
-
/*
-
* Uncomment For Debugging
-
*
-
* echo '<pre>';
-
* print_r($ssl_actions);
-
* echo '</pre>';
-
* exit();
-
*
-
*/
-
-
if (!$request->isSecure())
-
{
-
//Redirect to the Secure Url
-
//If the module and/or action match $ssl_actions set in app.yml
-
foreach($ssl_actions as $action)
-
{
-
-
if($action['module'] == $context->getModuleName() && !$action['action']){
-
-
//The entire module needs to be secure
-
//Redired no matter what the action is.
-
-
return $context->getController()->redirect($secure_url, 0 , 301);
-
-
-
} else if($action['module'] == $context->getModuleName() && $action['action'] == $context->getActionName())
-
{
-
-
//Redirect if the module and action need to be secure
-
-
return $context->getController()->redirect($secure_url, 0 , 301);
-
}
-
}
-
-
} else if($request->isSecure() && !$allow_ssl)
-
{
-
$redirect = true;
-
-
//Redirect to the Non-Secure Url
-
//If the module and/or action are not in $ssl_actions set in app.yml
-
foreach($ssl_actions as $action)
-
{
-
if(($action['module'] == $context->getModuleName() && !$action['action']) || ($action['module'] == $context->getModuleName() && $action['action'] == $context->getActionName()))
-
{
-
$redirect = false;
-
}
-
}
-
-
if($redirect)
-
{
-
return $context->getController()->redirect($non_secure_url, 0 , 301);
-
}
-
}
-
-
$filterChain->execute();
-
-
}
-
}
Finally, add to the MyProject/apps/MyApp/config/filters.yml file:
-
sslFilter:
-
class: sslFilter
Clear the cache (symfony cc), and there you have it. Let me know if you have a better or different way of dealing with this on a per-module or per-action basis. Hopefully sfSslRequirementPlugin will get ported to work with Symfony 1.2, as the method above will not alter routes on your application.
Additionally, I specifically used 301 redirects to make this more search engine friendly, in case Google or another bot gets on a ssl page. This will help prevent getting duplicate pages indexed due to http and https versions of the same page.
20 Great non-PHP Tools for PHP Developers
By nature I always strive to find more efficient, and better ways to perform tasks. There are a number of development tools that I use that really help me develop better applications in a reduced amount of time. These are the tools I use every day for web development.
Continue reading...
PHP Magic __get, __set Methods, and Retaining Private and Protected Properties
I have been making an integration with a complex API with hundreds of potential user provided variables, necessitating me use of PHP's Magic __get and __set methods.
Unfortunately, by using these methods, PHP's restriction on private and protected properties is bypassed, making all properties public. This is completely unacceptable from my coding perspective.
This class model overrides the magic __get and __set's ability to alter and access private and protected properties. Public properties are unaffected. This script also allows the class to set and access private and protected properties.
-
<?php
-
-
class setter_getter_respect
-
{
-
-
private $current_page;
-
-
public function __construct()
-
{
-
-
$class = new ReflectionClass(__CLASS__);
-
$this->current_page = $class->getFileName();
-
-
-
foreach($class_properties as $class_property_name => $property_value)
-
{
-
$prop = new ReflectionProperty(__CLASS__, $class_property_name);
-
-
if($prop->isPrivate() || $prop->isProtected())
-
{
-
$this->private_properties[$prop->getName()] = ($prop->isPrivate()) ? 'private' : 'protected';
-
}
-
}
-
}
-
-
public function __set($var, $val)
-
{
-
-
if(($requesting_page[0]['file'] != $this->current_page) && (array_key_exists($var,$this->private_properties)))
-
{
-
-
trigger_error("Cannot access ".$this->private_properties[$var]." property ".__CLASS__."::".$var." in ".$requesting_page[0]['file']."on line ". $requesting_page[0]['line'],E_USER_ERROR);
-
-
}
-
-
$this->$var = $val;
-
}
-
-
public function __get($var)
-
{
-
-
-
-
if(($requesting_page[0]['file'] != $this->current_page) && (array_key_exists($var,$this->private_properties)))
-
{
-
-
trigger_error("Cannot access ".$this->private_properties[$var]." property ".__CLASS__."::".$var." in ".$requesting_page[0]['file']."on line ". $requesting_page[0]['line'],E_USER_ERROR);
-
-
}
-
-
return $this->$var;
-
-
} else {
-
-
throw new Exception("Required property [" . $var . "] has not been set!");
-
-
}
-
}
-
}
-
-
?>
Extended classes will not have access to __get or __set protected properties. I will alter this snippet when I find a suitable method of handling extended classes.
I'm hoping that php alters the way it handles private and protected properties through the magic methods but until then, this is a way to semi-preserve private and protected properties.
US States Snippet and SQL Dump
Here's some US states snippets. Included are php arrays, and a MySQL states dump...
PHP – Script benchmark / bottleneck debugging snippet
Here's a really simple function that I use for finding bottlenecks in php scripts. You can add any number of steps to the the script using the microtime() function, and this function shows the execution time of each step.
-
/**
-
* Benchmark a php script
-
*
-
* @param array $time_sample
-
* @return string HTML
-
*/
-
{
-
$output = '';
-
-
for($i=0;$i<$steps;$i++)
-
{
-
if($i<($steps-1))
-
{
-
$output .= '<p>Time '. ($i+1) .': '. number_format(($time_sample[$i+1] - $time_sample[$i]),6,'.','') .' seconds.</p>';
-
}
-
}
-
-
$output .= '<p>Total time: '. number_format(($time_sample[$steps-1] - $time_sample[0]),6,'.','') .' seconds.</p>';
-
-
return $output;
-
}
This is a simple example using sleep() to demonstrate the output.
The script outputs:
Time 1: 1.001833 seconds.
Time 2: 2.001427 seconds.
Time 3: 3.001124 seconds.
Time 4: 1.001720 seconds.
Total time: 7.006104 seconds.
It's a good idea to comment each time you record a microtime so that you know which section of script took that amount of time.
PHP – Random string generator snippet
This is a little function that I use all the time to generate random strings. There are 3 options for random strings with this: Alpha, Alpha-numeric, and Alpha-numeric with symbols. This is important because sometimes it's a good idea not to allow special characters in a php string. However, the special characters are great if you need to create a key or initialization vector for 2 way encryption.
This can be used to generate random passwords or keys or just about anything else that needs a random string. You can also throw this directly into a class and use it as a static method.
-
/**
-
* Generate a random string
-
*
-
* @param int $length
-
* @param int $mode 1 = Alpha, 2 = Alpha-numeric, 3 = Alpha-numeric with symbols
-
* @param boolian $char_set Set true for Upper and Lower case letters
-
* @return string
-
*/
-
function random_string($length=16,$mode=1,$char_set=false)
-
{
-
$string = '';
-
$possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
-
-
if($char_set) {
-
-
-
}
-
-
switch($mode) {
-
-
case 3:
-
-
$possible .= '`~!@#$%^&*()_-+=|}]{[":;<,>.?/';
-
-
case 2:
-
-
$possible .= '0123456789';
-
break;
-
-
}
-
-
for($i=1;$i<$length;$i++) {
-
$string .= $char;
-
}
-
-
return $string;
-
}
Examples:
-
//WQTISVJVMWSEFXEIQISJPCBENFEHQAN
-
//cZhVGHJb0PqJIk3
-
//=,:UT__GN[ST>GH
25 Point Basic MySQL Setup/Optimization Checklist
Daily I run into new web programmers that are using PHP and MySQL to create their blogs and websites. I created this checklist as a guide for new and experienced to make sure they are covering the basics of a MySQL server setup.
This guide is by no means all inclusive, but should help to cover some of the major gaps in knowledge and commonly overlooked fundamentals that I run into on a daily basis.
The checklist is separated into 5 equal sections: Server Setup, Schema Design, Table Design, Index Optimization, Query Optimization, and a 6th Bonus Tips section.