Files
@ 6012ec60a8b0
Branch filter:
Location: SlatePermutate/auto.php
6012ec60a8b0
4.5 KiB
text/x-php
Consistently provide the JS frontend with unescaped values for add_section_n() and add_class_n() calls, whereas in the past the autocomplete data didn't htmlentities() escape the values while editing a saved schedule did. Now properly handle unescaped data in the javascript.
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 | <?php
/*
* Copyright 2010 Nathan Phillip Brink <ohnobinki@ohnopublishing.net>
*
* This file is a part of slate_permutate.
*
* slate_permutate is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* slate_permutate is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with slate_permutate. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file
* This file's purpose is to autocomplete class names for supporting
* the autocomplete JS based off of crawling schools' registration
* websites. This shall only perform the autocompletion of class
* names.
*
* Since we output JSON, no special Page classes and stuff
* :-p. Except we still call the Page class's session_start()
* function because we apparently need sessions.... oh yeah, for
* school profile supports ;-).
*/
require_once('inc/school.inc');
require_once('inc/class.page.php');
require_once('inc/class.course.inc');
page::session_start();
if (isset($_REQUEST['txt'])) {
header('Content-Type: text/plain; encoding=utf-8');
}
else {
header('Content-Type: application/json; encoding=utf-8');
}
if (!isset($_REQUEST['term'])) {
clean_empty_exit();
}
$getsections = FALSE;
if (isset($_REQUEST['getsections'])) {
$getsections = TRUE;
}
$term = $_REQUEST['term'];
$term_parts = Course::parse($term);
if (!count($term_parts)) {
clean_empty_exit();
}
$school = school_load_guess();
if (!$school['crawled']) {
clean_empty_exit();
}
$semester = school_semester_guess($school);
$cache_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'auto'
. DIRECTORY_SEPARATOR . $school['id'] . DIRECTORY_SEPARATOR . $semester['id'] . DIRECTORY_SEPARATOR;
/*
* autocomplete the list of departments. If the user has already
* entered a valid department name _and_ delimitted it, however, go on
* to the next autocompletion step.
*/
$term_strlen = strlen($term);
$dept_strlen = strlen($term_parts['department']);
$dept = $term_parts['department'];
if (!$getsections && count($term_parts) == 1 && $term_strlen == strlen($dept))
{
$dept_file = $cache_dir . '-depts';
if (!file_exists($dept_file)) {
clean_empty_exit();
}
$departments = unserialize(file_get_contents($dept_file));
$json_depts = array();
foreach ($departments as $key => $department) {
if (!strncmp($department, $dept, $dept_strlen)) {
$json_depts[] = $department;
}
}
echo json_encode($json_depts);
exit(0);
}
if ($getsections)
{
if (!isset($term_parts['course']))
{
/* user didn't give us enough information */
header('HTTP/1.1 404: Nof found');
header('Content-Type: text/plain; encoding=utf-8');
echo 'Not a fully-qualified course name: ' . implode('-', $term_parts) . "\n";
exit(0);
}
$section_file = $cache_dir . $dept . DIRECTORY_SEPARATOR . $term_parts['course'];
if (file_exists($section_file))
{
readfile($section_file);
exit(0);
}
/* Section not found! */
header('HTTP/1.1 404: Not found');
header('Content-Type: text/plain; encoding=utf-8');
echo 'Could not find course ' . implode('-', $term_parts) . "\n";
exit(0);
}
/*
* If a department is fully entered, life gets slightly more
* complicated. I suppose I only want to autocomplete the first digit
* of the course/class number. I.e., CS-2 for CS-262 for when the
* student has entered CS- or 'CS'. But for now we can just dump the entire department at the user ;-).
*/
$classes_file = $cache_dir . $dept . '.sects';
if (file_exists($classes_file))
{
$classes = unserialize(file_get_contents($classes_file));
$class_start = '';
if (count($term_parts) > 1)
$class_start = $term_parts['course'];
$class_start_strlen = strlen($class_start);
/* reduce/create resultset */
$json_classes = array();
foreach ($classes as $class)
if (!strncmp($class, $class_start, $class_start_strlen))
{
$json_classes[] = $dept . '-' . $class;
}
echo json_encode($json_classes);
exit(0);
}
/**
* Nothing caught
*/
clean_empty_exit();
/**
* \brief
* Send an empty JSON array and exit.
*/
function clean_empty_exit()
{
echo '[]';
exit(0);
}
|