1 | <?php |
---|
2 | |
---|
3 | |
---|
4 | $accept = $_SERVER["HTTP_ACCEPT"]; |
---|
5 | |
---|
6 | if(!function_exists('getRealREQUEST')) |
---|
7 | { |
---|
8 | function getRealREQUEST() { |
---|
9 | $vars = array(); |
---|
10 | |
---|
11 | $input = $_SERVER['REDIRECT_QUERY_STRING']; |
---|
12 | if(!empty($input)){ |
---|
13 | $pairs = explode("&", $input); |
---|
14 | foreach ($pairs as $pair) { |
---|
15 | $nv = explode("=", $pair); |
---|
16 | |
---|
17 | $name = urldecode($nv[0]); |
---|
18 | $nameSanitize = preg_replace('/([^\[]*)\[.*$/','$1',$name); |
---|
19 | |
---|
20 | $nameMatched = str_replace('.','_',$nameSanitize); |
---|
21 | $nameMatched = str_replace(' ','_',$nameMatched); |
---|
22 | |
---|
23 | $vars[$nameSanitize] = $_REQUEST[$nameMatched]; |
---|
24 | } |
---|
25 | } |
---|
26 | |
---|
27 | $input = file_get_contents("php://input"); |
---|
28 | if(!empty($input)){ |
---|
29 | $pairs = explode("&", $input); |
---|
30 | foreach ($pairs as $pair) { |
---|
31 | $nv = explode("=", $pair); |
---|
32 | |
---|
33 | $name = urldecode($nv[0]); |
---|
34 | $nameSanitize = preg_replace('/([^\[]*)\[.*$/','$1',$name); |
---|
35 | |
---|
36 | $nameMatched = str_replace('.','_',$nameSanitize); |
---|
37 | $nameMatched = str_replace(' ','_',$nameMatched); |
---|
38 | |
---|
39 | $vars[$nameSanitize] = $_REQUEST[$nameMatched]; |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | return $vars; |
---|
44 | } |
---|
45 | } |
---|
46 | if( !isset( $args ) ) |
---|
47 | $args = getRealREQUEST(); |
---|
48 | |
---|
49 | if(!function_exists('parseURI')) |
---|
50 | { |
---|
51 | function parseURI( $URI ) |
---|
52 | { |
---|
53 | // $regex = "#^([a-zA-Z0-9-_]+)\(([a-zA-Z0-9-_]+)\)://(.*)|([a-zA-Z0-9-_]+)://(.*)$#";//TODO: checar essa RegExp |
---|
54 | $regex = "#^([a-zA-Z0-9-_]+)://(.*)$#"; |
---|
55 | |
---|
56 | preg_match( $regex, $URI, $matches ); |
---|
57 | |
---|
58 | if( !$matches || empty($matches) ) |
---|
59 | return( array(false, $URI, false) ); |
---|
60 | |
---|
61 | return( $matches ); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | if(!function_exists('formatURI')) |
---|
66 | { |
---|
67 | function formatURI( $concept = false, $id = false, $service = false ) |
---|
68 | { |
---|
69 | return $concept ? $id ? $service ? |
---|
70 | |
---|
71 | $concept.'://'.$id.'('.$service.')': |
---|
72 | |
---|
73 | $concept.'://'.$id: |
---|
74 | |
---|
75 | $concept: |
---|
76 | |
---|
77 | false; |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | ///Conversor Para utf8 ante de codificar para json pois o json so funciona com utf8 |
---|
82 | if(!function_exists('toUtf8')) |
---|
83 | { |
---|
84 | function toUtf8($data) |
---|
85 | { |
---|
86 | if(!is_array($data)) |
---|
87 | return mb_convert_encoding( $data , 'UTF-8' , 'UTF-8 , ISO-8859-1' ); |
---|
88 | |
---|
89 | $return = array(); |
---|
90 | |
---|
91 | foreach ($data as $i => $v) |
---|
92 | $return[toUtf8($i)] = toUtf8($v); |
---|
93 | |
---|
94 | return $return; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | require_once 'api/controller.php'; |
---|
99 | |
---|
100 | $mounted = array(); $synced = array(); |
---|
101 | |
---|
102 | if(!function_exists('prepare')) |
---|
103 | { |
---|
104 | function prepare( $concept, $id, $dt, &$data, &$oldIds, &$mounted, &$synced ) |
---|
105 | { |
---|
106 | $oldIds[] = $id; |
---|
107 | |
---|
108 | if( $dt === 'false' || |
---|
109 | $dt === false ) |
---|
110 | return( false ); |
---|
111 | |
---|
112 | if( !preg_match( '#^([a-zA-Z0-9-_]+)\(.*\)$#', $id ) ) |
---|
113 | $dt['id'] = $id; |
---|
114 | elseif( isset($dt['id']) && $dt['id'] === $id ) |
---|
115 | unset($dt['id']); |
---|
116 | |
---|
117 | $links = Controller::links( $concept ); |
---|
118 | |
---|
119 | foreach( $links as $linkName => $linkTarget ) |
---|
120 | { |
---|
121 | if( isset( $dt[$linkName] ) ) |
---|
122 | { |
---|
123 | if( $notArray = Controller::hasOne( $concept, $linkName ) ) |
---|
124 | $dt[$linkName] = array( $dt[$linkName] ); |
---|
125 | |
---|
126 | foreach( $dt[$linkName] as $i => $d ) |
---|
127 | { |
---|
128 | $currentURI = formatURI($links[$linkName], $d); |
---|
129 | |
---|
130 | if( isset( $mounted[ $currentURI ] ) ) |
---|
131 | { |
---|
132 | unset( $dt[$linkName][$i] ); |
---|
133 | } |
---|
134 | elseif( isset( $synced[ $d ] ) ) |
---|
135 | { |
---|
136 | $dt[$linkName][$i] = $synced[ $d ]; |
---|
137 | } |
---|
138 | elseif( isset($data[ $currentURI ]) ) |
---|
139 | { |
---|
140 | $value = $data[$currentURI]; |
---|
141 | unset( $data[ $currentURI ] ); |
---|
142 | |
---|
143 | $mounted[ $currentURI ] = true; |
---|
144 | |
---|
145 | $dt[$linkName][$i] = prepare( $links[$linkName], $d, $value, $data, $oldIds, $mounted, $synced ); |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | if( empty( $dt[$linkName] ) ) |
---|
150 | unset( $dt[$linkName] ); |
---|
151 | elseif( $notArray ) |
---|
152 | $dt[$linkName] = $dt[$linkName][0]; |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | return( $dt ); |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | $return = array(); |
---|
161 | |
---|
162 | if( !isset( $args[0] ) ) |
---|
163 | $args = array( $args ); |
---|
164 | |
---|
165 | Controller::addFallbackHandler( 0, function($e, $URI){ |
---|
166 | |
---|
167 | throw new Exception( $e->getMessage(), 100, $e ); |
---|
168 | |
---|
169 | } ); |
---|
170 | |
---|
171 | Controller::addFallbackHandler( 100, function( $e, $URI ){ |
---|
172 | |
---|
173 | Controller::rollback( $URI ); |
---|
174 | throw $e; |
---|
175 | |
---|
176 | }); |
---|
177 | |
---|
178 | foreach( $args as $i => $data ) |
---|
179 | { |
---|
180 | foreach( $data as $uri => $dt ) |
---|
181 | { |
---|
182 | if( !isset($data[$uri]) ) |
---|
183 | continue; |
---|
184 | |
---|
185 | list( , $concept, $id ) = parseURI( $uri ); |
---|
186 | |
---|
187 | unset( $data[$uri] ); |
---|
188 | $mounted[$uri] = true; |
---|
189 | |
---|
190 | $oldIds = array(); |
---|
191 | |
---|
192 | $dt = prepare( $concept, $id, $dt, $data, $oldIds, $mounted, $synced ); |
---|
193 | |
---|
194 | try{ |
---|
195 | $result = Controller::put( array( 'concept' => $concept, 'id' => $id ), $dt ); |
---|
196 | } |
---|
197 | catch( Exception $e ){ |
---|
198 | $return[ $uri ] = toUtf8( $e->getMessage() ); |
---|
199 | unset( $data[$uri] ); |
---|
200 | continue; |
---|
201 | } |
---|
202 | |
---|
203 | if( !$result ) |
---|
204 | { |
---|
205 | $return[ $uri ] = 'ROLLBACK'; |
---|
206 | unset( $data[$uri] ); |
---|
207 | continue; |
---|
208 | } |
---|
209 | |
---|
210 | foreach( $result as $ii => $tx ) |
---|
211 | { |
---|
212 | if( !isset($tx['order']) ) |
---|
213 | continue; |
---|
214 | |
---|
215 | $oldId = $oldIds[ $tx['order'] ]; |
---|
216 | |
---|
217 | if( isset( $oldId ) && $oldId ) |
---|
218 | { |
---|
219 | $synced[ $oldId ] = $tx['id']; |
---|
220 | unset( $oldIds[ $tx['order'] ] ); |
---|
221 | } |
---|
222 | |
---|
223 | $oldURI = formatURI( $tx['concept'], $oldId ); |
---|
224 | unset( $data[$oldURI] ); |
---|
225 | |
---|
226 | $return[ $oldURI ] = !$tx['rollback'] ? $dt ? |
---|
227 | array( 'id' => $tx['id'] ) : false : 'ROLLBACK'; |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | foreach( $data as $oldURI => $oldVal ) |
---|
232 | $return[ $oldURI ] = 'ROLLBACK'; |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | echo json_encode( $return ); |
---|
237 | |
---|
238 | Controller::closeAll(); |
---|
239 | |
---|
240 | // ob_start(); |
---|
241 | // print "\n"; |
---|
242 | // print "result: "; |
---|
243 | // print_r( $result ); |
---|
244 | // $output = ob_get_clean(); |
---|
245 | // file_put_contents( "/tmp/prototype.log", $output , FILE_APPEND ); |
---|