Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. <?
  2. $fbroot ='http://fritz.box:49000'; //Adresse + Port (immer 49000)
  3. $desc = "igddesc.xml"; //Hier sind die Infos über die Dienste enthalten
  4. $SCPD = "igdicfgSCPD.xml"; // Hier sind die Infos über die Funktionen und Parameter/Variablen enthalten, sowie die Definition der Variablen Typen.
  5. $action="GetAddonInfos"; //Diese Funktion soll ausgeführt werden
  6.  
  7. // FB_getServiceData holt alle benötigten Daten aus der $desc XML
  8. $service = FB_getServiceData($fbroot,$desc,$SCPD,$action);
  9. //$service =
  10. /*array(3) {
  11. ["uri"]=>
  12. string(55) "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1"
  13. ["location"]=>
  14. string(52) "http://fritz.box:49000/igdupnp/control/WANCommonIFC1"
  15. ["SCPDURL"]=>
  16. string(15) "igdicfgSCPD.xml"
  17. }
  18. */
  19. // FB_getStateVars holt die Definition der Variablen aus der SCPD XML für die gewählte Aktion
  20. // Wen die die Typen kennt/manuell die IPS-Variablen befüllt, braucht Ihr diese Funktion nicht ausführen.
  21. $StateVars = FB_getStateVars ($fbroot,$service,$action);
  22. /*
  23. $StateVars = (Beispiel)
  24. array(14) {
  25. ["NewByteSendRate"]=>
  26. string(3) "ui4"
  27. ["NewByteReceiveRate"]=>
  28. string(3) "ui4"
  29. ["NewPacketSendRate"]=>
  30. string(3) "ui4"
  31. ["NewPacketReceiveRate"]=>
  32. string(3) "ui4"
  33. ["NewTotalBytesSent"]=>
  34. string(3) "ui4"
  35. ["NewTotalBytesReceived"]=>
  36. string(3) "ui4"
  37. ["NewAutoDisconnectTime"]=>
  38. string(3) "ui4"
  39. ["NewIdleDisconnectTime"]=>
  40. string(3) "ui4"
  41. ["NewDNSServer1"]=>
  42. string(6) "string"
  43. ["NewDNSServer2"]=>
  44. string(6) "string"
  45. ["NewVoipDNSServer1"]=>
  46. string(6) "string"
  47. ["NewVoipDNSServer2"]=>
  48. string(6) "string"
  49. ["NewUpnpControlEnabled"]=>
  50. string(7) "boolean"
  51. ["NewRoutedBridgedModeBoth"]=>
  52. string(7) "boolean"
  53. }
  54. */
  55.  
  56. // FB_SoapAction($service,$action,$user = false,$pass = false) führt den SOAP Request aus. Variablen wie oben beschrieben. $service könnt ihr auch manuell befüllen, dann wird FB_getServiceData auch nicht benötigt.
  57. $result = FB_SoapAction($service,$action);
  58. /*
  59. $result = (Beispiel)
  60. array(14) {
  61. ["NewByteSendRate"]=>
  62. string(5) "14380"
  63. ["NewByteReceiveRate"]=>
  64. string(6) "900533"
  65. ["NewPacketSendRate"]=>
  66. string(3) "209"
  67. ["NewPacketReceiveRate"]=>
  68. string(3) "605"
  69. ["NewTotalBytesSent"]=>
  70. string(8) "27991885"
  71. ["NewTotalBytesReceived"]=>
  72. string(10) "1116689220"
  73. ["NewAutoDisconnectTime"]=>
  74. string(1) "0"
  75. ["NewIdleDisconnectTime"]=>
  76. string(1) "0"
  77. ["NewDNSServer1"]=>
  78. string(14) "111.111.111"
  79. ["NewDNSServer2"]=>
  80. string(14) "111.111.111"
  81. ["NewVoipDNSServer1"]=>
  82. string(14) "111.111.111"
  83. ["NewVoipDNSServer2"]=>
  84. string(14) "111.111.111"
  85. ["NewUpnpControlEnabled"]=>
  86. string(1) "0"
  87. ["NewRoutedBridgedModeBoth"]=>
  88. string(1) "0"
  89. }
  90. */
  91.  
  92. // UpdateIPSvar($parent,$ident,$value,$type) ist eine 'typische' Hilfsfunktion, welche eine Variable erstellt wenn sie nicht vorhanden ist und sie mit den aktuellen Daten (bei Änderung) befüllt.
  93. // $parent = IPS_ID welche der Parent der Variable ist /wird
  94. // $ident = IDENT der Variable zum wiederfinden (wird beim anlegen auch als Name genutzt; Name kann jederzeit geändert werden im Objektbaum)
  95. // $value = Neuer Wert der Variable
  96. // $type = String welcher den Type der Variable im FritzBox-Format enthält. Oder als int den Typ in IPS-Format (0 = bool, 1 = int, 2 = float, 3 = string)
  97. UpdateIPSvar(IPS_GetParent($_IPS['SELF']),'KByteSendRate',$result['NewByteSendRate']/1024,2);
  98. UpdateIPSvar(IPS_GetParent($_IPS['SELF']),'KByteReceiveRate',$result['NewByteReceiveRate']/1024,2);
  99.  
  100.  
  101. function FB_getServiceData($fbroot,$desc,$SCPD,$action)
  102. {
  103. $xml = @simplexml_load_file($fbroot.'/'.$desc);
  104. if ($xml === false)
  105. {
  106. echo "Not found:".$descXML.PHP_EOL;
  107. return false;
  108. }
  109. $xml->registerXPathNamespace('fb', $xml->getNameSpaces(false)[""]);
  110. $xmlservice = $xml->xpath("//fb:service[fb:SCPDURL='/".$SCPD."']");
  111. $service['uri'] = (string)$xmlservice[0]->serviceType;
  112. $service['location'] =$fbroot.(string)$xmlservice[0]->controlURL;
  113. $service['SCPDURL'] =trim((string)$xmlservice[0]->SCPDURL,"/");
  114. return $service;
  115. }
  116.  
  117. function FB_getStateVars ($fbroot,$service,$action)
  118. {
  119. $xmlDesc = @simplexml_load_file($fbroot.'/'.$service['SCPDURL']);
  120. if ($xmlDesc === false)
  121. {
  122. echo "Not found:".$service['SCPDURL'].PHP_EOL;
  123. return false;
  124. }
  125. $xmlDesc->registerXPathNamespace('fritzbox', $xmlDesc->getNameSpaces(false)[""]);
  126. $xmlArgumentList = $xmlDesc->xpath("//fritzbox:actionList/fritzbox:action[fritzbox:name='".$action."']/fritzbox:argumentList/fritzbox:argument");
  127. $StateVariables=false;
  128. foreach ($xmlArgumentList as $xmlArgument)
  129. {
  130. $xmlStateVariable = $xmlDesc->xpath("//fritzbox:stateVariable[fritzbox:name='".(string)$xmlArgument->relatedStateVariable."']");
  131. $StateVariables[(string)$xmlArgument->name] = (string)$xmlStateVariable[0]->dataType;
  132. }
  133. return $StateVariables;
  134. }
  135.  
  136. function FB_SoapAction($service,$action,$user = false,$pass = false)
  137. {
  138. $service['noroot'] = true;
  139. $service['trace'] = false;
  140. $service['exceptions'] = false;
  141. if (!($user === false))
  142. {
  143. $service['login'] = $user;
  144. $service['password'] = $pass;
  145. }
  146. $client = new SoapClient(null,$service);
  147. $status = $client->{$action}();
  148. if (is_soap_fault($status))
  149. {
  150. return false;
  151. }
  152. return $status;
  153. }
  154.  
  155. function UpdateIPSvar($parent,$ident,$value,$type)
  156. {
  157. $ident=str_replace(array("_","-"),array("",""),$ident);
  158. if (is_int($type))
  159. {
  160. $ips_type=$type;
  161. } else {
  162. switch ($type)
  163. {
  164. case "i1":
  165. case "i2":
  166. case "i4":
  167. case "ui1":
  168. case "ui2":
  169. case "ui4":
  170. $ips_type=1;
  171. break;
  172. case "boolean":
  173. $ips_type=0;
  174. break;
  175. case "uuid":
  176. case "dateTime":
  177. case "string":
  178. $ips_type=3;
  179. break;
  180. default:
  181. echo "Unbekannter Datentyp:".$type.PHP_EOL;
  182. return;
  183. break;
  184. }
  185. }
  186. $var_id = @IPS_GetObjectIDByIdent($ident,$parent);
  187. if ($var_id === false)
  188. {
  189. $var_id = IPS_CreateVariable($ips_type);
  190. IPS_SetName($var_id,$ident);
  191. IPS_SetIdent($var_id,$ident);
  192. IPS_SetParent($var_id,$parent);
  193. }
  194. switch ($ips_type)
  195. {
  196. case 0:
  197. if (GetValueBoolean($var_id) <> (bool)$value)
  198. {
  199. SetValueBoolean($var_id,(bool)$value);
  200. }
  201. break;
  202. case 1:
  203. if (GetValueInteger($var_id) <> (int)$value)
  204. {
  205. SetValueInteger($var_id,(int)$value);
  206. }
  207. break;
  208. case 2:
  209. if (GetValueFloat($var_id) <> round((float)$value,2))
  210. {
  211. SetValueFloat($var_id,round((float)$value,2));
  212. }
  213. break;
  214. case 3:
  215. if (GetValueString($var_id) <> $value)
  216. {
  217. SetValueString($var_id,$value);
  218. }
  219. break;
  220. }
  221. }
  222. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement