diff --git a/application/libraries/Gitlib.php b/application/libraries/Gitlib.php index ce0d944..5eae228 100644 --- a/application/libraries/Gitlib.php +++ b/application/libraries/Gitlib.php @@ -1,137 +1,150 @@ -\d+)\.(?P\d+)(?P-[\w\.]+)?-(?P\d+)-(?Pg.+)/'; - - protected $_hash; - protected $CI; - - static $REVISION_FILE = 'version.json'; - - public function __construct() { - $this->CI =& get_instance(); - - self::$REVISION_FILE = dirname(__FILE__) .'/../../'. self::$REVISION_FILE; - } - - /** - * Return the start of the most recent commit hash (from file). - * Maybe md5/ sha() the result? - */ - public function lastHash($length = 6) { - if ($length < 4) $length = 4; - - if ($this->_hash) { - return substr($this->_hash, 0, $length); - } - - $log = $this->get_revision(); - $this->_hash = $log->commit; - - return substr($this->_hash, 0, $length); - } - - /** Save revision meta-data to a '.' file, JSON-encoded. - * (CloudEngine's Hglib uses PHP (un)serialize.) - */ - public function put_revision() { - $log = $this->_exec('log -1'); - - $log = explode("\n", $log); - $result = FALSE; - //Hmm, a more efficient way? - foreach ($log as $line) { - if (FALSE !== ($p = strpos($line, ' '))) { #':' - $key = trim(substr($line, 0, $p), ' :'); - if (!$key) $key = 'message'; - $result[strtolower($key)] = trim(substr($line, $p+1)); - if ('message'==$key) break; - } - } - // Describe "v0.86-usertest-95-g.." - // Semantic Versioning, http://semver.org - $result['describe'] = trim($this->_exec('describe --tags --long')); - $result[ 'version' ] = $result[ 'describe' ]; - if (preg_match( self::GIT_DESCRIBE_REGEX, $result[ 'describe' ], $m )) { - $result[ 'version' ] = $m['major'] .'.'. $m['minor'] .'.'. $m['patch'] . $m['id'] .'+'. $m['hash']; - } - // http://stackoverflow.com/questions/4089430/how-can-i-determine-the-url-that-a-local-git-repo-was-originally-pulled-from - $result['origin'] = rtrim($this->_exec('config --get remote.origin.url'), "\r\n"); - #$result['origin url'] = str_replace(array('git@', 'com:'), array('https://', 'com/'), $result['origin']); - #$result['agent'] = basename(__FILE__); - #$result['git'] = rtrim($this->_exec('--version'), "\r\n "); - $result['file_date'] = date( 'c' ); - - $bytes = $this->put_json(self::$REVISION_FILE, $result); - - echo "File written, $bytes: ", self::$REVISION_FILE; - - return $result; - } - - /** Read revision meta-data from the '.' file. - */ - public function get_revision() { - return (object) json_decode(file_get_contents(self::$REVISION_FILE)); - } - - - protected function put_json( $filename, $data ) { - return file_put_contents( $filename, str_replace( '","', "\",\n\"", json_encode( $data ))); - } - - /** Execute a Git command, if allowed. - */ - protected function _exec($cmd) { - - if (! $this->CI->input->is_cli_request()) { - echo "Warning, Git must be run from the commandline.".PHP_EOL; - return FALSE; - } - - //Security? - $git_path = $this->CI->config->item('git_path'); - - - if (! $git_path) { - $git_path = "git"; #"/usr/bin/env git"; - #$git_path = "/usr/bin/git"; #Redhat6 - #$git_path = "/usr/local/git/bin/git"; #Mac - } - - $git_cmd = "$git_path $cmd"; - - $result = FALSE; - // The path may contain 'sudo ../git'. - if (! file_exists($git_path)) { - echo "Warning, not found, $git_path".PHP_EOL; - } - - - #if (file_exists($git_path)) { - $cwd = getcwd(); - if ($cwd) { - chdir(APPPATH); - } - - $handle = popen(escapeshellcmd($git_cmd), 'r'); //2>&1 - $result = fread($handle, 2096); - pclose($handle); - - if ($cwd) { - chdir($cwd); - } - #} - return $result; - } - -} +\d+)\.(?P\d+)(?P-[\w\.]+)?-(?P\d+)-(?Pg.+)/'; + + protected $_hash; + protected $CI; + + protected static $REVISION_FILE = 'version.json'; + + public function __construct() + { + $this->CI =& get_instance(); + + self::$REVISION_FILE = dirname(__FILE__) .'/../../'. self::$REVISION_FILE; + } + + /** + * Return the start of the most recent commit hash (from file). + * Maybe md5/ sha() the result? + */ + public function lastHash($length = 6) + { + if ($length < 4) { + $length = 4; + } + + if ($this->_hash) { + return substr($this->_hash, 0, $length); + } + + $log = $this->get_revision(); + $this->_hash = $log->commit; + + return substr($this->_hash, 0, $length); + } + + /** Save revision meta-data to a '.' file, JSON-encoded. + * (CloudEngine's Hglib uses PHP (un)serialize.) + */ + public function put_revision() + { + $log = $this->_exec('log -1'); + + $log = explode("\n", $log); + $result = false; + //Hmm, a more efficient way? + foreach ($log as $line) { + if (false !== ($p = strpos($line, ' '))) { +#':' + $key = trim(substr($line, 0, $p), ' :'); + if (!$key) { + $key = 'message'; + } + $result[strtolower($key)] = trim(substr($line, $p+1)); + if ('message'==$key) { + break; + } + } + } + // Describe "v0.86-usertest-95-g.." + // Semantic Versioning, http://semver.org + $result['describe'] = trim($this->_exec('describe --tags --long')); + $result[ 'version' ] = $result[ 'describe' ]; + if (preg_match(self::GIT_DESCRIBE_REGEX, $result[ 'describe' ], $m)) { + $result[ 'version' ] = $m['major'] .'.'. $m['minor'] .'.'. $m['patch'] . $m['id'] .'+'. $m['hash']; + } + // http://stackoverflow.com/questions/4089430/how-can-i-determine-the-url-that-a-local-git-repo-was-originally-pulled-from + $result['origin'] = rtrim($this->_exec('config --get remote.origin.url'), "\r\n"); + #$result['origin url'] = str_replace(array('git@', 'com:'), array('https://', 'com/'), $result['origin']); + #$result['agent'] = basename(__FILE__); + #$result['git'] = rtrim($this->_exec('--version'), "\r\n "); + $result['file_date'] = date('c'); + + $bytes = $this->put_json(self::$REVISION_FILE, $result); + + echo "File written, $bytes: ", self::$REVISION_FILE; + + return $result; + } + + /** Read revision meta-data from the '.' file. + */ + public function get_revision() + { + return (object) json_decode(file_get_contents(self::$REVISION_FILE)); + } + + + protected function put_json($filename, $data) + { + return file_put_contents($filename, str_replace('","', "\",\n\"", json_encode($data))); + } + + /** Execute a Git command, if allowed. + */ + protected function _exec($cmd) + { + + if (! $this->CI->input->is_cli_request()) { + echo "Warning, Git must be run from the commandline.".PHP_EOL; + return false; + } + + //Security? + $git_path = $this->CI->config->item('git_path'); + + + if (! $git_path) { + $git_path = "git"; #"/usr/bin/env git"; + #$git_path = "/usr/bin/git"; #Redhat6 + #$git_path = "/usr/local/git/bin/git"; #Mac + } + + $git_cmd = "$git_path $cmd"; + + $result = false; + // The path may contain 'sudo ../git'. + if (! file_exists($git_path)) { + echo "Warning, not found, $git_path".PHP_EOL; + } + + + #if (file_exists($git_path)) { + $cwd = getcwd(); + if ($cwd) { + chdir(APPPATH); + } + + $handle = popen(escapeshellcmd($git_cmd), 'r'); //2>&1 + $result = fread($handle, 2096); + pclose($handle); + + if ($cwd) { + chdir($cwd); + } + #} + return $result; + } +} diff --git a/application/libraries/Layout.php b/application/libraries/Layout.php index 02a7515..ecd0820 100644 --- a/application/libraries/Layout.php +++ b/application/libraries/Layout.php @@ -1,13 +1,12 @@ -layout = $layout; } - function setLayout($layout) + public function setLayout($layout) { - $this->layout = $layout; + $this->layout = $layout; } - function view($view, $data=null, $return=false) + public function view($view, $data = null, $return = false) { $loadedData = array(); - $loadedData['content_for_layout'] = $this->obj->load->view($view,$data,true); + $loadedData['content_for_layout'] = $this->obj->load->view($view, $data, true); - if($return) - { + if ($return) { $output = $this->obj->load->view($this->layout, $loadedData, true); return $output; - } - else - { + } else { $this->obj->load->view($this->layout, $loadedData, false); } } } -?> \ No newline at end of file diff --git a/application/libraries/MY_User_agent.php b/application/libraries/MY_User_agent.php index 49c0d1a..ca98112 100644 --- a/application/libraries/MY_User_agent.php +++ b/application/libraries/MY_User_agent.php @@ -1,97 +1,102 @@ -agent_string(); - $res = 'x'; # Unknown/ other. - $codes = array( - 'MSIE 9.0' => 'ie9', - 'MSIE 8.0' => 'ie8', - 'MSIE 7.0' => 'ie7', - 'MSIE 6.0' => 'ie6', - 'MSIE ' => 'ie', #-ms? - 'Gecko/' => 'moz', - 'AppleWebKit/'=>'webkit', # Safari, Chrome. - 'Opera/' => 'o', # Presto - ); - foreach ($codes as $str => $code) { - if (false!==strpos($ua, $str)) { - $res = $code; - break; - } - } + /** Return a short code, for the browser family/ rendering engine. + */ + public function agent_code() + { + $ua = $this->agent_string(); + $res = 'x'; # Unknown/ other. + $codes = array( + 'MSIE 9.0' => 'ie9', + 'MSIE 8.0' => 'ie8', + 'MSIE 7.0' => 'ie7', + 'MSIE 6.0' => 'ie6', + 'MSIE ' => 'ie', #-ms? + 'Gecko/' => 'moz', + 'AppleWebKit/'=>'webkit', # Safari, Chrome. + 'Opera/' => 'o', # Presto + ); + foreach ($codes as $str => $code) { + if (false!==strpos($ua, $str)) { + $res = $code; + break; + } + } - return $res; - } + return $res; + } - public function browser_code() { - return str_replace('Internet Explorer', 'MSIE', $this->browser()); - } + public function browser_code() + { + return str_replace('Internet Explorer', 'MSIE', $this->browser()); + } - public function version_code() { - if ($this->is_browser('Safari')) { - $version = preg_replace('/^.*Version\/(\d+)\.(\d+).*$/', '$1 $2', $this->agent_string()); - } else { - $version = preg_replace('/(\d+)\.(\d+).*$/', '$1 $2', $this->version()); + public function version_code() + { + if ($this->is_browser('Safari')) { + $version = preg_replace('/^.*Version\/(\d+)\.(\d+).*$/', '$1 $2', $this->agent_string()); + } else { + $version = preg_replace('/(\d+)\.(\d+).*$/', '$1 $2', $this->version()); + } + return $version; } - return $version; - } - /** Return a short code, indicating the platform/ operating system. - * @return string 'win', 'osx', 'ios', 'android'.. - */ - public function platform_code() { - $platform = $this->platform(); - $pres = 'yy'; # Unknown/ other. - if (FALSE !== strpos($platform, 'Windows')) { - $pres = 'win'; - } - elseif ('Mac OS X' == $platform) { - $pres = 'osx'; + /** Return a short code, indicating the platform/ operating system. + * @return string 'win', 'osx', 'ios', 'android'.. + */ + public function platform_code() + { + $platform = $this->platform(); + $pres = 'yy'; # Unknown/ other. + if (false !== strpos($platform, 'Windows')) { + $pres = 'win'; + } elseif ('Mac OS X' == $platform) { + $pres = 'osx'; + } else { + $pres = strtolower(str_replace(array(' ', '/'), '', $platform)); + } + return $pres; } - else { - $pres = strtolower(str_replace(array(' ', '/'), '', $platform)); - } - return $pres; - } - /** + /** * Return the first acceptable language for the user (browser), from an input list of supported languages. * - * @access public - * @param mixed Either a single language code, or an array of languages supported by the application. - * @return mixed (Was:bool) + * @access public + * @param mixed Either a single language code, or an array of languages supported by the application. + * @return mixed (Was:bool) */ - public function accept_lang($lang = NULL) { - $lang_res = NULL; + public function accept_lang($lang = null) + { + $lang_res = null; - if (is_string($lang)) return parent::accept_lang($lang); + if (is_string($lang)) { + return parent::accept_lang($lang); + } - // Else, an array of locales supported by the application. - $supported_locales = $lang; + // Else, an array of locales supported by the application. + $supported_locales = $lang; - $user_accept_languages = $this->languages(); + $user_accept_languages = $this->languages(); - // Important: find the first acceptable language for the user (browser) - order matters! - foreach ($user_accept_languages as $try_lang) { - if (in_array($try_lang, $supported_locales)) { - $lang_res = $try_lang; - break; - } - } + // Important: find the first acceptable language for the user (browser) - order matters! + foreach ($user_accept_languages as $try_lang) { + if (in_array($try_lang, $supported_locales)) { + $lang_res = $try_lang; + break; + } + } - return $lang_res; - //return (in_array(strtolower($lang), $this->languages(), TRUE)); - } - + return $lang_res; + //return (in_array(strtolower($lang), $this->languages(), TRUE)); + } } diff --git a/application/libraries/Oembed_Provider.php b/application/libraries/Oembed_Provider.php index 3d68498..9962cb7 100644 --- a/application/libraries/Oembed_Provider.php +++ b/application/libraries/Oembed_Provider.php @@ -1,4 +1,4 @@ -CI =& get_instance(); + /** Constructor: auto-generate 'name' property. + */ + public function __construct() + { + $this->CI =& get_instance(); - // We use $this - an instance, not a class. - $this->name = strtolower(preg_replace('#_serv$#i', '', get_class($this))); + // We use $this - an instance, not a class. + $this->name = strtolower(preg_replace('#_serv$#i', '', get_class($this))); - // Get the Google Analytics ID, if available. - $this->_google_analytics = google_analytics_id($this->name); - } + // Get the Google Analytics ID, if available. + $this->_google_analytics = google_analytics_id($this->name); + } - /** Get the machine-readable name for the Scripts controller. + /** Get the machine-readable name for the Scripts controller. * @return string */ - public function getName() { return $this->name; } + public function getName() + { + return $this->name; + } - /** Get the oEmbed type for the Scripts controller. - */ - public function getType() { return $this->type; } + /** Get the oEmbed type for the Scripts controller. + */ + public function getType() + { + return $this->type; + } - /** Get the path to the view for the Oembed controller (relative to application/views directory, without '.php'). + /** Get the path to the view for the Oembed controller (relative to application/views directory, without '.php'). * @return string */ - public function getView() { - return 'oembed/'. $this->name; - } + public function getView() + { + return 'oembed/'. $this->name; + } - /** Get the regular expression for the Oembed controller. + /** Get the regular expression for the Oembed controller. * @return string */ - public function getInternalRegex() { - return $this->_regex_real ? $this->_regex_real : str_replace(array('*', '/'), array('([\w_-]*?)', '\/'), $this->regex); - } + public function getInternalRegex() + { + return $this->_regex_real ? $this->_regex_real : str_replace(array('*', '/'), array('([\w_-]*?)', '\/'), $this->regex); + } - /** Get the Google Analytics account ID. + /** Get the Google Analytics account ID. * @return string */ - public function getAnalyticsId() { - return $this->_google_analytics; - } + public function getAnalyticsId() + { + return $this->_google_analytics; + } - /** Get 'published' provider-properties for Services controller (Cf. http://api.embed.ly/1/services) + /** Get 'published' provider-properties for Services controller (Cf. http://api.embed.ly/1/services) * @return object */ - public function getProperties() { - $choose = explode('|', 'regex|about|displayname|name|domain|subdomains|favicon|type'); - $props = (object)array(); - foreach (get_object_vars($this) as $key => $value) { - if (in_array($key, $choose)) { - $props->{$key} = $value; - } - } - if (is_string($props->regex)) { - $props->regex = array($props->regex); - } - $props->about = str_replace(array(' ', "\r"), '', $props->about); - if (isset($this->_endpoint_url)) { - $props->_oembed_endpoint = $this->_endpoint_url; - } - if (isset($this->_comment)) { - $props->_comment = $this->_comment; + public function getProperties() + { + $choose = explode('|', 'regex|about|displayname|name|domain|subdomains|favicon|type'); + $props = (object)array(); + foreach (get_object_vars($this) as $key => $value) { + if (in_array($key, $choose)) { + $props->{$key} = $value; + } + } + if (is_string($props->regex)) { + $props->regex = array($props->regex); + } + $props->about = str_replace(array(' ', "\r"), '', $props->about); + if (isset($this->_endpoint_url)) { + $props->_oembed_endpoint = $this->_endpoint_url; + } + if (isset($this->_comment)) { + $props->_comment = $this->_comment; + } + return $props; } - return $props; - } - /** Get one or more example URLs. + /** Get one or more example URLs. * @return mixed Array or FALSE. */ - public function getExamples($count = 1) { - if ('public' != $this->_access || count($this->_examples) < 1) return FALSE; - - // '-1' means 'all'. - if ($count < 1) return $this->_examples; - - return array_slice($this->_examples, 0, $count); - } + public function getExamples($count = 1) + { + if ('public' != $this->_access || count($this->_examples) < 1) { + return false; + } + + // '-1' means 'all'. + if ($count < 1) { + return $this->_examples; + } + + return array_slice($this->_examples, 0, $count); + } - protected function _error($message, $code=500, $from=null, $obj=null) { - return $this->CI->_error($message, $code, $from, $obj); - } + protected function _error($message, $code = 500, $from = null, $obj = null) + { + return $this->CI->_error($message, $code, $from, $obj); + } - protected function _log($level='error', $message, $php_error=FALSE) { - return $this->CI->_log($level, $message, $php_error); - } + protected function _log($level = 'error', $message = 'unknown', $php_error = false) + { + return $this->CI->_log($level, $message, $php_error); + } - protected function _http_request_curl($url, $spoof=TRUE, $options=array()) { - $this->CI->load->library('http'); - return $this->CI->http->request($url, $spoof, $options); - } + protected function _http_request_curl($url, $spoof = true, $options = array()) + { + $this->CI->load->library('http'); + return $this->CI->http->request($url, $spoof, $options); + } - protected function _http_request_json($url, $spoof=TRUE, $options=array()) { - $result = $this->_http_request_curl($url, $spoof, $options); - if ($result->success) { - $result->json = json_decode($result->data); + protected function _http_request_json($url, $spoof = true, $options = array()) + { + $result = $this->_http_request_curl($url, $spoof, $options); + if ($result->success) { + $result->json = json_decode($result->data); + } + return $result; } - return $result; - } - protected function _safe_xml($xml) { - $safe = array('&', '>', '<', ''', '"'); - $place= array('#AMP#', '#GT#', '#LT#', '#APOS#', '#QUOT#'); - $result = str_replace($safe, $place, $xml); - $result = preg_replace('@&[^#]\w+?;@', '', $result); - $result = str_replace($place, $safe, $result); - return $result; - } + protected function _safe_xml($xml) + { + $safe = array('&', '>', '<', ''', '"'); + $place= array('#AMP#', '#GT#', '#LT#', '#APOS#', '#QUOT#'); + $result = str_replace($safe, $place, $xml); + $result = preg_replace('@&[^#]\w+?;@', '', $result); + $result = str_replace($place, $safe, $result); + return $result; + } /**Safely, recursively create directories. * Status: not working fully, on Windows. * Source: https://github.com/nfreear/moodle-filter_simplespeak/blob/master/simplespeaklib.php */ - function _mkdir_safe($base, $path, $perm=0777) { #Or 0664. - $parts = explode('/', trim($path, '/')); - $dir = $base; - $success = true; - foreach ($parts as $p) { - $dir .= "/$p"; - if (is_dir($dir)) { - break; - } elseif (file_exists($dir)) { - #error("File exists '$p'."); - } - $success = mkdir($dir, $perm); - } - return $success; - } - - /** Get the Embed.ly API key + public function _mkdir_safe($base, $path, $perm = 0777) + { + #Or 0664. + $parts = explode('/', trim($path, '/')); + $dir = $base; + $success = true; + foreach ($parts as $p) { + $dir .= "/$p"; + if (is_dir($dir)) { + break; + } elseif (file_exists($dir)) { + #error("File exists '$p'."); + } + $success = mkdir($dir, $perm); + } + return $success; + } + + /** Get the Embed.ly API key * @return string */ - protected function _embedly_api_key() { - return $this->CI->config->item('embedly_api_key'); - } + protected function _embedly_api_key() + { + return $this->CI->config->item('embedly_api_key'); + } - /** Get an Embed.ly oEmbed URL / JSON format. + /** Get an Embed.ly oEmbed URL / JSON format. * @return string */ - protected function _embedly_oembed_url($url) { - return "http://api.embed.ly/1/oembed?format=json&url=$url&key=".$this->_embedly_api_key(); - } + protected function _embedly_oembed_url($url) + { + return "http://api.embed.ly/1/oembed?format=json&url=$url&key=".$this->_embedly_api_key(); + } } - /** * Extend the base class for a generic IFRAME oEmbed provider. */ -abstract class Generic_Iframe_Oembed_Provider extends Oembed_Provider { +abstract class Generic_Iframe_Oembed_Provider extends Oembed_Provider +{ - public function getView() { - return 'oembed/_generic_iframe'; - } + public function getView() + { + return 'oembed/_generic_iframe'; + } - protected function getIframeResponse($url) { - return (object) array( + protected function getIframeResponse($url) + { + return (object) array( '_comment' => '/*TODO: work-in-progress! */', 'original_url' => $url, #'is_iframe' => TRUE, @@ -224,25 +253,24 @@ protected function getIframeResponse($url) { 'provider_url' => $this->_about_url, 'provider_icon' => $this->favicon, 'type' => $this->type, #rich - 'title'=> NULL, + 'title'=> null, 'width' => '100%', #640, #720, 'height'=> 400, #$height, - 'embed_url'=> NULL, - ); - } + 'embed_url'=> null, + ); + } } - /** * */ -abstract class External_Oembed_Provider extends Oembed_Provider { +abstract class External_Oembed_Provider extends Oembed_Provider +{ #protected $_endpoint_url; # oEmbed endpoint for 'external' providers, eg. iSpot. - public function call($url, $matches) { - $this->_error('sorry the endpoint is: '. $this->_endpoint_url, 400.9); - } + public function call($url, $matches) + { + $this->_error('sorry the endpoint is: '. $this->_endpoint_url, 400.9); + } } - - diff --git a/application/libraries/Sams_Auth.php b/application/libraries/Sams_Auth.php index 7eac972..ac38801 100644 --- a/application/libraries/Sams_Auth.php +++ b/application/libraries/Sams_Auth.php @@ -6,55 +6,61 @@ * @author N.D.Freear, 27 July 2012. */ -abstract class Privacy_Auth { +abstract class Privacy_Auth +{ - abstract public function authenticate(); + abstract public function authenticate(); - /** + /** * Determine if the caller is a private site/ client. * * Note, it is the responsibility of the caller to set a HTTP GET parameter. * Otherwise the caller is assumed to be public, with restricted-access warning being set as appropriate. */ - public function is_private_caller() { - return ('private' == $this->CI->input->get('site_access')); - } + public function is_private_caller() + { + return ('private' == $this->CI->input->get('site_access')); + } } +class Sams_Auth extends Privacy_Auth +{ -class Sams_Auth extends Privacy_Auth { - - public function __construct() { - $this->CI =& get_instance(); - } + public function __construct() + { + $this->CI =& get_instance(); + } - /** + /** * Basic OU-SAMS session cookie check and redirect - used for VLE demo/test pages. */ - public function authenticate() { - // Security: note the 'localhost' check. - if (#'localhost' != $this->CI->input->server('HTTP_HOST') && + public function authenticate() + { + // Security: note the 'localhost' check. + if (#'localhost' != $this->CI->input->server('HTTP_HOST') && !$this->CI->input->cookie('SAMSsession') - || !$this->CI->input->cookie('SAMS2session')) { - redirect($this->login_link(current_url())); + || !$this->CI->input->cookie('SAMS2session')) { + redirect($this->login_link(current_url())); + } } - } - public static function login_link($url) { - //( Redirect to: https://msds.open.ac.uk/signon/SAMSDefault/SAMS001_Default.aspx?URL= ) + public static function login_link($url) + { + //( Redirect to: https://msds.open.ac.uk/signon/SAMSDefault/SAMS001_Default.aspx?URL= ) - // Ensure abbreviated URLs like "//example.org" resolve to "http://example.org" [Bug: #1] - // IMPORTANT: urlencode() breaks the SAMS sign-on re-direction! :( - return str_replace(array( '=%2F%2F', '=//' ), '=http://', 'https://msds.open.ac.uk/signon/?URL=' . $url ); - } + // Ensure abbreviated URLs like "//example.org" resolve to "http://example.org" [Bug: #1] + // IMPORTANT: urlencode() breaks the SAMS sign-on re-direction! :( + return str_replace(array( '=%2F%2F', '=//' ), '=http://', 'https://msds.open.ac.uk/signon/?URL=' . $url); + } - /** + /** * Determine if the authenticated user is staff, including OU tutors. */ - public function is_staff() { - $sess = $this->CI->input->cookie('SAMS2session'); - return $sess && (FALSE !== strpos($sess, 'samsStaffID=') || FALSE !== strpos($sess, 'samsTutorID=')); - } + public function is_staff() + { + $sess = $this->CI->input->cookie('SAMS2session'); + return $sess && (false !== strpos($sess, 'samsStaffID=') || false !== strpos($sess, 'samsTutorID=')); + } } diff --git a/application/libraries/http.php b/application/libraries/http.php index f8b78e1..f19e536 100644 --- a/application/libraries/http.php +++ b/application/libraries/http.php @@ -1,4 +1,4 @@ -CI =& get_instance(); - } + public function __construct() + { + $this->CI =& get_instance(); + } - public function request($url, $spoof=TRUE, $options=array()) { - $result = $this->_prepare_request($url, $spoof, $options); + public function request($url, $spoof = true, $options = array()) + { + $result = $this->_prepare_request($url, $spoof, $options); - return $this->_http_request_curl($url, $spoof, $options, $result); - } + return $this->_http_request_curl($url, $spoof, $options, $result); + } - /** Prepare the HTTP request. - */ -#http://api.drupal.org/api/drupal/core%21includes%21common.inc/function/drupal_http_request/8 - protected function _prepare_request($url, $spoof, &$options) { - $result = new stdClass(); + /** Prepare the HTTP request. + */ + #http://api.drupal.org/api/drupal/core%21includes%21common.inc/function/drupal_http_request/8 + protected function _prepare_request($url, $spoof, &$options) + { + $result = new stdClass(); - // Parse the URL and make sure we can handle the schema. - $uri = @parse_url($url); + // Parse the URL and make sure we can handle the schema. + $uri = @parse_url($url); - if ($uri == FALSE) { - $result->error = 'unable to parse URL'; - $result->code = -1001; - return $result; - } + if ($uri == false) { + $result->error = 'unable to parse URL'; + $result->code = -1001; + return $result; + } - if (!isset($uri['scheme'])) { - $result->error = 'missing schema'; - $result->code = -1002; - return $result; - } + if (!isset($uri['scheme'])) { + $result->error = 'missing schema'; + $result->code = -1002; + return $result; + } - #timer_start(__FUNCTION__); + #timer_start(__FUNCTION__); - // Bug #1334, Proxy mode to fix VLE caption redirects (Timedtext controller). - $options[ 'cookie' ] = NULL; - if (isset($options['proxy_cookies'])) { - $cookie_names = $this->CI->config->item('httplib_proxy_cookies'); - if (! is_array($cookie_names)) { - $this->CI->_error('Array expected for $config[httplib_proxy_cookies]', 400); - } + // Bug #1334, Proxy mode to fix VLE caption redirects (Timedtext controller). + $options[ 'cookie' ] = null; + if (isset($options['proxy_cookies'])) { + $cookie_names = $this->CI->config->item('httplib_proxy_cookies'); + if (! is_array($cookie_names)) { + $this->CI->_error('Array expected for $config[httplib_proxy_cookies]', 400); + } - $cookies = ''; - foreach ($cookie_names as $cname) { - $cookies .= "$cname=". $this->CI->input->cookie($cname) .'; '; - } - $options['cookie'] = rtrim($cookies, '; '); - } + $cookies = ''; + foreach ($cookie_names as $cname) { + $cookies .= "$cname=". $this->CI->input->cookie($cname) .'; '; + } + $options['cookie'] = rtrim($cookies, '; '); + } - // Bug #4, Optionally add cookies for every request to a host/ domain. - $cookie_r = $this->CI->config->item( 'http_cookie' ); - if (is_array( $cookie_r )) { - foreach ($cookie_r as $domain => $cookie) { - if (FALSE !== strpos( $url, $domain )) { - $options[ 'cookie' ] .= $cookie; + // Bug #4, Optionally add cookies for every request to a host/ domain. + $cookie_r = $this->CI->config->item('http_cookie'); + if (is_array($cookie_r)) { + foreach ($cookie_r as $domain => $cookie) { + if (false !== strpos($url, $domain)) { + $options[ 'cookie' ] .= $cookie; + } + } + } + + $ua = 'OU Player/1.1.* (PHP/cURL)'; + if ($spoof) { + // Updated, April 2012. + $ua = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.142 Safari/535.19"; + #$ua="Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-GB; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3"; } - } - } - $ua = 'OU Player/1.1.* (PHP/cURL)'; - if ($spoof) { - // Updated, April 2012. - $ua = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.142 Safari/535.19"; - #$ua="Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-GB; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3"; + // Merge the default options. + $options += array( + 'proxy' => $this->CI->config->item('http_proxy'), + 'no_proxy' => $this->CI->config->item('http_no_proxy'), + 'headers' => array(), + 'method' => 'GET', + 'data' => null, + 'max_redirects' => 2, #3, + 'timeout' => 5, #15, 30.0 seconds, + #'context' => NULL, + + 'cookie' => null, + 'ua' => $ua, + 'debug' => false, + 'auth' => null, #'[domain\]user:password' + 'ssl_verify' => true, + ); + + return $result; } - // Merge the default options. - $options += array( - 'proxy' => $this->CI->config->item('http_proxy'), - 'no_proxy' => $this->CI->config->item( 'http_no_proxy' ), - 'headers' => array(), - 'method' => 'GET', - 'data' => NULL, - 'max_redirects' => 2, #3, - 'timeout' => 5, #15, 30.0 seconds, - #'context' => NULL, - - 'cookie' => NULL, - 'ua' => $ua, - 'debug' => FALSE, - 'auth' => NULL, #'[domain\]user:password' - 'ssl_verify' => TRUE, - ); - - return $result; - } - - - /** Perform the HTTP request using cURL. + + /** Perform the HTTP request using cURL. */ - protected function _http_request_curl($url, $spoof, $options, $result) { - if (!function_exists('curl_init')) die('Error, cURL is required.'); + protected function _http_request_curl($url, $spoof, $options, $result) + { + if (!function_exists('curl_init')) { + die('Error, cURL is required.'); + } - $this->CI->_debug($options); + $this->CI->_debug($options); - $h_curl = curl_init($url); + $h_curl = curl_init($url); - curl_setopt($h_curl, CURLOPT_USERAGENT, $options['ua']); - if (!$spoof) { - curl_setopt($h_curl, CURLOPT_REFERER, base_url()); - } + curl_setopt($h_curl, CURLOPT_USERAGENT, $options['ua']); + if (!$spoof) { + curl_setopt($h_curl, CURLOPT_REFERER, base_url()); + } - // Required for intranet-restricted content [Bug: #1] - if ($options['cookie']) { - curl_setopt($h_curl, CURLOPT_COOKIE, $options['cookie']); - header('X-Proxy-Cookie: '.$options['cookie']); - } + // Required for intranet-restricted content [Bug: #1] + if ($options['cookie']) { + curl_setopt($h_curl, CURLOPT_COOKIE, $options['cookie']); + header('X-Proxy-Cookie: '.$options['cookie']); + } - if (! $options['ssl_verify']) { - curl_setopt($h_curl, CURLOPT_SSL_VERIFYPEER, FALSE); - curl_setopt($h_curl, CURLOPT_SSL_VERIFYHOST, FALSE); - } + if (! $options['ssl_verify']) { + curl_setopt($h_curl, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($h_curl, CURLOPT_SSL_VERIFYHOST, false); + } - curl_setopt($h_curl, CURLOPT_AUTOREFERER, TRUE); - curl_setopt($h_curl, CURLOPT_MAXREDIRS, $options['max_redirects']); - curl_setopt($h_curl, CURLOPT_FOLLOWLOCATION, TRUE); - curl_setopt($h_curl, CURLOPT_TIMEOUT, $options['timeout']); - curl_setopt($h_curl, CURLOPT_CONNECTTIMEOUT, $options['timeout']); + curl_setopt($h_curl, CURLOPT_AUTOREFERER, true); + curl_setopt($h_curl, CURLOPT_MAXREDIRS, $options['max_redirects']); + curl_setopt($h_curl, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($h_curl, CURLOPT_TIMEOUT, $options['timeout']); + curl_setopt($h_curl, CURLOPT_CONNECTTIMEOUT, $options['timeout']); - if ($options['debug']) { - curl_setopt($h_curl, CURLOPT_HEADER, TRUE); - curl_setopt($h_curl, CURLINFO_HEADER_OUT, TRUE); - } + if ($options['debug']) { + curl_setopt($h_curl, CURLOPT_HEADER, true); + curl_setopt($h_curl, CURLINFO_HEADER_OUT, true); + } - if ($options['auth']) { - //TODO: http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/ - curl_setopt($h_curl, CURLOPT_SSL_VERIFYPEER, FALSE); - curl_setopt($h_curl, CURLOPT_SSL_VERIFYHOST, FALSE); + if ($options['auth']) { + //TODO: http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/ + curl_setopt($h_curl, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($h_curl, CURLOPT_SSL_VERIFYHOST, false); - curl_setopt($h_curl, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); - curl_setopt($h_curl, CURLOPT_USERPWD, $options['auth']); - } + curl_setopt($h_curl, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); + curl_setopt($h_curl, CURLOPT_USERPWD, $options['auth']); + } - if ($options[ 'proxy' ]) { - curl_setopt($h_curl, CURLOPT_PROXY, $options[ 'proxy' ]); - curl_setopt($h_curl, CURLOPT_NOPROXY, $options[ 'no_proxy' ]); - } - curl_setopt($h_curl, CURLOPT_RETURNTRANSFER, TRUE); + if ($options[ 'proxy' ]) { + curl_setopt($h_curl, CURLOPT_PROXY, $options[ 'proxy' ]); + curl_setopt($h_curl, CURLOPT_NOPROXY, $options[ 'no_proxy' ]); + } + curl_setopt($h_curl, CURLOPT_RETURNTRANSFER, true); - $result->data = curl_exec($h_curl); - $result->http_code = FALSE; + $result->data = curl_exec($h_curl); + $result->http_code = false; - $result->_headers = NULL; - // Fragile: rely on cURL always putting 'Content-Length' last.. - if ($options['debug'] && preg_match('#^(HTTP\/1\..+Content\-Length: \d+\s)(.*)$#ms', $result->data, $matches)) { - $result->_headers = $matches[1]; - $result->data = trim($matches[2], "\r\n"); - } - if ($errno = curl_errno($h_curl)) { - //Error. Quietly log? - $this->CI->_log('error', "cURL $errno, ".curl_error($h_curl)." GET $url"); - #$this->CI->firephp->fb("cURL $errno", "cURL error", "ERROR"); - $result->http_code = "500." . $errno; - $result->curl_errno = $errno; - $result->curl_error = curl_error($h_curl); - $result->success = FALSE; - } - $result->info = curl_getinfo($h_curl); - if (!$result->http_code) { - $result->success = ($result->info['http_code'] < 300); - $result->http_code = $result->info['http_code']; - } + $result->_headers = null; + // Fragile: rely on cURL always putting 'Content-Length' last.. + if ($options['debug'] && preg_match('#^(HTTP\/1\..+Content\-Length: \d+\s)(.*)$#ms', $result->data, $matches)) { + $result->_headers = $matches[1]; + $result->data = trim($matches[2], "\r\n"); + } + if ($errno = curl_errno($h_curl)) { + //Error. Quietly log? + $this->CI->_log('error', "cURL $errno, ".curl_error($h_curl)." GET $url"); + #$this->CI->firephp->fb("cURL $errno", "cURL error", "ERROR"); + $result->http_code = "500." . $errno; + $result->curl_errno = $errno; + $result->curl_error = curl_error($h_curl); + $result->success = false; + } + $result->info = curl_getinfo($h_curl); + if (!$result->http_code) { + $result->success = ($result->info['http_code'] < 300); + $result->http_code = $result->info['http_code']; + } - curl_close($h_curl); + curl_close($h_curl); - return (object) $result; - } - -} \ No newline at end of file + return (object) $result; + } +} diff --git a/application/libraries/ouplayer_lib.php b/application/libraries/ouplayer_lib.php index 5be3229..68d6f7d 100644 --- a/application/libraries/ouplayer_lib.php +++ b/application/libraries/ouplayer_lib.php @@ -1,4 +1,4 @@ - 848, 'height' => 480, 'label' => 'x-large'), array('width' => 640, 'height' => 360, 'label' => 'large'), array('width' => 560, 'height' => 315, 'label' => 'medium'), //'default'? array('width' => 480, 'height' => 270, 'label' => 'small'), array('width' => 295, 'height' => 166, 'label' => 'x-small'), - ); + ); // Hmm, public? - public $title; - public $media_url; - public $media_type; #audio|video (not MIME!) - public $media_html5 = TRUE; - public $codec; #? - public $width = self::DEF_WIDTH; - public $height= self::DEF_HEIGHT; - public $object_height = self::DEF_HEIGHT; - public $size_label = 'default'; - public $duration; - public $language = 'en'; + public $title; + public $media_url; + public $media_type; #audio|video (not MIME!) + public $media_html5 = true; + public $codec; #? + public $width = self::DEF_WIDTH; + public $height= self::DEF_HEIGHT; + public $object_height = self::DEF_HEIGHT; + public $size_label = 'default'; + public $duration; + public $language = 'en'; #public $is_vle = FALSE; - public $poster_url; #poster/ thumbnail? - public $transcript_url; - public $caption_url; - public $iframe_url; #Our