1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16: $plugin_is_filter = 5 | CLASS_PLUGIN;
17: $plugin_description = gettext('Provides short URLs to Zenphoto objects.');
18: $plugin_author = "Stephen Billard (sbillard)";
19: $plugin_disable = (MOD_REWRITE) ? '' : gettext('Shortened URLs require the <code>mod_rewrite</code> option be enabled.');
20:
21: $option_interface = 'tinyURL';
22:
23: if (getOption('tinyURL_agressive'))
24: zp_register_filter('getLink', 'tinyURL::getTinyURL');
25:
26: switch (OFFSET_PATH) {
27: case 0:
28: if (!$plugin_disable) {
29: zp_register_filter('load_request', 'tinyURL::parse');
30: }
31: break;
32: case 2:
33: setOptionDefault('zp_plugin_tinyURL', $plugin_is_filter);
34: setOptionDefault('tinyURL_agressive', 0);
35: setOptionDefault('tinyURL_text', 'tiny');
36: break;
37: default:
38: break;
39: }
40: $_zp_conf_vars['special_pages']['tiny'] = array('define' => '_TINY_', 'rewrite' => getOption('tinyURL_text'),
41: 'option' => 'tinyURL_text', 'default' => 'tiny/');
42: $_zp_conf_vars['special_pages'][] = array('define' => false, 'rewrite' => '^%TINY%([0-9]+)/?$',
43: 'rule' => '%REWRITE% index.php?p=$1&t [L,QSA]');
44: $_zp_conf_vars['special_pages'][] = array('define' => false, 'rewrite' => '^%TINY%([0-9]+)/([0-9]+)/?$', 'rule' => '%REWRITE% index.php?p=$1&page=$2&t [L,QSA]');
45: $_zp_conf_vars['special_pages'][] = array('definition' => '%TINY%', 'rewrite' => '_TINY_');
46:
47: class tinyURL {
48:
49: const albums = 1;
50: const images = 2;
51: const news = 4;
52: const news_categories = 8;
53: const pages = 16;
54:
55: static $DBassoc = array('albums' => self::albums, 'images' => self::images, 'news' => self::news, 'news_categories' => self::news_categories, 'pages' => self::pages);
56: static $tableAsoc = array('1' => 'albums', '2' => 'images', '3' => 'news', '4' => 'pages', '5' => 'comments', '6' => 'news_categories');
57:
58: function __construct() {
59:
60: }
61:
62: function getOptionsSupported() {
63: $options = array();
64: $options[gettext('Use in themes for')] = array(
65: 'key' => 'tinyURL_agressive',
66: 'type' => OPTION_TYPE_CUSTOM,
67: 'order' => 1,
68: 'desc' => gettext('If an option is chosen, normal theme URLs will be replaced with <i>tinyURL</i>s for that object.')
69: );
70:
71: return $options;
72: }
73:
74: function handleOption($option, $currentValue) {
75: ?>
76: <label class="nowrap"><input type="checkbox" name="tinyURL_albums" value="<?php echo self::albums; ?>" <?php if ($currentValue & self::albums) echo 'checked="checked" '; ?>/><?php echo gettext('albums'); ?></label>
77: <label class="nowrap"><input type="checkbox" name="tinyURL_images" value="<?php echo self::images; ?>" <?php if ($currentValue & self::images) echo 'checked="checked" '; ?>/><?php echo gettext('images'); ?></label>
78: <?php
79: if (extensionEnabled('zenpage')) {
80: ?>
81: <label class="nowrap"><input type="checkbox" name="tinyURL_news" value="<?php echo self::news; ?>" <?php if ($currentValue & self::news) echo 'checked="checked" '; ?>/><?php echo gettext('news'); ?></label>
82:
83: <label class="nowrap"><input type="checkbox" name="tinyURL_news_categories" value="<?php echo self::news_categories; ?>" <?php if ($currentValue & self::news_categories) echo 'checked="checked" '; ?>/><?php echo gettext('news categories'); ?></label>
84:
85: <label class="nowrap"><input type="checkbox" name="tinyURL_pages" value="<?php echo self::pages; ?>" <?php if ($currentValue & self::pages) echo 'checked="checked" '; ?>/><?php echo gettext('pages'); ?></label>
86: <?php
87: }
88: }
89:
90: function handleOptionSave($themename, $themealbum) {
91: $result = 0;
92: if (isset($_POST['tinyURL_albums']))
93: $result = $result | self::albums;
94: if (isset($_POST['tinyURL_images']))
95: $result = $result | self::images;
96: if (isset($_POST['tinyURL_news']))
97: $result = $result | self::news;
98: if (isset($_POST['tinyURL_news_categories']))
99: $result = $result | self::news_categories;
100: if (isset($_POST['tinyURL_pages']))
101: $result = $result | self::pages;
102: setOption('tinyURL_agressive', $result);
103: return false;
104: }
105:
106: 107: 108: 109: 110:
111: static function getURL($obj, $page = NULL) {
112: $asoc = array_flip(self::$tableAsoc);
113: $tiny = ($obj->getID() << 3) | $asoc[$obj->table];
114: if (MOD_REWRITE) {
115: if ($page > 1)
116: $tiny.='/' . $page;
117: if (class_exists('seo_locale')) {
118: return seo_locale::localePath(false) . '/' . _TINY_ . $tiny;
119: } else {
120: return WEBPATH . '/' . _TINY_ . $tiny;
121: }
122: } else {
123: if ($page > 1)
124: $tiny.= '&page=' . $page;
125: return WEBPATH . '/index.php?p=' . $tiny . '&t';
126: }
127: }
128:
129: static function getTinyURL($link, $obj, $page) {
130: if (is_object($obj) && (self::$DBassoc[$obj->table] & getOption('tinyURL_agressive'))) {
131: return self::getURL($obj, $page);
132: } else {
133: return $link;
134: }
135: }
136:
137: static function parse($success) {
138: if (isset($_GET['p']) && isset($_GET['t'])) {
139: unset($_GET['t']);
140: $tiny = sanitize_numeric($_GET['p']);
141: $tbl = $tiny & 7;
142:
143: if (array_key_exists($tbl, self::$tableAsoc)) {
144: $tbl = self::$tableAsoc[$tbl];
145: $id = $tiny >> 3;
146: $result = query_single_row('SELECT * FROM ' . prefix($tbl) . ' WHERE `id`=' . $id);
147: if ($result) {
148: switch ($tbl) {
149: case 'news':
150: case 'pages':
151: $_GET['p'] = $tbl;
152: $_GET['title'] = $result['titlelink'];
153: break;
154: case 'news_categories';
155: $_GET['p'] = 'news';
156: $_GET['category'] = $result['titlelink'];
157: break;
158: case 'images':
159: $image = $_GET['image'] = $result['filename'];
160: $result = query_single_row('SELECT * FROM ' . prefix('albums') . ' WHERE `id`=' . $result['albumid']);
161: case 'albums':
162: $album = $_GET['album'] = $result['folder'];
163: unset($_GET['p']);
164: if (!empty($image)) {
165: $success = zp_load_image($album, $image);
166: } else if (!empty($album)) {
167: $success = zp_load_album($album);
168: }
169: break;
170: case 'comments':
171: unset($_GET['p']);
172: $commentid = $id;
173: $type = $result['type'];
174: $result = query_single_row('SELECT * FROM ' . prefix($result['type']) . ' WHERE `id`=' . $result['ownerid']);
175: switch ($type) {
176: case 'images':
177: $image = $result['filename'];
178: $result = query_single_row('SELECT * FROM ' . prefix('albums') . ' WHERE `id`=' . $result['albumid']);
179: $redirect = 'index.php?album=' . $result['folder'] . '&image=' . $image;
180: break;
181: case 'albums':
182: $album = $result['folder'];
183: $redirect = 'index.php?album=' . $result['folder'];
184: break;
185: case 'pages':
186: $redirect = 'index.php?p=pages&title=' . $result['titlelink'];
187: break;
188: }
189: $redirect .= '#zp_comment_id_' . $commentid;
190: header("HTTP/1.0 301 Moved Permanently");
191: header("Status: 301 Moved Permanently");
192: header('Location: ' . FULLWEBPATH . '/' . $redirect);
193: exitZP();
194: break;
195: }
196: }
197: }
198: }
199: return $success;
200: }
201:
202: }
203: