static function get_instance()
 {
     if (is_null(self::$instance)) {
         self::$instance = new syncml_logger();
     }
     return self::$instance;
 }
    function insert_channel($database_id, $device_uri)
    {
        syncml_logger::get_instance()->log("insert_channel({$database_id},{$device_uri})");
        syncml_logger::get_instance()->log("insert_channel :" . sprintf('
						                INSERT INTO phpgw_syncml_channels(database_id, device_uri, last_merge) VALUES(\'%d\', \'%s\', 0)', $database_id, $device_uri));
        $GLOBALS['phpgw']->db->query(sprintf('
				INSERT INTO phpgw_syncml_channels(database_id, device_uri, last_merge) VALUES( \'%d\', \'%s\', 0)', $database_id, $device_uri), __LINE__, __FILE__);
        syncml_logger::get_instance()->log('insert_channel done');
        return $GLOBALS['phpgw']->db->get_last_insert_id('phpgw_syncml_channel', 'channel_id');
    }
 function _process_sync_init(&$response, &$session)
 {
     $sodatabase = new syncml_sodatabase();
     $sochannel = new syncml_sochannel();
     syncml_logger::get_instance()->log_data("_process_sync_init item", $this->item[0]);
     $database = $sodatabase->get_database_by_uri($this->item[0]['target']['locuri']);
     $database_id = $database['database_id'];
     $owner_id = $database['account_id'];
     syncml_logger::get_instance()->log("_process_sync_init user {$database_id} {$owner_id} ");
     $status = $this->validate_database($database_id, $owner_id, $session->account_id);
     syncml_logger::get_instance()->log("_process_sync_init status {$status} ");
     if (!$status) {
         list($channel_id, $device_last, $phpgw_last) = $sochannel->get_channel_by_database_and_device($this->item[0]['target']['locuri'], $session->get_var('device_uri'));
         $status = $this->validate_channel($device_last);
     }
     syncml_logger::get_instance()->log_data("_process_sync_init status", $status);
     $response->add_status_with_anchor($this->cmdid, $session->msgid, 'Alert', $this->item[0]['target']['locuri'], $this->item[0]['source']['locuri'], $status[0], $this->item[0]['meta']['anchor']['next']);
     if ($status[0] == SYNCML_STATUS_OK || $status[0] == SYNCML_STATUS_REFRESHREQUIRED) {
         syncml_logger::get_instance()->log_data("_process_sync_init ok channel ", $channel_id);
         if (!$channel_id) {
             $channel_id = $sochannel->insert_channel($database_id, $session->get_var('device_uri'));
         }
         syncml_logger::get_instance()->log_data("_process_sync_init channel ", $channel_id);
         $database = new syncml_database($channel_id);
         $database->merge_changes();
         unset($database);
         // todo: make iso 8601 instead.
         $phpgw_next = time();
         // save phpgw and device next anchors
         $session->save_next_device_anchor($channel_id, $this->item[0]['meta']['anchor']['next']);
         $session->save_next_phpgw_anchor($channel_id, $phpgw_next);
         // save this suggested sync
         $session->set_open_channel($this->item[0]['target']['locuri'], $this->item[0]['source']['locuri'], $channel_id, $status[1], min(isset($this->item[0]['meta']['maxobjsize']) ? $this->item[0]['meta']['maxobjsize'] : 0, SYNCML_MAXOBJSIZE));
         // output alert with phpgw next anchor
         $cmdid = $response->add_alert($status[1], array('trg_uri' => $this->item[0]['source']['locuri'], 'src_uri' => $this->item[0]['target']['locuri'], 'meta' => array('last' => empty($phpgw_last) ? NULL : $phpgw_last, 'next' => $phpgw_next, 'maxobjsize' => SYNCML_MAXOBJSIZE)), (bool) $session->get_var(SYNCML_SUPPORTLARGEOBJS));
     }
 }
    /**
     * Update a mapping.
     *
     * @param $ch_id  Channel ID of mapping. NULL to include all.
     * @param $luid   LUID of mapping. NULL to include all.
     * @param $guid   GUID of mapping. NULL to include all.
     * @return int    Number of mappings updated.
     */
    function update_mapping($ch_id, $luid, $guid, $flag)
    {
        syncml_logger::get_instance()->log("update_mapping({$ch_id}, {$luid}, {$guid}, {$flag})");
        syncml_logger::get_instance()->log(" update_mapping :" . '
				UPDATE phpgw_syncml_mappings SET dirty = \'' . intval($flag) . '\' WHERE ' . (!is_null($ch_id) ? 'channel_id = \'' . $ch_id . '\' AND ' : '') . (!is_null($luid) ? 'luid = \'' . $luid . '\' AND ' : '') . (!is_null($guid) ? 'guid = \'' . $guid . '\' AND ' : '') . '1 = 1');
        $GLOBALS['phpgw']->db->query('
				UPDATE phpgw_syncml_mappings SET dirty = \'' . intval($flag) . '\' WHERE ' . (!is_null($ch_id) ? 'channel_id = \'' . $ch_id . '\' AND ' : '') . (!is_null($luid) ? 'luid = \'' . $luid . '\' AND ' : '') . (!is_null($guid) ? 'guid = \'' . $guid . '\' AND ' : '') . '1 = 1', __LINE__, __FILE__);
        return $GLOBALS['phpgw']->db->affected_rows();
    }
 function handle_success(&$response, &$session)
 {
     $session->account_id = $GLOBALS['phpgw']->session->account_id;
     $session->session_data = $GLOBALS['phpgw']->session->appsession('session_data', 'syncml');
     syncml_logger::get_instance()->log_data("loaded session data", $session->session_data);
     $this->add_authentication_status(SYNCML_STATUS_AUTHENTICATIONACCEPTED, $response, $session);
 }
 /**
  * Merge information about changes from phpgw to the mappings table.
  *
  * @param $except_list Array of GUIDs not to merge.
  * @return int         Number of changes merged.
  */
 function merge_changes($except_list = array())
 {
     syncml_logger::get_instance()->log_data("merge_changes : ipc set  ", is_object($this->ipc) ? 'true' : 'false');
     if (!$this->ipc) {
         return 0;
     }
     $sochannel = new syncml_sochannel();
     list(, , , $last_merge) = $sochannel->get_channel($this->channel_id);
     syncml_logger::get_instance()->log_data("merge_changes last_merge : ", $last_merge);
     $changed_guids = array_diff($this->ipc->getIdList($last_merge), $except_list);
     syncml_logger::get_instance()->log_data("merge_changes changed_guids : ", $changed_guids);
     foreach ($changed_guids as $guid) {
         syncml_logger::get_instance()->log_data("merge_changes guid : ", $guid);
         $this->somappings->update_mapping($this->channel_id, NULL, $guid, 1);
         syncml_logger::get_instance()->log_data("merge_changes update_mapping done  for guids : ", $guid);
     }
     $sochannel->update_last_merge($this->channel_id);
     syncml_logger::get_instance()->log("merge_changes update_last_merge done for " . $this->channel_id);
 }
 /**
  * Save changes on session data back to storage.
  *
  * @access public
  */
 function commit()
 {
     syncml_logger::get_instance()->log_data("saved session data", $this->session_data);
     $GLOBALS['phpgw']->session->appsession('session_data', 'syncml', $this->session_data);
     $sosession = new syncml_sosession();
     $sosession->set_next_nonce($this->id, $this->next_nonce);
 }
    /**
     *
     */
    function set_next_nonce($header, $next_nonce)
    {
        $syncml_hash = $this->generate_session_hash($header);
        syncml_logger::get_instance()->log("set_next_nonce : header = '" . print_r($header, true) . "' next_nonce = {$next_nonce} syncml_hash={$syncml_hash}");
        $GLOBALS['phpgw']->db->query(sprintf('
				UPDATE phpgw_syncml_sessions
				SET next_nonce = \'%s\'
				WHERE
					syncml_hash = \'%s\'', $GLOBALS["phpgw"]->db->db_addslashes($next_nonce), $syncml_hash), __LINE__, __FILE__);
    }