public
Description: a small url shortener service
Homepage:
Clone URL: git://github.com/winks/shortcore.git
winks (author)
Fri Jul 03 01:24:22 -0700 2009
commit  53de3c5377a3c36df38fa1d3512fcffd246db34e
tree    a327ad499775221a3099ca96b9c9bb6fb248c563
parent  918cbe47ad225d72c979ddcd7a51c652b20177f9
shortcore / shortcore.php
100644 271 lines (243 sloc) 7.858 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/**
* Shortcore, a small url shortener service
* (c) 2009 Florian Anderiasch, <fa at art dash core dot org>
* BSD-licenced
*/
 
 
// START DEFAULT CONFIG, do no change, use the extra .config.php
$cfg = array(
    'dbfile' => '/home/www/shortcore/db/shortcore.db',
    'table' => 'shortcore',
    'DEBUG' => false,
    'home' => 'http://example.org/',
    'tpl_body' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Shortcore</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
<div>
%s
</div>
<p style="float: right;">
<a href="http://validator.w3.org/check?uri=referer"><img
src="http://www.w3.org/Icons/valid-xhtml10"
alt="Valid XHTML 1.0 Strict" height="31" width="88" style="border:0;" /></a>
</p>
</body>
</html>'
);
// END DEFAULT CONFIG
 
require './shortcore.config.php';
 
$sc = new Shortcore($cfg);
 
/**
* Everything Shortcore
* @package shortcore
*/
class Shortcore {
    private $cfg;
    private $db;
    private $DEBUG;
    private $version;
 
    /**
* Constructor
* @param array $cfg config values
*/
    function __construct($cfg) {
        $this->version = '0.2';
        $this->cfg = $cfg;
        $this->DEBUG = $cfg['DEBUG'];
        $this->db = new PDO('sqlite://'.$this->cfg['dbfile']);
        $this->handle();
    }
 
    function exec($sql, $array) {
        try {
            $q = $this->db->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
            $q->execute($array);
        } catch (Exception $e) {
            $q = false;
        }
        return $q;
    }
 
 
    /**
* Grabs a result from the database
* @param string $id the desired id
* @return mixed
*/
    function getResult($id) {
        $sql_select = sprintf('SELECT * FROM %s WHERE id=:id', $this->cfg['table']);
        $q = $this->exec($sql_select, array(':id' => $id));
 
        if ($q === false) {
            $result = false;
        } else {
            $result = $q->fetch(PDO::FETCH_ASSOC);
        }
        return $result;
    }
 
    /**
* Redirects to the shortened url
* @param string $id what to look up
*/
    function redirect($id) {
        $preview = false;
        if (substr($id, -1) == '_') {
            $preview = true;
            $id = substr($id, 0, -1);
        }
        $result = $this->getResult($id);
        if (false === $result) {
            $this->page();
        } else {
            $counter = intval($result['counter']) + 1;
            $sql_update = sprintf('UPDATE %s SET counter="%s" WHERE id=:id;', $this->cfg['table'], $counter);
            $p = $this->exec($sql_update, array(':id' => $id));
 
            if ($preview) {
                $link1 = sprintf('<a href="%s_%s">%s_%s</a>', $this->cfg['home'], $id, $this->cfg['home'], $id);
                $link2 = sprintf('<a href="%s">%s</a>', $result['url'], $result['url']);
                $text = sprintf('The link you clicked on, <em>%s</em>, is a redirect to <strong>%s</strong>,<br />'.
                                ' was shortened on <em>%s</em> and has been clicked %s times.',
                                $link1, $link2, date('d.m.Y H:i', $result['created']), $counter);
                echo sprintf($this->cfg['tpl_body'], $text);
            } else {
                $this->page($result['url']);
            }
        }
    }
 
    /**
* Adds a new shortened url
* @param mixed $id the desired id
* @param string $url the target url
* @param mixed $title an optional title
*/
    function add($id = null, $url, $title = null) {
        $time = time();
        if (is_null($id)) {
            $id = $this->randomId();
            $result = $this->getResult($id);
            while (false !== $result) {
                $id = $this->randomId();
                $result = $this->getResult($id);
            }
        } else {
            $result = $this->getResult($id);
 
            while (false !== $result) {
                $last = substr($id, -1);
                if (intval($last) > 0 && intval($last) < 9) {
                    $id = substr( $id, 0, -1) . ($last+1);
                } else {
                    $id .= '2';
                }
                $result = $this->getResult($id);
            }
 
        }
        if (is_null($title)) {
            $title = 'untitled';
        }
        $sql_insert = sprintf('INSERT INTO %s VALUES(:id, :url, :title, 0, "%s");',
                                $this->cfg['table'], $time);
        $this->exec($sql_insert, array(':id' => $id, ':url' => $url, ':title' => $title));
        $this->page($cfg['home'].'_'.$id.'_');
    }
 
    /**
* Executes a redirect
* @param mixed $arg where to go
*/
    function page($arg = null) {
        if (is_null($arg)) {
            $arg = $this->cfg['home'];
        }
        $this->_e('[page] '.$arg);
        header('HTTP/1.1 301 Moved Permanently');
        header('Location: '.$arg);
        exit;
    }
 
    /**
* Handles front controller stuff
*/
    function handle() {
        $_id = null;
        $_url = null;
        $_title = null;
        $_help = null;
 
        if(isset($_GET['help'])) {
            $_help = true;
        }
 
        if (isset($_GET['url']) && substr($_GET['url'],0,4) == 'http' && strlen($_GET['url']) > 10) {
            $_url = $_GET['url'];
        }
        if (isset($_GET['title']) && strlen(strip_tags($_GET['title'])) > 0 ) {
            $_title = strip_tags($_GET['title']);
        }
        if (isset($_GET['id'])) {
            $pat = '([a-z0-9-]{2,})i';
            if (preg_match($pat, $_GET['id'])) {
                $_id = $_GET['id'];
            }
        }
 
        // writing
        if (!is_null($_url)) {
            $this->_e('adding:'.$_id);
            $this->add($_id, $_url, $_title);
        // reading
        } else {
            // this is "/_<id>"
            if (!is_null($_id)) {
                $this->_e('redir:'.$_id);
                $this->redirect($_id);
            }
        }
        if (!is_null($_help)) {
            
            $bookmarklet = <<<BML
javascript:foo=prompt('id?');location.href='%shortcore.php?url='+encodeURIComponent(location.href)+'&amp;title='+encodeURIComponent(document.title)+'&amp;id='+foo
BML;
            $bookmarklet = sprintf($bookmarklet, $this->cfg['home']);
            $link = sprintf('<a href="%s">shorten</a>', $bookmarklet);
 
            $text = sprintf('<p>Powered by Shortcore v. %s</p>Bookmarklet: %s', $this->cfg['version'], $link);
 
            echo sprintf($this->cfg['tpl_body'],$text);
            exit;
        }
        $this->page();
    }
 
    /**
* Generates a random id
* @param int $len the desired length
* @return string
*/
    function randomId($len = 4) {
        $choice = array();
        $a = ord('a');
        $z = ord('z');
        $A = ord('A');
        $Z = ord('Z');
 
        // ignore the hard to read chars
        $banned = array(ord('l'), ord('I'), ord('O'));
 
        for($i=$a;$i<=$z;$i++) {
            if (!in_array($i, $banned)) {
                $choice[] = chr($i);
            }
        }
        for($i=$A;$i<=$Z;$i++) {
            if (!in_array($i, $banned)) {
                $choice[] = chr($i);
            }
        }
        for($i=0;$i<=9;$i++) {
            $choice[] = $i;
        }
        $out = '';
        for ($i=0;$i<$len;$i++) {
            $rand = rand(0, count($choice)-1);
            $out .= $choice[$rand];
        }
        return $out;
    }
 
    /**
* Debug wrapper to error_log()
* @param mixed $arg what to show
*/
    function _e($arg) {
        error_log('(sho) '.$arg);
    }
}
?>