]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/File_oembed.php
7ef1b7cde6aa3a169298ee9563341fdc74990886
[quix0rs-gnu-social.git] / classes / File_oembed.php
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
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
23 require_once INSTALLDIR.'/classes/File_redirection.php';
24
25 /**
26  * Table Definition for file_oembed
27  */
28
29 class File_oembed extends Managed_DataObject
30 {
31     ###START_AUTOCODE
32     /* the code below is auto generated do not remove the above tag */
33
34     public $__table = 'file_oembed';                     // table name
35     public $file_id;                         // int(4)  primary_key not_null
36     public $version;                         // varchar(20)
37     public $type;                            // varchar(20)
38     public $mimetype;                        // varchar(50)
39     public $provider;                        // varchar(50)
40     public $provider_url;                    // varchar(255)
41     public $width;                           // int(4)
42     public $height;                          // int(4)
43     public $html;                            // text()
44     public $title;                           // varchar(255)
45     public $author_name;                     // varchar(50)
46     public $author_url;                      // varchar(255)
47     public $url;                             // varchar(255)
48     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
49
50     /* the code above is auto generated do not remove the tag below */
51     ###END_AUTOCODE
52
53     public static function schemaDef()
54     {
55         return array(
56             'fields' => array(
57                 'file_id' => array('type' => 'int', 'not null' => true, 'description' => 'oEmbed for that URL/file'),
58                 'version' => array('type' => 'varchar', 'length' => 20, 'description' => 'oEmbed spec. version'),
59                 'type' => array('type' => 'varchar', 'length' => 20, 'description' => 'oEmbed type: photo, video, link, rich'),
60                 'mimetype' => array('type' => 'varchar', 'length' => 50, 'description' => 'mime type of resource'),
61                 'provider' => array('type' => 'varchar', 'length' => 50, 'description' => 'name of this oEmbed provider'),
62                 'provider_url' => array('type' => 'varchar', 'length' => 255, 'description' => 'URL of this oEmbed provider'),
63                 'width' => array('type' => 'int', 'description' => 'width of oEmbed resource when available'),
64                 'height' => array('type' => 'int', 'description' => 'height of oEmbed resource when available'),
65                 'html' => array('type' => 'text', 'description' => 'html representation of this oEmbed resource when applicable'),
66                 'title' => array('type' => 'varchar', 'length' => 255, 'description' => 'title of oEmbed resource when available'),
67                 'author_name' => array('type' => 'varchar', 'length' => 50, 'description' => 'author name for this oEmbed resource'),
68                 'author_url' => array('type' => 'varchar', 'length' => 255, 'description' => 'author URL for this oEmbed resource'),
69                 'url' => array('type' => 'varchar', 'length' => 255, 'description' => 'URL for this oEmbed resource when applicable (photo, link)'),
70                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
71             ),
72             'primary key' => array('file_id'),
73             'foreign keys' => array(
74                 'file_oembed_file_id_fkey' => array('file', array('file_id' => 'id')),
75             ),
76         );
77     }
78
79     function _getOembed($url) {
80         $parameters = array(
81             'maxwidth' => common_config('attachments', 'thumb_width'),
82             'maxheight' => common_config('attachments', 'thumb_height'),
83         );
84         try {
85             return oEmbedHelper::getObject($url, $parameters);
86         } catch (Exception $e) {
87             common_log(LOG_ERR, "Error during oembed lookup for $url - " . $e->getMessage());
88             return false;
89         }
90     }
91
92     /**
93      * Save embedding info for a new file.
94      *
95      * @param object $data Services_oEmbed_Object_*
96      * @param int $file_id
97      */
98     function saveNew($data, $file_id) {
99         $file_oembed = new File_oembed;
100         $file_oembed->file_id = $file_id;
101         $file_oembed->version = $data->version;
102         $file_oembed->type = $data->type;
103         if (!empty($data->provider_name)) $file_oembed->provider = $data->provider_name;
104         if (!empty($data->provider)) $file_oembed->provider = $data->provider;
105         if (!empty($data->provider_url)) $file_oembed->provider_url = $data->provider_url;
106         if (!empty($data->width)) $file_oembed->width = intval($data->width);
107         if (!empty($data->height)) $file_oembed->height = intval($data->height);
108         if (!empty($data->html)) $file_oembed->html = $data->html;
109         if (!empty($data->title)) $file_oembed->title = $data->title;
110         if (!empty($data->author_name)) $file_oembed->author_name = $data->author_name;
111         if (!empty($data->author_url)) $file_oembed->author_url = $data->author_url;
112         if (!empty($data->url)){
113             $file_oembed->url = $data->url;
114             $given_url = File_redirection::_canonUrl($file_oembed->url);
115             if (! empty($given_url)){
116                 $file = File::staticGet('url', $given_url);
117                 if (empty($file)) {
118                     $file_redir = File_redirection::staticGet('url', $given_url);
119                     if (empty($file_redir)) {
120                         $redir_data = File_redirection::where($given_url);
121                         $file_oembed->mimetype = $redir_data['type'];
122                     } else {
123                         $file_id = $file_redir->file_id;
124                     }
125                 } else {
126                     $file_oembed->mimetype=$file->mimetype;
127                 }
128             }
129         }
130         $file_oembed->insert();
131         if (!empty($data->thumbnail_url) || ($data->type == 'photo')) {
132             $ft = File_thumbnail::staticGet('file_id', $file_id);
133             if (!empty($ft)) {
134                 common_log(LOG_WARNING, "Strangely, a File_thumbnail object exists for new file $file_id",
135                            __FILE__);
136             } else {
137                 File_thumbnail::saveNew($data, $file_id);
138             }
139         }
140     }
141 }