Commit d89c99a3 authored by BorisPAING's avatar BorisPAING

Première soumission

parent 81420e73
Pipeline #7273 failed with stages
in 54 seconds
Funding PHP
===
## Une classe PHP pour accéder aux données d'un portefeuille et afficher l'état d'avancement d'un crowdfunding
Voir le fichier ```exemple.php``` pour comprendre l'usage.
Funding PHP
===
## A PHP class to access Duniter wallet data and display crowdfunding status
See ```exemple.php``` file to learn usage.
\ No newline at end of file
# Funding PHP
Funding PHP
===
- français : [LISEZ-MOI.md](LISEZ-MOI.md)
- english : [READ-ME.md](READ-ME.md)
\ No newline at end of file
<?php
define('FUNDING_PUBKEY', 'CitdnuQgZ45tNFCagay7Wh12gwwHM8VLej1sWmfHWnQX');
define('FUNDING_TARGET', '100');
<?php
include('inc/Funding.class.php');
include('config.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Exemple de Funding PHP</title>
<link rel="stylesheet" type="text/css" href="funding.css" />
</head>
<body>
<h1><?php echo _("Coucou"); ?></h1>
<p>
Nous sommes Axiom-Team et nous œuvrons à la promotion de la monnaie libre Ğ1.
</p>
<p>
On prend la parole ici parce qu'on voulait vous informer de la chose suivante&nbsp;:
</p>
<p>
Il existe une caisse de côtisations qui permet à la communauté de valoriser (en monnaie libre)
la contribution que les développeurs de Cesium apportent au développement de la Ğ1.
</p>
<p>
L'objectif est de réunir sur cette caisse la somme de <?php echo FUNDING_TARGET; ?> DU<sub>Ğ1</sub> sur 30 jours glissant.
</p>
<p>
Voilà où nous en sommes aujourd'hui ce mois-ci par rapport à cet objectif&nbsp;:
</p>
<?php
$startDate = date('d/m/Y', (time() - (30*24*3600)));
$target = 100;
$funding = new Funding(FUNDING_PUBKEY, $target, $startDate, 'relative');
echo '
<aside class="crowdfunding-widget">
<meter min="0" max="100" value="'. $funding->getPercentage() .'" high="75" low="25" class="progress-bar">
'. $funding->getPercentage() .'%
</meter>
<p>
<strong>'. $funding->getPercentage() .'%</strong>
<span>du montant souhaité est atteint</span>
</p>
<p>
<strong>'. $funding->getAmountDonated() . ' DU<sub>Ğ1</sub></strong>
<span>ont déjà donnés, sur un total de '. $target .' DU<sub>Ğ1</sub></span>
</p>
<p>
<span>grâce à </span>
<strong>'. $funding->getDonorsNb() . '</strong>
<span>donateurs</span>
</p>
</aside>
';
?>
<p>
Si vous souhaitez soutenir le projet Cesium, c'est simple :
</p>
<div id="pubkey-and-copy-button">
<p class="pubkey">
Copiez la clef suivante :
<input id="pubkey" type="text" value="<?php echo FUNDING_PUBKEY; ?>" size="8" />...
dans votre presse-papier en cliquant sur le bouton ci-dessous :
</p>
<p class="CTA-button">
<button id="copyButton">
Copier la clef
</button>
</p>
<div id="successMsg">
<p>Et maintenant collez-la dans Cesium afin de faire votre don 😉</p>
<p style="text-align: center;">Merci pour votre générosité ❤️</p>
<p style="text-align: right;">Axiom-Team</p>
</div>
</div>
<script>
function copy() {
var copyText = document.querySelector("#pubkey");
copyText.select();
document.execCommand("copy");
var successMsg = document.querySelector("#successMsg");
successMsg.style.opacity = "1";
/*successMsg.style.height = "3em";*/
var copyButton = document.querySelector("#copyButton");
copyButton.style.animation = "none";
}
function support() {
var pubkeyAndCopyButton = document.querySelector("#pubkey-and-copy-button");
var supportButtonContainer = document.querySelector("#supportButtonContainer");
supportButtonContainer.style.opacity = "0";
supportButtonContainer.style.height = "0";
pubkeyAndCopyButton.style.height = "100%";
pubkeyAndCopyButton.style.opacity = "1";
var supportButton = document.querySelector("#supportButton");
$(this).style.animation = "none";
}
document.querySelector("#copyButton").addEventListener("click", copy);
document.querySelector("#supportButton").addEventListener("click", support);
</script>
</body>
</html>
\ No newline at end of file
/* ======= Funding ======= */
#supportButtonContainer,
#projectStatus
{
height: 100%;
overflow: hidden;
transition-property: opacity, height;
transition-delay: 0s, 0.50s;
transition-duration: 0.49s, 0s;
}
#pubkey-and-copy-button
{
/*opacity: 0;
height: 0;*/
transition-property: opacity, height;
transition-delay: 0.51s, 0.51s;
transition-duration: 0.50s, 0s;
}
#pubkey
{
font-family: monospace;
padding: 8px 0;
border-radius: 5px;
border: transparent;
background: transparent;
font-size: 1rem;
}
#copyButton
{
}
#successMsg
{
opacity: 0;
/*height: 0;*/
line-height: 1.5em;
transition-property: opacity, height;
transition-duration: 1.5s, 0.5s;
}
input#pubkey
{
color: black;
}
input#pubkey::selection
{
color: black;
}
input##pubkey::selection
{
background: transparent;
}
input##pubkey:after
{
content: "...";
}
/*
#copyButton,
#supportButton
{
background: #111;
color: white;
border: 0;
font-weight: light;
height: 3rem;
line-height: 3rem;
font-size: 1rem;
width: 100%;
margin-bottom: 0.5rem;
}
*/
.crowdfunding-widget
{
margin: auto;
border: white;
background: white;
background: linear-gradient(0deg, hsl(39, 85.2%, 90%) 0%, hsl(39, 85.2%, 105%) 95%);
padding: 1em;
box-shadow: -2px 2px 5px black;
}
@media (min-width: 1200px) {
.crowdfunding-widget
{
max-width: 450px;
}
}
.crowdfunding-widget .progress-bar
{
width: 100%;
height: 0.5em;
background-color: red;
}
.crowdfunding-widget strong
{
display: block;
font-size: 2rem;
}
.crowdfunding-widget > p:nth-of-type(3) span:first-child
{
display: none;
}
<?php
/*
-----
Ce script est basé sur la "barre de financement intégrable" développée par Pierre-Jean Chancellier.
Son travail original se trouve ici :
https://git.duniter.org/paidge/barre-de-financement-int-grable
-----
This script is based on Pierre-Jean Chancellier's "barre de financement intégrable".
See original work at :
https://git.duniter.org/paidge/barre-de-financement-int-grable
*/
class Funding
{
private $units = ['quantitative','relative'];
private $unit = 'relative';
private $pubkeyFormat = '#^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{43,44}$#';
private $node = 'g1.duniter.org';
private $pubkey;
private $target;
private $startDate;
private $total = 0;
private $donorsNb = 0;
private $percentage;
public function __construct ($pubkey, $target, $startDate, $unit = 'relative')
{
if (!preg_match($this->pubkeyFormat, $pubkey)) {
echo "<p>La pubkey n'a pas le format attendu. Vérifiez votre syntaxe.</p>";
exit;
}
$this->pubkey = $pubkey;
$this->target = $target;
$this->startDate = $startDate;
$this->unit = $unit;
$this->computeAmountDonatedAndDonorsNb();
$this->percentage = round($this->total / $this->target * 100);
}
public function getPercentage ()
{
return $this->percentage;
}
public function getAmountDonated ()
{
return $this->total;
}
public function getDonorsNb ()
{
return $this->donorsNb;
}
private function computeAmountDonatedAndDonorsNb ()
{
$today = new DateTime();
$format = "d/m/Y";
function isDate(&$date, $format){
$champsDate = date_parse_from_format($format, $date);
$date = DateTime::createFromFormat($format, $date);
return checkdate($champsDate["month"], $champsDate["day"], $champsDate["year"]);
}
// Vérification des dates et calcul du nombre de jours entre la date du jour et la date de fin
if (!empty($this->startDate)){
$start_date = $this->startDate;
if (!isDate($start_date, $format)){
echo "<p>La date de début n'est pas correcte. Vérifiez votre syntaxe.</p>";
exit;
}
$start_date->sub(new DateInterval('P1D'));
} else {
echo "<p>Il manque la date de début. Vérifiez votre syntaxe.</p>";
exit;
}
// Récupération des transactions entrantes entre la date de début et la date du jour
$url_json = "https://" . $this->node . "/tx/history/" . $this->pubkey . "/times/" . $start_date->getTimestamp() . "/" . $today->getTimestamp();
$json = file_get_contents($url_json);
$json = json_decode($json);
$transactions = $json->history->received;
$donors = [];
$current_date = $start_date->format($format);
$array_dates = [$current_date];
$array_montants = [];
foreach ($transactions as $transaction){
$donor = $transaction->issuers[0];
if ($donor != $this->pubkey){
if(!in_array($donor, $donors)){
array_push($donors, $donor);
}
$outputs = $transaction->outputs;
foreach ($outputs as $output){
if (strstr($output,$this->pubkey)){
$timestamp = $transaction->blockstampTime;
$date_transaction = date('d/m/Y', $timestamp);
if ($date_transaction != $current_date){
array_push($array_dates, $date_transaction);
array_push($array_montants, $this->total);
$current_date = $date_transaction;
}
$output = explode(":", $output);
$montant = $output[0]/100;
$this->total += $montant;
}
}
}
}
array_push($array_montants, $this->total);
$this->donorsNb = count($donors);
if ($this->unit == 'relative')
{
$this->total = round($this->total / $this->getLastUDAmount());
}
}
public function setNode ($node)
{
$this->node = $node;
}
public function getLastUDAmount ()
{
// On récupère le dernier block qui contient le DU
$url_json = "https://" . $this->node . "/blockchain/with/ud";
$json = file_get_contents($url_json);
$json = json_decode($json);
$last_block_with_ud = end($json->result->blocks);
// Puis on récupère le montant du DU
$url_json = "https://" . $this->node . "/blockchain/block/" . $last_block_with_ud;
$json = file_get_contents($url_json);
$json = json_decode($json);
$ud = $json->dividend/100;
return $ud;
}
}
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