ImapLib.php 21 KB

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