-
Notifications
You must be signed in to change notification settings - Fork 2
/
api_counter.php
111 lines (78 loc) · 1.98 KB
/
api_counter.php
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
<?php
// log visits
/*
echo '<pre>';
print_r($_SERVER);
echo '</pre>';
*/
require_once (dirname(__FILE__) . '/couchsimple.php');
require_once (dirname(__FILE__) . '/lib.php');
require_once (dirname(__FILE__) . '/api_utils.php');
//--------------------------------------------------------------------------------------------------
function default_display()
{
echo "hi";
}
//--------------------------------------------------------------------------------------------------
// Authors with same last name and similar first names
function display_view_counter($id, $callback = '')
{
global $config;
global $couch;
$id = str_replace('biostor/', '', $id);
$startkey = array($id);
$endkey = array($id, 'z'); //mb_convert_encoding('￰', 'UTF-8', 'HTML-ENTITIES'));
// We count the number of distinct IP addresses that have visited this page
$url = '_design/counter/_view/views?startkey=' . json_encode($startkey) . '&endkey=' . json_encode($endkey) . '&group_level=2';
$resp = $couch->send("GET", "/" . $config['couchdb_options']['database'] . "/" . $url);
//echo $url . '<br/>';
//echo $resp;
$response_obj = json_decode($resp);
$obj = new stdclass;
$obj->status = 404;
$obj->url = $url;
if (isset($response_obj->error))
{
$obj->error = $response_obj->error;
}
else
{
if (count($response_obj->rows) == 0)
{
$obj->error = 'Not found';
}
else
{
$obj->status = 200;
$obj->results = array();
$obj->results[] = count($response_obj->rows);
}
}
api_output($obj, $callback);
}
//--------------------------------------------------------------------------------------------------
function main()
{
$callback = '';
$handled = false;
// If no query parameters
if (count($_GET) == 0)
{
default_display();
exit(0);
}
if (isset($_GET['callback']))
{
$callback = $_GET['callback'];
}
if (!$handled)
{
if (isset($_GET['id']))
{
display_view_counter($_GET['id'], $callback);
$handled = true;
}
}
}
main();
?>