class.swpm-access-control.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. class SwpmAccessControl {
  3. private $lastError;
  4. private $moretags;
  5. private static $_this;
  6. private function __construct(){
  7. $this->lastError = '';
  8. $this->moretags = array();
  9. }
  10. public static function get_instance(){
  11. self::$_this = empty(self::$_this)? new SwpmAccessControl():self::$_this;
  12. return self::$_this;
  13. }
  14. public function can_i_read_post($post){
  15. if (!is_a($post, 'WP_Post')) {
  16. //This is not a WP_Post object. So we don't want to handle it in our plugin.
  17. return true;
  18. }
  19. $id = $post->ID;
  20. $this->lastError = '';
  21. $auth = SwpmAuth::get_instance();
  22. //$protect_everything = SwpmSettings::get_instance()->get_value('protect-everything');
  23. //if(!empty($protect_everything)){
  24. //Protect everything is enabled.
  25. //TODO - This feature is not implemented yet.
  26. //}
  27. //Check if this is a protected post.
  28. $protected = SwpmProtection::get_instance();
  29. if (!$protected->is_protected($id)){
  30. //This is a totally unprotected post. So everyone has access to it.
  31. return true;
  32. }
  33. /*** At this point, we have a protected post. So we need to check if this user can view this post. ***/
  34. //Check if the user is logged in.
  35. if(!$auth->is_logged_in()){
  36. //This user is not logged into the site. No access to this protected post.
  37. $text = SwpmUtils::_('You need to login to view this content. ') . SwpmMiscUtils::get_login_link();
  38. $error_msg = '<div class="swpm-post-not-logged-in-msg">'.$text.'</div>';
  39. $this->lastError = apply_filters('swpm_not_logged_in_post_msg', $error_msg);
  40. return false;
  41. }
  42. //Check if the account is expired
  43. if ($auth->is_expired_account()){
  44. //This user's account is expired. No access to this post. Show account expiry message.
  45. $text = SwpmUtils::_('Your account has expired. ') . SwpmMiscUtils::get_renewal_link();
  46. $error_msg = '<div class="swpm-post-account-expired-msg swpm-yellow-box">'.$text.'</div>';
  47. $this->lastError = apply_filters('swpm_account_expired_msg', $error_msg);
  48. return false;
  49. }
  50. //Check older post protection addon settings (if being used on this site).
  51. $protect_older_posts = apply_filters('swpm_should_protect_older_post', false, $id);
  52. if ($protect_older_posts){
  53. //This post falls under the older post protection condition. No access to it.
  54. $text = SwpmUtils::_('This content can only be viewed by members who joined on or before ') . SwpmUtils::get_formatted_and_translated_date_according_to_wp_settings($post->post_date);
  55. $error_msg = '<div class="swpm-post-older-post-msg">'.$text.'</div>';
  56. $this->lastError = apply_filters ('swpm_restricted_post_msg_older_post', $error_msg);
  57. return false;
  58. }
  59. //Check if this user's membership level has access to this post
  60. $permission = SwpmPermission::get_instance($auth->get('membership_level'));
  61. if($permission->is_permitted($id)) {
  62. //This user's membership level has access to it. Show this post to this user.
  63. return true;
  64. } else {
  65. //User's level DOES NOT have access to this post.
  66. $text = SwpmUtils::_('This content is not permitted for your membership level.');
  67. $error_msg = '<div class="swpm-post-no-access-msg">'.$text.'</div>';
  68. $this->lastError = apply_filters ('swpm_restricted_post_msg', $error_msg);
  69. return false;
  70. }
  71. //All checks have passed. Show this post to the user.
  72. return true;
  73. }
  74. public function can_i_read_comment($comment){
  75. if (!is_a($comment, 'WP_Comment')) {
  76. //This is not a valid WP_Comment object. So we don't want to handle it in our plugin.
  77. return true;
  78. }
  79. $id = $comment->comment_ID;
  80. $post_id = $comment->comment_post_ID;
  81. $post = get_post($post_id);
  82. $this->lastError = '';
  83. $auth = SwpmAuth::get_instance();
  84. //Check if everything protected settings is on.
  85. //$protect_everything = SwpmSettings::get_instance()->get_value('protect-everything');
  86. //if(!empty($protect_everything)){
  87. //Everything is protected by default.
  88. //TODO - This feature is currently not implemented.
  89. //}
  90. //Check if the post (that this comment belongs to) is protected.
  91. $protected = SwpmProtection::get_instance();
  92. if (!$protected->is_protected($post_id)){
  93. //The post of this comment is not protected. So this is an unprotected comment. Show it to everyone.
  94. return true;
  95. }
  96. /*** At this point, we have a protected comment. So we need to check if this user can view this comment. ***/
  97. //Check if the user is logged-in as a member.
  98. if(!$auth->is_logged_in()){
  99. //User is not logged-in. Not allowed to see this protected comment.
  100. $error_msg = '<div class="swpm-comment-not-logged-in">' . SwpmUtils::_("You need to login to view this content. ") . '</div>';
  101. $this->lastError = apply_filters('swpm_not_logged_in_comment_msg', $error_msg);
  102. return false;
  103. }
  104. //Check if member account is expired.
  105. if ($auth->is_expired_account()){
  106. //This user's account is expired. Not allowed to see this comment. Show account expiry notice also.
  107. $text = SwpmUtils::_('Your account has expired. ') . SwpmMiscUtils::get_renewal_link();
  108. $error_msg = '<div class="swpm-comment-account-expired-msg swpm-yellow-box">'.$text.'</div>';
  109. $this->lastError = apply_filters('swpm_account_expired_msg', $error_msg);
  110. return false;
  111. }
  112. //Check if older post protection addon is active and protection according to it's settings.
  113. $protect_older_posts = apply_filters('swpm_should_protect_older_post', false, $post_id);
  114. if ($protect_older_posts){
  115. //This comment is protected due to the older post protection addon settings configuration.
  116. $text = SwpmUtils::_('This content can only be viewed by members who joined on or before ') . SwpmUtils::get_formatted_and_translated_date_according_to_wp_settings($post->post_date);
  117. $error_msg = '<div class="swpm-comment-older-post-msg">'.$text.'</div>';
  118. $this->lastError = apply_filters ('swpm_restricted_comment_older_post', $error_msg);
  119. return false;
  120. }
  121. //Check if this member can view this comment based on his membership level
  122. $permission = SwpmPermission::get_instance($auth->get('membership_level'));
  123. if(!$permission->is_permitted($post_id)) {
  124. //This member's membership level doesn't have access to this comment's post. Not allowed to see this comment.
  125. $error_msg = '<div class="swpm-comment-no-access-msg">' . SwpmUtils::_('This content is not permitted for your membership level.').'</div>';
  126. $this->lastError = apply_filters ('swpm_restricted_comment_msg', $error_msg);
  127. return false;
  128. }
  129. //All checks have passed at this stage. Show this comment to this user.
  130. return true;
  131. }
  132. public function filter_post($post,$content){
  133. if (!is_a($post, 'WP_Post')) {
  134. //This is not a WP_Post object. So we don't want to handle it in our plugin.
  135. return $content;
  136. //return SwpmUtils::_('Error! $post is not a valid WP_Post object.');
  137. }
  138. if(self::expired_user_has_access_to_this_page()) {
  139. return $content;//An expired user is viewing this page and it is a system page, so allow access.
  140. }
  141. if(SwpmUtils::is_first_click_free($content)) {
  142. return $content;//First click free is true, so allow access.
  143. }
  144. if($this->can_i_read_post($post)) {
  145. return $content;//This member has access to this post, so allow access.
  146. }
  147. //Check and apply more tag protection.
  148. $more_tag_protection_value = $this->check_and_apply_more_tag_protection($post, $content);
  149. if(!empty($more_tag_protection_value)){
  150. //More tag protection was found in the post. Return the modified $content.
  151. return $more_tag_protection_value;
  152. }
  153. //Return whatever the result is from calling the earlier protection check functions.
  154. return $this->lastError;
  155. }
  156. public function check_and_apply_more_tag_protection($post, $content){
  157. //More tag protection is checked after all the OTHER protections have alrady been checked.
  158. //So if a valid logged-in member is accessing a post he has access to then this code won't execute.
  159. //Check if more tag protection is enabled.
  160. $moretag = SwpmSettings::get_instance()->get_value('enable-moretag');
  161. if (empty($moretag)){
  162. //More tag protection is disabled in this site. So return empty string.
  163. return '';
  164. } else {
  165. //More tag protection is enabled in this site. Need to check the post segments to see if there is content after more tag.
  166. $post_segments = explode( '<!--more-->', $post->post_content);
  167. if (count($post_segments) >= 2){
  168. //There is content after the more tag.
  169. $auth = SwpmAuth::get_instance();
  170. if(!$auth->is_logged_in()){
  171. //User is not logged-in. Need to show the login message after the more tag.
  172. $text = SwpmUtils::_("You need to login to view the rest of the content. ") . SwpmMiscUtils::get_login_link();
  173. $error_msg = '<div class="swpm-more-tag-not-logged-in swpm-margin-top-10">' . $text . '</div>';
  174. $more_tag_check_msg = apply_filters('swpm_not_logged_in_more_tag_msg', $error_msg);
  175. } else {
  176. //The user is logged in.
  177. //Lets check if the user's account is expired.
  178. if ($auth->is_expired_account()){
  179. //This user's account is expired. Not allowed to see this post. Show account expiry notice also.
  180. $text = SwpmUtils::_('Your account has expired. ') . SwpmMiscUtils::get_renewal_link();
  181. $error_msg = '<div class="swpm-more-tag-account-expired-msg swpm-yellow-box">'.$text.'</div>';
  182. $more_tag_check_msg = apply_filters('swpm_account_expired_more_tag_msg', $error_msg);
  183. } else {
  184. //At this stage, the user does not have permission to view the content after the more tag.
  185. $text = SwpmUtils::_(" The rest of the content is not permitted for your membership level.");
  186. $error_msg = '<div class="swpm-more-tag-restricted-msg swpm-margin-top-10">' . $text . '</div>';
  187. $more_tag_check_msg = apply_filters ('swpm_restricted_more_tag_msg', $error_msg);
  188. }
  189. }
  190. $filtered_before_more_content = SwpmMiscUtils::format_raw_content_for_front_end_display($post_segments[0]);
  191. $new_post_content = $filtered_before_more_content . $more_tag_check_msg;
  192. return $new_post_content;
  193. }//End of segment count condition check.
  194. }//End of more tag enabled condition check.
  195. //More tag protection not applicable for this post. Return empty string.
  196. return '';
  197. }
  198. public function filter_comment($comment, $content){
  199. if($this->can_i_read_comment($comment)) {
  200. //This user has access to this comment.
  201. return $content;
  202. }
  203. return $this->lastError;
  204. }
  205. public function why(){
  206. return $this->lastError;
  207. }
  208. /*
  209. * This function checks if the current user is an expired user and has access to the system page content (if the current URL is a system page).
  210. */
  211. public static function expired_user_has_access_to_this_page(){
  212. $auth = SwpmAuth::get_instance();
  213. //Check if the user is logged-into the site.
  214. if(!$auth->is_logged_in()){
  215. //Anonymous user. No access. No need to check anything else.
  216. return false;
  217. }
  218. //Check if account is expired.
  219. if (!$auth->is_expired_account()){
  220. //This users account is not expired. No need to check anything else.
  221. return false;
  222. }
  223. /*** We have a expired member. Lets check if he is viewing a page that is a core system used URL. ***/
  224. if (self::is_current_url_a_system_page()){
  225. //Allow this expired user to view this post/page content since this is a core system page.
  226. return true;
  227. }
  228. //Not a system used page. So the expired user has no access to this page.
  229. return false;
  230. }
  231. /*
  232. * This function checks if the current page being viewed is one of the system used URLs
  233. */
  234. public static function is_current_url_a_system_page(){
  235. $current_page_url = SwpmMiscUtils::get_current_page_url();
  236. //Check if the current page is the membership renewal page.
  237. $renewal_url = SwpmSettings::get_instance()->get_value('renewal-page-url');
  238. if (empty($renewal_url)) {return false;}
  239. if (SwpmMiscUtils::compare_url($renewal_url, $current_page_url)) {return true;}
  240. //Check if the current page is the membership logn page.
  241. $login_page_url = SwpmSettings::get_instance()->get_value('login-page-url');
  242. if (empty($login_page_url)) {return false;}
  243. if (SwpmMiscUtils::compare_url($login_page_url, $current_page_url)) {return true;}
  244. //Check if the current page is the membership join page.
  245. $registration_page_url = SwpmSettings::get_instance()->get_value('registration-page-url');
  246. if (empty($registration_page_url)) {return false;}
  247. if (SwpmMiscUtils::compare_url($registration_page_url, $current_page_url)) {return true;}
  248. return false;
  249. }
  250. }