/app_old/membershipincludes/classes/class.ping.php

  1. <?php 
  2. if(!class_exists('M_Ping')) { 
  3.  
  4. class M_Ping { 
  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', 'ping_history', 'pings'); 
  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. var $ping_history; 
  20. var $pings; 
  21.  
  22. // if the data needs reloaded, or hasn't been loaded yet 
  23. var $dirty = true; 
  24.  
  25. var $ping; 
  26. var $id; 
  27.  
  28. var $pingconstants = array( '%blogname%' => '',  
  29. '%blogurl%' => '',  
  30. '%username%' => '',  
  31. '%usernicename%' => '',  
  32. '%useremail%' => '',  
  33. '%userid%' => '',  
  34. '%networkname%' => '',  
  35. '%networkurl%' => '',  
  36. '%subscriptionname%' => '',  
  37. '%levelname%' => '',  
  38. '%timestamp%' => '' 
  39. ); 
  40.  
  41. function __construct( $id = false) { 
  42.  
  43. global $wpdb; 
  44.  
  45. $this->db =& $wpdb; 
  46.  
  47. foreach($this->tables as $table) { 
  48. $this->$table = membership_db_prefix($this->db, $table); 
  49.  
  50. $this->id = $id; 
  51.  
  52.  
  53. function M_Ping( $id = false ) { 
  54. $this->__construct( $id ); 
  55.  
  56. function ping_name() { 
  57. $this->ping = $this->get_ping(); 
  58.  
  59. return $this->ping->pingname; 
  60.  
  61. function ping_url() { 
  62. $this->ping = $this->get_ping(); 
  63.  
  64. return $this->ping->pingurl; 
  65.  
  66. function get_ping( $force = false ) { 
  67.  
  68. if(!empty($this->ping) && !$force) { 
  69. return $this->ping; 
  70. } else { 
  71. $sql = $this->db->prepare( "SELECT * FROM {$this->pings} WHERE id = %d ", $this->id ); 
  72.  
  73. return $this->db->get_row( $sql ); 
  74.  
  75.  
  76. function get_specifc_ping( $id ) { 
  77.  
  78. $sql = $this->db->prepare( "SELECT * FROM {$this->pings} WHERE id = %d ", $id ); 
  79.  
  80. return $this->db->get_row( $sql ); 
  81.  
  82.  
  83. function editform() { 
  84.  
  85. $this->ping = $this->get_ping(); 
  86.  
  87. echo '<table class="form-table">'; 
  88.  
  89. echo '<tr class="form-field form-required">'; 
  90. echo '<th style="" scope="row" valign="top">' . __('Ping name', 'membership') . '</th>'; 
  91. echo '<td valign="top"><input name="pingname" type="text" size="50" title="' . __('Ping name', 'membership') . '" style="width: 50%;" value="' . esc_attr(stripslashes($this->ping->pingname)) . '" /></td>'; 
  92. echo '</tr>'; 
  93.  
  94. echo '<tr class="form-field form-required">'; 
  95. echo '<th style="" scope="row" valign="top">' . __('Ping URL', 'membership') . '</th>'; 
  96. echo '<td valign="top"><input name="pingurl" type="text" size="50" title="' . __('Ping URL', 'membership') . '" style="width: 50%;" value="' . esc_attr(stripslashes($this->ping->pingurl)) . '" /></td>'; 
  97. echo '</tr>'; 
  98.  
  99. echo '<tr class="form-field form-required">'; 
  100. echo '<th style="" scope="row" valign="top">' . __('Ping data', 'membership') . '</th>'; 
  101. echo '<td valign="top"><textarea name="pinginfo" rows="15" cols="40" style="float: left; width: 40%;">' . esc_html(stripslashes($this->ping->pinginfo)) . '</textarea>'; 
  102. // Display some instructions for the message. 
  103. echo '<div class="instructions" style="float: left; width: 40%; margin-left: 10px;">'; 
  104. echo __('You can use the following constants within the message body to embed database information.', 'membership'); 
  105. echo '<br /><em>'; 
  106.  
  107. echo implode('<br/>', array_keys(apply_filters('membership_ping_constants_list', $this->pingconstants)) ); 
  108.  
  109. echo '</em><br/>' . __('One entry per line. e.g. key=value', 'membership'); 
  110. echo '</div>'; 
  111.  
  112. echo '</td>'; 
  113. echo '</tr>'; 
  114.  
  115. echo '<tr class="form-field form-required">'; 
  116. echo '<th style="" scope="row" valign="top">' . __('Ping method', 'membership') . '</th>'; 
  117. echo '<td valign="top" align="left">'; 
  118. echo '<select name="pingtype">'; 
  119. echo '<option value="GET"'; 
  120. if($this->ping->pingtype == 'GET') echo ' selected="selected"'; 
  121. echo '>' . __('GET', 'membership') . '</option>'; 
  122. echo '<option value="POST"'; 
  123. if($this->ping->pingtype == 'POST') echo ' selected="selected"'; 
  124. echo '>' . __('POST', 'membership') . '</option>'; 
  125. echo '</select>'; 
  126. echo '</td></tr>'; 
  127.  
  128. echo '</table>'; 
  129.  
  130.  
  131. function addform() { 
  132.  
  133. echo '<table class="form-table">'; 
  134.  
  135. echo '<tr class="form-field form-required">'; 
  136. echo '<th style="" scope="row" valign="top">' . __('Ping name', 'membership') . '</th>'; 
  137. echo '<td valign="top"><input name="pingname" type="text" size="50" title="' . __('Ping name', 'membership') . '" style="width: 50%;" value="" /></td>'; 
  138. echo '</tr>'; 
  139.  
  140. echo '<tr class="form-field form-required">'; 
  141. echo '<th style="" scope="row" valign="top">' . __('Ping URL', 'membership') . '</th>'; 
  142. echo '<td valign="top"><input name="pingurl" type="text" size="50" title="' . __('Ping URL', 'membership') . '" style="width: 50%;" value="" /></td>'; 
  143. echo '</tr>'; 
  144.  
  145. echo '<tr class="form-field form-required">'; 
  146. echo '<th style="" scope="row" valign="top">' . __('Ping data', 'membership') . '</th>'; 
  147. echo '<td valign="top"><textarea name="pinginfo" rows="15" cols="40" style="float: left; width: 40%;"></textarea>'; 
  148. // Display some instructions for the message. 
  149. echo '<div class="instructions" style="float: left; width: 40%; margin-left: 10px;">'; 
  150. echo __('You can use the following constants within the message body to embed database information.', 'membership'); 
  151. echo '<br /><em>'; 
  152.  
  153. echo implode('<br/>', array_keys(apply_filters('membership_ping_constants_list', $this->pingconstants)) ); 
  154.  
  155. echo '</em><br/>' . __('One entry per line. e.g. key=value', 'membership'); 
  156.  
  157. echo '</div>'; 
  158. echo '</td>'; 
  159. echo '</tr>'; 
  160.  
  161. echo '<tr class="form-field form-required">'; 
  162. echo '<th style="" scope="row" valign="top">' . __('Ping method', 'membership') . '</th>'; 
  163. echo '<td valign="top" align="left">'; 
  164. echo '<select name="pingtype">'; 
  165. echo '<option value="GET"'; 
  166. echo '>' . __('GET', 'membership') . '</option>'; 
  167. echo '<option value="POST"'; 
  168. echo '>' . __('POST', 'membership') . '</option>'; 
  169. echo '</select>'; 
  170. echo '</td></tr>'; 
  171.  
  172. echo '</table>'; 
  173.  
  174.  
  175. function add() { 
  176.  
  177. $insert = array( 
  178. "pingname" => $_POST['pingname'],  
  179. "pingurl" => $_POST['pingurl'],  
  180. "pinginfo" => $_POST['pinginfo'],  
  181. "pingtype" => $_POST['pingtype'] 
  182. ); 
  183.  
  184. return $this->db->insert( $this->pings, $insert ); 
  185.  
  186.  
  187. function update() { 
  188.  
  189. $updates = array( 
  190. "pingname" => $_POST['pingname'],  
  191. "pingurl" => $_POST['pingurl'],  
  192. "pinginfo" => $_POST['pinginfo'],  
  193. "pingtype" => $_POST['pingtype'] 
  194. ); 
  195.  
  196. return $this->db->update( $this->pings, $updates, array( "id" => $this->id) ); 
  197.  
  198.  
  199. function delete() { 
  200.  
  201. $sql = $this->db->prepare( "DELETE FROM {$this->pings} WHERE id = %d", $this->id ); 
  202.  
  203. return $this->db->query( $sql ); 
  204.  
  205.  
  206. // History 
  207. function get_history() { 
  208. $sql = $this->db->prepare( "SELECT * FROM {$this->ping_history} WHERE ping_id = %d ORDER BY ping_sent DESC LIMIT 0, 50 ", $this->id ); 
  209.  
  210. return $this->db->get_results( $sql ); 
  211.  
  212. function get_history_item( $history_id ) { 
  213. $sql = $this->db->prepare( "SELECT * FROM {$this->ping_history} WHERE id = %d ", $history_id ); 
  214.  
  215. return $this->db->get_row( $sql ); 
  216.  
  217. function add_history( $sent, $return ) { 
  218.  
  219. $insert = array( 
  220. "ping_id" => $this->id,  
  221. "ping_sent" => gmdate( 'Y-m-d H:i:s' ),  
  222. "ping_info" => serialize($sent),  
  223. "ping_return" => serialize($return) 
  224. ); 
  225.  
  226. return $this->db->insert( $this->ping_history, $insert ); 
  227.  
  228. function update_history( $history_id, $sent, $return ) { 
  229. $update = array( 
  230. "ping_id" => $this->id,  
  231. "ping_sent" => gmdate( 'Y-m-d H:i:s' ),  
  232. "ping_info" => serialize($sent),  
  233. "ping_return" => serialize($return) 
  234. ); 
  235.  
  236. return $this->db->update( $this->ping_history, $update, array( "id" => $history_id ) ); 
  237.  
  238. // processing 
  239. function send_ping( $sub_id = false, $level_id = false, $user_id = false ) { 
  240.  
  241. $this->ping = $this->get_ping(); 
  242.  
  243. if( !class_exists( 'WP_Http' ) ) { 
  244. include_once( ABSPATH . WPINC. '/class-http.php' ); 
  245.  
  246. $pingdata = $this->pingconstants; 
  247.  
  248. if(empty($user_id)) { 
  249. $user = wp_get_current_user(); 
  250. $member = new M_Membership( $user->ID ); 
  251. } else { 
  252. $member = new M_Membership( $user_id ); 
  253.  
  254. foreach($pingdata as $key => $value) { 
  255. switch($key) { 
  256. case '%blogname%': $pingdata[$key] = get_option('blogname'); 
  257. break; 
  258.  
  259. case '%blogurl%': $pingdata[$key] = get_option('home'); 
  260. break; 
  261.  
  262. case '%username%': $pingdata[$key] = $member->user_login; 
  263. break; 
  264.  
  265. case '%usernicename%': $pingdata[$key] = $member->user_nicename; 
  266. break; 
  267.  
  268. case '%useremail%': $pingdata[$key] = $member->user_email; 
  269. break; 
  270.  
  271. case '%userid%': $pingdata[$key] = $member->ID; 
  272. break; 
  273.  
  274. case '%networkname%': $pingdata[$key] = get_site_option('site_name'); 
  275. break; 
  276.  
  277. case '%networkurl%': $pingdata[$key] = get_site_option('siteurl'); 
  278. break; 
  279.  
  280. case '%subscriptionname%': if(!$sub_id) { 
  281. $ids = $member->get_subscription_ids(); 
  282. if(!empty($ids)) { 
  283. $sub_id = $ids[0]; 
  284.  
  285. if(!empty($sub_id)) { 
  286. $sub = new M_Subscription( $sub_id ); 
  287. $pingdata[$key] = $sub->sub_name(); 
  288. } else { 
  289. $pingdata[$key] = ''; 
  290.  
  291. break; 
  292.  
  293. case '%levelname%': if(!$level_id) { 
  294. $ids = $member->get_level_ids(); 
  295. if(!empty($ids)) { 
  296. $levels = $ids[0]; 
  297.  
  298. if(!empty($levels->level_id)) { 
  299. $level = new M_Level( $levels->level_id ); 
  300. $pingdata[$key] = $level->level_title(); 
  301. } else { 
  302. $pingdata[$key] = ''; 
  303. break; 
  304.  
  305. case '%timestamp%': $pingdata[$key] = time(); 
  306. break; 
  307.  
  308. default: $pingdata[$key] = apply_filter( 'membership_pingfield_' . $key, '' ); 
  309. break; 
  310.  
  311. $url = $this->ping->pingurl; 
  312.  
  313. // Globally replace the values in the ping and then make it into an array to send 
  314. $pingmessage = str_replace(array_keys($pingdata), array_values($pingdata), $this->ping->pinginfo); 
  315. $pingmessage = array_map( 'trim', explode("\n", $pingmessage) ); 
  316.  
  317. // make the ping message into a sendable bit of text 
  318. $pingtosend = array(); 
  319. foreach($pingmessage as $key => $value) { 
  320. $temp = explode("=", $value); 
  321. $pingtosend[$temp[0]] = $temp[1]; 
  322.  
  323. // Send the request 
  324. if( class_exists( 'WP_Http' ) ) { 
  325. $request = new WP_Http; 
  326.  
  327. switch( $this->ping->pingtype ) { 
  328. case 'GET': $url = untrailingslashit($url) . "?"; 
  329. foreach($pingtosend as $key => $val) { 
  330. if(substr($url, -1) != '?') $url .= "&"; 
  331. $url .= $key . "=" . urlencode($val); 
  332. $result = $request->request( $url, array( 'method' => 'GET', 'body' => '' ) ); 
  333. break; 
  334.  
  335. case 'POST': $result = $request->request( $url, array( 'method' => 'POST', 'body' => $pingtosend ) ); 
  336. break; 
  337.  
  338. /** 
  339. 'headers': an array of response headers, such as "x-powered-by" => "PHP/5.2.1" 
  340. 'body': the response string sent by the server, as you would see it with you web browser 
  341. 'response': an array of HTTP response codes. Typically, you'll want to have array('code'=>200, 'message'=>'OK') 
  342. 'cookies': an array of cookie information 
  343. */ 
  344.  
  345. $this->add_history( $pingtosend, $result ); 
  346.  
  347.  
  348.  
  349. function resend_historic_ping( $history_id, $rewrite ) { 
  350. $history = $this->get_history_item( $history_id ); 
  351.  
  352. if(!empty($history)) { 
  353. $this->id = $history->ping_id; 
  354. $ping = $this->get_specifc_ping( $history->ping_id ); 
  355.  
  356. if( !class_exists( 'WP_Http' ) ) { 
  357. include_once( ABSPATH . WPINC. '/class-http.php' ); 
  358.  
  359. $url = $ping->pingurl; 
  360. $pingtosend = unserialize($history->ping_info); 
  361.  
  362. // Send the request 
  363. if( class_exists( 'WP_Http' ) ) { 
  364. $request = new WP_Http; 
  365.  
  366. switch( $ping->pingtype ) { 
  367. case 'GET': $url = untrailingslashit($url) . "?"; 
  368. foreach($pingtosend as $key => $val) { 
  369. if(substr($url, -1) != '?') $url .= "&"; 
  370. $url .= $key . "=" . urlencode($val); 
  371. $result = $request->request( $url, array( 'method' => 'GET', 'body' => '' ) ); 
  372. break; 
  373.  
  374. case 'POST': $result = $request->request( $url, array( 'method' => 'POST', 'body' => $pingtosend ) ); 
  375. break; 
  376.  
  377. /** 
  378. 'headers': an array of response headers, such as "x-powered-by" => "PHP/5.2.1" 
  379. 'body': the response string sent by the server, as you would see it with you web browser 
  380. 'response': an array of HTTP response codes. Typically, you'll want to have array('code'=>200, 'message'=>'OK') 
  381. 'cookies': an array of cookie information 
  382. */ 
  383.  
  384. if($rewrite) { 
  385. $this->add_history( $pingtosend, $result ); 
  386. } else { 
  387. $this->update_history( $history_id, $pingtosend, $result ); 
  388.  
  389.  
  390. // Ping integration functions and hooks 
  391. /** 
  392. do_action( 'membership_add_level', $tolevel_id, $this->ID ); 
  393. do_action( 'membership_drop_level', $fromlevel_id, $this->ID ); 
  394. do_action( 'membership_move_level', $fromlevel_id, $tolevel_id, $this->ID ); 
  395.   
  396. do_action( 'membership_add_subscription', $tosub_id, $tolevel_id, $to_order, $this->ID); 
  397. do_action( 'membership_drop_subscription', $fromsub_id, $this->ID ); 
  398. do_action( 'membership_move_subscription', $fromsub_id, $tosub_id, $tolevel_id, $to_order, $this->ID ); 
  399. */ 
  400.  
  401. function M_ping_joinedlevel( $tolevel_id, $user_id ) { 
  402.  
  403. // Set up the level and find out if it has a joining ping 
  404. $level = new M_Level( $tolevel_id ); 
  405.  
  406. $joiningping_id = $level->get_meta( 'joining_ping' ); 
  407. if(!empty($joiningping_id)) { 
  408. $ping = new M_Ping( $joiningping_id ); 
  409.  
  410. $ping->send_ping( false, $tolevel_id, $user_id ); 
  411.  
  412.  
  413. add_action( 'membership_add_level', 'M_ping_joinedlevel', 10, 2 ); 
  414.  
  415. function M_ping_leftlevel( $fromlevel_id, $user_id ) { 
  416.  
  417. // Set up the level and find out if it has a leaving ping 
  418. $level = new M_Level( $fromlevel_id ); 
  419.  
  420. $leavingping_id = $level->get_meta( 'leaving_ping' ); 
  421. if(!empty($leavingping_id)) { 
  422. $ping = new M_Ping( $leavingping_id ); 
  423.  
  424. $ping->send_ping( false, $fromlevel_id, $user_id ); 
  425.  
  426. add_action( 'membership_drop_level', 'M_ping_leftlevel', 10, 2 ); 
  427.  
  428. function M_ping_movedlevel( $fromlevel_id, $tolevel_id, $user_id ) { 
  429.  
  430. M_ping_leftlevel( $fromlevel_id, $user_id ); 
  431. M_ping_joinedlevel( $tolevel_id, $user_id ); 
  432.  
  433. add_action( 'membership_move_level', 'M_ping_movedlevel', 10, 3 ); 
  434.  
  435. function M_ping_joinedsub( $tosub_id, $tolevel_id, $to_order, $user_id ) { 
  436.  
  437. $sub = new M_Subscription( $tosub_id ); 
  438. $subjoiningping_id = $sub->get_meta( 'joining_ping' ); 
  439.  
  440. if(!empty($subjoiningping_id)) { 
  441. $ping = new M_Ping( $subjoiningping_id ); 
  442.  
  443. $ping->send_ping( $tosub_id, $tolevel_id, $user_id ); 
  444.  
  445. $level = new M_Level( $tolevel_id ); 
  446. $joiningping_id = $level->get_meta( 'joining_ping' ); 
  447.  
  448. if(!empty($joiningping_id)) { 
  449. $ping = new M_Ping( $joiningping_id ); 
  450.  
  451. $ping->send_ping( $tosub_id, $tolevel_id, $user_id ); 
  452.  
  453. add_action( 'membership_add_subscription', 'M_ping_joinedsub', 10, 4 ); 
  454.  
  455. function M_ping_leftsub( $fromsub_id, $fromlevel_id, $user_id ) { 
  456.  
  457. // Leaving the level 
  458. M_ping_leftlevel( $fromlevel_id, $user_id ); 
  459.  
  460. // Leaving the sub 
  461. $sub = new M_Subscription( $fromsub_id ); 
  462. $subleavingping_id = $sub->get_meta( 'leaving_ping' ); 
  463.  
  464. if(!empty($subleavingping_id)) { 
  465. $ping = new M_Ping( $subleavingping_id ); 
  466.  
  467. $ping->send_ping( $fromsub_id, false, $user_id ); 
  468.  
  469. add_action( 'membership_drop_subscription', 'M_ping_leftsub', 10, 3 ); 
  470.  
  471. function M_ping_movedsub( $fromsub_id, $fromlevel_id, $tosub_id, $tolevel_id, $to_order, $user_id ) { 
  472.  
  473. M_ping_leftsub( $fromsub_id, $fromlevel_id, $user_id ); 
  474. M_ping_joinedsub( $tosub_id, $tolevel_id, $to_order, $user_id ); 
  475.  
  476. add_action( 'membership_move_subscription', 'M_ping_movedsub', 10, 6 ); 
  477.  
  478.  
  479. ?> 
.