-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.php
108 lines (92 loc) · 2.62 KB
/
plugin.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
<?php
/*
Plugin Name: Time-Period Clicks
Plugin URI: https://github.com/rinogo/yourls-time-period-clicks
Description: A simple plugin for reporting clicks in a specific time period.
Version: 1.0
Author: Rich Christiansen
Author URI: http://endorkins.com
*/
// Define custom action "url-stats-period"
yourls_add_filter("api_action_url-stats-period", "tpc_get_stats");
//Get stats for `url` or `shorturl` between `since` and `until`.
function tpc_get_stats() {
try {
global $ydb;
//Process `url` or `shorturl` parameter
if(!isset($_REQUEST["url"]) && !isset($_REQUEST["shorturl"])) {
return array(
"errorCode" => 400,
"message" => "error: Missing 'url' or 'shorturl' parameter.",
);
}
if(isset($_REQUEST["url"])) {
$keywords = tpc_get_url_keywords($_REQUEST["url"]);
} else {
$pos = strrpos($_REQUEST["shorturl"], "/");
//Accept "http://sho.rt/abc"
if($pos !== false) {
$keywords = array(substr($_REQUEST["shorturl"], $pos + 1));
//Accept "abc"
} else {
$keywords = array($_REQUEST["shorturl"]);
}
}
if(empty($keywords[0]) || !yourls_is_shorturl($keywords[0])) {
return array(
"errorCode" => 404,
"message" => "error: not found",
);
}
//Process `since` and `until` parameters.
if(isset($_REQUEST["since"])) {
$since = intval($_REQUEST["since"]);
} else {
$since = 0; //Default to the Unix Epoch
}
if(isset($_REQUEST["until"])) {
$until = intval($_REQUEST["until"]);
} else {
$until = time(); //Default to now
}
if($since >= $until) {
return array(
"errorCode" => 400,
"message" => "error: The 'since' value ($since) must be smaller than the 'until' value ($until).",
);
}
$params = array(
"shorturls" => $keywords,
"since" => date("Y-m-d H:i:s", $since),
"until" => date("Y-m-d H:i:s", $until),
);
$sql = "SELECT COUNT(*)
FROM " . YOURLS_DB_TABLE_LOG . "
WHERE
shorturl IN (:shorturls) AND
click_time > :since AND
click_time <= :until";
$result = $ydb->fetchValue($sql, $params);
return array(
"statusCode" => 200,
"message" => "success",
"url-stats-period" => array(
"clicks" => $result
)
);
} catch (Exception $e) {
return array(
"errorCode" => 500,
"message" => "error: " . $e->getMessage(),
);
}
}
//Get all keywords associated with `$url`
function tpc_get_url_keywords( $url ) {
global $ydb;
$table = YOURLS_DB_TABLE_URL;
$url = yourls_sanitize_url($url);
$rows = $ydb->fetchObjects("SELECT `keyword` FROM `$table` WHERE `url` = :url", array("url" => $url));
$keywords = array_map(function($r) { return $r->keyword; }, $rows);
return $keywords;
}