<?php
/**
* -v 1.1 : optimizadas funciones de consulta POST
* -v 1.0 : primer esbozo de la clase (nada serio)
*
*/



/**
* Clase para el acceso a la API de Google Calendar
*
* @author Oswaldo Rubio <[email protected]>
* @version 1.1
*/ 
class PHPGCalendar{
    var 
$count_email "";
    var 
$count_passwd ="";
    var 
$google_login "";
    var 
$ERROR_CODE_NOT_LOGED = -1;
    var 
$ERROR_CODE_NOT_SESSION_ID = -2;
    var 
$ERROR_CODE_SENT_OK 1;
    var 
$gsessionid "";

    var 
$debug_output = array();                 
    
/**
    *  HTTP POST (funci�n "privada" )
    *
    * @param url : url a enviar
    * @param data : datos    
    */ 
    
function _exec_POST($url,$data){
       
$ch curl_init();
       
curl_setopt($chCURLOPT_URL$url);
       
curl_setopt($chCURLOPT_POST);
       
curl_setopt($chCURLOPT_VERBOSE1);
       
curl_setopt($chCURLOPT_POSTFIELDS$data);
       
curl_setopt($chCURLOPT_RETURNTRANSFER1);
       
$postResult curl_exec($ch);

       if (
curl_errno($ch)) {
          
$postResult "";
       }
       
curl_close($ch);
       
       return 
$postResult;
       
    }
    
/**
    *  HTTP "CUSTOM" ""POST (funci�n "privada" )
    *
    * @param url : url a enviar
    * @param data : datos    
    */ 
    
function _exec_CUSTOM_POST($url,$header,$data){      
       
curl_setopt($chCURLOPT_URL,$url);           
       
curl_setopt($chCURLOPT_VERBOSE1);
       
curl_setopt($chCURLOPT_HEADER1);       
       
curl_setopt($chCURLOPT_RETURNTRANSFER1);
       
curl_setopt($chCURLOPT_TIMEOUT4);
       
curl_setopt($chCURLOPT_CUSTOMREQUEST,'POST');
       
$header_all $header;
       
$header_all[] = $data;
       
curl_setopt($chCURLOPT_HTTPHEADER$header_all);       
       
$data curl_exec($ch);  
       return 
$data;
    }    
    
/**
    * Obtiene el par�metro Auth desde https://www.google.com/accounts/ClientLogin para el servicio "cl" (calendar)
    *
    * @param email : nombre de cuenta de gmail
    * @param passwd : password de la cuenta
    */    
    
function PHPGCalendar($email,$passwd){
       
$this->count_email $email;
       
$this->count_passwd $passwd;
        
       
$post_data  ="Email=".$email."&Passwd=".$passwd."&source=arrozconnori&service=cl";
       
$url "https://www.google.com/accounts/ClientLogin";
       
       
$get_login_result $this->_exec_POST($url,$post_data);
       
       if(
$get_login_result==""){
         
$this->google_login "";
       }else{
           
$get_login_result explode("\n",$get_login_result);
           
$get_login_result =explode("=",$get_login_result[2]);
           
$this->google_login $get_login_result[1];
        }
    }
    
/**
    * Comprueba si nos hemos logeado
    */
    
function is_loged(){
        if(
$this->google_login == ""
            return 
0;
        else 
            return 
1;
    }
    
/**
    * OBtiene el SessionID
    *
    */
    
function get_session_id($event_info){
        
$ch curl_init();
        
$event_template "<?xml version=\"1.0\"?>
        <entry xmlns='http://www.w3.org/2005/Atom'
            xmlns:gd='http://schemas.google.com/g/2005'>
          <category scheme='http://schemas.google.com/g/2005#kind'
            term='http://schemas.google.com/g/2005#event'></category>
          <title type='text'>"
.$event_info['asunto']."</title>
          <content type='text'>"
.$event_info['memo']."</content>
          <author>
            <name>Autor</name>
            <email>[email protected]</email>
          </author>
          <gd:transparency
            value='http://schemas.google.com/g/2005#event.opaque'>
          </gd:transparency>
          <gd:eventStatus
            value='http://schemas.google.com/g/2005#event.confirmed'>
          </gd:eventStatus>
          <gd:where valueString='"
.$event_info['donde']."'></gd:where>
          <gd:when startTime='"
.$event_info['cuando']."'
            endTime='"
.$event_info['cuando']."'></gd:when>
        </entry>"
;

       
$header[] = "Host: $host";
       
$header[] = "MIME-Version: 1.0";
       
$header[] = "Content-type: application/atom+xml";
       
$header[] = "Authorization: GoogleLogin auth=".$this->google_login;
       
$header[] = "Accept: text/xml";
       
$header[] = "Content-length: ".strlen($event_template);
       
$header[] = "Cache-Control: no-cache";
       
$header[] = "Connection: close \r\n";
       
       
$header[] = $event_template;
    
       
       
       
curl_setopt($chCURLOPT_URL,"http://www.google.com/calendar/feeds/default/private/full");       
    
       
curl_setopt($chCURLOPT_VERBOSE1);
       
curl_setopt($chCURLOPT_HEADER1);       
       
curl_setopt($chCURLOPT_RETURNTRANSFER1);
       
curl_setopt($chCURLOPT_TIMEOUT4);
       
curl_setopt($chCURLOPT_CUSTOMREQUEST,'POST');
       
curl_setopt($chCURLOPT_HTTPHEADER$header);
       
       
$data curl_exec($ch);       
       if (
curl_errno($ch)) {
        
       } else {
           
curl_close($ch);
           
$data_lineas explode("\n",$data);
           if(
strcmpsubstr($data_lineas[0],0,12),"HTTP/1.1 302")==0){
              
$tmp_gsess explode("?",$data_lineas[3]);              
              
$gsessionid substr($tmp_gsess[1],11,11);
              
$this->debug_output[] = $data;
              return 
$gsessionid;
              
         }
        }
    }
    
    
/**
    * envia en evento (ya tiene un sessionid)
    *
    */
    
function send_event($event_info){
        
$ch curl_init();
        
$event_template "<?xml version=\"1.0\"?>
        <entry xmlns='http://www.w3.org/2005/Atom'
            xmlns:gd='http://schemas.google.com/g/2005'>
          <category scheme='http://schemas.google.com/g/2005#kind'
            term='http://schemas.google.com/g/2005#event'></category>
          <title type='text'>"
.$event_info['asunto']."</title>
          <content type='text'>"
.$event_info['memo']."</content>
          <author>
            <name>Autor</name>
            <email>[email protected]</email>
          </author>
          <gd:transparency
            value='http://schemas.google.com/g/2005#event.opaque'>
          </gd:transparency>
          <gd:eventStatus
            value='http://schemas.google.com/g/2005#event.confirmed'>
          </gd:eventStatus>
          <gd:where valueString='"
.$event_info['donde']."'></gd:where>
          <gd:when startTime='"
.$event_info['cuando']."'
            endTime='"
.$event_info['cuando']."'></gd:when>
        </entry>"
;

       
$header[] = "Host: $host";
       
$header[] = "MIME-Version: 1.0";
       
$header[] = "Content-type: application/atom+xml";
       
$header[] = "Authorization: GoogleLogin auth=".$this->google_login;
       
$header[] = "Accept: text/xml";
       
$header[] = "Content-length: ".strlen($event_template);
       
$header[] = "Cache-Control: no-cache";
       
$header[] = "Connection: close \r\n";
       
       
$header[] = $event_template;
       
curl_setopt($chCURLOPT_URL,"http://www.google.com/calendar/feeds/default/private/full?gsessionid=".$this->gsessionid);       
       
curl_setopt($chCURLOPT_VERBOSE1);
       
curl_setopt($chCURLOPT_HEADER1);       
       
curl_setopt($chCURLOPT_RETURNTRANSFER1);
       
curl_setopt($chCURLOPT_TIMEOUT4);
       
curl_setopt($chCURLOPT_CUSTOMREQUEST,'POST');
       
curl_setopt($chCURLOPT_HTTPHEADER$header);
       
       
$data curl_exec($ch);       
       if (
curl_errno($ch)) {
        
       } else {
        
            
$this->debug_output[] = $data;
         
           
curl_close($ch);
           
$data_lineas explode("\n",$data);
           if(
strcmpsubstr($data_lineas[0],0,12),"HTTP/1.1 201")==0){
                return     
$this->ERROR_CODE_SENT_OK;                  
           }
    }    
}
    
    
/**
    *  Inserta un nuevo evento
    *
    * @param asunto : descripcion del asunto
    * @param dia : dia en formato yyyy-mm-dd
    * @param hora : hora en formato hh:mm
    * @param memo : texto del evento
    */ 
    
function new_event($asunto,$dia,$hora,$memo){
      
      if(
$this->is_loged()){
            
$evento = array();
          
$evento['asunto'] = $asunto;
          
$evento['cuando'] = $dia."T".$hora.":00.000Z";

          
$evento['memo'] = $memo;    
          
          
$this->gsessionid  $this->get_session_id($evento);
          if(
$this->gsessionid!=""){
            
$this->send_event($evento);
            return 
$this->ERROR_CODE_SENT_OK;
          }
          else{
            return 
$this->ERROR_CODE_NOT_SESSION_ID;
            }
        }
       else
       {
         return 
$this->ERROR_CODE_NOT_LOGED;
        }
    
    
    }

}
?>