GPWExtUtil.php
4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/*********************************************************************************************************************
** Copyright (C) 2008 Sistema GP-Web Ltda - ME.
** Contato: http://www.sistemagpweb.com.br
** sac@sistemagpweb.com.br
**
** Este arquivo é parte do sistema GPWeb Profissional.
** Este software esta registrado no INPI sob o número RS 11802-5 e protegido pelo direito de autor.
**
** É expressamente proibido utilizar este código em parte ou integralmente sem o expresso consentimento do autor.
**
** Usuário: Evandro
** Data: 03/09/2015
* /********************************************************************************************************************/
require_once(BASE_DIR.'/incluir/funcoes_principais.php');
class GPWExtUtil {
static public function getParam($container, $nomeParametro, $valorPadrao){
return getParam($container, $nomeParametro, $valorPadrao);
}
static public function toUtf8( $data ) {
if( is_array( $data ) ) {
$d = array();
foreach( $data as $k => $v ) {
if( is_string( $k ) ) {
$k = utf8_encode( $k );
}
if( is_string( $v ) ) {
$v = utf8_encode( $v );
}
elseif( is_array( $v ) ) {
$v = toUtf8( $v );
}
$d[ $k ] = $v;
}
return $d;
}
if( is_string( $data ) ) {
return utf8_encode( $data );
}
return $data;
}
static public function fromUtf8( $data ) {
if( is_array( $data ) ) {
$d = array();
foreach( $data as $k => $v ) {
if( is_string( $k ) ) {
$k = utf8_decode( $k );
}
if( is_string( $v ) ) {
$v = utf8_decode( $v );
}
elseif( is_array( $v ) ) {
$v = fromUtf8( $v );
}
$d[ $k ] = $v;
}
return $d;
}
if( is_string( $data ) ) {
return utf8_decode( $data );
}
return $data;
}
static public function sendError( $msg, $encode = true ) {
ob_clean();
echo json_encode( array( 'success' => false, 'msg' => $encode ? self::toUtf8( $msg ) : $msg ) );
ob_flush();
}
static public function sendSuccess() {
ob_clean();
echo json_encode( array( 'success' => true ) );
ob_flush();
}
static public function sendData( $root, $data, $encode = true ) {
ob_clean();
echo json_encode( array( 'success' => true, $root => $encode ? self::toUtf8( $data ) : $data ) );
ob_flush();
}
static public function sendList( $countField, $count, $root, $data, $encode = true ) {
ob_clean();
echo json_encode( array( 'success' => true, $countField => $count,
$root => $encode ? self::toUtf8( $data ) : $data ) );
ob_flush();
}
static public function jsonDecode( $data, $toArray = false ) {
if( !isset( $data ) ) {
return $toArray ? '[]' : '{}';
}
return json_decode( get_magic_quotes_gpc() ? stripslashes( $data ) : $data, $toArray );
}
static public function extRoute($metodos = null) {
@header( 'Content-Type: application/json; charset=utf-8' );
$acao = self::getParam( $_REQUEST, 'f', null );
if( !$acao || !function_exists( $acao ) ) {
self::sendError( 'Parâmetros inválidos.' );
exit();
}
if(is_array($metodos)){
if(!in_array($acao, $metodos)){
self::sendError( 'Parâmetros inválidos.' );
exit();
}
}
try {
call_user_func( $acao );
}
catch(\Exception $e){
self::sendError( $e->getMessage() );
exit();
}
}
}