set_backend_action.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // ob_start();
  58. shell_exec('sync;sync;sync');
  59. chdir("/root");
  60. $str_json=json_encode($json);//var_dump($str_json);
  61. exec("'./WebService' '4' '".$str_json."'",$output,$return_var);
  62. if(count($output)!=0){
  63. $jsone['result'] = "Success";
  64. $jsone['message'] = $json;
  65. echo json_encode($jsone);
  66. exit;
  67. }
  68. else{
  69. $jsone['result'] = "Error";
  70. $jsone['message'] = "Something went wrong on machine";
  71. echo json_encode($jsone);
  72. return false;
  73. exit;
  74. }
  75. // ob_end_clean();
  76. }
  77. function checkValue($id){
  78. if($_REQUEST[$id] != ""){
  79. if(strlen($_REQUEST[$id])!=1){
  80. $jsone['result'] = "Fail";
  81. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  82. echo json_encode($jsone);
  83. exit;
  84. }
  85. if(!ereg("[0-9]",$_REQUEST[$id])){
  86. $jsone['result'] = "Fail";
  87. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  88. echo json_encode($jsone);
  89. exit;
  90. }
  91. }
  92. }
  93. function checkLength($id,$value){
  94. if(strlen($_REQUEST[$id])>$value){
  95. $jsone['result'] = "Fail";
  96. $jsone['message'] = "Length of " . $id . " should be less than ".$value;
  97. echo json_encode($jsone);
  98. exit;
  99. }
  100. }
  101. ?>