4 * This script will get mails and store it into artifact DB
6 * Copyright 2004 GForge, LLC
7 * Copyright 2009, Roland Mas
9 * @author Tim Perdue tim@gforge.org
11 * @author Francisco Gimeno <kikov@kikov.org>
13 * This file is based on forum_gateway.php
15 * This file is part of FusionForge. FusionForge is free software;
16 * you can redistribute it and/or modify it under the terms of the
17 * GNU General Public License as published by the Free Software
18 * Foundation; either version 2 of the Licence, or (at your option)
21 * FusionForge is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License along
27 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
28 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 require dirname(__FILE__).'/../www/env.inc.php';
32 require_once $gfcommon.'include/pre.php';
33 require_once $gfcommon.'include/Group.class.php';
34 require_once $gfcommon.'include/MailParser.class.php';
35 require_once $gfcommon.'tracker/Artifact.class.php';
36 require_once $gfcommon.'tracker/ArtifactFactory.class.php';
38 class TrackerGateway extends Error {
55 function TrackerGateway() {
58 /* Copy mail message to tmp file */
59 $tmpfile = $this->copyMailTmp();
60 //DBG("Tmpname: ". $tmpfile);
63 $ret = $this->parseMail($tmpfile);
65 /* Delete temp file */
68 /* Check the return variable from parseMail */
73 /* add the info to tracker */
74 $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", "artifact_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();
126 //subjects are in this required format: '[group - tracker_name][123456] My Subject'
127 //where 123456 is the artifact_id of the artifact message.
128 //we parse that ID to get the artifact that this should post to
130 $subj = $mp->getSubject();
131 if (preg_match('/(\[)([0-9]*)(\])/',$subj,$arr)) {
132 $this->ArtifactId=$arr[2];
133 $artifactid_end=(strpos($subj,'['.$arr[2].']')) + strlen('['.$arr[2].']');
134 $this->Subject = substr($subj,$artifactid_end);
136 $this->FromEmail = ''; // Do not reply if no pattern found.
137 $this->Subject = $subj;
138 $this->ArtifactId=0; // Not supported at the moment
139 $this->setError("ArtifactId needed at the moment. Artifact creation not supported");
143 $body = $mp->getBody();
144 // find first occurrence of the marker in the message
145 $begin = strpos($body, ARTIFACT_MAIL_MARKER);
146 if ($begin === false) {
147 $this->setError("Response message wasn't found in your mail. Please verify that ".
148 "you entered your message between the correct text markers.".
149 "\nYour message was:".
150 "\n".$mp->getBody());
153 // get the part of the message located after the marker
154 $body = substr($body, $begin+strlen(ARTIFACT_MAIL_MARKER));
155 // now look for the ending marker
156 $end = strpos($body, ARTIFACT_MAIL_MARKER);
157 if ($end === false) {
158 $this->setError("Response message wasn't found in your mail. Please verify that ".
159 "you entered your message between the correct text markers.".
160 "\nYour message was:".
161 "\n".$mp->getBody());
164 $message = substr($body, 0, $end);
165 $message = trim($message);
167 // maybe the last line was "> (ARTIFACT_MAIL_MARKER)". In that case, delete the last ">"
168 $message = preg_replace('/>$/', '', $message);
169 $this->Message = $message;
175 * Insert data into the tracker db
179 function addMessage() {
183 $user_id = $this->getUserId();
184 $Artifact =& $this->getArtifact();
185 if (!$Artifact || !is_object($Artifact)) {
186 $this->setError("Could Not Get Artifact");
191 // Set up this user's session before posting
193 session_set_new($user_id);
194 if (!forge_check_perm_for_user ($user_id, 'tracker',$this->ArtifactType->getID(),'submit')) {
195 $this->setError(_('This user is not allowed to submit items to this tracker.'));
198 } elseif (!forge_check_perm ('tracker',$this->ArtifactType->getID(),'submit')) {
199 $this->setError(_('Could not match sender email address to user, and tracker does not allow anonymous posts.'));
204 // Create artifact message
206 if ( !$Artifact->addMessage($this->Message,$this->FromName,true) )
208 $this->setError("ArtifactMessage Error:".$Artifact->getErrorMessage());
215 /*------------------------------------------------------------------------
217 *-----------------------------------------------------------------------*/
219 /* Find user_id from email */
220 function getUserId() {
221 // Find User id using email
222 // If no user id, user id is 0;
223 $res = db_query_params ('SELECT user_id FROM users WHERE lower(email)=$1 AND status=$2',
224 array(strtolower($this->FromEmail),
226 if (!$res || db_numrows($res) < 1) {
229 $user_id = db_result($res,0,'user_id');
231 db_free_result($res);
236 function &getArtifact() {
239 // $Group not needed, but let the code here to support
240 // tracker additions in the Future
241 $Group = group_get_object_by_name($argv[1]);
242 if (!$Group || !is_object($Group)) {
243 $this->setError('Could Not Get Group Object');
245 } elseif ($Group->isError()) {
246 $this->setError('Getting Group Object: '.$Group->getErrorMessage());
249 // DBG("Artifact_get_object(".$this->ArtifactId.");");
250 $this->Artifact =& artifact_get_object($this->ArtifactId);
252 return $this->Artifact;
259 * Simple debugging printput
261 * Add this in /etc/syslog.conf and see /var/log/debug file:
263 * *.=debug /var/log/debug
270 file_put_contents('/tmp/tracker-gateway.log', "artifact: ".$str."\n", FILE_APPEND);
271 syslog(LOG_DEBUG, "artifact_gateway: ". $str);
272 } elseif ($debug==2) {
279 $myTrackerGateway = new TrackerGateway();
280 if ($myTrackerGateway->isError()) {
281 DBG ("From: ". $myTrackerGateway->FromEmail);
282 DBG ("Subject: ". $myTrackerGateway->Subject);
283 if ($myTrackerGateway->FromEmail) {
284 mail ($myTrackerGateway->FromEmail,'Tracker Post Rejected',$myTrackerGateway->getErrorMessage());
286 DBG('Final Message: '.$myTrackerGateway->getErrorMessage());