PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/app/scheduler/tasks/mailcheck/index.php

https://github.com/lux/sitellite
PHP | 336 lines | 225 code | 40 blank | 71 comment | 64 complexity | 9198f4166b91d1308d3e1211b6827a11 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?php
  2. // BEGIN CLI KEEPOUT CHECKING
  3. if (php_sapi_name () !== 'cli') {
  4. // Add these lines to the very top of any file you don't want people to
  5. // be able to access directly.
  6. header ('HTTP/1.1 404 Not Found');
  7. echo "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
  8. . "<html><head>\n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\n"
  9. . "The requested URL " . $PHP_SELF . " was not found on this server.<p>\n<hr>\n"
  10. . $_SERVER['SERVER_SIGNATURE'] . "</body></html>";
  11. exit;
  12. }
  13. // END CLI KEEPOUT CHECKING
  14. // mailcheck scheduler block
  15. // Checks for new incoming mail.
  16. global $conf;
  17. if (empty ($conf['Messaging']['email_server'])) {
  18. return;
  19. }
  20. loader_import ('saf.Mail.Pop3');
  21. loader_import ('pear.Mail.mimeDecode');
  22. loader_import ('cms.Workspace.Message');
  23. //loader_import ('cms.Workspace.Task');
  24. $wmsg = new WorkspaceMessage ();
  25. //$wtsk = new WorkspaceTask ();
  26. $pop3 = new Pop3 ($conf['Messaging']['email_server'], $conf['Messaging']['email_port']);
  27. if (! $pop3->connect ()) {
  28. echo $pop3->error . "\n";
  29. return;
  30. }
  31. if (! $pop3->authenticate ($conf['Messaging']['email_username'], $conf['Messaging']['email_password'])) {
  32. echo $pop3->error . "\n";
  33. return;
  34. }
  35. $messages = $pop3->listMessages ();
  36. foreach ($messages as $number => $message) {
  37. set_time_limit (30);
  38. $messages[$number]['message'] = $pop3->getMessage ($number);
  39. if ($messages[$number]['message'] === false) {
  40. echo $pop3->error . "\n";
  41. }
  42. // parse message and send to system
  43. $md = new Mail_mimeDecode ($messages[$number]['message']);
  44. $decoded = $md->decode (array (
  45. 'include_bodies' => true,
  46. 'decode_bodies' => true,
  47. 'decode_headers' => true,
  48. ));
  49. /* mapping:
  50. *
  51. * if it's a comment, task, or message => from subject \[(C|T|M)([0-9]+)\]
  52. *
  53. */
  54. if (preg_match ('/\[(C|T|M)([0-9]+)\] ?/', $decoded->headers['subject'], $regs)) {
  55. $message_type = $regs[1];
  56. $message_id = $regs[2];
  57. $subject = str_replace ($regs[0], '', $decoded->headers['subject']);
  58. } else {
  59. // invalid message
  60. echo 'Invalid subject header: ' . $decoded->headers['subject'] . "\n";
  61. //return;
  62. }
  63. switch ($message_type) {
  64. /* message
  65. *
  66. * send(subject,body,recipients,attachments,response_id,priority,from)
  67. * subject => headers/subject minus []
  68. * body => body or parts w/ disposition != attachment
  69. * recipients => empty array
  70. * attachments => parts w/ disposition == attachment
  71. * responding_to => from subject
  72. * priority => headers/x-priority (1 => high)
  73. * from => headers/from <([^>]+)> use $1
  74. *
  75. */
  76. case 'M':
  77. // put together pieces
  78. // $subject done
  79. // $responding_to done ($message_id)
  80. // $recipients is empty array()
  81. // priority
  82. if ($decoded->headers['x-priority'] == 1) {
  83. $priority = 'high';
  84. } else {
  85. $priority = 'normal';
  86. }
  87. if (! empty ($decoded->body)) {
  88. if ($decoded->ctype_secondary == 'plain') {
  89. $body = $wmsg->formatBody ($decoded->body);
  90. } else {
  91. $body = $decoded->body;
  92. }
  93. $attachments = array ();
  94. } else {
  95. $body = '';
  96. $attachments = array ();
  97. foreach ($decoded->parts as $part) {
  98. if ($part->disposition == 'attachment' || $part->ctype_primary != 'text') {
  99. $a = array (
  100. 'type' => 'document',
  101. );
  102. $a['name'] = $part->d_parameters['filename'];
  103. $a['body'] = $part->body;
  104. $a['mime'] = $part->ctype_primary . '/' . $part->ctype_secondary;
  105. $a['summary'] = '';
  106. $attachments[] = $a;
  107. } else {
  108. if ($part->ctype_secondary == 'plain') {
  109. $body .= $wmsg->formatBody ($part->body);
  110. } else {
  111. $body .= $part->body;
  112. }
  113. }
  114. }
  115. }
  116. // - if M{id} is valid
  117. $res = $wmsg->get ($message_id, false);
  118. if (! $res) {
  119. echo 'no such message (' . $message_id . ')' . NEWLINE;
  120. info ($res, true);
  121. continue;
  122. }
  123. if (preg_match ('/<([^>]+)>/', $decoded->headers['from'], $regs)) {
  124. $from = $regs[1];
  125. } else {
  126. $from = $decoded->headers['from'];
  127. }
  128. // - recipient list based on original message
  129. $list = $wmsg->getRecipients ($message_id);
  130. if (! is_array ($list)) {
  131. $list = array ();
  132. }
  133. $_list = array ($res->from_user);
  134. foreach ($list as $obj) {
  135. if (! $obj->user) {
  136. $_list[] = $obj->email;
  137. } else {
  138. $_list[] = $obj->user;
  139. }
  140. }
  141. $list = $_list;
  142. $from_user = $wmsg->getUserFromForward ('email', $from);
  143. if (! $from_user) {
  144. continue;
  145. } elseif (is_object ($from_user)) {
  146. $from_user = $from_user->user;
  147. if (! in_array ($from_user, $list)) {
  148. continue; // can't send if you didn't receive
  149. }
  150. } else {
  151. foreach ($from_user as $u) {
  152. if (in_array ($u->user, $list)) {
  153. $from_user = $u->user;
  154. break;
  155. }
  156. }
  157. }
  158. // remove self, add $res->from_user
  159. foreach ($list as $k => $v) {
  160. if ($v == $from_user) {
  161. unset ($list[$k]);
  162. }
  163. }
  164. $list = array_unique ($list);
  165. $id = $wmsg->send ($subject, $body, $list, $attachments, $message_id, $priority, $from_user);
  166. if (! $id) {
  167. echo $wmsg->error . "\n";
  168. } else {
  169. $pop3->removeMessage ($number);
  170. }
  171. break;
  172. /* task
  173. *
  174. * comment(taskid,comment,user)
  175. * taskid => from subject
  176. * comment => body or parts w/ disposition != attachment
  177. *
  178. * attach(taskid,type,name,summary,body,mime,user)
  179. * taskid => from subject
  180. * type => always 'document'
  181. * name => from parts/#/ctype_parameters/name
  182. * summary => from subject
  183. * body => from parts/#/body
  184. * mime => from parts/#/ctype_primary . '/' . parts/#/ctype_secondary
  185. *
  186. *
  187. */
  188. case 'T':
  189. // put together pieces
  190. // $taskid done ($message_id)
  191. if (! empty ($decoded->body)) {
  192. $body = $decoded->body;
  193. $attachments = array ();
  194. } else {
  195. $body = '';
  196. $attachments = array ();
  197. foreach ($decoded->parts as $part) {
  198. if ($part->disposition == 'attachment' || $part->ctype_primary != 'text') {
  199. $a = array (
  200. 'type' => 'document',
  201. );
  202. $a['name'] = $part->d_parameters['filename'];
  203. $a['body'] = $part->body;
  204. $a['mime'] = $part->ctype_primary . '/' . $part->ctype_secondary;
  205. $attachments[] = $a;
  206. } else {
  207. $body .= $part->body;
  208. }
  209. }
  210. }
  211. if (preg_match ('/<([^>]+)>/', $decoded->headers['from'], $regs)) {
  212. $from = $regs[1];
  213. } else {
  214. $from = $decoded->headers['from'];
  215. }
  216. $id = $wtsk->comment ($message_id, $body, $from);
  217. if (! $id) {
  218. echo $wtsk->error . "\n";
  219. } else {
  220. $erase = true;
  221. foreach ($attachments as $attachment) {
  222. $res = $wtsk->attach ($message_id, $attachment['type'], $attachment['name'], '', $attachment['body'], $attachment['mime']);
  223. if (! $res) {
  224. echo $wtsk->error . "\n";
  225. $erase = false;
  226. }
  227. }
  228. if ($erase) {
  229. $pop3->removeMessage ($number);
  230. }
  231. }
  232. break;
  233. /* comment
  234. *
  235. * comment(taskid,comment,user)
  236. * taskid => from subject
  237. * comment => body or parts w/ disposition != attachment
  238. *
  239. * attach(taskid,type,name,summary,body,mime,user)
  240. * taskid => from subject
  241. * type => always 'document'
  242. * name => from parts/#/ctype_parameters/name
  243. * summary => from subject
  244. * body => from parts/#/body
  245. * mime => from parts/#/ctype_primary . '/' . parts/#/ctype_secondary
  246. *
  247. */
  248. case 'C':
  249. // put together pieces
  250. // $taskid done ($message_id)
  251. if (! empty ($decoded->body)) {
  252. $body = $decoded->body;
  253. $attachments = array ();
  254. } else {
  255. $body = '';
  256. $attachments = array ();
  257. foreach ($decoded->parts as $part) {
  258. if ($part->disposition == 'attachment' || $part->ctype_primary != 'text') {
  259. $a = array (
  260. 'type' => 'document',
  261. );
  262. $a['name'] = $part->d_parameters['filename'];
  263. $a['body'] = $part->body;
  264. $a['mime'] = $part->ctype_primary . '/' . $part->ctype_secondary;
  265. $attachments[] = $a;
  266. } else {
  267. $body .= $part->body;
  268. }
  269. }
  270. }
  271. if (preg_match ('/<([^>]+)>/', $decoded->headers['from'], $regs)) {
  272. $from = $regs[1];
  273. } else {
  274. $from = $decoded->headers['from'];
  275. }
  276. $id = $wtsk->comment ($message_id, $body, $from);
  277. if (! $id) {
  278. echo $wtsk->error . "\n";
  279. } else {
  280. $erase = true;
  281. foreach ($attachments as $attachment) {
  282. $res = $wtsk->attach ($message_id, $attachment['type'], $attachment['name'], '', $attachment['body'], $attachment['mime']);
  283. if (! $res) {
  284. echo $wtsk->error . "\n";
  285. $erase = false;
  286. }
  287. }
  288. if ($erase) {
  289. $pop3->removeMessage ($number);
  290. }
  291. }
  292. break;
  293. } // end switch ($message_type)
  294. }
  295. $pop3->disconnect ();
  296. ?>