PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/FSN/mediatheque/zp-core/rewrite.php

https://gitlab.com/r.collas/site_central
PHP | 137 lines | 93 code | 8 blank | 36 comment | 20 complexity | 53b12839949bd4050e27b87604c5e7c9 MD5 | raw file
  1. <?php
  2. /**
  3. * "Rewrite" handling for Zenphoto
  4. *
  5. * The basic rules are found in the zenphoto-rewrite.txt file. Additional rules can be provided by plugins. But
  6. * for the plugin to load in time for the rules to be seen it must be either a CLASS_PLUGIN or a FEATURE_PLUGIN.
  7. * Plugins add rules by inserting them into the $_zp_conf_vars['special_pages'] array. Each "rule" is an array
  8. * of three elements: <var>define</var>, <var>rewrite</var>, and (optionally) <var>rule</rule>.
  9. *
  10. * Elemments which have a <var>define</var> and no <var>rule</rule> are processed by rewrite rules in the
  11. * zenphoto-rewrite.txt file and the <var>define</var> is used internally to Zenphoto to reference
  12. * the rewrite text when building links.
  13. *
  14. * Elements with a <var>rule</rule> defined are processed after Search, Pages, and News rewrite rules and before
  15. * Image and album rewrite rules. The tag %REWRITE% in the rule is replaced with the <var>rewrite</var> text
  16. * before processing the rule. Thus <var>rewrite</var> is the token that should appear in the acutal URL.
  17. *
  18. * It makes no sense to have an element without either a <var>define</var> or a <var>rule</rule> as nothing will happen.
  19. *
  20. * At present all rules are presumed to to stop processing the rule set. Historically that is what all our rules have done, but I suppose
  21. * we could change that. The "R" flag may be used to cause a <var>header</var> status to be sent. However, we do not redirect
  22. * back to index.php, so the "R" flag is only useful if the target is a different script.
  23. *
  24. * @package admin
  25. */
  26. function rewriteHandler() {
  27. global $_zp_conf_vars, $_zp_rewritten;
  28. $_zp_rewritten = false;
  29. $definitions = array();
  30. // query parameters should already be loaded into the $_GET and $_REQUEST arrays, so we discard them here
  31. $request = explode('?', getRequestURI());
  32. //rewrite base
  33. $requesturi = ltrim(substr($request[0], strlen(WEBPATH)), '/');
  34. list($definitions, $rules) = getRules();
  35. //process the rules
  36. foreach ($rules as $rule) {
  37. if ($rule = trim($rule)) {
  38. if ($rule{0} != '#') {
  39. if (preg_match('~^rewriterule~i', $rule)) {
  40. // it is a rewrite rule, see if it is applicable
  41. $rule = strtr($rule, $definitions);
  42. preg_match('~^rewriterule\s+(.*?)\s+(.*?)\s*\[(.*)\]$~i', $rule, $matches);
  43. if (array_key_exists(1, $matches)) {
  44. if (preg_match('~' . $matches[1] . '~', $requesturi, $subs)) {
  45. $params = array();
  46. // setup the rule replacement values
  47. foreach ($subs as $key => $sub) {
  48. $params['$' . $key] = urlencode($sub); // parse_str is going to decode the string!
  49. }
  50. // parse rewrite rule flags
  51. $flags = array();
  52. $banner = explode(',', strtoupper($matches[3]));
  53. foreach ($banner as $flag) {
  54. $flag = strtoupper(trim($flag));
  55. $f = explode('=', $flag);
  56. $flags[trim($f[0])] = isset($f[1]) ? trim($f[1]) : NULL;
  57. }
  58. if (!array_key_exists('QSA', $flags)) {
  59. // QSA means merge the query parameters. Otherwise we clear them
  60. $_REQUEST = array_diff($_REQUEST, $_GET);
  61. $_GET = array();
  62. }
  63. preg_match('~(.*?)\?(.*)~', $matches[2], $action);
  64. if (empty($action)) {
  65. $action[1] = $matches[2];
  66. }
  67. if (array_key_exists(2, $action)) {
  68. // process the rules replacements
  69. $query = strtr($action[2], $params);
  70. parse_str($query, $gets);
  71. $_GET = array_merge($_GET, $gets);
  72. $_REQUEST = array_merge($_REQUEST, $gets);
  73. }
  74. // we will execute the index.php script in due course. But if the rule
  75. // action takes us elsewhere we will have to re-direct to that script.
  76. if (isset($action[1]) && $action[1] != 'index.php') {
  77. $qs = http_build_query($_GET);
  78. if ($qs) {
  79. $qs = '?' . $qs;
  80. }
  81. if (array_key_exists('R', $flags)) {
  82. header('Status: ' . $flags['R']);
  83. }
  84. header('Location: ' . WEBPATH . '/' . $action[1] . $qs);
  85. exit();
  86. }
  87. $_zp_rewritten = true;
  88. break;
  89. }
  90. } else {
  91. zp_error(sprintf(gettext('Error processing rewrite rule: ā€œ%sā€'), trim(preg_replace('~^rewriterule~i', '', $rule))), E_USER_WARNING);
  92. }
  93. } else {
  94. if (preg_match('~define\s+(.*?)\s*\=\>\s*(.*)$~i', $rule, $matches)) {
  95. // store definitions
  96. eval('$definitions[$matches[1]] = ' . $matches[2] . ';');
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. function getRules() {
  104. global $_zp_conf_vars;
  105. // load rewrite rules
  106. $rules = trim(file_get_contents(SERVERPATH . '/' . ZENFOLDER . '/zenphoto-rewrite.txt'));
  107. $definitions = $specialPageRules = array();
  108. foreach ($_zp_conf_vars['special_pages'] as $special) {
  109. if (array_key_exists('rule', $special)) {
  110. $specialPageRules[] = "\tRewriteRule " . str_replace('%REWRITE%', $special['rewrite'], $special['rule']);
  111. }
  112. if (array_key_exists('definition', $special)) {
  113. eval('$definitions[$special[\'definition\']] = ' . $special['rewrite'] . ';');
  114. }
  115. }
  116. $rules = explode("_SPECIAL_", trim($rules));
  117. $rules = array_merge(explode("\n", $rules[0]), $specialPageRules, explode("\n", $rules[1]), array("\t#### Catch-all", "\t" . 'RewriteRule ^(.*)/?$ index.php?album=$1 [L,QSA]'));
  118. return array($definitions, $rules);
  119. }
  120. $_definitions = array();
  121. foreach ($_zp_conf_vars['special_pages'] as $definition) {
  122. if (@$definition['define']) {
  123. define($definition['define'], strtr($definition['rewrite'], $_definitions));
  124. eval('$_definitions[$definition[\'define\']]=' . $definition['define'] . ';');
  125. }
  126. }
  127. unset($definition);
  128. unset($_definitions);
  129. ?>