message.lib.php 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\Message;
  4. use Chamilo\UserBundle\Entity\User;
  5. use ChamiloSession as Session;
  6. /**
  7. * Class MessageManager.
  8. *
  9. * This class provides methods for messages management.
  10. * Include/require it in your code to use its features.
  11. *
  12. * @package chamilo.library
  13. */
  14. class MessageManager
  15. {
  16. const MESSAGE_TYPE_INBOX = 1;
  17. const MESSAGE_TYPE_OUTBOX = 2;
  18. const MESSAGE_TYPE_PROMOTED = 3;
  19. /**
  20. * Get count new messages for the current user from the database.
  21. *
  22. * @return int
  23. */
  24. public static function getCountNewMessages()
  25. {
  26. $userId = api_get_user_id();
  27. if (empty($userId)) {
  28. return false;
  29. }
  30. static $count;
  31. if (!isset($count)) {
  32. $cacheAvailable = api_get_configuration_value('apc');
  33. if ($cacheAvailable === true) {
  34. $var = api_get_configuration_value('apc_prefix').'social_messages_unread_u_'.$userId;
  35. if (apcu_exists($var)) {
  36. $count = apcu_fetch($var);
  37. } else {
  38. $count = self::getCountNewMessagesFromDB($userId);
  39. apcu_store($var, $count, 60);
  40. }
  41. } else {
  42. $count = self::getCountNewMessagesFromDB($userId);
  43. }
  44. }
  45. return $count;
  46. }
  47. /**
  48. * Gets the total number of messages, used for the inbox sortable table.
  49. *
  50. * @param array $params
  51. *
  52. * @return int
  53. */
  54. public static function getNumberOfMessages($params)
  55. {
  56. $table = Database::get_main_table(TABLE_MESSAGE);
  57. $conditions = self::getWhereConditions($params);
  58. $sql = "SELECT COUNT(id) as number_messages
  59. FROM $table
  60. WHERE
  61. $conditions
  62. ";
  63. $result = Database::query($sql);
  64. $result = Database::fetch_array($result);
  65. if ($result) {
  66. return (int) $result['number_messages'];
  67. }
  68. return 0;
  69. }
  70. /**
  71. * @param array $extraParams
  72. *
  73. * @return string
  74. */
  75. public static function getWhereConditions($extraParams)
  76. {
  77. $userId = api_get_user_id();
  78. $keyword = isset($extraParams['keyword']) && !empty($extraParams['keyword']) ? $extraParams['keyword'] : '';
  79. $type = isset($extraParams['type']) && !empty($extraParams['type']) ? $extraParams['type'] : '';
  80. if (empty($type)) {
  81. return '';
  82. }
  83. switch ($type) {
  84. case self::MESSAGE_TYPE_INBOX:
  85. $statusList = [MESSAGE_STATUS_NEW, MESSAGE_STATUS_UNREAD];
  86. $userCondition = " user_receiver_id = $userId AND";
  87. break;
  88. case self::MESSAGE_TYPE_OUTBOX:
  89. $statusList = [MESSAGE_STATUS_OUTBOX];
  90. $userCondition = " user_sender_id = $userId AND";
  91. break;
  92. case self::MESSAGE_TYPE_PROMOTED:
  93. $statusList = [MESSAGE_STATUS_PROMOTED];
  94. $userCondition = " user_receiver_id = $userId AND";
  95. break;
  96. }
  97. if (empty($statusList)) {
  98. return '';
  99. }
  100. $keywordCondition = '';
  101. if (!empty($keyword)) {
  102. $keyword = Database::escape_string($keyword);
  103. $keywordCondition = " AND (title like '%$keyword%' OR content LIKE '%$keyword%') ";
  104. }
  105. $messageStatusCondition = implode("','", $statusList);
  106. return " $userCondition
  107. msg_status IN ('$messageStatusCondition')
  108. $keywordCondition";
  109. }
  110. /**
  111. * Gets information about some messages, used for the inbox sortable table.
  112. *
  113. * @param int $from
  114. * @param int $numberOfItems
  115. * @param string $column
  116. * @param string $direction
  117. * @param array $extraParams
  118. *
  119. * @return array
  120. */
  121. public static function getMessageData(
  122. $from,
  123. $numberOfItems,
  124. $column,
  125. $direction,
  126. $extraParams = []
  127. ) {
  128. $from = (int) $from;
  129. $numberOfItems = (int) $numberOfItems;
  130. // Forcing this order.
  131. if (!isset($direction)) {
  132. $column = 2;
  133. $direction = 'DESC';
  134. } else {
  135. $column = (int) $column;
  136. if (!in_array($direction, ['ASC', 'DESC'])) {
  137. $direction = 'ASC';
  138. }
  139. }
  140. if (!in_array($column, [0, 1, 2])) {
  141. $column = 2;
  142. }
  143. $type = isset($extraParams['type']) && !empty($extraParams['type']) ? $extraParams['type'] : '';
  144. if (empty($type)) {
  145. return [];
  146. }
  147. $viewUrl = '';
  148. switch ($type) {
  149. case self::MESSAGE_TYPE_OUTBOX:
  150. case self::MESSAGE_TYPE_INBOX:
  151. $viewUrl = api_get_path(WEB_CODE_PATH).'messages/view_message.php';
  152. break;
  153. case self::MESSAGE_TYPE_PROMOTED:
  154. $viewUrl = api_get_path(WEB_CODE_PATH).'social/view_promoted_message.php';
  155. break;
  156. }
  157. $viewUrl .= '?type='.$type;
  158. $whereConditions = self::getWhereConditions($extraParams);
  159. if (empty($whereConditions)) {
  160. return [];
  161. }
  162. $table = Database::get_main_table(TABLE_MESSAGE);
  163. $sql = "SELECT
  164. id as col0,
  165. title as col1,
  166. send_date as col2,
  167. msg_status as col3,
  168. user_sender_id
  169. FROM $table
  170. WHERE
  171. $whereConditions
  172. ORDER BY col$column $direction
  173. LIMIT $from, $numberOfItems";
  174. $result = Database::query($sql);
  175. $messageList = [];
  176. $newMessageLink = api_get_path(WEB_CODE_PATH).'messages/new_message.php';
  177. $actions = $extraParams['actions'];
  178. $url = api_get_self();
  179. while ($row = Database::fetch_array($result, 'ASSOC')) {
  180. $messageId = $row['col0'];
  181. $title = $row['col1'];
  182. $sendDate = $row['col2'];
  183. $status = $row['col3'];
  184. $senderId = $row['user_sender_id'];
  185. $title = Security::remove_XSS($title, STUDENT, true);
  186. $title = cut($title, 80, true);
  187. $class = 'class = "read"';
  188. if ($status == 1) {
  189. $class = 'class = "unread"';
  190. }
  191. $userInfo = api_get_user_info($senderId);
  192. $message[3] = '';
  193. if (!empty($senderId) && !empty($userInfo)) {
  194. $message[1] = '<a '.$class.' href="'.$viewUrl.'&id='.$messageId.'">'.$title.'</a><br />';
  195. $message[1] .= $userInfo['complete_name_with_username'];
  196. if (in_array('reply', $actions)) {
  197. $message[3] =
  198. Display::url(
  199. Display::returnFontAwesomeIcon('reply', 2),
  200. $newMessageLink.'?re_id='.$messageId,
  201. ['title' => get_lang('ReplyToMessage')]
  202. );
  203. }
  204. } else {
  205. $message[1] = '<a '.$class.' href="'.$viewUrl.'&id='.$messageId.'">'.$title.'</a><br />';
  206. $message[1] .= get_lang('UnknownUser');
  207. if (in_array('reply', $actions)) {
  208. $message[3] =
  209. Display::url(
  210. Display::returnFontAwesomeIcon('reply', 2),
  211. '#',
  212. ['title' => get_lang('ReplyToMessage')]
  213. );
  214. }
  215. }
  216. $message[0] = $messageId;
  217. $message[2] = api_convert_and_format_date($sendDate, DATE_TIME_FORMAT_LONG);
  218. // Actions
  219. if (in_array('edit', $actions)) {
  220. $message[3] .=
  221. '&nbsp;&nbsp;'.
  222. Display::url(
  223. Display::returnFontAwesomeIcon('pencil', 2),
  224. $newMessageLink.'?action=edit&id='.$messageId,
  225. ['title' => get_lang('ForwardMessage')]
  226. );
  227. }
  228. // Actions
  229. if (in_array('forward', $actions)) {
  230. $message[3] .=
  231. '&nbsp;&nbsp;'.
  232. Display::url(
  233. Display::returnFontAwesomeIcon('share', 2),
  234. $newMessageLink.'?forward_id='.$messageId,
  235. ['title' => get_lang('ForwardMessage')]
  236. );
  237. }
  238. if (in_array('delete', $actions)) {
  239. $message[3] .= '&nbsp;&nbsp;<a title="'.addslashes(
  240. get_lang('DeleteMessage')
  241. ).'" onclick="javascript:if(!confirm('."'".addslashes(
  242. api_htmlentities(get_lang('ConfirmDeleteMessage'))
  243. )."'".')) return false;" href="'.$url.'?action=deleteone&id='.$messageId.'">'.
  244. Display::returnFontAwesomeIcon('trash', 2).'</a>';
  245. }
  246. foreach ($message as $key => $value) {
  247. $message[$key] = api_xml_http_response_encode($value);
  248. }
  249. $messageList[] = $message;
  250. }
  251. return $messageList;
  252. }
  253. /**
  254. * @param array $aboutUserInfo
  255. * @param array $fromUserInfo
  256. * @param string $subject
  257. * @param string $content
  258. *
  259. * @return bool
  260. */
  261. public static function sendMessageAboutUser(
  262. $aboutUserInfo,
  263. $fromUserInfo,
  264. $subject,
  265. $content
  266. ) {
  267. if (empty($aboutUserInfo) || empty($fromUserInfo)) {
  268. return false;
  269. }
  270. if (empty($fromUserInfo['id']) || empty($aboutUserInfo['id'])) {
  271. return false;
  272. }
  273. $table = Database::get_main_table(TABLE_MESSAGE);
  274. $now = api_get_utc_datetime();
  275. $params = [
  276. 'user_sender_id' => $fromUserInfo['id'],
  277. 'user_receiver_id' => $aboutUserInfo['id'],
  278. 'msg_status' => MESSAGE_STATUS_CONVERSATION,
  279. 'send_date' => $now,
  280. 'title' => $subject,
  281. 'content' => $content,
  282. 'group_id' => 0,
  283. 'parent_id' => 0,
  284. 'update_date' => $now,
  285. ];
  286. $id = Database::insert($table, $params);
  287. if ($id) {
  288. return true;
  289. }
  290. return false;
  291. }
  292. /**
  293. * @param array $aboutUserInfo
  294. *
  295. * @return array
  296. */
  297. public static function getMessagesAboutUser($aboutUserInfo)
  298. {
  299. if (!empty($aboutUserInfo)) {
  300. $table = Database::get_main_table(TABLE_MESSAGE);
  301. $sql = 'SELECT id FROM '.$table.'
  302. WHERE
  303. user_receiver_id = '.$aboutUserInfo['id'].' AND
  304. msg_status = '.MESSAGE_STATUS_CONVERSATION.'
  305. ';
  306. $result = Database::query($sql);
  307. $messages = [];
  308. $repo = Database::getManager()->getRepository('ChamiloCoreBundle:Message');
  309. while ($row = Database::fetch_array($result)) {
  310. $message = $repo->find($row['id']);
  311. $messages[] = $message;
  312. }
  313. return $messages;
  314. }
  315. return [];
  316. }
  317. /**
  318. * @param array $userInfo
  319. *
  320. * @return string
  321. */
  322. public static function getMessagesAboutUserToString($userInfo)
  323. {
  324. $messages = self::getMessagesAboutUser($userInfo);
  325. $html = '';
  326. if (!empty($messages)) {
  327. /** @var Message $message */
  328. foreach ($messages as $message) {
  329. $tag = 'message_'.$message->getId();
  330. $tagAccordion = 'accordion_'.$message->getId();
  331. $tagCollapse = 'collapse_'.$message->getId();
  332. $date = Display::dateToStringAgoAndLongDate(
  333. $message->getSendDate()
  334. );
  335. $localTime = api_get_local_time(
  336. $message->getSendDate(),
  337. null,
  338. null,
  339. false,
  340. false
  341. );
  342. $senderId = $message->getUserSenderId();
  343. $senderInfo = api_get_user_info($senderId);
  344. $html .= Display::panelCollapse(
  345. $localTime.' '.$senderInfo['complete_name'].' '.$message->getTitle(),
  346. $message->getContent().'<br />'.$date.'<br />'.get_lang(
  347. 'Author'
  348. ).': '.$senderInfo['complete_name_with_message_link'],
  349. $tag,
  350. null,
  351. $tagAccordion,
  352. $tagCollapse,
  353. false
  354. );
  355. }
  356. }
  357. return $html;
  358. }
  359. /**
  360. * @param int $senderId
  361. * @param int $receiverId
  362. * @param string $subject
  363. * @param string $message
  364. *
  365. * @return bool
  366. */
  367. public static function messageWasAlreadySent($senderId, $receiverId, $subject, $message)
  368. {
  369. $table = Database::get_main_table(TABLE_MESSAGE);
  370. $senderId = (int) $senderId;
  371. $receiverId = (int) $receiverId;
  372. $subject = Database::escape_string($subject);
  373. $message = Database::escape_string($message);
  374. $sql = "SELECT * FROM $table
  375. WHERE
  376. user_sender_id = $senderId AND
  377. user_receiver_id = $receiverId AND
  378. title = '$subject' AND
  379. content = '$message' AND
  380. (msg_status = ".MESSAGE_STATUS_UNREAD." OR msg_status = ".MESSAGE_STATUS_NEW.")
  381. ";
  382. $result = Database::query($sql);
  383. return Database::num_rows($result) > 0;
  384. }
  385. /**
  386. * Sends a message to a user/group.
  387. *
  388. * @param int $receiver_user_id
  389. * @param string $subject
  390. * @param string $content
  391. * @param array $attachments files array($_FILES) (optional)
  392. * @param array $fileCommentList about attachment files (optional)
  393. * @param int $group_id (optional)
  394. * @param int $parent_id (optional)
  395. * @param int $editMessageId id for updating the message (optional)
  396. * @param int $topic_id (optional) the default value is the current user_id
  397. * @param int $sender_id
  398. * @param bool $directMessage
  399. * @param int $forwardId
  400. * @param array $smsParameters
  401. * @param bool $checkCurrentAudioId
  402. * @param bool $forceTitleWhenSendingEmail force the use of $title as subject instead of "You have a new message"
  403. *
  404. * @return bool
  405. */
  406. public static function send_message(
  407. $receiver_user_id,
  408. $subject,
  409. $content,
  410. array $attachments = [],
  411. array $fileCommentList = [],
  412. $group_id = 0,
  413. $parent_id = 0,
  414. $editMessageId = 0,
  415. $topic_id = 0,
  416. $sender_id = 0,
  417. $directMessage = false,
  418. $forwardId = 0,
  419. $smsParameters = [],
  420. $checkCurrentAudioId = false,
  421. $forceTitleWhenSendingEmail = false,
  422. $status = 0
  423. ) {
  424. $table = Database::get_main_table(TABLE_MESSAGE);
  425. $group_id = (int) $group_id;
  426. $receiver_user_id = (int) $receiver_user_id;
  427. $parent_id = (int) $parent_id;
  428. $editMessageId = (int) $editMessageId;
  429. $topic_id = (int) $topic_id;
  430. $status = empty($status) ? MESSAGE_STATUS_UNREAD : (int) $status;
  431. if (!empty($receiver_user_id)) {
  432. $receiverUserInfo = api_get_user_info($receiver_user_id);
  433. // Disabling messages for inactive users.
  434. if ($receiverUserInfo['active'] == 0) {
  435. return false;
  436. }
  437. }
  438. $user_sender_id = empty($sender_id) ? api_get_user_id() : (int) $sender_id;
  439. if (empty($user_sender_id)) {
  440. Display::addFlash(Display::return_message(get_lang('UserDoesNotExist'), 'warning'));
  441. return false;
  442. }
  443. $totalFileSize = 0;
  444. $attachmentList = [];
  445. if (is_array($attachments)) {
  446. $counter = 0;
  447. foreach ($attachments as $attachment) {
  448. $attachment['comment'] = isset($fileCommentList[$counter]) ? $fileCommentList[$counter] : '';
  449. $fileSize = isset($attachment['size']) ? $attachment['size'] : 0;
  450. if (is_array($fileSize)) {
  451. foreach ($fileSize as $size) {
  452. $totalFileSize += $size;
  453. }
  454. } else {
  455. $totalFileSize += $fileSize;
  456. }
  457. $attachmentList[] = $attachment;
  458. $counter++;
  459. }
  460. }
  461. if ($checkCurrentAudioId) {
  462. // Add the audio file as an attachment
  463. $audioId = Session::read('current_audio_id');
  464. if (!empty($audioId)) {
  465. $file = api_get_uploaded_file('audio_message', api_get_user_id(), $audioId);
  466. if (!empty($file)) {
  467. $audioAttachment = [
  468. 'name' => basename($file),
  469. 'comment' => 'audio_message',
  470. 'size' => filesize($file),
  471. 'tmp_name' => $file,
  472. 'error' => 0,
  473. 'type' => DocumentManager::file_get_mime_type(basename($file)),
  474. ];
  475. // create attachment from audio message
  476. $attachmentList[] = $audioAttachment;
  477. }
  478. }
  479. }
  480. // Validating fields
  481. if (empty($subject) && empty($group_id)) {
  482. Display::addFlash(
  483. Display::return_message(
  484. get_lang('YouShouldWriteASubject'),
  485. 'warning'
  486. )
  487. );
  488. return false;
  489. } elseif ($totalFileSize > intval(api_get_setting('message_max_upload_filesize'))) {
  490. $warning = sprintf(
  491. get_lang('FilesSizeExceedsX'),
  492. format_file_size(api_get_setting('message_max_upload_filesize'))
  493. );
  494. Display::addFlash(Display::return_message($warning, 'warning'));
  495. return false;
  496. }
  497. // Just in case we replace the and \n and \n\r while saving in the DB
  498. // $content = str_replace(array("\n", "\n\r"), '<br />', $content);
  499. $now = api_get_utc_datetime();
  500. if (!empty($receiver_user_id) || !empty($group_id)) {
  501. // message for user friend
  502. //@todo it's possible to edit a message? yes, only for groups
  503. if (!empty($editMessageId)) {
  504. $query = " UPDATE $table SET
  505. update_date = '".$now."',
  506. content = '".Database::escape_string($content)."'
  507. WHERE id = '$editMessageId' ";
  508. Database::query($query);
  509. $messageId = $editMessageId;
  510. } else {
  511. $params = [
  512. 'user_sender_id' => $user_sender_id,
  513. 'user_receiver_id' => $receiver_user_id,
  514. 'msg_status' => $status,
  515. 'send_date' => $now,
  516. 'title' => $subject,
  517. 'content' => $content,
  518. 'group_id' => $group_id,
  519. 'parent_id' => $parent_id,
  520. 'update_date' => $now,
  521. ];
  522. $messageId = Database::insert($table, $params);
  523. }
  524. // Forward also message attachments
  525. if (!empty($forwardId)) {
  526. $attachments = self::getAttachmentList($forwardId);
  527. foreach ($attachments as $attachment) {
  528. if (!empty($attachment['file_source'])) {
  529. $file = [
  530. 'name' => $attachment['filename'],
  531. 'tmp_name' => $attachment['file_source'],
  532. 'size' => $attachment['size'],
  533. 'error' => 0,
  534. 'comment' => $attachment['comment'],
  535. ];
  536. // Inject this array so files can be added when sending and email with the mailer
  537. $attachmentList[] = $file;
  538. }
  539. }
  540. }
  541. // Save attachment file for inbox messages
  542. if (is_array($attachmentList)) {
  543. foreach ($attachmentList as $attachment) {
  544. if ($attachment['error'] == 0) {
  545. $comment = $attachment['comment'];
  546. self::saveMessageAttachmentFile(
  547. $attachment,
  548. $comment,
  549. $messageId,
  550. null,
  551. $receiver_user_id,
  552. $group_id
  553. );
  554. }
  555. }
  556. }
  557. // Save message in the outbox for user friend or group.
  558. if (empty($group_id) && $status == MESSAGE_STATUS_UNREAD) {
  559. $params = [
  560. 'user_sender_id' => $user_sender_id,
  561. 'user_receiver_id' => $receiver_user_id,
  562. 'msg_status' => MESSAGE_STATUS_OUTBOX,
  563. 'send_date' => $now,
  564. 'title' => $subject,
  565. 'content' => $content,
  566. 'group_id' => $group_id,
  567. 'parent_id' => $parent_id,
  568. 'update_date' => $now,
  569. ];
  570. $outbox_last_id = Database::insert($table, $params);
  571. // save attachment file for outbox messages
  572. if (is_array($attachmentList)) {
  573. foreach ($attachmentList as $attachment) {
  574. if ($attachment['error'] == 0) {
  575. $comment = $attachment['comment'];
  576. self::saveMessageAttachmentFile(
  577. $attachment,
  578. $comment,
  579. $outbox_last_id,
  580. $user_sender_id
  581. );
  582. }
  583. }
  584. }
  585. }
  586. // Load user settings.
  587. $notification = new Notification();
  588. $sender_info = api_get_user_info($user_sender_id);
  589. // add file attachment additional attributes
  590. $attachmentAddedByMail = [];
  591. foreach ($attachmentList as $attachment) {
  592. $attachmentAddedByMail[] = [
  593. 'path' => $attachment['tmp_name'],
  594. 'filename' => $attachment['name'],
  595. ];
  596. }
  597. if (empty($group_id)) {
  598. $type = Notification::NOTIFICATION_TYPE_MESSAGE;
  599. if ($directMessage) {
  600. $type = Notification::NOTIFICATION_TYPE_DIRECT_MESSAGE;
  601. }
  602. $notification->saveNotification(
  603. $messageId,
  604. $type,
  605. [$receiver_user_id],
  606. $subject,
  607. $content,
  608. $sender_info,
  609. $attachmentAddedByMail,
  610. $smsParameters,
  611. $forceTitleWhenSendingEmail
  612. );
  613. } else {
  614. $usergroup = new UserGroup();
  615. $group_info = $usergroup->get($group_id);
  616. $group_info['topic_id'] = $topic_id;
  617. $group_info['msg_id'] = $messageId;
  618. $user_list = $usergroup->get_users_by_group(
  619. $group_id,
  620. false,
  621. [],
  622. 0,
  623. 1000
  624. );
  625. // Adding more sense to the message group
  626. $subject = sprintf(get_lang('ThereIsANewMessageInTheGroupX'), $group_info['name']);
  627. $new_user_list = [];
  628. foreach ($user_list as $user_data) {
  629. $new_user_list[] = $user_data['id'];
  630. }
  631. $group_info = [
  632. 'group_info' => $group_info,
  633. 'user_info' => $sender_info,
  634. ];
  635. $notification->saveNotification(
  636. $messageId,
  637. Notification::NOTIFICATION_TYPE_GROUP,
  638. $new_user_list,
  639. $subject,
  640. $content,
  641. $group_info,
  642. $attachmentAddedByMail,
  643. $smsParameters
  644. );
  645. }
  646. return $messageId;
  647. }
  648. return false;
  649. }
  650. /**
  651. * @param int $receiver_user_id
  652. * @param int $subject
  653. * @param string $message
  654. * @param int $sender_id
  655. * @param bool $sendCopyToDrhUsers send copy to related DRH users
  656. * @param bool $directMessage
  657. * @param array $smsParameters
  658. * @param bool $uploadFiles Do not upload files using the MessageManager class
  659. * @param array $attachmentList
  660. *
  661. * @return bool
  662. */
  663. public static function send_message_simple(
  664. $receiver_user_id,
  665. $subject,
  666. $message,
  667. $sender_id = 0,
  668. $sendCopyToDrhUsers = false,
  669. $directMessage = false,
  670. $smsParameters = [],
  671. $uploadFiles = true,
  672. $attachmentList = []
  673. ) {
  674. $files = $_FILES ? $_FILES : [];
  675. if ($uploadFiles === false) {
  676. $files = [];
  677. }
  678. // $attachmentList must have: tmp_name, name, size keys
  679. if (!empty($attachmentList)) {
  680. $files = $attachmentList;
  681. }
  682. $result = self::send_message(
  683. $receiver_user_id,
  684. $subject,
  685. $message,
  686. $files,
  687. [],
  688. null,
  689. null,
  690. null,
  691. null,
  692. $sender_id,
  693. $directMessage,
  694. 0,
  695. $smsParameters
  696. );
  697. if ($sendCopyToDrhUsers) {
  698. $userInfo = api_get_user_info($receiver_user_id);
  699. $drhList = UserManager::getDrhListFromUser($receiver_user_id);
  700. if (!empty($drhList)) {
  701. foreach ($drhList as $drhInfo) {
  702. $message = sprintf(
  703. get_lang('CopyOfMessageSentToXUser'),
  704. $userInfo['complete_name']
  705. ).' <br />'.$message;
  706. self::send_message_simple(
  707. $drhInfo['user_id'],
  708. $subject,
  709. $message,
  710. $sender_id,
  711. false,
  712. $directMessage
  713. );
  714. }
  715. }
  716. }
  717. return $result;
  718. }
  719. /**
  720. * Update parent ids for other receiver user from current message in groups.
  721. *
  722. * @author Christian Fasanando Flores
  723. *
  724. * @param int $parent_id
  725. * @param int $receiver_user_id
  726. * @param int $messageId
  727. */
  728. public static function update_parent_ids_from_reply(
  729. $parent_id,
  730. $receiver_user_id,
  731. $messageId
  732. ) {
  733. $table = Database::get_main_table(TABLE_MESSAGE);
  734. $parent_id = intval($parent_id);
  735. $receiver_user_id = intval($receiver_user_id);
  736. $messageId = intval($messageId);
  737. // first get data from message id (parent)
  738. $sql = "SELECT * FROM $table WHERE id = '$parent_id'";
  739. $rs_message = Database::query($sql);
  740. $row_message = Database::fetch_array($rs_message);
  741. // get message id from data found early for other receiver user
  742. $sql = "SELECT id FROM $table
  743. WHERE
  744. user_sender_id ='{$row_message['user_sender_id']}' AND
  745. title='{$row_message['title']}' AND
  746. content='{$row_message['content']}' AND
  747. group_id='{$row_message['group_id']}' AND
  748. user_receiver_id='$receiver_user_id'";
  749. $result = Database::query($sql);
  750. $row = Database::fetch_array($result);
  751. // update parent_id for other user receiver
  752. $sql = "UPDATE $table SET parent_id = ".$row['id']."
  753. WHERE id = $messageId";
  754. Database::query($sql);
  755. }
  756. /**
  757. * @param int $user_receiver_id
  758. * @param int $id
  759. *
  760. * @return bool
  761. */
  762. public static function delete_message_by_user_receiver($user_receiver_id, $id)
  763. {
  764. $table = Database::get_main_table(TABLE_MESSAGE);
  765. $id = (int) $id;
  766. $user_receiver_id = (int) $user_receiver_id;
  767. if (empty($id) || empty($user_receiver_id)) {
  768. return false;
  769. }
  770. $sql = "SELECT * FROM $table
  771. WHERE
  772. id = $id AND
  773. user_receiver_id = $user_receiver_id AND
  774. msg_status <> ".MESSAGE_STATUS_OUTBOX;
  775. $rs = Database::query($sql);
  776. if (Database::num_rows($rs) > 0) {
  777. // Delete attachment file.
  778. self::delete_message_attachment_file($id, $user_receiver_id);
  779. // Soft delete message.
  780. $query = "UPDATE $table
  781. SET msg_status = ".MESSAGE_STATUS_DELETED."
  782. WHERE
  783. id = $id AND
  784. user_receiver_id = $user_receiver_id ";
  785. Database::query($query);
  786. return true;
  787. }
  788. return false;
  789. }
  790. /**
  791. * Set status deleted.
  792. *
  793. * @author Isaac FLores Paz <isaac.flores@dokeos.com>
  794. *
  795. * @param int
  796. * @param int
  797. *
  798. * @return bool
  799. */
  800. public static function delete_message_by_user_sender($user_sender_id, $id)
  801. {
  802. $user_sender_id = (int) $user_sender_id;
  803. $id = (int) $id;
  804. if (empty($id) || empty($user_sender_id)) {
  805. return false;
  806. }
  807. $table = Database::get_main_table(TABLE_MESSAGE);
  808. $sql = "SELECT * FROM $table WHERE id = $id AND user_sender_id= $user_sender_id";
  809. $rs = Database::query($sql);
  810. if (Database::num_rows($rs) > 0) {
  811. // delete attachment file
  812. self::delete_message_attachment_file($id, $user_sender_id);
  813. // delete message
  814. $sql = "UPDATE $table
  815. SET msg_status = '".MESSAGE_STATUS_DELETED."'
  816. WHERE user_sender_id= $user_sender_id AND id= $id";
  817. Database::query($sql);
  818. return true;
  819. }
  820. return false;
  821. }
  822. /**
  823. * Saves a message attachment files.
  824. *
  825. * @param array $file_attach $_FILES['name']
  826. * @param string a comment about the uploaded file
  827. * @param int message id
  828. * @param int receiver user id (optional)
  829. * @param int sender user id (optional)
  830. * @param int group id (optional)
  831. */
  832. public static function saveMessageAttachmentFile(
  833. $file_attach,
  834. $file_comment,
  835. $message_id,
  836. $receiver_user_id = 0,
  837. $sender_user_id = 0,
  838. $group_id = 0
  839. ) {
  840. $table = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  841. // Try to add an extension to the file if it hasn't one
  842. $type = isset($file_attach['type']) ? $file_attach['type'] : '';
  843. if (empty($type)) {
  844. $type = DocumentManager::file_get_mime_type($file_attach['name']);
  845. }
  846. $new_file_name = add_ext_on_mime(stripslashes($file_attach['name']), $type);
  847. // user's file name
  848. $file_name = $file_attach['name'];
  849. if (!filter_extension($new_file_name)) {
  850. Display::addFlash(Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error'));
  851. } else {
  852. $new_file_name = uniqid('');
  853. if (!empty($receiver_user_id)) {
  854. $message_user_id = $receiver_user_id;
  855. } else {
  856. $message_user_id = $sender_user_id;
  857. }
  858. // User-reserved directory where photos have to be placed.*
  859. $userGroup = new UserGroup();
  860. if (!empty($group_id)) {
  861. $path_user_info = $userGroup->get_group_picture_path_by_id(
  862. $group_id,
  863. 'system',
  864. true
  865. );
  866. } else {
  867. $path_user_info['dir'] = UserManager::getUserPathById($message_user_id, 'system');
  868. }
  869. $path_message_attach = $path_user_info['dir'].'message_attachments/';
  870. // If this directory does not exist - we create it.
  871. if (!file_exists($path_message_attach)) {
  872. @mkdir($path_message_attach, api_get_permissions_for_new_directories(), true);
  873. }
  874. $new_path = $path_message_attach.$new_file_name;
  875. $fileCopied = false;
  876. if (isset($file_attach['tmp_name']) && !empty($file_attach['tmp_name'])) {
  877. if (is_uploaded_file($file_attach['tmp_name'])) {
  878. @copy($file_attach['tmp_name'], $new_path);
  879. $fileCopied = true;
  880. } else {
  881. // 'tmp_name' can be set by the ticket or when forwarding a message
  882. if (file_exists($file_attach['tmp_name'])) {
  883. @copy($file_attach['tmp_name'], $new_path);
  884. $fileCopied = true;
  885. }
  886. }
  887. }
  888. if ($fileCopied) {
  889. // Storing the attachments if any
  890. $params = [
  891. 'filename' => $file_name,
  892. 'comment' => $file_comment,
  893. 'path' => $new_file_name,
  894. 'message_id' => $message_id,
  895. 'size' => $file_attach['size'],
  896. ];
  897. return Database::insert($table, $params);
  898. }
  899. }
  900. return false;
  901. }
  902. /**
  903. * Delete message attachment files (logically updating the row with a suffix _DELETE_id).
  904. *
  905. * @param int message id
  906. * @param int message user id (receiver user id or sender user id)
  907. * @param int group id (optional)
  908. */
  909. public static function delete_message_attachment_file(
  910. $message_id,
  911. $message_uid,
  912. $group_id = 0
  913. ) {
  914. $message_id = (int) $message_id;
  915. $message_uid = (int) $message_uid;
  916. $table_message_attach = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  917. $sql = "SELECT * FROM $table_message_attach
  918. WHERE message_id = '$message_id'";
  919. $rs = Database::query($sql);
  920. while ($row = Database::fetch_array($rs)) {
  921. $path = $row['path'];
  922. $attach_id = (int) $row['id'];
  923. $new_path = $path.'_DELETED_'.$attach_id;
  924. if (!empty($group_id)) {
  925. $userGroup = new UserGroup();
  926. $path_user_info = $userGroup->get_group_picture_path_by_id(
  927. $group_id,
  928. 'system',
  929. true
  930. );
  931. } else {
  932. $path_user_info['dir'] = UserManager::getUserPathById(
  933. $message_uid,
  934. 'system'
  935. );
  936. }
  937. $path_message_attach = $path_user_info['dir'].'message_attachments/';
  938. if (is_file($path_message_attach.$path)) {
  939. if (rename($path_message_attach.$path, $path_message_attach.$new_path)) {
  940. $sql = "UPDATE $table_message_attach
  941. SET path = '$new_path'
  942. WHERE id = $attach_id ";
  943. Database::query($sql);
  944. }
  945. }
  946. }
  947. }
  948. /**
  949. * @param int $user_id
  950. * @param int $message_id
  951. * @param int $type
  952. *
  953. * @return bool
  954. */
  955. public static function update_message_status($user_id, $message_id, $type)
  956. {
  957. $user_id = (int) $user_id;
  958. $message_id = (int) $message_id;
  959. $type = (int) $type;
  960. if (empty($user_id) || empty($message_id)) {
  961. return false;
  962. }
  963. $table_message = Database::get_main_table(TABLE_MESSAGE);
  964. $sql = "UPDATE $table_message SET
  965. msg_status = '$type'
  966. WHERE
  967. user_receiver_id = ".$user_id." AND
  968. id = '".$message_id."'";
  969. $result = Database::query($sql);
  970. return Database::affected_rows($result) > 0;
  971. }
  972. /**
  973. * get messages by group id.
  974. *
  975. * @param int $group_id group id
  976. *
  977. * @return array
  978. */
  979. public static function get_messages_by_group($group_id)
  980. {
  981. $group_id = (int) $group_id;
  982. if (empty($group_id)) {
  983. return false;
  984. }
  985. $table = Database::get_main_table(TABLE_MESSAGE);
  986. $sql = "SELECT * FROM $table
  987. WHERE
  988. group_id= $group_id AND
  989. msg_status NOT IN ('".MESSAGE_STATUS_OUTBOX."', '".MESSAGE_STATUS_DELETED."')
  990. ORDER BY id";
  991. $rs = Database::query($sql);
  992. $data = [];
  993. if (Database::num_rows($rs) > 0) {
  994. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  995. $data[] = $row;
  996. }
  997. }
  998. return $data;
  999. }
  1000. /**
  1001. * get messages by group id.
  1002. *
  1003. * @param int $group_id
  1004. * @param int $message_id
  1005. *
  1006. * @return array
  1007. */
  1008. public static function get_messages_by_group_by_message($group_id, $message_id)
  1009. {
  1010. $group_id = (int) $group_id;
  1011. if (empty($group_id)) {
  1012. return false;
  1013. }
  1014. $table = Database::get_main_table(TABLE_MESSAGE);
  1015. $sql = "SELECT * FROM $table
  1016. WHERE
  1017. group_id = $group_id AND
  1018. msg_status NOT IN ('".MESSAGE_STATUS_OUTBOX."', '".MESSAGE_STATUS_DELETED."')
  1019. ORDER BY id ";
  1020. $rs = Database::query($sql);
  1021. $data = [];
  1022. $parents = [];
  1023. if (Database::num_rows($rs) > 0) {
  1024. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  1025. if ($message_id == $row['parent_id'] || in_array($row['parent_id'], $parents)) {
  1026. $parents[] = $row['id'];
  1027. $data[] = $row;
  1028. }
  1029. }
  1030. }
  1031. return $data;
  1032. }
  1033. /**
  1034. * Get messages by parent id optionally with limit.
  1035. *
  1036. * @param int parent id
  1037. * @param int group id (optional)
  1038. * @param int offset (optional)
  1039. * @param int limit (optional)
  1040. *
  1041. * @return array
  1042. */
  1043. public static function getMessagesByParent($parentId, $groupId = 0, $offset = 0, $limit = 0)
  1044. {
  1045. $table = Database::get_main_table(TABLE_MESSAGE);
  1046. $parentId = (int) $parentId;
  1047. if (empty($parentId)) {
  1048. return [];
  1049. }
  1050. $condition_group_id = '';
  1051. if (!empty($groupId)) {
  1052. $groupId = (int) $groupId;
  1053. $condition_group_id = " AND group_id = '$groupId' ";
  1054. }
  1055. $condition_limit = '';
  1056. if ($offset && $limit) {
  1057. $offset = (int) $offset;
  1058. $limit = (int) $limit;
  1059. $offset = ($offset - 1) * $limit;
  1060. $condition_limit = " LIMIT $offset,$limit ";
  1061. }
  1062. $sql = "SELECT * FROM $table
  1063. WHERE
  1064. parent_id='$parentId' AND
  1065. msg_status NOT IN (".MESSAGE_STATUS_OUTBOX.", ".MESSAGE_STATUS_WALL_DELETE.")
  1066. $condition_group_id
  1067. ORDER BY send_date DESC $condition_limit ";
  1068. $rs = Database::query($sql);
  1069. $data = [];
  1070. if (Database::num_rows($rs) > 0) {
  1071. while ($row = Database::fetch_array($rs)) {
  1072. $data[$row['id']] = $row;
  1073. }
  1074. }
  1075. return $data;
  1076. }
  1077. /**
  1078. * Gets information about messages sent.
  1079. *
  1080. * @param int
  1081. * @param int
  1082. * @param string
  1083. * @param string
  1084. *
  1085. * @return array
  1086. */
  1087. public static function get_message_data_sent(
  1088. $from,
  1089. $numberOfItems,
  1090. $column,
  1091. $direction,
  1092. $extraParams = []
  1093. ) {
  1094. $from = (int) $from;
  1095. $numberOfItems = (int) $numberOfItems;
  1096. if (!isset($direction)) {
  1097. $column = 2;
  1098. $direction = 'DESC';
  1099. } else {
  1100. $column = (int) $column;
  1101. if (!in_array($direction, ['ASC', 'DESC'])) {
  1102. $direction = 'ASC';
  1103. }
  1104. }
  1105. if (!in_array($column, [0, 1, 2])) {
  1106. $column = 2;
  1107. }
  1108. $table = Database::get_main_table(TABLE_MESSAGE);
  1109. $request = api_is_xml_http_request();
  1110. $keyword = isset($extraParams['keyword']) && !empty($extraParams['keyword']) ? $extraParams['keyword'] : '';
  1111. $keywordCondition = '';
  1112. if (!empty($keyword)) {
  1113. $keyword = Database::escape_string($keyword);
  1114. $keywordCondition = " AND (title like '%$keyword%' OR content LIKE '%$keyword%') ";
  1115. }
  1116. $sql = "SELECT
  1117. id as col0,
  1118. title as col1,
  1119. send_date as col2,
  1120. user_receiver_id,
  1121. msg_status,
  1122. user_sender_id
  1123. FROM $table
  1124. WHERE
  1125. user_sender_id = ".api_get_user_id()." AND
  1126. msg_status = ".MESSAGE_STATUS_OUTBOX."
  1127. $keywordCondition
  1128. ORDER BY col$column $direction
  1129. LIMIT $from, $numberOfItems";
  1130. $result = Database::query($sql);
  1131. $message_list = [];
  1132. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1133. $messageId = $row['col0'];
  1134. $title = $row['col1'];
  1135. $sendDate = $row['col2'];
  1136. $senderId = $row['user_sender_id'];
  1137. if ($request === true) {
  1138. $message[0] = '<input type="checkbox" value='.$messageId.' name="out[]">';
  1139. } else {
  1140. $message[0] = $messageId;
  1141. }
  1142. $class = 'class = "read"';
  1143. $title = Security::remove_XSS($title);
  1144. $userInfo = api_get_user_info($senderId);
  1145. if ($request === true) {
  1146. $message[1] = '<a onclick="show_sent_message('.$messageId.')" href="javascript:void(0)">'.
  1147. $userInfo['complete_name_with_username'].'</a>';
  1148. $message[2] = '<a onclick="show_sent_message('.$messageId.')" href="javascript:void(0)">'.str_replace(
  1149. "\\",
  1150. "",
  1151. $title
  1152. ).'</a>';
  1153. //date stays the same
  1154. $message[3] = api_convert_and_format_date($sendDate, DATE_TIME_FORMAT_LONG);
  1155. $message[4] = '&nbsp;&nbsp;<a title="'.addslashes(
  1156. get_lang('DeleteMessage')
  1157. ).'" onclick="delete_one_message_outbox('.$messageId.')" href="javascript:void(0)" >'.
  1158. Display::returnFontAwesomeIcon('trash', 2).'</a>';
  1159. } else {
  1160. $message[1] = '<a '.$class.' onclick="show_sent_message('.$messageId.')" href="../messages/view_message.php?id_send='.$messageId.'">'.$title.'</a><br />'.$userInfo['complete_name_with_username'];
  1161. $message[2] = api_convert_and_format_date($sendDate, DATE_TIME_FORMAT_LONG);
  1162. $message[3] = '<a title="'.addslashes(
  1163. get_lang('DeleteMessage')
  1164. ).'" href="outbox.php?action=deleteone&id='.$messageId.'" onclick="javascript:if(!confirm('."'".addslashes(
  1165. api_htmlentities(get_lang('ConfirmDeleteMessage'))
  1166. )."'".')) return false;" >'.
  1167. Display::returnFontAwesomeIcon('trash', 2).'</a>';
  1168. }
  1169. $message_list[] = $message;
  1170. }
  1171. return $message_list;
  1172. }
  1173. /**
  1174. * display message box in the inbox.
  1175. *
  1176. * @param int $messageId
  1177. * @param int $type
  1178. *
  1179. * @todo replace numbers with letters in the $row array pff...
  1180. *
  1181. * @return string html with the message content
  1182. */
  1183. public static function showMessageBox($messageId, $type)
  1184. {
  1185. $messageId = (int) $messageId;
  1186. if (empty($messageId) || empty($type)) {
  1187. return '';
  1188. }
  1189. $currentUserId = api_get_user_id();
  1190. $table = Database::get_main_table(TABLE_MESSAGE);
  1191. if (empty($type)) {
  1192. return '';
  1193. }
  1194. switch ($type) {
  1195. case self::MESSAGE_TYPE_OUTBOX:
  1196. $status = MESSAGE_STATUS_OUTBOX;
  1197. $userCondition = " user_sender_id = $currentUserId AND ";
  1198. break;
  1199. case self::MESSAGE_TYPE_INBOX:
  1200. $status = MESSAGE_STATUS_NEW;
  1201. $userCondition = " user_receiver_id = $currentUserId AND ";
  1202. $query = "UPDATE $table SET
  1203. msg_status = '".MESSAGE_STATUS_NEW."'
  1204. WHERE id = $messageId ";
  1205. Database::query($query);
  1206. break;
  1207. case self::MESSAGE_TYPE_PROMOTED:
  1208. $status = MESSAGE_STATUS_PROMOTED;
  1209. $userCondition = " user_receiver_id = $currentUserId AND ";
  1210. break;
  1211. }
  1212. if (empty($userCondition)) {
  1213. return '';
  1214. }
  1215. $query = "SELECT * FROM $table
  1216. WHERE
  1217. id = $messageId AND
  1218. $userCondition
  1219. msg_status = $status";
  1220. $result = Database::query($query);
  1221. $row = Database::fetch_array($result, 'ASSOC');
  1222. if (empty($row)) {
  1223. return '';
  1224. }
  1225. $user_sender_id = $row['user_sender_id'];
  1226. // get file attachments by message id
  1227. $files_attachments = self::getAttachmentLinkList($messageId, $type);
  1228. $row['content'] = str_replace('</br>', '<br />', $row['content']);
  1229. $title = Security::remove_XSS($row['title'], STUDENT, true);
  1230. $content = Security::remove_XSS($row['content'], STUDENT, true);
  1231. $name = get_lang('UnknownUser');
  1232. $fromUser = api_get_user_info($user_sender_id);
  1233. $userImage = '';
  1234. if (!empty($user_sender_id) && !empty($fromUser)) {
  1235. $name = $fromUser['complete_name_with_username'];
  1236. $userImage = Display::img(
  1237. $fromUser['avatar_small'],
  1238. $name,
  1239. ['title' => $name, 'class' => 'img-responsive img-circle', 'style' => 'max-width:35px'],
  1240. false
  1241. );
  1242. }
  1243. $message_content = Display::page_subheader(str_replace("\\", '', $title));
  1244. $receiverUserInfo = [];
  1245. if (!empty($row['user_receiver_id'])) {
  1246. $receiverUserInfo = api_get_user_info($row['user_receiver_id']);
  1247. }
  1248. $message_content .= '<tr>';
  1249. if (api_get_setting('allow_social_tool') === 'true') {
  1250. $message_content .= '<div class="row">';
  1251. $message_content .= '<div class="col-md-12">';
  1252. $message_content .= '<ul class="list-message">';
  1253. if (!empty($user_sender_id)) {
  1254. $message_content .= '<li>'.$userImage.'</li>';
  1255. $message_content .= '<li>';
  1256. $message_content .= Display::url(
  1257. $name,
  1258. api_get_path(WEB_PATH).'main/social/profile.php?u='.$user_sender_id
  1259. );
  1260. } else {
  1261. $message_content .= '<li>'.$name;
  1262. }
  1263. switch ($type) {
  1264. case self::MESSAGE_TYPE_INBOX:
  1265. //$message_content .= api_strtolower(get_lang('To')).'&nbsp;<b>-</b></li>';
  1266. $message_content .= '&nbsp;'.api_strtolower(get_lang('To')).'&nbsp;'.get_lang('Me');
  1267. break;
  1268. case self::MESSAGE_TYPE_OUTBOX:
  1269. if (!empty($receiverUserInfo)) {
  1270. $message_content .= '&nbsp;'.api_strtolower(
  1271. get_lang('To')
  1272. ).'&nbsp;<b>'.$receiverUserInfo['complete_name_with_username'].'</b></li>';
  1273. }
  1274. break;
  1275. case self::MESSAGE_TYPE_PROMOTED:
  1276. break;
  1277. }
  1278. $message_content .= '&nbsp;<li>'.Display::dateToStringAgoAndLongDate($row['send_date']).'</li>';
  1279. $message_content .= '</ul>';
  1280. $message_content .= '</div>';
  1281. $message_content .= '</div>';
  1282. } else {
  1283. switch ($type) {
  1284. case self::MESSAGE_TYPE_INBOX:
  1285. $message_content .= get_lang('From').':&nbsp;'.$name.'</b> '.api_strtolower(get_lang('To')).' <b>'.
  1286. get_lang('Me').'</b>';
  1287. break;
  1288. case self::MESSAGE_TYPE_OUTBOX:
  1289. $message_content .= get_lang('From').':&nbsp;'.$name.'</b> '.api_strtolower(get_lang('To')).' <b>'.
  1290. $receiverUserInfo['complete_name_with_username'].'</b>';
  1291. break;
  1292. }
  1293. }
  1294. $message_content .= '
  1295. <hr style="color:#ddd" />
  1296. <table width="100%">
  1297. <tr>
  1298. <td valign=top class="view-message-content">'.str_replace("\\", "", $content).'</td>
  1299. </tr>
  1300. </table>
  1301. <div id="message-attach">'.(!empty($files_attachments) ? implode('<br />', $files_attachments) : '').'</div>
  1302. <div style="padding: 15px 0px 5px 0px">';
  1303. $social_link = '';
  1304. if (isset($_GET['f']) && $_GET['f'] == 'social') {
  1305. $social_link = 'f=social';
  1306. }
  1307. switch ($type) {
  1308. case self::MESSAGE_TYPE_OUTBOX:
  1309. $message_content .= '<a href="outbox.php?'.$social_link.'">'.
  1310. Display::return_icon('back.png', get_lang('ReturnToOutbox')).'</a> &nbsp';
  1311. $message_content .= '<a href="outbox.php?action=deleteone&id='.$messageId.'&'.$social_link.'" >'.
  1312. Display::return_icon('delete.png', get_lang('DeleteMessage')).'</a>&nbsp';
  1313. break;
  1314. case self::MESSAGE_TYPE_INBOX:
  1315. $message_content .= '<a href="inbox.php?'.$social_link.'">'.
  1316. Display::return_icon('back.png', get_lang('ReturnToInbox')).'</a> &nbsp';
  1317. $message_content .= '<a href="new_message.php?re_id='.$messageId.'&'.$social_link.'">'.
  1318. Display::return_icon('message_reply.png', get_lang('ReplyToMessage')).'</a> &nbsp';
  1319. $message_content .= '<a href="inbox.php?action=deleteone&id='.$messageId.'&'.$social_link.'" >'.
  1320. Display::return_icon('delete.png', get_lang('DeleteMessage')).'</a>&nbsp';
  1321. break;
  1322. }
  1323. $message_content .= '</div></td>
  1324. <td width=10></td>
  1325. </tr>
  1326. </table>';
  1327. return $message_content;
  1328. }
  1329. /**
  1330. * get user id by user email.
  1331. *
  1332. * @param string $user_email
  1333. *
  1334. * @return int user id
  1335. */
  1336. public static function get_user_id_by_email($user_email)
  1337. {
  1338. $table = Database::get_main_table(TABLE_MAIN_USER);
  1339. $sql = 'SELECT user_id
  1340. FROM '.$table.'
  1341. WHERE email="'.Database::escape_string($user_email).'";';
  1342. $rs = Database::query($sql);
  1343. $row = Database::fetch_array($rs, 'ASSOC');
  1344. if (isset($row['user_id'])) {
  1345. return $row['user_id'];
  1346. }
  1347. return null;
  1348. }
  1349. /**
  1350. * Displays messages of a group with nested view.
  1351. *
  1352. * @param int $groupId
  1353. *
  1354. * @return string
  1355. */
  1356. public static function display_messages_for_group($groupId)
  1357. {
  1358. global $my_group_role;
  1359. $rows = self::get_messages_by_group($groupId);
  1360. $topics_per_page = 10;
  1361. $html_messages = '';
  1362. $query_vars = ['id' => $groupId, 'topics_page_nr' => 0];
  1363. if (is_array($rows) && count($rows) > 0) {
  1364. // prepare array for topics with its items
  1365. $topics = [];
  1366. $x = 0;
  1367. foreach ($rows as $index => $value) {
  1368. if (empty($value['parent_id'])) {
  1369. $topics[$value['id']] = $value;
  1370. }
  1371. }
  1372. $new_topics = [];
  1373. foreach ($topics as $id => $value) {
  1374. $rows = null;
  1375. $rows = self::get_messages_by_group_by_message($groupId, $value['id']);
  1376. if (!empty($rows)) {
  1377. $count = count(self::calculate_children($rows, $value['id']));
  1378. } else {
  1379. $count = 0;
  1380. }
  1381. $value['count'] = $count;
  1382. $new_topics[$id] = $value;
  1383. }
  1384. $array_html = [];
  1385. foreach ($new_topics as $index => $topic) {
  1386. $html = '';
  1387. // topics
  1388. $user_sender_info = api_get_user_info($topic['user_sender_id']);
  1389. $name = $user_sender_info['complete_name'];
  1390. $html .= '<div class="groups-messages">';
  1391. $html .= '<div class="row">';
  1392. $items = $topic['count'];
  1393. $reply_label = ($items == 1) ? get_lang('GroupReply') : get_lang('GroupReplies');
  1394. $label = '<i class="fa fa-envelope"></i> '.$items.' '.$reply_label;
  1395. $topic['title'] = trim($topic['title']);
  1396. if (empty($topic['title'])) {
  1397. $topic['title'] = get_lang('Untitled');
  1398. }
  1399. $html .= '<div class="col-xs-8 col-md-10">';
  1400. $html .= Display::tag(
  1401. 'h4',
  1402. Display::url(
  1403. Security::remove_XSS($topic['title'], STUDENT, true),
  1404. api_get_path(WEB_CODE_PATH).'social/group_topics.php?id='.$groupId.'&topic_id='.$topic['id']
  1405. ),
  1406. ['class' => 'title']
  1407. );
  1408. $actions = '';
  1409. if ($my_group_role == GROUP_USER_PERMISSION_ADMIN ||
  1410. $my_group_role == GROUP_USER_PERMISSION_MODERATOR
  1411. ) {
  1412. $actions = '<br />'.Display::url(
  1413. get_lang('Delete'),
  1414. api_get_path(
  1415. WEB_CODE_PATH
  1416. ).'social/group_topics.php?action=delete&id='.$groupId.'&topic_id='.$topic['id'],
  1417. ['class' => 'btn btn-default']
  1418. );
  1419. }
  1420. $date = '';
  1421. if ($topic['send_date'] != $topic['update_date']) {
  1422. if (!empty($topic['update_date'])) {
  1423. $date .= '<i class="fa fa-calendar"></i> '.get_lang(
  1424. 'LastUpdate'
  1425. ).' '.Display::dateToStringAgoAndLongDate($topic['update_date']);
  1426. }
  1427. } else {
  1428. $date .= '<i class="fa fa-calendar"></i> '.get_lang(
  1429. 'Created'
  1430. ).' '.Display::dateToStringAgoAndLongDate($topic['send_date']);
  1431. }
  1432. $html .= '<div class="date">'.$label.' - '.$date.$actions.'</div>';
  1433. $html .= '</div>';
  1434. $image = $user_sender_info['avatar'];
  1435. $user_info = '<div class="author"><img class="img-responsive img-circle" src="'.$image.'" alt="'.$name.'" width="64" height="64" title="'.$name.'" /></div>';
  1436. $user_info .= '<div class="name"><a href="'.api_get_path(
  1437. WEB_PATH
  1438. ).'main/social/profile.php?u='.$topic['user_sender_id'].'">'.$name.'&nbsp;</a></div>';
  1439. $html .= '<div class="col-xs-4 col-md-2">';
  1440. $html .= $user_info;
  1441. $html .= '</div>';
  1442. $html .= '</div>';
  1443. $html .= '</div>';
  1444. $array_html[] = [$html];
  1445. }
  1446. // grids for items and topics with paginations
  1447. $html_messages .= Display::return_sortable_grid(
  1448. 'topics',
  1449. [],
  1450. $array_html,
  1451. [
  1452. 'hide_navigation' => false,
  1453. 'per_page' => $topics_per_page,
  1454. ],
  1455. $query_vars,
  1456. false,
  1457. [true, true, true, false],
  1458. false
  1459. );
  1460. }
  1461. return $html_messages;
  1462. }
  1463. /**
  1464. * Displays messages of a group with nested view.
  1465. *
  1466. * @param $groupId
  1467. * @param $topic_id
  1468. *
  1469. * @return string
  1470. */
  1471. public static function display_message_for_group($groupId, $topic_id)
  1472. {
  1473. global $my_group_role;
  1474. $main_message = self::get_message_by_id($topic_id);
  1475. if (empty($main_message)) {
  1476. return false;
  1477. }
  1478. $webCodePath = api_get_path(WEB_CODE_PATH);
  1479. $iconCalendar = Display::returnFontAwesomeIcon('calendar');
  1480. $langEdit = get_lang('Edit');
  1481. $langReply = get_lang('Reply');
  1482. $langLastUpdated = get_lang('LastUpdated');
  1483. $langCreated = get_lang('Created');
  1484. $rows = self::get_messages_by_group_by_message($groupId, $topic_id);
  1485. $rows = self::calculate_children($rows, $topic_id);
  1486. $current_user_id = api_get_user_id();
  1487. $items_per_page = 50;
  1488. $query_vars = ['id' => $groupId, 'topic_id' => $topic_id, 'topics_page_nr' => 0];
  1489. // Main message
  1490. $links = '';
  1491. $main_content = '';
  1492. $html = '';
  1493. $items_page_nr = null;
  1494. $user_sender_info = api_get_user_info($main_message['user_sender_id']);
  1495. $files_attachments = self::getAttachmentLinkList($main_message['id'], 0);
  1496. $name = $user_sender_info['complete_name'];
  1497. $topic_page_nr = isset($_GET['topics_page_nr']) ? (int) $_GET['topics_page_nr'] : null;
  1498. $links .= '<div class="pull-right">';
  1499. $links .= '<div class="btn-group btn-group-sm">';
  1500. if (($my_group_role == GROUP_USER_PERMISSION_ADMIN || $my_group_role == GROUP_USER_PERMISSION_MODERATOR) ||
  1501. $main_message['user_sender_id'] == $current_user_id
  1502. ) {
  1503. $urlEdit = $webCodePath.'social/message_for_group_form.inc.php?'
  1504. .http_build_query(
  1505. [
  1506. 'user_friend' => $current_user_id,
  1507. 'group_id' => $groupId,
  1508. 'message_id' => $main_message['id'],
  1509. 'action' => 'edit_message_group',
  1510. 'anchor_topic' => 'topic_'.$main_message['id'],
  1511. 'topics_page_nr' => $topic_page_nr,
  1512. 'items_page_nr' => $items_page_nr,
  1513. 'topic_id' => $main_message['id'],
  1514. ]
  1515. );
  1516. $links .= Display::toolbarButton(
  1517. $langEdit,
  1518. $urlEdit,
  1519. 'pencil',
  1520. 'default',
  1521. ['class' => 'ajax', 'data-title' => $langEdit, 'data-size' => 'lg'],
  1522. false
  1523. );
  1524. }
  1525. $links .= self::getLikesButton($main_message['id'], $current_user_id, $groupId);
  1526. $urlReply = $webCodePath.'social/message_for_group_form.inc.php?'
  1527. .http_build_query(
  1528. [
  1529. 'user_friend' => $current_user_id,
  1530. 'group_id' => $groupId,
  1531. 'message_id' => $main_message['id'],
  1532. 'action' => 'reply_message_group',
  1533. 'anchor_topic' => 'topic_'.$main_message['id'],
  1534. 'topics_page_nr' => $topic_page_nr,
  1535. 'topic_id' => $main_message['id'],
  1536. ]
  1537. );
  1538. $links .= Display::toolbarButton(
  1539. $langReply,
  1540. $urlReply,
  1541. 'commenting',
  1542. 'default',
  1543. ['class' => 'ajax', 'data-title' => $langReply, 'data-size' => 'lg'],
  1544. false
  1545. );
  1546. if (api_is_platform_admin()) {
  1547. $links .= Display::toolbarButton(
  1548. get_lang('Delete'),
  1549. 'group_topics.php?action=delete&id='.$groupId.'&topic_id='.$topic_id,
  1550. 'trash',
  1551. 'default',
  1552. [],
  1553. false
  1554. );
  1555. }
  1556. $links .= '</div>';
  1557. $links .= '</div>';
  1558. $title = '<h4>'.Security::remove_XSS($main_message['title'], STUDENT, true).$links.'</h4>';
  1559. $userPicture = $user_sender_info['avatar'];
  1560. $main_content .= '<div class="row">';
  1561. $main_content .= '<div class="col-md-2">';
  1562. $main_content .= '<div class="avatar-author">';
  1563. $main_content .= Display::img(
  1564. $userPicture,
  1565. $name,
  1566. ['width' => '60px', 'class' => 'img-responsive img-circle'],
  1567. false
  1568. );
  1569. $main_content .= '</div>';
  1570. $main_content .= '</div>';
  1571. $date = '';
  1572. if ($main_message['send_date'] != $main_message['update_date']) {
  1573. if (!empty($main_message['update_date'])) {
  1574. $date = '<div class="date"> '
  1575. ."$iconCalendar $langLastUpdated "
  1576. .Display::dateToStringAgoAndLongDate($main_message['update_date'])
  1577. .'</div>';
  1578. }
  1579. } else {
  1580. $date = '<div class="date"> '
  1581. ."$iconCalendar $langCreated "
  1582. .Display::dateToStringAgoAndLongDate($main_message['send_date'])
  1583. .'</div>';
  1584. }
  1585. $attachment = '<div class="message-attach">'
  1586. .(!empty($files_attachments) ? implode('<br />', $files_attachments) : '')
  1587. .'</div>';
  1588. $main_content .= '<div class="col-md-10">';
  1589. $user_link = Display::url(
  1590. $name,
  1591. $webCodePath.'social/profile.php?u='.$main_message['user_sender_id']
  1592. );
  1593. $main_content .= '<div class="message-content"> ';
  1594. $main_content .= '<div class="username">'.$user_link.'</div>';
  1595. $main_content .= $date;
  1596. $main_content .= '<div class="message">'.$main_message['content'].$attachment.'</div></div>';
  1597. $main_content .= '</div>';
  1598. $main_content .= '</div>';
  1599. $html .= Display::div(
  1600. Display::div(
  1601. $title.$main_content,
  1602. ['class' => 'message-topic']
  1603. ),
  1604. ['class' => 'sm-groups-message']
  1605. );
  1606. $topic_id = $main_message['id'];
  1607. if (is_array($rows) && count($rows) > 0) {
  1608. $topics = $rows;
  1609. $array_html_items = [];
  1610. foreach ($topics as $index => $topic) {
  1611. if (empty($topic['id'])) {
  1612. continue;
  1613. }
  1614. $items_page_nr = isset($_GET['items_'.$topic['id'].'_page_nr'])
  1615. ? (int) $_GET['items_'.$topic['id'].'_page_nr']
  1616. : null;
  1617. $links = '';
  1618. $links .= '<div class="pull-right">';
  1619. $html_items = '';
  1620. $user_sender_info = api_get_user_info($topic['user_sender_id']);
  1621. $files_attachments = self::getAttachmentLinkList($topic['id'], 0);
  1622. $name = $user_sender_info['complete_name'];
  1623. $links .= '<div class="btn-group btn-group-sm">';
  1624. if (
  1625. ($my_group_role == GROUP_USER_PERMISSION_ADMIN ||
  1626. $my_group_role == GROUP_USER_PERMISSION_MODERATOR
  1627. ) ||
  1628. $topic['user_sender_id'] == $current_user_id
  1629. ) {
  1630. $links .= Display::toolbarButton(
  1631. $langEdit,
  1632. $webCodePath.'social/message_for_group_form.inc.php?'
  1633. .http_build_query(
  1634. [
  1635. 'user_friend' => $current_user_id,
  1636. 'group_id' => $groupId,
  1637. 'message_id' => $topic['id'],
  1638. 'action' => 'edit_message_group',
  1639. 'anchor_topic' => 'topic_'.$topic_id,
  1640. 'topics_page_nr' => $topic_page_nr,
  1641. 'items_page_nr' => $items_page_nr,
  1642. 'topic_id' => $topic_id,
  1643. ]
  1644. ),
  1645. 'pencil',
  1646. 'default',
  1647. ['class' => 'ajax', 'data-title' => $langEdit, 'data-size' => 'lg'],
  1648. false
  1649. );
  1650. }
  1651. $links .= self::getLikesButton($topic['id'], $current_user_id, $groupId);
  1652. $links .= Display::toolbarButton(
  1653. $langReply,
  1654. $webCodePath.'social/message_for_group_form.inc.php?'
  1655. .http_build_query(
  1656. [
  1657. 'user_friend' => $current_user_id,
  1658. 'group_id' => $groupId,
  1659. 'message_id' => $topic['id'],
  1660. 'action' => 'reply_message_group',
  1661. 'anchor_topic' => 'topic_'.$topic_id,
  1662. 'topics_page_nr' => $topic_page_nr,
  1663. 'items_page_nr' => $items_page_nr,
  1664. 'topic_id' => $topic_id,
  1665. ]
  1666. ),
  1667. 'commenting',
  1668. 'default',
  1669. ['class' => 'ajax', 'data-title' => $langReply, 'data-size' => 'lg'],
  1670. false
  1671. );
  1672. $links .= '</div>';
  1673. $links .= '</div>';
  1674. $userPicture = $user_sender_info['avatar'];
  1675. $user_link = Display::url(
  1676. $name,
  1677. $webCodePath.'social/profile.php?u='.$topic['user_sender_id']
  1678. );
  1679. $html_items .= '<div class="row">';
  1680. $html_items .= '<div class="col-md-2">';
  1681. $html_items .= '<div class="avatar-author">';
  1682. $html_items .= Display::img(
  1683. $userPicture,
  1684. $name,
  1685. ['width' => '60px', 'class' => 'img-responsive img-circle'],
  1686. false
  1687. );
  1688. $html_items .= '</div>';
  1689. $html_items .= '</div>';
  1690. $date = '';
  1691. if ($topic['send_date'] != $topic['update_date']) {
  1692. if (!empty($topic['update_date'])) {
  1693. $date = '<div class="date"> '
  1694. ."$iconCalendar $langLastUpdated "
  1695. .Display::dateToStringAgoAndLongDate($topic['update_date'])
  1696. .'</div>';
  1697. }
  1698. } else {
  1699. $date = '<div class="date"> '
  1700. ."$iconCalendar $langCreated "
  1701. .Display::dateToStringAgoAndLongDate($topic['send_date'])
  1702. .'</div>';
  1703. }
  1704. $attachment = '<div class="message-attach">'
  1705. .(!empty($files_attachments) ? implode('<br />', $files_attachments) : '')
  1706. .'</div>';
  1707. $html_items .= '<div class="col-md-10">'
  1708. .'<div class="message-content">'
  1709. .$links
  1710. .'<div class="username">'.$user_link.'</div>'
  1711. .$date
  1712. .'<div class="message">'
  1713. .Security::remove_XSS($topic['content'], STUDENT, true)
  1714. .'</div>'.$attachment.'</div>'
  1715. .'</div>'
  1716. .'</div>';
  1717. $base_padding = 20;
  1718. if ($topic['indent_cnt'] == 0) {
  1719. $indent = $base_padding;
  1720. } else {
  1721. $indent = (int) $topic['indent_cnt'] * $base_padding + $base_padding;
  1722. }
  1723. $html_items = Display::div($html_items, ['class' => 'message-post', 'id' => 'msg_'.$topic['id']]);
  1724. $html_items = Display::div($html_items, ['class' => '', 'style' => 'margin-left:'.$indent.'px']);
  1725. $array_html_items[] = [$html_items];
  1726. }
  1727. // grids for items with paginations
  1728. $options = ['hide_navigation' => false, 'per_page' => $items_per_page];
  1729. $visibility = [true, true, true, false];
  1730. $style_class = [
  1731. 'item' => ['class' => 'user-post'],
  1732. 'main' => ['class' => 'user-list'],
  1733. ];
  1734. if (!empty($array_html_items)) {
  1735. $html .= Display::return_sortable_grid(
  1736. 'items_'.$topic['id'],
  1737. [],
  1738. $array_html_items,
  1739. $options,
  1740. $query_vars,
  1741. null,
  1742. $visibility,
  1743. false,
  1744. $style_class
  1745. );
  1746. }
  1747. }
  1748. return $html;
  1749. }
  1750. /**
  1751. * Add children to messages by id is used for nested view messages.
  1752. *
  1753. * @param array $rows rows of messages
  1754. *
  1755. * @return array $first_seed new list adding the item children
  1756. */
  1757. public static function calculate_children($rows, $first_seed)
  1758. {
  1759. $rows_with_children = [];
  1760. foreach ($rows as $row) {
  1761. $rows_with_children[$row["id"]] = $row;
  1762. $rows_with_children[$row["parent_id"]]["children"][] = $row["id"];
  1763. }
  1764. $rows = $rows_with_children;
  1765. $sorted_rows = [0 => []];
  1766. self::message_recursive_sort($rows, $sorted_rows, $first_seed);
  1767. unset($sorted_rows[0]);
  1768. return $sorted_rows;
  1769. }
  1770. /**
  1771. * Sort recursively the messages, is used for for nested view messages.
  1772. *
  1773. * @param array original rows of messages
  1774. * @param array list recursive of messages
  1775. * @param int seed for calculate the indent
  1776. * @param int indent for nested view
  1777. */
  1778. public static function message_recursive_sort(
  1779. $rows,
  1780. &$messages,
  1781. $seed = 0,
  1782. $indent = 0
  1783. ) {
  1784. if ($seed > 0 && isset($rows[$seed]["id"])) {
  1785. $messages[$rows[$seed]["id"]] = $rows[$seed];
  1786. $messages[$rows[$seed]["id"]]["indent_cnt"] = $indent;
  1787. $indent++;
  1788. }
  1789. if (isset($rows[$seed]["children"])) {
  1790. foreach ($rows[$seed]["children"] as $child) {
  1791. self::message_recursive_sort($rows, $messages, $child, $indent);
  1792. }
  1793. }
  1794. }
  1795. /**
  1796. * @param int $messageId
  1797. *
  1798. * @return array
  1799. */
  1800. public static function getAttachmentList($messageId)
  1801. {
  1802. $table = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  1803. $messageId = (int) $messageId;
  1804. if (empty($messageId)) {
  1805. return [];
  1806. }
  1807. $messageInfo = self::get_message_by_id($messageId);
  1808. if (empty($messageInfo)) {
  1809. return [];
  1810. }
  1811. $attachmentDir = UserManager::getUserPathById($messageInfo['user_receiver_id'], 'system');
  1812. $attachmentDir .= 'message_attachments/';
  1813. $sql = "SELECT * FROM $table
  1814. WHERE message_id = '$messageId'";
  1815. $result = Database::query($sql);
  1816. $files = [];
  1817. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1818. $row['file_source'] = '';
  1819. if (file_exists($attachmentDir.$row['path'])) {
  1820. $row['file_source'] = $attachmentDir.$row['path'];
  1821. }
  1822. $files[] = $row;
  1823. }
  1824. return $files;
  1825. }
  1826. /**
  1827. * Get array of links (download) for message attachment files.
  1828. *
  1829. * @param int $messageId
  1830. * @param int $type
  1831. *
  1832. * @return array
  1833. */
  1834. public static function getAttachmentLinkList($messageId, $type)
  1835. {
  1836. $files = self::getAttachmentList($messageId);
  1837. // get file attachments by message id
  1838. $list = [];
  1839. if ($files) {
  1840. $attachIcon = Display::return_icon('attachment.gif', '');
  1841. $archiveURL = api_get_path(WEB_CODE_PATH).'messages/download.php?type='.$type.'&file=';
  1842. foreach ($files as $row_file) {
  1843. $archiveFile = $row_file['path'];
  1844. $filename = $row_file['filename'];
  1845. $size = format_file_size($row_file['size']);
  1846. $comment = Security::remove_XSS($row_file['comment']);
  1847. $filename = Security::remove_XSS($filename);
  1848. $link = Display::url($filename, $archiveURL.$archiveFile);
  1849. $comment = !empty($comment) ? '&nbsp;-&nbsp;<i>'.$comment.'</i>' : '';
  1850. $attachmentLine = $attachIcon.'&nbsp;'.$link.'&nbsp;('.$size.')'.$comment;
  1851. if ($row_file['comment'] === 'audio_message') {
  1852. $attachmentLine = '<audio src="'.$archiveURL.$archiveFile.'"/>';
  1853. }
  1854. $list[] = $attachmentLine;
  1855. }
  1856. }
  1857. return $list;
  1858. }
  1859. /**
  1860. * Get message list by id.
  1861. *
  1862. * @param int $messageId
  1863. *
  1864. * @return array
  1865. */
  1866. public static function get_message_by_id($messageId)
  1867. {
  1868. $table = Database::get_main_table(TABLE_MESSAGE);
  1869. $messageId = (int) $messageId;
  1870. $sql = "SELECT * FROM $table
  1871. WHERE
  1872. id = '$messageId' AND
  1873. msg_status <> '".MESSAGE_STATUS_DELETED."' ";
  1874. $res = Database::query($sql);
  1875. $item = [];
  1876. if (Database::num_rows($res) > 0) {
  1877. $item = Database::fetch_array($res, 'ASSOC');
  1878. }
  1879. return $item;
  1880. }
  1881. /**
  1882. * @return string
  1883. */
  1884. public static function generate_message_form()
  1885. {
  1886. $form = new FormValidator('send_message');
  1887. $form->addText(
  1888. 'subject',
  1889. get_lang('Subject'),
  1890. false,
  1891. ['id' => 'subject_id']
  1892. );
  1893. $form->addTextarea(
  1894. 'content',
  1895. get_lang('Message'),
  1896. ['id' => 'content_id', 'rows' => '5']
  1897. );
  1898. return $form->returnForm();
  1899. }
  1900. /**
  1901. * @return string
  1902. */
  1903. public static function generate_invitation_form()
  1904. {
  1905. $form = new FormValidator('send_invitation');
  1906. $form->addTextarea(
  1907. 'content',
  1908. get_lang('AddPersonalMessage'),
  1909. ['id' => 'content_invitation_id', 'rows' => 5]
  1910. );
  1911. return $form->returnForm();
  1912. }
  1913. /**
  1914. * @param string $type
  1915. * @param string $keyword
  1916. * @param array $actions
  1917. *
  1918. * @return string
  1919. */
  1920. public static function getMessageGrid($type, $keyword, $actions = [])
  1921. {
  1922. $html = '';
  1923. // display sortable table with messages of the current user
  1924. $table = new SortableTable(
  1925. 'message_inbox',
  1926. ['MessageManager', 'getNumberOfMessages'],
  1927. ['MessageManager', 'getMessageData'],
  1928. 2,
  1929. 20,
  1930. 'DESC'
  1931. );
  1932. $table->setDataFunctionParams(
  1933. ['keyword' => $keyword, 'type' => $type, 'actions' => $actions]
  1934. );
  1935. $table->set_header(0, '', false, ['style' => 'width:15px;']);
  1936. $table->set_header(1, get_lang('Messages'), false);
  1937. $table->set_header(2, get_lang('Date'), true, ['style' => 'width:180px;']);
  1938. $table->set_header(3, get_lang('Modify'), false, ['style' => 'width:120px;']);
  1939. if (isset($_REQUEST['f']) && $_REQUEST['f'] === 'social') {
  1940. $parameters['f'] = 'social';
  1941. $table->set_additional_parameters($parameters);
  1942. }
  1943. $defaultActions = [
  1944. 'delete' => get_lang('DeleteSelectedMessages'),
  1945. 'mark_as_unread' => get_lang('MailMarkSelectedAsUnread'),
  1946. 'mark_as_read' => get_lang('MailMarkSelectedAsRead'),
  1947. ];
  1948. if (!in_array('delete', $actions)) {
  1949. unset($defaultActions['delete']);
  1950. }
  1951. if (!in_array('mark_as_unread', $actions)) {
  1952. unset($defaultActions['mark_as_unread']);
  1953. }
  1954. if (!in_array('mark_as_read', $actions)) {
  1955. unset($defaultActions['mark_as_read']);
  1956. }
  1957. $table->set_form_actions($defaultActions);
  1958. $html .= $table->return_table();
  1959. return $html;
  1960. }
  1961. /**
  1962. * @param string $keyword
  1963. *
  1964. * @return string
  1965. */
  1966. public static function inboxDisplay($keyword = '')
  1967. {
  1968. $success = get_lang('SelectedMessagesDeleted');
  1969. $success_read = get_lang('SelectedMessagesRead');
  1970. $success_unread = get_lang('SelectedMessagesUnRead');
  1971. $currentUserId = api_get_user_id();
  1972. if (isset($_REQUEST['action'])) {
  1973. switch ($_REQUEST['action']) {
  1974. case 'mark_as_unread':
  1975. if (is_array($_POST['id'])) {
  1976. foreach ($_POST['id'] as $index => $messageId) {
  1977. self::update_message_status(
  1978. $currentUserId,
  1979. $messageId,
  1980. MESSAGE_STATUS_UNREAD
  1981. );
  1982. }
  1983. }
  1984. Display::addFlash(Display::return_message(
  1985. $success_unread,
  1986. 'normal',
  1987. false
  1988. ));
  1989. break;
  1990. case 'mark_as_read':
  1991. if (is_array($_POST['id'])) {
  1992. foreach ($_POST['id'] as $index => $messageId) {
  1993. self::update_message_status(
  1994. $currentUserId,
  1995. $messageId,
  1996. MESSAGE_STATUS_NEW
  1997. );
  1998. }
  1999. }
  2000. Display::addFlash(Display::return_message(
  2001. $success_read,
  2002. 'normal',
  2003. false
  2004. ));
  2005. break;
  2006. case 'delete':
  2007. foreach ($_POST['id'] as $index => $messageId) {
  2008. self::delete_message_by_user_receiver($currentUserId, $messageId);
  2009. }
  2010. Display::addFlash(Display::return_message(
  2011. $success,
  2012. 'normal',
  2013. false
  2014. ));
  2015. break;
  2016. case 'deleteone':
  2017. $result = self::delete_message_by_user_receiver($currentUserId, $_GET['id']);
  2018. if ($result) {
  2019. Display::addFlash(
  2020. Display::return_message(
  2021. $success,
  2022. 'confirmation',
  2023. false
  2024. )
  2025. );
  2026. }
  2027. break;
  2028. }
  2029. header('Location: '.api_get_self());
  2030. exit;
  2031. }
  2032. $actions = ['reply', 'mark_as_unread', 'mark_as_read', 'forward', 'delete'];
  2033. $html = self::getMessageGrid(self::MESSAGE_TYPE_INBOX, $keyword, $actions);
  2034. return $html;
  2035. }
  2036. /**
  2037. * @param string $keyword
  2038. *
  2039. * @return string
  2040. */
  2041. public static function getPromotedMessagesGrid($keyword)
  2042. {
  2043. $actions = ['delete'];
  2044. $currentUserId = api_get_user_id();
  2045. $success = get_lang('SelectedMessagesDeleted');
  2046. if (isset($_REQUEST['action'])) {
  2047. switch ($_REQUEST['action']) {
  2048. case 'delete':
  2049. foreach ($_POST['id'] as $index => $messageId) {
  2050. self::delete_message_by_user_receiver($currentUserId, $messageId);
  2051. }
  2052. Display::addFlash(Display::return_message(
  2053. $success,
  2054. 'normal',
  2055. false
  2056. ));
  2057. break;
  2058. case 'deleteone':
  2059. self::delete_message_by_user_receiver($currentUserId, $_GET['id']);
  2060. Display::addFlash(Display::return_message(
  2061. $success,
  2062. 'confirmation',
  2063. false
  2064. ));
  2065. break;
  2066. }
  2067. header('Location: '.api_get_self());
  2068. exit;
  2069. }
  2070. $html = self::getMessageGrid(self::MESSAGE_TYPE_PROMOTED, $keyword, $actions);
  2071. return $html;
  2072. }
  2073. /**
  2074. * @param string $keyword
  2075. *
  2076. * @return string
  2077. */
  2078. public static function outBoxDisplay($keyword)
  2079. {
  2080. $actions = ['delete'];
  2081. $success = get_lang('SelectedMessagesDeleted');
  2082. $currentUserId = api_get_user_id();
  2083. if (isset($_REQUEST['action'])) {
  2084. switch ($_REQUEST['action']) {
  2085. case 'delete':
  2086. foreach ($_POST['id'] as $index => $messageId) {
  2087. self::delete_message_by_user_sender($currentUserId, $messageId);
  2088. }
  2089. Display::addFlash(Display::return_message(
  2090. $success,
  2091. 'normal',
  2092. false
  2093. ));
  2094. break;
  2095. case 'deleteone':
  2096. self::delete_message_by_user_sender($currentUserId, $_GET['id']);
  2097. Display::addFlash(Display::return_message(
  2098. $success,
  2099. 'confirmation',
  2100. false
  2101. ));
  2102. break;
  2103. }
  2104. header('Location: '.api_get_self());
  2105. exit;
  2106. }
  2107. $html = self::getMessageGrid(self::MESSAGE_TYPE_OUTBOX, $keyword, $actions);
  2108. return $html;
  2109. }
  2110. /**
  2111. * @param string $keyword
  2112. *
  2113. * @return string
  2114. */
  2115. public static function outbox_display($keyword = '')
  2116. {
  2117. Session::write('message_sent_search_keyword', $keyword);
  2118. $success = get_lang('SelectedMessagesDeleted').'&nbsp</b><br />
  2119. <a href="outbox.php">'.get_lang('BackToOutbox').'</a>';
  2120. $html = '';
  2121. if (isset($_REQUEST['action'])) {
  2122. switch ($_REQUEST['action']) {
  2123. case 'delete':
  2124. $count = count($_POST['id']);
  2125. if ($count != 0) {
  2126. foreach ($_POST['id'] as $index => $messageId) {
  2127. self::delete_message_by_user_receiver(
  2128. api_get_user_id(),
  2129. $messageId
  2130. );
  2131. }
  2132. }
  2133. $html .= Display::return_message(api_xml_http_response_encode($success), 'normal', false);
  2134. break;
  2135. case 'deleteone':
  2136. self::delete_message_by_user_receiver(api_get_user_id(), $_GET['id']);
  2137. $html .= Display::return_message(api_xml_http_response_encode($success), 'normal', false);
  2138. $html .= '<br/>';
  2139. break;
  2140. }
  2141. }
  2142. // display sortable table with messages of the current user
  2143. $table = new SortableTable(
  2144. 'message_outbox',
  2145. ['MessageManager', 'getNumberOfMessages'],
  2146. ['MessageManager', 'getMessageData'],
  2147. 2,
  2148. 20,
  2149. 'DESC'
  2150. );
  2151. $table->setDataFunctionParams(
  2152. ['keyword' => $keyword, 'type' => self::MESSAGE_TYPE_OUTBOX]
  2153. );
  2154. $table->set_header(0, '', false, ['style' => 'width:15px;']);
  2155. $table->set_header(1, get_lang('Messages'), false);
  2156. $table->set_header(2, get_lang('Date'), true, ['style' => 'width:180px;']);
  2157. $table->set_header(3, get_lang('Modify'), false, ['style' => 'width:70px;']);
  2158. $table->set_form_actions(['delete' => get_lang('DeleteSelectedMessages')]);
  2159. $html .= $table->return_table();
  2160. return $html;
  2161. }
  2162. /**
  2163. * Get the data of the last received messages for a user.
  2164. *
  2165. * @param int $userId The user id
  2166. * @param int $lastId The id of the last received message
  2167. *
  2168. * @return array
  2169. */
  2170. public static function getMessagesFromLastReceivedMessage($userId, $lastId = 0)
  2171. {
  2172. $userId = (int) $userId;
  2173. $lastId = (int) $lastId;
  2174. if (empty($userId)) {
  2175. return [];
  2176. }
  2177. $messagesTable = Database::get_main_table(TABLE_MESSAGE);
  2178. $userTable = Database::get_main_table(TABLE_MAIN_USER);
  2179. $sql = "SELECT m.*, u.user_id, u.lastname, u.firstname
  2180. FROM $messagesTable as m
  2181. INNER JOIN $userTable as u
  2182. ON m.user_sender_id = u.user_id
  2183. WHERE
  2184. m.user_receiver_id = $userId AND
  2185. m.msg_status = ".MESSAGE_STATUS_UNREAD."
  2186. AND m.id > $lastId
  2187. ORDER BY m.send_date DESC";
  2188. $result = Database::query($sql);
  2189. $messages = [];
  2190. if ($result !== false) {
  2191. while ($row = Database::fetch_assoc($result)) {
  2192. $messages[] = $row;
  2193. }
  2194. }
  2195. return $messages;
  2196. }
  2197. /**
  2198. * Check whether a message has attachments.
  2199. *
  2200. * @param int $messageId The message id
  2201. *
  2202. * @return bool Whether the message has attachments return true. Otherwise return false
  2203. */
  2204. public static function hasAttachments($messageId)
  2205. {
  2206. $messageId = (int) $messageId;
  2207. if (empty($messageId)) {
  2208. return false;
  2209. }
  2210. $messageAttachmentTable = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  2211. $conditions = [
  2212. 'where' => [
  2213. 'message_id = ?' => $messageId,
  2214. ],
  2215. ];
  2216. $result = Database::select(
  2217. 'COUNT(1) AS qty',
  2218. $messageAttachmentTable,
  2219. $conditions,
  2220. 'first'
  2221. );
  2222. if (!empty($result)) {
  2223. if ($result['qty'] > 0) {
  2224. return true;
  2225. }
  2226. }
  2227. return false;
  2228. }
  2229. /**
  2230. * @param int $messageId
  2231. *
  2232. * @return array|bool
  2233. */
  2234. public static function getAttachment($messageId)
  2235. {
  2236. $messageId = (int) $messageId;
  2237. if (empty($messageId)) {
  2238. return false;
  2239. }
  2240. $table = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  2241. $conditions = [
  2242. 'where' => [
  2243. 'id = ?' => $messageId,
  2244. ],
  2245. ];
  2246. $result = Database::select(
  2247. '*',
  2248. $table,
  2249. $conditions,
  2250. 'first'
  2251. );
  2252. if (!empty($result)) {
  2253. return $result;
  2254. }
  2255. return false;
  2256. }
  2257. /**
  2258. * @param string $url
  2259. *
  2260. * @return FormValidator
  2261. */
  2262. public static function getSearchForm($url)
  2263. {
  2264. $form = new FormValidator(
  2265. 'search',
  2266. 'post',
  2267. $url,
  2268. null,
  2269. [],
  2270. FormValidator::LAYOUT_INLINE
  2271. );
  2272. $form->addElement(
  2273. 'text',
  2274. 'keyword',
  2275. false,
  2276. [
  2277. 'aria-label' => get_lang('Search'),
  2278. ]
  2279. );
  2280. $form->addButtonSearch(get_lang('Search'));
  2281. return $form;
  2282. }
  2283. /**
  2284. * Send a notification to all admins when a new user is registered.
  2285. *
  2286. * @param User $user
  2287. */
  2288. public static function sendNotificationByRegisteredUser(User $user)
  2289. {
  2290. $tplMailBody = new Template(
  2291. null,
  2292. false,
  2293. false,
  2294. false,
  2295. false,
  2296. false,
  2297. false
  2298. );
  2299. $tplMailBody->assign('user', $user);
  2300. $tplMailBody->assign('is_western_name_order', api_is_western_name_order());
  2301. $tplMailBody->assign(
  2302. 'manageUrl',
  2303. api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$user->getId()
  2304. );
  2305. $layoutContent = $tplMailBody->get_template('mail/new_user_mail_to_admin.tpl');
  2306. $emailsubject = '['.get_lang('UserRegistered').'] '.$user->getUsername();
  2307. $emailbody = $tplMailBody->fetch($layoutContent);
  2308. $admins = UserManager::get_all_administrators();
  2309. foreach ($admins as $admin_info) {
  2310. self::send_message(
  2311. $admin_info['user_id'],
  2312. $emailsubject,
  2313. $emailbody,
  2314. [],
  2315. [],
  2316. null,
  2317. null,
  2318. null,
  2319. null,
  2320. $user->getId()
  2321. );
  2322. }
  2323. }
  2324. /**
  2325. * Get the error log from failed mailing
  2326. * This assumes a complex setup where you have a cron script regularly copying the mail queue log
  2327. * into app/cache/mail/mailq.
  2328. * This can be done with a cron command like (check the location of your mail log file first):.
  2329. *
  2330. * @example 0,30 * * * * root cp /var/log/exim4/mainlog /var/www/chamilo/app/cache/mail/mailq
  2331. *
  2332. * @return array|bool
  2333. */
  2334. public static function failedSentMailErrors()
  2335. {
  2336. $base = api_get_path(SYS_ARCHIVE_PATH).'mail/';
  2337. $mailq = $base.'mailq';
  2338. if (!file_exists($mailq) || !is_readable($mailq)) {
  2339. return false;
  2340. }
  2341. $file = fopen($mailq, 'r');
  2342. $i = 1;
  2343. while (!feof($file)) {
  2344. $line = fgets($file);
  2345. if (trim($line) == '') {
  2346. continue;
  2347. }
  2348. // Get the mail code, something like 1WBumL-0002xg-FF
  2349. if (preg_match('/(.*)\s((.*)-(.*)-(.*))\s<(.*)$/', $line, $codeMatches)) {
  2350. $mail_queue[$i]['code'] = $codeMatches[2];
  2351. }
  2352. $fullMail = $base.$mail_queue[$i]['code'];
  2353. $mailFile = fopen($fullMail, 'r');
  2354. // Get the reason of mail fail
  2355. $iX = 1;
  2356. while (!feof($mailFile)) {
  2357. $mailLine = fgets($mailFile);
  2358. //if ($iX == 4 && preg_match('/(.*):\s(.*)$/', $mailLine, $matches)) {
  2359. if ($iX == 2 &&
  2360. preg_match('/(.*)(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\s(.*)/', $mailLine, $detailsMatches)
  2361. ) {
  2362. $mail_queue[$i]['reason'] = $detailsMatches[3];
  2363. }
  2364. $iX++;
  2365. }
  2366. fclose($mailFile);
  2367. // Get the time of mail fail
  2368. if (preg_match('/^\s?(\d+)(\D+)\s+(.*)$/', $line, $timeMatches)) {
  2369. $mail_queue[$i]['time'] = $timeMatches[1].$timeMatches[2];
  2370. } elseif (preg_match('/^(\s+)((.*)@(.*))\s+(.*)$/', $line, $emailMatches)) {
  2371. $mail_queue[$i]['mail'] = $emailMatches[2];
  2372. $i++;
  2373. }
  2374. }
  2375. fclose($file);
  2376. return array_reverse($mail_queue);
  2377. }
  2378. /**
  2379. * @param int $userId
  2380. *
  2381. * @return array
  2382. */
  2383. public static function getUsersThatHadConversationWithUser($userId)
  2384. {
  2385. $messagesTable = Database::get_main_table(TABLE_MESSAGE);
  2386. $userId = (int) $userId;
  2387. $sql = "SELECT DISTINCT
  2388. user_sender_id
  2389. FROM $messagesTable
  2390. WHERE
  2391. user_receiver_id = ".$userId;
  2392. $result = Database::query($sql);
  2393. $users = Database::store_result($result);
  2394. $userList = [];
  2395. foreach ($users as $userData) {
  2396. $userId = $userData['user_sender_id'];
  2397. if (empty($userId)) {
  2398. continue;
  2399. }
  2400. $userInfo = api_get_user_info($userId);
  2401. if ($userInfo) {
  2402. $userList[$userId] = $userInfo;
  2403. }
  2404. }
  2405. return $userList;
  2406. }
  2407. /**
  2408. * @param int $userId
  2409. * @param int $otherUserId
  2410. *
  2411. * @return array
  2412. */
  2413. public static function getAllMessagesBetweenStudents($userId, $otherUserId)
  2414. {
  2415. $messagesTable = Database::get_main_table(TABLE_MESSAGE);
  2416. $userId = (int) $userId;
  2417. $otherUserId = (int) $otherUserId;
  2418. if (empty($otherUserId) || empty($userId)) {
  2419. return [];
  2420. }
  2421. $sql = "SELECT DISTINCT *
  2422. FROM $messagesTable
  2423. WHERE
  2424. (user_receiver_id = $userId AND user_sender_id = $otherUserId) OR
  2425. (user_receiver_id = $otherUserId AND user_sender_id = $userId)
  2426. ORDER BY send_date DESC
  2427. ";
  2428. $result = Database::query($sql);
  2429. $messages = Database::store_result($result);
  2430. $list = [];
  2431. foreach ($messages as $message) {
  2432. $list[] = $message;
  2433. }
  2434. return $list;
  2435. }
  2436. /**
  2437. * @param string $subject
  2438. * @param string $message
  2439. * @param array $courseInfo
  2440. * @param int $sessionId
  2441. *
  2442. * @return bool
  2443. */
  2444. public static function sendMessageToAllUsersInCourse($subject, $message, $courseInfo, $sessionId = 0)
  2445. {
  2446. if (empty($courseInfo)) {
  2447. return false;
  2448. }
  2449. $senderId = api_get_user_id();
  2450. if (empty($senderId)) {
  2451. return false;
  2452. }
  2453. if (empty($sessionId)) {
  2454. // Course students and teachers
  2455. $users = CourseManager::get_user_list_from_course_code($courseInfo['code']);
  2456. } else {
  2457. // Course-session students and course session coaches
  2458. $users = CourseManager::get_user_list_from_course_code($courseInfo['code'], $sessionId);
  2459. }
  2460. if (empty($users)) {
  2461. return false;
  2462. }
  2463. foreach ($users as $userInfo) {
  2464. self::send_message_simple(
  2465. $userInfo['user_id'],
  2466. $subject,
  2467. $message,
  2468. $senderId,
  2469. false,
  2470. false,
  2471. [],
  2472. false
  2473. );
  2474. }
  2475. }
  2476. /**
  2477. * Clean audio messages already added in the message tool.
  2478. */
  2479. public static function cleanAudioMessage()
  2480. {
  2481. $audioId = Session::read('current_audio_id');
  2482. if (!empty($audioId)) {
  2483. api_remove_uploaded_file_by_id('audio_message', api_get_user_id(), $audioId);
  2484. Session::erase('current_audio_id');
  2485. }
  2486. }
  2487. /**
  2488. * @param int $senderId
  2489. * @param string $subject
  2490. * @param string $message
  2491. */
  2492. public static function sendMessageToAllAdminUsers(
  2493. $senderId,
  2494. $subject,
  2495. $message
  2496. ) {
  2497. $admins = UserManager::get_all_administrators();
  2498. foreach ($admins as $admin) {
  2499. self::send_message_simple($admin['user_id'], $subject, $message, $senderId);
  2500. }
  2501. }
  2502. /**
  2503. * @param int $messageId
  2504. * @param int $userId
  2505. *
  2506. * @return array
  2507. */
  2508. public static function countLikesAndDislikes($messageId, $userId)
  2509. {
  2510. if (!api_get_configuration_value('social_enable_messages_feedback')) {
  2511. return [];
  2512. }
  2513. $messageId = (int) $messageId;
  2514. $userId = (int) $userId;
  2515. $em = Database::getManager();
  2516. $query = $em
  2517. ->createQuery('
  2518. SELECT SUM(l.liked) AS likes, SUM(l.disliked) AS dislikes FROM ChamiloCoreBundle:MessageFeedback l
  2519. WHERE l.message = :message
  2520. ')
  2521. ->setParameters(['message' => $messageId]);
  2522. try {
  2523. $counts = $query->getSingleResult();
  2524. } catch (Exception $e) {
  2525. $counts = ['likes' => 0, 'dislikes' => 0];
  2526. }
  2527. $userLike = $em
  2528. ->getRepository('ChamiloCoreBundle:MessageFeedback')
  2529. ->findOneBy(['message' => $messageId, 'user' => $userId]);
  2530. return [
  2531. 'likes' => (int) $counts['likes'],
  2532. 'dislikes' => (int) $counts['dislikes'],
  2533. 'user_liked' => $userLike ? $userLike->isLiked() : false,
  2534. 'user_disliked' => $userLike ? $userLike->isDisliked() : false,
  2535. ];
  2536. }
  2537. /**
  2538. * @param int $messageId
  2539. * @param int $userId
  2540. * @param int $groupId Optional.
  2541. *
  2542. * @return string
  2543. */
  2544. public static function getLikesButton($messageId, $userId, $groupId = 0)
  2545. {
  2546. if (!api_get_configuration_value('social_enable_messages_feedback')) {
  2547. return '';
  2548. }
  2549. $countLikes = self::countLikesAndDislikes($messageId, $userId);
  2550. $class = $countLikes['user_liked'] ? 'btn-primary' : 'btn-default';
  2551. $btnLike = Display::button(
  2552. 'like',
  2553. Display::returnFontAwesomeIcon('thumbs-up', '', true)
  2554. .PHP_EOL.'<span>'.$countLikes['likes'].'</span>',
  2555. [
  2556. 'title' => get_lang('VoteLike'),
  2557. 'class' => 'btn social-like '.$class,
  2558. 'data-status' => 'like',
  2559. 'data-message' => $messageId,
  2560. 'data-group' => $groupId,
  2561. ]
  2562. );
  2563. $btnDislike = '';
  2564. if (api_get_configuration_value('disable_dislike_option') === false) {
  2565. $disabled = $countLikes['user_disliked'] ? 'btn-danger' : 'btn-default';
  2566. $btnDislike = Display::button(
  2567. 'like',
  2568. Display::returnFontAwesomeIcon('thumbs-down', '', true)
  2569. .PHP_EOL.'<span>'.$countLikes['dislikes'].'</span>',
  2570. [
  2571. 'title' => get_lang('VoteDislike'),
  2572. 'class' => 'btn social-like '.$disabled,
  2573. 'data-status' => 'dislike',
  2574. 'data-message' => $messageId,
  2575. 'data-group' => $groupId,
  2576. ]
  2577. );
  2578. }
  2579. return $btnLike.PHP_EOL.$btnDislike;
  2580. }
  2581. /**
  2582. * Execute the SQL necessary to know the number of messages in the database.
  2583. *
  2584. * @param int $userId The user for which we need the unread messages count
  2585. *
  2586. * @return int The number of unread messages in the database for the given user
  2587. */
  2588. public static function getCountNewMessagesFromDB($userId)
  2589. {
  2590. $userId = (int) $userId;
  2591. if (empty($userId)) {
  2592. return 0;
  2593. }
  2594. $table = Database::get_main_table(TABLE_MESSAGE);
  2595. $sql = "SELECT COUNT(id) as count
  2596. FROM $table
  2597. WHERE
  2598. user_receiver_id = $userId AND
  2599. msg_status = ".MESSAGE_STATUS_UNREAD;
  2600. $result = Database::query($sql);
  2601. $row = Database::fetch_assoc($result);
  2602. return (int) $row['count'];
  2603. }
  2604. }