/
common.php
194 lines (173 loc) · 5.58 KB
/
common.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
<?php
error_reporting(E_ALL & ~E_NOTICE);
//error_reporting(E_ALL);
ini_set('display_errors', '1');
$execution_time = microtime();
session_start();
date_default_timezone_set("America/New_York");
function databaseConnect($database = "lonelydadmeetup.com"){
$con = mysql_connect("localhost","lonelydadmeetup","justdoit");
if (!$con){
die(errorDisplay('Could not connect: ' . mysql_error(), '***EXTREME***'));
}
mysql_select_db($database, $con);
}
databaseConnect(); //Make an active connection to the database
include_once('functions.php');
//-------------- Check if $_POST variables sent ---------------------
function parameter($parameter, $form = '0')
{
if ($form == 0) {
if (isset($_GET[$parameter])) {
return ($_GET[$parameter]);
exit;
}
if (isset($_POST[$parameter])) {
return ($_POST[$parameter]);
exit;
}
if (isset($_COOKIE[$parameter])) {
return ($_COOKIE[$parameter]);
exit;
}
} elseif ($form == 1) {
if (parameter($parameter) != "") {
return decrypt(parameter($parameter));
}
}
return false;
}
function lastUpdated($ptime) {
$etime = time() - $ptime;
if ($etime < 1) {
return '0 seconds';
}
$a = array( 12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
7 * 24 * 60 * 60 => 'week',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($a as $secs => $str) {
$d = $etime / $secs;
if ($d >= 1) {
$r = round($d);
return $r . ' ' . $str . ($r > 1 ? 's' : '').' ago';
}
}
}
function websiteLastUpdated(){
$path = realpath($_SERVER['DOCUMENT_ROOT']);
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object){
$filetime[]=filemtime($name);
}
$filetime=max($filetime);
return lastUpdated($filetime);
}
function addOrdinalNumberSuffix($num) {
if (!in_array(($num % 100),array(11,12,13))){
switch ($num % 10) {
// Handle 1st, 2nd, 3rd
case 1: return $num.'st';
case 2: return $num.'nd';
case 3: return $num.'rd';
}
}
return $num.'th';
}
function timeTotal($ptime)
{
$etime = $ptime;
if ($etime < 1) {
return '0 seconds';
}
$a = array( //24 * 60 * 60 * 30 *12 => 'year',
//24 * 60 * 60 * 30 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($a as $secs => $str) {
$d = $etime / $secs;
if ($d >= 1) {
$r = round($d, 2);
return $r . ' ' . $str . ($r > 1 ? 's' : '');
}
}
}
function timeago($ptime)
{
$etime = $ptime - time();
if ($etime < 1) {
return '0 seconds';
}
$a = array(24 * 60 * 60 * 30 * 12 => 'year',
24 * 60 * 60 * 30 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($a as $secs => $str) {
$d = $etime / $secs;
if ($d >= 1) {
$r = round($d);
return 'in '. $r . ' ' . $str . ($r > 1 ? 's' : '') . '';
}
}
}
function getBetween($content=NULL,$start=NULL,$end=NULL){
$r = explode($start, $content);
if (isset($r[1])){
$r = explode($end, $r[1]);
return $r[0];
}
if ($start==""){
$r = explode($end, $content);
return $r[0];
}
return 'Not Found';
}
function readImageForText($filename){
mysql_query("INSERT INTO `files` (`uid`, `name`) VALUES ('".$_SESSION['uid']."', '".$filename."')");
$url_listindexes = 'https://api.idolondemand.com/1/api/sync/ocrdocument/v1';
$params1 = 'url=http://hackathon.gavalchin.com/live/uploads/'.$filename.'&mode=scene_photo&apikey=8e025234-2c19-4af9-b337-40803dfdd176';
$response = file_get_contents($url_listindexes .'?'.$params1);
$response = getBetween($response, '"text": "', '",');
$response_formatted = str_replace('\n', '<br>', $response);
$response = str_replace('\n', ' ', $response);
$tags = explode(" ", $response);
$fid = mysql_result(mysql_query("SELECT `fid` FROM `files` WHERE `uid` = '".$_SESSION['uid']."' ORDER BY `uploadTime` DESC"), 0, 'fid');
mysql_query("INSERT INTO `classFiles` (`cid`, `fid`) VALUES ('4', '".$fid."')");
foreach($tags as $tag){
mysql_query("REPLACE INTO `fileTags` (`fid` ,`tag`)VALUES ('".$fid."', '".$tag."')");
}
return $response_formatted;
}
function byteFormat($bytes, $unit = "", $decimals = 2) {
$units = array('B' => 0, 'KiB' => 1, 'MiB' => 2, 'GiB' => 3, 'TiB' => 4,
'PiB' => 5, 'EiB' => 6, 'ZiB' => 7, 'YiB' => 8);
$value = 0;
if ($bytes > 0) {
// Generate automatic prefix by bytes
// If wrong prefix given
if (!array_key_exists($unit, $units)) {
$pow = floor(log($bytes)/log(1024));
$unit = array_search($pow, $units);
}
// Calculate byte value by prefix
$value = ($bytes/pow(1024,floor($units[$unit])));
}
// If decimals is not numeric or decimals is less than 0
// then set default value
if (!is_numeric($decimals) || $decimals < 0) {
$decimals = 2;
}
// Format output
return sprintf('%.' . $decimals . 'f '.$unit, $value);
}
// -------------------------------------------- Data Handling sections in here --------------------------------------------------------