PageRenderTime 62ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/include/class.message.php

https://bitbucket.org/icarito/pmc
PHP | 384 lines | 282 code | 43 blank | 59 comment | 24 complexity | acb7db80b59cd4aa46c730c77553b358 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * The class provides methods for the realization of messages and replies.
  4. *
  5. * @author Open Dynamics <info@o-dyn.de>
  6. * @name message
  7. * @version 0.4.6
  8. * @package Collabtive
  9. * @link http://www.o-dyn.de
  10. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License v3 or later
  11. */
  12. class message
  13. {
  14. public $mylog;
  15. /**
  16. * Konstruktor
  17. * Initialisiert den Eventlog
  18. */
  19. function __construct()
  20. {
  21. $this->mylog = new mylog;
  22. }
  23. /**
  24. * Creates a new message or a reply to a message
  25. *
  26. * @param int $project Project ID the message belongs to
  27. * @param string $title Title/Subject of the message
  28. * @param string $text Textbody of the message
  29. * @param string $tags Tags for the message
  30. * @param int $user User ID of the user adding the message
  31. * @param string $username Name of the user adding the message
  32. * @param int $replyto ID of the message this message is replying to. Standardmessage: 0
  33. * @return bool
  34. */
  35. function add($project, $title, $text, $tags, $user, $username, $replyto, $milestone)
  36. {
  37. $project = (int) $project;
  38. $title = mysql_real_escape_string($title);
  39. $text = mysql_real_escape_string($text);
  40. $tags = mysql_real_escape_string($tags);
  41. $user = (int) $user;
  42. $username = mysql_real_escape_string($username);
  43. $replyto = (int) $replyto;
  44. $milestone = (int) $milestone;
  45. $posted = time();
  46. $sql = "INSERT INTO messages (`project`,`title`,`text`,`tags`,`posted`,`user`,`username`,`replyto`,`milestone`) VALUES ($project,'$title','$text','$tags','$posted',$user,'$username',$replyto,$milestone) ";
  47. $ins = mysql_query($sql);
  48. $insid = mysql_insert_id();
  49. if ($ins)
  50. {
  51. $this->mylog->add($title, 'message', 1, $project);
  52. return $insid;
  53. }
  54. else
  55. {
  56. return false;
  57. }
  58. }
  59. /**
  60. * Edits a message
  61. *
  62. * @param int $id Eindeutige Nummer der Nachricht
  63. * @param string $title Titel der Nachricht
  64. * @param string $text Text der Nachricht
  65. * @param string $tags Tags for the message
  66. * @return bool
  67. */
  68. function edit($id, $title, $text, $tags)
  69. {
  70. $id = (int) $id;
  71. $title = mysql_real_escape_string($title);
  72. $text = mysql_real_escape_string($text);
  73. $tags = mysql_real_escape_string($tags);
  74. $upd = mysql_query("UPDATE messages SET title='$title', text='$text', tags='$tags' WHERE ID = $id");
  75. if ($upd)
  76. {
  77. $proj = mysql_query("SELECT project FROM messages WHERE ID = $id");
  78. $proj = mysql_fetch_row($proj);
  79. $proj = $proj[0];
  80. $this->mylog->add($title, 'message', 2, $proj);
  81. return true;
  82. }
  83. else
  84. {
  85. return false;
  86. }
  87. }
  88. /**
  89. * Deletes a message and all dependent messages
  90. *
  91. * @param int $id Eindeutige Nummer der Nachricht
  92. * @return bool
  93. */
  94. function del($id)
  95. {
  96. $id = (int) $id;
  97. $msg = mysql_query("SELECT title,project FROM messages WHERE ID = $id");
  98. $msg = mysql_fetch_row($msg);
  99. $del = mysql_query("DELETE FROM messages WHERE ID = $id LIMIT 1");
  100. $del2 = mysql_query("DELETE FROM messages WHERE replyto = $id");
  101. $del3 = mysql_query("DELETE FROM files_attached WHERE message = $id");
  102. if ($del)
  103. {
  104. $this->mylog->add($msg[0], 'message', 3, $msg[1]);
  105. return true;
  106. }
  107. else
  108. {
  109. return false;
  110. }
  111. }
  112. /**
  113. * Return a message including its answers
  114. *
  115. * @param int $id Eindeutige Nummer der Nachricht
  116. * @return array $message Eigenschaften der Nachricht
  117. */
  118. function getMessage($id)
  119. {
  120. $id = (int) $id;
  121. $sel = mysql_query("SELECT * FROM messages WHERE ID = $id LIMIT 1");
  122. $message = mysql_fetch_array($sel,MYSQL_ASSOC);
  123. $tagobj = new tags();
  124. $milesobj = new milestone();
  125. if (!empty($message))
  126. {
  127. $replies = mysql_query("SELECT COUNT(*) FROM messages WHERE replyto = $id");
  128. $replies = mysql_fetch_row($replies);
  129. $replies = $replies[0];
  130. $user = new user();
  131. $avatar = $user->getAvatar($message["user"]);
  132. $sel = mysql_query("SELECT gender FROM user WHERE ID = $message[user]");
  133. $ds = mysql_fetch_row($sel);
  134. $gender = $ds[0];
  135. $message["gender"] = $gender;
  136. $project = mysql_query("SELECT name FROM projekte WHERE ID = $message[project]");
  137. $project = mysql_fetch_row($project);
  138. $project = $project[0];
  139. $project["name"] = stripslashes($project["name"]);
  140. $message["pname"] = $project;
  141. $posted = date(CL_DATEFORMAT . " - H:i", $message["posted"]);
  142. $message["postdate"] = $posted;
  143. $message["endstring"] = $posted;
  144. $message["replies"] = $replies;
  145. $message["avatar"] = $avatar;
  146. $message["title"] = stripslashes($message["title"]);
  147. $message["text"] = stripslashes($message["text"]);
  148. $message["username"] = stripslashes($message["username"]);
  149. $message["tagsarr"] = $tagobj->splitTagStr($message["tags"]);
  150. $message["tagnum"] = count($message["tagsarr"]);
  151. $attached = $this->getAttachedFiles($message["ID"]);
  152. $message["files"] = $attached;
  153. if ($message["milestone"] > 0)
  154. {
  155. $miles = $milesobj->getMilestone($message["milestone"]);
  156. }
  157. else
  158. {
  159. $miles = array();
  160. }
  161. $message["milestones"] = $miles;
  162. return $message;
  163. }
  164. else
  165. {
  166. return false;
  167. }
  168. }
  169. /**
  170. * Return all answers to a given message
  171. *
  172. * @param int $id Eindeutige Nummer der Nachricht
  173. * @return array $replies Antworten zur Nachricht
  174. */
  175. function getReplies($id)
  176. {
  177. $id = (int) $id;
  178. $sel = mysql_query("SELECT ID FROM messages WHERE replyto = $id ORDER BY posted DESC");
  179. $replies = array();
  180. $tagobj = new tags();
  181. $milesobj = new milestone();
  182. $user = new user();
  183. while ($reply = mysql_fetch_array($sel))
  184. {
  185. if (!empty($reply))
  186. {
  187. $thereply = $this->getMessage($reply["ID"]);
  188. array_push($replies, $thereply);
  189. }
  190. }
  191. if (!empty($replies))
  192. {
  193. return $replies;
  194. }
  195. else
  196. {
  197. return false;
  198. }
  199. }
  200. function getLatestMessages($limit = 5)
  201. {
  202. $limit = (int) $limit;
  203. $userid = $_SESSION["userid"];
  204. $sel3 = mysql_query("SELECT projekt FROM projekte_assigned WHERE user = $userid");
  205. $prstring = "";
  206. while ($upro = mysql_fetch_row($sel3))
  207. {
  208. $projekt = $upro[0];
  209. $prstring .= $projekt . ",";
  210. }
  211. $prstring = substr($prstring, 0, strlen($prstring)-1);
  212. if ($prstring)
  213. {
  214. $sel1 = mysql_query("SELECT ID FROM messages WHERE project IN($prstring) ORDER BY posted DESC LIMIT $limit ");
  215. $messages = array();
  216. $tagobj = new tags();
  217. $milesobj = new milestone();
  218. while ($message = mysql_fetch_array($sel1))
  219. {
  220. $themessage = $this->getMessage($message["ID"]);
  221. array_push($messages, $themessage);
  222. }
  223. }
  224. if (!empty($messages))
  225. {
  226. return $messages;
  227. }
  228. else
  229. {
  230. return false;
  231. }
  232. }
  233. /**
  234. * Returns all messages belonging to a project (without answers)
  235. *
  236. * @param int $project Eindeutige Nummer des Projekts
  237. * @return array $messages Nachrichten zum Projekt
  238. */
  239. function getProjectMessages($project)
  240. {
  241. $project = (int) $project;
  242. $messages = array();
  243. $sel1 = mysql_query("SELECT ID FROM messages WHERE project = $project AND replyto = 0 ORDER BY posted DESC");
  244. $tagobj = new tags();
  245. $milesobj = new milestone();
  246. while ($message = mysql_fetch_array($sel1))
  247. {
  248. $themessage = $this->getMessage($message["ID"]);
  249. array_push($messages, $themessage);
  250. }
  251. if (!empty($messages))
  252. {
  253. return $messages;
  254. }
  255. else
  256. {
  257. return false;
  258. }
  259. }
  260. function attachFile($fid, $mid, $id = 0)
  261. {
  262. $fid = (int) $fid;
  263. $mid = (int) $mid;
  264. $id = (int) $id;
  265. $myfile = new datei();
  266. if ($fid > 0)
  267. {
  268. $ins = mysql_query("INSERT INTO files_attached (ID,file,message) VALUES ('',$fid,$mid)");
  269. }
  270. else
  271. {
  272. $num = $_POST["numfiles"];
  273. $chk = 0;
  274. for($i = 1;$i <= $num;$i++)
  275. {
  276. $fid = $myfile->upload("userfile$i", "files/" . CL_CONFIG . "/$id", $id);
  277. $ins = mysql_query("INSERT INTO files_attached (ID,file,message) VALUES ('',$fid,$mid)");
  278. }
  279. }
  280. if ($ins)
  281. {
  282. return true;
  283. }
  284. else
  285. {
  286. return false;
  287. }
  288. }
  289. private function getAttachedFiles($msg)
  290. {
  291. $msg = (int) $msg;
  292. $files = array();
  293. $sel = mysql_query("SELECT file FROM files_attached WHERE message = $msg");
  294. while ($file = mysql_fetch_row($sel))
  295. {
  296. $sel2 = mysql_query("SELECT * FROM files WHERE ID = $file[0]");
  297. $thisfile = mysql_fetch_array($sel2);
  298. $thisfile["type"] = str_replace("/", "-", $thisfile["type"]);
  299. if (isset($thisfile["desc"]))
  300. {
  301. $thisfile["desc"] = stripslashes($thisfile["desc"]);
  302. }
  303. if (isset($thisfile["tags"]))
  304. {
  305. $thisfile["tags"] = stripslashes($thisfile["tags"]);
  306. }
  307. if (isset($thisfile["title"]))
  308. {
  309. $thisfile["title"] = stripslashes($thisfile["title"]);
  310. }
  311. $set = new settings();
  312. $settings = $set->getSettings();
  313. $myfile = "./templates/" . $settings["template"] . "/img/symbols/files/" . $thisfile["type"] . ".png";
  314. if (stristr($thisfile["type"], "image"))
  315. {
  316. $thisfile["imgfile"] = 1;
  317. } elseif (stristr($thisfile["type"], "text"))
  318. {
  319. $thisfile["imgfile"] = 2;
  320. }
  321. else
  322. {
  323. $thisfile["imgfile"] = 0;
  324. }
  325. if (!file_exists($myfile))
  326. {
  327. $thisfile["type"] = "none";
  328. }
  329. array_push($files, $thisfile);
  330. }
  331. if (!empty($files))
  332. {
  333. return $files;
  334. }
  335. else
  336. {
  337. return false;
  338. }
  339. }
  340. }
  341. ?>