set_backend_action.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. $json['OcppServerURL'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['OcppServerURL'])))));
  16. if(!preg_match($pattern, $json['OcppServerURL'])){
  17. $jsone['result'] = "Fail";
  18. $jsone['message'] = "OcppServerURL format error, fill it with 'ws://' or 'wss://'";
  19. echo json_encode($jsone);
  20. return false;
  21. }
  22. }
  23. if($_REQUEST['MaintainServerURL']!=""){
  24. $json['MaintainServerURL'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['MaintainServerURL'])))));
  25. if(!preg_match($pattern, $json['MaintainServerURL'])){
  26. $jsone['result'] = "Fail";
  27. $jsone['message'] = "MaintainServerURL format error, fill it with 'ws://' or 'wss://'";
  28. echo json_encode($jsone);
  29. return false;
  30. }
  31. }
  32. checkValue("OfflinePolicy");
  33. checkLength("ChargeBoxId",25);
  34. checkLength("chargePointVendor",20);
  35. if(isset($_REQUEST['BackendConnTimeout'])){
  36. $json['BackendConnTimeout'] = (int)$_REQUEST['BackendConnTimeout'];
  37. }
  38. if(isset($_REQUEST['OfflinePolicy'])){
  39. $json['OfflinePolicy'] = (int)$_REQUEST['OfflinePolicy'];
  40. }
  41. if(isset($_REQUEST['OfflineMaxChargeEnergy'])){
  42. $json['OfflineMaxChargeEnergy'] = (int)$_REQUEST['OfflineMaxChargeEnergy'];
  43. }
  44. if(isset($_REQUEST['OfflineMaxChargeDuration'])){
  45. $json['OfflineMaxChargeDuration'] = (int)$_REQUEST['OfflineMaxChargeDuration'];
  46. }
  47. if(isset($_REQUEST['OcppServerURL'])){
  48. $json['OcppServerURL'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['OcppServerURL'])))));
  49. }
  50. if(isset($_REQUEST['MaintainServerURL'])){
  51. $json['MaintainServerURL'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['MaintainServerURL'])))));
  52. }
  53. if(isset($_REQUEST['ChargeBoxId'])){
  54. $json['ChargeBoxId'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['ChargeBoxId'])))));
  55. }
  56. if(isset($_REQUEST['chargePointVendor'])){
  57. $json['chargePointVendor'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['chargePointVendor'])))));
  58. }
  59. if(isset($_REQUEST['OcppSecurityProfile'])){
  60. checkValue("OcppSecurityProfile");
  61. $json['OcppSecurityProfile'] = (int)$_REQUEST['OcppSecurityProfile'];
  62. }
  63. if(isset($_REQUEST['OcppSecurityPassword'])){
  64. $json['OcppSecurityPassword'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['OcppSecurityPassword'])))));
  65. }
  66. if(isset($_REQUEST['MaintainServerSecurityProfile'])){
  67. checkValue("MaintainServerSecurityProfile");
  68. $json['MaintainServerSecurityProfile'] = (int)$_REQUEST['MaintainServerSecurityProfile'];
  69. }
  70. if(isset($_REQUEST['MaintainServerSecurityPassword'])){
  71. $json['MaintainServerSecurityPassword'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['MaintainServerSecurityPassword'])))));
  72. }
  73. if(isset($_REQUEST['isEnableLocalPowerSharging'])){
  74. checkValue("isEnableLocalPowerSharging");
  75. $json['isEnableLocalPowerSharging'] = (int)$_REQUEST['isEnableLocalPowerSharging'];
  76. }
  77. if(isset($_REQUEST['PowerSharingCapacity'])){
  78. $json['PowerSharingCapacity'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['PowerSharingCapacity'])))));
  79. }
  80. if(isset($_REQUEST['PowerSharingServerIP'])){
  81. $json['PowerSharingServerIP'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['PowerSharingServerIP'])))));
  82. }
  83. if(isset($_REQUEST['OcppReceiptrURL'])){
  84. $json['OcppReceiptrURL'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['OcppReceiptrURL'])))));
  85. }
  86. if(isset($_REQUEST['isEnableTTIA'])){
  87. checkValue("isEnableTTIA");
  88. $json['isEnableTTIA'] = (int)$_REQUEST['isEnableTTIA'];
  89. }
  90. if(isset($_REQUEST['server_addr'])){
  91. $json['server_addr'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['server_addr'])))));
  92. }
  93. if(isset($_REQUEST['server_port'])){
  94. isNumeric("server_port",65535);
  95. $json['server_port'] = (int)$_REQUEST['server_port'];
  96. }
  97. if(isset($_REQUEST['busVenderId'])){
  98. isNumeric("busVenderId",65535);
  99. $json['busVenderId'] = (int)$_REQUEST['busVenderId'];
  100. }
  101. if(isset($_REQUEST['EquipmentProvider'])){
  102. checkLength("EquipmentProvider",15);
  103. $json['EquipmentProvider'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['EquipmentProvider'])))));
  104. }
  105. if(isset($_REQUEST['TransportationCompanyNo'])){
  106. isNumeric("TransportationCompanyNo",255);
  107. $json['TransportationCompanyNo'] = (int)$_REQUEST['TransportationCompanyNo'];
  108. }
  109. if(isset($_REQUEST['TTIAChargeBoxId'])){
  110. isNumeric("TTIAChargeBoxId",255);
  111. $json['TTIAChargeBoxId'] = (int)$_REQUEST['TTIAChargeBoxId'];
  112. }
  113. if(isset($_REQUEST['evseStation'])){
  114. checkLength("evseStation",15);
  115. $json['evseStation'] = str_replace("&amp;","&",str_replace("&quot;",'"',str_replace("&#039;","'",str_replace("&lt;","<",str_replace("&gt;",">",$_REQUEST['evseStation'])))));
  116. }
  117. $fCount = count($_FILES['private_key']['name']);
  118. if($fCount>0){
  119. if ($_FILES["private_key"]["error"] > 0)
  120. {
  121. echo "Return Code: " . $_FILES["private_key"]["error"] . "<br />";
  122. }
  123. sleep(1);
  124. move_uploaded_file($_FILES["private_key"]["tmp_name"],"/Storage/OCPP/certCP.key");
  125. }
  126. $fCount = count($_FILES['certificate']['name']);
  127. if($fCount>0){
  128. if ($_FILES["certificate"]["error"] > 0)
  129. {
  130. echo "Return Code: " . $_FILES["certificate"]["error"] . "<br />";
  131. }
  132. sleep(1);
  133. move_uploaded_file($_FILES["certificate"]["tmp_name"],"/Storage/OCPP/certCP.pem");
  134. }
  135. // ob_start();
  136. shell_exec('sync;sync;sync');
  137. chdir("/root");
  138. $str_json=json_encode($json);//var_dump($str_json);
  139. exec("'./WebService' '4' '".$str_json."'",$output,$return_var);
  140. if(count($output)!=0){
  141. $jsone['result'] = "Success";
  142. $jsone['message'] = $json;
  143. echo json_encode($jsone);
  144. exit;
  145. }
  146. else{
  147. $jsone['result'] = "Error";
  148. $jsone['message'] = "Something went wrong on machine";
  149. echo json_encode($jsone);
  150. return false;
  151. exit;
  152. }
  153. // ob_end_clean();
  154. }
  155. function checkValue($id){
  156. if($_REQUEST[$id] != ""){
  157. if(strlen($_REQUEST[$id])!=1){
  158. $jsone['result'] = "Fail";
  159. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  160. echo json_encode($jsone);
  161. exit;
  162. }
  163. if(!is_numeric($_REQUEST[$id])){
  164. $jsone['result'] = "Fail";
  165. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  166. echo json_encode($jsone);
  167. exit;
  168. }
  169. }
  170. }
  171. function checkLength($id,$value){
  172. if(strlen($_REQUEST[$id])>$value){
  173. $jsone['result'] = "Fail";
  174. $jsone['message'] = "Length of " . $id . " should be less than ".$value+"(Chinese 5 words)";
  175. echo json_encode($jsone);
  176. exit;
  177. }
  178. }
  179. function isNumeric($id,$value){
  180. if($_REQUEST[$id] != ""){
  181. if(!is_numeric($_REQUEST[$id])){
  182. $jsone['result'] = "Fail";
  183. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  184. echo json_encode($jsone);
  185. exit;
  186. }
  187. if($_REQUEST[$id]>$value){
  188. $jsone['result'] = "Fail";
  189. $jsone['message'] = "Value of " . $id . " should be less than ".$value;
  190. echo json_encode($jsone);
  191. exit;
  192. }
  193. }
  194. }
  195. ?>