Json.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. /**
  3. * @file
  4. * TeamSpeak 3 PHP Framework
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * @package TeamSpeak3
  20. * @author Sven 'ScP' Paulsen
  21. * @copyright Copyright (c) Planet TeamSpeak. All rights reserved.
  22. */
  23. /**
  24. * @class TeamSpeak3_Viewer_Json
  25. * @brief Generates a JSON struct used in JS-based TeamSpeak 3 viewers.
  26. */
  27. class TeamSpeak3_Viewer_Json implements TeamSpeak3_Viewer_Interface
  28. {
  29. /**
  30. * Stores an array of data parsed from TeamSpeak3_Node_Abstract objects.
  31. *
  32. * @var array
  33. */
  34. protected $data = null;
  35. /**
  36. * The TeamSpeak3_Node_Abstract object which is currently processed.
  37. *
  38. * @var TeamSpeak3_Node_Abstract
  39. */
  40. protected $currObj = null;
  41. /**
  42. * An array filled with siblings for the TeamSpeak3_Node_Abstract object which is currently
  43. * processed.
  44. *
  45. * @var array
  46. */
  47. protected $currSib = null;
  48. /**
  49. * An internal counter indicating the depth of the TeamSpeak3_Node_Abstract object previously
  50. * processed.
  51. *
  52. * @var integer
  53. */
  54. protected $lastLvl = 0;
  55. /**
  56. * The TeamSpeak3_Viewer_Json constructor.
  57. *
  58. * @param array $data
  59. * @return TeamSpeak3_Viewer_Json
  60. */
  61. public function __construct(array &$data = array())
  62. {
  63. $this->data = &$data;
  64. }
  65. /**
  66. * Assembles an stdClass object for the current element.
  67. *
  68. * @param TeamSpeak3_Node_Abstract $node
  69. * @param array $siblings
  70. * @return void
  71. */
  72. public function fetchObject(TeamSpeak3_Node_Abstract $node, array $siblings = array())
  73. {
  74. $this->currObj = $node;
  75. $this->currSib = $siblings;
  76. $obj = new stdClass();
  77. $obj->ident = $this->getId();
  78. $obj->parent = $this->getParent();
  79. $obj->children = $node->count();
  80. $obj->level = $this->getLevel();
  81. $obj->first = (bool) ($obj->level != $this->lastLvl);
  82. $obj->last = (bool) array_pop($siblings);
  83. $obj->siblings = array_map("boolval", $siblings);
  84. $obj->class = $this->getType();
  85. $obj->name = $this->getName();
  86. $obj->image = $this->getImage();
  87. $obj->props = $this->getProps();
  88. $this->data[] = $obj;
  89. $this->lastLvl = $obj->level;
  90. }
  91. /**
  92. * Returns the ID of the current element.
  93. *
  94. * @return mixed
  95. */
  96. protected function getId()
  97. {
  98. if($this->currObj instanceof TeamSpeak3_Node_Server)
  99. {
  100. return "ts3_s" . $this->currObj->virtualserver_id;
  101. }
  102. elseif($this->currObj instanceof TeamSpeak3_Node_Channel)
  103. {
  104. return "ts3_c" . $this->currObj->cid;
  105. }
  106. elseif($this->currObj instanceof TeamSpeak3_Node_Client)
  107. {
  108. return "ts3_u" . $this->currObj->clid;
  109. }
  110. return FALSE;
  111. }
  112. /**
  113. * Returns the parent ID of the current element.
  114. *
  115. * @return mixed
  116. */
  117. protected function getParent()
  118. {
  119. if($this->currObj instanceof TeamSpeak3_Node_Channel)
  120. {
  121. return $this->currObj->pid ? "ts3_c" . $this->currObj->pid : "ts3_s" . $this->currObj->getParent()->getId();
  122. }
  123. elseif($this->currObj instanceof TeamSpeak3_Node_Client)
  124. {
  125. return $this->currObj->cid ? "ts3_c" . $this->currObj->cid : "ts3_s" . $this->currObj->getParent()->getId();
  126. }
  127. return "ts3";
  128. }
  129. /**
  130. * Returns the level of the current element.
  131. *
  132. * @return integer
  133. */
  134. protected function getLevel()
  135. {
  136. if($this->currObj instanceof TeamSpeak3_Node_Channel)
  137. {
  138. return $this->currObj->getLevel()+2;
  139. }
  140. elseif($this->currObj instanceof TeamSpeak3_Node_Client)
  141. {
  142. return $this->currObj->channelGetById($this->currObj->cid)->getLevel()+3;
  143. }
  144. return 1;
  145. }
  146. /**
  147. * Returns a single type identifier for the current element.
  148. *
  149. * @return string
  150. */
  151. protected function getType()
  152. {
  153. if($this->currObj instanceof TeamSpeak3_Node_Server)
  154. {
  155. return "server";
  156. }
  157. elseif($this->currObj instanceof TeamSpeak3_Node_Channel)
  158. {
  159. return "channel";
  160. }
  161. elseif($this->currObj instanceof TeamSpeak3_Node_Client)
  162. {
  163. return "client";
  164. }
  165. elseif($this->currObj instanceof TeamSpeak3_Node_Servergroup || $this->currObj instanceof TeamSpeak3_Node_Channelgroup)
  166. {
  167. return "group";
  168. }
  169. return "host";
  170. }
  171. /**
  172. * Returns a string for the current corpus element which can be used as a HTML class
  173. * property. If the current node is a channel spacer the class string will contain
  174. * additional class names to allow further customization of the content via CSS.
  175. *
  176. * @return string
  177. */
  178. protected function getClass()
  179. {
  180. $extras = "";
  181. if($this->currObj instanceof TeamSpeak3_Node_Channel && $this->currObj->isSpacer())
  182. {
  183. switch($this->currObj->spacerGetType())
  184. {
  185. case (string) TeamSpeak3::SPACER_SOLIDLINE:
  186. $extras .= " solidline";
  187. break;
  188. case (string) TeamSpeak3::SPACER_DASHLINE:
  189. $extras .= " dashline";
  190. break;
  191. case (string) TeamSpeak3::SPACER_DASHDOTLINE:
  192. $extras .= " dashdotline";
  193. break;
  194. case (string) TeamSpeak3::SPACER_DASHDOTDOTLINE:
  195. $extras .= " dashdotdotline";
  196. break;
  197. case (string) TeamSpeak3::SPACER_DOTLINE:
  198. $extras .= " dotline";
  199. break;
  200. }
  201. switch($this->currObj->spacerGetAlign())
  202. {
  203. case TeamSpeak3::SPACER_ALIGN_REPEAT:
  204. $extras .= " repeat";
  205. break;
  206. case TeamSpeak3::SPACER_ALIGN_CENTER:
  207. $extras .= " center";
  208. break;
  209. case TeamSpeak3::SPACER_ALIGN_RIGHT:
  210. $extras .= " right";
  211. break;
  212. case TeamSpeak3::SPACER_ALIGN_LEFT:
  213. $extras .= " left";
  214. break;
  215. }
  216. }
  217. return $this->currObj->getClass(null) . $extras;
  218. }
  219. /**
  220. * Returns an individual type for a spacer.
  221. *
  222. * @return string
  223. */
  224. protected function getSpacerType()
  225. {
  226. $type = "";
  227. if(!$this->currObj instanceof TeamSpeak3_Node_Channel || !$this->currObj->isSpacer())
  228. {
  229. return "none";
  230. }
  231. switch($this->currObj->spacerGetType())
  232. {
  233. case (string) TeamSpeak3::SPACER_SOLIDLINE:
  234. $type .= "solidline";
  235. break;
  236. case (string) TeamSpeak3::SPACER_DASHLINE:
  237. $type .= "dashline";
  238. break;
  239. case (string) TeamSpeak3::SPACER_DASHDOTLINE:
  240. $type .= "dashdotline";
  241. break;
  242. case (string) TeamSpeak3::SPACER_DASHDOTDOTLINE:
  243. $type .= "dashdotdotline";
  244. break;
  245. case (string) TeamSpeak3::SPACER_DOTLINE:
  246. $type .= "dotline";
  247. break;
  248. default:
  249. $type .= "custom";
  250. }
  251. if($type == "custom")
  252. {
  253. switch($this->currObj->spacerGetAlign())
  254. {
  255. case TeamSpeak3::SPACER_ALIGN_REPEAT:
  256. $type .= "repeat";
  257. break;
  258. case TeamSpeak3::SPACER_ALIGN_CENTER:
  259. $type .= "center";
  260. break;
  261. case TeamSpeak3::SPACER_ALIGN_RIGHT:
  262. $type .= "right";
  263. break;
  264. default:
  265. $type .= "left";
  266. }
  267. }
  268. return $type;
  269. }
  270. /**
  271. * Returns a string for the current corpus element which contains the display name
  272. * for the current TeamSpeak_Node_Abstract object.
  273. *
  274. * @return string
  275. */
  276. protected function getName()
  277. {
  278. if($this->currObj instanceof TeamSpeak3_Node_Channel && $this->currObj->isSpacer())
  279. {
  280. return $this->currObj["channel_name"]->section("]", 1, 99)->toString();
  281. }
  282. elseif($this->currObj instanceof TeamSpeak3_Node_Client)
  283. {
  284. $before = array();
  285. $behind = array();
  286. foreach($this->currObj->memberOf() as $group)
  287. {
  288. if($group->getProperty("namemode") == TeamSpeak3::GROUP_NAMEMODE_BEFORE)
  289. {
  290. $before[] = "[" . $group["name"] . "]";
  291. }
  292. elseif($group->getProperty("namemode") == TeamSpeak3::GROUP_NAMEMODE_BEHIND)
  293. {
  294. $behind[] = "[" . $group["name"] . "]";
  295. }
  296. }
  297. return trim(implode("", $before) . " " . $this->currObj . " " . implode("", $behind));
  298. }
  299. return $this->currObj->toString();
  300. }
  301. /**
  302. * Returns the parent ID of the current element.
  303. *
  304. * @return stdClass
  305. */
  306. protected function getProps()
  307. {
  308. $props = new stdClass();
  309. if($this->currObj instanceof TeamSpeak3_Node_Host)
  310. {
  311. $this->id = 0;
  312. $this->icon = 0;
  313. $props->version = $this->currObj->version("version")->toString();
  314. $props->platform = $this->currObj->version("platform")->toString();
  315. $props->users = $this->currObj->virtualservers_total_clients_online;
  316. $props->slots = $this->currObj->virtualservers_total_maxclients;
  317. $props->flags = 0;
  318. }
  319. elseif($this->currObj instanceof TeamSpeak3_Node_Server)
  320. {
  321. $props->id = $this->currObj->getId();
  322. $props->icon = $this->currObj->virtualserver_icon_id < 0 ? pow(2, 32)-($this->currObj->virtualserver_icon_id*-1) : $this->currObj->virtualserver_icon_id;
  323. $props->welcmsg = strlen($this->currObj->virtualserver_welcomemessage) ? trim($this->currObj->virtualserver_welcomemessage) : null;
  324. $props->hostmsg = strlen($this->currObj->virtualserver_hostmessage) ? trim($this->currObj->virtualserver_hostmessage) : null;
  325. $props->version = TeamSpeak3_Helper_Convert::versionShort($this->currObj->virtualserver_version)->toString();
  326. $props->platform = $this->currObj->virtualserver_platform->toString();
  327. $props->country = null;
  328. $props->users = $this->currObj->clientCount();
  329. $props->slots = $this->currObj->virtualserver_maxclients;
  330. $props->flags = 0;
  331. $props->flags += $this->currObj->virtualserver_status == "online" ? 1 : 0;
  332. $props->flags += $this->currObj->virtualserver_flag_password ? 2 : 0;
  333. $props->flags += $this->currObj->virtualserver_autostart ? 4 : 0;
  334. $props->flags += $this->currObj->virtualserver_weblist_enabled ? 8 : 0;
  335. $props->flags += $this->currObj->virtualserver_ask_for_privilegekey ? 16 : 0;
  336. }
  337. elseif($this->currObj instanceof TeamSpeak3_Node_Channel)
  338. {
  339. $props->id = $this->currObj->getId();
  340. $props->icon = $this->currObj->isSpacer() ? 0 : $this->currObj->channel_icon_id < 0 ? pow(2, 32)-($this->currObj->channel_icon_id*-1) : $this->currObj->channel_icon_id;
  341. $props->path = trim($this->currObj->getPathway());
  342. $props->topic = strlen($this->currObj->channel_topic) ? trim($this->currObj->channel_topic) : null;
  343. $props->codec = $this->currObj->channel_codec;
  344. $props->users = $this->currObj->total_clients == -1 ? 0 : $this->currObj->total_clients;
  345. $props->slots = $this->currObj->channel_maxclients == -1 ? $this->currObj->getParent()->virtualserver_maxclients : $this->currObj->channel_maxclients;
  346. $props->famusers = $this->currObj->total_clients_family == -1 ? 0 : $this->currObj->total_clients_family;
  347. $props->famslots = $this->currObj->channel_maxfamilyclients == -1 ? $this->currObj->getParent()->virtualserver_maxclients : $this->currObj->channel_maxfamilyclients;
  348. $props->spacer = $this->getSpacerType();
  349. $props->flags = 0;
  350. $props->flags += $this->currObj->channel_flag_default ? 1 : 0;
  351. $props->flags += $this->currObj->channel_flag_password ? 2 : 0;
  352. $props->flags += $this->currObj->channel_flag_permanent ? 4 : 0;
  353. $props->flags += $this->currObj->channel_flag_semi_permanent ? 8 : 0;
  354. $props->flags += ($props->codec == 3 || $props->codec == 5) ? 16 : 0;
  355. $props->flags += $this->currObj->channel_needed_talk_power != 0 ? 32 : 0;
  356. $props->flags += $this->currObj->total_clients != -1 ? 64 : 0;
  357. $props->flags += $this->currObj->isSpacer() ? 128 : 0;
  358. }
  359. elseif($this->currObj instanceof TeamSpeak3_Node_Client)
  360. {
  361. $props->id = $this->currObj->getId();
  362. $props->icon = $this->currObj->client_icon_id < 0 ? pow(2, 32)-($this->currObj->client_icon_id*-1) : $this->currObj->client_icon_id;
  363. $props->version = TeamSpeak3_Helper_Convert::versionShort($this->currObj->client_version)->toString();
  364. $props->platform = $this->currObj->client_platform->toString();
  365. $props->country = strlen($this->currObj->client_country) ? trim($this->currObj->client_country) : null;
  366. $props->awaymesg = strlen($this->currObj->client_away_message) ? trim($this->currObj->client_away_message) : null;
  367. $props->memberof = array();
  368. $props->badges = $this->currObj->getBadges();
  369. $props->flags = 0;
  370. foreach($this->currObj->memberOf() as $num => $group)
  371. {
  372. $props->memberof[$num] = new stdClass();
  373. $props->memberof[$num]->name = trim($group->name);
  374. $props->memberof[$num]->icon = $group->iconid < 0 ? pow(2, 32)-($group->iconid*-1) : $group->iconid;
  375. $props->memberof[$num]->order = $group->sortid;
  376. $props->memberof[$num]->flags = 0;
  377. $props->memberof[$num]->flags += $group->namemode;
  378. $props->memberof[$num]->flags += $group->type == 2 ? 4 : 0;
  379. $props->memberof[$num]->flags += $group->type == 0 ? 8 : 0;
  380. $props->memberof[$num]->flags += $group->savedb ? 16 : 0;
  381. $props->memberof[$num]->flags += $group instanceof TeamSpeak3_Node_Servergroup ? 32 : 0;
  382. }
  383. $props->flags += $this->currObj->client_away ? 1 : 0;
  384. $props->flags += $this->currObj->client_is_recording ? 2 : 0;
  385. $props->flags += $this->currObj->client_is_channel_commander ? 4 : 0;
  386. $props->flags += $this->currObj->client_is_priority_speaker ? 8 : 0;
  387. $props->flags += $this->currObj->client_is_talker ? 16 : 0;
  388. $props->flags += $this->currObj->channelGetById($this->currObj->cid)->channel_needed_talk_power > $this->currObj->client_talk_power && !$this->currObj->client_is_talker ? 32 : 0;
  389. $props->flags += $this->currObj->client_input_muted || !$this->currObj->client_input_hardware ? 64 : 0;
  390. $props->flags += $this->currObj->client_output_muted || !$this->currObj->client_output_hardware ? 128 : 0;
  391. }
  392. elseif($this->currObj instanceof TeamSpeak3_Node_Servergroup || $this->currObj instanceof TeamSpeak3_Node_Channelgroup)
  393. {
  394. $props->id = $this->currObj->getId();
  395. $props->icon = $this->currObj->iconid < 0 ? pow(2, 32)-($this->currObj->iconid*-1) : $this->currObj->iconid;
  396. $props->order = $this->currObj->sortid;
  397. $props->n_map = $this->currObj->n_member_addp;
  398. $props->n_mrp = $this->currObj->n_member_removep;
  399. $props->flags = 0;
  400. $props->flags += $this->currObj->namemode;
  401. $props->flags += $this->currObj->type == 2 ? 4 : 0;
  402. $props->flags += $this->currObj->type == 0 ? 8 : 0;
  403. $props->flags += $this->currObj->savedb ? 16 : 0;
  404. $props->flags += $this->currObj instanceof TeamSpeak3_Node_Servergroup ? 32 : 0;
  405. }
  406. return $props;
  407. }
  408. /**
  409. * Returns the status icon URL of the current element.
  410. *
  411. * @return string
  412. */
  413. protected function getImage()
  414. {
  415. return str_replace("_", "-", $this->currObj->getIcon());
  416. }
  417. /**
  418. * Returns a string representation of this node.
  419. *
  420. * @return string
  421. */
  422. public function toString()
  423. {
  424. return $this->__toString();
  425. }
  426. /**
  427. * Returns a string representation of this node.
  428. *
  429. * @return string
  430. */
  431. public function __toString()
  432. {
  433. return json_encode($this->data);
  434. }
  435. }