irc.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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,$response->faultCode());
  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. foreach($params as $param){
  60. $message->addParam(new xmlrpcval($param, "string"));
  61. }
  62. }
  63. $response = $client->send($message);
  64. if(!$response->faultCode()){
  65. $response = explode("<string>", $response->serialize());
  66. $response = explode("</string", $response[1]);
  67. $response = $response[0];
  68. return Array(true,$response,0);
  69. }else{
  70. return Array(false,"Command failed: " . $response->faultString(),$response->faultCode());
  71. }
  72. }
  73. $ircret = "";
  74. function ircputs($line){
  75. global $msg;
  76. global $irc;
  77. $msg .= str_replace(get_conf('rehash-pass','string'),'**********',$line);
  78. try{
  79. error_reporting(0);
  80. $r = fputs($irc,$line);
  81. error_reporting(E_ALL);
  82. }catch(Exception $e){
  83. $r = false;
  84. ircclose($e->code,$e->message);
  85. }
  86. return $r;
  87. }
  88. function ircclose($code=0,$message=null,$ret_type='string'){
  89. global $msg;
  90. global $irc;
  91. global $ircret;
  92. try{
  93. error_reporting(0);
  94. $msg .= 'QUIT :'.$message;
  95. fputs($irc,'QUIT :'.$message);
  96. error_reporting(E_ALL);
  97. }catch(Exception $e){}
  98. while(!feof($irc) && $line = fgets($irc,128)){
  99. if(is_string($line)){
  100. $msg .= $line;
  101. }
  102. }
  103. fclose($irc);
  104. if($ret_type == 'string'){
  105. $ircret = '{"code":'.$code.',"message":"'.$message.'","log":'.json_encode($msg).'}';
  106. }else{
  107. $ircret = Array(
  108. 'code'=>$code,
  109. 'message'=>$message,
  110. 'log'=>$msg
  111. );
  112. }
  113. return $ircret;
  114. }
  115. function isval($src,$prop,$val){
  116. return isset($src[$prop]) && $src[$prop] == $val;
  117. }
  118. function ircrehash(){
  119. global $msg;
  120. global $irc;
  121. global $ircret;
  122. global $u;
  123. global $user;
  124. if(!isset($u)){
  125. $u = $user;
  126. }
  127. $msg = '';
  128. if(!$irc = fsockopen(get_conf('irc-server'),get_conf('irc-port'))){return ircclose(1,__("Could not connect."));}
  129. stream_set_timeout($irc,1) or ircclose(2,__("Could not set timeout."));
  130. while(!feof($irc)&&!$msg = fgets($irc,128)){}
  131. if(!ircputs("NICK RehashServ\r\n")){return $ircret;}
  132. if(!ircputs("USER RehashServ omni.irc.omnimaga.org RehashServ :RehashServ\r\n")){return $ircret;}
  133. while(!feof($irc)){
  134. $line = fgets($irc,128);
  135. if(is_string($line)){
  136. $msg .= $line;
  137. $data = explode(' ',$line);
  138. if(isval($data,1,'433')){
  139. return ircclose(4,__("RehashServ is already running."));
  140. }elseif(strrpos($line,'ERROR :Closing Link:') !== false){
  141. return ircclose(3,__("IRC Server refused the connection."));
  142. }elseif($data[0] == 'PING'){
  143. if(!ircputs("PONG {$data[1]}")){return $ircret;}
  144. }elseif(isval($data,1,'001')){
  145. break;
  146. }
  147. }
  148. }
  149. if(!ircputs("IDENTIFY ".get_conf('rehash-pass','string')."\r\n")){return $ircret;}
  150. while(!feof($irc)){
  151. $line = fgets($irc,128);
  152. if(is_string($line)){
  153. $msg .= $line;
  154. $data = explode(' ',$line);
  155. if(isval($data,1,'433')){
  156. return ircclose(4,__("RehashServ is already running."));
  157. }elseif(strrpos($line,'ERROR :Closing Link:') !== false){
  158. return ircclose(3,__("IRC Server refused the connection."));
  159. }elseif(strrpos($line,":You are now identified for") !== false){
  160. break;
  161. }elseif(strrpos($line,'Password incorrect.') !== false){
  162. return ircclose(5,__("Failed to authenticate with NickServ"));
  163. }
  164. }
  165. }
  166. if(!ircputs("HS ON\r\n")){return $ircret;}
  167. while(!feof($irc)){
  168. $line = fgets($irc,128);
  169. if(is_string($line)){
  170. $msg .= $line;
  171. $data = explode(' ',$line);
  172. if(isval($data,1,'433')){
  173. return ircclose(4,__("RehashServ is already running."));
  174. }elseif(strrpos($line,'ERROR :Closing Link:') !== false){
  175. return ircclose(3,__("IRC Server refused the connection."));
  176. }elseif(strrpos($line,':Your vhost of') !== false && strrpos($line,'is now activated') !== false){
  177. break;
  178. }elseif(strrpos($line,"Please contact an Operator to get a vhost assigned to this nick") !== false){
  179. return ircclose(6,__("vhost not set."));
  180. }
  181. }
  182. }
  183. if(!ircputs("OPER RehashServ ".get_conf('rehash-pass','string')."\r\n")){return $ircret;}
  184. if(!ircputs("REHASH -global\r\n")){return $ircret;}
  185. if(!ircputs("WALLOPS :{$u['nick']} has rehashed the server\r\n")){return $ircret;}
  186. try{
  187. error_reporting(0);
  188. $msg .= 'QUIT :'.$message;
  189. fputs($irc,'QUIT :'.$message);
  190. error_reporting(E_ALL);
  191. }catch(Exception $e){}
  192. while(!feof($irc) && $line = fgets($irc,128)){
  193. if(is_string($line)){
  194. $msg .= $line;
  195. }
  196. }
  197. fclose($irc);
  198. if(strrpos($msg,':*** Notice -- Configuration loaded without any problems ..') === false){
  199. return '{"code":6,"message":"'.__('There is an error in the config. See console for output.').'","log":'.json_encode($msg).'}';
  200. }
  201. return '{"code":0,"message":"'.__('Rehashed. View console for output.').'","log":'.json_encode($msg).'}';
  202. }
  203. function irccommands($commands,$runas="RehashServ"){
  204. global $msg;
  205. global $irc;
  206. global $ircret;
  207. global $u;
  208. global $user;
  209. $ircret = Array(
  210. 'code'=>1
  211. );
  212. if(!isset($u)){
  213. $u = $user;
  214. }
  215. $msg = '';
  216. if(!$irc = fsockopen(get_conf('irc-server'),get_conf('irc-port'))){return ircclose(1,__("Could not connect."),'array');}
  217. stream_set_timeout($irc,1) or ircclose(2,__("Could not set timeout."),'array');
  218. while(!feof($irc)&&!$msg = fgets($irc,128)){}
  219. if($runas == 'RehashServ'){
  220. if(!ircputs("NICK RehashServ\r\n")){return $ircret;}
  221. if(!ircputs("USER RehashServ omni.irc.omnimaga.org RehashServ :RehashServ\r\n")){return $ircret;}
  222. }else{
  223. if(!ircputs("NICK RunServ\r\n")){return $ircret;}
  224. if(!ircputs("USER {$runas} omni.irc.omnimaga.org {$runas} :{$runas}\r\n")){return $ircret;}
  225. }
  226. while(!feof($irc)){
  227. $line = fgets($irc,128);
  228. if(is_string($line)){
  229. $msg .= $line;
  230. $data = explode(' ',$line);
  231. if(isval($data,1,'433')){
  232. return ircclose(4,__("RunServ is already running."),'array');
  233. }elseif(strrpos($line,'ERROR :Closing Link:') !== false){
  234. return ircclose(3,__("IRC Server refused the connection."),'array');
  235. }elseif($data[0] == 'PING'){
  236. if(!ircputs("PONG {$data[1]}")){return $ircret;}
  237. }elseif(isval($data,1,'001')){
  238. break;
  239. }
  240. }
  241. }
  242. if($runas == 'RehashServ'){
  243. if(!ircputs("OPER RehashServ ".get_conf('rehash-pass','string')."\r\n")){return $ircret;}
  244. if(!ircputs("IDENTIFY RehashServ ".get_conf('rehash-pass','string')."\r\n")){return $ircret;}
  245. }else{
  246. if(!ircputs("IDENTIFY {$runas} ".$_SESSION['password']."\r\n")){return $ircret;}
  247. }
  248. while(!feof($irc)){
  249. $line = fgets($irc,128);
  250. if(is_string($line)){
  251. $msg .= $line;
  252. $data = explode(' ',$line);
  253. if(isval($data,1,'433')){
  254. return ircclose(4,__("RunServ is already running."),'array');
  255. }elseif(isval($data,1,'375')){
  256. while(!feof($irc)){
  257. $line = fgets($irc,128);
  258. if(is_string($line)){
  259. $msg .= $line;
  260. $data = explode(' ',$line);
  261. if(isval($data,1,'376')){
  262. break;
  263. }
  264. }
  265. }
  266. }elseif(strrpos($line,'ERROR :Closing Link:') !== false){
  267. return ircclose(3,__("IRC Server refused the connection."),'array');
  268. }elseif(strrpos($line,":You are now identified for") !== false){
  269. break;
  270. }elseif(strrpos($line,'Password incorrect.') !== false){
  271. return ircclose(5,__("Failed to authenticate with NickServ"),'array');
  272. }
  273. }
  274. }
  275. if($runas == 'RehashServ'){
  276. if(!ircputs("HS ON\r\n")){return $ircret;}
  277. while(!feof($irc)){
  278. $line = fgets($irc,128);
  279. if(is_string($line)){
  280. $msg .= $line;
  281. $data = explode(' ',$line);
  282. if(isval($data,1,'433')){
  283. return ircclose(4,__("RunServ is already running."),'array');
  284. }elseif(strrpos($line,'ERROR :Closing Link:') !== false){
  285. return ircclose(3,__("IRC Server refused the connection."),'array');
  286. }elseif(strrpos($line,':Your vhost of') !== false && strrpos($line,'is now activated') !== false){
  287. break;
  288. }elseif(strrpos($line,"Please contact an Operator to get a vhost assigned to this nick") !== false){
  289. return ircclose(6,__("vhost not set."),'array');
  290. }
  291. }
  292. }
  293. }
  294. foreach($commands as $k => $command){
  295. if(!ircputs($command."\r\n")){return $ircret;}
  296. }
  297. try{
  298. error_reporting(0);
  299. $msg .= 'QUIT :'.$message;
  300. fputs($irc,'QUIT :'.$message);
  301. error_reporting(E_ALL);
  302. }catch(Exception $e){}
  303. while(!feof($irc) && $line = fgets($irc,128)){
  304. if(is_string($line)){
  305. $msg .= $line;
  306. }
  307. }
  308. fclose($irc);
  309. return array(
  310. 'code'=>0,
  311. 'log'=>$msg
  312. );
  313. }
  314. ?>