Published 2008-10-01 20:15:00
[
{
id: 1, // your data base id (ignored)
uid: "XXXXXXXXXXXXXXXXX",
dtstart: "2008-10-10 12:12:12",
dtend: "2008-10-10 12:12:12",
caltype : 'C',
ical : " <<< Calender data in ical format >>"
// all the other data is optional (if you do not have a ical record, it can build
// most of the event data from properties
privacy: "default";
created: "2008.....",
last_modified: "2008....."
},
......
]
CREATE TABLE `Cal` (Example PHP script (depends on various things, but you should be able to implement your own based on the ideas here)
`id` int(11) NOT NULL auto_increment,
`dtstart` datetime NOT NULL,
`dtend` datetime NOT NULL,
`uid` varchar(32) NOT NULL,
`ical` text NOT NULL,
`privicy` varchar(32) NOT NULL,
`owner` INT(11) NOT NULL,
`calname` VARCHAR(128) NOT NULL DEFAULT '',
`last_modified` datetime default NULL,
`created` datetime default NULL,
caltype VARCHAR(1) DEFAULT 'C',
PRIMARY KEY (`id`),
KEY `new_index` (`dtstart`,`dtend`,`uid`, owner, calname),
KEY `uid` (`uid`)
)
class Cal extends HTML_FlexyFrameWork_Page
{
function sendFail($msg= "empty")
{
header('WWW-Authenticate: Basic realm="Calendar"');
header('HTTP/1.0 401 Unauthorized');
echo $msg;
exit;
}
function getAuth()
{
// use basic auth..
$u = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : "";
$p = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : "";
if (empty($u) || empty($p)) {
$this->sendFail();
}
// check the password combo...
$bits = explode('@', $u);
$d = DB_DataObject::factory('Domains');
$d->is_external = 0;
if (!$d->get('domain', $bits[1])) {
$this->sendFail('invalid domain');
}
$user =DB_DataObject::factory('Users');
$user->dom_id = $d->id;
$user->login = $bits[0];
if (!$user->find(true)) {
$this->sendFail('invalid user');
}
if ($user->checkPassword($p)) {
$this->authUser = $user;
return true;
//exit;
}
$this->sendFail('invalid details');
}
function get()
{
if (isset($_GET['POST'])) {
return $this->post();
}
$start =empty($_REQUEST['aRangeStart']) ?
"1970-01-01" : $_REQUEST['aRangeStart'];
$end =empty($_REQUEST['aRangeEnd']) ?
date("Y-m-d",strtotime("TODAY + 1 DAY")) : $_REQUEST['aRangeEnd'];
$d = DB_DataObject::factory('Cal');
if (!empty($_REQUEST['aRangeStart'])) {
$d->whereAdd("dtstart >= '". date("Y-m-d H:i:s", strtotime($start)) ."'");
$d->whereAdd("dtend <= '". date("Y-m-d H:i:s", strtotime($end)) ."'");
$d->caltype = 'C';
} else {
$d->caltype = 'T'; // task..
}
if (!empty($_REQUEST['aCount']) && ($_REQUEST['aCount'] * 1)> 0) {
$d->limit(1 * $_REQUEST['aCount']);
}
$d->owner = $this->authUser->id;
$d->find();
$ret = array();
while ($d->fetch()) {
if (empty($d->uid)) {
$d->uid = md5("mailfort". $d->id); // gen our uid.
}
$ret[] = $d->toArray();
}
require_once 'Services/JSON.php';
$json = new Services_JSON();
echo $json->encode( $ret);
exit;
}
function post()
{
$d = DB_DataObject::factory('Cal');
if (!empty($_REQUEST['_delete_'])) {
$d->owner = $this->authUser->id;
if (! $d->get('uid', $_REQUEST['_delete_'])) {
die("OOPPS");
}
$d->delete();
echo "OK";
exit;
}
if (!empty($_REQUEST['uid'])) {
$d->owner = $this->authUser->id;
if (!$d->get('uid', $_REQUEST['uid'])) {
die("OOPS");
}
}
// check for id...
$d->setFrom($_REQUEST);
$d->owner = $this->authUser->id;
if (empty($d->caltype)) {
if (empty($_REQUEST['dtstart'])) {
$d->caltype = 'T';
} else {
$d->caltype = 'C';
}
}
if (!$d->id) {
$d->insert();
$d->uid = md5("cal". $d->id);
}
$d->update();
require_once 'Services/JSON.php';
$json = new Services_JSON();
echo $json->encode($d->toArray());
exit;
}
}