PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/endpoint/grandstream/base.php

https://github.com/mx504/Provisioner
PHP | 135 lines | 96 code | 22 blank | 17 comment | 16 complexity | d57aa39dfe3ee72489f99561a31e3b8e MD5 | raw file
  1. <?PHP
  2. /**
  3. * Grandstream Base File
  4. *
  5. * @author Andrew Nagy
  6. * @license MPL / GPLv2 / LGPL
  7. * @package Provisioner
  8. */
  9. class endpoint_grandstream_base extends endpoint_base {
  10. public $brand_name = 'grandstream';
  11. function reboot() {
  12. if(($this->engine == "asterisk") AND ($this->system == "unix")) {
  13. exec($this->engine_location. " -rx 'sip show peers like ".$this->lines[1]['ext']."'", $output);
  14. if(preg_match("/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/",$output[1],$matches)) {
  15. $ip = $matches[0];
  16. $pass = (isset($this->options['admin_pass']) ? $this->options['admin_pass'] : 'admin');
  17. //This is lame. I need to do this in php not over the command line. etc, I AM THE LAME.
  18. exec('curl -c cookies.txt -d"P2='.$pass.'&Login=Login&gnkey=0b82" http://'.$ip.'/dologin.htm');
  19. exec("curl -b cookies.txt http://".$ip."/rs.htm");
  20. }
  21. }
  22. }
  23. function create_encrypted_file($list) {
  24. foreach($list as $key=>$data) {
  25. $fp = fopen($this->root_dir. self::$modules_path . $this->brand_name . "/" . $this->family_line . "/".$key, 'w');
  26. fwrite($fp, $data);
  27. fclose($fp);
  28. if(file_exists("/usr/src/GS_CFG_GEN/bin/encode.sh")) {
  29. exec("/usr/src/GS_CFG_GEN/bin/encode.sh ".$this->mac." ".$this->root_dir. self::$modules_path . $this->brand_name . "/" . $this->family_line . "/".$this->mac.".cfg ".$this->root_dir. self::$modules_path . $this->brand_name . "/" . $this->family_line . "/cfg".$this->mac);
  30. $handle = fopen($this->root_dir. self::$modules_path . $this->brand_name . "/" . $this->family_line . "/cfg".$this->mac, 'rb');
  31. $contents = stream_get_contents($handle);
  32. fclose($handle);
  33. unlink($this->root_dir. self::$modules_path . $this->brand_name . "/" . $this->family_line . "/cfg".$this->mac);
  34. } else {
  35. $params = $this->parse_gs_config($this->root_dir. self::$modules_path . $this->brand_name . "/" . $this->family_line . "/".$key);
  36. $contents = $this->gs_config_out($this->mac,$params);
  37. }
  38. $files["cfg".$this->mac] = $contents;
  39. unlink($this->root_dir. self::$modules_path . $this->brand_name . "/" . $this->family_line . "/".$key);
  40. }
  41. return($files);
  42. }
  43. function parse_gs_config ($filename)
  44. {
  45. if (!($f = @fopen ($filename, "r"))) {
  46. echo ("Unable to open " . $filename . "\n");
  47. return FALSE;
  48. }
  49. while ($str = fgets ($f)) {
  50. if (($pos = strpos ($str, "#")) !== FALSE) {
  51. $str = substr ($str, 0, $pos);
  52. }
  53. if (strlen($str)) {
  54. if (preg_match ("/(.+)=(.*)/", $str, $matches)) {
  55. $params[trim($matches[1])] = trim($matches[2]);
  56. }
  57. }
  58. }
  59. fclose ($f);
  60. return $params;
  61. }
  62. function prepare_for_generateconfig() {
  63. //Grandstream likes lower case letters in its mac address
  64. $this->mac = strtolower($this->mac);
  65. parent::prepare_for_generateconfig();
  66. $this->timezone['timezone']=720+$this->timezone['gmtoffset']/60;
  67. }
  68. function generate_file($file,$extradata,$ignoredynamicmapping=FALSE) {
  69. $data=parent::generate_file($file,$extradata,$ignoredynamicmapping);
  70. if ($ignoredynamicmapping==FALSE) {
  71. $data = array_values($this->create_encrypted_file(array("_tmp"=>$data)));
  72. $data=$data[0];
  73. }
  74. return $data;
  75. }
  76. // MAC : 12 hex digits string
  77. // $params : array ("P01" => "something", ...)
  78. function gs_config_out ($mac, $params)
  79. {
  80. $prev = 0;
  81. //if (!preg_match ("/^[0-9a-fA-F]{12}$/", $mac))
  82. // return FALSE;
  83. $params["gnkey"] = "0b82";
  84. $str = "";
  85. foreach ($params as $key => $val) {
  86. if ($prev)
  87. $str .= "&";
  88. else
  89. $prev = 1;
  90. $str .= $key . "=" . $val;
  91. }
  92. if (strlen ($str) & 1) $str .= chr(0);
  93. // Insert the beginning
  94. $new_str = chr(0) . chr(0) . chr((16+strlen ($str)) / 2 >> 8 & 0xff) . chr((16+strlen ($str)) / 2 & 0xff) . chr(0) . chr(0);
  95. // Insert the MAC address
  96. for ($i = 0; $i < 6; $i++) {
  97. $new_str .= chr(hexdec (substr ($mac, $i*2, 2)));
  98. }
  99. // Insert the end of the first line
  100. $new_str .= chr(13) . chr(10) . chr(13) . chr(10) . $str;
  101. // Basic checksum
  102. $k = 0;
  103. for ($i = 0; $i < strlen ($new_str) / 2; $i++) {
  104. $k += ord($new_str[$i*2]) << 8 & 0xff00;
  105. $k += ord($new_str[$i*2 + 1]) & 0xff;
  106. $k &= 0xffff;
  107. }
  108. $k = 0x10000 - $k;
  109. $new_str[4] = chr($k >> 8 & 0xff);
  110. $new_str[5] = chr($k & 0xff);
  111. return $new_str;
  112. }
  113. }