PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Branch_4_0_2/gforge/cronjobs/tracker_gateway.php

https://gitlab.com/oslc-cm-server/olbergers-ff5-oslc
PHP | 257 lines | 132 code | 36 blank | 89 comment | 21 complexity | 4f35278253eaacb893ac7c6da87b7b60 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception
  1. #! /usr/bin/php4 -f
  2. <?php
  3. /**
  4. * This script will get mails and store it into artifact DB
  5. *
  6. * Copyright 2004 GForge, LLC
  7. *
  8. * @version $Id$
  9. * @author Tim Perdue tim@gforge.org
  10. * @author Sung Kim
  11. * @author Francisco Gimeno <kikov@kikov.org>
  12. *
  13. * This file is part of GForge.
  14. *
  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.
  19. *
  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.
  24. *
  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
  28. *
  29. * This file is based on forum_gateway.php
  30. */
  31. require_once ('squal_pre.php');
  32. require_once ('common/include/Group.class');
  33. require_once ('common/include/MailParser.class');
  34. require_once ('common/tracker/Artifact.class');
  35. require_once ('common/tracker/ArtifactFactory.class');
  36. class TrackerGateway extends Error {
  37. /*
  38. * variables
  39. */
  40. var $From = "";
  41. var $FromName = "";
  42. var $FromEmail = "";
  43. var $Subject = "";
  44. var $ListId = "";
  45. var $Reference = "";
  46. var $MsgId = "";
  47. var $Sender="";
  48. var $Body="";
  49. var $IsFollowUp=0;
  50. var $ArtifactId=-1;
  51. var $Artifact=null;
  52. function TrackerGateway() {
  53. $this->Error();
  54. /* Copy mail message to tmp file */
  55. $tmpfile = $this->copyMailTmp();
  56. //DBG("Tmpname: ". $tmpfile);
  57. /* parse email */
  58. $ret = $this->parseMail($tmpfile);
  59. /* Delete temp file */
  60. unlink($tmpfile);
  61. /* Check the return variable from parseMail */
  62. if (!$ret) {
  63. return false;
  64. }
  65. /* add the info to tracker */
  66. $ret = $this->addMessage();
  67. if (!$ret) {
  68. return false;
  69. }
  70. return true;
  71. }
  72. /**
  73. * function - Copy mail(from stdin to tmp and return the tmp file
  74. *
  75. * @return tmp file name
  76. */
  77. function copyMailTmp() {
  78. // Unfortunatly we need a temp file
  79. // mailparse needs to read content several times
  80. $tmpfile = tempnam ("/tmp", "artifact_gateway.".rand()."-".rand());
  81. $in = fopen("php://stdin", "r");
  82. $out = fopen($tmpfile, "w");
  83. while($buffer = fgets($in, 4096)) {
  84. fputs($out, $buffer);
  85. }
  86. fclose($in);
  87. fclose($out);
  88. return $tmpfile;
  89. }
  90. /*
  91. * function - Parse mail and fill all kinds of head and body info
  92. *
  93. * @param string tmp file name
  94. * @return boolean true if success
  95. */
  96. function parseMail($input_file) {
  97. global $argv;
  98. if (!$mp = new MailParser($input_file)) {
  99. $this->setError('Error In MailParser');
  100. return false;
  101. } elseif ($mp->isError()) {
  102. $this->setError('Error In MailParser '.$mp->getErrorMessage());
  103. return false;
  104. }
  105. $this->FromEmail = $mp->getFromEmail();
  106. //
  107. //subjects are in this required format: '[group - tracker_name][123456] My Subject'
  108. //where 123456 is the artifact_id of the artifact message.
  109. //we parse that ID to get the artifact that this should post to
  110. //
  111. $subj = $mp->getSubject();
  112. if (ereg('(\[)([0-9]*)(\])',$subj,$arr)) {
  113. $this->ArtifactId=$arr[2];
  114. $artifactid_end=(strpos($subj,'['.$arr[2].']')) + strlen('['.$arr[2].']');
  115. $this->Subject = addslashes(substr($subj,$artifactid_end));
  116. } else {
  117. $this->Subject = addslashes($subj);
  118. $this->ArtifactId=0; // Not supported at the moment
  119. $this->setError("ArtifactId needed at the moment. Artifact creation not supported");
  120. return false;
  121. }
  122. $this->Body =& addslashes($mp->getBody());
  123. return true;
  124. }
  125. /**
  126. * Insert data into the tracker db
  127. *
  128. * @return - true or false
  129. */
  130. function addMessage() {
  131. //
  132. // get user_id
  133. //
  134. $user_id = $this->getUserId();
  135. if ($user_id) {
  136. //
  137. // Set up this user's session before posting
  138. //
  139. session_set_new($user_id);
  140. }
  141. $Artifact =& $this->getArtifact();
  142. if (!$Artifact || !is_object($Artifact)) {
  143. $this->setError("Could Not Get Artifact");
  144. return false;
  145. }
  146. if (!$user_id && !$Artifact->ArtifactType->allowsAnon()) {
  147. $this->setError("Could Not Match Sender Email Address to User and Tracker Does Not Allow Anonymous Posts");
  148. return false;
  149. }
  150. //
  151. // Create artifact message
  152. //
  153. if ( !$Artifact->addMessage($this->Body,$this->FromName) )
  154. {
  155. $this->setError("ArtifactMessage Error:".$Artifact->getErrorMessage());
  156. return false;
  157. }
  158. return true;
  159. }
  160. /*------------------------------------------------------------------------
  161. * Utility functions
  162. *-----------------------------------------------------------------------*/
  163. /* Find user_id from email */
  164. function getUserId() {
  165. // Find User id using email
  166. // If no user id, user id is 0;
  167. $sql = "SELECT user_id FROM users
  168. WHERE lower(email) ='".strtolower($this->FromEmail)."' AND status='A'";
  169. $res = db_query($sql);
  170. if (!$res || db_numrows($res) < 1) {
  171. return false;
  172. } else {
  173. $user_id = db_result($res,0,'user_id');
  174. }
  175. db_free_result($res);
  176. return $user_id;
  177. }
  178. function &getArtifact() {
  179. global $argv;
  180. // $Group not needed, but let the code here to support
  181. // tracker additions in the Future
  182. $Group =& group_get_object_by_name($argv[1]);
  183. if (!$Group || !is_object($Group)) {
  184. $this->setError('Could Not Get Group Object');
  185. return false;
  186. } elseif ($Group->isError()) {
  187. $this->setError('Getting Group Object: '.$Group->getErrorMessage());
  188. return false;
  189. }
  190. // DBG("Artifact_get_object(".$this->ArtifactId.");");
  191. $this->Artifact =& artifact_get_object($this->ArtifactId);
  192. return $this->Artifact;
  193. }
  194. }
  195. /**
  196. * Simple debugging printput
  197. *
  198. * Add this in /etc/syslog.conf and see /var/log/debug file:
  199. * # Debug
  200. * *.=debug /var/log/debug
  201. *
  202. */
  203. function DBG($str) {
  204. global $debug;
  205. if ($debug==1) {
  206. system("echo \"artifact: ".$str."\n\" >> /tmp/tracker.log");
  207. syslog(LOG_DEBUG, "artifact_gateway: ". $str);
  208. } else if ($debug==2) {
  209. echo $str."\n";
  210. }
  211. }
  212. /* Main routine */
  213. $debug = 0;
  214. $myTrackerGateway = new TrackerGateway();
  215. if ($myTrackerGateway->isError()) {
  216. mail ($myTrackerGateway->FromEmail,'Forum Post Rejected',$myTrackerGateway->getErrorMessage());
  217. DBG('Final Message: '.$myTrackerGateway->getErrorMessage());
  218. } else {
  219. // DBG("Success!!");
  220. }
  221. ?>