1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # This file is part of kUtRL, a plugin for Dotclear 2. |
---|
4 | # |
---|
5 | # Copyright (c) 2009-2010 JC Denis and contributors |
---|
6 | # jcdenis@gdwd.com |
---|
7 | # |
---|
8 | # Licensed under the GPL version 2.0 license. |
---|
9 | # A copy of this license is available in LICENSE file or at |
---|
10 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
11 | # -- END LICENSE BLOCK ------------------------------------ |
---|
12 | |
---|
13 | # This file contents class to acces local short links records |
---|
14 | |
---|
15 | class kutrlLog |
---|
16 | { |
---|
17 | public $core; |
---|
18 | public $table; |
---|
19 | public $blog; |
---|
20 | public $con; |
---|
21 | |
---|
22 | public function __construct($core) |
---|
23 | { |
---|
24 | $this->core = $core; |
---|
25 | $this->table = $core->prefix.'kutrl'; |
---|
26 | $this->blog = $core->con->escape($core->blog->id); |
---|
27 | $this->con = $core->con; |
---|
28 | } |
---|
29 | |
---|
30 | public function nextId() |
---|
31 | { |
---|
32 | return $this->con->select( |
---|
33 | 'SELECT MAX(kut_id) FROM '.$this->table |
---|
34 | )->f(0) + 1; |
---|
35 | } |
---|
36 | |
---|
37 | public function insert($url,$hash,$type,$service='kutrl') |
---|
38 | { |
---|
39 | $cur = $this->con->openCursor($this->table); |
---|
40 | $this->con->writeLock($this->table); |
---|
41 | |
---|
42 | try { |
---|
43 | $cur->kut_id = $this->nextId(); |
---|
44 | $cur->blog_id = $this->blog; |
---|
45 | $cur->kut_url = (string) $url; |
---|
46 | $cur->kut_hash = (string) $hash; |
---|
47 | $cur->kut_type = (string) $type; |
---|
48 | $cur->kut_service = (string) $service; |
---|
49 | $cur->kut_dt = date('Y-m-d H:i:s'); |
---|
50 | $cur->kut_counter = 0; |
---|
51 | |
---|
52 | $cur->insert(); |
---|
53 | $this->con->unlock(); |
---|
54 | |
---|
55 | return array( |
---|
56 | 'id' => $cur->kut_id, |
---|
57 | 'url' => $url, |
---|
58 | 'hash' => $hash, |
---|
59 | 'type' => $type, |
---|
60 | 'service' => $service, |
---|
61 | 'counter '=> 0 |
---|
62 | ); |
---|
63 | } |
---|
64 | catch (Exception $e) |
---|
65 | { |
---|
66 | $this->con->unlock(); |
---|
67 | throw $e; |
---|
68 | } |
---|
69 | return false; |
---|
70 | } |
---|
71 | |
---|
72 | public function select($url=null,$hash=null,$type=null,$service='kutrl') |
---|
73 | { |
---|
74 | //$this->con->writeLock($this->table); |
---|
75 | |
---|
76 | $req = |
---|
77 | 'SELECT kut_id as id, kut_hash as hash, kut_url as url, '. |
---|
78 | 'kut_type as type, kut_service as service, kut_counter as counter '. |
---|
79 | 'FROM '.$this->table.' '. |
---|
80 | "WHERE blog_id = '".$this->blog."' ". |
---|
81 | "AND kut_service = '".$this->con->escape($service)."' "; |
---|
82 | |
---|
83 | if (null !== $url) |
---|
84 | $req .= "AND kut_url = '".$this->con->escape($url)."' "; |
---|
85 | |
---|
86 | if (null !== $hash) |
---|
87 | $req .= "AND kut_hash = '".$this->con->escape($hash)."' "; |
---|
88 | |
---|
89 | if (null !== $type) { |
---|
90 | if (is_array($type)) { |
---|
91 | $req .= "AND kut_type '".$this->con->in($type)."' "; |
---|
92 | } |
---|
93 | else { |
---|
94 | $req .= "AND kut_type = '".$this->con->escape($type)."' "; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | $req .= 'ORDER BY kut_dt DESC '.$this->con->limit(1); |
---|
99 | |
---|
100 | $rs = $this->con->select($req); |
---|
101 | //$this->con->unlock(); |
---|
102 | |
---|
103 | return $rs->isEmpty() ? false : $rs; |
---|
104 | } |
---|
105 | |
---|
106 | public function delete($id) |
---|
107 | { |
---|
108 | $id = (integer) $id; |
---|
109 | |
---|
110 | return $this->con->execute( |
---|
111 | 'DELETE FROM '.$this->table.' '. |
---|
112 | "WHERE blog_id='".$this->blog."' ". |
---|
113 | "AND kut_id='".$id."' " |
---|
114 | ); |
---|
115 | } |
---|
116 | |
---|
117 | public function counter($id,$do='get') |
---|
118 | { |
---|
119 | $id = (integer) $id; |
---|
120 | |
---|
121 | $rs = $this->con->select( |
---|
122 | 'SELECT kut_counter '. |
---|
123 | 'FROM '.$this->table.' '. |
---|
124 | "WHERE blog_id='".$this->blog."' ". |
---|
125 | "AND kut_id='".$id."' " |
---|
126 | ); |
---|
127 | |
---|
128 | $counter = $rs->isEmpty() ? 0 : $rs->kut_counter; |
---|
129 | |
---|
130 | if ('get' == $do) |
---|
131 | { |
---|
132 | return $counter; |
---|
133 | } |
---|
134 | elseif ('up' == $do) |
---|
135 | { |
---|
136 | $counter += 1; |
---|
137 | } |
---|
138 | elseif ('reset' == $do) |
---|
139 | { |
---|
140 | $counter = 0; |
---|
141 | } |
---|
142 | else |
---|
143 | { |
---|
144 | return 0; |
---|
145 | } |
---|
146 | |
---|
147 | $cur = $this->con->openCursor($this->table); |
---|
148 | $this->con->writeLock($this->table); |
---|
149 | |
---|
150 | $cur->kut_counter = (integer) $counter; |
---|
151 | $cur->update( |
---|
152 | "WHERE blog_id='".$this->blog."' ". |
---|
153 | "AND kut_id='".$id."'" |
---|
154 | ); |
---|
155 | $this->con->unlock(); |
---|
156 | |
---|
157 | return $counter; |
---|
158 | } |
---|
159 | |
---|
160 | public function getLogs($p,$count_only=false) |
---|
161 | { |
---|
162 | if ($count_only) |
---|
163 | { |
---|
164 | $r = 'SELECT count(S.kut_id) '; |
---|
165 | } |
---|
166 | else |
---|
167 | { |
---|
168 | $content_req = ''; |
---|
169 | |
---|
170 | if (!empty($p['columns']) && is_array($p['columns'])) |
---|
171 | { |
---|
172 | $content_req .= implode(', ',$p['columns']).', '; |
---|
173 | } |
---|
174 | $r = |
---|
175 | 'SELECT S.kut_id, S.kut_type, S.kut_hash, S.kut_url, '. |
---|
176 | $content_req.'S.kut_dt '; |
---|
177 | } |
---|
178 | |
---|
179 | $r .= 'FROM '.$this->table.' S '; |
---|
180 | |
---|
181 | if (!empty($p['from'])) |
---|
182 | { |
---|
183 | $r .= $p['from'].' '; |
---|
184 | } |
---|
185 | $r .= "WHERE S.blog_id = '".$this->blog."' "; |
---|
186 | |
---|
187 | if (isset($p['kut_service'])) |
---|
188 | { |
---|
189 | $r .= "AND kut_service='".$this->con->escape($p['kut_service'])."' "; |
---|
190 | } |
---|
191 | else |
---|
192 | { |
---|
193 | $r .= "AND kut_service='kutrl' "; |
---|
194 | } |
---|
195 | |
---|
196 | if (isset($p['kut_type'])) |
---|
197 | { |
---|
198 | if (is_array($p['kut_type']) && !empty($p['kut_type'])) |
---|
199 | { |
---|
200 | $r .= 'AND kut_type '.$this->con->in($p['kut_type']); |
---|
201 | } |
---|
202 | elseif ($p['kut_type'] != '') |
---|
203 | { |
---|
204 | $r .= "AND kut_type = '".$this->con->escape($p['kut_type'])."' "; |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | if (isset($p['kut_id'])) |
---|
209 | { |
---|
210 | if (is_array($p['kut_id']) && !empty($p['kut_id'])) |
---|
211 | { |
---|
212 | $r .= 'AND kut_id '.$this->con->in($p['kut_id']); |
---|
213 | } |
---|
214 | elseif ($p['kut_id'] != '') |
---|
215 | { |
---|
216 | $r .= "AND kut_id = '".$this->con->escape($p['kut_id'])."' "; |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | if (isset($p['kut_hash'])) |
---|
221 | { |
---|
222 | if (is_array($p['kut_hash']) && !empty($p['kut_hash'])) |
---|
223 | { |
---|
224 | $r .= 'AND kut_hash '.$this->con->in($p['kut_hash']); |
---|
225 | } |
---|
226 | elseif ($p['kut_hash'] != '') |
---|
227 | { |
---|
228 | $r .= "AND kut_hash = '".$this->con->escape($p['kut_hash'])."' "; |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | if (isset($p['kut_url'])) |
---|
233 | { |
---|
234 | if (is_array($p['kut_url']) && !empty($p['kut_url'])) |
---|
235 | { |
---|
236 | $r .= 'AND kut_url '.$this->con->in($p['kut_url']); |
---|
237 | } |
---|
238 | elseif ($p['kut_url'] != '') |
---|
239 | { |
---|
240 | $r .= "AND kut_url = '".$this->con->escape($p['kut_url'])."' "; |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | if (!empty($p['kut_year'])) |
---|
245 | { |
---|
246 | $r .= |
---|
247 | 'AND '.$this->con->dateFormat('kut_dt','%Y').' = '. |
---|
248 | "'".sprintf('%04d',$p['kut_year'])."' "; |
---|
249 | } |
---|
250 | |
---|
251 | if (!empty($p['kut_month'])) |
---|
252 | { |
---|
253 | $r .= |
---|
254 | 'AND '.$this->con->dateFormat('kut_dt','%m').' = '. |
---|
255 | "'".sprintf('%02d',$p['kut_month'])."' "; |
---|
256 | } |
---|
257 | |
---|
258 | if (!empty($p['kut_day'])) |
---|
259 | { |
---|
260 | $r .= |
---|
261 | 'AND '.$this->con->dateFormat('kut_dt','%d').' = '. |
---|
262 | "'".sprintf('%02d',$p['kut_day'])."' "; |
---|
263 | } |
---|
264 | |
---|
265 | if (!empty($p['sql'])) |
---|
266 | { |
---|
267 | $r .= $p['sql'].' '; |
---|
268 | } |
---|
269 | |
---|
270 | if (!$count_only) |
---|
271 | { |
---|
272 | $r .= empty($p['order']) ? |
---|
273 | 'ORDER BY kut_dt DESC ' : |
---|
274 | 'ORDER BY '.$this->con->escape($p['order']).' '; |
---|
275 | } |
---|
276 | |
---|
277 | if (!$count_only && !empty($p['limit'])) |
---|
278 | { |
---|
279 | $r .= $this->con->limit($p['limit']); |
---|
280 | } |
---|
281 | |
---|
282 | return $this->con->select($r); |
---|
283 | } |
---|
284 | } |
---|
285 | ?> |
---|