-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
155 lines (131 loc) · 5.02 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
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
<?php
include "secrets.php";
function name(){
if(isset($_GET["who"])) $name = $_GET["who"];
else $name = "Paul Allen";
preg_match('/[A-Za-z ,]+/', $name, $matches);
if(count($matches) > 0) return implode($matches,"");
else return "Paul Allen";
}
function place(){
if(isset($_GET["where"])) $place = $_GET["where"];
else $place = "Dorsia";
preg_match('/[A-Za-z0-9, ]+/', $place, $matches);
if(count($matches) > 0) return $matches[0];
else return "Dorsia";
}
function reason(){
if(isset($_GET["why"])) $reason = $_GET["why"];
else $reason = "you know the maitre d'";
preg_match('/[A-Za-z\.\' ,\-?]+/', $reason, $matches);
if(count($matches) > 0) return $matches[0];
else return "I know the Maitre D'";
}
function sanitize_when(){
if(isset($_GET["when"])) $when = $_GET["when"];
else $when = "tomorrow 1pm";
preg_match('/[A-Za-z\-0-9\ ]+/', $when, $matches);
if(count($matches) > 0) return implode($matches,"");
else return "tomorrow 1pm";
}
function duration(){
if(isset($_GET["duration"])) $duration = $_GET["duration"];
else $duration = "1";
preg_match('/[0-9]*\.?[0-9]+/', $duration, $matches);
if(count($matches) > 0) return floatval($matches[0]);
else return 1;
}
function plural($number, $text){
if($number > 1) return $text . 's';
else return $text;
}
function when(){
$when = sanitize_when();
if (($timestamp = strtotime("$when")) === false) {
return "'$when' - a non-sensical time (failed to parse)";
} else {
return date('l d F Y Hi', $timestamp);
}
}
function email(){
if(isset($_GET["email"])) $email = $_GET["email"];
else $email = "";
$sanitized_email = filter_var($email, FILTER_SANITIZE_EMAIL);
return $sanitized_email;
}
function is_valid_email(){
$email = email();
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
return true;
} else {
return false;
}
}
function busy(){
$when = when();
if($when == "a non-sensical time") {
return -1;
}
$start = strtotime($when);
$hours = duration();
$end = $start + ($hours * 3600);
$start_datetime = date(DateTime::RFC3339, $start);
$end_datetime = date(DateTime::RFC3339, $end);
$free_busy = [];
$free_busy["timeMin"] = $start_datetime;
$free_busy["timeMax"] = $end_datetime;
$free_busy["items"] = array(array("id" => EMAIL));
$key = GOOGLE_CAL_KEY;
$ch = curl_init("https://www.googleapis.com/calendar/v3/freeBusy?key=$key");
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
CURLOPT_POSTFIELDS => json_encode($free_busy)
)
);
$response = curl_exec($ch);
if($response === false){
//be ambigous on error
return -1;
}
$responseData = json_decode($response, TRUE);
$busy = $responseData["calendars"][EMAIL]["busy"];
return count($busy);
}
function swap_subjects($reason){
$swaps = array();
//ideally implement this http://www.learnersdictionary.com/qa/when-to-use-i-and-when-to-use-me
//TODO If a 2nd person subject is preceded by a verb or a conjunction, it should be mapped to me, otherwise I
//Look for exceptions
$swaps["know you"] = "know me";
$swaps["yourself"] = "myself";
$swaps["you are"] = "I am";
$swaps["myself"] = "yourself";
$swaps["you've"] = "I have";
$swaps["you're"] = "I am";
$swaps["yours"] = "mine";
$swaps["you'd"] = "I would or I had";
$swaps["mine"] = "yours";
$swaps["your"] = "my";
$swaps["i am"] = "you are";
$swaps["i've"] = "you have";
$swaps["i'm"] = "you are";
$swaps["i'd"] = "you would or you had";
$swaps["you"] = "I";
$swaps["me"] = "you";
$swaps["my"] = "your";
$swaps["i"] = "you";
foreach($swaps as $from => $to) {
$from_hash = md5($from);
$reason = preg_replace("/\b$from\b/i",$from_hash,$reason);
}
foreach($swaps as $from => $to) {
$from_hash = md5($from);
$reason = preg_replace("/\b$from_hash\b/",$to,$reason);
}
return $reason;
}
?>