commit be7c68a8db9402f1b2eddffa1e8d46deeabea821 Author: Boris Paing Date: Thu Dec 10 15:49:47 2020 +0100 proto diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/cache/nodes-gchange.txt b/cache/nodes-gchange.txt new file mode 100644 index 0000000..489bb16 --- /dev/null +++ b/cache/nodes-gchange.txt @@ -0,0 +1 @@ +data.gchange.fr \ No newline at end of file diff --git a/config.php b/config.php new file mode 100644 index 0000000..2f97178 --- /dev/null +++ b/config.php @@ -0,0 +1,9 @@ + + diff --git a/functions.php b/functions.php new file mode 100644 index 0000000..471cc92 --- /dev/null +++ b/functions.php @@ -0,0 +1,35 @@ + + + +gMaville + + + + +

gMaville - Toutes les infos pertinentes pour vous

diff --git a/index.php b/index.php new file mode 100644 index 0000000..51cc34e --- /dev/null +++ b/index.php @@ -0,0 +1,25 @@ +Où êtes-vous ? + +
+

+ +

+

+ +

+
'; +} + +include('footer.php'); + + diff --git a/lib/CesiumPlus.class.php b/lib/CesiumPlus.class.php new file mode 100644 index 0000000..42fcd73 --- /dev/null +++ b/lib/CesiumPlus.class.php @@ -0,0 +1,50 @@ +dao = DAO::getInstance(); + } + + public function getNearbyUsers ($lat, $lon, $radius) { + + $n = 15; + + $queryParams = [ + 'size' => $n, + + 'query' => [ + 'bool' => [ + 'must' => [ + [ + 'geo_distance' => [ + + "distance" => $radius, + "geoPoint"=> [ + "lat" => $lat, + "lon" => $lon + ] + ] + ] + ] + ] + ], + + "sort" => [ + [ "time" => ["order" => "desc"] ], + "_score" + ] + + ]; + + $json = $this->dao->fetchJson('/user/profile/_search?pretty', 'gchange', $queryParams); + $result = json_decode($json); + + return $result->hits->hits; + } +} diff --git a/lib/DAO.class.php b/lib/DAO.class.php new file mode 100755 index 0000000..b170d7b --- /dev/null +++ b/lib/DAO.class.php @@ -0,0 +1,586 @@ + [ + + 'data.gchange.fr' + ], + + 'cesiumplus' => [ + + 'g1.data.le-sou.org', + 'g1.data.duniter.fr' + ], + + 'duniter' => [ + + 'duniter.g1.1000i100.fr', + 'duniter-g1.p2p.legal', + 'duniter.normandie-libre.fr', + 'g1.mithril.re', + 'g1.presles.fr', + 'duniter.vincentux.fr', + 'g1.le-sou.org', + 'g1.donnadieu.fr', + ] + ]; + + private $nodeTimeout = [ + + 'duniter' => 2, + 'cesiumplus' => 5, + 'gchange' => 5, + ]; + + private $nodeTimeoutIncrement = [ + + 'duniter' => 2, + 'cesiumplus' => 10, + 'gchange' => 10 + ]; + + private $node = NULL; + + private $unit = 'quantitative'; + + + /********************** + * Methods + **********************/ + + public function __construct () { + + } + + public function getInstance () { + + if (!isset(DAO::$dao)) { + + DAO::$dao = new DAO(); + } + + return DAO::$dao; + } + + private function setUnit ($unit) { + + if (!empty($unit)) { + + if (!in_array($unit, $this->units)) { + + $out = []; + $out[] = _('L\'unité renseignée n\'existe pas.'); + $out[] = _('Vérifiez votre synthaxe.'); + + $this->decease($out); + + } else { + + $this->unit = $unit; + } + } + } + + public function decease ($errorMsgs) { + + if (!is_array($errorMsgs)) { + + $errorMsgs = explode("\n", $errorMsgs); + } + + + if ($this->displayType == 'img') { + + $source = imagecreatetruecolor(500, 200); + + $bgColor = imagecolorallocate($source, + 255, 255, 255); + + imagefill($source, + 0, 0, + $bgColor); + + $txtColor = imagecolorallocate($source, + 0, 0, 0); + + $errorMsgFontSize = 3; + $x = 5; + $y = 5; + + foreach ($errorMsgs as $msg) { + + imagestring($source, $errorMsgFontSize, $x, $y, utf8_decode($msg), $txtColor); + + $y += $errorMsgFontSize + 20; + } + + + imagepng($source); + imagedestroy($source); + + } else if ($this->displayType == 'svg') { + + echo ' + + + '; + + $x = 25; + $y = 25; + + foreach ($errorMsgs as $msg) { + + echo ' + + '. $msg . ' + '; + + $y += 25; + } + + echo ' + + '; + + } else { + + ob_get_clean(); // to prevent error message to display inside an HTML container (case of error generated by get method calls) + + echo ' + + + + '. _('Erreur critique') . ' + + + + + +
'; + + + foreach ($errorMsgs as $msg) { + + echo '

' . $msg . '

'; + } + + echo ' +
+ + '; + } + + exit; + } + + public function printUnit () { + + if ($this->unit == 'relative') { + + if ($this->displayType == 'img') { + + return _('DUĞ1'); + + } else { + + return _('DUĞ1'); + } + + } else { + + return _('Ğ1'); + } + } + + public function convertIntoChosenUnit ($amountInQuantitative) { + + if ($this->unit == 'quantitative') { + + return $amountInQuantitative; + + } else { + + if (!isset($this->startDateUdAmount)) { + + $this->startDateUdAmount = $this->getUdAmount($this->startDate); + } + + return round($amountInQuantitative / $this->startDateUdAmount, 2); + } + } + + + + public function addNode ($node) { + + $node = htmlspecialchars($node); + + $this->nodes = array_unique( + array_merge( + (array)$node, + $this->nodes + ) + ); + } + + + + public function addNodes ($nodes) { + + if (!is_array($nodes)) { + + $nodes = explode(' ', $nodes); + } + + foreach ($nodes as $node) { + + $this->addNode($node); + } + + } + + /** + * @return $nodes array + */ + public function getNodesList ($nodeType = 'duniter') { + + switch ($nodeType) { + + case 'gchange': + $nodesFilename = 'nodes-gchange'; + break; + case 'cesiumplus': + $nodesFilename = 'nodes-cesiumplus'; + break; + default: + $nodesFilename = 'nodes'; + break; + } + + $nodesFilename .= '.txt'; + $nodesFullpath = $this->cacheDir . $nodesFilename; + + $nodes = $this->nodes[$nodeType]; + + if ($this->isActivatedCache) { + + if (!file_exists($nodesFullpath)) { + + shuffle($nodes); + + $this->cacheNodes($nodes, $nodeType); + + + } else { + + $nodesStr = file_get_contents($nodesFullpath); + + $nodes = explode("\n", $nodesStr); + } + + } else { + + shuffle($nodes); + + } + + return $nodes; + } + + protected function cacheNodes ($nodes, $nodeType = 'duniter') { + + switch ($nodeType) { + + case 'gchange': + $nodesFilename = 'nodes-gchange'; + break; + case 'cesiumplus': + $nodesFilename = 'nodes-cesiumplus'; + break; + default: + $nodesFilename = 'nodes'; + break; + } + $nodesFilename .= '.txt'; + + if (!file_exists($this->cacheDir)) { + + mkdir($this->cacheDir, 0777, true); + + } + + file_put_contents($this->cacheDir . $nodesFilename, implode("\n", $nodes)); + } + + protected function saveNodes ($nodes, $nodeType = 'duniter') { + + $this->nodes[$nodeType] = $nodes; + } + + protected function fetchJson_aux ($nodes, $uri, $nodeType, $queryParams, $nodesNb, $nodeTimeout) { + + // $header = 'Content-Type: application/x-www-form-urlencoded'; + // $header = "Content-Type: text/xml\r\n"; + + if (!empty($queryParams)) { + + $opts = [ + 'http' => [ + 'method' => 'POST', + 'content' => json_encode($queryParams), + // 'header' => $header, + 'timeout' => $nodeTimeout + ] + ]; + + } else { + + $opts = [ + 'http' => [ + 'method' => 'GET', + 'timeout' => $nodeTimeout + ] + ]; + + } + + + $streamContext = stream_context_create($opts); + + $i = 0; + + do { + + + $json = @file_get_contents("https://" . current($nodes) . $uri, + false, + $streamContext); + + if (empty($json)) { + + $nodes[] = array_shift($nodes); + ++$i; + } + + } while (empty($json) and ($i < $nodesNb)); + + if (!empty($json)) { + + // Let's save node order for other queries : + $this->saveNodes($nodes, $nodeType); + + if ($this->isActivatedCache) { + + $this->cacheNodes($nodes, $nodeType); + } + } + + return $json; + } + + + public function fetchJson ($uri, $nodeType = 'duniter', $queryParams = NULL) { + + $json = NULL; + + $nodes = $this->getNodesList($nodeType); + + $nodesNb = count($nodes); + + $maxTries = 3; + + $nodeTimeout = $this->nodeTimeout[$nodeType]; + $nodeTimeoutIncrement = $this->nodeTimeoutIncrement[$nodeType]; + + for ($i = 0; ($i < 3) and empty($json); ++$i) { + + $json = $this->fetchJson_aux($nodes, $uri, $nodeType, $queryParams, $nodesNb, $nodeTimeout); + + $nodeTimeout += $nodeTimeoutIncrement; + } + + if (empty($json)) { + + $out = []; + $out[] = _('Aucun noeud '. $nodeType .' n\'a été trouvé.'); + $out[] = _('Noeud interrogés : '); + + if (isset($queryParams)) { + + $out[] = _('Paramètres de la requête : '); + $out[] = print_r($queryParams, true); + } + + $out = array_merge($out, $nodes); + + $this->decease($out); + } + + return $json; + } + + protected function fetchUdAmount ($date) { + + // On récupère les numéros de chaque blocks de DU journalier + $json = $this->fetchJson('/blockchain/with/ud'); + $blocks = json_decode($json)->result->blocks; + + if ($date > $this->now) { + + // On récupère le dernier block + $blockNum = end($blocks); + + } else { + + // On récupère le bloc de la date qui nous intéresse + $blockNum = $blocks[count($blocks) - $this->today->diff($date)->format("%a") - 1]; + } + + // Puis on récupère le montant du DU + $json = $this->fetchJson('/blockchain/block/' . $blockNum); + $block = json_decode($json); + + + return ($block->dividend / 100); + } + + public function getUdAmount ($date) { + + $udFilename = $this->getUdFilename($date); + $udsCacheDir = $this->cacheDir . 'uds/'; + $udFullPath = $udsCacheDir . $udFilename; + + if ($this->isActivatedCache) { + + if (file_exists($udFullPath)) { + + $udCachedAmount = file_get_contents($udFullPath); + + if (is_numeric($udCachedAmount) and $udCachedAmount != 0) { + + $udAmount = floatval($udCachedAmount); + } + } + + + + if (!isset($udAmount)) { + + $udAmount = $this->fetchUdAmount($date); + + // Cache UD amount + + if (!file_exists($udsCacheDir)) { + + mkdir($udsCacheDir, 0777, true); + + } + + file_put_contents($udFullPath, $udAmount); + + } + + + } else { + + $udAmount = $this->fetchUdAmount($date); + + } + + return $udAmount; + } + + + protected function getUdFilename ($date) { + + $datePreviousAutumnEquinox = new DateTime($date->format('Y') . '-09-22'); + $datePreviousSpringEquinox = new DateTime($date->format('Y') . '-03-20'); + + if ($date > $datePreviousAutumnEquinox) { + + $udFilename = $date->format('Y') . '-autumn'; + + } elseif ($date > $datePreviousSpringEquinox) { + + $udFilename = $date->format('Y') . '-spring'; + + } else { + + $udFilename = ($date->sub(new DateInterval('P1Y'))->format('Y')). '-autumn'; + } + + return $udFilename . '.txt'; + + } + + +} diff --git a/lib/Gchange.class.php b/lib/Gchange.class.php new file mode 100644 index 0000000..bbb37d1 --- /dev/null +++ b/lib/Gchange.class.php @@ -0,0 +1,110 @@ +dao = DAO::getInstance(); + } + + public function getNearbyOffers ($lat, $lon, $max, $min = NULL) { + + $n = 20; + + $queryParams = [ + 'size' => $n, + 'query' => [ + 'bool' => [ + 'must' => [ + [ + 'geo_distance' => [ + + "distance" => "50km", + "geoPoint"=> [ + "lat" => $lat, + "lon" => $lon + ] + ] + ], [ + 'range' => [ + 'stock' => [ + 'gte' => 1 + ] + ] + ] + ] + ] + ] + ]; + + $json = $this->dao->fetchJson('/market/record/_search?pretty', 'gchange', $queryParams); + $result = json_decode($json); + + return $result->hits->hits; + } + + public function getImmaterialOffers () { + + $n = 20; + + $queryParams = [ + 'size' => $n, + 'query' => [ + 'nested' => [ + 'path' => 'category', + 'query' => [ + 'bool' => [ + 'should' => [ + [ 'term' => [ 'category.parent' => 'cat31' ] ], + [ 'term' => [ 'category.id' => 'cat31' ] ], + [ 'term' => [ 'category.parent' => 'cat74' ] ], + [ 'term' => [ 'category.id' => 'cat74' ] ] + ], + 'must_not' => [ + [ "term" => ["category.id" => "cat65"] ] + ] + ] + ] + ] + ] + ]; + + $json = $this->dao->fetchJson('/market/record/_search?pretty', 'gchange', $queryParams); + $result = json_decode($json); + + return $result->hits->hits; + } + + public function getShippable () { + + $n = 20; + + $queryParams = [ + 'size' => $n, + 'query' => [ + 'match' => [ + 'description' => 'envoi possible' + ] + ] + ]; + + $json = $this->dao->fetchJson('/market/record/_search?pretty', 'gchange', $queryParams); + $result = json_decode($json); + + return $result->hits->hits; + } + + public function getHousingOffers () { + + + } + + public function getShippableOffers () { + + + } +} diff --git a/mytown.php b/mytown.php new file mode 100644 index 0000000..abebfcf --- /dev/null +++ b/mytown.php @@ -0,0 +1,157 @@ + array( + "header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36" + ) + ) + ); + + + $url = 'http://nominatim.openstreetmap.org/search?q='. urlencode($_POST['city']) . '&format=json'; + + $citiesJson = file_get_contents($url, false, $streamContext); + $cities = json_decode($citiesJson); + + $_SESSION['lat'] = $cities[0]->lat; + $_SESSION['lon'] = $cities[0]->lon; + $_SESSION['city'] = htmlspecialchars($_POST['city']); + +} + + + +include('header.php'); + + +echo '

Événements à venir près de '. $_SESSION['city'] . '

'; + +echo '

Pas d\'événement prévu en ce moment.

'; + + + +echo '

Contacter d\'autres junistes près de '. $_SESSION['city'] . '

'; + +echo ' +

+ + Groupe de discussion par mail du groupe de '. $_SESSION['city']. ' + +

'; + + +$users = $cesiumPlus->getNearbyUsers($_SESSION['lat'], $_SESSION['lon'], RADIUS); + +echo '

Nouveaux utilisateurs à '. RADIUS . ' autour de '. $_SESSION['city'] .'

'; + +echo ''; + foreach ($users as $user) { + + $date = new DateTime(); + $date->setTimestamp($user->_source->time); + + echo ' + + + + + + + '; + } +echo '
'. $date->format('j') . ' ' . moisFr($date->format('n')) . ' ' . $date->format('Y') . ' + + ' . $user->_source->title . ' + +
'; + + +$offers = $gchange->getNearbyOffers($_SESSION['lat'], $_SESSION['lon'], RADIUS); + +echo '

Annonces à '. RADIUS . ' autour de '. $_SESSION['city'] .'

'; +echo '
    '; + foreach ($offers as $offer) { + + $description = isset($offer->_source->description) ? ' title="'. substr($offer->_source->description, 0, 30) . '"' : ''; + + echo ' + +
  1. + + ' . $offer->_source->title . ' + + ('. $offer->_source->city . ') +
  2. + + '; + } +echo '
'; + + + + +$offers = $gchange->getShippable(); + +echo '

Envoi possible

'; +echo '
    '; + foreach ($offers as $offer) { + + $description = isset($offer->_source->description) ? ' title="'. substr($offer->_source->description, 0, 30) . '"' : ''; + + echo ' + +
  1. + + ' . $offer->_source->title . ' + +
  2. + + '; + } +echo '
'; + + + +$offers = $gchange->getImmaterialOffers(); + +echo '

Annonces d\'ordre "immatériel"

'; +echo '
    '; + foreach ($offers as $offer) { + + $description = isset($offer->_source->description) ? ' title="'. substr($offer->_source->description, 0, 30) . '"' : ''; + + echo ' + +
  1. + + ' . $offer->_source->title . ' + +
  2. + + '; + } +echo '
'; + + + + +include('footer.php'); + + +