26require_once DOL_DOCUMENT_ROOT.
'/core/class/socialnetworkmanager.class.php';
57 private $clientSecret;
73 $this->clientId = $authParams[
'client_id'] ??
'';
74 $this->clientSecret = $authParams[
'client_secret'] ??
'';
75 $this->redirectUri = $authParams[
'redirect_uri'] ??
'';
76 $this->accessToken = $authParams[
'access_token'] ??
'';
90 public function fetch($urlAPI, $maxNb = 5, $cacheDelay = 60, $cacheDir =
'', $authParams = [])
92 if (empty($this->accessToken) && isset($authParams[
'access_token'])) {
95 $cacheFile = $cacheDir.
'/'.
dol_hash($urlAPI,
'3');
96 $foundInCache =
false;
100 if ($cacheDelay > 0 && $cacheDir &&
dol_is_file($cacheFile)) {
102 if ($fileDate >= (
dol_now() - $cacheDelay)) {
103 $foundInCache =
true;
104 $data = file_get_contents($cacheFile);
108 if (!$foundInCache) {
110 'Authorization: Bearer ' . $this->accessToken,
111 'Content-Type: application/json'
114 $result =
getURLContent($urlAPI,
'GET',
'', 1, $headers, array(
'http',
'https'), 0);
115 if (!empty($result[
'content'])) {
116 $data = $result[
'content'];
120 file_put_contents($cacheFile, $data);
123 $this->error =
'Error retrieving URL ' . $urlAPI;
127 if (!is_null($data)) {
128 $data = json_decode($data,
true);
129 if (is_array($data)) {
133 foreach ($data as $postData) {
134 if ($count >= $maxNb) {
142 $this->error =
'Invalid data format or empty response';
146 $this->error =
'Failed to retrieve or decode data';
159 if (!is_array($postData)) {
163 'id' => $postData[
'id'] ??
'',
164 'content' => strip_tags($postData[
'content'] ??
''),
165 'created_at' => $this->
formatDate($postData[
'created_at'] ??
''),
166 'url' => $postData[
'url'] ??
'',
167 'media_url' => $postData[
'media_attachments'][0][
'url'] ??
''
178 $timestamp = is_numeric($dateString) ? (int) $dateString : strtotime($dateString);
179 return $timestamp > 0 ?
dol_print_date($timestamp,
"dayhour",
'tzuserrel') :
'Invalid Date';
198 return $this->redirectUri;
207 return $this->accessToken;
216 return $this->clientId;
225 return $this->clientSecret;
Class for handler Mastodon.
formatDate($dateString)
Format date for normalize date.
getClientSecret()
Getter for secret client.
__construct($authParams)
Constructor to set the necessary credentials.
normalizeData($postData)
Normalize data of retrieved posts.
getClientId()
Getter for client Id.
fetch($urlAPI, $maxNb=5, $cacheDelay=60, $cacheDir='', $authParams=[])
Fetch posts from Mastodon API using the access token.
getPosts()
Get the list of retrieved posts.
getAccessToken()
Getter for access token.
getRedirectUri()
Getter for url to redirect.
dol_filemtime($pathoffile)
Return time of a file.
dol_is_file($pathoffile)
Return if path is a file.
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1)
Function to get a content from an URL (use proxy if proxy defined).
dol_hash($chain, $type='0', $nosalt=0)
Returns a hash (non reversible encryption) of a string.