if this is wrong"); require_once("schools/$schoolYear/config.php"); // exports $elections } // // A hash is used to keep the student_id itself secret and ensure that // the student_id is not used directly for the vote booths function getHash($student_id) { return sprintf("%u", crc32($student_id)); } // Fetches a short or long human-friendly school name from per-year config file // If the given school does not have anything set up for the current $year, // then this returns $schoolNameDefault. function schoolName($form = SHORT, $school = false) { $file = null; if($school) { global $year; $file = "schools/$school/$year/config.ini"; } return config("{$form}name", "", $file); } // config("superlatives/votes"); attempts to get config_ini()['superlatives']['votes']; // If there is no configuration file for the current school, $default is returned. $configs = array(); function config($path, $default = "", $filename = null) { global $configs; // Cache per-filename configuration so we don't hit the disk time after time. if(!array_key_exists($filename, $configs)) { $configs[$filename] = config_ini(null, $filename); } $config = $configs[$filename]; $parts = explode('/', $path); foreach($parts as $part) { @$config = $config[ $part ]; } if($config == NULL) return $default; else return $config; } // Dual-use function: // 1) config() fetches configuration from ini file, // 2) config(array, filename?) writes out array to ini file. function config_ini( $array = null, $filename = null) { global $config_file; if(!$filename) $filename = $config_file; if(!is_readable($filename)) { //$filename = "schools/default.ini"; return NULL; } if(!$array) return parse_ini_file($filename, true); else file_put_contents($filename, ini_stringify($array)); } // Helper function for config(), takes a possibly-nested array and flattens it function ini_stringify($array) { $rv = ''; foreach($array as $key => $value) { if(gettype($value) == 'array') { $rv .= "\n[$key]\n"; $rv .= ini_stringify($value); } else if(preg_match('/[{}|&~!\[()"]/', $value)) $rv .= "$key = \"$value\"\n"; // add quotes if special chars else $rv .= "$key = $value\n"; // most strings don't need quotes } return $rv; } function redirect_to($url) { header("Location: ".$url); exit; } function last_char_of($str) { return $str[strlen($str)-1]; } // URI = http://v.e.o/dir/action // link_to('action2') returns http://v.e.o/dir/action2 // link_to('/detail') returns http://v.e.o/dir/action/detail function link_to($path) { global $CURRENT_URI; return path_swap($CURRENT_URI, $path); } // for $dir = "/append" // converts "http://vote.eschew.org/brandywine" // to "http://vote.eschew.org/brandywine/append" function path_swap($path, $dir) { $path_parts = explode("?", $path, 2); $path = $path_parts[0]; if(last_char_of($path) != '/') $path = "$path/"; if($dir[0] == '/') { $path .= substr($dir, 1); } else { $path = dirname($path) . '/' . $dir; } $path_parts[0] = $path; return implode('?', $path_parts); } function ulist($callback, $array) { $str = '"; return $str; } ?>