pineapple.php 3.0 KB

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