/app_old/membershipincludes/classes/class.urlgroup.php

  1. <?php 
  2. if(!class_exists('M_Urlgroup')) { 
  3.  
  4. class M_Urlgroup { 
  5.  
  6. var $build = 1; 
  7.  
  8. var $db; 
  9. var $tables = array('membership_levels', 'membership_rules', 'subscriptions', 'subscriptions_levels', 'membership_relationships', 'membermeta', 'communications', 'urlgroups'); 
  10.  
  11. var $membership_levels; 
  12. var $membership_rules; 
  13. var $membership_relationships; 
  14. var $subscriptions; 
  15. var $subscriptions_levels; 
  16. var $membermeta; 
  17. var $communications; 
  18. var $urlgroups; 
  19.  
  20. // if the data needs reloaded, or hasn't been loaded yet 
  21. var $dirty = true; 
  22.  
  23. var $group; 
  24.  
  25. function __construct( $id = false) { 
  26.  
  27. global $wpdb; 
  28.  
  29. $this->db =& $wpdb; 
  30.  
  31. foreach($this->tables as $table) { 
  32. $this->$table = membership_db_prefix($this->db, $table); 
  33.  
  34. $this->id = $id; 
  35.  
  36.  
  37. function M_Urlgroup( $id = false ) { 
  38. $this->__construct( $id ); 
  39.  
  40. function get_group() { 
  41. $sql = $this->db->prepare( "SELECT * FROM {$this->urlgroups} WHERE id = %d ", $this->id ); 
  42.  
  43. return $this->db->get_row( $sql ); 
  44.  
  45. function editform() { 
  46.  
  47. $this->group = $this->get_group(); 
  48.  
  49. echo '<table class="form-table">'; 
  50.  
  51. echo '<tr class="form-field form-required">'; 
  52. echo '<th style="" scope="row" valign="top">' . __('Group name', 'membership') . '</th>'; 
  53. echo '<td valign="top"><input name="groupname" type="text" size="50" title="' . __('Group name', 'membership') . '" style="width: 50%;" value="' . esc_attr(stripslashes($this->group->groupname)) . '" /></td>'; 
  54. echo '</tr>'; 
  55.  
  56. echo '<tr class="form-field form-required">'; 
  57. echo '<th style="" scope="row" valign="top">' . __('Page URLs', 'membership') . '</th>'; 
  58. echo '<td valign="top"><textarea name="groupurls" rows="15" cols="40">' . esc_html(stripslashes($this->group->groupurls)) . '</textarea>'; 
  59. // Display some instructions for the message. 
  60. echo "<br/><em style='font-size:smaller;'>" . __("You should place each page URL or expression on a new line.", 'membership') . "</em>"; 
  61. echo '</td>'; 
  62. echo '</tr>'; 
  63.  
  64. echo '<tr class="form-field form-required">'; 
  65. echo '<th style="" scope="row" valign="top">' . __('Strip query strings from URL', 'membership') . '</th>'; 
  66. echo '<td valign="top" align="left">'; 
  67. echo '<select name="stripquerystring">'; 
  68. echo '<option value="0"'; 
  69. if($this->group->stripquerystring == 0) echo ' selected="selected"'; 
  70. echo '>' . __('No', 'membership') . '</option>'; 
  71. echo '<option value="1"'; 
  72. if($this->group->stripquerystring == 1) echo ' selected="selected"'; 
  73. echo '>' . __('Yes', 'membership') . '</option>'; 
  74. echo '</select>'; 
  75. echo "<br/><em style='font-size:smaller;'>" . __("Remove any query string values prior to checking URL.", 'membership') . "</em>"; 
  76. echo '</td></tr>'; 
  77.  
  78. echo '<tr class="form-field form-required">'; 
  79. echo '<th style="" scope="row" valign="top">' . __('Regular Expression', 'membership') . '</th>'; 
  80. echo '<td valign="top" align="left">'; 
  81. echo '<select name="isregexp">'; 
  82. echo '<option value="0"'; 
  83. if($this->group->isregexp == 0) echo ' selected="selected"'; 
  84. echo '>' . __('No', 'membership') . '</option>'; 
  85. echo '<option value="1"'; 
  86. if($this->group->isregexp == 1) echo ' selected="selected"'; 
  87. echo '>' . __('Yes', 'membership') . '</option>'; 
  88. echo '</select>'; 
  89. echo "<br/><em style='font-size:smaller;'>" . __("If any of the page URLs are regular expressions then set this to yes.", 'membership') . "</em>"; 
  90. echo '</td></tr>'; 
  91.  
  92. echo '</table>'; 
  93.  
  94.  
  95. function addform() { 
  96.  
  97. echo '<table class="form-table">'; 
  98.  
  99. echo '<tr class="form-field form-required">'; 
  100. echo '<th style="" scope="row" valign="top">' . __('Group name', 'membership') . '</th>'; 
  101. echo '<td valign="top"><input name="groupname" type="text" size="50" title="' . __('Group name', 'membership') . '" style="width: 50%;" value="" /></td>'; 
  102. echo '</tr>'; 
  103.  
  104. echo '<tr class="form-field form-required">'; 
  105. echo '<th style="" scope="row" valign="top">' . __('Page URLs', 'membership') . '</th>'; 
  106. echo '<td valign="top"><textarea name="groupurls" rows="15" cols="40"></textarea>'; 
  107. // Display some instructions for the message. 
  108. echo "<br/><em style='font-size:smaller;'>" . __("You should place each page URL or expression on a new line.", 'membership') . "</em>"; 
  109. echo '</td>'; 
  110. echo '</tr>'; 
  111.  
  112. echo '<tr class="form-field form-required">'; 
  113. echo '<th style="" scope="row" valign="top">' . __('Strip query strings from URL', 'membership') . '</th>'; 
  114. echo '<td valign="top" align="left">'; 
  115. echo '<select name="stripquerystring">'; 
  116. echo '<option value="0"'; 
  117. echo '>' . __('No', 'membership') . '</option>'; 
  118. echo '<option value="1"'; 
  119. echo '>' . __('Yes', 'membership') . '</option>'; 
  120. echo '</select>'; 
  121. echo "<br/><em style='font-size:smaller;'>" . __("Remove any query string values prior to checking URL.", 'membership') . "</em>"; 
  122. echo '</td></tr>'; 
  123.  
  124. echo '<tr class="form-field form-required">'; 
  125. echo '<th style="" scope="row" valign="top">' . __('Regular Expression', 'membership') . '</th>'; 
  126. echo '<td valign="top" align="left">'; 
  127. echo '<select name="isregexp">'; 
  128. echo '<option value="0"'; 
  129. echo '>' . __('No', 'membership') . '</option>'; 
  130. echo '<option value="1"'; 
  131. echo '>' . __('Yes', 'membership') . '</option>'; 
  132. echo '</select>'; 
  133. echo "<br/><em style='font-size:smaller;'>" . __("If any of the page URLs are regular expressions then set this to yes.", 'membership') . "</em>"; 
  134. echo '</td></tr>'; 
  135.  
  136. echo '</table>'; 
  137.  
  138.  
  139. function add() { 
  140.  
  141. $insert = array( 
  142. "groupname" => $_POST['groupname'],  
  143. "groupurls" => $_POST['groupurls'],  
  144. "isregexp" => $_POST['isregexp'],  
  145. "stripquerystring" => $_POST['stripquerystring'] 
  146. ); 
  147.  
  148. return $this->db->insert( $this->urlgroups, $insert ); 
  149.  
  150.  
  151. function update() { 
  152.  
  153. $updates = array( 
  154. "groupname" => $_POST['groupname'],  
  155. "groupurls" => $_POST['groupurls'],  
  156. "isregexp" => $_POST['isregexp'],  
  157. "stripquerystring" => $_POST['stripquerystring'] 
  158. ); 
  159.  
  160. return $this->db->update( $this->urlgroups, $updates, array( "id" => $this->id) ); 
  161.  
  162.  
  163. function delete() { 
  164.  
  165. $sql = $this->db->prepare( "DELETE FROM {$this->urlgroups} WHERE id = %d", $this->id ); 
  166.  
  167. return $this->db->query( $sql ); 
  168.  
  169.  
  170. // processing 
  171. function url_matches( $host, $exclude = array() ) { 
  172.  
  173. $this->group = $this->get_group(); 
  174.  
  175. $groups = array_map('strtolower', array_map('trim', explode("\n", $this->group->groupurls))); 
  176.  
  177. if($this->group->stripquerystring == 1 && strpos($host, '?') !== false) { 
  178. $host = substr( $host, 0, strpos($host, '?')); 
  179.  
  180. if($this->group->isregexp == 0) { 
  181. // straight match 
  182. $newgroups = array_map('untrailingslashit', $groups); 
  183. $groups = array_merge($groups, $newgroups); 
  184. if(in_array( strtolower($host), $groups ) ) { 
  185. return true; 
  186. } else { 
  187. return false; 
  188. } else { 
  189. //reg expression match 
  190. $matchstring = ""; 
  191. foreach($groups as $key => $value) { 
  192. if($matchstring != "") $matchstring .= "|"; 
  193.  
  194. if( stripos($value, '\/') ) { 
  195. $matchstring .= stripcslashes($value); 
  196. } else { 
  197. $matchstring .= $value; 
  198.  
  199. // switched to using a character that won't be in a url as the start and end markers 
  200. $matchstring = "#" . $matchstring . "#i"; 
  201.  
  202. if(preg_match($matchstring, $host, $matches) ) { 
  203. return true; 
  204. } else { 
  205. return false; 
  206.  
  207.  
  208.  
  209. function M_create_internal_URL_group( $rule, $post, $level_id ) { 
  210.  
  211. global $wpdb; 
  212.  
  213. switch( $rule ) { 
  214. case 'posts': $permalinks = array(); 
  215. foreach( $_POST[$rule] as $rule ) { 
  216. $thelink = get_permalink( $rule ); 
  217. $thelink = str_replace('http://', 'https?://', $thelink ); 
  218. $permalinks[] = untrailingslashit($thelink) . '(/.*)'; 
  219.  
  220. $sql = $wpdb->prepare( "SELECT id FROM " . membership_db_prefix($wpdb, 'urlgroups') . " WHERE groupname = %s ORDER BY id DESC LIMIT 0, 1", '_posts-' . $level_id ); 
  221. $id = $wpdb->get_var( $sql ); 
  222.  
  223. $data = array( "groupname" => '_posts-' . $level_id,  
  224. "groupurls" => implode("\n", $permalinks),  
  225. "isregexp" => 1,  
  226. "stripquerystring" => 1 
  227. ); 
  228.  
  229. if(!empty($id)) { 
  230. // exists so we're going to do an update 
  231. $wpdb->update( membership_db_prefix($wpdb, 'urlgroups'), $data, array( "id" => $id) ); 
  232. } else { 
  233. // doesn't exist so we're going to do an add. 
  234. $wpdb->insert( membership_db_prefix($wpdb, 'urlgroups'), $data ); 
  235.  
  236. break; 
  237.  
  238. case 'pages': $permalinks = array(); 
  239. foreach( $_POST[$rule] as $rule ) { 
  240. $thelink = get_permalink( $rule ); 
  241. $thelink = str_replace('http://', 'https?://', $thelink ); 
  242. $permalinks[] = untrailingslashit($thelink) . '(/.*)'; 
  243.  
  244. $sql = $wpdb->prepare( "SELECT id FROM " . membership_db_prefix($wpdb, 'urlgroups') . " WHERE groupname = %s ORDER BY id DESC LIMIT 0, 1", '_pages-' . $level_id ); 
  245. $id = $wpdb->get_var( $sql ); 
  246.  
  247. $data = array( "groupname" => '_pages-' . $level_id,  
  248. "groupurls" => implode("\n", $permalinks),  
  249. "isregexp" => 1,  
  250. "stripquerystring" => 1 
  251. ); 
  252.  
  253. if(!empty($id)) { 
  254. // exists so we're going to do an update 
  255. $wpdb->update( membership_db_prefix($wpdb, 'urlgroups'), $data, array( "id" => $id) ); 
  256. } else { 
  257. // doesn't exist so we're going to do an add. 
  258. $wpdb->insert( membership_db_prefix($wpdb, 'urlgroups'), $data ); 
  259.  
  260. break; 
  261.  
  262. case 'bppages': $permalinks = array(); 
  263. foreach( $_POST[$rule] as $rule ) { 
  264. $thelink = get_permalink( $rule ); 
  265. $thelink = str_replace('http://', 'https?://', $thelink ); 
  266. $permalinks[] = untrailingslashit($thelink) . '(/.*)'; 
  267.  
  268. $sql = $wpdb->prepare( "SELECT id FROM " . membership_db_prefix($wpdb, 'urlgroups') . " WHERE groupname = %s ORDER BY id DESC LIMIT 0, 1", '_bppages-' . $level_id); 
  269. $id = $wpdb->get_var( $sql ); 
  270.  
  271. $data = array( "groupname" => '_bppages-' . $level_id,  
  272. "groupurls" => implode("\n", $permalinks),  
  273. "isregexp" => 1,  
  274. "stripquerystring" => 1 
  275. ); 
  276.  
  277. if(!empty($id)) { 
  278. // exists so we're going to do an update 
  279. $wpdb->update( membership_db_prefix($wpdb, 'urlgroups'), $data, array( "id" => $id) ); 
  280. } else { 
  281. // doesn't exist so we're going to do an add. 
  282. $wpdb->insert( membership_db_prefix($wpdb, 'urlgroups'), $data ); 
  283.  
  284. break; 
  285.  
  286. case 'bpgroups': $permalinks = array(); 
  287. if(function_exists('bp_get_group_permalink')) { 
  288. foreach( $_POST[$rule] as $rule ) { 
  289. $group = new BP_Groups_Group( $rule ); 
  290. $thelink = bp_get_group_permalink( $group ); 
  291. $thelink = str_replace('http://', 'https?://', $thelink ); 
  292. $permalinks[] = untrailingslashit($thelink) . '(/.*)'; 
  293.  
  294.  
  295. $sql = $wpdb->prepare( "SELECT id FROM " . membership_db_prefix($wpdb, 'urlgroups') . " WHERE groupname = %s ORDER BY id DESC LIMIT 0, 1", '_bpgroups-' . $level_id); 
  296. $id = $wpdb->get_var( $sql ); 
  297.  
  298. $data = array( "groupname" => '_bpgroups-' . $level_id,  
  299. "groupurls" => implode("\n", $permalinks),  
  300. "isregexp" => 1,  
  301. "stripquerystring" => 1 
  302. ); 
  303.  
  304. if(!empty($id)) { 
  305. // exists so we're going to do an update 
  306. $wpdb->update( membership_db_prefix($wpdb, 'urlgroups'), $data, array( "id" => $id) ); 
  307. } else { 
  308. // doesn't exist so we're going to do an add. 
  309. $wpdb->insert( membership_db_prefix($wpdb, 'urlgroups'), $data ); 
  310.  
  311. break; 
  312.  
  313.  
  314.  
  315. add_action( 'membership_update_positive_rule', 'M_create_internal_URL_group', 10, 3 ); 
  316. add_action( 'membership_update_negative_rule', 'M_create_internal_URL_group', 10, 3 ); 
  317. add_action( 'membership_add_positive_rule', 'M_create_internal_URL_group', 10, 3 ); 
  318. add_action( 'membership_add_negative_rule', 'M_create_internal_URL_group', 10, 3 ); 
  319.  
  320. ?> 
.