0) { $Dippler->view_page($page, $handler); } else if (is_array($page) AND count($page)<=0){ $page []= "home"; $Dippler->view_page($page, "courses"); } else { $Dippler->view_page("page_not_found"); } function forward($location = "") { if (!headers_sent()) { if ((substr_count($location, 'http://') == 0) && (substr_count($location, 'https://') == 0)) { $location = WWW_ROOT.$location; } // Only our own locations are acceptable if (!(strpos($location,WWW_ROOT) === 0)) { $location = WWW_ROOT; } header("location:".$location); exit; } return false; } function gatekeeper() { global $Dippler; if (!$Dippler->is_logged_in()) { $Dippler->add_system_message(_("Login required."), "error"); forward(); } } function admin_gatekeeper() { global $Dippler; if (!($Dippler->is_logged_in() && $Dippler->is_admin())) { $Dippler->add_system_message(_("Admin login required."), "error"); forward(); } } function query($sql) { global $Dippler; return $Dippler->db->query($sql); } function query_row($sql) { $res = query($sql); if ($res) { $ret = mysql_fetch_object($res); return $ret; } return false; } function query_insert($sql) { $res = query($sql); if ($res) { return mysql_insert_id(); } return false; } function query_update($sql) { $res = query($sql); if ($res) { return true; } return false; } function get_input($variable, $default = "") { if (isset($_REQUEST[$variable])) { if (is_array($_REQUEST[$variable])) { $var = $_REQUEST[$variable]; } else { $var = trim($_REQUEST[$variable]); } return $var; } return $default; } function get_user_by_id($user_id) { $user = new User($user_id); $user->load(); return $user; } function get_course_by_id($course_id) { $course = new Course($course_id); $course->load(); return $course; } function is_logged_in() { global $Dippler; return $Dippler->is_logged_in(); } function is_admin_logged_in() { global $Dippler; if ($Dippler->is_logged_in()) { return $Dippler->is_admin(); } return false; } function is_admin($user = NULL) { global $Dippler; return $Dippler->is_admin($user); } function is_teacher($user = NULL) { global $Dippler; return $Dippler->is_teacher($user); } function is_learner($user = NULL) { global $Dippler; return $Dippler->is_learner($user); } function get_logged_in_user() { global $Dippler; return $Dippler->get_logged_in_user(); } function get_logged_in_user_id() { global $Dippler; $user = $Dippler->get_logged_in_user(); if ($user) { return $user->getId(); } return 0; } function normalize_credits($value) { if (substr_count($value, ',') > 0) { return str_replace(',', '.', $value); } return $value; } function check_credits($value) { $value = normalize_credits($value); if (is_numeric($value)) { return true; } return false; } function date_into_timestamp($date) { if (is_numeric($date) && $date > 86400) { return $date; } $replacables = array('.', ' ', '/', ','); $date = str_replace($replacables, '-', $date); return strtotime($date); } /* Action token system */ // TODO Consider adding $visibleerrors as first parameter function validate_action_token($token = NULL, $ts = NULL) { global $Dippler; if (!$token) { $token = get_input("__token"); } if (!$ts) { $ts = get_input("__ts"); } // TODO Either define that in the config.php or just drop that alltogether and use the hard-coded value if (!defined('ACTION_TOKEN_TIMEOUT')) { $timeout = 2; } else { $timeout = ACTION_TOKEN_TIMEOUT; } $session_id = session_id(); $bos_session_id = $_SESSION['__sid']; if (($token) && ($ts) && ($session_id) && ($bos_session_id)) { $generated_token = generate_action_token($ts); if ($token == $generated_token) { $hour = 60 * 60; $timeout = $timeout * $hour; $now = time(); if (($timeout == 0) || (($ts>$now-$timeout) && ($ts<$now+$timeout))) { return true; } else { $Dippler->add_system_message(_("Action has timed out."), "error"); } } else { $Dippler->add_system_message(_("Token mismatch"), "error"); } } else { $Dippler->add_system_message(_("Missing token values"), "error"); } return false; } function generate_action_token($timestamp) { global $Dippler; return $Dippler->generate_action_token($timestamp); } function action_gatekeeper() { if (validate_action_token()) { return true; } // TODO Check if using referer makes sense if ($_SERVER['HTTP_REFERER'] && (strpos($_SERVER['HTTP_REFERER'],WWW_ROOT) === 0)) { forward($_SERVER['HTTP_REFERER']); } else { forward(); } exit; } function check_url_exists($url) { if (!empty($url)) { $parsed_url = parse_url($url); if ($parsed_url && isset($parsed_url['scheme'])) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($ch, CURLOPT_MAXREDIRS, 5); curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpcode>=200 && $httpcode<300) return true; } } return false; } function get_oembed_code($url) { $oembed_url = ""; $parsed_url = parse_url($url); if (in_array($parsed_url['host'], array('youtube.com', 'www.youtube.com'))) { $oembed_url = "http://www.youtube.com/oembed?url=".urlencode($url)."&format=json&maxwidth=560"; } else if (in_array($parsed_url['host'], array('slideshare.net', 'www.slideshare.net'))) { $oembed_url = "http://www.slideshare.net/api/oembed/2?url=".urlencode($url)."&format=json&maxwidth=560"; } else if (in_array($parsed_url['host'], array('lemill.net', 'www.lemill.net'))) { // Hard-coded collection case for lemill.net $lemill_collection_regex_pattern = "/^http\:\/\/(?:www\.)?lemill.net\/(?:lemill-server\/)?community\/people\/[a-zA-Z0-9_-]+\/collections\/[a-zA-Z0-9_-]+\//"; // Add slash to the end of the URL if not present, regualr expression requires that if (!(substr($url, -1) == '/')) { $url .= '/'; } if(preg_match($lemill_collection_regex_pattern, $url, $matches) === 1) { // Using the result returned for the first match return ""; } // Fail if pattern does not match return false; } else { return false; } $ch = curl_init($oembed_url); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($ch, CURLOPT_MAXREDIRS, 5); $data = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpcode == 200) { $data = json_decode($data); if (is_object($data) && isset($data->html)) return $data->html; } return false; } ?>