26require_once DOL_DOCUMENT_ROOT.
'/core/class/socialnetworkmanager.class.php';
62 foreach ($this->params as $key => $value) {
63 if (stripos($key,
'cookie') !==
false && !empty($value)) {
80 public function fetch($urlAPI, $maxNb = 5, $cacheDelay = 60, $cacheDir =
'', $authParams = [])
84 $this->params = $authParams;
87 $this->error =
'Invalid or missing authentication parameters';
91 $cacheFile = $cacheDir.
'/'.
dol_hash($urlAPI,
'3');
92 $foundInCache =
false;
96 if ($cacheDelay > 0 && $cacheDir &&
dol_is_file($cacheFile)) {
98 if ($fileDate >= (
dol_now() - $cacheDelay)) {
100 $data = file_get_contents($cacheFile);
104 if (!$foundInCache) {
107 'Accept: application/json'
110 $result =
getURLContent($urlAPI,
'GET',
'', 1, $headers, array(
'http',
'https'), 0);
111 if (!empty($result[
'content'])) {
112 $data = $result[
'content'];
115 file_put_contents($cacheFile, $data);
118 $this->error =
'Error retrieving URL ' . $urlAPI;
123 $data = json_decode((
string) $data,
true);
124 if (!is_null($data)) {
125 if (is_array($data)) {
129 foreach ($data as $postData) {
130 if ($count >= $maxNb) {
138 $this->error =
'Invalid data format or empty response';
142 $this->error =
'Failed to retrieve or decode data';
155 if (!is_array($postData)) {
160 'id' => $postData[
'guid'] ??
'',
161 'content' => strip_tags($postData[
'text'] ?? $postData[
'title'] ??
''),
162 'created_at' => $this->
formatDate($postData[
'created_at'] ??
''),
163 'url' =>
'https://diaspora-fr.org/posts/' . ($postData[
'guid'] ??
''),
'media_url' => !empty($postData[
'photos']) && isset($postData[
'photos'][0][
'url']) ? $postData[
'photos'][0][
'url'] :
'',
164 'author_name' => $postData[
'author'][
'name'] ??
'',
165 'author_avatar' => $postData[
'author'][
'avatar'][
'small'] ??
''
176 $timestamp = is_numeric($dateString) ? (int) $dateString : strtotime($dateString);
177 return $timestamp > 0 ?
dol_print_date($timestamp,
"dayhour",
'tzuserrel') :
'Invalid Date';
Class for handling Diaspora API interactions.
getCookieFromParams()
Get the cookie value from params, regardless of the exact key name.
normalizeData($postData)
Normalize data of retrieved posts.
getPosts()
Get the list of retrieved posts.
formatDate($dateString)
Format date for normalize date.
fetch($urlAPI, $maxNb=5, $cacheDelay=60, $cacheDir='', $authParams=[])
Fetch Social Network API to retrieve posts.
isCookieValid()
Check if the provided cookie in params is valid.
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.