Location: PHPKode > projects > Gallina 4 Wordpress > gallina/include/Gallina.php
<?php
//
// Gallina.php - A simple GMail based blog :-P
//
// Jonathan Hernandez <hide@address.com>
// <c> 2004 ion
//

require_once "libgmailer.php";

class wm_mypost {
				var $post_content;
				var $post_title;    
				var $post_status;    
}
			
class GallinaEntry
{
	var     $ID,
		$Author,
		$AuthorEmail,
		$Title,
		$Body,
		$Date,
		$DateEasy,
		$Published,
		$Replies,
		$ParentID,
		$Comments,
		$Attachments;
						 
	function GallinaEntry ( $id = null, $author = null, $authorEmail = null, $title = null, $body = null, $dt = null, $dtEasy = null,
		$published = null, $replies = null, $parent = null, $attachments = null)
	{
		if (!empty($id)) $this->ID = $id;
		if (!empty($author)) $this->Author = $author;
		if (!empty($authorEmail)) $this->AuthorEmail = $authorEmail;
		if (!empty($title)) $this->Title = $title;
		if (!empty($body)) $this->Body = $body;
		if (!empty($dt)) $this->Date = $dt;
		if (!empty($dtEasy)) $this->DateEasy = $dtEasy;
		if (!empty($published)) $this->Published = $published;
		if (!empty($replies)) $this->Replies = $replies;
		if (!empty($parent)) $this->Parent = $parent;
		if (!empty($attachments)) {
			$this->Attachments = $attachments;
		}
	}

	function AddComment ($comment)
	{
		$this->Comments[] = $comment;
	}
}

class Gallina 
{
	var $GMailer;
	var $BlogName;
	var $OutputDir;
	var $User;

	function Gallina ($user, $password, $timezone)
	{
		$this->GMailer = new GMailer ();
		$this->GMailer->setLoginInfo ($user, $password, $timezone);
		$this->User = $user;
	}

	function Connect ()
	{
		return $this->GMailer->connect ();
	}

	function GetInfo ()
        {
		$this->GMailer->fetchBox(GM_STANDARD, "inbox", 0);
		$snapshot = $this->GMailer->getSnapshot(GM_STANDARD);
		$info = array();
		if ($snapshot) {
			$info["quota"]["MB"] = $snapshot->quota_mb;
			$info["quota"]["Percentage"] = $snapshot->quota_per;
			$info["invitations"] = $snapshot->have_invit;
			$info["labels"] = $snapshot->label_list;
			$info["total"] = $snapshot->box_total;
		}
		return $info;
	}

	function GetRecentEntries ($limit = null)
	{
		$this->GMailer->fetchBox(GM_STANDARD, "inbox", 0);
		$snapshot = $this->GMailer->getSnapshot(GM_STANDARD);
		if ($snapshot) {
			$entries = array();
			$i = 0;
			$attachments = array();
			foreach ($snapshot->box as $conversation) {
				if ($conversation["is_starred"] == 1) {

					// Get all msg id in conversation (comments)
					$this->GMailer->fetchBox(GM_CONVERSATION, $conversation["id"], 0);
					$this->GMailer->performAction(GM_ACT_UNSTAR, $conversation["id"], 0);
					$conv2 = $this->GMailer->getSnapshot(GM_CONVERSATION);
					$firstId = false;
					$idMsgInConv = array();
					// Attachments
					foreach ($conv2->conv as $msg) {
						if (!$firstId) 
							$firstId = $msg["id"];
						else
							$idMsgInConv[] = $msg["id"];
						if (!empty($msg["attachment"]))
						foreach ($msg["attachment"] as $att) {
							if ((eregi ("image/jpeg", $att["type"])) || (eregi ("image/png", $att["type"]))) {
								if (!file_exists("cache/{$msg["id"]}/{$att["filename"]}")) {
									if (!is_dir("cache/{$msg["id"]}/{$att["filename"]}"))
										mkdir ("cache/{$msg["id"]}", 0755);
									$this->GMailer->getAttachment($att["id"], 
												$msg["id"], 
												"cache/{$msg["id"]}/{$att["filename"]}");
								}
								$attachments[$msg["id"]][] = "../cache/{$msg["id"]}/{$att["filename"]}";
							}
						}
					}
					// Get entry
					$this->GMailer->fetchBox(GM_CONVERSATION, array($conversation["id"],$firstId) , 0);
					$snap2 = $this->GMailer->getSnapshot(GM_CONVERSATION);
					$c = $snap2->conv[0];

					$entry = new GallinaEntry ($c["id"], $c["sender"], $c["sender_email"], $conversation["subj"],
					                           $c["body"], $c["dt"], $c["dt_easy"], $conversation["is_starred"],
					                           $conversation["sender"],  $conversation["id"], (!empty($attachments[$c["id"]]))?$attachments[$c["id"]]:null);


					if (count ($idMsgInConv) > 0) {
						foreach ($idMsgInConv as $id) {
							// Add comment
							$this->GMailer->fetchBox(GM_CONVERSATION, array($conversation["id"],$id) , 0);
							$snap2 = $this->GMailer->getSnapshot(GM_CONVERSATION);
							$c = $snap2->conv[0];

							$entry->AddComment (new GallinaEntry ($c["id"], $c["sender"], $c["sender_email"], 
											      $c["subj"], $c["body"], $c["dt"], $c["dt_easy"],
						                                              $conversation["is_starred"], $conversation["sender"], 
											      $conversation["id"], 
											      (!empty($attachments["id"])?$attachments["id"]:null)));
						}
					}
					$entries[] = $entry;
					$i++;
					// Limit
					if ((!empty($limit)) && ($i >= $limit))
						break;
				}
			}
			return $entries;
		} else {
			return false;
		}
	}

	function WriteFile ($file, $contents)
	{
		$fp = @fopen($file, "w");
		$write = @fputs($fp, $contents);
		@fclose($fp);
	}

	function PrepareText ($txt)
	{
		$stripAttrib = "' (clear|style|class)=\"(.*?)\"'i";
		$txt = stripslashes($txt);
		$txt = preg_replace($stripAttrib, '', $txt);
		$txt = strip_tags ($txt,"<br>");
		$txt = str_replace ("<br>","<br />",$txt);
		$txt = str_replace ("&nbsp;","&#160;",$txt);
		$txt = str_replace ("&gt;","&#62;",$txt);
		$txt = str_replace ("&lt;","&#60;",$txt);
		$txt = str_replace ("&frac14;","(1/4)",$txt);
		$txt = str_replace ("&frac12;","(1/2)",$txt);
		$txt = str_replace ("&frac34;","(3/4)",$txt);
		$txt = nl2br($txt);
		//$txt = "<![CDATA[ {$txt} ]]>";
		return $txt;
	}
	


	function updatePost () 
	{
			$info = $this->GetInfo ();
			$entries = $this->GetRecentEntries ();
			//// my first object :-)
			$wm_myobject = new wm_mypost();
			// fill object
			
			// feed object to wp_insert_post
			foreach ($entries as $e) {	
				$wm_myobject->post_title = $this->PrepareText ($e->Title);
				$wm_myobject->post_content =$e->Body;
				$wm_myobject->post_status = 'publish';
				$wm_myobject->post_author = 1;
				wp_insert_post($wm_myobject);
			}				
	}
	
	function CreateXML () 
	{
		$secRefresh =  $this->Refresh * 60;
                if (file_exists($this->OutputDir."index.xml")) {
                        $secCreated = mktime() - filemtime($this->OutputDir."index.xml");
		}
                else
                        $secCreated = $secRefresh + 1;
	
                if ($secCreated < $secRefresh)
			return false;

		$info = $this->GetInfo ();
		$entries = $this->GetRecentEntries ();

		$bXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
		$bXml .= "<?xml-stylesheet type=\"text/xsl\" href=\"gallina.xsl\"?>";
		$bXml .= "<gallina>\n";
		$bXml .= "<gmail>".$this->User."</gmail>\n";
		$bXml .= "<name>".$this->BlogName."</name>\n";
		$bXml .= "<url>".$this->BlogURL."</url>\n";
		$bXml .= "<output>".$this->OutputDir."</output>\n";
		$bXml .= "<quotaMB>".$info["quota"]["MB"]."</quotaMB>\n";
		$bXml .= "<quotaPercentage>".$info["quota"]["Percentage"]."</quotaPercentage>\n";
		$bXml .= "<invitations>".$info["invitations"]."</invitations>\n";
		foreach ($info["labels"] as $label)
			$bXml .= "<label>".$label."</label>\n";
		$bXml .= "<total>".$info["total"]."</total>\n";
		$xml = $bXml;	
		foreach ($entries as $e) {	
			$eXml = "<id>".$e->ID."</id>\n";
			$eXml .= "<author>".$e->Author."</author>\n";
			$eXml .= "<authorEmail>".$e->AuthorEmail."</authorEmail>\n";
			$eXml .= "<title>".$this->PrepareText ($e->Title)."</title>\n";
			$eXml .= "<body>".$this->PrepareText ($e->Body)."</body>\n";
			$eXml .= "<date>".$e->Date."</date>\n";
			$eXml .= "<dateEasy>".$this->PrepareText ($e->DateEasy)."</dateEasy>\n";
			$eXml .= "<published>".$e->Published."</published>\n";
			$eXml .= "<replies>".$e->Replies."</replies>\n";
			$eXml .= "<parentId>".$e->ParentID."</parentId>\n";
			if (!empty($e->Attachments))
				foreach ($e->Attachments as $item)
					$eXml .= "<attachment>{$item}</attachment>\n";
				
			$xml .= "<entry>".$eXml."</entry>\n";
			// Save each entry in a separate xml
			$cXml = $bXml;
			$cXml .= "<entry>".$eXml;
			if (count($e->Comments) > 0)
				foreach ($e->Comments as $c) {
					$cXml .= "<comment>\n";
					$cXml .= "<id>".$c->ID."</id>\n";
					$cXml .= "<author>".$c->Author."</author>\n";
					$cXml .= "<authorEmail>".$c->AuthorEmail."</authorEmail>\n";
					$cXml .= "<title>".$this->PrepareText ($c->Title)."</title>\n";
					$cXml .= "<body>".$this->PrepareText ($c->Body)."</body>\n";
					$cXml .= "<date>".$c->Date."</date>\n";
					$cXml .= "<dateEasy>".$this->PrepareText ($c->DateEasy)."</dateEasy>\n";
					$cXml .= "<published>".$c->Published."</published>\n";
					$cXml .= "<replies>".$c->Replies."</replies>\n";
					$cXml .= "<parentId>".$c->ParentID."</parentId>\n";
					if (!empty($c->Attachments))
						foreach ($c->Attachments as $item)
							$cXml .= "<attachment>{$item}</attachment>\n";
					$cXml .= "</comment>\n";
				}
			$cXml .= "</entry>\n";
			$cXml .= "</gallina>\n";
			$this->WriteFile ($this->OutputDir.$e->ID.".xml", $cXml);
		}
		$xml .= "</gallina>\n";
		$this->WriteFile ($this->OutputDir."index.xml", $xml);
	}
}
Return current item: Gallina 4 Wordpress