]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Bookmark/actions/importdelicious.php
[ROUTES] Allow accept-header specification during router creation
[quix0rs-gnu-social.git] / plugins / Bookmark / actions / 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 class ImportdeliciousAction extends Action
48 {
49     protected $success = false;
50     private $inprogress = false;
51
52     /**
53      * Return the title of the page
54      *
55      * @return string page title
56      */
57     function title()
58     {
59         // TRANS: Title for page to import del.icio.us bookmark backups on.
60         return _m("Import del.icio.us bookmarks");
61     }
62
63     /**
64      * For initializing members of the class.
65      *
66      * @param array $args misc. arguments
67      *
68      * @return boolean true
69      * @throws ClientException
70      */
71     function prepare(array $args = [])
72     {
73         parent::prepare($args);
74
75         $cur = common_current_user();
76
77         if (empty($cur)) {
78             // TRANS: Client exception thrown when trying to import bookmarks without being logged in.
79             throw new ClientException(_m('Only logged-in users can ' .
80                 'import del.icio.us backups.'),
81                 403);
82         }
83
84         if (!$cur->hasRight(BookmarkPlugin::IMPORTDELICIOUS)) {
85             // TRANS: Client exception thrown when trying to import bookmarks without having the rights to do so.
86             throw new ClientException(_m('You may not restore your account.'), 403);
87         }
88
89         return true;
90     }
91
92     /**
93      * Handler method
94      *
95      * @return void
96      * @throws ClientException
97      */
98     function handle()
99     {
100         parent::handle();
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      * @throws ClientException
117      */
118     function importDelicious()
119     {
120         $this->checkSessionToken();
121
122         if (!isset($_FILES[ImportDeliciousForm::FILEINPUT]['error'])) {
123             // TRANS: Client exception thrown when trying to import bookmarks and upload fails.
124             throw new ClientException(_m('No uploaded file.'));
125         }
126
127         switch ($_FILES[ImportDeliciousForm::FILEINPUT]['error']) {
128             case UPLOAD_ERR_OK: // success, jump out
129                 break;
130             case UPLOAD_ERR_INI_SIZE:
131                 // TRANS: Client exception thrown when an uploaded file is too large.
132                 throw new ClientException(_m('The uploaded file exceeds the ' .
133                     'upload_max_filesize directive in php.ini.'));
134             case UPLOAD_ERR_FORM_SIZE:
135                 throw new ClientException(
136                 // TRANS: Client exception thrown when an uploaded file is too large.
137                     _m('The uploaded file exceeds the MAX_FILE_SIZE directive' .
138                         ' that was specified in the HTML form.'));
139             case UPLOAD_ERR_PARTIAL:
140                 @unlink($_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name']);
141                 // TRANS: Client exception thrown when a file was only partially uploaded.
142                 throw new ClientException(_m('The uploaded file was only' .
143                     ' partially uploaded.'));
144             case UPLOAD_ERR_NO_FILE:
145                 // No file; probably just a non-AJAX submission.
146                 // TRANS: Client exception thrown when a file upload has failed.
147                 throw new ClientException(_m('No uploaded file.'));
148             case UPLOAD_ERR_NO_TMP_DIR:
149                 // TRANS: Client exception thrown when a temporary folder is not present.
150                 throw new ClientException(_m('Missing a temporary folder.'));
151             case UPLOAD_ERR_CANT_WRITE:
152                 // TRANS: Client exception thrown when writing to disk is not possible.
153                 throw new ClientException(_m('Failed to write file to disk.'));
154             case UPLOAD_ERR_EXTENSION:
155                 // TRANS: Client exception thrown when a file upload has been stopped.
156                 throw new ClientException(_m('File upload stopped by extension.'));
157             default:
158                 common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " .
159                     $_FILES[ImportDeliciousForm::FILEINPUT]['error']);
160                 // TRANS: Client exception thrown when a file upload operation has failed.
161                 throw new ClientException(_m('System error uploading file.'));
162         }
163
164         $filename = $_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name'];
165
166         try {
167             if (!file_exists($filename)) {
168                 // TRANS: Server exception thrown when a file upload cannot be found.
169                 // TRANS: %s is the file that could not be found.
170                 throw new ServerException(sprintf(_m('No such file "%s".'), $filename));
171             }
172
173             if (!is_file($filename)) {
174                 // TRANS: Server exception thrown when a file upload is incorrect.
175                 // TRANS: %s is the irregular file.
176                 throw new ServerException(sprintf(_m('Not a regular file: "%s".'), $filename));
177             }
178
179             if (!is_readable($filename)) {
180                 // TRANS: Server exception thrown when a file upload is not readable.
181                 // TRANS: %s is the file that could not be read.
182                 throw new ServerException(sprintf(_m('File "%s" not readable.'), $filename));
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([common_current_user(), $html], 'dlcsback');
193
194             if ($qm instanceof UnQueueManager) {
195                 // No active queuing means we've actually just completed the job!
196                 $this->success = true;
197             } else {
198                 // We've fed data into background queues, and it's probably still running.
199                 $this->inprogress = true;
200             }
201
202             $this->showPage();
203
204         } catch (Exception $e) {
205             // Delete the file and re-throw
206             @unlink($_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name']);
207             throw $e;
208         }
209     }
210
211     /**
212      * Show the content of the page
213      *
214      * @return void
215      */
216     function showContent()
217     {
218         if ($this->success) {
219             $this->element('p', null,
220                 // TRANS: Success message after importing bookmarks.
221                 _m('Bookmarks have been imported. Your bookmarks should now appear in search and your profile page.'));
222         } else if ($this->inprogress) {
223             $this->element('p', null,
224                 // TRANS: Busy message for importing bookmarks.
225                 _m('Bookmarks are being imported. Please wait a few minutes for results.'));
226         } else {
227             $form = new ImportDeliciousForm($this);
228             $form->show();
229         }
230     }
231
232     /**
233      * Return true if read only.
234      *
235      * MAY override
236      *
237      * @param array $args other arguments
238      *
239      * @return boolean is read only action?
240      */
241     function isReadOnly($args)
242     {
243         return !$this->isPost();
244     }
245 }
246
247 /**
248  * A form for backing up the account.
249  *
250  * @category  Account
251  * @package   StatusNet
252  * @author    Evan Prodromou <evan@status.net>
253  * @copyright 2010 StatusNet, Inc.
254  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
255  * @link      http://status.net/
256  */
257 class ImportDeliciousForm extends Form
258 {
259     const FILEINPUT = 'deliciousbackupfile';
260
261     /**
262      * Constructor
263      *
264      * Set the encoding type, since this is a file upload.
265      *
266      * @param HTMLOutputter $out output channel
267      *
268      */
269     function __construct($out = null)
270     {
271         parent::__construct($out);
272         $this->enctype = 'multipart/form-data';
273     }
274
275     /**
276      * Class of the form.
277      *
278      * @return string the form's class
279      */
280     function formClass()
281     {
282         return 'form_import_delicious';
283     }
284
285     /**
286      * URL the form posts to
287      *
288      * @return string the form's action URL
289      */
290     function action()
291     {
292         return common_local_url('importdelicious');
293     }
294
295     /**
296      * Output form data
297      *
298      * Really, just instructions for doing a backup.
299      *
300      * @return void
301      */
302     function formData()
303     {
304         $this->out->elementStart('p', 'instructions');
305
306         // TRANS: Form instructions for importing bookmarks.
307         $this->out->raw(_m('You can upload a backed-up ' .
308             'delicious.com bookmarks file.'));
309
310         $this->out->elementEnd('p');
311
312         $this->out->elementStart('ul', 'form_data');
313
314         $this->out->elementStart('li', array('id' => 'settings_attach'));
315         $this->out->element('input', array('name' => self::FILEINPUT,
316             'type' => 'file',
317             'id' => self::FILEINPUT));
318         $this->out->elementEnd('li');
319
320         $this->out->elementEnd('ul');
321     }
322
323     /**
324      * Buttons for the form
325      *
326      * In this case, a single submit button
327      *
328      * @return void
329      */
330
331     function formActions()
332     {
333         $this->out->submit('submit',
334             // TRANS: Button text on form to import bookmarks.
335             _m('BUTTON', 'Upload'),
336             'submit',
337             null,
338             // TRANS: Button title on form to import bookmarks.
339             _m('Upload the file.'));
340     }
341 }