source: trunk/prototype/Sync.php @ 7025

Revision 7025, 6.6 KB checked in by gustavo, 12 years ago (diff)

Ticket #3030 - Problema para remover marcadores em pastas com espaco

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