-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_remote_file.php
175 lines (149 loc) · 4.86 KB
/
copy_remote_file.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
<?php
//TODO: protect page with authentication
$valid_users = array("admin" => "password2014");
$auth = false;
//AUTH
if (isset($_COOKIE["copy_remote_file"]) && $_COOKIE["copy_remote_file"] == true) {
$auth = true;
}
if (isset($_POST) && isset($_POST["login_username"]) && $_POST["login_username"] != "" && isset($_POST["login_password"]) && $_POST["login_password"] != "") {
foreach ($valid_users as $username => $pwd) {
if ($username == $_POST["login_username"] && $pwd == $_POST["login_password"]) {
$auth = true;
setcookie("copy_remote_file", true, time() + 3600);
}
}
}
//REMOTE FILE COPY
$errors = true;
$error_message = "Generic errors: inform [email protected]";
$remote_file_url = "";
$copy_action = false;
if (isset($_POST) && isset($_POST["remote_file_url"]) && $_POST["remote_file_url"] != "") {
$copy_action = true;
set_time_limit(0);
ini_set('max_execution_time', 300000000);
ini_set('memory_limit', '-1');
try {
$remote_file_url = $_POST["remote_file_url"];
$file_name = basename($remote_file_url);
$filePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . $file_name;
if (file_exists($filePath)) {
$error_message = "Local file '" . $filePath . "' already exist!";
}
if (filter_var($remote_file_url, FILTER_VALIDATE_URL) === false) {
$error_message = "Url '" . $remote_file_url . "' not valid!";
} else {
$error_message = shell_exec("wget " . $filePath . " " . $remote_file_url);
$errors = false;
}
} catch (Exception $e) {
$error_message = $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<?php
if (!$auth){
?>
<div class="col-md-6 col-md-offset-3 login">
<br> <br>
<div class="jumbotron">
<h1 style="text-align:center;">Login</h1>
<br>
<form name="form-login" method="POST" class="form-horizontal">
<fieldset>
<div class="form-group">
<label class="control-label col-sm-3" for="username">Email</label>
<div class="col-sm-9">
<div class="input-group">
<span class="input-group-addon">
<span class="glyphicon glyphicon-user"></span>
</span>
<input type="text" id="login_username" name="login_username" class="form-control" placeholder="Insert here the username">
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="password">Password</label>
<div class="col-sm-9">
<div class="input-group">
<span class="input-group-addon">
<span class="glyphicon glyphicon-lock"></span>
</span>
<input type="password" id="login_password" name="login_password" class="form-control" placeholder="Insert here the password">
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-9 col-sm-offset-3">
<button type="submit" class="btn btn-primary">
<span class="glyphicon glyphicon-log-in"></span>
Login
</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
<?php
}
else{
?>
<div class="col-xs-12">
<br> <br>
<div class="jumbotron">
<h1>Copy remote file to local</h1>
<p>
Copy remote file to local inserting remote url of file only!
</p>
<form role="form" method="post">
<div class="form-group">
<label for="remote_file_url">Remote file url</label>
<input
name="remote_file_url"
class="form-control"
placeholder="File url like https://github.com/twbs/bootstrap/archive/v3.2.0.zip"
value="<?php echo $remote_file_url; ?>">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<div>
<br> <br>
<?php
if ($copy_action) {
if ($errors) {
?>
<div class="alert alert-danger" role="alert">
<span class="glyphicon glyphicon-remove"></span>
File not copied. Errors: <b><?php echo $error_message; ?></b></div>
<?php
} else {
?>
<div class="alert alert-success" role="alert">
<span class="glyphicon glyphicon-ok"></span>
File copied in <b><?php echo $filePath; ?></b></div>
<?php
}
}
?>
</div>
</div>
<?php
}
?>
</div>
</div>
</div>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</body>
</html>