PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/jabber/doc/web/subscribe.php

https://github.com/eliasbaixas/openser-xmlrpc
PHP | 500 lines | 438 code | 7 blank | 55 comment | 39 complexity | fefa393233eae46a7b39cb0318f16857 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. # *** ---------------------------------------- ***
  3. # IM Gateway subscription page
  4. # contact daniel for anything related to it
  5. # *** ---------------------------------------- ***
  6. #
  7. include ("libjab.php");
  8. $jserver = "127.0.0.1"; # Jabber server address
  9. $jport = "5222"; # Jabber server port
  10. $jcid = 0; # Jabber communication ID
  11. #
  12. /* **************************************
  13. # main database - users profile table - used for authentication
  14. $sip_db_srv="127.0.0.1"; # database server
  15. $sip_db_usr="openser"; # database user
  16. $sip_db_pas="***"; # database user's password
  17. $sip_db_db="openser"; # database name
  18. $sip_db_tab="subscriber"; # name of users table
  19. $sip_db_cusr="user"; # column name for username
  20. $sip_db_cpas="password"; # column name for user's password
  21. *************************************** */
  22. #
  23. # Jabber module database
  24. $jab_db_srv="127.0.0.1"; # database server
  25. $jab_db_usr="openser"; # database user
  26. $jab_db_pas="***"; # database user's password
  27. $jab_db_db="sip_jab"; # database name
  28. #
  29. function html_die($message)
  30. {
  31. echo "$message </DIV></BODY></HTML>";
  32. exit();
  33. }
  34. #
  35. function dbg_msg($message)
  36. {
  37. # echo "$message";
  38. }
  39. ?>
  40. <HTML>
  41. <HEAD>
  42. <TITLE>IM Gateway registration</TITLE>
  43. </HEAD>
  44. <BODY>
  45. <DIV ALIGN="center">
  46. <?php
  47. if(!isset($action) || $action=="" || !isset($sipname) || $sipname=="" || !isset($imtype) || $imtype=="" || ($action=="Subscribe" && (!isset($imname) || $imname=="")))
  48. {
  49. ?>
  50. <b>Subscription page for Instant Messaging gateway</b>
  51. <br>
  52. You MUST have a SIP account
  53. <br><hr size="1" width="60%"><br>
  54. <TABLE>
  55. <FORM action="/im/subscribe.php" method="post">
  56. <TR>
  57. <TD>SIP username:</TD><TD><INPUT type="text" value="<?php echo $sipname;?>" name="sipname" size="32"></TD>
  58. </TR>
  59. <TR>
  60. <TD>SIP password:</TD><TD><INPUT type="password" name="sippasswd" size="32"></TD>
  61. </TR>
  62. <TR>
  63. <TD COLSPAN="2"><b><i><hr></i></b></TD>
  64. </TR>
  65. <TR>
  66. <TD>My Jabber account:</TD>
  67. <TD align="right">
  68. <INPUT type="submit" name="action" value="Enable">
  69. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  70. <INPUT type="submit" name="action" value="Disable">
  71. </TD>
  72. </TR>
  73. <TR>
  74. <TD COLSPAN="2"><b><i><hr></i></b></TD>
  75. </TR>
  76. <TR>
  77. <TD>IM service:</TD>
  78. <TD>
  79. <SELECT name="imtype">
  80. <OPTION value="aim">AIM </OPTION>
  81. <OPTION value="icq">ICQ </OPTION>
  82. <OPTION value="msn">MSN </OPTION>
  83. <OPTION value="yahoo">Yahoo </OPTION>
  84. </SELECT>
  85. </TD>
  86. </TR>
  87. <TR>
  88. <TD>IM nickname:</TD><TD><INPUT type="text" value="<?php echo $imnick;?>" name="imnick" size="32"></TD>
  89. </TR>
  90. <TR>
  91. <TD>IM account:</TD><TD><INPUT type="text" value="<?php echo $imname;?>" name="imname" size="32"></TD>
  92. </TR>
  93. <TR>
  94. <TD>IM password:</TD><TD><INPUT type="password" name="impasswd" size="32"></TD>
  95. </TR>
  96. <TR>
  97. <TD COLSPAN="2"><b><i><hr></i></b></TD>
  98. </TR>
  99. <TR>
  100. <TD><INPUT type="reset" value="Reset"></TD>
  101. <TD>
  102. <INPUT type="submit" name="action" value="Subscribe">
  103. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  104. <INPUT type="submit" name="action" value="Unsubscribe">
  105. </TD>
  106. </TR>
  107. </FORM>
  108. </TABLE>
  109. <DIV ALIGN="left">
  110. <br><br>
  111. <h4>User info</h4>
  112. - for any operation you MUST provide your username and password
  113. <br>
  114. <br>
  115. <b><i>AIM Gateway subscription</i></b>
  116. <br>
  117. - choose 'AIM' as 'IM service'<br>
  118. - 'IM nickname' is your display name for AIM network<br>
  119. - 'IM account' is your AIM account name (ex: 'alpha')<br>
  120. - 'IM password' is the password of your AIM account<br>
  121. - click on 'Subscribe'<br>
  122. <b><i>ICQ Gateway subscription</i></b>
  123. <br>
  124. - choose 'ICQ' as 'IM service'<br>
  125. - 'IM nickname' is your display name for ICQ network<br>
  126. - 'IM account' is your ICQ number (ex: '158251040')<br>
  127. - 'IM password' is the password of your ICQ account<br>
  128. - click on 'Subscribe'<br>
  129. <b><i>MSN Gateway subscription</i></b>
  130. <br>
  131. - choose 'MSN' as 'IM service'<br>
  132. - 'IM nickname' is your display name for MSN network<br>
  133. - 'IM account' is your MSN account (ex: 'alpha@hotmail.com' or 'alpha@msn.com')<br>
  134. - 'IM password' is the password of your MSN account<br>
  135. - click on 'Subscribe'<br>
  136. <b><i>Yahoo Gateway subscription</i></b>
  137. <br>
  138. - choose 'Yahoo' as 'IM service'<br>
  139. - 'IM nickname' is your display name for Yahoo network<br>
  140. - 'IM account' is your Yahoo account (ex: 'alpha')<br>
  141. - 'IM password' is the password of your Yahoo account<br>
  142. - click on 'Subscribe'<br>
  143. <br>
  144. <b><i>IM Gateway unsubscription</i></b>
  145. <br>
  146. - choose the 'IM service' from which you want to unsubscribe<br>
  147. - click on 'Unsubscribe'<br>
  148. </DIV>
  149. <?php
  150. }
  151. else
  152. {
  153. # -----
  154. # AUTHENTICATION - verify username and password
  155. # -----
  156. /* ****************************************
  157. echo "<br><h2>Instant Messaging Gateway</h2><hr size=\"1\" width=\"60%\"><br>";
  158. $dblink = mysql_connect($sip_db_srv, $sip_db_usr, $sip_db_pas) or
  159. html_die("Could not connect to SIP database server");
  160. mysql_select_db($sip_db_db, $dblink)
  161. or html_die("Could not select SIP database");
  162. $query = "SELECT $sip_db_cusr FROM $sip_db_tab WHERE $sip_db_cusr='$sipname' AND $sip_db_cpas='$sippasswd'";
  163. dbg_msg("$query <BR>");
  164. $result = mysql_query($query) or html_die("Invalid SQL query");
  165. if(mysql_num_rows($result) == 0)
  166. html_die("Invalid SIP username or password");
  167. mysql_close($dblink);
  168. ***************************************** */
  169. #
  170. #------------------------------------------------------
  171. #
  172. # -----
  173. # check if is already registered to Jabber gateway
  174. # -----
  175. $sipuri = "sip:".$sipname."@example.org";
  176. $dblink = mysql_connect($jab_db_srv, $jab_db_usr, $jab_db_pas) or html_die("Could not connect to Jabber database");
  177. mysql_select_db($jab_db_db, $dblink) or html_die("Could not use Jabber database");
  178. # ----
  179. if($action == "Disable")
  180. {
  181. $query = "UPDATE jusers SET tyep=1 WHERE sip_id='$sipuri'";
  182. $result = mysql_query($query, $dblink);
  183. if(mysql_affected_rows() != 1)
  184. {
  185. mysql_close($dblink);
  186. html_die("<br>Cannot find Jabber ID of '$sipname' in database");
  187. }
  188. mysql_close($dblink);
  189. html_die("<br>Your IM account was updated");
  190. }
  191. # ----
  192. $query = "SELECT jab_id FROM jusers WHERE sip_id='$sipuri'";
  193. $result = mysql_query($query, $dblink) or html_die("Invalid SQL query");
  194. if(mysql_num_rows($result) == 0)
  195. { // no Jabber account - create one
  196. $fd = jab_connect($jserver, $jport);
  197. if(!$fd)
  198. html_die("Could not connect to Jabber server");
  199. $buf_recv = fread($fd, 2048);
  200. while(!$buf_recv)
  201. {
  202. usleep(100);
  203. $buf_recv = fread($fd, 2048);
  204. }
  205. $jid1 = stristr($buf_recv, "id='");
  206. $jid1 = substr($jid1, 4);
  207. if($jid1)
  208. {
  209. $jid2 = strstr($jid1, "'");
  210. if($jid2)
  211. {
  212. $jid = substr($jid1, 0, strlen($jid1)-strlen($jid2));
  213. dbg_msg("JID: $jid<BR>");
  214. }
  215. }
  216. $jcid = $jcid + 1;
  217. jab_get_reg($fd, $jcid, $jserver);
  218. $buf_recv = fread($fd, 2048);
  219. while(!$buf_recv)
  220. {
  221. usleep(100);
  222. $buf_recv = fread($fd, 2048);
  223. }
  224. $jcid = $jcid + 1;
  225. $new_passwd = "#".$sipname."%";
  226. jab_set_reg($fd, $jcid, $jserver, $sipname, $new_passwd);
  227. $buf_recv = fread($fd, 2048);
  228. while(!$buf_recv)
  229. {
  230. usleep(100);
  231. $buf_recv = fread($fd, 2048);
  232. }
  233. if(stristr($buf_recv, " id='$jcid'") && stristr($buf_recv, " type='error'"))
  234. {
  235. mysql_close($dblink);
  236. jab_disconnect($fd);
  237. html_die("<br>Something bizarre with account '$sipname'");
  238. }
  239. # -----
  240. # Add user in database
  241. # -----
  242. $query = "INSERT INTO jusers (jab_id, jab_passwd, sip_id) VALUES ('$sipname', '$new_passwd', '$sipuri')";
  243. $result = mysql_query($query, $dblink);
  244. if(mysql_affected_rows() != 1)
  245. {
  246. mysql_close($dblink);
  247. jab_disconnect($fd);
  248. html_die("<br>Can not insert '$sipname' in database");
  249. }
  250. jab_disconnect($fd);
  251. }
  252. # -----
  253. if($action == "Enable")
  254. {
  255. $query = "UPDATE jusers SET type=0 WHERE sip_id='$sipuri'";
  256. $result = mysql_query($query, $dblink);
  257. if(mysql_affected_rows() != 1)
  258. {
  259. mysql_close($dblink);
  260. html_die("<br>Cannot find Jabber ID of '$sipname' in database");
  261. }
  262. mysql_close($dblink);
  263. html_die("<br>Your IM account was updated");
  264. }
  265. # -----
  266. $query="SELECT juid,jab_id,jab_passwd,type FROM jusers WHERE
  267. sip_id='$sipuri' and type=0";
  268. $result = mysql_query($query, $dblink) or html_die("Invalid SQL query");
  269. if(mysql_num_rows($result) != 1 || (!($row = mysql_fetch_array($result))))
  270. {
  271. mysql_close($dblink);
  272. html_die("<br>You do not have an associated Jabber account or it is
  273. disabled!<br>Press 'Enable' in order to create a new one or to activate an
  274. old one.<br>If error persists, please inform the administrator.");
  275. }
  276. $juid = $row[0];
  277. $jab_id = $row[1];
  278. $jab_passwd = $row[2];
  279. $jab_type = $row[3];
  280. dbg_msg("Jabber User ID: $juid<BR>");
  281. $fd = jab_connect($jserver, $jport);
  282. if(!$fd)
  283. html_die("Could not connect to Jabber server");
  284. $buf_recv = fread($fd, 2048);
  285. while(!$buf_recv)
  286. {
  287. usleep(100);
  288. $buf_recv = fread($fd, 2048);
  289. }
  290. $jid1 = stristr($buf_recv, "id='");
  291. $jid1 = substr($jid1, 4);
  292. if($jid1)
  293. {
  294. $jid2 = strstr($jid1, "'");
  295. if($jid2)
  296. {
  297. $jid = substr($jid1, 0, strlen($jid1)-strlen($jid2));
  298. dbg_msg("JID: $jid<BR>");
  299. }
  300. }
  301. $jcid = $jcid + 1;
  302. jab_get_auth($fd, $jcid, $jab_id);
  303. $buf_recv = fread($fd, 2048);
  304. while(!$buf_recv)
  305. {
  306. usleep(100);
  307. $buf_recv = fread($fd, 2048);
  308. }
  309. $jcid = $jcid + 1;
  310. jab_set_auth($fd, $jcid, $jab_id, $jab_passwd);
  311. $buf_recv = fread($fd, 2048);
  312. while(!$buf_recv)
  313. {
  314. usleep(100);
  315. $buf_recv = fread($fd, 2048);
  316. }
  317. if(stristr($buf_recv, " id='$jcid'") && stristr($buf_recv, " type='error'"))
  318. {
  319. jab_disconnect($fd);
  320. html_die("<br>Wrong username or password at Jabber authentication");
  321. }
  322. # -----
  323. # browse agents
  324. # -----
  325. $jcid = $jcid + 1;
  326. jab_get_agents($fd, $jcid, $jserver);
  327. $buf_agents = fread($fd, 4096);
  328. while(!$buf_agents)
  329. {
  330. usleep(100);
  331. $buf_agents = fread($fd, 4096);
  332. }
  333. # dbg_msg("\n<!-- $buf_agents -->");
  334. # -----
  335. $imag1 = stristr($buf_agents, "<agent jid='".$imtype.".");
  336. $imag1 = substr($imag1, strlen("<agent jid='"));
  337. if($imag1)
  338. {
  339. $imag2 = strstr($imag1, "'");
  340. if($imag2)
  341. {
  342. $imag = substr($imag1, 0, strlen($imag1)-strlen($imag2));
  343. dbg_msg("IMAgent: $imag<BR>");
  344. }
  345. }
  346. # -----
  347. if(isset($imag))
  348. {
  349. if($action == "Subscribe" && isset($imname) && $imname != "")
  350. {
  351. echo "<h3><i>IM ($imtype) subscription</i></h3><BR>";
  352. # -----
  353. # unsubscribe the previous IM account (if exists)
  354. # -----
  355. $jcid = $jcid + 1;
  356. jab_set_unreg($fd, $jcid, $icqag);
  357. $buf_recv = fread($fd, 2048);
  358. while(!$buf_recv)
  359. {
  360. usleep(100);
  361. $buf_recv = fread($fd, 2048);
  362. }
  363. sleep(1);
  364. # -----
  365. # subscription
  366. # -----
  367. $jcid = $jcid + 1;
  368. jab_get_reg($fd, $jcid, $imag);
  369. $buf_recv = fread($fd, 2048);
  370. while(!$buf_recv)
  371. {
  372. usleep(100);
  373. $buf_recv = fread($fd, 2048);
  374. }
  375. $imkey1 = stristr($buf_recv, "<key>");
  376. $imkey1 = substr($imkey1, 5);
  377. if($imkey1)
  378. {
  379. $imkey2 = strstr($imkey1, "</key>");
  380. if($imkey2)
  381. {
  382. $imkey = substr($imkey1, 0, strlen($imkey1)-strlen($imkey2));
  383. dbg_msg("IM key: $imkey<BR>");
  384. }
  385. }
  386. if(!isset($imkey))
  387. {
  388. jab_disconnect($fd);
  389. mysql_close($dblink);
  390. html_die("<br>Session key for IM ($imtype) Transport not found");
  391. }
  392. $jcid = $jcid + 1;
  393. jab_set_regk($fd, $jcid, $imag, $imname, $impasswd, $imnick, $imkey);
  394. $buf_recv = fread($fd, 2048);
  395. while(!$buf_recv)
  396. {
  397. usleep(100);
  398. $buf_recv = fread($fd, 2048);
  399. }
  400. if(stristr($buf_recv, " id='$jcid'") && stristr($buf_recv, " type='error'"))
  401. {
  402. $err1 = stristr($buf_recv, "<error ");
  403. $err1 = substr($err1, 7);
  404. $err1 = strstr($err1, ">");
  405. $err1 = substr($err1, 1);
  406. if($err1)
  407. {
  408. $err2 = strstr($err1, "</error>");
  409. if($err2)
  410. $err = substr($err1, 0, strlen($err1)-strlen($err2));
  411. }
  412. jab_disconnect($fd);
  413. mysql_close($dblink);
  414. html_die("<br><b>Error registering your IM ($imtype) account: <i>$err</i></BODY></HTML>");
  415. }
  416. jab_send_presence($fd, $imag."/registered", "subscribed");
  417. # -----
  418. # Update database
  419. $query = "SELECT ".$imtype."_id FROM ".$imtype." WHERE juid='$juid'";
  420. $result = mysql_query($query, $dblink) or html_die("Invalid SQL query");
  421. if(mysql_num_rows($result) == 0)
  422. { # INSERT
  423. $query = "INSERT INTO ".$imtype." (juid, ".$imtype."_id, ".$imtype."_passwd, ".$imtype."_nick) VALUES ('$juid', '$imname', '$impasswd', '$imnick')";
  424. dbg_msg("$query <br>");
  425. $result = mysql_query($query, $dblink);
  426. if(mysql_affected_rows() != 1)
  427. {
  428. echo "<br><b>Can not register '$sipname'/'$imname'</b></br>";
  429. }
  430. else
  431. {
  432. echo "<b>Your IM ($imtype) account was successfully registered</b><BR>";
  433. }
  434. }
  435. else
  436. { # UPDATE
  437. $query = "UPDATE ".$imtype." SET ".$imtype."_id='$imname', ".$imtype."_passwd='$impasswd', ".$imtype."_nick='$imnick' WHERE juid='$juid'";
  438. dbg_msg("$query <br>");
  439. $result = mysql_query($query, $dblink);
  440. if(!$result)
  441. {
  442. echo "<br>Can not update '$sipname'/'$imname'<br>";
  443. }
  444. else
  445. {
  446. if(mysql_affected_rows() == 1)
  447. {
  448. echo "Your IM ($imtype) account was successfully updated<BR>";
  449. }
  450. else
  451. {
  452. echo "No modification in your IM ($imtype) account<BR>";
  453. }
  454. }
  455. }
  456. }
  457. else
  458. {
  459. echo "<h3><i>IM ($imtype) unsubscription</i></h3><BR>";
  460. # -----
  461. # unsubscribe the IM account
  462. # -----
  463. $jcid = $jcid + 1;
  464. jab_set_unreg($fd, $jcid, $icqag);
  465. $buf_recv = fread($fd, 2048);
  466. while(!$buf_recv)
  467. {
  468. usleep(100);
  469. $buf_recv = fread($fd, 2048);
  470. }
  471. sleep(1);
  472. $query = "DELETE FROM ".$imtype." WHERE juid='$juid'";
  473. dbg_msg("$query <br>");
  474. $result = mysql_query($query, $dblink);
  475. if(!$result)
  476. {
  477. echo "<br>Can not remove IM ($imtype) information from database<br>";
  478. }
  479. else
  480. {
  481. echo "<br>Unsubscription from IM ($imtype) completed<br>";
  482. }
  483. }
  484. }
  485. sleep(1);
  486. jab_disconnect($fd);
  487. mysql_close($dblink);
  488. }
  489. ?>
  490. </DIV>
  491. </BODY>
  492. </HTML>