-
Notifications
You must be signed in to change notification settings - Fork 4
/
UserData.php
82 lines (67 loc) · 2.35 KB
/
UserData.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace Sunlight;
use Sunlight\Database\Database as DB;
use Sunlight\Util\Filesystem;
use Sunlight\Util\Json;
use Sunlight\Util\TemporaryFile;
class UserData
{
/** @var int */
private $userId;
/** @var array */
private $options;
function __construct(int $userId, array $options)
{
$this->userId = $userId;
$this->options = $options;
}
function generate(): TemporaryFile
{
// load account data
$userData = DB::queryRow('SELECT * FROM ' . DB::table('user') . ' WHERE id = ' . $this->userId);
if ($userData === false) {
throw new \UnexpectedValueException('User not found');
}
// create tmp file and ZIP archive
$tmpFile = Filesystem::createTmpFile();
$zip = new \ZipArchive();
$zip->open($tmpFile->getPathname(), \ZipArchive::OVERWRITE);
// add content
$this->addAccountInfo($zip, $userData);
$this->addAvatar($zip, $userData['avatar']);
// event
Extend::call('user.data.generate', [
'archive' => $zip,
'user_id' => $this->userId,
'user_data' => $userData,
'options' => $this->options,
]);
return $tmpFile;
}
private function addAccountInfo(\ZipArchive $zip, array $userData): void
{
$info = [
'User name' => $userData['username'],
'Display name' => $userData['publicname'],
'Login counter' => $userData['logincounter'],
'Registration time' => DB::datetime($userData['registertime']),
'Activity time' => DB::datetime($userData['activitytime']),
'IP address' => $userData['ip'],
'Comment IP addresses' => DB::queryRows('SELECT DISTINCT ip FROM ' . DB::table('post') . ' WHERE author = ' . $this->userId, null, 'ip'),
'email' => $userData['email'],
'note' => $userData['note'],
];
$zip->addFromString(
'account.json',
Json::encode($info, Json::PRETTY)
);
}
private function addAvatar(\ZipArchive $zip, ?string $avatar): void
{
if ($avatar === null) {
return;
}
$avatarPath = new \SplFileInfo(User::getAvatarPath($avatar));
$zip->addFile($avatarPath->getPathname(), sprintf('avatar.%s', $avatarPath->getExtension()));
}
}