Commit cfe5628c authored by Max Koder's avatar Max Koder

[MAJ] TootoPHP

parent c6b8a250
MIT License
Copyright (c) 2017 MaxenceCauderlier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
<?php
/**
* TootoPHP
*/
/**
* TootoPHP is a simple class to get toots from a specified Mastodon instance
* and use them with PHP
*
* @author Maxence Cauderlier
* @link http://max-koder.fr
* @license http://opensource.org/licenses/MIT The MIT License
*/
class TootoPHP
{
/** @var string Atom File URL */
protected $xmlFileURL;
/** @var \DOMDocument Atom DOMDocument */
protected $dom;
/** @var \DomNodeList Feed Node List */
protected $feed;
/** @var string User URL */
public $uri;
/** @var string User Name */
public $name;
/** @var string User Email */
public $email;
/** @var string User Summary */
public $summary;
/** @var string User Alternate */
public $alternate;
/** @var string User Avatar URL */
public $avatar;
/** @var string User Header URL */
public $header;
/** @var boolean Is error */
public $on_error = false;
/**
* Constructor
*
* Test if instance and name are ok
*
* @param string $instance
* @param string $name
*/
public function __construct($instance, $name)
{
$this->xmlFileURL = 'https://' . $instance . '/users/' . $name . '.atom';
$file_headers = @get_headers($this->xmlFileURL);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
$this->on_error = true;
return false;
}
$this->loadXMLFile($this->xmlFileURL);
$this->getUserInfos();
}
/**
* Load DOM from XML document
*/
protected function loadXMLFile($url)
{
$this->dom = new DOMDocument();
$this->dom->load($url);
$this->feed = $this->dom->getElementsByTagName('feed')->item(0);
}
/**
* Get Author Infos
*/
protected function getUserInfos()
{
$author = $this->feed->getElementsByTagName('author')->item(0);
$this->uri = $author->getElementsByTagName('uri')->item(0)->nodeValue;
$this->name = $author->getElementsByTagName('name')->item(0)->nodeValue;
$this->email = $author->getElementsByTagName('email')->item(0)->nodeValue;
$this->summary = $author->getElementsByTagName('summary')->item(0)->nodeValue;
foreach ($author->getElementsByTagName('link') as $link) {
if ($link->hasAttribute("rel")) {
$this->{$link->getAttribute('rel')} = $link->getAttribute('href');
}
}
}
/**
* Get many toots by timestamp order DESC
* param must be an integer greater than 0
* return an array of entries with :
* 'published' : Publish timestamp
* 'updated' : Last modification timestamp
* 'title' : Entry title
* 'content' : Entry content
* 'pic' : Array with pictures URL
*
* @param integer $number_of_toots
* @return array
*/
public function getToots($number_of_toots = 5)
{
$actualToot = 0;
$entries = [];
foreach ($this->feed->getElementsByTagName('entry') as $entry) {
if ($actualToot >= $number_of_toots) break;
$entries[$actualToot] = [];
$entries[$actualToot]['published'] = strtotime($entry->getElementsByTagName('published')->item(0)->nodeValue);
$entries[$actualToot]['updated'] = strtotime($entry->getElementsByTagName('updated')->item(0)->nodeValue);
$entries[$actualToot]['title'] = $entry->getElementsByTagName('title')->item(0)->nodeValue;
$entries[$actualToot]['content'] = $entry->getElementsByTagName('content')->item(0)->nodeValue;
$entries[$actualToot]['pic'] = [];
foreach ($entry->getElementsByTagName('link') as $link) {
if ($link->hasAttribute("rel") && $link->getAttribute('rel') === 'enclosure') {
$entries[$actualToot]['pic'][] = $link->getAttribute('href');
}
}
$actualToot ++;
}
return $entries;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment