forked from johnwarne/reddit-top-rss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
117 lines (112 loc) · 3.68 KB
/
functions.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
<?php
// Get file
function getFile($url, $requestType, $cachedFileLocation, $cacheExpiration) {
if($requestType == "mercuryJSON") {
// Get Mercury content
if(CACHE_MERCURY_CONTENT) {
// Use cached file if present
if (file_exists($cachedFileLocation) && time()-filemtime($cachedFileLocation) < $cacheExpiration) {
return file_get_contents($cachedFileLocation, true);
} else {
// Otherwise, CURL the file and cache it
$ch = curl_init();
$curlOptHttpHeaderArray = ['Content-Type: application/json'];
if (defined('MERCURY_API_KEY') && !empty(MERCURY_API_KEY)) {
$curlOptHttpHeaderArray[] = 'x-api-key: ' . MERCURY_API_KEY;
}
curl_setopt($ch, CURLOPT_URL, MERCURY_URL . '/parser?url=' . $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $curlOptHttpHeaderArray);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$mercuryJSON = curl_exec($ch);
if(!$mercuryJSON) {
die("Connection Failure");
}
file_put_contents($cachedFileLocation, $mercuryJSON);
return $mercuryJSON;
curl_close($ch);
}
} else {
// Regularly CURL the Mercury content
$curl = curl_init();
$curlOptHttpHeaderArray = ['Content-Type: application/json'];
if (defined('MERCURY_API_KEY') && !empty(MERCURY_API_KEY)) {
$curlOptHttpHeaderArray[] = 'x-api-key: ' . MERCURY_API_KEY;
}
curl_setopt($curl, CURLOPT_URL, MERCURY_URL . '/parser?url=' . $itemDataUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, $curlOptHttpHeaderArray);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$mercuryJSON = curl_exec($curl);
if(!$mercuryJSON) {
die("Connection Failure");
}
return $mercuryJSON;
curl_close($ch);
}
} elseif($requestType == "redditJSON" && CACHE_REDDIT_JSON) {
// Get Reddit JSON file
// Use cached file if present
if (file_exists($cachedFileLocation) && time() - filemtime($cachedFileLocation) < $cacheExpiration) {
return file_get_contents($cachedFileLocation, true);
} else {
// Otherwise, CURL the file and cache it
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_URL, $url);
$curledFile = curl_exec($ch);
file_put_contents($cachedFileLocation, $curledFile);
return $curledFile;
curl_close($ch);
}
} else {
// Regularly CURL the file
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_URL, $url);
$curledFile = curl_exec($ch);
return $curledFile;
curl_close($ch);
}
}
// Get directory size
// https://www.a2zwebhelp.com/folder-size-php
function directorySize($dir) {
$countSize = 0;
$count = 0;
$dirArray = scandir($dir);
foreach($dirArray as $key => $filename) {
if($filename != ".." && $filename != ".") {
if(is_dir($dir . "/" . $filename)) {
$newFolderSize = directorySize($dir . "/" . $filename);
$countSize = $countSize + $newFolderSize;
} elseif(is_file($dir . "/" . $filename)) {
$countSize = $countSize + filesize($dir . "/" . $filename);
$count++;
}
}
}
return $countSize;
}
// Format size in bytes
// https://www.a2zwebhelp.com/folder-size-php
function sizeFormat($bytes) {
$kb = 1024;
$mb = $kb * 1024;
$gb = $mb * 1024;
$tb = $gb * 1024;
if (($bytes >= 0) && ($bytes < $kb)) {
return $bytes . " B";
} elseif (($bytes >= $kb) && ($bytes < $mb)) {
return ceil($bytes / $kb) . " KB";
} elseif (($bytes >= $mb) && ($bytes < $gb)) {
return ceil($bytes / $mb) . " MB";
} elseif (($bytes >= $gb) && ($bytes < $tb)) {
return ceil($bytes / $gb) . " GB";
} elseif ($bytes >= $tb) {
return ceil($bytes / $tb) . " TB";
} else {
return $bytes . " B";
}
}