Protocol.php 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. <?php
  2. // +-----------------------------------------------------------------------+
  3. // | |
  4. // | Copyright © 2003 Heino H. Gehlsen. All Rights Reserved. |
  5. // | http://www.heino.gehlsen.dk/software/license |
  6. // | |
  7. // +-----------------------------------------------------------------------+
  8. // | |
  9. // | This work (including software, documents, or other related items) is |
  10. // | being provided by the copyright holders under the following license. |
  11. // | By obtaining, using and/or copying this work, you (the licensee) |
  12. // | agree that you have read, understood, and will comply with the |
  13. // | following terms and conditions: |
  14. // | |
  15. // | Permission to use, copy, modify, and distribute this software and |
  16. // | its documentation, with or without modification, for any purpose and |
  17. // | without fee or royalty is hereby granted, provided that you include |
  18. // | the following on ALL copies of the software and documentation or |
  19. // | portions thereof, including modifications, that you make: |
  20. // | |
  21. // | 1. The full text of this NOTICE in a location viewable to users of |
  22. // | the redistributed or derivative work. |
  23. // | |
  24. // | 2. Any pre-existing intellectual property disclaimers, notices, or |
  25. // | terms and conditions. If none exist, a short notice of the |
  26. // | following form (hypertext is preferred, text is permitted) should |
  27. // | be used within the body of any redistributed or derivative code: |
  28. // | "Copyright © 2003 Heino H. Gehlsen. All Rights Reserved. |
  29. // | http://www.heino.gehlsen.dk/software/license" |
  30. // | |
  31. // | 3. Notice of any changes or modifications to the files, including |
  32. // | the date changes were made. (We recommend you provide URIs to |
  33. // | the location from which the code is derived.) |
  34. // | |
  35. // | THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT |
  36. // | HOLDERS MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, |
  37. // | INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR |
  38. // | FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE |
  39. // | OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, |
  40. // | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. |
  41. // | |
  42. // | COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, |
  43. // | SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE |
  44. // | SOFTWARE OR DOCUMENTATION. |
  45. // | |
  46. // | The name and trademarks of copyright holders may NOT be used in |
  47. // | advertising or publicity pertaining to the software without specific, |
  48. // | written prior permission. Title to copyright in this software and any |
  49. // | associated documentation will at all times remain with copyright |
  50. // | holders. |
  51. // | |
  52. // +-----------------------------------------------------------------------+
  53. // | |
  54. // | This license is based on the "W3C® SOFTWARE NOTICE AND LICENSE". |
  55. // | No changes have been made to the "W3C® SOFTWARE NOTICE AND LICENSE", |
  56. // | except for the references to the copyright holder, which has either |
  57. // | been changes or removed. |
  58. // | |
  59. // +-----------------------------------------------------------------------+
  60. // $Id: Protocol.php,v 1.6.2.1 2003/11/14 08:44:19 heino Exp $
  61. require_once 'PEAR.php';
  62. require_once 'Net/Socket.php';
  63. define('NET_NNTP_PROTOCOL_DEFAULT_HOST', 'localhost');
  64. define('NET_NNTP_PROTOCOL_DEFAULT_PORT', '119');
  65. /**
  66. * The Net_NNTP_Protocol class implements the NNTP standard acording to
  67. * RFX 977, RFC 2980, RFC 850/1036, and RFC 822/2822
  68. *
  69. * @version 0.0.1
  70. * @author Heino H. Gehlsen <heino@gehlsen.dk>
  71. */
  72. class Net_NNTP_Protocol extends PEAR
  73. {
  74. // {{{ properties
  75. /**
  76. * The socket resource being used to connect to the IMAP server.
  77. *
  78. * @var resource
  79. * @access private
  80. */
  81. var $_socket = null;
  82. /**
  83. *
  84. *
  85. * @var resource
  86. * @access private
  87. */
  88. var $_currentStatusResponse = null;
  89. // }}}
  90. // {{{ constructor
  91. /**
  92. *
  93. */
  94. function Net_NNTP_Protocol() {
  95. parent::PEAR();
  96. $this->_socket = new Net_Socket();
  97. }
  98. // }}}
  99. // {{{ Connect()
  100. /**
  101. * Connect to the server
  102. *
  103. * @param optional string $host The adress of the NNTP-server to connect to, defaults to 'localhost'.
  104. * @param optional int $port The port number to connect to, defaults to 119.
  105. *
  106. * @return mixed (bool) true on success or (object) pear_error on failure
  107. * @access public
  108. */
  109. function connect($host = NET_NNTP_PROTOCOL_DEFAULT_HOST, $port = NET_NNTP_PROTOCOL_DEFAULT_PORT)
  110. {
  111. if ($this->isConnected() ) {
  112. return PEAR::throwError('Already connected, disconnect first!', null);
  113. }
  114. // Open Connection
  115. $R = @$this->_socket->connect($host, $port, false, 15);
  116. if ($this->isError($R)) {
  117. return PEAR::throwError('Could not connect to the server', null, $R->getMessage());
  118. }
  119. // Retrive the server's initial response.
  120. $response = $this->_getStatusResponse();
  121. if (PEAR::isError($response)) {
  122. return $response;
  123. }
  124. switch ($response) {
  125. case 200: // Posting allowed
  126. // TODO: Set some variable
  127. return true;
  128. break;
  129. case 201: // Posting NOT allowed
  130. // TODO: Set some variable
  131. return true;
  132. break;
  133. case 502: // 'access restriction or permission denied'
  134. return PEAR::throwError('Server refused connection', $response, $this->currentStatusResponse());
  135. break;
  136. default:
  137. return PEAR::throwError('Unidentified response code', $response, $this->currentStatusResponse());
  138. }
  139. }
  140. // }}}
  141. // {{{ disconnect()
  142. /**
  143. * alias for cmdQuit()
  144. *
  145. * @access public
  146. */
  147. function disconnect()
  148. {
  149. return $this->cmdQuit();
  150. }
  151. // }}}
  152. // {{{ cmdQuit()
  153. /**
  154. * Close connection to the server
  155. *
  156. * @access public
  157. */
  158. function cmdQuit()
  159. {
  160. // Tell the server to close the connection
  161. $response = $this->_sendCommand('QUIT');
  162. if (PEAR::isError($response)) {
  163. return $response;
  164. }
  165. switch ($response) {
  166. case 205: // RFC977: 'closing connection - goodbye!'
  167. // If socket is still open, close it.
  168. if ($this->isConnected()) {
  169. $this->_socket->disconnect();
  170. }
  171. return true;
  172. break;
  173. default:
  174. return PEAR::throwError('Unidentified response code', $response, $this->currentStatusResponse());
  175. }
  176. }
  177. // }}}
  178. /**
  179. * The authentication process i not yet standarized but used any way
  180. * (http://www.mibsoftware.com/userkt/nntpext/index.html).
  181. */
  182. // {{{ cmdAuthinfo()
  183. /**
  184. * Authenticates the user using the original method
  185. *
  186. * @param string $user The username to authenticate as.
  187. * @param string $pass The password to authenticate with.
  188. *
  189. * @return mixed (bool) true on success or (object) pear_error on failure
  190. * @access private
  191. */
  192. function cmdAuthinfo($user, $pass)
  193. {
  194. // Send the username
  195. $response = $this->_sendCommand('AUTHINFO user '.$user);
  196. if (PEAR::isError($response)) {
  197. return $response;
  198. }
  199. // Send the password, if the server asks
  200. if (($response == 381) && ($pass !== null)) {
  201. // Send the password
  202. $response = $this->_sendCommand('AUTHINFO pass '.$pass);
  203. if (PEAR::isError($response)) {
  204. return $response;
  205. }
  206. }
  207. switch ($response) {
  208. case 281: // RFC2980: 'Authentication accepted'
  209. return true;
  210. break;
  211. case 381: // RFC2980: 'More authentication information required'
  212. return PEAR::throwError('Authentication uncompleted', $response, $this->currentStatusResponse());
  213. break;
  214. case 482: // RFC2980: 'Authentication rejected'
  215. return PEAR::throwError('Authentication rejected', $response, $this->currentStatusResponse());
  216. break;
  217. case 502: // RFC2980: 'No permission'
  218. return PEAR::throwError('Authentication rejected', $response, $this->currentStatusResponse());
  219. break;
  220. // case 500:
  221. // case 501:
  222. // return PEAR::throwError('Authentication failed', $response, $this->currentStatusResponse());
  223. // break;
  224. default:
  225. return PEAR::throwError('Unexpected authentication error!', $response, $this->currentStatusResponse());
  226. }
  227. }
  228. // }}}
  229. // {{{ cmdAuthinfoSimple()
  230. /**
  231. * Authenticates the user using the simple method
  232. *
  233. * @param string $user The username to authenticate as.
  234. * @param string $pass The password to authenticate with.
  235. *
  236. * @return mixed (bool) true on success or (object) pear_error on failure
  237. * @access private
  238. */
  239. function cmdAuthinfoSimple($user, $pass)
  240. {
  241. return PEAR::throwError("The auth mode: 'simple' is has not been implemented yet", null);
  242. }
  243. // }}}
  244. // {{{ cmdAuthinfoGeneric()
  245. /**
  246. * Authenticates the user using the simple method
  247. *
  248. * @param string $user The username to authenticate as.
  249. * @param string $pass The password to authenticate with.
  250. *
  251. * @return mixed (bool) true on success or (object) pear_error on failure
  252. * @access private
  253. */
  254. function cmdAuthinfoGeneric($user, $pass)
  255. {
  256. return PEAR::throwError("The auth mode: 'generic' is has not been implemented yet", null);
  257. }
  258. // }}}
  259. // {{{ cmdModeReader()
  260. /**
  261. *
  262. * @return mixed (bool) true when one can post on success or (object) pear_error on failure
  263. * @access public
  264. */
  265. function cmdModeReader()
  266. {
  267. // tell the newsserver we want an article
  268. $response = $this->_sendCommand('MODE READER');
  269. if (PEAR::isError($response)) {
  270. return $response;
  271. }
  272. switch ($response) {
  273. case 200: // RFC2980: 'Hello, you can post'
  274. break;
  275. case 201: // RFC2980: 'Hello, you can't post'
  276. break;
  277. default:
  278. return PEAR::throwError('Unidentified response code', $response, $this->currentStatusResponse());
  279. }
  280. }
  281. // }}}
  282. // {{{ cmdArticle()
  283. /**
  284. * Get an article from the currently open connection.
  285. *
  286. * @param mixed $article Either a message-id or a message-number of the article to fetch. If null or '', then use current article.
  287. *
  288. * @return mixed (array) article on success or (object) pear_error on failure
  289. * @access public
  290. */
  291. function cmdArticle($article)
  292. {
  293. // tell the newsserver we want an article
  294. $response = $this->_sendCommand('ARTICLE '.$article);
  295. if (PEAR::isError($response)) {
  296. return $response;
  297. }
  298. switch ($response) {
  299. case 220: // RFC977: 'n <a> article retrieved - head and body follow (n = article number, <a> = message-id)'
  300. case 221: // RFC977: 'n <a> article retrieved - head follows'
  301. case 222: // RFC977: 'n <a> article retrieved - body follows'
  302. case 223: // RFC977: 'n <a> article retrieved - request text separately'
  303. $data = $this->_getTextResponse();
  304. if (PEAR::isError($data)) {
  305. return $data;
  306. }
  307. return $data;
  308. break;
  309. case 412: // RFC977: 'no newsgroup has been selected'
  310. return PEAR::throwError('No newsgroup has been selected', $response, $this->currentStatusResponse());
  311. break;
  312. case 420: // RFC977: 'no current article has been selected'
  313. return PEAR::throwError('No current article has been selected', $response, $this->currentStatusResponse());
  314. break;
  315. case 423: // RFC977: 'no such article number in this group'
  316. return PEAR::throwError('No such article number in this group', $response, $this->currentStatusResponse());
  317. break;
  318. case 430: // RFC977: 'no such article found'
  319. return PEAR::throwError('No such article found', $response, $this->currentStatusResponse());
  320. break;
  321. default:
  322. return PEAR::throwError('Unidentified response code', $response, $this->currentStatusResponse());
  323. }
  324. }
  325. // }}}
  326. // {{{ cmdHead()
  327. /**
  328. * Get the headers of an article from the currently open connection.
  329. *
  330. * @param mixed $article Either a message-id or a message-number of the article to fetch the headers from. If null or '', then use current article.
  331. *
  332. * @return mixed (array) headers on success or (object) pear_error on failure
  333. * @access public
  334. */
  335. function cmdHead($article)
  336. {
  337. // tell the newsserver we want the header of an article
  338. $response = $this->_sendCommand('HEAD '.$article);
  339. if (PEAR::isError($response)) {
  340. return $response;
  341. }
  342. switch ($response) {
  343. case 220: // RFC977: 'n <a> article retrieved - head and body follow (n = article number, <a> = message-id)'
  344. case 221: // RFC977: 'n <a> article retrieved - head follows'
  345. case 222: // RFC977: 'n <a> article retrieved - body follows'
  346. case 223: // RFC977: 'n <a> article retrieved - request text separately'
  347. $data = $this->_getTextResponse();
  348. if (PEAR::isError($data)) {
  349. return $data;
  350. }
  351. return $data;
  352. break;
  353. case 412: // RFC977: 'no newsgroup has been selected'
  354. return PEAR::throwError('No newsgroup has been selected', $response, $this->currentStatusResponse());
  355. break;
  356. case 420: // RFC977: 'no current article has been selected'
  357. return PEAR::throwError('No current article has been selected', $response, $this->currentStatusResponse());
  358. break;
  359. case 423: // RFC977: 'no such article number in this group'
  360. return PEAR::throwError('No such article number in this group', $response, $this->currentStatusResponse());
  361. break;
  362. case 430: // RFC977: 'no such article found'
  363. return PEAR::throwError('No such article found', $response, $this->currentStatusResponse());
  364. break;
  365. default:
  366. return PEAR::throwError('Unidentified response code', $response, $this->currentStatusResponse());
  367. }
  368. }
  369. // }}}
  370. // {{{ cmdBody()
  371. /**
  372. * Get the body of an article from the currently open connection.
  373. *
  374. * @param mixed $article Either a message-id or a message-number of the article to fetch the body from. If null or '', then use current article.
  375. *
  376. * @return mixed (array) body on success or (object) pear_error on failure
  377. * @access public
  378. */
  379. function cmdBody($article)
  380. {
  381. // tell the newsserver we want the body of an article
  382. $response = $this->_sendCommand('BODY '.$article);
  383. if (PEAR::isError($response)) {
  384. return $response;
  385. }
  386. switch ($response) {
  387. case 220: // RFC977: 'n <a> article retrieved - head and body follow (n = article number, <a> = message-id)'
  388. case 221: // RFC977: 'n <a> article retrieved - head follows'
  389. case 222: // RFC977: 'n <a> article retrieved - body follows'
  390. case 223: // RFC977: 'n <a> article retrieved - request text separately'
  391. $data = $this->_getTextResponse();
  392. if (PEAR::isError($data)) {
  393. return $data;
  394. }
  395. return $data;
  396. break;
  397. case 412: // RFC977: 'no newsgroup has been selected'
  398. return PEAR::throwError('No newsgroup has been selected', $response, $this->currentStatusResponse());
  399. break;
  400. case 420: // RFC977: 'no current article has been selected'
  401. return PEAR::throwError('No current article has been selected', $response, $this->currentStatusResponse());
  402. break;
  403. case 423: // RFC977: 'no such article number in this group'
  404. return PEAR::throwError('No such article number in this group', $response, $this->currentStatusResponse());
  405. break;
  406. case 430: // RFC977: 'no such article found'
  407. return PEAR::throwError('No such article found', $response, $this->currentStatusResponse());
  408. break;
  409. default:
  410. return PEAR::throwError('Unidentified response code', $response, $this->currentStatusResponse());
  411. }
  412. }
  413. // }}}
  414. // {{{ cmdPost()
  415. /**
  416. * Post an article to a newsgroup.
  417. *
  418. * Among the aditional headers you might think of adding could be:
  419. * "NNTP-Posting-Host: <ip-of-author>", which should contain the IP-adress
  420. * of the author of the post, so the message can be traced back to him.
  421. * "Organization: <org>" which contain the name of the organization
  422. * the post originates from.
  423. *
  424. * @param string $newsgroup The newsgroup to post to.
  425. * @param string $subject The subject of the post.
  426. * @param string $body The body of the post itself.
  427. * @param string $from Name + email-adress of sender.
  428. * @param optional string $aditional Aditional headers to send.
  429. *
  430. * @return mixed (bool) true on success or (object) pear_error on failure
  431. * @access public
  432. */
  433. function cmdPost($newsgroup, $subject, $body, $from, $aditional = '')
  434. {
  435. // tell the newsserver we want to post an article
  436. $response = $this->_sendCommand('POST');
  437. if (PEAR::isError($response)) {
  438. return $response;
  439. }
  440. if ($response == 340) { // RFC977: 'send article to be posted. End with <CR-LF>.<CR-LF>'
  441. // should be presented in the format specified by RFC850
  442. $this->_socket->write("Newsgroups: $newsgroup\r\n");
  443. $this->_socket->write("Subject: $subject\r\n");
  444. $this->_socket->write("From: $from\r\n");
  445. $this->_socket->write("X-poster: PEAR::Net_NNTP\r\n");
  446. $this->_socket->write("$aditional\r\n");
  447. $this->_socket->write("\r\n");
  448. $this->_socket->write("$body\r\n");
  449. $this->_socket->write(".\r\n");
  450. // Retrive server's response.
  451. $response = $this->_getStatusResponse();
  452. if (PEAR::isError($response)) {
  453. return $response;
  454. }
  455. }
  456. switch ($response) {
  457. case 240: // RFC977: 'article posted ok'
  458. return true;
  459. break;
  460. case 340: // RFC977: 'send article to be posted. End with <CR-LF>.<CR-LF>'
  461. // This should not happen here!
  462. return PEAR::throwError('Unknown error during post', $response, $this->currentStatusResponse());
  463. break;
  464. case 440: // RFC977: 'posting not allowed'
  465. return PEAR::throwError('Posting not allowed', $response, $this->currentStatusResponse());
  466. break;
  467. case 441: // RFC977: 'posting failed'
  468. return PEAR::throwError('Posting failed', $response, $this->currentStatusResponse());
  469. break;
  470. default:
  471. return PEAR::throwError('Unidentified response code', $response, $this->currentStatusResponse());
  472. }
  473. }
  474. // }}}
  475. // {{{ cmdGroup()
  476. /**
  477. * Selects a news group (issue a GROUP command to the server)
  478. *
  479. * @param string $newsgroup The newsgroup name
  480. *
  481. * @return mixed (array) groupinfo on success or (object) pear_error on failure
  482. * @access public
  483. */
  484. function cmdGroup($newsgroup)
  485. {
  486. $response = $this->_sendCommand('GROUP '.$newsgroup);
  487. if (PEAR::isError($response)) {
  488. return $response;
  489. }
  490. switch ($response) {
  491. case 211: // RFC977: 'n f l s group selected'
  492. $response_arr = split(' ', trim($this->currentStatusResponse()));
  493. $response_arr['count'] =& $response_arr[0];
  494. $response_arr['first'] =& $response_arr[1];
  495. $response_arr['last'] =& $response_arr[2];
  496. $response_arr['group'] =& $response_arr[3];
  497. return $response_arr;
  498. break;
  499. case 411: // RFC977: 'no such news group'
  500. return PEAR::throwError('No such news group', $response, $this->currentStatusResponse());
  501. break;
  502. default:
  503. return PEAR::throwError('Unidentified response code', $response, $this->currentStatusResponse());
  504. }
  505. }
  506. // }}}
  507. // {{{ cmdList()
  508. /**
  509. * Fetches a list of all avaible newsgroups
  510. *
  511. * @return mixed (array) nested array with informations about existing newsgroups on success or (object) pear_error on failure
  512. * @access public
  513. */
  514. function cmdList()
  515. {
  516. $response = $this->_sendCommand('LIST');
  517. if (PEAR::isError($response)){
  518. return $response;
  519. }
  520. switch ($response) {
  521. case 215: // RFC977: 'list of newsgroups follows'
  522. $data = $this->_getTextResponse();
  523. if (PEAR::isError($data)) {
  524. return $data;
  525. }
  526. foreach($data as $line) {
  527. $arr = explode(' ', trim($line));
  528. $arr['group'] =& $arr[0];
  529. $arr['last'] =& $arr[1];
  530. $arr['first'] =& $arr[2];
  531. $arr['posting' ] = (bool) ($arr[3] == 'y');
  532. $groups[$arr[0]] = $arr;
  533. }
  534. return $groups;
  535. break;
  536. default:
  537. return PEAR::throwError('Unidentified response code', $response, $this->currentStatusResponse());
  538. }
  539. }
  540. // }}}
  541. // {{{ cmdListNewsgroups()
  542. /**
  543. * Fetches a list of (all) avaible newsgroup descriptions.
  544. *
  545. * @param string $wildmat Wildmat of the groups, that is to be listed, defaults to '';
  546. *
  547. * @return mixed (array) nested array with description of existing newsgroups on success or (object) pear_error on failure
  548. * @access public
  549. */
  550. function cmdListNewsgroups($wildmat = '')
  551. {
  552. $response = $this->_sendCommand('LIST NEWSGROUPS '.$wildmat);
  553. if (PEAR::isError($response)){
  554. return $response;
  555. }
  556. switch ($response) {
  557. case 215: // RFC2980: 'information follows'
  558. $data = $this->_getTextResponse();
  559. if (PEAR::isError($data)) {
  560. return $data;
  561. }
  562. foreach($data as $line) {
  563. preg_match("/^(.*?)\s(.*?$)/", trim($line), $matches);
  564. $groups[$matches[1]] = (string) $matches[2];
  565. }
  566. return $groups;
  567. break;
  568. case 503: // RFC2980: 'program error, function not performed'
  569. return PEAR::throwError('Internal server error, function not performed', $response, $this->currentStatusResponse());
  570. break;
  571. default:
  572. return PEAR::throwError('Unidentified response code', $response, $this->currentStatusResponse());
  573. }
  574. }
  575. // }}}
  576. /**
  577. * Fetches a list of (all) avaible newsgroup descriptions.
  578. * Depresated as of RFC2980.
  579. *
  580. * @param string $wildmat Wildmat of the groups, that is to be listed, defaults to '*';
  581. *
  582. * @return mixed (array) nested array with description of existing newsgroups on success or (object) pear_error on failure
  583. * @access public
  584. */
  585. function cmdXGTitle($wildmat = '*')
  586. {
  587. $response = $this->_sendCommand('XGTITLE '.$wildmat);
  588. if (PEAR::isError($response)){
  589. return $response;
  590. }
  591. switch ($response) {
  592. case 282: // RFC2980: 'list of groups and descriptions follows'
  593. $data = $this->_getTextResponse();
  594. if (PEAR::isError($data)) {
  595. return $data;
  596. }
  597. foreach($data as $line) {
  598. preg_match("/^(.*?)\s(.*?$)/", trim($line), $matches);
  599. $groups[$matches[1]] = (string) $matches[2];
  600. }
  601. return $groups;
  602. break;
  603. case 481: // RFC2980: 'Groups and descriptions unavailable'
  604. return PEAR::throwError('Groups and descriptions unavailable', $response, $this->currentStatusResponse());
  605. break;
  606. default:
  607. return PEAR::throwError('Unidentified response code', $response, $this->currentStatusResponse());
  608. }
  609. }
  610. // }}}
  611. // {{{ cmdNewgroups()
  612. /**
  613. * Fetches a list of all newsgroups created since a specified date.
  614. *
  615. * @param int $time Last time you checked for groups (timestamp).
  616. *
  617. * @return mixed (array) nested array with informations about existing newsgroups on success or (object) pear_error on failure
  618. * @access public
  619. */
  620. function cmdNewgroups($time)
  621. {
  622. $response = $this->_sendCommand('NEWGROUPS '.date('ymd His', $time));
  623. if (PEAR::isError($response)){
  624. return $response;
  625. }
  626. switch ($response) {
  627. case 231: // REF977: 'list of new newsgroups follows'
  628. $groups = array();
  629. foreach($this->_getTextResponse() as $line) {
  630. $arr = explode(' ', $line);
  631. $groups[$arr[0]]['group'] = $arr[0];
  632. $groups[$arr[0]]['last'] = $arr[1];
  633. $groups[$arr[0]]['first'] = $arr[2];
  634. $groups[$arr[0]]['posting'] = $arr[3];
  635. }
  636. return $groups;
  637. break;
  638. default:
  639. return PEAR::throwError('Unidentified response code', $response, $this->currentStatusResponse());
  640. }
  641. }
  642. // }}}
  643. // {{{ cmdListOverviewFmt()
  644. /**
  645. * Returns a list of avaible headers which are send from newsserver to client for every news message
  646. *
  647. * @return mixed (array) of header names on success or (object) pear_error on failure
  648. * @access public
  649. */
  650. function cmdListOverviewFmt()
  651. {
  652. $response = $this->_sendCommand('LIST OVERVIEW.FMT');
  653. if (PEAR::isError($response)){
  654. return $response;
  655. }
  656. switch ($response) {
  657. case 215: // RFC2980: 'information follows'
  658. $data = $this->_getTextResponse();
  659. if (PEAR::isError($data)) {
  660. return $data;
  661. }
  662. $format = array('number');
  663. // XXX Use the splitHeaders() algorithm for supporting
  664. // multiline headers?
  665. foreach ($data as $line) {
  666. $line = current(explode(':', trim($line)));
  667. $format[] = $line;
  668. }
  669. return $format;
  670. break;
  671. case 503: // RFC2980: 'program error, function not performed'
  672. return PEAR::throwError('Internal server error, function not performed', $response, $this->currentStatusResponse());
  673. break;
  674. default:
  675. return PEAR::throwError('Unidentified response code', $response, $this->currentStatusResponse());
  676. }
  677. }
  678. // }}}
  679. // {{{ cmdXOver()
  680. /**
  681. * Fetch message header from message number $first until $last
  682. *
  683. * The format of the returned array is:
  684. * $messages[message_id][header_name]
  685. *
  686. * @param integer $first first article to fetch
  687. * @param integer $last last article to fetch
  688. *
  689. * @return mixed (array) nested array of message and there headers on success or (object) pear_error on failure
  690. * @access public
  691. */
  692. function cmdXOver($first, $last)
  693. {
  694. $format = $this->cmdListOverviewFmt();
  695. if (PEAR::isError($format)){
  696. return $formt;
  697. }
  698. $response = $this->_sendCommand('XOVER '.$first.'-'.$last);
  699. if (PEAR::isError($response)){
  700. return $response;
  701. }
  702. switch ($response) {
  703. case 224: // RFC2980: 'Overview information follows'
  704. $data = $this->_getTextResponse();
  705. if (PEAR::isError($data)) {
  706. return $data;
  707. }
  708. $messages = array();
  709. foreach($data as $line) {
  710. $i=0;
  711. foreach(explode("\t", trim($line)) as $line) {
  712. $message[$format[$i++]] = $line;
  713. }
  714. $messages[$message['Message-ID']] = $message;
  715. }
  716. return $messages;
  717. break;
  718. case 412: // RFC2980: 'No news group current selected'
  719. return PEAR::throwError('No news group current selected', $response, $this->currentStatusResponse());
  720. break;
  721. case 420: // RFC2980: 'No article(s) selected'
  722. return PEAR::throwError('No article(s) selected', $response, $this->currentStatusResponse());
  723. break;
  724. case 502: // RFC2980: 'no permission'
  725. return PEAR::throwError('No permission', $response, $this->currentStatusResponse());
  726. break;
  727. default:
  728. return PEAR::throwError('Unidentified response code', $response, $this->currentStatusResponse());
  729. }
  730. }
  731. // }}}
  732. // {{{ cmdXROver()
  733. /**
  734. * Fetch message references from message number $first to $last
  735. *
  736. * @param integer $first first article to fetch
  737. * @param integer $last last article to fetch
  738. *
  739. * @return mixed (array) assoc. array of message references on success or (object) pear_error on failure
  740. * @access public
  741. */
  742. function cmdXROver($first, $last)
  743. {
  744. $response = $this->_sendCommand('XROVER '.$first.'-'.$last);
  745. if (PEAR::isError($response)){
  746. return $response;
  747. }
  748. switch ($response) {
  749. case 224: // RFC2980: 'Overview information follows'
  750. $data = $this->_getTextResponse();
  751. if (PEAR::isError($data)) {
  752. return $data;
  753. }
  754. foreach($data as $line) {
  755. $references = preg_split("/ +/", trim($line), -1, PREG_SPLIT_NO_EMPTY);
  756. $id = array_shift($references);
  757. $messages[$id] = $references;
  758. }
  759. return $messages;
  760. break;
  761. case 412: // RFC2980: 'No news group current selected'
  762. return PEAR::throwError('No news group current selected', $response, $this->currentStatusResponse());
  763. break;
  764. case 420: // RFC2980: 'No article(s) selected'
  765. return PEAR::throwError('No article(s) selected', $response, $this->currentStatusResponse());
  766. break;
  767. case 502: // RFC2980: 'no permission'
  768. return PEAR::throwError('No permission', $response, $this->currentStatusResponse());
  769. break;
  770. default:
  771. return PEAR::throwError('Unidentified response code', $response, $this->currentStatusResponse());
  772. }
  773. }
  774. // }}}
  775. // {{{ cmdListgroup()
  776. /**
  777. *
  778. * @param string $newsgroup
  779. *
  780. * @return mixed (array) on success or (object) pear_error on failure
  781. */
  782. function cmdListgroup($newsgroup)
  783. {
  784. $response = $this->_sendCommand('LISTGROUP '.$newsgroup);
  785. if (PEAR::isError($response)){
  786. return $response;
  787. }
  788. switch ($response) {
  789. case 211: // RFC2980: 'list of article numbers follow'
  790. $data = $this->_getTextResponse();
  791. if (PEAR::isError($data)) {
  792. return $data;
  793. }
  794. return $data;
  795. break;
  796. case 412: // RFC2980: 'Not currently in newsgroup'
  797. return PEAR::throwError('Not currently in newsgroup', $response, $this->currentStatusResponse());
  798. break;
  799. case 502: // RFC2980: 'no permission'
  800. return PEAR::throwError('No permission', $response, $this->currentStatusResponse());
  801. break;
  802. default:
  803. return PEAR::throwError('Unidentified response code', $response, $this->currentStatusResponse());
  804. }
  805. }
  806. // }}}
  807. // {{{ cmdNewnews()
  808. /**
  809. *
  810. */
  811. function cmdNewnews($time, $newsgroups = '*')
  812. {
  813. // TODO: the lenght of the request string may not exceed 510 chars
  814. $response = $this->_sendCommand('NEWNEWS '.$newsgroups.' '.date('ymd His', $time));
  815. if (PEAR::isError($response)){
  816. return $response;
  817. }
  818. switch ($response) {
  819. case 230: // RFC977: 'list of new articles by message-id follows'
  820. $messages = array();
  821. foreach($this->_getTextResponse() as $line) {
  822. $messages[] = $line;
  823. }
  824. return $messages;
  825. break;
  826. default:
  827. return PEAR::throwError('Unidentified response code', $response, $this->currentStatusResponse());
  828. }
  829. }
  830. // }}}
  831. // {{{ cmdDate()
  832. /**
  833. * Get the date from the newsserver format of returned date
  834. *
  835. * @param bool $timestap when false function returns string, and when true function returns int/timestamp.
  836. *
  837. * @return mixed (string) 'YYYYMMDDhhmmss' / (int) timestamp on success or (object) pear_error on failure
  838. * @access public
  839. */
  840. function cmdDate($timestamp = false)
  841. {
  842. $response = $this->_sendCommand('DATE');
  843. if (PEAR::isError($response)){
  844. return $response;
  845. }
  846. switch ($response) {
  847. case 111: // RFC2980: 'YYYYMMDDhhmmss'
  848. $d = $this->currentStatusResponse();
  849. if ($timestamp === false) {
  850. return (string) $d;
  851. } else {
  852. return (int) strtotime(substr($d, 0, 8).' '.$d[8].$d[9].':'.$d[10].$d[11].':'.$d[12].$d[13]);
  853. }
  854. break;
  855. default:
  856. return PEAR::throwError('Unidentified response code', $response, $this->currentStatusResponse());
  857. }
  858. }
  859. // }}}
  860. // {{{ isConnected()
  861. /**
  862. * Test whether we are connected or not.
  863. *
  864. * @return bool true or false
  865. *
  866. * @access public
  867. */
  868. function isConnected()
  869. {
  870. return (is_resource($this->_socket->fp) && !feof($this->_socket->fp));
  871. }
  872. // }}}
  873. // {{{ setDebug()
  874. /**
  875. * Sets the debuging information on or off
  876. *
  877. * @param boolean True or false
  878. *
  879. * @return bool previos state
  880. * @access public
  881. */
  882. function setDebug($debug = true)
  883. {
  884. $tmp = $this->_debug;
  885. $this->_debug = $debug;
  886. return $tmp;
  887. }
  888. // }}}
  889. // {{{ _getStatusResponse()
  890. /**
  891. * Get servers statusresponse after a command.
  892. *
  893. * @return mixed (int) statuscode on success or (object) pear_error on failure
  894. * @access private
  895. */
  896. function _getStatusResponse()
  897. {
  898. // Retrieve a line (terminated by "\r\n") from the server.
  899. $response = $this->_socket->gets(256);
  900. if (PEAR::isError($response) ) {
  901. return PEAR::throwError('Failed to read from socket!', null, $response->getMessage());
  902. }
  903. if ($this->_debug) {
  904. echo "S: $response\r\n";
  905. }
  906. // Trim the start of the response in case of misplased whitespace (should not be needen!!!)
  907. $response = ltrim($response);
  908. $this->_currentStatusResponse = array(
  909. (int) substr($response, 0, 3),
  910. (string) rtrim(substr($response, 4))
  911. );
  912. return $this->_currentStatusResponse[0];
  913. }
  914. // }}}
  915. // {{{ currentStatusResponse()
  916. /**
  917. *
  918. *
  919. * @return string status text
  920. * @access private
  921. */
  922. function currentStatusResponse()
  923. {
  924. return $this->_currentStatusResponse[1];
  925. }
  926. // }}}
  927. // {{{ _getTextResponse()
  928. /**
  929. * Get data until a line with only a '.' in it is read and return data.
  930. *
  931. * @return mixed (array) text response on success or (object) pear_error on failure
  932. * @access private
  933. */
  934. function _getTextResponse()
  935. {
  936. $data = array();
  937. $line = '';
  938. // Continue until connection is lost
  939. while(!$this->_socket->eof()) {
  940. // Retrieve and append up to 1024 characters from the server.
  941. $line .= $this->_socket->gets(1024);
  942. if (PEAR::isError($line) ) {
  943. return PEAR::throwError( 'Failed to read from socket!', null, $line->getMessage());
  944. }
  945. // Continue if the line is not terminated by CRLF
  946. if (substr($line, -2) != "\r\n" || strlen($line) < 2) {
  947. continue;
  948. }
  949. // Validate recieved line
  950. if (false) {
  951. // Lines should/may not be longer than 998+2 chars (RFC2822 2.3)
  952. if (strlen($line) > 1000) {
  953. return PEAR::throwError('Invalid line recieved!', null);
  954. }
  955. }
  956. // Remove CRLF from the end of the line
  957. $line = substr($line, 0, -2);
  958. // Check if the line terminates the textresponse
  959. if ($line == '.') {
  960. // return all previous lines
  961. return $data;
  962. break;
  963. }
  964. // If 1st char is '.' it's doubled (NNTP/RFC977 2.4.1)
  965. if (substr($line, 0, 2) == '..') {
  966. $line = substr($line, 1);
  967. }
  968. // Add the line to the array of lines
  969. $data[] = $line;
  970. // Reset/empty $line
  971. $line = '';
  972. }
  973. return PEAR::throwError('Data stream not terminated with period', null);
  974. }
  975. // }}}
  976. // {{{ _sendCommand()
  977. /**
  978. * Send a command to the server. A carriage return / linefeed (CRLF) sequence
  979. * will be appended to each command string before it is sent to the IMAP server.
  980. *
  981. * @param string $cmd The command to launch, ie: "ARTICLE 1004853"
  982. *
  983. * @return mixed (int) response code on success or (object) pear_error on failure
  984. * @access private
  985. */
  986. function _sendCommand($cmd)
  987. {
  988. // NNTP/RFC977 only allows command up to 512 (-2) chars.
  989. if (!strlen($cmd) > 510) {
  990. return PEAR::throwError('Failed to write to socket! (Command to long - max 510 chars)');
  991. }
  992. // Check if connected
  993. if (!$this->isConnected()) {
  994. return PEAR::throwError('Failed to write to socket! (connection lost!)');
  995. }
  996. // Send the command
  997. $R = $this->_socket->writeLine($cmd);
  998. if ( PEAR::isError($R) ) {
  999. return PEAR::throwError('Failed to write to socket!', null, $R->getMessage());
  1000. }
  1001. if ($this->_debug) {
  1002. echo "C: $cmd\r\n";
  1003. }
  1004. return $this->_getStatusResponse();
  1005. }
  1006. // }}}
  1007. }
  1008. ?>