Bookmark.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Data class to mark notices as bookmarks
  18. *
  19. * @category Data
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2009 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * For storing the fact that a notice is a bookmark
  28. *
  29. * @category Bookmark
  30. * @package GNUsocial
  31. * @author Evan Prodromou <evan@status.net>
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. *
  34. * @see DB_DataObject
  35. */
  36. class Bookmark extends Managed_DataObject
  37. {
  38. public $__table = 'bookmark'; // table name
  39. public $id; // char(36) primary_key not_null
  40. public $profile_id; // int(4) not_null
  41. public $url; // varchar(191) not_null not 255 because utf8mb4 takes more space
  42. public $title; // varchar(191) not 255 because utf8mb4 takes more space
  43. public $uri; // varchar(191) not 255 because utf8mb4 takes more space
  44. public $description; // text
  45. public $created; // datetime
  46. public static function schemaDef()
  47. {
  48. return array(
  49. 'fields' => array(
  50. 'id' => array('type' => 'char',
  51. 'length' => 36,
  52. 'not null' => true),
  53. 'profile_id' => array('type' => 'int', 'not null' => true),
  54. 'uri' => array('type' => 'varchar',
  55. 'length' => 191,
  56. 'not null' => true),
  57. 'url' => array('type' => 'varchar',
  58. 'length' => 191,
  59. 'not null' => true),
  60. 'title' => array('type' => 'varchar', 'length' => 191),
  61. 'description' => array('type' => 'text'),
  62. 'created' => array('type' => 'datetime', 'not null' => true),
  63. ),
  64. 'primary key' => array('uri'),
  65. 'unique keys' => array(
  66. 'bookmark_id_key' => array('id'),
  67. ),
  68. 'foreign keys' => array(
  69. 'bookmark_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  70. 'bookmark_uri_fkey' => array('notice', array('uri' => 'uri')),
  71. ),
  72. 'indexes' => array(
  73. 'bookmark_url_idx' => array('url'),
  74. 'bookmark_profile_id_created_idx' => array('profile_id', 'created'),
  75. ),
  76. );
  77. }
  78. /**
  79. * Get a bookmark based on a notice
  80. *
  81. * @param Notice $stored Notice activity which represents the Bookmark
  82. *
  83. * @return Bookmark The found bookmark object.
  84. * @throws NoResultException When you don't find it after all.
  85. */
  86. public static function fromStored(Notice $stored)
  87. {
  88. return self::getByPK(array('uri' => $stored->getUri()));
  89. }
  90. public function getStored()
  91. {
  92. return Notice::getByKeys(array('uri' => $this->getUri()));
  93. }
  94. public function getDescription()
  95. {
  96. return $this->description;
  97. }
  98. public function getTitle()
  99. {
  100. return $this->title;
  101. }
  102. public function getUri()
  103. {
  104. return $this->uri;
  105. }
  106. public function getUrl()
  107. {
  108. if (empty($this->url)) {
  109. throw new InvalidUrlException($this->url);
  110. }
  111. return $this->url;
  112. }
  113. /**
  114. * Get the bookmark that a user made for an URL
  115. *
  116. * @param Profile $profile Profile to check for
  117. * @param string $url URL to check for
  118. *
  119. * @return Bookmark bookmark found or null
  120. */
  121. public static function getByURL(Profile $profile, $url)
  122. {
  123. $nb = new Bookmark();
  124. $nb->profile_id = $profile->getID();
  125. $nb->url = $url;
  126. if (!$nb->find(true)) {
  127. throw new NoResultException($nb);
  128. }
  129. return $nb;
  130. }
  131. /**
  132. * Store a Bookmark object
  133. *
  134. * @param Profile $profile To save the bookmark for
  135. * @param string $title Title of the bookmark
  136. * @param string $url URL of the bookmark
  137. * @param string $description Description of the bookmark
  138. *
  139. * @return Bookmark the Bookmark object
  140. */
  141. public static function saveActivityObject(
  142. ActivityObject $actobj,
  143. Notice $stored
  144. ): self {
  145. $url = null;
  146. // each extra element is array('tagname', array('attr'=>'val', ...), 'content')
  147. foreach ($actobj->extra as $extra) {
  148. if ($extra[0] !== ActivityUtils::LINK || $extra[1][ActivityUtils::REL] !== 'related') {
  149. continue;
  150. }
  151. if ($url===null && strlen($extra[1][ActivityUtils::HREF])>0) {
  152. $url = $extra[1][ActivityUtils::HREF];
  153. } elseif ($url !== null) {
  154. // TRANS: Client exception thrown when a bookmark is formatted incorrectly.
  155. throw new ClientException(sprintf(_m('Expected exactly 1 link rel=related in a Bookmark, got more than that.')));
  156. }
  157. }
  158. if (is_null($url)) {
  159. // TRANS: Client exception thrown when a bookmark is formatted incorrectly.
  160. throw new ClientException(sprintf(_m('Expected exactly 1 link rel=related in a Bookmark, got 0.')));
  161. }
  162. if (!strlen($actobj->title)) {
  163. throw new ClientException(_m('You must provide a non-empty title.'));
  164. }
  165. if (!common_valid_http_url($url)) {
  166. throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).'));
  167. }
  168. try {
  169. $object = self::getByURL($stored->getProfile(), $url);
  170. throw new ClientException(_m('You have already bookmarked this URL.'));
  171. } catch (NoResultException $e) {
  172. // Alright, so then we have to create it.
  173. }
  174. $nb = new Bookmark();
  175. $nb->id = UUID::gen();
  176. $nb->uri = $stored->getUri();
  177. $nb->profile_id = $stored->getProfile()->getID();
  178. $nb->title = $actobj->title;
  179. $nb->url = $url;
  180. $nb->description = $actobj->summary;
  181. $nb->created = $stored->created;
  182. $result = $nb->insert();
  183. if ($result === false) {
  184. throw new ServerException('Could not insert Bookmark into database!');
  185. }
  186. return $nb;
  187. }
  188. public function asActivityObject()
  189. {
  190. $stored = $this->getStored();
  191. $object = new ActivityObject();
  192. $object->id = $this->getUri();
  193. $object->type = ActivityObject::BOOKMARK;
  194. $object->title = $this->getTitle();
  195. $object->summary = $this->getDescription();
  196. $object->link = $stored->getUrl();
  197. $object->content = $stored->getRendered();
  198. // Attributes of the URL
  199. $attachments = $stored->attachments();
  200. if (count($attachments) != 1) {
  201. // TRANS: Server exception thrown when a bookmark has multiple attachments.
  202. throw new ServerException(_m('Bookmark notice with the '.
  203. 'wrong number of attachments.'));
  204. }
  205. $bookmarkedurl = $attachments[0];
  206. $attrs = array('rel' => 'related',
  207. 'href' => $bookmarkedurl->getUrl());
  208. if (!strlen($bookmarkedurl->title)) {
  209. $attrs['title'] = $bookmarkedurl->title;
  210. }
  211. $object->extra[] = array('link', $attrs, null);
  212. // Attributes of the thumbnail, if any
  213. try {
  214. $thumbnail = $bookmarkedurl->getThumbnail();
  215. $tattrs = array('rel' => 'preview',
  216. 'href' => $thumbnail->getUrl());
  217. if (!empty($thumbnail->width)) {
  218. $tattrs['media:width'] = $thumbnail->width;
  219. }
  220. if (!empty($thumbnail->height)) {
  221. $tattrs['media:height'] = $thumbnail->height;
  222. }
  223. $object->extra[] = array('link', $tattrs, null);
  224. } catch (UnsupportedMediaException $e) {
  225. // No image thumbnail metadata available
  226. }
  227. return $object;
  228. }
  229. /**
  230. * Save a new notice bookmark
  231. *
  232. * @param Profile $profile To save the bookmark for
  233. * @param string $title Title of the bookmark
  234. * @param string $url URL of the bookmark
  235. * @param array $rawtags array of tags
  236. * @param string $description Description of the bookmark
  237. * @param array $options Options for the Notice::saveNew()
  238. *
  239. * @return Notice saved notice
  240. */
  241. public static function addNew(
  242. Profile $actor,
  243. $title,
  244. $url,
  245. array $rawtags,
  246. $description,
  247. array $options = []
  248. ) {
  249. $act = new Activity();
  250. $act->verb = ActivityVerb::POST;
  251. $act->time = time();
  252. $act->actor = $actor->asActivityObject();
  253. $actobj = new ActivityObject();
  254. $actobj->type = ActivityObject::BOOKMARK;
  255. $actobj->title = $title;
  256. $actobj->summary = $description;
  257. $actobj->extra[] = array('link', array('rel'=>'related', 'href'=>$url), null);
  258. $act->objects[] = $actobj;
  259. $act->enclosures[] = $url;
  260. $tags = array();
  261. $replies = array();
  262. // filter "for:nickname" tags
  263. foreach ($rawtags as $tag) {
  264. if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
  265. // skip if done by caller
  266. if (!array_key_exists('replies', $options)) {
  267. $nickname = mb_substr($tag, 4);
  268. $other = common_relative_profile($actor, $nickname);
  269. if (!empty($other)) {
  270. $replies[] = $other->getUri();
  271. }
  272. }
  273. } else {
  274. $tags[] = common_canonical_tag($tag);
  275. }
  276. }
  277. $hashtags = array();
  278. $taglinks = array();
  279. foreach ($tags as $tag) {
  280. $hashtags[] = '#'.$tag;
  281. $attrs = array('href' => Notice_tag::url($tag),
  282. 'rel' => $tag,
  283. 'class' => 'tag');
  284. $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
  285. }
  286. // Use user's preferences for short URLs, if possible
  287. // FIXME: Should be possible to with the Profile object...
  288. try {
  289. $user = $actor->getUser();
  290. $shortUrl = File_redirection::makeShort($url, empty($user) ? null : $user);
  291. } catch (Exception $e) {
  292. // Don't let this stop us.
  293. $shortUrl = $url;
  294. }
  295. // TRANS: Rendered bookmark content.
  296. // TRANS: %1$s is a URL, %2$s the bookmark title, %3$s is the bookmark description,
  297. // TRANS: %4$s is space separated list of hash tags.
  298. $actobj->content = sprintf(
  299. _m('<span class="xfolkentry">'
  300. . '<a class="taggedlink" href="%1$s">%2$s</a> '
  301. . '<span class="description">%3$s</span> '
  302. . '<span class="meta">%4$s</span>'
  303. . '</span>'),
  304. htmlspecialchars($url),
  305. htmlspecialchars($title),
  306. htmlspecialchars($description),
  307. implode(' ', $taglinks)
  308. );
  309. foreach ($tags as $term) {
  310. $catEl = new AtomCategory();
  311. $catEl->term = $term;
  312. $activity->categories[] = $catEl;
  313. }
  314. $options = array_merge([
  315. 'urls' => [$url],
  316. 'rendered' => $actobj->content,
  317. 'tags' => $tags,
  318. 'replies' => $replies,
  319. 'object_type' => ActivityObject::BOOKMARK,
  320. ], $options);
  321. return Notice::saveActivity($act, $actor, $options);
  322. }
  323. }