-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfunctions.php
More file actions
148 lines (128 loc) · 3.49 KB
/
functions.php
File metadata and controls
148 lines (128 loc) · 3.49 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
<?php defined('BLUDIT') or die('BLUDIT');
// Returns the translation of the key
function l($key, $print=true) {
global $languageArray;
$key = mb_strtolower($key, CHARSET);
$key = str_replace(' ','-',$key);
if (isset($languageArray[$key])) {
if ($print) {
echo $languageArray[$key];
} else {
return $languageArray[$key];
}
} else {
if(!$print){
return func_get_arg(0);
}
print(func_get_arg(0));
}
}
function buildItem($data, $key) {
global $currentLanguage;
global $_topbar;
$data['translated'] = true;
$data['key'] = $key;
$data['screenshoot_twitter_url'] = CDN.'items/'.$data['key'].'/screenshot.png';
$data['screenshoot_facebook_url'] = CDN.'items/'.$data['key'].'/screenshot.png';
$data['permalink'] = rtrim($_topbar['website'],'/').'/'.ITEM_TYPE.'/'.$key;
if (!empty($data['description_'.$currentLanguage])) {
$data['description'] = $data['description_'.$currentLanguage];
} else {
if ($currentLanguage!='en') {
$data['translated'] = false;
}
}
if (!empty($data['features_'.$currentLanguage])) {
$data['features'] = $data['features_'.$currentLanguage];
}
$data['author'] = getAuthor($data['author_username']);
// Check Screenshot
$data['screenshoot_url'] = CDN.'items/'.$data['key'].'/screenshot.png';
return $data;
}
function buildAuthor($data) {
return $data;
}
// Returns the items order by date, new to old.
function getItems() {
$tmp = array();
$it = new RecursiveDirectoryIterator(PATH_ITEMS);
foreach (new RecursiveIteratorIterator($it) as $file) {
if ($file->getExtension()=='json') {
$json = file_get_contents($file);
$data = json_decode($json, true);
$item = buildItem($data, basename(dirname(($file))));
array_push($tmp, $item);
}
}
// Sort by date
usort($tmp, "sortByDate");
return $tmp;
}
function getItem($key) {
$metadataFile = PATH_ITEMS.$key.DS.'metadata.json';
if (!file_exists($metadataFile)) {
return false;
}
$json = file_get_contents($metadataFile);
$data = json_decode($json, true);
$item = buildItem($data, $key);
return $item;
}
function getAuthor($username) {
$metadataFile = PATH_AUTHORS.$username.'.json';
if (!file_exists($metadataFile)) {
return false;
}
$json = file_get_contents($metadataFile);
$data = json_decode($json, true);
$author = buildAuthor($data);
return $author;
}
function getItemsByAuthor($username) {
$tmp = array();
$it = new RecursiveDirectoryIterator(PATH_ITEMS);
foreach (new RecursiveIteratorIterator($it) as $file) {
if ($file->getExtension()=='json') {
$json = file_get_contents($file);
$data = json_decode($json, true);
if ($data['author_username']==$username) {
$item = buildItem($data, basename(dirname(($file))));
array_push($tmp, $item);
}
}
}
// Sort by date
usort($tmp, "sortByDate");
return $tmp;
}
function sortByDate($a, $b) {
if ($a['release_date'] == $b['release_date']) {
return 0;
}
return ($a['release_date'] > $b['release_date']) ? -1 : 1;
}
function sanitize($string) {
if(strpos($string, "plugin/") === 0){
$string = substr($string, 7);
}
$string = str_replace(' ', '-', $string);
return preg_replace('/[^A-Za-z0-9\-]/', '', $string);
}
function timeago($time){
$time = time() - $time;
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}