1
0

ts3lib.class.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. /**
  3. * ts3lib.class.php <br />
  4. * ---------------- <br />
  5. * begin : Sunday, Dec 21, 2009 <br />
  6. * copyright : (C) 2009-2010 RK Programming <br />
  7. * email : [email protected] <br />
  8. * version : 1.1.0 <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 1.1.0
  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('TS3LIB_VERSION', '1.1.0');
  19. error_reporting( E_ERROR );
  20. set_time_limit(15);
  21. define("TS3_OK", true);
  22. define("TS3_ERROR", false);
  23. define("TS3_PARSED", true);
  24. define("TS3_RAW", false);
  25. define("NL", '
  26. ');
  27. class TS3lib
  28. {
  29. private $socket;
  30. private $errorArray;
  31. private $deEscapeResults;
  32. private $performLog;
  33. function __construct($ip, $port)
  34. {
  35. $this->connected = false;
  36. $this->errorArray = array();
  37. $this->deEscapeResults = true;
  38. $this->performLog = array();
  39. $this->socket = fsockopen($ip, $port, $errno, $error, 5);
  40. stream_set_timeout($this->socket, 3);
  41. if( !is_resource($this->socket) || !$this->socket )
  42. {
  43. echo '<p>TS3lib error: connection failed: '.$errno.' '.$error.'</p>';
  44. /* $this->errorArray = array('error', $errnom, 'connection failed: '.$error); */
  45. }
  46. else
  47. {
  48. if( strpos(fgets($this->socket), 'TS3') === false )
  49. {
  50. echo ' - TS3lib error: no TS3 service running - '."\n";
  51. $this->errorArray = array('error', 0, 'no TS3 service running');
  52. }
  53. /*else
  54. {
  55. echo ' - TS3lib notice: TS3 service running - '."\n";
  56. }*/
  57. }
  58. }
  59. function __destruct()
  60. {
  61. $this->disconnect();
  62. }
  63. public function disconnect()
  64. {
  65. if( !$this->socket ) return;
  66. else if( is_resource($this->socket) )
  67. {
  68. fputs($this->socket, 'quit'."\n");
  69. fclose($this->socket);
  70. $this->performLog[] = 'quit';
  71. }
  72. }
  73. // alias for disconnect()
  74. public function close()
  75. {
  76. $this->disconnect();
  77. }
  78. public function isConnected()
  79. {
  80. if( is_resource($this->socket) )
  81. return true;
  82. else
  83. return false;
  84. }
  85. public function getError()
  86. {
  87. $tmpErrorArray = $this->errorArray; // backup error array
  88. $this->errorArray = array(); // clear error array
  89. return $this->de_escape($tmpErrorArray); // return backup error array
  90. }
  91. public function getLog()
  92. {
  93. return $this->performLog;
  94. }
  95. public function setDeEscapeResults($val)
  96. {
  97. $this->deEscapeResults = (bool)$val;
  98. }
  99. public function getDeEscapeResults()
  100. {
  101. return $this->deEscapeResults;
  102. }
  103. private function readResponse()
  104. {
  105. $result = '';
  106. $tmp = '';
  107. do
  108. {
  109. $tmp = fgets($this->socket);
  110. $result .= $tmp;
  111. } while ( preg_match('/id=[0-9]+? msg=.*/', $tmp, $tmparray) === 0 );
  112. return $result;
  113. }
  114. public function perform($command, $parse=TS3_PARSED)
  115. {
  116. if( !$this->socket ) return false;
  117. fputs($this->socket, $command."\n");
  118. $this->performLog[] = $command;
  119. if( $parse == TS3_PARSED )
  120. {
  121. if( $this->deEscapeResults )
  122. return $this->de_escape($this->parseResult($this->readResponse()));
  123. else
  124. return $this->parseResult($this->readResponse());
  125. }
  126. else
  127. {
  128. return trim($this->readResponse());
  129. }
  130. }
  131. public function performResultless($command)
  132. {
  133. if( !$this->socket ) return false;
  134. fputs($this->socket, $command."\n");
  135. $this->performLog[] = $command;
  136. return $this->checkResult($this->readResponse());
  137. }
  138. public function checkResult($resultString)
  139. {
  140. if( strpos($resultString, 'id=0 msg=ok') === false )
  141. {
  142. $tmparray = array();
  143. preg_match('/id=([0-9]+?) msg=(.*)/', $resultString, $tmparray);
  144. array_unshift($tmparray, 'ERROR');
  145. $this->errorArray = $tmparray;
  146. return false;
  147. }
  148. else
  149. {
  150. return true;
  151. }
  152. }
  153. private function parseResult($resultString)
  154. {
  155. if( $checkResult = $this->checkResult($resultString) )
  156. {
  157. // parse
  158. $result = array();
  159. $resultVars = explode('error id=', $resultString, -1);
  160. $resultVarsSplitted = explode('|', trim($resultVars[0]));
  161. $count = count($resultVarsSplitted);
  162. for($i=0; $i<$count; $i++)
  163. {
  164. $resultVarsSplitted[$i] = explode(' ', $resultVarsSplitted[$i]);
  165. $countSub = count($resultVarsSplitted[$i]);
  166. for($t=0; $t<$countSub; $t++)
  167. {
  168. if( strpos($resultVarsSplitted[$i][$t], '=') === false )
  169. {
  170. $resultVarsSplitted[$i][$t].= '=';
  171. }
  172. $resultVarsSplitted[$i][$t] = explode('=', $resultVarsSplitted[$i][$t], 2);
  173. $result[$i][$resultVarsSplitted[$i][$t][0]] = $resultVarsSplitted[$i][$t][1];
  174. }
  175. }
  176. //if( count($result) == 1 ) $result = $result[0];
  177. return $result;
  178. }
  179. else
  180. {
  181. return $checkResult;
  182. }
  183. }
  184. public function escape($string)
  185. {
  186. $search = array('\\', '/', ' ', '|', "\n");
  187. $replace = array('\\\\', '\\/', '\\s', '\\p', '\\n');
  188. if( is_array($string) )
  189. {
  190. foreach($string as $key => $value )
  191. {
  192. if( is_array($string[$key]) )
  193. {
  194. $string[$key] = $this->escape($string[$key]);
  195. }
  196. else
  197. {
  198. $string = str_replace($search, $replace, $string);
  199. }
  200. }
  201. }
  202. else if( is_string($string) )
  203. {
  204. $string = str_replace($search, $replace, $string);
  205. }
  206. return $string;
  207. }
  208. public function de_escape($string)
  209. {
  210. $search = array('\\n', '\\p', '\\s', '\\/', '\\\\');
  211. $replace = array('\n', '|', ' ', '/', '\\');
  212. if( is_array($string) )
  213. {
  214. foreach($string as $key => $value )
  215. {
  216. if( is_array($string[$key]) )
  217. {
  218. $string[$key] = $this->de_escape($string[$key]);
  219. }
  220. else
  221. {
  222. $string = str_replace($search, $replace, $string);
  223. }
  224. }
  225. }
  226. else if( is_string($string) )
  227. {
  228. $string = str_replace($search, $replace, $string);
  229. }
  230. return $string;
  231. }
  232. }
  233. //print_r($result = $ts3->perform('serverlist', TS3_PARSED));
  234. /*var_dump($result = $ts3->performResultless('use 8'));
  235. var_dump($result = $ts3->performResultless('login client_login_name=admin client_login_password=xxxx'));
  236. print_r($result = $ts3->perform('clientlist', TS3_PARSED));
  237. var_dump($result = $ts3->performResultless('clientmove clid=3 cid=51'));
  238. sleep(1);
  239. var_dump($result = $ts3->performResultless('clientmove clid=3 cid=55'));
  240. sleep(1);
  241. var_dump($result = $ts3->performResultless('clientmove clid=3 cid=56'));
  242. sleep(1);
  243. var_dump($result = $ts3->performResultless('clientmove clid=3 cid=51'));
  244. sleep(1);
  245. var_dump($result = $ts3->performResultless('clientmove clid=3 cid=55'));
  246. sleep(1);
  247. var_dump($result = $ts3->performResultless('clientmove clid=3 cid=56'));*/
  248. //$ts3 = new TS3lib('xxx.xxx.xxx', 10011);
  249. //
  250. //$ts3->disconnect();
  251. /*$param = "Test String fürs escapen|ziemlich X\\X normal/anders
  252. oder nicht?\nder autor denkt nichts\nziemlich sinnlos ;)";
  253. echo $param.'
  254. '.$ts3->escape($param).'
  255. '.$ts3->de_escape($ts3->escape($param));*/
  256. ?>