offlinebackupqueuehandler.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Offline backup queue handler
  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 Offline backup
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011 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. * Offline backup queue handler
  37. *
  38. * @category General
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2011 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 OfflineBackupQueueHandler extends QueueHandler
  46. {
  47. public function transport()
  48. {
  49. return 'backoff';
  50. }
  51. public function handle($object): bool
  52. {
  53. $userId = $object;
  54. $user = User::getKV($userId);
  55. common_log(LOG_INFO, "Making backup file for user ".$user->nickname);
  56. $fileName = $this->makeBackupFile($user);
  57. common_log(LOG_INFO, "Notifying user ".$user->nickname . " of their new backup file.");
  58. $this->notifyBackupFile($user, $fileName);
  59. return true;
  60. }
  61. public function makeBackupFile($user)
  62. {
  63. // XXX: this is pretty lose-y; try another way
  64. $tmpdir = sys_get_temp_dir() . '/offline-backup/' . $user->nickname . '/' . common_date_iso8601(common_sql_now());
  65. common_log(LOG_INFO, 'Writing backup data to ' . $tmpdir . ' for ' . $user->nickname);
  66. mkdir($tmpdir, 0700, true);
  67. $this->dumpNotices($user, $tmpdir);
  68. $this->dumpFaves($user, $tmpdir);
  69. $this->dumpSubscriptions($user, $tmpdir);
  70. $this->dumpSubscribers($user, $tmpdir);
  71. $this->dumpGroups($user, $tmpdir);
  72. $fileName = File::filename($user->getProfile(), "backup", "application/atom+xml");
  73. $fullPath = File::path($fileName);
  74. $this->makeActivityFeed($user, $tmpdir, $fullPath);
  75. $this->delTree($tmpdir);
  76. return $fileName;
  77. }
  78. public function notifyBackupFile($user, $fileName)
  79. {
  80. $fileUrl = File::url($fileName);
  81. $body = sprintf(
  82. _m(
  83. "The backup file you requested is ready for download.\n\n".
  84. "%s\n".
  85. "Thanks for your time,\n",
  86. "%s\n"
  87. ),
  88. $fileUrl,
  89. common_config('site', 'name')
  90. );
  91. $headers = _mail_prepare_headers('offlinebackup', $user->nickname, $user->nickname);
  92. mail_to_user($user, _('Backup file ready for download'), $body, $headers);
  93. }
  94. public function dumpNotices($user, $dir)
  95. {
  96. common_log(LOG_INFO, 'dumping notices by ' . $user->nickname . ' to directory ' . $dir);
  97. $profile = $user->getProfile();
  98. $stream = new ProfileNoticeStream($profile, $profile);
  99. $page = 1;
  100. do {
  101. $notice = $stream->getNotices(($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
  102. while ($notice->fetch()) {
  103. try {
  104. $fname = $dir . '/'. common_date_iso8601($notice->created) . '-notice-' . $notice->id . '.atom';
  105. $data = $notice->asAtomEntry(false, false, false, null);
  106. common_log(LOG_INFO, 'dumping notice ' . $notice->id . ' to file ' . $fname);
  107. file_put_contents($fname, $data);
  108. $data = null;
  109. } catch (Exception $e) {
  110. common_log(LOG_ERR, "Error backing up notice " . $notice->id . ": " . $e->getMessage());
  111. continue;
  112. }
  113. }
  114. $page++;
  115. } while ($notice->N > NOTICES_PER_PAGE);
  116. }
  117. public function dumpFaves($user, $dir)
  118. {
  119. common_log(LOG_INFO, 'dumping faves by ' . $user->nickname . ' to directory ' . $dir);
  120. $page = 1;
  121. do {
  122. $fave = Fave::byProfile($user->id, ($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
  123. while ($fave->fetch()) {
  124. try {
  125. $fname = $dir . '/'. common_date_iso8601($fave->modified) . '-fave-' . $fave->notice_id . '.atom';
  126. $act = $fave->asActivity();
  127. $data = $act->asString(false, false, false);
  128. common_log(LOG_INFO, 'dumping fave of ' . $fave->notice_id . ' to file ' . $fname);
  129. file_put_contents($fname, $data);
  130. $data = null;
  131. } catch (Exception $e) {
  132. common_log(LOG_ERR, "Error backing up fave of " . $fave->notice_id . ": " . $e->getMessage());
  133. continue;
  134. }
  135. }
  136. $page++;
  137. } while ($fave->N > NOTICES_PER_PAGE);
  138. }
  139. public function dumpSubscriptions($user, $dir)
  140. {
  141. common_log(LOG_INFO, 'dumping subscriptions by ' . $user->nickname . ' to directory ' . $dir);
  142. $page = 1;
  143. do {
  144. $sub = Subscription::bySubscriber($user->id, ($page-1)*PROFILES_PER_PAGE, PROFILES_PER_PAGE + 1);
  145. while ($sub->fetch()) {
  146. try {
  147. if ($sub->subscribed == $user->id) {
  148. continue;
  149. }
  150. $fname = $dir . '/'. common_date_iso8601($sub->created) . '-subscription-' . $sub->subscribed . '.atom';
  151. $act = $sub->asActivity();
  152. $data = $act->asString(false, false, false);
  153. common_log(LOG_INFO, 'dumping sub of ' . $sub->subscribed . ' to file ' . $fname);
  154. file_put_contents($fname, $data);
  155. $data = null;
  156. } catch (Exception $e) {
  157. common_log(LOG_ERR, "Error backing up subscription to " . $sub->subscribed . ": " . $e->getMessage());
  158. continue;
  159. }
  160. }
  161. $page++;
  162. } while ($sub->N > PROFILES_PER_PAGE);
  163. }
  164. public function dumpSubscribers($user, $dir)
  165. {
  166. common_log(LOG_INFO, 'dumping subscribers to ' . $user->nickname . ' to directory ' . $dir);
  167. $page = 1;
  168. do {
  169. $sub = Subscription::bySubscribed($user->id, ($page-1)*PROFILES_PER_PAGE, PROFILES_PER_PAGE + 1);
  170. while ($sub->fetch()) {
  171. try {
  172. if ($sub->subscriber == $user->id) {
  173. continue;
  174. }
  175. $fname = $dir . '/'. common_date_iso8601($sub->created) . '-subscriber-' . $sub->subscriber . '.atom';
  176. $act = $sub->asActivity();
  177. $data = $act->asString(false, true, false);
  178. common_log(LOG_INFO, 'dumping sub by ' . $sub->subscriber . ' to file ' . $fname);
  179. file_put_contents($fname, $data);
  180. $data = null;
  181. } catch (Exception $e) {
  182. common_log(LOG_ERR, "Error backing up subscription from " . $sub->subscriber . ": " . $e->getMessage());
  183. continue;
  184. }
  185. }
  186. $page++;
  187. } while ($sub->N > PROFILES_PER_PAGE);
  188. }
  189. public function dumpGroups($user, $dir)
  190. {
  191. common_log(LOG_INFO, 'dumping memberships of ' . $user->nickname . ' to directory ' . $dir);
  192. $page = 1;
  193. do {
  194. $mem = Group_member::byMember($user->id, ($page-1)*GROUPS_PER_PAGE, GROUPS_PER_PAGE + 1);
  195. while ($mem->fetch()) {
  196. try {
  197. $fname = $dir . '/'. common_date_iso8601($mem->created) . '-membership-' . $mem->group_id . '.atom';
  198. $act = $mem->asActivity();
  199. $data = $act->asString(false, false, false);
  200. common_log(LOG_INFO, 'dumping membership in ' . $mem->group_id . ' to file ' . $fname);
  201. file_put_contents($fname, $data);
  202. $data = null;
  203. } catch (Exception $e) {
  204. common_log(LOG_ERR, "Error backing up membership in " . $mem->group_id . ": " . $e->getMessage());
  205. continue;
  206. }
  207. }
  208. $page++;
  209. } while ($mem->N > GROUPS_PER_PAGE);
  210. }
  211. public function makeActivityFeed($user, $tmpdir, $fullPath)
  212. {
  213. $handle = fopen($fullPath, 'c');
  214. $this->writeFeedHeader($user, $handle);
  215. $objects = scandir($tmpdir);
  216. rsort($objects);
  217. foreach ($objects as $object) {
  218. $objFull = $tmpdir . '/' . $object;
  219. if (!is_dir($objFull)) {
  220. $entry = file_get_contents($objFull);
  221. fwrite($handle, $entry);
  222. $entry = null;
  223. }
  224. }
  225. $this->writeFeedFooter($user, $handle);
  226. fclose($handle);
  227. }
  228. public function writeFeedHeader($user, $handle)
  229. {
  230. fwrite($handle, '<?xml version="1.0" encoding="UTF-8"?>');
  231. fwrite($handle, "\n");
  232. fwrite($handle, '<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:georss="http://www.georss.org/georss" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:media="http://purl.org/syndication/atommedia" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:statusnet="http://status.net/schema/api/1/">');
  233. fwrite($handle, "\n");
  234. $profile = $user->getProfile();
  235. $author = $profile->asActivityObject();
  236. $xs = new XMLStringer();
  237. $author->outputTo($xs, 'author');
  238. fwrite($handle, $xs->getString());
  239. fwrite($handle, "\n");
  240. }
  241. public function writeFeedFooter($user, $handle)
  242. {
  243. fwrite($handle, '</feed>');
  244. }
  245. public function delTree($dir)
  246. {
  247. if (is_dir($dir)) {
  248. $objects = scandir($dir);
  249. foreach ($objects as $object) {
  250. if ($object != "." && $object != "..") {
  251. if (filetype($dir."/".$object) == "dir") {
  252. $this->delTree($dir."/".$object);
  253. } else {
  254. unlink($dir."/".$object);
  255. }
  256. }
  257. }
  258. reset($objects);
  259. rmdir($dir);
  260. }
  261. }
  262. }