added google xoauth2 support + fixed a journaling issue

This commit is contained in:
SJ
2012-09-28 10:34:04 +02:00
parent f930aacc84
commit 55de53bb26
111 changed files with 38812 additions and 4 deletions

View File

@ -0,0 +1,133 @@
<?php
class ModelGoogleGoogle extends Model {
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 '';
}
}
public function download_users_emails($email, $accessToken) {
$last_msg_id = -1;
$from = 1;
if(!$email || !$accessToken) { return 0; }
$imap = $this->try_imap_login($email, $accessToken);
if($imap) {
$storage = new Zend_Mail_Storage_Imap($imap);
$storage->selectFolder('[Gmail]/All Mail');
$from = $this->get_last_message_id_by_unique_id($email, $storage) + 1;
//print "will download messages from: $from\n";
$num = $storage->countMessages();
$to = $from;
while($from <= $num) {
if($num - $from > 9) { $delta = 9; }
else { $delta = $num-$from; }
$to = $from + $delta;
//print "downloading $from, $to\n";
$unique_msg_id = $storage->getUniqueId($to);
$messages = $storage->piler_batch_fetch($from, $to);
while(list($k, $v) = each($messages)) {
$uuid = $storage->getUniqueId($k);
$tmpname = DIR_TMP . "piler-" . $email . "-" . $k . "-" . $uuid . ".eml";
$f = fopen($tmpname, "w+");
if($f){
fwrite($f, $v['RFC822.HEADER'] . $v['RFC822.TEXT']);
fclose($f);
}
//print "k: $k\n";
}
$this->update_imap_table($email, $unique_msg_id, $to);
$from += $delta + 1;
}
}
}
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();
}
}
?>

View File

@ -0,0 +1,85 @@
<?php
class ModelUserGoogle extends Model {
public function check_for_account($google_account = array()) {
$query = $this->db->query("SELECT " . TABLE_USER . ".username, " . TABLE_USER . ".uid, " . TABLE_USER . ".realname, " . TABLE_USER . ".dn, " . TABLE_USER . ".password, " . TABLE_USER . ".isadmin, " . TABLE_USER . ".domain FROM " . TABLE_USER . ", " . TABLE_EMAIL . " WHERE " . TABLE_EMAIL . ".email=? AND " . TABLE_EMAIL . ".uid=" . TABLE_USER . ".uid", array($google_account['email']));
if($query->num_rows == 1) {
$user = $query->row;
}
else {
/*
[id] => 117316069531814989987
[email] => forum@hfp.hu
[verified_email] => 1
[name] => Forum Admin
[given_name] => Forum
[family_name] => Admin
*/
$d = explode('@', $google_account['email']);
$user['uid'] = $this->model_user_user->get_next_uid();
$user['username'] = $google_account['email'];
$user['realname'] = $google_account['name'];
$user['email'] = $google_account['email'];
$user['domain'] = $d[1];
$user['dn'] = '*';
$user['isadmin'] = 0;
$user['password'] = generate_random_string(12);
$user['group'] = '';
$user['folder'] = '';
$this->model_user_user->add_user($user);
}
$_SESSION['username'] = $user['username'];
$_SESSION['uid'] = $user['uid'];
$_SESSION['admin_user'] = 0;
$_SESSION['email'] = $user['username'];
$_SESSION['domain'] = $query->row['domain'];
$_SESSION['realname'] = $query->row['realname'];
$_SESSION['emails'] = $this->model_user_user->get_users_all_email_addresses($user['uid']);
$_SESSION['folders'] = $this->model_folder_folder->get_all_folder_ids($user['uid']);
$_SESSION['extra_folders'] = $this->model_folder_folder->get_all_extra_folder_ids($user['uid']);
AUDIT(ACTION_LOGIN, $user['username'], '', '', 'successful auth against Google');
}
public function update_tokens($email = '', $id = 0, $token = array()) {
if($email == '') { return 0; }
$query = $this->db->query("SELECT email FROM " . TABLE_GOOGLE . " WHERE email=?", array($email));
if($query->num_rows > 0) {
$query = $this->db->query("UPDATE " . TABLE_GOOGLE . " SET id=?, access_token=?, refresh_token=?, created=? WHERE email=?", array($id, $token->{'access_token'}, $token->{'refresh_token'}, $token->{'created'}, $email));
}
else {
$query = $this->db->query("INSERT INTO " . TABLE_GOOGLE . " (id, email, access_token, refresh_token, created) VALUES(?,?,?,?,?)", array($id, $email, $token->{'access_token'}, $token->{'refresh_token'}, $token->{'created'}));
}
return $this->db->countAffected();
/*
[access_token] => ya29.AHES6ZSavh4CnWXyfAYRNwqqZ3FmZ-bHPZkWIEPlutG6K_E
[token_type] => Bearer
[expires_in] => 3600
[refresh_token] => 1/J7bwdfCfQkjCu3Q51ypdJeGOzOtw6F1uyg1vaAwOZ2Q
[created] => 1348415267
*/
}
}
?>