1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # This file is part of kUtRL, a plugin for Dotclear 2. |
---|
4 | # |
---|
5 | # Copyright (c) 2009 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 | if (!defined('DC_RC_PATH')){return;} |
---|
14 | |
---|
15 | $core->addBehavior('initWidgets',array('widgetKutrl','adminShorten')); |
---|
16 | $core->addBehavior('initWidgets',array('widgetKutrl','adminRank')); |
---|
17 | |
---|
18 | class widgetKutrl |
---|
19 | { |
---|
20 | public static function adminShorten($w) |
---|
21 | { |
---|
22 | $w->create('shortenkutrl',__('Links shortener'), |
---|
23 | array('widgetKutrl','publicShorten') |
---|
24 | ); |
---|
25 | $w->shortenkutrl->setting('title', |
---|
26 | __('Title:'),__('Shorten link'),'text' |
---|
27 | ); |
---|
28 | $w->shortenkutrl->setting('homeonly', |
---|
29 | __('Home page only'),1,'check' |
---|
30 | ); |
---|
31 | } |
---|
32 | |
---|
33 | public static function adminRank($w) |
---|
34 | { |
---|
35 | $w->create('rankkutrl',__('Top mini links'), |
---|
36 | array('widgetKutrl','publicRank') |
---|
37 | ); |
---|
38 | $w->rankkutrl->setting('title', |
---|
39 | __('Title:'),__('Top mini links'),'text' |
---|
40 | ); |
---|
41 | $w->rankkutrl->setting('text', |
---|
42 | __('Text:'),'%rank% - %url% - %counttext%','text' |
---|
43 | ); |
---|
44 | $w->rankkutrl->setting('urllen', |
---|
45 | __('Link length (if truncate)'),20 |
---|
46 | ); |
---|
47 | $w->rankkutrl->setting('type', |
---|
48 | __('Type:'),'all','combo',array( |
---|
49 | __('All') => '-', |
---|
50 | __('Mini URL') => 'normal', |
---|
51 | __('Custom URL')=>'custom' |
---|
52 | ) |
---|
53 | ); |
---|
54 | $w->rankkutrl->setting('sort', |
---|
55 | __('Sort:'),'desc','combo',array( |
---|
56 | __('Ascending') => 'asc', |
---|
57 | __('Descending') => 'desc' |
---|
58 | ) |
---|
59 | ); |
---|
60 | $w->rankkutrl->setting('limit', |
---|
61 | __('Limit:'),'10','text' |
---|
62 | ); |
---|
63 | $w->rankkutrl->setting('hideempty', |
---|
64 | __('Hide no followed links'),0,'check' |
---|
65 | ); |
---|
66 | $w->rankkutrl->setting('homeonly', |
---|
67 | __('Home page only'),1,'check' |
---|
68 | ); |
---|
69 | } |
---|
70 | |
---|
71 | public static function publicShorten($w) |
---|
72 | { |
---|
73 | global $core; |
---|
74 | |
---|
75 | if ($w->homeonly && $core->url->type != 'default') return; |
---|
76 | |
---|
77 | if (!$core->blog->settings->kutrl_active |
---|
78 | || !$core->blog->settings->kutrl_srv_local_public) return; |
---|
79 | |
---|
80 | $hmf = hmfKutrl::create(); |
---|
81 | $hmfp = hmfKutrl::protect($hmf); |
---|
82 | |
---|
83 | return |
---|
84 | '<div class="shortenkutrlwidget">'. |
---|
85 | ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : ''). |
---|
86 | '<form name="shortenkutrlwidget" method="post" action="'. |
---|
87 | $core->blog->url.$core->url->getBase('kutrl').'">'. |
---|
88 | '<p><label>'. |
---|
89 | __('Long link:').'<br />'. |
---|
90 | form::field('longurl',20,255,''). |
---|
91 | '</label></p>'. |
---|
92 | '<p><label>'. |
---|
93 | sprintf(__('Write "%s" in next field to see if you are not a robot:'),$hmf).'<br />'. |
---|
94 | form::field('hmf',20,255,''). |
---|
95 | '</label></p>'. |
---|
96 | '<p><input class="submit" type="submit" name="submiturl" value="'.__('Create').'" />'. |
---|
97 | form::hidden('hmfp',$hmfp). |
---|
98 | $core->formNonce(). |
---|
99 | '</p>'. |
---|
100 | '</form>'. |
---|
101 | '</div>'; |
---|
102 | } |
---|
103 | |
---|
104 | public static function publicRank($w) |
---|
105 | { |
---|
106 | global $core; |
---|
107 | |
---|
108 | if ($w->homeonly && $core->url->type != 'default') return; |
---|
109 | |
---|
110 | if (!$core->blog->settings->kutrl_active) return; |
---|
111 | |
---|
112 | if ($w->type == 'normal') |
---|
113 | { |
---|
114 | $type = "AND kut_type ='".$core->con->escape('localnormal')."' "; |
---|
115 | } |
---|
116 | elseif ($w->type == 'custom') |
---|
117 | { |
---|
118 | $type = "AND kut_type ='".$core->con->escape('localcustom')."' "; |
---|
119 | } |
---|
120 | else |
---|
121 | { |
---|
122 | $type = "AND kut_type ".$core->con->in(array('localnormal','localcustom'))." "; |
---|
123 | } |
---|
124 | |
---|
125 | $hide = (boolean) $w->hideempty ? 'AND kut_counter > 0 ' : ''; |
---|
126 | |
---|
127 | $rs = $core->con->select( |
---|
128 | 'SELECT kut_counter, kut_hash '. |
---|
129 | "FROM ".$core->prefix."kutrl ". |
---|
130 | "WHERE blog_id='".$core->con->escape($core->blog->id)."' ". |
---|
131 | "AND kut_service = 'local' ". |
---|
132 | $type.$hide. |
---|
133 | 'ORDER BY kut_counter '.($w->sort == 'asc' ? 'ASC' : 'DESC').',kut_hash ASC '. |
---|
134 | $core->con->limit(abs((integer) $w->limit))); |
---|
135 | |
---|
136 | if ($rs->isEmpty()) return; |
---|
137 | |
---|
138 | $content = ''; |
---|
139 | $i = 0; |
---|
140 | |
---|
141 | while($rs->fetch()) |
---|
142 | { |
---|
143 | $i++; |
---|
144 | $rank = '<span class="rankkutrl-rank">'.$i.'</span>'; |
---|
145 | |
---|
146 | $hash = $rs->kut_hash; |
---|
147 | $url = $core->blog->url.$core->url->getBase('kutrl').'/'.$hash; |
---|
148 | $cut_len = - abs((integer) $w->urllen); |
---|
149 | |
---|
150 | if (strlen($url) > $cut_len) |
---|
151 | $url = '...'.substr($url,$cut_len); |
---|
152 | /* |
---|
153 | if (strlen($hash) > $cut_len) |
---|
154 | $url = '...'.substr($hash,$cut_len); |
---|
155 | */ |
---|
156 | if ($rs->kut_counter == 0) |
---|
157 | $counttext = __('never followed'); |
---|
158 | elseif ($rs->kut_counter == 1) |
---|
159 | $counttext = __('followed one time'); |
---|
160 | else |
---|
161 | $counttext = sprintf(__('followed %s times'),$rs->kut_counter); |
---|
162 | |
---|
163 | $content .= |
---|
164 | '<li><a href="'. |
---|
165 | $core->blog->url.$core->url->getBase('kutrl').'/'.$rs->kut_hash. |
---|
166 | '">'. |
---|
167 | str_replace( |
---|
168 | array('%rank%','%hash%','%url%','%count%','%counttext%'), |
---|
169 | array($rank,$hash,$url,$rs->kut_counter,$counttext), |
---|
170 | $w->text |
---|
171 | ). |
---|
172 | '</a></li>'; |
---|
173 | |
---|
174 | } |
---|
175 | |
---|
176 | if (!$content) return; |
---|
177 | |
---|
178 | return |
---|
179 | '<div class="rankkutrlwidget">'. |
---|
180 | ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : ''). |
---|
181 | '<ul>'.$content.'</ul>'. |
---|
182 | '</div>'; |
---|
183 | } |
---|
184 | } |
---|
185 | ?> |
---|