3 # Copyright (c) STMicroelectronics, 2005. All Rights Reserved.
5 # Originally written by Jean-Philippe Giola, 2005
7 # This file is a part of codendi.
9 # codendi is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # codendi is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with this program; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 /* This script allows the transfer of an mbox-formatted mail to ForumML database.
27 * First argument: mailing-list name
28 * Second argument: type of transfer, depending on the input
29 * '1': transfer from '/var/run/forumml/mail_tmp_xyz' temporary file (1-message mbox file)
30 * '2': transfer from whole list archive (real mbox file)
31 * Third argument: temporary file name (used when 2nd arg = 1)
34 ini_set('max_execution_time', 0);
35 ini_set('memory_limit', -1);
37 require_once 'pre.php';
38 error_reporting(E_ALL);
39 require 'Mail/Mbox.php';
40 require_once(dirname(__FILE__).'/../include/ForumML_mimeDecode.class.php');
41 require_once(dirname(__FILE__).'/../include/ForumMLInsert.class.php');
42 require_once(dirname(__FILE__).'/../include/ForumML_FileStorage.class.php');
43 require_once 'common/plugin/PluginManager.class.php';
44 require_once 'plugins_utils.php';
45 require_once 'www/mail/mail_utils.php';
46 require_once 'utils.php';
49 // get list id and group id from list name
50 $sql = "SELECT group_id, group_list_id FROM mail_group_list WHERE list_name=$1";
51 $res = db_query_params($sql,array(db_escape_string($list)));
52 if (db_numrows($res) > 0) {
53 $id_list = db_result($res,0,'group_list_id');
54 $gr_id = db_result($res,0,'group_id');
56 $stderr = fopen('php://stderr', 'w');
57 fwrite($stderr, "Invalid mailing-list $list \n");
62 $plugin_manager =& PluginManager::instance();
63 $p =& $plugin_manager->getPluginByName('forumml');
64 if ($p && $plugin_manager->isPluginAvailable($p) ) {
65 $info =& $p->getPluginInfo();
68 $forumml_arch = $GLOBALS['forumml_arch'];
69 $mbox_file = $forumml_arch."/private/".$list.".mbox/".$list.".mbox";
70 // check if mbox file exists
71 if (! is_file($mbox_file)) {
72 $stderr = fopen('php://stderr', 'w');
73 fwrite($stderr, "Invalid mbox file $mbox_file \n");
78 // Do not import from archives if there are already messages for this list
79 $sql = "SELECT NULL FROM plugin_forumml_message WHERE id_list=$1 LIMIT 1";
80 $res = db_query_params($sql,array(db_ei($id_list)));
81 if ($res && db_numrows($res) > 0) {
82 $stderr = fopen('php://stderr', 'w');
83 fwrite($stderr, "Cannot import messages from archive.\nThere are already messages in the database for $list ($mbox_file)\n");
89 $temp_file = $argv[3];
90 // get temp file parent dir
91 $forumml_tmp = $GLOBALS['forumml_tmp'];
92 $mbox_file = $forumml_tmp."/".$temp_file;
93 print "mboxfile=".$mbox_file;
96 // Open the mail that has been temporary stored
97 $mbox = new Mail_Mbox($mbox_file);
99 if (PEAR::isError($mbox)) {
100 print "Unable to open mbox: ".$mbox->getMessage().PHP_EOL;
103 print "nb mail inserted=".$nbMailInserted;
104 $num_msg = $mbox->size();
105 for ($i = 0; $i < $num_msg; $i++) {
106 $thisMessage = $mbox->get($i);
107 if (PEAR::isError($thisMessage)) {
108 print "Unable to get message $i: ".$thisMessage->getMessage().PHP_EOL;
111 $args['include_bodies'] = TRUE;
112 $args['decode_bodies'] = TRUE;
113 $args['decode_headers'] = TRUE;
114 $args['crlf'] = "\r\n";
115 $decoder = new ForumML_mimeDecode($thisMessage, "\r\n");
116 $structure = $decoder->decode($args);
118 // Get ForumML storage
119 $forumml_dir = $GLOBALS['forumml_dir'];
120 $forumml_storage = new ForumML_FileStorage($forumml_dir);
123 $insert = new ForumMLInsert($id_list);
124 $msgId = $insert->storeEmail($structure, $forumml_storage);
131 // Display message when importing a mail archive
133 if ($num_msg == $nbMailInserted) {
134 echo 'Operation Completed.'.$num_msg.' imported'.PHP_EOL;
136 echo '*** Error: '.$num_msg.' in '.$mbox_file.' file but '. $nbMailInserted.' stored in database'.PHP_EOL;
142 // delete temporary file
144 if (is_file($mbox_file)) {