charconfig_controller.cpp 5.7 KB

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