Thursday, September 6, 2012

TCPDF PrintScaling

Today while using the TCPDF PHP class to generate Avery 5267 labels I discovered that when I went to go print, Page Scaling was automatically set to "Fit to printable area" and I needed to have it as "None". After about 20 minutes of poking around forums I finally found something that led me to the TCPDF::setViewerPreferences() method and the PrintScaling preference. I decided the cleanest way to achieve this was to write a new method.

If you are extending TCPDF this is going to be pretty easy, do something like

class PDF extends TCPDF {

     * @author Brian Wendt (
     * @link
     * @param string $preference key of preference 
     * @param mixed $value
    function setViewerPreference($preference, $value){
        $this->viewer_preferences[$preference] = $value;


$pdf->setViewerPreference('PrintScaling', 'None');

Otherwise, you'll need to be modifying the TCPDF class directly to do this cleanly.
Add the setViewerPreference() method from above to the TCPDF class. If you aren't extending and you aren't interested in changing the TCPDF class, then you'd simply use TCPDF::setViewerPreferences() like below but it has drawbacks.

    'PrintScaling' => 'None'

(Note: using TCPFD::setViewerPreferences() overwrites other preferences you've set which is why I suggest creating the setViewerPreference() method)

TCPDF (©Nicola Asuni) is a powerful PHP class to generate PDF documents.

Wednesday, July 4, 2012

Sites Hacked? .htaccess files were code injected

(Previous Title: "Inmotion Hosting Hacked?")
At 7/3/2012 11:25:22 PM, my existing .htaccess files were injected with the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^.*(google|ask|yahoo|youtube|wikipedia|excite|altavista|msn|aol|goto|infoseek|lycos|search|bing|dogpile|facebook|twitter|live|myspace|linkedin|flickr)\.(.*)
RewriteRule ^(.*)$ [R=301,L]
ErrorDocument 400
ErrorDocument 401
ErrorDocument 403
ErrorDocument 404
ErrorDocument 500
I corrected the problem by removing the injected code from my existing files and deleting the one's that were added which I identified quickly as being 1.57 KB (1,608 bytes)

It may be related to a file called ".cache_000.php" injected via a vulnerability with Wordpress. Look in your /wp-content/uploads/ directory for .cache_000.php
For me the file had the same timestamp as some of the .htaccess files did.

I updated my title to un-point the finger at Inmotion Hosting... the problem does appear to be fixed since I deleted that file and updated Wordpress.

Update Wordpress or make the .cache_000.php file not accessible

Tuesday, May 22, 2012

PHP Merge Multidimensional Arrays

function md_array_merge(){
    $arrays = func_get_args();
    $return = array();
    foreach($arrays as $array){
        foreach($array as $key=>$val){
            if(is_array($val) && isset($return[$key])){
                $return[$key] = md_array_merge($return[$key], $val);
            } else {
                $return[$key] = $val;
    return $return;

I wrote the above function to merge 2 or more multidimensional arrays. Use is similar to array_merge().

Provided with Open Source license without warranty

Friday, November 11, 2011 and Your Email Address

Spoiler/summary: don't use the same email address for Craigslist as Facebook.

Recently a friend of mine ran into one of those random crazy people that seem to exist on the internet for the sole reason to make it worse. My pal posted a Craigslist classified ad for some collectable items and received a response shortly after by someone claiming to be interested. Everyone who's posted on Craigslist is used to the classic scammers, however stalkers and creeps are normally safely kept over at the shady dating section that you don't go to... he didn't expect what he got.

On the internet, you're best defense is anonymity to the public. Here are a few common tips:
  • Don't make your profile information public (Facebook, MySpace, etc)
  • Don't post your where-abouts
  • Don't post your phone number or email address
  • Its even been recommended that you don't share photos of yourself
Some of those recommendations are based on the paranoia instilled by the News talking about women being kidnapped because they share there entire lives on the internet. Frankly, due to the small amount of times that happens, I think Facebook is safer than going to public events or the grocery store even. The  only tip above that I follow is not posting my phone number, but I'm in the web/social industry and am kind of expected to participate. Share at your own comfort, but I'd recommend not sharing exact location and phone number.

Going back to the story of my friend, he like most people who use Craigslist used Craigslist's random email address feature (the one that looks like That's great and all, keeps people from collecting your email address for spam easily, but it only one way and only for the first email. When you reply to someone's email to you at that email address, it responds using your normal address and now that person you don't know has your email. He responded and next thing he knew, they guy turns out to be a freak. Worst of all, he has his email address which is the same one my friend uses for Facebook which just supplied the weirdo with his personal info.

Long story short, if you want to keep that distance between you and your internet (and all the creeps that come with it), keep in mind who gets your email address because it could make it very easy for them to get your other information.

Tuesday, October 18, 2011

Wordpress: You do not have sufficient permissions to access this page.

I ran around in circles last night trying to figure out the culprit to this Wordpress error
You do not have sufficient permissions to access this page.
I finally ran into this blog this morning... saving my day (more or less literally).

Wednesday, April 20, 2011


Cutting to the Chase

So I've noticed there isn't much for documentation on the use of CURLOPT_HEADERFUNCTION so I've decided to try explaining it. So lets start with an example.


class cURL {
    public $response;
    public $header;
    function cURL($url){
        $ch = curl_init($url);
        curl_setopt_array($ch, array(
            CURLOPT_RETURNTRANSFER => true, //Causes curl_exec() to return the response
            CURLOPT_HEADER         => false, //Suppress headers from returning in curl_exec()
            CURLOPT_HEADERFUNCTION => array($this, 'header_callback'),
        $this->response = curl_exec($ch);
        return $this->response;

    function header_callback($ch, $header_line){
        $this->header .= $header_line;
        return strlen($header_line);

This is a really simple example of the use of curl. Really a solid class would have a whole lot more going on, but that isn't what this example is for...

CURLOPT_HEADERFUNCTION expects the name of the function to callback when it receives the headers from the request. There are two common ways you'll see this:

  • CURLOPT_HEADERFUNCTION => 'header_callback',

  • CURLOPT_HEADERFUNCTION => array($this, 'header_callback'),

If you are familiar with call_user_func_array() you're a step ahead and can skip to the next section.
For a normal function, simply the name of the function will suffice, but to call a method you'll need to pass an array with the object and the method to call on it.
In my example, I'm calling $this->header_callback() by using array($this, 'header_callback').

The Callback Function/Method


You'll need to define the function or method you'll want to use as the callback.
There isn't a whole lot of variation between working example's I've found, that is probably because there isn't a whole lot you can do.

Parameter 1: The curl object created by curl_init. This is handy for some rare scenarios, but for the most part useless.

Parameter 2: A single line from the header. This is where some people get lost, its the GOTCHA and I'll explain it below.

Returns: The function/method MUST return the number of bytes written. Why? I'm really not sure, but it must be done or it throws errors at you till you die.


Something not clearly stated anywhere is that the header doesn't come all in one chunk, in my experience the callback is executed for each line of the header.
This makes using CURLOPT_HEADERFUNCTION outside of an object cumbersome because you'll likely end up using a global variable (which is just ugly and some old guys will call you the devil).
For OOP'ers, you'll have no problem just appending the lines to a property like in my example where I use $this->header.

Comment, Tweet, Like, Share, or whatever is hip with the kids these days

If you found this useful and better than other tutorials for CURLOPT_HEADERFUNCTION, please show me some love on the great interwebs.

Saturday, January 8, 2011

New Website is a small, quick one page business site. From first contact on Wednesday to publication tonight, the project went smoothly without a hook.

*Revised: Never say "without a hook". Client decided she wanted more for free and then demanded a refund because she didn't get new clients (within first month of new site). Fun fun