1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
#! /usr/bin/php4 -f
<?php
/**
* This script will get mails and store it into artifact DB
*
* Copyright 2004 GForge, LLC
*
* @version $Id: tracker_gateway.php 4397 2005-06-03 03:14:19Z tperdue $
* @author Tim Perdue tim@gforge.org
* @author Sung Kim
* @author Francisco Gimeno <kikov@kikov.org>
*
* This file is part of GForge.
*
* GForge is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GForge is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GForge; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* This file is based on forum_gateway.php
*/
require_once ('squal_pre.php');
require_once ('common/include/Group.class');
require_once ('common/include/MailParser.class');
require_once ('common/tracker/Artifact.class');
require_once ('common/tracker/ArtifactFactory.class');
class TrackerGateway extends Error {
/*
* variables
*/
var $From = "";
var $FromName = "";
var $FromEmail = "";
var $Subject = "";
var $ListId = "";
var $Reference = "";
var $MsgId = "";
var $Sender="";
var $Message="";
var $IsFollowUp=0;
var $ArtifactId=-1;
var $Artifact=null;
function TrackerGateway() {
$this->Error();
/* Copy mail message to tmp file */
$tmpfile = $this->copyMailTmp();
//DBG("Tmpname: ". $tmpfile);
/* parse email */
$ret = $this->parseMail($tmpfile);
/* Delete temp file */
unlink($tmpfile);
/* Check the return variable from parseMail */
if (!$ret) {
return false;
}
/* add the info to tracker */
$ret = $this->addMessage();
if (!$ret) {
return false;
}
return true;
}
/**
* function - Copy mail(from stdin to tmp and return the tmp file
*
* @return tmp file name
*/
function copyMailTmp() {
// Unfortunatly we need a temp file
// mailparse needs to read content several times
$tmpfile = tempnam ("/tmp", "artifact_gateway.".rand()."-".rand());
$in = fopen("php://stdin", "r");
$out = fopen($tmpfile, "w");
while($buffer = fgets($in, 4096)) {
fputs($out, $buffer);
}
fclose($in);
fclose($out);
return $tmpfile;
}
/*
* function - Parse mail and fill all kinds of head and body info
*
* @param string tmp file name
* @return boolean true if success
*/
function parseMail($input_file) {
global $argv;
if (!$mp = new MailParser($input_file)) {
$this->setError('Error In MailParser');
return false;
} elseif ($mp->isError()) {
$this->setError('Error In MailParser '.$mp->getErrorMessage());
// even if it is an error, try to get the address of the sender so we
// can send him back the error
$this->FromEmail = $mp->getFromEmail();
return false;
}
$this->FromEmail = $mp->getFromEmail();
//
//subjects are in this required format: '[group - tracker_name][123456] My Subject'
//where 123456 is the artifact_id of the artifact message.
//we parse that ID to get the artifact that this should post to
//
$subj = $mp->getSubject();
if (ereg('(\[)([0-9]*)(\])',$subj,$arr)) {
$this->ArtifactId=$arr[2];
$artifactid_end=(strpos($subj,'['.$arr[2].']')) + strlen('['.$arr[2].']');
$this->Subject = addslashes(substr($subj,$artifactid_end));
} else {
$this->Subject = addslashes($subj);
$this->ArtifactId=0; // Not supported at the moment
$this->setError("ArtifactId needed at the moment. Artifact creation not supported");
return false;
}
$body = addslashes($mp->getBody());
// find first occurrence of the marker in the message
$begin = strpos($body, ARTIFACT_MAIL_MARKER);
if ($begin === false) {
$this->setError("Response message wasn't found in your mail. Please verify that ".
"you entered your message between the correct text markers.".
"\nYour message was:".
"\n".$mp->getBody());
return false;
}
// get the part of the message located after the marker
$body = substr($body, $begin+strlen(ARTIFACT_MAIL_MARKER));
// now look for the ending marker
$end = strpos($body, ARTIFACT_MAIL_MARKER);
if ($end === false) {
$this->setError("Response message wasn't found in your mail. Please verify that ".
"you entered your message between the correct text markers.".
"\nYour message was:".
"\n".$mp->getBody());
return false;
}
$message = substr($body, 0, $end);
$message = trim($message);
// maybe the last line was "> (ARTIFACT_MAIL_MARKER)". In that case, delete the last ">"
$message = preg_replace('/>$/', '', $message);
$this->Message = $message;
return true;
}
/**
* Insert data into the tracker db
*
* @return - true or false
*/
function addMessage() {
//
// get user_id
//
$user_id = $this->getUserId();
if ($user_id) {
//
// Set up this user's session before posting
//
session_set_new($user_id);
}
$Artifact =& $this->getArtifact();
if (!$Artifact || !is_object($Artifact)) {
$this->setError("Could Not Get Artifact");
return false;
}
if (!$user_id && !$Artifact->ArtifactType->allowsAnon()) {
$this->setError("Could Not Match Sender Email Address to User and Tracker Does Not Allow Anonymous Posts");
return false;
}
//
// Create artifact message
//
if ( !$Artifact->addMessage($this->Message,$this->FromName,true) )
{
$this->setError("ArtifactMessage Error:".$Artifact->getErrorMessage());
return false;
}
return true;
}
/*------------------------------------------------------------------------
* Utility functions
*-----------------------------------------------------------------------*/
/* Find user_id from email */
function getUserId() {
// Find User id using email
// If no user id, user id is 0;
$sql = "SELECT user_id FROM users
WHERE lower(email) ='".strtolower($this->FromEmail)."' AND status='A'";
$res = db_query($sql);
if (!$res || db_numrows($res) < 1) {
return false;
} else {
$user_id = db_result($res,0,'user_id');
}
db_free_result($res);
return $user_id;
}
function &getArtifact() {
global $argv;
// $Group not needed, but let the code here to support
// tracker additions in the Future
$Group =& group_get_object_by_name($argv[1]);
if (!$Group || !is_object($Group)) {
$this->setError('Could Not Get Group Object');
return false;
} elseif ($Group->isError()) {
$this->setError('Getting Group Object: '.$Group->getErrorMessage());
return false;
}
// DBG("Artifact_get_object(".$this->ArtifactId.");");
$this->Artifact =& artifact_get_object($this->ArtifactId);
return $this->Artifact;
}
}
/**
* Simple debugging printput
*
* Add this in /etc/syslog.conf and see /var/log/debug file:
* # Debug
* *.=debug /var/log/debug
*
*/
function DBG($str) {
global $debug;
if ($debug==1) {
system("echo \"artifact: ".$str."\n\" >> /tmp/tracker.log");
syslog(LOG_DEBUG, "artifact_gateway: ". $str);
} else if ($debug==2) {
echo $str."\n";
}
}
/* Main routine */
$debug = 0;
$myTrackerGateway = new TrackerGateway();
if ($myTrackerGateway->isError()) {
if ($myTrackerGateway->FromEmail) {
mail ($myTrackerGateway->FromEmail,'Tracker Post Rejected',$myTrackerGateway->getErrorMessage());
}
DBG('Final Message: '.$myTrackerGateway->getErrorMessage());
} else {
// DBG("Success!!");
}
?>
|