PDF On The Fly And E-mail From CakePHP

I have been trying to put interesting issues on my blog instead of documenting them. This has a few advantages, such as I can share my ideas and explain how I solve issues and I can also listen to what other developers have to say.  Plus, I can help someone if they’re having a similar problem. So now, let’s get to the problem…

If you work with BLOB data type, you might need to create a .pdf file on the fly and e-mail or download it. Let’s see how I did that. The code should be self-explanatory, but I commented where it’s necessary. If you have any questions, just e-mail me or post a comment here.

FYI, I did this on CakePHP

class YourcontrollerController extends AppController {
    var $name = 'Yourcontroller';
    var $components = array('Email', 'RequestHandler');
 
    /*
     * Just need to download as PDF
     * from BLOB data type?
     */
    function get_pdf($id = null) {
        /*
         * Say, you have a table name requests
         * and it has a field name called pdf (blob type)
         * So our model will be Request
         */
        if (!empty($id)) {
            $this->Request->recursive = -1;
            $data = $this->Request->findById($id);
            $pdf = $data['Request']['pdf'];
            header('Content-Type: application/pdf');
            header('Content-Disposition: attachment; filename=Report.pdf');
            echo($pdf);
            exit;
        }
    }
 
    /*
     * We will call this method to save our
     * generated pdf file on a file direcotry,
     * lets call it "pdf"
     */
    function upload_pdf($id=null) {
        if (!empty($id)) {
            $this->Request->recursive = -1;
            $data = $this->Request->findById($id);
            $pdf = $data['Request']['pdf'];
            $file_name = date('Y-m-d') . $id . "_report.pdf";
            $path = ROOT . DS . 'app' . DS . 'webroot' . DS . 'pdf' . DS;
            $result = file_put_contents($path . $file_name, $pdf);
        }
    }
 
    /*
     * Now send the email
     */
    function email() {
        /*
         * Generate PDF and save
         */
        $this->upload_pdf($id);
        $email_to = $receiver_email_address;
        $html = 'Pdf is attached';
        $this->Email->from = '< your_email_address >\You Company';
        $this->Email->to = $email_to;
        $this->Email->subject = "Report";
        $this->Email->sendAs = 'html';
        /*
         * Attach the PDF
         */
        $file_name = date('Y-m-d') . $id . "_report.pdf";
        $this->Email->attachments = array(
            ROOT . DS . 'app' . DS . 'webroot' . DS . 'pdf' . DS . $file_name,
            $file_name => ROOT . DS . 'app' . DS . 'webroot' . DS . 'pdf' . DS . $file_name
        );
        $this->Email->send("<div>$html</div>");
    }
Posted in CakePHP, For me, PHP | Tagged | 4 Comments

Password Protected Site Without Using .htacces

Using .htaccess you can password protect a site easily. What if your host server doesn’t allow .htaccess for security reasons? No wonder, some hosting doesn’t allow .htaccess .  Here is a quick solution for that. Thanks Tapos for this reference.

 if (!isset($_SERVER['PHP_AUTH_USER'])) {
      header('WWW-Authenticate: Basic realm="Temporary Authentication"');
      header('HTTP/1.0 401 Unauthorized');
      echo 'General message for all...';
      exit;
} else {
    if ($_SERVER['PHP_AUTH_USER'] == 'temp_username' && $_SERVER['PHP_AUTH_PW'] == 'temp_password') {
       //restricted area
   }
   else {
       echo 'Try again, or press cancel';
   }
}

You can do this on the root file such as index.php. It’s simple, but there is a disadvantage for this as well, hosting that doesn’t allow .htaccess, most like they won’t allow $_SERVER array in the script. In that case there must be a server software to handle this kind of situations, so you have to contact with your host and ask them to provide support.

Posted in Apache, PHP, Web | Tagged , | Leave a comment

CakePHP: Checkbox And Radio Button Problem On Firefox

If you are using CakePHP latest version 1.3, you might have noticed these issues that checkbox doesn’t show up on latest Firefox 3.6 and both radio button and checkbox return empty value on form submit. If these weird things happened to you then you can try the following solutions.

CSS:

input[type="checkbox"] {
     float: none;
}

Checkbox:

/*By default hiddenField is true, you have to make it false to get the value on form submit. 
  And do the SAME for radio button.
*/
  echo $form->checkbox('something', array('hiddenField' => false));

Just to confirm, these issues only happen on Firefox. And these fixes doesn’t affect any other browsers.

Hope it helps someone.

Update: If you need both 0/1 value for checkbox then you can’t use $hiddenField=>false; In that case using TABLE and keeping checkbox in column works well (for firefox issue)

Posted in CakePHP, CSS, Firefox, PHP | Tagged , | Leave a comment