dolibarr 23.0.4
socialnetworkmanager.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2024 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
25require_once DOL_DOCUMENT_ROOT.'/core/class/mastodonhandler.class.php';
26
27
32{
36 public $db;
37
41 private $platform;
42
46 private $handler;
47
51 public $error = '';
52
56 private $lastFetchDate;
57
64 public function __construct($platform, $authParams = [])
65 {
66 $this->platform = $platform;
67 $this->initializeHandler($authParams);
68 }
69
75 private function initializeHandler($authParams)
76 {
77 // Load the file of the handler (only the handlers provided with Dolibarr, the platform is the name given in setup)
78 $handlerFile = strtolower((string) $this->platform);
79 if (in_array($handlerFile, array('mastodon', 'diaspora', 'reddit'))) {
80 require_once DOL_DOCUMENT_ROOT.'/core/class/'.$handlerFile.'handler.class.php';
81 }
82
83 $handlerClass = dol_ucfirst($this->platform).'Handler';
84 if (class_exists($handlerClass)) {
85 $this->handler = new $handlerClass($authParams);
86 } else {
87 $this->error = "Handler for $this->platform not found.";
88 }
89 }
90
101 public function fetchPosts($urlAPI, $maxNb = 5, $cacheDelay = 60, $cacheDir = '', $authParams = [])
102 {
103 if (!$this->handler) {
104 return false;
105 }
106
107 // This fetch URL
108 $result = $this->handler->fetch($urlAPI, $maxNb, $cacheDelay, $cacheDir, $authParams);
109
110 if (!empty($this->handler->error)) {
111 $this->error = $this->handler->error;
112 }
113
114 // Date of the data shown: date of the cache file (also the date of the last successful fetch when this one failed)
115 $cacheFile = $cacheDir.'/'.dol_hash($urlAPI, '3');
116 if ($cacheDir && dol_is_file($cacheFile)) {
117 $this->lastFetchDate = dol_filemtime($cacheFile);
118 } elseif ($result !== false) {
119 $this->lastFetchDate = dol_now();
120 }
121
122 return $result;
123 }
124
130 public function getPosts()
131 {
132 $posts = $this->handler ? $this->handler->getPosts() : [];
133 if (is_array($posts)) {
134 // The links come from the social network API and are used as href: keep only http(s) links (a "javascript:" link would run in Dolibarr)
135 foreach ($posts as $key => $post) {
136 if (!empty($post['url']) && !preg_match('/^https?:\/\//i', $post['url'])) {
137 $posts[$key]['url'] = '';
138 }
139 }
140 }
141 return $posts;
142 }
143
149 public function getLastFetchDate()
150 {
151 return $this->lastFetchDate;
152 }
153}
Class to manage Social network posts.
initializeHandler($authParams)
Initialize the social network needed.
getLastFetchDate()
Get the last fetch date.
fetchPosts($urlAPI, $maxNb=5, $cacheDelay=60, $cacheDir='', $authParams=[])
Fetch Social Network API to retrieve posts.
__construct($platform, $authParams=[])
Constructor.
getPosts()
Get the list of retrieved posts.
dol_filemtime($pathoffile)
Return time of a file.
dol_is_file($pathoffile)
Return if path is a file.
dol_now($mode='gmt')
Return date for now.
dol_ucfirst($string, $encoding="UTF-8")
Convert first character of the first word of a string to upper.
dol_hash($chain, $type='0', $nosalt=0, $mode=0)
Returns a hash (non reversible encryption) of a string.