XBMC Gmail notifications.

I have this php script currently running on my nas to let me know if I have any new mail.  It works with gmail.com, googlemail.com and google app address’.  I simply use cron to execute the script every 15 minutes.

i.e. (0,15,30,45 17-23 * * * php /var/www/checkgmail.php > /dev/null   #Check Gmail)


   1: <?php


   2: $emailaddress = 'someemailaddress@gmail.com'; // i.e. bob@gmail.com or bob@bobsdomain.com if you use google apps


   3: $password = 'somepassword'; // email password


   4: $xbmcaddress = '192.168.1.1'; // IP or hostname of your xbmc


   5: $xbmcport = '8080'; // port the xbmc web server is running on.


   6:


   7:


   8: $domain = explode('@',$emailaddress);


   9: if ($domain['1'] == 'gmail.com' || $domain['1'] == 'googlemail.com')


  10:     {


  11:     $use_googleapps = false;


  12:     } else {


  13:     $use_googleapps = true;


  14:     }


  15: if ($use_googleapps == true)


  16:     {


  17:     $url = 'https://mail.google.com/a/'.$domain['1'].'/feed/atom';


  18:     } else {


  19:     $url = 'https://mail.google.com/mail/feed/atom';


  20:     }


  21:


  22:


  23:     $ch = curl_init ($url);


  24:     curl_setopt($ch, CURLOPT_HEADER, 0);


  25:     curl_setopt($ch, CURLOPT_PROTOCOLS,CURLPROTO_HTTPS);


  26:     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);


  27:     curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);


  28:     $curllogin = $emailaddress.':'.$password;


  29:     curl_setopt($ch, CURLOPT_USERPWD,$curllogin);


  30:     $rawdata=curl_exec($ch);


  31:     curl_close ($ch);


  32:


  33: $array = xml2array($rawdata);


  34: $array = $array['feed'];


  35: //print_r($array);


  36:


  37: if ($array['fullcount'] < 1)


  38:     {


  39:     echo ' no new mail ';


  40:     } else {


  41:     if ($array['fullcount'] == 1)


  42:         {


  43:         fopen("http://".$xbmcaddress.":".$xbmcport."/xbmcCmds/xbmcHttp?command=ExecBuiltIn(Notification(".$emailaddress.",You%20Have%20A%20New%20Email!))",r);


  44:         } else {


  45:         fopen("http://".$xbmcaddress.":".$xbmcport."/xbmcCmds/xbmcHttp?command=ExecBuiltIn(Notification(".$emailaddress.".",You%20Have%20".$array['fullcount']."%20New%20Emails!))",r);


  46:         }


  47:


  48:


  49:     }


  50:


  51:


  52:


  53: function xml2array($contents, $get_attributes=1, $priority = 'tag') {


  54:     if(!$contents) return array();


  55:


  56:     if(!function_exists('xml_parser_create')) {


  57:         //print "'xml_parser_create()' function not found!"; 


  58:         return array();


  59:     }


  60:


  61:     //Get the XML parser of PHP - PHP must have this module for the parser to work


  62:     $parser = xml_parser_create('');


  63:     xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss 


  64:     xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);


  65:     xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);


  66:     xml_parse_into_struct($parser, trim($contents), $xml_values);


  67:     xml_parser_free($parser);


  68:


  69:     if(!$xml_values) return;//Hmm...


  70:


  71:     //Initializations


  72:     $xml_array = array();


  73:     $parents = array();


  74:     $opened_tags = array();


  75:     $arr = array();


  76:


  77:     $current = &$xml_array; //Refference


  78:


  79:     //Go through the tags.


  80:     $repeated_tag_index = array();//Multiple tags with same name will be turned into an array


  81:     foreach($xml_values as $data) {


  82:         unset($attributes,$value);//Remove existing values, or there will be trouble


  83:


  84:         //This command will extract these variables into the foreach scope


  85:         // tag(string), type(string), level(int), attributes(array).


  86:         extract($data);//We could use the array by itself, but this cooler.


  87:


  88:         $result = array();


  89:         $attributes_data = array();


  90:


  91:         if(isset($value)) {


  92:             if($priority == 'tag') $result = $value;


  93:             else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode


  94:         }


  95:


  96:         //Set the attributes too.


  97:         if(isset($attributes) and $get_attributes) {


  98:             foreach($attributes as $attr => $val) {


  99:                 if($priority == 'tag') $attributes_data[$attr] = $val;


 100:                 else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'


 101:             }


 102:         }


 103:


 104:         //See tag status and do the needed.


 105:         if($type == "open") {//The starting of the tag '<tag>' 


 106:             $parent[$level-1] = &$current;


 107:             if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag


 108:                 $current[$tag] = $result;


 109:                 if($attributes_data) $current[$tag. '_attr'] = $attributes_data;


 110:                 $repeated_tag_index[$tag.'_'.$level] = 1;


 111:


 112:                 $current = &$current[$tag];


 113:


 114:             } else { //There was another element with the same tag name


 115:


 116:                 if(isset($current[$tag][0])) {//If there is a 0th element it is already an array


 117:                     $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;


 118:                     $repeated_tag_index[$tag.'_'.$level]++;


 119:                 } else {//This section will make the value an array if multiple tags with the same name appear together


 120:                     $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array


 121:                     $repeated_tag_index[$tag.'_'.$level] = 2;


 122:


 123:                     if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well


 124:                         $current[$tag]['0_attr'] = $current[$tag.'_attr'];


 125:                         unset($current[$tag.'_attr']);


 126:                     }


 127:


 128:                 }


 129:                 $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;


 130:                 $current = &$current[$tag][$last_item_index];


 131:             }


 132:


 133:         } elseif($type == "complete") { //Tags that ends in 1 line '<tag />' 


 134:             //See if the key is already taken. 


 135:             if(!isset($current[$tag])) { //New Key 


 136:                 $current[$tag] = $result;


 137:                 $repeated_tag_index[$tag.'_'.$level] = 1;


 138:                 if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;


 139:


 140:             } else { //If taken, put all things inside a list(array) 


 141:                 if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array... 


 142:


 143:                     // ...push the new element into that array. 


 144:                     $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;


 145:


 146:                     if($priority == 'tag' and $get_attributes and $attributes_data) {


 147:                         $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;


 148:                     }


 149:                     $repeated_tag_index[$tag.'_'.$level]++;


 150:


 151:                 } else { //If it is not an array... 


 152:                     $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value 


 153:                     $repeated_tag_index[$tag.'_'.$level] = 1;


 154:                     if($priority == 'tag' and $get_attributes) {


 155:                         if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well 


 156:


 157:                             $current[$tag]['0_attr'] = $current[$tag.'_attr'];


 158:                             unset($current[$tag.'_attr']);


 159:                         }


 160:


 161:                         if($attributes_data) {


 162:                             $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;


 163:                         }


 164:                     }


 165:                     $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken 


 166:                 }


 167:             }


 168:


 169:         } elseif($type == 'close') { //End of tag '</tag>' 


 170:             $current = &$parent[$level-1];


 171:         }


 172:     }


 173:


 174:     return($xml_array);


 175: }


 176: ?>




Comments

Popular posts from this blog

The Beer Belly

T-Rextasy