ImapLib.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. <?php
  2. /**
  3. * LICENSE: The MIT License
  4. * Copyright (c) 2010 Chris Nizzardini (http://www.cnizz.com)
  5. */
  6. /**
  7. * Basic idea from
  8. * http://www.phpclasses.org/package/6256-PHP-Retrieve-messages-from-an-IMAP-server.html
  9. * added enhancements
  10. *
  11. * @modified 2011-11-13 Mark Scherer
  12. * @php 5
  13. *
  14. * ImapLib for accessing IMAP and POP email accounts
  15. */
  16. class ImapLib {
  17. const S_MAILBOX = 'mailbox';
  18. const S_SERVER = 'server';
  19. const S_PORT = 'port';
  20. const S_SERVICE = 'service';
  21. const S_USER = 'user';
  22. const S_PASSWORD = 'password';
  23. const S_AUTHUSER = 'authuser';
  24. const S_DEBUG = 'debug';
  25. const S_SECURE = 'secure';
  26. const S_NORSH = 'norsa';
  27. const S_SSL = 'ssl';
  28. const S_VALIDATECERT = 'validatecert';
  29. const S_TLS = 'tls';
  30. const S_NOTLS = 'notls';
  31. const S_READONLY = 'readonly';
  32. public $stream;
  33. public $settings = [
  34. self::S_MAILBOX => 'INBOX',
  35. self::S_SERVER => '',
  36. self::S_PORT => '',
  37. self::S_SERVICE => 'imap',
  38. self::S_USER => false,
  39. self::S_PASSWORD => '',
  40. self::S_AUTHUSER => false,
  41. self::S_DEBUG => false,
  42. self::S_SECURE => false,
  43. self::S_NORSH => false,
  44. self::S_SSL => false,
  45. self::S_VALIDATECERT => false,
  46. self::S_TLS => false,
  47. self::S_NOTLS => false,
  48. self::S_READONLY => false
  49. ];
  50. public $currentSettings = [];
  51. public $currentRef = '';
  52. /**
  53. * Check for basic dependencies in constructor.
  54. */
  55. public function __construct() {
  56. $this->dependenciesMatch();
  57. }
  58. /**
  59. * ImapLib::buildConnector()
  60. *
  61. * @param array $data
  62. * @return string
  63. */
  64. public function buildConnector($data = []) {
  65. $data = array_merge($this->settings, $data);
  66. $string = '{';
  67. $string .= $data[static::S_SERVER];
  68. if ($data[static::S_PORT]) {
  69. $string .= ':' . $data[static::S_PORT];
  70. }
  71. if ($data[static::S_SERVICE]) {
  72. $string .= '/service=' . $data[static::S_SERVICE];
  73. }
  74. if ($data[static::S_USER]) {
  75. $string .= '/user=' . $data[static::S_USER];
  76. } else {
  77. $string .= '/anonymous';
  78. }
  79. if ($data[static::S_AUTHUSER]) {
  80. $string .= '/authuser=' . $data[static::S_AUTHUSER];
  81. }
  82. if ($data[static::S_DEBUG]) {
  83. $string .= '/debug';
  84. }
  85. if ($data[static::S_SECURE]) {
  86. $string .= '/secure';
  87. }
  88. if ($data[static::S_NORSH]) {
  89. $string .= '/norsh';
  90. }
  91. if ($data[static::S_SSL]) {
  92. $string .= '/ssl';
  93. }
  94. if ($data[static::S_VALIDATECERT]) {
  95. $string .= '/validate-cert';
  96. } else {
  97. $string .= '/novalidate-cert';
  98. }
  99. if ($data[static::S_TLS]) {
  100. $string .= '/tls';
  101. }
  102. if ($data[static::S_NOTLS]) {
  103. $string .= '/notls';
  104. }
  105. if ($data[static::S_READONLY]) {
  106. $string .= '/readonly';
  107. }
  108. $string .= '}';
  109. $string .= $data[static::S_MAILBOX];
  110. $this->currentRef = $string;
  111. $this->currentSettings = $data;
  112. return $string;
  113. }
  114. /**
  115. * ImapLib::set()
  116. *
  117. * @param string $key
  118. * @param mixed $value
  119. * @return void
  120. */
  121. public function set($key, $value) {
  122. $this->settings[$key] = $value;
  123. }
  124. /**
  125. * ImapLib::lastError()
  126. *
  127. * @return string
  128. */
  129. public function lastError() {
  130. return imap_last_error();
  131. }
  132. /**
  133. * @return bool Success
  134. */
  135. public function connect($user, $pass, $server, $port = null) {
  136. $this->settings[static::S_SERVER] = $server;
  137. if ($port || !$port && $this->settings[static::S_SERVICE] === 'imap') {
  138. $this->settings[static::S_PORT] = $port;
  139. }
  140. $this->settings[static::S_USER] = $user;
  141. $this->settings[static::S_PASSWORD] = $pass;
  142. $connector = $this->buildConnector();
  143. //$options = OP_DEBUG;
  144. $this->stream = @imap_open($connector, $user, $pass, $options);
  145. if ($this->stream === false) {
  146. if ($error = $this->checkConnection()) {
  147. throw new ImapException($error);
  148. }
  149. return false;
  150. }
  151. return true;
  152. }
  153. /**
  154. * ImapLib::checkConnection()
  155. *
  156. * @return mixed
  157. */
  158. public function checkConnection() {
  159. if ($this->stream) {
  160. return $this->lastError();
  161. }
  162. return false;
  163. }
  164. /**
  165. * ImapLib::msgCount()
  166. *
  167. * @return int Count
  168. */
  169. public function msgCount() {
  170. return imap_num_msg($this->stream);
  171. }
  172. public function listMailboxes($current = true) {
  173. if (is_bool($current)) {
  174. if ($current) {
  175. $current = '%';
  176. } else {
  177. $current = '*';
  178. }
  179. }
  180. return imap_list($this->stream, $this->currentRef, $current);
  181. }
  182. public function getFolder() {
  183. return new ImapFolderLib($this);
  184. }
  185. public function expunge() {
  186. return imap_expunge($this->stream);
  187. }
  188. public function close($expunge = false) {
  189. if ($expunge) {
  190. return @imap_close($this->stream, CL_EXPUNGE);
  191. }
  192. return @imap_close($this->stream);
  193. }
  194. public function __destruct() {
  195. $this->close();
  196. }
  197. /**
  198. * Main listing of messages
  199. * - body, structure, attachments
  200. *
  201. * @return array
  202. */
  203. public function msgList($msgList = []) {
  204. $return = [];
  205. if (empty($msgList)) {
  206. $count = $this->msgCount();
  207. for ($i = 1; $i <= $count; $i++) {
  208. $header = imap_headerinfo($this->stream, $i);
  209. $msgNo = trim($header->Msgno);
  210. foreach ($header as $id => $value) {
  211. // Simple array
  212. if (!is_array($value)) {
  213. $return[$msgNo][$id] = imap_utf8($value);
  214. } else {
  215. foreach ($value as $newid => $arrayValue) {
  216. foreach ($arrayValue as $key => $aValue) {
  217. $return[$msgNo][$id][$key] = imap_utf8($aValue);
  218. }
  219. }
  220. }
  221. }
  222. //lets add attachments
  223. $return[$msgNo]['structure'] = (array)imap_fetchstructure($this->stream, $msgNo);
  224. $encodingValue = $return[$msgNo]['structure']['encoding'];
  225. if (!empty($return[$msgNo]['structure']['parts'])) {
  226. $part = $return[$msgNo]['structure']['parts'][0];
  227. $encodingValue = $part->encoding;
  228. }
  229. // Let's add the body
  230. $return[$msgNo]['body'] = $this->_getDecodedValue(imap_fetchbody($this->stream, $msgNo, 1), $encodingValue);
  231. //debug(imap_fetchstructure($this->stream, $header->Msgno, FT_UID));
  232. $return[$msgNo]['attachments'] = $this->attachments($header);
  233. }
  234. }
  235. // We want to search a specific array of messages
  236. else {
  237. foreach ($msgList as $i) {
  238. $header = imap_headerinfo($this->stream, $i);
  239. foreach ($header as $id => $value) {
  240. // Simple array
  241. if (!is_array($value)) {
  242. $return[$header->Msgno][$id] = $value;
  243. } else {
  244. foreach ($value as $newid => $arrayValue) {
  245. foreach ($value[0] as $key => $aValue) {
  246. $return[$header->Msgno][$id][$key] = quoted_printable_decode($aValue);
  247. }
  248. }
  249. }
  250. $return[$header->Msgno]['structure'] = imap_fetchstructure($this->stream, $header->Msgno);
  251. $encodingValue = $return[$header->Msgno]['structure']->encoding;
  252. if (!empty($return[$header->Msgno]['structure']->parts)) {
  253. $part = $return[$header->Msgno]['structure']->parts[0];
  254. $encodingValue = $part->encoding;
  255. }
  256. // Let's add the body too!
  257. $return[$header->Msgno]['body'] = $this->_getDecodedValue(imap_fetchbody($this->stream, $header->Msgno, 1), $encodingValue);
  258. $return[$header->Msgno]['attachments'] = $this->attachments($header);
  259. }
  260. }
  261. }
  262. return $return;
  263. }
  264. /**
  265. * @see http://www.nerdydork.com/download-pop3imap-email-attachments-with-php.html
  266. * @param object $header
  267. * @return array
  268. */
  269. public function attachments($header) {
  270. $structure = imap_fetchstructure($this->stream, $header->Msgno);
  271. if (!$structure || !isset($structure->parts)) {
  272. return [];
  273. }
  274. $parts = $structure->parts;
  275. $fpos = 2;
  276. $message = [];
  277. $message["attachment"]["type"][0] = 'text';
  278. $message["attachment"]["type"][1] = 'multipart';
  279. $message["attachment"]["type"][2] = 'message';
  280. $message["attachment"]["type"][3] = 'application';
  281. $message["attachment"]["type"][4] = 'audio';
  282. $message["attachment"]["type"][5] = 'image';
  283. $message["attachment"]["type"][6] = 'video';
  284. $message["attachment"]["type"][7] = 'other';
  285. $attachments = [];
  286. $count = count($parts);
  287. for ($i = 1; $i < $count; $i++) {
  288. $attachment = [];
  289. $part = $parts[$i];
  290. if (isset($part->disposition) && $part->disposition === 'ATTACHMENT') {
  291. $attachment["pid"] = $i;
  292. $attachment["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
  293. $attachment["subtype"][$i] = strtolower($part->subtype);
  294. $ext = $part->subtype;
  295. $params = $part->dparameters;
  296. $data = imap_fetchbody($this->stream, $header->Msgno, $fpos);
  297. $attachment['filename'] = $part->dparameters[0]->value;
  298. $attachment['data'] = $this->_getDecodedValue($data, $part->encoding);
  299. $attachment['filesize'] = strlen($attachment['data']);
  300. $fpos++;
  301. $attachments[] = $attachment;
  302. } elseif (isset($part->subtype) && $part->subtype === "OCTET-STREAM") {
  303. $attachment["pid"] = $i;
  304. $attachment["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
  305. $attachment["subtype"][$i] = strtolower($part->subtype);
  306. $ext = $part->subtype;
  307. $params = $part->parameters;
  308. $data = imap_fetchbody($this->stream, $header->Msgno, $fpos);
  309. $attachment['filename'] = $part->dparameters[0]->value;
  310. $attachment['data'] = $this->_getDecodedValue($data, $part->encoding);
  311. $attachment['filesize'] = strlen($attachment['data']);
  312. $fpos++;
  313. $attachments[] = $attachment;
  314. } else { // inline attachments etc
  315. $attachment["pid"] = $i;
  316. $type = '';
  317. if (!empty($message["attachment"]["type"][$part->type])) {
  318. $type = $message["attachment"]["type"][$part->type] . "/";
  319. }
  320. $attachment["type"][$i] = $type . strtolower($part->subtype);
  321. $attachment["subtype"][$i] = strtolower($part->subtype);
  322. $ext = $part->subtype;
  323. $params = $part->parameters;
  324. $data = imap_fetchbody($this->stream, $header->Msgno, $fpos);
  325. $attachment['filename'] = !is_object($part->parameters) ? $part->parameters[0]->value : '';
  326. $attachment['data'] = $this->_getDecodedValue($data, $part->encoding);
  327. $attachment['filesize'] = strlen($attachment['data']);
  328. $fpos++;
  329. $attachments[] = $attachment;
  330. }
  331. }
  332. return $attachments;
  333. }
  334. /**
  335. * Decode message text.
  336. *
  337. * @param string $message
  338. * @param int $encoding
  339. * @return string Message
  340. * @see http://www.nerdydork.com/download-pop3imap-email-attachments-with-php.html
  341. */
  342. protected function _getDecodedValue($message, $encoding) {
  343. if ($encoding == 0) {
  344. $message = imap_8bit($message);
  345. $message = $this->_decode7Bit($message);
  346. $message = imap_utf8($message);
  347. } elseif ($encoding == 1) {
  348. $message = imap_8bit($message);
  349. $message = quoted_printable_decode($message);
  350. $message = imap_utf8($message);
  351. } elseif ($encoding == 2) {
  352. $message = imap_binary($message);
  353. } elseif ($encoding == 3) {
  354. $message = imap_base64($message);
  355. } elseif ($encoding == 4) {
  356. $message = imap_qprint($message);
  357. $message = mb_convert_encoding($message, "UTF-8", "ISO-8859-1");
  358. } elseif ($encoding == 5) {
  359. // plain
  360. }
  361. return $message;
  362. }
  363. /**
  364. * Decodes 7-Bit text.
  365. *
  366. * @param string $text 7-Bit text to convert.
  367. * @return string Decoded text.
  368. */
  369. protected function _decode7Bit($text) {
  370. // Manually convert common encoded characters into their UTF-8 equivalents.
  371. $characters = [
  372. '=20' => ' ', // space.
  373. '=E2=80=99' => "'", // single quote.
  374. '=0A' => "\r\n", // line break.
  375. '=A0' => ' ', // non-breaking space.
  376. '=C2=A0' => ' ', // non-breaking space.
  377. "=\r\n" => '', // joined line.
  378. '=E2=80=A6' => '…', // ellipsis.
  379. '=E2=80=A2' => '•', // bullet.
  380. ];
  381. foreach ($characters as $key => $value) {
  382. $text = str_replace($key, $value, $text);
  383. }
  384. return $text;
  385. }
  386. /**
  387. * ImapLib::search()
  388. *
  389. * @param mixed $params
  390. * @return string
  391. */
  392. public function search($params) {
  393. if ($this->stream) {
  394. if (is_array($params)) {
  395. $searchString = '';
  396. foreach ($params as $field => $value) {
  397. if (is_numeric($field)) {
  398. // Make sure the value is uppercase
  399. $searchString .= strtoupper($value) . ' ';
  400. } else {
  401. $searchString .= strtoupper($field) . ' "' . $value . '" ';
  402. }
  403. }
  404. // Perform the search
  405. #echo "'$searchString'";
  406. return imap_search($this->stream, $searchString);
  407. }
  408. return imap_last_error();
  409. }
  410. return imap_last_error();
  411. }
  412. /**
  413. * ImapLib::flag()
  414. *
  415. * @param mixed $flag
  416. * @return bool
  417. */
  418. public function flag($flag) {
  419. return imap_setflag_full($this->ImapFolder->Imap->stream, $this->uid, $flag, ST_UID);
  420. }
  421. /**
  422. * ImapLib::delete()
  423. *
  424. * @param string|array $emails
  425. * @param bool $delete
  426. * @return bool Success
  427. */
  428. public function delete($emails, $delete = false) {
  429. $emails = (array)$emails;
  430. foreach ($emails as $email) {
  431. if ($delete) {
  432. imap_delete($this->stream, (int)$email);
  433. } else {
  434. imap_mail_move($this->stream, (int)$email, "Inbox/Trash");
  435. }
  436. }
  437. return imap_expunge($this->stream);
  438. }
  439. /**
  440. * @deprecated
  441. */
  442. public function delx($emails, $delete = false) {
  443. if (!$this->stream) {
  444. return false;
  445. }
  446. $emails = (array)$emails;
  447. foreach ($emails as $key => $val) {
  448. $emails[$key] = (int)$val;
  449. }
  450. // Let's delete multiple emails
  451. if (count($emails) > 0) {
  452. $deleteString = '';
  453. $emailError = [];
  454. foreach ($emails as $email) {
  455. if ($delete) {
  456. if (!imap_delete($this->stream, $email)) {
  457. $emailError[] = $email;
  458. }
  459. }
  460. }
  461. if (!$delete) {
  462. // Need to take the last comma out!
  463. $deleteString = implode(',', $emails);
  464. echo $deleteString;
  465. imap_mail_move($this->stream, $deleteString, "Inbox/Trash");
  466. //imap_expunge($this->stream);
  467. } else {
  468. // NONE of the emails were deleted
  469. //imap_expunge($this->stream);
  470. if (count($emailError) === count($emails)) {
  471. return imap_last_error();
  472. }
  473. $return['status'] = false;
  474. $return['not_deleted'] = $emailError;
  475. return $return;
  476. }
  477. }
  478. // Not connected
  479. return imap_last_error();
  480. }
  481. /**
  482. * @deprecated
  483. */
  484. public function switch_mailbox($mailbox = '') {
  485. if ($this->stream) {
  486. $this->mbox = '{' . $this->server;
  487. if ($this->port) {
  488. $this->mbox .= ':' . $this->port;
  489. }
  490. if ($this->flags) {
  491. $this->mbox .= $this->flags;
  492. }
  493. $this->mbox .= '/user="' . $this->user . '"';
  494. $this->mbox .= '}';
  495. $this->mbox .= $this->defaultMailbox;
  496. if ($mailbox) {
  497. $this->mbox .= '.' . $mailbox;
  498. }
  499. return @imap_reopen($this->stream, $this->mbox);
  500. }
  501. // Not connected
  502. return imap_last_error();
  503. }
  504. /**
  505. * @deprecated
  506. */
  507. public function current_mailbox() {
  508. if ($this->stream) {
  509. $info = imap_mailboxmsginfo($this->stream);
  510. if ($info) {
  511. return $info->Mailbox;
  512. }
  513. // There was an error
  514. return imap_last_error();
  515. }
  516. // Not connected
  517. return imap_last_error();
  518. }
  519. /**
  520. * @deprecated
  521. */
  522. public function mailbox_info($type = 'obj') {
  523. if ($this->stream) {
  524. $info = imap_mailboxmsginfo($this->stream);
  525. if ($info) {
  526. if ($type === 'array') {
  527. $infoArray = get_object_vars($info);
  528. return $infoArray;
  529. }
  530. return $info;
  531. }
  532. // There was an error
  533. return imap_last_error();
  534. }
  535. // Not connected
  536. return imap_last_error();
  537. }
  538. /**
  539. * Makes sure imap_open is available etc
  540. *
  541. * @throws InternalErrorException
  542. * @return bool Success
  543. */
  544. public function dependenciesMatch() {
  545. if (!function_exists('imap_open')) {
  546. throw new InternalErrorException('imap_open not available. Please install extension/module.');
  547. }
  548. return true;
  549. }
  550. }
  551. // Currently NOT IN USE: //
  552. /**
  553. * IMAP Postf�cher mit CakePHP abfragen
  554. * @see http://www.interaktionsdesigner.de/2009/05/11/imap-postfacher-mit-cakephp-abfragen/
  555. *
  556. * $this->Imap->connect();
  557. * Der R�ckgabewert dieser Funktion ist negativ wenn es nicht funktioniert hat.
  558. *
  559. * Eine gute Hilfe gegen verr�ckte Sonderzeichen und Kodierungen ist die Kombination von utf8_encode und quoted_printable_decode. Damit werden die meisten Umlaute richtig dargestellt.
  560. * F�r den Text der Mail w�re das dann innerhalb der foreach-Schleife:
  561. * debug(utf8_encode(quoted_printable_decode($message['body'])));
  562. *
  563. * fixes: pop3 connect etc
  564. */
  565. class ImapMessageInfoLib {
  566. const BS = "\\";
  567. public $ImapFolder;
  568. public $ImapMessage;
  569. public $subject;
  570. public $from;
  571. public $to;
  572. public $date;
  573. public $messageId;
  574. public $references;
  575. public $inReplyTo;
  576. public $size;
  577. public $uid;
  578. public $msgno;
  579. public $recent;
  580. public $flagged;
  581. public $answered;
  582. public $deleted;
  583. public $seen;
  584. public $draft;
  585. public function __construct($ImapFolder, $data) {
  586. if (!is_object($data)) {
  587. $list = new ImapMessagesListLib($ImapFolder, [$data]);
  588. $list = $list->overview(false);
  589. $data = $list[0];
  590. }
  591. foreach ($data as $key => $value) {
  592. $this->{$key} = $value;
  593. }
  594. $this->ImapFolder = $ImapFolder;
  595. }
  596. public function messageObject() {
  597. if (!isset($this->ImapMessage)) {
  598. return $this->ImapMessage = new ImapMessageLib($this->ImapFolder, $this->uid, $this);
  599. }
  600. return $this->ImapMessage;
  601. }
  602. public function flag($flag) {
  603. return imap_setflag_full($this->ImapFolder->Imap->stream, $this->uid, $flag, ST_UID);
  604. }
  605. public function unFlag($flag) {
  606. return imap_clearflag_full($this->ImapFolder->Imap->stream, $this->uid, $flag, ST_UID);
  607. }
  608. public function seen($set = null) {
  609. if ($set === null) {
  610. return $this->seen;
  611. }
  612. if ($set) {
  613. return $this->flag(static::BS . 'Seen');
  614. }
  615. return $this->unFlag(static::BS . 'Seen');
  616. }
  617. public function answered($set = null) {
  618. if ($set === null) {
  619. return $this->answered;
  620. }
  621. if ($set) {
  622. return $this->flag(static::BS . 'Answered');
  623. }
  624. return $this->unFlag(static::BS . 'Answered');
  625. }
  626. public function flagged($set = null) {
  627. if ($set === null) {
  628. return $this->flagged;
  629. }
  630. if ($set) {
  631. return $this->flag(static::BS . 'Flagged');
  632. }
  633. return $this->unFlag(static::BS . 'Flagged');
  634. }
  635. public function deleted($set = null) {
  636. if ($set === null) {
  637. return $this->deleted;
  638. }
  639. if ($set) {
  640. return $this->flag(static::BS . 'Deleted');
  641. }
  642. return $this->unFlag(static::BS . 'Deleted');
  643. }
  644. public function draft($set = null) {
  645. if ($set === null) {
  646. return $this->draft;
  647. }
  648. if ($set) {
  649. return $this->flag(static::BS . 'Draft');
  650. }
  651. return $this->unFlag(static::BS . 'Draft');
  652. }
  653. }
  654. class ImapMessageLib {
  655. public $ImapFolder;
  656. public $MessageInfo;
  657. public $uid;
  658. public function __construct($ImapFolder, $uid, $ImapMessageInfo = null) {
  659. $this->ImapFolder = $ImapFolder;
  660. if ($ImapMessageInfo === null) {
  661. $this->MessageInfo = new ImapMessageInfoLib($this->ImapFolder, $uid);
  662. } else {
  663. $this->MessageInfo = $ImapMessageInfo;
  664. }
  665. $this->MessageInfo->ImapMessage = $this;
  666. $this->uid = $uid;
  667. }
  668. public function move($folder) {
  669. }
  670. public function id() {
  671. //CHANGE DIR TO CURRENT
  672. return imap_msgno($this->ImapFolder->Imap->stream, $this->uid);
  673. }
  674. public function uid($ID) {
  675. return $this->uid;
  676. }
  677. public function fetchstructure() {
  678. return imap_fetchstructure($this->ImapFolder->Imap->stream, $this->uid, FT_UID);
  679. }
  680. public function fetchbody($section = 0) {
  681. return imap_fetchbody($this->ImapFolder->Imap->stream, $this->uid, $section, (FT_UID + FT_PEEK));
  682. }
  683. }
  684. class ImapMessagesListLib {
  685. public $ImapFolder;
  686. public $messageUIDs = [];
  687. public function __construct($ImapFolder, $messageUIDs) {
  688. $this->ImapFolder = $ImapFolder;
  689. $this->messageUIDs = $messageUIDs;
  690. }
  691. public function messgageObject($id) {
  692. if (isset($this->messageUIDs[$id])) {
  693. if (is_object($this->messageUIDs[$id])) {
  694. return $this->messageUIDs[$id];
  695. }
  696. return $this->messageUIDs[$id] = new ImapMessageLib($this->ImapFolder, $this->messageUIDs[$id]);
  697. }
  698. return false;
  699. }
  700. public function count() {
  701. return count($this->messageUIDs);
  702. }
  703. public function overview($returnInfo = true) {
  704. //CHANGE DIR TO CURRENT
  705. $overview = imap_fetch_overview($this->ImapFolder->Imap->stream, implode(',', $this->messageUIDs), FT_UID);
  706. if ($returnInfo) {
  707. $msgObjs = [];
  708. foreach ($overview as $info) {
  709. $msgObjs[] = new ImapMessageInfoLib($this->ImapFolder, $info);
  710. }
  711. return $msgObjs;
  712. }
  713. return $overview;
  714. }
  715. }
  716. class ImapFolderLib {
  717. const S_ALL = 'ALL';
  718. const S_ANSWERED = 'ANSWERED';
  719. const S_BCC = 'BCC';
  720. const S_BEFORE = 'BEFORE';
  721. const S_BODY = 'BODY';
  722. const S_CC = 'CC';
  723. const S_DELETED = 'DELETED';
  724. const S_FLAGGED = 'FLAGGED';
  725. const S_FROM = 'FROM';
  726. const S_KEYWORD = 'KEYWORD';
  727. const S_NEW = 'NEW';
  728. const S_OLD = 'OLD';
  729. const S_ON = 'ON';
  730. const S_RECENT = 'RECENT';
  731. const S_SEEN = 'SEEN';
  732. const S_SINCE = 'SINCE';
  733. const S_SUBJECT = 'SUBJECT';
  734. const S_TEXT = 'TEXT';
  735. const S_TO = 'TO';
  736. const S_UNANSWERED = 'UNANSWERED';
  737. const S_UNDELETED = 'UNDELETED';
  738. const S_UNFLAGGED = 'UNFLAGGED';
  739. const S_UNKEYWORD = 'UNKEYWORD';
  740. const S_UNSEEN = 'UNSEEN';
  741. public $Imap;
  742. public $currentRef = '';
  743. public function __construct($Imap) {
  744. $this->Imap = $Imap;
  745. $this->currentRef = $this->Imap->currentRef;
  746. }
  747. public function listFolder() {
  748. }
  749. public function searchMessages($options = [self::ALL]) {
  750. $optionstring = '';
  751. foreach ($options as $key => $value) {
  752. if (is_int($key)) {
  753. $key = $value;
  754. $value = null;
  755. }
  756. switch ($key) {
  757. case static::S_FROM:
  758. $param = '"' . $value . '" ';
  759. break;
  760. default:
  761. $param = '';
  762. break;
  763. }
  764. $optionstring .= $key . ' ' . $param;
  765. }
  766. //CHANGE DIR TO CURRENT
  767. $msg = imap_search($this->Imap->stream, $optionstring, SE_UID);
  768. if ($msg !== false) {
  769. return new ImapMessagesListLib($this, $msg);
  770. }
  771. return false;
  772. }
  773. public function allMessages() {
  774. return $this->searchMessages();
  775. }
  776. }
  777. class ImapException extends CakeException {
  778. }