zeg1jeux/lib/Jaklis.class.php

145 lines
3.0 KiB
PHP

<?php
require_once('Fred.class.php');
class Jaklis {
private $mode;
private $jaklisPath = __DIR__ . '/../vendors/jaklis/jaklis';
// private $jaklisPath = 'jaklis'; // if you use Docker
private $nodes = [
'gchange' => 'data.gchange.fr'
];
private $msgLimit = 15;
private $pubsecDir = __DIR__ .'/../cache/pubsec/';
private $userPubsecPath;
public function __construct ($userPubkey, $mode = 'local') {
$this->userPubsecPath = $this->pubsecDir . $userPubkey .'.dunikey';
if ($this->mode != 'local') {
$this->fred = new Fred();
}
}
public function getMessages () {
if ($this->mode = 'local') {
try {
$msg_in = $this->getInboundMessages();
$msg_out = $this->getOutboundMessages();
} catch (Exception $errMsg) {
throw new Exception($errMsg);
}
return [$msg_in, $msg_out];
} else {
$this->fred->donneMoiSesPutainDeMessagesGchange($_SESSION['salt'], $_SESSION['pepper'], 'coucou');
}
}
public function getInboundMessages () {
$cmd = $this->jaklisPath;
$cmd .= ' --key '. $this->userPubsecPath;
$cmd .= ' --node https://'. $this->nodes['gchange'];
$cmd .= ' read';
$cmd .= ' --number ' . $this->msgLimit;
$cmd .= ' --json';
$output=null;
$result_code=null;
exec($cmd, $output, $result_code);
if (empty($output)) {
throw new Exception('Jaklis marche pô pour les messages entrants.');
}
$json = implode("\n", $output);
// echo '<p>' . $cmd . '</p>';
// echo '<pre>'; var_dump($json); echo '</pre>';
$result = json_decode($json);
// echo '<pre>'; var_dump($result); echo '</pre>';
return $result;
}
public function getOutboundMessages () {
$cmd = $this->jaklisPath;
$cmd .= ' --key '. $this->userPubsecPath;
$cmd .= ' --node http://'. $this->nodes['gchange'];
$cmd .= ' read';
$cmd .= ' --number ' . $this->msgLimit;
$cmd .= ' --json';
$cmd .= ' --outbox';
$output=null;
$result_code=null;
exec($cmd, $output, $result_code);
if (empty($output)) {
throw new Exception('Jaklis marche pô pour les messages sortants.');
}
$json = implode("\n", $output);
// echo '<p>' . $cmd . '</p>';
// echo '<pre>'; var_dump($json); echo '</pre>';
$result = json_decode($json);
// echo '<pre>'; var_dump($result); echo '</pre>';
return $result;
}
private function jescape ($str) {
return str_replace('"', '\"', $str);
}
public function sendMessage ($msg, $toPubkey, $title, $fromPubkey) {
$cmd = $this->jaklisPath;
$cmd .= ' --key '. $this->userPubsecPath;
$cmd .= ' --node http://'. $this->nodes['gchange'];
$cmd .= ' send';
$cmd .= ' --destinataire "' . $this->jescape($toPubkey) . '"';
$cmd .= ' --titre "' . $this->jescape($title) . '"';
$cmd .= ' --message "' . $this->jescape($msg) . '"';
// echo '<pre>'. $cmd . '</pre>';
$output=null;
$result_code=null;
exec($cmd, $output, $result_code);
// echo '<pre>'; var_dump($result_code); echo '</pre>';
// echo '<pre>'; var_dump($output); echo '</pre>';
// list($jaklisResult, $id) = $output;
// echo '<p>'. $jaklisResult .'</p>';
}
}