-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIdentity.php
212 lines (180 loc) · 5.4 KB
/
Identity.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
<?php
/**
* Simple 'identity to user ID'
*
* TODO: get table schema
*
* @author Se#
* @version 0.0.1
*/
class Evil_Identity
{
/**
* Encrypt or not an identity
*
* @var bool
*/
public static $encrypt = false;
/**
* Table name without prefix
*
* @var string
*/
public static $table = 'identity';
/**
* Table name prefix
*
* @var string
*/
public static $prefix = '';
/**
* Record data
*
* @var array
*/
protected $_data = array();
/**
* Try to load identity-record by identity
*
* @param $identity
*/
public function __construct($identity)
{
if(is_array($identity)){
$this->_data = $identity;
return true;
}
$db = Zend_Registry::get('db');
self::$prefix = empty(self::$prefix) ? Zend_Registry::get('db-prefix') : self::$prefix;
$select = $db->select()->from(self::$prefix . self::$table)->where('identity=?', self::_encrypt($identity));
$row = $db->fetchRow($select);
$row = is_object($row) ? $row->toArray() : $row;
$this->_data = $row;
}
/**
* Encrypt an identity
*
* @static
* @param string $identity
* @return string
*/
protected static function _encrypt($identity)
{
if(self::$encrypt)
return sha1($identity . md5($identity));
return $identity;
}
/**
* @description public alias for _encrypt
* @static
* @param $identity
* @return string
*/
public static function encrypt($identity)
{
return self::_encrypt($identity);
}
/**
* Get a record attribute
*
* @param string $name
* @return string|number|null
*/
public function __get($name)
{
return isset($this->_data[$name]) ? $this->_data[$name] : null;
}
/**
* Return list of identities for uid
*
* @static
* @param string $uid
* @param bool $object
* @return array
*/
public static function getList($uid, $object = false)
{
$db = Zend_Registry::get('db');
self::$prefix = empty(self::$prefix) ? Zend_Registry::get('db-prefix') : self::$prefix;
$list = $db->fetchAll($db->select()->from(self::$prefix . self::$table)->where('uid=?', $uid));
if($object && !empty($list)){
foreach($list as $index => $item)
$list[$index] = new self($item);
}
return $list;
}
/**
* Static alias for __construct()
*
* @static
* @param string $identity
* @return Evil_Identity
*/
public static function get($identity)
{
return new self($identity);
}
public static function where($field, $selector, $value)
{
$db = Zend_Registry::get('db');
$select = $db->select()->from(self::$prefix . self::$table);
if(is_array($field) && is_array($selector) && is_array($value))
{
foreach($field as $index => $name)
$select->where($name . $selector[$index] . '?',$value[$index]);
}
elseif(is_string($field) && is_string($selector) && is_string($value))
$select->where($field . $selector . '?', $value);
return $db->fetchAll($select);
}
/**
* Create an identity record in the current DB
*
* @static
* @param string $identity
* @param string $uid
* @return Evil_Identity|null
*/
public static function create($title, $identity, $uid)
{
$db = Zend_Registry::get('db');
self::$prefix = empty(self::$prefix) ? Zend_Registry::get('db-prefix') : self::$prefix;
$existed = new self($identity);
if(null != $existed->uid)
return $existed;
if($db->insert(self::$prefix . self::$table, array('identity' => self::_encrypt($identity),
'uid' => $uid,
'title' => $title)))
return new self($identity);
return null;
}
/**
* Delete an identity. May get an array('title' => , 'identity' =>, 'uid' => )
*
* @static
* @param string|array $title
* @param string $identity
* @param string $uid
* @return bool|null
*/
public static function delete($title, $identity = '', $uid = '')
{
$db = Zend_Registry::get('db');
self::$prefix = empty(self::$prefix) ? Zend_Registry::get('db-prefix') : self::$prefix;
if(is_array($title))
{
$uid = isset($title['uid']) ? $title['uid'] : -1;
$identity = isset($title['identity']) ? $title['identity'] : '';
$title = isset($title['title']) ? $title['title'] : '';
}
$existed = new self($identity);
if(null == $existed->uid)
return null;
$where = '(title="' . htmlspecialchars($title) . '")
&&(identity="' . self::encrypt($identity) . '")
&&(uid="' . htmlspecialchars($uid) . '")';
if($db->delete(self::$prefix . self::$table, $where))
return true;
return null;
}
}