-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAccount.php
216 lines (191 loc) · 6.22 KB
/
Account.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
namespace model;
use util as u;
use core as c;
class Account
{
const TABLE_NAME = 'account';
const STATUS_VALID = 1;
const STATUS_UNVALID = 0;
const COMPRESS_LEVEL = 9;
public static $sSecretKey = '';
public function getAllAccount()
{
$sSQL = 'SELECT *, rowid FROM ' . self::TABLE_NAME . ' WHERE valid=' . self::STATUS_VALID . ' AND parent=0';
return u\DB::getList($sSQL);
}
/**
* Get the detail of an account.
* @param int $iAccountID Account ID.
* @return array
*/
public function getAccountDetail($iAccountID)
{
$sSQL = 'SELECT *, rowid
FROM ' . self::TABLE_NAME . '
WHERE rowid = ?
AND parent = 0
AND valid=' . self::STATUS_VALID . '
Limit 1';
return u\DB::getOne($sSQL, [$iAccountID]);
}
/**
* Get all fields for an account.
* @param int $iAccountID Account ID.
* @return array
*/
public function getAccountFields($iAccountID)
{
$sSQL = 'SELECT *, rowid FROM ' . self::TABLE_NAME . ' WHERE parent=? AND valid=' . self::STATUS_VALID;
$aResult = u\DB::getList($sSQL, [$iAccountID]);
foreach ($aResult as $k => $v) {
$aResult[$k]['value'] = $sRealValue = $this->decrypt(gzinflate($v['value']));
$aResult[$k]['name'] = base64_decode(gzinflate($v['name']));
if (substr($sRealValue, 0, 5) === 'link:') {
$sSQLAccount = 'SELECT rowid FROM ' . self::TABLE_NAME;
$sSQLAccount .= ' WHERE valid=' . self::STATUS_VALID . ' AND name=? LIMIT 1';
$sLinkAccount = substr($sRealValue, 5);
$aAccountID = u\DB::getOne($sSQLAccount, [$sLinkAccount]);
$aResult[$k]['link'] = c\Router::genURL('Detail', ['id' => $aAccountID['rowid']]);
$aResult[$k]['linkname'] = $sLinkAccount;
}
}
return $aResult;
}
/**
* Delete an account or a field.
* @param int $iRowID
* @return bool
*/
public function del($iRowID)
{
return u\DB::update(self::TABLE_NAME, ' WHERE rowid = ' . $iRowID, ['valid' => self::STATUS_UNVALID]);
}
/**
* Add a new account.
* @param string $sName Account name.
* @param string $sURL Account URL.
* @return int
*/
public function addAccount($sName, $sURL)
{
$sSQLCheck = 'SELECT rowid FROM ' . self::TABLE_NAME;
$sSQLCheck .= ' WHERE name=? AND parent=0 AND valid=' . self::STATUS_UNVALID . ' LIMIT 1';
$aResult = u\DB::getOne($sSQLCheck, [$sName]);
if ($aResult) {
$iRowID = $aResult['rowid'];
$aData = [
'valid' => self::STATUS_VALID,
];
u\DB::update(self::TABLE_NAME, 'WHERE rowid = ' . $iRowID, $aData);
return $iRowID;
}
$aData = [
'name' => $sName,
'value' => $sURL,
'parent' => 0,
'valid' => self::STATUS_VALID
];
return u\DB::add($aData, self::TABLE_NAME);
}
/**
* Add a field to an account.
* @param string $sName
* @param string $sValue
* @param int $iAccountID
* @return int
*/
public function addField($sName, $sValue, $iAccountID)
{
$aData = [
'name' => gzdeflate(base64_encode($sName), self::COMPRESS_LEVEL),
'value' => gzdeflate($this->encrypt($sValue), self::COMPRESS_LEVEL),
'parent' => $iAccountID,
'valid' => self::STATUS_VALID
];
return u\DB::add($aData, self::TABLE_NAME);
}
/**
* Update an account.
* @param string $sName
* @param string $sValue
* @param int $iRowID
* @return bool
*/
public function updateAccount($sName, $sValue, $iRowID, $bEncrypt = true)
{
$aData = [
'name' => $sName,
'value' => $sValue
];
return u\DB::update(self::TABLE_NAME, 'WHERE rowid = ' . $iRowID, $aData);
}
/**
* Update a field for an account.
* @param string $sName
* @param string $sValue
* @param int $iRowID
* @return bool
*/
public function updateField($sName, $sValue, $iRowID, $bEncrypt = true)
{
$name= $bEncrypt ? base64_encode($sName) : $sName;
$value = $bEncrypt ? $this->encrypt($sValue) : $sValue;
$aData = [
'name' => gzdeflate($name, self::COMPRESS_LEVEL),
'value' => gzdeflate($value, self::COMPRESS_LEVEL)
];
return u\DB::update(self::TABLE_NAME, 'WHERE rowid = ' . $iRowID, $aData);
}
/**
* Get the total of account.
* @return int
*/
public function getTotal()
{
$sSQL = 'SELECT count(*) as total FROM ' . self::TABLE_NAME;
$sSQL .= ' WHERE valid=' . self::STATUS_VALID . ' AND parent=0 Limit 1';
$aResult = u\DB::getOne($sSQL);
$mResult = ($aResult === false) ? false : $aResult['total'];
return $mResult;
}
/**
* Create the table.
*/
public function createTable()
{
$sSQL = 'create table account (name text, value text, parent interger, valid interger)';
u\DB::query($sSQL);
}
/**
* The encrypt function.
* @param string $sData
* @return string
*/
public function encrypt($sData)
{
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, self::$sSecretKey, $iv);
$encrypted = mcrypt_generic($td, $sData);
mcrypt_generic_deinit($td);
return $iv . $encrypted;
}
/**
* The decrypt function.
* @param string $sData
* @return string
*/
public function decrypt($sData)
{
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, '');
$iv = mb_substr($sData, 0, 32, 'latin1');
mcrypt_generic_init($td, self::$sSecretKey, $iv);
$data = mb_substr($sData, 32, mb_strlen($sData, 'latin1'), 'latin1');
$data = mdecrypt_generic($td, $data);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return trim($data);
}
}
# end of this file