Location: PHPKode > projects > Laikwan WMC > wmc_20070325/gmlite/loadEmails.php
<?php

   require_once("config.php");

   $login = (isset($_REQUEST["login"]))? $_REQUEST["login"]: "";
   $pass = (isset($_REQUEST["pass"]))? $_REQUEST["pass"]: ""; 
   $tz = (isset($_REQUEST["tz"]))? 0+$_REQUEST["tz"] : 8;  
   $tz2 = (isset($_REQUEST["tz2"]))? 0+$_REQUEST["tz2"] : 0; 
   
   if (!(strlen($login) == 0 || strlen($pass) == 0)) {
      setcookie("TZ", $tz); 
      setcookie("TZ2",$tz2); 
   } 
   
   
   $gm = new GMailer();
   if (!$gm->created) {
      die("fail to create gmailer");
   }
   quick_init($gm);
   $tz = $tz + $tz2;
   $gm->setLoginInfo($login, $pass, $tz);
   if (!$gm->connect()) {
      header("Location: index.php?err=10");
      exit();
   }
   
   $th = (isset($_REQUEST["th"]))? $_REQUEST["th"] : 0;
   $mg = (isset($_REQUEST["mg"]))? $_REQUEST["mg"] : 0;
   $html = (isset($_REQUEST["html"]))? $_REQUEST["html"] : 0;
   $std = (isset($_REQUEST["std"]))? $_REQUEST["std"] : 0;
   $label = (isset($_REQUEST["label"]))? $_REQUEST["label"] : 0;
   $query = stripslashes((isset($_REQUEST["query"]))? $_REQUEST["query"] : 0);
   $sum = (isset($_REQUEST["sum"]))? $_REQUEST["sum"] : 0;   
   $unread = (isset($_REQUEST["unread"]))? $_REQUEST["unread"] : 0; 
   $pos = (isset($_REQUEST["pos"]))? $_REQUEST["pos"] : 0;    
   $orig = (isset($_REQUEST["orig"]))? $_REQUEST["orig"] : 0;    
   // perform action?
   $act = isset($_REQUEST["act"])? $_REQUEST["act"] : 0;
   $lbl = isset($_REQUEST["lbl"])? $_REQUEST["lbl"] : 0;    
   $back = isset($_REQUEST["back"])? $_REQUEST["back"] : 0;
   $t = isset($_REQUEST["t"])? $_REQUEST["t"] : array();    
   
   if ($act && count($t) > 0 && $back) {
      $gm->performAction($act, $t, $lbl);
      header("Location: ".$back);
      exit();   // redirect right away
   }
   
   // $tit : title
   // $q : query
   // $std : standard gm (top level folder for me) - inbox, starred, sent, drafts, all
   // $th : conversation (preview/open)
   
   if ($sum) {
	   // summary
      $q = "search=cat&cat=dummyjusttoretrievesummary&view=tl&start=0&init=1";
      $tit = "summary";
   } elseif ($orig && $mg) {
      $q = "view=om&th=".$mg;
      // dump the whole page... with some escaping
      header("Content-type: text/plain; charset=big5");
      echo $gm->dump($q);
      exit();
   } else {   
      if ($unread) {
		  // get unread?
         $q = "search=query&q=is%3Aunread&view=tl&start=".$pos;
         $tit = "unread";
      } else {   
         if ($th) {
            $tit = "conversation";
            if (!$std)
               $std = "inbox";
               
            if ($mg)
               $q = "search=".$std."&view=cv&th=".$th."&msgs=".$mg;      
            else
               $q = "search=".$std."&view=cv&th=".$th;
               
            if ($html)
               $q .= "&ser=1";      
         } else {
            if ($query) {
               $q = "search=query&q=".urlencode($query)."&view=tl&start=".$pos;
               $tit = "search";
            } elseif ($label) {
               $q = "search=cat&cat=".urlencode($label)."&view=tl&start=".$pos;
               $tit = $label;
            } else {
               if ($std)
                  $q = "search=".$std."&view=tl&start=".$pos;
               else {
                  $q = "search=inbox&view=tl&start=0&init=1";
                  $std = "inbox";
               }
               $tit = $std;
            }
         }
      }
   }
   
   $gm->fetch($q);
   $snapshot = $gm->getSnapshot(GM_STANDARD|GM_LABEL|GM_QUERY|GM_CONVERSATION);   
   if ($snapshot->created == 0) {
      //echo "<pre>".print_r($gm->raw,1)."</pre>";
      // error occurred
      header("Location: main.php?std=".$std."&label=".$label."&".SID);
      exit;
   }   
 /*  $stdbox = GMailer::getStandardBox();
   
   $show_total = ($sum == 1 && S_SHOW_TOTAL == 1);
   if ($show_total) {
      $gm->fetchBox(GM_PREFERENCE, 0, 0);
      $snapshot2 = $gm->getSnapshot(GM_PREFERENCE);
   }
   */
   
	header("Content-type: text/xml; charset=utf-8");  // must use text/xml for ajax/xmlhttp to work :(
	header("Pragma: no-cache");  // stop caching	
?> 

<body>
<msgType>emails</msgType>

<?php

      // total email in box (not just ones in current loop
	  echo "<boxTotal>" . $snapshot->box_total . "</boxTotal>";

	  //$emailsShown = 100;
      $lastidx = count($snapshot->box)-1;
      for ($i = 0; $i <= $lastidx; $i++) { //  && $i < $emailsShown

         $contents = "<email>";
         $contents .= "<id>". cleanData($snapshot->box[$i]["id"])."</id>";
         $contents .= "<sender>". cleanData($snapshot->box[$i]["sender"])."</sender>";
         $contents .= "<subj>". cleanData($snapshot->box[$i]["subj"])."</subj>";

		// want yyyy-mm-dd hh:mm:ss format for sorting
         //$contents .= "<date>". cleanData($snapshot->box[$i]["date"])."</date>";
		 $datetime = $snapshot->box[$i]["date"];
		 $mm = sprintf("%02d", date("m"));
		 $dd = sprintf("%02d", date("d"));
		 $yy = date("Y");
		 if(strpos($datetime, ":") !== false){	// in "hh:mm am" format
			 $a = explode(" ", $datetime);
			 $b = explode(":", $a[0]);

			 $hour = $b[0];
			 $min  = sprintf("%02d", $b[1]);
			 $pm   = $a[1];
			 
			 $hour = sprintf("%02d", ($pm ? ($hour%12)+12 : $hour%12));
			 $datetime = $yy."-".$mm."-".$dd." ".$hour.":".$min;
		 }elseif(strpos($datetime, "/") !== false){	// in "mm/dd/yy" format
			 $a = explode("/", $datetime);
			 
			 $mm = sprintf("%02d", $a[0]);
			 $dd  = sprintf("%02d", $a[1]);
			 $yy  = 2000 + $a[2];
			 
			 $datetime = $yy."-".$mm."-".$dd;
		 }else{	// in "mon d" format
			 $monthsHash = array("Jan"=>"01","Feb"=>"02","Mar"=>"03",
					 "Apr"=>"04","May"=>"05","Jun"=>"06",
					 "Jul"=>"07","Aug"=>"08","Sep"=>"09",
					 "Oct"=>"10","Nov"=>"11","Dec"=>"12");
			 $a = explode(" ", $datetime);

			 $mm = sprintf("%02d", $monthsHash[$a[0]]); 
			 $dd = sprintf("%02d", $a[1]);
			 $datetime = $yy."-".$mm."-".$dd;
		 }
         $contents .= "<date>". cleanData($datetime)."</date>";

         $contents .= "<attachment>". count($snapshot->box[$i]["attachment"])."</attachment>";
       
		 if (eregi("<b>", $snapshot->box[$i]["sender"])){
		 	$contents .= "<unread>1</unread>";
         }else{
			 $contents .= "<unread>0</unread>";
		 }

         if ($std == "trash" || $std == "spam") {
	         $contents .= "<url>". cleanData("std=".$std."&amp;th=".$snapshot->box[$i]["id"])."</url>"; //main.php?
         } else {
 	         $contents .= "<url>". cleanData("th=".$snapshot->box[$i]["id"])."</url>";//main.php?
         }
         
         if ($snapshot->box[$i]["is_starred"] == 1){
  	         $contents .= "<starred>1</starred>";
		 }else{
  	         $contents .= "<starred>0</starred>";			 
		 }
		
//         if ($query)
  //          $contents .= " :: snippet: ".$snapshot->box[$i]["snippet"];

        $contents .= "</email>";
		echo $contents;
		
      }
	  
function cleanData($contents){	  
	//$contents = strip_tags($contents);
	$contents = ereg_replace("&", "&amp;", $contents); 
	$contents = ereg_replace("<", "&lt;", $contents); 
	$contents = ereg_replace(">", "&gt;", $contents); 
	return $contents;
}

	  
?> 
</body>
Return current item: Laikwan WMC