servers.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. require_once(dirname(dirname(__FILE__))."/header.php");
  3. function build_server_tree(&$server,$depth=-1,$curdepth=0){
  4. $server['children'] = Array();
  5. if($depth == -1 || $depth != $curdepth){
  6. $children = query("SELECT s.id,s.description,s.host,s.name,s.ip,s.user_id,c.parent_id as parent FROM children_v c JOIN servers s ON c.child_id = s.id WHERE c.parent_id = %d",Array($server['id']));
  7. if($children && $children->num_rows != 0 ){
  8. while($row = $children->fetch_assoc()){
  9. array_push($server['children'],$row);
  10. }
  11. }
  12. foreach($server['children'] as $k => $child){
  13. build_server_tree($server['children'][$k],$depth,$curdepth+1);
  14. }
  15. }
  16. $parent = null;
  17. if(!is_null($server['parent']) && $curdepth == 0){
  18. $parent = query("SELECT s.id,s.description,s.host,s.name,s.user_id,s.ip FROM servers s WHERE s.id = '%s'",Array($server['parent']));
  19. if($parent && $parent->num_rows == 1){
  20. $parent = $parent->fetch_assoc();
  21. $parent['opers'] = get_opers_for_server_obj($parent['id']);
  22. $parent['children'] = Array();
  23. }else{
  24. $parent = null;
  25. }
  26. }
  27. $server['parent'] = $parent;
  28. $server['opers'] = get_opers_for_server_obj($server['id']);
  29. return $server;
  30. }
  31. function get_current_server_obj(){
  32. return get_server_obj($_GET['server']);
  33. }
  34. function get_servers_obj(){
  35. $servers = Array();
  36. $res = query("SELECT s.id FROM servers s");
  37. if($res && $res->num_rows != 0){
  38. while($server = $res->fetch_assoc()){
  39. array_push($servers,get_server_for_id_obj($server['id']));
  40. }
  41. }
  42. return $servers;
  43. }
  44. function get_ulines(){
  45. $ulines = Array();
  46. $res = query("SELECT u.host FROM ulines_v u");
  47. if($res && $res->num_rows != 0){
  48. while($server = $res->fetch_assoc()){
  49. array_push($ulines,$server['host']);
  50. }
  51. }
  52. return $ulines;
  53. }
  54. function get_ulines_obj(){
  55. $ulines = Array();
  56. $res = query("SELECT u.id FROM ulines_v u");
  57. if($res && $res->num_rows != 0){
  58. while($server = $res->fetch_assoc()){
  59. array_push($ulines,get_server_for_id_obj($server['id']));
  60. }
  61. }
  62. return $ulines;
  63. }
  64. function get_server_obj($name){
  65. global $user;
  66. $server = query("SELECT id,description,host,name,ip,user_id,parent_id as parent FROM servers s WHERE user_id = %d AND lower(name) = lower('%s')",Array($user['id'],$name));
  67. if($server && $server->num_rows == 1){
  68. $server = $server->fetch_assoc();
  69. build_server_tree($server);
  70. return $server;
  71. }
  72. return false;
  73. }
  74. function get_server_for_id_obj($id){
  75. $server = query("SELECT id,description,host,name,ip,user_id,parent_id as parent FROM servers s WHERE s.id = %d",Array($id));
  76. if($server && $server->num_rows == 1){
  77. $server = $server->fetch_assoc();
  78. build_server_tree($server);
  79. return $server;
  80. }
  81. return false;
  82. }
  83. function get_servers_for_user_obj($id){
  84. $res = query("SELECT id,description,host,name,ip,user_id,parent_id as parent FROM servers s WHERE user_id = %d",Array($id));
  85. $servers = Array();
  86. if($res && $res->num_rows != 0){
  87. while($server = $res->fetch_assoc()){
  88. build_server_tree($server);
  89. array_push($servers,$server);
  90. }
  91. }
  92. return $servers;
  93. }
  94. function get_servers_for_current_user_obj(){
  95. global $user;
  96. return get_servers_for_user_obj($user['id']);
  97. }
  98. function get_servers_list_html($servers,$depth=0){
  99. global $user;
  100. $r = '<div class="accordion">';
  101. foreach($servers as $k => $server){
  102. $r .= "<h3>{$server['name']} ({$server['host']}) - {$server['description']}</h3><div>";
  103. $suser = get_user_from_id_obj($server['user_id']);
  104. $r .= "<h4>"._('Owner').":</h4>{$suser['nick']}<ul>";
  105. $r .= "<li>"._('Real Name').": {$suser['real_name']}</li><li>"._('Email').": {$suser['email']}</li></ul>";
  106. $id = 0;
  107. if(count($server['opers']) > 0){
  108. $r .= "<h4>"._('Opers').":</h4><table class='tree'>";
  109. $id++;
  110. $pid = $id;
  111. $r .= "<tr style='font-weight:bold;' class='treegrid-".($id)."'><td>"._('Nick')."</td><td>"._('Role')."</td></tr>";
  112. foreach($server['opers'] as $kk => $oper){
  113. $id++;
  114. $r .= "<tr class='treegrid-{$id} treegrid-parent-{$pid}'><td>{$oper['nick']}</td><td>{$oper['role']}</td></tr>";
  115. }
  116. $r .= "</table>";
  117. }
  118. if(isset($server['parent'])){
  119. $r .= "<h4>"._('Parent')."</h4>";
  120. $r .= get_servers_list_html(Array($server['parent']),$depth+1);
  121. }
  122. if(count($server['children']) > 0){
  123. $r .= "<h4>"._('Children')."</h4>";
  124. $r .= get_servers_list_html($server['children'],$depth+1);
  125. }
  126. $r .="</div>";
  127. }
  128. return $r.'</div>';
  129. }
  130. ?>