-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
executable file
·296 lines (279 loc) · 11.7 KB
/
index.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
error_reporting( E_ALL );
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
// var to store error messages out of function scope
$error_out = false;
// Checks if a directory exists
function ftp_is_dir($ftp, $dir) {
$pushd = ftp_pwd($ftp);
if ($pushd !== false && @ftp_chdir($ftp, $dir)) {
ftp_chdir($ftp, $pushd);
return true;
}
return false;
}
if(empty($_FILES) && empty($_POST) && isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'post'){ // catch file overload error...
$postMax = ini_get('post_max_size'); //grab the size limits...
$error_out = '<div data-alert class="alert-box warning">Your upload exceeded the maximum filesize, which is ' . $postMax . ' on this server. <br />Please optimize the PDF using Acrobat or Smallpdf (linked below) and try again.</div>';
} else if(isset($_FILES["image"])) {
if ($_FILES["image"]["error"] > 0) {
if ( $_FILES["image"]["error"] == 4 ) {
// No image file was uploaded
$error_out = '<div data-alert class="alert-box warning">No Image was chosen to be uploaded</div>';
} else {
// Another error occurred
$error_out = '<div data-alert class="alert-box warning">Error Code: ' . $_FILES["image"]["error"] . '</div>';
}
} else {
// FTP credentials stored in non-repo file
require('constants.php');
$conn_id = ftp_connect($FTP_SERVER) or die("Couldn't connect to $ftp_server");
ftp_login($conn_id,$FTP_USER_NAME,$FTP_USER_PASS);
ftp_pasv($conn_id, TRUE);
$selectedDate = explode("/", $_POST["date"]);
$month = $selectedDate[0];
$day = $selectedDate[1];
$year = $selectedDate[2];
// location the file will be put into
$location = "/".$year."/".$month."/";
// rename the file to today's date
$_FILES["image"]["name"] = $year."-".$month."-".$day;
if (!ftp_is_dir($conn_id, $FTP_DIRECTORY."/".$year."/")) {
// if the year folder does not exist, create it
ftp_mkdir($conn_id, $FTP_DIRECTORY."/".$year);
}
if (!ftp_is_dir($conn_id, $FTP_DIRECTORY."/".$year."/".$month."/")) {
// if the month folder does not exist, create it
ftp_mkdir($conn_id, $FTP_DIRECTORY."/".$year."/".$month);
}
if (($_FILES["image"]["type"]=="application/pdf") || ($_FILES["image"]["type"]=="image/jpeg")) {
$extension = ".pdf";
if($_FILES["image"]["type"]=="image/jpeg") {
$extension = "-original.jpg";
}
// drop original file in current folder for imagick to use
move_uploaded_file($_FILES["image"]["tmp_name"], $_FILES["image"]["name"].$extension);
$im = new imagick();
$im->setResolution(72,72);
$im->readimage($_FILES["image"]["name"].$extension);
$im->setImageFormat('jpg');
$im->scaleImage(350,0);
$im->writeImage($_FILES["image"]["name"].".jpg"); // Create the smaller jpg version in current folder
$im->clear();
// FTP the thumbnail and PDF to Extras
if (ftp_put($conn_id, $FTP_DIRECTORY.$location.$_FILES["image"]["name"].$extension, $_FILES["image"]["name"].$extension, FTP_BINARY)) {
$error_out = '<div data-alert class="alert-box success">Original file uploaded!</div>';
}
else { $error_out = '<div data-alert class="alert-box alert">ERROR: The original file failed to upload!</div>'; }
if (ftp_put($conn_id, $FTP_DIRECTORY.$location.$_FILES["image"]["name"].".jpg", $_FILES["image"]["name"].".jpg", FTP_BINARY)) {
// Adds the date and image URLs to the JSNO file that's pushed to extras
$filename = '"'.$_FILES["image"]["name"].'"';
$json_a = explode("[", file_get_contents("frontpages.json"));
$json_b = explode("]", $json_a[1]);
$json_c = explode(",", $json_b[0]);
if (!in_array($filename, $json_c)) {
if ($filename > $json_c[0]) { // date is today, add to beginning
array_unshift($json_c, $filename);
}
else {
for($i=0;$i<sizeof($json_c);$i++) {
$i2 = $i+1;
if($i2<sizeof($json_c)) {
if(($json_c[$i] > $filename)&&($json_c[$i2] < $filename)) { // date is somewhere in between, find the right spot and drop it in
array_splice($json_c, $i2, 0, $filename);
break;
}
}
else { // date is older than anything in array, add to end
array_push($json_c, $filename);
break; // prevents infinite loop
}
}
}
// sorting and formatting the JSON
$newString = "frontPages({arr:[";
for($i=0;$i<sizeof($json_c);$i++) {
$newString .= $json_c[$i];
if( $i < sizeof($json_c)-1 ) { $newString .= ","; }
}
$newString .= "]})";
// Save the JSON locally and FTP it to Extras
file_put_contents("frontpages.json", $newString);
ftp_put($conn_id, $FTP_DIRECTORY."/frontpages.json", "frontpages.json", FTP_BINARY);
}
$error_out = '<div data-alert class="alert-box success">.jpg created and uploaded!</div>';
}
else { $error_out = '<div data-alert class="alert-box alert">ERROR: The .jpg file failed to upload!</div>'; }
unlink($_FILES["image"]["name"].".jpg"); // delete the jpg file in current folder
unlink($_FILES["image"]["name"].$extension); // delete the pdf file in current folder
}
else { $error_out = '<div data-alert class="alert-box alert">File must be a PDF or JPG file!<br> This file is: ' . $_FILES["image"]["type"] . '</div>'; }
ftp_close($conn_id);
}
}
?>
<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Denver Post Front Pages Uploader</title>
<meta charset="utf-8">
<link rel="stylesheet" href="//extras.denverpost.com/transgender/css/normalize.css" />
<link rel="stylesheet" href="//extras.denverpost.com/transgender/css/foundation.min.css" />
<link href='https://fonts.googleapis.com/css?family=Noticia+Text:400,700,400italic,700italic|PT+Sans:400,700,400italic,700italic|PT+Sans+Narrow:400,700' rel='stylesheet' type='text/css'>
<link rel="shortcut icon" href="https://plus.denverpost.com/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="assets/jquery-ui.css">
<script src="assets/jquery-latest.min.js"></script>
<script src="assets/jquery-ui.js"></script>
<style type="text/css">
div#radios {
padding-left: 4em;
}
div#radios input {
margin-bottom: .4em;
}
div#radios {
line-height:1.5
}
button.ui-datepicker-trigger {
background-color:rgba(0,100,255,0.25);
height:28px;
width:28px;
padding:0;
}
button.ui-datepicker-trigger img {
width:100%;
}
div#uploadHere {
margin:1.5em 0;
}
div#uploadHere input {
padding-left: 4em;
}
#submitButton {
}
p.grey {
line-height: 2;
padding-left: 5em;
font-size: .85em;
color:rgba(0,0,0,0.6);
}
fieldset {
padding:1em 2em 0;
}
</style>
<script>
// Sets up the date field and datepicker
var date = new Date();
var curr_year = date.getFullYear();
var curr_month = date.getMonth()+1; if(curr_month<10) curr_month='0'+curr_month;
var curr_day = date.getDate(); if(curr_day<10) curr_day='0'+curr_day;
var sel_year = curr_year;
var sel_month = curr_month;
var sel_day = curr_day;
var today = curr_month+"/"+curr_day+"/"+curr_year;
var newdate = new Date(curr_year,curr_month,curr_day);
newdate.setDate(newdate.getDate() + 1);
var nd = new Date(newdate);
var tom_year = nd.getFullYear();
var tom_month = nd.getMonth(); if(tom_month<10) tom_month='0'+tom_month;
var tom_day = nd.getDate(); if(tom_day<10) tom_day='0'+tom_day;
var tomorrow = tom_month+"/"+tom_day+"/"+tom_year;
function dateFieldChange(){
var d = $('#dateField').val();
if (d.indexOf("-") != -1) { d = d.replace(/\-/g,'/') }
if (!$.isNumeric(d.substring(0,1))) {
d = d.replace(",", "").replace("th", "").replace("rd", "");
d2 = d.split(' ');
switch(d2[0].substring(0,3).toLowerCase()){
case "jan": sel_month='01'; break; case "feb": sel_month='02'; break; case "mar": sel_month='03'; break; case "apr": sel_month='04'; break;
case "may": sel_month='05'; break; case "jun": sel_month='06'; break; case "jul": sel_month='07'; break; case "aug": sel_month='08'; break;
case "sep": sel_month='09'; break; case "oct": sel_month='10'; break; case "nov": sel_month='11'; break; case "dec": sel_month='12'; break;
}
sel_day = parseInt(d2[1]); if(sel_day<10) { sel_day='0'+sel_day; }
sel_year = parseInt(d2[2]); if(sel_year.length<3) { sel_year='20'+sel_year; }
$('#datepicker, #dateField, #custom').val(sel_month+"/"+sel_day+"/"+sel_year);
}
else { $('#datepicker, #datepicker, #custom').val(d); }
var nm = parseInt(sel_month)-1;
if(new Date(sel_year,nm,sel_day) > new Date()) { $('#datepicker, #dateField, #custom').val(curr_month+"/"+curr_day+"/"+curr_year); updateImage(); }
}
function populateInput() {
$('#dateField, #custom').val($('#datepicker').val());
$('#custom').val($('#datepicker').val());
$('#message').remove();
}
</script>
</head>
<body style="margin:0;">
<section id="main" style="margin:0;">
<div style="background:#e1e1e1;border-bottom:10px solid #b1b1b1;padding:2em 0;margin:0 0 2em;text-align:center;">
<h1>Denver Post Front Pages uploader</h1>
</div>
<div class="row">
<div class="large-9 large-centered columns">
<form action="" id="upLoadImage" name='upLoadImage' method='post' enctype="multipart/form-data">
<fieldset>
<div class="row">
<div id="errorOut">
<?php echo $error_out ?>
</div>
</div>
<h5>This front page is for:</h5>
<div class="row">
<div class="large-6 columns">
<div id="radios">
<input type="radio" name="date" id="today" value="today" checked> Today<br />
<input type="radio" name="date" id="tomorrow" value="tomorrow"> Tomorrow<br />
<input type="radio" name="date" id="custom" value="custom" onchange="populateInput()"> Custom date:
</div>
</div>
<div class="large-6 columns">
<form name="chooseDateForm" id="chooseDateForm">
<input type="text" id="dateField" onchange="dateFieldChange()">
<input type="hidden" id="datepicker" onchange="populateInput()">
</form>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<div id="uploadHere">
<h5>Image to upload:</h5> <input name="image" type="file">
<p class="grey"><em>This should the <strong>unlocked</strong> PDF from dp_stor. <br />If the PDF file is too large, you can <a href="https://smallpdf.com/compress-pdf">use Smallpdf to optimize it</a> before uploading!</em></p>
</div>
</div>
</div>
<div class="row">
<input class="button large-12" id="submitButton" type="submit" name="submit" value="Upload the PDF" onclick="populateInput();">
</div>
</form>
</fieldset>
</div>
</div>
</section>
<script>
$(function(){
// JS for the calendar-type date picker
$('#datepicker').datepicker({
inline: true,
nextText: '→',
prevText: '←',
showOtherMonths: true,
dateFormat: 'mm/dd/yy',
dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
showOn: "button",
buttonImage: "https://extras.denverpost.com/frontpages/calendar.svg",
buttonImageOnly: false,
});
$('#datepicker, #dateField').val(sel_month+"/"+sel_day+"/"+sel_year);
$('#chooseDateForm').submit(function () { return false; });
$('#today').val(today);
$('#tomorrow').val(tomorrow);
$('.ui-datepicker-trigger, #dateField').click(function(){
$('#today, #tomorrow').prop('checked', false);
$('#custom').prop('checked', true);
});
});
</script>