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

/lib/horde/framework/Horde/Imap/Client/Url.php

https://bitbucket.org/moodle/moodle
PHP | 302 lines | 146 code | 41 blank | 115 comment | 36 complexity | f4cb62bd11e4ef6ef19ad8122147692d MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. /**
  3. * Copyright 2008-2017 Horde LLC (http://www.horde.org/)
  4. *
  5. * See the enclosed file LICENSE for license information (LGPL). If you
  6. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  7. *
  8. * @category Horde
  9. * @copyright 2008-2017 Horde LLC
  10. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11. * @package Imap_Client
  12. */
  13. /**
  14. * Object representation of a a POP3 (RFC 2384) or IMAP (RFC 5092/5593) URL.
  15. *
  16. * @author Michael Slusarz <slusarz@horde.org>
  17. * @category Horde
  18. * @copyright 2008-2016 Horde LLC
  19. * @deprecated Use Horde_Imap_Client_Url_Base instead
  20. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  21. * @package Imap_Client
  22. *
  23. * @property-read boolean $relative Is this a relative URL?
  24. */
  25. class Horde_Imap_Client_Url implements Serializable
  26. {
  27. /**
  28. * The authentication method to use.
  29. *
  30. * @var string
  31. */
  32. public $auth = null;
  33. /**
  34. * The remote server (not present for relative URLs).
  35. *
  36. * @var string
  37. */
  38. public $hostspec = null;
  39. /**
  40. * The IMAP mailbox.
  41. *
  42. * @todo Make this a Horde_Imap_Client_Mailbox object.
  43. *
  44. * @var string
  45. */
  46. public $mailbox = null;
  47. /**
  48. * A byte range for use with IMAP FETCH.
  49. *
  50. * @var string
  51. */
  52. public $partial = null;
  53. /**
  54. * The remote port (not present for relative URLs).
  55. *
  56. * @var integer
  57. */
  58. public $port = null;
  59. /**
  60. * The protocol type. Either 'imap' or 'pop' (not present for relative
  61. * URLs).
  62. *
  63. * @var string
  64. */
  65. public $protocol = null;
  66. /**
  67. * A search query to be run with IMAP SEARCH.
  68. *
  69. * @var string
  70. */
  71. public $search = null;
  72. /**
  73. * A MIME part ID.
  74. *
  75. * @var string
  76. */
  77. public $section = null;
  78. /**
  79. * The username to use on the remote server.
  80. *
  81. * @var string
  82. */
  83. public $username = null;
  84. /**
  85. * The IMAP UID.
  86. *
  87. * @var string
  88. */
  89. public $uid = null;
  90. /**
  91. * The IMAP UIDVALIDITY for the given mailbox.
  92. *
  93. * @var integer
  94. */
  95. public $uidvalidity = null;
  96. /**
  97. * URLAUTH info (not parsed).
  98. *
  99. * @var string
  100. */
  101. public $urlauth = null;
  102. /**
  103. * Constructor.
  104. *
  105. * Absolute IMAP URLs takes one of the following forms:
  106. * - imap://<iserver>[/]
  107. * - imap://<iserver>/<enc-mailbox>[<uidvalidity>][?<enc-search>]
  108. * - imap://<iserver>/<enc-mailbox>[<uidvalidity>]<iuid>[<isection>][<ipartial>][<iurlauth>]
  109. *
  110. * POP URLs take one of the following forms:
  111. * - pop://<user>;auth=<auth>@<host>:<port>
  112. *
  113. * @param string $url A URL string.
  114. */
  115. public function __construct($url = null)
  116. {
  117. if (!is_null($url)) {
  118. $this->_parse($url);
  119. }
  120. }
  121. /**
  122. * Create a POP3 (RFC 2384) or IMAP (RFC 5092/5593) URL.
  123. *
  124. * @return string A URL string.
  125. */
  126. public function __toString()
  127. {
  128. $url = '';
  129. if (!is_null($this->protocol)) {
  130. $url = $this->protocol . '://';
  131. if (!is_null($this->username)) {
  132. $url .= $this->username;
  133. if (!is_null($this->auth)) {
  134. $url .= ';AUTH=' . $this->auth;
  135. }
  136. $url .= '@';
  137. }
  138. $url .= $this->hostspec;
  139. if (!is_null($this->port)) {
  140. switch ($this->protocol) {
  141. case 'imap':
  142. if ($this->port != 143) {
  143. $url .= ':' . $this->port;
  144. }
  145. break;
  146. case 'pop':
  147. if ($this->port != 110) {
  148. $url .= ':' . $this->port;
  149. }
  150. break;
  151. }
  152. }
  153. }
  154. $url .= '/';
  155. if (is_null($this->protocol) || ($this->protocol == 'imap')) {
  156. $url .= rawurlencode($this->mailbox);
  157. if (!empty($this->uidvalidity)) {
  158. $url .= ';UIDVALIDITY=' . $this->uidvalidity;
  159. }
  160. if (!is_null($this->search)) {
  161. $url .= '?' . rawurlencode($this->search);
  162. } else {
  163. if (!is_null($this->uid)) {
  164. $url .= '/;UID=' . $this->uid;
  165. }
  166. if (!is_null($this->section)) {
  167. $url .= '/;SECTION=' . $this->section;
  168. }
  169. if (!is_null($this->partial)) {
  170. $url .= '/;PARTIAL=' . $this->partial;
  171. }
  172. if (!is_null($this->urlauth)) {
  173. $url .= '/;URLAUTH=' . $this->urlauth;
  174. }
  175. }
  176. }
  177. return $url;
  178. }
  179. /**
  180. */
  181. public function __get($name)
  182. {
  183. switch ($name) {
  184. case 'relative':
  185. return (is_null($this->hostspec) &&
  186. is_null($this->port) &&
  187. is_null($this->protocol));
  188. }
  189. }
  190. /**
  191. */
  192. protected function _parse($url)
  193. {
  194. $data = parse_url(trim($url));
  195. if (isset($data['scheme'])) {
  196. $protocol = Horde_String::lower($data['scheme']);
  197. if (!in_array($protocol, array('imap', 'pop'))) {
  198. return;
  199. }
  200. if (isset($data['host'])) {
  201. $this->hostspec = $data['host'];
  202. }
  203. $this->port = isset($data['port'])
  204. ? $data['port']
  205. : (($protocol === 'imap') ? 143 : 110);
  206. $this->protocol = $protocol;
  207. }
  208. /* Check for username/auth information. */
  209. if (isset($data['user'])) {
  210. if (($pos = stripos($data['user'], ';AUTH=')) !== false) {
  211. $auth = substr($data['user'], $pos + 6);
  212. if ($auth !== '*') {
  213. $this->auth = $auth;
  214. }
  215. $data['user'] = substr($data['user'], 0, $pos);
  216. }
  217. if (strlen($data['user'])) {
  218. $this->username = $data['user'];
  219. }
  220. }
  221. /* IMAP-only information. */
  222. if (is_null($this->protocol) || ($this->protocol == 'imap')) {
  223. if (isset($data['path'])) {
  224. $data['path'] = ltrim($data['path'], '/');
  225. $parts = explode('/;', $data['path']);
  226. $mbox = array_shift($parts);
  227. if (($pos = stripos($mbox, ';UIDVALIDITY=')) !== false) {
  228. $this->uidvalidity = intval(substr($mbox, $pos + 13));
  229. $mbox = substr($mbox, 0, $pos);
  230. }
  231. $this->mailbox = rawurldecode($mbox);
  232. if (isset($data['query'])) {
  233. $this->search = rawurldecode($data['query']);
  234. $parts = array();
  235. }
  236. } else {
  237. $parts = array();
  238. }
  239. if (count($parts)) {
  240. foreach ($parts as $val) {
  241. list($k, $v) = explode('=', $val);
  242. $property = Horde_String::lower($k);
  243. $this->$property = $v;
  244. }
  245. }
  246. }
  247. }
  248. /* Serializable methods. */
  249. /**
  250. */
  251. public function serialize()
  252. {
  253. return strval($this);
  254. }
  255. /**
  256. */
  257. public function unserialize($data)
  258. {
  259. $this->_parse($data);
  260. }
  261. }