]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/rpcconfig/user_helper.cpp
c858eba1c97090259f97a22668f1ddfa9a9154ab
[stg.git] / projects / stargazer / plugins / configuration / rpcconfig / user_helper.cpp
1 #include "user_helper.h"
2
3 #include "../../../tariffs.h"
4 #include "../../../admin.h"
5 #include "base_store.h"
6 #include "user_ips.h"
7 #include "utils.h"
8 #include "common.h"
9
10 //------------------------------------------------------------------------------
11
12 void USER_HELPER::GetUserInfo(xmlrpc_c::value * info,
13                               bool hidePassword)
14 {
15 std::string enc;
16
17 std::map<std::string, xmlrpc_c::value> structVal;
18
19 structVal["result"] = xmlrpc_c::value_boolean(true);
20 structVal["login"] = xmlrpc_c::value_string(iter->GetLogin());
21
22 if (!hidePassword)
23     {
24     structVal["password"] = xmlrpc_c::value_string(iter->property.password.Get());
25     }
26 else
27     {
28     structVal["password"] = xmlrpc_c::value_string("++++++++");
29     }
30
31 structVal["cash"] = xmlrpc_c::value_double(iter->property.cash.Get());
32 structVal["freemb"] = xmlrpc_c::value_double(iter->property.freeMb.Get());
33 structVal["credit"] = xmlrpc_c::value_double(iter->property.credit.Get());
34
35 if (iter->property.nextTariff.Get() != "")
36     {
37     structVal["tariff"] = xmlrpc_c::value_string(
38             iter->property.tariffName.Get() +
39             "/" +
40             iter->property.nextTariff.Get()
41             );
42     }
43 else
44     {
45     structVal["tariff"] = xmlrpc_c::value_string(iter->property.tariffName.Get());
46     }
47
48 structVal["note"] = xmlrpc_c::value_string(IconvString(iter->property.note, "KOI8-R", "UTF-8"));
49
50 structVal["phone"] = xmlrpc_c::value_string(IconvString(iter->property.phone, "KOI8-R", "UTF-8"));
51
52 structVal["address"] = xmlrpc_c::value_string(IconvString(iter->property.address, "KOI8-R", "UTF-8"));
53
54 structVal["email"] = xmlrpc_c::value_string(IconvString(iter->property.email, "KOI8-R", "UTF-8"));
55
56 std::vector<xmlrpc_c::value> userdata;
57
58 userdata.push_back(xmlrpc_c::value_string(IconvString(iter->property.userdata0.Get(), "KOI8-R", "UTF-8")));
59 userdata.push_back(xmlrpc_c::value_string(IconvString(iter->property.userdata1.Get(), "KOI8-R", "UTF-8")));
60 userdata.push_back(xmlrpc_c::value_string(IconvString(iter->property.userdata2.Get(), "KOI8-R", "UTF-8")));
61 userdata.push_back(xmlrpc_c::value_string(IconvString(iter->property.userdata3.Get(), "KOI8-R", "UTF-8")));
62 userdata.push_back(xmlrpc_c::value_string(IconvString(iter->property.userdata4.Get(), "KOI8-R", "UTF-8")));
63 userdata.push_back(xmlrpc_c::value_string(IconvString(iter->property.userdata5.Get(), "KOI8-R", "UTF-8")));
64 userdata.push_back(xmlrpc_c::value_string(IconvString(iter->property.userdata6.Get(), "KOI8-R", "UTF-8")));
65 userdata.push_back(xmlrpc_c::value_string(IconvString(iter->property.userdata7.Get(), "KOI8-R", "UTF-8")));
66 userdata.push_back(xmlrpc_c::value_string(IconvString(iter->property.userdata8.Get(), "KOI8-R", "UTF-8")));
67 userdata.push_back(xmlrpc_c::value_string(IconvString(iter->property.userdata9.Get(), "KOI8-R", "UTF-8")));
68
69 structVal["userdata"] = xmlrpc_c::value_array(userdata);
70
71 structVal["name"] = xmlrpc_c::value_string(IconvString(iter->property.realName, "KOI8-R", "UTF-8"));
72
73 structVal["group"] = xmlrpc_c::value_string(IconvString(iter->property.group, "KOI8-R", "UTF-8"));
74
75 structVal["status"] = xmlrpc_c::value_boolean(iter->GetConnected());
76 structVal["aonline"] = xmlrpc_c::value_boolean(iter->property.alwaysOnline.Get());
77 structVal["currip"] = xmlrpc_c::value_string(inet_ntostring(iter->GetCurrIP()));
78 structVal["pingtime"] = xmlrpc_c::value_int(iter->GetPingTime());
79 structVal["ips"] = xmlrpc_c::value_string(iter->property.ips.Get().GetIpStr());
80
81 std::map<std::string, xmlrpc_c::value> traffInfo;
82 std::vector<xmlrpc_c::value> mu(DIR_NUM);
83 std::vector<xmlrpc_c::value> md(DIR_NUM);
84 std::vector<xmlrpc_c::value> su(DIR_NUM);
85 std::vector<xmlrpc_c::value> sd(DIR_NUM);
86
87 DIR_TRAFF upload;
88 DIR_TRAFF download;
89 DIR_TRAFF supload;
90 DIR_TRAFF sdownload;
91 download = iter->property.down.Get();
92 upload = iter->property.up.Get();
93 sdownload = iter->GetSessionUpload();
94 supload = iter->GetSessionDownload();
95
96 for (int j = 0; j < DIR_NUM; j++)
97     {
98     std::string value;
99     x2str(upload[j], value);
100     mu[j] = xmlrpc_c::value_string(value);
101     x2str(download[j], value);
102     md[j] = xmlrpc_c::value_string(value);
103     x2str(supload[j], value);
104     su[j] = xmlrpc_c::value_string(value);
105     x2str(sdownload[j], value);
106     sd[j] = xmlrpc_c::value_string(value);
107     }
108
109 traffInfo["mu"] = xmlrpc_c::value_array(mu);
110 traffInfo["md"] = xmlrpc_c::value_array(md);
111 traffInfo["su"] = xmlrpc_c::value_array(su);
112 traffInfo["sd"] = xmlrpc_c::value_array(sd);
113
114 structVal["traff"] = xmlrpc_c::value_struct(traffInfo);
115
116 structVal["down"] = xmlrpc_c::value_boolean(iter->property.disabled.Get());
117 structVal["disableddetailstat"] = xmlrpc_c::value_boolean(iter->property.disabledDetailStat.Get());
118 structVal["passive"] = xmlrpc_c::value_boolean(iter->property.passive.Get());
119 structVal["lastcash"] = xmlrpc_c::value_double(iter->property.lastCashAdd.Get());
120 structVal["lasttimecash"] = xmlrpc_c::value_int(iter->property.lastCashAddTime.Get());
121 structVal["lastactivitytime"] = xmlrpc_c::value_int(iter->property.lastActivityTime.Get());
122 structVal["creditexpire"] = xmlrpc_c::value_int(iter->property.creditExpire.Get());
123
124 *info = xmlrpc_c::value_struct(structVal);
125 }
126
127 //------------------------------------------------------------------------------
128
129 bool USER_HELPER::SetUserInfo(const xmlrpc_c::value & info,
130                               const ADMIN & admin,
131                               const std::string & login,
132                               const BASE_STORE & store,
133                               TARIFFS * tariffs)
134 {
135 std::map<std::string, xmlrpc_c::value> structVal(
136     static_cast<std::map<std::string, xmlrpc_c::value> >(xmlrpc_c::value_struct(info))
137     );
138
139 std::map<std::string, xmlrpc_c::value>::iterator it;
140
141 if ((it = structVal.find("password")) != structVal.end())
142     {
143     std::string value(xmlrpc_c::value_string(it->second));
144     if (iter->property.password.Get() != value)
145         if (!iter->property.password.Set(value,
146                                          admin,
147                                          login,
148                                          &store))
149             return true;
150     }
151
152 if ((it = structVal.find("ips")) != structVal.end())
153     {
154     USER_IPS ips;
155     ips = StrToIPS(xmlrpc_c::value_string(it->second));
156     if (!iter->property.ips.Set(ips,
157                                 admin,
158                                 login,
159                                 &store))
160         return true;
161     }
162
163 if ((it = structVal.find("address")) != structVal.end())
164     {
165     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-R"));
166     if (iter->property.address.Get() != value)
167         if (!iter->property.address.Set(value,
168                                         admin,
169                                         login,
170                                         &store))
171             return true;
172     }
173
174 if ((it = structVal.find("phone")) != structVal.end())
175     {
176     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-R"));
177     if (iter->property.phone.Get() != value)
178         if (!iter->property.phone.Set(value,
179                                       admin,
180                                       login,
181                                       &store))
182             return true;
183     }
184
185 if ((it = structVal.find("email")) != structVal.end())
186     {
187     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-R"));
188     if (iter->property.email.Get() != value)
189         if (!iter->property.email.Set(value,
190                                       admin,
191                                       login,
192                                       &store))
193             return true;
194     }
195
196 if ((it = structVal.find("cash")) != structVal.end())
197     {
198     double value(xmlrpc_c::value_double(it->second));
199     if (iter->property.cash.Get() != value)
200         if (!iter->property.cash.Set(value,
201                                      admin,
202                                      login,
203                                      &store))
204             return true;
205     }
206
207 if ((it = structVal.find("creditexpire")) != structVal.end())
208     {
209     time_t value(xmlrpc_c::value_int(it->second));
210     if (iter->property.creditExpire.Get() != value)
211         if (!iter->property.creditExpire.Set(value,
212                                              admin,
213                                              login,
214                                              &store))
215             return true;
216     }
217
218 if ((it = structVal.find("credit")) != structVal.end())
219     {
220     double value(xmlrpc_c::value_double(it->second));
221     if (iter->property.credit.Get() != value)
222         if (!iter->property.credit.Set(value,
223                                        admin,
224                                        login,
225                                        &store))
226             return true;
227     }
228
229 if ((it = structVal.find("freemb")) != structVal.end())
230     {
231     double value(xmlrpc_c::value_double(it->second));
232     if (iter->property.freeMb.Get() != value)
233         if (!iter->property.freeMb.Set(value,
234                                        admin,
235                                        login,
236                                        &store))
237             return true;
238     }
239
240 if ((it = structVal.find("down")) != structVal.end())
241     {
242     bool value(xmlrpc_c::value_boolean(it->second));
243     if (iter->property.disabled.Get() != value)
244         if (!iter->property.disabled.Set(value,
245                                          admin,
246                                          login,
247                                          &store))
248             return true;
249     }
250
251 if ((it = structVal.find("passive")) != structVal.end())
252     {
253     bool value(xmlrpc_c::value_boolean(it->second));
254     if (iter->property.passive.Get())
255         if (!iter->property.passive.Set(value,
256                                         admin,
257                                         login,
258                                         &store))
259             return true;
260     }
261
262 if ((it = structVal.find("aonline")) != structVal.end())
263     {
264     bool value(xmlrpc_c::value_boolean(it->second));
265     if (iter->property.alwaysOnline.Get() != value)
266         if (!iter->property.alwaysOnline.Set(value,
267                                              admin,
268                                              login,
269                                              &store))
270             return true;
271     }
272
273 if ((it = structVal.find("disableddetailstat")) != structVal.end())
274     {
275     bool value(xmlrpc_c::value_boolean(it->second));
276     if (iter->property.disabledDetailStat.Get() != value)
277         if (!iter->property.disabledDetailStat.Set(value,
278                                                    admin,
279                                                    login,
280                                                    &store))
281             return true;
282     }
283
284 if ((it = structVal.find("name")) != structVal.end())
285     {
286     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-R"));
287     if (iter->property.realName.Get() != value)
288         if (!iter->property.realName.Set(value,
289                                          admin,
290                                          login,
291                                          &store))
292             return true;
293     }
294
295 if ((it = structVal.find("group")) != structVal.end())
296     {
297     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-R"));
298     if (iter->property.group.Get() != value)
299         if (!iter->property.group.Set(value,
300                                       admin,
301                                       login,
302                                       &store))
303             return true;
304     }
305
306 if ((it = structVal.find("note")) != structVal.end())
307     {
308     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-R"));
309     if (iter->property.note.Get() != value)
310         if (!iter->property.note.Set(value,
311                                      admin,
312                                      login,
313                                      &store))
314             return true;
315     }
316
317 if ((it = structVal.find("userdata")) != structVal.end())
318     {
319     std::vector<USER_PROPERTY_LOGGED<string> *> userdata;
320     userdata.push_back(iter->property.userdata0.GetPointer());
321     userdata.push_back(iter->property.userdata1.GetPointer());
322     userdata.push_back(iter->property.userdata2.GetPointer());
323     userdata.push_back(iter->property.userdata3.GetPointer());
324     userdata.push_back(iter->property.userdata4.GetPointer());
325     userdata.push_back(iter->property.userdata5.GetPointer());
326     userdata.push_back(iter->property.userdata6.GetPointer());
327     userdata.push_back(iter->property.userdata7.GetPointer());
328     userdata.push_back(iter->property.userdata8.GetPointer());
329     userdata.push_back(iter->property.userdata9.GetPointer());
330
331     std::vector<xmlrpc_c::value> udata(
332         xmlrpc_c::value_array(it->second).vectorValueValue()
333         );
334
335     for (unsigned i = 0; i < userdata.size(); ++i)
336         {
337         std::string value(IconvString(xmlrpc_c::value_string(udata[i]), "UTF-8", "KOI8-R"));
338         if (userdata[i]->Get() != value)
339             if (!userdata[i]->Set(value,
340                                   admin,
341                                   login,
342                                   &store))
343                 return true;
344         }
345     }
346
347 if ((it = structVal.find("traff")) != structVal.end())
348     {
349     std::map<std::string, xmlrpc_c::value> traff(
350         static_cast<std::map<std::string, xmlrpc_c::value> >(xmlrpc_c::value_struct(it->second))
351         );
352
353     DIR_TRAFF dtData;
354     dtData = iter->property.up.Get();
355     if ((it = traff.find("mu")) != traff.end())
356         {
357         std::vector<xmlrpc_c::value> data(xmlrpc_c::value_array(it->second).vectorValueValue());
358
359         for (int i = 0; i < std::min(DIR_NUM, static_cast<int>(data.size())); ++i)
360             {
361             int64_t value;
362             if (str2x(xmlrpc_c::value_string(data[i]), value))
363                 printfd(__FILE__, "USER_HELPER::SetUserInfo(): 'Invalid month upload value'\n");
364             else
365                 dtData[i] = value;
366             }
367         if (!iter->property.up.Set(dtData,
368                                    admin,
369                                    login,
370                                    &store))
371             return true;
372         }
373     dtData = iter->property.down.Get();
374     if ((it = traff.find("md")) != traff.end())
375         {
376         std::vector<xmlrpc_c::value> data(xmlrpc_c::value_array(it->second).vectorValueValue());
377
378         for (int i = 0; i < std::min(DIR_NUM, static_cast<int>(data.size())); ++i)
379             {
380             int64_t value;
381             if (str2x(xmlrpc_c::value_string(data[i]), value))
382                 printfd(__FILE__, "USER_HELPER::SetUserInfo(): 'Invalid month download value'\n");
383             else
384                 dtData[i] = value;
385             }
386         if (!iter->property.down.Set(dtData,
387                                      admin,
388                                      login,
389                                      &store))
390             return true;
391         }
392     }
393
394 if ((it = structVal.find("tariff")) != structVal.end())
395     {
396     std::string tariff(xmlrpc_c::value_string(it->second));
397     size_t pos = tariff.find('/');
398     std::string nextTariff;
399     if (pos != std::string::npos)
400         {
401         nextTariff = tariff.substr(pos + 1);
402         tariff = tariff.substr(0, pos);
403         }
404
405     if (tariffs->FindByName(tariff))
406         if (iter->property.tariffName.Get() != tariff)
407             if (!iter->property.tariffName.Set(tariff,
408                                                admin,
409                                                login,
410                                                &store))
411                 return true;
412
413     if (nextTariff != "" &&
414         tariffs->FindByName(nextTariff))
415         if (iter->property.nextTariff.Get() != nextTariff)
416             if (!iter->property.nextTariff.Set(tariff,
417                                                admin,
418                                                login,
419                                                &store))
420                 return true;
421     }
422
423 return false;
424 }