ts3webinterface.class.php 33 KB

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