-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocialMedia.class.php
More file actions
206 lines (171 loc) · 5.57 KB
/
SocialMedia.class.php
File metadata and controls
206 lines (171 loc) · 5.57 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
<?php
abstract class SocialMedia
{
protected $isCache = true;
protected $cache_key;
// Seconds in the cache
private $cache_length = 600;
/**
* @param boolean $isCache
*/
public function setIsCache($isCache)
{
$this->isCache = $isCache;
}
/**
* @return boolean
*/
public function getIsCache()
{
return $this->isCache;
}
/**
* @param int $cache_length
*/
public function setCacheLength($cache_length)
{
$this->cache_length = $cache_length;
}
/**
* @return int
*/
public function getCacheLength()
{
return $this->cache_length;
}
/**
* @param string $cache_key
*/
public function setCacheKey($cache_key)
{
$this->cache_key = $cache_key;
}
/**
* @return string
*/
public function getCacheKey()
{
return $this->cache_key;
}
/**
* The main method for getting posts that should be called from external scripts
* @param int $start
* @param int $count
* @return bool|mixed
*
*/
public function getLatestPosts($start = 0, $count = 1)
{
$latest_posts = false;
$apc = (extension_loaded('apc') && ini_get('apc.enabled'));
$cache_enabled = ($apc && $this->getIsCache());
if ($cache_enabled) {
// Look in cache
$cache_key = $this->cache_key . $start . $count;
$latest_posts = apc_fetch($cache_key);
}
if (!$latest_posts) {
$latest_posts = $this->loadPosts($start, $count);
if ($cache_enabled) {
// Cache for 10 minutes
apc_store( $cache_key, $latest_posts, $this->getCacheLength() );
}
}
// Add nice date e.g About a minute ago
foreach ($latest_posts as $index => $post) {
$latest_posts[$index]["date"] = $this->timeSince($post["date"]);
}
return $latest_posts;
}
public abstract function loadPosts($start = 0, $count = 1);
/**
* Create a nice string telling the user how long ago this was post was made
* @param $timestamp
* @return string
*/
protected function timeSince($timestamp)
{
if (defined("ICL_LANGUAGE_CODE") && ICL_LANGUAGE_CODE == "fr") {
$timePrefix = "Il y a ";
$timeSuffix = "";
// Common time periods as an array of arrays
$periods = array(
array(60 * 60 * 24 * 365, 'ans'),
array(60 * 60 * 24 * 30, 'mois'),
array(60 * 60 * 24 * 7, 'semaine'),
array(60 * 60 * 24, 'jour'),
array(60 * 60, 'heure'),
array(60, 'minute'),
);
} else {
$timePrefix = "";
$timeSuffix = " ago";
// Common time periods as an array of arrays
$periods = array(
array(60 * 60 * 24 * 365, 'year'),
array(60 * 60 * 24 * 30, 'month'),
array(60 * 60 * 24 * 7, 'week'),
array(60 * 60 * 24, 'day'),
array(60 * 60, 'hour'),
array(60, 'minute'),
);
}
$today = time();
$since = $today - $timestamp; // Find the difference of time between now and the past
// Loop around the periods, starting with the biggest
for ($i = 0, $j = count($periods);$i < $j;$i ++) {
$seconds = $periods[$i][0];
$name = $periods[$i][1];
// Find the biggest whole period
if (($count = floor($since / $seconds)) != 0) {
break;
}
}
if ($count == 1) {
$output = $timePrefix . '1 ' . $name;
} else {
if ($name == "mois" || $name == "ans") {
//don't add an S for these two french times
$output = $timePrefix . $count . " " . $name;
} else {
$output = $timePrefix . $count . " " . $name . "s";
}
}
if ($i + 1 < $j) {
// Retrieving the second relevant period
$seconds2 = $periods[$i + 1][0];
$name2 = $periods[$i + 1][1];
// Only show it if it's greater than 0
if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) {
if ($name2 == "mois" || $name2 == "ans") {
$output .= ($count2 == 1) ? ', 1 ' . $name2 : ", $count2 {$name2}";
} else {
$output .= ($count2 == 1) ? ', 1 ' . $name2 : ", $count2 {$name2}s";
}
}
}
$output .= $timeSuffix;
return $output;
}
/**
* Add links to any urls in the text
* @param $text
* @return mixed
*/
protected function addUrls($text)
{
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// Check if there is a url in the text
// force http: on www.
$text = preg_replace( "@www\.@", "http://www.", $text );
// eliminate duplicates after force
$text = preg_replace( "@http://http://www\.@", "http://www.", $text );
$text = preg_replace( "@https://http://www\.@", "https://www.", $text );
if(preg_match($reg_exUrl, $text, $url)) {
// make the urls hyper links
$text = preg_replace($reg_exUrl, '<a href="'.$url[0].'" rel="nofollow" target="_blank">'.$url[0].'</a>', $text);
}
return $text;
}
}