Class to parse email content recursively

emails, parsing, zend Comments Off on Class to parse email content recursively

I needed to parse emails coming to our system, and it turned out that there is no way to parse it recursively, since Multi-part emails can be nested one into another unlimited number of times.

So here is the class that parses and returns the first part of email, no matter how deep the recursion is. You are welcome!

<?php

/**
 * a service to parse an Multipart/Plain emails
 * supports base64 and quoted-printable formats
 * supports recursively nested parts (the most tricky part)
 * @author Alexander Skakunov
 * @since 2014-04-04
 */
class Service_Mail_Parser {

    /**
     * @param Zend_Mail_Message $message
     * @return string $content
     */
    public function parse(Zend_Mail_Message $message) {
        return $this->_getContent($message);
    }

    /**
     * gets content from the email object
     * @param Zend_Mail_Message $message
     * @return string $content
     * @throws Exception
     */
    protected function _getContent(Zend_Mail_Message $message) {
        if ($message->isMultipart()) {
            $content = $this->_getMultipartContent($message);
        }
        else {
            $content = $message->getContent();
        }

        if (empty($content)) {
            throw new Exception('Content is not parsable');
        }
        return $content;
    }

    /**
     * gets content from the multipart email
     * @param Zend_Mail_Message $message
     * @return string $content
     */
    protected function _getMultipartContent(Zend_Mail_Message $message) {
        $plainPart = $this->_getFirstPlainPart($message);
        if (empty($plainPart)) {
            return;
        }

        switch ($plainPart->contentTransferEncoding) {
            case 'base64':
                return base64_decode($plainPart->getContent());
            case 'quoted-printable':
                return quoted_printable_decode($plainPart->getContent());
        }
    }

    /**
     * recursively (!) gets the first plain content from the multipart email
     * i.e. the one that is not multipart
     * @param Zend_Mail_Message $message
     * @return Zend_Mail_Message $part
     */
    protected function _getFirstPlainPart(Zend_Mail_Part $message) {
        if (!$message->isMultipart()) {
            return $message;
        }
        $part = $message->getPart(1);
        return $this->_getFirstPlainPart($part);       
    }
}

This is how to use it:

$service = new Service_Mail_Parser;
$content = $service->parse($message);

Specifying CSS class of a Zend_Navigation li element

delete your code, development, php, zend Comments Off on Specifying CSS class of a Zend_Navigation li element

Zend Navigation is a really nice tool to handle menus.

You just specify an array of your menu items:

$pages = array(
    array(
        'label'      => 'Privacy Statement',
        'controller' => 'terms',
        'action'     => 'privacy-statement',
        'class'      => 'firstNav',
    ),
    array(
        'label'      => 'General Terms of Use',
        'controller' => 'terms',
        'action'     => 'general-terms-of-use',
    ),
);

and initialize the navigation object with it — and it works:

$container = new Zend_Navigation($pages);
$this->view->navigation($container)->menu()
    ->setUlClass('nav')
    ->setActiveClass('active');

Result is the following:

<ul class="nav">
    <li class="active">
        <a class="firstNav" href="/terms/privacy-statement">Privacy Statement</a>
    </li>
    <li>
        <a href="/terms/general-terms-of-use">General Terms of Use</a>
    </li>
</ul>

There are options to specify the CSS class of the whole UL tag. If you specify CSS of a menu item (“firstNav” in my example), it’s added to to the A tag, not the LI tag as required by sliced design I have.

Googling shows that people are trying to work-around that by jQuery fixes.

It seems there is a proper way to solve this; you just need to add this option:

$this->view->navigation()->menu()->addPageClassToLi(true);

Enjoy!

Zend View Helpers inheritance

development, php, plugin, zend Comments Off on Zend View Helpers inheritance

It’s handy to use View Helpers in Zend FW driven project. For example, you want to make an in-place tracker (e.g. Google Analytics) — you create a helper class My_View_Helper_Tracker inherited from Zend_View_Helper, Zend finds its automatically and then you are free to use your helper method:

echo $this->tracker( $trackerID );

The question is what you gonna do if you want a base class for a family of trackers?

It’s not so obvoius due to naming conventions.

Let’s say you want 2 kinds of trackers: Google Analytics and Euroads.

1. You create such files structure:


library
 - My
   - View 
    - Helper
      - Tracker
        Abstract.php
        - Google
          Page.php
        - Euroads
          Owner.php
          Guest.php

2. You name your classes as:

  • My_View_Helper_Tracker_Euroads_Guest (extends My_View_Helper_Tracker_Abstract)
  • … so on …

3. Class methods are:

public function tracker_euroads_guest(...) ...

4. And the trickiest part: the helper call:

echo $this->tracker_Euroads_Guest

Currency exchange in your application

db, development, ideas, php, zend 1 Comment »

That’s easy, you need 2 things:

  1. Fresh currencies exchange rates
  2. Some way to excange amount from one currency to another.

This how I did it: get values from European Central Bank (ECB) for step #1 and wrote MySQL user defined function for step #2.

Here is how to export currencies rates from ECB (EUR is a base currency, and I add self rate as 1:1). First I create such database table:

CREATE TABLE IF NOT EXISTS `currency` (
  `code` char(3) NOT NULL DEFAULT '',
  `rate` decimal(10,5) NOT NULL COMMENT 'Rate to EUR got from www.ecb.int',
  PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Currency rates (regularly updated)';

Now let’s fill it with rates:

<?php

class CurrencyController extends Controller_Ajax_Action {

  public function importAction() {
  
    $db = Zend_Registry::get('db');
    $db->beginTransaction();
    
    $url = 'http://www.ecb.int/stats/eurofxref/eurofxref.zip?1c7a343768baab4322620e3498553b5a';
    try {
      $contents = file_get_contents($url);
      $contents = archive::unzip($contents);
      $contents = explode("\n", $contents);
      
      $names = explode(',', $contents[0]);
      $rates = explode(',', $contents[1]);
      
      $names[] = 'EUR';
      $rates[] = 1;
    
      for ($i = 1; $i < sizeof($names); $i++) {
        if (!(float) $rates[$i]) continue;
        $db->query( sprintf('INSERT INTO `currency`(`code`, `rate`)
              VALUES ("%s", %10.5f)
              ON DUPLICATE KEY UPDATE `rate`=VALUES(`rate`)', 
             trim( $names[$i] ), 
             trim( $rates[$i] )
        ) );
      }
      
      $db->commit();
    } catch ( Exception $O_o ) {
      error_log( $O_o->getMessage() );
      $db->rollback();
    }
    
  }
}

Now let’s create a SQL function for handy converts. Create a udf.sql file and add this in it:

DELIMITER //

DROP  FUNCTION IF EXISTS EXCHANGE;
CREATE FUNCTION EXCHANGE( amount DOUBLE, cFrom CHAR(3), cTo CHAR(3) ) RETURNS DOUBLE READS SQL DATA DETERMINISTIC
    COMMENT 'converts money amount from one currency to another'
BEGIN
    DECLARE rateFrom DOUBLE DEFAULT 0;
    DECLARE rateTo DOUBLE DEFAULT 0;
    
    
    SELECT `rate` INTO rateFrom FROM `currency` WHERE `code` = cFrom;
    SELECT `rate` INTO rateTo   FROM `currency` WHERE `code` = cTo;
    
    IF ISNULL( rateFrom ) OR ISNULL( rateTo ) THEN
        RETURN NULL;
    END IF;
    
    RETURN amount * rateTo / rateFrom;
END; //

DELIMITER ;

and run this command in your shell:

mysql --user=USER --password=PASS DATABASE < udf.sql

This is how you can use this function — how to convert 10 US dollars to Canadian dollars:

SELECT EXCHANGE( 10, 'USD', 'CAD')

which results in $10 = 10.93 Canadian dollars.

P.S. Consider adding the currency export action call to your cron scripts.

P.P.S. A function to unzip the data file can be got at php.net

Ajax controller in Zend project

ideas, php, zend 1 Comment »

I want to share a couple of features I use to handle AJAX requests in projects based on Zend Framework.

1. AJAX request handling

What: some parts of your application can be not loaded if currect request is AJAX.

Why: you don’t need views, templates, some routes — so you can add an AJAX check in your Initializer or bootstrap file and avoid loading not necessary things.

How: Zend Request object has a isXmlHttpRequest method to find out whether it’s AJAX request or not. It’s based on ‘X-Requested-With‘ header, which is sent by jQuery, Prototype, Scriptaculous, YUI and MochiKit frameworks.

2. AJAX Controller

Most AJAX controller’s methods I saw had an exit() inside to not to output Zend’s template — it is a work-around. The proper way to do so is to tell to Zend not to load anything. One step forward is to create an abstract Controller class and inherit all you AJAX classes from it:

/library/Koodix/Controller/Ajax/Action.php:

<?php

require_once 'Zend/Controller/Action.php';

abstract class Koodix_Controller_Ajax_Action 
  extends Zend_Controller_Action 
{
    public function init() {
        //disable the standard layout output
        $this->_helper->layout()->disableLayout();  
        $this->_helper->viewRenderer->setNoRender();
    }

    public function postDispatch() {
        //envelope and output json field
            if( !empty( $this->json ) ) {
                echo json_encode( $this->json );
            }
        }
}

/application/modules/default/controllers/AjaxController.php:

<?php

class AjaxController extends Koodix_Controller_Ajax_Action 
{
 // bla-bla-bla

Take a look at postDispatch method — idea behind it is to convert to JSON and output anything that is set to json field of your controller. If you want to send JSON data in special header (and not in body, like it’s done in my example), you can do it in this method.

Flash uploader and HTTP password protection

development, ideas, zend Comments Off on Flash uploader and HTTP password protection

You are working on a project and you want to protect the beta version with password so that only allowed people (beta testers) could access it.

You decide not to invent the wheel and to use the standard HTTP authentication.

First idea is to use your Apache web server to do this, so you write something like that in .htaccess file:

AuthName "Private zone"
AuthType Basic
AuthUserFile /path/to/.htpasswd
require valid-use

This solution is simple and that’s why good.

A problem comes on stage when a Flash file uploader is added to your project – usually it cannot “login” to your site, i.e. users are not able to use the Flash file uploader behind beta login.

That’s how I solved it.

It’s not the web server who must solve this (Apache), it’s the application server (PHP). So remove the lines above from .htaccess and use Zend_Auth_Adapter_Http for this purpose — it’s Zend’s HTTP Authentication Adapter.

What concerns the Flash uploader: it sends ‘Shockwave Flash’ as value of ‘User-Agent’ request header. So in your Initializer or Bootstrap file (where you load Zend_Auth_Adapter_Http) check this header value, and if it’s not Flash’s, go for HTTP authentication.

P.S. Hackers can assume this and fake the header to access your site. To cope with that, use an additional secret request variable (Flash uploaders allow this) and check it at server side.

Zend Auth Session Expiration Time

development, php, zend Comments Off on Zend Auth Session Expiration Time

After authentication in my project it takes about 10-20 minutes for the auth session to expire, which is not handy — you go to get a snack and see a login screen coming back.

This is how to make the TTL of your auth session longer:

$s = new Zend_Session_Namespace( 
    $this->_auth->getStorage()->getNamespace() );

$s->setExpirationSeconds( strtotime('30 day', 0) );

Now your users will log in for 30 days.

You can call this code in your login action after successful authentication and before the redirect to the landing page.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in