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.
 
 
 
 
 
 

327 lines
8.8 KiB

  1. <?php
  2. function item_post(&$a) {
  3. if((! local_user()) && (! remote_user()))
  4. return;
  5. require_once('include/security.php');
  6. $uid = $_SESSION['uid'];
  7. $parent = ((x($_POST,'parent')) ? intval($_POST['parent']) : 0);
  8. $parent_item = null;
  9. if($parent) {
  10. $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
  11. intval($parent)
  12. );
  13. if(! count($r)) {
  14. notice( t('Unable to locate original post.') . EOL);
  15. goaway($a->get_baseurl() . "/" . $_POST['return'] );
  16. }
  17. $parent_item = $r[0];
  18. }
  19. $profile_uid = ((x($_POST,'profile_uid')) ? intval($_POST['profile_uid']) : 0);
  20. if(! can_write_wall($a,$profile_uid)) {
  21. notice("Permission denied." . EOL) ;
  22. return;
  23. }
  24. $user = null;
  25. $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
  26. intval($profile_uid)
  27. );
  28. if(count($r))
  29. $user = $r[0];
  30. $str_group_allow = '';
  31. $group_allow = $_POST['group_allow'];
  32. if(is_array($group_allow)) {
  33. array_walk($group_allow,'sanitise_acl');
  34. $str_group_allow = implode('',$group_allow);
  35. }
  36. $str_contact_allow = '';
  37. $contact_allow = $_POST['contact_allow'];
  38. if(is_array($contact_allow)) {
  39. array_walk($contact_allow,'sanitise_acl');
  40. $str_contact_allow = implode('',$contact_allow);
  41. }
  42. $str_group_deny = '';
  43. $group_deny = $_POST['group_deny'];
  44. if(is_array($group_deny)) {
  45. array_walk($group_deny,'sanitise_acl');
  46. $str_group_deny = implode('',$group_deny);
  47. }
  48. $str_contact_deny = '';
  49. $contact_deny = $_POST['contact_deny'];
  50. if(is_array($contact_deny)) {
  51. array_walk($contact_deny,'sanitise_acl');
  52. $str_contact_deny = implode('',$contact_deny);
  53. }
  54. $title = notags(trim($_POST['title']));
  55. $body = escape_tags(trim($_POST['body']));
  56. if(! strlen($body)) {
  57. notice( t('Empty post discarded.') . EOL );
  58. goaway($a->get_baseurl() . "/" . $_POST['return'] );
  59. }
  60. // get contact info for poster
  61. $author = null;
  62. if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
  63. $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
  64. intval($_SESSION['uid'])
  65. );
  66. }
  67. else {
  68. if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
  69. $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
  70. intval($_SESSION['visitor_id'])
  71. );
  72. }
  73. }
  74. if(count($r)) {
  75. $author = $r[0];
  76. $contact_id = $author['id'];
  77. }
  78. // get contact info for owner
  79. if($profile_uid == $_SESSION['uid']) {
  80. $contact_record = $author;
  81. }
  82. else {
  83. $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
  84. intval($profile_uid)
  85. );
  86. if(count($r))
  87. $contact_record = $r[0];
  88. }
  89. $post_type = notags(trim($_POST['type']));
  90. if($post_type == 'net-comment') {
  91. if($parent_item !== null) {
  92. if($parent_item['type'] == 'remote')
  93. $post_type = 'remote-comment';
  94. else
  95. $post_type = 'wall-comment';
  96. }
  97. }
  98. $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
  99. do {
  100. $dups = false;
  101. $hash = random_string();
  102. $uri = "urn:X-dfrn:" . $a->get_hostname() . ':' . $profile_uid . ':' . $hash;
  103. $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
  104. dbesc($uri));
  105. if(count($r))
  106. $dups = true;
  107. } while($dups == true);
  108. $r = q("INSERT INTO `item` (`uid`,`type`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`,
  109. `author-name`, `author-link`, `author-avatar`, `created`,
  110. `edited`, `uri`, `title`, `body`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
  111. VALUES( %d, '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
  112. intval($profile_uid),
  113. dbesc($post_type),
  114. intval($contact_id),
  115. dbesc($contact_record['name']),
  116. dbesc($contact_record['url']),
  117. dbesc($contact_record['thumb']),
  118. dbesc($author['name']),
  119. dbesc($author['url']),
  120. dbesc($author['thumb']),
  121. datetime_convert(),
  122. datetime_convert(),
  123. dbesc($uri),
  124. dbesc($title),
  125. dbesc($body),
  126. dbesc($str_contact_allow),
  127. dbesc($str_group_allow),
  128. dbesc($str_contact_deny),
  129. dbesc($str_group_deny)
  130. );
  131. $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
  132. dbesc($uri));
  133. if(count($r)) {
  134. $post_id = $r[0]['id'];
  135. if($parent) {
  136. // This item is the last leaf and gets the comment box, clear any ancestors
  137. $r = q("UPDATE `item` SET `last-child` = 0 WHERE `parent` = %d ",
  138. intval($parent)
  139. );
  140. // Inherit ACL's from the parent item.
  141. // TODO merge with subsequent UPDATE operation and save a db write
  142. $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
  143. WHERE `id` = %d LIMIT 1",
  144. dbesc($parent_item['allow_cid']),
  145. dbesc($parent_item['allow_gid']),
  146. dbesc($parent_item['deny_cid']),
  147. dbesc($parent_item['deny_gid']),
  148. intval($post_id)
  149. );
  150. if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
  151. require_once('bbcode.php');
  152. $from = $author['name'];
  153. $tpl = file_get_contents('view/cmnt_received_eml.tpl');
  154. $email_tpl = replace_macros($tpl, array(
  155. '$sitename' => $a->config['sitename'],
  156. '$siteurl' => $a->get_baseurl(),
  157. '$username' => $user['username'],
  158. '$email' => $user['email'],
  159. '$from' => $from,
  160. '$body' => strip_tags(bbcode($body))
  161. ));
  162. $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
  163. $email_tpl,t("From: Administrator@") . $a->get_hostname() );
  164. }
  165. }
  166. else {
  167. $parent = $post_id;
  168. if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
  169. require_once('bbcode.php');
  170. $from = $author['name'];
  171. $tpl = file_get_contents('view/wall_received_eml.tpl');
  172. $email_tpl = replace_macros($tpl, array(
  173. '$sitename' => $a->config['sitename'],
  174. '$siteurl' => $a->get_baseurl(),
  175. '$username' => $user['username'],
  176. '$email' => $user['email'],
  177. '$from' => $from,
  178. '$body' => strip_tags(bbcode($body))
  179. ));
  180. $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
  181. $email_tpl,t("From: Administrator@") . $a->get_hostname() );
  182. }
  183. }
  184. $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `last-child` = 1, `visible` = 1
  185. WHERE `id` = %d LIMIT 1",
  186. intval($parent),
  187. dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
  188. intval($post_id)
  189. );
  190. // photo comments turn the corresponding item visible to the profile wall
  191. if(! $parent_item['visible']) {
  192. $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
  193. intval($parent_item['id'])
  194. );
  195. }
  196. }
  197. $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
  198. proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &",
  199. array(),$foo));
  200. goaway($a->get_baseurl() . "/" . $_POST['return'] );
  201. return; // NOTREACHED
  202. }
  203. function item_content(&$a) {
  204. if((! local_user()) && (! remote_user()))
  205. return;
  206. require_once('include/security.php');
  207. $uid = $_SESSION['uid'];
  208. if(($a->argc == 3) && ($a->argv[1] == 'drop') && intval($a->argv[2])) {
  209. // locate item to be deleted
  210. $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
  211. intval($a->argv[2])
  212. );
  213. if(! count($r)) {
  214. notice( t('Item not found.') . EOL);
  215. goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
  216. }
  217. $item = $r[0];
  218. // check if logged in user is either the author or owner of this item
  219. if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
  220. // delete the item
  221. $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s' WHERE `id` = %d LIMIT 1",
  222. dbesc(datetime_convert()),
  223. intval($item['id'])
  224. );
  225. // If item is a link to a photo resource, nuke all the associated photos
  226. // (visitors will not have photo resources)
  227. // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
  228. // generate a resource-id and therefore aren't intimately linked to the item.
  229. if(strlen($item['resource-id'])) {
  230. $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
  231. dbesc($item['resource-id']),
  232. intval($item['uid'])
  233. );
  234. // ignore the result
  235. }
  236. // If it's the parent of a comment thread, kill all the kids
  237. if($item['uri'] == $item['parent-uri']) {
  238. $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
  239. WHERE `parent-uri` = '%s' AND `uid` = %d ",
  240. dbesc(datetime_convert()),
  241. dbesc($item['parent-uri']),
  242. intval($item['uid'])
  243. );
  244. // ignore the result
  245. }
  246. $drop_id = intval($item['id']);
  247. $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
  248. // send the notification upstream/downstream as the case may be
  249. proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" &",
  250. array(),$foo));
  251. goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
  252. return; //NOTREACHED
  253. }
  254. else {
  255. notice( t('Permission denied.') . EOL);
  256. goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
  257. return; //NOTREACHED
  258. }
  259. }
  260. }