PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/GameEngine/Message.php

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