[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')) |
---|
| 10 | { |
---|
| 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')) |
---|
| 26 | { |
---|
| 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 | |
---|
[5399] | 41 | require_once 'api/controller.php'; |
---|
[5341] | 42 | |
---|
| 43 | $mounted = array(); $synced = array(); |
---|
| 44 | |
---|
| 45 | if(!function_exists('prepare')) |
---|
| 46 | { |
---|
| 47 | function prepare( $concept, $id, $dt, &$data, &$oldIds, &$mounted, &$synced ) |
---|
| 48 | { |
---|
| 49 | $oldIds[] = $id; |
---|
| 50 | |
---|
| 51 | if( $dt === 'false' || |
---|
| 52 | $dt === false ) |
---|
| 53 | return( false ); |
---|
| 54 | |
---|
| 55 | if( !preg_match( '#^([a-zA-Z0-9-_]+)\(.*\)$#', $id ) ) |
---|
| 56 | $dt['id'] = $id; |
---|
| 57 | elseif( isset($dt['id']) && $dt['id'] === $id ) |
---|
| 58 | unset($dt['id']); |
---|
| 59 | |
---|
| 60 | $links = Controller::links( $concept ); |
---|
| 61 | |
---|
| 62 | foreach( $links as $linkName => $linkTarget ) |
---|
| 63 | { |
---|
| 64 | if( isset( $dt[$linkName] ) ) |
---|
| 65 | { |
---|
| 66 | if( $notArray = Controller::isConcept( $linkName ) ) |
---|
| 67 | $dt[$linkName] = array( $dt[$linkName] ); |
---|
| 68 | |
---|
| 69 | foreach( $dt[$linkName] as $i => $d ) |
---|
| 70 | { |
---|
| 71 | $currentURI = formatURI($links[$linkName], $d); |
---|
| 72 | |
---|
| 73 | if( isset( $mounted[ $currentURI ] ) ) |
---|
| 74 | { |
---|
| 75 | unset( $dt[$linkName][$i] ); |
---|
| 76 | } |
---|
| 77 | elseif( isset( $synced[ $d ] ) ) |
---|
| 78 | { |
---|
| 79 | $dt[$linkName][$i] = $synced[ $d ]; |
---|
| 80 | } |
---|
| 81 | elseif( isset($data[ $currentURI ]) ) |
---|
| 82 | { |
---|
| 83 | $value = $data[$currentURI]; |
---|
| 84 | unset( $data[ $currentURI ] ); |
---|
| 85 | |
---|
| 86 | $mounted[ $currentURI ] = true; |
---|
| 87 | |
---|
| 88 | $dt[$linkName][$i] = prepare( $links[$linkName], $d, $value, $data, $oldIds, $mounted, $synced ); |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | if( empty( $dt[$linkName] ) ) |
---|
| 93 | unset( $dt[$linkName] ); |
---|
| 94 | elseif( $notArray ) |
---|
| 95 | $dt[$linkName] = $dt[$linkName][0]; |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | return( $dt ); |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | $return = array(); |
---|
| 104 | |
---|
| 105 | if( !isset( $args[0] ) ) |
---|
| 106 | $args = array( $args ); |
---|
| 107 | |
---|
| 108 | foreach( $args as $i => $data ) |
---|
| 109 | { |
---|
| 110 | foreach( $data as $uri => $dt ) |
---|
| 111 | { |
---|
| 112 | if( !isset($data[$uri]) ) |
---|
| 113 | continue; |
---|
| 114 | |
---|
| 115 | list( , $concept, $id ) = parseURI( $uri ); |
---|
| 116 | |
---|
| 117 | unset( $data[$uri] ); |
---|
| 118 | $mounted[$uri] = true; |
---|
| 119 | |
---|
| 120 | $oldIds = array(); |
---|
| 121 | |
---|
| 122 | $dt = prepare( $concept, $id, $dt, $data, $oldIds, $mounted, $synced ); |
---|
| 123 | |
---|
| 124 | $result = Controller::put( array( 'concept' => $concept, 'id' => $id ), $dt ); |
---|
| 125 | |
---|
| 126 | if( !$result ) |
---|
| 127 | { |
---|
| 128 | $return[ $uri ] = 'ROLLBACK'; |
---|
| 129 | unset( $data[$uri] ); |
---|
| 130 | continue; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | foreach( $result as $ii => $tx ) |
---|
| 134 | { |
---|
[5437] | 135 | if( !isset($tx['order']) ) |
---|
| 136 | continue; |
---|
| 137 | |
---|
[5341] | 138 | $oldId = $oldIds[ $tx['order'] ]; |
---|
| 139 | |
---|
| 140 | if( isset( $oldId ) && $oldId ) |
---|
| 141 | { |
---|
| 142 | $synced[ $oldId ] = $tx['id']; |
---|
| 143 | unset( $oldIds[ $tx['order'] ] ); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | $oldURI = formatURI( $tx['concept'], $oldId ); |
---|
| 147 | unset( $data[$oldURI] ); |
---|
| 148 | |
---|
[5376] | 149 | $return[ $oldURI ] = !$tx['rollback'] ? $dt ? |
---|
| 150 | array( 'id' => $tx['id'] ) : false : 'ROLLBACK';; |
---|
[5341] | 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | foreach( $data as $oldURI => $oldVal ) |
---|
| 155 | $return[ $oldURI ] = 'ROLLBACK'; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | echo json_encode( $return ); |
---|
| 159 | |
---|
| 160 | Controller::closeAll(); |
---|
[5437] | 161 | |
---|
| 162 | // ob_start(); |
---|
| 163 | // print "\n"; |
---|
| 164 | // print "result: "; |
---|
| 165 | // print_r( $result ); |
---|
| 166 | // $output = ob_get_clean(); |
---|
| 167 | // file_put_contents( "/tmp/prototype.log", $output , FILE_APPEND ); |
---|