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.

1715 lines
54KB

  1. <?php
  2. /**
  3. * @file mod/photos.php
  4. */
  5. use Friendica\App;
  6. use Friendica\Content\Feature;
  7. use Friendica\Content\Nav;
  8. use Friendica\Content\Pager;
  9. use Friendica\Content\Text\BBCode;
  10. use Friendica\Core\ACL;
  11. use Friendica\Core\Addon;
  12. use Friendica\Core\Config;
  13. use Friendica\Core\L10n;
  14. use Friendica\Core\Logger;
  15. use Friendica\Core\Renderer;
  16. use Friendica\Core\System;
  17. use Friendica\Core\Worker;
  18. use Friendica\Database\DBA;
  19. use Friendica\Model\Contact;
  20. use Friendica\Model\Group;
  21. use Friendica\Model\Item;
  22. use Friendica\Model\Photo;
  23. use Friendica\Model\Profile;
  24. use Friendica\Model\User;
  25. use Friendica\Network\Probe;
  26. use Friendica\Object\Image;
  27. use Friendica\Protocol\DFRN;
  28. use Friendica\Util\Crypto;
  29. use Friendica\Util\DateTimeFormat;
  30. use Friendica\Util\Map;
  31. use Friendica\Util\Security;
  32. use Friendica\Util\Temporal;
  33. use Friendica\Util\Strings;
  34. use Friendica\Util\XML;
  35. function photos_init(App $a) {
  36. if ($a->argc > 1) {
  37. DFRN::autoRedir($a, $a->argv[1]);
  38. }
  39. if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
  40. return;
  41. }
  42. Nav::setSelected('home');
  43. if ($a->argc > 1) {
  44. $nick = $a->argv[1];
  45. $user = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
  46. DBA::escape($nick)
  47. );
  48. if (!DBA::isResult($user)) {
  49. return;
  50. }
  51. $a->data['user'] = $user[0];
  52. $a->profile_uid = $user[0]['uid'];
  53. $is_owner = (local_user() && (local_user() == $a->profile_uid));
  54. $profile = Profile::getByNickname($nick, $a->profile_uid);
  55. $account_type = Contact::getAccountType($profile);
  56. $tpl = Renderer::getMarkupTemplate("vcard-widget.tpl");
  57. $vcard_widget = Renderer::replaceMacros($tpl, [
  58. '$name' => $profile['name'],
  59. '$photo' => $profile['photo'],
  60. '$addr' => defaults($profile, 'addr', ''),
  61. '$account_type' => $account_type,
  62. '$pdesc' => defaults($profile, 'pdesc', ''),
  63. ]);
  64. $albums = Photo::getAlbums($a->data['user']['uid']);
  65. $albums_visible = ((intval($a->data['user']['hidewall']) && !local_user() && !remote_user()) ? false : true);
  66. // add various encodings to the array so we can just loop through and pick them out in a template
  67. $ret = ['success' => false];
  68. if ($albums) {
  69. $a->data['albums'] = $albums;
  70. if ($albums_visible) {
  71. $ret['success'] = true;
  72. }
  73. $ret['albums'] = [];
  74. foreach ($albums as $k => $album) {
  75. //hide profile photos to others
  76. if (!$is_owner && !remote_user() && ($album['album'] == L10n::t('Profile Photos')))
  77. continue;
  78. $entry = [
  79. 'text' => $album['album'],
  80. 'total' => $album['total'],
  81. 'url' => 'photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($album['album']),
  82. 'urlencode' => urlencode($album['album']),
  83. 'bin2hex' => bin2hex($album['album'])
  84. ];
  85. $ret['albums'][] = $entry;
  86. }
  87. }
  88. if (local_user() && $a->data['user']['uid'] == local_user()) {
  89. $can_post = true;
  90. } else {
  91. $can_post = false;
  92. }
  93. if ($ret['success']) {
  94. $photo_albums_widget = Renderer::replaceMacros(Renderer::getMarkupTemplate('photo_albums.tpl'), [
  95. '$nick' => $a->data['user']['nickname'],
  96. '$title' => L10n::t('Photo Albums'),
  97. '$recent' => L10n::t('Recent Photos'),
  98. '$albums' => $ret['albums'],
  99. '$baseurl' => System::baseUrl(),
  100. '$upload' => [L10n::t('Upload New Photos'), 'photos/' . $a->data['user']['nickname'] . '/upload'],
  101. '$can_post' => $can_post
  102. ]);
  103. }
  104. if (empty($a->page['aside'])) {
  105. $a->page['aside'] = '';
  106. }
  107. $a->page['aside'] .= $vcard_widget;
  108. if (!empty($photo_albums_widget)) {
  109. $a->page['aside'] .= $photo_albums_widget;
  110. }
  111. $tpl = Renderer::getMarkupTemplate("photos_head.tpl");
  112. $a->page['htmlhead'] .= Renderer::replaceMacros($tpl,[
  113. '$ispublic' => L10n::t('everybody')
  114. ]);
  115. }
  116. return;
  117. }
  118. function photos_post(App $a)
  119. {
  120. Logger::log('mod-photos: photos_post: begin' , Logger::DEBUG);
  121. Logger::log('mod_photos: REQUEST ' . print_r($_REQUEST, true), Logger::DATA);
  122. Logger::log('mod_photos: FILES ' . print_r($_FILES, true), Logger::DATA);
  123. $phototypes = Image::supportedTypes();
  124. $can_post = false;
  125. $visitor = 0;
  126. $page_owner_uid = $a->data['user']['uid'];
  127. $community_page = $a->data['user']['page-flags'] == Contact::PAGE_COMMUNITY;
  128. if (local_user() && (local_user() == $page_owner_uid)) {
  129. $can_post = true;
  130. } elseif ($community_page && remote_user()) {
  131. $contact_id = 0;
  132. if (!empty($_SESSION['remote']) && is_array($_SESSION['remote'])) {
  133. foreach ($_SESSION['remote'] as $v) {
  134. if ($v['uid'] == $page_owner_uid) {
  135. $contact_id = $v['cid'];
  136. break;
  137. }
  138. }
  139. }
  140. if ($contact_id > 0) {
  141. $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
  142. intval($contact_id),
  143. intval($page_owner_uid)
  144. );
  145. if (DBA::isResult($r)) {
  146. $can_post = true;
  147. $visitor = $contact_id;
  148. }
  149. }
  150. }
  151. if (!$can_post) {
  152. notice(L10n::t('Permission denied.') . EOL);
  153. killme();
  154. }
  155. $owner_record = User::getOwnerDataById($page_owner_uid);
  156. if (!$owner_record) {
  157. notice(L10n::t('Contact information unavailable') . EOL);
  158. Logger::log('photos_post: unable to locate contact record for page owner. uid=' . $page_owner_uid);
  159. killme();
  160. }
  161. if ($a->argc > 3 && $a->argv[2] === 'album') {
  162. $album = hex2bin($a->argv[3]);
  163. if ($album === L10n::t('Profile Photos') || $album === 'Contact Photos' || $album === L10n::t('Contact Photos')) {
  164. $a->internalRedirect($_SESSION['photo_return']);
  165. return; // NOTREACHED
  166. }
  167. $r = q("SELECT `album` FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
  168. DBA::escape($album),
  169. intval($page_owner_uid)
  170. );
  171. if (!DBA::isResult($r)) {
  172. notice(L10n::t('Album not found.') . EOL);
  173. $a->internalRedirect($_SESSION['photo_return']);
  174. return; // NOTREACHED
  175. }
  176. // Check if the user has responded to a delete confirmation query
  177. if (!empty($_REQUEST['canceled'])) {
  178. $a->internalRedirect($_SESSION['photo_return']);
  179. }
  180. // RENAME photo album
  181. $newalbum = Strings::escapeTags(trim($_POST['albumname']));
  182. if ($newalbum != $album) {
  183. q("UPDATE `photo` SET `album` = '%s' WHERE `album` = '%s' AND `uid` = %d",
  184. DBA::escape($newalbum),
  185. DBA::escape($album),
  186. intval($page_owner_uid)
  187. );
  188. // Update the photo albums cache
  189. Photo::clearAlbumCache($page_owner_uid);
  190. $a->internalRedirect('photos/' . $a->user['nickname'] . '/album/' . bin2hex($newalbum));
  191. return; // NOTREACHED
  192. }
  193. /*
  194. * DELETE photo album and all its photos
  195. */
  196. if ($_POST['dropalbum'] == L10n::t('Delete Album')) {
  197. // Check if we should do HTML-based delete confirmation
  198. if (!empty($_REQUEST['confirm'])) {
  199. $drop_url = $a->query_string;
  200. $extra_inputs = [
  201. ['name' => 'albumname', 'value' => $_POST['albumname']],
  202. ];
  203. $a->page['content'] = Renderer::replaceMacros(Renderer::getMarkupTemplate('confirm.tpl'), [
  204. '$method' => 'post',
  205. '$message' => L10n::t('Do you really want to delete this photo album and all its photos?'),
  206. '$extra_inputs' => $extra_inputs,
  207. '$confirm' => L10n::t('Delete Album'),
  208. '$confirm_url' => $drop_url,
  209. '$confirm_name' => 'dropalbum', // Needed so that confirmation will bring us back into this if statement
  210. '$cancel' => L10n::t('Cancel'),
  211. ]);
  212. $a->error = 1; // Set $a->error so the other module functions don't execute
  213. return;
  214. }
  215. $res = [];
  216. // get the list of photos we are about to delete
  217. if ($visitor) {
  218. $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d AND `album` = '%s'",
  219. intval($visitor),
  220. intval($page_owner_uid),
  221. DBA::escape($album)
  222. );
  223. } else {
  224. $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `uid` = %d AND `album` = '%s'",
  225. intval(local_user()),
  226. DBA::escape($album)
  227. );
  228. }
  229. if (DBA::isResult($r)) {
  230. foreach ($r as $rr) {
  231. $res[] = "'" . DBA::escape($rr['rid']) . "'";
  232. }
  233. } else {
  234. $a->internalRedirect($_SESSION['photo_return']);
  235. return; // NOTREACHED
  236. }
  237. $str_res = implode(',', $res);
  238. // remove the associated photos
  239. q("DELETE FROM `photo` WHERE `resource-id` IN ($str_res) AND `uid` = %d",
  240. intval($page_owner_uid)
  241. );
  242. // find and delete the corresponding item with all the comments and likes/dislikes
  243. Item::deleteForUser(['resource-id' => $res, 'uid' => $page_owner_uid], $page_owner_uid);
  244. // Update the photo albums cache
  245. Photo::clearAlbumCache($page_owner_uid);
  246. }
  247. $a->internalRedirect('photos/' . $a->data['user']['nickname']);
  248. return; // NOTREACHED
  249. }
  250. // Check if the user has responded to a delete confirmation query for a single photo
  251. if ($a->argc > 2 && !empty($_REQUEST['canceled'])) {
  252. $a->internalRedirect($_SESSION['photo_return']);
  253. }
  254. if ($a->argc > 2 && defaults($_POST, 'delete', '') === L10n::t('Delete Photo')) {
  255. // same as above but remove single photo
  256. // Check if we should do HTML-based delete confirmation
  257. if (!empty($_REQUEST['confirm'])) {
  258. $drop_url = $a->query_string;
  259. $a->page['content'] = Renderer::replaceMacros(Renderer::getMarkupTemplate('confirm.tpl'), [
  260. '$method' => 'post',
  261. '$message' => L10n::t('Do you really want to delete this photo?'),
  262. '$extra_inputs' => [],
  263. '$confirm' => L10n::t('Delete Photo'),
  264. '$confirm_url' => $drop_url,
  265. '$confirm_name' => 'delete', // Needed so that confirmation will bring us back into this if statement
  266. '$cancel' => L10n::t('Cancel'),
  267. ]);
  268. $a->error = 1; // Set $a->error so the other module functions don't execute
  269. return;
  270. }
  271. if ($visitor) {
  272. $r = q("SELECT `id`, `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d AND `resource-id` = '%s' LIMIT 1",
  273. intval($visitor),
  274. intval($page_owner_uid),
  275. DBA::escape($a->argv[2])
  276. );
  277. } else {
  278. $r = q("SELECT `id`, `resource-id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' LIMIT 1",
  279. intval(local_user()),
  280. DBA::escape($a->argv[2])
  281. );
  282. }
  283. if (DBA::isResult($r)) {
  284. q("DELETE FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'",
  285. intval($page_owner_uid),
  286. DBA::escape($r[0]['resource-id'])
  287. );
  288. Item::deleteForUser(['resource-id' => $r[0]['resource-id'], 'uid' => $page_owner_uid], $page_owner_uid);
  289. // Update the photo albums cache
  290. Photo::clearAlbumCache($page_owner_uid);
  291. }
  292. $a->internalRedirect('photos/' . $a->data['user']['nickname']);
  293. return; // NOTREACHED
  294. }
  295. if ($a->argc > 2 && (!empty($_POST['desc']) || !empty($_POST['newtag']) || isset($_POST['albname']))) {
  296. $desc = !empty($_POST['desc']) ? Strings::escapeTags(trim($_POST['desc'])) : '';
  297. $rawtags = !empty($_POST['newtag']) ? Strings::escapeTags(trim($_POST['newtag'])) : '';
  298. $item_id = !empty($_POST['item_id']) ? intval($_POST['item_id']) : 0;
  299. $albname = !empty($_POST['albname']) ? Strings::escapeTags(trim($_POST['albname'])) : '';
  300. $origaname = !empty($_POST['origaname']) ? Strings::escapeTags(trim($_POST['origaname'])) : '';
  301. $str_group_allow = !empty($_POST['group_allow']) ? perms2str($_POST['group_allow']) : '';
  302. $str_contact_allow = !empty($_POST['contact_allow']) ? perms2str($_POST['contact_allow']) : '';
  303. $str_group_deny = !empty($_POST['group_deny']) ? perms2str($_POST['group_deny']) : '';
  304. $str_contact_deny = !empty($_POST['contact_deny']) ? perms2str($_POST['contact_deny']) : '';
  305. $resource_id = $a->argv[2];
  306. if (!strlen($albname)) {
  307. $albname = DateTimeFormat::localNow('Y');
  308. }
  309. if (!empty($_POST['rotate']) && (intval($_POST['rotate']) == 1 || intval($_POST['rotate']) == 2)) {
  310. Logger::log('rotate');
  311. $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = 0 LIMIT 1",
  312. DBA::escape($resource_id),
  313. intval($page_owner_uid)
  314. );
  315. if (DBA::isResult($r)) {
  316. $image = new Image($r[0]['data'], $r[0]['type']);
  317. if ($image->isValid()) {
  318. $rotate_deg = ((intval($_POST['rotate']) == 1) ? 270 : 90);
  319. $image->rotate($rotate_deg);
  320. $width = $image->getWidth();
  321. $height = $image->getHeight();
  322. $x = q("UPDATE `photo` SET `data` = '%s', `height` = %d, `width` = %d WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = 0",
  323. DBA::escape($image->asString()),
  324. intval($height),
  325. intval($width),
  326. DBA::escape($resource_id),
  327. intval($page_owner_uid)
  328. );
  329. if ($width > 640 || $height > 640) {
  330. $image->scaleDown(640);
  331. $width = $image->getWidth();
  332. $height = $image->getHeight();
  333. $x = q("UPDATE `photo` SET `data` = '%s', `height` = %d, `width` = %d WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = 1",
  334. DBA::escape($image->asString()),
  335. intval($height),
  336. intval($width),
  337. DBA::escape($resource_id),
  338. intval($page_owner_uid)
  339. );
  340. }
  341. if ($width > 320 || $height > 320) {
  342. $image->scaleDown(320);
  343. $width = $image->getWidth();
  344. $height = $image->getHeight();
  345. $x = q("UPDATE `photo` SET `data` = '%s', `height` = %d, `width` = %d WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = 2",
  346. DBA::escape($image->asString()),
  347. intval($height),
  348. intval($width),
  349. DBA::escape($resource_id),
  350. intval($page_owner_uid)
  351. );
  352. }
  353. }
  354. }
  355. }
  356. $p = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ORDER BY `scale` DESC",
  357. DBA::escape($resource_id),
  358. intval($page_owner_uid)
  359. );
  360. if (DBA::isResult($p)) {
  361. $ext = $phototypes[$p[0]['type']];
  362. $r = q("UPDATE `photo` SET `desc` = '%s', `album` = '%s', `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s' WHERE `resource-id` = '%s' AND `uid` = %d",
  363. DBA::escape($desc),
  364. DBA::escape($albname),
  365. DBA::escape($str_contact_allow),
  366. DBA::escape($str_group_allow),
  367. DBA::escape($str_contact_deny),
  368. DBA::escape($str_group_deny),
  369. DBA::escape($resource_id),
  370. intval($page_owner_uid)
  371. );
  372. // Update the photo albums cache if album name was changed
  373. if ($albname !== $origaname) {
  374. Photo::clearAlbumCache($page_owner_uid);
  375. }
  376. }
  377. /* Don't make the item visible if the only change was the album name */
  378. $visibility = 0;
  379. if ($p[0]['desc'] !== $desc || strlen($rawtags)) {
  380. $visibility = 1;
  381. }
  382. if (!$item_id) {
  383. // Create item container
  384. $title = '';
  385. $uri = Item::newURI($page_owner_uid);
  386. $arr = [];
  387. $arr['guid'] = System::createUUID();
  388. $arr['uid'] = $page_owner_uid;
  389. $arr['uri'] = $uri;
  390. $arr['parent-uri'] = $uri;
  391. $arr['post-type'] = Item::PT_IMAGE;
  392. $arr['wall'] = 1;
  393. $arr['resource-id'] = $p[0]['resource-id'];
  394. $arr['contact-id'] = $owner_record['id'];
  395. $arr['owner-name'] = $owner_record['name'];
  396. $arr['owner-link'] = $owner_record['url'];
  397. $arr['owner-avatar'] = $owner_record['thumb'];
  398. $arr['author-name'] = $owner_record['name'];
  399. $arr['author-link'] = $owner_record['url'];
  400. $arr['author-avatar'] = $owner_record['thumb'];
  401. $arr['title'] = $title;
  402. $arr['allow_cid'] = $p[0]['allow_cid'];
  403. $arr['allow_gid'] = $p[0]['allow_gid'];
  404. $arr['deny_cid'] = $p[0]['deny_cid'];
  405. $arr['deny_gid'] = $p[0]['deny_gid'];
  406. $arr['visible'] = $visibility;
  407. $arr['origin'] = 1;
  408. $arr['body'] = '[url=' . System::baseUrl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $p[0]['resource-id'] . ']'
  409. . '[img]' . System::baseUrl() . '/photo/' . $p[0]['resource-id'] . '-' . $p[0]['scale'] . '.'. $ext . '[/img]'
  410. . '[/url]';
  411. $item_id = Item::insert($arr);
  412. }
  413. if ($item_id) {
  414. $item = Item::selectFirst(['tag', 'inform'], ['id' => $item_id, 'uid' => $page_owner_uid]);
  415. }
  416. if (DBA::isResult($item)) {
  417. $old_tag = $item['tag'];
  418. $old_inform = $item['inform'];
  419. }
  420. if (strlen($rawtags)) {
  421. $str_tags = '';
  422. $inform = '';
  423. // if the new tag doesn't have a namespace specifier (@foo or #foo) give it a hashtag
  424. $x = substr($rawtags, 0, 1);
  425. if ($x !== '@' && $x !== '#') {
  426. $rawtags = '#' . $rawtags;
  427. }
  428. $taginfo = [];
  429. $tags = BBCode::getTags($rawtags);
  430. if (count($tags)) {
  431. foreach ($tags as $tag) {
  432. if (strpos($tag, '@') === 0) {
  433. $profile = '';
  434. $name = substr($tag,1);
  435. if ((strpos($name, '@')) || (strpos($name, 'http://'))) {
  436. $newname = $name;
  437. $links = @Probe::lrdd($name);
  438. if (count($links)) {
  439. foreach ($links as $link) {
  440. if ($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page') {
  441. $profile = $link['@attributes']['href'];
  442. }
  443. if ($link['@attributes']['rel'] === 'salmon') {
  444. $salmon = '$url:' . str_replace(',', '%sc', $link['@attributes']['href']);
  445. if (strlen($inform)) {
  446. $inform .= ',';
  447. }
  448. $inform .= $salmon;
  449. }
  450. }
  451. }
  452. $taginfo[] = [$newname, $profile, $salmon];
  453. } else {
  454. $newname = $name;
  455. $alias = '';
  456. $tagcid = 0;
  457. if (strrpos($newname, '+')) {
  458. $tagcid = intval(substr($newname, strrpos($newname, '+') + 1));
  459. }
  460. if ($tagcid) {
  461. $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  462. intval($tagcid),
  463. intval($profile_uid)
  464. );
  465. } else {
  466. $newname = str_replace('_',' ',$name);
  467. //select someone from this user's contacts by name
  468. $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
  469. DBA::escape($newname),
  470. intval($page_owner_uid)
  471. );
  472. if (!DBA::isResult($r)) {
  473. //select someone by attag or nick and the name passed in
  474. $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
  475. DBA::escape($name),
  476. DBA::escape($name),
  477. intval($page_owner_uid)
  478. );
  479. }
  480. }
  481. if (DBA::isResult($r)) {
  482. $newname = $r[0]['name'];
  483. $profile = $r[0]['url'];
  484. $notify = 'cid:' . $r[0]['id'];
  485. if (strlen($inform)) {
  486. $inform .= ',';
  487. }
  488. $inform .= $notify;
  489. }
  490. }
  491. if ($profile) {
  492. if (substr($notify, 0, 4) === 'cid:') {
  493. $taginfo[] = [$newname, $profile, $notify, $r[0], '@[url=' . str_replace(',','%2c',$profile) . ']' . $newname . '[/url]'];
  494. } else {
  495. $taginfo[] = [$newname, $profile, $notify, null, $str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]'];
  496. }
  497. if (strlen($str_tags)) {
  498. $str_tags .= ',';
  499. }
  500. $profile = str_replace(',', '%2c', $profile);
  501. $str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]';
  502. }
  503. } elseif (strpos($tag, '#') === 0) {
  504. $tagname = substr($tag, 1);
  505. $str_tags .= '#[url=' . System::baseUrl() . "/search?tag=" . $tagname . ']' . $tagname . '[/url],';
  506. }
  507. }
  508. }
  509. $newtag = $old_tag;
  510. if (strlen($newtag) && strlen($str_tags)) {
  511. $newtag .= ',';
  512. }
  513. $newtag .= $str_tags;
  514. $newinform = $old_inform;
  515. if (strlen($newinform) && strlen($inform)) {
  516. $newinform .= ',';
  517. }
  518. $newinform .= $inform;
  519. $fields = ['tag' => $newtag, 'inform' => $newinform, 'edited' => DateTimeFormat::utcNow(), 'changed' => DateTimeFormat::utcNow()];
  520. $condition = ['id' => $item_id];
  521. Item::update($fields, $condition);
  522. $best = 0;
  523. foreach ($p as $scales) {
  524. if (intval($scales['scale']) == 2) {
  525. $best = 2;
  526. break;
  527. }
  528. if (intval($scales['scale']) == 4) {
  529. $best = 4;
  530. break;
  531. }
  532. }
  533. if (count($taginfo)) {
  534. foreach ($taginfo as $tagged) {
  535. $uri = Item::newURI($page_owner_uid);
  536. $arr = [];
  537. $arr['guid'] = System::createUUID();
  538. $arr['uid'] = $page_owner_uid;
  539. $arr['uri'] = $uri;
  540. $arr['parent-uri'] = $uri;
  541. $arr['wall'] = 1;
  542. $arr['contact-id'] = $owner_record['id'];
  543. $arr['owner-name'] = $owner_record['name'];
  544. $arr['owner-link'] = $owner_record['url'];
  545. $arr['owner-avatar'] = $owner_record['thumb'];
  546. $arr['author-name'] = $owner_record['name'];
  547. $arr['author-link'] = $owner_record['url'];
  548. $arr['author-avatar'] = $owner_record['thumb'];
  549. $arr['title'] = '';
  550. $arr['allow_cid'] = $p[0]['allow_cid'];
  551. $arr['allow_gid'] = $p[0]['allow_gid'];
  552. $arr['deny_cid'] = $p[0]['deny_cid'];
  553. $arr['deny_gid'] = $p[0]['deny_gid'];
  554. $arr['visible'] = 1;
  555. $arr['verb'] = ACTIVITY_TAG;
  556. $arr['gravity'] = GRAVITY_PARENT;
  557. $arr['object-type'] = ACTIVITY_OBJ_PERSON;
  558. $arr['target-type'] = ACTIVITY_OBJ_IMAGE;
  559. $arr['tag'] = $tagged[4];
  560. $arr['inform'] = $tagged[2];
  561. $arr['origin'] = 1;
  562. $arr['body'] = L10n::t('%1$s was tagged in %2$s by %3$s', '[url=' . $tagged[1] . ']' . $tagged[0] . '[/url]', '[url=' . System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . L10n::t('a photo') . '[/url]', '[url=' . $owner_record['url'] . ']' . $owner_record['name'] . '[/url]');
  563. $arr['body'] .= "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . '[img]' . System::baseUrl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.' . $ext . '[/img][/url]' . "\n";
  564. $arr['object'] = '<object><type>' . ACTIVITY_OBJ_PERSON . '</type><title>' . $tagged[0] . '</title><id>' . $tagged[1] . '/' . $tagged[0] . '</id>';
  565. $arr['object'] .= '<link>' . XML::escape('<link rel="alternate" type="text/html" href="' . $tagged[1] . '" />' . "\n");
  566. if ($tagged[3]) {
  567. $arr['object'] .= XML::escape('<link rel="photo" type="'.$p[0]['type'].'" href="' . $tagged[3]['photo'] . '" />' . "\n");
  568. }
  569. $arr['object'] .= '</link></object>' . "\n";
  570. $arr['target'] = '<target><type>' . ACTIVITY_OBJ_IMAGE . '</type><title>' . $p[0]['desc'] . '</title><id>'
  571. . System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '</id>';
  572. $arr['target'] .= '<link>' . XML::escape('<link rel="alternate" type="text/html" href="' . System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '" />' . "\n" . '<link rel="preview" type="'.$p[0]['type'].'" href="' . System::baseUrl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.' . $ext . '" />') . '</link></target>';
  573. $item_id = Item::insert($arr);
  574. }
  575. }
  576. }
  577. $a->internalRedirect($_SESSION['photo_return']);
  578. return; // NOTREACHED
  579. }
  580. // default post action - upload a photo
  581. Addon::callHooks('photo_post_init', $_POST);
  582. // Determine the album to use
  583. $album = !empty($_REQUEST['album']) ? Strings::escapeTags(trim($_REQUEST['album'])) : '';
  584. $newalbum = !empty($_REQUEST['newalbum']) ? Strings::escapeTags(trim($_REQUEST['newalbum'])) : '';
  585. Logger::log('mod/photos.php: photos_post(): album= ' . $album . ' newalbum= ' . $newalbum , Logger::DEBUG);
  586. if (!strlen($album)) {
  587. if (strlen($newalbum)) {
  588. $album = $newalbum;
  589. } else {
  590. $album = DateTimeFormat::localNow('Y');
  591. }
  592. }
  593. /*
  594. * We create a wall item for every photo, but we don't want to
  595. * overwhelm the data stream with a hundred newly uploaded photos.
  596. * So we will make the first photo uploaded to this album in the last several hours
  597. * visible by default, the rest will become visible over time when and if
  598. * they acquire comments, likes, dislikes, and/or tags
  599. */
  600. $r = q("SELECT * FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `created` > UTC_TIMESTAMP() - INTERVAL 3 HOUR ",
  601. DBA::escape($album),
  602. intval($page_owner_uid)
  603. );
  604. if (!DBA::isResult($r) || ($album == L10n::t('Profile Photos'))) {
  605. $visible = 1;
  606. } else {
  607. $visible = 0;
  608. }
  609. if (!empty($_REQUEST['not_visible']) && $_REQUEST['not_visible'] !== 'false') {
  610. $visible = 0;
  611. }
  612. $group_allow = defaults($_REQUEST, 'group_allow' , []);
  613. $contact_allow = defaults($_REQUEST, 'contact_allow', []);
  614. $group_deny = defaults($_REQUEST, 'group_deny' , []);
  615. $contact_deny = defaults($_REQUEST, 'contact_deny' , []);
  616. $str_group_allow = perms2str(is_array($group_allow) ? $group_allow : explode(',', $group_allow));
  617. $str_contact_allow = perms2str(is_array($contact_allow) ? $contact_allow : explode(',', $contact_allow));
  618. $str_group_deny = perms2str(is_array($group_deny) ? $group_deny : explode(',', $group_deny));
  619. $str_contact_deny = perms2str(is_array($contact_deny) ? $contact_deny : explode(',', $contact_deny));
  620. $ret = ['src' => '', 'filename' => '', 'filesize' => 0, 'type' => ''];
  621. Addon::callHooks('photo_post_file', $ret);
  622. if (!empty($ret['src']) && !empty($ret['filesize'])) {
  623. $src = $ret['src'];
  624. $filename = $ret['filename'];
  625. $filesize = $ret['filesize'];
  626. $type = $ret['type'];
  627. $error = UPLOAD_ERR_OK;
  628. } elseif (!empty($_FILES['userfile'])) {
  629. $src = $_FILES['userfile']['tmp_name'];
  630. $filename = basename($_FILES['userfile']['name']);
  631. $filesize = intval($_FILES['userfile']['size']);
  632. $type = $_FILES['userfile']['type'];
  633. $error = $_FILES['userfile']['error'];
  634. } else {
  635. $error = UPLOAD_ERR_NO_FILE;
  636. }
  637. if ($error !== UPLOAD_ERR_OK) {
  638. switch ($error) {
  639. case UPLOAD_ERR_INI_SIZE:
  640. notice(L10n::t('Image exceeds size limit of %s', ini_get('upload_max_filesize')) . EOL);
  641. break;
  642. case UPLOAD_ERR_FORM_SIZE:
  643. notice(L10n::t('Image exceeds size limit of %s', Strings::formatBytes(defaults($_REQUEST, 'MAX_FILE_SIZE', 0))) . EOL);
  644. break;
  645. case UPLOAD_ERR_PARTIAL:
  646. notice(L10n::t('Image upload didn\'t complete, please try again') . EOL);
  647. break;
  648. case UPLOAD_ERR_NO_FILE:
  649. notice(L10n::t('Image file is missing') . EOL);
  650. break;
  651. case UPLOAD_ERR_NO_TMP_DIR:
  652. case UPLOAD_ERR_CANT_WRITE:
  653. case UPLOAD_ERR_EXTENSION:
  654. notice(L10n::t('Server can\'t accept new file upload at this time, please contact your administrator') . EOL);
  655. break;
  656. }
  657. @unlink($src);
  658. $foo = 0;
  659. Addon::callHooks('photo_post_end', $foo);
  660. return;
  661. }
  662. if ($type == "") {
  663. $type = Image::guessType($filename);
  664. }
  665. Logger::log('photos: upload: received file: ' . $filename . ' as ' . $src . ' ('. $type . ') ' . $filesize . ' bytes', Logger::DEBUG);
  666. $maximagesize = Config::get('system', 'maximagesize');
  667. if ($maximagesize && ($filesize > $maximagesize)) {
  668. notice(L10n::t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize)) . EOL);
  669. @unlink($src);
  670. $foo = 0;
  671. Addon::callHooks('photo_post_end', $foo);
  672. return;
  673. }
  674. if (!$filesize) {
  675. notice(L10n::t('Image file is empty.') . EOL);
  676. @unlink($src);
  677. $foo = 0;
  678. Addon::callHooks('photo_post_end', $foo);
  679. return;
  680. }
  681. Logger::log('mod/photos.php: photos_post(): loading the contents of ' . $src , Logger::DEBUG);
  682. $imagedata = @file_get_contents($src);
  683. $image = new Image($imagedata, $type);
  684. if (!$image->isValid()) {
  685. Logger::log('mod/photos.php: photos_post(): unable to process image' , Logger::DEBUG);
  686. notice(L10n::t('Unable to process image.') . EOL);
  687. @unlink($src);
  688. $foo = 0;
  689. Addon::callHooks('photo_post_end',$foo);
  690. killme();
  691. }
  692. $exif = $image->orient($src);
  693. @unlink($src);
  694. $max_length = Config::get('system', 'max_image_length');
  695. if (!$max_length) {
  696. $max_length = MAX_IMAGE_LENGTH;
  697. }
  698. if ($max_length > 0) {
  699. $image->scaleDown($max_length);
  700. }
  701. $width = $image->getWidth();
  702. $height = $image->getHeight();
  703. $smallest = 0;
  704. $photo_hash = Photo::newResource();
  705. $r = Photo::store($image, $page_owner_uid, $visitor, $photo_hash, $filename, $album, 0 , 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
  706. if (!$r) {
  707. Logger::log('mod/photos.php: photos_post(): image store failed', Logger::DEBUG);
  708. notice(L10n::t('Image upload failed.') . EOL);
  709. killme();
  710. }
  711. if ($width > 640 || $height > 640) {
  712. $image->scaleDown(640);
  713. Photo::store($image, $page_owner_uid, $visitor, $photo_hash, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
  714. $smallest = 1;
  715. }
  716. if ($width > 320 || $height > 320) {
  717. $image->scaleDown(320);
  718. Photo::store($image, $page_owner_uid, $visitor, $photo_hash, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
  719. $smallest = 2;
  720. }
  721. $uri = Item::newURI($page_owner_uid);
  722. // Create item container
  723. $lat = $lon = null;
  724. if ($exif && $exif['GPS'] && Feature::isEnabled($page_owner_uid, 'photo_location')) {
  725. $lat = Photo::getGps($exif['GPS']['GPSLatitude'], $exif['GPS']['GPSLatitudeRef']);
  726. $lon = Photo::getGps($exif['GPS']['GPSLongitude'], $exif['GPS']['GPSLongitudeRef']);
  727. }
  728. $arr = [];
  729. if ($lat && $lon) {
  730. $arr['coord'] = $lat . ' ' . $lon;
  731. }
  732. $arr['guid'] = System::createUUID();
  733. $arr['uid'] = $page_owner_uid;
  734. $arr['uri'] = $uri;
  735. $arr['parent-uri'] = $uri;
  736. $arr['type'] = 'photo';
  737. $arr['wall'] = 1;
  738. $arr['resource-id'] = $photo_hash;
  739. $arr['contact-id'] = $owner_record['id'];
  740. $arr['owner-name'] = $owner_record['name'];
  741. $arr['owner-link'] = $owner_record['url'];
  742. $arr['owner-avatar'] = $owner_record['thumb'];
  743. $arr['author-name'] = $owner_record['name'];
  744. $arr['author-link'] = $owner_record['url'];
  745. $arr['author-avatar'] = $owner_record['thumb'];
  746. $arr['title'] = '';
  747. $arr['allow_cid'] = $str_contact_allow;
  748. $arr['allow_gid'] = $str_group_allow;
  749. $arr['deny_cid'] = $str_contact_deny;
  750. $arr['deny_gid'] = $str_group_deny;
  751. $arr['visible'] = $visible;
  752. $arr['origin'] = 1;
  753. $arr['body'] = '[url=' . System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo_hash . ']'
  754. . '[img]' . System::baseUrl() . "/photo/{$photo_hash}-{$smallest}.".$image->getExt() . '[/img]'
  755. . '[/url]';
  756. $item_id = Item::insert($arr);
  757. // Update the photo albums cache
  758. Photo::clearAlbumCache($page_owner_uid);
  759. Addon::callHooks('photo_post_end', $item_id);
  760. // addon uploaders should call "killme()" [e.g. exit] within the photo_post_end hook
  761. // if they do not wish to be redirected
  762. $a->internalRedirect($_SESSION['photo_return']);
  763. // NOTREACHED
  764. }
  765. function photos_content(App $a)
  766. {
  767. // URLs:
  768. // photos/name
  769. // photos/name/upload
  770. // photos/name/upload/xxxxx (xxxxx is album name)
  771. // photos/name/album/xxxxx
  772. // photos/name/album/xxxxx/edit
  773. // photos/name/image/xxxxx
  774. // photos/name/image/xxxxx/edit
  775. if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
  776. notice(L10n::t('Public access denied.') . EOL);
  777. return;
  778. }
  779. if (empty($a->data['user'])) {
  780. notice(L10n::t('No photos selected') . EOL);
  781. return;
  782. }
  783. $phototypes = Image::supportedTypes();
  784. $_SESSION['photo_return'] = $a->cmd;
  785. // Parse arguments
  786. $datum = null;
  787. if ($a->argc > 3) {
  788. $datatype = $a->argv[2];
  789. $datum = $a->argv[3];
  790. } elseif (($a->argc > 2) && ($a->argv[2] === 'upload')) {
  791. $datatype = 'upload';
  792. } else {
  793. $datatype = 'summary';
  794. }
  795. if ($a->argc > 4) {
  796. $cmd = $a->argv[4];
  797. } else {
  798. $cmd = 'view';
  799. }
  800. // Setup permissions structures
  801. $can_post = false;
  802. $visitor = 0;
  803. $contact = null;
  804. $remote_contact = false;
  805. $contact_id = 0;
  806. $edit = false;
  807. $owner_uid = $a->data['user']['uid'];
  808. $community_page = (($a->data['user']['page-flags'] == Contact::PAGE_COMMUNITY) ? true : false);
  809. if (local_user() && (local_user() == $owner_uid)) {
  810. $can_post = true;
  811. } else {
  812. if ($community_page && remote_user()) {
  813. if (is_array($_SESSION['remote'])) {
  814. foreach ($_SESSION['remote'] as $v) {
  815. if ($v['uid'] == $owner_uid) {
  816. $contact_id = $v['cid'];
  817. break;
  818. }
  819. }
  820. }
  821. if ($contact_id) {
  822. $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
  823. intval($contact_id),
  824. intval($owner_uid)
  825. );
  826. if (DBA::isResult($r)) {
  827. $can_post = true;
  828. $contact = $r[0];
  829. $remote_contact = true;
  830. $visitor = $contact_id;
  831. }
  832. }
  833. }
  834. }
  835. $groups = [];
  836. // perhaps they're visiting - but not a community page, so they wouldn't have write access
  837. if (remote_user() && !$visitor) {
  838. $contact_id = 0;
  839. if (is_array($_SESSION['remote'])) {
  840. foreach ($_SESSION['remote'] as $v) {
  841. if ($v['uid'] == $owner_uid) {
  842. $contact_id = $v['cid'];
  843. break;
  844. }
  845. }
  846. }
  847. if ($contact_id) {
  848. $groups = Group::getIdsByContactId($contact_id);
  849. $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
  850. intval($contact_id),
  851. intval($owner_uid)
  852. );
  853. if (DBA::isResult($r)) {
  854. $contact = $r[0];
  855. $remote_contact = true;
  856. }
  857. }
  858. }
  859. if (!$remote_contact && local_user()) {
  860. $contact_id = $_SESSION['cid'];
  861. $contact = $a->contact;
  862. }
  863. if ($a->data['user']['hidewall'] && (local_user() != $owner_uid) && !$remote_contact) {
  864. notice(L10n::t('Access to this item is restricted.') . EOL);
  865. return;
  866. }
  867. $sql_extra = Security::getPermissionsSQLByUserId($owner_uid, $remote_contact, $groups);
  868. $o = "";
  869. // tabs
  870. $is_owner = (local_user() && (local_user() == $owner_uid));
  871. $o .= Profile::getTabs($a, $is_owner, $a->data['user']['nickname']);
  872. // Display upload form
  873. if ($datatype === 'upload') {
  874. if (!$can_post) {
  875. notice(L10n::t('Permission denied.'));
  876. return;
  877. }
  878. $selname = $datum ? hex2bin($datum) : '';
  879. $albumselect = '';
  880. $albumselect .= '<option value="" ' . (!$selname ? ' selected="selected" ' : '') . '>&lt;current year&gt;</option>';
  881. if (!empty($a->data['albums'])) {
  882. foreach ($a->data['albums'] as $album) {
  883. if (($album['album'] === '') || ($album['album'] === 'Contact Photos') || ($album['album'] === L10n::t('Contact Photos'))) {
  884. continue;
  885. }
  886. $selected = (($selname === $album['album']) ? ' selected="selected" ' : '');
  887. $albumselect .= '<option value="' . $album['album'] . '"' . $selected . '>' . $album['album'] . '</option>';
  888. }
  889. }
  890. $uploader = '';
  891. $ret = ['post_url' => 'photos/' . $a->data['user']['nickname'],
  892. 'addon_text' => $uploader,
  893. 'default_upload' => true];
  894. Addon::callHooks('photo_upload_form',$ret);
  895. $default_upload_box = Renderer::replaceMacros(Renderer::getMarkupTemplate('photos_default_uploader_box.tpl'), []);
  896. $default_upload_submit = Renderer::replaceMacros(Renderer::getMarkupTemplate('photos_default_uploader_submit.tpl'), [
  897. '$submit' => L10n::t('Submit'),
  898. ]);
  899. $usage_message = '';
  900. $tpl = Renderer::getMarkupTemplate('photos_upload.tpl');
  901. $aclselect_e = ($visitor ? '' : ACL::getFullSelectorHTML($a->user));
  902. $o .= Renderer::replaceMacros($tpl,[
  903. '$pagename' => L10n::t('Upload Photos'),
  904. '$sessid' => session_id(),
  905. '$usage' => $usage_message,
  906. '$nickname' => $a->data['user']['nickname'],
  907. '$newalbum' => L10n::t('New album name: '),
  908. '$existalbumtext' => L10n::t('or select existing album:'),
  909. '$nosharetext' => L10n::t('Do not show a status post for this upload'),
  910. '$albumselect' => $albumselect,
  911. '$permissions' => L10n::t('Permissions'),
  912. '$aclselect' => $aclselect_e,
  913. '$lockstate' => is_array($a->user)
  914. && (strlen($a->user['allow_cid'])
  915. || strlen($a->user['allow_gid'])
  916. || strlen($a->user['deny_cid'])
  917. || strlen($a->user['deny_gid'])
  918. ) ? 'lock' : 'unlock',
  919. '$alt_uploader' => $ret['addon_text'],
  920. '$default_upload_box' => ($ret['default_upload'] ? $default_upload_box : ''),
  921. '$default_upload_submit' => ($ret['default_upload'] ? $default_upload_submit : ''),
  922. '$uploadurl' => $ret['post_url'],
  923. // ACL permissions box
  924. '$group_perms' => L10n::t('Show to Groups'),
  925. '$contact_perms' => L10n::t('Show to Contacts'),
  926. '$return_path' => $a->query_string,
  927. ]);
  928. return $o;
  929. }
  930. // Display a single photo album
  931. if ($datatype === 'album') {
  932. $album = hex2bin($datum);
  933. $total = 0;
  934. $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s'
  935. AND `scale` <= 4 $sql_extra GROUP BY `resource-id`",
  936. intval($owner_uid),
  937. DBA::escape($album)
  938. );
  939. if (DBA::isResult($r)) {
  940. $total = count($r);
  941. }
  942. $pager = new Pager($a->query_string, 20);
  943. /// @TODO I have seen this many times, maybe generalize it script-wide and encapsulate it?
  944. $order_field = defaults($_GET, 'order', '');
  945. if ($order_field === 'posted') {
  946. $order = 'ASC';
  947. } else {
  948. $order = 'DESC';
  949. }
  950. $r = q("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`,
  951. ANY_VALUE(`type`) AS `type`, max(`scale`) AS `scale`, ANY_VALUE(`desc`) as `desc`,
  952. ANY_VALUE(`created`) as `created`
  953. FROM `photo` WHERE `uid` = %d AND `album` = '%s'
  954. AND `scale` <= 4 $sql_extra GROUP BY `resource-id` ORDER BY `created` $order LIMIT %d , %d",
  955. intval($owner_uid),
  956. DBA::escape($album),
  957. $pager->getStart(),
  958. $pager->getItemsPerPage()
  959. );
  960. // edit album name
  961. if ($cmd === 'edit') {
  962. if (($album !== L10n::t('Profile Photos')) && ($album !== 'Contact Photos') && ($album !== L10n::t('Contact Photos'))) {
  963. if ($can_post) {
  964. $edit_tpl = Renderer::getMarkupTemplate('album_edit.tpl');
  965. $album_e = $album;
  966. $o .= Renderer::replaceMacros($edit_tpl,[
  967. '$nametext' => L10n::t('New album name: '),
  968. '$nickname' => $a->data['user']['nickname'],
  969. '$album' => $album_e,
  970. '$hexalbum' => bin2hex($album),
  971. '$submit' => L10n::t('Submit'),
  972. '$dropsubmit' => L10n::t('Delete Album')
  973. ]);
  974. }
  975. }
  976. } else {
  977. if (($album !== L10n::t('Profile Photos')) && ($album !== 'Contact Photos') && ($album !== L10n::t('Contact Photos')) && $can_post) {
  978. $edit = [L10n::t('Edit Album'), 'photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '/edit'];
  979. }
  980. }
  981. if ($order_field === 'posted') {
  982. $order = [L10n::t('Show Newest First'), 'photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($album), 'oldest'];
  983. } else {
  984. $order = [L10n::t('Show Oldest First'), 'photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '?f=&order=posted', 'newest'];
  985. }
  986. $photos = [];
  987. if (DBA::isResult($r)) {
  988. // "Twist" is only used for the duepunto theme with style "slackr"
  989. $twist = false;
  990. foreach ($r as $rr) {
  991. $twist = !$twist;
  992. $ext = $phototypes[$rr['type']];
  993. $imgalt_e = $rr['filename'];
  994. $desc_e = $rr['desc'];
  995. $photos[] = [
  996. 'id' => $rr['id'],
  997. 'twist' => ' ' . ($twist ? 'rotleft' : 'rotright') . rand(2,4),
  998. 'link' => 'photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id']
  999. . ($order_field === 'posted' ? '?f=&order=posted' : ''),
  1000. 'title' => L10n::t('View Photo'),
  1001. 'src' => 'photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.' .$ext,
  1002. 'alt' => $imgalt_e,
  1003. 'desc'=> $desc_e,
  1004. 'ext' => $ext,
  1005. 'hash'=> $rr['resource-id'],
  1006. ];
  1007. }
  1008. }
  1009. $tpl = Renderer::getMarkupTemplate('photo_album.tpl');
  1010. $o .= Renderer::replaceMacros($tpl, [
  1011. '$photos' => $photos,
  1012. '$album' => $album,
  1013. '$can_post' => $can_post,
  1014. '$upload' => [L10n::t('Upload New Photos'), 'photos/' . $a->data['user']['nickname'] . '/upload/' . bin2hex($album)],
  1015. '$order' => $order,
  1016. '$edit' => $edit,
  1017. '$paginate' => $pager->renderFull($total),
  1018. ]);
  1019. return $o;
  1020. }
  1021. // Display one photo
  1022. if ($datatype === 'image') {
  1023. // fetch image, item containing image, then comments
  1024. $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'
  1025. $sql_extra ORDER BY `scale` ASC ",
  1026. intval($owner_uid),
  1027. DBA::escape($datum)
  1028. );
  1029. if (!DBA::isResult($ph)) {
  1030. $ph = q("SELECT `id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'
  1031. LIMIT 1",
  1032. intval($owner_uid),
  1033. DBA::escape($datum)
  1034. );
  1035. if (DBA::isResult($ph)) {
  1036. notice(L10n::t('Permission denied. Access to this item may be restricted.'));
  1037. } else {
  1038. notice(L10n::t('Photo not available') . EOL);
  1039. }
  1040. return;
  1041. }
  1042. $prevlink = '';
  1043. $nextlink = '';
  1044. /*
  1045. * @todo This query is totally bad, the whole functionality has to be changed
  1046. * The query leads to a really intense used index.
  1047. * By now we hide it if someone wants to.
  1048. */
  1049. if (!Config::get('system', 'no_count', false)) {
  1050. $order_field = defaults($_GET, 'order', '');
  1051. if ($order_field === 'posted') {
  1052. $order = 'ASC';
  1053. } else {
  1054. $order = 'DESC';
  1055. }
  1056. $prvnxt = q("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0
  1057. $sql_extra ORDER BY `created` $order ",
  1058. DBA::escape($ph[0]['album']),
  1059. intval($owner_uid)
  1060. );
  1061. if (DBA::isResult($prvnxt)) {
  1062. $prv = null;
  1063. $nxt = null;
  1064. foreach ($prvnxt as $z => $entry) {
  1065. if ($entry['resource-id'] == $ph[0]['resource-id']) {
  1066. $prv = $z - 1;
  1067. $nxt = $z + 1;
  1068. if ($prv < 0) {
  1069. $prv = count($prvnxt) - 1;
  1070. }
  1071. if ($nxt >= count($prvnxt)) {
  1072. $nxt = 0;
  1073. }
  1074. break;
  1075. }
  1076. }
  1077. $edit_suffix = ((($cmd === 'edit') && $can_post) ? '/edit' : '');
  1078. if (!is_null($prv)) {
  1079. $prevlink = 'photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$prv]['resource-id'] . $edit_suffix . ($order_field === 'posted' ? '?f=&order=posted' : '');
  1080. }
  1081. if (!is_null($nxt)) {
  1082. $nextlink = 'photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$nxt]['resource-id'] . $edit_suffix . ($order_field === 'posted' ? '?f=&order=posted' : '');
  1083. }
  1084. }
  1085. }
  1086. if (count($ph) == 1) {
  1087. $hires = $lores = $ph[0];
  1088. }
  1089. if (count($ph) > 1) {
  1090. if ($ph[1]['scale'] == 2) {
  1091. // original is 640 or less, we can display it directly
  1092. $hires = $lores = $ph[0];
  1093. } else {
  1094. $hires = $ph[0];
  1095. $lores = $ph[1];
  1096. }
  1097. }
  1098. $album_link = 'photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']);
  1099. $tools = null;
  1100. $lock = null;
  1101. if ($can_post && ($ph[0]['uid'] == $owner_uid)) {
  1102. $tools = [
  1103. 'edit' => ['photos/' . $a->data['user']['nickname'] . '/image/' . $datum . (($cmd === 'edit') ? '' : '/edit'), (($cmd === 'edit') ? L10n::t('View photo') : L10n::t('Edit photo'))],
  1104. 'profile'=>['profile_photo/use/'.$ph[0]['resource-id'], L10n::t('Use as profile photo')],
  1105. ];
  1106. // lock
  1107. $lock = ((($ph[0]['uid'] == local_user()) && (strlen($ph[0]['allow_cid']) || strlen($ph[0]['allow_gid'])
  1108. || strlen($ph[0]['deny_cid']) || strlen($ph[0]['deny_gid'])))
  1109. ? L10n::t('Private Message')
  1110. : Null);
  1111. }
  1112. if ($cmd === 'edit') {
  1113. $tpl = Renderer::getMarkupTemplate('photo_edit_head.tpl');
  1114. $a->page['htmlhead'] .= Renderer::replaceMacros($tpl,[
  1115. '$prevlink' => $prevlink,
  1116. '$nextlink' => $nextlink
  1117. ]);
  1118. }
  1119. if ($prevlink) {
  1120. $prevlink = [$prevlink, '<div class="icon prev"></div>'];
  1121. }
  1122. $photo = [
  1123. 'href' => 'photo/' . $hires['resource-id'] . '-' . $hires['scale'] . '.' . $phototypes[$hires['type']],
  1124. 'title'=> L10n::t('View Full Size'),
  1125. 'src' => 'photo/' . $lores['resource-id'] . '-' . $lores['scale'] . '.' . $phototypes[$lores['type']] . '?f=&_u=' . DateTimeFormat::utcNow('ymdhis'),
  1126. 'height' => $hires['height'],
  1127. 'width' => $hires['width'],
  1128. 'album' => $hires['album'],
  1129. 'filename' => $hires['filename'],
  1130. ];
  1131. if ($nextlink) {
  1132. $nextlink = [$nextlink, '<div class="icon next"></div>'];
  1133. }
  1134. // Do we have an item for this photo?
  1135. // FIXME! - replace following code to display the conversation with our normal
  1136. // conversation functions so that it works correctly and tracks changes
  1137. // in the evolving conversation code.
  1138. // The difference is that we won't be displaying the conversation head item
  1139. // as a "post" but displaying instead the photo it is linked to
  1140. /// @todo Rewrite this query. To do so, $sql_extra must be changed
  1141. $linked_items = q("SELECT `id` FROM `item` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
  1142. DBA::escape($datum)
  1143. );
  1144. $map = null;
  1145. $link_item = [];
  1146. $total = 0;
  1147. if (DBA::isResult($linked_items)) {
  1148. // This is a workaround to not being forced to rewrite the while $sql_extra handling
  1149. $link_item = Item::selectFirst([], ['id' => $linked_items[0]['id']]);
  1150. $condition = ["`parent` = ? AND `parent` != `id`", $link_item['parent']];
  1151. $total = DBA::count('item', $condition);
  1152. $pager = new Pager($a->query_string);
  1153. $params = ['order' => ['id'], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
  1154. $result = Item::selectForUser($link_item['uid'], Item::ITEM_FIELDLIST, $condition, $params);
  1155. $items = Item::inArray($result);
  1156. if (local_user() && (local_user() == $link_item['uid'])) {
  1157. Item::update(['unseen' => false], ['parent' => $link_item['parent']]);
  1158. }
  1159. if ($link_item['coord']) {
  1160. $map = Map::byCoordinates($link_item['coord']);
  1161. }
  1162. }
  1163. $tags = null;
  1164. if (count($linked_items) && strlen($link_item['tag'])) {
  1165. $arr = explode(',', $link_item['tag']);
  1166. // parse tags and add links
  1167. $tag_arr = [];
  1168. foreach ($arr as $tag) {
  1169. $tag_arr[] = [
  1170. 'name' => BBCode::convert($tag),
  1171. 'removeurl' => '/tagrm/'.$link_item['id'] . '/' . bin2hex($tag)
  1172. ];
  1173. }
  1174. $tags = ['title' => L10n::t('Tags: '), 'tags' => $tag_arr];
  1175. if ($cmd === 'edit') {
  1176. $tags['removeanyurl'] = 'tagrm/' . $link_item['id'];
  1177. $tags['removetitle'] = L10n::t('[Select tags to remove]');
  1178. }
  1179. }
  1180. $edit = Null;
  1181. if ($cmd === 'edit' && $can_post) {
  1182. $edit_tpl = Renderer::getMarkupTemplate('photo_edit.tpl');
  1183. $album_e = $ph[0]['album'];
  1184. $caption_e = $ph[0]['desc'];
  1185. $aclselect_e = ACL::getFullSelectorHTML($a->user, false, $ph[0]);
  1186. $edit = Renderer::replaceMacros($edit_tpl, [
  1187. '$id' => $ph[0]['id'],
  1188. '$album' => ['albname', L10n::t('New album name'), $album_e,''],
  1189. '$caption' => ['desc', L10n::t('Caption'), $caption_e, ''],
  1190. '$tags' => ['newtag', L10n::t('Add a Tag'), "", L10n::t('Example: @bob, @Barbara_Jensen, @jim@example.com, #California, #camping')],
  1191. '$rotate_none' => ['rotate', L10n::t('Do not rotate'),0,'', true],
  1192. '$rotate_cw' => ['rotate', L10n::t("Rotate CW \x28right\x29"),1,''],
  1193. '$rotate_ccw' => ['rotate', L10n::t("Rotate CCW \x28left\x29"),2,''],
  1194. '$nickname' => $a->data['user']['nickname'],
  1195. '$resource_id' => $ph[0]['resource-id'],
  1196. '$permissions' => L10n::t('Permissions'),
  1197. '$aclselect' => $aclselect_e,
  1198. '$item_id' => defaults($link_item, 'id', 0),
  1199. '$submit' => L10n::t('Submit'),
  1200. '$delete' => L10n::t('Delete Photo'),
  1201. // ACL permissions box
  1202. '$group_perms' => L10n::t('Show to Groups'),
  1203. '$contact_perms' => L10n::t('Show to Contacts'),
  1204. '$return_path' => $a->query_string,
  1205. ]);
  1206. }
  1207. $like = '';
  1208. $dislike = '';
  1209. $likebuttons = '';
  1210. $comments = '';
  1211. $paginate = '';
  1212. $responses = '';
  1213. if (count($linked_items)) {
  1214. $cmnt_tpl = Renderer::getMarkupTemplate('comment_item.tpl');
  1215. $tpl = Renderer::getMarkupTemplate('photo_item.tpl');
  1216. $return_path = $a->cmd;
  1217. if ($can_post || Security::canWriteToUserWall($owner_uid)) {
  1218. $like_tpl = Renderer::getMarkupTemplate('like_noshare.tpl');
  1219. $likebuttons = Renderer::replaceMacros($like_tpl, [
  1220. '$id' => $link_item['id'],
  1221. '$likethis' => L10n::t("I like this \x28toggle\x29"),
  1222. '$nolike' => L10n::t("I don't like this \x28toggle\x29"),
  1223. '$wait' => L10n::t('Please wait'),
  1224. '$return_path' => $a->query_string,
  1225. ]);
  1226. }
  1227. if (!DBA::isResult($items)) {
  1228. if (($can_post || Security::canWriteToUserWall($owner_uid))) {
  1229. $comments .= Renderer::replaceMacros($cmnt_tpl, [
  1230. '$return_path' => '',
  1231. '$jsreload' => $return_path,
  1232. '$id' => $link_item['id'],
  1233. '$parent' => $link_item['id'],
  1234. '$profile_uid' => $owner_uid,
  1235. '$mylink' => $contact['url'],
  1236. '$mytitle' => L10n::t('This is you'),
  1237. '$myphoto' => $contact['thumb'],
  1238. '$comment' => L10n::t('Comment'),
  1239. '$submit' => L10n::t('Submit'),
  1240. '$preview' => L10n::t('Preview'),
  1241. '$sourceapp' => L10n::t($a->sourcename),
  1242. '$ww' => '',
  1243. '$rand_num' => Crypto::randomDigits(12)
  1244. ]);
  1245. }
  1246. }
  1247. $conv_responses = [
  1248. 'like' => ['title' => L10n::t('Likes','title')],'dislike' => ['title' => L10n::t('Dislikes','title')],
  1249. 'attendyes' => ['title' => L10n::t('Attending','title')], 'attendno' => ['title' => L10n::t('Not attending','title')], 'attendmaybe' => ['title' => L10n::t('Might attend','title')]
  1250. ];
  1251. // display comments
  1252. if (DBA::isResult($items)) {
  1253. foreach ($items as $item) {
  1254. builtin_activity_puller($item, $conv_responses);
  1255. }
  1256. if (!empty($conv_responses['like'][$link_item['uri']])) {
  1257. $like = format_like($conv_responses['like'][$link_item['uri']], $conv_responses['like'][$link_item['uri'] . '-l'], 'like', $link_item['id']);
  1258. }
  1259. if (!empty($conv_responses['dislike'][$link_item['uri']])) {
  1260. $dislike = format_like($conv_responses['dislike'][$link_item['uri']], $conv_responses['dislike'][$link_item['uri'] . '-l'], 'dislike', $link_item['id']);
  1261. }
  1262. if (($can_post || Security::canWriteToUserWall($owner_uid))) {
  1263. $comments .= Renderer::replaceMacros($cmnt_tpl,[
  1264. '$return_path' => '',
  1265. '$jsreload' => $return_path,
  1266. '$id' => $link_item['id'],
  1267. '$parent' => $link_item['id'],
  1268. '$profile_uid' => $owner_uid,
  1269. '$mylink' => $contact['url'],
  1270. '$mytitle' => L10n::t('This is you'),
  1271. '$myphoto' => $contact['thumb'],
  1272. '$comment' => L10n::t('Comment'),
  1273. '$submit' => L10n::t('Submit'),
  1274. '$preview' => L10n::t('Preview'),
  1275. '$sourceapp' => L10n::t($a->sourcename),
  1276. '$ww' => '',
  1277. '$rand_num' => Crypto::randomDigits(12)
  1278. ]);
  1279. }
  1280. foreach ($items as $item) {
  1281. $comment = '';
  1282. $template = $tpl;
  1283. $sparkle = '';
  1284. if ((activity_match($item['verb'], ACTIVITY_LIKE) || activity_match($item['verb'], ACTIVITY_DISLIKE)) && ($item['id'] != $item['parent'])) {
  1285. continue;
  1286. }
  1287. $profile_url = Contact::MagicLinkById($item['author-id']);
  1288. if (strpos($profile_url, 'redir/') === 0) {
  1289. $sparkle = ' sparkle';
  1290. } else {
  1291. $sparkle = '';
  1292. }
  1293. $dropping = (($item['contact-id'] == $contact_id) || ($item['uid'] == local_user()));
  1294. $drop = [
  1295. 'dropping' => $dropping,
  1296. 'pagedrop' => false,
  1297. 'select' => L10n::t('Select'),
  1298. 'delete' => L10n::t('Delete'),
  1299. ];
  1300. $title_e = $item['title'];
  1301. $body_e = BBCode::convert($item['body']);
  1302. $comments .= Renderer::replaceMacros($template,[
  1303. '$id' => $item['id'],
  1304. '$profile_url' => $profile_url,
  1305. '$name' => $item['author-name'],
  1306. '$thumb' => $item['author-avatar'],
  1307. '$sparkle' => $sparkle,
  1308. '$title' => $title_e,
  1309. '$body' => $body_e,
  1310. '$ago' => Temporal::getRelativeDate($item['created']),
  1311. '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
  1312. '$drop' => $drop,
  1313. '$comment' => $comment
  1314. ]);
  1315. if (($can_post || Security::canWriteToUserWall($owner_uid))) {
  1316. $comments .= Renderer::replaceMacros($cmnt_tpl, [
  1317. '$return_path' => '',
  1318. '$jsreload' => $return_path,
  1319. '$id' => $item['item_id'],
  1320. '$parent' => $item['parent'],
  1321. '$profile_uid' => $owner_uid,
  1322. '$mylink' => $contact['url'],
  1323. '$mytitle' => L10n::t('This is you'),
  1324. '$myphoto' => $contact['thumb'],
  1325. '$comment' => L10n::t('Comment'),
  1326. '$submit' => L10n::t('Submit'),
  1327. '$preview' => L10n::t('Preview'),
  1328. '$sourceapp' => L10n::t($a->sourcename),
  1329. '$ww' => '',
  1330. '$rand_num' => Crypto::randomDigits(12)
  1331. ]);
  1332. }
  1333. }
  1334. }
  1335. $response_verbs = ['like'];
  1336. $response_verbs[] = 'dislike';
  1337. $responses = get_responses($conv_responses, $response_verbs, '', $link_item);
  1338. $paginate = $pager->renderFull($total);
  1339. }
  1340. $photo_tpl = Renderer::getMarkupTemplate('photo_view.tpl');
  1341. $o .= Renderer::replaceMacros($photo_tpl, [
  1342. '$id' => $ph[0]['id'],
  1343. '$album' => [$album_link, $ph[0]['album']],
  1344. '$tools' => $tools,
  1345. '$lock' => $lock,
  1346. '$photo' => $photo,
  1347. '$prevlink' => $prevlink,
  1348. '$nextlink' => $nextlink,
  1349. '$desc' => $ph[0]['desc'],
  1350. '$tags' => $tags,
  1351. '$edit' => $edit,
  1352. '$map' => $map,
  1353. '$map_text' => L10n::t('Map'),
  1354. '$likebuttons' => $likebuttons,
  1355. '$like' => $like,
  1356. '$dislike' => $dislike,
  1357. 'responses' => $responses,
  1358. '$comments' => $comments,
  1359. '$paginate' => $paginate,
  1360. ]);
  1361. $a->page['htmlhead'] .= "\n" . '<meta name="twitter:card" content="summary_large_image" />' . "\n";
  1362. $a->page['htmlhead'] .= '<meta name="twitter:title" content="' . $photo["album"] . '" />' . "\n";
  1363. $a->page['htmlhead'] .= '<meta name="twitter:image" content="' . $photo["href"] . '" />' . "\n";
  1364. $a->page['htmlhead'] .= '<meta name="twitter:image:width" content="' . $photo["width"] . '" />' . "\n";
  1365. $a->page['htmlhead'] .= '<meta name="twitter:image:height" content="' . $photo["height"] . '" />' . "\n";
  1366. return $o;
  1367. }
  1368. // Default - show recent photos with upload link (if applicable)
  1369. //$o = '';
  1370. $total = 0;
  1371. $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s'
  1372. $sql_extra GROUP BY `resource-id`",
  1373. intval($a->data['user']['uid']),
  1374. DBA::escape('Contact Photos'),
  1375. DBA::escape(L10n::t('Contact Photos'))
  1376. );
  1377. if (DBA::isResult($r)) {
  1378. $total = count($r);
  1379. }
  1380. $pager = new Pager($a->query_string, 20);
  1381. $r = q("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`,
  1382. ANY_VALUE(`type`) AS `type`, ANY_VALUE(`album`) AS `album`, max(`scale`) AS `scale`,
  1383. ANY_VALUE(`created`) AS `created` FROM `photo`
  1384. WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s'
  1385. $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
  1386. intval($a->data['user']['uid']),
  1387. DBA::escape('Contact Photos'),
  1388. DBA::escape(L10n::t('Contact Photos')),
  1389. $pager->getStart(),
  1390. $pager->getItemsPerPage()
  1391. );
  1392. $photos = [];
  1393. if (DBA::isResult($r)) {
  1394. // "Twist" is only used for the duepunto theme with style "slackr"
  1395. $twist = false;
  1396. foreach ($r as $rr) {
  1397. //hide profile photos to others
  1398. if (!$is_owner && !remote_user() && ($rr['album'] == L10n::t('Profile Photos'))) {
  1399. continue;
  1400. }
  1401. $twist = !$twist;
  1402. $ext = $phototypes[$rr['type']];
  1403. $alt_e = $rr['filename'];
  1404. $name_e = $rr['album'];
  1405. $photos[] = [
  1406. 'id' => $rr['id'],
  1407. 'twist' => ' ' . ($twist ? 'rotleft' : 'rotright') . rand(2,4),
  1408. 'link' => 'photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
  1409. 'title' => L10n::t('View Photo'),
  1410. 'src' => 'photo/' . $rr['resource-id'] . '-' . ((($rr['scale']) == 6) ? 4 : $rr['scale']) . '.' . $ext,
  1411. 'alt' => $alt_e,
  1412. 'album' => [
  1413. 'link' => 'photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
  1414. 'name' => $name_e,
  1415. 'alt' => L10n::t('View Album'),
  1416. ],
  1417. ];
  1418. }
  1419. }
  1420. $tpl = Renderer::getMarkupTemplate('photos_recent.tpl');
  1421. $o .= Renderer::replaceMacros($tpl, [
  1422. '$title' => L10n::t('Recent Photos'),
  1423. '$can_post' => $can_post,
  1424. '$upload' => [L10n::t('Upload New Photos'), 'photos/'.$a->data['user']['nickname'].'/upload'],
  1425. '$photos' => $photos,
  1426. '$paginate' => $pager->renderFull($total),
  1427. ]);
  1428. return $o;
  1429. }