Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding missing PHP classes #11

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions php/src/ApiClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
namespace SomLabs\Intercoop\ApiClient;

namespace SomLabs\Intercoop;

use SomLabs\Intercoop\Crypto;
use SomLabs\Intercoop\Packaging;

function post_request($url, $params) {
// $query_content = http_build_query($params);
$fp = fopen($url, 'r', FALSE, // do not use_include_path
stream_context_create([
'http' => [
'header' => [ // header array does not need '\r\n'
'Content-type: application/yaml',
'Content-Length: ' . strlen($params)
],
'method' => 'POST',
'content' => $params
]
]));
if ($fp === FALSE) {
fclose($fp);
return json_encode(['error' => 'Failed to get contents...']);
}
$result = stream_get_contents($fp); // no maxlength/offset
fclose($fp);
return $result;
}

class ApiClient {
private $targetUrl;
private $keyfile;

public function __construct($apiurl, $key){
$this->targetUrl = $apiurl;
$this->keyfile = $key;
}

public function activateService($service, $personalData){
$package = new Packaging();
$privateKey = Crypto::loadKey($this->keyfile);
$package = $package->generate($privateKey, $personalData);
$response = post_request($this->targetUrl.'/activateService', $package);
return $response;
}
}
?>
45 changes: 45 additions & 0 deletions php/src/Catalog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
namespace SomLabs\Intercoop\Catalog;

class MessageError extends \Exception {
public function __construct(...$args) {
$message = sprintf($this->message, ...$args);
parent::__construct($message);
$this->arguments = $args;
}
}

class KeyNotExists extends MessageError {
protected $message = 'Given key not exists';
}

namespace SomLabs\Intercoop;

use SomLabs\Intercoop\ApiClient;

class Catalog {
public $peers_obj;
private $users_obj;
private $keyfile;

public function __construct($keyfile, $peers, $users){
if(file_exists($keyfile)){
$this->peers_obj = $peers;
$this->users_obj = $users;
$this->keyfile = $keyfile;
}else{
throw new Catalog\KeyNotExists("The given key does not exists");
}
}

public function activate($peer, $service, $user){
// fields = self.requiredFields(peer, service)
// data = self.users.getFields(user, fields)
$peerData = ($this->peers_obj)->getPeer($peer);
$data = ($this->users_obj)->getData($user);
$api = new ApiClient($peerData['targetUrl'], $this->keyfile);
$continuationUrl = $api->activateService($service, $data);
return $continuationUrl;
}
}
?>
2 changes: 1 addition & 1 deletion php/src/Crypto.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ static function uuid(){
}


// vim: noet ts=4 sw=4
// vim: noet ts=4 sw=4
4 changes: 1 addition & 3 deletions php/src/KeyRing.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ public function get(string $key) : RSA;
}

namespace SomLabs\Intercoop;

use phpseclib\Crypt\RSA;


class KeyRing implements KeyRing\KeyRingInterface{

public function __construct(array $keys) {
Expand All @@ -44,4 +42,4 @@ public function get(string $key) : RSA {
}


// vim: noet ts=4 sw=4
// vim: noet ts=4 sw=4
2 changes: 1 addition & 1 deletion php/src/Packaging.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,4 @@ static function parse(KeyRing\KeyRingInterface $keyring, $message) {
}


// vim: noet ts=4 sw=4
// vim: noet ts=4 sw=4
6 changes: 2 additions & 4 deletions php/src/PeerInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ class DatadirNotExists extends MessageError {
protected $message = 'Given datadir not exists';
}


namespace SomLabs\Intercoop;

use SomLabs\Intercoop\Crypto as crypto;
use SomLabs\Intercoop\KeyRing;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception as YamlException;


class PeerInfo {

private $peers=array();
Expand All @@ -30,7 +28,7 @@ public function __construct($datadir){
if(file_exists($datadir)){
$peerFiles= scandir($datadir);
foreach ($peerFiles as $peer) {
if(is_file($datadir.$peer)){
if(is_file($datadir.'/'.$peer)){
try {
$temp=Yaml::parse(file_get_contents($datadir."/".$peer));
$this->peers[$temp['peerid']] = $temp;
Expand Down Expand Up @@ -87,4 +85,4 @@ public function getService($peer,$service){
}


// vim: noet ts=4 sw=4
// vim: noet ts=4 sw=4
26 changes: 26 additions & 0 deletions php/src/PortalExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
// require_once('vendor/autoload.php');

use SomLabs\Intercoop\Catalog;
use SomLabs\Intercoop\Crypto;
use SomLabs\Intercoop\Packaging;
use SomLabs\Intercoop\KeyRing;
use SomLabs\Intercoop\PeerInfo;
use SomLabs\Intercoop\UserInfo;

$keyfile = dirname(__FILE__).'/../testkey.pem';
$peers = new PeerInfo(dirname(__FILE__).'/instance/sommobilitat/peers');
$users = new UserInfo(dirname(__FILE__).'/instance/sommobilitat/users');
$catalog = new Catalog($keyfile, $peers, $users);

$peer = "sommobilitat";
$service = "newUser";
$user = "
originpeer: wordpress
email: [email protected]
service_type: factures
";

$uuid = $catalog->activate($peer, $service, $user);
echo $uuid;
?>
75 changes: 75 additions & 0 deletions php/src/UserInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
namespace SomLabs\Intercoop\UserInfo;

class MessageError extends \Exception {
public function __construct(...$args) {
$message = sprintf($this->message, ...$args);
parent::__construct($message);
$this->arguments = $args;
}
}

class DatadirNotExists extends MessageError {
protected $message = 'Given datadir not exists';
}

namespace SomLabs\Intercoop;

use SomLabs\Intercoop\Crypto as crypto;
use SomLabs\Intercoop\KeyRing;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception as YamlException;


class UserInfo {

private $users=array();

public function __construct($datadir){
if(file_exists($datadir)){
$userFiles= scandir($datadir);
foreach ($userFiles as $user) {
if(is_file($datadir.'/'.$user)){
try {
$temp=Yaml::parse(file_get_contents($datadir."/".$user));
$this->user[$temp['userid']] = $temp;
} catch (ParseException $e) {
printf("Unable to parse YAML file: %s", $e->getMessage());
}
}
}
}else{
throw new UserInfo\DatadirNotExists("The given datadir of users does not exists");
}
}

public function constructor($datadir){

}

//get array of all peers
public function getUsers(){
return $this->users;
}

//get array of a specific peer
public function getUser($user){
$users = array();
foreach ($this->users as $key => $value) {
$users[]=$key;
}
if(!in_array($user,$users)){
throw new \Exception(sprintf('User %d does not exist', $user));
}
return $this->users[$user];
}

//parse yaml formatted user
public function getData($user){
return Yaml::parse($user);
}

}


// vim: noet ts=4 sw=4
16 changes: 16 additions & 0 deletions php/tests/ApiClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
use SomLabs\Intercoop\ApiClient;
use SomLabs\Intercoop\Test\AssertThrows;
use PHPUnit\Framework\TestCase;

class ApiClientTest extends TestCase{

use AssertThrows;

public function test(){
//
}

}

// vim: noet ts=4 sw=4
16 changes: 16 additions & 0 deletions php/tests/CatalogTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
use SomLabs\Intercoop\Catalog;
use SomLabs\Intercoop\Test\AssertThrows;
use PHPUnit\Framework\TestCase;

class CatalogTest extends TestCase{

use AssertThrows;

public function test(){
//
}

}

// vim: noet ts=4 sw=4
16 changes: 16 additions & 0 deletions php/tests/PortalExampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
use SomLabs\Intercoop\PortalExample;
use SomLabs\Intercoop\Test\AssertThrows;
use PHPUnit\Framework\TestCase;

class PortalExampleTest extends TestCase{

use AssertThrows;

public function test(){
//
}

}

// vim: noet ts=4 sw=4
25 changes: 25 additions & 0 deletions php/tests/UserInfoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
use SomLabs\Intercoop\UserInfo;
use SomLabs\Intercoop\Test\AssertThrows;
use PHPUnit\Framework\TestCase;

class UserInfoTest extends TestCase{

use AssertThrows;

private $datadir = "instance/somillusio/users/";

public function test_datadir_invalid(){
$this->assertThrows(function() {
$users = new UserInfo('ffhf');
},
SomLabs\Intercoop\UserInfo\DatadirNotExists::class,
"Given datadir not exists"
);
}



}

// vim: noet ts=4 sw=4