eZ 4.x Custom User Group Assignment Workflow

This is a simple workflow event that will extract a member type attribute (single select) and use it to assign the member into a group on register.

It catches not only self-registration, but updates made through the admin interface.

<?php
class HPMemberRegisterType extends eZWorkflowEventType
{
	const WORKFLOW_TYPE_STRING = 'hpmemberregister';

	public function __construct() 
	{
		parent::__construct( HPMemberRegisterType::WORKFLOW_TYPE_STRING, 'HP Member Register' );
	}
	
	public function execute ( $process, $event )
	{
		$parameters = $process->attribute( 'parameter_list' );
		
		$ini = eZINI::instance( 'hpmember.ini' );

		$objectID = $parameters['object_id']; 
		$object = eZContentObject::fetch( $objectID );
		$nodeID = $object->attribute( 'main_node_id' );
		$node = eZContentObjectTreeNode::fetch( $nodeID );

		if ( $object->contentClassIdentifier() === 'member' ) {

			$dataMap = $object->dataMap();
			$memberTypeValue = $dataMap[ 'member_type' ]->content();
			$contentClass = $object->contentClass();
			$memberTypes = $contentClass->fetchAttributeByIdentifier( 'member_type' )->content();
			$memberType = $memberTypes['options'][$memberTypeValue[0]]['name'];
			$memberGroup = $ini->variable( 'MemberGroup', $memberType );
			if ( $memberGroup !== null ) 
			{
				$node->setAttribute ( 'parent_node_id', $memberGroup );
				$node->store();
			}
		}

		return eZWorkflowType::STATUS_ACCEPTED;
	}
}
eZWorkflowEventType::registerEventType( HPMemberRegisterType::WORKFLOW_TYPE_STRING, 'hpmemberregistertype' );

.ini file:

<?php /* #?ini charset="utf-8"?

# The node id members with that type should be placed in
[MemberGroup]
Contractor=60
Homeowner=61
Lender=62
Realtor=63

*/ ?>

You can place your nodes where you like. Be sure the names under [MemberGroup] are identical to the options in the selection attribute added to the class used for user registration. This code uses a custom Member class to distinguish eZ Users from site members.

PHP - Catching session timeout issues

I've been working on a bug where an application is logging out unexpectedly.

To identify the piece of code that is destroying the session namespace I've been using the code snippet:

 ob_start();
 debug_print_backtrace();
 $trace = ob_get_contents();
 ob_end_clean(); 
 file_put_contents(basename(__FILE__).'.out', var_export($trace,true).PHP_EOL.var_export($_SERVER, ,true).PHP_EOL);

What it does is dumps out the stack that called the function it is placed in.

You can add additional information like a timestamp or FILE_APPEND.

You may also want to use Zend's logging. The reason I don't use it is that every instruction adds overhead. Once this issue is resolved, I'll remove all the debug code. I hope.

Credit to the link. :)

Sifting Through Spam

If you are setting up email filters for an account, some useful tactics are:

Display the headers of the emails in the account:

grep -iE "^(subject|from|reply-to|X-Spam-Level):" *

Once you identify messages of interest, you can use more to view them.

If you're using cPanel's filter interface with a RegEx, you can use this to exclude all .eu and .us (and any other) TLDs.

\.(eu|us)>?$

Type it in exactly as displayed. I put it on both the From and Reply-To headers.

Another good rule is to match on the spam score in the X-Spam-Status header, like so:

score=(3|4)

Emails with a spam score of 3 or 4 are rejected with a message that the sender should use the contact form on the site. Almost all of these will be spam, but for the few that aren't, the sender will have a way to resubmit their message.

Be sure to test to make sure it works the way you want it to.

If you find domains that are clearly just spammers, block them explicitly.

Report spam to spam@uce.gov, and scams to the organization that's being misrepresented.

Linux Command Line - Convert XML to JSON

This was the answer to a question that came in an email - "Do you know of a good XML to JSON converter?"

Obviously you need php-cli, JSON, and SimpleXML. :)

The "xml" is the name of the file that contains the XML, the output can be piped to a different file.

php -r 'echo json_encode(simplexml_load_file("xml"));'

If you would like it pretty printed, this page is really nice: http://jsonprettyprint.com/

Upgrading PHP 5.3 to 5.4 - Checking your code (Linux)

If you are upgrading PHP from 5.3 to 5.4 for an existing application, you can use the following commands to check for parse errors.

tree -fi application | grep -E '.php|.phtml' | sed "s/^/php -l /" > files
source files > results

The output is really what comes out of stderr, which will be the parse errors.

Be sure to check out the short_open_tag directive (http://www.php.net/manual/en/ini.core.php#ini.short-open-tag).

By default <?= is enabled, to use <? for statements other than echo, you must enable the short_open_tag option in /etc/php.ini.