admin.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /*
  3. * tngp/docs/admin.php
  4. *
  5. * Copyright (C) 2022 bzt
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. * @brief Example class to integrate the TirNanoG Server into your website
  22. *
  23. */
  24. namespace TirNanoG {
  25. class Admin {
  26. /* configure where the TirNanoG Server is running */
  27. public static $host = "localhost";
  28. public static $port = 4433;
  29. /* configure the desired date time format */
  30. public static $dateformat = \DateTime::ISO8601;
  31. /**
  32. * Get Server Status
  33. */
  34. public static function Status()
  35. {
  36. $ret = self::call("status");
  37. if(!empty($ret) && !empty($ret->now)) {
  38. $d = new \DateTime();
  39. $d->setTimestamp($ret->now);
  40. $ret->now = $d->format(self::$dateformat);
  41. if(!empty($ret->saved)) {
  42. $d->setTimestamp($ret->saved);
  43. $ret->saved = $d->format(self::$dateformat);
  44. } else
  45. $ret->saved = "";
  46. }
  47. return $ret;
  48. }
  49. /**
  50. * Get World File Details
  51. */
  52. public static function TNG()
  53. {
  54. return self::call("tng");
  55. }
  56. /**
  57. * Synchronize to Storage (Save Game)
  58. */
  59. public static function Sync()
  60. {
  61. return self::call("sync");
  62. }
  63. /**
  64. * Enable / Disable Public User Registration
  65. */
  66. public static function Registration($enabled)
  67. {
  68. return self::call($enabled ? "regon" : "regoff");
  69. }
  70. /**
  71. * Get List of Registered Users
  72. */
  73. public static function Users()
  74. {
  75. return self::call("users");
  76. }
  77. /**
  78. * Add User (can register and create a character if public registration is turned off)
  79. */
  80. public static function Add($username)
  81. {
  82. if(empty($username)) return (object)[ "ret"=> false ];
  83. return self::call("add", $username);
  84. }
  85. /**
  86. * Authenticate User
  87. * Also returns the language code that the user last used with the TirNanoG client
  88. */
  89. public static function Authenticate($username, $password)
  90. {
  91. if(empty($username) || empty($password)) return (object)[ "ret"=> false ];
  92. return self::call("auth", $username, $password);
  93. }
  94. /**
  95. * Set Password
  96. */
  97. public static function SetPassword($username, $password)
  98. {
  99. if(empty($username) || empty($password)) return (object)[ "ret"=> false ];
  100. return self::call("pass", $username, $password);
  101. }
  102. /**
  103. * Change the User's Password
  104. */
  105. public static function ChangePassword($username, $oldpass, $newpass1, $newpass2)
  106. {
  107. if(empty($username) || empty($oldpass) || empty($newpass1) || empty($newpass2)) return (object)[ "ret"=> false ];
  108. if($newpass1 != $newpass2) return (object)[ "ret"=> false, "msg"=> "New password and confirmation does not match." ];
  109. $ret = self::call("auth", $username, $oldpass);
  110. if(@$ret->ret != true) return (object)[ "ret"=> false, "msg"=> "Bad old password." ];
  111. return self::call("pass", $username, $newpass1);
  112. }
  113. /**
  114. * Forcefully Logout User from the Game
  115. */
  116. public static function Kick($username)
  117. {
  118. if(empty($username)) return (object)[ "ret"=> false ];
  119. return self::call("kick", $username);
  120. }
  121. /**
  122. * Ban User
  123. */
  124. public static function Ban($username)
  125. {
  126. if(empty($username)) return (object)[ "ret"=> false ];
  127. return self::call("ban", $username);
  128. }
  129. /**
  130. * Unban User
  131. */
  132. public static function Unban($username)
  133. {
  134. if(empty($username)) return (object)[ "ret"=> false ];
  135. return self::call("unban", $username);
  136. }
  137. /**
  138. * Get List of Denied IP Addresses
  139. */
  140. public static function DeniedIPList()
  141. {
  142. return self::call("denied");
  143. }
  144. /**
  145. * Add to Denied List
  146. */
  147. public static function Deny($username)
  148. {
  149. if(empty($username)) return (object)[ "ret"=> false ];
  150. return self::call("deny", $username);
  151. }
  152. /**
  153. * Remove from Denied List (by user name)
  154. */
  155. public static function Allow($username)
  156. {
  157. if(empty($username)) return (object)[ "ret"=> false ];
  158. return self::call("allow", $username);
  159. }
  160. /**
  161. * Remove from Denied List (by IP address)
  162. */
  163. public static function AllowIP($ip)
  164. {
  165. if(empty($ip)) return (object)[ "ret"=> false ];
  166. return self::call("allowip", $ip);
  167. }
  168. /**
  169. * Get User's Status
  170. */
  171. public static function UserStatus($username)
  172. {
  173. if(empty($username)) return (object)[ "ret"=> false ];
  174. $ret = self::call("user", $username);
  175. if(!empty($ret) && !empty($ret->created)) {
  176. $d = new \DateTime();
  177. $d->setTimestamp($ret->created);
  178. $ret->created = $d->format(self::$dateformat);
  179. $d->setTimestamp($ret->logind);
  180. $ret->logind = $d->format(self::$dateformat);
  181. if(!empty($ret->logoutd)) {
  182. $d->setTimestamp($ret->logoutd);
  183. $ret->logoutd = $d->format(self::$dateformat);
  184. } else
  185. $ret->logoutd = "";
  186. }
  187. return $ret;
  188. }
  189. /**
  190. * Set User's Attribute
  191. */
  192. public static function SetAttribute($username, $attribute, $value)
  193. {
  194. if(empty($username) || empty($attribute)) return (object)[ "ret"=> false ];
  195. return self::call("set", $username, $attribute, $value);
  196. }
  197. /**
  198. * Get User's Inventory
  199. */
  200. public static function Inventory($username)
  201. {
  202. if(empty($username)) return array();
  203. return self::call("inv", $username);
  204. }
  205. /**
  206. * Get User's Skills
  207. */
  208. public static function Skills($username)
  209. {
  210. if(empty($username)) return array();
  211. return self::call("skills", $username);
  212. }
  213. /**
  214. * Get User's Quests
  215. */
  216. public static function Quests($username)
  217. {
  218. if(empty($username)) return array();
  219. return self::call("quests", $username);
  220. }
  221. /**
  222. * Add Item to User
  223. */
  224. public static function Give($username, $qty, $object)
  225. {
  226. if(empty($username) || empty($qty) || empty($object)) return (object)[ "ret"=> false ];
  227. return self::call("give", $username, $qty, $object);
  228. }
  229. /**
  230. * Take Item from User
  231. */
  232. public static function Take($username, $qty, $object)
  233. {
  234. if(empty($username) || empty($qty) || empty($object)) return (object)[ "ret"=> false ];
  235. return self::call("take", $username, $qty, $object);
  236. }
  237. /* internal function to make the actual call to the server */
  238. private static function call($func, $u = "", $q = "", $o = "")
  239. {
  240. switch($func) {
  241. case "add": case "kick": case "ban": case "unban": case "deny": case "allow": case "allowip": case "user": case "inv": $url = $func."?u=".urlencode($u); break;
  242. case "auth": case "pass": $url = $func."?u=".urlencode($u)."&p=".urlencode($q); break;
  243. case "set": $url = $func."?u=".urlencode($u)."&".urlencode($q)."=".urlencode($o); break;
  244. case "give": case "take": $url = $func."?u=".urlencode($u)."&q=".urlencode($q)."&o=".urlencode($o); break;
  245. default: $url = $func; break;
  246. }
  247. $ch = curl_init();
  248. $ipv6 = strpos(self::$host, ":") !== false;
  249. curl_setopt($ch, CURLOPT_URL, "https://".($ipv6 ? "[" : "").self::$host.($ipv6 ? "]" : "").":".self::$port."/".$url);
  250. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  251. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  252. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  253. $d = curl_exec($ch);
  254. curl_close($ch);
  255. return empty($d) ? (object)[ "ret"=> false, "msg"=> "connection error" ] : json_decode($d);
  256. }
  257. }
  258. /* just a quick test, remove this */
  259. //print_r(\TirNanoG\Admin::Status());
  260. //print_r(\TirNanoG\Admin::TNG());
  261. //print_r(\TirNanoG\Admin::Sync());
  262. print_r(\TirNanoG\Admin::Users());
  263. print_r(\TirNanoG\Admin::UserStatus("valaki"));
  264. }