Alpha version

This commit is contained in:
Juan Ferrer Toribio 2016-08-25 12:47:09 +02:00
parent 1c5612a840
commit 25ddb17f35
32 changed files with 134 additions and 123 deletions

View File

@ -6,7 +6,7 @@ Vn.Contact = new Class
,activate: function ()
{
var self = this;
var form = this.$('contact-form').onsubmit = function ()
this.$('contact-form').onsubmit = function ()
{ self.onSubmit (); return false; };
this.refreshCaptcha ();

View File

@ -1,9 +1,8 @@
<?php
require_once ('vn/hedera/hedera.php');
require_once ('vn/lib/cli-app.php');
$cliApp = new Vn\Lib\CliApp (Vn\Hedera\APP_NAME, 'rest');
$cliApp = new Vn\Lib\CliApp ('hedera-web', 'rest');
$cliApp->run ();
?>

View File

@ -1,9 +1,8 @@
<?php
require_once ('vn/hedera/hedera.php');
require_once ('vn/web/html-app.php');
$htmlApp = new Vn\Web\HtmlApp (Vn\Hedera\APP_NAME, 'pages');
$htmlApp = new Vn\Web\HtmlApp ('hedera-web', 'pages');
$htmlApp->run ();
?>

View File

@ -41,7 +41,7 @@ Vn.Tpv = new Class
,'company': company
};
var request = new Vn.JsonRequest ('tpv');
var request = new Vn.JsonRequest ('tpv/transaction');
request.send (params,
this._onTransactionStart.bind (this));
}

View File

@ -198,7 +198,7 @@ Htk.Image = new Class
,_onFileUpload: function (cell, editor)
{
this._stamp = new Date ().getTime ();
this.setSrc (cell);
this._refreshSrc (cell);
this.popup.hide ();
}

View File

@ -14,9 +14,9 @@ Htk.ImageEditor = new Class
{
this.builderInit (this.constructor.Xml);
this.$('max-size').value = 10 /* MB */ * 1048576;
this.$('form').action =
'//'+ Vn.Config['image_host'] +'/rest.php?action=image';
var self = this;
this.$('form').onsubmit = function ()
{ self.onSubmit (); return false; };
this.parent (props);
}
@ -31,30 +31,28 @@ Htk.ImageEditor = new Class
this.signalEmit ('name-changed', newValue);
}
,onFormSubmit: function ()
,onSubmit: function ()
{
this.$('submit').disabled = true;
this.$('spinner').start ();
var request = new Vn.JsonRequest ();
request.sendFormMultipart (this.$('form'),
this.onResponse.bind (this));
}
,onIframeLoad: function ()
,onResponse: function (request, json, error)
{
this.$('submit').disabled = false;
this.$('spinner').stop ();
try {
var responseText = this.$('iframe').contentDocument.body.textContent;
var response = eval ('('+ responseText +')');
if (response.data)
{
this.signalEmit ('file-uploaded', this.$('name').value);
Htk.Toast.showMessage (_('ImageAdded'));
}
else
Htk.Toast.showError (response.error.message +' ('+ response.error.code +')');
if (json)
{
Htk.Toast.showMessage (_('ImageAdded'));
this.signalEmit ('file-uploaded', this.$('name').value);
}
catch (e) {}
else
Htk.Toast.showError (error.message);
}
,setData: function (image, directory)

View File

@ -3,9 +3,8 @@
<form
id="form"
method="post"
target="image-editor"
enctype="multipart/form-data"
on-submit="onFormSubmit">
action="json.php?method=image/upload"
enctype="multipart/form-data">
<div class="form-group">
<label for="name"><t>FileName</t></label>
<input id="name" type="text" name="name" on-change="onNameChange"/>
@ -22,9 +21,5 @@
<input id="schema" type="hidden" name="schema"/>
<input id="max-size" type="hidden" name="MAX_FILE_SIZE"/>
</form>
<iframe
id="iframe"
name="image-editor"
on-load="onIframeLoad"/>
</div>
</vn>

View File

@ -75,5 +75,14 @@ Vn.JsonRequest = new Class
this.sendWithUrl (params, callback, form.method, form.action);
}
,sendFormMultipart: function (form, callback)
{
var request = new XMLHttpRequest ();
request.open (form.method, form.action, true);
request.onreadystatechange =
this._onStateChange.bind (this, request, callback);
request.send (new FormData (form));
}
});

View File

@ -1,9 +1,8 @@
<?php
require_once ('vn/hedera/hedera.php');
require_once ('vn/web/json-app.php');
$restApp = new Vn\Web\JsonApp (Vn\Hedera\APP_NAME, 'rest');
$restApp = new Vn\Web\JsonApp ('hedera-web', 'rest');
$restApp->run ();
?>

View File

@ -11,6 +11,6 @@ Vn.include ('js/hedera/main');
Vn.main (function ()
{
app = new Vn.App ();
app.run ();
hederaWeb = new Vn.App ();
hederaWeb.run ();
});

View File

@ -1,9 +1,8 @@
<?php
require_once ('vn/hedera/hedera.php');
require_once ('vn/web/rest-app.php');
$restApp = new Vn\Web\RestApp (Vn\Hedera\APP_NAME, 'rest');
$restApp = new Vn\Web\RestApp ('hedera-web', 'rest');
$restApp->run ();
?>

View File

@ -1,10 +1,10 @@
<?php
require_once ('vn/web/http-request.php');
require_once ('vn/web/rest-request.php');
require_once ('PEAR.php');
require_once ('Text/CAPTCHA.php');
class Captcha extends Vn\Web\HttpRequest
class Captcha extends Vn\Web\RestRequest
{
function run ()
{

View File

@ -20,8 +20,10 @@ class Log extends Vn\Web\JsonRequest
if (!$this->checkParams ($_REQUEST, $params))
return FALSE;
error_log (sprintf ('Browser: %s(%d): %s. Stack: %s'
$user = isset ($_SESSION['user']) ? $_SESSION['user'] : 'guest';
error_log (sprintf ("Javascript: User: %s: %s(%d): %s.\n%s"
,$user
,$_REQUEST['file']
,$_REQUEST['line']
,$_REQUEST['message']

View File

@ -47,7 +47,7 @@ class Login extends Vn\Web\JsonRequest
}
/**
* Escapes the double cuotes from an string.
* Escapes the double quotes from an string.
**/
function escape ($str)
{

View File

@ -128,7 +128,7 @@ class Query extends Vn\Web\JsonRequest
{
if ($row['Code'] == 1265
&& ($warning = $db->getRow ($sql)))
$this->app->addWarning ($warning['description'], $warning['code']);
trigger_error ("${warning['code']}: ${warning['description']}", E_USER_WARNING);
else
trigger_error ("${row['Code']}: ${row['Message']}", E_USER_WARNING);
}
@ -166,7 +166,7 @@ class Query extends Vn\Web\JsonRequest
}
if ($row)
throw new Lib\Exception ($row['description'], $row['code']);
throw new Lib\UserException ($row['description'], $row['code']);
else
throw $e;
}

View File

@ -1,9 +1,9 @@
<?php
require_once ('vn/web/json-request.php');
require_once ('vn/web/rest-request.php');
require_once ('vn/web/util.php');
class Invoice extends Vn\Web\JsonRequest
class Invoice extends Vn\Web\RestRequest
{
function run ()
{

View File

@ -1,8 +1,6 @@
<?php
namespace Vn\Hedera;
static class Image
class Image
{
/**
* Creates an image resource from a valid image file.
@ -140,7 +138,7 @@ static class Image
}
if (!$saved)
throw new Exception (sprintf (s('File save error: %s'), $dstFile), 'saveError', ));
throw new Exception (sprintf (s('File save error: %s'), $dstFile), 'saveError');
}
}

View File

@ -1,8 +1,7 @@
<?php
require_once (__DIR__.'/image-method.php');
use Vn\Hedera\Image;
require_once ('vn/lib/method.php');
require_once (__DIR__.'/lib.php');
/**
* Resizes all images allocated in a directory.
@ -13,7 +12,7 @@ use Vn\Hedera\Image;
* @param integer $maxWidth The maximum width of resized image in pixels
* @param boolean $rewrite Wether to rewrite the destination file if it exits
**/
class Resize extends ImageMethod
class Resize extends Vn\Lib\Method
{
function run ()
{
@ -65,7 +64,7 @@ class Resize extends ImageMethod
catch (\Exception $e) {}
}
return $count;
echo "$count files resized";
}
}

View File

@ -1,16 +1,23 @@
<?php
require_once (__DIR__.'/image-request.php');
use Vn\Hedera\Image;
require_once ('vn/lib/method.php');
require_once (__DIR__.'/util.php');
/**
* Syncronizes the data directory with the database, this may take
* some time.
**/
class Sync extends ImageRequest
class Sync extends Vn\Lib\Method
{
private $trashSubdir;
private $util;
function __construct ($app)
{
parent::__construct ($app);
$this->util = new Util ($app);
$this->dataDir = $this->util->dataDir;
}
function run ()
{
@ -79,7 +86,7 @@ class Sync extends ImageRequest
$this->cleanImages ($schema, $size, $map);
}
return TRUE;
echo "Syncronization finished";
}
function cleanImages ($schema, $size, &$map)

View File

@ -1,8 +1,7 @@
<?php
require_once (__DIR__.'/image-request.php');
use Vn\Hedera\Image;
require_once ('vn/web/rest-request.php');
require_once (__DIR__.'/util.php');
/**
* Creates a thumb from an existing full image.
@ -12,7 +11,7 @@ use Vn\Hedera\Image;
* @param integer $width The width of the thumb
* @param integer $height The height of the thumb
**/
class Thumb extends ImageRequest
class Thumb extends Vn\Web\RestRequest
{
function run ()
{
@ -67,7 +66,9 @@ class Thumb extends ImageRequest
// Creates the thumb.
$baseDir = "{$this->dataDir}/$schema";
$util = new Util ($this->app);
$baseDir = "{$util->dataDir}/$schema";
$srcFile = "$baseDir/full/$file";
$dstFile = "$baseDir/{$width}x{$height}/$file";
$symbolicSrc = "../full/$file";

View File

@ -1,18 +1,19 @@
<?php
require_once (__DIR__.'/image-request.php');
require_once ('vn/web/json-request.php');
require_once (__DIR__.'/util.php');
use Vn\Lib;
use Vn\Hedera\Image;
/**
* Uploads a file creating its corresponding sizes.
**/
class Upload extends ImageRequest
class Upload extends Vn\Web\JsonRequest
{
static function run ()
function run ()
{
$db = $this->login ();
$util = new Util ($this->app);
// Checks schema.
@ -22,7 +23,7 @@ class Upload extends ImageRequest
throw new Lib\UserException (s('BadSchemaName'), 'badSchemaName');
$schema = $_REQUEST['schema'];
$info = $this->loadInfo ($schema);
$info = $util->loadInfo ($schema);
if (!$info)
throw new Lib\UserException (s('SchemaNotExists'), 'schemaNotExists');
@ -126,12 +127,11 @@ class Upload extends ImageRequest
// Resizes and saves the image.
$fileName = "{$_REQUEST['name']}.png";
$schemaPath = "{$this->dataDir}/$schema";
$schemaPath = "{$util->dataDir}/$schema";
$fullFile = "$schemaPath/full/$fileName";
$symbolicSrc = "../full/$fileName";
$image = Image::create ($_FILES['image']['tmp_name']);
Image::resizeSave ($image, $fullFile, $info['max_height'], $info['max_width']);
foreach ($info['sizes'] as $size => $i)

View File

@ -1,18 +1,18 @@
<?php
require_once ('vn/web/json-request.php');
require_once (__DIR__.'/image.php');
/**
* Base class for image methods.
**/
abstract class ImageRequest extends Vn\Web\JsonRequest
class Util
{
protected $dataDir;
var $app;
var $dataDir;
function __construct ($app)
{
parent::__construct ($app);
$this->app = $app;
$this->dataDir = _DATA_DIR .'/'. $app->getName () .'/image';
}
@ -23,9 +23,9 @@ abstract class ImageRequest extends Vn\Web\JsonRequest
**/
function loadInfo ($schema)
{
$db = $this->getSysConn ();
$db = $this->app->getSysConn ();
$info = $conn->getRow (
$info = $db->getRow (
'SELECT id, max_width, max_height, `schema`, `table`, `column`
FROM image_schema WHERE name = #schema'
,['schema' => $schema]
@ -34,7 +34,7 @@ abstract class ImageRequest extends Vn\Web\JsonRequest
if (!$info)
return NULL;
$res = $conn->query (
$res = $db->query (
'SELECT width, height, crop
FROM image_schema_size WHERE image_schema_id = #id'
,['id' => $info['id']]

View File

@ -1,13 +1,11 @@
<?php
require_once ('vn/web/json-request.php');
require_once ('vn/lib/method.php');
class VisitsSync extends Vn\Web\JsonRequest
class VisitsSync extends Vn\Lib\Method
{
function run ()
{
$this->login ();
$db = $this->getSysConn ();
$result = $db->query ("SELECT id, agent FROM visit_agent
WHERE version = '0.0' OR platform = 'unknown' OR cookies IS NULL ORDER BY id DESC");
@ -29,6 +27,8 @@ class VisitsSync extends Vn\Web\JsonRequest
);
// Update the visit info using browscap
$count = 0;
while ($row = $result->fetch_assoc ())
{
@ -40,19 +40,17 @@ class VisitsSync extends Vn\Web\JsonRequest
$cookies = $info->cookies;
$id = $row['id'];
$stmt->execute ();
$count++;
}
$result = TRUE;
echo "$count records updated";
}
else
$result = FALSE;
if ($stmt)
$stmt->close ();
if ($result)
$result->free ();
return $result;
}
}

View File

@ -1,13 +1,13 @@
<?php
require_once ('vn/web/http-request.php');
require_once ('vn/web/rest-request.php');
require_once ('vn/web/util.php');
require_once (__DIR__.'/tpv.php');
/**
* Gets transaction confirmation from SOAP service.
**/
class ConfirmSoap extends Vn\Web\HttpRequest
class ConfirmSoap extends Vn\Web\RestRequest
{
function run ()
{

View File

@ -1,8 +1,6 @@
<?php
namespace Vn\Tpv;
static class Tpv
class Tpv
{
/**
* Tryes to confirm a transaction with the given params.

View File

@ -1,9 +1,6 @@
<?php
require_once ('vn/web/json-request.php');
require_once ('vn/tpv/tpv.php');
use Vn\Lib;
/**
* Starts a new TPV transaction and returns the params.
@ -12,15 +9,14 @@ class Transaction extends Vn\Web\JsonRequest
{
function run ()
{
$this->login ();
$db = $this->login ();
if (empty ($_REQUEST['amount']))
throw new Exception ('Amount parameter not defined');
$amount = (int) $_REQUEST['amount'];
$companyId = empty ($_REQUEST['company']) ? NULL : $_REQUEST['company'];
$db = $this->getSysConn ();
$row = $db->getRow ('CALL transaction_start_sha256 (#, #)',
[$amount, $companyId]);
@ -55,11 +51,11 @@ class Transaction extends Vn\Web\JsonRequest
$signature = base64_encode (hash_hmac ('sha256', $encodedParams, $key, TRUE));
$url = $row['url'];
$this->json ([
return [
'url' => $url
,'params' => $encodedParams
,'signature' => $signature
]);
];
}
}

View File

@ -1,9 +0,0 @@
<?php
namespace Vn\Hedera;
require_once ('vn/web/app.php');
const APP_NAME = 'hedera-web';
?>

View File

@ -1,12 +0,0 @@
<?php
namespace Vn\Web;
require_once (__DIR__.'/rest-app.php');
/**
* Base class for REST services.
**/
abstract class HttpRequest extends \Vn\Lib\Method {}
?>

View File

@ -3,6 +3,7 @@
namespace Vn\Web;
require_once (__DIR__.'/rest-app.php');
require_once (__DIR__.'/json-request.php');
require_once (__DIR__.'/json-reply.php');
use Vn\Lib;

View File

@ -3,10 +3,11 @@
namespace Vn\Web;
require_once (__DIR__.'/json-app.php');
require_once (__DIR__.'/rest-request.php');
/**
* Base class for JSON services.
**/
abstract class JsonRequest extends \Vn\Lib\Method {}
abstract class JsonRequest extends RestRequest {}
?>

View File

@ -3,6 +3,7 @@
namespace Vn\Web;
require_once (__DIR__.'/app.php');
require_once (__DIR__.'/rest-request.php');
use Vn\Lib;

32
vn/web/rest-request.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace Vn\Web;
require_once (__DIR__.'/rest-app.php');
/**
* Base class for REST services.
**/
abstract class RestRequest extends \Vn\Lib\Method
{
/**
* Authenticates the user agaisnt database and returns its associated
* database connection.
*
* return Db\Conn The database connection
**/
function login ()
{
return $this->app->login ();
}
/**
* Logouts the current user.
**/
function logout ()
{
$this->app->logout ();
}
}
?>