irc.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. require_once(dirname(dirname(__FILE__))."/header.php");
  3. require_once('xmlrpc.php');
  4. function atheme_login($hostname,$port, $path, $username, $password){
  5. $message = new xmlrpcmsg("atheme.login");
  6. $message->addParam(new xmlrpcval($username, "string"));
  7. $message->addParam(new xmlrpcval($password, "string"));
  8. $client = new xmlrpc_client($path, $hostname, $port);
  9. $response = $client->send($message);
  10. if(!$response->faultCode()){
  11. $session = explode("<string>", $response->serialize());
  12. $session = explode("</string", $session[1]);
  13. $session = $session[0];
  14. return Array(true,$session);
  15. }else{
  16. return Array(
  17. false,
  18. '['.$response->faultCode().'] '.$response->faultString()
  19. );
  20. }
  21. }
  22. function atheme_command($hostname, $port, $path, $sourceip, $username, $password, $service, $command, $params=NULL){
  23. $message = new xmlrpcmsg("atheme.login");
  24. $message->addParam(new xmlrpcval($username, "string"));
  25. $message->addParam(new xmlrpcval($password, "string"));
  26. $client = new xmlrpc_client($path, $hostname, $port);
  27. $response = $client->send($message);
  28. $session = NULL;
  29. if(!$response->faultCode()){
  30. $session = explode("<string>", $response->serialize());
  31. $session = explode("</string", $session[1]);
  32. $session = $session[0];
  33. }else{
  34. switch($response->faultCode()){
  35. case 1:
  36. $m = 'Insufficient Parameters to login';
  37. break;
  38. case 3:
  39. $m = "Account is not registered";
  40. break;
  41. case 5:
  42. $m = "Invalid Username/Password";
  43. break;
  44. case 6:
  45. $m = "Account is frozen";
  46. break;
  47. default:
  48. $m = "Could not log in";
  49. }
  50. return Array(false,$m);
  51. }
  52. $message = new xmlrpcmsg("atheme.command");
  53. $message->addParam(new xmlrpcval($session, "string"));
  54. $message->addParam(new xmlrpcval($username, "string"));
  55. $message->addParam(new xmlrpcval($sourceip, "string"));
  56. $message->addParam(new xmlrpcval($service, "string"));
  57. $message->addParam(new xmlrpcval($command, "string"));
  58. if($params != NULL){
  59. if(sizeof($params) < 2){
  60. foreach($params as $param){
  61. $message->addParam(new xmlrpcval($param, "string"));
  62. }
  63. }else{
  64. $firstparam = $params[0];
  65. $secondparam = "";
  66. for($i = 1; $i < sizeof($params); $i++){
  67. $secondparam .= $params[$i] . " ";
  68. }
  69. $secondparam = rtrim($secondparam);
  70. $message->addParam(new xmlrpcval($firstparam, "string"));
  71. $message->addParam(new xmlrpcval($secondparam, "string"));
  72. }
  73. }
  74. $response = $client->send($message);
  75. if(!$response->faultCode()){
  76. $response = explode("<string>", $response->serialize());
  77. $response = explode("</string", $response[1]);
  78. $response = $response[0];
  79. return Array(true,$response);
  80. }else{
  81. return Array(false,"Command failed: " . $response->faultString());
  82. }
  83. }
  84. $ircret = "";
  85. function ircputs($line){
  86. global $msg;
  87. global $irc;
  88. $msg .= str_replace(get_conf('rehash-pass','string'),'**********',$line);
  89. try{
  90. error_reporting(0);
  91. $r = fputs($irc,$line);
  92. error_reporting(E_ALL);
  93. }catch(Exception $e){
  94. $r = false;
  95. ircclose($e->code,$e->message);
  96. }
  97. return $r;
  98. }
  99. function ircclose($code=0,$message=null){
  100. global $msg;
  101. global $irc;
  102. global $ircret;
  103. try{
  104. error_reporting(0);
  105. $msg .= 'QUIT :'.$message;
  106. fputs($irc,'QUIT :'.$message);
  107. error_reporting(E_ALL);
  108. }catch(Exception $e){}
  109. while(!feof($irc) && $line = fgets($irc,128)){
  110. if(is_string($line)){
  111. $msg .= $line;
  112. }
  113. }
  114. fclose($irc);
  115. $ircret = '{"code":'.$code.',"message":"'.$message.'","log":'.json_encode($msg).'}';
  116. return $ircret;
  117. }
  118. function isval($src,$prop,$val){
  119. return isset($src[$prop]) && $src[$prop] == $val;
  120. }
  121. function ircrehash(){
  122. global $msg;
  123. global $irc;
  124. global $ircret;
  125. global $u;
  126. global $user;
  127. if(!isset($u)){
  128. $u = $user;
  129. }
  130. $msg = '';
  131. if(!$irc = fsockopen(get_conf('irc-server'),get_conf('irc-port'))){return ircclose(1,"Could not connect.");}
  132. stream_set_timeout($irc,1) or ircclose(2,"Could not set timeout.");
  133. while(!feof($irc)&&!$msg = fgets($irc,128)){}
  134. if(!ircputs("NICK RehashServ\r\n")){return $ircret;}
  135. if(!ircputs("USER RehashServ omni.irc.omnimaga.org RehashServ :RehashServ\r\n")){return $ircret;}
  136. while(!feof($irc)){
  137. $line = fgets($irc,128);
  138. if(is_string($line)){
  139. $msg .= $line;
  140. $data = explode(' ',$line);
  141. if(isval($data,1,'433')){
  142. return ircclose(4,"RehashServ is already running.");
  143. }elseif(strrpos($line,'ERROR :Closing Link:') !== false){
  144. return ircclose(3,"IRC Server refused the connection.");
  145. }elseif($data[0] == 'PING'){
  146. if(!ircputs("PONG {$data[1]}")){return $ircret;}
  147. }elseif(isval($data,1,'001')){
  148. break;
  149. }
  150. }
  151. }
  152. if(!ircputs("IDENTIFY ".get_conf('rehash-pass','string')."\r\n")){return $ircret;}
  153. while(!feof($irc)){
  154. $line = fgets($irc,128);
  155. if(is_string($line)){
  156. $msg .= $line;
  157. $data = explode(' ',$line);
  158. if(isval($data,1,'433')){
  159. return ircclose(4,"RehashServ is already running.");
  160. }elseif(strrpos($line,'ERROR :Closing Link:') !== false){
  161. return ircclose(3,"IRC Server refused the connection.");
  162. }elseif(strrpos($line,":You are now identified for") !== false){
  163. break;
  164. }elseif(strrpos($line,'Password incorrect.') !== false){
  165. return ircclose(5,"Failed to authenticate with NickServ");
  166. }
  167. }
  168. }
  169. if(!ircputs("HS ON\r\n")){return $ircret;}
  170. while(!feof($irc)){
  171. $line = fgets($irc,128);
  172. if(is_string($line)){
  173. $msg .= $line;
  174. $data = explode(' ',$line);
  175. if(isval($data,1,'433')){
  176. return ircclose(4,"RehashServ is already running.");
  177. }elseif(strrpos($line,'ERROR :Closing Link:') !== false){
  178. return ircclose(3,"IRC Server refused the connection.");
  179. }elseif(strrpos($line,':Your vhost of') !== false && strrpos($line,'is now activated') !== false){
  180. break;
  181. }elseif(strrpos($line,"Please contact an Operator to get a vhost assigned to this nick") !== false){
  182. return ircclose(6,"vhost not set.");
  183. }
  184. }
  185. }
  186. if(!ircputs("OPER RehashServ ".get_conf('rehash-pass','string')."\r\n")){return $ircret;}
  187. if(!ircputs("REHASH -global\r\n")){return $ircret;}
  188. if(!ircputs("WALLOPS :{$u['nick']} has rehashed the server\r\n")){return $ircret;}
  189. try{
  190. error_reporting(0);
  191. $msg .= 'QUIT :'.$message;
  192. fputs($irc,'QUIT :'.$message);
  193. error_reporting(E_ALL);
  194. }catch(Exception $e){}
  195. while(!feof($irc) && $line = fgets($irc,128)){
  196. if(is_string($line)){
  197. $msg .= $line;
  198. }
  199. }
  200. fclose($irc);
  201. if(strrpos($msg,':*** Notice -- Configuration loaded without any problems ..') === false){
  202. return '{"code":6,"message":"There is an error in the config. See console for output.","log":'.json_encode($msg).'}';
  203. }
  204. return '{"code":0,"message":"Rehashed. View console for output.","log":'.json_encode($msg).'}';
  205. }
  206. ?>