1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # |
---|
4 | # This file is part of filesAlias, a plugin for Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2009-2010 Osku and contributors |
---|
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 | # |
---|
12 | # -- END LICENSE BLOCK ------------------------------------ |
---|
13 | |
---|
14 | class FilesAliases |
---|
15 | { |
---|
16 | protected $core; |
---|
17 | protected $aliases; |
---|
18 | |
---|
19 | public function __construct($core) |
---|
20 | { |
---|
21 | $this->core =& $core; |
---|
22 | } |
---|
23 | |
---|
24 | public function getAliases() |
---|
25 | { |
---|
26 | if (is_array($this->aliases)) { |
---|
27 | return $this->aliases; |
---|
28 | } |
---|
29 | |
---|
30 | $this->aliases = array(); |
---|
31 | $sql = 'SELECT filesalias_url, filesalias_destination, filesalias_position, filesalias_disposable '. |
---|
32 | 'FROM '.$this->core->prefix.'filesalias '. |
---|
33 | "WHERE blog_id = '".$this->core->con->escape($this->core->blog->id)."' ". |
---|
34 | 'ORDER BY filesalias_position ASC '; |
---|
35 | $this->aliases = $this->core->con->select($sql)->rows(); |
---|
36 | return $this->aliases; |
---|
37 | } |
---|
38 | |
---|
39 | public function getAlias($url) |
---|
40 | { |
---|
41 | $strReq = 'SELECT filesalias_url, filesalias_destination, filesalias_position, filesalias_disposable '. |
---|
42 | 'FROM '.$this->core->prefix.'filesalias '. |
---|
43 | "WHERE blog_id = '".$this->core->con->escape($this->core->blog->id)."' ". |
---|
44 | "AND filesalias_url = '".$url."' ". |
---|
45 | 'ORDER BY filesalias_position ASC '; |
---|
46 | |
---|
47 | $rs = $this->core->con->select($strReq); |
---|
48 | return $rs; |
---|
49 | } |
---|
50 | |
---|
51 | public function updateAliases($aliases) |
---|
52 | { |
---|
53 | $this->core->con->begin(); |
---|
54 | try |
---|
55 | { |
---|
56 | $this->deleteAliases(); |
---|
57 | foreach ($aliases as $k => $v) |
---|
58 | { |
---|
59 | if (!empty($v['filesalias_url']) && !empty($v['filesalias_destination'])) |
---|
60 | { |
---|
61 | $this->createAlias($v['filesalias_url'],$v['filesalias_destination'],$k+1,$v['filesalias_disposable']); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | $this->core->con->commit(); |
---|
66 | } |
---|
67 | catch (Exception $e) |
---|
68 | { |
---|
69 | $this->core->con->rollback(); |
---|
70 | throw $e; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | public function createAlias($url,$destination,$position,$disposable=0) |
---|
75 | { |
---|
76 | if (!$url) { |
---|
77 | throw new Exception(__('File URL is empty.')); |
---|
78 | } |
---|
79 | |
---|
80 | if (!$destination) { |
---|
81 | throw new Exception(__('File destination is empty.')); |
---|
82 | } |
---|
83 | |
---|
84 | $cur = $this->core->con->openCursor($this->core->prefix.'filesalias'); |
---|
85 | $cur->blog_id = (string) $this->core->blog->id; |
---|
86 | $cur->filesalias_url = (string) $url; |
---|
87 | $cur->filesalias_destination = (string) $destination; |
---|
88 | $cur->filesalias_position = abs((integer) $position); |
---|
89 | $cur->filesalias_disposable = abs((integer) $disposable); |
---|
90 | $cur->insert(); |
---|
91 | } |
---|
92 | |
---|
93 | public function deleteAliases() |
---|
94 | { |
---|
95 | $this->core->con->execute( |
---|
96 | 'DELETE FROM '.$this->core->prefix.'filesalias '. |
---|
97 | "WHERE blog_id = '".$this->core->con->escape($this->core->blog->id)."' " |
---|
98 | ); |
---|
99 | } |
---|
100 | |
---|
101 | public function deleteAlias($url) |
---|
102 | { |
---|
103 | $this->core->con->execute( |
---|
104 | 'DELETE FROM '.$this->core->prefix.'filesalias '. |
---|
105 | "WHERE blog_id = '".$this->core->con->escape($this->core->blog->id)."' ". |
---|
106 | "AND filesalias_url = '".$url."' " |
---|
107 | ); |
---|
108 | } |
---|
109 | } |
---|
110 | ?> |
---|