set_backend_action.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 = empty($_FILES['private_key']['tmp_name'][0]);
  118. if($fCount!=1){
  119. if ($_FILES["private_key"]["error"] > 0)
  120. {
  121. $jsone['result'] = "Error";
  122. $jsone['message'] = $_FILES["private_key"]["error"];
  123. echo json_encode($jsone);
  124. exit;
  125. }
  126. else if(strpos(exec('openssl rsa -noout -modulus -in '.$_FILES['private_key']['tmp_name']),"Modulus")===false){ //unable to load Private Key
  127. $jsone['result'] = "Error";
  128. $jsone['message'] = "format of Private Key is incorrect";
  129. echo json_encode($jsone);
  130. exit;
  131. }
  132. sleep(1);
  133. move_uploaded_file($_FILES["private_key"]["tmp_name"],"/Storage/OCPP/certCP.key");
  134. }
  135. $fCount = empty($_FILES['certificate']['tmp_name'][0]);
  136. if($fCount!=1){
  137. if ($_FILES["certificate"]["error"] > 0)
  138. {
  139. $jsone['result'] = "Error";
  140. $jsone['message'] = $_FILES["certificate"]["error"];
  141. echo json_encode($jsone);
  142. exit;
  143. }
  144. else if(strpos(exec('openssl x509 -noout -modulus -in '.$_FILES['certificate']['tmp_name']),"Modulus")===false){ //unable to load certificate
  145. $jsone['result'] = "Error";
  146. $jsone['message'] = "format of Certificate is incorrect";
  147. echo json_encode($jsone);
  148. exit;
  149. }
  150. sleep(1);
  151. move_uploaded_file($_FILES["certificate"]["tmp_name"],"/Storage/OCPP/certCP.pem");
  152. }
  153. $fCount = empty($_FILES['MORootCertificate']['tmp_name'][0]);
  154. if($fCount!=1){
  155. if ($_FILES["MORootCertificate"]["error"] > 0)
  156. {
  157. $jsone['result'] = "Error";
  158. $jsone['message'] = $_FILES["MORootCertificate"]["error"];
  159. echo json_encode($jsone);
  160. exit;
  161. }
  162. else if(strpos(exec('openssl x509 -noout -modulus -in '.$_FILES['MORootCertificate']['tmp_name']),"Modulus")===false){ //unable to load MORootCertificate
  163. $jsone['result'] = "Error";
  164. $jsone['message'] = "format of MO Root Certificate is incorrect";
  165. echo json_encode($jsone);
  166. exit;
  167. }
  168. sleep(1);
  169. move_uploaded_file($_FILES["MORootCertificate"]["tmp_name"],"/Storage/rootCaMo.pem");
  170. }
  171. $fCount = empty($_FILES['CSRootCertificate']['tmp_name'][0]);
  172. if($fCount!=1){
  173. if ($_FILES["CSRootCertificate"]["error"] > 0)
  174. {
  175. $jsone['result'] = "Error";
  176. $jsone['message'] = $_FILES["CSRootCertificate"]["error"];
  177. echo json_encode($jsone);
  178. exit;
  179. }
  180. else if(strpos(exec('openssl x509 -noout -modulus -in '.$_FILES['CSRootCertificate']['tmp_name']),"Modulus")===false){ //unable to load CSRootCertificate
  181. $jsone['result'] = "Error";
  182. $jsone['message'] = "format of CS Root Certificate is incorrect";
  183. echo json_encode($jsone);
  184. exit;
  185. }
  186. sleep(1);
  187. move_uploaded_file($_FILES["CSRootCertificate"]["tmp_name"],"/Storage/OCPP/rootCaCs.pem");
  188. }
  189. $fCount = empty($_FILES['ManufacturerRootCertificate']['tmp_name'][0]);
  190. if($fCount!=1){
  191. if ($_FILES["ManufacturerRootCertificate"]["error"] > 0)
  192. {
  193. $jsone['result'] = "Error";
  194. $jsone['message'] = $_FILES["ManufacturerRootCertificate"]["error"];
  195. echo json_encode($jsone);
  196. exit;
  197. }
  198. else if(strpos(exec('openssl x509 -noout -modulus -in '.$_FILES['ManufacturerRootCertificate']['tmp_name']),"Modulus")===false){ //unable to load ManufacturerRootCertificate
  199. $jsone['result'] = "Error";
  200. $jsone['message'] = "format of Manufacturer Root Certificate is incorrect";
  201. echo json_encode($jsone);
  202. exit;
  203. }
  204. sleep(1);
  205. move_uploaded_file($_FILES["ManufacturerRootCertificate"]["tmp_name"],"/Storage/OCPP/rootCaMf.pem");
  206. }
  207. // ob_start();
  208. shell_exec('sync;sync;sync');
  209. chdir("/root");
  210. $str_json=json_encode($json);//var_dump($str_json);
  211. exec("'./WebService' '4' '".$str_json."'",$output,$return_var);
  212. if(count($output)!=0){
  213. $jsone['result'] = "Success";
  214. $jsone['message'] = $json;
  215. echo json_encode($jsone);
  216. exit;
  217. }
  218. else{
  219. $jsone['result'] = "Error";
  220. $jsone['message'] = "Something went wrong on machine";
  221. echo json_encode($jsone);
  222. return false;
  223. exit;
  224. }
  225. // ob_end_clean();
  226. }
  227. function checkValue($id){
  228. if($_REQUEST[$id] != ""){
  229. if(strlen($_REQUEST[$id])!=1){
  230. $jsone['result'] = "Fail";
  231. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  232. echo json_encode($jsone);
  233. exit;
  234. }
  235. if(!is_numeric($_REQUEST[$id])){
  236. $jsone['result'] = "Fail";
  237. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  238. echo json_encode($jsone);
  239. exit;
  240. }
  241. }
  242. }
  243. function checkLength($id,$value){
  244. if(strlen($_REQUEST[$id])>$value){
  245. $jsone['result'] = "Fail";
  246. $jsone['message'] = "Length of " . $id . " should be less than ".$value+"(Chinese 5 words)";
  247. echo json_encode($jsone);
  248. exit;
  249. }
  250. }
  251. function isNumeric($id,$value){
  252. if($_REQUEST[$id] != ""){
  253. if(!is_numeric($_REQUEST[$id])){
  254. $jsone['result'] = "Fail";
  255. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  256. echo json_encode($jsone);
  257. exit;
  258. }
  259. if($_REQUEST[$id]>$value){
  260. $jsone['result'] = "Fail";
  261. $jsone['message'] = "Value of " . $id . " should be less than ".$value;
  262. echo json_encode($jsone);
  263. exit;
  264. }
  265. }
  266. }
  267. ?>