set_backend_action.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. // 設置資料類型 json,編碼格式 utf-8
  3. header('Content-Type: application/json; charset=UTF-8');
  4. // 判斷如果是 GET 請求,則進行搜尋;如果是 POST 請求,則進行新建
  5. // $_SERVER['REQUEST_METHOD'] 返回訪問頁面使用的請求方法
  6. if ($_SERVER['REQUEST_METHOD'] == "GET") {
  7. create();
  8. } else if ($_SERVER['REQUEST_METHOD'] == "POST") {
  9. create();
  10. }
  11. // 新建員工
  12. function create() {
  13. $pattern="/^(ws|wss):\/\/((([0-9]{1,3}\.){3}[0-9]{1,3})|(([a-zA-Z0-9]+(([\-]?[a-zA-Z0-9]+)*\.)+)*[a-zA-Z]{2,}))+/";
  14. if($_REQUEST['OcppServerURL']!=""){
  15. if(!preg_match($pattern, $_REQUEST['OcppServerURL'])){
  16. $jsone['result'] = "Fail";
  17. $jsone['message'] = "OcppServerURL format error, fill it with 'ws://' or 'wss://'";
  18. echo json_encode($jsone);
  19. return false;
  20. }
  21. }
  22. checkValue("OfflinePolicy");
  23. checkLength("ChargeBoxId",25);
  24. checkLength("chargePointVendor",20);
  25. if(isset($_REQUEST['BackendConnTimeout'])){
  26. $json['BackendConnTimeout'] = (int)$_REQUEST['BackendConnTimeout'];
  27. }
  28. if(isset($_REQUEST['OfflinePolicy'])){
  29. $json['OfflinePolicy'] = (int)$_REQUEST['OfflinePolicy'];
  30. }
  31. if(isset($_REQUEST['OfflineMaxChargeEnergy'])){
  32. $json['OfflineMaxChargeEnergy'] = (int)$_REQUEST['OfflineMaxChargeEnergy'];
  33. }
  34. if(isset($_REQUEST['OfflineMaxChargeDuration'])){
  35. $json['OfflineMaxChargeDuration'] = (int)$_REQUEST['OfflineMaxChargeDuration'];
  36. }
  37. if(isset($_REQUEST['OcppServerURL'])){
  38. $json['OcppServerURL'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['OcppServerURL'])))));
  39. }
  40. if(isset($_REQUEST['ChargeBoxId'])){
  41. $json['ChargeBoxId'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['ChargeBoxId'])))));
  42. }
  43. if(isset($_REQUEST['chargePointVendor'])){
  44. $json['chargePointVendor'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['chargePointVendor'])))));
  45. }
  46. if(isset($_REQUEST['OcppSecurityProfile'])){
  47. checkValue("OcppSecurityProfile");
  48. $json['OcppSecurityProfile'] = (int)$_REQUEST['OcppSecurityProfile'];
  49. }
  50. if(isset($_REQUEST['OcppSecurityPassword'])){
  51. $json['OcppSecurityPassword'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['OcppSecurityPassword'])))));
  52. }
  53. if(isset($_REQUEST['isEnableLocalPowerSharging'])){
  54. checkValue("isEnableLocalPowerSharging");
  55. $json['isEnableLocalPowerSharging'] = (int)$_REQUEST['isEnableLocalPowerSharging'];
  56. }
  57. if(isset($_REQUEST['OcppReceiptrURL'])){
  58. $json['OcppReceiptrURL'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['OcppReceiptrURL'])))));
  59. }
  60. if(isset($_REQUEST['isEnableTTIA'])){
  61. checkValue("isEnableTTIA");
  62. $json['isEnableTTIA'] = (int)$_REQUEST['isEnableTTIA'];
  63. }
  64. if(isset($_REQUEST['server_addr'])){
  65. $json['server_addr'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['server_addr'])))));
  66. }
  67. if(isset($_REQUEST['server_port'])){
  68. isNumeric("server_port",65535);
  69. $json['server_port'] = (int)$_REQUEST['server_port'];
  70. }
  71. if(isset($_REQUEST['busVenderId'])){
  72. isNumeric("busVenderId",65535);
  73. $json['busVenderId'] = (int)$_REQUEST['busVenderId'];
  74. }
  75. if(isset($_REQUEST['EquipmentProvider'])){
  76. checkLength("EquipmentProvider",15);
  77. $json['EquipmentProvider'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['EquipmentProvider'])))));
  78. }
  79. if(isset($_REQUEST['TransportationCompanyNo'])){
  80. isNumeric("TransportationCompanyNo",255);
  81. $json['TransportationCompanyNo'] = (int)$_REQUEST['TransportationCompanyNo'];
  82. }
  83. if(isset($_REQUEST['TTIAChargeBoxId'])){
  84. isNumeric("TTIAChargeBoxId",255);
  85. $json['TTIAChargeBoxId'] = (int)$_REQUEST['TTIAChargeBoxId'];
  86. }
  87. if(isset($_REQUEST['evseStation'])){
  88. checkLength("evseStation",15);
  89. $json['evseStation'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['evseStation'])))));
  90. }
  91. // ob_start();
  92. shell_exec('sync;sync;sync');
  93. chdir("/root");
  94. $str_json=json_encode($json);//var_dump($str_json);
  95. exec("'./WebService' '4' '".$str_json."'",$output,$return_var);
  96. if(count($output)!=0){
  97. $jsone['result'] = "Success";
  98. $jsone['message'] = $json;
  99. echo json_encode($jsone);
  100. exit;
  101. }
  102. else{
  103. $jsone['result'] = "Error";
  104. $jsone['message'] = "Something went wrong on machine";
  105. echo json_encode($jsone);
  106. return false;
  107. exit;
  108. }
  109. // ob_end_clean();
  110. }
  111. function checkValue($id){
  112. if($_REQUEST[$id] != ""){
  113. if(strlen($_REQUEST[$id])!=1){
  114. $jsone['result'] = "Fail";
  115. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  116. echo json_encode($jsone);
  117. exit;
  118. }
  119. if(!ereg("[0-9]",$_REQUEST[$id])){
  120. $jsone['result'] = "Fail";
  121. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  122. echo json_encode($jsone);
  123. exit;
  124. }
  125. }
  126. }
  127. function checkLength($id,$value){
  128. if(strlen($_REQUEST[$id])>$value){
  129. $jsone['result'] = "Fail";
  130. $jsone['message'] = "Length of " . $id . " should be less than ".$value+"(Chinese 5 words)";
  131. echo json_encode($jsone);
  132. exit;
  133. }
  134. }
  135. function isNumeric($id,$value){
  136. if($_REQUEST[$id] != ""){
  137. if(!ereg("[0-9]",$_REQUEST[$id])){
  138. $jsone['result'] = "Fail";
  139. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  140. echo json_encode($jsone);
  141. exit;
  142. }
  143. if($_REQUEST[$id]>$value){
  144. $jsone['result'] = "Fail";
  145. $jsone['message'] = "Value of " . $id . " should be less than ".$value;
  146. echo json_encode($jsone);
  147. exit;
  148. }
  149. }
  150. }
  151. ?>