File_oembed.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008, 2009, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('GNUSOCIAL')) { exit(1); }
  20. /**
  21. * Table Definition for file_oembed
  22. */
  23. class File_oembed extends Managed_DataObject
  24. {
  25. public $__table = 'file_oembed'; // table name
  26. public $file_id; // int(4) primary_key not_null
  27. public $version; // varchar(20)
  28. public $type; // varchar(20)
  29. public $mimetype; // varchar(50)
  30. public $provider; // varchar(50)
  31. public $provider_url; // varchar(255)
  32. public $width; // int(4)
  33. public $height; // int(4)
  34. public $html; // text()
  35. public $title; // varchar(255)
  36. public $author_name; // varchar(50)
  37. public $author_url; // varchar(255)
  38. public $url; // varchar(255)
  39. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  40. public static function schemaDef()
  41. {
  42. return array(
  43. 'fields' => array(
  44. 'file_id' => array('type' => 'int', 'not null' => true, 'description' => 'oEmbed for that URL/file'),
  45. 'version' => array('type' => 'varchar', 'length' => 20, 'description' => 'oEmbed spec. version'),
  46. 'type' => array('type' => 'varchar', 'length' => 20, 'description' => 'oEmbed type: photo, video, link, rich'),
  47. 'mimetype' => array('type' => 'varchar', 'length' => 50, 'description' => 'mime type of resource'),
  48. 'provider' => array('type' => 'varchar', 'length' => 50, 'description' => 'name of this oEmbed provider'),
  49. 'provider_url' => array('type' => 'varchar', 'length' => 255, 'description' => 'URL of this oEmbed provider'),
  50. 'width' => array('type' => 'int', 'description' => 'width of oEmbed resource when available'),
  51. 'height' => array('type' => 'int', 'description' => 'height of oEmbed resource when available'),
  52. 'html' => array('type' => 'text', 'description' => 'html representation of this oEmbed resource when applicable'),
  53. 'title' => array('type' => 'varchar', 'length' => 255, 'description' => 'title of oEmbed resource when available'),
  54. 'author_name' => array('type' => 'varchar', 'length' => 50, 'description' => 'author name for this oEmbed resource'),
  55. 'author_url' => array('type' => 'varchar', 'length' => 255, 'description' => 'author URL for this oEmbed resource'),
  56. 'url' => array('type' => 'varchar', 'length' => 255, 'description' => 'URL for this oEmbed resource when applicable (photo, link)'),
  57. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  58. ),
  59. 'primary key' => array('file_id'),
  60. 'foreign keys' => array(
  61. 'file_oembed_file_id_fkey' => array('file', array('file_id' => 'id')),
  62. ),
  63. );
  64. }
  65. function _getOembed($url) {
  66. $parameters = array(
  67. 'maxwidth' => common_config('thumbnail', 'width'),
  68. 'maxheight' => common_config('thumbnail', 'height'),
  69. );
  70. try {
  71. return oEmbedHelper::getObject($url, $parameters);
  72. } catch (Exception $e) {
  73. common_log(LOG_INFO, "Error during oembed lookup for $url - " . $e->getMessage());
  74. return false;
  75. }
  76. }
  77. /**
  78. * Fetch an entry by using a File's id
  79. */
  80. static function byFile(File $file) {
  81. $file_oembed = self::getKV('file_id', $file->id);
  82. if (!$file_oembed instanceof File_oembed) {
  83. throw new ServerException(sprintf('No File_oembed entry for File id==%u', $file->id));
  84. }
  85. return $file_oembed;
  86. }
  87. public function getUrl()
  88. {
  89. return $this->url;
  90. }
  91. /**
  92. * Save embedding info for a new file.
  93. *
  94. * @param object $data Services_oEmbed_Object_*
  95. * @param int $file_id
  96. */
  97. public static function saveNew($data, $file_id) {
  98. $file_oembed = new File_oembed;
  99. $file_oembed->file_id = $file_id;
  100. if (!isset($data->version)) {
  101. common_debug('DEBUGGING oEmbed: data->version undefined in variable $data: '.var_export($data, true));
  102. }
  103. $file_oembed->version = $data->version;
  104. $file_oembed->type = $data->type;
  105. if (!empty($data->provider_name)) $file_oembed->provider = $data->provider_name;
  106. if (!empty($data->provider)) $file_oembed->provider = $data->provider;
  107. if (!empty($data->provider_url)) $file_oembed->provider_url = $data->provider_url;
  108. if (!empty($data->width)) $file_oembed->width = intval($data->width);
  109. if (!empty($data->height)) $file_oembed->height = intval($data->height);
  110. if (!empty($data->html)) $file_oembed->html = $data->html;
  111. if (!empty($data->title)) $file_oembed->title = $data->title;
  112. if (!empty($data->author_name)) $file_oembed->author_name = $data->author_name;
  113. if (!empty($data->author_url)) $file_oembed->author_url = $data->author_url;
  114. if (!empty($data->url)){
  115. $file_oembed->url = $data->url;
  116. $given_url = File_redirection::_canonUrl($file_oembed->url);
  117. if (! empty($given_url)){
  118. $file = File::getKV('url', $given_url);
  119. if ($file instanceof File) {
  120. $file_oembed->mimetype = $file->mimetype;
  121. } else {
  122. $file_redir = File_redirection::getKV('url', $given_url);
  123. if (empty($file_redir)) {
  124. $redir_data = File_redirection::where($given_url);
  125. $file_oembed->mimetype = $redir_data['type'];
  126. } else {
  127. $file_id = $file_redir->file_id;
  128. }
  129. }
  130. }
  131. }
  132. $file_oembed->insert();
  133. if (!empty($data->thumbnail_url) || ($data->type == 'photo')) {
  134. $ft = File_thumbnail::getKV('file_id', $file_id);
  135. if ($ft instanceof File_thumbnail) {
  136. common_log(LOG_WARNING, "Strangely, a File_thumbnail object exists for new file $file_id",
  137. __FILE__);
  138. } else {
  139. File_thumbnail::saveNew($data, $file_id);
  140. }
  141. }
  142. }
  143. }