set_backend_action.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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($_POST['OcppServerURL']!=""){
  15. if(!preg_match($pattern, $_POST['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($_POST['BackendConnTimeout'])){
  26. $json['BackendConnTimeout'] = (int)$_POST['BackendConnTimeout'];
  27. }
  28. if(isset($_POST['OfflinePolicy'])){
  29. $json['OfflinePolicy'] = (int)$_POST['OfflinePolicy'];
  30. }
  31. if(isset($_POST['OfflineMaxChargeEnergy'])){
  32. $json['OfflineMaxChargeEnergy'] = (int)$_POST['OfflineMaxChargeEnergy'];
  33. }
  34. if(isset($_POST['OfflineMaxChargeDuration'])){
  35. $json['OfflineMaxChargeDuration'] = (int)$_POST['OfflineMaxChargeDuration'];
  36. }
  37. if(isset($_POST['OcppServerURL'])){
  38. $json['OcppServerURL'] = $_POST['OcppServerURL'];
  39. }
  40. if(isset($_POST['ChargeBoxId'])){
  41. $json['ChargeBoxId'] = $_POST['ChargeBoxId'];
  42. }
  43. if(isset($_POST['chargePointVendor'])){
  44. $json['chargePointVendor'] = $_POST['chargePointVendor'];
  45. }
  46. // ob_start();
  47. shell_exec('sync;sync;sync');
  48. chdir("/root");
  49. $str_json=json_encode($json);//var_dump($str_json);
  50. exec("'./WebService' '4' '".$str_json."'",$output,$return_var);
  51. if(count($output)!=0){
  52. $jsone['result'] = "Success";
  53. $jsone['message'] = $json;
  54. echo json_encode($jsone);
  55. exit;
  56. }
  57. else{
  58. $jsone['result'] = "Error";
  59. $jsone['message'] = "Something went wrong on machine";
  60. echo json_encode($jsone);
  61. return false;
  62. exit;
  63. }
  64. // ob_end_clean();
  65. }
  66. function checkValue($id){
  67. if($_POST[$id] != ""){
  68. if(strlen($_POST[$id])!=1){
  69. $jsone['result'] = "Fail";
  70. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  71. echo json_encode($jsone);
  72. exit;
  73. }
  74. if(!ereg("[0-9]",$_POST[$id])){
  75. $jsone['result'] = "Fail";
  76. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  77. echo json_encode($jsone);
  78. exit;
  79. }
  80. }
  81. }
  82. function checkLength($id,$value){
  83. if(strlen($_POST[$id])>$value){
  84. $jsone['result'] = "Fail";
  85. $jsone['message'] = "Length of " . $id . " should be less than ".$value;
  86. echo json_encode($jsone);
  87. exit;
  88. }
  89. }
  90. ?>