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