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.

396 lines
10 KiB

Cleanups: isResult() more used, readability improved (#5608) * [diaspora]: Maybe SimpleXMLElement is the right type-hint? * Changes proposed + pre-renaming: - pre-renamed $db -> $connection - added TODOs for not allowing bad method invocations (there is a BadMethodCallException in SPL) * If no record is found, below $r[0] will fail with a E_NOTICE and the code doesn't behave as expected. * Ops, one more left ... * Continued: - added documentation for Contact::updateSslPolicy() method - added type-hint for $contact of same method - empty lines added + TODO where the bug origins that $item has no element 'body' * Added empty lines for better readability * Cleaned up: - no more x() (deprecated) usage but empty() instead - fixed mixing of space/tab indending - merged else/if block goether in elseif() (lesser nested code blocks) * Re-fixed DBM -> DBA switch * Fixes/rewrites: - use empty()/isset() instead of deprecated x() - merged 2 nested if() blocks into one - avoided nested if() block inside else block by rewriting it to elseif() - $contact_id is an integer, let's test on > 0 here - added a lot spaces and some empty lines for better readability * Rewrite: - moved all CONTACT_* constants from boot.php to Contact class * CR request: - renamed Contact::CONTACT_IS_* -> Contact::* ;-) * Rewrites: - moved PAGE_* to Friendica\Model\Profile class - fixed mixure with "Contact::* rewrite" * Ops, one still there (return is no function) * Rewrite to Proxy class: - introduced new Friendica\Network\Proxy class for in exchange of proxy_*() functions - moved also all PROXY_* constants there as Proxy::* - removed now no longer needed mod/proxy.php loading as composer's auto-load will do this for us - renamed those proxy_*() functions to better names: + proxy_init() -> Proxy::init() (public) + proxy_url() -> Proxy::proxifyUrl() (public) + proxy_parse_html() -> Proxy::proxifyHtml() (public) + proxy_is_local_image() -> Proxy::isLocalImage() (private) + proxy_parse_query() -> Proxy::parseQuery() (private) + proxy_img_cb() -> Proxy::replaceUrl() (private) * CR request: - moved all PAGE_* constants to Friendica\Model\Contact class - fixed all references of both classes * Ops, need to set $a here ... * CR request: - moved Proxy class to Friendica\Module - extended BaseModule * Ops, no need for own instance of $a when self::getApp() is around. * Proxy-rewrite: - proxy_url() and proxy_parse_html() are both non-module functions (now methods) - so they must be splitted into a seperate class - also the SIZE_* and DEFAULT_TIME constants are both not relevant to module * No instances from utility classes * Fixed error: - proxify*() is now located in `Friendica\Util\ProxyUtils` * Moved back to original place, ops? How did they move here? Well, it was not intended by me. * Removed duplicate (left-over from split) constants and static array. Thank to MrPetovan finding it. * Renamed ProxyUtils -> Proxy and aliased it back to ProxyUtils. * Rewrite: - stopped using deprecated NETWORK_* constants, now Protocol::* should be used - still left them intact for slow/lazy developers ... * Ops, was added accidentally ... * Ops, why these wrong moves? * Ops, one to much (thanks to MrPetovan) * Ops, wrong moving ... * moved back to original place ... * spaces added * empty lines add for better readability. * convertered spaces -> tab for code indenting. * CR request: Add space between if and brace. * CR requests fixed + move reverted - ops, src/Module/*.php has been moved to src/Network/ accidentally - reverted some parts in src/Database/DBA.php as pointed out by Annando - removed internal TODO items - added some spaces for better readability
3 years ago
  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\System;
  11. use Friendica\Database\DBA;
  12. use Friendica\Model\Contact;
  13. use Friendica\Model\Group;
  14. use Friendica\Model\Item;
  15. use Friendica\Model\Profile;
  16. use Friendica\Protocol\DFRN;
  17. use Friendica\Util\Security;
  18. require_once 'include/items.php';
  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 = get_markup_template("vcard-widget.tpl");
  42. $vcard_widget = replace_macros($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 = get_markup_template("videos_head.tpl");
  81. $a->page['htmlhead'] .= replace_macros($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'] = replace_macros(get_markup_template('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. require_once 'include/conversation.php';
  153. if (empty($a->data['user'])) {
  154. notice(L10n::t('No videos selected') . EOL );
  155. return;
  156. }
  157. //$phototypes = Photo::supportedTypes();
  158. $_SESSION['video_return'] = $a->cmd;
  159. //
  160. // Parse arguments
  161. //
  162. if ($a->argc > 3) {
  163. $datatype = $a->argv[2];
  164. $datum = $a->argv[3];
  165. } elseif(($a->argc > 2) && ($a->argv[2] === 'upload')) {
  166. $datatype = 'upload';
  167. } else {
  168. $datatype = 'summary';
  169. }
  170. if ($a->argc > 4) {
  171. $cmd = $a->argv[4];
  172. } else {
  173. $cmd = 'view';
  174. }
  175. //
  176. // Setup permissions structures
  177. //
  178. $can_post = false;
  179. $visitor = 0;
  180. $contact = null;
  181. $remote_contact = false;
  182. $contact_id = 0;
  183. $owner_uid = $a->data['user']['uid'];
  184. $community_page = (($a->data['user']['page-flags'] == Contact::PAGE_COMMUNITY) ? true : false);
  185. if ((local_user()) && (local_user() == $owner_uid)) {
  186. $can_post = true;
  187. } elseif ($community_page && remote_user()) {
  188. if (!empty($_SESSION['remote'])) {
  189. foreach ($_SESSION['remote'] as $v) {
  190. if ($v['uid'] == $owner_uid) {
  191. $contact_id = $v['cid'];
  192. break;
  193. }
  194. }
  195. }
  196. if ($contact_id > 0) {
  197. $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
  198. intval($contact_id),
  199. intval($owner_uid)
  200. );
  201. if (DBA::isResult($r)) {
  202. $can_post = true;
  203. $contact = $r[0];
  204. $remote_contact = true;
  205. $visitor = $contact_id;
  206. }
  207. }
  208. }
  209. $groups = [];
  210. // perhaps they're visiting - but not a community page, so they wouldn't have write access
  211. if (remote_user() && (!$visitor)) {
  212. $contact_id = 0;
  213. if (!empty($_SESSION['remote'])) {
  214. foreach($_SESSION['remote'] as $v) {
  215. if($v['uid'] == $owner_uid) {
  216. $contact_id = $v['cid'];
  217. break;
  218. }
  219. }
  220. }
  221. if ($contact_id > 0) {
  222. $groups = Group::getIdsByContactId($contact_id);
  223. $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
  224. intval($contact_id),
  225. intval($owner_uid)
  226. );
  227. if (DBA::isResult($r)) {
  228. $contact = $r[0];
  229. $remote_contact = true;
  230. }
  231. }
  232. }
  233. if (!$remote_contact && local_user()) {
  234. $contact_id = $_SESSION['cid'];
  235. $contact = $a->contact;
  236. }
  237. if ($a->data['user']['hidewall'] && (local_user() != $owner_uid) && (!$remote_contact)) {
  238. notice(L10n::t('Access to this item is restricted.') . EOL);
  239. return;
  240. }
  241. $sql_extra = Security::getPermissionsSQLByUserId($owner_uid, $remote_contact, $groups);
  242. $o = "";
  243. // tabs
  244. $_is_owner = (local_user() && (local_user() == $owner_uid));
  245. $o .= Profile::getTabs($a, $_is_owner, $a->data['user']['nickname']);
  246. //
  247. // dispatch request
  248. //
  249. if ($datatype === 'upload') {
  250. return; // no uploading for now
  251. // DELETED -- look at mod/photos.php if you want to implement
  252. }
  253. if ($datatype === 'album') {
  254. return; // no albums for now
  255. // DELETED -- look at mod/photos.php if you want to implement
  256. }
  257. if ($datatype === 'video') {
  258. return; // no single video view for now
  259. // DELETED -- look at mod/photos.php if you want to implement
  260. }
  261. // Default - show recent videos (no upload link for now)
  262. //$o = '';
  263. $total = 0;
  264. $r = q("SELECT hash FROM `attach` WHERE `uid` = %d AND filetype LIKE '%%video%%'
  265. $sql_extra GROUP BY hash",
  266. intval($a->data['user']['uid'])
  267. );
  268. if (DBA::isResult($r)) {
  269. $total = count($r);
  270. }
  271. $pager = new Pager($a->query_string, 20);
  272. $r = q("SELECT hash, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`created`) AS `created`,
  273. ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`filetype`) as `filetype`
  274. FROM `attach`
  275. WHERE `uid` = %d AND filetype LIKE '%%video%%'
  276. $sql_extra GROUP BY hash ORDER BY `created` DESC LIMIT %d , %d",
  277. intval($a->data['user']['uid']),
  278. $pager->getStart(),
  279. $pager->getItemsPerPage()
  280. );
  281. $videos = [];
  282. if (DBA::isResult($r)) {
  283. foreach ($r as $rr) {
  284. $alt_e = $rr['filename'];
  285. /// @todo The album isn't part of the above query. This seems to be some unfinished code that needs to be reworked completely.
  286. $rr['album'] = '';
  287. $name_e = $rr['album'];
  288. $videos[] = [
  289. 'id' => $rr['id'],
  290. 'link' => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/video/' . $rr['hash'],
  291. 'title' => L10n::t('View Video'),
  292. 'src' => System::baseUrl() . '/attach/' . $rr['id'] . '?attachment=0',
  293. 'alt' => $alt_e,
  294. 'mime' => $rr['filetype'],
  295. 'album' => [
  296. 'link' => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
  297. 'name' => $name_e,
  298. 'alt' => L10n::t('View Album'),
  299. ],
  300. ];
  301. }
  302. }
  303. $tpl = get_markup_template('videos_recent.tpl');
  304. $o .= replace_macros($tpl, [
  305. '$title' => L10n::t('Recent Videos'),
  306. '$can_post' => $can_post,
  307. '$upload' => [L10n::t('Upload New Videos'), System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/upload'],
  308. '$videos' => $videos,
  309. '$delete_url' => (($can_post) ? System::baseUrl() . '/videos/' . $a->data['user']['nickname'] : false)
  310. ]);
  311. $o .= $pager->renderFull($total);
  312. return $o;
  313. }