userconfig_controller.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "userconfig_controller.hpp"
  4. #include <string>
  5. #include <nlohmann/json.hpp>
  6. #include <common/showmsg.hpp>
  7. #include <common/sql.hpp>
  8. #include "auth.hpp"
  9. #include "http.hpp"
  10. #include "sqllock.hpp"
  11. #include "webutils.hpp"
  12. #include "web.hpp"
  13. HANDLER_FUNC(userconfig_save) {
  14. if (!isAuthorized(req, false)) {
  15. res.status = HTTP_BAD_REQUEST;
  16. res.set_content("Error", "text/plain");
  17. return;
  18. }
  19. auto account_id = std::stoi(req.get_file_value("AID").content);
  20. auto world_name = req.get_file_value("WorldName").content;
  21. auto data = nlohmann::json::object();
  22. if (req.has_file("data")) {
  23. data = nlohmann::json::parse(req.get_file_value("data").content);
  24. }
  25. SQLLock sl(WEB_SQL_LOCK);
  26. sl.lock();
  27. auto handle = sl.getHandle();
  28. SqlStmt * stmt = SqlStmt_Malloc(handle);
  29. if (SQL_SUCCESS != SqlStmt_Prepare(stmt,
  30. "SELECT `data` FROM `%s` WHERE (`account_id` = ? AND `world_name` = ?) LIMIT 1",
  31. user_configs_table)
  32. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_INT, &account_id, sizeof(account_id))
  33. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (void *)world_name.c_str(), world_name.length())
  34. || SQL_SUCCESS != SqlStmt_Execute(stmt)
  35. ) {
  36. SqlStmt_ShowDebug(stmt);
  37. SqlStmt_Free(stmt);
  38. sl.unlock();
  39. res.status = HTTP_BAD_REQUEST;
  40. res.set_content("Error", "text/plain");
  41. return;
  42. }
  43. if (SqlStmt_NumRows(stmt) > 0) {
  44. char databuf[SQL_BUFFER_SIZE];
  45. if (SQL_SUCCESS != SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &databuf, sizeof(databuf), NULL, NULL)
  46. || SQL_SUCCESS != SqlStmt_NextRow(stmt)
  47. ) {
  48. SqlStmt_ShowDebug(stmt);
  49. SqlStmt_Free(stmt);
  50. sl.unlock();
  51. res.status = HTTP_BAD_REQUEST;
  52. res.set_content("Error", "text/plain");
  53. return;
  54. }
  55. auto db_data = nlohmann::json::parse(databuf);
  56. mergeData(db_data, data, true);
  57. data = std::move(db_data);
  58. }
  59. auto data_str = data.dump();
  60. if (SQL_SUCCESS != SqlStmt_Prepare(stmt,
  61. "REPLACE INTO `%s` (`account_id`, `world_name`, `data`) VALUES (?, ?, ?)",
  62. user_configs_table)
  63. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_INT, &account_id, sizeof(account_id))
  64. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (void *)world_name.c_str(), world_name.length())
  65. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 2, SQLDT_STRING, (void *)data_str.c_str(), data_str.length())
  66. || SQL_SUCCESS != SqlStmt_Execute(stmt)
  67. ) {
  68. SqlStmt_ShowDebug(stmt);
  69. SqlStmt_Free(stmt);
  70. sl.unlock();
  71. res.status = HTTP_BAD_REQUEST;
  72. res.set_content("Error", "text/plain");
  73. return;
  74. }
  75. SqlStmt_Free(stmt);
  76. sl.unlock();
  77. res.set_content(data_str, "application/json");
  78. }
  79. HANDLER_FUNC(userconfig_load) {
  80. if (!req.has_file("AID") || !req.has_file("WorldName")) {
  81. res.status = HTTP_BAD_REQUEST;
  82. res.set_content("Error", "text/plain");
  83. return;
  84. }
  85. // TODO: Figure out when client sends AuthToken for this path, then add packetver check
  86. // if (!isAuthorized(req)) {
  87. // ShowError("Not authorized!\n");
  88. // message.reply(web::http::status_codes::Forbidden);
  89. // return;
  90. // }
  91. auto account_id = std::stoi(req.get_file_value("AID").content);
  92. auto world_name_str = req.get_file_value("WorldName").content;
  93. auto world_name = world_name_str.c_str();
  94. SQLLock sl(WEB_SQL_LOCK);
  95. sl.lock();
  96. auto handle = sl.getHandle();
  97. SqlStmt * stmt = SqlStmt_Malloc(handle);
  98. if (SQL_SUCCESS != SqlStmt_Prepare(stmt,
  99. "SELECT `data` FROM `%s` WHERE (`account_id` = ? AND `world_name` = ?) LIMIT 1",
  100. user_configs_table)
  101. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_INT, &account_id, sizeof(account_id))
  102. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (void *)world_name, strlen(world_name))
  103. || SQL_SUCCESS != SqlStmt_Execute(stmt)
  104. ) {
  105. SqlStmt_ShowDebug(stmt);
  106. SqlStmt_Free(stmt);
  107. sl.unlock();
  108. res.status = HTTP_BAD_REQUEST;
  109. res.set_content("Error", "text/plain");
  110. return;
  111. }
  112. if (SqlStmt_NumRows(stmt) <= 0) {
  113. std::string data = "{\"Type\": 1}";
  114. if( SQL_SUCCESS != SqlStmt_Prepare( stmt, "INSERT INTO `%s` (`account_id`, `world_name`, `data`) VALUES (?, ?, ?)", user_configs_table ) ||
  115. SQL_SUCCESS != SqlStmt_BindParam( stmt, 0, SQLDT_INT, &account_id, sizeof( account_id ) ) ||
  116. SQL_SUCCESS != SqlStmt_BindParam( stmt, 1, SQLDT_STRING, (void *)world_name, strlen( world_name ) ) ||
  117. SQL_SUCCESS != SqlStmt_BindParam( stmt, 2, SQLDT_STRING, (void *)data.c_str(), strlen( data.c_str() ) ) ||
  118. SQL_SUCCESS != SqlStmt_Execute( stmt ) ){
  119. SqlStmt_ShowDebug( stmt );
  120. SqlStmt_Free( stmt );
  121. sl.unlock();
  122. res.status = HTTP_BAD_REQUEST;
  123. res.set_content( "Error", "text/plain" );
  124. return;
  125. }
  126. SqlStmt_Free( stmt );
  127. sl.unlock();
  128. res.set_content( data, "application/json" );
  129. return;
  130. }
  131. char databuf[SQL_BUFFER_SIZE];
  132. if (SQL_SUCCESS != SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &databuf, sizeof(databuf), NULL, NULL)
  133. || SQL_SUCCESS != SqlStmt_NextRow(stmt)
  134. ) {
  135. SqlStmt_ShowDebug(stmt);
  136. SqlStmt_Free(stmt);
  137. sl.unlock();
  138. res.status = HTTP_BAD_REQUEST;
  139. res.set_content("Error", "text/plain");
  140. return;
  141. }
  142. SqlStmt_Free(stmt);
  143. sl.unlock();
  144. databuf[sizeof(databuf) - 1] = 0;
  145. auto response = nlohmann::json::parse(databuf);
  146. response["Type"] = 1;
  147. res.set_content(response.dump(), "application/json");}