offlinebackupqueuehandler.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. function transport()
  48. {
  49. return 'backoff';
  50. }
  51. function handle($object)
  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. 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. function notifyBackupFile($user, $fileName)
  79. {
  80. $fileUrl = File::url($fileName);
  81. $body = sprintf(_m("The backup file you requested is ready for download.\n\n".
  82. "%s\n".
  83. "Thanks for your time,\n",
  84. "%s\n"),
  85. $fileUrl,
  86. common_config('site', 'name'));
  87. $headers = _mail_prepare_headers('offlinebackup', $user->nickname, $user->nickname);
  88. mail_to_user($user, _('Backup file ready for download'), $body, $headers);
  89. }
  90. function dumpNotices($user, $dir)
  91. {
  92. common_log(LOG_INFO, 'dumping notices by ' . $user->nickname . ' to directory ' . $dir);
  93. $profile = $user->getProfile();
  94. $stream = new ProfileNoticeStream($profile, $profile);
  95. $page = 1;
  96. do {
  97. $notice = $stream->getNotices(($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
  98. while ($notice->fetch()) {
  99. try {
  100. $fname = $dir . '/'. common_date_iso8601($notice->created) . '-notice-' . $notice->id . '.atom';
  101. $data = $notice->asAtomEntry(false, false, false, null);
  102. common_log(LOG_INFO, 'dumping notice ' . $notice->id . ' to file ' . $fname);
  103. file_put_contents($fname, $data);
  104. $data = null;
  105. } catch (Exception $e) {
  106. common_log(LOG_ERR, "Error backing up notice " . $notice->id . ": " . $e->getMessage());
  107. continue;
  108. }
  109. }
  110. $page++;
  111. } while ($notice->N > NOTICES_PER_PAGE);
  112. }
  113. function dumpFaves($user, $dir)
  114. {
  115. common_log(LOG_INFO, 'dumping faves by ' . $user->nickname . ' to directory ' . $dir);
  116. $page = 1;
  117. do {
  118. $fave = Fave::byProfile($user->id, ($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
  119. while ($fave->fetch()) {
  120. try {
  121. $fname = $dir . '/'. common_date_iso8601($fave->modified) . '-fave-' . $fave->notice_id . '.atom';
  122. $act = $fave->asActivity();
  123. $data = $act->asString(false, false, false);
  124. common_log(LOG_INFO, 'dumping fave of ' . $fave->notice_id . ' to file ' . $fname);
  125. file_put_contents($fname, $data);
  126. $data = null;
  127. } catch (Exception $e) {
  128. common_log(LOG_ERR, "Error backing up fave of " . $fave->notice_id . ": " . $e->getMessage());
  129. continue;
  130. }
  131. }
  132. $page++;
  133. } while ($fave->N > NOTICES_PER_PAGE);
  134. }
  135. function dumpSubscriptions($user, $dir)
  136. {
  137. common_log(LOG_INFO, 'dumping subscriptions by ' . $user->nickname . ' to directory ' . $dir);
  138. $page = 1;
  139. do {
  140. $sub = Subscription::bySubscriber($user->id, ($page-1)*PROFILES_PER_PAGE, PROFILES_PER_PAGE + 1);
  141. while ($sub->fetch()) {
  142. try {
  143. if ($sub->subscribed == $user->id) {
  144. continue;
  145. }
  146. $fname = $dir . '/'. common_date_iso8601($sub->created) . '-subscription-' . $sub->subscribed . '.atom';
  147. $act = $sub->asActivity();
  148. $data = $act->asString(false, false, false);
  149. common_log(LOG_INFO, 'dumping sub of ' . $sub->subscribed . ' to file ' . $fname);
  150. file_put_contents($fname, $data);
  151. $data = null;
  152. } catch (Exception $e) {
  153. common_log(LOG_ERR, "Error backing up subscription to " . $sub->subscribed . ": " . $e->getMessage());
  154. continue;
  155. }
  156. }
  157. $page++;
  158. } while ($sub->N > PROFILES_PER_PAGE);
  159. }
  160. function dumpSubscribers($user, $dir)
  161. {
  162. common_log(LOG_INFO, 'dumping subscribers to ' . $user->nickname . ' to directory ' . $dir);
  163. $page = 1;
  164. do {
  165. $sub = Subscription::bySubscribed($user->id, ($page-1)*PROFILES_PER_PAGE, PROFILES_PER_PAGE + 1);
  166. while ($sub->fetch()) {
  167. try {
  168. if ($sub->subscriber == $user->id) {
  169. continue;
  170. }
  171. $fname = $dir . '/'. common_date_iso8601($sub->created) . '-subscriber-' . $sub->subscriber . '.atom';
  172. $act = $sub->asActivity();
  173. $data = $act->asString(false, true, false);
  174. common_log(LOG_INFO, 'dumping sub by ' . $sub->subscriber . ' to file ' . $fname);
  175. file_put_contents($fname, $data);
  176. $data = null;
  177. } catch (Exception $e) {
  178. common_log(LOG_ERR, "Error backing up subscription from " . $sub->subscriber . ": " . $e->getMessage());
  179. continue;
  180. }
  181. }
  182. $page++;
  183. } while ($sub->N > PROFILES_PER_PAGE);
  184. }
  185. function dumpGroups($user, $dir)
  186. {
  187. common_log(LOG_INFO, 'dumping memberships of ' . $user->nickname . ' to directory ' . $dir);
  188. $page = 1;
  189. do {
  190. $mem = Group_member::byMember($user->id, ($page-1)*GROUPS_PER_PAGE, GROUPS_PER_PAGE + 1);
  191. while ($mem->fetch()) {
  192. try {
  193. $fname = $dir . '/'. common_date_iso8601($mem->created) . '-membership-' . $mem->group_id . '.atom';
  194. $act = $mem->asActivity();
  195. $data = $act->asString(false, false, false);
  196. common_log(LOG_INFO, 'dumping membership in ' . $mem->group_id . ' to file ' . $fname);
  197. file_put_contents($fname, $data);
  198. $data = null;
  199. } catch (Exception $e) {
  200. common_log(LOG_ERR, "Error backing up membership in " . $mem->group_id . ": " . $e->getMessage());
  201. continue;
  202. }
  203. }
  204. $page++;
  205. } while ($mem->N > GROUPS_PER_PAGE);
  206. }
  207. function makeActivityFeed($user, $tmpdir, $fullPath)
  208. {
  209. $handle = fopen($fullPath, 'c');
  210. $this->writeFeedHeader($user, $handle);
  211. $objects = scandir($tmpdir);
  212. rsort($objects);
  213. foreach ($objects as $object) {
  214. $objFull = $tmpdir . '/' . $object;
  215. if (!is_dir($objFull)) {
  216. $entry = file_get_contents($objFull);
  217. fwrite($handle, $entry);
  218. $entry = null;
  219. }
  220. }
  221. $this->writeFeedFooter($user, $handle);
  222. fclose($handle);
  223. }
  224. function writeFeedHeader($user, $handle)
  225. {
  226. fwrite($handle, '<?xml version="1.0" encoding="UTF-8"?>');
  227. fwrite($handle, "\n");
  228. 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/">');
  229. fwrite($handle, "\n");
  230. $profile = $user->getProfile();
  231. $author = $profile->asActivityObject();
  232. $xs = new XMLStringer();
  233. $author->outputTo($xs, 'author');
  234. fwrite($handle, $xs->getString());
  235. fwrite($handle, "\n");
  236. }
  237. function writeFeedFooter($user, $handle)
  238. {
  239. fwrite($handle, '</feed>');
  240. }
  241. function delTree($dir)
  242. {
  243. if (is_dir($dir)) {
  244. $objects = scandir($dir);
  245. foreach ($objects as $object) {
  246. if ($object != "." && $object != "..") {
  247. if (filetype($dir."/".$object) == "dir") {
  248. $this->delTree($dir."/".$object);
  249. } else {
  250. unlink($dir."/".$object);
  251. }
  252. }
  253. }
  254. reset($objects);
  255. rmdir($dir);
  256. }
  257. }
  258. }