_data = $xml->get_data ("Data"); $this->_meta_format = $xml->get_data ("Meta/Format"); $this->_meta_type = $xml->get_data ("Meta/Type"); } function get_data() { return base64_decode($this->_data); } }; class syncml_header { var $_fields; var $_cred; function syncml_header ($xml) { $this->_fields["VerDTD"] = $xml->get_data("VerDTD"); $this->_fields["VerProto"] = $xml->get_data("VerProto"); $this->_fields["SessionID"] = $xml->get_data("SessionID"); $this->_fields["MsgID"] = $xml->get_data("MsgID"); $this->_fields["Target_LocURI"] = $xml->get_data("Target/LocURI"); $this->_fields["Source_LocURI"] = $xml->get_data("Source/LocURI"); $this->_fields["Meta_MaxMsgSize"] = $xml->get_data("Meta/MaxMsgSize"); if ($xml->get_child ("Cred")) { $this->_cred = new syncml_cred ($xml->get_child ("Cred")); } } function get_field ($name) { return $this->_fields[$name]; } function &get_cred () { return $this->_cred; } }; class syncml_session { var $_datastores; var $_rcvd_msg_id; var $_next_msg_id; // ID of the next message we'll send var $_next_cmd_id; var $_syncml_version; var $_authenticated; var $_username; var $_session_id; var $_target_uri; var $_source_uri; function syncml_session ($datastores) { $this->_datastores = $datastores; $this->_rcvd_msg_id = 0; $this->_next_msg_id = 1; $this->_next_cmd_id = 1; $this->_authenticated = 0; } function handle_next_header ($header, &$response_tree) { // printf ("Next header\n"); $this->_syncml_version = $header->get_field("VerDTD"); $this->_rcvd_msg_id = $header->get_field("MsgID"); $this->_session_id = $header->get_field("SessionID"); $this->_target_uri = $header->get_field("Target_LocURI"); $this->_source_uri = $header->get_field("Source_LocURI"); $this->_next_cmd_id = 1; $response = & $response_tree->new_child ("Status", NULL); $response->new_child_data ("CmdID", NULL, $this->_next_cmd_id++); $response->new_child_data ("MsgRef", NULL, $this->_rcvd_msg_id); $response->new_child_data ("CmdRef", NULL, 0); $response->new_child_data ("Cmd", NULL, "SyncHdr"); $response->new_child_data ("TargetRef", NULL, $header->get_field("Target_LocURI")); $response->new_child_data ("SourceRef", NULL, $this->_source_uri); if ($this->_authenticated) $response->new_child_data ("Data", NULL, 200); else { // Do we have a Cred ? if (!$header->get_cred()) { $response->new_child_data ("Data", NULL, 407); //Credentials missing } else { // Try to authenticate $cred = & $header->get_cred(); $colon_ofs = strcspn ($cred->get_data(), ":"); $this->_username = substr ($cred->get_data(), 0, $colon_ofs); $this->_password = substr ($cred->get_data(), $colon_ofs + 1, strlen ($cred->get_data()) - $colon_ofs - 1); if (check_auth_basic ($this->_username, $this->_password) == 0) { $response->new_child_data ("Data", NULL, 212); //Authorized for session $this->_authenticated = 1; } else { $response->new_child_data ("Data", NULL, 401); // Unauthorized } } } } function make_response_header (&$header) { $header->new_child_data ("VerDTD", NULL, $this->_syncml_version); $header->new_child_data ("VerProto", NULL, "SyncML/" . $this->_syncml_version); $header->new_child_data ("SessionID", NULL, $this->_session_id); $header->new_child_data ("MsgID", NULL, $this->_next_msg_id++); $target = &$header->new_child ("Target", NULL); $source = &$header->new_child ("Source", NULL); $source->new_child_data ("LocURI", NULL, $this->_target_uri); $target->new_child_data ("LocURI", NULL, $this->_source_uri); } function handle_command ($xml_command, &$response_tree) { switch ($xml_command->get_name()) { case "Put": $this->handle_command_put ($xml_command, $response_tree); break; case "Get": $this->handle_command_get ($xml_command, $response_tree); break; case "Alert": $this->handle_command_alert ($xml_command, $response_tree); break; case "Sync": $this->handle_command_sync ($xml_command, $response_tree); break; case "Status": case "Final": break; default: printf ("Unknown SyncML command " . $xml_command->get_name()); } } function handle_command_put ($xml_command, &$response_tree) { $response = & $response_tree->new_child ("Status", NULL); $response->new_child_data ("CmdID", NULL, $this->_next_cmd_id++); $response->new_child_data ("MsgRef", NULL, $this->_rcvd_msg_id); $response->new_child_data ("CmdRef", NULL, $xml_command->get_data ("CmdID")); $response->new_child_data ("Cmd", NULL, "Put"); if ($xml_command->get_data ("Item/Target/LocURI")) $response->new_child_data ("TargetRef", NULL, $xml_command->get_data ("Item/Target/LocURI")); if ($xml_command->get_data ("Item/Source/LocURI")) $response->new_child_data ("SourceRef", NULL, $xml_command->get_data ("Item/Source/LocURI")); $response->new_child_data ("Data", NULL, 200); } function handle_command_alert ($xml_command, &$response_tree) { $response = & $response_tree->new_child ("Status", NULL); $response->new_child_data ("CmdID", NULL, $this->_next_cmd_id++); $response->new_child_data ("MsgRef", NULL, $this->_rcvd_msg_id); $response->new_child_data ("CmdRef", NULL, $xml_command->get_data ("CmdID")); $response->new_child_data ("Cmd", NULL, "Alert"); $remote_next_anchor = $xml_command->get_data ("Item/Meta/Anchor/Next"); $target = $xml_command->get_data ("Item/Target/LocURI"); $response->new_child_data ("TargetRef", NULL, $target); if ($xml_command->get_data ("Item/Source/LocURI")) $response->new_child_data ("SourceRef", NULL, $xml_command->get_data ("Item/Source/LocURI")); $ds = &datastore_search (&$this->_datastores, $target); if ($remote_next_anchor && $ds) { if ($ds["driver"]->start_sync ($this->_username, $this->_password, $this->_source_uri) < 0) { $response->new_child_data ("Data", NULL, 500); // Internal error return; } if ($ds["driver"]->get_anchors (&$local_last_anchor, &$remote_last_anchor, &$local_next_anchor, $remote_next_anchor)) { $response->new_child_data ("Data", NULL, 500); // Internal error return; } // Now choose between normal sync, and slow sync if ($xml_command->get_data ("Item/Meta/Anchor/Last") === $remote_last_anchor) { $status_data = 200; } else { $status_data = 508; } $alert_data = $xml_command->get_data ("Data"); $item = & $response->new_child ("Item", NULL); $data = & $item->new_child ("Data", NULL); $anchor = & $data->new_child ("Anchor", array ("xmlns" => "syncml:metinf")); $anchor->new_child_data ("Next", NULL, $remote_next_anchor); $response->new_child_data ("Data", NULL, $status_data); // Create a new Alert for the client $response = & $response_tree->new_child ("Alert", NULL); $response->new_child_data ("CmdID", NULL, $this->_next_cmd_id++); $response->new_child_data ("Data", NULL, $alert_data); $item = & $response->new_child ("Item", NULL); $target = & $item->new_child ("Target", NULL); $target->new_child_data ("LocURI", NULL, $xml_command->get_data ("Item/Source/LocURI")); $source = & $item->new_child ("Source", NULL); $source->new_child_data ("LocURI", NULL, $xml_command->get_data ("Item/Target/LocURI")); $meta = & $item->new_child ("Meta", NULL); $anchor = & $meta->new_child ("Anchor", array ("xmlns" => "syncml:metinf")); $anchor->new_child_data ("Last", NULL, $local_last_anchor); $anchor->new_child_data ("Next", NULL, $local_next_anchor); } else { $response->new_child_data ("Data", NULL, 412); // Incomplete command } } function handle_command_get ($xml_command, &$response_tree) { $response = & $response_tree->new_child ("Status", NULL); $response->new_child_data ("CmdID", NULL, $this->_next_cmd_id++); $response->new_child_data ("MsgRef", NULL, $this->_rcvd_msg_id); $response->new_child_data ("CmdRef", NULL, $xml_command->get_data ("CmdID")); $response->new_child_data ("Cmd", NULL, "Get"); if ($xml_command->get_data ("Item/Target/LocURI")) $response->new_child_data ("TargetRef", NULL, $xml_command->get_data ("Item/Target/LocURI")); if ($xml_command->get_data ("Item/Source/LocURI")) $response->new_child_data ("SourceRef", NULL, $xml_command->get_data ("Item/Source/LocURI")); // What does the client want ? if (substr ($xml_command->get_data ("Item/Target/LocURI"), 0, 9) == "./devinf1") { $response->new_child_data ("Data", NULL, 200); $response = & $response_tree->new_child ("Results", NULL); $response->new_child_data ("CmdID", NULL, $this->_next_cmd_id++); $response->new_child_data ("MsgRef", NULL, $this->_rcvd_msg_id); $response->new_child_data ("CmdRef", NULL, $xml_command->get_data ("CmdID")); $meta = & $response->new_child ("Meta", NULL); $meta->new_child_data ("Type", array ("xmlns" => "syncml:metinf"), "application/vnd.syncml-devinf+xml"); $item = & $response->new_child ("Item", NULL); $source = & $item->new_child ("Source", NULL); $source->new_child_data ("LocURI", NULL, $xml_command->get_data ("Item/Target/LocURI")); $data = & $item->new_child ("Data", NULL); $devinf = & $data->new_child ("DevInf", array ("xmlns" => "syncml:devinf")); $devinf->new_child_data ("VerDTD", NULL, $this->_syncml_version); $devinf->new_child_data ("Man", NULL, "Nicolas Bougues"); $devinf->new_child_data ("Mod", NULL, "SyncML/PHP server"); $devinf->new_child_data ("DevID", NULL, "12345"); $devinf->new_child_data ("DevTyp", NULL, "server"); // For all the backends we know, send the caps foreach ($this->_datastores as $ds) { $devinf->add_child ($ds["driver"]->get_datastore_caps ($ds["name"])); $devinf->add_child ($ds["driver"]->get_ctcaps ()); } } else { $response->new_child_data ("Data", NULL, 404); } } function handle_command_sync ($xml_command, &$response_tree) { $response = & $response_tree->new_child ("Status", NULL); $response->new_child_data ("CmdID", NULL, $this->_next_cmd_id++); $response->new_child_data ("MsgRef", NULL, $this->_rcvd_msg_id); $response->new_child_data ("CmdRef", NULL, $xml_command->get_data ("CmdID")); $response->new_child_data ("Cmd", NULL, "Sync"); if ($xml_command->get_data ("Target/LocURI")) $response->new_child_data ("TargetRef", NULL, $xml_command->get_data ("Target/LocURI")); if ($xml_command->get_data ("Source/LocURI")) $response->new_child_data ("SourceRef", NULL, $xml_command->get_data ("Source/LocURI")); $ds = &datastore_search (&$this->_datastores, $xml_command->get_data("Target/LocURI")); $children = &$xml_command->children(); for ($i = 0; $i < $xml_command->children_count(); $i++) { $cmd = &$children[$i]; if (($cmd->get_name() == "Replace") || ($cmd->get_name() == "Add") || ($cmd->get_name() == "Delete")) { $sub_response = & $response_tree->new_child ("Status", NULL); $sub_response->new_child_data ("CmdID", NULL, $this->_next_cmd_id++); $sub_response->new_child_data ("MsgRef", NULL, $this->_rcvd_msg_id); $sub_response->new_child_data ("CmdRef", NULL, $cmd->get_data ("CmdID")); $sub_response->new_child_data ("Cmd", NULL, $cmd->get_name()); $sub_response->new_child_data ("SourceRef", NULL, $cmd->get_data ("Item/Source/LocURI")); $res = $ds["driver"]->{"sync_".strtolower($cmd->get_name())} ($cmd->get_data ("Item/Source/LocURI"), $cmd->get_data ("Item/Data")); $sub_response->new_child_data ("Data", NULL, $res); } } $response->new_child_data ("Data", NULL, 200); // Now sends our Sync data $response = & $response_tree->new_child ("Sync", NULL); $response->new_child_data ("CmdID", NULL, $this->_next_cmd_id++); $target = & $response->new_child ("Target", NULL); $target->new_child_data ("LocURI", NULL, $xml_command->get_data ("Source/LocURI")); $source = & $response->new_child ("Source", NULL); $source->new_child_data ("LocURI", NULL, $xml_command->get_data ("Target/LocURI")); $add_list = $ds["driver"]->sync_server_add (); foreach ($add_list as $add) { $cmd = & $response->new_child ("Add", NULL); $cmd->new_child_data ("CmdID", NULL, $this->_next_cmd_id++); $meta = & $cmd->new_child ("Meta", NULL); $meta->new_child_data ("Type", array ("xmlns" => "syncml:metinf"), $ds["driver"]->get_ct()); $item = & $cmd->new_child ("Item", NULL); $source = & $item->new_child ("Source", NULL); $source->new_child_data ("LocURI", NULL, $add["guid"]); $item->new_child_data ("Data", NULL, ""); } $replace_list = $ds["driver"]->sync_server_replace (); foreach ($replace_list as $replace) { $cmd = & $response->new_child ("Replace", NULL); $cmd->new_child_data ("CmdID", NULL, $this->_next_cmd_id++); $meta = & $cmd->new_child ("Meta", NULL); $meta->new_child_data ("Type", array ("xmlns" => "syncml:metinf"), $ds["driver"]->get_ct()); $item = & $cmd->new_child ("Item", NULL); $target = & $item->new_child ("Target", NULL); $target->new_child_data ("LocURI", NULL, $replace["guid"]); $item->new_child_data ("Data", NULL, ""); } $delete_list = $ds["driver"]->sync_server_delete (); foreach ($delete_list as $delete) { $cmd = & $response->new_child ("Delete", NULL); $cmd->new_child_data ("CmdID", NULL, $this->_next_cmd_id++); $meta = & $cmd->new_child ("Meta", NULL); $meta->new_child_data ("Type", array ("xmlns" => "syncml:metinf"), $ds["driver"]->get_ct()); $item = & $cmd->new_child ("Item", NULL); $target = & $item->new_child ("Target", NULL); $target->new_child_data ("LocURI", NULL, $delete); } // Ok, I know, it should be done later. But it's simpler here $ds["driver"]->end_sync(); } }; ?>