Desktop.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. ApplicationSettings::RegisterDefaultSetting("desktop", "mac_address", "");
  3. ApplicationSettings::RegisterDefaultSetting("desktop", "wake_command", "wakeonlan");
  4. ApplicationSettings::RegisterDefaultSetting("desktop", "ip_command", "/usr/sbin/arp -n | grep <mac> | awk '{print $1}'");
  5. ApplicationSettings::RegisterDefaultSetting("desktop", "hardware_monitor_port", "8085");
  6. class Desktop extends Controller {
  7. public function Index() {
  8. return new View("Desktop/index.view");
  9. }
  10. public function Wake() {
  11. $command=ApplicationSettings::GetSetting("desktop", "wake_command");
  12. $mac=ApplicationSettings::GetSetting("desktop", "mac_address");
  13. return $output=shell_exec(sprintf("%s %s", $command, $mac));
  14. }
  15. public function GetDesktopMonitor($file) {
  16. if (!isset($file))
  17. $file="";
  18. $ipCommand=ApplicationSettings::GetSetting("desktop", "ip_command");
  19. $mac=ApplicationSettings::GetSetting("desktop", "mac_address");
  20. $hwmPort=ApplicationSettings::GetSetting("desktop", "hardware_monitor_port");
  21. $ipCommand=str_replace("<mac>", $mac, $ipCommand);
  22. $ipAddress=trim(shell_exec($ipCommand));
  23. if ($ipAddress=="")
  24. return "Waiting for PC to wake<script>GetHWM(5000)</script>";
  25. ob_start();
  26. $ch=curl_init("http://$ipAddress:$hwmPort/".$file);
  27. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 500);
  28. curl_setopt($ch, CURLOPT_TIMEOUT, 500);
  29. $output=curl_exec($ch);
  30. $html=ob_get_clean();
  31. if ($output===true){
  32. switch ($file) {
  33. case "": // remove tags we don't want from html page
  34. preg_match("@<head>(.*?)</head>@ims", $html,$matches);
  35. $head=preg_replace("@\b(href|src)\b=(\"|')(.*?)(\"|')@", "$1=$2/desktop/GetDesktopMonitor?file=$3$4", $matches[1]);
  36. $html=preg_replace("@<head>.*?</head>@ims", $head, $html);
  37. $html=str_replace(array("<html>","</html>","<body>","</body>","<head>","</head>"),"",$html);
  38. $html=preg_replace("@<title>(.*?)</title>@im","",$html);
  39. break;
  40. case "js/ohm_web.js":
  41. $html=str_replace("data.json","/desktop/GetDesktopMonitor?file=data.json",$html);
  42. break;
  43. case "data.json":
  44. $html=preg_replace('@"ImageURL": "(.*?)"@', '"ImageURL": "/desktop/GetDesktopMonitor?file=$1"', $html);
  45. break;
  46. case "css/jquery.treeTable.css":
  47. case "css/custom-theme/jquery-ui-1.8.16.custom.css":
  48. case "css/ohm_web.css":
  49. $html=preg_replace('@url\(("?.*?"?)\)@','url("/desktop/GetDesktopMonitor?file=$1")',$html);
  50. break;
  51. }
  52. return $html;
  53. } else
  54. return "Waiting for hardware monitor to start<script>GetHWM(1000)</script>";
  55. }
  56. }