summaryrefslogtreecommitdiff
path: root/src/utils/string.cpp
blob: 71c06bf42c74faa1325d3f394e73e8d6298c16ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "string.hpp"

#include <sstream>
#include <string>
#include <vector>

namespace bot {
  namespace utils {
    namespace string {
      std::vector<std::string> split_text(const std::string &text,
                                          char delimiter) {
        std::vector<std::string> parts;

        std::istringstream iss(text);
        std::string part;

        while (std::getline(iss, part, delimiter)) {
          parts.push_back(part);
        }

        return parts;
      }

      std::string join_vector(const std::vector<std::string> &vec,
                              char delimiter) {
        if (vec.empty()) {
          return "";
        }

        std::string str;

        for (auto i = vec.begin(); i != vec.end() - 1; i++) {
          str += *i + delimiter;
        }

        str += vec[vec.size() - 1];

        return str;
      }

      std::string join_vector(const std::vector<std::string> &vec) {
        std::string str;

        for (const auto &e : vec) {
          str += e;
        }

        return str;
      }

      bool string_contains_sql_injection(const std::string &input) {
        std::string forbidden_strings[] = {";",   "--",     "'",      "\"",
                                           "/*",  "*/",     "xp_",    "exec",
                                           "sp_", "insert", "select", "delete"};

        for (const auto &str : forbidden_strings) {
          if (input.find(str) != std::string::npos) {
            return true;
          }
        }

        return false;
      }
    }
  }
}