PageRenderTime 44ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/functions.inc.php

https://gitlab.com/BGCX261/zona-cal-svn-to-git
PHP | 317 lines | 234 code | 43 blank | 40 comment | 48 complexity | beab1f830cbc0158356e9385649136bb MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?
  2. define("BASE_PATH", "/Users/drake/Sites/zona-cal/");
  3. function isAdmin() {
  4. return ($_GET['admin'] == 'weak');
  5. }
  6. function createCalendar($name, $email, $password) {
  7. // add entry to reference file
  8. $encryptedPassword = crypt($password, $email);
  9. $calendarFile = calendarFileName($email);
  10. $data = array(
  11. $email,
  12. $name,
  13. $encryptedPassword,
  14. $calendarFile
  15. );
  16. $handle = referenceFileOpen("a");
  17. $output = implode(',', $data) . "\n";
  18. fwrite($handle, $output);
  19. fclose($handle);
  20. // create the actual calendar
  21. $path = BASE_PATH . "calendars/" . $calendarFile . ".ics";
  22. fopen($path, "w")
  23. or die("<br><br>_alert! this process did not complete all the way! the calendar file was not created! please COPY \"none.ics\" to be named \"".$calendarFile.".ics\" in the proper calendar directory (wherever that is). sorry for the trouble. this is a nasty but known feature. it is on the list of things to fix.");
  24. //TODO: fix this so it actually creates a file.
  25. }
  26. // this will check if you are a valid user and return the calendar filename
  27. // if you are a valid user and false if no user is found
  28. function validateUser($email, $password) {
  29. $handle = referenceFileOpen("r");
  30. while ($data = fgetcsv($handle, 2000))
  31. {
  32. if ($data[0] == $email && $data[2] == crypt($password, $email))
  33. return $data[3];
  34. }
  35. fclose($handle);
  36. return false;
  37. }
  38. function referenceFileOpen($mode) {
  39. $path = BASE_PATH . 'admin/ref.txt';
  40. return fopen($path, $mode);
  41. }
  42. function calendarFileName($email) {
  43. $text = explode('@', $email);
  44. $filename = $text[0];
  45. // .ics extension will be added later for security reasons
  46. return $filename;
  47. }
  48. function scheduleMeeting($calendar, $startTime, $reason, $note, $name, $email) {
  49. $endTime = strtotime("+ 15 minutes", $startTime);
  50. $status = timeAvailable($calendar, $startTime, $endTime);
  51. if ($status == -1)
  52. return false;
  53. $attendee = $name . " - " . $email;
  54. $eventText = $reason . " - " . $note;
  55. // all meetings scheduled are private
  56. $class = 'PRIVATE';
  57. // all meetings are new events, so the uid is blank/new
  58. $uid = "";
  59. // the professor is now busy during this time, mark it as so.
  60. $categories = "UNAVAILABLE";
  61. $eventStart = date("Ymd", $startTime) . 'T' . date("His", $startTime);
  62. $eventEnd = date("Ymd", $endTime) . 'T' . date("His", $endTime);
  63. if ($status == 1)
  64. {
  65. return createEvent($uid, $calendar, $eventStart, $eventEnd, $eventText, $class, $attendee, $categories);
  66. }
  67. else if ($status == 0)
  68. {
  69. // on demand, you tricky case you.
  70. // give it a unique uid TODO: look this up to make sure i am doing it right.
  71. $uid = uniqid('-', true);
  72. // save the pending event to a file
  73. writePendingEvent($uid, $calendar, $eventStart, $eventEnd, $eventText, $class, $attendee, $categories);
  74. // send an email to a professor with a link to create the event or deny it
  75. // event is not identified by uid.
  76. $professor = infoForCalendar($calendar);
  77. $body = "Hello " . $professor['name'] . ",\n";
  78. $body .= $name . " would like to schedule a meeting with you\n";
  79. $body .= "on " . date("l", $startTime) . " the " . date("d", $startTime) . " of " . date("F", $startTime) . "\n";
  80. $body .= "at " . date("g:i a", $startTime) . "\n\n";
  81. //TODO: add correct links
  82. $body .= "accept - <link to event accept page>\n";
  83. $body .= "decline - <link to event decline page>\n";
  84. if (!sendEmail($professor['email'], "Meeting Requested", $body))
  85. return false;
  86. //TODO: fix!
  87. return "on_demand";
  88. }
  89. }
  90. function writePendingEvent($uid, $calendar, $eventStart, $eventEnd, $eventText, $class, $attendee, $categories) {
  91. $handle = openPendingFile("a");
  92. $out = "$uid,$calendar,$eventStart,$eventEnd,$eventText,$class,$attendee,$categories";
  93. return fwrite($handle, $out);
  94. }
  95. function openPendingFile($mode) {
  96. //TODO: get this filename from a config file
  97. $filename = BASE_PATH . "pending.txt";
  98. return fopen($filename, $mode);
  99. }
  100. function createEventFromPending($uid) {
  101. // load in the file
  102. $handle = openPendingFile("r");
  103. while ($line = fgets($handle))
  104. {
  105. if ($uid == $line.substr(0, $uid.length()))
  106. {
  107. $data = explode(',', $line);
  108. // if approved
  109. if ($decision = 'y')
  110. {
  111. createEvent("", $data[1], $data[2], $data[3], $data[4], $data[5], $data[6], $data[7]);
  112. return true;
  113. }
  114. // if rejected. else should be okay because it was validated above
  115. else
  116. {
  117. // send email to student notifying them of the rejection. fix!
  118. $student = infoForAttendee($data[6]);
  119. $body = "Hello " . $student['name'] . ".\n";
  120. $body .= "Your meeting was declined.\n";
  121. $body .= "Better luck next time!\n";
  122. sendEmail($student['email'], "Meeting Declined", $body);
  123. return true;
  124. }
  125. }
  126. }
  127. return false;
  128. }
  129. function scheduleEvent($calendar, $startTime, $endTime, $class, $summary, $categories, $recur = null) {
  130. //creating implies new, so the uid is blank/new
  131. $uid = "";
  132. $eventStart = date("Ymd", $startTime) . 'T' . date("His", $startTime);
  133. $eventEnd = date("Ymd", $endTime) . 'T' . date("His", $endTime);
  134. // nobody should be attending the event.
  135. $attendee = "";
  136. require_once(BASE_PATH . 'monket-cal/monket-cal-update.php');
  137. $result = doUpdate($uid, $calendar, $eventStart, $eventEnd, $summary, $class, $attendee, $categories, $recur);
  138. if (strpos($result, "success") != 0)
  139. return false;
  140. return true;
  141. }
  142. function infoForCalendar($calendar) {
  143. $handle = referenceFileOpen("r");
  144. while ($line = fgets($handle))
  145. {
  146. $parts = explode(",", $line);
  147. if (trim($parts[3]) == $calendar)
  148. return array('name' => $parts[1], 'email' => $parts[0]);
  149. }
  150. return "unknown";
  151. }
  152. function infoForAttendee($attendee) {
  153. $parts = explode(" - ", $attendee);
  154. return array("name" => $parts[0], "email" => $parts[1]);
  155. }
  156. // returns -1 on unschedulable
  157. // 0 on on_demand
  158. // 1 on available
  159. // more intuative name might be [prior] status of time or something?
  160. function timeAvailable($calendar, $eventStart, $eventEnd) {
  161. if (timeIsDuringHours($start, $end))
  162. return -1;
  163. $status = 1;
  164. $events = eventsDuringTime($eventStart, $eventEnd);
  165. foreach ($events as $event)
  166. {
  167. if (eventIsUnavailable($event))
  168. return -1;
  169. if (eventIsOndemand($event))
  170. $status = 0;
  171. }
  172. return $status;
  173. }
  174. function timeIsDuringHours($start, $end) {
  175. // can be set by user to determine valid hours in the day.
  176. // TODO: should be set by a config file
  177. $validHourStart = "08";
  178. $validHourEnd = "17";
  179. $hour = strtotime("H", $start);
  180. if ($validHourStart > $hour || $hour >= $validHourEnd)
  181. return false;
  182. $hour = strtotime("H", $end);
  183. //subtle difference here. notice the = sign
  184. if ($validHourStart > $hour || $hour > $validHourEnd)
  185. return false;
  186. return true;
  187. }
  188. function eventsDuringTime($start, $end) {
  189. global $master_array;
  190. $co_events = Array();
  191. $key = date("Ymd", $start);
  192. // potentials are all events occuring that same day
  193. $potentials = $master_array[$key];
  194. $start = date("Hi", $start);
  195. $end = date("Hi", $end);
  196. if (isset($potentials))
  197. {
  198. foreach ($potentials as $potential)
  199. {
  200. foreach ($potential as $event)
  201. {
  202. $start_test = $event['event_end'] >= $start && $start >= $event['event_start'];
  203. $end_test = $event['event_end'] >= $end && $end >= $event['event_start'];
  204. $inside_test = $event['event_start'] > $start && $event['event_end'] < $end;
  205. if ($start_test || $end_test || $inside_test)
  206. {
  207. $co_events[] = $event;
  208. }
  209. }
  210. }
  211. }
  212. return $co_events;
  213. }
  214. function eventIsUnavailable($event){
  215. return $event['categories'] == 2;
  216. }
  217. function eventIsAvailable($event){
  218. return $event['categories'] == 1;
  219. }
  220. function eventIsOndemand($event){
  221. return $event['categories'] == 3;
  222. }
  223. function createEvent($uid, $calendar, $eventStart, $eventEnd, $eventText, $class, $attendee, $categories) {
  224. require_once(BASE_PATH . 'monket-cal/monket-cal-update.php');
  225. $result = doUpdate($uid, $calendar, $eventStart, $eventEnd, $eventText, $class, $attendee, $categories);
  226. if (strpos($result, "success") != 0)
  227. return false;
  228. $professor = infoForCalendar($calendar);
  229. // the writing of this email should probably be done in a function
  230. // send email to scheduler
  231. $body = "A meeting has been scheduled.\n";
  232. $body .= $professor['name'] . " will be meeting with " . $name . "\n";
  233. $body .= "on " . date("l", $startTime) . " the " . date("d", $startTime) . " of " . date("F", $startTime) . "\n";
  234. $body .= "at " . date("g:i a", $startTime) . "\n";
  235. $body .= "\nThis meeting was scheduled via zona-cal";
  236. if (!sendEmail($email, "Meeting Scheduled", $body))
  237. return false;
  238. // send email to professor
  239. if (!sendEmail($professor['email'], "Meeting Scheduled", $body))
  240. return false;
  241. return $body;
  242. }
  243. function sendEmail($to, $subject, $body) {
  244. // TODO: cut the message length down to 70 chars per line.
  245. return mail($to, $subject, $body);
  246. }
  247. function debug_dump($data) {
  248. echo "<pre>";
  249. print_r($data);
  250. echo "</pre>";
  251. }
  252. function html_calendarSelect($name, $selectedVal) {
  253. $handle = referenceFileOpen("r");
  254. echo "<select name=\"$name\" onchange=\"window.location='?cal='+this.value\">\n";
  255. // put in the default case
  256. echo "<option value=\"none\">Select a professor</option>\n";
  257. // print all calendars names
  258. while ($line = fgets($handle))
  259. {
  260. // get out the name and calendar from the file
  261. $fields = explode(',', $line);
  262. $value =trim($fields[3]);
  263. $name = $fields[1];
  264. //deal with the selected value
  265. if ($selectedVal == $value)
  266. $selected = " selected ";
  267. else
  268. $selected = "";
  269. //print the option statement
  270. echo "<option value=\"$value\"$selected>$name</option>\n";
  271. }
  272. echo "</select>\n";
  273. }
  274. ?>