_data = $data; } function db_connect() { global $db_server, $db_username, $db_password, $db_name; $this->db = mysql_pconnect ($db_server, $db_username, $db_password); mysql_select_db ($db_name, $this->db); } // Checks username and password // Saves everything for the laster stages of the sync // Returns -1 on error, or 0 if OK function start_sync ($username, $password, $source_uri) { $this->db_connect(); $q = mysql_query (sprintf ("SELECT * FROM users ". "WHERE name = '%s' AND password = '%s'", mysql_escape_string ($username), mysql_escape_string ($password)), $this->db); if (mysql_num_rows ($q) == 0) { // User not found or incorrect password return -1; } else { $row = mysql_fetch_array ($q); $this->_user_id = $row["user_id"]; $q = mysql_query (sprintf ("SELECT * FROM devices ". "WHERE uri = '%s' AND user_id = %d", mysql_escape_string ($source_uri), $this->_user_id), $this->db); $row = mysql_fetch_array ($q); if ($row) { $this->_device_id = $row["device_id"]; } else { $q = mysql_query (sprintf ("INSERT INTO devices (user_id, uri) ". "VALUES (%d, '%s')", $this->_user_id, $source_uri), $this->db); $this->_device_id = mysql_insert_id (); } return 0; } } // This function has to return, for a specific user and device pair, // the last sync "anchors" (timestamps), for us and the remote device, // in order to be sure that both us and the remote haven't suffered // some major problem in the meantime. // Returns our next anchor too. function get_anchors (&$local_last_anchor, &$remote_last_anchor, &$local_next_anchor, $remote_next_anchor) { $this->db_connect(); $q = mysql_query (sprintf ("SELECT * FROM %ss_syncs " . "WHERE device_id = %d", $this->_data, $this->_device_id), $this->db); if (mysql_num_rows($q) == 0) { // We'll have to do a full (slow) sync $this->_local_last_anchor = $this->_remote_last_anchor = $local_last_anchor = $remote_last_anchor = 0; } else { $row = mysql_fetch_array ($q); $this->_local_last_anchor = $local_last_anchor = $row["local_last_anchor"]; $this->_remote_last_anchor = $remote_last_anchor = $row["remote_last_anchor"]; } $this->_local_next_anchor = $local_next_anchor = time(); $this->_remote_next_anchor = $remote_next_anchor; return 0; } // Returns 201 on success // 418 already exists // 500 failed function sync_add ($client_uid, $item) { $this->db_connect(); // First, try to find the item $q = mysql_query (sprintf ("SELECT * FROM %ss_maps ". "LEFT JOIN %ss ". "ON %ss_maps.%s_id = %ss.%s_id " . "WHERE %ss_maps.device_id = %d ". "AND %ss_maps.remote_id = '%s'", $this->_data, $this->_data, $this->_data, $this->_data, $this->_data, $this->_data, $this->_data, $this->_device_id, $this->_data, mysql_escape_string ($client_uid)), $this->db); if (mysql_num_rows ($q)) { return 418; // it exists already } else { // This should be a transaction... mysql_query (sprintf ("INSERT INTO %ss (user_id, %s, creation_ts, ". "update_ts) ". "VALUES (%d, '%s', %d, %d)", $this->_data, $this->_data, $this->_user_id, mysql_escape_string ($item), $this->_local_next_anchor, $this->_local_next_anchor), $this->db); $item_id = mysql_insert_id (); mysql_query (sprintf ("INSERT INTO %ss_maps ". "(device_id, %s_id, remote_id) ". "VALUES (%d, %d, '%s')", $this->_data, $this->_data, $this->_device_id, $item_id, mysql_escape_string ($client_uid)), $this->db); } return 201; } // Deletes an entry // 200 OK // 211 Not deleted (doesn't exist) function sync_delete ($client_uid, $null) { $this->db_connect(); // First, try to find the item $q = mysql_query (sprintf ("SELECT * FROM %ss_maps ". "LEFT JOIN %ss ". "ON %ss_maps.%s_id = %ss.%s_id " . "WHERE %ss_maps.device_id = %d ". "AND %ss_maps.remote_id = '%s'", $this->_data, $this->_data, $this->_data, $this->_data, $this->_data, $this->_data, $this->_data, $this->_device_id, $this->_data, mysql_escape_string ($client_uid)), $this->db); if (mysql_num_rows ($q)) { $row = mysql_fetch_array ($q); $item_id = $row[$this->_data . "_id"]; // Delete the item mysql_query (sprintf ("DELETE FROM %ss WHERE %s_id = %d", $this->_data, $this->_data, $item_id), $this->db); // Delete its reference for the current device mysql_query (sprintf ("DELETE FROM %ss_maps ". "WHERE %s_id = %d AND device_id = %d", $this->_data, $this->_data, $item_id, $this->_device_id), $this->db); // Note that we don't delete *all* the references for any other device, // so that when other devices synchronize, we can tell them to delete // the item return 200; } else { return 218; } } // Called when the client sends us an updated (or maybe new) item // Returns 200 OK // 500 Failed function sync_replace ($client_uid, $item) { $this->db_connect(); // First, try to find the item $q = mysql_query (sprintf ("SELECT * FROM %ss_maps ". "LEFT JOIN %ss ". "ON %ss_maps.%s_id = %ss.%s_id " . "WHERE %ss_maps.device_id = %d ". "AND %ss_maps.remote_id = '%s'", $this->_data, $this->_data, $this->_data, $this->_data, $this->_data, $this->_data, $this->_data, $this->_device_id, $this->_data, mysql_escape_string ($client_uid)), $this->db); if (mysql_num_rows ($q)) { $row = mysql_fetch_array ($q); if ($row["update_ts"] > $this->_local_last_anchor) { // In the meantime, the item has been updated on the server as well // There is a conflict. As of now, do nothing, use data from the client } mysql_query (sprintf ("UPDATE %ss SET %s = '%s', update_ts = %d ". "WHERE %s_id = %d", $this->_data, $this->_data, mysql_escape_string ($item), $this->_local_next_anchor, $this->_data, $row[$this->_data . "_id"]), $this->db); } else { // This item doesn't exist, insert it return $this->sync_add ($client_uid, $item); } return 200; } // Returns the newly server-added items // Returns an array of arrays with : // array ( array ("guid" => xx, "item" => "..."), ...) function sync_server_add () { // How to find the added data ? Simple... It's the data that has // an update_ts > to the local_last_anchor for this device $this->db_connect(); $q = mysql_query (sprintf ("SELECT * FROM %ss ". "WHERE user_id = %d AND creation_ts > %d " . " AND creation_ts != %d", $this->_data, $this->_user_id, $this->_local_last_anchor, $this->_local_last_anchor), $this->db); $ret = array(); while ($row = mysql_fetch_array ($q)) { $ret[] = array ("guid" => $row[$this->_data . "_id"], "item" => $row[$this->_data]); } return $ret; } function sync_server_replace () { $this->db_connect(); $q = mysql_query (sprintf ("SELECT * FROM %ss ". "WHERE user_id = %d AND update_ts > %d " . " AND update_ts != %d" . " AND update_ts != creation_ts", $this->_data, $this->_user_id, $this->_local_last_anchor, $this->_local_next_anchor), $this->db); $ret = array(); while ($row = mysql_fetch_array ($q)) { $ret[] = array ("guid" => $row[$this->_data . "_id"], "item" => $row[$this->_data]); } return $ret; } // Returns a set of device UIDs // array (uid1, uid2, ...) function sync_server_delete () { $this->db_connect (); // Finds entries for which we have a map entry, but no main item entry $q = mysql_query (sprintf ("SELECT %ss_maps.remote_id, %ss_maps.%s_id ". "FROM %ss_maps ". "LEFT JOIN %ss ON %ss.%s_id = %ss_maps.%s_id ". "WHERE ISNULL(%ss.%s_id)", $this->_data, $this->_data, $this->_data, $this->_data, $this->_data, $this->_data, $this->_data, $this->_data, $this->_data, $this->_data, $this->_data, $this->_data), $this->db); $ret = array (); while ($row = mysql_fetch_array ($q)) { $ret[] = $row["remote_id"]; $this->_del_items[] = $row[$this->_data . "_id"]; } return $ret; } // Function called when the sync process is over. Save the new anchors. function end_sync () { $this->db_connect(); if (isset($this->_del_items)) { foreach ($this->_del_items as $item) { if (isset($where)) $where .= " OR "; else $where = "WHERE device_id = " . $this->_device_id . " AND ("; $where .= $this->_data . "_id = " . $item; } $where .= ")"; // Deletes the items we sent as "deleted" mysql_query (sprintf ("DELETE FROM %ss_maps ".$where, $this->_data), $this->db); } mysql_query (sprintf ("UPDATE %ss_syncs ". "SET local_last_anchor=%d, ". " remote_last_anchor = '%s' ". "WHERE device_id = %d", $this->_data, $this->_local_next_anchor, mysql_escape_string ($this->_remote_next_anchor), $this->_device_id), $this->db); if (mysql_affected_rows() == 0) { mysql_query (sprintf ("INSERT INTO %ss_syncs ". "SET local_last_anchor=%d, ". " remote_last_anchor = '%s', ". " device_id = %d", $this->_data, $this->_local_next_anchor, mysql_escape_string ($this->_remote_next_anchor), $this->_device_id), $this->db); } return 0; } }; ?>