Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

mod/bookmarks/actions/bookmarks/save.php (1 issue)

1
<?php
2
/**
3
* Elgg bookmarks save action
4
*
5
* @package Bookmarks
6
*/
7
8
$title = elgg_get_title_input();
9
$description = get_input('description');
10
$address = get_input('address');
11
$access_id = get_input('access_id');
12
$tags = get_input('tags');
13
$guid = get_input('guid');
14
$container_guid = get_input('container_guid', elgg_get_logged_in_user_guid());
15
16
elgg_make_sticky_form('bookmarks');
17
18
// don't use elgg_normalize_url() because we don't want
19
// relative links resolved to this site.
20
if ($address && !preg_match("#^((ht|f)tps?:)?//#i", $address)) {
21
	$address = "http://$address";
22
}
23
24
if (!$title || !$address) {
25
	return elgg_error_response(elgg_echo('bookmarks:save:failed'));
26
}
27
28
if (!filter_var($address, FILTER_VALIDATE_URL)) {
29
	return elgg_error_response(elgg_echo('bookmarks:save:failed'));
30
}
31
32
if ($guid == 0) {
0 ignored issues
show
Bug Best Practice introduced by Brett Profitt
It seems like you are loosely comparing $guid of type null|mixed|string to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
33
	$bookmark = new ElggBookmark;
34
	$bookmark->container_guid = (int) get_input('container_guid', elgg_get_logged_in_user_guid());
35
	$new = true;
36
} else {
37
	$bookmark = get_entity($guid);
38
	if (!$bookmark instanceof ElggBookmark || !$bookmark->canEdit()) {
39
		return elgg_error_response(elgg_echo('bookmarks:save:failed'));
40
	}
41
}
42
43
$bookmark->title = $title;
44
$bookmark->address = $address;
45
$bookmark->description = $description;
46
$bookmark->access_id = $access_id;
47
$bookmark->tags = string_to_tag_array($tags);
48
49
if (!$bookmark->save()) {
50
	return elgg_error_response(elgg_echo('bookmarks:save:failed'));
51
}
52
53
elgg_clear_sticky_form('bookmarks');
54
55
//add to river only if new
56
if ($new) {
57
	elgg_create_river_item([
58
		'view' => 'river/object/bookmarks/create',
59
		'action_type' => 'create',
60
		'object_guid' => $bookmark->getGUID(),
61
	]);
62
}
63
64
return elgg_ok_response('', elgg_echo('bookmarks:save:success'), $bookmark->getURL());
65