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 | 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 of short links'), |
---|
36 | array('widgetKutrl','publicRank') |
---|
37 | ); |
---|
38 | $w->rankkutrl->setting('title', |
---|
39 | __('Title:'),__('Top of short links'),'text' |
---|
40 | ); |
---|
41 | $w->rankkutrl->setting('text', |
---|
42 | __('Text: (Use wildcard %rank%, %hash%, %url%, %count%, %counttext%)'),'%rank% - %url% - %counttext%','text' |
---|
43 | ); |
---|
44 | $w->rankkutrl->setting('urllen', |
---|
45 | __('URL length (if truncate)'),20,'text' |
---|
46 | ); |
---|
47 | $w->rankkutrl->setting('type', |
---|
48 | __('Type:'),'all','combo',array( |
---|
49 | __('All') => '-', |
---|
50 | __('Mini URL') => 'localnormal', |
---|
51 | __('Custom URL') => 'localcustom', |
---|
52 | __('Semi-custom') => 'localmix' |
---|
53 | ) |
---|
54 | ); |
---|
55 | $w->rankkutrl->setting('mixprefix', |
---|
56 | __('Semi-custom prefix: (only if you want limit to a particular prefix)'), |
---|
57 | '','text' |
---|
58 | ); |
---|
59 | $w->rankkutrl->setting('sortby', |
---|
60 | __('Sort by:'),'kut_counter','combo',array( |
---|
61 | __('Date') => 'kut_dt', |
---|
62 | __('Rank') => 'kut_counter', |
---|
63 | __('Hash') => 'kut_hash' |
---|
64 | ) |
---|
65 | ); |
---|
66 | $w->rankkutrl->setting('sort', |
---|
67 | __('Sort:'),'desc','combo',array( |
---|
68 | __('Ascending') => 'asc', |
---|
69 | __('Descending') => 'desc' |
---|
70 | ) |
---|
71 | ); |
---|
72 | $w->rankkutrl->setting('limit', |
---|
73 | __('Limit:'),'10','text' |
---|
74 | ); |
---|
75 | $w->rankkutrl->setting('hideempty', |
---|
76 | __('Hide no followed links'),0,'check' |
---|
77 | ); |
---|
78 | $w->rankkutrl->setting('homeonly', |
---|
79 | __('Home page only'),1,'check' |
---|
80 | ); |
---|
81 | } |
---|
82 | |
---|
83 | public static function publicShorten($w) |
---|
84 | { |
---|
85 | global $core; |
---|
86 | $s = kutrlSettings($core); |
---|
87 | |
---|
88 | if (!$s->kutrl_active |
---|
89 | || !$s->kutrl_srv_local_public |
---|
90 | || !$w->homeonly && $core->url->type != 'default') return; |
---|
91 | |
---|
92 | $hmf = hmfKutrl::create(); |
---|
93 | $hmfp = hmfKutrl::protect($hmf); |
---|
94 | |
---|
95 | return |
---|
96 | '<div class="shortenkutrlwidget">'. |
---|
97 | ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : ''). |
---|
98 | '<form name="shortenkutrlwidget" method="post" action="'. |
---|
99 | $core->blog->url.$core->url->getBase('kutrl').'">'. |
---|
100 | '<p><label>'. |
---|
101 | __('Long link:').'<br />'. |
---|
102 | form::field('longurl',20,255,''). |
---|
103 | '</label></p>'. |
---|
104 | '<p><label>'. |
---|
105 | sprintf(__('Rewrite \"%s\" in next field to show that you are not a robot:'),$hmf).'<br />'. |
---|
106 | form::field('hmf',20,255,''). |
---|
107 | '</label></p>'. |
---|
108 | '<p><input class="submit" type="submit" name="submiturl" value="'.__('Create').'" />'. |
---|
109 | form::hidden('hmfp',$hmfp). |
---|
110 | $core->formNonce(). |
---|
111 | '</p>'. |
---|
112 | '</form>'. |
---|
113 | '</div>'; |
---|
114 | } |
---|
115 | |
---|
116 | public static function publicRank($w) |
---|
117 | { |
---|
118 | global $core; |
---|
119 | $s = kutrlSettings($core); |
---|
120 | |
---|
121 | if (!$s->kutrl_active |
---|
122 | || $w->homeonly && $core->url->type != 'default') return; |
---|
123 | |
---|
124 | $type = in_array($w->type,array('localnormal','localmix','localcustom')) ? |
---|
125 | "AND kut_type ='".$w->type."' " : |
---|
126 | "AND kut_type ".$core->con->in(array('localnormal','localmix','localcustom'))." "; |
---|
127 | |
---|
128 | $hide = (boolean) $w->hideempty ? 'AND kut_counter > 0 ' : ''; |
---|
129 | |
---|
130 | $more = ''; |
---|
131 | if ($w->type == 'localmix' && '' != $w->mixprefix) { |
---|
132 | $more = "AND kut_hash LIKE '".$core->con->escape($w->mixprefix)."%' "; |
---|
133 | } |
---|
134 | |
---|
135 | $order = ($w->sortby && in_array($w->sortby,array('kut_dt','kut_counter','kut_hash'))) ? |
---|
136 | $w->sortby.' ' : 'kut_dt '; |
---|
137 | |
---|
138 | $order .= $w->sort == 'desc' ? 'DESC' : 'ASC'; |
---|
139 | |
---|
140 | $limit = $core->con->limit(abs((integer) $w->limit)); |
---|
141 | |
---|
142 | $rs = $core->con->select( |
---|
143 | 'SELECT kut_counter, kut_hash '. |
---|
144 | "FROM ".$core->prefix."kutrl ". |
---|
145 | "WHERE blog_id='".$core->con->escape($core->blog->id)."' ". |
---|
146 | "AND kut_service = 'local' ". |
---|
147 | $type.$hide.$more.'ORDER BY '.$order.$limit |
---|
148 | ); |
---|
149 | |
---|
150 | if ($rs->isEmpty()) return; |
---|
151 | |
---|
152 | $content = ''; |
---|
153 | $i = 0; |
---|
154 | |
---|
155 | while($rs->fetch()) |
---|
156 | { |
---|
157 | $i++; |
---|
158 | $rank = '<span class="rankkutrl-rank">'.$i.'</span>'; |
---|
159 | |
---|
160 | $hash = $rs->kut_hash; |
---|
161 | $url = $core->blog->url.$core->url->getBase('kutrl').'/'.$hash; |
---|
162 | $cut_len = - abs((integer) $w->urllen); |
---|
163 | |
---|
164 | if (strlen($url) > $cut_len) |
---|
165 | $url = '...'.substr($url,$cut_len); |
---|
166 | /* |
---|
167 | if (strlen($hash) > $cut_len) |
---|
168 | $url = '...'.substr($hash,$cut_len); |
---|
169 | */ |
---|
170 | if ($rs->kut_counter == 0) |
---|
171 | $counttext = __('never followed'); |
---|
172 | elseif ($rs->kut_counter == 1) |
---|
173 | $counttext = __('followed one time'); |
---|
174 | else |
---|
175 | $counttext = sprintf(__('followed %s times'),$rs->kut_counter); |
---|
176 | |
---|
177 | $content .= |
---|
178 | '<li><a href="'. |
---|
179 | $core->blog->url.$core->url->getBase('kutrl').'/'.$rs->kut_hash. |
---|
180 | '">'. |
---|
181 | str_replace( |
---|
182 | array('%rank%','%hash%','%url%','%count%','%counttext%'), |
---|
183 | array($rank,$hash,$url,$rs->kut_counter,$counttext), |
---|
184 | $w->text |
---|
185 | ). |
---|
186 | '</a></li>'; |
---|
187 | |
---|
188 | } |
---|
189 | |
---|
190 | if (!$content) return; |
---|
191 | |
---|
192 | return |
---|
193 | '<div class="rankkutrlwidget">'. |
---|
194 | ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : ''). |
---|
195 | '<ul>'.$content.'</ul>'. |
---|
196 | '</div>'; |
---|
197 | } |
---|
198 | } |
---|
199 | ?> |
---|