Posts Tagged ‘php’

PHP: checking links validity using multi-thread processes

janvier 12th, 2012

I recently came to a situation where I had to check 1800 links validity. No chance to do it manually. I decided to write some PHP code to check these URLs using the PHP get_headers function. Checking an URL seems easy but it takes a tiny amount of time to process because it calls distant resources: DNS lookup, connection time, download time… for each request. Using a simple « while » PHP function, you may come across three issues:

  1. it may be very long to complete and it’s not always possible to increase the PHP maximum execution time
  2. the display: your web page will be blank for quite a long time until enough data is processed. Of course you may play with the buffer handler but still!
  3. the process: as PHP is not a multi-threaded language out of the box, it will check one link by one. Much longer…

Therefore, I developed a very simple peace of code to solve these issues. As it is working quite softly, I decided to share it with you.
Read the rest of this entry »

Popularity: 5% [?]

Tags: , , , , , , ,
Posted in Développement, Technologies | Comments (0)

PHP: How to extract attachments from email

janvier 4th, 2012

I’ve been busy working on a piece of code whose role is to extract all attachments from emails using PHP and IMAP. It is not really simple so here’s a recursive function doing that work:

<?php

// You may change options here
$server = "{imap.domain.com:143/notls}INBOX";
$username = "valentin@domain.com";
$password = "valentin";
$mbox = imap_open($server, $username, $password);

// Getting all emails
if ($headers = imap_headers($mbox)) {
	$i = 0;
	foreach ($headers as $val) {
		$i ++;

		// Will return many infos about current email
		// Use var_dump($info) to check content
		$info = imap_headerinfo($mbox, $i);

		// Gets the current email structure (including parts)
		// Use var_dump($structure) to check it out
		$structure = imap_fetchstructure($mbox, $info->Msgno);

		// Getting attachments
		// Will return an array with all included files
		// Also works with inline attachments
		$attachments = get_attachments($structure);

		// You are now able to get attachments' raw content
		foreach ($attachments as $k => $at) {
			$content = imap_fetchbody($mbox, $info->Msgno, $at['part']);
			switch ($at['encoding']) {
				case '3':
					$content = base64_decode($content);
				break;

				case '4':
					$content = quoted_printable_decode($content);
				break;
			}
		}

	}
}
// Shutting down
imap_close($mbox); 

/**
* Gets all attachments
* Including inline images or such
* @author: Axel de Vignon
* @param $content: the email structure
* @param $part: not to be set, used for recursivity
* @return array(type, encoding, part, filename)
*
*/
function get_attachments($content, $part = null) {
	static $results;

	// First round, emptying results
	if (is_null($part)) {
		$results = array();
	}

	// Removing first dot (.)
	if (substr($part, 0, 1) == '.') {
		$part = substr($part, 1);
	}

	// Checking ifdparameters
	if (isset($content->ifdparameters) && $content->ifdparameters == 1 && isset($content->dparameters) && is_array($content->dparameters)) {
		foreach ($content->dparameters as $object) {
			if (isset($object->attribute) && strtolower($object->attribute) == 'filename') {
				$results[] = array(
					'type'			=> (isset($content->subtype)) ? $content->subtype : '',
					'encoding'		=> $content->encoding,
					'part'			=> (is_null($part)) ? 1 : $part,
					'filename'		=> $object->value
				);
			}
		}
	}

	// Checking ifparameters
	else if (isset($content->ifparameters) && $content->ifparameters == 1 && isset($content->parameters) && is_array($content->parameters)) {
		foreach ($content->parameters as $object) {
			if (isset($object->attribute) && strtolower($object->attribute) == 'name') {
				$results[] = array(
					'type'			=> (isset($content->subtype)) ? $content->subtype : '',
					'encoding'		=> $content->encoding,
					'part'			=> (is_null($part)) ? 1 : $part,
					'filename'		=> $object->value
				);
			}
		}
	}

	// Recursivity
	if (isset($content->parts) && count($content->parts) > 0) {
		// Other parts into content
		foreach ($content->parts as $key => $parts) {
			get_attachments($parts, ($part.'.'.($key + 1)));
		}
	}
	return $results;
}

Bonus tip: you may display images using their raw content data. For instance, the following HTML code will display a red dot:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">

The result:
Red dot

Popularity: 3% [?]

Tags: , , , , ,
Posted in Développement, Technologies | Comments (0)

Subversion pre-commit hook for PHP

mars 8th, 2010

If you are using Subversion for your development projects, you may know about hooks: these are scripts that are executed on the server at different times. For instance you may want to check that a commit message is not empty before allowing the commit. In this case, this is a pre-commit hook. Read the rest of this entry »

Popularity: 11% [?]

Tags: , , ,
Posted in Development | Comments (0)

Code snippet: a PHP virus

février 25th, 2010

A quick post to share with you a tiny code sample I’ve written: this is a PHP virus.

Read the rest of this entry »

Popularity: 10% [?]

Tags: , , , ,
Posted in Development | Comments (0)