ts3webinterface.class.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. <?php
  2. /**
  3. * ts3webinterface.class.php <br />
  4. * ------------------------- <br />
  5. * begin : Sunday, Dec 24, 2009 <br />
  6. * copyright : (C) 2009-2010 RK Programming <br />
  7. * email : [email protected] <br />
  8. * version : 0.3.6 <br />
  9. * last modified : Tuesday, Dec 29, 2009 <br />
  10. *
  11. * @author RK Programming <[email protected]>
  12. * @copyright Copyright (c) 2009-2010, Robin K.
  13. * @version 0.3.6
  14. TS3webinterface is free software. You can redistribute it and/or modify
  15. it under the terms of the GNU General Public License as published by
  16. the Free Software Foundation, version 1.3.
  17. **/
  18. define('TS3WEBINTERFACE_VERSION', '0.3.6');
  19. require_once('ts3remote.class.php');
  20. function getAssignedServerUsers()
  21. {
  22. global $db;
  23. $ts3vservers = $db->resultQuery("SELECT * FROM OGP_DB_PREFIXts3_homes
  24. WHERE vserver_id='".
  25. TS3WEBINTERFACE_VSERVER_ID."' AND rserver_id=".
  26. $_SESSION['rserver_id'] );
  27. if($ts3vservers != FALSE)
  28. {
  29. $users_assigned = array();
  30. foreach($ts3vservers as $ts3vserver)
  31. {
  32. if($ts3vserver['user_id'] != $_SESSION['user_id'])
  33. $users_assigned[] = $ts3vserver['user_id'];
  34. }
  35. }
  36. $subusers = $db->getUsersSubUsersIds($_SESSION['user_id']);
  37. if(is_array($subusers))
  38. {
  39. $subusers_list = array();
  40. $subusersb = array();
  41. foreach($subusers as $subuser)
  42. {
  43. if(!in_array($subuser,$users_assigned))
  44. $subusers_list[] = $db->getUserById($subuser);
  45. $subusersb[] = $subuser;
  46. }
  47. if(empty($subusers_list))
  48. $subusers_list = FALSE;
  49. }
  50. if(is_array($users_assigned))
  51. {
  52. $subusers_assigned_list = array();
  53. foreach($users_assigned as $user_assigned)
  54. {
  55. if(in_array($user_assigned,$subusersb))
  56. $subusers_assigned_list[] = $db->getUserById($user_assigned);
  57. }
  58. if(empty($subusers_assigned_list))
  59. $subusers_assigned_list = FALSE;
  60. }
  61. return array('users_assigned' => $users_assigned,
  62. 'subusers' => $subusersb,
  63. 'subusers_assigned_list' => $subusers_assigned_list,
  64. 'subusers_list' => $subusers_list);
  65. }
  66. class TS3webinterface
  67. {
  68. private $serverIP;
  69. private $serverPort;
  70. private $server;
  71. private $errorString;
  72. private $session;
  73. private $template;
  74. private $language;
  75. function __construct($ip, $port)
  76. {
  77. global $db;
  78. $isAdmin = $db->isAdmin( $_SESSION['user_id'] );
  79. $this->loadLanguage();
  80. $this->server = new TS3remote($ip, $port);
  81. // CHECK IF CONNECTED
  82. $tmpConnectError = $this->server->getError();
  83. if( !empty($tmpConnectError) )
  84. {
  85. die($this->language['error'].' '.$tmpConnectError[1].': '.$tmpConnectError[2]);
  86. }
  87. $this->server->r_clientupdate(array(array('client_nickname', 'TS3webinterface Client | by Prookie')));
  88. $this->server->setDeEscapeResults(true);
  89. $this->template = null;
  90. $this->serverIP = $ip;
  91. $this->serverPort = $port;
  92. $this->errorString = '';
  93. $this->session = $_SESSION;
  94. if( !isset($this->session['loggedin']) ) $this->session['loggedin'] = false;
  95. if( !isset($this->session['lvserver']) ) $this->session['lvserver'] = 0;
  96. if( !isset($this->session['luser']) ) $this->session['luser'] = '';
  97. if( !isset($this->session['lpwd']) ) $this->session['lpwd'] = '';
  98. if( !isset($this->session['lastaction']) ) $this->session['lastaction'] = 0;
  99. $user = TS3WEBINTERFACE_NAME;
  100. $password = TS3WEBINTERFACE_PWD;
  101. if( !empty($user) && !empty($password) )
  102. {
  103. if( $this->checkLoginData(TS3WEBINTERFACE_NAME, TS3WEBINTERFACE_PWD) )
  104. {
  105. $this->session['loggedin'] = true;
  106. $this->session['luser'] = TS3WEBINTERFACE_NAME;
  107. $this->session['lpwd'] = TS3WEBINTERFACE_PWD;
  108. if ( !$isAdmin )
  109. $this->session['lvserver'] = TS3WEBINTERFACE_VSERVER_ID;
  110. $this->session['lastaction'] = time();
  111. }
  112. }
  113. if( $isAdmin AND isset( $_GET['changevServer'] ) )
  114. {
  115. $this->session['lvserver'] = 0;
  116. }
  117. else if( $isAdmin AND isset( $_POST['vserverSubmit'] ) && !empty( $_POST['vserver'] ) )
  118. {
  119. $this->session['lvserver'] = $_POST['vserver'];
  120. }
  121. if( $this->checkSession() )
  122. {
  123. if( !$this->login() )
  124. {
  125. echo 'TS3webinterface ERROR: '.$this->errorString;
  126. $this->renewSession();
  127. }
  128. else
  129. {
  130. $this->switchAction();
  131. }
  132. }
  133. else
  134. {
  135. $this->initSmarty();
  136. $this->template->assign('title', $this->language['title'].' :: Login');
  137. $this->template->display('header.tpl');
  138. $this->template->display('loginForm.tpl');
  139. $this->template->display('footer.tpl');
  140. }
  141. $this->server->disconnect();
  142. }
  143. function __destruct()
  144. {
  145. $_SESSION = $this->session;
  146. }
  147. private function initSmarty()
  148. {
  149. require_once('smarty/Smarty.class.php');
  150. $this->template = new Smarty();
  151. $this->template->compile_check = true;
  152. $this->template->debugging = false;
  153. $this->template->template_dir = dirname(__FILE__) .'/templates';
  154. $this->template->compile_dir = dirname(__FILE__) .'/templates_c';
  155. if( is_array($this->language) )
  156. {
  157. $this->template->assign('lang', $this->language);
  158. }
  159. else
  160. {
  161. die('TS3webinterface ERROR: no language file available');
  162. }
  163. $this->template->assign_by_ref('webinterface', $this, array('parseTime', 'parseDate', 'convertByteToMB', 'convertByteToKB'));
  164. }
  165. private function loadLanguage($lang=false)
  166. {
  167. $includeStandardFile = 'lang/English/modules/TS3Admin.php';
  168. $includeLangFile = 'lang/'.(($lang != false) ? $lang : TS3WEBINTERFACE_LANG).'/modules/TS3Admin.php';
  169. if( $includeLangFile != $includeStandardFile && file_exists($includeLangFile) )
  170. {
  171. require($includeLangFile);// overwrite standard strings
  172. }
  173. else
  174. {
  175. require($includeStandardFile);
  176. }
  177. $constants = get_defined_constants(true);
  178. $this->language = $constants['user'];
  179. }
  180. public function getError()
  181. {
  182. $tmp = $this->errorString;
  183. $this->errorString = '';
  184. return $tmp;
  185. }
  186. private function renewSession()
  187. {
  188. $this->session['loggedin'] = false;
  189. $this->session['lvserver'] = 0;
  190. $this->session['luser'] = '';
  191. $this->session['lpwd'] = '';
  192. $this->session['lastaction'] = 0;
  193. }
  194. private function checkSession()
  195. {
  196. if( $this->session['loggedin'] === true && $this->session['luser'] != '' &&
  197. $this->session['lpwd'] != '' && (time()-1800 < $this->session['lastaction']) )
  198. {
  199. $this->session['lastaction'] = time();
  200. return true;
  201. }
  202. else if( time()-1800 <= $this->session['lastaction'] )
  203. {
  204. $this->errorString .= '['.$this->language['e_session_timedout'].']';
  205. $this->renewSession();
  206. return false;
  207. }
  208. else
  209. {
  210. $this->renewSession();
  211. return false;
  212. }
  213. }
  214. private function checkLoginData($user, $pwd, $vserver=false)
  215. {
  216. if( !$this->server->isConnected() ) return false;
  217. if( $vserver )
  218. {
  219. if( !$this->server->r_use($vserver) )
  220. {
  221. $this->errorString .= '['.$this->language['e_conn_vserver'].']';
  222. return false;
  223. }
  224. }
  225. if( !$this->server->r_login($user, $pwd) )
  226. {
  227. $this->errorString .= '['.$this->language['e_conn_serverquery'].']';
  228. return false;
  229. }
  230. return true;
  231. }
  232. private function login($vserver=true)
  233. {
  234. if( !$this->server->isConnected() ) return false;
  235. if( $vserver )
  236. {
  237. if( !$this->server->r_use($this->session['lvserver']) )
  238. {
  239. $this->errorString .= '['.$this->language['e_conn_vserver'].']';
  240. return false;
  241. }
  242. }
  243. if( !$this->server->r_login($this->session['luser'], $this->session['lpwd']) )
  244. {
  245. $this->errorString .= '['.$this->language['e_conn_serverquery'].']';
  246. return false;
  247. }
  248. return true;
  249. }
  250. private function switchAction()
  251. {
  252. global $db;
  253. if( isset($_GET['getchannelbackup']) && $this->session['lvserver'] != 0 )
  254. {
  255. if( $channelBackupString = $this->getChannelBackupString() )
  256. {
  257. header('Content-Disposition: attachment; filename="ts3wi_channelbackup_vserver'.$this->session['lvserver'].'.txt"');
  258. header('Content-Type: x-type/subtype');
  259. echo $channelBackupString;
  260. }
  261. $this->server->disconnect();
  262. exit();
  263. }
  264. if( isset($_POST['ajaxRequest']) )
  265. {
  266. $response = '';
  267. switch($_POST['do'])
  268. {
  269. case 'serveredit':
  270. if( !empty($_POST['serverprop']) && isset($_POST['value']) )
  271. {
  272. if( $this->server->r_serveredit(array(array($_POST['serverprop'], $_POST['value']))) )
  273. {
  274. $response = array('OK', $_POST['serverprop'], $_POST['value']);
  275. }
  276. else
  277. {
  278. $error = $this->server->getError();
  279. $response = array('ERROR', $error[2], $this->language['n_server_responded'].' '.$error[3]);
  280. if( $resolvedError = $this->resolveErrorID($error[2]) )
  281. {
  282. $response[2] .= "\n\n".$resolvedError;
  283. }
  284. }
  285. }
  286. else
  287. {
  288. $response = array('ERROR', 2, 'missing arguments');
  289. }
  290. break;
  291. case 'serverupdate':
  292. if( !empty($_POST['serverprop']) )
  293. {
  294. if( $tmp = $this->server->r_serverinfo() )
  295. {
  296. $tmp = $tmp[0];
  297. $response = array('OK');
  298. if( !is_array($_POST['serverprop']) ) $_POST['serverprop'] = array($_POST['serverprop']);
  299. for($i=0; $i<count($_POST['serverprop']); $i++)
  300. {
  301. if( $_POST['serverprop'][$i] == 'virtualserver_uptime' )
  302. {
  303. $tmp[$_POST['serverprop'][$i]] = $this->parseTime($tmp[$_POST['serverprop'][$i]]);
  304. }
  305. else if( $_POST['serverprop'][$i] == 'connection_bytes_sent_total' || $_POST['serverprop'][$i] == 'connection_bytes_received_total' )
  306. {
  307. $tmp[$_POST['serverprop'][$i]] = $this->convertByteToMB($tmp[$_POST['serverprop'][$i]]);
  308. }
  309. else if( $_POST['serverprop'][$i] == 'connection_bandwidth_sent_last_second_total' || $_POST['serverprop'][$i] == 'connection_bandwidth_received_last_second_total'
  310. || $_POST['serverprop'][$i] == 'connection_bandwidth_sent_last_minute_total' || $_POST['serverprop'][$i] == 'connection_bandwidth_received_last_minute_total' )
  311. {
  312. $tmp[$_POST['serverprop'][$i]] = $this->convertByteToKB($tmp[$_POST['serverprop'][$i]]);
  313. }
  314. $response[] = array($_POST['serverprop'][$i], $tmp[$_POST['serverprop'][$i]]);
  315. }
  316. }
  317. else
  318. {
  319. $error = $this->server->getError();
  320. $response = array('ERROR', $error[2], $this->language['n_server_responded'].' '.$error[3]);
  321. }
  322. }
  323. else
  324. {
  325. $response = array('ERROR', 2, 'missing arguments');
  326. }
  327. break;
  328. case 'startserver':
  329. case 'stopserver':
  330. if( !empty($_POST['serverid']) )
  331. {
  332. $actionDone = '';
  333. if( $_POST['do'] == 'startserver' )
  334. {
  335. $tmp = $this->server->r_serverstart($_POST['serverid']);
  336. $actionDone = 'serverstart';
  337. }
  338. else if( $_POST['do'] == 'stopserver' )
  339. {
  340. $tmp = $this->server->r_serverstop($_POST['serverid']);
  341. $actionDone = 'serverstop';
  342. }
  343. if( $tmp )
  344. {
  345. $response = array('OK', $_POST['serverid'], $actionDone);
  346. }
  347. else
  348. {
  349. $error = $this->server->getError();
  350. $response = array('ERROR', $error[2], $this->language['n_server_responded'].' '.$error[3]);
  351. }
  352. }
  353. else
  354. {
  355. $response = array('ERROR', 2, 'missing arguments');
  356. }
  357. break;
  358. case 'deleteserver':
  359. if( !empty($_POST['serverid']) )
  360. {
  361. $this->server->r_serverstop($_POST['serverid']);
  362. if( $this->server->r_serverdelete($_POST['serverid']) )
  363. {
  364. $response = array('OK', $_POST['serverid'], 'serverdelete');
  365. }
  366. else
  367. {
  368. $error = $this->server->getError();
  369. $response = array('ERROR', $error[2], $this->language['n_server_responded'].' '.$error[3]);
  370. }
  371. }
  372. else
  373. {
  374. $response = array('ERROR', 2, 'missing arguments');
  375. }
  376. break;
  377. case 'deletetoken':
  378. if( !empty($_POST['token']) )
  379. {
  380. if( $this->server->r_tokendelete($_POST['token']) )
  381. {
  382. $response = array('OK', $_POST['token']);
  383. }
  384. else
  385. {
  386. $error = $this->server->getError();
  387. $response = array('ERROR', $error[2], $this->language['n_server_responded'].' '.$error[3]);
  388. }
  389. }
  390. else
  391. {
  392. $response = array('ERROR', 2, 'missing arguments');
  393. }
  394. break;
  395. case 'serverviewupdate':
  396. $this->initSmarty();
  397. $this->template->assign('serverTree', $this->getLiveview());
  398. $this->template->display('liveview.tpl');
  399. break;
  400. case 'clientkick':
  401. if( !empty($_POST['clid']) )
  402. {
  403. if( $this->server->r_clientkick($_POST['clid']) )
  404. {
  405. $response = array('OK', $_POST['clid']);
  406. }
  407. else
  408. {
  409. $error = $this->server->getError();
  410. $response = array('ERROR', $error[2], $this->language['n_server_responded'].' '.$error[3]);
  411. }
  412. }
  413. else
  414. {
  415. $response = array('ERROR', 2, 'missing arguments');
  416. }
  417. break;
  418. case 'clientban':
  419. if( isset($_POST['clid']) && isset($_POST['duration']) && isset($_POST['reason']) )
  420. {
  421. if( $this->server->r_banclient($_POST['clid'], $_POST['duration'], $_POST['reason']) )
  422. {
  423. $response = array('OK', $_POST['clid']);
  424. }
  425. else
  426. {
  427. $error = $this->server->getError();
  428. $response = array('ERROR', $error[2], $this->language['n_server_responded'].' '.$error[3]);
  429. }
  430. }
  431. else
  432. {
  433. $response = array('ERROR', 2, 'missing arguments');
  434. }
  435. break;
  436. case 'clientmove':
  437. if( !empty($_POST['clid']) && !empty($_POST['cid']) )
  438. {
  439. if( $this->server->r_clientmove($_POST['clid'], $_POST['cid']) )
  440. {
  441. $response = array('OK', $_POST['clid'], $_POST['cid']);
  442. }
  443. else
  444. {
  445. $error = $this->server->getError();
  446. $response = array('ERROR', $error[2], $this->language['n_server_responded'].' '.$error[3]);
  447. }
  448. }
  449. else
  450. {
  451. $response = array('ERROR', 2, 'missing arguments');
  452. }
  453. break;
  454. case 'clientpoke':
  455. if( !empty($_POST['clid']) && isset($_POST['msg']) )
  456. {
  457. if( $this->server->r_clientpoke($_POST['clid'], $_POST['msg']) )
  458. {
  459. $response = array('OK', $_POST['clid']);
  460. }
  461. else
  462. {
  463. $error = $this->server->getError();
  464. $response = array('ERROR', $error[2], $this->language['n_server_responded'].' '.$error[3]);
  465. }
  466. }
  467. else
  468. {
  469. $response = array('ERROR', 2, 'missing arguments');
  470. }
  471. break;
  472. case 'clientmsg':
  473. if( !empty($_POST['cid']) && !empty($_POST['mode']) && isset($_POST['msg']) )
  474. {
  475. if( $this->server->r_sendtextmessage($_POST['mode'], $_POST['cid'], $_POST['msg']) )
  476. {
  477. $response = array('OK', $_POST['cid'], $_POST['mode']);
  478. }
  479. else
  480. {
  481. $error = $this->server->getError();
  482. $response = array('ERROR', $error[2], $this->language['n_server_responded'].' '.$error[3]);
  483. }
  484. }
  485. else
  486. {
  487. $response = array('ERROR', 2, 'missing arguments');
  488. }
  489. break;
  490. case 'deleteban':
  491. if( !empty($_POST['banid']) )
  492. {
  493. if( $this->server->r_bandel($_POST['banid']) )
  494. {
  495. $response = array('OK', $_POST['banid']);
  496. }
  497. else
  498. {
  499. $error = $this->server->getError();
  500. $response = array('ERROR', $error[2], $this->language['n_server_responded'].' '.$error[3]);
  501. }
  502. }
  503. else
  504. {
  505. $response = array('ERROR', 2, 'missing arguments');
  506. }
  507. break;
  508. case 'addban':
  509. if( isset($_POST['ip']) && isset($_POST['name']) && isset($_POST['uid']) && isset($_POST['reason']) && isset($_POST['duration']) )
  510. {
  511. if( $this->server->r_banadd($_POST['ip'], $_POST['name'], $_POST['uid'], $_POST['duration'], $_POST['reason']) )
  512. {
  513. $response = array('OK');
  514. }
  515. else
  516. {
  517. $error = $this->server->getError();
  518. $response = array('ERROR', $error[2], $this->language['n_server_responded'].' '.$error[3]);
  519. }
  520. }
  521. else
  522. {
  523. $response = array('ERROR', 2, 'missing arguments');
  524. }
  525. break;
  526. case 'banlistupdate':
  527. $this->initSmarty();
  528. if( is_array($this->language) )
  529. $this->template->assign('lang', $this->language);
  530. else
  531. die('TS3webinterface ERROR: no language file available');
  532. $this->template->assign('banList', $this->server->r_banlist());
  533. $this->template->display('banlist.tpl');
  534. break;
  535. default:
  536. $response = array('ERROR', 1, 'unknown action requested');
  537. }
  538. if( !empty($response) )
  539. echo json_encode($response);
  540. }
  541. else
  542. {
  543. // normal
  544. $this->initSmarty();
  545. if( $this->session['lvserver'] == 0 )
  546. {
  547. $this->template->assign('title', $this->language['title'].' :: '.$this->language['login']);
  548. $this->template->display('header.tpl');
  549. $addData = array();
  550. /*if( !isset($_GET['do']) ) $_GET['do'] = '';
  551. switch($_GET['do'])
  552. {
  553. case 'addserver':*/
  554. if( isset($_GET['do']) && $_GET['do'] == 'addserver' )
  555. {
  556. if( isset($_POST['serverAddSubmit']) && !empty($_POST['servername']) && !empty($_POST['serverslots']) )
  557. {
  558. if( $addData = $this->server->r_servercreate($_POST['servername'], array(array('virtualserver_maxclients', $_POST['serverslots']))) )
  559. {
  560. //$addData = $this->server->de_escape($addData[0]);
  561. $addData = $addData[0];
  562. array_unshift($addData, 'OK');
  563. }
  564. else
  565. {
  566. $error = $this->server->getError();
  567. $addData = array('ERROR', $error[2], $this->language['n_server_responded'].$error[3]);
  568. }
  569. }
  570. else
  571. {
  572. $addData = array('ERROR', 0, $this->language['e_fill_out']);
  573. }
  574. }
  575. /*break;
  576. }*/
  577. $vServerList = $this->server->r_serverlist();
  578. if( !isset($vServerList[0]) )
  579. {
  580. $tmp = array($vServerList);
  581. $vServerList = $tmp;
  582. }
  583. $getPublicIp = $db->resultQuery("SELECT display_public_ip FROM OGP_DB_PREFIXremote_servers WHERE remote_server_id=".$_SESSION['rserver_id']);
  584. $display_ip = checkDisplayPublicIP($getPublicIp[0]['display_public_ip'],$this->serverIP);
  585. $this->template->assign('IP', $this->serverIP);
  586. $this->template->assign('display_public_ip', $display_ip);
  587. $this->template->assign('selectvServer', $vServerList);
  588. $this->template->assign('addData', $addData);
  589. $updateAvailable = $this->checkForUpdate();
  590. $this->template->assign('updateAvailable', $updateAvailable);
  591. if( $updateAvailable != "" )
  592. {
  593. $this->template->display('updateAvailable.tpl');
  594. }
  595. $this->template->display('selectvServer.tpl');
  596. $this->template->display('footer.tpl');
  597. }
  598. else
  599. {
  600. if( isset($_GET['liveview']) ) $this->template->assign('liveviewAutoUpdate', true);
  601. else $this->template->assign('liveviewAutoUpdate', false);
  602. $this->template->assign('title', $this->language['title']);
  603. $this->template->display('header.tpl');
  604. $updateAvailable = $this->checkForUpdate();
  605. $this->template->assign('updateAvailable', $updateAvailable);
  606. if( $updateAvailable != "" )
  607. {
  608. $this->template->display('updateAvailable.tpl');
  609. }
  610. $infoBoxData = $this->server->r_serverinfo();
  611. $this->template->assign('data', $infoBoxData[0]);
  612. if( isset($_GET['token']) )
  613. {
  614. $addToken = array();
  615. if( isset($_GET['do']) && $_GET['do'] == 'addtoken' )
  616. {
  617. $tokenid1 = false;
  618. $tokenid2 = false;
  619. if( isset($_POST['tokentype']) )
  620. {
  621. switch((int)$_POST['tokentype'])
  622. {
  623. case 0:
  624. if( !isset($_POST['tokenid1_0']) ) break;
  625. $tokenid1 = $_POST['tokenid1_0'];
  626. $tokenid2 = 0;
  627. break;
  628. case 1:
  629. if( !isset($_POST['tokenid1_1']) || !isset($_POST['tokenid2_1']) ) break;
  630. $tokenid1 = $_POST['tokenid1_1'];
  631. $tokenid2 = $_POST['tokenid2_1'];
  632. break;
  633. }
  634. }
  635. if( $tokenid1 !== false )
  636. {
  637. if( $this->server->r_tokenadd($_POST['tokentype'], $tokenid1, $tokenid2) )
  638. {
  639. $addToken = array('OK');
  640. }
  641. else
  642. {
  643. $error = $this->server->getError();
  644. $addToken = array('ERROR', $error[2], $this->language['n_server_responded'].$error[3]);
  645. }
  646. }
  647. else
  648. {
  649. $addToken = array('ERROR', 0, $this->language['e_fill_out']);
  650. }
  651. }
  652. $this->template->assign('addToken', $addToken);
  653. $this->template->assign('tokenList', $this->server->r_tokenlist());
  654. $serverGroupList = $this->server->r_servergrouplist();
  655. $serverGroupListNames = array();
  656. for($i=0; $i<count($serverGroupList); $i++)
  657. {
  658. $serverGroupListNames[$serverGroupList[$i]['sgid']] = $serverGroupList[$i];
  659. }
  660. $this->template->assign('serverGroupList', $serverGroupList);
  661. $this->template->assign('serverGroupListNames', $serverGroupListNames);
  662. $channelGroupList = $this->server->r_channelgrouplist();
  663. $channelGroupListNames = array();
  664. for($i=0; $i<count($channelGroupList); $i++)
  665. {
  666. $channelGroupListNames[$channelGroupList[$i]['cgid']] = $channelGroupList[$i];
  667. }
  668. $this->template->assign('channelGroupList', $channelGroupList);
  669. $this->template->assign('channelGroupListNames', $channelGroupListNames);
  670. $channelList = $this->server->r_channellist();
  671. $channelListNames = array();
  672. for($i=0; $i<count($channelList); $i++)
  673. {
  674. $channelListNames[$channelList[$i]['cid']] = $channelList[$i];
  675. }
  676. $this->template->assign('channelList', $channelList);
  677. $this->template->assign('channelListNames', $channelListNames);
  678. $this->template->display('vServerToken.tpl');
  679. }
  680. else if( isset($_GET['liveview']) )
  681. {
  682. $insertResult = array();
  683. if( isset($_FILES['backup']) )
  684. {
  685. $insertResult = array('ERROR', 0, $this->language['e_upload_failed']);
  686. $filename = 'modules/TS3Admin/templates_c/channelbackup_'.rand(111,999).rand(111,999).'.txt';
  687. if( $_FILES['backup']['error'] == 0 && move_uploaded_file($_FILES['backup']['tmp_name'], $filename) )
  688. {
  689. if( $this->insertChannelBackup(file_get_contents($filename)) )
  690. {
  691. $insertResult = array('OK');
  692. }
  693. unlink($filename);
  694. }
  695. }
  696. $this->template->assign('insertResult', $insertResult);
  697. $this->template->assign('serverTree', $this->getLiveview($infoBoxData[0])); // serverinfo param for no double query
  698. $this->template->assign('banList', $this->server->r_banlist());
  699. $this->template->display('vServerLiveview.tpl');
  700. }
  701. else
  702. {
  703. global $db;
  704. $is_parent_user = FALSE;
  705. $subusers_installed = $db->isModuleInstalled('subusers');
  706. if( $subusers_installed )
  707. {
  708. $assigned_info = getAssignedServerUsers();
  709. if( isset($_POST['assign_subuser']) and
  710. in_array($_POST['user_id'],$assigned_info['subusers']) )
  711. {
  712. $db->query("INSERT INTO OGP_DB_PREFIXts3_homes
  713. (`rserver_id`, `ip`, `pwd`, `vserver_id`, `user_id`)
  714. VALUES ('".$_SESSION['rserver_id']."', '".
  715. TS3WEBINTERFACE_IP."', '".
  716. TS3WEBINTERFACE_PWD."', '".
  717. TS3WEBINTERFACE_VSERVER_ID."', '".
  718. $_POST['user_id'].
  719. "');");
  720. $assigned_info = getAssignedServerUsers();
  721. }
  722. if( isset($_POST['unassign_subuser']) and
  723. is_array($assigned_info['subusers']) and
  724. in_array($_POST['user_id'],$assigned_info['users_assigned']) and
  725. in_array($_POST['user_id'],$assigned_info['subusers']) )
  726. {
  727. $db->query( "DELETE FROM OGP_DB_PREFIXts3_homes WHERE vserver_id='".
  728. TS3WEBINTERFACE_VSERVER_ID."' AND user_id='".
  729. $_POST['user_id']."' AND rserver_id='".
  730. $_SESSION['rserver_id'].
  731. "';" );
  732. $assigned_info = getAssignedServerUsers();
  733. }
  734. $this->template->assign('subusers_assigned', $assigned_info['subusers_assigned_list']);
  735. $this->template->assign('subusers', $assigned_info['subusers_list']);
  736. $is_parent_user = is_array($assigned_info['subusers']);
  737. }
  738. $this->template->assign('is_parent_user', $is_parent_user);
  739. $this->template->assign('subusers_installed', $subusers_installed);
  740. $getPublicIp = $db->resultQuery("SELECT display_public_ip FROM OGP_DB_PREFIXremote_servers WHERE remote_server_id=".$_SESSION['rserver_id']);
  741. $display_ip = checkDisplayPublicIP($getPublicIp[0]['display_public_ip'],$this->serverIP);
  742. $this->template->assign('display_public_ip', $display_ip);
  743. $this->template->assign('IP', $this->serverIP);
  744. $this->template->display('vServerOverview.tpl');
  745. }
  746. $this->template->display('footer.tpl');
  747. }
  748. }
  749. }
  750. private function getLiveview($server=false)
  751. {
  752. if( !$server )
  753. {
  754. $server = $this->server->r_serverinfo();
  755. if( !$server )
  756. {
  757. $errArray = $this->server->getError();
  758. echo strtoupper($this->language['error']).' #'.$errArray[2].': '.$this->language['n_server_responded'].' \''.$errArray[3].'\'';
  759. return false;
  760. }
  761. $server = $server[0];
  762. }
  763. else
  764. {
  765. if( !empty($server[0]) )
  766. $server = $server[0];
  767. }
  768. $channels = $this->server->r_channellist(true, true, true, true);
  769. if( !$channels )
  770. {
  771. $errArray = $this->server->getError();
  772. echo strtoupper($this->language['error']).' #'.$errArray[2].': '.$this->language['n_server_responded'].' \''.$errArray[3].'\'';
  773. return false;
  774. }
  775. $clients = $this->server->r_clientlist(true, true, true, true, true);
  776. if( !$clients )
  777. {
  778. $errArray = $this->server->getError();
  779. echo strtoupper($this->language['error']).' #'.$errArray[2].': '.$this->language['n_server_responded'].' \''.$errArray[3].'\'';
  780. return false;
  781. }
  782. function sortClients($a, $b)
  783. {
  784. if( strpos(',', $a['client_servergroups']) !== false )
  785. {
  786. $aID = explode(',', $a['client_servergroups'], 2);
  787. $aID = $aID[0];
  788. }
  789. else
  790. {
  791. $aID = $a['client_servergroups'];
  792. }
  793. if( strpos(',', $b['client_servergroups']) !== false )
  794. {
  795. $bID = explode(',', $b['client_servergroups'], 2);
  796. $bID = $bID[0];
  797. }
  798. else
  799. {
  800. $bID = $b['client_servergroups'];
  801. }
  802. if( $aID > $bID ) return 1;
  803. if( $aID < $bID ) return -1;
  804. if( $aID = $bID ) return 0;
  805. }
  806. usort($clients, "sortClients");
  807. $pidStack = array(0);
  808. $lastLevel = array();
  809. $cidConnection = array();
  810. $channelNum = count($channels);
  811. for($i=0; $i<$channelNum; $i++)
  812. {
  813. $cidConnection[$channels[$i]['cid']] = $i;
  814. $channels[$i]['is_last_channel'] = false;
  815. /*if( $channels[$i]['pid'] != $pidStack[count($pidStack)-1] )
  816. {
  817. if( in_array($channels[$i]['pid'], $pidStack) )
  818. {
  819. do
  820. {
  821. array_pop($pidStack);
  822. } while( in_array($channels[$i]['pid'], $pidStack) && count($pidStack) > 1 );
  823. //$pidStack[] = $channels[$i]['pid'];
  824. $channels[$i]['is_last_channel'] = true;
  825. }
  826. else
  827. {
  828. $pidStack[] = $channels[$i]['pid'];
  829. }
  830. }*/
  831. //$channels[$i]['level'] = count($pidStack)-1;
  832. if( $channels[$i]['pid'] == 0 ) $channels[$i]['level'] = 0;
  833. else $channels[$i]['level'] = $channels[$cidConnection[$channels[$i]['pid']]]['level'] + 1;
  834. $channels[$i]['is_server'] = false;
  835. $channels[$i]['clients'] = array();
  836. $lastLevel[$channels[$i]['level']] = $i;
  837. }
  838. /*$reversedCounter = count($channels)-1;
  839. do
  840. {
  841. if( $channels[$reversedCounter]['pid'] == 0 )
  842. {
  843. $channels[$reversedCounter]['is_last_channel'] = true;
  844. break;
  845. }
  846. $reversedCounter--;
  847. } while( $reversedCounter >= 0 );*/
  848. for($i=0; $i<$channelNum; $i++)
  849. {
  850. if( !isset($channels[$i-1]) && !isset($channels[$i+1]) )
  851. {
  852. $channels[$i]['mode'] = 'l';
  853. //continue;
  854. }
  855. else if( isset($channels[$i-1]) && !isset($channels[$i+1]) )
  856. {
  857. $channels[$i]['mode'] = 'l';
  858. //continue;
  859. }
  860. else if( !isset($channels[$i-1]) && isset($channels[$i+1]) )
  861. {
  862. $channels[$i]['mode'] = 't';
  863. //continue;
  864. }
  865. else if( $channels[$i]['level'] == $channels[$i+1]['level'] )
  866. {
  867. $channels[$i]['mode'] = 't';
  868. }
  869. else if( $channels[$i]['level'] > $channels[$i+1]['level'] )
  870. {
  871. $channels[$i]['mode'] = 'l';
  872. }
  873. else
  874. {
  875. if( $lastLevel[$channels[$i]['level']] == $i )
  876. {
  877. $channels[$i]['mode'] = 'l';
  878. }
  879. else
  880. {
  881. $channels[$i]['mode'] = 't';
  882. }
  883. }
  884. //$channels[$i]['TEST'] = $i;
  885. if( $channels[$i]['pid'] != 0 )
  886. {
  887. $before = array();
  888. //$subtractLevels = 0;
  889. //$tmp = $channels[$cidConnection[$channels[$i]['pid']]];
  890. //$tmpPID = $channels[$i]['pid'];
  891. //echo $tmpPID.' ';
  892. $tmp = $channels[$cidConnection[$channels[$i]['pid']]];
  893. //echo $channels[$i]['pid'].'='.$cidConnection[$channels[$i]['pid']].' ';
  894. //var_dump($channels[$cidConnection[$channels[$i]['pid']]]['is_last_channel']);
  895. $tmpPID = $channels[$i]['pid'];
  896. //if( $tmp['mode'] == 'l' ) $subtractLevels++;
  897. if( $tmp['mode'] != 'l' )
  898. array_unshift($before, 'line_i');
  899. else
  900. array_unshift($before, 'spacer');
  901. //for($ii=$channels[$i]['level']; $ii>0; $ii--)
  902. while(true)
  903. {
  904. $tmpPID = $tmp['pid'];
  905. if( /*$tmpPID != 0 &&*/ isset($cidConnection[$tmpPID]) && isset($channels[$cidConnection[$tmpPID]]) )
  906. $tmp = $channels[$cidConnection[$tmpPID]];
  907. else
  908. break;
  909. //echo $tmpPID.' ';
  910. if( $tmp['mode'] != 'l' )
  911. array_unshift($before, 'line_i');
  912. else
  913. array_unshift($before, 'spacer');
  914. //if( $tmp['mode'] != 'l' ) break;
  915. //$subtractLevels++;
  916. }
  917. $channels[$i]['draw_before'] = $before;
  918. //print_r($before);
  919. //$channels[$i]['num_i_lines'] = $channels[$i]['level'] - $subtractLevels;
  920. //$channels[$i]['num_spacers'] = $subtractLevels;
  921. }
  922. }
  923. $clientNum = count($clients);
  924. for($i=0; $i<$clientNum; $i++)
  925. {
  926. if( $clients[$i]['client_input_hardware'] == '0' ) $clients[$i]['status_img'] = '16x16_hardware_input_muted';
  927. else if( $clients[$i]['client_output_muted'] == '1' ) $clients[$i]['status_img'] = '16x16_output_muted';
  928. else if( $clients[$i]['client_input_muted'] == '1' ) $clients[$i]['status_img'] = '16x16_input_muted';
  929. else if( $clients[$i]['client_flag_talking'] == '1') $clients[$i]['status_img'] = '16x16_player_on';
  930. else $clients[$i]['status_img'] = '16x16_player_off';
  931. $channels[$cidConnection[$clients[$i]['cid']]]['clients'][] = $clients[$i];
  932. $channels[$cidConnection[$clients[$i]['cid']]]['clients'][(count($channels[$cidConnection[$clients[$i]['cid']]]['clients']))-1]['level'] = $channels[$cidConnection[$clients[$i]['cid']]]['level'] + 1;
  933. $clients[$i]['level'] = $channels[$cidConnection[$clients[$i]['cid']]]['level'] + 1;
  934. }
  935. array_unshift($channels, array(
  936. 'channel_name' => $server['virtualserver_name'],
  937. 'cid' => $server['virtualserver_id'],
  938. 'is_server' => true
  939. ));
  940. return $channels;
  941. }
  942. private function getChannelBackupString()
  943. {
  944. $this->server->setDeEscapeResults(false);
  945. $result = $this->server->r_channellist(true, true, true, true);
  946. if( !$result )
  947. {
  948. $errArray = $ts->getError();
  949. echo 'ERROR #'.$errArray[2].': the server returned: \''.$errArray[3].'\'';
  950. return false;
  951. }
  952. $channels = array();
  953. $cidConnection = array();
  954. $channelNum = count($result);
  955. for($i=0; $i<$channelNum; $i++ )
  956. {
  957. $cidConnection[$result[$i]['cid']] = $i;
  958. $tmp = $this->server->r_channelinfo($result[$i]['cid']);
  959. if( !$tmp )
  960. {
  961. echo "channelinfo failed\n";
  962. return false;
  963. }
  964. else
  965. {
  966. $result[$i] = array_merge($result[$i], $tmp[0]);
  967. }
  968. if( $result[$i]['pid'] != 0 )
  969. {
  970. $result[$i]['pid'] = $cidConnection[$result[$i]['pid']];
  971. }
  972. if( $result[$i]['channel_order'] != 0 )
  973. {
  974. $result[$i]['channel_order'] = $cidConnection[$result[$i]['channel_order']];
  975. }
  976. unset($result[$i]['cid']);
  977. unset($result[$i]['total_clients']);
  978. unset($result[$i]['channel_filepath']);
  979. }
  980. $this->server->setDeEscapeResults(true);
  981. return json_encode($result);
  982. }
  983. private function insertChannelBackup($backup)
  984. {
  985. if( !$backup = json_decode($backup, true) )
  986. {
  987. echo "json parse error\n";
  988. return false;
  989. }
  990. $cidConnection = array();
  991. $backupNum = count($backup);
  992. for($i=0; $i<$backupNum; $i++)
  993. {
  994. $tmpCreate = array();
  995. $channelPropNum = count($backup[$i]);
  996. foreach($backup[$i] as $key => $value)
  997. {
  998. if( $key == 'pid' || $key == 'channel_order' || $key == 'channel_name' ) continue;
  999. $tmpCreate[] = array($key, $value);
  1000. }
  1001. $tmp = $this->server->r_channelcreate($backup[$i]['channel_name'], $tmpCreate, '', false);
  1002. if( !$tmp )
  1003. {
  1004. echo "channelcreate error\n";
  1005. return false;
  1006. }
  1007. else
  1008. {
  1009. if( $backup[$i]['pid'] != 0 )
  1010. {
  1011. if( $backup[$i]['channel_order'] == 0 ) $channelOrder = 0;
  1012. else $channelOrder = $cidConnection[$backup[$i]['channel_order']];
  1013. if( !$this->server->r_channelmove($backup[$i]['cid'], $cidConnection[$backup[$i]['pid']], $channelOrder) )
  1014. {
  1015. echo "channelmove error, ".$backup[$i]['cid']." under ".$cidConnection[$backup[$i]['pid']]."\n";
  1016. return false;
  1017. }
  1018. }
  1019. }
  1020. }
  1021. return true;
  1022. }
  1023. private function resolveErrorID($id)
  1024. {
  1025. switch($id)
  1026. {
  1027. case 2568:
  1028. return $this->language['e_2568'];
  1029. break;
  1030. default:
  1031. return '';
  1032. }
  1033. }
  1034. public function parseTime($timeSeconds)
  1035. {
  1036. $timeSeconds = round($timeSeconds);
  1037. $days = floor($timeSeconds / 86400);
  1038. $daysForm = ( $days === 1 ) ? $this->language['time_day'] : $this->language['time_days'];
  1039. $hours = floor(($timeSeconds % 86400) / 3600);
  1040. $hoursForm = ( $days === 1 ) ? $this->language['time_hour'] : $this->language['time_hours'];
  1041. $minutes = floor(($timeSeconds % 3600) / 60);
  1042. $minutesForm = ( $days === 1 ) ? $this->language['time_minute'] : $this->language['time_minutes'];
  1043. $seconds = $timeSeconds % 60;
  1044. $secondsForm = ( $days === 1 ) ? $this->language['time_second'] : $this->language['time_seconds'];
  1045. return "$days $daysForm, $hours $hoursForm, $minutes $minutesForm, $seconds $secondsForm";
  1046. }
  1047. public function parseDate($timeSeconds, $add=0, $format='r')
  1048. {
  1049. if( $add != 0 ) $timeSeconds += $add;
  1050. return date($format, $timeSeconds);
  1051. }
  1052. public function convertByteToMB($num, $prec=2)
  1053. {
  1054. return round($num/1024/1024, $prec);
  1055. }
  1056. public function convertByteToKB($num, $prec=2)
  1057. {
  1058. return round($num/1024, $prec);
  1059. }
  1060. private function checkForUpdate()
  1061. {
  1062. return '';
  1063. }
  1064. }
  1065. ?>