<?php
/*
Plugin Name: WP-SprintPhoto
Version: 0.2.1
Plugin URI: http://bekit.net/archives/category/blog-development/
Description: Allow Sprint Picture Share to be posted with email blogging.
Author: Brint E. Kriebel
Author URI: http://bekit.net
*/

/*
Notes: Based partially off of code from phone2blog by LaughingLizard: http://weblogtoolscollection.com/archives/2004/07/17/sprint-pcs-moblog-to-wordpress-12-mingus/
    To be used in addition to cool_blade's modified wp-mail.php code.

Instructions:
    1. Install the modified wp-mail.php code from http://blade.lansmash.com/index.php?cat=5 (or my modified version of it).
    2. Copy SprintPhoto.php into the wordpress plugins directory (typically wp-content/plugins)
    3. Create the directories for photo storage specified in wp-mail.php. Downloaded images will be stored here.
    4. Activate the SprintPhoto plugin through your administrative pages.
    5. Follow the "How to Blog by eMail" guide:
        http://wiki.wordpress.org/How%20To%20Blog%20By%20Email
    6. Send a picture share as usual to the email adress you have created.
        a. Voice attachments not currently implimented.
        b. For text messages:
            By default it will pull a text message in as the title (example: "This is my title!")
            You can modify the Category of the post using brackets with the category id (example: "[2] This is my title!" - this will post into the category with id #2)
            You can add a text post that will show under the image by sepating the title with a semicolon (example: "[2] This is my title!; This is the text of the post.")
            If no text message is selected, the default title can be defined by the variable "$defaultTitle".
    7. Browse to the wp-mail.php page - it will begin processing your email.
    8. Regular emails will process as normal posts.
    9. The full scale image as well as a thumbnail will be downloaded to the photos directory.
        A blog will be posted per your configuration of wp-m2.php with the thumbnail linking to the
        full image, the category, title and text of the post will be implimented as described above.

Changelog:
    0.2.1
        Minor bugfix
        Fixed:
            Special characters (such as & or ') would cause the script to bomb with an SQL error
    0.2
        Fixed:
            Incorrect date/time stamp.
            Permalinks not working correctly.
        Added:
            Title can now be adjusted (and defaulted).
            Now using wp-mail.php hack from http://blade.lansmash.com/index.php?cat=5 (Some modifications made).
            Category can be adjusted (thanks to blade's wp-mail).
            A message can be planted in addition to the Title.
            The downloaded images are named to the date-title. 
    0.1
        Initial release.
To-Do:
    1. Bug-fix as needed.
        Known bugs:
            None that i've found :)
    2. Add functionality for voice messages.
    3. Possibly turn this into a full Sprint Vision plugin for text messages, picturemail and videomail. (Optimism is a good thing).

Last Modified: 2004-10-11
*/

function SprintPictureShare($post_ID) {

    
//Initiate global variables
    
global $wpdb$tableposts,$post_categories,$photosdir;

    
//Pull in post information
    
$postData get_postdata($post_ID);
    
    
//Define variables.
    
$defaultTitle "MobLog: " $postData['Date']; //Default title is none is specified in the message.
    
$thumbSize "235"//Thumbnail image size - largest side. (Default: 235)
        
    //Gather content to be examined / modified
    
$content $postData['Content'];
    
$post_title $postData['Title'];
    
    
//Check if it is a Sprint Picture Mail message.
    
if (preg_match('/A Picture from my PCS Vision Camera/'$content)) {

        
//Get the position of the image URL from the message
        
$imagePos strpos($content"shareImage") + 11;
        
$imageLength strpos($content"_235.jpg"$imagePos) - $imagePos;

        
//The invite string is needed for authorization
        
$invitePos strpos($content"invite=") + 7;
        
$inviteLength strpos($content"&quot"$invitePos) - $invitePos;

        
//Get the text portion of the message - this will be attached to the blog entry
        
$messagePos strpos($content"Message:&lt;/b&gt;&lt;br/&gt;") + 29;
        
$messageLength strpos($content"&lt;/font&gt;"$messagePos) - $messagePos;

        
//Take all of the locations and pull the data from the message
        
$imageName substr($content$imagePos$imageLength);
        
$inviteName substr($content$invitePos$inviteLength);
        if (
$messageLength != 0$message substr($content$messagePos$messageLength);

        
//Try and determine category.
        
if ( preg_match('/.*\[(.+)\](.+)/'$message$matches) )
        {
            
$post_categories[0] = $matches[1];
            
$message $matches[2];
        }
        if (empty(
$post_categories)) $post_categories[] = get_settings('default_category');

        
//If there is a semicolon, make the text after it be the post text.
        
list($mypost_title$mypost_text) = explode(";"html_entity_decode($message));
        
trim($mypost_title);
        
trim($mypost_text);
    
        
//If there is a text message, set the title of the post to the text.
        
if ($mypost_title) {
            
$post_title $mypost_title;
        } elseif(
$defaultTitle) {
            
$post_title $defaultTitle;
        }

        
//Create the post name (slug) from the post title.
        
$post_name sanitize_title($post_title);

        
//Put together the location of the full quality image and the thumbnail
        
$thumbLoc "http://pictures.sprintpcs.com//shareImage/" $imageName "_$thumbSize.jpg?invite=$inviteName";
        
$imageLoc "http://pictures.sprintpcs.com//shareImage/$imageName.jpg?invite=$inviteName";

        
//Create image name. (format at will)
        
$myImageName sanitize_title($postData['Date']) . "-" $post_name

        
//Download the thumbnail
        
$thumbDL LoadJpeg($thumbLoc);
        
$thumbImage ImageJPEG($thumbDLABSPATH.$photosdir.$myImageName."_thumb.jpg");

        
//Download the image
        
$imageDL LoadJpeg($imageLoc);
        
$image ImageJPEG($imageDLABSPATH.$photosdir.$myImageName.".jpg");

        
//Create the local image/thumb URLs
        
$photosdir preg_replace('|^/+|'''$photosdir); //Clean up spare forward slashes
        
$localImage get_settings('home') . "/" $photosdir $myImageName ".jpg";
        
$localThumb get_settings('home') . "/" $photosdir $myImageName "_thumb.jpg";

        
//Create the link from the thumbnail to the full image
        
$imageLink "<a href=$localImage><img src=$localThumb></a>";

        
//Post the image as the message content. Add formatting at will.
        
if (!$mypost_text) {
            
$myContent "$imageLink";
         } else {
            
$myContent "$imageLink<p>$mypost_text";
        }

        
//Clean up the data to avoid special characters
        
$myContent addslashes($myContent);
        
$post_title addslashes($post_title);
        
        
//Output reformatted data.
        
$wpdb->query("UPDATE $tableposts SET post_content = '$myContent', post_title = '$post_title', post_name = '$post_name' WHERE ID = '$post_ID'");

    }

}

#Global Function definitions

function LoadJpeg($imgname) {
    
$im = @ImageCreateFromJPEG ($imgname); /* Attempt to open */
    
if (!$im) { /* See if it failed */
        
$im ImageCreate (85030); /* Create a blank image */
        
$bgc ImageColorAllocate ($im255255255);
        
$tc  ImageColorAllocate ($im000);
        
ImageFilledRectangle ($im0085030$bgc);
        
/* Output an errmsg */
        
ImageString ($im155"Error loading $imgname"$tc);
    }
    return 
$im;
}

//Add the action to be used in the wp-mail.php script through the "publish_phone" hook
add_action('publish_phone''SprintPictureShare'9);
?>