piler/webui/model/google/google.php

173 lines
4.6 KiB
PHP
Raw Normal View History

<?php
class ModelGoogleGoogle extends Model {
2013-09-17 12:04:24 +02:00
//Holds the Zend Storage
private $storage;
private function constructAuthString($email, $accessToken) {
return base64_encode("user=$email\1auth=Bearer $accessToken\1\1");
}
public function oauth2Authenticate($imap, $email, $accessToken) {
$authenticateParams = array('XOAUTH2', $this->constructAuthString($email, $accessToken));
$imap->sendRequest('AUTHENTICATE', $authenticateParams);
while (true) {
$response = "";
$is_plus = $imap->readLine($response, '+', true);
if($is_plus) {
error_log("got an extra server challenge: $response");
// Send empty client response.
$imap->sendRequest('');
} else {
if(preg_match('/^NO /i', $response) ||
preg_match('/^BAD /i', $response)) {
error_log("got failure response: $response");
return false;
} else if (preg_match("/^OK /i", $response)) {
return true;
} else {
// Some untagged response, such as CAPABILITY
}
}
}
}
public function try_imap_login($email, $accessToken) {
$imap = new Zend_Mail_Protocol_Imap('imap.gmail.com', '993', true);
if($this->oauth2Authenticate($imap, $email, $accessToken)) {
return $imap;
} else {
return '';
}
}
2014-04-18 21:28:53 +02:00
2013-09-17 12:04:24 +02:00
// Save all Messages from selected folder
2013-09-17 12:04:24 +02:00
private function saveMessages($email) {
2014-04-18 21:28:53 +02:00
$last_msg_id = -1;
$from = 1;
$count = 0;
$storage = $this->storage;
2014-04-18 21:28:53 +02:00
$from = $this->get_last_message_id_by_unique_id($email, $storage) + 1;
2014-04-18 21:28:53 +02:00
//print "will download messages from: $from\n";
2014-04-18 21:28:53 +02:00
$num = $storage->countMessages();
2014-04-18 21:28:53 +02:00
$to = $from;
2014-04-18 21:28:53 +02:00
while($from <= $num) {
if($num - $from > 9) { $delta = 9; }
else { $delta = $num-$from; }
2014-04-18 21:28:53 +02:00
$to = $from + $delta;
2014-04-18 21:28:53 +02:00
//print "downloading $from, $to\n";
2014-04-18 21:28:53 +02:00
$unique_msg_id = $storage->getUniqueId($to);
2014-04-18 21:28:53 +02:00
$messages = $storage->piler_batch_fetch($from, $to);
2014-04-18 21:28:53 +02:00
while(list($k, $v) = each($messages)) {
$uuid = $storage->getUniqueId($k);
2014-04-18 21:28:53 +02:00
$tmpname = "piler-" . $email . "-" . $k . "-" . $uuid . ".eml";
$f = fopen(DIR_TMP . "/" . $tmpname, "w+");
if($f){
fwrite($f, $v['RFC822.HEADER'] . $v['RFC822.TEXT']);
fclose($f);
2012-09-28 14:15:45 +02:00
2014-04-18 21:28:53 +02:00
rename(DIR_TMP . "/" . $tmpname, DIR_IMAP . "/" . $tmpname);
2012-09-28 14:15:45 +02:00
2014-04-18 21:28:53 +02:00
$count++;
}
2014-04-18 21:28:53 +02:00
//print "k: $k\n";
}
2014-04-18 21:28:53 +02:00
$this->update_imap_table($email, $unique_msg_id, $to);
2014-04-18 21:28:53 +02:00
$from += $delta + 1;
}
2014-04-18 21:28:53 +02:00
syslog(LOG_INFO, "downloaded $count messages for $email");
2013-09-17 12:04:24 +02:00
2014-04-18 21:28:53 +02:00
return $count;
2013-09-17 12:04:24 +02:00
}
public function download_users_emails($email, $accessToken) {
2014-04-18 21:28:53 +02:00
$count = 0;
2013-09-17 12:04:24 +02:00
if(!$email || !$accessToken) { return 0; }
$imap = $this->try_imap_login($email, $accessToken);
if($imap) {
$this->storage = new Zend_Mail_Storage_Imap($imap);
//$storage->selectFolder('[Gmail]/INBOX');
$folders = new RecursiveIteratorIterator($this->storage->getFolders(),
RecursiveIteratorIterator::SELF_FIRST);
2014-04-18 21:28:53 +02:00
foreach ($folders as $localName => $folder) {
2013-09-17 12:04:24 +02:00
2014-04-18 21:28:53 +02:00
if($folder->isSelectable()) {
2013-09-17 12:04:24 +02:00
$this->storage->selectFolder($folder);
2014-04-18 21:28:53 +02:00
$count += $this->saveMessages($email);
2013-09-17 12:04:24 +02:00
}
2014-04-18 21:28:53 +02:00
}
}
2014-04-24 10:18:29 +02:00
if($count > 0) { $this->run_import_command(); }
2014-04-18 21:28:53 +02:00
return $count;
}
private function get_last_message_id_by_unique_id($email = '', $storage) {
if($email == '') { return 0; }
$query = $this->db->query("SELECT last_msg_id FROM " . TABLE_GOOGLE_IMAP . " WHERE email=? ORDER BY last_msg_id DESC", array($email));
if(isset($query->rows)) {
foreach ($query->rows as $q) {
try {
$num = $storage->getNumberByUniqueId($q['last_msg_id']);
return $num;
}
catch(Exception $exception) {}
}
}
return 0;
}
private function update_imap_table($email, $unique_msg_id, $id) {
$query = $this->db->query("INSERT INTO " . TABLE_GOOGLE_IMAP . " (id, email, last_msg_id) VALUES(?,?,?)", array($id, $email, $unique_msg_id));
return $this->db->countAffected();
}
2014-04-24 10:18:29 +02:00
private function run_import_command() {
syslog(LOG_INFO, "importing emails ...");
system(PILERIMPORT_IMAP_COMMAND, $val);
syslog(LOG_INFO, "importing emails done");
}
}
?>