This commit is contained in:
Juan Ferrer Toribio 2016-08-30 09:43:47 +02:00
parent 9812519677
commit 25e56bea80
22 changed files with 215 additions and 190 deletions

View File

@ -6,38 +6,37 @@ Vn.Photos = new Class
,activate: function () ,activate: function ()
{ {
this.$('schema').value = 'catalog'; this.$('schema').value = 'catalog';
this.$('photo-size').value = 10 /* MB */ * 1048576;
this.$('photo-id').focus (); this.$('photo-id').focus ();
this.$('html-form').action =
'//'+ Vn.Config['image_host'] +'/rest.php?action=image'; var self = this;
this.$('html-form').onsubmit = function ()
{ self.onSubmit (); return false; };
} }
,onFormSubmit: function () ,onSubmit: function ()
{ {
this.$('schema-field').value = this.$('schema').value; this.$('schema-field').value = this.$('schema').value;
this.$('submit').disabled = true; this.$('submit').disabled = true;
this.gui.loaderPush (); this.gui.loaderPush ();
var request = new Vn.JsonRequest ();
request.sendFormMultipart (this.$('html-form'),
this.onResponse.bind (this));
} }
,onImageUpload: function () ,onResponse: function (request, json, error)
{ {
this.gui.loaderPop (); this.gui.loaderPop ();
this.$('submit').disabled = false; this.$('submit').disabled = false;
try { if (json)
var responseText = this.$('iframe').contentDocument.body.textContent; {
var response = eval ('('+ responseText +')'); this.$('photo-id').value = '';
this.$('photo-id').focus ();
if (response.data) Htk.Toast.showMessage (_('ImageAdded'));
{
this.$('photo-id').value = '';
this.$('photo-id').focus ();
Htk.Toast.showMessage (_('ImageUploaded'));
}
else
Htk.Toast.showError (response.error.message +' ('+ response.error.code +')');
} }
catch (e) {} else
Htk.Toast.showError (error.message);
} }
}); });

View File

@ -20,11 +20,12 @@
/* Footer */ /* Footer */
.photos button .photos input[type=submit]
{ {
display: block; display: block;
margin: 0 auto; margin: 0 auto;
padding: 0.6em; padding: 0.6em;
margin-top: 1.5em; margin-top: 1.5em;
font-size: 1.2em;
} }

View File

@ -7,10 +7,8 @@
<div class="form"> <div class="form">
<form <form
id="html-form" id="html-form"
method="post" enctype="multipart/form-data">
enctype="multipart/form-data" <input type="hidden" name="srv" value="json:image/upload"/>
target="photos-iframe"
on-submit="onFormSubmit">
<div class="form-group"> <div class="form-group">
<label><t>Id</t></label> <label><t>Id</t></label>
<input type="number" name="id" id="photo-id"/> <input type="number" name="id" id="photo-id"/>
@ -20,27 +18,22 @@
<input type="text" name="name"/> <input type="text" name="name"/>
</div> </div>
<div class="form-group"> <div class="form-group">
<label><t>Schema</t></label> <label><t>Collection</t></label>
<input type="hidden" name="schema" id="schema-field"/>
<htk-combo id="schema"> <htk-combo id="schema">
<db-model property="model"> <db-model property="model">
SELECT name, `desc` FROM image_schema ORDER BY `desc` <custom>
SELECT name, `desc` FROM image_schema ORDER BY `desc`
</custom>
</db-model> </db-model>
</htk-combo> </htk-combo>
<input type="hidden" name="schema" id="schema-field"/>
</div> </div>
<div class="form-group"> <div class="form-group">
<label><t>ImageFile</t></label> <label><t>ImageFile</t></label>
<input type="file" name="image"/> <input type="file" name="image"/>
<input type="hidden" name="MAX_FILE_SIZE" id="photo-size"/>
</div> </div>
<button class="thin" id="submit"> <input id="submit" type="submit" class="thin"/>
<t>Upload</t>
</button>
</form> </form>
<iframe
id="iframe"
name="photos-iframe"
on-load="onImageUpload"/>
</div> </div>
</div> </div>
</div> </div>

View File

@ -8,7 +8,12 @@ Vn.Invoices = new Class
if (!invoiceId) if (!invoiceId)
return; return;
var url = 'rest.php?method=dms/invoice&invoice='+ invoiceId; var params = {
'srv': 'rest:dms/invoice',
'invoice': invoiceId
};
var url = '?'+ Vn.Url.makeUri (params);
window.open (url, '_blank'); window.open (url, '_blank');
} }
}); });

View File

@ -113,6 +113,7 @@ Htk.Select = new Class
,initialize: function (props) ,initialize: function (props)
{ {
var button = this.createElement ('button'); var button = this.createElement ('button');
button.type = 'button';
button.className = 'htk-select input'; button.className = 'htk-select input';
button.addEventListener ('mousedown', this._onButtonMouseDown.bind (this)); button.addEventListener ('mousedown', this._onButtonMouseDown.bind (this));

View File

@ -18,7 +18,6 @@
<div class="clear"/> <div class="clear"/>
</div> </div>
<input id="schema" type="hidden" name="schema"/> <input id="schema" type="hidden" name="schema"/>
<input id="max-size" type="hidden" name="MAX_FILE_SIZE"/>
</form> </form>
</div> </div>
</vn> </vn>

View File

@ -21,8 +21,11 @@ Vn.JsonRequest = new Class
,send: function (params, callback) ,send: function (params, callback)
{ {
if (!params)
params = {};
params['srv'] = 'json:'+ this._methodName; params['srv'] = 'json:'+ this._methodName;
this.sendWithUrl (params, callback, 'post', url); this.sendWithUrl (params, callback, 'post', '.');
} }
,sendForm: function (form, callback) ,sendForm: function (form, callback)
@ -34,16 +37,13 @@ Vn.JsonRequest = new Class
if (elements[i].name) if (elements[i].name)
params[elements[i].name] = elements[i].value; params[elements[i].name] = elements[i].value;
this.sendWithUrl (params, callback, form.method, form.action); this.sendWithUrl (params, callback, 'post', form.action);
} }
,sendFormMultipart: function (form, callback) ,sendFormMultipart: function (form, callback)
{ {
var formData = new FormData (form);
formData.append ('service', 'Json');
var request = new XMLHttpRequest (); var request = new XMLHttpRequest ();
request.open (form.method, form.action, true); request.open ('post', form.action, true);
request.onreadystatechange = request.onreadystatechange =
this._onStateChange.bind (this, request, callback); this._onStateChange.bind (this, request, callback);
request.send (new FormData (form)); request.send (new FormData (form));
@ -58,6 +58,31 @@ Vn.JsonRequest = new Class
var error = null; var error = null;
try { try {
if (request.status == 0)
{
var ex = new Vn.JsonException ();
ex.message = _('The server does not respond');
throw ex;
}
var contentType = null;
try {
contentType = request
.getResponseHeader ('Content-Type')
.split (';')[0]
.trim ();
}
catch (e) {}
if (contentType != 'application/json')
{
var ex = new Vn.JsonException ();
ex.message = request.statusText;
ex.code = request.status;
throw ex;
}
var json = JSON.parse (request.responseText); var json = JSON.parse (request.responseText);
var jsData = json.data; var jsData = json.data;
var jsWarns = json.warnings; var jsWarns = json.warnings;

View File

@ -1,7 +1,7 @@
{ {
"Photos": "Fotos" "Photos": "Fotos"
,"Schema": "Esquema" ,"Collection": "Colección"
,"ImageName": "Nom de la imatge" ,"ImageName": "Nom de la imatge"
,"Id": "Id" ,"Id": "Id"
,"ImageFile": "Arxiu d'imatge" ,"ImageFile": "Arxiu d'imatge"

View File

@ -1,7 +1,7 @@
{ {
"Photos": "Photos" "Photos": "Photos"
,"Schema": "Schema" ,"Collection": "Collection"
,"ImageName": "Image name" ,"ImageName": "Image name"
,"Id": "Id" ,"Id": "Id"
,"ImageFile": "Image file" ,"ImageFile": "Image file"

View File

@ -1,7 +1,7 @@
{ {
"Photos": "Fotos" "Photos": "Fotos"
,"Schema": "Esquema" ,"Collection": "Colección"
,"ImageName": "Nombre de la imagen" ,"ImageName": "Nombre de la imagen"
,"Id": "Id" ,"Id": "Id"
,"ImageFile": "Archivo de imagen" ,"ImageFile": "Archivo de imagen"

View File

@ -1,7 +1,7 @@
{ {
"Photos": "Photos" "Photos": "Photos"
,"Schema": "Schéma" ,"Collection": "Collection"
,"ImageName": "Nom de l'image" ,"ImageName": "Nom de l'image"
,"Id": "Id" ,"Id": "Id"
,"ImageFile": "Fichier image" ,"ImageFile": "Fichier image"

View File

@ -1,7 +1,7 @@
{ {
"Photos": "Photos" "Photos": "Photos"
,"Schema": "Schema" ,"Collection": "цуглуулга"
,"ImageName": "Image name" ,"ImageName": "Image name"
,"Id": "Id" ,"Id": "Id"
,"ImageFile": "Image file" ,"ImageFile": "Image file"

View File

@ -1,7 +1,7 @@
{ {
"Photos": "Fotos" "Photos": "Fotos"
,"Schema": "Esquema" ,"Collection": "Coleção"
,"ImageName": "Nome da imagem" ,"ImageName": "Nome da imagem"
,"Id": "Id" ,"Id": "Id"
,"ImageFile": "Arquivo de imagem" ,"ImageFile": "Arquivo de imagem"

View File

@ -1,12 +1,12 @@
<?php <?php
require_once ('vn/web/json-request.php'); require_once ('vn/web/rest-request.php');
require_once (__DIR__.'/tpv.php'); require_once (__DIR__.'/tpv.php');
/** /**
* Gets transaction confirmation from HTTP POST. * Gets transaction confirmation from HTTP POST.
**/ **/
class ConfirmPost extends Vn\Web\HttpRequest class ConfirmPost extends Vn\Web\RestRequest
{ {
function run () function run ()
{ {

View File

@ -6,9 +6,9 @@ if (isset ($_POST['key']))
{ {
ini_set ('soap.wsdl_cache_enabled', FALSE); ini_set ('soap.wsdl_cache_enabled', FALSE);
$requestString = file_get_contents ('soap-request.xml'); $requestString = file_get_contents (__DIR__.'/soap-request.xml');
$client = new SoapClient (stream_resolve_include_path ('vn/tpv/soap.wsdl')); $client = new SoapClient (__DIR__.'/soap.wsdl');
$result = $client->__soapCall ('procesaNotificacionSIS', [ $result = $client->__soapCall ('procesaNotificacionSIS', [
'XML' => $requestString 'XML' => $requestString
]); ]);

View File

@ -12,6 +12,7 @@ require_once ('vn/lib/app.php');
**/ **/
class App extends \Vn\Lib\App class App extends \Vn\Lib\App
{ {
protected $conn = NULL;
private $allowedServices = private $allowedServices =
[ [
'html', 'html',
@ -65,6 +66,137 @@ class App extends \Vn\Lib\App
else else
return parent::getConfigFile (); return parent::getConfigFile ();
} }
/**
* Tries to retrieve user credentials from many sources such as POST,
* SESSION or COOKIES. If $_POST['remember'] is defined the user credentials
* are saved on the client brownser for future logins, cookies names are
* 'vn_user' for the user name and 'vn_pass' for user password, the
* password is encoded using base64_encode() function and should be decoded
* using base64_decode().
*
* return Db\Conn The database connection
**/
function login ()
{
if ($this->conn)
return $this->conn;
$user = NULL;
$password = NULL;
$rememberUser = TRUE;
$rememberPass = FALSE;
$credentialsChanged = TRUE;
$wasLoged = isset ($_SESSION['user']);
if (isset ($_POST['guest']))
{
$sysConn = $this->getSysConn ();
$row = $sysConn->getRow (
'SELECT guest_user, guest_pass FROM config');
if ($row)
{
$user = $row['guest_user'];
$password = base64_decode ($row['guest_pass']);
$rememberUser = FALSE;
}
}
elseif (isset ($_POST['user']) && isset ($_POST['password']))
{
$user = $_POST['user'];
$password = $_POST['password'];
if (isset ($_POST['remember']) && $_POST['remember'])
$rememberPass = TRUE;
}
elseif (isset ($_SESSION['user']))
{
$user = $_SESSION['user'];
$password = $_SESSION['password'];
$credentialsChanged = FALSE;
}
elseif (isset ($_COOKIE['vn_user']) && isset ($_COOKIE['vn_pass']))
{
$user = $_COOKIE['vn_user'];
$password = base64_decode ($_COOKIE['vn_pass']);
$rememberPass = TRUE;
}
if (!isset ($user))
throw new SessionExpiredException ();
try {
$db = $this->createConnection ($user, $password);
$db->query ('CALL user_session_start (#)', [session_id ()]);
$this->conn = $db;
if ($rememberUser)
{
$cookieLife = time () + 7 * 86400; // 7 Days
setcookie ('vn_user', $user, $cookieLife);
if ($rememberPass)
setcookie ('vn_pass',
base64_encode ($password), $cookieLife);
}
$_SESSION['user'] = $user;
$_SESSION['password'] = $password;
}
catch (\Exception $e)
{
$this->conn = NULL;
throw new BadLoginException ();
}
// Registering the user access
if (!$wasLoged)
unset ($_SESSION['visitUser']);
if (isset ($_SESSION['access'])
&& !isset ($_SESSION['visitUser']))
{
$sysConn = $this->getSysConn ();
$_SESSION['visitUser'] = $sysConn->getValue (
'CALL visit_user_new (#, #, #)',
[
$_SESSION['access']
,nullIf ($_SESSION, 'visitUser')
,session_id ()
]
);
if (!isset ($_SESSION['visitUnknown']))
$_SESSION['visitUnknown'] = $_SESSION['visitUser'];
}
return $db;
}
/**
* Logouts the current user. Cleans the last saved used credentials.
**/
function logout ()
{
$_SESSION['visitUser'] = nullIf ($_SESSION, 'visitUnknown');
setcookie ('vn_pass', '', -1);
unset ($_COOKIE['vn_pass']);
unset ($_SESSION['user']);
unset ($_SESSION['password']);
if ($this->conn)
{
$this->conn->query (
'DELETE FROM user_session_view '
.'WHERE connection_id = CONNECTION_ID()'
);
$this->conn->close ();
$this->conn = NULL;
}
}
} }
?> ?>

View File

@ -74,7 +74,7 @@ class HtmlService extends Service
// Loading the requested page // Loading the requested page
$basePath = "./html/$page"; $basePath = "pages/$page";
if (file_exists ($basePath)) if (file_exists ($basePath))
{ {

View File

@ -2,7 +2,7 @@
namespace Vn\Web; namespace Vn\Web;
require_once (__DIR__.'/json-app.php'); require_once (__DIR__.'/json-service.php');
require_once (__DIR__.'/rest-request.php'); require_once (__DIR__.'/rest-request.php');
/** /**

View File

@ -2,7 +2,7 @@
namespace Vn\Web; namespace Vn\Web;
require_once (__DIR__.'/rest-app.php'); require_once (__DIR__.'/rest-service.php');
/** /**
* Base class for REST services. * Base class for REST services.

View File

@ -25,135 +25,6 @@ class RestService extends Service
$method->run (); $method->run ();
} }
/**
* Tries to retrieve user credentials from many sources such as POST,
* SESSION or COOKIES. If $_POST['remember'] is defined the user credentials
* are saved on the client brownser for future logins, cookies names are
* 'vn_user' for the user name and 'vn_pass' for user password, the
* password is encoded using base64_encode() function and should be decoded
* using base64_decode().
**/
function login ()
{
if ($this->conn)
return $this->conn;
$user = NULL;
$password = NULL;
$rememberUser = TRUE;
$rememberPass = FALSE;
$credentialsChanged = TRUE;
$wasLoged = isset ($_SESSION['user']);
if (isset ($_POST['guest']))
{
$sysConn = $this->app->getSysConn ();
$row = $sysConn->getRow (
'SELECT guest_user, guest_pass FROM config');
if ($row)
{
$user = $row['guest_user'];
$password = base64_decode ($row['guest_pass']);
$rememberUser = FALSE;
}
}
elseif (isset ($_POST['user']) && isset ($_POST['password']))
{
$user = $_POST['user'];
$password = $_POST['password'];
if (isset ($_POST['remember']) && $_POST['remember'])
$rememberPass = TRUE;
}
elseif (isset ($_SESSION['user']))
{
$user = $_SESSION['user'];
$password = $_SESSION['password'];
$credentialsChanged = FALSE;
}
elseif (isset ($_COOKIE['vn_user']) && isset ($_COOKIE['vn_pass']))
{
$user = $_COOKIE['vn_user'];
$password = base64_decode ($_COOKIE['vn_pass']);
$rememberPass = TRUE;
}
if (!isset ($user))
throw new SessionExpiredException ();
try {
$db = $this->app->createConnection ($user, $password);
$db->query ('CALL user_session_start (#)', [session_id ()]);
$this->conn = $db;
if ($rememberUser)
{
$cookieLife = time () + 7 * 86400; // 7 Days
setcookie ('vn_user', $user, $cookieLife);
if ($rememberPass)
setcookie ('vn_pass',
base64_encode ($password), $cookieLife);
}
$_SESSION['user'] = $user;
$_SESSION['password'] = $password;
}
catch (\Exception $e)
{
$this->conn = NULL;
throw new BadLoginException ();
}
// Registering the user access
if (!$wasLoged)
unset ($_SESSION['visitUser']);
if (isset ($_SESSION['access'])
&& !isset ($_SESSION['visitUser']))
{
$sysConn = $this->getSysConn ();
$_SESSION['visitUser'] = $sysConn->getValue (
'CALL visit_user_new (#, #, #)',
[
$_SESSION['access']
,nullIf ($_SESSION, 'visitUser')
,session_id ()
]
);
if (!isset ($_SESSION['visitUnknown']))
$_SESSION['visitUnknown'] = $_SESSION['visitUser'];
}
return $db;
}
/**
* Cleans the last saved used credentials.
**/
function logout ()
{
$_SESSION['visitUser'] = nullIf ($_SESSION, 'visitUnknown');
setcookie ('vn_pass', '', -1);
unset ($_COOKIE['vn_pass']);
unset ($_SESSION['user']);
unset ($_SESSION['password']);
if ($this->conn)
{
$this->conn->query (
'DELETE FROM user_session_view '
.'WHERE connection_id = CONNECTION_ID()'
);
$this->conn->close ();
$this->conn = NULL;
}
}
/** /**
* Deinitializes the Application. When init method is called, this * Deinitializes the Application. When init method is called, this
* function is called automatically at the end of the script . * function is called automatically at the end of the script .

View File

@ -29,7 +29,6 @@ class OutdatedVersionException extends Lib\UserException {}
abstract class Service abstract class Service
{ {
protected $app; protected $app;
protected $conn = NULL;
function __construct ($app) function __construct ($app)
{ {
@ -132,7 +131,7 @@ abstract class Service
function getVersion () function getVersion ()
{ {
return (int) strftime ('%G%m%d%H%M%S', return (int) strftime ('%G%m%d%H%M%S',
filectime (__FILE__ /* $_SERVER['SCRIPT_FILENAME'] */)); filectime ($_SERVER['SCRIPT_FILENAME']));
} }
} }