#!/usr/local/bin/php
<?php

    
require('Mail/mimeDecode.php');
    require(
'HTTP/Request.php');

    
$verbose false;

    
$stdin '';
    while(
$in fread(STDIN1000000))
        
$stdin .= $in;

    
// Write out a time-stamped file with the entire
    // STDIN, for possible later debugging or whatever
    
$report fopen(time().'.txt''w');    
    
fwrite($report$stdin);
    
fclose($report);

    
ob_start(); {

        
$structure Mail_mimeDecode::decode(array('input' => $stdin'include_bodies' => true));
        
        
// Defaults are empty
        
$status_text '';
        
$image_data false;
        
$image_type false;
        
        
// Look at each piece of the multipart message to find the
        // text and the image. The actual layout of a message will
        // vary depending on your client - this particular loop was
        // written for messages sent from my w810i, and will no doubt
        // need tuning for yours. That's what the STDIN file written
        // above is for.
        
foreach($structure->parts as $part) {
        
            if(
substr($part->headers['content-type'], 010) == 'text/plain') {

                if(
$verbose)
                    
printf("found text...\n");
    
                
$status_text = (strtolower($part->headers['content-transfer-encoding']) == 'base64')
                    ? 
base64_decode($part->body)
                    : 
$part->body;
                
                if(
$verbose)
                    
printf("text is: %s\n\n"$status_text);
    
            } elseif(
preg_match('#^image/(\w+)\b#'$part->headers['content-type'], $matches)) {
            
                
$image_type $matches[1];

                if(
$verbose)
                    
printf("found %s...\n"$part->headers['content-location']);
    
                
$image_data = (strtolower($part->headers['content-transfer-encoding']) == 'base64')
                    ? 
base64_decode($part->body)
                    : 
$part->body;
            }
        }
    
        
print_r($structure);
        
        if(
$image_data && $image_type) {

            
// Short, hopefully-uniqe filename.
            
$image_filename substr(md5(date('r')), 06).".{$image_type}";
            
            
$image_url "http://teczno.com/tw/{$image_filename}";
            
$status_length 140 strlen($image_url);

            
// Add your own username & password here
            
$req = new HTTP_Request('http://twitter.com/statuses/update.json',
                                    array(
'user' => '******''pass' => '******',
                                          
'method' => 'POST'));
                                          
            
// Post to Twitter...
            
$req->addPostData('status'sprintf("%.{$status_length}s %s"$status_text$image_url));
            
$req->sendRequest();
        
            
// ...and save the image.
            
if($fh fopen($image_filename'w')) {
                
fwrite($fh$image_data);
                
fclose($fh);
                
chmod($image_filename0644);
            }
        }
    }
    
    if(
$verbose) {
        
$report fopen('report.txt''w');    
        
fwrite($reportob_get_contents());
    }

    
ob_end_clean();
    
    if(
$verbose)
        
fclose($report);
?>