Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

294 lines
8.2 KiB

  1. <?php
  2. require_once('view/acl_selectors.php');
  3. function message_init(&$a) {
  4. }
  5. function message_post(&$a) {
  6. if(! local_user()) {
  7. notice( t('Permission denied.') . EOL);
  8. return;
  9. }
  10. $replyto = notags(trim($_POST['replyto']));
  11. $recipient = intval($_POST['messageto']);
  12. $subject = notags(trim($_POST['subject']));
  13. $body = escape_tags(trim($_POST['body']));
  14. if(! $recipient) {
  15. notice( t('No recipient selected.') . EOL );
  16. return;
  17. }
  18. $me = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
  19. intval($_SESSION['uid'])
  20. );
  21. $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  22. intval($recipient),
  23. intval($_SESSION['uid'])
  24. );
  25. if(! (count($me) && (count($contact)))) {
  26. notice( t('Unable to locate contact information.') . EOL );
  27. return;
  28. }
  29. $hash = random_string();
  30. $uri = 'urn:X-dfrn:' . $a->get_baseurl() . ':' . $_SESSION['uid'] . ':' . $hash ;
  31. if(! strlen($replyto))
  32. $replyto = $uri;
  33. $r = q("INSERT INTO `mail` ( `uid`, `from-name`, `from-photo`, `from-url`,
  34. `contact-id`, `title`, `body`, `delivered`, `seen`, `replied`, `uri`, `parent-uri`, `created`)
  35. VALUES ( %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s' )",
  36. intval($_SESSION['uid']),
  37. dbesc($me[0]['name']),
  38. dbesc($me[0]['thumb']),
  39. dbesc($me[0]['url']),
  40. intval($recipient),
  41. dbesc($subject),
  42. dbesc($body),
  43. 0,
  44. 1,
  45. 0,
  46. dbesc($uri),
  47. dbesc($replyto),
  48. datetime_convert()
  49. );
  50. $r = q("SELECT * FROM `mail` WHERE `uri` = '%s' and `uid` = %d LIMIT 1",
  51. dbesc($uri),
  52. intval($_SESSION['uid'])
  53. );
  54. if(count($r))
  55. $post_id = $r[0]['id'];
  56. $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
  57. if($post_id) {
  58. proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"mail\" \"$post_id\" &",
  59. array(),$foo));
  60. notice( t('Message sent.') . EOL );
  61. }
  62. else {
  63. notice( t('Message could not be sent.') . EOL );
  64. }
  65. return;
  66. }
  67. function message_content(&$a) {
  68. if(! local_user()) {
  69. notice( t('Permission denied.') . EOL);
  70. return;
  71. }
  72. $myprofile = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
  73. $tpl = file_get_contents('view/mail_head.tpl');
  74. $header = replace_macros($tpl, array(
  75. '$messages' => t('Messages'),
  76. '$inbox' => t('Inbox'),
  77. '$outbox' => t('Outbox'),
  78. '$new' => t('New Message')
  79. ));
  80. if(($a->argc == 3) && ($a->argv[1] == 'drop' || $a->argv[1] == 'dropconv')) {
  81. if(! intval($a->argv[2]))
  82. return;
  83. $cmd = $a->argv[1];
  84. if($cmd == 'drop') {
  85. $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  86. intval($a->argv[2]),
  87. intval($_SESSION['uid'])
  88. );
  89. if($r) {
  90. notice( t('Message deleted.') . EOL );
  91. }
  92. goaway($a->get_baseurl() . '/message' );
  93. }
  94. else {
  95. $r = q("SELECT `parent-uri` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  96. intval($a->argv[2]),
  97. intval($_SESSION['uid'])
  98. );
  99. if(count($r)) {
  100. $parent = $r[0]['parent-uri'];
  101. $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
  102. dbesc($parent),
  103. intval($_SESSION['uid'])
  104. );
  105. if($r)
  106. notice( t('Conversation removed.') . EOL );
  107. }
  108. goaway($a->get_baseurl() . '/message' );
  109. }
  110. }
  111. if(($a->argc > 2) && ($a->argv[1] == 'redeliver') && intval($a->argv[2])) {
  112. $post_id = intval($a->argv[2]);
  113. $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
  114. proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"mail\" \"$post_id\" &",
  115. array(),$foo));
  116. goaway($a->get_baseurl() . '/message' );
  117. }
  118. if(($a->argc > 1) && ($a->argv[1] == 'new')) {
  119. $tpl = file_get_contents('view/msg-header.tpl');
  120. $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
  121. $select .= contact_select('messageto','message-to-select', false, 4, true);
  122. $tpl = file_get_contents('view/prv_message.tpl');
  123. $o .= replace_macros($tpl,array(
  124. '$header' => t('Send Private Message'),
  125. '$to' => t('To:'),
  126. '$subject' => t('Subject:'),
  127. '$subjtxt' => '',
  128. '$readonly' => '',
  129. '$yourmessage' => t('Your message:'),
  130. '$select' => $select,
  131. '$parent' => '',
  132. '$upload' => t('Upload photo'),
  133. '$insert' => t('Insert web link'),
  134. '$wait' => t('Please wait')
  135. ));
  136. return $o;
  137. }
  138. if(($a->argc == 1) || ($a->argc == 2 && $a->argv[1] == 'sent')) {
  139. $o .= $header;
  140. if($a->argc == 2)
  141. $eq = '='; // I'm not going to bother escaping this.
  142. else
  143. $eq = '!='; // or this.
  144. $r = q("SELECT count(*) AS `total` FROM `mail`
  145. WHERE `mail`.`uid` = %d AND `from-url` $eq '%s' GROUP BY `parent-uri` ORDER BY `created` DESC",
  146. intval($_SESSION['uid']),
  147. dbesc($myprofile)
  148. );
  149. if(count($r))
  150. $a->set_pager_total($r[0]['total']);
  151. $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
  152. `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb`
  153. FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
  154. WHERE `mail`.`uid` = %d AND `from-url` $eq '%s' GROUP BY `parent-uri` ORDER BY `created` DESC LIMIT %d , %d ",
  155. intval($_SESSION['uid']),
  156. dbesc($myprofile),
  157. intval($a->pager['start']),
  158. intval($a->pager['itemspage'])
  159. );
  160. if(! count($r)) {
  161. notice( t('No messages.') . EOL);
  162. return $o;
  163. }
  164. $tpl = file_get_contents('view/mail_list.tpl');
  165. foreach($r as $rr) {
  166. $o .= replace_macros($tpl, array(
  167. '$id' => $rr['id'],
  168. '$from_name' =>$rr['from-name'],
  169. '$from_url' => $a->get_baseurl() . '/redir/' . $rr['contact-id'],
  170. '$from_photo' => $rr['from-photo'],
  171. '$subject' => (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'),
  172. '$delete' => t('Delete conversation'),
  173. '$body' => $rr['body'],
  174. '$to_name' => $rr['name'],
  175. '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'],'D, d M Y - g:i A')
  176. ));
  177. }
  178. $o .= paginate($a);
  179. return $o;
  180. }
  181. if(($a->argc > 1) && (intval($a->argv[1]))) {
  182. $o .= $header;
  183. $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
  184. FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
  185. WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
  186. intval($_SESSION['uid']),
  187. intval($a->argv[1])
  188. );
  189. if(count($r)) {
  190. $contact_id = $r[0]['contact-id'];
  191. $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
  192. FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
  193. WHERE `mail`.`uid` = %d AND `mail`.`parent-uri` = '%s' ORDER BY `mail`.`created` ASC",
  194. intval($_SESSION['uid']),
  195. dbesc($r[0]['parent-uri'])
  196. );
  197. }
  198. if(! count($messages)) {
  199. notice( t('Message not available.') . EOL );
  200. return $o;
  201. }
  202. $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
  203. dbesc($r[0]['parent-uri']),
  204. intval($_SESSION['uid'])
  205. );
  206. require_once("include/bbcode.php");
  207. $tpl = file_get_contents('view/msg-header.tpl');
  208. $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
  209. $tpl = file_get_contents('view/mail_conv.tpl');
  210. foreach($messages as $message) {
  211. $o .= replace_macros($tpl, array(
  212. '$id' => $message['id'],
  213. '$from_name' =>$message['from-name'],
  214. '$from_url' => (($message['from-url'] == $myprofile)
  215. ? $myprofile : $a->get_baseurl() . '/redir/' . $message['contact-id']),
  216. '$from_photo' => $message['from-photo'],
  217. '$subject' => $message['title'],
  218. '$body' => bbcode($message['body']),
  219. '$delete' => t('Delete message'),
  220. '$to_name' => $message['name'],
  221. '$date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A')
  222. ));
  223. }
  224. $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
  225. $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
  226. $tpl = file_get_contents('view/prv_message.tpl');
  227. $o .= replace_macros($tpl,array(
  228. '$header' => t('Send Reply'),
  229. '$to' => t('To:'),
  230. '$subject' => t('Subject:'),
  231. '$subjtxt' => $message['title'],
  232. '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
  233. '$yourmessage' => t('Your message:'),
  234. '$select' => $select,
  235. '$parent' => $parent,
  236. '$upload' => t('Upload photo'),
  237. '$insert' => t('Insert web link'),
  238. '$wait' => t('Please wait')
  239. ));
  240. return $o;
  241. }
  242. }