[ Index ]

PHP Cross Reference of Eventum

title

Body

[close]

/misc/ -> download_emails.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 encoding=utf-8: */
   3  // +----------------------------------------------------------------------+
   4  // | Eventum - Issue Tracking System                                      |
   5  // +----------------------------------------------------------------------+
   6  // | Copyright (c) 2003, 2004, 2005, 2006, 2007 MySQL AB                              |
   7  // |                                                                      |
   8  // | This program is free software; you can redistribute it and/or modify |
   9  // | it under the terms of the GNU General Public License as published by |
  10  // | the Free Software Foundation; either version 2 of the License, or    |
  11  // | (at your option) any later version.                                  |
  12  // |                                                                      |
  13  // | This program is distributed in the hope that it will be useful,      |
  14  // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
  15  // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
  16  // | GNU General Public License for more details.                         |
  17  // |                                                                      |
  18  // | You should have received a copy of the GNU General Public License    |
  19  // | along with this program; if not, write to:                           |
  20  // |                                                                      |
  21  // | Free Software Foundation, Inc.                                       |
  22  // | 59 Temple Place - Suite 330                                          |
  23  // | Boston, MA 02111-1307, USA.                                          |
  24  // +----------------------------------------------------------------------+
  25  // | Authors: João Prado Maia <jpm@mysql.com>                             |
  26  // +----------------------------------------------------------------------+
  27  //
  28  // @(#) $Id: download_emails.php 3266 2007-03-06 20:18:51Z glen $
  29  
  30  ini_set("memory_limit", "256M");
  31  
  32  require_once(dirname(__FILE__) . "/../init.php");
  33  require_once (APP_INC_PATH . "class.support.php");
  34  require_once (APP_INC_PATH . "class.lock.php");
  35  require_once (APP_INC_PATH . "class.project.php");
  36  require_once (APP_INC_PATH . "db_access.php");
  37  
  38  // we need the IMAP extension for this to work
  39  if (!function_exists('imap_open')) {
  40      echo "Error: Eventum requires the IMAP extension in order to download messages saved on a IMAP/POP3 mailbox.\n";
  41      echo "Please refer to the PHP manual for more details about how to enable the IMAP extension.\n";
  42      exit;
  43  }
  44  
  45  // determine if this script is being called from the web or command line
  46  $fix_lock = false;
  47  if (isset($_SERVER['HTTP_HOST'])) {
  48      // web
  49      $type = 'web';
  50      if (@$_GET['fix-lock'] == 1) {
  51          $fix_lock = true;
  52      }
  53      $username = @$_GET['username'];
  54      $hostname = @$_GET['hostname'];
  55      $mailbox = @$_GET['mailbox'];
  56  } else {
  57      // command line
  58      // argv/argc needs to be enabled
  59      if (ini_get("register_argc_argv") == "Off") {
  60          echo "Error: Eventum requires the ini setting register_argc_argv to be enabled to run this script.\n";
  61          echo "Please refer to the PHP manual for more details on how to change this ini setting.\n";
  62          exit;
  63      }
  64  
  65      $type = 'cli';
  66      if (in_array('--fix-lock', $argv)) {
  67          $fix_lock = true;
  68      }
  69      $username = @$argv[1];
  70      $hostname = @$argv[2];
  71      $mailbox = @$argv[3];
  72  }
  73  
  74  
  75  // check for the required parameters
  76  if (($fix_lock != true) && ((empty($username)) || (empty($hostname)))) {
  77      if ($type == 'cli') {
  78          echo "Error: Wrong number of parameters given. Expected parameters related to the email account:\n";
  79          echo " 1 - username\n";
  80          echo " 2 - hostname\n";
  81          echo " 3 - mailbox (only required if IMAP account)\n";
  82          echo "Example: php download_emails.php user example.com INBOX\n";
  83      } else {
  84          echo "Error: Wrong number of parameters given. Expected parameters related to email account:<br />\n";
  85          echo "download_emails.php?username=<i>username</i>&hostname=<i>hostname</i>&mailbox=<i>mailbox</i><br />";
  86      }
  87      exit;
  88  }
  89  
  90  // get the account ID since we need it for locking.
  91  $account_id = Email_Account::getAccountID($username, $hostname, $mailbox);
  92  if (($account_id == 0) && ($fix_lock != true)) {
  93      echo "Error: Could not find a email account with the parameter provided. Please verify your email account settings and try again.\n";
  94      exit;
  95  }
  96  
  97  if ($fix_lock == true) {
  98      // if there is no account id, unlock all accounts
  99      if (empty($account_id)) {
 100          $prj_ids = array_keys(Project::getAll());
 101          foreach ($prj_ids as $prj_id) {
 102              $ema_ids = Email_Account::getAssocList($prj_id);
 103              foreach ($ema_ids as $ema_id => $ema_title) {
 104                  Lock::release('download_emails_' . $ema_id);
 105              }
 106          }
 107      } else {
 108          Lock::release('download_emails_' . $account_id);
 109      }
 110      echo "The lock file was removed successfully.\n";
 111      exit;
 112  }
 113  
 114  // check if there is another instance of this script already running
 115  if (!Lock::acquire('download_emails_' . $account_id)) {
 116      if ($type == 'cli') {
 117          echo "Error: Another instance of the script is still running for the specified account. " .
 118                      "If this is not accurate, you may fix it by running this script with '--fix-lock' " .
 119                      "as the 4th parameter or you may unlock ALL accounts by running this script with '--fix-lock' " .
 120                      "as the only parameter.\n";
 121      } else {
 122          echo "Error: Another instance of the script is still running for the specified account. " .
 123                      "If this is not accurate, you may fix it by running this script with 'fix-lock=1' " .
 124                      "in the query string or you may unlock ALL accounts by running this script with 'fix-lock=1' " .
 125                      "as the only parameter.<br />\n";
 126      }
 127      exit;
 128  }
 129  
 130  $account = Email_Account::getDetails($account_id);
 131  $mbox = Support::connectEmailServer($account);
 132  if ($mbox == false) {
 133      echo "Error: Could not connect to the email server. Please verify your email account settings and try again.\n";
 134      Lock::release('download_emails_' . $account_id);
 135      exit;
 136  } else {
 137      $total_emails = Support::getTotalEmails($mbox);
 138  
 139      if ($total_emails > 0) {
 140          for ($i = 1; $i <= $total_emails; $i++) {
 141              Support::getEmailInfo($mbox, $account, $i);
 142          }
 143      }
 144      imap_expunge($mbox);
 145      Support::clearErrors();
 146  }
 147  
 148  // clear the lock
 149  Lock::release('download_emails_' . $account_id);


Generated: Wed Dec 19 21:21:33 2007 Cross-referenced by PHPXref 0.7