$qry->Begin(); $etag = md5($request->raw_post); $params = array(':user_no' => $dest->GetProperty('user_no'), ':dav_name' => $dest->bound_from(), ':etag' => $etag, ':dav_data' => $request->raw_post, ':session_user' => $session->user_no); if ($dest->Exists()) { $sql = 'UPDATE caldav_data SET caldav_data=:dav_data, dav_etag=:etag, logged_user=:session_user, modified=current_timestamp, user_no=:user_no, caldav_type=\'VCARD\' WHERE dav_name=:dav_name'; $response_code = 200; $qry->QDo($sql, $params); $qry->QDo("SELECT dav_id FROM caldav_data WHERE dav_name = :dav_name ", array(':dav_name' => $params[':dav_name'])); } else { $sql = 'INSERT INTO caldav_data ( user_no, dav_name, dav_etag, caldav_data, caldav_type, logged_user, created, modified, collection_id ) VALUES( :user_no, :dav_name, :etag, :dav_data, \'VCARD\', :session_user, current_timestamp, current_timestamp, :collection_id )'; $params[':collection_id'] = $collection_id; $response_code = 201; $qry->QDo($sql, $params); $qry->QDo("SELECT currval('dav_id_seq') AS dav_id"); } $row = $qry->Fetch(); require_once 'vcard.php'; $vcard = new vCard($request->raw_post); $vcard->Write($row->dav_id, $dest->Exists()); $qry->QDo("SELECT write_sync_change( {$collection_id}, {$response_code}, :dav_name)", array(':dav_name' => $dest->bound_from())); if (!$qry->Commit()) { $qry->Rollback(); $request->DoResponse(500, "A database error occurred"); } header('ETag: "' . $etag . '"'); if ($response_code == 200) { $response_code = 204; } $request->DoResponse($response_code);
/** * This function will import a whole calendar * @param string $ics_content the ics file to import * @param int $user_no the user wich will receive this ics file * @param string $path the $path where it will be store such as /user_foo/home/ * @param boolean $caldav_context Whether we are responding via CalDAV or interactively * * Any VEVENTs with the same UID will be concatenated together */ function import_addressbook_collection($vcard_content, $user_no, $path, $caldav_context, $appending = false) { global $c, $session; // We hack this into an enclosing component because vComponent only expects a single root component $addressbook = new vComponent("BEGIN:ADDRESSES\r\n" . $vcard_content . "\r\nEND:ADDRESSES\r\n"); require_once 'vcard.php'; $sql = 'SELECT * FROM collection WHERE dav_name = :dav_name'; $qry = new AwlQuery($sql, array(':dav_name' => $path)); if (!$qry->Exec('PUT', __LINE__, __FILE__)) { rollback_on_error($caldav_context, $user_no, $path); } if (!$qry->rows() == 1) { dbg_error_log('ERROR', ' PUT: Collection does not exist at "%s" for user %d', $path, $user_no); rollback_on_error($caldav_context, $user_no, $path); } $collection = $qry->Fetch(); if (!(isset($c->skip_bad_event_on_import) && $c->skip_bad_event_on_import)) { $qry->Begin(); } $base_params = array(':collection_id' => $collection->collection_id, ':session_user' => $session->user_no, ':caldav_type' => 'VCARD'); if (!$appending) { if (!$qry->QDo('DELETE FROM caldav_data WHERE collection_id = :collection_id', $base_params)) { rollback_on_error($caldav_context, $user_no, $collection->collection_id); } } $dav_data_insert = <<<EOSQL INSERT INTO caldav_data ( user_no, dav_name, dav_etag, caldav_data, caldav_type, logged_user, created, modified, collection_id ) VALUES( :user_no, :dav_name, :etag, :dav_data, :caldav_type, :session_user, :created, :modified, :collection_id ) EOSQL; $resources = $addressbook->GetComponents(); foreach ($resources as $k => $resource) { if (isset($c->skip_bad_event_on_import) && $c->skip_bad_event_on_import) { $qry->Begin(); } $vcard = new vCard($resource->Render()); $uid = $vcard->GetPValue('UID'); if (empty($uid)) { $uid = uuid(); $vcard->AddProperty('UID', $uid); } $last_modified = $vcard->GetPValue('REV'); if (empty($last_modified)) { $last_modified = gmdate('Ymd\\THis\\Z'); $vcard->AddProperty('REV', $last_modified); } $created = $vcard->GetPValue('X-CREATED'); if (empty($last_modified)) { $created = gmdate('Ymd\\THis\\Z'); $vcard->AddProperty('X-CREATED', $created); } $rendered_card = $vcard->Render(); $dav_data_params = $base_params; $dav_data_params[':user_no'] = $user_no; // We don't allow any of &?\/@%+: in the UID to appear in the path, but anything else is fair game. $dav_data_params[':dav_name'] = sprintf('%s%s.ics', $path, preg_replace('{[&?\\/@%+:]}', '', $uid)); $dav_data_params[':etag'] = md5($rendered_card); $dav_data_params[':dav_data'] = $rendered_card; $dav_data_params[':modified'] = $last_modified; $dav_data_params[':created'] = $created; if (isset($c->skip_bad_event_on_import) && $c->skip_bad_event_on_import) { $qry->Begin(); } if (!$qry->QDo($dav_data_insert, $dav_data_params)) { rollback_on_error($caldav_context, $user_no, $path); } $qry->QDo('SELECT dav_id FROM caldav_data WHERE dav_name = :dav_name ', array(':dav_name' => $dav_data_params[':dav_name'])); if ($qry->rows() == 1 && ($row = $qry->Fetch())) { $dav_id = $row->dav_id; } $vcard->Write($row->dav_id, false); if (isset($c->skip_bad_event_on_import) && $c->skip_bad_event_on_import) { $qry->Commit(); } } if (!(isset($c->skip_bad_event_on_import) && $c->skip_bad_event_on_import)) { if (!$qry->Commit()) { rollback_on_error($caldav_context, $user_no, $path); } } }