File: files.cpp

package info (click to toggle)
bibledit-cloud 5.1.036-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 250,636 kB
  • sloc: xml: 915,934; ansic: 261,349; cpp: 92,628; javascript: 32,542; sh: 4,915; makefile: 586; php: 69
file content (108 lines) | stat: -rw-r--r-- 3,917 bytes parent folder | download | duplicates (3)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 Copyright (©) 2003-2025 Teus Benschop.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */


#include <sync/files.h>
#include <filter/url.h>
#include <filter/roles.h>
#include <filter/string.h>
#include <database/logs.h>
#include <client/logic.h>
#include <locale/translate.h>
#include <webserver/request.h>
#include <sync/logic.h>
#include <checksum/logic.h>


std::string sync_files_url ()
{
  return "sync/files";
}


std::string sync_files (Webserver_Request& webserver_request)
{
  Sync_Logic sync_logic (webserver_request);

  if (!sync_logic.security_okay ()) {
    // When the Cloud enforces https, inform the client to upgrade.
    webserver_request.response_code = 426;
    return std::string();
  }

  // If the client's IP address very recently made a prioritized server call,
  // then delay the current call.
  // This is the way to give priority to the other call:
  // Not clogging the client's internet connection.
  if (sync_logic.prioritized_ip_address_active ()) {
    std::this_thread::sleep_for (std::chrono::seconds (5));
  }
  
  if (webserver_request.post.empty ()) {
    for (auto& [key, value] : webserver_request.query) {
      webserver_request.post.emplace_back(key, value);
    }
  }
  const std::string user = filter::strings::hex2bin (webserver_request.post_get("u"));
  const int action = filter::strings::convert_to_int (webserver_request.post_get("a"));
  const int version = filter::strings::convert_to_int (webserver_request.post_get("v"));
  const size_t d = static_cast<size_t>(filter::strings::convert_to_int (webserver_request.post_get("d")));
  const std::string file = webserver_request.post_get("f");

  // For security reasons a client does not specify the directory of the file to be downloaded.
  // Rather it specifies the offset within the list of allowed directories for the version.
  const std::vector <std::string> directories = Sync_Logic::files_get_directories (version, user);
  if (d >= directories.size ()) {
    webserver_request.response_code = 400;
    return std::string();
  }
  const std::string directory = directories [d];
  
  if (action == Sync_Logic::files_total_checksum) {
    return std::to_string (Sync_Logic::files_get_total_checksum (version, user));
  }
  
  else if (action == Sync_Logic::files_directory_checksum) {
    int checksum = Sync_Logic::files_get_directory_checksum (directory);
    return std::to_string (checksum);
  }

  else if (action == Sync_Logic::files_directory_files) {
    std::vector <std::string> paths = Sync_Logic::files_get_files (directory);
    return filter::strings::implode (paths, "\n");
  }

  else if (action == Sync_Logic::files_file_checksum) {
    int checksum = Sync_Logic::files_get_file_checksum (directory, file);
    return std::to_string (checksum);
  }
  
  else if (action == Sync_Logic::files_file_download) {
    // This triggers the correct mime type.
    webserver_request.get = "file.download";
    // Return the file's contents.
    const std::string path = filter_url_create_root_path ({directory, file});
    return filter_url_file_get_contents (path);
  }
  
  // Bad request. Delay flood of bad requests.
  std::this_thread::sleep_for (std::chrono::seconds (1));
  webserver_request.response_code = 400;
  return std::string();
}