PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.9.1
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.9.1
2.11.0 2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 All 78 releases
fluent-community / app / Hooks / Handlers / RateLimitHandler.php

RateLimitHandler.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.9.1, at app/Hooks/Handlers/RateLimitHandler.php

98 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\App\Hooks\Handlers;
4
5 use FluentCommunity\App\Models\Comment;
6 use FluentCommunity\App\Models\Feed;
7 use FluentCommunity\App\Models\Media;
8 use FluentCommunity\App\Models\User;
9 use FluentCommunity\App\Services\Helper;
10
11 class RateLimitHandler
12 {
13 public function register()
14 {
15 add_action('fluent_community/check_rate_limit/create_post', [$this, 'maybeLimitPost'], 10, 1);
16 add_action('fluent_community/check_rate_limit/create_comment', [$this, 'maybeLimitComment'], 10, 1);
17 add_action('fluent_community/check_rate_limit/media_upload', [$this, 'maybeLimitMediaUpload'], 10, 1);
18 add_action('fluent_community/check_rate_limit/oembed', [$this, 'maybeLimitOembed'], 10, 1);
19 }
20
21 public function maybeLimitPost(User $user)
22 {
23 if (Helper::isSiteAdmin($user->ID, $user)) {
24 return;
25 }
26
27 // Check how many posts user has created in last 5 minutes
28 $postsCount = Feed::query()->withoutGlobalScopes()->where('user_id', $user->ID)
29 ->where('created_at', '>', gmdate('Y-m-d H:i:s', current_time('timestamp') - 300))
30 ->count();
31
32 $limitPer5Minutes = apply_filters('fluent_community/rate_limit/posts_per_5_minutes', 5);
33
34 if ($postsCount >= $limitPer5Minutes) {
35 throw new \Exception(esc_html__('You have reached the limit of posting. Please try after some time', 'fluent-community'));
36 }
37 }
38
39 public function maybeLimitComment(User $user)
40 {
41 if (Helper::isSiteAdmin($user->ID, $user)) {
42 return;
43 }
44
45 // Check how many comments user has created in last 1 minute
46 $commentsCount = Comment::query()->withoutGlobalScopes()->where('user_id', $user->ID)
47 ->where('created_at', '>', gmdate('Y-m-d H:i:s', current_time('timestamp') - 60))
48 ->count();
49
50 $limitPerMinute = apply_filters('fluent_community/rate_limit/comments_per_minute', 5);
51
52 if ($commentsCount >= $limitPerMinute) {
53 throw new \Exception(esc_html__('You have reached the limit of commenting. Please try after some time', 'fluent-community'));
54 }
55 }
56
57 public function maybeLimitMediaUpload(User $user)
58 {
59 if (Helper::isSiteAdmin($user->ID, $user)) {
60 return;
61 }
62
63 // Check how many media items user has uploaded in last 1 minute
64 $mediaCount = Media::query()->withoutGlobalScopes()->where('user_id', $user->ID)
65 ->where('created_at', '>', gmdate('Y-m-d H:i:s', current_time('timestamp') - 60))
66 ->count();
67
68 $limitPerMinute = apply_filters('fluent_community/rate_limit/media_upload_per_minute', 10);
69
70 if ($mediaCount >= $limitPerMinute) {
71 throw new \Exception(esc_html__('You have reached the limit of media uploads. Please try after some time', 'fluent-community'));
72 }
73 }
74
75 public function maybeLimitOembed(User $user)
76 {
77 if (Helper::isSiteAdmin($user->ID, $user)) {
78 return;
79 }
80
81 $limitPerMinute = apply_filters('fluent_community/rate_limit/oembed_per_minute', 20);
82
83 if (wp_using_ext_object_cache()) {
84 $cacheKey = 'oembed_rate_limit_' . $user->ID;
85 wp_cache_add($cacheKey, 0, 'fluent-community', MINUTE_IN_SECONDS);
86 $previewCount = (int) wp_cache_incr($cacheKey, 1, 'fluent-community');
87 } else {
88 $transientKey = 'fcom_oembed_rate_limit_' . $user->ID;
89 $previewCount = (int) get_transient($transientKey) + 1;
90 set_transient($transientKey, $previewCount, MINUTE_IN_SECONDS);
91 }
92
93 if ($previewCount > $limitPerMinute) {
94 throw new \Exception(esc_html__('You have reached the limit of link previews. Please try after some time', 'fluent-community'));
95 }
96 }
97 }
98