This repository has been archived by the owner on Jun 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirect.php
197 lines (184 loc) · 7.38 KB
/
redirect.php
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
//region Includes
include_once "wp-load.php";
include_once "gestoria/constants.php";
include_once "gestoria/Procedure.php";
require_once __DIR__ . "/gestoria/vendor/autoload.php";
//endregion
use Krizalys\Onedrive\Client;
use Krizalys\Onedrive\Constant\ConflictBehavior;
use Krizalys\Onedrive\Exception\ConflictException;
use Krizalys\Onedrive\Onedrive;
use Krizalys\Onedrive\Proxy\DriveItemProxy;
if (!isset($text)) {
$text = [];
}
const CODE = "code";
const PROCEDURE = "procedure";
/**
* Returns the display name if the document of the name is not present
* @param array $procedure procedure data
* @return string a concatenation of the document and user name
*/
function generate_folder_name(array $procedure): string
{
if (
empty($procedure["document_type"]) ||
empty($procedure["document_number"]) ||
empty($procedure["first_name"]) ||
empty($procedure["last_name"])
) {
return $procedure["display_name"];
}
return $procedure["document_type"] . $procedure["document_number"] . "_" . $procedure["first_name"] . "-" . $procedure["last_name"];
}
/**
* Creates the user folder in the selected path or the root folder otherwise
* @param Client $client onedrive client
* @param string $folder_id selected folder
* @param string $folder_name folder name to create
* @return DriveItemProxy|null the created folder, null in case of exception
*/
function create_user_folder(Client $client, string $folder_id, string $folder_name): ?DriveItemProxy
{
try {
$folder = $client->getMyDrive()->getDriveItemById($folder_id);
$folder = $folder->createFolder($folder_name);
} catch (Exception $e) {
try {
$folder = $client->getMyDrive()->getRoot()->createFolder($folder_name);
$folder = $folder->createFolder($folder_name);
} catch (Exception $e) {
return null;
}
}
return $folder;
}
/**
* @param array $files
* @param DriveItemProxy|null $folder
* @return array
*/
function copy_files(array $files, ?DriveItemProxy $folder): array
{
$items = [];
if ($folder != null) {
try {
foreach ($files as $file) {
$segments = explode("/", $file);
$filename = $segments[count($segments) - 1];
$items[$filename] = $folder->upload($filename, fopen($file, "r"), ["conflictBehavior" => ConflictBehavior::REPLACE]);
}
} catch (ConflictException | Exception $e) {
krumo($e);
}
}
return $items;
}
if (array_key_exists("submit", $_POST)) {
$folder_id = $_POST["folder"];
$procedure_id = filter_var($_POST["procedure_id"], FILTER_SANITIZE_NUMBER_INT);
$procedure = new Procedure("");
$order_and_procedure = $procedure->get_order_and_procedure($procedure_id);
$folder_name = generate_folder_name($order_and_procedure[PROCEDURE]);
$user_files = $procedure->get_user_files($order_and_procedure[PROCEDURE]["email"]);
$files_path = array_map(fn($element) => $element["file_path"], $user_files["data"]);
$client = Onedrive::client(ONEDRIVE_CLIENT_ID, ["state" => $_SESSION[ONEDRIVE_CLIENT_STATE]]);
$user_folder = create_user_folder($client, $folder_id, $folder_name);
$items = copy_files($files_path, $user_folder);
} else {
if (!array_key_exists(CODE, $_GET)) {
throw new Exception("undefined code in request");
}
$client = Onedrive::client(ONEDRIVE_CLIENT_ID,
[
"state" => $_SESSION[ONEDRIVE_CLIENT_STATE]
]
);
try {
$client->obtainAccessToken(ONEDRIVE_CLIENT_SECRET, $_GET[CODE]);
$_SESSION[ONEDRIVE_CLIENT_STATE] = $client->getState();
$_SESSION["ACCESS_TOKEN"] = $_GET[CODE];
$children = $client->getMyDrive()->getRoot()->getChildren();
} catch (Exception $e) {
krumo($e);
echo $e->getMessage();
krumo($_SESSION);
}
}
if (array_key_exists("error", $_GET)) {
echo '<strong>' . $_GET["error"] . '</strong><p>' . $_GET["error_description"] . '</p>';
}
?>
<!doctype html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" media="all" type="text/css"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"/>
<title><?= $text["site_name"] . " - " . $text["procedure_title"]; ?></title>
<script src="https://code.jquery.com/jquery-3.5.1.js" crossorigin="anonymous"></script>
<link rel="icon" href="https://www.documentofacil.com/wp-content/uploads/2020/12/LOGO-DODUMENTO-LETRAS-EN-NEGRO.png"
sizes="32x32"/>
<link rel="icon" href="https://www.documentofacil.com/wp-content/uploads/2020/12/LOGO-DODUMENTO-LETRAS-EN-NEGRO.png"
sizes="192x192"/>
<link rel="apple-touch-icon"
href="https://www.documentofacil.com/wp-content/uploads/2020/12/LOGO-DODUMENTO-LETRAS-EN-NEGRO.png"/>
<link rel="stylesheet" media="all" type="text/css" href="gestoria.css"/>
</head>
<body>
<?php if (!IS_DEVELOPMENT) {
get_header();
} ?>
<div class="container">
<div class="jumbotron">
<h1 class="display-4"><?= $text["procedure_title"]; ?></h1>
</div>
<div class="row">
<div class="col">
<?php if (isset($items)): ?>
<div class="alert alert-success">
<?= $text["process_success"]; ?>.
</div>
<h4><?= $text["documents"]; ?></h4>
<?php foreach ($items as $item): ?>
<p><i class="far fa-file"></i>
<a target="_blank" href="<?= $item->webUrl; ?>"><?= $item->name; ?></a>
</p>
<?php endforeach; ?>
<div class="text-align-center">
<a href="tramites.php" class="btn btn-secondary"><?= $text["back_to_list"]; ?></a>
<br/><br/>
</div>
<?php else: ?>
<form action="" method="post">
<input type="hidden" name="procedure_id" value="<?= $_GET["state"]; ?>">
<div class="form-row">
<div class="form-group col-md-12">
<?php if (isset($children)) : ?>
<label for="folder" class="form-label">Selecciona una carpeta</label>
<select name="folder" id="folder" class="form-control">
<?php foreach ($children as $child): ?>
<?php if (is_object($child->folder)) : ?>
<option value="<?= $child->id ?>"><?= $child->name . " (" . $child->folder->childCount . ")"; ?></option>
<?php endif; ?>
<?php endforeach; ?>
</select>
<button id="submit" type="submit" name="submit"
class="btn btn-success"><?= $text["sent_to_onedrive"]; ?></button>
<?php endif; ?>
</div>
</div>
</form>
<?php endif; ?>
</div>
</div>
</div>
<?php if (!IS_DEVELOPMENT) {
get_footer();
} ?>
</body>
</html>