3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Import del.icio.us bookmarks backups
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * UI for importing del.icio.us bookmark backups
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/
48 class ImportdeliciousAction extends Action
50 protected $success = false;
54 return _("Import del.icio.us bookmarks");
58 * For initializing members of the class.
60 * @param array $argarray misc. arguments
62 * @return boolean true
65 function prepare($argarray)
67 parent::prepare($argarray);
69 $cur = common_current_user();
72 throw new ClientException(_('Only logged-in users can import del.icio.us backups.'), 403);
75 if (!$cur->hasRight(BookmarkPlugin::IMPORTDELICIOUS)) {
76 throw new ClientException(_('You may not restore your account.'), 403);
85 * @param array $argarray is ignored since it's now passed in in prepare()
90 function handle($argarray=null)
92 parent::handle($argarray);
94 if ($this->isPost()) {
95 $this->importDelicious();
103 * Queue a file for importation
105 * Uses the DeliciousBackupImporter class; may take a long time!
110 function importDelicious()
112 $this->checkSessionToken();
114 if (!isset($_FILES[ImportDeliciousForm::FILEINPUT]['error'])) {
115 throw new ClientException(_('No uploaded file.'));
118 switch ($_FILES[ImportDeliciousForm::FILEINPUT]['error']) {
119 case UPLOAD_ERR_OK: // success, jump out
121 case UPLOAD_ERR_INI_SIZE:
122 // TRANS: Client exception thrown when an uploaded file is larger than set in php.ini.
123 throw new ClientException(_('The uploaded file exceeds the ' .
124 'upload_max_filesize directive in php.ini.'));
126 case UPLOAD_ERR_FORM_SIZE:
127 throw new ClientException(
128 // TRANS: Client exception.
129 _('The uploaded file exceeds the MAX_FILE_SIZE directive' .
130 ' that was specified in the HTML form.'));
132 case UPLOAD_ERR_PARTIAL:
133 @unlink($_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name']);
134 // TRANS: Client exception.
135 throw new ClientException(_('The uploaded file was only' .
136 ' partially uploaded.'));
138 case UPLOAD_ERR_NO_FILE:
139 // No file; probably just a non-AJAX submission.
140 throw new ClientException(_('No uploaded file.'));
142 case UPLOAD_ERR_NO_TMP_DIR:
143 // TRANS: Client exception thrown when a temporary folder is not present to store a file upload.
144 throw new ClientException(_('Missing a temporary folder.'));
146 case UPLOAD_ERR_CANT_WRITE:
147 // TRANS: Client exception thrown when writing to disk is not possible during a file upload operation.
148 throw new ClientException(_('Failed to write file to disk.'));
150 case UPLOAD_ERR_EXTENSION:
151 // TRANS: Client exception thrown when a file upload operation has been stopped by an extension.
152 throw new ClientException(_('File upload stopped by extension.'));
155 common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " .
156 $_FILES[ImportDeliciousForm::FILEINPUT]['error']);
157 // TRANS: Client exception thrown when a file upload operation has failed with an unknown reason.
158 throw new ClientException(_('System error uploading file.'));
162 $filename = $_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name'];
165 if (!file_exists($filename)) {
166 throw new ServerException("No such file '$filename'.");
169 if (!is_file($filename)) {
170 throw new ServerException("Not a regular file: '$filename'.");
173 if (!is_readable($filename)) {
174 throw new ServerException("File '$filename' not readable.");
177 common_debug(sprintf(_("Getting backup from file '%s'."), $filename));
179 $html = file_get_contents($filename);
181 // Enqueue for processing.
183 $qm = QueueManager::get();
184 $qm->enqueue(array(common_current_user(), $html), 'dlcsback');
186 $this->success = true;
190 } catch (Exception $e) {
191 // Delete the file and re-throw
192 @unlink($_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name']);
197 function showContent()
199 if ($this->success) {
200 $this->element('p', null,
201 _('Feed will be restored. Please wait a few minutes for results.'));
203 $form = new ImportDeliciousForm($this);
209 * Return true if read only.
213 * @param array $args other arguments
215 * @return boolean is read only action?
218 function isReadOnly($args)
220 return !$this->isPost();
225 * A form for backing up the account.
229 * @author Evan Prodromou <evan@status.net>
230 * @copyright 2010 StatusNet, Inc.
231 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
232 * @link http://status.net/
235 class ImportDeliciousForm extends Form
237 const FILEINPUT = 'deliciousbackupfile';
239 function __construct($out=null) {
240 parent::__construct($out);
241 $this->enctype = 'multipart/form-data';
247 * @return string the form's class
252 return 'form_import_delicious';
256 * URL the form posts to
258 * @return string the form's action URL
263 return common_local_url('importdelicious');
269 * Really, just instructions for doing a backup.
276 $this->out->elementStart('p', 'instructions');
278 $this->out->raw(_('You can upload a backed-up delicious.com bookmarks file.'));
280 $this->out->elementEnd('p');
282 $this->out->elementStart('ul', 'form_data');
284 $this->out->elementStart('li', array ('id' => 'settings_attach'));
285 $this->out->element('input', array('name' => self::FILEINPUT,
287 'id' => self::FILEINPUT));
288 $this->out->elementEnd('li');
290 $this->out->elementEnd('ul');
294 * Buttons for the form
296 * In this case, a single submit button
301 function formActions()
303 $this->out->submit('submit',
304 _m('BUTTON', 'Upload'),
307 _('Upload the file'));