4 * This script will get mails and store it into forum DB
6 * Copyright 2004 GForge, LLC
8 * @author Tim Perdue tim@gforge.org
11 * This file is part of FusionForge. FusionForge is free software;
12 * you can redistribute it and/or modify it under the terms of the
13 * GNU General Public License as published by the Free Software
14 * Foundation; either version 2 of the Licence, or (at your option)
17 * FusionForge is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 require dirname(__FILE__).'/../../common/include/env.inc.php';
28 require_once $gfcommon.'include/pre.php';
29 require_once $gfcommon.'include/Group.class.php';
30 require_once $gfcommon.'include/MailParser.class.php';
31 require_once $gfcommon.'forum/Forum.class.php';
32 require_once $gfcommon.'forum/ForumMessage.class.php';
34 class ForumGateway extends FFError {
54 function __construct() {
55 parent::__construct();
57 /* Copy mail message to tmp file */
58 $tmpfile = $this->copyMailTmp();
59 //DBG("Tmpname: ". $tmpfile);
62 $ret = $this->parseMail($tmpfile);
64 /* Delete temp file */
67 /* Check the return variable from parseMail */
72 /* add the info to forum */
73 $ret = $this->addMessage();
83 * function - Copy mail(from stdin to tmp and return the tmp file
85 * @return tmp file name
87 function copyMailTmp() {
88 // Unfortunatly we need a temp file
89 // mailparse needs to read content several times
90 $tmpfile = tempnam ("/tmp", "forum_gateway.".util_randnum()."-".util_randnum());
91 $in = fopen("php://stdin", "r");
92 $out = fopen($tmpfile, "w");
94 while($buffer = fgets($in, 4096)) {
106 * function - Parse mail and fill all kinds of head and body info
108 * @param string tmp file name
109 * @return boolean true if success
111 function parseMail($input_file) {
113 if (!$mp = new MailParser($input_file)) {
114 $this->setError('Error In MailParser');
116 } elseif ($mp->isError()) {
117 $this->setError('Error In MailParser '.$mp->getErrorMessage());
118 // even if it is an error, try to get the address of the sender so we
119 // can send him back the error
120 $this->FromEmail = $mp->getFromEmail();
124 $this->FromEmail = $mp->getFromEmail();
125 $subj = $mp->getSubject();
126 if ($mp->isError()) {
127 $this->setError($mp->getErrorMessage());
129 if ($subj === false) {
132 if (preg_match('/(\[)([0-9]*)(\])/',$subj,$arr)) {
133 $this->Parent=$arr[2];
134 $parent_end=(strpos($subj,'['.$arr[2].']')) + strlen('['.$arr[2].']');
135 $this->Subject = substr($subj,$parent_end);
137 $this->Subject = $subj;
140 $this->Body =& $mp->getBody();
142 $begin = strpos($this->Body, FORUM_MAIL_MARKER);
143 if ($begin === false) { //do nothing
144 $this->Message = $this->Body;
147 // get the part of the message located after the marker
148 $this->Body = substr($this->Body, $begin+strlen(FORUM_MAIL_MARKER));
149 // now look for the ending marker
150 $end = strpos($this->Body, FORUM_MAIL_MARKER);
151 if ($end === false) {
154 $message = substr($this->Body, 0, $end);
155 $message = trim($message);
157 // maybe the last line was "> (FORUM_MAIL_MARKER)". In that case, delete the last ">"
158 $message = preg_replace('/>$/', '', $message);
159 $this->Message = $message;
165 * Insert data into the forum db
169 function addMessage() {
173 $user_id = $this->getUserId();
176 // Set up this user's session before posting
178 session_set_new($user_id);
181 //DBG( "AddMessage 1\n");
182 $Forum =& $this->getForum();
183 if (!$Forum || !is_object($Forum)) {
184 $this->setError("Could Not Get Forum");
186 } elseif ($Forum->isError()) {
187 $this->setError("Forum Error: ".$Forum->getErrorMessage());
190 if (!$user_id && !$Forum->AllowAnonymous()) {
191 $this->setError("Could Not Match Sender Email Address to User and Forum Does Not Allow Anonymous Posts");
195 //DBG( "AddMessage 2\n");
197 // Create a blank forum message
199 $ForumMessage = new ForumMessage($Forum);
200 if (!$ForumMessage || !is_object($Forum)) {
201 $this->setError("Could Not Get Forum Message");
203 } elseif ($ForumMessage->isError()) {
204 $this->setError("ForumMessage Error: ".$ForumMessage->getErrorMessage());
207 //DBG( "AddMessage 3\n");
208 if ($this->Message!=""){
209 if (!$ForumMessage->create($this->Subject,$this->Message,$this->ThreadId,$this->Parent)) {
210 //DBG( "AddMessage 4.".$ForumMessage->getErrorMessage()."\n");
211 $this->setError("ForumMessage Create Error: ".$ForumMessage->getErrorMessage());
214 //DBG( "AddMessage 5.".$ForumMessage->getErrorMessage()."\n");
223 /*------------------------------------------------------------------------
225 *-----------------------------------------------------------------------*/
227 /* Find user_id from email */
228 function getUserId() {
229 // Find User id using email
230 $from = strtolower($this->FromEmail);
231 // If no user id, user id is 0;
235 $res = db_query_params ('SELECT user_id FROM users
236 WHERE lower(email) = $1 AND status = $2',
237 array (strtolower($from),
239 if (!$res || db_numrows($res) < 1) {
242 $user_id = db_result($res,0,'user_id');
244 db_free_result($res);
249 function &getForum() {
252 if ($this->Forum==-1) {
253 $Group = group_get_object_by_name($argv[1]);
254 if (!$Group || !is_object($Group)) {
255 $this->setError('Could Not Get Group Object');
257 } elseif ($Group->isError()) {
258 $this->setError('Getting Group Object: '.$Group->getErrorMessage());
263 // Find Forum id by parent
265 $res = db_query_params ('SELECT group_forum_id,thread_id
268 array ($this->Parent));
271 // Find forum by arguments passed by aliases file
273 $res = db_query_params ('SELECT group_forum_id, 0 AS thread_id
274 FROM forum_group_list
280 if (!$res || db_numrows($res) < 1) {
281 $this->setError('Getting Forum IDs: '.db_error());
284 $this->ForumId = db_result($res,0,'group_forum_id');
285 $this->ThreadId = db_result($res,0,'thread_id');
286 db_free_result($res);
288 $this->Forum = new Forum($Group,$this->ForumId);
298 * Simple debugging printput
300 * Add this in /etc/syslog.conf and see /var/log/debug file:
302 * *.=debug /var/log/debug
308 file_put_contents('/tmp/forum.log', "forum: ".$str."\n", FILE_APPEND);
311 syslog(LOG_DEBUG, "forum_gateway: ". $str);
312 } elseif ($debug==2) {
320 $listforum = new ForumGateway();
321 if ($listforum->isError()) {
322 mail ($listforum->FromEmail,'Forum Post Rejected',$listforum->getErrorMessage());
323 DBG('Final Message: '.$listforum->getErrorMessage());