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.

510 lines
16 KiB

10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
11 years ago
  1. <?php
  2. // Included here for completeness, but this is a very dangerous operation.
  3. // It is the caller's responsibility to confirm the requestor's intent and
  4. // authorisation to do this.
  5. function user_remove($uid) {
  6. if(! $uid)
  7. return;
  8. $a = get_app();
  9. logger('Removing user: ' . $uid);
  10. $r = q("select * from user where uid = %d limit 1", intval($uid));
  11. call_hooks('remove_user',$r[0]);
  12. // save username (actually the nickname as it is guaranteed
  13. // unique), so it cannot be re-registered in the future.
  14. q("insert into userd ( username ) values ( '%s' )",
  15. $r[0]['nickname']
  16. );
  17. // don't delete yet, will be done later when contacts have deleted my stuff
  18. // q("DELETE FROM `contact` WHERE `uid` = %d", intval($uid));
  19. q("DELETE FROM `gcign` WHERE `uid` = %d", intval($uid));
  20. q("DELETE FROM `group` WHERE `uid` = %d", intval($uid));
  21. q("DELETE FROM `group_member` WHERE `uid` = %d", intval($uid));
  22. q("DELETE FROM `intro` WHERE `uid` = %d", intval($uid));
  23. q("DELETE FROM `event` WHERE `uid` = %d", intval($uid));
  24. q("DELETE FROM `item` WHERE `uid` = %d", intval($uid));
  25. q("DELETE FROM `item_id` WHERE `uid` = %d", intval($uid));
  26. q("DELETE FROM `mail` WHERE `uid` = %d", intval($uid));
  27. q("DELETE FROM `mailacct` WHERE `uid` = %d", intval($uid));
  28. q("DELETE FROM `manage` WHERE `uid` = %d", intval($uid));
  29. q("DELETE FROM `notify` WHERE `uid` = %d", intval($uid));
  30. q("DELETE FROM `photo` WHERE `uid` = %d", intval($uid));
  31. q("DELETE FROM `attach` WHERE `uid` = %d", intval($uid));
  32. q("DELETE FROM `profile` WHERE `uid` = %d", intval($uid));
  33. q("DELETE FROM `profile_check` WHERE `uid` = %d", intval($uid));
  34. q("DELETE FROM `pconfig` WHERE `uid` = %d", intval($uid));
  35. q("DELETE FROM `search` WHERE `uid` = %d", intval($uid));
  36. q("DELETE FROM `spam` WHERE `uid` = %d", intval($uid));
  37. // don't delete yet, will be done later when contacts have deleted my stuff
  38. // q("DELETE FROM `user` WHERE `uid` = %d", intval($uid));
  39. q("UPDATE `user` SET `account_removed` = 1, `account_expires_on` = UTC_TIMESTAMP() WHERE `uid` = %d", intval($uid));
  40. proc_run('php', "include/notifier.php", "removeme", $uid);
  41. // Send an update to the directory
  42. proc_run('php', "include/directory.php", $r[0]['url']);
  43. if($uid == local_user()) {
  44. unset($_SESSION['authenticated']);
  45. unset($_SESSION['uid']);
  46. goaway($a->get_baseurl());
  47. }
  48. }
  49. function contact_remove($id) {
  50. $r = q("select uid from contact where id = %d limit 1",
  51. intval($id)
  52. );
  53. if((! count($r)) || (! intval($r[0]['uid'])))
  54. return;
  55. $archive = get_pconfig($r[0]['uid'], 'system','archive_removed_contacts');
  56. if($archive) {
  57. q("update contact set `archive` = 1, `network` = 'none', `writable` = 0 where id = %d",
  58. intval($id)
  59. );
  60. return;
  61. }
  62. q("DELETE FROM `contact` WHERE `id` = %d",
  63. intval($id)
  64. );
  65. q("DELETE FROM `item` WHERE `contact-id` = %d ",
  66. intval($id)
  67. );
  68. q("DELETE FROM `photo` WHERE `contact-id` = %d ",
  69. intval($id)
  70. );
  71. q("DELETE FROM `mail` WHERE `contact-id` = %d ",
  72. intval($id)
  73. );
  74. q("DELETE FROM `event` WHERE `cid` = %d ",
  75. intval($id)
  76. );
  77. q("DELETE FROM `queue` WHERE `cid` = %d ",
  78. intval($id)
  79. );
  80. }
  81. // sends an unfriend message. Does not remove the contact
  82. function terminate_friendship($user,$self,$contact) {
  83. $a = get_app();
  84. require_once('include/datetime.php');
  85. if($contact['network'] === NETWORK_OSTATUS) {
  86. $slap = replace_macros(get_markup_template('follow_slap.tpl'), array(
  87. '$name' => $user['username'],
  88. '$profile_page' => $a->get_baseurl() . '/profile/' . $user['nickname'],
  89. '$photo' => $self['photo'],
  90. '$thumb' => $self['thumb'],
  91. '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
  92. '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':unfollow:' . get_guid(32),
  93. '$title' => '',
  94. '$type' => 'text',
  95. '$content' => t('stopped following'),
  96. '$nick' => $user['nickname'],
  97. '$verb' => 'http://ostatus.org/schema/1.0/unfollow', // ACTIVITY_UNFOLLOW,
  98. '$ostat_follow' => '' // '<as:verb>http://ostatus.org/schema/1.0/unfollow</as:verb>' . "\r\n"
  99. ));
  100. if((x($contact,'notify')) && (strlen($contact['notify']))) {
  101. require_once('include/salmon.php');
  102. slapper($user,$contact['notify'],$slap);
  103. }
  104. }
  105. elseif($contact['network'] === NETWORK_DIASPORA) {
  106. require_once('include/diaspora.php');
  107. diaspora_unshare($user,$contact);
  108. }
  109. elseif($contact['network'] === NETWORK_DFRN) {
  110. require_once('include/items.php');
  111. dfrn_deliver($user,$contact,'placeholder', 1);
  112. }
  113. }
  114. // Contact has refused to recognise us as a friend. We will start a countdown.
  115. // If they still don't recognise us in 32 days, the relationship is over,
  116. // and we won't waste any more time trying to communicate with them.
  117. // This provides for the possibility that their database is temporarily messed
  118. // up or some other transient event and that there's a possibility we could recover from it.
  119. if(! function_exists('mark_for_death')) {
  120. function mark_for_death($contact) {
  121. if($contact['archive'])
  122. return;
  123. if($contact['term-date'] == '0000-00-00 00:00:00') {
  124. q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d",
  125. dbesc(datetime_convert()),
  126. intval($contact['id'])
  127. );
  128. }
  129. else {
  130. // TODO: We really should send a notification to the owner after 2-3 weeks
  131. // so they won't be surprised when the contact vanishes and can take
  132. // remedial action if this was a serious mistake or glitch
  133. $expiry = $contact['term-date'] . ' + 32 days ';
  134. if(datetime_convert() > datetime_convert('UTC','UTC',$expiry)) {
  135. // relationship is really truly dead.
  136. // archive them rather than delete
  137. // though if the owner tries to unarchive them we'll start the whole process over again
  138. q("update contact set `archive` = 1 where id = %d",
  139. intval($contact['id'])
  140. );
  141. q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact['id']), intval($contact['uid']));
  142. //contact_remove($contact['id']);
  143. }
  144. }
  145. }}
  146. if(! function_exists('unmark_for_death')) {
  147. function unmark_for_death($contact) {
  148. // It's a miracle. Our dead contact has inexplicably come back to life.
  149. q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d",
  150. dbesc('0000-00-00 00:00:00'),
  151. intval($contact['id'])
  152. );
  153. }}
  154. function get_contact_details_by_url($url, $uid = -1) {
  155. require_once("mod/proxy.php");
  156. require_once("include/bbcode.php");
  157. if ($uid == -1)
  158. $uid = local_user();
  159. $r = q("SELECT `url`, `name`, `nick`, `addr`. `photo`, `location`, `about`, `keywords`, `gender`, `community`, `network` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
  160. dbesc(normalise_link($url)));
  161. if ($r)
  162. $profile = $r[0];
  163. else {
  164. $r = q("SELECT `url`, `name`, `nick`, `avatar` AS `photo`, `location`, `about` FROM `unique_contacts` WHERE `url` = '%s'",
  165. dbesc(normalise_link($url)));
  166. if (count($r)) {
  167. $profile = $r[0];
  168. $profile["keywords"] = "";
  169. $profile["gender"] = "";
  170. $profile["community"] = false;
  171. $profile["network"] = "";
  172. $profile["addr"] = "";
  173. }
  174. }
  175. // Fetching further contact data from the contact table
  176. $r = q("SELECT `id`, `uid`, `url`, `network`, `name`, `nick`, `addr`, `location`, `about`, `keywords`, `gender`, `photo`, `addr`, `forum`, `prv`, `bd` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d AND `network` = '%s'",
  177. dbesc(normalise_link($url)), intval($uid), dbesc($profile["network"]));
  178. if (!count($r))
  179. $r = q("SELECT `id`, `uid`, `url`, `network`, `name`, `nick`, `addr`, `location`, `about`, `keywords`, `gender`, `photo`, `addr`, `forum`, `prv`, `bd` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
  180. dbesc(normalise_link($url)), intval($uid));
  181. if (!count($r))
  182. $r = q("SELECT `id`, `uid`, `url`, `network`, `name`, `nick`, `addr`, `location`, `about`, `keywords`, `gender`, `photo`, `addr`, `forum`, `prv`, `bd` FROM `contact` WHERE `nurl` = '%s' AND `uid` = 0",
  183. dbesc(normalise_link($url)));
  184. if ($r) {
  185. if (isset($r[0]["url"]) AND $r[0]["url"])
  186. $profile["url"] = $r[0]["url"];
  187. if (isset($r[0]["name"]) AND $r[0]["name"])
  188. $profile["name"] = $r[0]["name"];
  189. if (isset($r[0]["nick"]) AND $r[0]["nick"] AND ($profile["nick"] == ""))
  190. $profile["nick"] = $r[0]["nick"];
  191. if (isset($r[0]["addr"]) AND $r[0]["addr"] AND ($profile["addr"] == ""))
  192. $profile["addr"] = $r[0]["addr"];
  193. if (isset($r[0]["photo"]) AND $r[0]["photo"])
  194. $profile["photo"] = $r[0]["photo"];
  195. if (isset($r[0]["location"]) AND $r[0]["location"])
  196. $profile["location"] = $r[0]["location"];
  197. if (isset($r[0]["about"]) AND $r[0]["about"])
  198. $profile["about"] = $r[0]["about"];
  199. if (isset($r[0]["keywords"]) AND $r[0]["keywords"])
  200. $profile["keywords"] = $r[0]["keywords"];
  201. if (isset($r[0]["gender"]) AND $r[0]["gender"])
  202. $profile["gender"] = $r[0]["gender"];
  203. if (isset($r[0]["forum"]) OR isset($r[0]["prv"]))
  204. $profile["community"] = ($r[0]["forum"] OR $r[0]["prv"]);
  205. if (isset($r[0]["network"]) AND $r[0]["network"])
  206. $profile["network"] = $r[0]["network"];
  207. if (isset($r[0]["addr"]) AND $r[0]["addr"])
  208. $profile["addr"] = $r[0]["addr"];
  209. if (isset($r[0]["bd"]) AND $r[0]["bd"])
  210. $profile["bd"] = $r[0]["bd"];
  211. if ($r[0]["uid"] == 0)
  212. $profile["cid"] = 0;
  213. else
  214. $profile["cid"] = $r[0]["id"];
  215. } else
  216. $profile["cid"] = 0;
  217. if (isset($profile["photo"]))
  218. $profile["photo"] = proxy_url($profile["photo"], false, PROXY_SIZE_SMALL);
  219. if (isset($profile["location"]))
  220. $profile["location"] = bbcode($profile["location"]);
  221. if (isset($profile["about"]))
  222. $profile["about"] = bbcode($profile["about"]);
  223. if (($profile["cid"] == 0) AND ($profile["network"] == NETWORK_DIASPORA)) {
  224. $profile["location"] = "";
  225. $profile["about"] = "";
  226. }
  227. return($profile);
  228. }
  229. if(! function_exists('contact_photo_menu')){
  230. function contact_photo_menu($contact) {
  231. $a = get_app();
  232. $contact_url="";
  233. $pm_url="";
  234. $status_link="";
  235. $photos_link="";
  236. $posts_link="";
  237. $contact_drop_link = "";
  238. $poke_link="";
  239. $sparkle = false;
  240. if($contact['network'] === NETWORK_DFRN) {
  241. $sparkle = true;
  242. $profile_link = $a->get_baseurl() . '/redir/' . $contact['id'];
  243. }
  244. else
  245. $profile_link = $contact['url'];
  246. if($profile_link === 'mailbox')
  247. $profile_link = '';
  248. if($sparkle) {
  249. $status_link = $profile_link . "?url=status";
  250. $photos_link = $profile_link . "?url=photos";
  251. $profile_link = $profile_link . "?url=profile";
  252. }
  253. if (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA)))
  254. $pm_url = $a->get_baseurl() . '/message/new/' . $contact['id'];
  255. if ($contact["network"] == NETWORK_DFRN)
  256. $poke_link = $a->get_baseurl() . '/poke/?f=&c=' . $contact['id'];
  257. $contact_url = $a->get_baseurl() . '/contacts/' . $contact['id'];
  258. $posts_link = $a->get_baseurl() . '/network/0?nets=all&cid=' . $contact['id'];
  259. $contact_drop_link = $a->get_baseurl() . "/contacts/" . $contact['id'] . '/drop?confirm=1';
  260. $menu = Array(
  261. 'status' => array(t("View Status"), $status_link),
  262. 'profile' => array(t("View Profile"), $profile_link),
  263. 'photos' => array(t("View Photos"), $photos_link),
  264. 'network' => array(t("Network Posts"), $posts_link),
  265. 'edit' => array(t("Edit Contact"), $contact_url),
  266. 'drop' => array(t("Drop Contact"), $contact_drop_link),
  267. 'pm' => array(t("Send PM"), $pm_url),
  268. 'poke' => array(t("Poke"), $poke_link),
  269. );
  270. $args = array('contact' => $contact, 'menu' => &$menu);
  271. call_hooks('contact_photo_menu', $args);
  272. /* $o = "";
  273. foreach($menu as $k=>$v){
  274. if ($v!="") {
  275. if(($k !== t("Network Posts")) && ($k !== t("Send PM")) && ($k !== t('Edit Contact')))
  276. $o .= "<li><a target=\"redir\" href=\"$v\">$k</a></li>\n";
  277. else
  278. $o .= "<li><a href=\"$v\">$k</a></li>\n";
  279. }
  280. }
  281. return $o;*/
  282. foreach($menu as $k=>$v){
  283. if ($v[1]!="") {
  284. if(($v[0] !== t("Network Posts")) && ($v[0] !== t("Send PM")) && ($v[0] !== t('Edit Contact')))
  285. $menu[$k][2] = 1;
  286. else
  287. $menu[$k][2] = 0;
  288. }
  289. }
  290. $menucondensed = array();
  291. foreach ($menu AS $menuitem)
  292. if ($menuitem[1] != "")
  293. $menucondensed[] = $menuitem;
  294. return $menucondensed;
  295. }}
  296. function random_profile() {
  297. $r = q("SELECT `url` FROM `gcontact` WHERE `network` = '%s'
  298. AND `last_contact` >= `last_failure`
  299. AND `updated` > UTC_TIMESTAMP - INTERVAL 1 MONTH
  300. ORDER BY rand() LIMIT 1",
  301. dbesc(NETWORK_DFRN));
  302. if(count($r))
  303. return dirname($r[0]['url']);
  304. return '';
  305. }
  306. function contacts_not_grouped($uid,$start = 0,$count = 0) {
  307. if(! $count) {
  308. $r = q("select count(*) as total from contact where uid = %d and self = 0 and id not in (select distinct(`contact-id`) from group_member where uid = %d) ",
  309. intval($uid),
  310. intval($uid)
  311. );
  312. return $r;
  313. }
  314. $r = q("select * from contact where uid = %d and self = 0 and id not in (select distinct(`contact-id`) from group_member where uid = %d) and blocked = 0 and pending = 0 limit %d, %d",
  315. intval($uid),
  316. intval($uid),
  317. intval($start),
  318. intval($count)
  319. );
  320. return $r;
  321. }
  322. function get_contact($url, $uid = 0) {
  323. require_once("include/Scrape.php");
  324. $data = array();
  325. $contactid = 0;
  326. // is it an address in the format user@server.tld?
  327. if (!strstr($url, "http") OR strstr($url, "@")) {
  328. $data = probe_url($url);
  329. $url = $data["url"];
  330. if ($url == "")
  331. return 0;
  332. }
  333. $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
  334. dbesc(normalise_link($url)),
  335. intval($uid));
  336. if (!$contact)
  337. $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `uid` = %d",
  338. dbesc($url),
  339. dbesc(normalise_link($url)),
  340. intval($uid));
  341. if ($contact) {
  342. $contactid = $contact[0]["id"];
  343. // Update the contact every 7 days
  344. $update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -7 days'));
  345. //$update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
  346. if (!$update_photo)
  347. return($contactid);
  348. } elseif ($uid != 0)
  349. return 0;
  350. if (!count($data))
  351. $data = probe_url($url);
  352. // Does this address belongs to a valid network?
  353. if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
  354. return 0;
  355. // tempory programming. Can be deleted after 2015-02-07
  356. if (($data["alias"] == "") AND (normalise_link($data["url"]) != normalise_link($url)))
  357. $data["alias"] = normalise_link($url);
  358. if ($contactid == 0) {
  359. q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
  360. `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
  361. `batch`, `request`, `confirm`, `poco`,
  362. `writable`, `blocked`, `readonly`, `pending`)
  363. VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s', 1, 0, 0, 0)",
  364. intval($uid),
  365. dbesc(datetime_convert()),
  366. dbesc($data["url"]),
  367. dbesc(normalise_link($data["url"])),
  368. dbesc($data["addr"]),
  369. dbesc($data["alias"]),
  370. dbesc($data["notify"]),
  371. dbesc($data["poll"]),
  372. dbesc($data["name"]),
  373. dbesc($data["nick"]),
  374. dbesc($data["photo"]),
  375. dbesc($data["network"]),
  376. dbesc($data["pubkey"]),
  377. intval(CONTACT_IS_SHARING),
  378. intval($data["priority"]),
  379. dbesc($data["batch"]),
  380. dbesc($data["request"]),
  381. dbesc($data["confirm"]),
  382. dbesc($data["poco"])
  383. );
  384. $contact = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
  385. dbesc(normalise_link($data["url"])),
  386. intval($uid));
  387. if (!$contact)
  388. return 0;
  389. $contactid = $contact[0]["id"];
  390. }
  391. require_once("Photo.php");
  392. $photos = import_profile_photo($data["photo"],$uid,$contactid);
  393. q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s',
  394. `addr` = '%s', `alias` = '%s', `name` = '%s', `nick` = '%s',
  395. `name-date` = '%s', `uri-date` = '%s', `avatar-date` = '%s' WHERE `id` = %d",
  396. dbesc($photos[0]),
  397. dbesc($photos[1]),
  398. dbesc($photos[2]),
  399. dbesc($data["addr"]),
  400. dbesc($data["alias"]),
  401. dbesc($data["name"]),
  402. dbesc($data["nick"]),
  403. dbesc(datetime_convert()),
  404. dbesc(datetime_convert()),
  405. dbesc(datetime_convert()),
  406. intval($contactid)
  407. );
  408. return $contactid;
  409. }