22 define(
'PKP_LIB_PATH',
'lib' . DIRECTORY_SEPARATOR .
'pkp');
23 define(
'COUNTER_USER_AGENTS_FILE',
Core::getBaseDir() . DIRECTORY_SEPARATOR . PKP_LIB_PATH . DIRECTORY_SEPARATOR .
'lib' . DIRECTORY_SEPARATOR .
'counterBots' . DIRECTORY_SEPARATOR .
'generated' . DIRECTORY_SEPARATOR .
'COUNTER_Robots_list.txt');
37 if (!isset($baseDir)) {
61 return date(
'Y-m-d H:i:s', isset($ts) ? $ts : time());
69 list($usec, $sec) = explode(
' ',
microtime());
70 return (
float)$sec + (float)$usec;
87 if (extension_loaded($moduleName)) {
100 static function isUserAgentBot($userAgent, $botRegexpsFile = COUNTER_USER_AGENTS_FILE) {
105 $botFileCacheId = md5($botRegexpsFile);
107 $cache = $cacheManager->getCache(
'core', $botFileCacheId, array(
'Core',
'_botFileListCacheMiss'),
CACHE_TYPE_FILE);
108 $botRegexps[$botRegexpsFile] = $cache->getContents();
111 foreach (
$botRegexps[$botRegexpsFile] as $regexp) {
134 static function getContextPaths($urlInfo, $isPathInfo, $contextList =
null, $contextDepth =
null, $userVars = array()) {
135 $contextPaths = array();
141 if (!$contextDepth) {
146 if (!$contextDepth)
return $contextPaths;
152 $contextPaths = explode(
'/', trim($urlInfo,
'/'), $contextDepth + 1);
154 unset($contextPaths[$contextDepth]);
157 foreach($contextList as $key => $contextName) {
158 $contextPaths[$key] = Core::_getUserVar($urlInfo, $contextName, $userVars);
163 for($key = 0; $key < $contextDepth; $key++) {
164 $contextPaths[$key] = (
165 isset($contextPaths[$key]) && !empty($contextPaths[$key]) ?
166 $contextPaths[$key] :
'index'
171 return $contextPaths;
185 static function getPage($urlInfo, $isPathInfo, $userVars = array()) {
186 $page = Core::_getUrlComponents($urlInfo, $isPathInfo, 0,
'page', $userVars);
201 static function getOp($urlInfo, $isPathInfo, $userVars = array()) {
202 $operation = Core::_getUrlComponents($urlInfo, $isPathInfo, 1,
'op', $userVars);
218 static function getArgs($urlInfo, $isPathInfo, $userVars = array()) {
219 return Core::_getUrlComponents($urlInfo, $isPathInfo, 2,
'path', $userVars);
233 if (!$baseUrl)
return false;
236 $url = str_replace($baseUrl,
'', $url);
240 $baseUrlPath = parse_url($baseUrl, PHP_URL_PATH);
241 if ($baseUrlPath == $url) {
248 if (strpos($url, $baseUrlPath .
'?') === 0) {
250 $baseSystemEscapedPath = preg_quote($baseUrlPath .
'?',
'/');
253 $baseSystemEscapedPath = preg_quote($baseUrlPath .
'/',
'/');
255 $url = preg_replace(
'/^' . $baseSystemEscapedPath .
'/', $replacement, $url);
258 $url = str_replace(
'/index.php',
'', $url);
265 if (strpos($url,
'/' . $contextPath .
'/') !== 0) {
266 $url =
'/' . $contextPath . $url;
271 $url = rtrim($url,
'/');
285 $contextPath =
false;
290 if (empty($contextBaseUrls)) {
294 if (isset($contextBaseUrls[
'index'])) {
295 unset($contextBaseUrls[
'index']);
303 $sortedBaseUrls = array_combine($contextBaseUrls, array_map(
'strlen', $contextBaseUrls));
304 arsort($sortedBaseUrls);
306 foreach (array_keys($sortedBaseUrls) as $workingBaseUrl) {
307 $urlHost = parse_url($url, PHP_URL_HOST);
308 if (is_null($urlHost)) {
310 $baseUrlHost = parse_url($workingBaseUrl, PHP_URL_HOST);
311 if (is_null($baseUrlHost))
break;
312 $baseUrlToSearch = substr($workingBaseUrl, strpos($workingBaseUrl, $baseUrlHost) + strlen($baseUrlHost));
315 if (!$baseUrlToSearch) $baseUrlToSearch =
'/';
317 $baseUrlToSearch = $workingBaseUrl;
321 if (is_null($baseUrlCheck)) {
324 }
else if ($baseUrlCheck ===
true) {
325 $contextPath = array_search($workingBaseUrl, $contextBaseUrls);
326 $baseUrl = $workingBaseUrl;
332 return array($baseUrl, $contextPath);
350 $baseUrlHasHost = (boolean) parse_url($baseUrl, PHP_URL_HOST);
351 $urlHasHost = (boolean) parse_url($url, PHP_URL_HOST);
352 if ($baseUrlHasHost !== $urlHasHost)
return false;
358 if (strpos($url, $baseUrl) === 0) {
359 if (strpos($url,
'/index.php') == strlen($baseUrl) - 1) {
368 foreach ($contextBaseUrls as $contextPath => $workingBaseUrl) {
369 $urlToCheck = $workingBaseUrl .
'/' . $contextPath;
370 if (!$baseUrlHasHost) $urlToCheck = parse_url($urlToCheck, PHP_URL_PATH);
371 if ($baseUrl == $urlToCheck) {
389 $id = $cache->getCacheId();
390 $filteredBotRegexps = array_filter(file(
Registry::get(
'currentUserAgentsFile')),
392 $regexp = trim($regexp);
393 return !empty($regexp) && $regexp[0] !=
'#';
398 $regexp = trim($regexp);
399 if(strpos($regexp, $delimiter) !== 0) {
401 $regexp = $delimiter . $regexp . $delimiter;
418 private static function _getUserVar($url, $varName, $userVars = array()) {
420 parse_str(parse_url($url, PHP_URL_QUERY), $userVarsFromUrl);
421 if (isset($userVarsFromUrl[$varName])) $returner = $userVarsFromUrl[$varName];
423 if (is_null($returner)) {
425 if (!empty($userVars) && isset($userVars[$varName])) {
426 $returner = $userVars[$varName];
444 private static function _getUrlComponents($urlInfo, $isPathInfo, $offset, $varName =
'', $userVars = array()) {
447 $isArrayComponent =
false;
448 if ($varName ==
'path') {
449 $isArrayComponent =
true;
455 $vars = explode(
'/', trim($urlInfo,
'/'));
456 if (
count($vars) > $contextDepth + $offset) {
457 if ($isArrayComponent) {
458 $component = array_slice($vars, $contextDepth + $offset);
460 $component = $vars[$contextDepth + $offset];
464 $component = Core::_getUserVar($urlInfo, $varName, $userVars);
467 if ($isArrayComponent) {
468 if (empty($component)) $component = array();
469 elseif (!is_array($component)) $component = array($component);