PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/public_html/lists/admin/readtestmail.php

https://github.com/samtuke/phplist
PHP | 339 lines | 256 code | 31 blank | 52 comment | 38 complexity | 2285cfae24cef9a7f57911b891a8d68b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. require_once dirname(__FILE__) . '/accesscheck.php';
  3. ########################################################################
  4. # Reads mail from a test account that will recieve all sent mail.
  5. # Use developer_email to send all mail to one account.
  6. # Use test_email settings to pop this box.
  7. # This file shows the links from the first email foun d for a selected user.
  8. # The links can be use to for instance confirm the user in an automated test.
  9. #
  10. # 2007 Bas Ovink - tincan ltd
  11. ########################################################################
  12. //CREATE TABLE `dev-phplist`.`phplist_testemail` (
  13. //`id` int( 11 ) NOT NULL AUTO_INCREMENT ,
  14. //`date` datetime default NULL ,
  15. //`header` text,
  16. //`data` blob,
  17. //`status` varchar( 255 ) default NULL ,
  18. //`comment` text,
  19. //PRIMARY KEY ( `id` ) ,
  20. //KEY `dateindex` ( `date` )
  21. //) ENGINE = MYISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT =34;
  22. if (!$GLOBALS["commandline"]) {
  23. ob_end_flush();
  24. if (!MANUALLY_PROCESS_testS) {
  25. print $GLOBALS['I18N']->get("This page can only be called from the commandline");
  26. return;
  27. }
  28. } else {
  29. ob_end_clean();
  30. print ClineSignature();
  31. ob_start();
  32. }
  33. function prepareOutput() {
  34. global $outputdone;
  35. if (!$outputdone) {
  36. $outputdone= 1;
  37. return formStart('name="outputform" class="readtestmailOutput" ') . '<textarea name="output" rows=10 cols=70></textarea></form>';
  38. }
  39. }
  40. $report= "";
  41. ## some general functions
  42. function finish($flag, $message) {
  43. if ($flag == "error") {
  44. $subject= $GLOBALS['I18N']->get("test processing error");
  45. }
  46. elseif ($flag == "info") {
  47. $subject= $GLOBALS['I18N']->get("test Processing info");
  48. }
  49. if (!TEST && $message)
  50. sendReport($subject, $message);
  51. # try..catch
  52. global $link;
  53. imap_close($link);
  54. }
  55. function ProcessError($message) {
  56. output("$message");
  57. finish('error', $message);
  58. exit;
  59. }
  60. function processTestEmails_shutdown() {
  61. global $report, $process_id;
  62. releaseLock($process_id);
  63. # $report .= "Connection status:".connection_status();
  64. finish('info', $report);
  65. if (!$GLOBALS["commandline"]) {
  66. include_once dirname(__FILE__) . '/footer.inc';
  67. }
  68. }
  69. function output($message, $reset= 0) {
  70. $infostring= "[" . date("D j M Y H:i", time()) . "] [" . getenv("REMOTE_HOST") . "] [" . getenv("REMOTE_ADDR") . "]";
  71. #print "$infostring $message<br/>\n";
  72. $message= preg_replace("/\n/", '', $message);
  73. ## contribution from http://forums.phplist.com/viewtopic.php?p=14648
  74. ## in languages with accented characters replace the HTML back
  75. //Replace the "&rsquo;" which is not replaced by html_decode
  76. $message= preg_replace("/&rsquo;/", "'", $message);
  77. //Decode HTML chars
  78. #$message = html_entity_decode($message,ENT_QUOTES,$_SESSION['adminlanguage']['charset']);
  79. $message= html_entity_decode($message, ENT_QUOTES, 'UTF-8');
  80. if ($GLOBALS["commandline"]) {
  81. ob_end_clean();
  82. print strip_tags($message) . "\n";
  83. ob_start();
  84. } else {
  85. if ($reset)
  86. print '<script language="Javascript" type="text/javascript">
  87. // if (document.forms[0].name == "outputform") {
  88. document.outputform.output.value = "";
  89. document.outputform.output.value += "\n";
  90. // }
  91. </script>' . "\n";
  92. print '<script language="Javascript" type="text/javascript">
  93. // if (document.forms[0].name == "outputform") {
  94. document.outputform.output.value += "' . $message . '";
  95. document.outputform.output.value += "\n";
  96. // } else
  97. // document.writeln("' . $message . '");
  98. </script>' . "\n";
  99. }
  100. flush();
  101. }
  102. //function processTestEmail($link, $mailIndex, $header) {
  103. // global $tables;
  104. // $headerinfo= imap_headerinfo($link, $mailIndex);
  105. // $header= imap_fetchheader($link, $mailIndex);
  106. //
  107. // $body= imap_body($link, $mailIndex);
  108. // $msgid= 0;
  109. // $user= 0;
  110. // preg_match("/X-MessageId: (.*)/i", $header, $match);
  111. // if (is_array($match) && isset ($match[1]))
  112. // $msgid= trim($match[1]);
  113. // if (!$msgid) {
  114. // # older versions use X-Message
  115. // preg_match("/X-Message: (.*)/i", $header, $match);
  116. // if (is_array($match) && isset ($match[1]))
  117. // $msgid= trim($match[1]);
  118. // }
  119. //
  120. // preg_match("/X-ListMember: (.*)/i", $header, $match);
  121. // if (is_array($match) && isset ($match[1]))
  122. // $user= trim($match[1]);
  123. // if (!$user) {
  124. // # older version use X-User
  125. // preg_match("/X-User: (.*)/i", $header, $match);
  126. // if (is_array($match) && isset ($match[1]))
  127. // $user= trim($match[1]);
  128. // }
  129. // # some versions used the email to identify the users, some the userid and others the uniqid
  130. // # use backward compatible way to find user
  131. // if (preg_match("/.*@.*/i", $user, $match)) {
  132. // $userid_req= Sql_Fetch_Row_Query("select id from {$tables["user"]} where email = \"$user\"");
  133. // if (VERBOSE)
  134. // output("UID" .
  135. // $userid_req[0] . " MSGID" . $msgid);
  136. // $userid= $userid_req[0];
  137. // }
  138. // elseif (preg_match("/^\d$/", $user)) {
  139. // $userid= $user;
  140. // if (VERBOSE)
  141. // output("UID" . $userid . " MSGID" . $msgid);
  142. // }
  143. // elseif ($user) {
  144. // $userid_req= Sql_Fetch_Row_Query("select id from {$tables["user"]} where uniqid = \"$user\"");
  145. // if (VERBOSE)
  146. // output("UID" . $userid_req[0] . " MSGID" . $msgid);
  147. // $userid= $userid_req[0];
  148. // } else {
  149. // $userid= '';
  150. // }
  151. // // Sql_Query(sprintf('insert into %s (date,header,data)
  152. // // values("%s","%s","%s")', 'phplist_testemail', date("Y-m-d H:i", @ strtotime($headerinfo->date)), addslashes($header), addslashes($body)));
  153. // // $testid= Sql_Insert_id();
  154. // // if ($userid) {
  155. // // Sql_Query(sprintf('update %s
  156. // // set status = "test system message",
  157. // // comment = "userid %s"
  158. // // where id = %d', 'phplist_testemail', $userid, $testid));
  159. // // } else {
  160. // // Sql_Query(sprintf('update %s
  161. // // set status = "unidentified test",
  162. // // comment = "not processed"
  163. // // where id = %d', 'phplist_testemail', $testid));
  164. // // return false;
  165. // // }
  166. // dbg($userid, '$userid');
  167. // return true;
  168. //}
  169. function openPop($server, $user, $password) {
  170. $port= $GLOBALS["test_mailbox_port"];
  171. if (!$port) {
  172. $port= '110/pop3/notls';
  173. }
  174. set_time_limit(6000);
  175. if (!TEST) {
  176. $link= imap_open("{" . $server . ":" . $port . "}INBOX", $user, $password, CL_EXPUNGE);
  177. } else {
  178. $link= imap_open("{" . $server . ":" . $port . "}INBOX", $user, $password);
  179. }
  180. if (!$link) {
  181. output($GLOBALS['I18N']->get("Cannot create POP3 connection to") . " $server: " . imap_last_error());
  182. return null;
  183. }
  184. return $link;
  185. }
  186. function openMbox($file) {
  187. set_time_limit(6000);
  188. if (!TEST) {
  189. $link= imap_open($file, "", "", CL_EXPUNGE);
  190. } else {
  191. $link= imap_open($file, "", "");
  192. }
  193. if (!$link) {
  194. output($GLOBALS['I18N']->get("Cannot open mailbox file") . " " . imap_last_error());
  195. return null;
  196. }
  197. return $link;
  198. }
  199. function findEmailForUser($link, $mailToFind, $max= 3000) {
  200. global $test_mailbox_purge_unprocessed, $test_mailbox_purge;
  201. output(sprintf('Looking for emails to %s', $mailToFind));
  202. $num= imap_num_msg($link);
  203. output($num . " " . $GLOBALS['I18N']->get("mails in mailbox") . "\n");
  204. output($GLOBALS['I18N']->get("Please do not interrupt this process") . "\n");
  205. if ($num > $max) {
  206. print $GLOBALS['I18N']->get("Processing first") . " $max " . $GLOBALS['I18N']->get("tests") . "<br/>";
  207. $num= $max;
  208. }
  209. $nberror= 0;
  210. $found= false;
  211. $mailIndex= 0;
  212. while (!$found && $mailIndex++ <= $num) { //for ($x= 1; $x <= $num; $x++)
  213. set_time_limit(60);
  214. $header= imap_fetchheader($link, $mailIndex);
  215. preg_match("/X-ListMember: (.*)/i", $header, $match);
  216. if (is_array($match) && isset ($match[1]) ) {
  217. $match[1] = trim($match[1]);
  218. $found= $mailToFind == $match[1];
  219. if (!$found) {
  220. printf('<a href="?page=readtestmail&amp;email=%s">Get (& delete) %s</a><br />', $match[1], $match[1]);
  221. }
  222. }
  223. if ($found) {
  224. output('Message found');
  225. if (!TEST && $test_mailbox_purge) {
  226. output($GLOBALS['I18N']->get("Deleting message") . " $mailIndex");
  227. imap_delete($link, $mailIndex);
  228. }
  229. } else {
  230. if (!TEST && $test_mailbox_purge_unprocessed) {
  231. output($GLOBALS['I18N']->get("Deleting message") . " $mailIndex");
  232. imap_delete($link, $mailIndex);
  233. }
  234. }
  235. flush();
  236. }
  237. flush();
  238. output($GLOBALS['I18N']->get("Closing mailbox, and purging messages"));
  239. if ($found) {
  240. return $num;
  241. } else {
  242. return null;
  243. }
  244. }
  245. #############################################
  246. # main
  247. function main() {
  248. };
  249. if (!function_exists('imap_open')) {
  250. Error($GLOBALS['I18N']->get('IMAP is not included in your PHP installation, cannot continue') .
  251. '<br/>' . $GLOBALS['I18N']->get('Check out') .
  252. ' <a href="http://www.php.net/manual/en/ref.imap.php">http://www.php.net/manual/en/ref.imap.php</a>');
  253. return;
  254. }
  255. //print '<script language="Javascript" src="js/progressbar.js" type="text/javascript"></script>';
  256. flush();
  257. $outputdone= 0;
  258. # lets not do this unless we do some locking first
  259. register_shutdown_function('processTestEmails_shutdown');
  260. $abort= ignore_user_abort(1);
  261. $process_id= getPageLock();
  262. if (!empty ($_REQUEST['email'])) {
  263. $mailToFind= $_REQUEST['email'];
  264. print prepareOutput();
  265. switch ($test_protocol) {
  266. case "pop" :
  267. $link= openPop($test_mailbox_host, $test_mailbox_user, $test_mailbox_password);
  268. break;
  269. case "mbox" :
  270. $link= openMbox($test_mailbox);
  271. break;
  272. default :
  273. Error($GLOBALS['I18N']->get("test_protocol not supported"));
  274. return;
  275. }
  276. if (isset ($link)) {
  277. $mailIndex= findEmailForUser($link, $mailToFind);
  278. if ( !is_null($mailIndex) ) {
  279. $body= imap_body($link, $mailIndex);
  280. $overview= imap_fetch_overview($link, $mailIndex);
  281. printf('Subject: %s<br />', $overview[0]->subject);
  282. preg_match_all("/<a(.*)href=[\"\'](.*)[\"\']([^>]*)>/Umis", $body, $links);
  283. foreach ($links[0] as $matchindex => $fullmatch) {
  284. preg_match('/p=(\w+)/', $fullmatch, $linkPages);
  285. printf('<a href="%s" id=%s>Link %s: %s</a><br />', $fullmatch, $matchindex++, $matchindex, $linkPages[1]);
  286. }
  287. preg_match_all('/http:\/\/\S+/', $body, $links);
  288. foreach ($links[0] as $matchindex => $fullmatch) {
  289. preg_match('/p=(\w+)/', $fullmatch, $linkPages);
  290. printf('<a href="%s" id=%s>Link %s: %s</a><br />', $fullmatch, $matchindex++, $matchindex, $linkPages[1]);
  291. }
  292. }
  293. }
  294. }
  295. {
  296. print ('<form method="get">');
  297. print (' <input name="page" value="readtestmail" type="hidden" />');
  298. print (' <input class="submit" type="submit" name="action" value="Get email for user: " />');
  299. printf(' <input type="text" name="email" value="%s" />', $_REQUEST['email']);
  300. print ('</form>');
  301. }
  302. if (!$GLOBALS["commandline"]) {
  303. print '<script language="Javascript" type="text/javascript"> finish(); </script>';
  304. }
  305. ?>