gnu-social/plugins/OfflineBackup/lib/offlinebackupqueuehandler.php

324 lines
11 KiB
PHP
Raw Permalink Normal View History

2011-10-06 01:05:05 +09:00
<?php
/**
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2011, StatusNet, Inc.
*
* Offline backup queue handler
2019-08-11 06:35:45 +09:00
*
2011-10-06 01:05:05 +09:00
* PHP version 5
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Offline backup
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @copyright 2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
// This check helps protect against security problems;
// your code file can't be executed directly from the web.
exit(1);
}
/**
* Offline backup queue handler
*
* @category General
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @copyright 2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
*/
class OfflineBackupQueueHandler extends QueueHandler
{
2019-08-11 06:35:45 +09:00
public function transport()
2011-10-06 01:05:05 +09:00
{
return 'backoff';
}
2019-08-11 06:35:45 +09:00
public function handle($object): bool
2011-10-06 01:05:05 +09:00
{
$userId = $object;
$user = User::getKV($userId);
2011-10-07 06:31:50 +09:00
2011-10-08 02:56:47 +09:00
common_log(LOG_INFO, "Making backup file for user ".$user->nickname);
2011-10-07 06:31:50 +09:00
$fileName = $this->makeBackupFile($user);
2011-10-08 02:56:47 +09:00
common_log(LOG_INFO, "Notifying user ".$user->nickname . " of their new backup file.");
2011-10-07 22:36:52 +09:00
$this->notifyBackupFile($user, $fileName);
2011-10-07 06:31:50 +09:00
2011-10-06 01:05:05 +09:00
return true;
}
2011-10-07 06:31:50 +09:00
2019-08-11 06:35:45 +09:00
public function makeBackupFile($user)
2011-10-07 06:31:50 +09:00
{
// XXX: this is pretty lose-y; try another way
2011-10-08 02:56:47 +09:00
$tmpdir = sys_get_temp_dir() . '/offline-backup/' . $user->nickname . '/' . common_date_iso8601(common_sql_now());
2011-10-07 06:31:50 +09:00
2011-10-08 02:56:47 +09:00
common_log(LOG_INFO, 'Writing backup data to ' . $tmpdir . ' for ' . $user->nickname);
mkdir($tmpdir, 0700, true);
$this->dumpNotices($user, $tmpdir);
$this->dumpFaves($user, $tmpdir);
$this->dumpSubscriptions($user, $tmpdir);
$this->dumpSubscribers($user, $tmpdir);
$this->dumpGroups($user, $tmpdir);
$fileName = File::filename($user->getProfile(), "backup", "application/atom+xml");
$fullPath = File::path($fileName);
2011-10-07 06:31:50 +09:00
$this->makeActivityFeed($user, $tmpdir, $fullPath);
$this->delTree($tmpdir);
2011-10-07 06:31:50 +09:00
return $fileName;
}
2019-08-11 06:35:45 +09:00
public function notifyBackupFile($user, $fileName)
2011-10-07 06:31:50 +09:00
{
2011-10-07 22:36:52 +09:00
$fileUrl = File::url($fileName);
2019-08-11 06:35:45 +09:00
$body = sprintf(
_m(
"The backup file you requested is ready for download.\n\n".
2011-10-07 22:36:52 +09:00
"%s\n".
"Thanks for your time,\n",
2019-08-11 06:35:45 +09:00
"%s\n"
),
$fileUrl,
common_config('site', 'name')
);
2011-10-07 22:36:52 +09:00
$headers = _mail_prepare_headers('offlinebackup', $user->nickname, $user->nickname);
mail_to_user($user, _('Backup file ready for download'), $body, $headers);
2011-10-07 06:31:50 +09:00
}
2011-10-08 02:56:47 +09:00
2019-08-11 06:35:45 +09:00
public function dumpNotices($user, $dir)
2011-10-08 02:56:47 +09:00
{
common_log(LOG_INFO, 'dumping notices by ' . $user->nickname . ' to directory ' . $dir);
$profile = $user->getProfile();
$stream = new ProfileNoticeStream($profile, $profile);
$page = 1;
do {
$notice = $stream->getNotices(($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
while ($notice->fetch()) {
try {
2019-08-11 06:35:45 +09:00
$fname = $dir . '/'. common_date_iso8601($notice->created) . '-notice-' . $notice->id . '.atom';
$data = $notice->asAtomEntry(false, false, false, null);
common_log(LOG_INFO, 'dumping notice ' . $notice->id . ' to file ' . $fname);
file_put_contents($fname, $data);
$data = null;
} catch (Exception $e) {
common_log(LOG_ERR, "Error backing up notice " . $notice->id . ": " . $e->getMessage());
continue;
}
2011-10-08 02:56:47 +09:00
}
$page++;
} while ($notice->N > NOTICES_PER_PAGE);
}
2019-08-11 06:35:45 +09:00
public function dumpFaves($user, $dir)
2011-10-08 02:56:47 +09:00
{
common_log(LOG_INFO, 'dumping faves by ' . $user->nickname . ' to directory ' . $dir);
2019-08-11 06:35:45 +09:00
2011-10-08 02:56:47 +09:00
$page = 1;
do {
$fave = Fave::byProfile($user->id, ($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
while ($fave->fetch()) {
try {
2019-08-11 06:35:45 +09:00
$fname = $dir . '/'. common_date_iso8601($fave->modified) . '-fave-' . $fave->notice_id . '.atom';
$act = $fave->asActivity();
$data = $act->asString(false, false, false);
common_log(LOG_INFO, 'dumping fave of ' . $fave->notice_id . ' to file ' . $fname);
file_put_contents($fname, $data);
$data = null;
} catch (Exception $e) {
common_log(LOG_ERR, "Error backing up fave of " . $fave->notice_id . ": " . $e->getMessage());
continue;
}
2011-10-08 02:56:47 +09:00
}
2019-08-11 06:35:45 +09:00
$page++;
2011-10-08 02:56:47 +09:00
} while ($fave->N > NOTICES_PER_PAGE);
}
2019-08-11 06:35:45 +09:00
public function dumpSubscriptions($user, $dir)
2011-10-08 02:56:47 +09:00
{
common_log(LOG_INFO, 'dumping subscriptions by ' . $user->nickname . ' to directory ' . $dir);
2019-08-11 06:35:45 +09:00
2011-10-08 02:56:47 +09:00
$page = 1;
do {
$sub = Subscription::bySubscriber($user->id, ($page-1)*PROFILES_PER_PAGE, PROFILES_PER_PAGE + 1);
while ($sub->fetch()) {
try {
if ($sub->subscribed == $user->id) {
continue;
}
2019-08-11 06:35:45 +09:00
$fname = $dir . '/'. common_date_iso8601($sub->created) . '-subscription-' . $sub->subscribed . '.atom';
$act = $sub->asActivity();
$data = $act->asString(false, false, false);
common_log(LOG_INFO, 'dumping sub of ' . $sub->subscribed . ' to file ' . $fname);
file_put_contents($fname, $data);
$data = null;
} catch (Exception $e) {
common_log(LOG_ERR, "Error backing up subscription to " . $sub->subscribed . ": " . $e->getMessage());
2011-10-08 02:56:47 +09:00
continue;
}
}
$page++;
} while ($sub->N > PROFILES_PER_PAGE);
}
2019-08-11 06:35:45 +09:00
public function dumpSubscribers($user, $dir)
2011-10-08 02:56:47 +09:00
{
common_log(LOG_INFO, 'dumping subscribers to ' . $user->nickname . ' to directory ' . $dir);
2019-08-11 06:35:45 +09:00
2011-10-08 02:56:47 +09:00
$page = 1;
do {
$sub = Subscription::bySubscribed($user->id, ($page-1)*PROFILES_PER_PAGE, PROFILES_PER_PAGE + 1);
while ($sub->fetch()) {
try {
if ($sub->subscriber == $user->id) {
continue;
}
2019-08-11 06:35:45 +09:00
$fname = $dir . '/'. common_date_iso8601($sub->created) . '-subscriber-' . $sub->subscriber . '.atom';
$act = $sub->asActivity();
$data = $act->asString(false, true, false);
common_log(LOG_INFO, 'dumping sub by ' . $sub->subscriber . ' to file ' . $fname);
file_put_contents($fname, $data);
$data = null;
} catch (Exception $e) {
common_log(LOG_ERR, "Error backing up subscription from " . $sub->subscriber . ": " . $e->getMessage());
2011-10-08 02:56:47 +09:00
continue;
}
}
$page++;
} while ($sub->N > PROFILES_PER_PAGE);
}
2019-08-11 06:35:45 +09:00
public function dumpGroups($user, $dir)
2011-10-08 02:56:47 +09:00
{
common_log(LOG_INFO, 'dumping memberships of ' . $user->nickname . ' to directory ' . $dir);
2019-08-11 06:35:45 +09:00
2011-10-08 02:56:47 +09:00
$page = 1;
do {
$mem = Group_member::byMember($user->id, ($page-1)*GROUPS_PER_PAGE, GROUPS_PER_PAGE + 1);
while ($mem->fetch()) {
try {
2019-08-11 06:35:45 +09:00
$fname = $dir . '/'. common_date_iso8601($mem->created) . '-membership-' . $mem->group_id . '.atom';
$act = $mem->asActivity();
$data = $act->asString(false, false, false);
common_log(LOG_INFO, 'dumping membership in ' . $mem->group_id . ' to file ' . $fname);
file_put_contents($fname, $data);
$data = null;
} catch (Exception $e) {
common_log(LOG_ERR, "Error backing up membership in " . $mem->group_id . ": " . $e->getMessage());
continue;
}
2011-10-08 02:56:47 +09:00
}
$page++;
} while ($mem->N > GROUPS_PER_PAGE);
}
2019-08-11 06:35:45 +09:00
public function makeActivityFeed($user, $tmpdir, $fullPath)
{
$handle = fopen($fullPath, 'c');
$this->writeFeedHeader($user, $handle);
$objects = scandir($tmpdir);
rsort($objects);
foreach ($objects as $object) {
$objFull = $tmpdir . '/' . $object;
if (!is_dir($objFull)) {
$entry = file_get_contents($objFull);
fwrite($handle, $entry);
$entry = null;
}
}
$this->writeFeedFooter($user, $handle);
fclose($handle);
}
2019-08-11 06:35:45 +09:00
public function writeFeedHeader($user, $handle)
{
fwrite($handle, '<?xml version="1.0" encoding="UTF-8"?>');
fwrite($handle, "\n");
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/">');
fwrite($handle, "\n");
$profile = $user->getProfile();
$author = $profile->asActivityObject();
$xs = new XMLStringer();
$author->outputTo($xs, 'author');
fwrite($handle, $xs->getString());
fwrite($handle, "\n");
}
2019-08-11 06:35:45 +09:00
public function writeFeedFooter($user, $handle)
{
fwrite($handle, '</feed>');
}
2019-08-11 06:35:45 +09:00
public function delTree($dir)
{
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir") {
$this->delTree($dir."/".$object);
} else {
unlink($dir."/".$object);
}
}
}
reset($objects);
rmdir($dir);
}
}
2011-10-06 01:05:05 +09:00
}