]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Bookmark/importdelicious.php
Merge remote branch 'gitorious/0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / Bookmark / importdelicious.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Import del.icio.us bookmarks backups
7  * 
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Bookmark
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * UI for importing del.icio.us bookmark backups
39  *
40  * @category  Bookmark
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2010 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47
48 class ImportdeliciousAction extends Action
49 {
50     protected $success = false;
51
52     /**
53      * Return the title of the page
54      *
55      * @return string page title
56      */
57
58     function title()
59     {
60         return _("Import del.icio.us bookmarks");
61     }
62
63     /**
64      * For initializing members of the class.
65      *
66      * @param array $argarray misc. arguments
67      *
68      * @return boolean true
69      */
70
71     function prepare($argarray)
72     {
73         parent::prepare($argarray);
74
75         $cur = common_current_user();
76
77         if (empty($cur)) {
78             throw new ClientException(_('Only logged-in users can '.
79                                         'import del.icio.us backups.'),
80                                       403);
81         }
82
83         if (!$cur->hasRight(BookmarkPlugin::IMPORTDELICIOUS)) {
84             throw new ClientException(_('You may not restore your account.'), 403);
85         }
86
87         return true;
88     }
89
90     /**
91      * Handler method
92      *
93      * @param array $argarray is ignored since it's now passed in in prepare()
94      *
95      * @return void
96      */
97
98     function handle($argarray=null)
99     {
100         parent::handle($argarray);
101
102         if ($this->isPost()) {
103             $this->importDelicious();
104         } else {
105             $this->showPage();
106         }
107         return;
108     }
109
110     /**
111      * Queue a file for importation
112      * 
113      * Uses the DeliciousBackupImporter class; may take a long time!
114      *
115      * @return void
116      */
117
118     function importDelicious()
119     {
120         $this->checkSessionToken();
121
122         if (!isset($_FILES[ImportDeliciousForm::FILEINPUT]['error'])) {
123             throw new ClientException(_('No uploaded file.'));
124         }
125
126         switch ($_FILES[ImportDeliciousForm::FILEINPUT]['error']) {
127         case UPLOAD_ERR_OK: // success, jump out
128             break;
129         case UPLOAD_ERR_INI_SIZE:
130             // TRANS: Client exception thrown when an uploaded file is too large.
131             throw new ClientException(_('The uploaded file exceeds the ' .
132                 'upload_max_filesize directive in php.ini.'));
133             return;
134         case UPLOAD_ERR_FORM_SIZE:
135             throw new ClientException(
136                 // TRANS: Client exception.
137                 _('The uploaded file exceeds the MAX_FILE_SIZE directive' .
138                 ' that was specified in the HTML form.'));
139             return;
140         case UPLOAD_ERR_PARTIAL:
141             @unlink($_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name']);
142             // TRANS: Client exception.
143             throw new ClientException(_('The uploaded file was only' .
144                 ' partially uploaded.'));
145             return;
146         case UPLOAD_ERR_NO_FILE:
147             // No file; probably just a non-AJAX submission.
148             throw new ClientException(_('No uploaded file.'));
149             return;
150         case UPLOAD_ERR_NO_TMP_DIR:
151             // TRANS: Client exception thrown when a temporary folder is not present
152             throw new ClientException(_('Missing a temporary folder.'));
153             return;
154         case UPLOAD_ERR_CANT_WRITE:
155             // TRANS: Client exception thrown when writing to disk is not possible
156             throw new ClientException(_('Failed to write file to disk.'));
157             return;
158         case UPLOAD_ERR_EXTENSION:
159             // TRANS: Client exception thrown when a file upload has been stopped
160             throw new ClientException(_('File upload stopped by extension.'));
161             return;
162         default:
163             common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " .
164                 $_FILES[ImportDeliciousForm::FILEINPUT]['error']);
165             // TRANS: Client exception thrown when a file upload operation has failed
166             throw new ClientException(_('System error uploading file.'));
167             return;
168         }
169
170         $filename = $_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name'];
171
172         try {
173             if (!file_exists($filename)) {
174                 throw new ServerException("No such file '$filename'.");
175             }
176         
177             if (!is_file($filename)) {
178                 throw new ServerException("Not a regular file: '$filename'.");
179             }
180         
181             if (!is_readable($filename)) {
182                 throw new ServerException("File '$filename' not readable.");
183             }
184         
185             common_debug(sprintf(_("Getting backup from file '%s'."), $filename));
186
187             $html = file_get_contents($filename);
188
189             // Enqueue for processing.
190
191             $qm = QueueManager::get();
192             $qm->enqueue(array(common_current_user(), $html), 'dlcsback');
193
194             $this->success = true;
195
196             $this->showPage();
197
198         } catch (Exception $e) {
199             // Delete the file and re-throw
200             @unlink($_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name']);
201             throw $e;
202         }
203     }
204
205     /**
206      * Show the content of the page
207      *
208      * @return void
209      */
210
211     function showContent()
212     {
213         if ($this->success) {
214             $this->element('p', null,
215                            _('Feed will be restored. '.
216                              'Please wait a few minutes for results.'));
217         } else {
218             $form = new ImportDeliciousForm($this);
219             $form->show();
220         }
221     }
222
223     /**
224      * Return true if read only.
225      *
226      * MAY override
227      *
228      * @param array $args other arguments
229      *
230      * @return boolean is read only action?
231      */
232
233     function isReadOnly($args)
234     {
235         return !$this->isPost();
236     }
237 }
238
239 /**
240  * A form for backing up the account.
241  *
242  * @category  Account
243  * @package   StatusNet
244  * @author    Evan Prodromou <evan@status.net>
245  * @copyright 2010 StatusNet, Inc.
246  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
247  * @link      http://status.net/
248  */
249
250 class ImportDeliciousForm extends Form
251 {
252     const FILEINPUT = 'deliciousbackupfile';
253
254     /**
255      * Constructor
256      * 
257      * Set the encoding type, since this is a file upload.
258      *
259      * @param HTMLOutputter $out output channel
260      *
261      * @return ImportDeliciousForm this
262      */
263
264     function __construct($out=null)
265     {
266         parent::__construct($out);
267         $this->enctype = 'multipart/form-data';
268     }
269
270     /**
271      * Class of the form.
272      *
273      * @return string the form's class
274      */
275
276     function formClass()
277     {
278         return 'form_import_delicious';
279     }
280
281     /**
282      * URL the form posts to
283      *
284      * @return string the form's action URL
285      */
286
287     function action()
288     {
289         return common_local_url('importdelicious');
290     }
291
292     /**
293      * Output form data
294      * 
295      * Really, just instructions for doing a backup.
296      *
297      * @return void
298      */
299
300     function formData()
301     {
302         $this->out->elementStart('p', 'instructions');
303
304         $this->out->raw(_('You can upload a backed-up '.
305                           'delicious.com bookmarks file.'));
306         
307         $this->out->elementEnd('p');
308
309         $this->out->elementStart('ul', 'form_data');
310
311         $this->out->elementStart('li', array ('id' => 'settings_attach'));
312         $this->out->element('input', array('name' => self::FILEINPUT,
313                                            'type' => 'file',
314                                            'id' => self::FILEINPUT));
315         $this->out->elementEnd('li');
316
317         $this->out->elementEnd('ul');
318     }
319
320     /**
321      * Buttons for the form
322      * 
323      * In this case, a single submit button
324      *
325      * @return void
326      */
327
328     function formActions()
329     {
330         $this->out->submit('submit',
331                            _m('BUTTON', 'Upload'),
332                            'submit',
333                            null,
334                            _('Upload the file'));
335     }
336 }