PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/social/mod/messages/start.php

https://github.com/melvincarvalho/foafme
PHP | 338 lines | 171 code | 72 blank | 95 comment | 35 complexity | a94d016273834749acb467c8038f9418 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, AGPL-3.0, Apache-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Elgg internal messages plugin
  4. * This plugin lets user send each other messages.
  5. *
  6. * @package ElggMessages
  7. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
  8. * @author Curverider Ltd <info@elgg.com>
  9. * @copyright Curverider Ltd 2008-2010
  10. * @link http://elgg.com/
  11. */
  12. /**
  13. * Messages initialisation
  14. *
  15. * These parameters are required for the event API, but we won't use them:
  16. *
  17. * @param unknown_type $event
  18. * @param unknown_type $object_type
  19. * @param unknown_type $object
  20. */
  21. function messages_init() {
  22. // Load system configuration
  23. global $CONFIG;
  24. //add submenu options
  25. if (get_context() == "messages") {
  26. add_submenu_item(elgg_echo('messages:compose'), $CONFIG->wwwroot . "mod/messages/send.php");
  27. add_submenu_item(elgg_echo('messages:inbox'), $CONFIG->wwwroot . "pg/messages/" . $_SESSION['user']->username);
  28. add_submenu_item(elgg_echo('messages:sentmessages'), $CONFIG->wwwroot . "mod/messages/sent.php");
  29. }
  30. // Extend system CSS with our own styles, which are defined in the shouts/css view
  31. elgg_extend_view('css','messages/css');
  32. // Extend the elgg topbar
  33. elgg_extend_view('elgg_topbar/extend','messages/topbar');
  34. // Register a page handler, so we can have nice URLs
  35. register_page_handler('messages','messages_page_handler');
  36. // Register a URL handler for shouts posts
  37. register_entity_url_handler('messages_url','object','messages');
  38. // Extend hover-over and profile menu
  39. elgg_extend_view('profile/menu/links','messages/menu');
  40. // Register a notification handler for site messages
  41. register_notification_handler("site", "messages_site_notify_handler");
  42. register_plugin_hook('notify:entity:message','object','messages_notification_msg');
  43. if (is_callable('register_notification_object'))
  44. register_notification_object('object','messages',elgg_echo('messages:new'));
  45. // Shares widget
  46. // add_widget_type('messages',elgg_echo("messages:recent"),elgg_echo("messages:widget:description"));
  47. // Override metadata permissions
  48. register_plugin_hook('permissions_check:metadata','object','messages_can_edit_metadata');
  49. }
  50. /**
  51. * Override the canEditMetadata function to return true for messages
  52. *
  53. */
  54. function messages_can_edit_metadata($hook_name, $entity_type, $return_value, $parameters) {
  55. global $messagesendflag;
  56. if ($messagesendflag == 1) {
  57. $entity = $parameters['entity'];
  58. if ($entity->getSubtype() == "messages") {
  59. return true;
  60. }
  61. }
  62. return $return_value;
  63. }
  64. /**
  65. * Override the canEdit function to return true for messages within a particular context.
  66. *
  67. */
  68. function messages_can_edit($hook_name, $entity_type, $return_value, $parameters) {
  69. global $messagesendflag;
  70. if ($messagesendflag == 1) {
  71. $entity = $parameters['entity'];
  72. if ($entity->getSubtype() == "messages") {
  73. return true;
  74. }
  75. }
  76. return $return_value;
  77. }
  78. /**
  79. * We really don't want to send a notification message when a message is sent, if the method is messages ...
  80. *
  81. */
  82. function messages_notification_msg($hook_name, $entity_type, $return_value, $parameters) {
  83. global $CONFIG, $messages_pm;
  84. if ($parameters['entity'] instanceof ElggEntity) {
  85. if ($parameters['entity']->getSubtype() == 'messages') {
  86. return false;
  87. /*if (!$messages_pm) return false;
  88. if ($parameters['method'] == 'email') {
  89. return sprintf(
  90. elgg_echo('messages:email:body'),
  91. get_loggedin_user()->name,
  92. strip_tags($parameters['entity']->description),
  93. $CONFIG->wwwroot . "pg/messages/" . $user->username,
  94. get_loggedin_user()->name,
  95. $CONFIG->wwwroot . "mod/messages/send.php?send_to=" . get_loggedin_user()->guid
  96. );
  97. } else if ($parameters['method'] == 'site') return false;*/
  98. }
  99. }
  100. return null;
  101. }
  102. /**
  103. * Override the canEdit function to return true for messages within a particular context.
  104. *
  105. */
  106. function messages_can_edit_container($hook_name, $entity_type, $return_value, $parameters) {
  107. global $messagesendflag;
  108. if ($messagesendflag == 1) {
  109. return true;
  110. }
  111. return $return_value;
  112. }
  113. /**
  114. * Send an internal message
  115. *
  116. * @param string $subject The subject line of the message
  117. * @param string $body The body of the mesage
  118. * @param int $send_to The GUID of the user to send to
  119. * @param int $from Optionally, the GUID of the user to send from
  120. * @param int $reply The GUID of the message to reply from (default: none)
  121. * @param true|false $notify Send a notification (default: true)
  122. * @param true|false $add_to_sent If true (default), will add a message to the sender's 'sent' tray
  123. * @return true|false Depending on success
  124. */
  125. function messages_send($subject, $body, $send_to, $from = 0, $reply = 0, $notify = true, $add_to_sent = true) {
  126. global $messagesendflag;
  127. $messagesendflag = 1;
  128. global $messages_pm;
  129. if ($notify) {
  130. $messages_pm = 1;
  131. } else {
  132. $messages_pm = 0;
  133. }
  134. // If $from == 0, set to current user
  135. if ($from == 0)
  136. $from = (int) get_loggedin_user()->guid;
  137. // Initialise a new ElggObject
  138. $message_to = new ElggObject();
  139. $message_sent = new ElggObject();
  140. // Tell the system it's a message
  141. $message_to->subtype = "messages";
  142. $message_sent->subtype = "messages";
  143. // Set its owner to the current user
  144. // $message_to->owner_guid = $_SESSION['user']->getGUID();
  145. $message_to->owner_guid = $send_to;
  146. $message_to->container_guid = $send_to;
  147. $message_sent->owner_guid = $from;
  148. $message_sent->container_guid = $from;
  149. // For now, set its access to public (we'll add an access dropdown shortly)
  150. $message_to->access_id = ACCESS_PUBLIC;
  151. $message_sent->access_id = ACCESS_PUBLIC;
  152. // Set its description appropriately
  153. $message_to->title = $subject;
  154. $message_to->description = $body;
  155. $message_sent->title = $subject;
  156. $message_sent->description = $body;
  157. // set the metadata
  158. $message_to->toId = $send_to; // the user receiving the message
  159. $message_to->fromId = $from; // the user receiving the message
  160. $message_to->readYet = 0; // this is a toggle between 0 / 1 (1 = read)
  161. $message_to->hiddenFrom = 0; // this is used when a user deletes a message in their sentbox, it is a flag
  162. $message_to->hiddenTo = 0; // this is used when a user deletes a message in their inbox
  163. $message_sent->toId = $send_to; // the user receiving the message
  164. $message_sent->fromId = $from; // the user receiving the message
  165. $message_sent->readYet = 0; // this is a toggle between 0 / 1 (1 = read)
  166. $message_sent->hiddenFrom = 0; // this is used when a user deletes a message in their sentbox, it is a flag
  167. $message_sent->hiddenTo = 0; // this is used when a user deletes a message in their inbox
  168. $message_to->msg = 1;
  169. $message_sent->msg = 1;
  170. // Save the copy of the message that goes to the recipient
  171. $success = $message_to->save();
  172. // Save the copy of the message that goes to the sender
  173. if ($add_to_sent) $success2 = $message_sent->save();
  174. $message_to->access_id = ACCESS_PRIVATE;
  175. $message_to->save();
  176. if ($add_to_sent) {
  177. $message_sent->access_id = ACCESS_PRIVATE;
  178. $message_sent->save();
  179. }
  180. // if the new message is a reply then create a relationship link between the new message
  181. // and the message it is in reply to
  182. if($reply && $success){
  183. $create_relationship = add_entity_relationship($message_sent->guid, "reply", $reply);
  184. }
  185. global $CONFIG;
  186. $message_contents = strip_tags($body);
  187. if ($send_to != get_loggedin_user() && $notify)
  188. notify_user($send_to, get_loggedin_user()->guid, elgg_echo('messages:email:subject'),
  189. sprintf(
  190. elgg_echo('messages:email:body'),
  191. get_loggedin_user()->name,
  192. $message_contents,
  193. $CONFIG->wwwroot . "pg/messages/" . $user->username,
  194. get_loggedin_user()->name,
  195. $CONFIG->wwwroot . "mod/messages/send.php?send_to=" . get_loggedin_user()->guid
  196. )
  197. );
  198. $messagesendflag = 0;
  199. return $success;
  200. }
  201. /**
  202. * messages page handler; allows the use of fancy URLs
  203. *
  204. * @param array $page From the page_handler function
  205. * @return true|false Depending on success
  206. */
  207. function messages_page_handler($page) {
  208. // The first component of a messages URL is the username
  209. if (isset($page[0])) {
  210. set_input('username',$page[0]);
  211. }
  212. // The second part dictates what we're doing
  213. if (isset($page[1])) {
  214. switch($page[1]) {
  215. case "read": set_input('message',$page[2]);
  216. include(dirname(__FILE__) . "/read.php");
  217. return true;
  218. break;
  219. }
  220. // If the URL is just 'messages/username', or just 'messages/', load the standard messages index
  221. } else {
  222. include(dirname(__FILE__) . "/index.php");
  223. return true;
  224. }
  225. return false;
  226. }
  227. function messages_url($message) {
  228. global $CONFIG;
  229. return $CONFIG->url . "pg/messages/" . $message->getOwnerEntity()->username . "/read/" . $message->getGUID();
  230. }
  231. // A simple function to count the number of messages that are unread in a user's inbox
  232. function count_unread_messages() {
  233. //get the users inbox messages
  234. //$num_messages = get_entities_from_metadata("toId", $_SESSION['user']->getGUID(), "object", "messages", 0, 10, 0, "", 0, false);
  235. $num_messages = elgg_get_entities_from_metadata(array('metadata_name_value_pairs' => array(
  236. 'toId' => $_SESSION['user']->guid,
  237. 'readYet' => 0,
  238. 'msg' => 1
  239. ), 'types' => 'object', 'subtypes' => 'messages', 'owner_guid' => $_SESSION['user']->guid, 'limit' => 9999));
  240. if (is_array($num_messages))
  241. $counter = sizeof($num_messages);
  242. else
  243. $counter = 0;
  244. return $counter;
  245. }
  246. function messages_site_notify_handler(ElggEntity $from, ElggUser $to, $subject, $message, array $params = NULL)
  247. {
  248. global $CONFIG;
  249. if (!$from)
  250. throw new NotificationException(sprintf(elgg_echo('NotificationException:MissingParameter'), 'from'));
  251. if (!$to)
  252. throw new NotificationException(sprintf(elgg_echo('NotificationException:MissingParameter'), 'to'));
  253. global $messages_pm;
  254. if (!$messages_pm)
  255. return messages_send($subject,$message,$to->guid,$from->guid,0,false,false);
  256. else return true;
  257. }
  258. // Make sure the messages initialisation function is called on initialisation
  259. register_elgg_event_handler('init','system','messages_init');
  260. register_plugin_hook('permissions_check','object','messages_can_edit');
  261. register_plugin_hook('container_permissions_check','object','messages_can_edit_container');
  262. // Register actions
  263. global $CONFIG;
  264. register_action("messages/send",false,$CONFIG->pluginspath . "messages/actions/send.php");
  265. register_action("messages/delete",false,$CONFIG->pluginspath . "messages/actions/delete.php");
  266. ?>