aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Module/Rbmark.php
blob: b1aea259024e923ab35b466e4e965d84e7e1951c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace Zotlabs\Module;

require_once('include/acl_selectors.php');
require_once('include/crypto.php');
require_once('include/items.php');
require_once('include/taxonomy.php');
require_once('include/conversation.php');
require_once('include/bookmarks.php');

/**
 * remote bookmark
 *
 * https://yoursite/rbmark?f=&title=&url=&private=&remote_return=
 *
 * This can be called via either GET or POST, use POST for long body content as suhosin often limits GET parameter length
 *
 * f= placeholder, often required
 * title= link text
 * url= URL to bookmark
 * ischat=1 if this bookmark is a chatroom
 * private= Don't share this link
 * remote_return= absolute URL to return after posting is finished
 *
 */
class Rbmark extends \Zotlabs\Web\Controller {

	public function post(): void {
		if($_POST['submit'] !== t('Save'))
			return;

		logger('rbmark_post: ' . print_r($_REQUEST,true));

		$channel = \App::get_channel();

		$t = array('url' => escape_tags($_REQUEST['url']),'term' => escape_tags($_REQUEST['title']));

		bookmark_add($channel,$channel,$t,((x($_REQUEST,'private')) ? intval($_REQUEST['private']) : 0),
			array('menu_id' => ((x($_REQUEST,'menu_id')) ? intval($_REQUEST['menu_id']) : 0),
			'menu_name' => ((x($_REQUEST,'menu_name')) ? escape_tags($_REQUEST['menu_name']) : ''),
			'ischat' => ((x($_REQUEST['ischat'])) ? intval($_REQUEST['ischat']) : 0)
			));

		goaway(z_root() . '/bookmarks');
	}


	public function get(): string {

		$channel_id = local_channel();
		if($channel_id === false) {

			// The login procedure is going to bugger our $_REQUEST variables
			// so save them in the session.

			if(array_key_exists('url',$_REQUEST)) {
				$_SESSION['bookmark'] = $_REQUEST;
			}
			return login();
		}

		// If we have saved rbmark session variables, but nothing in the
		// current $_REQUEST, recover the saved variables

		if((! array_key_exists('url',$_REQUEST)) && (array_key_exists('bookmark',$_SESSION))) {
			$_REQUEST = $_SESSION['bookmark'];
			unset($_SESSION['bookmark']);
		}

		$menu_select = [
			'menu_id',
			t('Select a bookmark folder'),
			false,
			'',
			$this->get_bookmark_folders(intval($channel_id)),
		];

		return replace_macros(get_markup_template('rbmark.tpl'), array(
			'$header' => t('Save Bookmark'),
			'$url' => array('url',t('URL of bookmark'),$_REQUEST['url']),
			'$title' => array('title',t('Description'),$_REQUEST['title']),
			'$ischat' => ((x($_REQUEST,'ischat')) ? intval($_REQUEST['ischat']) : 0),
			'$private' => ((x($_REQUEST,'private')) ? intval($_REQUEST['private']) : 0),
			'$submit' => t('Save'),
			'$menu_name' => array('menu_name',t('Or enter new bookmark folder name'),'',''),
			'$menus' => $menu_select
		));
	}

	private function get_bookmark_folders(int $channel_id): array {
		$menu_list = menu_list($channel_id, '', MENU_BOOKMARK);

		$menus = [ 0 => '' ];

		if ($menu_list !== false) {
			foreach($menu_list as $n) {
				$menus[$n['menu_id']] = $n['menu_name'];
			}
		}

		return $menus;
	}
}