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