loadbalancer.class.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * @author ThemePunch <info@themepunch.com>
  4. * @link http://www.themepunch.com/
  5. * @copyright 2017 ThemePunch
  6. */
  7. if( !defined( 'ABSPATH') ) exit();
  8. class RevSliderLoadBalancer {
  9. public $servers = array();
  10. /**
  11. * set the server list on construct
  12. **/
  13. public function __construct(){
  14. $this->servers = get_option('revslider_servers', array());
  15. $this->servers = (empty($this->servers)) ? array('themepunch.tools') : $this->servers; //, 'themepunch-ext-a.tools'
  16. }
  17. /**
  18. * get the url depending on the purpose, here with key, you can switch do a different server
  19. **/
  20. public function get_url($purpose, $key = 0){
  21. $url = 'https://';
  22. $use_url = (!isset($this->servers[$key])) ? reset($this->servers) : $this->servers[$key];
  23. //$use_url = 'themepunch.tools';
  24. switch($purpose){
  25. case 'updates':
  26. $url .= 'updates.';
  27. break;
  28. case 'templates':
  29. $url .= 'templates.';
  30. break;
  31. case 'library':
  32. $url .= 'library.';
  33. break;
  34. default:
  35. return false;
  36. }
  37. $url .= $use_url;
  38. return $url;
  39. }
  40. /**
  41. * refresh the server list to be used, will be done once in a month
  42. **/
  43. public function refresh_server_list($force = false){
  44. global $wp_version;
  45. $last_check = get_option('revslider_server_refresh', false);
  46. if($force === true || $last_check === false || time() - $last_check > 60 * 60 * 24 * 14){
  47. //$url = $this->get_url('updates');
  48. $url = 'https://updates.themepunch.tools';
  49. $count = 0;
  50. /*$response = false;
  51. do{*/
  52. $request = wp_remote_post($url.'/get_server_list.php', array(
  53. 'user-agent' => 'WordPress/'.$wp_version.'; '.get_bloginfo('url'),
  54. 'body' => array(
  55. 'item' => urlencode(RS_PLUGIN_SLUG),
  56. 'version' => urlencode(RevSliderGlobals::SLIDER_REVISION)
  57. ),
  58. 'timeout' => 45
  59. ));
  60. if(!is_wp_error($request)){
  61. if($response = maybe_unserialize($request['body'])){
  62. $list = json_decode($response, true);
  63. update_option('revslider_servers', $list);
  64. }
  65. }/*else{
  66. $url = $this->get_url('updates');
  67. }
  68. $count++;
  69. }while($response === false && $count < 5);*/
  70. update_option('revslider_server_refresh', time());
  71. }
  72. }
  73. /**
  74. * move the server list, to take the next server as the one currently seems unavailable
  75. **/
  76. public function move_server_list(){
  77. $servers = $this->servers;
  78. $a = array_shift($servers);
  79. $servers[] = $a;
  80. $this->servers = $servers;
  81. update_option('revslider_servers', $servers);
  82. }
  83. }
  84. ?>