Skip to content

Commit

Permalink
Main php client code commit with demo account.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Squire committed Apr 13, 2012
1 parent 3fdbc90 commit 70a72cb
Show file tree
Hide file tree
Showing 534 changed files with 43,722 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/phpclient/SocialHelpers.php
@@ -0,0 +1,36 @@
<?php

class SocialHelpers extends HamlHelpers {

public static function link_to($block, $atts, $display) {
$link = "<a";
foreach($atts as $att => $value) {
$link .= ' ' . $att . '="' . $value . '"';
}
$link .= '>' . $display . '</a>';
return $link;
}


public static function person_link($block, $display, $guid) {
$data = array(
'href'=> page_filename(SECTION_USERPROFILE) . '?' . $guid,
'title' => 'written by ' . $display
);
return self::link_to($block, $data, $display);
}

public static function userprofile($block, $guid) {
$udb = StaticAccounts::getUDB();
$profile = $udb->getProfile($guid);
return $profile;
}

public static function profile($block, $guid, $data) {
$profile = self::userprofile($block, $guid);
return $profile->$data;
}

}

?>
137 changes: 137 additions & 0 deletions src/phpclient/accountlayer.php
@@ -0,0 +1,137 @@
<?php

require_once('static.php');
require_once('data.php');
require_once('storage.php');
require_once('profilesaver.php');
require_once('crypto.php');

interface AccountLayer {
public function getAccount($guid);
}

/**
Lookup accounts.
*/

class Accounts implements AccountLayer {
public $saver;
public $storage;
public $userinstall;

public function __construct($storage, $userinstall) {
$this->storage = $storage;
$this->saver = new ProfileSaver($this);
$this->userinstall = $userinstall;
}

public function getAccount($guid) {
$accfile = user_file($guid);
if (file_exists($accfile)) {
$doc = new DOMDocument();
$text = file_get_contents($accfile);
$doc->loadXML($text);
// create an account object
$account = Account::fromXML($doc->documentElement);
return $account;
} else {
return FALSE;
}
}

public function login($guid, $password) {
$fetch = $this->getAccount($guid);
// no such account exists
if ($fetch === FALSE) {
return LOGIN_ERROR;
}
$account = $fetch;
$privatekey = openssl_pkey_get_private($account->pkey, $password);
// password is wrong
if ($privatekey === FALSE) {
return LOGIN_ERROR;
}
return TRUE;
}

/**
Fetch the profile of a user using their GUID. This user may be a remote
or a local account.
*/
public function getProfile($guid) {
$localsearch = $this->getAccount($guid);
// this account is local to this server
if ($localsearch !== FALSE) {
return $localsearch->profile;
}
return FALSE;
}

/**
Add a newly discovered profile.
*/
public function addProfile($profile) {
echo "saving profile ", $profile->guid," to database";
$change = new KnownProfile($profile);
$this->storage->insert($change);
}

/**
Creates an account:
- saves the account file to the filesystem
- saves it to the database
*/
public function save($account, $password) {
// creates account files, keys and saves to filesystem
$this->saver->save($account, $password);
// save knowledge of the profile to the database
$this->addProfile($account->profile);
}

public function getKnownNames() {
$names = array();
$known = new KnownProfile();
return $this->storage->get($known);
}

public function getUserStorage($guid) {
$userdata = new UserStorage($guid, $this->userinstall);
return $userdata;
}

/**
Process the waiting packets for a user.
*/
public function processWaiting($guid, $password) {
$security = new Security($this);
$userdata = $this->getUserStorage($guid);
if ($userdata === FALSE) {
return;
}
$account = $this->getAccount($guid);

$waiting = new IncomingEncryptedPacket();
$packets = $waiting->getWaitingPackets($userdata);

// no packets to process
if ($packets === FALSE) {
return;
}

// decrypt each packet
foreach($packets as $packet) {
$packetguid = $packet['packet'];
$decrypted = $security->decryptPacket($packetguid, $account, $password);
if ($decrypted === TRUE) {
$waiting->data['packet'] = $packetguid;
// delete the record
$waiting->update($userdata);
}
}

}
}



?>
3 changes: 3 additions & 0 deletions src/phpclient/base64.php
@@ -0,0 +1,3 @@
<?php
echo base64_decode("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCtkWpracQ+Q1sypeNXkjeL1Ppo Vg3xSgFXrmmH7xOfidnyW0YTHCXmiFA4jC8Hzj7K0ncRavzoL02bimd6rpnffzPX QPl9jEWVG7UooEZRv69FeAzh8/wPThzmhLj9/LzAfGfIyAhabzHsegkRz67oV21h PqquRF8ZeEAyZoKdtwIDAQAB");
?>
158 changes: 158 additions & 0 deletions src/phpclient/core.php
@@ -0,0 +1,158 @@
<?php

require_once('static.php');
require_once('storage.php');
require_once('accountlayer.php');
require_once('packetfetcher.php');

/**
The CORE is responsible for reading inpackets and acting upon them.
*/

class Core {
// the server's storage mechanism
public $serverdata;
// the folder to read for incoming packets
public $in;
// the folder to place outgoing packets, should they be generated
public $out;
// the parser for reading packet files (xml->objects)
public $parser;
// the account layer
public $udb;

public function __construct($serverdata, $udb, $in, $out) {
$this->serverdata = $serverdata;
$this->udb = $udb;
$this->parser = new PacketParser();
$this->in = $in;
$this->out = $out;
}
/**
Handle a packet.
*/
public function handlePacket($packet) {
echo get_class($packet);
// get the user datastore
$recipients = $packet->recipients;
$udb = $this->udb;

foreach($recipients as $recipient) {
$userdata = $udb->getUserStorage($recipient);
echo "<br><strong>Connected to user storage for ", $recipient,"<br></strong>";

$update = new IncomingPacket($packet);
$update->insert($userdata);
}

echo "Processing packet","</br>";
var_dump($packet);
echo '<pre>',htmlspecialchars($packet->toXMl()->ownerDocument->saveXML()),'</pre>';
echo "<br>",var_dump($packet->fromfile);
if ($packet->fromfile) {
echo $packet->filename,"<br>";
}


}

/**
Process a folder and handle the packets within the folder.
*/
public function processFolder() {
$parser = $this->parser;
$newPackets = array();

// parse this folder
$pf = new PacketFetcher($this->in);
$packets = $pf->getPacketFiles();

foreach($packets as $packetfile) {
$multipackets = $parser->parse($this->in . $packetfile);
$newPackets = array_merge($newPackets, $multipackets);
}

foreach($newPackets as $aPacket) {
$this->handlePacket($aPacket);
unlink($aPacket->filename);
}
}

/**
Determines the proper path of a file depending on its filename.
The input array should be in the format:
$realfilename => $curpath
The new path is then created and an array returned that is indexed
with the old filename like:
$curpath => $newpath
This way you can loop over the result set and do some move operations
yourself or even create the files if you have not created them yet.
*/
public function handleReceivedFiles($files) {
$newdest = array();

// decide where received files should go depending
// on filename
foreach($files as $name => $curpath) {

$dest = "";
if (is_encrypted($name)) {
$dest = ENCRYPTED_INPACKETS_DIR . $name;

} else if (is_keyfile($name)) {
$data = encryptedpacketdata($name);

$recipient = $data['recipientguid'];
$packetguid = $data['packetguid'];

$dir = encryptedinpacketdir($recipient);
if (file_exists($dir) === FALSE) {
mkdir($dir);
}
$dest = $dir . $name;

$userdata = $this->udb->getUserStorage($recipient);
$waiting = new IncomingEncryptedPacket($packetguid);
$waiting->insert($userdata);
} else {
// un encrypted
$dest = INPACKETS_DIR . $name;
}
$newdest[$curpath] = $dest;
}

return $newdest;
}


/**
Accepts an array of uploaded files in the array format:
$currentfilename => $newfilename
*/
public function handleUploadedFiles($received) {
$newpaths = $this->handleReceivedFiles($received);

foreach($newpaths as $tmp => $newpath) {
echo "Received file: ", $newpath," moving from ", $tmp, "<br>";
var_dump(move_uploaded_file($tmp, $newpath));
}
}

/**
This user is requesting remote packets if they are available.
*/

public function checkForRemotePackets($guid) {
$account = $this->udb->getAccount($guid);

$remote = new Receiver($account);

$remote->receive($this);
}
}



?>

0 comments on commit 70a72cb

Please sign in to comment.