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