Anonymous 3D Imageboard http://cyberia.digital/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1142 lines
39KB

  1. <?php
  2. /*
  3. ________ ______ __ ____
  4. / ____/ /_ ____ _____ / ____/___ ____ / /__________ / / /__ _____
  5. / / / __ \/ __ `/ __ \ / / / __ \/ __ \/ __/ ___/ __ \/ / / _ \/ ___/
  6. / /___/ / / / /_/ / / / / / /___/ /_/ / / / / /_/ / / /_/ / / / __/ /
  7. \____/_/ /_/\__,_/_/ /_/ \____/\____/_/ /_/\__/_/ \____/_/_/\___/_/
  8. Functions:
  9. -getThreadsList
  10. -createThread
  11. -createPost
  12. */
  13. ini_set('display_errors', 1);
  14. require_once "Utils/Constants.php";
  15. require_once "Imports/LazerImports.php";
  16. require_once 'Utils/Helpers.php';
  17. require_once 'Libs/torrent-rw/Torrent.php';
  18. require_once 'Libs/php-bittorrent.phar';
  19. require_once 'AdminController.php';
  20. require_once 'MapController.php';
  21. require_once 'Libs/forceutf8/Encoding.php';
  22. require_once 'Models/Post.php';
  23. require_once 'Models/Game/GameData.php';
  24. require_once 'Models/Game/Tile.php';
  25. require_once 'Models/Game/Items.php';
  26. require_once 'Models/FakeFile.php';
  27. use \ForceUTF8\Encoding;
  28. //Autoload usage https://stackoverflow.com/questions/16175023/php-include-class-in-other-class
  29. use Lazer\Classes\Database as Lazer;
  30. defined('LAZER_DATA_PATH') or define('LAZER_DATA_PATH', realpath(dirname(__FILE__)).'/'.Constants::CONST_DB_FOLDER.'/');
  31. Class ChanController {
  32. function getPostByMapKey($mapKey) {
  33. $count = Lazer::table(Constants::CONST_TABLE_POSTS)->where('map_key', '=', $mapKey)-> findAll() -> count();
  34. if($count > 0){
  35. $postFromMap = Lazer::table(Constants::CONST_TABLE_POSTS)->where('map_key', '=', $mapKey)->where('show', '=', true)->findAll();
  36. if(!$this->isPlayerAdmin()){
  37. foreach ( $postFromMap as $key => $post) {
  38. $post -> player_ip = null;
  39. }
  40. }
  41. return Helpers::getDataFromLazer($postFromMap);
  42. }
  43. return array();
  44. }
  45. function getPostsByCanvas($mapKey,$canvas_key_) {
  46. $postFromThread = Lazer::table(Constants::CONST_TABLE_POSTS)->where('map_key', '=', $this->getPlayer()->map_key)->where('canvas_key', '=', $canvas_key_)->findAll();
  47. if(!$this->isPlayerAdmin()){
  48. foreach ( $postFromThread as $key => $post) {
  49. $post -> player_ip = null;
  50. }
  51. }
  52. return Helpers::getDataFromLazer($postFromThread);
  53. }
  54. function getReplies($postId_) {
  55. $postFromThread = Lazer::table(Constants::CONST_TABLE_POSTS)->where('reply', '=',(int)$postId_) -> orWhere('id', '=',(int)$postId_) ->findAll();
  56. if(!$this->isPlayerAdmin()){
  57. foreach ( $postFromThread as $key => $post) {
  58. $post -> player_ip = null;
  59. }
  60. }
  61. return Helpers::getDataFromLazer($postFromThread);
  62. }
  63. //Don't need the map key because it's embed in the player profile
  64. function getMapPosts() {
  65. $mapKey = $this -> getPlayerMapKey();
  66. $postArray = $this -> getPostByMapKey($mapKey);
  67. $firstPostArray = array();
  68. foreach ($postArray as &$post){
  69. array_push( $firstPostArray, $post);
  70. }
  71. //Move the player
  72. $this -> move();
  73. /*
  74. try{
  75. if(isset($postArray) && count($firstPostArray) > 0 ) usort($firstPostArray, array('ChanController','cmpPost'));
  76. }catch (Exception $e) {
  77. $firstPostArray = array();
  78. }*/
  79. //get the map data
  80. $gameData = new GameData();
  81. $gameData -> player = Helpers::getDataFromLazer($this -> getPlayer());
  82. $gameData -> players = $this -> getAllPlayers();
  83. $gameData -> posts = Helpers::getDataFromLazer($firstPostArray);
  84. //$gameData -> itemsPlayer = Helpers::getDataFromLazer($this -> getItemsForPlayer() );
  85. //$gameData -> itemsMap = Helpers::getDataFromLazer($this -> getItemsForMap() );
  86. $gameData -> itemsPlayer = [];
  87. $gameData -> itemsMap = [];
  88. $gameData -> map = Helpers::getDataFromLazer($this->getPlayerMapData());
  89. $gameData -> tiles = $this->getAllTiles();
  90. // $gameData -> actions = Helpers::getDataFromLazer($this->getActions());
  91. $gameData -> actions = [];
  92. $gameData -> postsLeft = $this->howManyPostLeft();
  93. return $gameData;
  94. }
  95. function createPost($post, $file, $img){
  96. $playerRow = $this->getPlayer();
  97. if( !isset($playerRow) || $playerRow -> hp <= 0 || $this->testIfBan() ) return false;
  98. if( strpos( strtolower($post->message) , 'cp') !== false
  99. || strpos( strtolower($post->message) , '<?') !== false
  100. || strpos( strtolower($post->message) , 'php') !== false
  101. || strpos( strtolower($post->url) , 'youdieifyou.work') !== false //216.130.236.22
  102. ){
  103. $playerRow -> hp = 0;
  104. $playerRow -> save();
  105. return false;
  106. }
  107. //TODO : test if it's an existing thread
  108. // $row = Lazer::table(Constants::CONST_TABLE_THREADS);
  109. $urlTorrent = '';
  110. $magnet = '';
  111. $hash = '';
  112. $file_name = null;
  113. $file_fake_path = null;
  114. $fakeFile = null;
  115. /***************************
  116. * ANTI HACKING!!!already posted!
  117. **************************/
  118. //Does the canvas key already exist?
  119. $countNumCanvas = Lazer::table(Constants::CONST_TABLE_POSTS)->where('map_key', '=', $this->getPlayer()->map_key)->where('canvas_key', '=', $post->canvas_key)->where('show', '=', true)-> findAll() -> count();
  120. if($this->howManyPostLeft() <= 0) return "no more post!";
  121. if($countNumCanvas > 1) {
  122. return "Canvas already done";
  123. }
  124. if( isset($post -> url)){
  125. if( isset($post -> url) && $post -> url != '') $countNumCanvas = Lazer::table(Constants::CONST_TABLE_POSTS)->where('url', '=', $post -> url)->where('show', '=', true)-> findAll() -> count();
  126. if($countNumCanvas > 6) return "already posted!";
  127. if(strlen($post -> url)> 3000) return "message too big";
  128. }
  129. if( isset($post -> message) && strlen($post -> message) > 0){
  130. $countNumCanvas = Lazer::table(Constants::CONST_TABLE_POSTS)->where('message', '=', $post -> message)->where('show', '=', true)-> findAll() -> count();
  131. if($countNumCanvas > 6) return "message already done";
  132. if(strlen($post -> message)> 3000) return "message too big";
  133. }
  134. //-----------------------
  135. $urlPreview = "";
  136. if(isset($post -> url) && $this->getYoutubePreview($post -> url)) $urlPreview = $this->getYoutubePreview($post -> url);
  137. $post-> map_key = $this->getPlayer()->map_key;
  138. if($this->getYoutubePreview($post -> url) || isset($img)){
  139. //The error validation could be done on the javascript client side.
  140. $errors = array();
  141. if (!file_exists(Constants::CONST_IMAGE_UPLOAD_FOLDER.'/'.$post->map_key)) {
  142. mkdir(Constants::CONST_IMAGE_UPLOAD_FOLDER.'/'.$post->map_key, 0777, true);
  143. }
  144. if($this->getYoutubePreview($post -> url)){
  145. $mediaFile = pathinfo($urlPreview);
  146. $file_ext = strtolower( $mediaFile['extension'] );
  147. $file_name = $mediaFile['filename'].'.'. $file_ext;
  148. if (!file_exists(Constants::CONST_IMAGE_UPLOAD_FOLDER.'/'.Constants::CONST_IMAGE_PREVIEW_FOLDER.'/')) {
  149. mkdir(Constants::CONST_IMAGE_UPLOAD_FOLDER.'/'.Constants::CONST_IMAGE_PREVIEW_FOLDER.'/', 0777, true);
  150. }
  151. $fakeFile = new FakeFile($this->getChanURI(),$file_ext,Constants::CONST_IMAGE_PREVIEW_FOLDER);
  152. $file_name = $fakeFile -> file_name;
  153. $internalUrl = $fakeFile ->fake_path;
  154. $isCreated = file_put_contents ( Constants::CONST_IMAGE_UPLOAD_FOLDER.'/'.Constants::CONST_IMAGE_PREVIEW_FOLDER.'/'.$file_name ,fopen($urlPreview, 'r') );
  155. if(!$isCreated) $errors[]= Constants::CONST_FILES_MAX_SIZE_ERROR;
  156. $urlPreview = $internalUrl;
  157. } else {
  158. //ANti hack
  159. if(strpos($img, '<?') !== false) return "stop faggot";
  160. if($post->graffiti === 'true'){
  161. if(strpos($img, 'snffch') === false) return "stop faggggot";
  162. $img = str_replace('snffch', '', $img);
  163. } else {
  164. if( !$playerRow -> is_admin ) return "not admin";
  165. }
  166. $fileExt = "";
  167. if(strpos($img, 'png;') !== false){
  168. $fileExt = "png";
  169. } else if(strpos($img, 'jpg;') !== false) {
  170. $fileExt = "jpg";
  171. } else if(strpos($img, 'jpeg;') !== false) {
  172. $fileExt = "jpeg";
  173. } else if(strpos($img, 'gif;') !== false) {
  174. $fileExt = "gif";
  175. }else if(strpos($img, 'webp;') !== false) {
  176. $fileExt = "webp";
  177. }
  178. $img = str_replace('data:image/'.$fileExt.';base64,', '', $img);
  179. $img = str_replace(' ', '+', $img);
  180. //echo 'fileExt= '.$fileExt;
  181. //echo 'img= '.$img;
  182. $fakeFile = new FakeFile($this->getChanURI(),$fileExt,$post->map_key);
  183. $file_name = $fakeFile -> file_name;
  184. $urlFileFull = $fakeFile ->fake_path;
  185. if(!Helpers::check_base64_image($img)) $errors[]= Constants::CONST_FILES_EXT_ERROR;
  186. if(!Helpers::getBase64ImageSize($img) > 400) $errors[]= Constants::CONST_FILES_EXT_ERROR;
  187. $data = base64_decode($img);
  188. if(empty($errors)==true) file_put_contents(Constants::CONST_IMAGE_UPLOAD_FOLDER.'/'.$post->map_key.'/'.$file_name, $data);
  189. }
  190. if(empty($errors)==true){
  191. if(!$this->getYoutubePreview($post -> url)) {
  192. //Multi files management
  193. //http://getright.com/seedtorrent.html
  194. //If file created, we need to create the torrent!
  195. // create torrent
  196. if (!file_exists('torrents/'.$post->map_key)) {
  197. mkdir('torrents/'.$post->map_key, 0777, true);
  198. }
  199. $url = Constants::CONST_IMAGE_UPLOAD_FOLDER.'/'.$post->map_key.'/'. $file_name;
  200. $urlTorrent = 'torrents/'.$post->map_key.'/'.$file_name.'.torrent';
  201. $urlTorrentFull = $this->getChanURI()."php/".$urlTorrent;
  202. $urlFileFull = $fakeFile ->fake_path;
  203. $torrent1 = PHP\BitTorrent\Torrent::createFromPath($url, $this->getChanURI().'php/tracker/announce.php');
  204. $torrent1 ->setComment(CONSTANTS::SITE_TORRENT_DESC)->save($urlTorrent);
  205. $torrent = new Torrent( $urlTorrent );
  206. $torrent->url_list(array($urlFileFull));
  207. $torrent->announce(array('wss://tracker.openwebtorrent.com/', 'wss://tracker.fastcast.nz/'));
  208. $torrent->save($urlTorrent); // save to disk
  209. $magnet = $torrent->magnet();
  210. $hash = $torrent->hash_info();
  211. $post->url = $urlFileFull;
  212. }
  213. } else {
  214. Helpers::makeGenericResponse(false, null, json_encode($errors));
  215. }
  216. }
  217. $this -> cleanMap($this->getPlayer()->map_key);
  218. $isGraffiti = ( $post->graffiti === 'true');
  219. $postRow = Lazer::table(Constants::CONST_TABLE_POSTS);
  220. $postRow -> date = time();
  221. $postRow -> key = uniqid();
  222. $postRow -> map_key = $this->getPlayer()->map_key;
  223. $postRow -> canvas_key = $post->canvas_key;
  224. $postRow -> player_ip = Helpers::getClientIp();
  225. $postRow -> player_id = (int)$this->getPlayer()->id;
  226. $postRow -> player_name = $post->player_name;
  227. $postRow -> message = $post->message;
  228. $postRow -> preview = $urlPreview;
  229. $postRow -> is_canon = false;
  230. $postRow -> show = true;
  231. //$postRow -> is_admin = $this->isPlayerAdmin();
  232. $postRow -> url = $post->url;
  233. $postRow -> reply = (int)$post->reply;
  234. $postRow -> graffiti = $isGraffiti;
  235. $postRow -> magnet = $magnet;
  236. $postRow -> torrent = $urlTorrent;
  237. $postRow -> hash = $hash;
  238. $postRow -> save();
  239. /*
  240. //Get the thread to update the post
  241. $threadData = Lazer::table(Constants::CONST_TABLE_THREADS)->where('key', '=', $post->map_key)->find();
  242. $threadData -> date_update = time();
  243. $threadData -> save();*/
  244. $this -> generateTorrentForAll($post->map_key);
  245. if(isset($file_name)) $this -> addFile($postRow, $fakeFile);
  246. //Return the created post
  247. return Helpers::getDataFromLazer($postRow);
  248. }
  249. function getActions(){
  250. $logList = Lazer::table(Constants::CONST_TABLE_LOGS)->where('map_key', '=', $this->getPlayer()->map_key )-> findAll();
  251. return Helpers::getDataFromLazer( $logList);
  252. }
  253. function setAction($action, $value){
  254. $numLogMax = 30;
  255. if(strlen($value)> 145) return "message too big";
  256. if(strlen($action)> 145) return "message too big";
  257. if(strlen($value) < 2) return "message too small";
  258. $message = str_ireplace("nigger", "noggle", $value);
  259. $row = Lazer::table(Constants::CONST_TABLE_LOGS);
  260. $row-> player_id = (int) ($this -> getPlayer()->id);
  261. $row-> ip = Helpers::getClientIp();
  262. $row-> map_key = $this->getPlayer()->map_key;
  263. $row -> last_action = time();
  264. $row -> action = $action;
  265. $row -> value = $message;
  266. $row->save();
  267. //Clean the old logs
  268. $count = Lazer::table(Constants::CONST_TABLE_LOGS)->where('map_key', '=', $this->getPlayer()->map_key)-> count();
  269. if( $count > $numLogMax ){
  270. $logList = Lazer::table(Constants::CONST_TABLE_LOGS)->where('map_key', '=', $this->getPlayer()->map_key )-> findAll();
  271. $toSupp = $count - $logList;
  272. foreach($logList as $log){
  273. if( $toSupp > 0){
  274. $logToSupp = Lazer::table(Constants::CONST_TABLE_LOGS)->find($log->id);
  275. $logToSupp -> delete();
  276. $toSupp --;
  277. }
  278. }
  279. }
  280. }
  281. public function cleanMap($mapKey_){
  282. $numPostMax = 120;
  283. $count = Lazer::table(Constants::CONST_TABLE_POSTS)->where('map_key', '=', $mapKey_)->where('show', '=', true)-> findAll() -> count();
  284. if( $count > $numPostMax ){
  285. $postList = Lazer::table(Constants::CONST_TABLE_POSTS)->where('is_canon', '=', false )->where('show', '=', true)-> findAll();
  286. $toSupp = $count - $numPostMax;
  287. //var_dump($postList );
  288. foreach($postList as $post){
  289. if( $toSupp > 0){
  290. $postToSupp = Lazer::table(Constants::CONST_TABLE_POSTS)->find($post->id);
  291. $postToSupp -> show = false;
  292. $postToSupp -> save();
  293. //echo "/id to supp ".$postToSupp -> id." ";
  294. $toSupp --;
  295. }
  296. }
  297. }
  298. }
  299. /*********************
  300. * FILE CONTROLLER
  301. ********************/
  302. public function addFile($postRow_, $fakeFile_){
  303. $fileRow =Lazer::table(Constants::CONST_TABLE_FILES);
  304. $fileRow ->id_post = $postRow_->id;
  305. $fileRow ->map_key = $postRow_->map_key;
  306. $fileRow ->fake_name = $fakeFile_->fake_name;
  307. $fileRow ->fake_path = $fakeFile_->fake_path;
  308. $fileRow ->file_name = $fakeFile_->file_name;
  309. $fileRow -> save();
  310. }
  311. public function deleteFileForPost($postId_){
  312. $postRow = Lazer::table(Constants::CONST_TABLE_POSTS)->where('id', '=', $postId_)->find();
  313. $fileRow = Lazer::table(Constants::CONST_TABLE_FILES)->where('id_post', '=', $postId_)->find();
  314. if( isset($fileRow) && isset($fileRow->file_name) ){
  315. //Delete the image
  316. $filePath = Constants::CONST_IMAGE_UPLOAD_FOLDER.'/'.$postRow->map_key.'/'. $fileRow->file_name;
  317. if(file_exists($filePath)) unlink($filePath) or die("Couldn't delete file");
  318. //Delete the torrent
  319. $torrentPath = 'torrents/'.$postRow->map_key.'/'.$fileRow->file_name.'.torrent';
  320. if(file_exists($filePath)) unlink($torrentPath) or die("Couldn't delete file");
  321. //reinit the torrent
  322. $this -> generateTorrentForAll($postRow->map_key);
  323. $fileRow -> delete();
  324. }
  325. }
  326. /*********************
  327. * PLAYER CONTROLLER
  328. ********************/
  329. public function init($id, $key, $password, $mapKey){
  330. //Clean the old players in the DB
  331. Lazer::table(Constants::CONST_TABLE_PLAYERS)->where('last_action', '<', Helpers::getDeadline() )->delete();
  332. $kPlayer = null;
  333. $row = null;
  334. $isAdmin = false;
  335. $levelName = 'alpha';
  336. if( isset($mapKey) ){
  337. $levelRow = Lazer::table(Constants::CONST_TABLE_MAP)->where('key', '=', $mapKey)->find();
  338. if( isset($levelRow) && isset($levelRow -> id)) $levelName = $levelRow -> key;
  339. }
  340. //We test if admin
  341. if( $password != null && strlen($password) > 0 ){
  342. $adminController = new AdminController();
  343. $isAdmin = $adminController->validPassword($password);
  344. }
  345. //We authentificate the id + the key
  346. try {
  347. if( $id != null && $key != null){
  348. //we get the player
  349. $row = Lazer::table(Constants::CONST_TABLE_PLAYERS)->find((int)$id);
  350. if($isAdmin) $row -> is_admin = $isAdmin;
  351. $token = $row -> player_key;
  352. if($key == $token){
  353. $row -> map_key = $levelName;
  354. $row -> save();
  355. //return Helpers::getDataFromLazer($row);
  356. }
  357. }
  358. } catch(Exception $e){
  359. //echo $e;
  360. $id = null;
  361. }
  362. //We make a new player
  363. if($id == null) {
  364. /*
  365. //$levelRow = Lazer::table(Constants::CONST_TABLE_MAP)->find($index);
  366. $level = Level::parseRow($row);
  367. //We need to find empty coordonates
  368. $coord = $level -> getEmptyCoord();
  369. */
  370. $row = Lazer::table(Constants::CONST_TABLE_PLAYERS);
  371. $row-> player_key = Helpers::generateToken();
  372. $row -> last_action = time();
  373. $row -> hp = 100;
  374. $row -> position_x = doubleval (-1);
  375. $row -> position_y = doubleval (-1);
  376. $row -> map_key = $levelName;
  377. $row -> is_admin = $isAdmin;
  378. $row->save();
  379. //We refill the user's items
  380. $this-> fillingMap();
  381. //Get the ID of the user saved
  382. //return Helpers::getDataFromLazer($row);
  383. }
  384. if(isset($row)){
  385. $_POST['player_id'] = $row -> id;
  386. $_POST['player_key'] = $row -> player_key;
  387. $_POST['position_x'] = $row -> position_x;
  388. $_POST['position_y'] = $row -> position_y;
  389. return $this->getMapPosts();
  390. }
  391. return null;
  392. }
  393. public function move(){
  394. $row = $this->getPlayer();
  395. //manage the player move
  396. $playerX = -1;
  397. $playerY = -1;
  398. $playerTalk = "";
  399. if( isset($_POST['position_x']) ) $playerX = doubleval ($_POST['position_x']);
  400. if( isset($_POST['position_y']) ) $playerY = doubleval ($_POST['position_y']);
  401. //if( isset($_POST['talk']) ) $playerTalk = $_POST['talk'];
  402. //else we are updateing his locations and diverse data;
  403. if( $playerX != -1){
  404. $row -> position_x = $playerX;
  405. $row -> position_y = $playerY;
  406. $row -> talk = $playerTalk;
  407. }
  408. $row -> last_action = time();
  409. $row->save();
  410. return Helpers::getDataFromLazer($row);
  411. }
  412. public function moveInteract($id){
  413. //return $this->mobiles;
  414. }
  415. public function getAllPlayers(){
  416. if( $this->getPlayer()->id != null ) {
  417. /*
  418. $playerCount = Lazer::table(Constants::CONST_TABLE_PLAYERS)->where('id', '!=',$this->getPlayer()->id)
  419. ->where('map_key', '=', $this->getPlayer()->map_key)
  420. ->count();
  421. if($playerCount == 0) return array();
  422. */
  423. $playerTable = Lazer::table(Constants::CONST_TABLE_PLAYERS)->where('id', '!=',$this->getPlayer()->id)
  424. ->where('map_key', '=', $this->getPlayer()->map_key)
  425. ->findAll();
  426. } else {
  427. $playerTable = Lazer::table(Constants::CONST_TABLE_PLAYERS)->findAll();
  428. }
  429. if(!$this->isPlayerAdmin()){
  430. foreach ( $playerTable as $key => $player) {
  431. $player -> player_key = null;
  432. }
  433. }
  434. return Helpers::getDataFromLazer($playerTable);
  435. }
  436. public function getPlayerByID($id){
  437. $row = Lazer::table(Constants::CONST_TABLE_PLAYERS)->find($id);
  438. return Helpers::getDataFromLazer($row);
  439. }
  440. public function isPlayerAdmin(){
  441. $playerId = null;
  442. $playerKey = null;
  443. if( isset($_POST['player_id']) ) $playerId = $_POST['player_id'];
  444. if( isset($_POST['player_key']) ) $playerKey = $_POST['player_key'];
  445. if($playerId == null || $playerKey == null) return false;
  446. $row = Lazer::table(Constants::CONST_TABLE_PLAYERS)->find($playerId);
  447. if($row -> player_key == $playerKey && ($row -> is_admin) ) return true;
  448. return false;
  449. }
  450. public function getPlayer(){
  451. $playerId = null;
  452. $playerKey = null;
  453. if( isset($_POST['player_id']) ) $playerId = $_POST['player_id'];
  454. if( isset($_POST['player_key']) ) $playerKey = $_POST['player_key'];
  455. if($playerId == null || $playerKey == null) return false;
  456. $count = Lazer::table(Constants::CONST_TABLE_PLAYERS)->findAll()->count();
  457. //echo "count ".$count." ";
  458. if($count == 0){
  459. //delete DB
  460. echo "DeLETE DB!!!!! --------- ";
  461. Lazer::remove(Constants::CONST_TABLE_PLAYERS);
  462. Lazer::create(Constants::CONST_TABLE_PLAYERS, array(
  463. 'id' => 'integer',
  464. 'name' => 'string',
  465. 'talk' => 'string',
  466. 'is_admin' => 'boolean',
  467. 'player_key' => 'string',
  468. 'hp' => 'integer',
  469. 'money' => 'integer',
  470. 'map_key' => 'string',
  471. 'last_action' => 'integer',
  472. 'position_x' => 'double',
  473. 'position_y' => 'double',
  474. 'position_z' => 'double'
  475. ));
  476. }
  477. $row = Lazer::table(Constants::CONST_TABLE_PLAYERS)->find($playerId);
  478. if($row -> player_key != $playerKey) return false;
  479. return $row;
  480. }
  481. public function getPlayerMapKey(){
  482. $playerId = null;
  483. $row = $this->getPlayer();
  484. return $row->map_key;
  485. }
  486. public function setItem($key_){
  487. //test if key exist
  488. /*
  489. $row = Lazer::table(Constants::CONST_TABLE_ITEMS)->where('player_id', '=', (int) $this -> getPlayer()->id )
  490. ->where('key', '=', $key_ )->findAll();*/
  491. //if(isset( $row)){
  492. $itemRow = Lazer::table(Constants::CONST_TABLE_ITEMS)->where('key', '=', $key_)->find();
  493. //Is already the player having this kind of itM?
  494. $countPlayerItem = Lazer::table(Constants::CONST_TABLE_ITEMS)->where('player_id', '=', (int) $this -> getPlayer()->id )->where('action', '=', $itemRow->action )->findAll() -> count();
  495. if($countPlayerItem == 0){
  496. $itemRow -> player_id = (int) ($this -> getPlayer()->id);
  497. $itemRow -> map_key = "";
  498. $itemRow -> save();
  499. $this-> fillingMap();
  500. }
  501. //}
  502. //We refill the user's items
  503. return true;
  504. }
  505. public function hasItemForAction($action_){
  506. $countPlayerItem = Lazer::table(Constants::CONST_TABLE_ITEMS)->where('player_id', '=', (int) $this -> getPlayer()->id )->where('action', '=', $action_ )->findAll() -> count();
  507. //echo "num player item ".$countPlayerItem." for player ".$this -> getPlayer()->id ;
  508. if($countPlayerItem > 0) return true;
  509. return false;
  510. }
  511. public function getItemsForPlayer(){
  512. $count = Lazer::table(Constants::CONST_TABLE_ITEMS)->where('player_id', '=', (int) $this -> getPlayer()->id )->findAll() -> count();
  513. //echo $count;
  514. if($count > 0){
  515. $itemsRow = Lazer::table(Constants::CONST_TABLE_ITEMS)->where('player_id', '=', (int) $this -> getPlayer()->id )->findAll();
  516. return $itemsRow ;
  517. }
  518. return array();
  519. }
  520. public function getItemsForMap(){
  521. $count = Lazer::table(Constants::CONST_TABLE_ITEMS)->where('map_key', '=', $this -> getPlayer()->map_key )->where('player_id', '=', -1 )->findAll() -> count();
  522. //echo $count;
  523. if($count > 0){
  524. $itemsRow = Lazer::table(Constants::CONST_TABLE_ITEMS)->where('map_key', '=', $this -> getPlayer()->map_key )->where('player_id', '=', -1 )->findAll();
  525. return $itemsRow ;
  526. }
  527. return array();
  528. }
  529. public function fillingMap(){
  530. //Get all map + all items
  531. $countMap = Lazer::table(Constants::CONST_TABLE_MAP)->findAll()-> count();
  532. $countPlayers = Lazer::table(Constants::CONST_TABLE_PLAYERS)->findAll()-> count();
  533. $Items = Item::getAllPotentialItems();
  534. //echo "map ".$countMap."; ".$countPlayers ;
  535. //Foreach item
  536. foreach ($Items as $item){
  537. //echo $item-> name;
  538. $count = Lazer::table(Constants::CONST_TABLE_ITEMS)->where('name', '=', $item-> name )->where('player_id', '=', -1 )->findAll()-> count();
  539. //echo $count;
  540. $neededItems = $countPlayers * ($item -> itemsPerPlayer);
  541. if($neededItems > 3 ) $neededItems = 3;
  542. for($i = $count ; $i < $neededItems; $i ++){
  543. //Choose random level
  544. $index = rand( 1, $countMap);
  545. //echo "index asked".index;
  546. $levelRow = Lazer::table(Constants::CONST_TABLE_MAP)->find($index);
  547. $level = Level::parseRow($levelRow);
  548. //We need to find empty coordonates
  549. $coord = $level -> getEmptyCoord() ;
  550. //We save the item!
  551. $row = Lazer::table(Constants::CONST_TABLE_ITEMS);
  552. $row -> action = $item -> action ;
  553. $row -> type = $item->type ;
  554. $row -> name = $item->name ;
  555. $row -> description = $item->description ;
  556. $row -> map_key = $level -> key;
  557. $row -> key = uniqid();
  558. $row -> player_id = -1;
  559. $row -> x = doubleval($coord -> x);
  560. $row -> y = doubleval($coord -> y);
  561. $row -> is_expendable = $item -> is_expendable;
  562. $row -> value = rand($item->valueMin, $item->valueMax);
  563. //echo 'before save! <br/>';
  564. $row -> save();
  565. }
  566. }
  567. //echo 'this eand! <br/>';
  568. }
  569. /*********************
  570. * BANNING SYSTEM
  571. ********************/
  572. function setBan($postId){
  573. if(!$this->isPlayerAdmin()) return false;
  574. $postRow = Lazer::table(Constants::CONST_TABLE_POSTS)->find((int)$postId);
  575. $ipToBan = $postRow -> player_ip;
  576. $banRow = Lazer::table(Constants::CONST_TABLE_BANS);
  577. $banRow -> id_post = (int)$postId;
  578. $banRow -> reason = $postRow -> canvas_key.' url: '.$postRow -> url.' message: '.$postRow -> url;
  579. $banRow -> date = time();
  580. $banRow -> player_ip = $ipToBan ;
  581. $banRow -> save();
  582. return true;
  583. }
  584. function testIfBan(){
  585. //echo Helpers::getClientIp();
  586. $result = Lazer::table(Constants::CONST_TABLE_BANS)->where('player_ip','=',Helpers::getClientIp())->findAll();
  587. //var_dump( $result );
  588. if( isset( $result ) && count($result)) return true;
  589. return false;
  590. }
  591. /*********************
  592. * MAP DATA
  593. ********************/
  594. public function getPlayerMapData(){
  595. $mapKey = $this -> getPlayerMapKey();
  596. $row = Lazer::table(Constants::CONST_TABLE_MAP)->where('key', '=', $mapKey)->find();
  597. //read the JSON file
  598. return $row;
  599. }
  600. function getMapAlphaKey(){
  601. $row = Lazer::table(Constants::CONST_TABLE_MAP)->where('name', '=', Constants::CONST_MAP_ALPHA_NAME)->find();
  602. return $row->key;
  603. }
  604. public function setPlayerMapKey($mapKey_){
  605. $row = $this -> getPlayer();
  606. $row ->position_x = -1;
  607. $row ->position_y = -1;
  608. $row -> map_key = $mapKey_;
  609. $row -> save();
  610. return Helpers::getDataFromLazer($row) ;
  611. }
  612. public function setPlayerMapName($mapName_){
  613. $mapRow = Lazer::table(Constants::CONST_TABLE_MAP)
  614. ->where('name', '=', $mapName_ )
  615. ->find();
  616. if( $mapRow == null && $mapRow -> name == null ) return false;
  617. $row = $this -> getPlayer();
  618. $row ->position_x = doubleval (-1);
  619. $row ->position_y = doubleval (-1);
  620. $row -> map_key = $mapRow -> map_key;
  621. $row -> save();
  622. return Helpers::getDataFromLazer($row) ;
  623. }
  624. public function setPlayerMapRand($secret_){
  625. $row = $this -> getPlayer();
  626. //Get a random map
  627. $mapList = Lazer::table(Constants::CONST_TABLE_MAP)->where('is_secret', '=', $secret_)
  628. ->where('key', '!=', $row -> map_key )
  629. ->where('name', '!=', 'alpha' )
  630. ->findAll();
  631. $selectedIndex = rand ( 0 , count($mapList)-1 );
  632. $m = 0;
  633. foreach( $mapList as $map)
  634. {
  635. if($m == $selectedIndex ) $mapRow = $map;
  636. $m ++;
  637. }
  638. $row ->position_x = doubleval (-1);
  639. $row ->position_y = doubleval (-1);
  640. $row -> map_key = $mapRow -> key;
  641. $row -> save();
  642. return Helpers::getDataFromLazer($row) ;
  643. }
  644. public function getJsonByPath($path_){
  645. $jsonFile = fopen($path_, 'r') or die("Unable to open file!");
  646. $json = fread($jsonFile,filesize($path_));
  647. fclose($jsonFile);
  648. return $json;
  649. }
  650. /*********************
  651. * ADMIN CONTROLLER
  652. ********************/
  653. //Set as canon
  654. function setPostCanon($mapKey, $postId){
  655. if(!$this->isPlayerAdmin()) return false;
  656. $postRow = Lazer::table(Constants::CONST_TABLE_POSTS)->find($postId);
  657. $postRow -> is_canon = !$postRow -> is_canon;
  658. $postRow -> save();
  659. return true;
  660. }
  661. //Upload post Url
  662. function setPostUrlToFile( $postId ){
  663. if(!$this->isPlayerAdmin()) return false;
  664. $postRow = Lazer::table(Constants::CONST_TABLE_POSTS)->find((int)$postId);
  665. //var_dump( $postRow);
  666. $urlToLoad = $postRow->url;
  667. //echo $urlToLoad;
  668. $mediaFile = pathinfo( $urlToLoad );
  669. if( isset($info['extension']) ){
  670. $file_ext = strtolower( $mediaFile['extension'] );
  671. } else if(strpos(strtolower($urlToLoad), '.gif')){
  672. //If GIf?
  673. $file_ext = 'gif';
  674. } else {
  675. $file_ext = 'jpg';
  676. }
  677. $file_name = $mediaFile['filename'].'.'. $file_ext;
  678. if (!file_exists(Constants::CONST_IMAGE_UPLOAD_FOLDER.'/'.$postRow->map_key.'/')) {
  679. mkdir(Constants::CONST_IMAGE_UPLOAD_FOLDER.'/'.$postRow->map_key.'/', 0777, true);
  680. }
  681. //echo $file_ext." ".$postRow->map_key." " ;
  682. $fakeFile = new FakeFile($this->getChanURI(),$file_ext,$postRow->map_key);
  683. $file_name = $fakeFile -> file_name;
  684. // echo " ".$file_name;
  685. $internalUrl = $fakeFile ->fake_path;
  686. $isCreated = file_put_contents ( Constants::CONST_IMAGE_UPLOAD_FOLDER.'/'.$postRow->map_key.'/'.$file_name ,fopen($urlToLoad, 'r') );
  687. if(!$isCreated) $errors[]= Constants::CONST_FILES_MAX_SIZE_ERROR;
  688. $finalURL = $internalUrl;
  689. //echo " ".$finalURL;
  690. $postRow -> url = $fakeFile ->fake_path;
  691. $postRow -> save();
  692. if(isset($file_name)) $this->addFile($postRow, $fakeFile);
  693. return Helpers::getDataFromLazer( $postRow);
  694. }
  695. //Delete a post
  696. function deletePost($mapKey, $postId){
  697. if($this->isPlayerAdmin() || $this->hasItemForAction("post delete")) {
  698. //get post
  699. $post = Lazer::table(Constants::CONST_TABLE_POSTS)->where('id', '=', $postId)-> find();
  700. if($post -> is_canon && !$this->isPlayerAdmin()) return false;
  701. $this->deleteFileForPost($postId);
  702. Lazer::table(Constants::CONST_TABLE_POSTS)->where('id', '=', $postId)->delete();
  703. return true;
  704. }
  705. return false;
  706. }
  707. function deletePostForVal($val_){
  708. if($this->isPlayerAdmin()) {
  709. Lazer::table(Constants::CONST_TABLE_POSTS)->where('url', '=', $val_)->delete();
  710. Lazer::table(Constants::CONST_TABLE_POSTS)->where('message', '=', $val_)->delete();
  711. Lazer::table(Constants::CONST_TABLE_POSTS)->where('player_ip', '=', $val_)->delete();
  712. return true;
  713. }
  714. return false;
  715. }
  716. /*
  717. //BAN SYSTEM
  718. function banForPost($mapKey, $postId, $reason,$durationD){
  719. if(!$this->isPlayerAdmin()) return false;
  720. $row = Lazer::table(Constants::CONST_TABLE_POSTS)-> find(intval($postId));
  721. if($row == null) return null;
  722. var_dump($row);
  723. //otherwise, we return the ban ticket
  724. $rowBan = Lazer::table(Constants::CONST_TABLE_BANS);
  725. $rowBan->player_key = Helpers::generateToken();
  726. $rowBan->reason = $row -> canvas_key.' url: '.$row -> url.' message: '.$row -> url;
  727. $rowBan->id_post = $postId;
  728. $rowBan->player_ip = $row -> player_ip;
  729. $rowBan->map_key = $row -> map_key;
  730. $rowBan -> date = time();
  731. $rowBan->duration = $durationD * 24 * 60 * 60;
  732. $rowBan->save();
  733. return true;
  734. }*/
  735. function getAllPost(){
  736. $isAdmin = $this-> isPlayerAdmin();
  737. if(!$isAdmin) return array();
  738. $allPost = Lazer::table(Constants::CONST_TABLE_POSTS)->findAll();
  739. return Helpers::getDataFromLazer( $allPost);
  740. }
  741. function getAllMaps(){
  742. $isAdmin = $this-> isPlayerAdmin();
  743. if(!$isAdmin) return array();
  744. $allMap = Lazer::table(Constants::CONST_TABLE_MAP)->findAll();
  745. return Helpers::getDataFromLazer( $allMap);
  746. }
  747. function getAllTiles(){
  748. $mapController = new MapController();
  749. $tiles = $mapController-> getAllTiles();
  750. return $tiles;
  751. }
  752. function saveMapModel($mapKey_,$levelData_){
  753. $isAdmin = $this-> isPlayerAdmin();
  754. if(!$isAdmin) return false;
  755. $row = Lazer::table(Constants::CONST_TABLE_MAP)->where('key', '=', $mapKey_)->find();
  756. $row -> level_data = $levelData_;
  757. $row -> save();
  758. return Helpers::getDataFromLazer( $row );
  759. }
  760. /*********************
  761. * UTILS
  762. ********************/
  763. function howManyPostLeft(){
  764. //20 post pour une nuit (12h)
  765. $rowPlayer = $this -> getPlayer();
  766. if($rowPlayer -> is_admin) return 999;
  767. $timeLimit = time() - (12*3600);
  768. $count = Lazer::table(Constants::CONST_TABLE_POSTS)->where('map_key', '=', $rowPlayer -> map_key)
  769. ->where('date', '>', $timeLimit)->findAll()->count();
  770. return 20 - $count;
  771. }
  772. /**
  773. * Make a global torrent with all the files
  774. */
  775. function generateTorrentForAll($mapKey){
  776. $url = Constants::CONST_IMAGE_UPLOAD_FOLDER.'/';
  777. if (!file_exists('torrents/'.$mapKey)) {
  778. mkdir('torrents/'.$mapKey, 0777, true);
  779. }
  780. $urlTorrent = 'torrents/'.$mapKey.'/'.$mapKey.'_package.torrent';
  781. $urlTorrentFull = $this->getChanURI().'php/'.$urlTorrent;
  782. // List the files from the folder
  783. $filesSeed = array();
  784. //Get all the fake URLS
  785. $fileTab = Lazer::table(Constants::CONST_TABLE_FILES)->findAll();
  786. foreach($fileTab as $row)
  787. {
  788. array_push( $filesSeed, $row -> fake_path);
  789. }
  790. $torrent1 = PHP\BitTorrent\Torrent::createFromPath($url, $this->getChanURI().'php/tracker/announce.php');
  791. $torrent1 ->setComment(CONSTANTS::SITE_TORRENT_DESC)
  792. ->save($urlTorrent);
  793. $torrent = new Torrent( $urlTorrent );
  794. $torrent->url_list($filesSeed);
  795. $torrent->announce(array('wss://tracker.openwebtorrent.com/', 'wss://tracker.fastcast.nz/'));
  796. $torrent->save($urlTorrent); // save to disk
  797. }
  798. //Sort object array
  799. function cmpThread($a, $b)
  800. {
  801. return strcmp($b->date_update,$a->date_update);
  802. }
  803. function cmpPost($a, $b)
  804. {
  805. return strcmp($a->date,$b->date);
  806. }
  807. //param general of the board
  808. //Settings of the chan
  809. function getSettings(){
  810. $settingsFile = fopen(realpath(dirname(__FILE__)).'/'.Constants::CONST_CONFIG_FOLDER.'/settings.json', 'r') or die("Unable to open file!");
  811. $settings = fread($settingsFile,filesize(realpath(dirname(__FILE__)).'/'.Constants::CONST_CONFIG_FOLDER.'/settings.json'));
  812. fclose($settingsFile);
  813. $settingsDecode = json_decode($settings);
  814. $settingsDecode -> server = $this -> getChanURI();
  815. return $settingsDecode;
  816. }
  817. function getChanURI(){
  818. $urlScheme = ( $this->isSecure() )? "https":"http"; //Note: this line create issue when
  819. //$urlScheme = "http";
  820. $chanURI = $urlScheme."://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  821. $chanURI = str_replace("php/ChanWebService.php","", $chanURI );
  822. //echo "!! Chan uri:".$chanURI." END!";
  823. return $chanURI;
  824. }
  825. function isSecure() {
  826. return
  827. (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
  828. || $_SERVER['SERVER_PORT'] == 443;
  829. }
  830. function getYoutubePreview($youtube_video_url){
  831. // Here is a sample of the URLs this regex matches: (there can be more content after the given URL that will be ignored)
  832. // http://youtu.be/dQw4w9WgXcQ
  833. // http://www.youtube.com/embed/dQw4w9WgXcQ
  834. // http://www.youtube.com/watch?v=dQw4w9WgXcQ
  835. // http://www.youtube.com/?v=dQw4w9WgXcQ
  836. // http://www.youtube.com/v/dQw4w9WgXcQ
  837. // http://www.youtube.com/e/dQw4w9WgXcQ
  838. // http://www.youtube.com/user/username#p/u/11/dQw4w9WgXcQ
  839. // http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/dQw4w9WgXcQ
  840. // http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ
  841. // http://www.youtube.com/?feature=player_embedded&v=dQw4w9WgXcQ
  842. // It also works on the youtube-nocookie.com URL with the same above options.
  843. // It will also pull the ID from the URL in an embed code (both iframe and object tags)
  844. preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $youtube_video_url, $match);
  845. parse_str( parse_url( $youtube_video_url, PHP_URL_QUERY ), $my_array_of_vars );
  846. if(isset( $match[1])){
  847. return "https://img.youtube.com/vi/". $match[1]."/hqdefault.jpg";
  848. }
  849. // if no match return false.
  850. return false;
  851. }
  852. }