4 * This script will get mails and store it into artifact DB
6 * Copyright 2004 GForge, LLC
9 * @author Tim Perdue tim@gforge.org
11 * @author Francisco Gimeno <kikov@kikov.org>
13 * This file is part of GForge.
15 * GForge is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * GForge is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with GForge; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * This file is based on forum_gateway.php
32 require_once ('squal_pre.php');
33 require_once ('common/include/Group.class.php');
34 require_once ('common/include/MailParser.class.php');
35 require_once ('common/tracker/Artifact.class.php');
36 require_once ('common/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();
84 * function - Copy mail(from stdin to tmp and return the tmp file
86 * @return tmp file name
88 function copyMailTmp() {
89 // Unfortunatly we need a temp file
90 // mailparse needs to read content several times
91 $tmpfile = tempnam ("/tmp", "artifact_gateway.".rand()."-".rand());
92 $in = fopen("php://stdin", "r");
93 $out = fopen($tmpfile, "w");
95 while($buffer = fgets($in, 4096)) {
107 * function - Parse mail and fill all kinds of head and body info
109 * @param string tmp file name
110 * @return boolean true if success
112 function parseMail($input_file) {
115 if (!$mp = new MailParser($input_file)) {
116 $this->setError('Error In MailParser');
118 } elseif ($mp->isError()) {
119 $this->setError('Error In MailParser '.$mp->getErrorMessage());
120 // even if it is an error, try to get the address of the sender so we
121 // can send him back the error
122 $this->FromEmail = $mp->getFromEmail();
126 $this->FromEmail = $mp->getFromEmail();
128 //subjects are in this required format: '[group - tracker_name][123456] My Subject'
129 //where 123456 is the artifact_id of the artifact message.
130 //we parse that ID to get the artifact that this should post to
132 $subj = $mp->getSubject();
133 if (ereg('(\[)([0-9]*)(\])',$subj,$arr)) {
134 $this->ArtifactId=$arr[2];
135 $artifactid_end=(strpos($subj,'['.$arr[2].']')) + strlen('['.$arr[2].']');
136 $this->Subject = addslashes(substr($subj,$artifactid_end));
138 $this->Subject = addslashes($subj);
139 $this->ArtifactId=0; // Not supported at the moment
140 $this->setError("ArtifactId needed at the moment. Artifact creation not supported");
144 $body = addslashes($mp->getBody());
145 // find first occurrence of the marker in the message
146 $begin = strpos($body, ARTIFACT_MAIL_MARKER);
147 if ($begin === false) {
148 $this->setError("Response message wasn't found in your mail. Please verify that ".
149 "you entered your message between the correct text markers.".
150 "\nYour message was:".
151 "\n".$mp->getBody());
154 // get the part of the message located after the marker
155 $body = substr($body, $begin+strlen(ARTIFACT_MAIL_MARKER));
156 // now look for the ending marker
157 $end = strpos($body, ARTIFACT_MAIL_MARKER);
158 if ($end === false) {
159 $this->setError("Response message wasn't found in your mail. Please verify that ".
160 "you entered your message between the correct text markers.".
161 "\nYour message was:".
162 "\n".$mp->getBody());
165 $message = substr($body, 0, $end);
166 $message = trim($message);
168 // maybe the last line was "> (ARTIFACT_MAIL_MARKER)". In that case, delete the last ">"
169 $message = preg_replace('/>$/', '', $message);
170 $this->Message = $message;
176 * Insert data into the tracker db
178 * @return - true or false
180 function addMessage() {
184 $user_id = $this->getUserId();
187 // Set up this user's session before posting
189 session_set_new($user_id);
192 $Artifact =& $this->getArtifact();
193 if (!$Artifact || !is_object($Artifact)) {
194 $this->setError("Could Not Get Artifact");
197 if (!$user_id && !$Artifact->ArtifactType->allowsAnon()) {
198 $this->setError("Could Not Match Sender Email Address to User and Tracker Does Not Allow Anonymous Posts");
203 // Create artifact message
205 if ( !$Artifact->addMessage($this->Message,$this->FromName,true) )
207 $this->setError("ArtifactMessage Error:".$Artifact->getErrorMessage());
214 /*------------------------------------------------------------------------
216 *-----------------------------------------------------------------------*/
218 /* Find user_id from email */
219 function getUserId() {
220 // Find User id using email
221 // If no user id, user id is 0;
222 $sql = "SELECT user_id FROM users
223 WHERE lower(email) ='".strtolower($this->FromEmail)."' AND status='A'";
224 $res = db_query($sql);
225 if (!$res || db_numrows($res) < 1) {
228 $user_id = db_result($res,0,'user_id');
230 db_free_result($res);
235 function &getArtifact() {
238 // $Group not needed, but let the code here to support
239 // tracker additions in the Future
240 $Group =& group_get_object_by_name($argv[1]);
241 if (!$Group || !is_object($Group)) {
242 $this->setError('Could Not Get Group Object');
244 } elseif ($Group->isError()) {
245 $this->setError('Getting Group Object: '.$Group->getErrorMessage());
248 // DBG("Artifact_get_object(".$this->ArtifactId.");");
249 $this->Artifact =& artifact_get_object($this->ArtifactId);
251 return $this->Artifact;
258 * Simple debugging printput
260 * Add this in /etc/syslog.conf and see /var/log/debug file:
262 * *.=debug /var/log/debug
269 system("echo \"artifact: ".$str."\n\" >> /tmp/tracker.log");
270 syslog(LOG_DEBUG, "artifact_gateway: ". $str);
271 } else if ($debug==2) {
279 $myTrackerGateway = new TrackerGateway();
280 if ($myTrackerGateway->isError()) {
281 if ($myTrackerGateway->FromEmail) {
282 mail ($myTrackerGateway->FromEmail,'Tracker Post Rejected',$myTrackerGateway->getErrorMessage());
284 DBG('Final Message: '.$myTrackerGateway->getErrorMessage());