File_embed.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. * OembedPlugin implementation for GNU social
  18. *
  19. * @package GNUsocial
  20. * @author Stephen Paul Weber
  21. * @author Mikael Nordfeldth
  22. * @author Diogo Cordeiro <diogo@fc.up.pt>
  23. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die();
  27. /**
  28. * Table Definition for file_embed
  29. *
  30. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  31. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  32. */
  33. class File_embed extends Managed_DataObject
  34. {
  35. public $__table = 'file_embed'; // table name
  36. public $file_id; // int(4) primary_key not_null
  37. public $version; // varchar(20)
  38. public $type; // varchar(20)
  39. public $mimetype; // varchar(50)
  40. public $provider; // varchar(50)
  41. public $provider_url; // varchar(191) not 255 because utf8mb4 takes more space
  42. public $width; // int(4)
  43. public $height; // int(4)
  44. public $html; // text()
  45. public $title; // varchar(191) not 255 because utf8mb4 takes more space
  46. public $author_name; // varchar(50)
  47. public $author_url; // varchar(191) not 255 because utf8mb4 takes more space
  48. public $url; // varchar(191) not 255 because utf8mb4 takes more space
  49. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  50. public static function schemaDef()
  51. {
  52. return array(
  53. 'fields' => array(
  54. 'file_id' => array('type' => 'int', 'not null' => true, 'description' => 'oEmbed for that URL/file'),
  55. 'version' => array('type' => 'varchar', 'length' => 20, 'description' => 'oEmbed spec. version'),
  56. 'type' => array('type' => 'varchar', 'length' => 20, 'description' => 'oEmbed type: photo, video, link, rich'),
  57. 'mimetype' => array('type' => 'varchar', 'length' => 50, 'description' => 'mime type of resource'),
  58. 'provider' => array('type' => 'text', 'description' => 'name of this oEmbed provider'),
  59. 'provider_url' => array('type' => 'text', 'description' => 'URL of this oEmbed provider'),
  60. 'width' => array('type' => 'int', 'description' => 'width of oEmbed resource when available'),
  61. 'height' => array('type' => 'int', 'description' => 'height of oEmbed resource when available'),
  62. 'html' => array('type' => 'text', 'description' => 'html representation of this oEmbed resource when applicable'),
  63. 'title' => array('type' => 'text', 'description' => 'title of oEmbed resource when available'),
  64. 'author_name' => array('type' => 'text', 'description' => 'author name for this oEmbed resource'),
  65. 'author_url' => array('type' => 'text', 'description' => 'author URL for this oEmbed resource'),
  66. 'url' => array('type' => 'text', 'description' => 'URL for this oEmbed resource when applicable (photo, link)'),
  67. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  68. ),
  69. 'primary key' => array('file_id'),
  70. 'foreign keys' => array(
  71. 'file_embed_file_id_fkey' => array('file', array('file_id' => 'id')),
  72. ),
  73. );
  74. }
  75. public static function getEmbed($url)
  76. {
  77. try {
  78. return EmbedHelper::getObject($url);
  79. } catch (Exception $e) {
  80. common_log(LOG_INFO, "Error during Embed lookup for {$url} - " . $e->getMessage());
  81. return false;
  82. }
  83. }
  84. /**
  85. * Fetch an entry by using a File's id
  86. */
  87. public static function getByFile(File $file)
  88. {
  89. $fo = new File_embed();
  90. $fo->file_id = $file->id;
  91. if (!$fo->find(true)) {
  92. throw new NoResultException($fo);
  93. }
  94. return $fo;
  95. }
  96. public function getUrl()
  97. {
  98. return $this->url;
  99. }
  100. /**
  101. * Save embedding info for a new file.
  102. *
  103. * @param object $data Services_oEmbed_Object_*
  104. * @param int $file_id
  105. */
  106. public static function saveNew($data, $file_id)
  107. {
  108. $file_embed = new File_embed;
  109. $file_embed->file_id = $file_id;
  110. if (!isset($data->version)) {
  111. common_debug('Embed: data->version undefined in variable $data: '.var_export($data, true));
  112. }
  113. $file_embed->version = $data->version;
  114. $file_embed->type = $data->type;
  115. if (!empty($data->provider)) {
  116. $file_embed->provider = $data->provider;
  117. }
  118. if (!empty($data->provider_name)) {
  119. $file_embed->provider = $data->provider_name;
  120. }
  121. if (!empty($data->provider_url)) {
  122. $file_embed->provider_url = $data->provider_url;
  123. }
  124. if (!empty($data->width)) {
  125. $file_embed->width = intval($data->width);
  126. }
  127. if (!empty($data->height)) {
  128. $file_embed->height = intval($data->height);
  129. }
  130. if (!empty($data->html)) {
  131. $file_embed->html = $data->html;
  132. }
  133. if (!empty($data->title)) {
  134. $file_embed->title = $data->title;
  135. }
  136. if (!empty($data->author_name)) {
  137. $file_embed->author_name = $data->author_name;
  138. }
  139. if (!empty($data->author_url)) {
  140. $file_embed->author_url = $data->author_url;
  141. }
  142. if (!empty($data->url)) {
  143. $file_embed->url = $data->url;
  144. $given_url = File_redirection::_canonUrl($file_embed->url);
  145. if (! empty($given_url)) {
  146. try {
  147. $file = File::getByUrl($given_url);
  148. $file_embed->mimetype = $file->mimetype;
  149. } catch (NoResultException $e) {
  150. // File_redirection::where argument 'discover' is false to avoid loops
  151. $redir = File_redirection::where($given_url, false);
  152. if (!empty($redir->file_id)) {
  153. $file_id = $redir->file_id;
  154. }
  155. }
  156. }
  157. }
  158. $result = $file_embed->insert();
  159. if ($result === false) {
  160. throw new ServerException('Failed to insert File_embed data into database!');
  161. }
  162. if (!empty($data->thumbnail_url) || ($data->type == 'photo')) {
  163. $ft = File_thumbnail::getKV('file_id', $file_id);
  164. if ($ft instanceof File_thumbnail) {
  165. common_log(
  166. LOG_WARNING,
  167. "Strangely, a File_thumbnail object exists for new file $file_id",
  168. __FILE__
  169. );
  170. } else {
  171. File_thumbnail::saveNew($data, $file_id);
  172. }
  173. }
  174. }
  175. }