index.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. header('Content-type: application/json');
  3. header('Access-Control-Allow-Origin: *');
  4. require_once("../../header.php");
  5. if(!isset($_GET['action'])){
  6. $opts = getopt('a:',Array('action:'));
  7. $_GET['action'] = isset($opts['action'])?$opts['action']:(isset($opts['a'])?$opts['a']:'');
  8. }
  9. $u = is_logged_in();
  10. switch($_GET['action']){
  11. case 'test':
  12. //$u or die();
  13. //print_r(atheme_command(get_conf('xmlrpc-server'),get_conf('xmlrpc-port'),get_conf('xmlrpc-path'),USER_IP,$_COOKIE['user'],$_SESSION['password'],'topic','#omnimaga'));
  14. //echo mkpasswd('root');
  15. break;
  16. case 'lang':
  17. echo file_get_contents(DIR.'/lang/'.LOCALE.'/C/LC_MESSAGES/omninet.po');
  18. die();
  19. break;
  20. case 'login':
  21. isset($_GET['username']) && isset($_GET['password']) or die('{"code":2,"message":"'.__('Missing username and/or password').'"}');
  22. isset($_GET['type']) or die('{"code":2,"message":"'.__('Missing user type').'"}');
  23. $r = login($_GET['username'],$_GET['password'],$_GET['type']);
  24. if($r !== true){
  25. die('{"code":2,"message":"'.$r.'"}');
  26. }else{
  27. die('{"code":0}');
  28. }
  29. break;
  30. case 'verify':
  31. isset($_GET['token']) or die('{"code":1,"message":"'.__('No token set').'"}');
  32. $r = verify($_GET['token']);
  33. if($r !== true){
  34. die('{"code":2,"message":"'.$r.'"}');
  35. }
  36. die('{"code":0,"message":"'.$r.'"}');
  37. break;
  38. case 'logout':
  39. logout();
  40. die('{"code":0}');
  41. break;
  42. case 'get-memos':
  43. $u or die('{"code":1,"message":"'.__('You have been logged out').'"}');
  44. $u['type'] = 'user' && isset($_COOKIE['user']) && isset($_SESSION['password']) or die('{"code":0}');
  45. $res = atheme_command(get_conf('xmlrpc-server'),get_conf('xmlrpc-port'),get_conf('xmlrpc-path'),USER_IP,$_COOKIE['user'],$_SESSION['password'],'MemoServ','list');
  46. if($res[0]){
  47. $res = explode('&#10;',$res[1]);
  48. $memos = Array();
  49. foreach($res as $k => $row){
  50. if($k != 0 && $k != 1){
  51. $row = preg_split('/^-\s/',$row);
  52. if(isset($row[1])){
  53. $row = explode(' ',$row[1]);
  54. $memo = atheme_command(get_conf('xmlrpc-server'),get_conf('xmlrpc-port'),get_conf('xmlrpc-path'),USER_IP,$_COOKIE['user'],$_SESSION['password'],'MemoServ','read',Array($row[0]));
  55. $memo = explode('&#10;',$memo[1]);
  56. array_push($memos,Array(
  57. 'id'=>$row[0],
  58. 'from'=>$row[2],
  59. 'date'=>Array(
  60. 'month'=>$row[4],
  61. 'day'=>$row[5],
  62. 'time'=>$row[6],
  63. 'year'=>$row[7]
  64. ),
  65. 'body'=>$memo[2]
  66. ));
  67. }
  68. }
  69. }
  70. die('{"code":0,"memos":'.json_encode($memos).'}');
  71. }else{
  72. die('{"code":1,"message":"'.__('Cannot fetch memos').'"}');
  73. }
  74. break;
  75. case 'get-news':
  76. $u or die('{"code":1,"message":"'.__('You have been logged out').'"}');
  77. $u['type'] = 'user' && isset($_COOKIE['user']) && isset($_SESSION['password']) or die('{"code":0}');
  78. $res = atheme_command(get_conf('xmlrpc-server'),get_conf('xmlrpc-port'),get_conf('xmlrpc-path'),USER_IP,$_COOKIE['user'],$_SESSION['password'],'InfoServ','list');
  79. if($res[0]){
  80. $res = explode('&#10;',$res[1]);
  81. $news = Array();
  82. foreach($res as $k => $row){
  83. if($k != count($res)-1){
  84. array_push($news,Array(
  85. 'id'=>preg_replace('/^(\d)+:.+$/i','\1',$row),
  86. 'title'=>preg_replace('/^\d+: \[(.+)\] .+/i','\1',$row),
  87. 'from'=>preg_replace('/^\d+: \[.+\] by (.+) at \d\d?:\d\d? on (\d\d)\/\d\d\/\d\d\d\d: .+/i','\1',$row),
  88. 'date'=>Array(
  89. 'time'=>preg_replace('/^\d+: \[.+\] by .+ at (\d\d?:\d\d?) on .+/','\1',$row),
  90. 'day'=>preg_replace('/^\d+: \[.+\] by .+ at \d\d?:\d\d? on (\d\d)\/\d\d\/\d\d\d\d: .+/i','\1',$row),
  91. 'month'=>preg_replace('/^\d+: \[.+\] by .+ at \d\d?:\d\d? on \d\d\/(\d\d)\/\d\d\d\d: .+/i','\1',$row),
  92. 'year'=>preg_replace('/^\d+: \[.+\] by .+ at \d\d?:\d\d? on \d\d\/\d\d\/(\d\d\d\d): .+/i','\1',$row)
  93. ),
  94. 'body'=>preg_replace('/^\d+: \[.+\] by .+ at \d\d?:\d\d? on \d\d\/\d\d\/\d\d\d\d: (.+)/i','\1',$row)
  95. ));
  96. }
  97. }
  98. die('{"code":0,"news":'.json_encode($news).'}');
  99. }else{
  100. die('{"code":1,"message":"'.__('Cannot fetch news').'"}');
  101. }
  102. break;
  103. case 'get-channels':
  104. $u or die('{"code":1,"message":"'.__('You have been logged out').'"}');
  105. $u['type'] = 'user' && isset($_COOKIE['user']) && isset($_SESSION['password']) or die('{"code":0}');
  106. $res = atheme_command(get_conf('xmlrpc-server'),get_conf('xmlrpc-port'),get_conf('xmlrpc-path'),USER_IP,$_COOKIE['user'],$_SESSION['password'],'NickServ','listchans');
  107. if($res[0]){
  108. $res = explode('&#10;',$res[1]);
  109. $channels = Array();
  110. foreach($res as $k => $row){
  111. if($k != count($res)-1){
  112. $flags_list = str_split(preg_replace('/^Access flag\(s\) \+(.+) in .+$/i','\1',$row));
  113. $flags = array();
  114. foreach($flags_list as $kk => $flag){
  115. switch($flag){
  116. case 'v':$name=__('Voice');break;
  117. case 'V':$name=__('Automatic voice');break;
  118. case 'h':$name=__('Halfop');break;
  119. case 'H':$name=__('Automatic Halfop');break;
  120. case 'o':$name=__('Op');break;
  121. case 'O':$name=__('Automatic Op');break;
  122. case 'a':$name=__('Admin');break;
  123. case 'q':$name=__('Owner');break;
  124. case 's':$name=__('Set');break;
  125. case 'i':$name=__('Invite/Getkey');break;
  126. case 'r':$name=__('Kick/Ban');break;
  127. case 'R':$name=__('Recover/Clear');break;
  128. case 'f':$name=__('Modify access lists');break;
  129. case 't':$name=__('Topic');break;
  130. case 'A':$name=__('View access lists');break;
  131. case 'F':$name=__('Founder');break;
  132. case 'b':$name=__('Banned');break;
  133. default:$name=$flag;
  134. }
  135. array_push($flags,array(
  136. 'flag'=>$flag,
  137. 'name'=>$name
  138. ));
  139. }
  140. $name = preg_replace('/^Access flag\(s\) \+.+ in (.+)$/i','\1',$row);
  141. $chan = array(
  142. 'name'=>$name,
  143. 'flags'=>$flags
  144. );
  145. if(in_array('F',$flags_list)){
  146. $chan['candrop'] = true;
  147. }
  148. array_push($channels,$chan);
  149. }
  150. }
  151. die('{"code":0,"channels":'.json_encode($channels).'}');
  152. }else{
  153. die('{"code":1,"message":"'.__('Cannot fetch channels').'"}');
  154. }
  155. break;
  156. case 'send-memo':
  157. $u or die('{"code":1,"message":"'.__('You have been logged out').'"}');
  158. isset($_GET['to']) && isset($_GET['message']) or die('{"code":1,"message":"'.__('No message or user entered').'"}');
  159. $res = atheme_command(get_conf('xmlrpc-server'),get_conf('xmlrpc-port'),get_conf('xmlrpc-path'),USER_IP,$_COOKIE['user'],$_SESSION['password'],'MemoServ','send',Array($_GET['to'],$_GET['message']));
  160. if($res[0]){
  161. if(substr($res[1],-19) == ' is not registered.'){
  162. die('{"code":1,"message":"'.__('User').' '.$_GET['to'].' '.__('does not exist').'"}');
  163. }else{
  164. die('{"code":0,"message":"'.__('Memo Sent').'"}');
  165. }
  166. }else{
  167. die('{"code":1,"message":"'.__('Cannot send memo').': '.$res[1].'"}');
  168. }
  169. break;
  170. case 'delete-memo':
  171. $u or die('{"code":1,"message":"'.__('You have been logged out').'"}');
  172. isset($_GET['id']) or die('{"code":1,"message":"'.__('No id given').'"}');
  173. $res = atheme_command(get_conf('xmlrpc-server'),get_conf('xmlrpc-port'),get_conf('xmlrpc-path'),USER_IP,$_COOKIE['user'],$_SESSION['password'],'MemoServ','delete',Array($_GET['id']));
  174. if(!$res[0]){
  175. die('{"code":1,"message":"'.__('Cannot delete memo').': '+$res[1]+'"}');
  176. }
  177. die('{"code":0}');
  178. break;
  179. case 'delete-channel':
  180. $u or die('{"code":1,"message":"'.__('You have been logged out').'"}');
  181. isset($_GET['channel']) or die('{"code":1,"message":"'.__('No channel given').'"}');
  182. $res = atheme_command(get_conf('xmlrpc-server'),get_conf('xmlrpc-port'),get_conf('xmlrpc-path'),USER_IP,$_COOKIE['user'],$_SESSION['password'],'ChanServ','drop',Array($_GET['channel']));
  183. if(!$res[0]){
  184. die('{"code":1,"message":"'.__('Cannot drop channel').': '+$res[1]+'"}');
  185. }
  186. die('{"code":0}');
  187. break;
  188. case 'register-channel':
  189. $u or die('{"code":1,"message":"'.__('You have been logged out').'"}');
  190. isset($_GET['channel']) or die('{"code":1,"message":"'.__('No channel given').'"}');
  191. $ret = irccommands(array(
  192. 'join '.$_GET['channel'],
  193. 'samode '.$_GET['channel'].' +o RehashServ',
  194. 'cs register '.$_GET['channel'],
  195. 'cs set '.$_GET['channel'].' keeptopic on',
  196. 'cs set '.$_GET['channel'].' founder '.$_COOKIE['user']
  197. ));
  198. if($ret['code'] !== 0){
  199. die(json_encode($ret));
  200. }
  201. $ret2 = irccommands(array(
  202. 'join '.$_GET['channel'],
  203. 'cs set '.$_GET['channel'].' founder '.$_COOKIE['user']
  204. ),$_COOKIE['user']);
  205. if($ret2['code'] !== 0){
  206. $ret2['message'] = 'Failed to register channel. See log for information.';
  207. }
  208. $ret2['log'] = $ret['log']."\r\n".$ret2['log'];
  209. die(json_encode($ret));
  210. break;
  211. case 'persona-login':
  212. if($u){
  213. $register = true;
  214. }else{
  215. $register = false;
  216. }
  217. $url = get_conf('persona-endpoint');
  218. $assert = filter_input(
  219. INPUT_POST,
  220. 'assertion',
  221. FILTER_UNSAFE_RAW,
  222. FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH
  223. );
  224. $params = 'assertion='.urlencode($assert).'&audience='.urlencode(get_conf('persona-audience'));
  225. $ch = curl_init();
  226. $options = array(
  227. CURLOPT_URL => $url,
  228. CURLOPT_RETURNTRANSFER => TRUE,
  229. CURLOPT_POST => 2,
  230. CURLOPT_SSL_VERIFYPEER => 0,
  231. CURLOPT_SSL_VERIFYHOST => 2,
  232. CURLOPT_POSTFIELDS => $params
  233. );
  234. curl_setopt_array($ch, $options);
  235. $result = curl_exec($ch);
  236. curl_close($ch);
  237. $result = json_decode($result);
  238. if($result->status == 'okay'){
  239. if($register && !add_email($u['id'],$result->email)){
  240. die('{"code":1,"message":"'.__('Failed to add email').' '.$result->email.' '.__('to user').' '.$u['nick'].'"}');
  241. }elseif(!$register && !$u = get_user_for_email($result->email)){
  242. die('{"code":1,"message":"'.__('Email does not match any users').'"}');
  243. }
  244. setcookie('personaUser',$result->email,null,'/');
  245. $pass = null;
  246. if(isset($_SESSION['password']) && !is_null($_SESSION['password']) && $_SESSION['password'] != ''){
  247. $pass = $_SESSION['password'];
  248. }
  249. $types = get_user_types($u['id']);
  250. $r = login($u['nick'],$pass,'persona',$types[0]);
  251. if($r !== true){
  252. if($r){
  253. die('{"code":2,"message":"'.$r.'"}');
  254. }else{
  255. die('{"code":2}');
  256. }
  257. }else{
  258. die('{"code":0,"assertion":'.json_encode($result).'}');
  259. }
  260. }else{
  261. die('{"code":1,"message":"'.$result->reason.'"}');
  262. }
  263. break;
  264. case 'persona-remove':
  265. $u or die('{"code":1,"message":"'.__('You have been logged out').'"}');
  266. isset($_GET['id']) or die('{"code":1,"message":"'.__('No ID set').'"}');
  267. if(!remove_email($u['id'],$_GET['id'],true)){
  268. die('{"code":1,"message":"'.__('Could not remove email address').'"}');
  269. }
  270. die('{"code":0}');
  271. break;
  272. case '2-factor-register':
  273. $r = register_token();
  274. if($r !== true){
  275. die('{"code":1,"message":"'.$r.'"}');
  276. }
  277. die('{"code":0}');
  278. break;
  279. case '2-factor-delete':
  280. $u or die('{"code":1,"message":"'.__('You have been logged out').'"}');
  281. $r = delete_token($u['id']);
  282. if($r !== true){
  283. die('{"code":1,"message":"'.$r.'"}');
  284. }
  285. die('{"code":0,"message":"'.__('2-factor disabled.').'"}');
  286. break;
  287. case 'ping':
  288. $u or die('{"code":1,"message":"'.__('You have been logged out').'"}');
  289. die('{"code":0}');
  290. break;
  291. case 'newpass':
  292. $u && isset($_GET['password']) && isset($_GET['newpass']) or die('{"code":2,"message":"'.__('Make sure that everything is filled in. Try reloading if it is.').'"}');
  293. $u['password'] == mkpasswd($_GET['password'],$u['salt']) or die('{"code":2,"message":"'.__('Invalid password').'"}');
  294. $u['api_key'] == $_COOKIE['key'] or die('{"code":3,"message":"Not Logged in to use '.$u['nick'].' with key '.$u['api_key'].' != '.$_COOKIE['key'].'."}');
  295. if($_COOKIE['type'] == 'user'){
  296. $res = atheme_command(get_conf('xmlrpc-server'),get_conf('xmlrpc-port'),get_conf('xmlrpc-path'),USER_IP,$u['nick'],$_GET['password'],'NickServ','set',Array('password',trim($_GET['newpass'])));
  297. if($res[0] === false){
  298. die('{"code":2,"message":"'.__('Could not update password with nickserv').': '.$res[1].'"}');
  299. }else{
  300. $_SESSION['password'] = $_GET['newpass'];
  301. }
  302. }
  303. query("UPDATE users u SET u.password='%s' WHERE u.id=%d",Array(mkpasswd($_GET['newpass']),$u['id']));
  304. die('{"code":0}');
  305. break;
  306. case 'sync-pass':
  307. $u && isset($_SESSION['password'])or die('{"code":2,"message":"'.__('Make sure that everything is filled in. Try reloading if it is.').'"}');
  308. $u['api_key'] == $_COOKIE['key'] or die('{"code":3,"message":"'.__('Not Logged in to use').' '.$u['nick'].' '.__('with key').' '.$u['api_key'].' != '.$_COOKIE['key'].'."}');
  309. $_COOKIE['type'] == 'user' or die('{"code":3,"message":"'.__('Must be logged in with type user to sync pass').'"}');
  310. $res = atheme_login(get_conf('xmlrpc-server'),get_conf('xmlrpc-port'),get_conf('xmlrpc-path'),$u['nick'],$_SESSION['password']);
  311. if($res[0] === false){
  312. die('{"code":2,"message":"'.__('Could not verify with nickserv').': '.$res[1].'"}');
  313. }
  314. query("UPDATE users u SET u.password='%s' WHERE u.id=%d",Array(mkpasswd($_SESSION['password']),$u['id']));
  315. die('{"code":0,"message":"'.__('Nickserv password synchronized with main account').'"}');
  316. break;
  317. case 'role':
  318. $u && isset($_GET['type']) or die('{"code":2,"message":"'.__('Make sure that everything is filled in. Try reloading if it is.').'"}');
  319. setcookie('type',$_GET['type'],null,'/');
  320. die('{"code":0}');
  321. break;
  322. case 'user':
  323. $u or die('{"code":10,"message":"'.__('Not logged in').'"}');
  324. isset($_GET['id']) or die('{"code":2,"message":"'.__('No user set.').'"}');
  325. isset($_GET['email']) or die('{"code":2,"message":"'.__('No email set.').'"}');
  326. isset($_GET['real_name']) or die('{"code":2,"message":"'.__('No real name set.').'"}');
  327. isset($_GET['nick']) or die('{"code":2,"message":"'.__('No nick set.').'"}');
  328. $user = get_user_from_id_obj($_GET['id']) or die('{"code":2,"message":"'.__('User with id').' '.$_GET['id'].' '.__('does not exist. You should reload the page.').'"}');
  329. if($u['id'] == $user['id']){
  330. setcookie('user',$_GET['nick'],null,'/');
  331. }
  332. query("UPDATE users u SET u.nick='%s', u.real_name='%s', u.email='%s' WHERE u.id=%d",Array($_GET['nick'],$_GET['real_name'],$_GET['email'],$_GET['id'])) or die('{"code":2,"message":"'.__('Unable to update user').'"}');
  333. die(ircrehash());
  334. break;
  335. case 'oper':
  336. $u or die('{"code":10,"message":"'.__('Not logged in').'"}');
  337. isset($_GET['id']) or die('{"code":2,"message":"'.__('No user set.').'"}');
  338. isset($_GET['nick']) or die('{"code":2,"message":"'.__('No nick set.').'"}');
  339. isset($_GET['swhois']) or die('{"code":2,"message":"'.__('No profile set.').'"}');
  340. $oper = get_oper_from_id_obj($_GET['id']) or die('{"code":2,"message":"'.__('Oper with id').' '.$_GET['id'].' '.__('does not exist. You should reload the page.').'"}');
  341. if(isset($_GET['password']) && $_GET['password'] != ""){
  342. query("UPDATE opers o SET o.nick='%s', o.swhois='%s', o.password='%s', o.password_type_id=2 WHERE o.id=%d",Array($_GET['nick'],$_GET['swhois'],mkpasswd($_GET['password']),$_GET['id'])) or die('{"code":2,"message":"'.__('Unable to update oper').'"}');
  343. }else{
  344. query("UPDATE opers o SET o.nick='%s', o.swhois='%s' WHERE o.id=%d",Array($_GET['nick'],$_GET['swhois'],$_GET['id'])) or die('{"code":2,"message":"'.__('Unable to update oper').'"}');
  345. }
  346. die(ircrehash());
  347. break;
  348. case 'config':
  349. foreach($_GET as $key => $val){
  350. set_conf($key,$val,get_conf_type($key)) or die('{"code":1,"message":"'.__('Failed to update setting').': '.$key.' '.__('with value').': '.$val.'"}');
  351. }
  352. die('{"code":0}');
  353. break;
  354. case 'rehash':
  355. $u or die('{"code":10,"message":"'.__('Not logged in').'"}');
  356. die(ircrehash());
  357. break;
  358. default:
  359. die('{"code":1,"message":"'.__('Invalid Action').': '.$_GET['action'].'"}');
  360. }
  361. ?>