services.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2018
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //includes
  22. require_once "root.php";
  23. require_once "resources/require.php";
  24. require_once "resources/check_auth.php";
  25. //check permissions
  26. if (permission_exists('service_view')) {
  27. //access granted
  28. }
  29. else {
  30. echo "access denied";
  31. exit;
  32. }
  33. global $IS_WINDOWS;
  34. if ($IS_WINDOWS == null) {
  35. if (stristr(PHP_OS, 'WIN')) { $IS_WINDOWS = true; } else { $IS_WINDOWS = false; }
  36. }
  37. $HAS_WIN_SVC = false;
  38. if($IS_WINDOWS){
  39. require_once "resources/classes/lib_win.php";
  40. $HAS_WIN_SVC = class_exists('win_service');
  41. }
  42. //add multi-lingual support
  43. $language = new text;
  44. $text = $language->get();
  45. require_once "resources/header.php";
  46. $document['title'] = $text['title-services'];
  47. require_once "resources/paging.php";
  48. $order_by = check_str($_GET["order_by"]);
  49. $order = check_str($_GET["order"]);
  50. if (strlen($_GET["a"]) > 0) {
  51. $service_uuid = check_str($_GET["id"]);
  52. $sql = "select * from v_services ";
  53. $sql .= "where service_uuid = '$service_uuid' ";
  54. $prep_statement = $db->prepare(check_sql($sql));
  55. $prep_statement->execute();
  56. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  57. foreach ($result as &$row) {
  58. $domain_uuid = $row["domain_uuid"];
  59. $service_name = $row["service_name"];
  60. $service_type = $row["service_type"];
  61. $service_data = $row["service_data"];
  62. $service_cmd_start = $row["service_cmd_start"];
  63. $service_cmd_stop = $row["service_cmd_stop"];
  64. $service_description = $row["service_description"];
  65. }
  66. unset ($prep_statement);
  67. if($service_type == 'svc'){
  68. if($HAS_WIN_SVC){
  69. $svc = new win_service($service_data);
  70. if ($_GET["a"] == "stop") {
  71. $_SESSION["message"] = $text['message-stopping'].': '.$service_name;
  72. $svc->stop();
  73. }
  74. if ($_GET["a"] == "start") {
  75. $_SESSION["message"] = $text['message-starting'].': '.$service_name;
  76. $svc->start();
  77. }
  78. }
  79. }
  80. else {
  81. if ($_GET["a"] == "stop") {
  82. $_SESSION["message"] = $text['message-stopping'].': '.$service_name;
  83. shell_exec($service_cmd_stop);
  84. }
  85. if ($_GET["a"] == "start") {
  86. $_SESSION["message"] = $text['message-starting'].': '.$service_name;
  87. shell_exec($service_cmd_start);
  88. }
  89. }
  90. header("Location: services.php");
  91. return;
  92. }
  93. //get the service count
  94. $sql = "select * from v_services ";
  95. if (strlen($order_by)> 0) { $sql .= "order by $order_by $order "; }
  96. $prep_statement = $db->prepare(check_sql($sql));
  97. $prep_statement->execute();
  98. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  99. $num_rows = count($result);
  100. unset ($prep_statement, $result, $sql);
  101. //paging
  102. $rows_per_page = ($_SESSION['domain']['paging']['numeric'] != '') ? $_SESSION['domain']['paging']['numeric'] : 50;
  103. $param = "";
  104. $page = $_GET['page'];
  105. if (strlen($page) == 0) { $page = 0; $_GET['page'] = 0; }
  106. list($paging_controls, $rows_per_page, $var_3) = paging($num_rows, $param, $rows_per_page);
  107. $offset = $rows_per_page * $page;
  108. //get the service data
  109. $sql = "select * from v_services ";
  110. if (strlen($order_by)> 0) { $sql .= "order by $order_by $order "; }
  111. $sql .= " limit $rows_per_page offset $offset ";
  112. $prep_statement = $db->prepare(check_sql($sql));
  113. $prep_statement->execute();
  114. $services = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  115. unset ($prep_statement, $sql);
  116. //set the row style
  117. $c = 0;
  118. $row_style["0"] = "row_style0";
  119. $row_style["1"] = "row_style1";
  120. //check if a process is running
  121. function is_process_running($pid) {
  122. $status = shell_exec( 'ps -p ' . $pid );
  123. $status_array = explode ("\n", $status);
  124. if (strlen(trim($status_array[1])) > 0) {
  125. return true;
  126. }
  127. else {
  128. return false;
  129. }
  130. }
  131. //show the table content
  132. echo "<table width='100%' cellpadding='0' cellspacing='0' border='0'>\n";
  133. echo "<tr>\n";
  134. echo "<td width='50%' align='left' nowrap='nowrap'><b>".$text['header-services']."</b></td>\n";
  135. echo "<td width='50%' align='right'>&nbsp;</td>\n";
  136. echo "</tr>\n";
  137. echo "<tr>\n";
  138. echo "<td align='left' colspan='2'>\n";
  139. echo $text['description-services']."<br /><br />\n";
  140. echo "</td>\n";
  141. echo "</tr>\n";
  142. echo "</tr></table>\n";
  143. echo "<table class='tr_hover' width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  144. echo "<tr>\n";
  145. echo th_order_by('service_name', $text['label-name'], $order_by, $order);
  146. echo "<th>".$text['label-status']."</th>\n";
  147. echo "<th>".$text['label-action']."</th>\n";
  148. echo th_order_by('service_description', $text['label-description'], $order_by, $order);
  149. echo "<td class='list_control_icons'>";
  150. if (permission_exists('service_add')) {
  151. echo "<a href='service_edit.php' alt='add'>$v_link_label_add</a>";
  152. }
  153. echo "</td>\n";
  154. echo "</tr>\n";
  155. if (is_array($services)) {
  156. foreach($services as $row) {
  157. $tr_link = (permission_exists('service_edit')) ? "href='service_edit.php?id=".escape($row[service_uuid])."'" : null;
  158. echo "<tr ".$tr_link.">\n";
  159. echo " <td valign='top' class='".$row_style[$c]."'>";
  160. if (permission_exists('service_edit')) {
  161. echo "<a href='service_edit.php?id=".escape($row[service_uuid])."'>".escape($row[service_name])."</a>";
  162. }
  163. else {
  164. echo escape($row[service_name]);
  165. }
  166. echo " </td>\n";
  167. echo " <td valign='top' class='".$row_style[$c]."'>\n";
  168. $service_running = false;
  169. if ($row[service_type] == "svc") {
  170. if ($HAS_WIN_SVC) {
  171. $service_data = $row[service_data];
  172. $svc = new win_service($service_data);
  173. $svc_state = $svc->state() or $svc->last_error();
  174. if(!$svc_state){
  175. $svc_state = 'NOT_INSTALL';
  176. }
  177. $service_running = (($svc_state == 'RUNNING') || ($svc_state == 'START_PENDING'));
  178. echo "<strong>$svc_state</strong>";
  179. echo "</td>\n";
  180. echo " <td valign='top' class='".$row_style[$c]."'>\n";
  181. if ($svc_state == 'NOT_INSTALL') {
  182. echo "<strong>$svc_state</strong>";
  183. }
  184. else {
  185. if ($service_running) {
  186. echo " <a href='services.php?id=".escape($row[service_uuid])."&a=stop' alt='stop'>".$text['label-stop']."</a>";
  187. }
  188. else {
  189. echo " <a href='services.php?id=".escape($row[service_uuid])."&a=start' alt='start'>".$text['label-start']."</a>";
  190. }
  191. }
  192. }
  193. else{
  194. echo "<strong>UNSUPPORT</strong>";
  195. echo "</td>\n";
  196. echo " <td valign='top' class='".$row_style[$c]."'>\n";
  197. echo "<strong>UNSUPPORT</strong>";
  198. }
  199. }
  200. else {
  201. if ($row[service_type] == "pid" || $row[service_type] == "pid_file") {
  202. $pid = file_get_contents($row[service_data]);
  203. $service_running = is_process_running($pid);
  204. }
  205. if ($row[service_type] == "file") {
  206. $service_data = $row[service_data];
  207. $service_running = file_exists($service_data);
  208. }
  209. if ($service_running) {
  210. echo "<strong>".$text['label-running']."</strong>";
  211. }
  212. else {
  213. echo "<strong>".$text['label-stopped']."</strong>";
  214. }
  215. echo "</td>\n";
  216. echo " <td valign='top' class='".$row_style[$c]."'>\n";
  217. if ($service_running) {
  218. echo " <a href='services.php?id=".escape($row[service_uuid])."&a=stop' alt='stop'>".$text['label-stop']."</a>";
  219. }
  220. else {
  221. echo " <a href='services.php?id=".escape($row[service_uuid])."&a=start' alt='start'>".$text['label-start']."</a>";
  222. }
  223. }
  224. echo "</td>\n";
  225. echo " <td valign='top' class='row_stylebg'>".escape($row[service_description])."&nbsp;</td>\n";
  226. echo " <td class='list_control_icons'>";
  227. if (permission_exists('service_edit')) {
  228. echo "<a href='service_edit.php?id=".escape($row[service_uuid])."' alt='".$text['button-edit']."'>$v_link_label_edit</a>";
  229. }
  230. if (permission_exists('service_delete')) {
  231. echo "<a href='service_delete.php?id=".escape($row[service_uuid])."' alt='".$text['button-delete']."' onclick=\"return confirm('".$text['confirm-delete']."')\">$v_link_label_delete</a>";
  232. }
  233. echo " </td>\n";
  234. echo "</tr>\n";
  235. if ($c==0) { $c=1; } else { $c=0; }
  236. } //end foreach
  237. unset($sql, $services);
  238. } //end if results
  239. echo "<tr>\n";
  240. echo "<td colspan='5' align='left'>\n";
  241. echo " <table width='100%' cellpadding='0' cellspacing='0'>\n";
  242. echo " <tr>\n";
  243. echo " <td width='33.3%' nowrap>&nbsp;</td>\n";
  244. echo " <td width='33.3%' align='center' nowrap>$paging_controls</td>\n";
  245. echo " <td class='list_control_icons'>";
  246. if (permission_exists('service_add')) {
  247. echo "<a href='service_edit.php' alt='add'>$v_link_label_add</a>";
  248. }
  249. echo " </td>\n";
  250. echo " </tr>\n";
  251. echo " </table>\n";
  252. echo "</td>\n";
  253. echo "</tr>\n";
  254. echo "</table>";
  255. echo "<br><br>";
  256. //include the footer
  257. require_once "resources/footer.php";
  258. ?>