Ver Fonte

add github sponsors to about page

CauseFX há 5 anos atrás
pai
commit
83ecfda6ef

+ 61 - 0
api/classes/organizr.class.php

@@ -4934,6 +4934,9 @@ class Organizr
 		$response = Requests::get($url, array(), $options);
 		if ($response->success) {
 			$api = json_decode($response->body, true);
+			foreach ($api as $k => $backer) {
+				$api[$k] = array_merge($api[$k], ['sortName' => strtolower($backer['name'])]);
+			}
 			$this->setAPIResponse('success', '', 200, $api);
 			return $api;
 		}
@@ -4941,6 +4944,64 @@ class Organizr
 		return false;
 	}
 	
+	public function getGithubSponsors()
+	{
+		$url = 'https://github.com/sponsors/causefx';
+		$options = ($this->localURL($url)) ? array('verify' => false) : array();
+		$response = Requests::get($url, array(), $options);
+		if ($response->success) {
+			$sponsors = [];
+			$dom = new PHPHtmlParser\Dom;
+			try {
+				$dom->loadStr($response->body);
+				$contents = $dom->find('#sponsors .clearfix div');
+				foreach ($contents as $content) {
+					$html = $content->innerHtml;
+					preg_match('/(@[a-zA-Z])\w+/', $html, $username);
+					preg_match('/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'\".,<>?«»“”‘’]))/', $html, $image);
+					if (isset($image[0]) && isset($username[0])) {
+						$sponsors[] = [
+							'name' => str_replace('@', '', $username[0]),
+							'sortName' => str_replace('@', '', strtolower($username[0])),
+							'image' => str_replace('s=60', 's=200', $image[0]),
+							'isActive' => true,
+							'type' => 'USER',
+							'role' => 'BACKER'
+						];
+					}
+				}
+				$this->setAPIResponse('success', '', 200, $sponsors);
+				return $sponsors;
+			} catch (\PHPHtmlParser\Exceptions\ChildNotFoundException | \PHPHtmlParser\Exceptions\CircularException | \PHPHtmlParser\Exceptions\LogicalException | \PHPHtmlParser\Exceptions\StrictException | \PHPHtmlParser\Exceptions\ContentLengthException | \PHPHtmlParser\Exceptions\NotLoadedException $e) {
+				$this->setAPIResponse('error', 'Error connecting to Github', 409);
+				return false;
+			}
+		}
+		$this->setAPIResponse('error', 'Error connecting to Github', 409);
+		return false;
+	}
+	
+	public function getAllSponsors()
+	{
+		$sponsors = [];
+		$list = [
+			'openCollective' => $this->getOpenCollectiveBackers(),
+			'github' => $this->getGithubSponsors()
+		];
+		foreach ($list as $k => $sponsor) {
+			if ($sponsor) {
+				$sponsors = array_merge($sponsor, $sponsors);
+			}
+		}
+		if ($sponsors) {
+			usort($sponsors, function ($a, $b) {
+				return $a['sortName'] <=> $b['sortName'];
+			});
+		}
+		$this->setAPIResponse('success', '', 200, $sponsors);
+		return $sponsors;
+	}
+	
 	public function getOrganizrSmtpFromAPI()
 	{
 		$url = 'https://api.organizr.app/?cmd=smtp';

+ 0 - 9
api/v2/routes/opencollective.php

@@ -1,9 +0,0 @@
-<?php
-$app->get('/opencollective', function ($request, $response, $args) {
-	$Organizr = ($request->getAttribute('Organizr')) ?? new Organizr();
-	$Organizr->getOpenCollectiveBackers();
-	$response->getBody()->write(jsonE($GLOBALS['api']));
-	return $response
-		->withHeader('Content-Type', 'application/json;charset=UTF-8')
-		->withStatus($GLOBALS['responseCode']);
-});

+ 25 - 0
api/v2/routes/sponsors.php

@@ -0,0 +1,25 @@
+<?php
+$app->get('/sponsors/opencollective', function ($request, $response, $args) {
+	$Organizr = ($request->getAttribute('Organizr')) ?? new Organizr();
+	$Organizr->getOpenCollectiveBackers();
+	$response->getBody()->write(jsonE($GLOBALS['api']));
+	return $response
+		->withHeader('Content-Type', 'application/json;charset=UTF-8')
+		->withStatus($GLOBALS['responseCode']);
+});
+$app->get('/sponsors/github', function ($request, $response, $args) {
+	$Organizr = ($request->getAttribute('Organizr')) ?? new Organizr();
+	$Organizr->getGithubSponsors();
+	$response->getBody()->write(jsonE($GLOBALS['api']));
+	return $response
+		->withHeader('Content-Type', 'application/json;charset=UTF-8')
+		->withStatus($GLOBALS['responseCode']);
+});
+$app->get('/sponsors/all', function ($request, $response, $args) {
+	$Organizr = ($request->getAttribute('Organizr')) ?? new Organizr();
+	$Organizr->getAllSponsors();
+	$response->getBody()->write(jsonE($GLOBALS['api']));
+	return $response
+		->withHeader('Content-Type', 'application/json;charset=UTF-8')
+		->withStatus($GLOBALS['responseCode']);
+});

+ 1 - 1
js/functions.js

@@ -3562,7 +3562,7 @@ function sponsorLoad(){
     });
 }
 function backersLoad(){
-	organizrAPI2('GET','api/v2/opencollective').success(function(data) {
+	organizrAPI2('GET','api/v2/sponsors/all').success(function(data) {
 		try {
 			let json = data.response;
 			$('#backersList').html(buildBackers(json.data));