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 Error {
54 function ForumGateway() {
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();
127 $this->setError($mp->getErrorMessage());
131 if (preg_match('/(\[)([0-9]*)(\])/',$subj,$arr)) {
132 $this->Parent=$arr[2];
133 $parent_end=(strpos($subj,'['.$arr[2].']')) + strlen('['.$arr[2].']');
134 $this->Subject = substr($subj,$parent_end);
136 $this->Subject = $subj;
139 $this->Body =& $mp->getBody();
141 $begin = strpos($this->Body, FORUM_MAIL_MARKER);
142 if ($begin === false) { //do nothing
143 $this->Message = $this->Body;
146 // get the part of the message located after the marker
147 $this->Body = substr($this->Body, $begin+strlen(FORUM_MAIL_MARKER));
148 // now look for the ending marker
149 $end = strpos($this->Body, FORUM_MAIL_MARKER);
150 if ($end === false) {
153 $message = substr($this->Body, 0, $end);
154 $message = trim($message);
156 // maybe the last line was "> (FORUM_MAIL_MARKER)". In that case, delete the last ">"
157 $message = preg_replace('/>$/', '', $message);
158 $this->Message = $message;
164 * Insert data into the forum db
168 function addMessage() {
172 $user_id = $this->getUserId();
175 // Set up this user's session before posting
177 session_set_new($user_id);
180 //DBG( "AddMessage 1\n");
181 $Forum =& $this->getForum();
182 if (!$Forum || !is_object($Forum)) {
183 $this->setError("Could Not Get Forum");
185 } elseif ($Forum->isError()) {
186 $this->setError("Forum Error: ".$Forum->getErrorMessage());
189 if (!$user_id && !$Forum->AllowAnonymous()) {
190 $this->setError("Could Not Match Sender Email Address to User and Forum Does Not Allow Anonymous Posts");
194 //DBG( "AddMessage 2\n");
196 // Create a blank forum message
198 $ForumMessage = new ForumMessage($Forum);
199 if (!$ForumMessage || !is_object($Forum)) {
200 $this->setError("Could Not Get Forum Message");
202 } elseif ($ForumMessage->isError()) {
203 $this->setError("ForumMessage Error: ".$ForumMessage->getErrorMessage());
206 //DBG( "AddMessage 3\n");
207 if ($this->Message!=""){
208 if (!$ForumMessage->create($this->Subject,$this->Message,$this->ThreadId,$this->Parent)) {
209 //DBG( "AddMessage 4.".$ForumMessage->getErrorMessage()."\n");
210 $this->setError("ForumMessage Create Error: ".$ForumMessage->getErrorMessage());
213 //DBG( "AddMessage 5.".$ForumMessage->getErrorMessage()."\n");
222 /*------------------------------------------------------------------------
224 *-----------------------------------------------------------------------*/
226 /* Find user_id from email */
227 function getUserId() {
228 // Find User id using email
229 $from = strtolower($this->FromEmail);
230 // If no user id, user id is 0;
231 if (! $from) return 0;
232 $res = db_query_params ('SELECT user_id FROM users
233 WHERE lower(email) = $1 AND status = $2',
234 array (strtolower($from),
236 if (!$res || db_numrows($res) < 1) {
239 $user_id = db_result($res,0,'user_id');
241 db_free_result($res);
246 function &getForum() {
249 if ($this->Forum==-1) {
250 $Group = group_get_object_by_name($argv[1]);
251 if (!$Group || !is_object($Group)) {
252 $this->setError('Could Not Get Group Object');
254 } elseif ($Group->isError()) {
255 $this->setError('Getting Group Object: '.$Group->getErrorMessage());
260 // Find Forum id by parent
262 $res = db_query_params ('SELECT group_forum_id,thread_id
265 array ($this->Parent));
268 // Find forum by arguments passed by aliases file
270 $res = db_query_params ('SELECT group_forum_id, 0 AS thread_id
271 FROM forum_group_list
277 if (!$res || db_numrows($res) < 1) {
278 $this->setError('Getting Forum IDs: '.db_error());
281 $this->ForumId = db_result($res,0,'group_forum_id');
282 $this->ThreadId = db_result($res,0,'thread_id');
283 db_free_result($res);
285 $this->Forum = new Forum($Group,$this->ForumId);
295 * Simple debugging printput
297 * Add this in /etc/syslog.conf and see /var/log/debug file:
299 * *.=debug /var/log/debug
305 file_put_contents('/tmp/forum.log', "forum: ".$str."\n", FILE_APPEND);
308 syslog(LOG_DEBUG, "forum_gateway: ". $str);
309 } elseif ($debug==2) {
317 $listforum = new ForumGateway();
318 if ($listforum->isError()) {
319 mail ($listforum->FromEmail,'Forum Post Rejected',$listforum->getErrorMessage());
320 DBG('Final Message: '.$listforum->getErrorMessage());