jabber_s/doc/web/subscribe.php

00001 <?php
00002 # *** ---------------------------------------- ***
00003 # IM Gateway subscription page
00004 # contact daniel for anything related to it
00005 # *** ---------------------------------------- ***
00006 #
00007 include ("libjab.php");
00008 $jserver = "127.0.0.1";   # Jabber server address
00009 $jport = "5222";     # Jabber server port
00010 $jcid  = 0;      # Jabber communication ID
00011 #
00012 /* **************************************
00013 # main SER database - users profile table - used for authentication
00014 $sip_db_srv="127.0.0.1";  # database server
00015 $sip_db_usr="ser";  # database user
00016 $sip_db_pas="***";  # database user's password
00017 $sip_db_db="ser";   # database name
00018 $sip_db_tab="subscriber";  # name of users table
00019 $sip_db_cusr="user"; # column name for username
00020 $sip_db_cpas="password"; # column name for user's password
00021 *************************************** */
00022 #
00023 # Jabber module database
00024 $jab_db_srv="127.0.0.1";  # database server
00025 $jab_db_usr="ser";  # database user
00026 $jab_db_pas="***";  # database user's password
00027 $jab_db_db="sip_jab";   # database name
00028 #
00029 function html_die($message)
00030 {
00031     echo "$message </DIV></BODY></HTML>";
00032     exit();
00033 }
00034 #
00035 function dbg_msg($message)
00036 {
00037     # echo "$message";
00038 }
00039 ?>
00040 <HTML>
00041 <HEAD>
00042 <TITLE>IM Gateway registration</TITLE>
00043 </HEAD>
00044 
00045 <BODY>
00046 <DIV ALIGN="center">
00047 <?php
00048 if(!isset($action) || $action=="" || !isset($sipname) || $sipname=="" || !isset($imtype) || $imtype=="" || ($action=="Subscribe" && (!isset($imname) || $imname=="")))
00049 {
00050 ?>
00051         <b>Subscription page for Instant Messaging gateway</b>
00052         <br>
00053         You MUST have a SIP account on IPTEL.ORG
00054         <br><hr size="1" width="60%"><br>
00055         <TABLE>
00056         <FORM action="/im/subscribe.php" method="post">
00057                 <TR>
00058                 <TD>SIP username:</TD><TD><INPUT type="text" value="<?php echo $sipname;?>" name="sipname" size="32"></TD>
00059                 </TR>
00060                 <TR>
00061                 <TD>SIP password:</TD><TD><INPUT type="password" name="sippasswd" size="32"></TD>
00062                 </TR>
00063 
00064                 <TR>
00065                 <TD COLSPAN="2"><b><i><hr></i></b></TD>
00066                 </TR>
00067                 <TR>
00068                 <TD>My Jabber account:</TD>
00069                 <TD align="right">
00070                     <INPUT type="submit" name="action" value="Enable">
00071                         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
00072                         <INPUT type="submit" name="action" value="Disable">
00073                 </TD>
00074                 </TR>
00075                 <TR>
00076                 <TD COLSPAN="2"><b><i><hr></i></b></TD>
00077                 </TR>
00078 
00079                 <TR>
00080                 <TD>IM service:</TD>
00081                 <TD>
00082                         <SELECT name="imtype">
00083                                 <OPTION value="aim">AIM </OPTION>
00084                                 <OPTION value="icq">ICQ </OPTION>
00085                                 <OPTION value="msn">MSN </OPTION>
00086                                 <OPTION value="yahoo">Yahoo </OPTION>
00087                         </SELECT>
00088                 </TD>
00089                 </TR>
00090                 <TR>
00091                 <TD>IM nickname:</TD><TD><INPUT type="text" value="<?php echo $imnick;?>" name="imnick" size="32"></TD>
00092                 </TR>
00093                 <TR>
00094                 <TD>IM account:</TD><TD><INPUT type="text" value="<?php echo $imname;?>" name="imname" size="32"></TD>
00095                 </TR>
00096                 <TR>
00097                 <TD>IM password:</TD><TD><INPUT type="password" name="impasswd" size="32"></TD>
00098                 </TR>
00099 
00100                 <TR>
00101                 <TD COLSPAN="2"><b><i><hr></i></b></TD>
00102                 </TR>
00103 
00104                 <TR>
00105                 <TD><INPUT type="reset" value="Reset"></TD>
00106                 <TD>
00107                     <INPUT type="submit" name="action" value="Subscribe">
00108                     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
00109                     <INPUT type="submit" name="action" value="Unsubscribe">
00110                 </TD>
00111                 </TR>
00112         </FORM>
00113         </TABLE>
00114     <DIV ALIGN="left">
00115     <br><br>
00116     <h4>User info</h4>
00117     - for any operation you MUST provide your 'iptel.org' username and password
00118     <br>
00119     <br>
00120     <b><i>AIM Gateway subscription</i></b>
00121     <br>
00122     - choose 'AIM' as 'IM service'<br>
00123     - 'IM nickname' is your display name for AIM network<br>
00124     - 'IM account' is your AIM account name (ex: 'alpha')<br>
00125     - 'IM password' is the password of your AIM account<br>
00126     - click on 'Subscribe'<br>
00127     <b><i>ICQ Gateway subscription</i></b>
00128     <br>
00129     - choose 'ICQ' as 'IM service'<br>
00130     - 'IM nickname' is your display name for ICQ network<br>
00131     - 'IM account' is your ICQ number (ex: '158251040')<br>
00132     - 'IM password' is the password of your ICQ account<br>
00133     - click on 'Subscribe'<br>
00134     <b><i>MSN Gateway subscription</i></b>
00135     <br>
00136     - choose 'MSN' as 'IM service'<br>
00137     - 'IM nickname' is your display name for MSN network<br>
00138     - 'IM account' is your MSN account (ex: 'alpha@hotmail.com' or 'alpha@msn.com')<br>
00139     - 'IM password' is the password of your MSN account<br>
00140     - click on 'Subscribe'<br>
00141     <b><i>Yahoo Gateway subscription</i></b>
00142     <br>
00143     - choose 'Yahoo' as 'IM service'<br>
00144     - 'IM nickname' is your display name for Yahoo network<br>
00145     - 'IM account' is your Yahoo account (ex: 'alpha')<br>
00146     - 'IM password' is the password of your Yahoo account<br>
00147     - click on 'Subscribe'<br>
00148     <br>
00149     <b><i>IM Gateway unsubscription</i></b>
00150     <br>
00151     - choose the 'IM service' from which you want to unsubscribe<br>
00152     - click on 'Unsubscribe'<br>
00153     </DIV>
00154 <?php
00155 }
00156 else
00157 {
00158         # -----
00159         # AUTHENTICATION - verify username and password
00160         # -----
00161         /* ****************************************
00162         echo "<br><h2>Instant Messaging Gateway</h2><hr size=\"1\" width=\"60%\"><br>";
00163         $dblink = mysql_connect($sip_db_srv, $sip_db_usr, $sip_db_pas) or
00164                 html_die("Could not connect to SIP database server");
00165         mysql_select_db($sip_db_db, $dblink) 
00166                 or html_die("Could not select SIP database");
00167         $query = "SELECT $sip_db_cusr FROM $sip_db_tab WHERE $sip_db_cusr='$sipname' AND $sip_db_cpas='$sippasswd'";
00168         dbg_msg("$query <BR>");
00169         $result = mysql_query($query) or html_die("Invalid SQL query");
00170         if(mysql_num_rows($result) == 0)
00171                 html_die("Invalid SIP username or password");
00172         mysql_close($dblink);
00173         ***************************************** */
00174         #
00175         #------------------------------------------------------
00176         #
00177         # -----
00178         # check if is already registered to Jabber gateway
00179         # -----
00180         $sipuri = "sip:".$sipname."@iptel.org";
00181         $dblink = mysql_connect($jab_db_srv, $jab_db_usr, $jab_db_pas) or html_die("Could not connect to Jabber database");
00182         mysql_select_db($jab_db_db, $dblink) or html_die("Could not use Jabber database");
00183         # ----
00184         if($action == "Disable")
00185         {
00186                 $query = "UPDATE jusers SET tyep=1 WHERE sip_id='$sipuri'";
00187                 $result = mysql_query($query, $dblink);
00188                 if(mysql_affected_rows() != 1)
00189                 {
00190                         mysql_close($dblink);
00191                         html_die("<br>Cannot find Jabber ID of '$sipname' in database");
00192                 }
00193                 mysql_close($dblink);
00194                 html_die("<br>Your IM account was updated");
00195         }
00196         # ----
00197         $query = "SELECT jab_id FROM jusers WHERE sip_id='$sipuri'";
00198         $result = mysql_query($query, $dblink) or html_die("Invalid SQL query");
00199         if(mysql_num_rows($result) == 0)
00200         { // no Jabber account - create one
00201                 $fd = jab_connect($jserver, $jport);
00202                 if(!$fd)
00203                         html_die("Could not connect to Jabber server");
00204                 $buf_recv = fread($fd, 2048);
00205                 while(!$buf_recv)
00206                 {
00207                         usleep(100);
00208                         $buf_recv = fread($fd, 2048);
00209                 }
00210                 $jid1 = stristr($buf_recv, "id='");
00211                 $jid1 = substr($jid1, 4);
00212                 if($jid1)
00213                 {
00214                         $jid2 = strstr($jid1, "'");
00215                         if($jid2)
00216                         {
00217                                 $jid = substr($jid1, 0, strlen($jid1)-strlen($jid2));
00218                                 dbg_msg("JID: $jid<BR>");
00219                         }
00220                 }
00221                 $jcid = $jcid + 1;
00222                 jab_get_reg($fd, $jcid, $jserver);
00223                 $buf_recv = fread($fd, 2048);
00224                 while(!$buf_recv)
00225                 {
00226                         usleep(100);
00227                         $buf_recv = fread($fd, 2048);
00228                 }
00229                 $jcid = $jcid + 1;
00230                 $new_passwd = "#".$sipname."%";
00231                 jab_set_reg($fd, $jcid, $jserver, $sipname, $new_passwd);
00232                 $buf_recv = fread($fd, 2048);
00233                 while(!$buf_recv)
00234                 {
00235                         usleep(100);
00236                         $buf_recv = fread($fd, 2048);
00237                 }
00238                 if(stristr($buf_recv, " id='$jcid'") && stristr($buf_recv, " type='error'"))
00239                 {
00240                         mysql_close($dblink);
00241                         jab_disconnect($fd);
00242                         html_die("<br>Something bizarre with account '$sipname'");
00243                 }
00244                 # -----
00245                 # Add user in database
00246                 # -----
00247                 $query = "INSERT INTO jusers (jab_id, jab_passwd, sip_id) VALUES ('$sipname', '$new_passwd', '$sipuri')";
00248                 $result = mysql_query($query, $dblink);
00249                 if(mysql_affected_rows() != 1)
00250                 {
00251                         mysql_close($dblink);
00252                         jab_disconnect($fd);
00253                         html_die("<br>Can not insert '$sipname' in database");
00254                 }
00255                 jab_disconnect($fd);
00256         }
00257         # -----
00258         if($action == "Enable")
00259         {
00260                 $query = "UPDATE jusers SET type=0 WHERE sip_id='$sipuri'";
00261                 $result = mysql_query($query, $dblink);
00262                 if(mysql_affected_rows() != 1)
00263                 {
00264                         mysql_close($dblink);
00265                         html_die("<br>Cannot find Jabber ID of '$sipname' in database");
00266                 }
00267                 mysql_close($dblink);
00268                 html_die("<br>Your IM account was updated");
00269         }
00270         # -----
00271         $query="SELECT juid,jab_id,jab_passwd,type FROM jusers WHERE
00272         sip_id='$sipuri' and type=0";
00273         $result = mysql_query($query, $dblink) or html_die("Invalid SQL query");
00274         if(mysql_num_rows($result) != 1 || (!($row = mysql_fetch_array($result))))
00275         {
00276                 mysql_close($dblink);
00277                 html_die("<br>You do not have an associated Jabber account or it is
00278                 disabled!<br>Press 'Enable' in order to create a new one or to activate an
00279                 old one.<br>If error persists, please inform the administrator.");
00280         }
00281 
00282         $juid = $row[0];
00283         $jab_id = $row[1];
00284         $jab_passwd = $row[2];
00285         $jab_type = $row[3];
00286         dbg_msg("Jabber User ID: $juid<BR>");
00287         $fd = jab_connect($jserver, $jport);
00288         if(!$fd)
00289                 html_die("Could not connect to Jabber server");
00290         $buf_recv = fread($fd, 2048);
00291         while(!$buf_recv)
00292         {
00293                 usleep(100);
00294                 $buf_recv = fread($fd, 2048);
00295         }
00296         $jid1 = stristr($buf_recv, "id='");
00297         $jid1 = substr($jid1, 4);
00298         if($jid1)
00299         {
00300                 $jid2 = strstr($jid1, "'");
00301                 if($jid2)
00302                 {
00303                         $jid = substr($jid1, 0, strlen($jid1)-strlen($jid2));
00304                         dbg_msg("JID: $jid<BR>");
00305                 }
00306         }
00307         $jcid = $jcid + 1;
00308         jab_get_auth($fd, $jcid, $jab_id);
00309         $buf_recv = fread($fd, 2048);
00310         while(!$buf_recv)
00311         {
00312                 usleep(100);
00313                 $buf_recv = fread($fd, 2048);
00314         }
00315         $jcid = $jcid + 1;
00316         jab_set_auth($fd, $jcid, $jab_id, $jab_passwd);
00317 
00318         $buf_recv = fread($fd, 2048);
00319         while(!$buf_recv)
00320         {
00321                 usleep(100);
00322                 $buf_recv = fread($fd, 2048);
00323         }
00324         if(stristr($buf_recv, " id='$jcid'") && stristr($buf_recv, " type='error'"))
00325         {
00326                 jab_disconnect($fd);
00327                 html_die("<br>Wrong username or password at Jabber authentication");
00328         }
00329         # -----
00330         # browse agents
00331         # -----
00332         $jcid = $jcid + 1;
00333         jab_get_agents($fd, $jcid, $jserver);
00334         $buf_agents = fread($fd, 4096);
00335         while(!$buf_agents)
00336         {
00337                 usleep(100);
00338                 $buf_agents = fread($fd, 4096);
00339         }
00340         # dbg_msg("\n<!-- $buf_agents -->");
00341         # -----
00342         $imag1 = stristr($buf_agents, "<agent jid='".$imtype.".");
00343         $imag1 = substr($imag1, strlen("<agent jid='"));
00344         if($imag1)
00345         {
00346                 $imag2 = strstr($imag1, "'");
00347                 if($imag2)
00348                 {
00349                         $imag = substr($imag1, 0, strlen($imag1)-strlen($imag2));
00350                         dbg_msg("IMAgent: $imag<BR>");
00351                 }
00352         }
00353         # -----
00354         if(isset($imag))
00355         {
00356                 if($action == "Subscribe" && isset($imname) && $imname != "")
00357                 {
00358                         echo "<h3><i>IM ($imtype) subscription</i></h3><BR>";
00359                         # -----
00360                         # unsubscribe the previous IM account (if exists)
00361                         # -----
00362                         $jcid = $jcid + 1;
00363                         jab_set_unreg($fd, $jcid, $icqag);
00364                         $buf_recv = fread($fd, 2048);
00365                         while(!$buf_recv)
00366                         {
00367                                 usleep(100);
00368                                 $buf_recv = fread($fd, 2048);
00369                         }
00370                         sleep(1);
00371                         # -----
00372                         # subscription
00373                         # -----
00374                         $jcid = $jcid + 1;
00375                         jab_get_reg($fd, $jcid, $imag);
00376                         $buf_recv = fread($fd, 2048);
00377                         while(!$buf_recv)
00378                         {
00379                                 usleep(100);
00380                                 $buf_recv = fread($fd, 2048);
00381                         }
00382                         $imkey1 = stristr($buf_recv, "<key>");
00383                         $imkey1 = substr($imkey1, 5);
00384                         if($imkey1)
00385                         {
00386                                 $imkey2 = strstr($imkey1, "</key>");
00387                                 if($imkey2)
00388                                 {
00389                                         $imkey = substr($imkey1, 0, strlen($imkey1)-strlen($imkey2));
00390                                         dbg_msg("IM key: $imkey<BR>");
00391                                 }
00392                         }
00393                         if(!isset($imkey))
00394                         {
00395                                 jab_disconnect($fd);
00396                                 mysql_close($dblink);
00397                                 html_die("<br>Session key for IM ($imtype) Transport not found");
00398                         }
00399                         $jcid = $jcid + 1;
00400                         jab_set_regk($fd, $jcid, $imag, $imname, $impasswd, $imnick, $imkey);
00401                         $buf_recv = fread($fd, 2048);
00402                         while(!$buf_recv)
00403                         {
00404                                 usleep(100);
00405                                 $buf_recv = fread($fd, 2048);
00406                         }
00407                         if(stristr($buf_recv, " id='$jcid'") && stristr($buf_recv, " type='error'"))
00408                         {
00409                                 $err1 = stristr($buf_recv, "<error ");
00410                                 $err1 = substr($err1, 7);
00411                                 $err1 = strstr($err1, ">");
00412                                 $err1 = substr($err1, 1);
00413                                 if($err1)
00414                                 {
00415                                         $err2 = strstr($err1, "</error>");
00416                                         if($err2)
00417                                                 $err = substr($err1, 0, strlen($err1)-strlen($err2));
00418                                 }
00419                                 jab_disconnect($fd);
00420                                 mysql_close($dblink);
00421                                 html_die("<br><b>Error registering your IM ($imtype) account: <i>$err</i></BODY></HTML>");
00422                         }
00423                         jab_send_presence($fd, $imag."/registered", "subscribed");
00424                         # -----
00425                         # Update database
00426                         $query = "SELECT ".$imtype."_id FROM ".$imtype." WHERE juid='$juid'";
00427                         $result = mysql_query($query, $dblink) or html_die("Invalid SQL query");
00428                         if(mysql_num_rows($result) == 0)
00429                         { # INSERT
00430                                 $query = "INSERT INTO ".$imtype." (juid, ".$imtype."_id, ".$imtype."_passwd, ".$imtype."_nick) VALUES ('$juid', '$imname', '$impasswd', '$imnick')";
00431                                 dbg_msg("$query <br>");
00432                                 $result = mysql_query($query, $dblink);
00433                                 if(mysql_affected_rows() != 1)
00434                                 {
00435                                         echo "<br><b>Can not register '$sipname'/'$imname'</b></br>";
00436                                 }
00437                                 else
00438                                 {
00439                                         echo "<b>Your IM ($imtype) account was successfully registered</b><BR>";
00440                                 }
00441                         }
00442                         else
00443                         { # UPDATE
00444                                 $query = "UPDATE ".$imtype." SET ".$imtype."_id='$imname', ".$imtype."_passwd='$impasswd', ".$imtype."_nick='$imnick' WHERE juid='$juid'";
00445                                 dbg_msg("$query <br>");
00446                                 $result = mysql_query($query, $dblink);
00447                                 if(!$result)
00448                                 {
00449                                         echo "<br>Can not update '$sipname'/'$imname'<br>";
00450                                 }
00451                                 else
00452                                 {
00453                                         if(mysql_affected_rows() == 1)
00454                                         {
00455                                                 echo "Your IM ($imtype) account was successfully updated<BR>";
00456                                         }
00457                                         else
00458                                         {
00459                                                 echo "No modification in your IM ($imtype) account<BR>";
00460                                         }
00461                                 }
00462                         }
00463                 }
00464                 else
00465                 {
00466                         echo "<h3><i>IM ($imtype) unsubscription</i></h3><BR>";
00467                         # -----
00468                         # unsubscribe the IM account
00469                         # -----
00470                         $jcid = $jcid + 1;
00471                         jab_set_unreg($fd, $jcid, $icqag);
00472                         $buf_recv = fread($fd, 2048);
00473                         while(!$buf_recv)
00474                         {
00475                                 usleep(100);
00476                                 $buf_recv = fread($fd, 2048);
00477                         }
00478                         sleep(1);
00479                         $query = "DELETE FROM ".$imtype." WHERE juid='$juid'";
00480                         dbg_msg("$query <br>");
00481                         $result = mysql_query($query, $dblink);
00482                         if(!$result)
00483                         {
00484                                 echo "<br>Can not remove IM ($imtype) information from database<br>";
00485                         }
00486                         else
00487                         {
00488                                 echo "<br>Unsubscription from IM ($imtype) completed<br>";
00489                         }
00490                 }
00491         }
00492         sleep(1);
00493         jab_disconnect($fd);
00494         mysql_close($dblink);
00495 }
00496 ?>
00497 </DIV>
00498 
00499 </BODY>
00500 </HTML>