]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/File_oembed.php
56afdde19724c280cf41e26c40b0578f8c93cdd2
[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     /* Static get */
51     function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('File_oembed',$k,$v); }
52
53     /* the code above is auto generated do not remove the tag below */
54     ###END_AUTOCODE
55
56     public static function schemaDef()
57     {
58         return array(
59             'fields' => array(
60                 'file_id' => array('type' => 'int', 'not null' => true, 'description' => 'oEmbed for that URL/file'),
61                 'version' => array('type' => 'varchar', 'length' => 20, 'description' => 'oEmbed spec. version'),
62                 'type' => array('type' => 'varchar', 'length' => 20, 'description' => 'oEmbed type: photo, video, link, rich'),
63                 'mimetype' => array('type' => 'varchar', 'length' => 50, 'description' => 'mime type of resource'),
64                 'provider' => array('type' => 'varchar', 'length' => 50, 'description' => 'name of this oEmbed provider'),
65                 'provider_url' => array('type' => 'varchar', 'length' => 255, 'description' => 'URL of this oEmbed provider'),
66                 'width' => array('type' => 'int', 'description' => 'width of oEmbed resource when available'),
67                 'height' => array('type' => 'int', 'description' => 'height of oEmbed resource when available'),
68                 'html' => array('type' => 'text', 'description' => 'html representation of this oEmbed resource when applicable'),
69                 'title' => array('type' => 'varchar', 'length' => 255, 'description' => 'title of oEmbed resource when available'),
70                 'author_name' => array('type' => 'varchar', 'length' => 50, 'description' => 'author name for this oEmbed resource'),
71                 'author_url' => array('type' => 'varchar', 'length' => 255, 'description' => 'author URL for this oEmbed resource'),
72                 'url' => array('type' => 'varchar', 'length' => 255, 'description' => 'URL for this oEmbed resource when applicable (photo, link)'),
73                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
74             ),
75             'primary key' => array('file_id'),
76             'foreign keys' => array(
77                 'file_oembed_file_id_fkey' => array('file', array('file_id' => 'id')),
78             ),
79         );
80     }
81
82     function sequenceKey()
83     {
84         return array(false, false, false);
85     }
86
87     function _getOembed($url) {
88         $parameters = array(
89             'maxwidth' => common_config('attachments', 'thumb_width'),
90             'maxheight' => common_config('attachments', 'thumb_height'),
91         );
92         try {
93             return oEmbedHelper::getObject($url, $parameters);
94         } catch (Exception $e) {
95             common_log(LOG_ERR, "Error during oembed lookup for $url - " . $e->getMessage());
96             return false;
97         }
98     }
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     function saveNew($data, $file_id) {
107         $file_oembed = new File_oembed;
108         $file_oembed->file_id = $file_id;
109         $file_oembed->version = $data->version;
110         $file_oembed->type = $data->type;
111         if (!empty($data->provider_name)) $file_oembed->provider = $data->provider_name;
112         if (!empty($data->provider)) $file_oembed->provider = $data->provider;
113         if (!empty($data->provide_url)) $file_oembed->provider_url = $data->provider_url;
114         if (!empty($data->width)) $file_oembed->width = intval($data->width);
115         if (!empty($data->height)) $file_oembed->height = intval($data->height);
116         if (!empty($data->html)) $file_oembed->html = $data->html;
117         if (!empty($data->title)) $file_oembed->title = $data->title;
118         if (!empty($data->author_name)) $file_oembed->author_name = $data->author_name;
119         if (!empty($data->author_url)) $file_oembed->author_url = $data->author_url;
120         if (!empty($data->url)){
121             $file_oembed->url = $data->url;
122             $given_url = File_redirection::_canonUrl($file_oembed->url);
123             if (! empty($given_url)){
124                 $file = File::staticGet('url', $given_url);
125                 if (empty($file)) {
126                     $file_redir = File_redirection::staticGet('url', $given_url);
127                     if (empty($file_redir)) {
128                         $redir_data = File_redirection::where($given_url);
129                         $file_oembed->mimetype = $redir_data['type'];
130                     } else {
131                         $file_id = $file_redir->file_id;
132                     }
133                 } else {
134                     $file_oembed->mimetype=$file->mimetype;
135                 }
136             }
137         }
138         $file_oembed->insert();
139         if (!empty($data->thumbnail_url) || ($data->type == 'photo')) {
140             $ft = File_thumbnail::staticGet('file_id', $file_id);
141             if (!empty($ft)) {
142                 common_log(LOG_WARNING, "Strangely, a File_thumbnail object exists for new file $file_id",
143                            __FILE__);
144             } else {
145                 File_thumbnail::saveNew($data, $file_id);
146             }
147         }
148     }
149 }