remotedhcp.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. class UbillingRemoteDHCP {
  3. /**
  4. * Contains default object config as key=>value
  5. *
  6. * @var array
  7. */
  8. protected $config = array();
  9. /**
  10. * Contains remote billing URL
  11. *
  12. * @var string
  13. */
  14. protected $billingUrl = '';
  15. /**
  16. * Contains Ubilling serial for API access
  17. *
  18. * @var string
  19. */
  20. protected $apiKey = '';
  21. /**
  22. * Some predefined URLs and paths
  23. */
  24. const URL_API = '/?module=remoteapi&action=remotedhcp&key=';
  25. const EXPORT_PATH = '/multinet/';
  26. /**
  27. * Creates new remote dhcp instance
  28. */
  29. public function __construct() {
  30. $this->loadConfig();
  31. $this->generateConfigs();
  32. }
  33. /**
  34. * Loads object configuration and sets all required properties
  35. *
  36. * @return void
  37. */
  38. protected function loadConfig() {
  39. $configPath = 'config.ini';
  40. $this->config = parse_ini_file($configPath);
  41. $this->billingUrl = $this->config['UBILLING_URL'];
  42. $this->apiKey = $this->config['UBILLING_SERIAL'];
  43. }
  44. /**
  45. * Returns remote DHCP configs data as array filename=>data
  46. *
  47. * @return array
  48. */
  49. protected function getRawData() {
  50. $result = array();
  51. if (!empty($this->apiKey) AND ! empty($this->billingUrl)) {
  52. $requestUrl = $this->billingUrl . self::URL_API . $this->apiKey;
  53. @$requestResult = file_get_contents($requestUrl);
  54. if (!empty($requestResult)) {
  55. @$result = json_decode($requestResult, true);
  56. }
  57. }
  58. return($result);
  59. }
  60. /**
  61. * Regenerates all config files with remotely fetched content
  62. *
  63. * @return void
  64. */
  65. protected function generateConfigs() {
  66. $rawData = $this->getRawData();
  67. if (is_array($rawData)) {
  68. if (!empty($rawData)) {
  69. foreach ($rawData as $eachConfigName => $eachConfigData) {
  70. $fileName = dirname(__FILE__) . self::EXPORT_PATH . $eachConfigName;
  71. file_put_contents($fileName, $eachConfigData['content']);
  72. }
  73. //yeah, we updated some configs and now we need server restart
  74. $this->restartDhcpd();
  75. } else {
  76. print('ERROR:EMPTY_CONFIGS_RECEIVED');
  77. }
  78. } else {
  79. print('ERROR:REMOTE_UBILLING_CONNECTION_FAILED');
  80. }
  81. }
  82. /**
  83. * Performs ISC-DHCP server restart
  84. *
  85. * @return void
  86. */
  87. protected function restartDhcpd() {
  88. $rcPath = $this->config['RC_DHCPD'];
  89. $restartCommad = $rcPath . ' restart';
  90. shell_exec($restartCommad);
  91. }
  92. }
  93. $remoteDhcp = new UbillingRemoteDHCP();