phpgroupware-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Phpgroupware-cvs] [19041] A bunch of changes.


From: Johan Gunnarsson
Subject: [Phpgroupware-cvs] [19041] A bunch of changes.
Date: Wed, 21 Jan 2009 23:22:34 +0000

Revision: 19041
          
http://svn.sv.gnu.org/viewvc/?view=rev&root=phpgroupware&revision=19041
Author:   johang
Date:     2009-01-21 23:22:34 +0000 (Wed, 21 Jan 2009)

Log Message:
-----------
A bunch of changes. Add source_id to mappings for example.

Modified Paths:
--------------
    modules/syncml/trunk/inc/class.somappings.inc.php
    modules/syncml/trunk/inc/class.syncml_database.inc.php
    modules/syncml/trunk/inc/class.syncml_message.inc.php
    modules/syncml/trunk/inc/class.syncml_response.inc.php
    modules/syncml/trunk/syncml.php

Modified: modules/syncml/trunk/inc/class.somappings.inc.php
===================================================================
--- modules/syncml/trunk/inc/class.somappings.inc.php   2009-01-21 23:20:52 UTC 
(rev 19040)
+++ modules/syncml/trunk/inc/class.somappings.inc.php   2009-01-21 23:22:34 UTC 
(rev 19041)
@@ -24,7 +24,7 @@
                 * @param $flag   Flag of mapping. NULL to include all.
                 * @return array  Multidimensional array of mappings matched.
                 */
-               function get_mapping($source_id, $ch_id, $luid, $guid, $flag)
+               function get_mapping($ch_id, $luid, $guid, $flag)
                {
                        $GLOBALS['phpgw']->db->query('
                                SELECT *
@@ -65,7 +65,7 @@
                 */
                function get_all_mapped_luids($ch_id)
                {
-                       $mappings = $this->get_mapping(NULL, $ch_id, NULL, 
NULL, NULL);
+                       $mappings = $this->get_mapping($ch_id, NULL, NULL, 
NULL);
 
                        $all_mapped_luids = array();
 
@@ -86,7 +86,7 @@
                 * @param $flag   Flag of mapping. NULL to include all.
                 * @return int    Number of mappings deleted.
                 */
-               function delete_mapping($source_id, $ch_id, $luid, $guid, $flag)
+               function delete_mapping($ch_id, $luid, $guid, $flag)
                {
                        $GLOBALS['phpgw']->db->query('
                                DELETE FROM phpgw_syncml_mappings
@@ -110,7 +110,7 @@
                 * @param $guid   GUID of mapping. NULL to include all.
                 * @param $flag   Flag of mapping. Optional. Defaults to 0.
                 */
-               function insert_mapping($source_id, $ch_id, $luid, $guid, $flag 
= 0)
+               function insert_mapping($ch_id, $luid, $guid, $flag = 0)
                {
                        $GLOBALS['phpgw']->db->query(sprintf("
                                INSERT INTO phpgw_syncml_mappings(
@@ -129,7 +129,7 @@
                 * @param $flag   New value of flag.
                 * @return int    Number of mappings updated.
                 */
-               function update_mapping($source_id, $ch_id, $luid, $guid, $flag)
+               function update_mapping($ch_id, $luid, $guid, $flag)
                {
                        $query =
                                'UPDATE phpgw_syncml_mappings ' . 
@@ -149,4 +149,28 @@
 
                        return $GLOBALS['phpgw']->db->affected_rows();
                }
+               
+               /**
+                *
+                */
+               function remap($channel_id, $guid, $new_guid)
+               {
+                       $query = sprintf("
+                               update
+                                       syncml_mappings m,
+                                       syncml_channels c1,
+                                       syncml_channels c2
+                               set
+                                       m.guid = '%s'
+                               where
+                                       c1.channel_id = %d and
+                                       c2.source_id = c1.source_id and
+                                       m.channel_id = c2.channel_id and
+                                       m.guid = '%s'",
+                               $new_guid, $channel_id, $new_guid);
+                       
+                       $GLOBALS['phpgw']->db->query($query, __LINE__, 
__FILE__);
+
+                       return $GLOBALS['phpgw']->db->affected_rows();
+               }
        }

Modified: modules/syncml/trunk/inc/class.syncml_database.inc.php
===================================================================
--- modules/syncml/trunk/inc/class.syncml_database.inc.php      2009-01-21 
23:20:52 UTC (rev 19040)
+++ modules/syncml/trunk/inc/class.syncml_database.inc.php      2009-01-21 
23:22:34 UTC (rev 19041)
@@ -62,8 +62,8 @@
                        $ipc_manager = CreateObject('phpgwapi.ipc_manager');
                        $this->somappings = new syncml_somappings();
 
-                       list($module, $this->mime_type, $this->mime_version) =
-                               $this->get_source_data($channel_id);
+                       list($this->source_id, $module, $this->mime_type,
+                               $this->mime_version) = 
$this->get_source_data($channel_id);
 
                        $this->ipc = $ipc_manager->getipc($module);
                }
@@ -78,6 +78,7 @@
                {
                        $GLOBALS['phpgw']->db->query(sprintf('
                                SELECT
+                                       s.source_id as source_id,
                                        s.modulename as modulename,
                                        s.mimetype as mimetype,
                                        s.mimeversion as mineversion
@@ -95,6 +96,7 @@
                        $GLOBALS['phpgw']->db->next_record();
 
                        return array(
+                               $GLOBALS['phpgw']->db->f('source_id'),
                                $GLOBALS['phpgw']->db->f('modulename'),
                                $GLOBALS['phpgw']->db->f('mimetype'),
                                $GLOBALS['phpgw']->db->f('mimeversion')
@@ -175,10 +177,10 @@
                 * @param string $override Override conflicts or not.
                 * @return int             Status code.
                 */
-               function add_item($luid, $data, $type, $override = FALSE)
+               function add_item($luid, $data, $type)
                {
                        list($status_code, $guid) =
-                               $this->replace_item($luid, $data, $type, 
$override);
+                               $this->replace_item($luid, $data, $type);
 
                        switch($status_code)
                        {
@@ -200,10 +202,10 @@
                 * @param $override bool Override any conflict.
                 * @return int           0 if conflict, 1 if replace, 2 if add.
                 */
-               function replace_item($luid, $data, $type, $override = FALSE)
+               function replace_item($luid, $data, $type)
                {
                        syncml_logger::get_instance()->log(sprintf(
-                               'Replace item %s (%s)', $luid, $type));
+                               'Replace LUID %s (%s)', $luid, $type));
 
                        $mappings = $this->somappings->get_mapping(
                                $this->channel_id, $luid, NULL, NULL);
@@ -222,7 +224,7 @@
                        syncml_logger::get_instance()->log_data('GUID is', 
$guid);
                        syncml_logger::get_instance()->log_data('Dirty is', 
$dirty);
 
-                       if(!$override && $dirty)
+                       if($dirty)
                        {
                                syncml_logger::get_instance()->log('Conflict, 
duplicate it');
 
@@ -238,18 +240,36 @@
                                        $this->channel_id, $luid, $new_guid, 0);
 
                                return 
array(SYNCML_STATUS_CONFLICTRESOLVEDWITHDUPLICATE,
-                                       $guid);
+                                       $new_guid);
                        }
-                       else if(count($mappings) > 0)
+                       else if($guid)
                        {
                                syncml_logger::get_instance()->log('Replace 
it');
 
-                               $this->ipc->replacedata($guid, $data, $type);
+                               $new_guid = $this->ipc->replacedata($guid, 
$data, $type);
 
+                               $code = SYNCML_STATUS_OK;
+
+                               if($new_guid != $guid)
+                               {
+                                       syncml_logger::get_instance()->log_data(
+                                               'Item was actually added', 
$new_guid);
+
+                                       // remap the item
+
+                                       $this->somappings->remap(
+                                               $this->source_id, $guid, 
$new_guid);
+
+                                       $code = SYNCML_STATUS_ITEMADDED;
+                               }
+
                                $this->somappings->update_mapping(
-                                       $this->channel_id, $guid, NULL, 0);
+                                       NULL, $new_guid, NULL, 1);
 
-                               return array(SYNCML_STATUS_OK, $guid);
+                               $this->somappings->update_mapping(
+                                       $this->channel_id, $new_guid, NULL, 0);
+
+                               return array($code, $new_guid);
                        }
                        else
                        {
@@ -278,20 +298,20 @@
 
                        if(count($mappings) > 0)
                        {
-                               list(list(,, $guid, $dirty)) = $mappings;
+                               $guid = $mappings[0][2];
+                               $dirty = $mappings[0][3];
 
-                               // here, we don't care about conflicts. delete 
even if there
-                               // are changes on phpgw side.
+                               // @todo take care of conflicts maybe?
 
-                               $deletes = $this->ipc->removedata($guid);
+                               $deleted = $this->ipc->removedata($guid);
 
                                $this->somappings->delete_mapping(
                                        $this->channel_id, $luid, NULL, NULL);
 
-                               return array($deletes > 0, $guid);
+                               return array($deleted, $guid);
                        }
 
-                       return array(0, NULL);
+                       return array(FALSE, NULL);
                }
 
                /**
@@ -409,35 +429,34 @@
                 *
                 * @return array Array containing the three types of 
modifications.
                 */
-               function get_modifications(&$response, &$session, &$commands,
-                       $maxobjsize, $size_left)
+               function add_modifications(&$response, &$session, $maxobjsize)
                {
                        foreach($this->get_added_items() as $guid)
                        {
-                               if($size_left <= 0)
+                               if($response->get_size_left() <= 0)
                                {
                                        return TRUE;
                                }
-
+                       
                                $data = $this->get_item_by_guid($guid, 
$this->mime_type);
 
                                list($chunk, $is_first, $is_last) = 
$this->get_chunk(
-                                       $data, $session, min($maxobjsize, 
$size_left));
-
-                               $commands[] = $last_command = 
$response->build_add(
+                                       $data, $session,
+                                       min($maxobjsize, 
$response->get_size_left())
+                               );
+                               
+                               $response->add_add(
                                        array(
                                                'type' => $this->mime_type,
-                                               'size' => $is_first && 
!$is_last ?
-                                                       strlen($data) : NULL),
+                                               'size' => $is_first && 
!$is_last ? strlen($data) : NULL
+                                       ),
                                        array(
                                                'data' => $chunk,
-                                               'src_uri' => $guid
-                                       ),
-                                       $is_last
+                                               'src_uri' => $guid,
+                                               'moredata' => !$is_last
+                                       )
                                );
 
-                               $size_left -= strlen($last_command);
-
                                $session->set_channel_id_from_cmd(
                                        $response->get_cmdid(), 
$this->channel_id);
 
@@ -449,26 +468,24 @@
 
                        foreach($this->get_deleted_items() as $luid)
                        {
-                               if($size_left <= 0)
+                               if($response->get_size_left() <= 0)
                                {
                                        return TRUE;
                                }
-
-                               $commands[] = $last_command = 
$response->build_delete(
+                       
+                               $response->add_delete(
                                        array(
                                                'trg_uri' => $luid
                                        )
                                );
 
-                               $size_left -= strlen($last_command);
-
                                $session->set_channel_id_from_cmd(
                                        $response->get_cmdid(), 
$this->channel_id);
                        }
 
                        foreach($this->get_changed_items() as $replace)
                        {
-                               if($size_left <= 0)
+                               if($response->get_size_left() <= 0)
                                {
                                        return TRUE;
                                }
@@ -477,22 +494,24 @@
                                        $replace['guid'], $this->mime_type);
 
                                list($chunk, $is_first, $is_last) = 
$this->get_chunk(
-                                       $data, $session, min($maxobjsize, 
$size_left));
+                                       $data, $session,
+                                       min($maxobjsize, 
$response->get_size_left())
+                               );
 
-                               $commands[] = $last_command = 
$response->build_replace(
+                               $response->add_replace(
                                        array(
                                                'type' => $this->mime_type,
                                                'size' => $is_first && 
!$is_last ?
-                                                       strlen($data) : NULL),
+                                                       strlen($data) : NULL
+                                       ),
                                        array(
                                                'data' => $chunk,
-                                               'trg_uri' => $replace['luid']
+                                               'trg_uri' => $replace['luid'],
+                                               'moredata' => !$is_last
                                        ),
                                        $is_last
                                );
 
-                               $size_left -= strlen($last_command);
-
                                $session->set_channel_id_from_cmd(
                                        $response->get_cmdid(), 
$this->channel_id);
 

Modified: modules/syncml/trunk/inc/class.syncml_message.inc.php
===================================================================
--- modules/syncml/trunk/inc/class.syncml_message.inc.php       2009-01-21 
23:20:52 UTC (rev 19040)
+++ modules/syncml/trunk/inc/class.syncml_message.inc.php       2009-01-21 
23:22:34 UTC (rev 19041)
@@ -69,11 +69,32 @@
                {
                        $session = new syncml_session();
 
-                       // process every command in message
+                       // execute header
 
-                       for($i = 0, $c = count($this->commands); $i < $c; $i++)
+                       $this->commands[0]->execute($response, $session);
+
+                       // execute every command in message
+
+                       $response->enc->header(sprintf(
+                               '-//SYNCML//DTD SyncML %s//EN', 
$response->header_verdtd));
+
+                       $response->encode_start('SyncML',
+                               'SYNCML:SYNCML' . $response->header_verdtd);
+
+                       syncml_logger::get_instance()->log_data('Outgoing 
VerDTD',
+                               'SYNCML:SYNCML' . $response->header_verdtd);
+
+                       $response->encode_header();
+
+                       $response->encode_start('SyncBody');
+                       $response->encode_pendning_status_commands();
+
+                       for($i = 1, $c = count($this->commands); $i < $c; $i++)
                        {
+                               
syncml_logger::get_instance()->log_data('Command', $i);
+
                                $this->commands[$i]->execute($response, 
$session);
+
                        }
 
                        // output modifications in form of SYNC commands, but 
only when
@@ -85,6 +106,9 @@
                                        $session->get_open_channels(), 
$session, $response);
                        }
 
+                       syncml_logger::get_instance()->log_data('Command',
+                               $session->get_open_channels());
+
                        // do we have to send a ALERT for more messages?
 
                        if($response->status_commands_only() && 
!$response->is_final())
@@ -92,6 +116,18 @@
                                $response->add_alert(SYNCML_ALERT_NEXTMESSAGE, 
array());
                        }
 
+                       // close response
+
+                       if($response->is_final())
+                       {
+                               $response->encode_start('Final');
+                               $response->encode_end();
+                       }
+
+                       $response->encode_end(); // SyncBody
+
+                       $response->encode_end(); // SyncML
+
                        // save the anchors if this is last message in session
 
                        if($response->status_commands_only())
@@ -123,8 +159,10 @@
 
                                
if(!$open_channel['all_server_modifications_sent'])
                                {
-                                       if($this->send_modifications($response, 
$session,
-                                               $open_channel, $database))
+                                       $more_to_send = 
$this->send_modifications($response,
+                                               $session, $open_channel, 
$database);
+                                       
+                                       if($more_to_send)
                                        {
                                                // there is more to send, 
continue with that in next
                                                // message.
@@ -132,12 +170,10 @@
                                                $response->set_final(FALSE);
                                                return;
                                        }
-                                       else
-                                       {
-                                               
$session->set_open_channel_property(
-                                                       
$open_channel['source'], $open_channel['target'],
-                                                       
'all_server_modifications_sent', TRUE);
-                                       }
+
+                                       $session->set_open_channel_property(
+                                               $open_channel['source'], 
$open_channel['target'],
+                                               
'all_server_modifications_sent', TRUE);
                                }
                        }
                }
@@ -232,6 +268,9 @@
                        // remove mappings not modified by client
                        foreach($luids_to_kill as $luid)
                        {
+                               syncml_logger::get_instance()->log(
+                                       "Remove mapping for LUID $luid and 
channel $channel_id");
+                       
                                $somappings->delete_mapping($channel_id, $luid, 
NULL, NULL);
                        }
                }
@@ -283,20 +322,17 @@
                 */
                function add_sync(&$response, &$session, $database, 
$open_channel)
                {
-                       $commands = array();
-
-                       // get server modification
-                       $more_to_send = $database->get_modifications(
-                               $response, $session, $commands, 
$open_channel['maxobjsize'],
-                               $response->get_size_left());
-
-                       // send modifications, which means all items.
-                       $response->add_sync(
+                       $response->add_sync_start(
                                $open_channel['source'], 
$open_channel['target'],
-                               $commands, 
$session->get_var(SYNCML_SUPPORTNUMBEROFCHANGES),
+                               
$session->get_var(SYNCML_SUPPORTNUMBEROFCHANGES),
                                $open_channel['server_modifications_sent'] ?
                                        NULL : 
$database->count_modifications());
 
+                       $more_to_send = $database->add_modifications(
+                               $response, $session, 
$open_channel['maxobjsize']);
+
+                       $response->add_sync_end();
+
                        return $more_to_send;
                }
        }

Modified: modules/syncml/trunk/inc/class.syncml_response.inc.php
===================================================================
--- modules/syncml/trunk/inc/class.syncml_response.inc.php      2009-01-21 
23:20:52 UTC (rev 19040)
+++ modules/syncml/trunk/inc/class.syncml_response.inc.php      2009-01-21 
23:22:34 UTC (rev 19041)
@@ -382,8 +382,10 @@
                        $this->enc->start('Item');
 
                        $this->encode_source_and_target(
-                               $item['src_uri'], $item['src_name'],
-                               $item['trg_uri'], $item['trg_name']);
+                               isset($item['src_uri']) ? $item['src_uri'] : 
NULL,
+                               isset($item['src_name']) ? $item['src_name'] : 
NULL,
+                               isset($item['trg_uri']) ? $item['trg_uri'] : 
NULL,
+                               isset($item['trg_name']) ? $item['trg_name'] : 
NULL);
 
                        if(isset($item['meta']))
                        {

Modified: modules/syncml/trunk/syncml.php
===================================================================
--- modules/syncml/trunk/syncml.php     2009-01-21 23:20:52 UTC (rev 19040)
+++ modules/syncml/trunk/syncml.php     2009-01-21 23:22:34 UTC (rev 19041)
@@ -25,7 +25,11 @@
 
        require_once 'inc/class.syncml_logger.inc.php';
 
-       require_once 'inc/class.xml_parser.inc.php';
+       require_once 'inc/class.syncml_xml_decoder.inc.php';
+       require_once 'inc/class.syncml_xml_encoder.inc.php';
+       require_once 'inc/class.syncml_wbxml_decoder.inc.php';
+       require_once 'inc/class.syncml_wbxml_encoder.inc.php';
+
        require_once 'inc/class.xml_offset_mapper.inc.php';
 
        require_once 'inc/class.syncml_response.inc.php';
@@ -36,13 +40,16 @@
        // this is a ugly, ugly hack
        $GLOBALS['phpgw']->db->query('TRUNCATE phpgw_access_log');
 
-       set_error_handler(array(syncml_logger::get_instance(), 'log_error'), 
E_ALL);
+       set_error_handler(
+               array(syncml_logger::get_instance(), 'log_error'), E_ALL);
 
        if(!isset($_SERVER['CONTENT_TYPE']) ||
                $_SERVER['REQUEST_METHOD'] != 'POST')
        {
                syncml_logger::get_instance()->log_data(
-                       "Unset content type or request method", 
$_SERVER['REQUEST_METHOD']);
+                       'Unset content type or invalid request method',
+                       $_SERVER['REQUEST_METHOD']);
+
                exit('I\'m a SyncML server (1)');
        }
 
@@ -51,84 +58,55 @@
        switch($_SERVER['CONTENT_TYPE'])
        {
                case 'application/vnd.syncml+wbxml':
-                       file_put_contents(
-                               sprintf("%s/%s-input.wbxml", SYNCML_LOG_DIR, 
$file_date),
-                               $post_input . "\n");
-
-                       $post_input = wbxml_decode($post_input);
+                       $decoder = new syncml_wbxml_decoder();
+                       $response = new syncml_response(new 
syncml_wbxml_encoder());
+                       $type = 'wbxml';
+                       break;
                case 'application/vnd.syncml+xml':
-                       $parser = new xml_parser();
-                       $response = new syncml_response();
+                       $decoder = new syncml_xml_decoder();
+                       $response = new syncml_response(new 
syncml_xml_encoder());
+                       $type = 'xml';
                        break;
                default:
                        exit('I\'m a SyncML server (2)');
        }
 
        file_put_contents(
-               sprintf("%s/%s-input.xml", SYNCML_LOG_DIR, $file_date),
-               $post_input . "\n");
+               sprintf('%s/%s-input.%s', SYNCML_LOG_DIR, $file_date, $type),
+               $post_input);
 
        $message = new syncml_message();
 
-       // the header
+       // process the header
 
-       $header = $parser->parse($post_input,
+       $header = $decoder->parse($post_input,
                new xml_offset_mapper(array('SYNCML', 'SYNCHDR')));
+
        $message->process_header($header);
 
        unset($header);
 
-       // the body
+       // process the body
 
-       $body = $parser->parse($post_input,
+       $body = $decoder->parse($post_input,
                new xml_offset_mapper(array('SYNCML', 'SYNCBODY')));
+
        $message->process_body($body);
 
        unset($body, $GLOBALS['HTTP_RAW_POST_DATA']);
 
-       // execute and print everything
+       // execute the message and print the response
 
+       ob_start();
+
        $message->execute($response);
+       
        $response->print_response();
 
        file_put_contents(
-               sprintf("%s/%s-output.xml", SYNCML_LOG_DIR, $file_date),
-               ob_get_contents() . "\n");
+               sprintf("%s/%s-output.%s", SYNCML_LOG_DIR, $file_date, $type),
+               ob_get_contents());
 
-       if($_SERVER['CONTENT_TYPE'] == 'application/vnd.syncml+wbxml')
-       {
-               /*
-               // remove the xml declaration tag
-               $xml = substr(ob_get_clean(), 38);
-               */
-
-               // replace some bogus FPI values
-               /*
-               I don't remember why this hack was needed. It appears to work 
without
-               it.
-
-               $wbxml = str_replace(
-                       array(
-                               chr(0x02) . chr(0xA4) . chr(0x01) . chr(0x6A),
-                               chr(0x02) . chr(0xA4) . chr(0x02) . chr(0x6A)),
-                       array(
-                               chr(0x02) . chr(0x9F) . chr(0x51) . chr(0x6A),
-                               chr(0x02) . chr(0x9F) . chr(0x52) . chr(0x6A)),
-                       wbxml_encode($xml, 0x02, FALSE, FALSE)
-               );
-               */
-
-               $wbxml = wbxml_encode(ob_get_clean(), 0x02, FALSE, FALSE);
-
-               ob_start();
-
-               file_put_contents(
-                       sprintf("%s/%s-output.wbxml", SYNCML_LOG_DIR, 
$file_date),
-                       $wbxml);
-
-               echo $wbxml;
-       }
-
        header('Content-Type: ' . $_SERVER['CONTENT_TYPE']);
        header('Content-Length: ' . ob_get_length());
 






reply via email to

[Prev in Thread] Current Thread [Next in Thread]