PageRenderTime 24ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/GameEngine/Message.php

https://github.com/advocaite/Travianx
PHP | 393 lines | 341 code | 38 blank | 14 comment | 87 complexity | f3ca91ecea7327da2da69af87348e79b MD5 | raw file
  1. <?php
  2. /** --------------------------------------------------- **\
  3. | ********* DO NOT REMOVE THIS COPYRIGHT NOTICE ********* |
  4. +---------------------------------------------------------+
  5. | Credits: All the developers including the leaders: |
  6. | Advocaite & Dzoki & Donnchadh |
  7. | |
  8. | Copyright: TravianX Project All rights reserved |
  9. \** --------------------------------------------------- **/
  10. class Message {
  11. public $unread, $nunread = false;
  12. public $note;
  13. public $inbox, $sent, $reading, $reply, $archived, $noticearray, $readingNotice = array();
  14. private $totalMessage, $totalNotice;
  15. private $allNotice = array();
  16. function Message() {
  17. $this->getMessages();
  18. $this->getNotice();
  19. if($this->totalMessage > 0) {
  20. $this->unread = $this->checkUnread();
  21. }
  22. if($this->totalNotice > 0) {
  23. $this->nunread = $this->checkNUnread();
  24. }
  25. if(isset($_SESSION['reply'])) {
  26. $this->reply = $_SESSION['reply'];
  27. unset($_SESSION['reply']);
  28. }
  29. }
  30. public function procMessage($post) {
  31. if(isset($post['ft'])) {
  32. switch($post['ft']) {
  33. case "m1":
  34. $this->quoteMessage($post['id']);
  35. break;
  36. case "m2":
  37. if ($post['an'] == "[ally]"){
  38. $this->sendAMessage($post['an'],$post['be'],$post['message']);
  39. }else{
  40. $this->sendMessage($post['an'],$post['be'],$post['message']);
  41. }header("Location: nachrichten.php?t=2");
  42. break;
  43. case "m3":
  44. case "m4":
  45. case "m5":
  46. if(isset($post['delmsg_x'])) {
  47. $this->removeMessage($post);
  48. }
  49. if(isset($post['archive_x'])) {
  50. $this->archiveMessage($post);
  51. }
  52. if(isset($post['start_x'])) {
  53. $this->unarchiveMessage($post);
  54. }
  55. break;
  56. case "m6":
  57. $this->createNote($post);
  58. break;
  59. }
  60. }
  61. }
  62. public function noticeType($get) {
  63. global $session, $database;
  64. if(isset($get['t'])) {
  65. if($get['t'] == 1) {
  66. $type = array(8, 15);
  67. }
  68. if($get['t'] == 2) {
  69. $type = array(10, 11, 12, 13);
  70. }
  71. if($get['t'] == 3) {
  72. $type = array(1, 2, 3, 4, 5, 6, 7);
  73. }
  74. if($get['t'] == 5) {
  75. if(!$session->plus){
  76. header("Location: berichte.php");
  77. } else {
  78. $type = 9;
  79. }
  80. }
  81. if (!is_array($type)) { $type = array($type); }
  82. $this->noticearray = $this->filter_by_value($database->getNotice($session->uid), "ntype", $type);
  83. }
  84. if(isset($get['id'])) {
  85. $this->readingNotice = $this->getReadNotice($get['id']);
  86. }
  87. }
  88. public function procNotice($post) {
  89. if(isset($post["del_x"])) {
  90. $this->removeNotice($post);
  91. }
  92. if(isset($post['archive_x'])) {
  93. $this->archiveNotice($post);
  94. }
  95. if(isset($post['start_x'])) {
  96. $this->unarchiveNotice($post);
  97. }
  98. }
  99. public function quoteMessage($id) {
  100. foreach($this->inbox as $message) {
  101. if($message['id'] == $id) {
  102. $this->reply = $_SESSION['reply'] = $message;
  103. header("Location: nachrichten.php?t=1&id=" . $message['owner']);
  104. }
  105. }
  106. }
  107. public function loadMessage($id) {
  108. global $database, $session;
  109. if($this->findInbox($id)) {
  110. foreach($this->inbox as $message) {
  111. if($message['id'] == $id) {
  112. $this->reading = $message;
  113. }
  114. }
  115. }
  116. if($this->findSent($id)) {
  117. foreach($this->sent as $message) {
  118. if($message['id'] == $id) {
  119. $this->reading = $message;
  120. }
  121. }
  122. }
  123. if($session->plus && $this->findArchive($id)) {
  124. foreach($this->archived as $message) {
  125. if($message['id'] == $id) {
  126. $this->reading = $message;
  127. }
  128. }
  129. }
  130. if($this->reading['viewed'] == 0) {
  131. $database->getMessage($id, 4);
  132. }
  133. }
  134. private function filter_by_value_except($array, $index, $value) {
  135. $newarray = array();
  136. if(is_array($array) && count($array) > 0) {
  137. foreach(array_keys($array) as $key) {
  138. $temp[$key] = $array[$key][$index];
  139. if($temp[$key] != $value) {
  140. array_push($newarray, $array[$key]);
  141. //$newarray[$key] = $array[$key];
  142. }
  143. }
  144. }
  145. return $newarray;
  146. }
  147. private function filter_by_value($array, $index, $value) {
  148. $newarray = array();
  149. if(is_array($array) && count($array) > 0) {
  150. foreach(array_keys($array) as $key) {
  151. $temp[$key] = $array[$key][$index];
  152. if(in_array($temp[$key], $value)) {
  153. array_push($newarray, $array[$key]);
  154. //$newarray[$key] = $array[$key];
  155. }
  156. }
  157. }
  158. return $newarray;
  159. }
  160. private function getNotice() {
  161. global $database, $session;
  162. $this->allNotice = $database->getNotice($session->uid);
  163. $this->noticearray = $this->filter_by_value_except($this->allNotice, "ntype", 9);
  164. $this->totalNotice = count($this->allNotice);
  165. }
  166. private function removeMessage($post) {
  167. global $database;
  168. for($i = 1; $i <= 10; $i++) {
  169. if(isset($post['n' . $i])) {
  170. $database->getMessage($post['n' . $i], 5);
  171. }
  172. }
  173. header("Location: nachrichten.php");
  174. }
  175. private function archiveMessage($post) {
  176. global $database;
  177. for($i = 1; $i <= 10; $i++) {
  178. if(isset($post['n' . $i])) {
  179. $database->setArchived($post['n' . $i]);
  180. }
  181. }
  182. header("Location: nachrichten.php");
  183. }
  184. private function unarchiveMessage($post) {
  185. global $database;
  186. for($i = 1; $i <= 10; $i++) {
  187. if(isset($post['n' . $i])) {
  188. $database->setNorm($post['n' . $i]);
  189. }
  190. }
  191. header("Location: nachrichten.php");
  192. }
  193. private function removeNotice($post) {
  194. global $database;
  195. for($i = 1; $i <= 10; $i++) {
  196. if(isset($post['n' . $i])) {
  197. $database->removeNotice($post['n' . $i], 5);
  198. }
  199. }
  200. header("Location: berichte.php");
  201. }
  202. private function archiveNotice($post) {
  203. global $database;
  204. for($i = 1; $i <= 10; $i++) {
  205. if(isset($post['n' . $i])) {
  206. $database->archiveNotice($post['n' . $i]);
  207. }
  208. }
  209. header("Location: berichte.php");
  210. }
  211. private function unarchiveNotice($post) {
  212. global $database;
  213. for($i = 1; $i <= 10; $i++) {
  214. if(isset($post['n' . $i])) {
  215. $database->unarchiveNotice($post['n' . $i]);
  216. }
  217. }
  218. header("Location: berichte.php");
  219. }
  220. private function getReadNotice($id) {
  221. global $database;
  222. foreach($this->allNotice as $notice) {
  223. if($notice['id'] == $id) {
  224. $database->noticeViewed($notice['id']);
  225. return $notice;
  226. }
  227. }
  228. }
  229. public function loadNotes() {
  230. global $session;
  231. if(file_exists("GameEngine/Notes/" . md5($session->username) . ".txt")) {
  232. $this->note = file_get_contents("GameEngine/Notes/" . md5($session->username) . ".txt");
  233. } else {
  234. $this->note = "";
  235. }
  236. }
  237. private function createNote($post) {
  238. global $session;
  239. if($session->plus) {
  240. $ourFileHandle = fopen("GameEngine/Notes/" . md5($session->username) . ".txt", 'w');
  241. fwrite($ourFileHandle, $post['notizen']);
  242. fclose($ourFileHandle);
  243. }
  244. }
  245. private function getMessages() {
  246. global $database, $session;
  247. $this->inbox = $database->getMessage($session->uid, 1);
  248. $this->sent = $database->getMessage($session->uid, 2);
  249. if($session->plus) {
  250. $this->archived = $database->getMessage($session->uid, 6);
  251. }
  252. $this->totalMessage = count($this->inbox) + count($this->sent);
  253. }
  254. private function sendAMessage($recieve,$topic,$text) {
  255. global $session,$database;
  256. $allmembersQ = mysql_query("SELECT id FROM ".TB_PREFIX."users WHERE alliance='".$session->alliance."'");
  257. $userally = $database->getUserField($recieve,"alliance",1);
  258. $permission=mysql_fetch_array(mysql_query("SELECT opt7 FROM ".TB_PREFIX."ali_permission WHERE uid='".$session->uid."'"));
  259. if(WORD_CENSOR) {
  260. $topic = $this->wordCensor($topic);
  261. $text = $this->wordCensor($text);
  262. }
  263. if($topic == "") {
  264. $topic = "No subject";
  265. }
  266. if($permission[opt7]==1){
  267. if ($userally != 0) {
  268. while ($allmembers = mysql_fetch_array($allmembersQ)) {
  269. $database->sendMessage($allmembers[id],$session->uid,$topic,$text,0);
  270. }
  271. }
  272. }
  273. }
  274. private function sendMessage($recieve, $topic, $text) {
  275. global $session, $database;
  276. $user = $database->getUserField($recieve, "id", 1);
  277. if(WORD_CENSOR) {
  278. $topic = $this->wordCensor($topic);
  279. $text = $this->wordCensor($text);
  280. }
  281. if($topic == "") {
  282. $topic = "No subject";
  283. }
  284. //if to multihunter
  285. if($user == "0") {
  286. //make mail server
  287. $database->sendMessage($user, $session->uid, $topic, $text, 0);
  288. } else {
  289. $database->sendMessage($user, $session->uid, $topic, $text, 0);
  290. }
  291. }
  292. //7 = village, attacker, att tribe, u1 - u10, lost %, w,c,i,c , cap
  293. //8 = village, attacker, att tribe, enforcement
  294. private function sendNotice($from, $vid, $fowner, $owner, $type, $extra) {
  295. }
  296. public function sendWelcome($uid, $username) {
  297. global $database;
  298. $welcomemsg = file_get_contents("GameEngine/Admin/welcome.tpl");
  299. $welcomemsg = preg_replace("'%USER%'", $username, $welcomemsg);
  300. $welcomemsg = preg_replace("'%START%'", date("y.m.d", COMMENCE), $welcomemsg);
  301. $welcomemsg = preg_replace("'%TIME%'", date("H:i", COMMENCE), $welcomemsg);
  302. $welcomemsg = preg_replace("'%PLAYERS%'", $database->countUser(), $welcomemsg);
  303. $welcomemsg = preg_replace("'%ALLI%'", $database->countAlli(), $welcomemsg);
  304. return $database->sendMessage($uid, 0, WEL_TOPIC, $welcomemsg, 0);
  305. }
  306. private function wordCensor($text) {
  307. $censorarray = explode(",", CENSORED);
  308. foreach($censorarray as $key => $value) {
  309. $censorarray[$key] = "/" . $value . "/i";
  310. }
  311. return preg_replace($censorarray, "****", $text);
  312. }
  313. private function checkUnread() {
  314. foreach($this->inbox as $message) {
  315. if($message['viewed'] == 0) {
  316. return true;
  317. }
  318. }
  319. return false;
  320. }
  321. private function checkNUnread() {
  322. foreach($this->allNotice as $notice) {
  323. if($notice['viewed'] == 0) {
  324. return true;
  325. }
  326. }
  327. return false;
  328. }
  329. private function findInbox($id) {
  330. foreach($this->inbox as $message) {
  331. if($message['id'] == $id) {
  332. return true;
  333. }
  334. }
  335. return false;
  336. }
  337. private function findSent($id) {
  338. foreach($this->sent as $message) {
  339. if($message['id'] == $id) {
  340. return true;
  341. }
  342. }
  343. return false;
  344. }
  345. private function findArchive($id) {
  346. foreach($this->archived as $message) {
  347. if($message['id'] == $id) {
  348. return true;
  349. }
  350. }
  351. return false;
  352. }
  353. }
  354. ;
  355. ?>