module.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php namespace pineapple;
  2. class Configuration extends SystemModule
  3. {
  4. public function route()
  5. {
  6. switch ($this->request->action) {
  7. case 'getCurrentTimeZone':
  8. $this->getCurrentTimeZone();
  9. break;
  10. case 'getLandingPageData':
  11. $this->getLandingPageData();
  12. break;
  13. case 'saveLandingPage':
  14. $this->saveLandingPageData();
  15. break;
  16. case 'changePass':
  17. $this->changePass();
  18. break;
  19. case 'changeTimeZone':
  20. $this->changeTimeZone();
  21. break;
  22. case 'resetPineapple':
  23. $this->resetPineapple();
  24. break;
  25. case 'haltPineapple':
  26. $this->haltPineapple();
  27. break;
  28. case 'rebootPineapple':
  29. $this->rebootPineapple();
  30. break;
  31. case 'getLandingPageStatus':
  32. $this->getLandingPageStatus();
  33. break;
  34. case 'getAutoStartStatus':
  35. $this->getAutoStartStatus();
  36. break;
  37. case 'enableLandingPage':
  38. $this->enableLandingPage();
  39. break;
  40. case 'disableLandingPage':
  41. $this->disableLandingPage();
  42. break;
  43. case 'enableAutoStart':
  44. $this->enableAutoStart();
  45. break;
  46. case 'disableAutoStart':
  47. $this->disableAutoStart();
  48. break;
  49. case 'getButtonScript':
  50. $this->getButtonScript();
  51. break;
  52. case 'saveButtonScript':
  53. $this->saveButtonScript();
  54. break;
  55. case 'getDevice':
  56. $this->getDeviceName();
  57. break;
  58. case 'getDeviceConfig':
  59. $this->getDeviceConfigArray();
  60. break;
  61. }
  62. }
  63. private function haltPineapple()
  64. {
  65. $this->execBackground("sync && led all off && halt");
  66. $this->response = array("success" => true);
  67. }
  68. private function rebootPineapple()
  69. {
  70. $this->execBackground("reboot");
  71. $this->response = array("success" => true);
  72. }
  73. private function resetPineapple()
  74. {
  75. $this->execBackground("jffs2reset -y && reboot &");
  76. $this->response = array("success" => true);
  77. }
  78. private function getCurrentTimeZone()
  79. {
  80. $currentTimeZone = exec('date +%Z%z');
  81. $this->response = array("currentTimeZone" => $currentTimeZone);
  82. }
  83. private function changeTimeZone()
  84. {
  85. $timeZone = $this->request->timeZone;
  86. file_put_contents('/etc/TZ', $timeZone);
  87. $this->uciSet('system.@system[0].timezone', $timeZone);
  88. $this->response = array("success" => true);
  89. }
  90. private function getLandingPageData()
  91. {
  92. $landingPage = file_get_contents('/etc/pineapple/landingpage.php');
  93. $this->response = array("landingPage" => $landingPage);
  94. }
  95. private function getLandingPageStatus()
  96. {
  97. if (!empty(exec("iptables -L -vt nat | grep 'www to:.*:80'"))) {
  98. $this->response = array("enabled" => true);
  99. return;
  100. }
  101. $this->response = array("enabled" => false);
  102. }
  103. private function enableLandingPage()
  104. {
  105. exec('iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination $(uci get network.lan.ipaddr):80');
  106. exec('iptables -t nat -A POSTROUTING -j MASQUERADE');
  107. copy('/pineapple/modules/Configuration/api/landingpage_index.php', '/www/index.php');
  108. $this->response = array("success" => true);
  109. }
  110. private function disableLandingPage()
  111. {
  112. @unlink('/www/index.php');
  113. exec('iptables -t nat -D PREROUTING -p tcp --dport 80 -j DNAT --to-destination $(uci get network.lan.ipaddr):80');
  114. $this->response = array("success" => true);
  115. }
  116. private function getAutoStartStatus()
  117. {
  118. if($this->uciGet("landingpage.@settings[0].autostart") == 1) {
  119. $this->response = array("enabled" => true);
  120. } else {
  121. $this->response = array("enabled" => false);
  122. }
  123. }
  124. private function enableAutoStart()
  125. {
  126. $this->uciSet("landingpage.@settings[0].autostart", "1");
  127. $this->response = array("success" => true);
  128. }
  129. private function disableAutoStart()
  130. {
  131. $this->uciSet("landingpage.@settings[0].autostart", "0");
  132. $this->response = array("success" => true);
  133. }
  134. private function saveLandingPageData()
  135. {
  136. if (file_put_contents('/etc/pineapple/landingpage.php', $this->request->landingPageData) !== false) {
  137. $this->response = array("success" => true);
  138. } else {
  139. $this->error = "Error saving Landing Page.";
  140. }
  141. }
  142. private function getButtonScript()
  143. {
  144. if (file_exists('/etc/pineapple/button_script')) {
  145. $script = file_get_contents('/etc/pineapple/button_script');
  146. $this->response = array("buttonScript" => $script);
  147. } else {
  148. $this->error = "The button script does not exist.";
  149. }
  150. }
  151. private function saveButtonScript()
  152. {
  153. if (file_exists('/etc/pineapple/button_script')) {
  154. file_put_contents('/etc/pineapple/button_script', $this->request->buttonScript);
  155. $this->response = array("success" => true);
  156. } else {
  157. $this->error = "The button script does not exist.";
  158. }
  159. }
  160. private function getDeviceName()
  161. {
  162. $this->response = array("device" => $this->getDevice());
  163. }
  164. private function getDeviceConfigArray()
  165. {
  166. $this->response = array("config" => $this->getDeviceConfig());
  167. }
  168. protected function changePass()
  169. {
  170. if ($this->request->newPassword === $this->request->newPasswordRepeat) {
  171. if (parent::changePassword($this->request->oldPassword, $this->request->newPassword) === true) {
  172. $this->response = array("success" => true);
  173. return;
  174. }
  175. }
  176. $this->response = array("success" => false);
  177. }
  178. }