1: <?php
2: /**
3: * xoAppUrl Smarty compiler plug-in
4: *
5: * See the enclosed file LICENSE for licensing information.
6: * If you did not receive this file, get it at http://www.gnu.org/licenses/gpl-2.0.html
7: *
8: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
9: * @license GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
10: * @author Skalpa Keo <skalpa@xoops.org>
11: * @package xos_opal
12: * @subpackage xos_opal_Smarty
13: * @since 2.0.14
14: */
15:
16: /**
17: * Inserts the URL of an application page
18: *
19: * This plug-in allows you to generate a module location URL. It uses any URL rewriting
20: * mechanism and rules you'll have configured for the system.
21: *
22: * To ensure this can be as optimized as possible, it accepts 2 modes of operation:
23: *
24: * <b>Static address generation</b>:<br>
25: * This is the default mode and fastest mode. When used, the URL is generated during
26: * the template compilation, and statically written in the compiled template file.
27: * To use it, you just need to provide a location in a format XOOPS understands.
28: *
29: * <code>
30: * // Generate an URL using a physical path
31: * ([xoAppUrl modules/something/yourpage.php])
32: * // Generate an URL using a module+location identifier (2.3+)
33: * ([xoAppUrl mod_xoops_Identification#logout])
34: * </code>
35: *
36: * <b>Dynamic address generation</b>:<br>
37: * The is the slowest mode, and its use should be prevented unless necessary. Here,
38: * the URL is generated dynamically each time the template is displayed, thus allowing
39: * you to use the value of a template variable in the location string. To use it, you
40: * must surround your location with double-quotes ("), and use the
41: * {@link http://smarty.php.net/manual/en/language.syntax.quotes.php Smarty quoted strings}
42: * syntax to insert variables values.
43: *
44: * <code>
45: * // Use the value of the $sortby template variable in the URL
46: * ([xoAppUrl "modules/something/yourpage.php?order=`$sortby`"])
47: * </code>
48: * @param $argStr
49: * @param $compiler
50: * @return string
51: */
52: function smarty_compiler_xoAppUrl($argStr, &$compiler)
53: {
54: global $xoops;
55: $argStr = trim($argStr);
56:
57: @list($url, $params) = explode(' ', $argStr, 2);
58:
59: if (substr($url, 0, 1) === '/') {
60: $url = 'www' . $url;
61: }
62: // Static URL generation
63: if (strpos($argStr, '$') === false && $url !== '.') {
64: if (isset($params)) {
65: $params = $compiler->_parse_attrs($params, false);
66: foreach ($params as $k => $v) {
67: if (in_array(substr($v, 0, 1), array('"', "'"))) {
68: $params[$k] = substr($v, 1, -1);
69: }
70: }
71: $url = $xoops->buildUrl($url, $params);
72: }
73: $url = $xoops->path($url, true);
74:
75: return "echo '" . addslashes(htmlspecialchars($url)) . "';";
76: }
77: // Dynamic URL generation
78: $str = "\$xoops->path('$url', true)";
79: if ($url === '.') {
80: $str = "\$_SERVER['REQUEST_URI']";
81: }
82: if (isset($params)) {
83: $params = $compiler->_parse_attrs($params, false);
84: $str = "\$xoops->buildUrl($str, array(\n";
85: foreach ($params as $k => $v) {
86: $str .= var_export($k, true) . " => $v,\n";
87: }
88: $str .= '))';
89: }
90:
91: return "echo htmlspecialchars($str);";
92: }
93: