1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10: $plugin_is_filter = 950 | ADMIN_PLUGIN;
11: $plugin_description = gettext('Utility to alter the rewrite token substitutions array in the configuration file.');
12: $plugin_author = "Stephen Billard (sbillard)";
13: $plugin_disable = (MOD_REWRITE) ? '' : gettext('Rewrite Tokens are not useful unless the <code>mod_rewrite</code> option is enabled.');
14:
15: $option_interface = 'rewriteTokens';
16:
17: if (OFFSET_PATH == 2) {
18: setOptionDefault('zp_plugin_rewriteTokens', $plugin_is_filter);
19: }
20: zp_register_filter('admin_tabs', 'rewriteTokens::tabs');
21:
22: require_once(SERVERPATH . '/' . ZENFOLDER . '/functions-config.php');
23:
24: class rewriteTokens {
25:
26: private $zp_cfg_a;
27: private $zp_cfg_b;
28: private $conf_vars = array();
29: private $plugin_vars = array();
30:
31: function __construct() {
32: global $_configMutex, $_zp_conf_vars;
33: $_configMutex->lock();
34: $zp_cfg = file_get_contents(SERVERPATH . '/' . DATA_FOLDER . '/' . CONFIGFILE);
35: $i = strpos($zp_cfg, "\$conf['special_pages']");
36: $j = strpos($zp_cfg, '//', $i);
37: if ($i === false || $j === false) {
38: $conf = array('special_pages' => array());
39: $this->conf_vars = $conf['special_pages'];
40: $i = strpos($zp_cfg, '/** Do not edit below this line. **/');
41: if ($i === false) {
42: zp_error(gettext('The Zenphoto configuration file is corrupt. You will need to restore it from a backup.'));
43: }
44: $this->zp_cfg_a = substr($zp_cfg, 0, $i);
45: $this->zp_cfg_b = "//\n" . substr($zp_cfg, $i);
46: } else {
47: $this->zp_cfg_a = substr($zp_cfg, 0, $i);
48: $this->zp_cfg_b = substr($zp_cfg, $j);
49: eval(substr($zp_cfg, $i, $j - $i));
50: $this->conf_vars = $conf['special_pages'];
51: foreach ($_zp_conf_vars['special_pages'] as $page => $element) {
52: if (isset($element['option'])) {
53: $this->plugin_vars[$page] = $element;
54: }
55: }
56: }
57: if (OFFSET_PATH == 2) {
58: $old = array_keys($conf['special_pages']);
59: $zp_cfg = file_get_contents(SERVERPATH . '/' . ZENFOLDER . '/zenphoto_cfg.txt');
60: $i = strpos($zp_cfg, "\$conf['special_pages']");
61: $j = strpos($zp_cfg, '//', $i);
62: eval(substr($zp_cfg, $i, $j - $i));
63: $new = array_keys($conf['special_pages']);
64: if ($old != $new) {
65:
66: setOption('rewriteTokens_restore', 1);
67: $this->handleOptionSave(NULL, NULL);
68: setupLog(gettext('rewriteTokens restored to default'), true);
69: }
70: } else {
71: enableExtension('rewriteTokens', 97 | ADMIN_PLUGIN);
72: }
73: }
74:
75: function __destruct() {
76: global $_configMutex;
77: $_configMutex->unlock();
78: }
79:
80: protected static function anOption($page, $element, &$_definitions) {
81: if ($define = $element['define']) {
82: $_definitions[$element['define']] = strtr($element['rewrite'], $_definitions);
83: $desc = sprintf(gettext('The <code>%1$s</code> rule defines <strong>%2$s</strong> as <em>%3$s</em>.'), $page, $define, strtr($element['rewrite'], $_definitions));
84: } else {
85: $desc = sprintf(gettext('Link for <em>%s</em> script page.'), $page);
86: }
87: return array('key' => 'rewriteTokens_' . $page, 'type' => OPTION_TYPE_CUSTOM,
88: 'desc' => $desc);
89: }
90:
91: function getOptionsSupported() {
92: $_definitions = array();
93: $options = array();
94: if (!MOD_REWRITE) {
95: $options['note'] = array(
96: 'key' => 'rewriteTokens_note',
97: 'type' => OPTION_TYPE_NOTE,
98: 'order' => 0,
99: 'desc' => gettext('<p class="notebox">Rewrite Tokens are not useful unless the <code>mod_rewrite</code> option is enabled.</p>')
100: );
101: }
102: $options[gettext('Reset')] = array('key' => 'rewriteTokens_restore', 'type' => OPTION_TYPE_CHECKBOX,
103: 'order' => 99999,
104: 'desc' => gettext('Restore defaults.'));
105: foreach ($this->conf_vars as $page => $element) {
106: $options[$page] = self::anOption($page, $element, $_definitions);
107: }
108: foreach ($this->plugin_vars as $page => $element) {
109: $options[$page] = self::anOption($page, $element, $_definitions);
110: }
111: return $options;
112: }
113:
114: function handleOption($option, $currentValue) {
115: $element = str_replace('rewriteTokens_', '', $option);
116: if (array_key_exists($element, $this->plugin_vars)) {
117: $element = $this->plugin_vars[$element]['rewrite'];
118: } else {
119: $element = $this->conf_vars[$element]['rewrite'];
120: }
121: ?>
122: <input type="textbox" name="<?php echo $option; ?>" value="<?php echo $element; ?>" >
123: <?php
124: }
125:
126: function handleOptionSave($theme, $album) {
127: $notify = false;
128: if (getOption('rewriteTokens_restore')) {
129: $updated = false;
130: purgeOption('rewriteTokens_restore');
131: $template = file_get_contents(SERVERPATH . '/' . ZENFOLDER . '/zenphoto_cfg.txt');
132: $i = strpos($template, "\$conf['special_pages']");
133: $j = strpos($template, '//', $i);
134: $newtext = substr($template, $i, $j - $i);
135: eval($newtext);
136: $this->conf_vars = $conf['special_pages'];
137: foreach ($this->plugin_vars as $page => $element) {
138: if (isset($element['option'])) {
139: $this->plugin_vars[$page]['rewrite'] = $element['default'];
140: setOption($element['option'], $element['default']);
141: }
142: }
143: } else {
144: foreach ($this->conf_vars as $page => $element) {
145: $rewrite = sanitize($_POST['rewriteTokens_' . $page]);
146: if (empty($rewrite)) {
147: $notify = '&custom=' . gettext('Rewrite tokens may not be empty.');
148: } else {
149: $this->conf_vars[$page]['rewrite'] = $rewrite;
150: }
151: foreach ($this->plugin_vars as $page => $element) {
152: if (isset($element['option'])) {
153: $rewrite = sanitize($_POST['rewriteTokens_' . $page]);
154: if (empty($rewrite)) {
155: $notify = '&custom=' . gettext('Rewrite tokens may not be empty.');
156: } else {
157: $this->plugin_vars[$page]['rewrite'] = $rewrite;
158: setOption($element['option'], $rewrite);
159: }
160: }
161: }
162: }
163: }
164: $newtext = "\$conf['special_pages'] = array(";
165: foreach ($this->conf_vars as $page => $element) {
166: if ($define = $element['define']) {
167: $define = "'" . $define . "'";
168: $desc = sprintf(gettext('Link for <em>%s</em> rule.'), $page);
169: } else {
170: $define = 'false';
171: $desc = sprintf(gettext('Link for <em>%s</em> script page.'), $page);
172: }
173: if (array_key_exists('rule', $element)) {
174: $rule = ", 'rule'=>'{$element['rule']}'";
175: } else {
176: $rule = '';
177: }
178: $newtext .= $token = "\n '$page'=> array('define'=>$define, 'rewrite'=>'{$element['rewrite']}'$rule),";
179: }
180: $newtext = substr($newtext, 0, -1) . "\n );\n";
181: $zp_cfg = $this->zp_cfg_a . $newtext . $this->zp_cfg_b;
182: storeConfig($zp_cfg);
183: return $notify;
184: }
185:
186: static function tabs($tabs) {
187: if (zp_loggedin(ADMIN_RIGHTS)) {
188: if (!isset($tabs['development'])) {
189: $tabs['development'] = array('text' => gettext("development"),
190: 'subtabs' => NULL);
191: }
192: $tabs['development']['subtabs'][gettext("tokens")] = PLUGIN_FOLDER . '/rewriteTokens/admin_tab.php?page=tokens&tab=' . gettext('tokens');
193: $named = array_flip($tabs['development']['subtabs']);
194: natcasesort($named);
195: $tabs['development']['subtabs'] = $named = array_flip($named);
196: $tabs['development']['link'] = array_shift($named);
197: }
198: return $tabs;
199: }
200:
201: }
202: ?>
203: