importdelicious.php 11 KB

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