Open Journal Systems  2.4.3
 All Classes Namespaces Functions Variables Groups Pages
Core.inc.php
1 <?php
2 
21 define('USER_AGENTS_FILE', Core::getBaseDir() . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'pkp' . DIRECTORY_SEPARATOR . 'registry' . DIRECTORY_SEPARATOR . 'botAgents.txt');
22 
23 class Core {
24 
26  static $botRegexps = array();
27 
32  function getBaseDir() {
33  static $baseDir;
34 
35  if (!isset($baseDir)) {
36  // Need to change if the index file moves
37  $baseDir = dirname(INDEX_FILE_LOCATION);
38  }
39 
40  return $baseDir;
41  }
42 
49  function cleanVar($var) {
50  // only normalize strings that are not UTF-8 already, and when the system is using UTF-8
51  if ( Config::getVar('i18n', 'charset_normalization') == 'On' && strtolower_codesafe(Config::getVar('i18n', 'client_charset')) == 'utf-8' && !String::utf8_is_valid($var) ) {
52 
53  $var = String::utf8_normalize($var);
54 
55  // convert HTML entities into valid UTF-8 characters (do not transcode)
56  if (checkPhpVersion('5.0.0')) {
57  $var = html_entity_decode($var, ENT_COMPAT, 'UTF-8');
58  } else {
59  $var = String::html2utf($var);
60  }
61 
62  // strip any invalid UTF-8 sequences
63  $var = String::utf8_bad_strip($var);
64 
65  // re-encode special HTML characters
66  if (checkPhpVersion('5.2.3')) {
67  $var = htmlspecialchars($var, ENT_NOQUOTES, 'UTF-8', false);
68  } else {
69  $var = htmlspecialchars($var, ENT_NOQUOTES, 'UTF-8');
70  }
71  }
72 
73  // strip any invalid ASCII control characters
74  $var = String::utf8_strip_ascii_ctrl($var);
75 
76  return trim($var);
77  }
78 
85  function cleanFileVar($var) {
86  return String::regexp_replace('/[^\w\-]/', '', $var);
87  }
88 
94  function getCurrentDate($ts = null) {
95  return date('Y-m-d H:i:s', isset($ts) ? $ts : time());
96  }
97 
102  function microtime() {
103  list($usec, $sec) = explode(' ', microtime());
104  return (float)$sec + (float)$usec;
105  }
106 
111  function serverPHPOS() {
112  return PHP_OS;
113  }
114 
119  function serverPHPVersion() {
120  return phpversion();
121  }
122 
127  function isWindows() {
128  return strtolower_codesafe(substr(Core::serverPHPOS(), 0, 3)) == 'win';
129  }
130 
138  function isUserAgentBot($userAgent, $botRegexpsFile = USER_AGENTS_FILE) {
139  static $botRegexps;
140 
141  if (!isset($botRegexps[$botRegexpsFile])) {
142  $cacheManager =& CacheManager::getManager();
143  $cache =& $cacheManager->getCache('core', $botRegexpsFile, array('Core', '_botFileListCacheMiss'), CACHE_TYPE_FILE);
144  $botRegexps[$botRegexpsFile] = $cache->getContents($botRegexpsFile);
145  }
146 
147  foreach ($botRegexps[$botRegexpsFile] as $regexp) {
148  if (String::regexp_match($regexp, $userAgent)) {
149  return true;
150  }
151  }
152 
153  return false;
154  }
155 
168  function getContextPaths($urlInfo, $isPathInfo, $contextList = null, $contextDepth = null, $userVars = array()) {
169  $contextPaths = array();
170  $application =& Application::getApplication();
171 
172  if (!$contextList) {
173  $contextList = $application->getContextList();
174  }
175  if (!$contextDepth) {
176  $contextDepth = $application->getContextDepth();
177  }
178 
179  // Handle context depth 0
180  if (!$contextDepth) return $contextPaths;
181 
182  if ($isPathInfo) {
183  // Split the path info into its constituents. Save all non-context
184  // path info in $contextPaths[$contextDepth]
185  // by limiting the explode statement.
186  $contextPaths = explode('/', trim($urlInfo, '/'), $contextDepth + 1);
187  // Remove the part of the path info that is not relevant for context (if present)
188  unset($contextPaths[$contextDepth]);
189  } else {
190  // Retrieve context from url query string
191  foreach($contextList as $key => $contextName) {
192  $contextPaths[$key] = Core::_getUserVar($urlInfo, $contextName, $userVars);
193  }
194  }
195 
196  // Canonicalize and clean context paths
197  for($key = 0; $key < $contextDepth; $key++) {
198  $contextPaths[$key] = (
199  isset($contextPaths[$key]) && !empty($contextPaths[$key]) ?
200  $contextPaths[$key] : 'index'
201  );
202  $contextPaths[$key] = Core::cleanFileVar($contextPaths[$key]);
203  }
204 
205  return $contextPaths;
206  }
207 
219  function getPage($urlInfo, $isPathInfo, $userVars = array()) {
220  $page = Core::_getUrlComponents($urlInfo, $isPathInfo, 0, 'page', $userVars);
221  return Core::cleanFileVar(is_null($page) ? '' : $page);
222  }
223 
235  function getOp($urlInfo, $isPathInfo, $userVars = array()) {
236  $operation = Core::_getUrlComponents($urlInfo, $isPathInfo, 1, 'op', $userVars);
237  return Core::cleanFileVar(empty($operation) ? 'index' : $operation);
238  }
239 
252  function getArgs($urlInfo, $isPathInfo, $userVars = array()) {
253  return Core::_getUrlComponents($urlInfo, $isPathInfo, 2, 'path', $userVars);
254  }
255 
261  function _botFileListCacheMiss(&$cache) {
262  $id = $cache->getCacheId();
263  $botRegexps = array_filter(file($id),
264  array('Core', '_filterBotRegexps'));
265 
266  $cache->setEntireCache($botRegexps);
267  return $botRegexps;
268  }
269 
275  function _filterBotRegexps(&$regexp) {
276  $delimiter = '/';
277  $regexp = trim($regexp);
278  if (!empty($regexp) && $regexp[0] != '#') {
279  if(strpos($regexp, $delimiter) !== 0) {
280  // Make sure delimiters are in place.
281  $regexp = $delimiter . $regexp . $delimiter;
282  }
283  return true;
284  } else {
285  return false;
286  }
287  }
288 
296  function _getUserVar($url, $varName, $userVars = array()) {
297  $returner = null;
298  parse_str(parse_url($url, PHP_URL_QUERY), $userVarsFromUrl);
299  if (isset($userVarsFromUrl[$varName])) $returner = $userVarsFromUrl[$varName];
300 
301  if (is_null($returner)) {
302  // Try to retrieve from passed user vars, if any.
303  if (!empty($userVars) && isset($userVars[$varName])) {
304  $returner = $userVars[$varName];
305  }
306  }
307 
308  return $returner;
309  }
310 
322  function _getUrlComponents($urlInfo, $isPathInfo, $offset, $varName = '', $userVars = array()) {
323  $component = null;
324 
325  $isArrayComponent = false;
326  if ($varName == 'path') {
327  $isArrayComponent = true;
328  }
329  if ($isPathInfo) {
330  $application = Application::getApplication();
331  $contextDepth = $application->getContextDepth();
332 
333  $vars = explode('/', trim($urlInfo, '/'));
334  if (count($vars) > $contextDepth + $offset) {
335  if ($isArrayComponent) {
336  $component = array_slice($vars, $contextDepth + $offset);
337  for ($i=0, $count=count($component); $i<$count; $i++) {
338  $component[$i] = Core::cleanVar(get_magic_quotes_gpc() ? stripslashes($component[$i]) : $component[$i]);
339  }
340  } else {
341  $component = $vars[$contextDepth + $offset];
342  }
343  }
344  } else {
345  $component = Core::_getUserVar($urlInfo, $varName, $userVars);
346  }
347 
348  if ($isArrayComponent) {
349  if (empty($component)) $component = array();
350  elseif (!is_array($component)) $component = array($component);
351  }
352 
353  return $component;
354  }
355 }
356 
357 ?>
serverPHPOS()
Definition: Core.inc.php:111
regexp_match($pattern, $subject)
Definition: String.inc.php:325
html2utf($str)
Definition: String.inc.php:691
serverPHPVersion()
Definition: Core.inc.php:119
getOp($urlInfo, $isPathInfo, $userVars=array())
Definition: Core.inc.php:235
getContextPaths($urlInfo, $isPathInfo, $contextList=null, $contextDepth=null, $userVars=array())
Definition: Core.inc.php:168
utf8_normalize($str)
Definition: String.inc.php:582
_botFileListCacheMiss(&$cache)
Definition: Core.inc.php:261
Class containing system-wide functions.
Definition: Core.inc.php:23
getCurrentDate($ts=null)
Definition: Core.inc.php:94
_getUrlComponents($urlInfo, $isPathInfo, $offset, $varName= '', $userVars=array())
Definition: Core.inc.php:322
microtime()
Definition: Core.inc.php:102
isUserAgentBot($userAgent, $botRegexpsFile=USER_AGENTS_FILE)
Definition: Core.inc.php:138
regexp_replace($pattern, $replacement, $subject, $limit=-1)
Definition: String.inc.php:350
_filterBotRegexps(&$regexp)
Definition: Core.inc.php:275
utf8_is_valid($str)
Definition: String.inc.php:520
getVar($section, $key, $default=null)
Definition: Config.inc.php:34
utf8_strip_ascii_ctrl($str)
Definition: String.inc.php:572
cleanFileVar($var)
Definition: Core.inc.php:85
getBaseDir()
Definition: Core.inc.php:32
utf8_bad_strip($str)
Definition: String.inc.php:551
cleanVar($var)
Definition: Core.inc.php:49
getArgs($urlInfo, $isPathInfo, $userVars=array())
Definition: Core.inc.php:252
isWindows()
Definition: Core.inc.php:127
_getUserVar($url, $varName, $userVars=array())
Definition: Core.inc.php:296
getPage($urlInfo, $isPathInfo, $userVars=array())
Definition: Core.inc.php:219