1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # This file is part of referer, a plugin for Dotclear. |
---|
4 | # |
---|
5 | # Copyright (c) 2008-2010 Tomtom |
---|
6 | # http://blog.zenstyle.fr/ |
---|
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('publicBeforeDocument',array('refererBehaviors','addReferer')); |
---|
16 | |
---|
17 | class refererBehaviors |
---|
18 | { |
---|
19 | /* |
---|
20 | * Adds referers in settings |
---|
21 | */ |
---|
22 | public static function addReferer($core) |
---|
23 | { |
---|
24 | $last = unserialize($core->blog->settings->referer->last_referer); |
---|
25 | $top = unserialize($core->blog->settings->referer->top_referer); |
---|
26 | |
---|
27 | $ref = isset($_SERVER['HTTP_REFERER']) ? trim(html::escapeHTML($_SERVER['HTTP_REFERER'])) : ''; |
---|
28 | |
---|
29 | if (!empty($ref)) { |
---|
30 | $url = parse_url($ref); |
---|
31 | $domain = !empty($url['host']) ? $url['scheme'].'://'.$url['host'] : __('Direct entrance'); |
---|
32 | $ownurl = parse_url($core->blog->url); |
---|
33 | $owndomain = $ownurl['scheme'].'://'.$ownurl['host']; |
---|
34 | $time = time() + dt::getTimeOffset($core->blog->settings->system->blog_timezone); |
---|
35 | |
---|
36 | if ($owndomain == $domain) { |
---|
37 | return; |
---|
38 | } |
---|
39 | # Added last referer |
---|
40 | $new_last = array( |
---|
41 | 'domain' => $domain, |
---|
42 | 'url' => $ref, |
---|
43 | 'dt' => $time |
---|
44 | ); |
---|
45 | if (count($last) > 19) { |
---|
46 | unset($last[19]); |
---|
47 | } |
---|
48 | array_unshift($last,$new_last); |
---|
49 | |
---|
50 | # Added top referer |
---|
51 | $new_top = array( |
---|
52 | 'domain' => $domain, |
---|
53 | 'count' => 0, |
---|
54 | ); |
---|
55 | $find = false; |
---|
56 | foreach ($top as $k => $v) { |
---|
57 | if ($v['domain'] == $domain) { |
---|
58 | $top[$k]['count']++; |
---|
59 | $find = true; |
---|
60 | break; |
---|
61 | } |
---|
62 | } |
---|
63 | if (count($top) > 19 && !$find) { |
---|
64 | usort($top,'refererBehaviors::cmp'); |
---|
65 | unset($top[19]); |
---|
66 | } |
---|
67 | if (!$find) { |
---|
68 | array_unshift($top,$new_top); |
---|
69 | } |
---|
70 | |
---|
71 | $core->blog->settings->referer->put('last_referer',serialize($last)); |
---|
72 | $core->blog->settings->referer->put('top_referer',serialize($top)); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | public static function cmp($a,$b) |
---|
77 | { |
---|
78 | if ($a['count'] == $b['count']) { |
---|
79 | return 0; |
---|
80 | } |
---|
81 | return $a['count'] < $b['count'] ? -1 : 1; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | class refererPublic |
---|
86 | { |
---|
87 | /** |
---|
88 | * This function displays the referer widget |
---|
89 | * |
---|
90 | * @param w Widget object |
---|
91 | * |
---|
92 | * @return string |
---|
93 | */ |
---|
94 | public static function last($w) |
---|
95 | { |
---|
96 | global $core; |
---|
97 | |
---|
98 | if ($w->homeonly && $core->url->type != 'default') { |
---|
99 | return; |
---|
100 | } |
---|
101 | |
---|
102 | $limask = '<li>%s</li>'; |
---|
103 | $amask = '<a href="%1$s">%2$s</a>'; |
---|
104 | |
---|
105 | $title = strlen($w->title) > 0 ? '<h2>'.html::escapeHTML($w->title).'</h2>' : ''; |
---|
106 | |
---|
107 | $last = unserialize($core->blog->settings->referer->last_referer); |
---|
108 | |
---|
109 | if (count($last) === 0) { |
---|
110 | return; |
---|
111 | } |
---|
112 | |
---|
113 | $res = ''; |
---|
114 | |
---|
115 | foreach ($last as $k => $v) { |
---|
116 | if ($k < $w->numbertodisplay) { |
---|
117 | $link = sprintf($amask,$v['url'],$v['domain']); |
---|
118 | $res .= sprintf($limask,$link); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | return |
---|
123 | '<div id="last_referers">'. |
---|
124 | $title. |
---|
125 | (!empty($res) ? '<ul>'.$res.'</ul>' : ''). |
---|
126 | '</div>'; |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * This function displays the top referer widget |
---|
131 | * |
---|
132 | * @param w Widget object |
---|
133 | * |
---|
134 | * @return string |
---|
135 | */ |
---|
136 | public static function top($w) |
---|
137 | { |
---|
138 | global $core; |
---|
139 | |
---|
140 | if ($w->homeonly && $core->url->type != 'default') { |
---|
141 | return; |
---|
142 | } |
---|
143 | |
---|
144 | $limask = '<li>%s</li>'; |
---|
145 | $amask = '<a href="%1$s">%2$s</a>'; |
---|
146 | |
---|
147 | $title = strlen($w->title) > 0 ? '<h2>'.html::escapeHTML($w->title).'</h2>' : ''; |
---|
148 | |
---|
149 | $top = unserialize($core->blog->settings->referer->top_referer); |
---|
150 | |
---|
151 | if (count($top) === 0) { |
---|
152 | return; |
---|
153 | } |
---|
154 | |
---|
155 | $res = ''; |
---|
156 | |
---|
157 | foreach ($top as $k => $v) { |
---|
158 | if ($k < $w->numbertodisplay) { |
---|
159 | $link = sprintf($amask,$v['domain'],$v['domain']); |
---|
160 | $res .= sprintf($limask,$link.' - <em>'.$v['count'].' '.__('visit(s)').'</em>'); |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | return |
---|
165 | '<div id="top_referers">'. |
---|
166 | $title. |
---|
167 | (!empty($res) ? '<ul>'.$res.'</ul>' : ''). |
---|
168 | '</div>'; |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | ?> |
---|