PageRenderTime 37ms CodeModel.GetById 5ms RepoModel.GetById 1ms app.codeStats 0ms

/application/core/MY_Controller.php

https://bitbucket.org/nfreear/trackoer-core
PHP | 184 lines | 92 code | 35 blank | 57 comment | 10 complexity | 81ff7ac0a7ec1101504acafcd370701a MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Track OER
  4. *
  5. * A web application to facilitate analytics for Open Educational Resources.
  6. *
  7. * @package trackoer-core
  8. * @copyright Copyright 2012 The Open University.
  9. * @author N.D.Freear, 9 August 2012.
  10. * @license [free/open source -- license to be decided]
  11. *
  12. * @link https://github.com/IET-OU/trackoer-core
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. /**
  17. * Based on,
  18. * @link https://github.com/IET-OU/ouplayer/blob/master/application/core/MY_Controller.php
  19. * @link https://bitbucket.org/cloudengine/cloudengine/src/tip/system/application/libraries/MY_Controller.php
  20. */
  21. class MY_Controller extends CI_Controller {
  22. // Default layout/template.
  23. const LAYOUT = 'ci'; #'bare';
  24. protected $status = array();
  25. protected $_request = array();
  26. public function __construct() {
  27. parent::__construct();
  28. $this->_prepare();
  29. // Enable Cross-Origin Resource Sharing (CORS), http://enable-cors.org | http://w3.org/TR/cors
  30. @header('Access-Control-Allow-Origin: *');
  31. @header('Content-Type: text/html; charset=UTF-8');
  32. if (ini_get('expose_php')) {
  33. // 'ofa' - OU flavoured Apache..?
  34. @header('X-Powered-By: iet-ou');
  35. }
  36. log_message('debug', __CLASS__." Class Initialized");
  37. }
  38. /** Initialize the application, including the request array.
  39. */
  40. protected function _prepare() {
  41. $this->_request = array(
  42. 'locale' => $this->input->get_default('locale', 'en'),
  43. 'format' => $this->input->get_default('format', 'html'),
  44. );
  45. }
  46. /**
  47. * Get a request variable, or the whole array.
  48. * @return mixed
  49. */
  50. public function request($key = NULL) {
  51. if (! $key) {
  52. return $this->_request;
  53. }
  54. return isset($this->_request[$key]) ? $this->_request[$key] : FALSE;
  55. }
  56. /** Load the layout library with a 'bare' or OUICE template.
  57. */
  58. protected function _load_layout($layout = self::LAYOUT) {
  59. $layout = 'ci'==$layout ? 'ci' : 'ouice_2';
  60. $this->load->library('Layout', array('layout'=>"site_layout/layout_$layout"));
  61. $this->load->helper('url');
  62. }
  63. protected function oembedUrl($url, $service = 'oembed', $format = NULL) {
  64. return site_url($service) .'?url='. urlencode($url);
  65. }
  66. /** Add a message to the status queue.
  67. */
  68. public function _addStatus($message) {
  69. $this->status[] = $message;
  70. }
  71. /** Get the status-message array.
  72. */
  73. protected function _getStatus() {
  74. return $this->status;
  75. }
  76. public function _is_debug($threshold = 0) {
  77. $is_debug = 0;
  78. $is_debug += (int) $this->input->get('debug');
  79. $is_debug += (int) $this->config->item('debug');
  80. #var_dump(__FUNCTION__, $is_debug, $threshold);
  81. return $is_debug > $threshold;
  82. }
  83. /**
  84. * Based on @link https://gist.github.com/1712707
  85. */
  86. public function _debug($exp) {
  87. static $where, $count = 0;
  88. if ($this->_is_debug()) {
  89. # $where could be based on __FUNCTION__ or debug_stacktrace().
  90. if(!$where) $where = str_replace(array('_', '.'), '-', basename(__FILE__));
  91. @header("X-D-$where-".sprintf('%02d', $count).': '.json_encode($exp));
  92. foreach (func_get_args() as $c => $arg) {
  93. if($c > 0) $this->_debug($arg); #Recurse.
  94. }
  95. $count++;
  96. }
  97. }
  98. /** Handle fatal errors.
  99. */
  100. public function _error($message, $code=500, $from=null, $obj=null) { #Was: protected.
  101. if ($this->input->is_cli_request()) {
  102. echo "Error, $message, $code, $from".PHP_EOL;
  103. #return;
  104. exit (2);
  105. }
  106. /*NDF: needs work!
  107. elseif ((integer) $code == 400 && 'Oerform' == get_class($this)) {
  108. if (preg_match('/\{(.+)\}/', $message, $matches)) {
  109. $this->form_validation->set_message($matches[1], $message);
  110. return;
  111. }
  112. }*/
  113. #$this->firephp->fb("$code: $message", $from, 'ERROR');
  114. $this->_log('error', "$from: $code, $message");
  115. #@header('HTTP/1.1 '. (integer) $code);
  116. $ex =& load_class('Exceptions', 'core');
  117. echo $ex->show_error('Track OER error', "$message ($code)", 'error_general', (integer) $code);
  118. exit;
  119. }
  120. public function _log($level='error', $message, $php_error=FALSE) {
  121. $_CI = $this;
  122. $_CI->load->library('user_agent');
  123. $ip = $_CI->input->server('REMOTE_ADDR');
  124. $ref= $_CI->agent->referrer();
  125. $ua = $_CI->agent->agent_string();
  126. $request = $_CI->uri->uri_string().'?'.$_CI->input->server('QUERY_STRING');
  127. $msg = "$message, $request -- $ip, $ref, $ua";
  128. log_message($level, $msg); #, $php_error);
  129. $fp_level = 'error'==$level ? 'ERROR' : 'INFO';
  130. $fp_label = 'error'==$level ? 'Error log' : 'Log';
  131. #$this->firephp->fb($msg, $fp_label, $fp_level);
  132. }
  133. }
  134. /* Placeholder for translate text function.
  135. * See: cloudengine/libs./MY_Language; Drupal.
  136. * @link https://github.com/IET-OU/ouplayer/blob/master/application/core/MY_Lang.php
  137. */
  138. if (!function_exists('t')) {
  139. function t($s, $args=null) {
  140. if (is_array($args)) {
  141. $s = vsprintf($s, $args);
  142. }
  143. // Important: accept empty string!
  144. elseif ($args || ''==$args) {
  145. $s = sprintf($s, $args);
  146. }
  147. return $s;
  148. }
  149. }