importdelicious.php 11 KB

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