]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/File.php
2ddc5deb8b69446230e953a5ed20b947f7e01ca7
[quix0rs-gnu-social.git] / classes / File.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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('LACONICA')) { exit(1); }
21
22 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
23 require_once INSTALLDIR.'/classes/File_redirection.php';
24 require_once INSTALLDIR.'/classes/File_oembed.php';
25 require_once INSTALLDIR.'/classes/File_thumbnail.php';
26 require_once INSTALLDIR.'/classes/File_to_post.php';
27 //require_once INSTALLDIR.'/classes/File_redirection.php';
28
29 /**
30  * Table Definition for file
31  */
32
33 class File extends Memcached_DataObject 
34 {
35     ###START_AUTOCODE
36     /* the code below is auto generated do not remove the above tag */
37
38     public $__table = 'file';                            // table name
39     public $id;                              // int(11)  not_null primary_key group_by
40     public $url;                             // varchar(255)  unique_key
41     public $mimetype;                        // varchar(50)  
42     public $size;                            // int(11)  group_by
43     public $title;                           // varchar(255)  
44     public $date;                            // int(11)  group_by
45     public $protected;                       // int(1)  group_by
46
47     /* Static get */
48     function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('File',$k,$v); }
49
50     /* the code above is auto generated do not remove the tag below */
51     ###END_AUTOCODE
52
53     function isProtected($url) {
54         return 'http://www.facebook.com/login.php' === $url;
55     }
56
57     function saveNew($redir_data, $given_url) {
58         $x = new File;
59         $x->url = $given_url;
60         if (!empty($redir_data['protected'])) $x->protected = $redir_data['protected'];
61         if (!empty($redir_data['title'])) $x->title = $redir_data['title'];
62         if (!empty($redir_data['type'])) $x->mimetype = $redir_data['type'];
63         if (!empty($redir_data['size'])) $x->size = intval($redir_data['size']);
64         if (isset($redir_data['time']) && $redir_data['time'] > 0) $x->date = intval($redir_data['time']);
65         $file_id = $x->insert();
66
67         if (isset($redir_data['type'])
68             && ('text/html' === substr($redir_data['type'], 0, 9))
69             && ($oembed_data = File_oembed::_getOembed($given_url))
70             && isset($oembed_data['json'])) {
71
72             File_oembed::saveNew($oembed_data['json'], $file_id);
73         }
74         return $x;
75     }
76
77     function processNew($given_url, $notice_id) {
78         if (empty($given_url)) return -1;   // error, no url to process
79         $given_url = File_redirection::_canonUrl($given_url);
80         if (empty($given_url)) return -1;   // error, no url to process
81         $file = File::staticGet('url', $given_url);
82         if (empty($file->id)) {
83             $file_redir = File_redirection::staticGet('url', $given_url);
84             if (empty($file_redir->id)) {
85                 $redir_data = File_redirection::where($given_url);
86                 $redir_url = $redir_data['url'];
87                 if ($redir_url === $given_url) {
88                     $x = File::saveNew($redir_data, $given_url);
89                     $file_id = $x->id;
90
91                 } else {
92                     $x = File::processNew($redir_url, $notice_id);
93                     $file_id = $x->id;
94                     File_redirection::saveNew($redir_data, $file_id, $given_url);
95                 }
96             } else {
97                 $file_id = $file_redir->file_id;
98             }
99         } else {
100             $file_id = $file->id;
101             $x = $file;
102         }
103
104         if (empty($x)) {
105             $x = File::staticGet($file_id);
106             if (empty($x)) die('Impossible!');
107         }
108        
109         File_to_post::processNew($file_id, $notice_id);
110         return $x;
111     }
112 }