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.

394 lines
10KB

  1. <?php
  2. /**
  3. * @file mod/videos.php
  4. */
  5. use Friendica\App;
  6. use Friendica\Content\Nav;
  7. use Friendica\Content\Pager;
  8. use Friendica\Core\Config;
  9. use Friendica\Core\L10n;
  10. use Friendica\Core\Renderer;
  11. use Friendica\Core\System;
  12. use Friendica\Database\DBA;
  13. use Friendica\Model\Contact;
  14. use Friendica\Model\Group;
  15. use Friendica\Model\Item;
  16. use Friendica\Model\Profile;
  17. use Friendica\Protocol\DFRN;
  18. use Friendica\Util\Security;
  19. function videos_init(App $a)
  20. {
  21. if ($a->argc > 1) {
  22. DFRN::autoRedir($a, $a->argv[1]);
  23. }
  24. if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
  25. return;
  26. }
  27. Nav::setSelected('home');
  28. $o = '';
  29. if ($a->argc > 1) {
  30. $nick = $a->argv[1];
  31. $user = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
  32. DBA::escape($nick)
  33. );
  34. if (!DBA::isResult($user)) {
  35. return;
  36. }
  37. $a->data['user'] = $user[0];
  38. $a->profile_uid = $user[0]['uid'];
  39. $profile = Profile::getByNickname($nick, $a->profile_uid);
  40. $account_type = Contact::getAccountType($profile);
  41. $tpl = Renderer::getMarkupTemplate("vcard-widget.tpl");
  42. $vcard_widget = Renderer::replaceMacros($tpl, [
  43. '$name' => $profile['name'],
  44. '$photo' => $profile['photo'],
  45. '$addr' => defaults($profile, 'addr', ''),
  46. '$account_type' => $account_type,
  47. '$pdesc' => defaults($profile, 'pdesc', ''),
  48. ]);
  49. /// @TODO Old-lost code?
  50. /*$sql_extra = Security::getPermissionsSQLByUserId($a->data['user']['uid']);
  51. $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d $sql_extra order by created desc",
  52. intval($a->data['user']['uid'])
  53. );
  54. if(count($albums)) {
  55. $a->data['albums'] = $albums;
  56. $albums_visible = ((intval($a->data['user']['hidewall']) && (!local_user()) && (!remote_user())) ? false : true);
  57. if($albums_visible) {
  58. $o .= '<div id="sidebar-photos-albums" class="widget">';
  59. $o .= '<h3>' . '<a href="' . System::baseUrl() . '/photos/' . $a->data['user']['nickname'] . '">' . L10n::t('Photo Albums') . '</a></h3>';
  60. $o .= '<ul>';
  61. foreach($albums as $album) {
  62. // don't show contact photos. We once translated this name, but then you could still access it under
  63. // a different language setting. Now we store the name in English and check in English (and translated for legacy albums).
  64. if((!strlen($album['album'])) || ($album['album'] === 'Contact Photos') || ($album['album'] === L10n::t('Contact Photos')))
  65. continue;
  66. $o .= '<li>' . '<a href="photos/' . $a->argv[1] . '/album/' . bin2hex($album['album']) . '" >' . $album['album'] . '</a></li>';
  67. }
  68. $o .= '</ul>';
  69. }
  70. if(local_user() && $a->data['user']['uid'] == local_user()) {
  71. $o .= '<div id="photo-albums-upload-link"><a href="' . System::baseUrl() . '/photos/' . $a->data['user']['nickname'] . '/upload" >' .L10n::t('Upload New Photos') . '</a></div>';
  72. }
  73. $o .= '</div>';
  74. }*/
  75. // If not there, create 'aside' empty
  76. if (!isset($a->page['aside'])) {
  77. $a->page['aside'] = '';
  78. }
  79. $a->page['aside'] .= $vcard_widget;
  80. $tpl = Renderer::getMarkupTemplate("videos_head.tpl");
  81. $a->page['htmlhead'] .= Renderer::replaceMacros($tpl,[
  82. '$baseurl' => System::baseUrl(),
  83. ]);
  84. }
  85. return;
  86. }
  87. function videos_post(App $a)
  88. {
  89. $owner_uid = $a->data['user']['uid'];
  90. if (local_user() != $owner_uid) {
  91. $a->internalRedirect('videos/' . $a->data['user']['nickname']);
  92. }
  93. if (($a->argc == 2) && !empty($_POST['delete']) && !empty($_POST['id'])) {
  94. // Check if we should do HTML-based delete confirmation
  95. if (empty($_REQUEST['confirm'])) {
  96. if (!empty($_REQUEST['canceled'])) {
  97. $a->internalRedirect('videos/' . $a->data['user']['nickname']);
  98. }
  99. $drop_url = $a->query_string;
  100. $a->page['content'] = Renderer::replaceMacros(Renderer::getMarkupTemplate('confirm.tpl'), [
  101. '$method' => 'post',
  102. '$message' => L10n::t('Do you really want to delete this video?'),
  103. '$extra_inputs' => [
  104. ['name' => 'id' , 'value' => $_POST['id']],
  105. ['name' => 'delete', 'value' => 'x']
  106. ],
  107. '$confirm' => L10n::t('Delete Video'),
  108. '$confirm_url' => $drop_url,
  109. '$confirm_name' => 'confirm', // Needed so that confirmation will bring us back into this if statement
  110. '$cancel' => L10n::t('Cancel'),
  111. ]);
  112. $a->error = 1; // Set $a->error so the other module functions don't execute
  113. return;
  114. }
  115. $video_id = $_POST['id'];
  116. $r = q("SELECT `id` FROM `attach` WHERE `uid` = %d AND `id` = '%s' LIMIT 1",
  117. intval(local_user()),
  118. DBA::escape($video_id)
  119. );
  120. if (DBA::isResult($r)) {
  121. q("DELETE FROM `attach` WHERE `uid` = %d AND `id` = '%s'",
  122. intval(local_user()),
  123. DBA::escape($video_id)
  124. );
  125. $i = q("SELECT `id` FROM `item` WHERE `attach` like '%%attach/%s%%' AND `uid` = %d LIMIT 1",
  126. DBA::escape($video_id),
  127. intval(local_user())
  128. );
  129. if (DBA::isResult($i)) {
  130. Item::deleteForUser(['id' => $i[0]['id']], local_user());
  131. }
  132. }
  133. $a->internalRedirect('videos/' . $a->data['user']['nickname']);
  134. return; // NOTREACHED
  135. }
  136. $a->internalRedirect('videos/' . $a->data['user']['nickname']);
  137. }
  138. function videos_content(App $a)
  139. {
  140. // URLs (most aren't currently implemented):
  141. // videos/name
  142. // videos/name/upload
  143. // videos/name/upload/xxxxx (xxxxx is album name)
  144. // videos/name/album/xxxxx
  145. // videos/name/album/xxxxx/edit
  146. // videos/name/video/xxxxx
  147. // videos/name/video/xxxxx/edit
  148. if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
  149. notice(L10n::t('Public access denied.') . EOL);
  150. return;
  151. }
  152. if (empty($a->data['user'])) {
  153. notice(L10n::t('No videos selected') . EOL );
  154. return;
  155. }
  156. //$phototypes = Photo::supportedTypes();
  157. $_SESSION['video_return'] = $a->cmd;
  158. //
  159. // Parse arguments
  160. //
  161. if ($a->argc > 3) {
  162. $datatype = $a->argv[2];
  163. $datum = $a->argv[3];
  164. } elseif(($a->argc > 2) && ($a->argv[2] === 'upload')) {
  165. $datatype = 'upload';
  166. } else {
  167. $datatype = 'summary';
  168. }
  169. if ($a->argc > 4) {
  170. $cmd = $a->argv[4];
  171. } else {
  172. $cmd = 'view';
  173. }
  174. //
  175. // Setup permissions structures
  176. //
  177. $can_post = false;
  178. $visitor = 0;
  179. $contact = null;
  180. $remote_contact = false;
  181. $contact_id = 0;
  182. $owner_uid = $a->data['user']['uid'];
  183. $community_page = (($a->data['user']['page-flags'] == Contact::PAGE_COMMUNITY) ? true : false);
  184. if ((local_user()) && (local_user() == $owner_uid)) {
  185. $can_post = true;
  186. } elseif ($community_page && remote_user()) {
  187. if (!empty($_SESSION['remote'])) {
  188. foreach ($_SESSION['remote'] as $v) {
  189. if ($v['uid'] == $owner_uid) {
  190. $contact_id = $v['cid'];
  191. break;
  192. }
  193. }
  194. }
  195. if ($contact_id > 0) {
  196. $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
  197. intval($contact_id),
  198. intval($owner_uid)
  199. );
  200. if (DBA::isResult($r)) {
  201. $can_post = true;
  202. $contact = $r[0];
  203. $remote_contact = true;
  204. $visitor = $contact_id;
  205. }
  206. }
  207. }
  208. $groups = [];
  209. // perhaps they're visiting - but not a community page, so they wouldn't have write access
  210. if (remote_user() && (!$visitor)) {
  211. $contact_id = 0;
  212. if (!empty($_SESSION['remote'])) {
  213. foreach($_SESSION['remote'] as $v) {
  214. if($v['uid'] == $owner_uid) {
  215. $contact_id = $v['cid'];
  216. break;
  217. }
  218. }
  219. }
  220. if ($contact_id > 0) {
  221. $groups = Group::getIdsByContactId($contact_id);
  222. $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
  223. intval($contact_id),
  224. intval($owner_uid)
  225. );
  226. if (DBA::isResult($r)) {
  227. $contact = $r[0];
  228. $remote_contact = true;
  229. }
  230. }
  231. }
  232. if (!$remote_contact && local_user()) {
  233. $contact_id = $_SESSION['cid'];
  234. $contact = $a->contact;
  235. }
  236. if ($a->data['user']['hidewall'] && (local_user() != $owner_uid) && (!$remote_contact)) {
  237. notice(L10n::t('Access to this item is restricted.') . EOL);
  238. return;
  239. }
  240. $sql_extra = Security::getPermissionsSQLByUserId($owner_uid, $remote_contact, $groups);
  241. $o = "";
  242. // tabs
  243. $_is_owner = (local_user() && (local_user() == $owner_uid));
  244. $o .= Profile::getTabs($a, $_is_owner, $a->data['user']['nickname']);
  245. //
  246. // dispatch request
  247. //
  248. if ($datatype === 'upload') {
  249. return; // no uploading for now
  250. // DELETED -- look at mod/photos.php if you want to implement
  251. }
  252. if ($datatype === 'album') {
  253. return; // no albums for now
  254. // DELETED -- look at mod/photos.php if you want to implement
  255. }
  256. if ($datatype === 'video') {
  257. return; // no single video view for now
  258. // DELETED -- look at mod/photos.php if you want to implement
  259. }
  260. // Default - show recent videos (no upload link for now)
  261. //$o = '';
  262. $total = 0;
  263. $r = q("SELECT hash FROM `attach` WHERE `uid` = %d AND filetype LIKE '%%video%%'
  264. $sql_extra GROUP BY hash",
  265. intval($a->data['user']['uid'])
  266. );
  267. if (DBA::isResult($r)) {
  268. $total = count($r);
  269. }
  270. $pager = new Pager($a->query_string, 20);
  271. $r = q("SELECT hash, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`created`) AS `created`,
  272. ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`filetype`) as `filetype`
  273. FROM `attach`
  274. WHERE `uid` = %d AND filetype LIKE '%%video%%'
  275. $sql_extra GROUP BY hash ORDER BY `created` DESC LIMIT %d , %d",
  276. intval($a->data['user']['uid']),
  277. $pager->getStart(),
  278. $pager->getItemsPerPage()
  279. );
  280. $videos = [];
  281. if (DBA::isResult($r)) {
  282. foreach ($r as $rr) {
  283. $alt_e = $rr['filename'];
  284. /// @todo The album isn't part of the above query. This seems to be some unfinished code that needs to be reworked completely.
  285. $rr['album'] = '';
  286. $name_e = $rr['album'];
  287. $videos[] = [
  288. 'id' => $rr['id'],
  289. 'link' => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/video/' . $rr['hash'],
  290. 'title' => L10n::t('View Video'),
  291. 'src' => System::baseUrl() . '/attach/' . $rr['id'] . '?attachment=0',
  292. 'alt' => $alt_e,
  293. 'mime' => $rr['filetype'],
  294. 'album' => [
  295. 'link' => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
  296. 'name' => $name_e,
  297. 'alt' => L10n::t('View Album'),
  298. ],
  299. ];
  300. }
  301. }
  302. $tpl = Renderer::getMarkupTemplate('videos_recent.tpl');
  303. $o .= Renderer::replaceMacros($tpl, [
  304. '$title' => L10n::t('Recent Videos'),
  305. '$can_post' => $can_post,
  306. '$upload' => [L10n::t('Upload New Videos'), System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/upload'],
  307. '$videos' => $videos,
  308. '$delete_url' => (($can_post) ? System::baseUrl() . '/videos/' . $a->data['user']['nickname'] : false)
  309. ]);
  310. $o .= $pager->renderFull($total);
  311. return $o;
  312. }