pineapple.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php namespace helper;
  2. function execBackground($command)
  3. {
  4. exec("echo \"{$command}\" | /usr/bin/at now", $var);
  5. return $var;
  6. }
  7. function checkDependency($dependencyName)
  8. {
  9. exec("/usr/bin/which $dependencyName", $output);
  10. return !(trim($output[0]) === "");
  11. }
  12. function isSDAvailable()
  13. {
  14. $output = exec('/bin/mount | /bin/grep "on /sd" -c');
  15. return $output >= 1;
  16. }
  17. function sdReaderPresent() {
  18. return file_exists('/sd');
  19. }
  20. function sdCardPresent() {
  21. return !file_exists('/sd/NO_SD');
  22. }
  23. function checkRunning($processName)
  24. {
  25. $processName = escapeshellarg($processName);
  26. exec("/usr/bin/pgrep {$processName}", $output);
  27. return count($output) > 0;
  28. }
  29. function checkRunningFull($processString) {
  30. $processString = escapeshellarg($processString);
  31. exec("/usr/bin/pgrep -f {$processString}", $output);
  32. return count($output) > 0;
  33. }
  34. function udsSend($path, $message)
  35. {
  36. $sock = stream_socket_client("unix://{$path}", $errno, $errstr);
  37. fwrite($sock, $message);
  38. fclose($sock);
  39. return true;
  40. }
  41. function dgramUdsSend($path, $message)
  42. {
  43. $sock = NULL;
  44. if(!($sock = socket_create(AF_UNIX, SOCK_DGRAM, 0))) {
  45. return false;
  46. }
  47. socket_sendto($sock, $message, strlen($message), 0, $path);
  48. return true;
  49. }
  50. function uciGet($uciString, $autoBool = true)
  51. {
  52. $uciString = escapeshellarg($uciString);
  53. $result = exec("uci get {$uciString}");
  54. if ($autoBool && ($result === "0" || $result === "1")) {
  55. return $result === "1";
  56. }
  57. return $result;
  58. }
  59. function uciSet($settingString, $value)
  60. {
  61. $settingString = escapeshellarg($settingString);
  62. if (!empty($value)) {
  63. $value = escapeshellarg($value);
  64. }
  65. if ($value === "''" || $value === "") {
  66. $value = "'0'";
  67. }
  68. exec("uci set {$settingString}={$value}");
  69. exec("uci commit {$settingString}");
  70. }
  71. function uciAddList($settingString, $value)
  72. {
  73. $settingString = escapeshellarg($settingString);
  74. if (!empty($value)) {
  75. $value = escapeshellarg($value);
  76. }
  77. if ($value === "''") {
  78. $value = "'0'";
  79. }
  80. exec("uci add_list {$settingString}={$value}");
  81. exec("uci commit {$settingString}");
  82. }
  83. function downloadFile($file)
  84. {
  85. $token = hash('sha256', $file . time());
  86. require_once('DatabaseConnection.php');
  87. $database = new \pineapple\DatabaseConnection("/etc/pineapple/pineapple.db");
  88. $database->exec("CREATE TABLE IF NOT EXISTS downloads (token VARCHAR NOT NULL, file VARCHAR NOT NULL, time timestamp default (strftime('%s', 'now')));");
  89. $database->exec("INSERT INTO downloads (token, file) VALUES ('%s', '%s')", $token, $file);
  90. return $token;
  91. }
  92. function getFirmwareVersion()
  93. {
  94. return trim(file_get_contents('/etc/pineapple/pineapple_version'));
  95. }
  96. function getDevice()
  97. {
  98. $data = file_get_contents('/proc/cpuinfo');
  99. if (preg_match('/NANO/', $data)) {
  100. return 'nano';
  101. } elseif (preg_match('/TETRA/', $data)) {
  102. return 'tetra';
  103. }
  104. return 'unknown';
  105. }
  106. function getMacFromInterface($interface)
  107. {
  108. $interface = escapeshellarg($interface);
  109. return trim(exec("ifconfig {$interface} | grep HWaddr | awk '{print $5}'"));
  110. }
  111. function getBoard()
  112. {
  113. $data = @file_get_contents('/tmp/sysinfo/board_name');
  114. if (!empty($data)) {
  115. $parts = explode(',', $data);
  116. return isset($parts[1]) ? $parts[1] : $parts[0];
  117. }
  118. return false;
  119. }