2012-09-28 14:15:45 +02:00
|
|
|
<?php
|
|
|
|
|
2018-11-03 16:35:41 +01:00
|
|
|
define('EMAIL_STR', 'email');
|
|
|
|
|
2014-01-16 23:02:23 +01:00
|
|
|
ini_set("session.save_path", "/tmp");
|
|
|
|
|
2012-09-28 14:15:45 +02:00
|
|
|
$webuidir = "";
|
2015-04-07 11:43:03 +02:00
|
|
|
$email = "";
|
2014-04-24 10:18:29 +02:00
|
|
|
$daemonize = 0;
|
|
|
|
|
|
|
|
|
|
|
|
$opts = 'hd::';
|
|
|
|
$lopts = array(
|
2015-04-07 11:43:03 +02:00
|
|
|
'webui:',
|
|
|
|
'email:'
|
2014-04-24 10:18:29 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if($options = getopt($opts, $lopts)) {
|
|
|
|
|
|
|
|
if(isset($options['webui'])) {
|
|
|
|
$webuidir = $options['webui'];
|
|
|
|
}
|
|
|
|
else {
|
2018-11-03 15:55:18 +01:00
|
|
|
echo "\nError: must provide path to WebUI directory\n\n";
|
2014-04-24 10:18:29 +02:00
|
|
|
exit;
|
|
|
|
}
|
2015-04-07 11:43:03 +02:00
|
|
|
|
2014-04-24 10:18:29 +02:00
|
|
|
if(isset($options['d'])) {
|
|
|
|
$daemonize = 1;
|
|
|
|
}
|
|
|
|
|
2018-11-03 16:35:41 +01:00
|
|
|
if(isset($options[EMAIL_STR])) {
|
|
|
|
$email = $options[EMAIL_STR];
|
2015-04-07 11:43:03 +02:00
|
|
|
}
|
|
|
|
|
2014-04-24 10:18:29 +02:00
|
|
|
}
|
2014-05-21 15:41:49 +02:00
|
|
|
else {
|
|
|
|
display_help();
|
|
|
|
exit;
|
|
|
|
}
|
2014-04-24 10:18:29 +02:00
|
|
|
|
|
|
|
|
2012-09-28 14:15:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
require_once($webuidir . "/config.php");
|
|
|
|
|
|
|
|
require(DIR_SYSTEM . "/startup.php");
|
|
|
|
|
|
|
|
require_once 'Zend/Mail/Protocol/Imap.php';
|
|
|
|
require_once 'Zend/Mail/Storage/Imap.php';
|
|
|
|
|
|
|
|
$request = new Request();
|
|
|
|
Registry::set("request", $request);
|
|
|
|
|
|
|
|
|
|
|
|
Registry::set('document', new Document());
|
|
|
|
|
|
|
|
|
|
|
|
$loader = new Loader();
|
|
|
|
Registry::set('load', $loader);
|
|
|
|
|
|
|
|
|
|
|
|
$language = new Language();
|
|
|
|
Registry::set('language', $language);
|
|
|
|
|
|
|
|
|
|
|
|
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE, DB_PREFIX);
|
|
|
|
Registry::set('DB_DATABASE', DB_DATABASE);
|
|
|
|
|
|
|
|
Registry::set('db', $db);
|
|
|
|
|
|
|
|
Registry::set('DB_DRIVER', DB_DRIVER);
|
|
|
|
|
|
|
|
|
|
|
|
$loader->model('user/google');
|
|
|
|
$loader->model('google/google');
|
|
|
|
|
|
|
|
openlog("piler-gmail-import", LOG_PID, LOG_MAIL);
|
|
|
|
|
2014-04-24 10:18:29 +02:00
|
|
|
if($daemonize == 1) {
|
|
|
|
while(1) {
|
|
|
|
syslog(LOG_INFO, "started another imap poll");
|
|
|
|
|
|
|
|
poll_imap_accounts();
|
|
|
|
|
|
|
|
sleep(300);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-04-07 11:43:03 +02:00
|
|
|
poll_imap_accounts($email);
|
2014-04-24 10:18:29 +02:00
|
|
|
}
|
|
|
|
|
2012-09-28 14:15:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-04-07 11:43:03 +02:00
|
|
|
function poll_imap_accounts($email = '') {
|
2014-04-24 10:18:29 +02:00
|
|
|
$db = Registry::get('db');
|
|
|
|
|
|
|
|
$ug = new ModelUserGoogle();
|
|
|
|
$g = new ModelGoogleGoogle();
|
|
|
|
|
|
|
|
|
2015-04-07 11:43:03 +02:00
|
|
|
if($email) {
|
|
|
|
$query = $db->query("SELECT email FROM " . TABLE_GOOGLE . " WHERE email=?", array($email));
|
|
|
|
} else {
|
|
|
|
$query = $db->query("SELECT email FROM " . TABLE_GOOGLE);
|
|
|
|
}
|
2014-04-24 10:18:29 +02:00
|
|
|
|
|
|
|
if(isset($query->rows)) {
|
|
|
|
foreach($query->rows as $q) {
|
2018-11-03 16:35:41 +01:00
|
|
|
$access_token = $ug->refresh_access_token($q[EMAIL_STR]);
|
|
|
|
$g->download_users_emails($q[EMAIL_STR], $access_token);
|
2014-04-24 10:18:29 +02:00
|
|
|
}
|
2012-09-28 14:15:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-24 10:18:29 +02:00
|
|
|
|
2014-05-21 15:41:49 +02:00
|
|
|
|
|
|
|
function display_help() {
|
|
|
|
$phpself = basename(__FILE__);
|
2018-11-03 15:55:18 +01:00
|
|
|
echo "\nUsage: $phpself --webui [PATH] [OPTIONS...]\n\n";
|
|
|
|
echo "\t--webui=\"[REQUIRED: path to the Piler WebUI Directory]\"\n\n";
|
|
|
|
echo "options:\n";
|
|
|
|
echo "\t--email: Email address to poll. Leave it empty to poll all email addresses\n";
|
|
|
|
echo "\t-d: Daemonize the imap polling\n";
|
|
|
|
echo "\t-h: Prints this help screen and exits\n";
|
2014-05-21 15:41:49 +02:00
|
|
|
}
|