PluginProbe
wpForo Forum / 2.4.2
wpForo Forum v2.4.2
3.1.5 3.1.4 3.1.2 3.1.1 3.1.0 3.0.9 3.0.8 3.0.7 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 All 137 releases
wpforo / classes / Logs.php

Logs.php in wpForo Forum 2.4.2, at classes/Logs.php

404 lines 14.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace wpforo\classes;
4
5 // Exit if accessed directly
6 if( ! defined( 'ABSPATH' ) ) exit;
7
8 class Logs {
9 public function read() {
10 $data = WPF()->current_object;
11
12 if( wpforo_setting( 'logging', 'view_logging' ) && wpfval( $data, 'template' ) ) {
13 if( $data['template'] === 'post' ) {
14 if( wpfval( $data, 'topicid' ) && wpfval( $data, 'topic', 'last_post' ) ) {
15 $this->read_item( $data['topicid'], $data['topic']['last_post'], wpforo_prefix( 'read_topics' ) );
16 }
17 if( wpfval( $data, 'forumid' ) ) {
18 $end_date = time() - ( 14 * 24 * 60 * 60 );
19 $args = [ 'read' => false, 'forumid' => $data['forumid'], 'orderby' => 'modified', 'order' => 'ASC', 'row_count' => 1, 'where' => "`modified` > '" . gmdate( 'Y-m-d H:i:s', $end_date ) . "'" ];
20 $topics = WPF()->topic->get_topics( $args, $items_count );
21 if( empty( $topics ) ) {
22 if( wpfval( $data, 'forum', 'last_postid' ) ) {
23 $last_postid = $data['forum']['last_postid'];
24 } elseif( wpfval( $data, 'topic', 'last_post' ) ) {
25 $last_postid = $data['topic']['last_post'];
26 }
27 if( ! empty( $last_postid ) ) $this->read_item( $data['forumid'], $last_postid, wpforo_prefix( 'read_forums' ) );
28 } else {
29 foreach( $topics as $topic ) {
30 if( wpfval( $topic, 'last_post' ) ) {
31 if( $items_count == 1 && $topic['topicid'] == $data['topicid'] ) {
32 if( wpfval( $data, 'forum', 'last_postid' ) ) {
33 $last_postid = $data['forum']['last_postid'];
34 } elseif( wpfval( $data, 'topic', 'last_post' ) ) {
35 $last_postid = $data['topic']['last_post'];
36 } elseif( wpfval( $topic, 'last_post' ) ) {
37 $last_postid = $topic['last_post'];
38 }
39 } else {
40 $last_postid = ( $topic['last_post'] - 1 );
41 }
42 if( isset( $last_postid ) ) {
43 $this->read_item( $data['forumid'], $last_postid, wpforo_prefix( 'read_forums' ) );
44 break;
45 }
46 }
47 }
48 }
49 }
50 }
51 }
52 }
53
54 public function read_item( $itemid, $item_last_postid, $key ) {
55
56 $log = false;
57 $logdb = false;
58 $read_ids = [];
59 $read_db_ids = [];
60 $login = is_user_logged_in();
61
62 if( $itemid && $key ) {
63 if( wpforo_setting( 'legal', 'cookies' ) ) $read_ids = wpforo_getcookie( $key, false );
64 if( wpfval( WPF()->current_usermeta, $key ) ) $read_db_ids = wpforo_current_usermeta( $key );
65 if( ! $read_ids || ! is_array( $read_ids ) ) $read_ids = [];
66 if( ! $read_db_ids || ! is_array( $read_ids ) ) $read_db_ids = [];
67
68 if( empty( $read_ids ) || ( $login && empty( $read_db_ids ) ) ) {
69 if( empty( $read_ids ) ) $log = true;
70 if( empty( $read_db_ids ) && $login ) $logdb = true;
71 $read_ids = [ $itemid => $item_last_postid ];
72 } elseif( ! wpfval( $read_ids, $itemid ) || ! wpfval( $read_db_ids, $itemid ) || (int) $read_ids[ $itemid ] < (int) $item_last_postid || (int) $read_db_ids[ $itemid ] < (int) $item_last_postid ) {
73 $log = true;
74 if( $login ) $logdb = true;
75 if( is_array( $read_ids ) && is_array( $read_db_ids ) ) {
76 $keep_guest_read = apply_filters( 'wpforo_keep_guest_read', false );
77 $merge_guest_read = apply_filters( 'wpforo_merge_guest_read', true );
78 if( $merge_guest_read ) {
79 $read_ids = ( $keep_guest_read ) ? $read_ids + $read_db_ids : $read_db_ids + $read_ids;
80 } else {
81 $read_ids = ( $login ) ? $read_db_ids : $read_ids;
82 }
83 }
84 $read_ids[ $itemid ] = $item_last_postid;
85 if( $key === wpforo_prefix( 'read_forums' ) ) {
86 $read_ids = $this->read_parent_items( $read_ids, $itemid, $item_last_postid );
87 }
88 }
89 $this->log_item( $read_ids, $key, $logdb, $log );
90 }
91 }
92
93 public function read_parent_items( $read_ids = [], $itemid = 0, $item_last_postid = 0 ) {
94 if( wpfval( $read_ids, $itemid ) && $item_last_postid ) {
95 WPF()->forum->get_parents( $itemid, $parents );
96 foreach( $parents as $parent ) {
97 $read_ids[ $parent ] = $item_last_postid;
98 }
99 }
100
101 return $read_ids;
102 }
103
104 public function log_item( $read_ids, $key, $logdb = true, $log = true ) {
105 if( $log && wpforo_setting( 'legal', 'cookies' ) ) {
106 wpforo_setcookie( $key, $read_ids, false );
107 }
108 if( $logdb && is_user_logged_in() ) {
109 $max = apply_filters( 'wpforo_max_logged_topics', 100 );
110 $num = count( $read_ids );
111 if( $num > $max ) {
112 $delta = $num - $max;
113 if( $delta > 0 ) $read_ids = array_slice( $read_ids, $delta, null, true );
114 }
115 update_user_meta( WPF()->current_userid, $key, $read_ids );
116 }
117 }
118
119 public function unread( $id, $in = 'forum', $postid = 0 ) {
120
121 $new = false;
122 if( ! wpforo_setting( 'logging', 'view_logging' ) ) return false;
123
124 if( $id ) {
125 if( $in === 'forum' ) {
126 $last_postid = wpforo_forum( $id, 'last_postid' );
127 if( ! $last_postid ) {
128 return false;
129 }
130 //Pass all posts created before "Mark all read" action
131 $last_read_postid = $this->get_all_read( 'post' );
132 if( $last_read_postid ) {
133 if( (int) $last_postid > $last_read_postid ) $new = true;
134 }
135 //Check the last read post of current forum
136 if( ! $last_read_postid || $new ) {
137 $read_forums = $this->get_read_forums();
138 if( wpfkey( $read_forums, $id ) ) {
139 $last_read_postid = $read_forums[ $id ];
140 if( (int) $last_postid > (int) $last_read_postid ) {
141 $new = true;
142 } else {
143 $new = false;
144 }
145 } else {
146 $new = true;
147 }
148 }
149 $new = apply_filters( 'wpforo_new_in_forum', $new, $id );
150 } elseif( $in == 'topic' || $in == 'post' ) {
151 $last_postid = wpforo_topic( $id, 'last_post' );
152 if( ! $last_postid ) {
153 return false;
154 }
155 //Pass all posts created before "Mark all read" action
156 $last_read_postid = $this->get_all_read( 'post' );
157 if( $last_read_postid ) {
158 if( (int) $last_postid > $last_read_postid ) $new = true;
159 }
160 //Check the last read post of current forum
161 if( ! $last_read_postid || $new ) {
162 $read_topics = $this->get_read_topics();
163 if( wpfkey( $read_topics, $id ) ) {
164 $last_read_postid = $read_topics[ $id ];
165 if( (int) $last_postid > (int) $last_read_postid ) {
166 $new = true;
167 } else {
168 $new = false;
169 }
170 } else {
171 $new = true;
172 }
173 }
174 //Check an individual post if it's unread in an unread topic
175 if( $in === 'post' && $postid && (int) $postid > (int) $last_read_postid ) $new = true;
176 $new = apply_filters( 'wpforo_new_in_topic', $new, $id );
177 }
178 }
179
180 return $new;
181 }
182
183 public function get_read( $return = 'topicid' ) {
184 $topic_ids = [];
185 $read_topics = $this->get_read_topics();
186 if( ! empty( $read_topics ) && is_array( $read_topics ) ) {
187 $last_read_postid = $this->get_all_read( 'post' );
188 foreach( $read_topics as $topicid => $postid ) {
189 if( $last_read_postid && (int) $postid <= $last_read_postid ) {
190 unset( $read_topics[ $topicid ] );
191 } else {
192 $current_last_postid = wpforo_topic( $topicid, 'last_post' );
193 if( $current_last_postid ) {
194 if( (int) $current_last_postid <= (int) $postid ) {
195 $topic_ids[] = $topicid;
196 }
197 } else {
198 $topic_ids[] = $topicid;
199 }
200 }
201 }
202 }
203 if( $return == 'topicid' ) {
204 return $topic_ids;
205 } else {
206 return $read_topics;
207 }
208 }
209
210 public function get_read_forums() {
211 if( is_user_logged_in() && wpfval( WPF()->current_usermeta, wpforo_prefix( 'read_forums' ) ) ) {
212 $read_forums = wpforo_current_usermeta( wpforo_prefix( 'read_forums' ) );
213 } else {
214 $read_forums = wpforo_getcookie( wpforo_prefix( 'read_forums' ), false );
215 }
216
217 return $read_forums;
218 }
219
220 public function get_read_topics() {
221 if( is_user_logged_in() && wpfval( WPF()->current_usermeta, wpforo_prefix( 'read_topics' ) ) ) {
222 $read_topics = wpforo_current_usermeta( wpforo_prefix( 'read_topics' ) );
223 } else {
224 $read_topics = wpforo_getcookie( wpforo_prefix( 'read_topics' ), false );
225 }
226
227 return $read_topics;
228 }
229
230 public function last( $item ) {
231 $id = 0;
232
233 if( $item === 'forum' ) {
234 $id = WPF()->db->get_var( "SELECT MAX(`forumid`) FROM " . WPF()->tables->forums );
235 } elseif( $item === 'topic' ) {
236 $id = WPF()->db->get_var( "SELECT MAX(`topicid`) FROM " . WPF()->tables->topics );
237 } elseif( $item === 'post' ) {
238 $id = WPF()->db->get_var( "SELECT MAX(`postid`) FROM " . WPF()->tables->posts );
239 }
240
241 return intval( $id );
242 }
243
244 public function mark_all_read() {
245 $last_forumid = $this->last( 'forum' );
246 $last_topicid = $this->last( 'topic' );
247 $last_postid = $this->last( 'post' );
248 $last = [ 'forum' => $last_forumid, 'topic' => $last_topicid, 'post' => $last_postid ];
249 if( is_user_logged_in() ) {
250 update_user_meta( WPF()->current_userid, wpforo_prefix( 'all_read' ), $last );
251 update_user_meta( WPF()->current_userid, wpforo_prefix( 'read_forums' ), [] );
252 update_user_meta( WPF()->current_userid, wpforo_prefix( 'read_topics' ), [] );
253 }
254 wpforo_setcookie( wpforo_prefix( 'all_read' ), $last );
255 wpforo_setcookie( wpforo_prefix( 'read_forums' ) );
256 wpforo_setcookie( wpforo_prefix( 'read_topics' ) );
257 }
258
259 public function get_all_read( $item ) {
260 if( is_user_logged_in() ) {
261 $last = wpforo_current_usermeta( wpforo_prefix( 'all_read' ) );
262 } else {
263 $last = wpforo_getcookie( wpforo_prefix( 'all_read' ) );
264 }
265 if( $item && wpfval( $last, $item ) ) {
266 return intval( $last[ $item ] );
267 }
268
269 return 0;
270 }
271
272 public function visitors( $item ) {
273 $data = [];
274 $visitors = [];
275 if( wpfval( $item, 'topicid' ) ) {
276 $keep_vistors_data = apply_filters( 'wpforo_keep_visitors_data', 4000 );
277 $time = (int) time() - (int) $keep_vistors_data;
278 $visitors = WPF()->db->get_results( "SELECT * FROM `" . WPF()->tables->visits . "` WHERE `topicid` = " . intval( $item['topicid'] ) . " AND `time` > " . $time . " ORDER BY `id` DESC", ARRAY_A );
279 } elseif( wpfval( $item, 'forumid' ) ) {
280 $keep_vistors_data = apply_filters( 'wpforo_keep_visitors_data', 4000 );
281 $time = (int) time() - (int) $keep_vistors_data;
282 $visitors = WPF()->db->get_results( "SELECT * FROM `" . WPF()->tables->visits . "` WHERE `forumid` = " . intval( $item['forumid'] ) . " AND `time` > " . $time . " ORDER BY `id` DESC", ARRAY_A );
283 }
284 if( ! empty( $visitors ) ) {
285 $online_period = (int) time() - (int) wpforo_setting( 'profiles', 'online_status_timeout' );
286 foreach( $visitors as $visitor ) {
287 if( wpfval( $visitor, 'userid' ) ) {
288 if( (int) $visitor['time'] < $online_period ) {
289 $data['users']['viewed'][] = $visitor;
290 } else {
291 $gone = WPF()->db->get_var( "SELECT `id` FROM `" . WPF()->tables->visits . "` WHERE `userid` = " . intval( $visitor['userid'] ) . " AND `time` > " . intval( $visitor['time'] ) . " LIMIT 1" );
292 if( $gone ) {
293 $data['users']['viewed'][] = $visitor;
294 } else {
295 $data['users']['viewing'][] = $visitor;
296 }
297 }
298 } elseif( (int) $visitor['time'] > $online_period ) {
299 $data['guests'][] = $visitor;
300 }
301 }
302 }
303
304 return $data;
305 }
306
307 public function visit() {
308 if( wpforo_setting( 'logging', 'track_logging' ) ) {
309 if( ( wpforo_current_user_is( 'admin' ) || WPF()->current_user_groupid == 1 ) && ! wpforo_setting( 'logging', 'display_admin_viewers' ) ) {
310 return false;
311 }
312 $data = WPF()->current_object;
313 $visitor = WPF()->current_user;
314 $template = ( wpfval( $data, 'template' ) ) ? $data['template'] : '';
315 $templates = [ 'forum', 'topic', 'post' ];
316 $templates = apply_filters( 'wpforo_track_visitors_in_pages', $templates );
317 if( $template ) {
318 if( $template == 'post' && in_array( $template, $templates ) ) {
319 //topic page (post list)
320 if( wpfval( $data, 'topic', 'topicid' ) && wpfval( $data, 'topic', 'forumid' ) ) {
321 $forumid = intval( $data['topic']['forumid'] );
322 $topicid = intval( $data['topic']['topicid'] );
323 $this->add_visit( $visitor, $forumid, $topicid );
324 }
325 } elseif( $template == 'topic' && in_array( $template, $templates ) ) {
326 //forum page (topic list)
327 if( wpfval( $data, 'forum', 'forumid' ) ) {
328 $forumid = intval( $data['forum']['forumid'] );
329 $this->add_visit( $visitor, $forumid );
330 }
331 } elseif( $template == 'forum' && in_array( $template, $templates ) ) {
332 //forum home page
333 $this->add_visit( $visitor );
334 } elseif( in_array( $template, $templates ) ) {
335 //other pages (profile, search, members, etc..)
336 $this->add_visit( $visitor );
337 }
338 }
339 }
340 return false;
341 }
342
343 public function add_visit( $visitor, $forumid = 0, $topicid = 0 ) {
344 $time = esc_sql( time() );
345 if( WPF()->current_userid ) {
346 $userid = ( wpfval( $visitor, 'userid' ) ) ? intval( $visitor['userid'] ) : 0;
347 $name = ( wpfval( $visitor, 'display_name' ) ) ? esc_sql( $visitor['display_name'] ) : '';
348 WPF()->db->query( "INSERT INTO `" . WPF()->tables->visits . "` (`userid`, `name`, `time`, `topicid`, `forumid`) VALUES( " . $userid . ", '" . $name . "', '" . $time . "', " . $topicid . ", " . $forumid . " ) ON DUPLICATE KEY UPDATE `time` = '" . $time . "'" );
349 } elseif( ! wpforo_is_bot() ) {
350 $ip = ( wpfval( $_SERVER, 'REMOTE_ADDR' ) ) ? substr( md5( (string) $_SERVER['REMOTE_ADDR'] ), 0, 32 ) : '-noip-';
351 if( $ip ) {
352 $guest = WPF()->db->get_var( "SELECT `id` FROM `" . WPF()->tables->visits . "` WHERE `ip` = '" . esc_sql( $ip ) . "' LIMIT 1" );
353 if( $guest ) {
354 WPF()->db->query( "UPDATE `" . WPF()->tables->visits . "` SET `topicid` = " . intval( $topicid ) . ", `forumid` = " . intval( $forumid ) . ", `time` = '" . $time . "' WHERE `id` = " . intval( $guest ) );
355 } else {
356 WPF()->db->query( "INSERT IGNORE INTO `" . WPF()->tables->visits . "` (`ip`, `time`, `topicid`, `forumid`) VALUES( '" . esc_sql( $ip ) . "', '" . $time . "', " . $topicid . ", " . $forumid . " )" );
357 }
358 }
359 }
360 }
361
362 /**
363 * do not use, this logic is not tested yet.
364 *
365 * @param int $forumid
366 *
367 * @return int
368 */
369 public function get_forum_unreads_count( $forumid ) {
370 $forumid = intval( $forumid );
371 $read_forums = $this->get_read_forums();
372 $last_read_postid = (int) wpfval( $read_forums, $forumid );
373 $sql = "SELECT SQL_NO_CACHE COUNT(*) FROM `" . WPF()->tables->posts . "`
374 WHERE `status` = 0
375 AND `private` = 0
376 AND `forumid` = $forumid
377 AND `postid` > $last_read_postid";
378 $count = (int) WPF()->db->get_var( $sql );
379
380 return (int) apply_filters( 'wpforo_logs_get_forum_unreads_count', $count, $forumid, $last_read_postid );
381 }
382
383 /**
384 * get unread posts count for topic by giving topicid
385 *
386 * @param int $topicid
387 *
388 * @return int
389 */
390 public function get_topic_unreads_count( $topicid ) {
391 $topicid = intval( $topicid );
392 $read_topics = $this->get_read_topics();
393 $last_read_postid = (int) wpfval( $read_topics, $topicid );
394 $sql = "SELECT SQL_NO_CACHE COUNT(*) FROM `" . WPF()->tables->posts . "`
395 WHERE `status` = 0
396 AND `topicid` = $topicid
397 AND `postid` > $last_read_postid";
398 $count = (int) WPF()->db->get_var( $sql );
399
400 return (int) apply_filters( 'wpforo_logs_get_topic_unreads_count', $count, $topicid, $last_read_postid );
401 }
402
403 }
404