+
+
\ No newline at end of file
diff --git a/app/Gdoo/Approach/views/approach/dialog.blade.php b/app/Gdoo/Approach/views/approach/dialog.blade.php
new file mode 100644
index 00000000..943bdcbf
--- /dev/null
+++ b/app/Gdoo/Approach/views/approach/dialog.blade.php
@@ -0,0 +1,188 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Approach/views/approach/index.blade.php b/app/Gdoo/Approach/views/approach/index.blade.php
new file mode 100644
index 00000000..3f820bca
--- /dev/null
+++ b/app/Gdoo/Approach/views/approach/index.blade.php
@@ -0,0 +1,83 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Approach/views/approach/print.blade.php b/app/Gdoo/Approach/views/approach/print.blade.php
new file mode 100644
index 00000000..ca2d71da
--- /dev/null
+++ b/app/Gdoo/Approach/views/approach/print.blade.php
@@ -0,0 +1 @@
+{{$form['tpl']}}
\ No newline at end of file
diff --git a/app/Gdoo/Approach/views/approach/product.blade.php b/app/Gdoo/Approach/views/approach/product.blade.php
new file mode 100644
index 00000000..abffa7d7
--- /dev/null
+++ b/app/Gdoo/Approach/views/approach/product.blade.php
@@ -0,0 +1,33 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Approach/views/approach/reference.blade.php b/app/Gdoo/Approach/views/approach/reference.blade.php
new file mode 100644
index 00000000..60c034a1
--- /dev/null
+++ b/app/Gdoo/Approach/views/approach/reference.blade.php
@@ -0,0 +1,121 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Approach/views/approach/serviceCostDetail.blade.php b/app/Gdoo/Approach/views/approach/serviceCostDetail.blade.php
new file mode 100644
index 00000000..15a541a2
--- /dev/null
+++ b/app/Gdoo/Approach/views/approach/serviceCostDetail.blade.php
@@ -0,0 +1,124 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Approach/views/approach/serviceReview.blade.php b/app/Gdoo/Approach/views/approach/serviceReview.blade.php
new file mode 100644
index 00000000..eaf7d104
--- /dev/null
+++ b/app/Gdoo/Approach/views/approach/serviceReview.blade.php
@@ -0,0 +1,179 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Approach/views/market/create.blade.php b/app/Gdoo/Approach/views/market/create.blade.php
new file mode 100644
index 00000000..db7f6d80
--- /dev/null
+++ b/app/Gdoo/Approach/views/market/create.blade.php
@@ -0,0 +1,3 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Approach/views/market/dialog.blade.php b/app/Gdoo/Approach/views/market/dialog.blade.php
new file mode 100644
index 00000000..44e8f667
--- /dev/null
+++ b/app/Gdoo/Approach/views/market/dialog.blade.php
@@ -0,0 +1,109 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Approach/views/market/index.blade.php b/app/Gdoo/Approach/views/market/index.blade.php
new file mode 100644
index 00000000..3816b88d
--- /dev/null
+++ b/app/Gdoo/Approach/views/market/index.blade.php
@@ -0,0 +1,59 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Approach/views/review/create.blade.php b/app/Gdoo/Approach/views/review/create.blade.php
new file mode 100644
index 00000000..95b9a01c
--- /dev/null
+++ b/app/Gdoo/Approach/views/review/create.blade.php
@@ -0,0 +1,120 @@
+
+
+
+
+ {{$form['btn']}}
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Approach/views/review/feeDetail.blade.php b/app/Gdoo/Approach/views/review/feeDetail.blade.php
new file mode 100644
index 00000000..24ac3a2e
--- /dev/null
+++ b/app/Gdoo/Approach/views/review/feeDetail.blade.php
@@ -0,0 +1,34 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Approach/views/review/index.blade.php b/app/Gdoo/Approach/views/review/index.blade.php
new file mode 100644
index 00000000..4745fd80
--- /dev/null
+++ b/app/Gdoo/Approach/views/review/index.blade.php
@@ -0,0 +1,59 @@
+{{$header["js"]}}
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Article/views/article/index.blade.php b/app/Gdoo/Article/views/article/index.blade.php
new file mode 100644
index 00000000..4745fd80
--- /dev/null
+++ b/app/Gdoo/Article/views/article/index.blade.php
@@ -0,0 +1,59 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Article/views/article/query.blade.php b/app/Gdoo/Article/views/article/query.blade.php
new file mode 100644
index 00000000..f4fd0a86
--- /dev/null
+++ b/app/Gdoo/Article/views/article/query.blade.php
@@ -0,0 +1,23 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Article/views/article/reader.blade.php b/app/Gdoo/Article/views/article/reader.blade.php
new file mode 100644
index 00000000..58562508
--- /dev/null
+++ b/app/Gdoo/Article/views/article/reader.blade.php
@@ -0,0 +1,43 @@
+
+
+ @if($rows['data'])
+ @foreach($rows['data'] as $row)
+
+
+ {{$loop->index + 1}}
+
+
+ {{$row['name']}}
+
+
+ {{$row['department']}}
+
+
+ @datetime($row['created_at'])
+
+
+ @endforeach
+ @endif
+
\ No newline at end of file
diff --git a/app/Gdoo/Article/views/article/show.blade.php b/app/Gdoo/Article/views/article/show.blade.php
new file mode 100644
index 00000000..d2347767
--- /dev/null
+++ b/app/Gdoo/Article/views/article/show.blade.php
@@ -0,0 +1,38 @@
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Article/views/widget/index.blade.php b/app/Gdoo/Article/views/widget/index.blade.php
new file mode 100644
index 00000000..9a892e4b
--- /dev/null
+++ b/app/Gdoo/Article/views/widget/index.blade.php
@@ -0,0 +1,32 @@
+
+
+
+
标题
+
发布时间
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Article/views/widget/info.blade.php b/app/Gdoo/Article/views/widget/info.blade.php
new file mode 100644
index 00000000..dd73358d
--- /dev/null
+++ b/app/Gdoo/Article/views/widget/info.blade.php
@@ -0,0 +1,15 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Calendar/Controllers/CalendarController.php b/app/Gdoo/Calendar/Controllers/CalendarController.php
new file mode 100644
index 00000000..c2b7ec9e
--- /dev/null
+++ b/app/Gdoo/Calendar/Controllers/CalendarController.php
@@ -0,0 +1,135 @@
+where('leader_id', $user_id)->get(['id', 'department_id', 'name']);
+ $departments = Department::orderBy('lft', 'asc')->get()->toNested()->toArray();
+ $underling = array();
+ foreach ($users as $row) {
+ $underling['role'][$row['department_id']] = $departments[$row['department_id']];
+ $underling['user'][$row['department_id']][$row['id']] = $row;
+ }
+ $user = User::find($user_id);
+
+ return $this->display([
+ 'user' => $user,
+ 'underling' => $underling,
+ ]);
+ }
+
+ /**
+ * 日历列表
+ */
+ public function calendarsAction()
+ {
+ $user_id = Request::get('user_id', Auth::id());
+ $calendars = CalendarService::getCalendars($user_id);
+
+ $calendars[] = [
+ 'id' => 'shared',
+ 'displayname' => '共享事件',
+ 'calendarcolor' => '#999',
+ ];
+ $sources = [];
+ foreach ($calendars as $calendar) {
+ if ($calendar['id'] == 'shared') {
+ $url = url('event/share', ['user_id'=>$user_id]);
+ } else {
+ $url = url('event/index', ['calendar_id'=>$calendar['id']]);
+ }
+ $sources[] = [
+ 'url' => $url,
+ 'id' => $calendar['id'],
+ 'userid' => $calendar['userid'],
+ 'backgroundColor' => $calendar['calendarcolor'],
+ "borderColor" => $calendar['calendarcolor'],
+ ];
+ }
+ return $this->json([
+ 'calendars' => $calendars,
+ 'sources' => $sources,
+ ], true);
+ }
+
+ public function activeAction()
+ {
+ if (Request::method() == 'POST') {
+ $gets = Request::all();
+ $calendar = CalendarService::getCalendar($gets['id'], true);
+ if ($calendar) {
+ try {
+ CalendarService::setCalendarActive($gets['id'], $gets['active']);
+ } catch (\Exception $e) {
+ return $this->json($e->getMessage());
+ }
+ /*} else {
+ return $this->json('permission denied');
+ */
+ }
+ $calendar = CalendarService::getCalendar($gets['id'], false);
+ return $this->json([
+ 'active' => $gets['active'],
+ 'eventSource' => array(
+ 'id' => $calendar['id'],
+ 'url' => url('event/index', ['calendar_id' => $calendar['id']]),
+ 'backgroundColor' => $calendar['calendarcolor'],
+ "borderColor" => $calendar['calendarcolor'],
+ )
+ ], true);
+ }
+ }
+
+ // 添加日历
+ public function addAction()
+ {
+ $gets = Request::all();
+ if (Request::method() == 'POST') {
+ if ($gets['id'] > 0) {
+ $id = CalendarService::editCalendar($gets['id'], $gets['displayname'], null, null, null, $gets['calendarcolor']);
+ } else {
+ $id = CalendarService::addCalendar(Auth::id(), $gets['displayname'], 'VEVENT,VTODO,VJOURNAL', null, 0, $gets['calendarcolor']);
+ }
+ return $this->json(['id' => $id], true);
+ }
+ $calendar = CalendarService::getCalendar((int)$gets['id']);
+ return $this->render(array(
+ 'calendar' => $calendar,
+ ));
+ }
+
+ // 帮助信息
+ public function helpAction()
+ {
+ return $this->render();
+ }
+
+ // 删除日历
+ public function deleteAction()
+ {
+ $id = Request::get('id');
+ if ($id > 0) {
+ CalendarService::deleteCalendar($id);
+ return $this->json(['id'=>$id], true);
+ }
+ }
+}
diff --git a/app/Gdoo/Calendar/Controllers/EventController.php b/app/Gdoo/Calendar/Controllers/EventController.php
new file mode 100644
index 00000000..a3e240ec
--- /dev/null
+++ b/app/Gdoo/Calendar/Controllers/EventController.php
@@ -0,0 +1,749 @@
+keyBy('id');
+
+ // 普通事件
+ $rows = CalendarService::getRangeEvents($ids, $gets['start'], $gets['end']);
+ foreach ($rows as $row) {
+ $calendar = $cals[$row['calendarid']];
+ if ($calendar['active'] == 1) {
+ $row['backgroundColor'] = $calendar['calendarcolor'];
+ $row['borderColor'] = $calendar['calendarcolor'];
+ $row['userid'] = $calendar['userid'];
+ $events[] = $row;
+ }
+ }
+
+ // 获取共享事件
+ $shared = ShareService::getItemsSourceBy(['event'], $gets['user_id']);
+ $share_id = Arr::pluck($shared, 'source_id');
+ if (count($share_id)) {
+ $share = Arr::pluck($shared, 'name', 'source_id');
+ $rows = CalendarService::getRangeEvents($share_id, $gets['start'], $gets['end'], true);
+ foreach ($rows as $row) {
+ $row['title'] = '['.$share[$row['id']].']'.$row['title'];
+ $row['backgroundColor'] = '#666666';
+ $row['borderColor'] = '#666666';
+ $row['shared'] = true;
+ $events[] = $row;
+ }
+ }
+ return response()->json($events);
+ }
+
+ // 客户端显示事件列表
+ public function itemsAction()
+ {
+ $gets = Request::all();
+ $start = strtotime($gets['start']);
+ $end = strtotime($gets['end']) + 86400;
+
+ $items = [];
+
+ // 读取共享事件
+ $shared = ShareService::getItemsSourceBy(['event'], auth()->id());
+ $share_id = Arr::pluck($shared, 'source_id');
+ $share = Arr::pluck($shared, 'name', 'source_id');
+
+ $events = CalendarService::getRangeEvents($share_id, $gets['start'], $gets['end'], true);
+
+ foreach ($events as $key => $row) {
+ $master = [
+ 'id' => $row['id'],
+ 'title' => '['.$share[$row['id']].']'.$row['title'],
+ 'start' => $row['start'],
+ 'end' => $row['end'],
+ 'allday' => $row['allDay'],
+ 'calendar' => [
+ 'color' => '#666666',
+ 'name' => '共享事件',
+ ],
+ ];
+
+ $repeat = CalendarObjectService::getEventRepeat($master, '1D', 'Y-n-j');
+ $items = array_merge($items, $repeat);
+ }
+
+ // 读取正常事件
+ $rows = DB::table('calendar_object')
+ ->leftJoin('calendar', 'calendar_object.calendarid', '=', 'calendar.id')
+ ->where('calendar.userid', auth()->id())
+ ->whereRaw('(
+ (calendar_object.firstoccurence between '.$start.' and '.$end.' or calendar_object.lastoccurence between '.$start.' and '.$end.')
+ or (calendar_object.rrule = 1 and calendar_object.firstoccurence <= '.$end.')
+ )')
+ ->get(['calendar_object.*','calendar.calendarcolor','calendar.displayname']);
+
+ foreach ($rows as $key => $row) {
+ $vcalendar = \Sabre\VObject\Reader::read($row['calendardata']);
+ $allday = ($vcalendar->VEVENT->DTSTART->getDateType() == \Sabre\VObject\Property\DateTime::DATE) ? true : false;
+ $master = [
+ 'id' => $row['id'],
+ 'title' => $vcalendar->VEVENT->SUMMARY->value,
+ 'start' => $vcalendar->VEVENT->DTSTART->value,
+ 'end' => $vcalendar->VEVENT->DTEND->value,
+ 'allday' => $allday,
+ 'calendar' => [
+ 'color' => $row['calendarcolor'],
+ 'name' => $row['displayname'],
+ ],
+ ];
+ $repeat = CalendarObjectService::getEventRepeat($master, '1D', 'Y-n-j');
+ $items = array_merge($items, $repeat);
+ }
+ return response()->json($items);
+ }
+
+ // 调整事件
+ public function resizeAction()
+ {
+ if (Request::method() == 'POST') {
+ $gets = Request::all();
+ $event = CalendarService::getEvent($gets['id']);
+
+ if ($event['lastmodified'] != $gets['lastmodified']) {
+ return $this->json('事件已被修改。');
+ }
+
+ $vcalendar = VObject::parse($event['calendardata']);
+ $vevent = $vcalendar->VEVENT;
+
+ /*
+ $accessclass = $vevent->getAsString('CLASS');
+ $permissions = CalendarService::getPermissions($id, Calendar::EVENT, $accessclass);
+ if(!$permissions & OCP\PERMISSION_UPDATE) {
+ return $this->json('permission denied');
+ }
+ */
+
+ $delta = new \DateInterval('P0D');
+ $delta->s = $gets['delta'];
+
+ $dtend = CalendarService::getDTEndFromVEvent($vevent);
+ $end_type = $dtend->getDateType();
+ $dtend->setDateTime($dtend->getDateTime()->add($delta), $end_type);
+ unset($vevent->DURATION);
+
+ $vevent->setDateTime('LAST-MODIFIED', 'now', \Sabre\VObject\Property\DateTime::UTC);
+ $vevent->setDateTime('DTSTAMP', 'now', \Sabre\VObject\Property\DateTime::UTC);
+
+ CalendarService::edit($gets['id'], $vcalendar->serialize());
+
+ $lastmodified = $vevent->__get('LAST-MODIFIED')->getDateTime();
+ return $this->json(['lastmodified' => $lastmodified->format('U')], true);
+ }
+ }
+
+ // 移动事件
+ public function moveAction()
+ {
+ if (Request::method() == 'POST') {
+ $gets = Request::all();
+ $event = CalendarService::getEvent($gets['id']);
+
+ if ($event['lastmodified'] != $gets['lastmodified']) {
+ return $this->json('事件已被修改。');
+ }
+
+ $vcalendar = VObject::parse($event['calendardata']);
+ $vevent = $vcalendar->VEVENT;
+
+ $allday = $gets['allday'] == 'true' ? 1 : 0;
+ $delta = new \DateInterval('P0D');
+ $delta->s = $gets['delta'];
+
+ $dtstart = $vevent->DTSTART;
+ $dtend = CalendarService::getDTEndFromVEvent($vevent);
+ $start_type = $dtstart->getDateType();
+ $end_type = $dtend->getDateType();
+
+ if ($allday && $start_type != \Sabre\VObject\Property\DateTime::DATE) {
+ $start_type = $end_type = \Sabre\VObject\Property\DateTime::DATE;
+ $dtend->setDateTime($dtend->getDateTime()->modify('+1 day'), $end_type);
+ }
+
+ if (!$allday && $start_type == \Sabre\VObject\Property\DateTime::DATE) {
+ $start_type = $end_type = \Sabre\VObject\Property\DateTime::LOCALTZ;
+ }
+
+ $dtstart->setDateTime($dtstart->getDateTime()->add($delta), $start_type);
+ $dtend->setDateTime($dtend->getDateTime()->add($delta), $end_type);
+ unset($vevent->DURATION);
+
+ $vevent->setDateTime('LAST-MODIFIED', 'now', \Sabre\VObject\Property\DateTime::UTC);
+ $vevent->setDateTime('DTSTAMP', 'now', \Sabre\VObject\Property\DateTime::UTC);
+
+ try {
+ CalendarService::edit($gets['id'], $vcalendar->serialize());
+ } catch (\Exception $e) {
+ return $this->json($e->getMessage());
+ }
+ $lastmodified = $vevent->__get('LAST-MODIFIED')->getDateTime();
+ return $this->json(['lastmodified' => $lastmodified->format('U')], true);
+ }
+ }
+
+ public function addAction()
+ {
+ $gets = Request::all();
+ // 更新数据
+ if (Request::method() == 'POST') {
+ $error = CalendarService::validateRequest($gets);
+ if ($error) {
+ return $this->json($error);
+ }
+ $vcalendar = CalendarService::createVCalendarFromRequest($gets);
+ try {
+ $attachment = join(',', array_filter((array)$gets['attachment']));
+ $id = CalendarService::add($gets['calendarid'], $vcalendar->serialize(), $attachment);
+ AttachmentService::publish($gets['attachment']);
+
+ // 写入共享数据
+ ShareService::addItem(array(
+ 'source_id' => $id,
+ 'source_type' => 'event',
+ 'receive_id' => $gets['receive_id'],
+ 'receive_name' => $gets['receive_name'],
+ ));
+
+ return $this->json(['id'=>$id], true);
+ } catch (\Exception $e) {
+ return $this->json($e->getMessage());
+ }
+ }
+
+ // 新增表单
+ if (Request::method() == 'GET') {
+ $start = $gets['start'];
+ $end = $gets['end'];
+ $allday = $gets['allDay'];
+
+ /*
+ if (!$end) {
+ $duration = 60;
+ $end = $start + ($duration * 60);
+ }
+ */
+
+ $start = new \DateTime('@'.strtotime($start));
+ $end = new \DateTime('@'.strtotime($end));
+
+ $timezone = CalendarService::getTimezone();
+ $start->setTimezone(new \DateTimeZone($timezone));
+ $end->setTimezone(new \DateTimeZone($timezone));
+
+ if ($allday == 'true') {
+ $end->modify('-1 day');
+ }
+
+ $calendar_options = CalendarService::getCalendars(Auth::id(), false);
+
+ /*
+ 分享日历暂时未实现
+ foreach($calendars as $calendar)
+ {
+ if($calendar['userid'] != OCP\User::getUser()) {
+ $sharedCalendar = OCP\Share::getItemSharedWithBySource('calendar', $calendar['id']);
+ if ($sharedCalendar && ($sharedCalendar['permissions'] & OCP\PERMISSION_UPDATE)) {
+ array_push($calendar_options, $calendar);
+ }
+ } else {
+ array_push($calendar_options, $calendar);
+ }
+ }*/
+
+ $options['calendar_options'] = $calendar_options;
+
+ $options['access_class_options'] = CalendarService::getAccessClassOptions();
+ $options['valarm_options'] = CalendarService::getValarmOptions();
+ $options['repeat_options'] = CalendarService::getRepeatOptions();
+ $options['repeat_end_options'] = CalendarService::getEndOptions();
+ $options['repeat_month_options'] = CalendarService::getMonthOptions();
+ $options['repeat_year_options'] = CalendarService::getYearOptions();
+ $options['repeat_weekly_options'] = CalendarService::getWeeklyOptions();
+ $options['repeat_weekofmonth_options'] = CalendarService::getWeekofMonth();
+ $options['repeat_byyearday_options'] = CalendarService::getByYearDayOptions();
+ $options['repeat_bymonth_options'] = CalendarService::getByMonthOptions();
+ $options['repeat_byweekno_options'] = CalendarService::getByWeekNoOptions();
+ $options['repeat_bymonthday_options'] = CalendarService::getByMonthDayOptions();
+
+ $options['access'] = 'owner';
+ $options['accessclass'] = 'PUBLIC';
+ $options['startdate'] = $start->format('Y-m-d');
+ $options['starttime'] = $start->format('H:i');
+ $options['enddate'] = $end->format('Y-m-d');
+ $options['endtime'] = $end->format('H:i');
+ $options['allday'] = $allday;
+ $options['valarm'] = '';
+
+ $repeats['repeat'] = 'doesnotrepeat';
+ $repeats['repeat_month'] = 'monthday';
+ $repeats['repeat_weekdays'] = array();
+ $repeats['repeat_interval'] = 1;
+ $repeats['repeat_end'] = 'never';
+ $repeats['repeat_count'] = 10;
+ $repeats['repeat_weekofmonth'] = 'auto';
+ $repeats['repeat_date'] = '';
+ $repeats['repeat_year'] = 'bydate';
+
+ $attachment['model'] = 'calendar_attachment';
+ $attachment['path'] = 'calendar';
+ $attachment['draft'] = AttachmentService::draft(Auth::id());
+
+ return $this->render(array(
+ 'attachList' => $attachment,
+ 'options' => $options,
+ 'repeats' => $repeats,
+ ));
+ }
+ }
+
+ // 编辑事件
+ public function editAction()
+ {
+ $gets = Request::all();
+
+ // 更新数据
+ if (Request::method() == 'POST') {
+ $error = CalendarService::validateRequest($gets);
+ if ($error) {
+ return $this->json($error);
+ }
+
+ $event = CalendarService::getEvent($gets['id']);
+
+ if ($event['lastmodified'] != $gets['lastmodified']) {
+ return $this->json('事件已被修改。', true);
+ }
+
+ $vcalendar = VObject::parse($event['calendardata']);
+ CalendarService::updateVCalendarFromRequest($gets, $vcalendar);
+ try {
+ $attachment = join(',', array_filter((array)$gets['attachment']));
+ CalendarService::edit($gets['id'], $vcalendar->serialize(), $attachment);
+ AttachmentService::publish($gets['attachment']);
+
+ $start_at = strtotime($gets['from'].' '.$gets['fromtime']);
+ $end_at = strtotime($gets['to'].' '.$gets['totime']);
+
+ $share_data = array(
+ 'source_id' => $gets['id'],
+ 'source_type' => 'event',
+ 'receive_id' => $gets['receive_id'],
+ 'receive_name' => $gets['receive_name'],
+ 'start_at' => $start_at,
+ 'end_at' => $end_at,
+ );
+
+ $share = ShareService::getItem('event', $gets['id']);
+ if (empty($share)) {
+ ShareService::addItem($share_data);
+ } else {
+ ShareService::editItem('event', $gets['id'], $share_data);
+ }
+ } catch (\Exception $e) {
+ return $this->json($e->getMessage());
+ }
+
+ if ($data['calendarid'] != $gets['calendarid']) {
+ try {
+ CalendarService::moveToCalendar($gets['id'], $gets['calendarid']);
+ } catch (\Exception $e) {
+ return $this->json($e->getMessage());
+ }
+ }
+ return $this->json('事件编辑完成。', true);
+ }
+
+ // 新增表单
+ if (Request::method() == 'GET') {
+ $event = CalendarService::getEvent($gets['id']);
+
+ if (empty($event)) {
+ return $this->json('事件数据不正确。');
+ }
+
+ $object = VObject::parse($event['calendardata']);
+ $vevent = $object->VEVENT;
+
+ /*
+ $object = Sabre_Calendar_Object::cleanByAccessClass($id, $object);
+ $accessclass = $vevent->getAsString('CLASS');
+ $permissions = CalendarService::getPermissions($id, Calendar::EVENT, $accessclass);
+ */
+
+ $dtstart = $vevent->DTSTART;
+ $dtend = CalendarService::getDTEndFromVEvent($vevent);
+
+ switch ($dtstart->getDateType()) {
+ case \Sabre\VObject\Property\DateTime::UTC:
+ $timezone = new \DateTimeZone(CalendarService::getTimezone());
+ $newDT = $dtstart->getDateTime();
+ $newDT->setTimezone($timezone);
+ $dtstart->setDateTime($newDT);
+ $newDT = $dtend->getDateTime();
+ $newDT->setTimezone($timezone);
+ $dtend->setDateTime($newDT);
+ // no break
+ case \Sabre\VObject\Property\DateTime::LOCALTZ:
+ case \Sabre\VObject\Property\DateTime::LOCAL:
+ $startdate = $dtstart->getDateTime()->format('Y-m-d');
+ $starttime = $dtstart->getDateTime()->format('H:i');
+ $enddate = $dtend->getDateTime()->format('Y-m-d');
+ $endtime = $dtend->getDateTime()->format('H:i');
+ $allday = false;
+ break;
+ case \Sabre\VObject\Property\DateTime::DATE:
+ $startdate = $dtstart->getDateTime()->format('Y-m-d');
+ $starttime = '';
+ $dtend->getDateTime()->modify('-1 day');
+ $enddate = $dtend->getDateTime()->format('Y-m-d');
+ $endtime = '';
+ $allday = true;
+ break;
+ }
+
+ $summary = strtr($vevent->getAsString('SUMMARY'), array('\,' => ',', '\;' => ';'));
+ $location = strtr($vevent->getAsString('LOCATION'), array('\,' => ',', '\;' => ';'));
+ $description = strtr($vevent->getAsString('DESCRIPTION'), array('\,' => ',', '\;' => ';'));
+ $categories = $vevent->getAsString('CATEGORIES');
+
+ if ($vevent->VALARM) {
+ $valarm = $vevent->VALARM->getAsString('TRIGGER');
+ }
+
+ if ($vevent->RRULE) {
+ $rrule = explode(';', $vevent->getAsString('RRULE'));
+ $rrulearr = array();
+ foreach ($rrule as $rule) {
+ list($attr, $val) = explode('=', $rule);
+ $rrulearr[$attr] = $val;
+ }
+ if (!isset($rrulearr['INTERVAL']) || $rrulearr['INTERVAL'] == '') {
+ $rrulearr['INTERVAL'] = 1;
+ }
+ if (array_key_exists('BYDAY', $rrulearr)) {
+ if (substr_count($rrulearr['BYDAY'], ',') == 0) {
+ if (strlen($rrulearr['BYDAY']) == 2) {
+ $repeat['weekdays'] = array($rrulearr['BYDAY']);
+ } elseif (strlen($rrulearr['BYDAY']) == 3) {
+ $repeat['weekofmonth'] = substr($rrulearr['BYDAY'], 0, 1);
+ $repeat['weekdays'] = array(substr($rrulearr['BYDAY'], 1, 2));
+ } elseif (strlen($rrulearr['BYDAY']) == 4) {
+ $repeat['weekofmonth'] = substr($rrulearr['BYDAY'], 0, 2);
+ $repeat['weekdays'] = array(substr($rrulearr['BYDAY'], 2, 2));
+ }
+ } else {
+ $byday_days = explode(',', $rrulearr['BYDAY']);
+ foreach ($byday_days as $byday_day) {
+ if (strlen($byday_day) == 2) {
+ $repeat['weekdays'][] = $byday_day;
+ } elseif (strlen($byday_day) == 3) {
+ $repeat['weekofmonth'] = substr($byday_day, 0, 1);
+ $repeat['weekdays'][] = substr($byday_day, 1, 2);
+ } elseif (strlen($byday_day) == 4) {
+ $repeat['weekofmonth'] = substr($byday_day, 0, 2);
+ $repeat['weekdays'][] = substr($byday_day, 2, 2);
+ }
+ }
+ }
+ }
+ if (array_key_exists('BYMONTHDAY', $rrulearr)) {
+ if (substr_count($rrulearr['BYMONTHDAY'], ',') == 0) {
+ $repeat['bymonthday'][] = $rrulearr['BYMONTHDAY'];
+ } else {
+ $bymonthdays = explode(',', $rrulearr['BYMONTHDAY']);
+ foreach ($bymonthdays as $bymonthday) {
+ $repeat['bymonthday'][] = $bymonthday;
+ }
+ }
+ }
+ if (array_key_exists('BYYEARDAY', $rrulearr)) {
+ if (substr_count($rrulearr['BYYEARDAY'], ',') == 0) {
+ $repeat['byyearday'][] = $rrulearr['BYYEARDAY'];
+ } else {
+ $byyeardays = explode(',', $rrulearr['BYYEARDAY']);
+ foreach ($byyeardays as $yearday) {
+ $repeat['byyearday'][] = $yearday;
+ }
+ }
+ }
+ if (array_key_exists('BYWEEKNO', $rrulearr)) {
+ if (substr_count($rrulearr['BYWEEKNO'], ',') == 0) {
+ $repeat['byweekno'][] = (string) $rrulearr['BYWEEKNO'];
+ } else {
+ $byweekno = explode(',', $rrulearr['BYWEEKNO']);
+ foreach ($byweekno as $weekno) {
+ $repeat['byweekno'][] = (string) $weekno;
+ }
+ }
+ }
+ if (array_key_exists('BYMONTH', $rrulearr)) {
+ if (substr_count($rrulearr['BYMONTH'], ',') == 0) {
+ $repeat['bymonth'][] = $month;
+ } else {
+ $bymonth = explode(',', $rrulearr['BYMONTH']);
+ foreach ($bymonth as $month) {
+ $repeat['bymonth'][] = $month;
+ }
+ }
+ }
+ switch ($rrulearr['FREQ']) {
+ case 'DAILY':
+ $repeat['repeat'] = 'daily';
+ break;
+ case 'WEEKLY':
+ if ($rrulearr['INTERVAL'] % 2 == 0) {
+ $repeat['repeat'] = 'biweekly';
+ $rrulearr['INTERVAL'] = $rrulearr['INTERVAL'] / 2;
+ } elseif ($rrulearr['BYDAY'] == 'MO,TU,WE,TH,FR') {
+ $repeat['repeat'] = 'weekday';
+ } else {
+ $repeat['repeat'] = 'weekly';
+ }
+ break;
+ case 'MONTHLY':
+ $repeat['repeat'] = 'monthly';
+ if (array_key_exists('BYDAY', $rrulearr)) {
+ $repeat['month'] = 'weekday';
+ } else {
+ $repeat['month'] = 'monthday';
+ }
+ break;
+ case 'YEARLY':
+ $repeat['repeat'] = 'yearly';
+ if (array_key_exists('BYMONTH', $rrulearr)) {
+ $repeat['year'] = 'bydaymonth';
+ } elseif (array_key_exists('BYWEEKNO', $rrulearr)) {
+ $repeat['year'] = 'byweekno';
+ } else {
+ $repeat['year'] = 'byyearday';
+ }
+ }
+ $repeat['interval'] = $rrulearr['INTERVAL'];
+ if (array_key_exists('COUNT', $rrulearr)) {
+ $repeat['end'] = 'count';
+ $repeat['count'] = $rrulearr['COUNT'];
+ } elseif (array_key_exists('UNTIL', $rrulearr)) {
+ $repeat['end'] = 'date';
+ $endbydate_year = substr($rrulearr['UNTIL'], 0, 4);
+ $endbydate_month = substr($rrulearr['UNTIL'], 4, 2);
+ $endbydate_day = substr($rrulearr['UNTIL'], 6, 2);
+ $repeat['date'] = $endbydate_year . '-' . $endbydate_month . '-' . $endbydate_day;
+ } else {
+ $repeat['end'] = 'never';
+ }
+ if (array_key_exists('weekdays', $repeat)) {
+ $repeat_weekdays_ = array();
+ foreach ($repeat['weekdays'] as $weekday) {
+ $repeat_weekdays_[] = $weekday;
+ }
+ $repeat['weekdays'] = $repeat_weekdays_;
+ }
+ } else {
+ $repeat['repeat'] = 'doesnotrepeat';
+ }
+
+ // $options['category_options'] = CalendarService::getCategoryOptions();
+ $options['calendar_options']= CalendarService::getCalendars(Auth::id(), false);
+ $options['access_class_options'] = CalendarService::getAccessClassOptions();
+ $options['valarm_options'] = CalendarService::getValarmOptions();
+ $options['repeat_options'] = CalendarService::getRepeatOptions();
+ $options['repeat_end_options'] = CalendarService::getEndOptions();
+ $options['repeat_month_options'] = CalendarService::getMonthOptions();
+ $options['repeat_year_options'] = CalendarService::getYearOptions();
+ $options['repeat_weekly_options'] = CalendarService::getWeeklyOptions();
+ $options['repeat_weekofmonth_options'] = CalendarService::getWeekofMonth();
+ $options['repeat_byyearday_options'] = CalendarService::getByYearDayOptions();
+ $options['repeat_bymonth_options'] = CalendarService::getByMonthOptions();
+ $options['repeat_byweekno_options'] = CalendarService::getByWeekNoOptions();
+ $options['repeat_bymonthday_options'] = CalendarService::getByMonthDayOptions();
+
+ /*
+ if($permissions & OCP\PERMISSION_UPDATE) {
+ $tmpl = new OCP\Template('calendar', 'part.editevent');
+ } elseif($permissions & OCP\PERMISSION_READ) {
+ $tmpl = new OCP\Template('calendar', 'part.showevent');
+ } elseif($permissions === 0) {
+ OCP\JSON::error(array('data' => array('message' => CalendarService::$l10n->t('You do not have the permissions to edit this event.'))));
+ exit;
+ }*/
+
+ $options['id'] = $gets['id'];
+ $options['permissions'] = $permissions;
+ $options['lastmodified'] = $event['lastmodified'];
+ $options['title'] = $summary;
+ $options['accessclass'] = $accessclass;
+ $options['location'] = $location;
+ $options['categories'] = $categories;
+ $options['calendarid'] = $event['calendarid'];
+ $options['allday'] = $allday;
+ $options['valarm'] = $valarm;
+ $options['startdate'] = $startdate;
+ $options['starttime'] = $starttime;
+ $options['enddate'] = $enddate;
+ $options['endtime'] = $endtime;
+ $options['description'] = $description;
+
+ $repeats['repeat'] = $repeat['repeat'];
+
+ if ($repeat['repeat'] != 'doesnotrepeat') {
+ $repeats['repeat_month'] = isset($repeat['month']) ? $repeat['month'] : 'monthday';
+ $repeats['repeat_weekdays'] = isset($repeat['weekdays']) ? $repeat['weekdays'] : array();
+ $repeats['repeat_interval'] = isset($repeat['interval']) ? $repeat['interval'] : '1';
+ $repeats['repeat_end'] = isset($repeat['end']) ? $repeat['end'] : 'never';
+ $repeats['repeat_count'] = isset($repeat['count']) ? $repeat['count'] : '10';
+ $repeats['repeat_weekofmonth'] = $repeat['weekofmonth'];
+ $repeats['repeat_date'] = isset($repeat['date']) ? $repeat['date'] : '';
+ $repeats['repeat_year'] = isset($repeat['year']) ? $repeat['year'] : array();
+ $repeats['repeat_byyearday'] = isset($repeat['byyearday']) ? $repeat['byyearday'] : array();
+ $repeats['repeat_bymonthday'] = isset($repeat['bymonthday']) ? $repeat['bymonthday'] : array();
+ $repeats['repeat_bymonth'] = isset($repeat['bymonth']) ? $repeat['bymonth'] : array();
+ $repeats['repeat_byweekno'] = isset($repeat['byweekno']) ? $repeat['byweekno'] : array();
+ } else {
+ $repeats['repeat_month'] = 'monthday';
+ $repeats['repeat_weekdays'] = array();
+ $repeats['repeat_byyearday'] = array();
+ $repeats['repeat_bymonthday'] = array();
+ $repeats['repeat_bymonth'] = array();
+ $repeats['repeat_byweekno'] = array();
+ $repeats['repeat_interval'] = '1';
+ $repeats['repeat_end'] = 'never';
+ $repeats['repeat_count'] = '10';
+ $repeats['repeat_weekofmonth'] = 'auto';
+ $repeats['repeat_date'] = '';
+ $repeats['repeat_year'] = 'bydate';
+ }
+
+ $attachment['model'] = 'calendar_attachment';
+ $attachment['path'] = 'calendar';
+ $attachment['draft'] = AttachmentService::draft(Auth::id());
+ $attachment['queue'] = AttachmentService::get($event['attachment']);
+
+ $share = ShareService::getItem('event', $gets['id']);
+ return $this->render(array(
+ 'attachList' => $attachment,
+ 'repeats' => $repeats,
+ 'options' => $options,
+ 'share' => $share,
+ ), 'add');
+ }
+ }
+
+ public function viewAction()
+ {
+ $id = (int)Request::get('id');
+ $event = CalendarService::getEvent($id);
+
+ if (empty($event)) {
+ return $this->json('事件数据不正确。');
+ }
+
+ $object = VObject::parse($event['calendardata']);
+ $vevent = $object->VEVENT;
+
+ $dtstart = $vevent->DTSTART;
+ $dtend = CalendarService::getDTEndFromVEvent($vevent);
+ switch ($dtstart->getDateType()) {
+ case \Sabre\VObject\Property\DateTime::UTC:
+ $timezone = new \DateTimeZone(CalendarService::getTimezone());
+ $newDT = $dtstart->getDateTime();
+ $newDT->setTimezone($timezone);
+ $dtstart->setDateTime($newDT);
+ $newDT = $dtend->getDateTime();
+ $newDT->setTimezone($timezone);
+ $dtend->setDateTime($newDT);
+ // no break
+ case \Sabre\VObject\Property\DateTime::LOCALTZ:
+ case \Sabre\VObject\Property\DateTime::LOCAL:
+ $startdate = $dtstart->getDateTime()->format('Y-m-d');
+ $starttime = $dtstart->getDateTime()->format('H:i');
+ $enddate = $dtend->getDateTime()->format('Y-m-d');
+ $endtime = $dtend->getDateTime()->format('H:i');
+ $allday = false;
+ break;
+ case \Sabre\VObject\Property\DateTime::DATE:
+ $startdate = $dtstart->getDateTime()->format('Y-m-d');
+ $starttime = '';
+ $dtend->getDateTime()->modify('-1 day');
+ $enddate = $dtend->getDateTime()->format('Y-m-d');
+ $endtime = '';
+ $allday = true;
+ break;
+ }
+
+ $summary = strtr($vevent->getAsString('SUMMARY'), array('\,' => ',', '\;' => ';'));
+ $location = strtr($vevent->getAsString('LOCATION'), array('\,' => ',', '\;' => ';'));
+ $categories = $vevent->getAsString('CATEGORIES');
+ $description = strtr($vevent->getAsString('DESCRIPTION'), array('\,' => ',', '\;' => ';'));
+
+ $options['id'] = $id;
+ $options['permissions'] = $permissions;
+ $options['lastmodified'] = $event['lastmodified'];
+ $options['title'] = $summary;
+ $options['location'] = $location;
+ $options['categories'] = $categories;
+ $options['calendarid'] = $event['calendarid'];
+ $options['allday'] = $allday;
+ $options['startdate'] = $startdate;
+ $options['starttime'] = $starttime;
+ $options['enddate'] = $enddate;
+ $options['endtime'] = $endtime;
+ $options['description'] = $description;
+ $options['accessclass'] = 'PUBLIC';
+ $options['access_class_options'] = CalendarService::getAccessClassOptions();
+
+ $attach['model'] = 'calendar_attachment';
+ $attach['path'] = 'calendar';
+ $attach['main'] = AttachmentService::get($event['attachment']);
+
+ $calendar = CalendarService::getCalendar($event['calendarid'], false);
+ $share = ShareService::getItem('event', $id);
+
+ return $this->render(array(
+ 'attachList' => $attach,
+ 'options' => $options,
+ 'calendar' => $calendar,
+ 'share' => $share,
+ ));
+ }
+
+ public function deleteAction()
+ {
+ $id = Request::get('id');
+ if ($id > 0) {
+ CalendarService::remove($id);
+ ShareService::removeItem('event', $id);
+ return $this->json('删除成功。', true);
+ }
+ }
+}
diff --git a/app/Gdoo/Calendar/Models/Calendar.php b/app/Gdoo/Calendar/Models/Calendar.php
new file mode 100644
index 00000000..8f372321
--- /dev/null
+++ b/app/Gdoo/Calendar/Models/Calendar.php
@@ -0,0 +1,8 @@
+ array(
+ * '{DAV:}displayname' => null,
+ * ),
+ * 424 => array(
+ * '{DAV:}owner' => null,
+ * )
+ * )
+ *
+ * In this example it was forbidden to update {DAV:}displayname.
+ * (403 Forbidden), which in turn also caused {DAV:}owner to fail
+ * (424 Failed Dependency) because the request needs to be atomic.
+ *
+ * @param mixed $calendarId
+ * @param array $mutations
+ * @return bool|array
+ */
+ public function updateCalendar($calendarId, array $mutations) {
+
+ return false;
+
+ }
+
+ /**
+ * Performs a calendar-query on the contents of this calendar.
+ *
+ * The calendar-query is defined in RFC4791 : CalDAV. Using the
+ * calendar-query it is possible for a client to request a specific set of
+ * object, based on contents of iCalendar properties, date-ranges and
+ * iCalendar component types (VTODO, VEVENT).
+ *
+ * This method should just return a list of (relative) urls that match this
+ * query.
+ *
+ * The list of filters are specified as an array. The exact array is
+ * documented by \Sabre\CalDAV\CalendarQueryParser.
+ *
+ * Note that it is extremely likely that getCalendarObject for every path
+ * returned from this method will be called almost immediately after. You
+ * may want to anticipate this to speed up these requests.
+ *
+ * This method provides a default implementation, which parses *all* the
+ * iCalendar objects in the specified calendar.
+ *
+ * This default may well be good enough for personal use, and calendars
+ * that aren't very large. But if you anticipate high usage, big calendars
+ * or high loads, you are strongly adviced to optimize certain paths.
+ *
+ * The best way to do so is override this method and to optimize
+ * specifically for 'common filters'.
+ *
+ * Requests that are extremely common are:
+ * * requests for just VEVENTS
+ * * requests for just VTODO
+ * * requests with a time-range-filter on either VEVENT or VTODO.
+ *
+ * ..and combinations of these requests. It may not be worth it to try to
+ * handle every possible situation and just rely on the (relatively
+ * easy to use) CalendarQueryValidator to handle the rest.
+ *
+ * Note that especially time-range-filters may be difficult to parse. A
+ * time-range filter specified on a VEVENT must for instance also handle
+ * recurrence rules correctly.
+ * A good example of how to interprete all these filters can also simply
+ * be found in \Sabre\CalDAV\CalendarQueryFilter. This class is as correct
+ * as possible, so it gives you a good idea on what type of stuff you need
+ * to think of.
+ *
+ * @param mixed $calendarId
+ * @param array $filters
+ * @return array
+ */
+ public function calendarQuery($calendarId, array $filters) {
+
+ $result = array();
+ $objects = $this->getCalendarObjects($calendarId);
+
+ $validator = new \Sabre\CalDAV\CalendarQueryValidator();
+
+ foreach($objects as $object) {
+
+ if ($this->validateFilterForObject($object, $filters)) {
+ $result[] = $object['uri'];
+ }
+
+ }
+
+ return $result;
+
+ }
+
+ /**
+ * This method validates if a filters (as passed to calendarQuery) matches
+ * the given object.
+ *
+ * @param array $object
+ * @param array $filters
+ * @return bool
+ */
+ protected function validateFilterForObject(array $object, array $filters) {
+
+ // Unfortunately, setting the 'calendardata' here is optional. If
+ // it was excluded, we actually need another call to get this as
+ // well.
+ if (!isset($object['calendardata'])) {
+ $object = $this->getCalendarObject($object['calendarid'], $object['uri']);
+ }
+
+ $data = is_resource($object['calendardata'])?stream_get_contents($object['calendardata']):$object['calendardata'];
+ $vObject = VObject\Reader::read($data);
+
+ $validator = new CalDAV\CalendarQueryValidator();
+ return $validator->validate($vObject, $filters);
+
+ }
+
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Backend/BackendInterface.php b/app/Gdoo/Calendar/Sabre/CalDAV/Backend/BackendInterface.php
new file mode 100644
index 00000000..e2a03424
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Backend/BackendInterface.php
@@ -0,0 +1,233 @@
+ array(
+ * '{DAV:}displayname' => null,
+ * ),
+ * 424 => array(
+ * '{DAV:}owner' => null,
+ * )
+ * )
+ *
+ * In this example it was forbidden to update {DAV:}displayname.
+ * (403 Forbidden), which in turn also caused {DAV:}owner to fail
+ * (424 Failed Dependency) because the request needs to be atomic.
+ *
+ * @param mixed $calendarId
+ * @param array $mutations
+ * @return bool|array
+ */
+ public function updateCalendar($calendarId, array $mutations);
+
+ /**
+ * Delete a calendar and all it's objects
+ *
+ * @param mixed $calendarId
+ * @return void
+ */
+ public function deleteCalendar($calendarId);
+
+ /**
+ * Returns all calendar objects within a calendar.
+ *
+ * Every item contains an array with the following keys:
+ * * id - unique identifier which will be used for subsequent updates
+ * * calendardata - The iCalendar-compatible calendar data
+ * * uri - a unique key which will be used to construct the uri. This can be any arbitrary string.
+ * * lastmodified - a timestamp of the last modification time
+ * * etag - An arbitrary string, surrounded by double-quotes. (e.g.:
+ * ' "abcdef"')
+ * * calendarid - The calendarid as it was passed to this function.
+ * * size - The size of the calendar objects, in bytes.
+ *
+ * Note that the etag is optional, but it's highly encouraged to return for
+ * speed reasons.
+ *
+ * The calendardata is also optional. If it's not returned
+ * 'getCalendarObject' will be called later, which *is* expected to return
+ * calendardata.
+ *
+ * If neither etag or size are specified, the calendardata will be
+ * used/fetched to determine these numbers. If both are specified the
+ * amount of times this is needed is reduced by a great degree.
+ *
+ * @param mixed $calendarId
+ * @return array
+ */
+ public function getCalendarObjects($calendarId);
+
+ /**
+ * Returns information from a single calendar object, based on it's object
+ * uri.
+ *
+ * The returned array must have the same keys as getCalendarObjects. The
+ * 'calendardata' object is required here though, while it's not required
+ * for getCalendarObjects.
+ *
+ * This method must return null if the object did not exist.
+ *
+ * @param mixed $calendarId
+ * @param string $objectUri
+ * @return array|null
+ */
+ public function getCalendarObject($calendarId,$objectUri);
+
+ /**
+ * Creates a new calendar object.
+ *
+ * It is possible return an etag from this function, which will be used in
+ * the response to this PUT request. Note that the ETag must be surrounded
+ * by double-quotes.
+ *
+ * However, you should only really return this ETag if you don't mangle the
+ * calendar-data. If the result of a subsequent GET to this object is not
+ * the exact same as this request body, you should omit the ETag.
+ *
+ * @param mixed $calendarId
+ * @param string $objectUri
+ * @param string $calendarData
+ * @return string|null
+ */
+ public function createCalendarObject($calendarId,$objectUri,$calendarData);
+
+ /**
+ * Updates an existing calendarobject, based on it's uri.
+ *
+ * It is possible return an etag from this function, which will be used in
+ * the response to this PUT request. Note that the ETag must be surrounded
+ * by double-quotes.
+ *
+ * However, you should only really return this ETag if you don't mangle the
+ * calendar-data. If the result of a subsequent GET to this object is not
+ * the exact same as this request body, you should omit the ETag.
+ *
+ * @param mixed $calendarId
+ * @param string $objectUri
+ * @param string $calendarData
+ * @return string|null
+ */
+ public function updateCalendarObject($calendarId,$objectUri,$calendarData);
+
+ /**
+ * Deletes an existing calendar object.
+ *
+ * @param mixed $calendarId
+ * @param string $objectUri
+ * @return void
+ */
+ public function deleteCalendarObject($calendarId,$objectUri);
+
+ /**
+ * Performs a calendar-query on the contents of this calendar.
+ *
+ * The calendar-query is defined in RFC4791 : CalDAV. Using the
+ * calendar-query it is possible for a client to request a specific set of
+ * object, based on contents of iCalendar properties, date-ranges and
+ * iCalendar component types (VTODO, VEVENT).
+ *
+ * This method should just return a list of (relative) urls that match this
+ * query.
+ *
+ * The list of filters are specified as an array. The exact array is
+ * documented by Sabre\CalDAV\CalendarQueryParser.
+ *
+ * Note that it is extremely likely that getCalendarObject for every path
+ * returned from this method will be called almost immediately after. You
+ * may want to anticipate this to speed up these requests.
+ *
+ * This method provides a default implementation, which parses *all* the
+ * iCalendar objects in the specified calendar.
+ *
+ * This default may well be good enough for personal use, and calendars
+ * that aren't very large. But if you anticipate high usage, big calendars
+ * or high loads, you are strongly adviced to optimize certain paths.
+ *
+ * The best way to do so is override this method and to optimize
+ * specifically for 'common filters'.
+ *
+ * Requests that are extremely common are:
+ * * requests for just VEVENTS
+ * * requests for just VTODO
+ * * requests with a time-range-filter on either VEVENT or VTODO.
+ *
+ * ..and combinations of these requests. It may not be worth it to try to
+ * handle every possible situation and just rely on the (relatively
+ * easy to use) CalendarQueryValidator to handle the rest.
+ *
+ * Note that especially time-range-filters may be difficult to parse. A
+ * time-range filter specified on a VEVENT must for instance also handle
+ * recurrence rules correctly.
+ * A good example of how to interprete all these filters can also simply
+ * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct
+ * as possible, so it gives you a good idea on what type of stuff you need
+ * to think of.
+ *
+ * @param mixed $calendarId
+ * @param array $filters
+ * @return array
+ */
+ public function calendarQuery($calendarId, array $filters);
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Backend/NotificationSupport.php b/app/Gdoo/Calendar/Sabre/CalDAV/Backend/NotificationSupport.php
new file mode 100644
index 00000000..0145dfb3
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Backend/NotificationSupport.php
@@ -0,0 +1,47 @@
+ 'displayname',
+ '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description',
+ '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'timezone',
+ '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder',
+ '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor',
+ );
+
+ /**
+ * Creates the backend
+ *
+ * @param \PDO $pdo
+ * @param string $calendarTableName
+ * @param string $calendarObjectTableName
+ */
+ public function __construct(\PDO $pdo, $calendarTableName = 'calendars', $calendarObjectTableName = 'calendarobjects') {
+
+ $this->pdo = $pdo;
+ $this->calendarTableName = $calendarTableName;
+ $this->calendarObjectTableName = $calendarObjectTableName;
+
+ }
+
+ /**
+ * Returns a list of calendars for a principal.
+ *
+ * Every project is an array with the following keys:
+ * * id, a unique id that will be used by other functions to modify the
+ * calendar. This can be the same as the uri or a database key.
+ * * uri, which the basename of the uri with which the calendar is
+ * accessed.
+ * * principaluri. The owner of the calendar. Almost always the same as
+ * principalUri passed to this method.
+ *
+ * Furthermore it can contain webdav properties in clark notation. A very
+ * common one is '{DAV:}displayname'.
+ *
+ * @param string $principalUri
+ * @return array
+ */
+ public function getCalendarsForUser($principalUri) {
+
+ $fields = array_values($this->propertyMap);
+ $fields[] = 'id';
+ $fields[] = 'uri';
+ $fields[] = 'ctag';
+ $fields[] = 'components';
+ $fields[] = 'principaluri';
+ $fields[] = 'transparent';
+
+ // Making fields a comma-delimited list
+ $fields = implode(', ', $fields);
+ $stmt = $this->pdo->prepare("SELECT " . $fields . " FROM ".$this->calendarTableName." WHERE principaluri = ? ORDER BY calendarorder ASC");
+ $stmt->execute(array($principalUri));
+
+ $calendars = array();
+ while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
+
+ $components = array();
+ if ($row['components']) {
+ $components = explode(',',$row['components']);
+ }
+
+ $calendar = array(
+ 'id' => $row['id'],
+ 'uri' => $row['uri'],
+ 'principaluri' => $row['principaluri'],
+ '{' . CalDAV\Plugin::NS_CALENDARSERVER . '}getctag' => $row['ctag']?$row['ctag']:'0',
+ '{' . CalDAV\Plugin::NS_CALDAV . '}supported-calendar-component-set' => new CalDAV\Property\SupportedCalendarComponentSet($components),
+ '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-calendar-transp' => new CalDAV\Property\ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
+ );
+
+
+ foreach($this->propertyMap as $xmlName=>$dbName) {
+ $calendar[$xmlName] = $row[$dbName];
+ }
+
+ $calendars[] = $calendar;
+
+ }
+
+ return $calendars;
+
+ }
+
+ /**
+ * Creates a new calendar for a principal.
+ *
+ * If the creation was a success, an id must be returned that can be used to reference
+ * this calendar in other methods, such as updateCalendar
+ *
+ * @param string $principalUri
+ * @param string $calendarUri
+ * @param array $properties
+ * @return string
+ */
+ public function createCalendar($principalUri, $calendarUri, array $properties) {
+
+ $fieldNames = array(
+ 'principaluri',
+ 'uri',
+ 'ctag',
+ 'transparent',
+ );
+ $values = array(
+ ':principaluri' => $principalUri,
+ ':uri' => $calendarUri,
+ ':ctag' => 1,
+ ':transparent' => 0,
+ );
+
+ // Default value
+ $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set';
+ $fieldNames[] = 'components';
+ if (!isset($properties[$sccs])) {
+ $values[':components'] = 'VEVENT,VTODO';
+ } else {
+ if (!($properties[$sccs] instanceof CalDAV\Property\SupportedCalendarComponentSet)) {
+ throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet');
+ }
+ $values[':components'] = implode(',',$properties[$sccs]->getValue());
+ }
+ $transp = '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-calendar-transp';
+ if (isset($properties[$transp])) {
+ $values[':transparent'] = $properties[$transp]->getValue()==='transparent';
+ }
+
+ foreach($this->propertyMap as $xmlName=>$dbName) {
+ if (isset($properties[$xmlName])) {
+
+ $values[':' . $dbName] = $properties[$xmlName];
+ $fieldNames[] = $dbName;
+ }
+ }
+
+ $stmt = $this->pdo->prepare("INSERT INTO ".$this->calendarTableName." (".implode(', ', $fieldNames).") VALUES (".implode(', ',array_keys($values)).")");
+ $stmt->execute($values);
+
+ return $this->pdo->lastInsertId();
+
+ }
+
+ /**
+ * Updates properties for a calendar.
+ *
+ * The mutations array uses the propertyName in clark-notation as key,
+ * and the array value for the property value. In the case a property
+ * should be deleted, the property value will be null.
+ *
+ * This method must be atomic. If one property cannot be changed, the
+ * entire operation must fail.
+ *
+ * If the operation was successful, true can be returned.
+ * If the operation failed, false can be returned.
+ *
+ * Deletion of a non-existent property is always successful.
+ *
+ * Lastly, it is optional to return detailed information about any
+ * failures. In this case an array should be returned with the following
+ * structure:
+ *
+ * array(
+ * 403 => array(
+ * '{DAV:}displayname' => null,
+ * ),
+ * 424 => array(
+ * '{DAV:}owner' => null,
+ * )
+ * )
+ *
+ * In this example it was forbidden to update {DAV:}displayname.
+ * (403 Forbidden), which in turn also caused {DAV:}owner to fail
+ * (424 Failed Dependency) because the request needs to be atomic.
+ *
+ * @param string $calendarId
+ * @param array $mutations
+ * @return bool|array
+ */
+ public function updateCalendar($calendarId, array $mutations) {
+
+ $newValues = array();
+ $result = array(
+ 200 => array(), // Ok
+ 403 => array(), // Forbidden
+ 424 => array(), // Failed Dependency
+ );
+
+ $hasError = false;
+
+ foreach($mutations as $propertyName=>$propertyValue) {
+
+ switch($propertyName) {
+ case '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-calendar-transp' :
+ $fieldName = 'transparent';
+ $newValues[$fieldName] = $propertyValue->getValue()==='transparent';
+ break;
+ default :
+ // Checking the property map
+ if (!isset($this->propertyMap[$propertyName])) {
+ // We don't know about this property.
+ $hasError = true;
+ $result[403][$propertyName] = null;
+ unset($mutations[$propertyName]);
+ continue;
+ }
+
+ $fieldName = $this->propertyMap[$propertyName];
+ $newValues[$fieldName] = $propertyValue;
+ }
+
+ }
+
+ // If there were any errors we need to fail the request
+ if ($hasError) {
+ // Properties has the remaining properties
+ foreach($mutations as $propertyName=>$propertyValue) {
+ $result[424][$propertyName] = null;
+ }
+
+ // Removing unused statuscodes for cleanliness
+ foreach($result as $status=>$properties) {
+ if (is_array($properties) && count($properties)===0) unset($result[$status]);
+ }
+
+ return $result;
+
+ }
+
+ // Success
+
+ // Now we're generating the sql query.
+ $valuesSql = array();
+ foreach($newValues as $fieldName=>$value) {
+ $valuesSql[] = $fieldName . ' = ?';
+ }
+ $valuesSql[] = 'ctag = ctag + 1';
+
+ $stmt = $this->pdo->prepare("UPDATE " . $this->calendarTableName . " SET " . implode(', ',$valuesSql) . " WHERE id = ?");
+ $newValues['id'] = $calendarId;
+ $stmt->execute(array_values($newValues));
+
+ return true;
+
+ }
+
+ /**
+ * Delete a calendar and all it's objects
+ *
+ * @param string $calendarId
+ * @return void
+ */
+ public function deleteCalendar($calendarId) {
+
+ $stmt = $this->pdo->prepare('DELETE FROM '.$this->calendarObjectTableName.' WHERE calendarid = ?');
+ $stmt->execute(array($calendarId));
+
+ $stmt = $this->pdo->prepare('DELETE FROM '.$this->calendarTableName.' WHERE id = ?');
+ $stmt->execute(array($calendarId));
+
+ }
+
+ /**
+ * Returns all calendar objects within a calendar.
+ *
+ * Every item contains an array with the following keys:
+ * * id - unique identifier which will be used for subsequent updates
+ * * calendardata - The iCalendar-compatible calendar data
+ * * uri - a unique key which will be used to construct the uri. This can be any arbitrary string.
+ * * lastmodified - a timestamp of the last modification time
+ * * etag - An arbitrary string, surrounded by double-quotes. (e.g.:
+ * ' "abcdef"')
+ * * calendarid - The calendarid as it was passed to this function.
+ * * size - The size of the calendar objects, in bytes.
+ *
+ * Note that the etag is optional, but it's highly encouraged to return for
+ * speed reasons.
+ *
+ * The calendardata is also optional. If it's not returned
+ * 'getCalendarObject' will be called later, which *is* expected to return
+ * calendardata.
+ *
+ * If neither etag or size are specified, the calendardata will be
+ * used/fetched to determine these numbers. If both are specified the
+ * amount of times this is needed is reduced by a great degree.
+ *
+ * @param string $calendarId
+ * @return array
+ */
+ public function getCalendarObjects($calendarId) {
+
+ $stmt = $this->pdo->prepare('SELECT id, uri, lastmodified, etag, calendarid, size FROM '.$this->calendarObjectTableName.' WHERE calendarid = ?');
+ $stmt->execute(array($calendarId));
+
+ $result = array();
+ foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) {
+ $result[] = array(
+ 'id' => $row['id'],
+ 'uri' => $row['uri'],
+ 'lastmodified' => $row['lastmodified'],
+ 'etag' => '"' . $row['etag'] . '"',
+ 'calendarid' => $row['calendarid'],
+ 'size' => (int)$row['size'],
+ );
+ }
+
+ return $result;
+
+ }
+
+ /**
+ * Returns information from a single calendar object, based on it's object
+ * uri.
+ *
+ * The returned array must have the same keys as getCalendarObjects. The
+ * 'calendardata' object is required here though, while it's not required
+ * for getCalendarObjects.
+ *
+ * This method must return null if the object did not exist.
+ *
+ * @param string $calendarId
+ * @param string $objectUri
+ * @return array|null
+ */
+ public function getCalendarObject($calendarId,$objectUri) {
+
+ $stmt = $this->pdo->prepare('SELECT id, uri, lastmodified, etag, calendarid, size, calendardata FROM '.$this->calendarObjectTableName.' WHERE calendarid = ? AND uri = ?');
+ $stmt->execute(array($calendarId, $objectUri));
+ $row = $stmt->fetch(\PDO::FETCH_ASSOC);
+
+ if(!$row) return null;
+
+ return array(
+ 'id' => $row['id'],
+ 'uri' => $row['uri'],
+ 'lastmodified' => $row['lastmodified'],
+ 'etag' => '"' . $row['etag'] . '"',
+ 'calendarid' => $row['calendarid'],
+ 'size' => (int)$row['size'],
+ 'calendardata' => $row['calendardata'],
+ );
+
+ }
+
+
+ /**
+ * Creates a new calendar object.
+ *
+ * It is possible return an etag from this function, which will be used in
+ * the response to this PUT request. Note that the ETag must be surrounded
+ * by double-quotes.
+ *
+ * However, you should only really return this ETag if you don't mangle the
+ * calendar-data. If the result of a subsequent GET to this object is not
+ * the exact same as this request body, you should omit the ETag.
+ *
+ * @param mixed $calendarId
+ * @param string $objectUri
+ * @param string $calendarData
+ * @return string|null
+ */
+ public function createCalendarObject($calendarId,$objectUri,$calendarData) {
+
+ $extraData = $this->getDenormalizedData($calendarData);
+
+ $stmt = $this->pdo->prepare('INSERT INTO '.$this->calendarObjectTableName.' (calendarid, uri, calendardata, lastmodified, etag, size, componenttype, firstoccurence, lastoccurence) VALUES (?,?,?,?,?,?,?,?,?)');
+ $stmt->execute(array(
+ $calendarId,
+ $objectUri,
+ $calendarData,
+ time(),
+ $extraData['etag'],
+ $extraData['size'],
+ $extraData['componentType'],
+ $extraData['firstOccurence'],
+ $extraData['lastOccurence'],
+ ));
+ $stmt = $this->pdo->prepare('UPDATE '.$this->calendarTableName.' SET ctag = ctag + 1 WHERE id = ?');
+ $stmt->execute(array($calendarId));
+
+ return '"' . $extraData['etag'] . '"';
+
+ }
+
+ /**
+ * Updates an existing calendarobject, based on it's uri.
+ *
+ * It is possible return an etag from this function, which will be used in
+ * the response to this PUT request. Note that the ETag must be surrounded
+ * by double-quotes.
+ *
+ * However, you should only really return this ETag if you don't mangle the
+ * calendar-data. If the result of a subsequent GET to this object is not
+ * the exact same as this request body, you should omit the ETag.
+ *
+ * @param mixed $calendarId
+ * @param string $objectUri
+ * @param string $calendarData
+ * @return string|null
+ */
+ public function updateCalendarObject($calendarId,$objectUri,$calendarData) {
+
+ $extraData = $this->getDenormalizedData($calendarData);
+
+ $stmt = $this->pdo->prepare('UPDATE '.$this->calendarObjectTableName.' SET calendardata = ?, lastmodified = ?, etag = ?, size = ?, componenttype = ?, firstoccurence = ?, lastoccurence = ? WHERE calendarid = ? AND uri = ?');
+ $stmt->execute(array($calendarData,time(), $extraData['etag'], $extraData['size'], $extraData['componentType'], $extraData['firstOccurence'], $extraData['lastOccurence'] ,$calendarId,$objectUri));
+ $stmt = $this->pdo->prepare('UPDATE '.$this->calendarTableName.' SET ctag = ctag + 1 WHERE id = ?');
+ $stmt->execute(array($calendarId));
+
+ return '"' . $extraData['etag'] . '"';
+
+ }
+
+ /**
+ * Parses some information from calendar objects, used for optimized
+ * calendar-queries.
+ *
+ * Returns an array with the following keys:
+ * * etag
+ * * size
+ * * componentType
+ * * firstOccurence
+ * * lastOccurence
+ *
+ * @param string $calendarData
+ * @return array
+ */
+ protected function getDenormalizedData($calendarData) {
+
+ $vObject = VObject\Reader::read($calendarData);
+ $componentType = null;
+ $component = null;
+ $firstOccurence = null;
+ $lastOccurence = null;
+ foreach($vObject->getComponents() as $component) {
+ if ($component->name!=='VTIMEZONE') {
+ $componentType = $component->name;
+ break;
+ }
+ }
+ if (!$componentType) {
+ throw new \Sabre\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component');
+ }
+ if ($componentType === 'VEVENT') {
+ $firstOccurence = $component->DTSTART->getDateTime()->getTimeStamp();
+ // Finding the last occurence is a bit harder
+ if (!isset($component->RRULE)) {
+ if (isset($component->DTEND)) {
+ $lastOccurence = $component->DTEND->getDateTime()->getTimeStamp();
+ } elseif (isset($component->DURATION)) {
+ $endDate = clone $component->DTSTART->getDateTime();
+ $endDate->add(VObject\DateTimeParser::parse($component->DURATION->getValue()));
+ $lastOccurence = $endDate->getTimeStamp();
+ } elseif (!$component->DTSTART->hasTime()) {
+ $endDate = clone $component->DTSTART->getDateTime();
+ $endDate->modify('+1 day');
+ $lastOccurence = $endDate->getTimeStamp();
+ } else {
+ $lastOccurence = $firstOccurence;
+ }
+ } else {
+ $it = new VObject\RecurrenceIterator($vObject, (string)$component->UID);
+ $maxDate = new \DateTime(self::MAX_DATE);
+ if ($it->isInfinite()) {
+ $lastOccurence = $maxDate->getTimeStamp();
+ } else {
+ $end = $it->getDtEnd();
+ while($it->valid() && $end < $maxDate) {
+ $end = $it->getDtEnd();
+ $it->next();
+
+ }
+ $lastOccurence = $end->getTimeStamp();
+ }
+
+ }
+ }
+
+ return array(
+ 'etag' => md5($calendarData),
+ 'size' => strlen($calendarData),
+ 'componentType' => $componentType,
+ 'firstOccurence' => $firstOccurence,
+ 'lastOccurence' => $lastOccurence,
+ );
+
+ }
+
+ /**
+ * Deletes an existing calendar object.
+ *
+ * @param string $calendarId
+ * @param string $objectUri
+ * @return void
+ */
+ public function deleteCalendarObject($calendarId,$objectUri) {
+
+ $stmt = $this->pdo->prepare('DELETE FROM '.$this->calendarObjectTableName.' WHERE calendarid = ? AND uri = ?');
+ $stmt->execute(array($calendarId,$objectUri));
+ $stmt = $this->pdo->prepare('UPDATE '. $this->calendarTableName .' SET ctag = ctag + 1 WHERE id = ?');
+ $stmt->execute(array($calendarId));
+
+ }
+
+ /**
+ * Performs a calendar-query on the contents of this calendar.
+ *
+ * The calendar-query is defined in RFC4791 : CalDAV. Using the
+ * calendar-query it is possible for a client to request a specific set of
+ * object, based on contents of iCalendar properties, date-ranges and
+ * iCalendar component types (VTODO, VEVENT).
+ *
+ * This method should just return a list of (relative) urls that match this
+ * query.
+ *
+ * The list of filters are specified as an array. The exact array is
+ * documented by \Sabre\CalDAV\CalendarQueryParser.
+ *
+ * Note that it is extremely likely that getCalendarObject for every path
+ * returned from this method will be called almost immediately after. You
+ * may want to anticipate this to speed up these requests.
+ *
+ * This method provides a default implementation, which parses *all* the
+ * iCalendar objects in the specified calendar.
+ *
+ * This default may well be good enough for personal use, and calendars
+ * that aren't very large. But if you anticipate high usage, big calendars
+ * or high loads, you are strongly adviced to optimize certain paths.
+ *
+ * The best way to do so is override this method and to optimize
+ * specifically for 'common filters'.
+ *
+ * Requests that are extremely common are:
+ * * requests for just VEVENTS
+ * * requests for just VTODO
+ * * requests with a time-range-filter on a VEVENT.
+ *
+ * ..and combinations of these requests. It may not be worth it to try to
+ * handle every possible situation and just rely on the (relatively
+ * easy to use) CalendarQueryValidator to handle the rest.
+ *
+ * Note that especially time-range-filters may be difficult to parse. A
+ * time-range filter specified on a VEVENT must for instance also handle
+ * recurrence rules correctly.
+ * A good example of how to interprete all these filters can also simply
+ * be found in \Sabre\CalDAV\CalendarQueryFilter. This class is as correct
+ * as possible, so it gives you a good idea on what type of stuff you need
+ * to think of.
+ *
+ * This specific implementation (for the PDO) backend optimizes filters on
+ * specific components, and VEVENT time-ranges.
+ *
+ * @param string $calendarId
+ * @param array $filters
+ * @return array
+ */
+ public function calendarQuery($calendarId, array $filters) {
+
+ $result = array();
+ $validator = new \Sabre\CalDAV\CalendarQueryValidator();
+
+ $componentType = null;
+ $requirePostFilter = true;
+ $timeRange = null;
+
+ // if no filters were specified, we don't need to filter after a query
+ if (!$filters['prop-filters'] && !$filters['comp-filters']) {
+ $requirePostFilter = false;
+ }
+
+ // Figuring out if there's a component filter
+ if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) {
+ $componentType = $filters['comp-filters'][0]['name'];
+
+ // Checking if we need post-filters
+ if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) {
+ $requirePostFilter = false;
+ }
+ // There was a time-range filter
+ if ($componentType == 'VEVENT' && isset($filters['comp-filters'][0]['time-range'])) {
+ $timeRange = $filters['comp-filters'][0]['time-range'];
+
+ // If start time OR the end time is not specified, we can do a
+ // 100% accurate mysql query.
+ if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) {
+ $requirePostFilter = false;
+ }
+ }
+
+ }
+
+ if ($requirePostFilter) {
+ $query = "SELECT uri, calendardata FROM ".$this->calendarObjectTableName." WHERE calendarid = :calendarid";
+ } else {
+ $query = "SELECT uri FROM ".$this->calendarObjectTableName." WHERE calendarid = :calendarid";
+ }
+
+ $values = array(
+ 'calendarid' => $calendarId,
+ );
+
+ if ($componentType) {
+ $query.=" AND componenttype = :componenttype";
+ $values['componenttype'] = $componentType;
+ }
+
+ if ($timeRange && $timeRange['start']) {
+ $query.=" AND lastoccurence > :startdate";
+ $values['startdate'] = $timeRange['start']->getTimeStamp();
+ }
+ if ($timeRange && $timeRange['end']) {
+ $query.=" AND firstoccurence < :enddate";
+ $values['enddate'] = $timeRange['end']->getTimeStamp();
+ }
+
+ $stmt = $this->pdo->prepare($query);
+ $stmt->execute($values);
+
+ $result = array();
+ while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
+ if ($requirePostFilter) {
+ if (!$this->validateFilterForObject($row, $filters)) {
+ continue;
+ }
+ }
+ $result[] = $row['uri'];
+
+ }
+
+ return $result;
+
+ }
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Backend/SharingSupport.php b/app/Gdoo/Calendar/Sabre/CalDAV/Backend/SharingSupport.php
new file mode 100644
index 00000000..2d1e3662
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Backend/SharingSupport.php
@@ -0,0 +1,243 @@
+caldavBackend = $caldavBackend;
+ $this->calendarInfo = $calendarInfo;
+
+ }
+
+ /**
+ * Returns the name of the calendar
+ *
+ * @return string
+ */
+ public function getName() {
+
+ return $this->calendarInfo['uri'];
+
+ }
+
+ /**
+ * Updates properties such as the display name and description
+ *
+ * @param array $mutations
+ * @return array
+ */
+ public function updateProperties($mutations) {
+
+ return $this->caldavBackend->updateCalendar($this->calendarInfo['id'],$mutations);
+
+ }
+
+ /**
+ * Returns the list of properties
+ *
+ * @param array $requestedProperties
+ * @return array
+ */
+ public function getProperties($requestedProperties) {
+
+ $response = array();
+
+ foreach($requestedProperties as $prop) switch($prop) {
+
+ case '{urn:ietf:params:xml:ns:caldav}supported-calendar-data' :
+ $response[$prop] = new Property\SupportedCalendarData();
+ break;
+ case '{urn:ietf:params:xml:ns:caldav}supported-collation-set' :
+ $response[$prop] = new Property\SupportedCollationSet();
+ break;
+ case '{DAV:}owner' :
+ $response[$prop] = new DAVACL\Property\Principal(DAVACL\Property\Principal::HREF,$this->calendarInfo['principaluri']);
+ break;
+ default :
+ if (isset($this->calendarInfo[$prop])) $response[$prop] = $this->calendarInfo[$prop];
+ break;
+
+ }
+ return $response;
+
+ }
+
+ /**
+ * Returns a calendar object
+ *
+ * The contained calendar objects are for example Events or Todo's.
+ *
+ * @param string $name
+ * @return \Sabre\CalDAV\ICalendarObject
+ */
+ public function getChild($name) {
+
+ $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'],$name);
+
+ if (!$obj) throw new DAV\Exception\NotFound('Calendar object not found');
+
+ $obj['acl'] = $this->getACL();
+ // Removing the irrelivant
+ foreach($obj['acl'] as $key=>$acl) {
+ if ($acl['privilege'] === '{' . Plugin::NS_CALDAV . '}read-free-busy') {
+ unset($obj['acl'][$key]);
+ }
+ }
+
+ return new CalendarObject($this->caldavBackend,$this->calendarInfo,$obj);
+
+ }
+
+ /**
+ * Returns the full list of calendar objects
+ *
+ * @return array
+ */
+ public function getChildren() {
+
+ $objs = $this->caldavBackend->getCalendarObjects($this->calendarInfo['id']);
+ $children = array();
+ foreach($objs as $obj) {
+ $obj['acl'] = $this->getACL();
+ // Removing the irrelivant
+ foreach($obj['acl'] as $key=>$acl) {
+ if ($acl['privilege'] === '{' . Plugin::NS_CALDAV . '}read-free-busy') {
+ unset($obj['acl'][$key]);
+ }
+ }
+ $children[] = new CalendarObject($this->caldavBackend,$this->calendarInfo,$obj);
+ }
+ return $children;
+
+ }
+
+ /**
+ * Checks if a child-node exists.
+ *
+ * @param string $name
+ * @return bool
+ */
+ public function childExists($name) {
+
+ $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'],$name);
+ if (!$obj)
+ return false;
+ else
+ return true;
+
+ }
+
+ /**
+ * Creates a new directory
+ *
+ * We actually block this, as subdirectories are not allowed in calendars.
+ *
+ * @param string $name
+ * @return void
+ */
+ public function createDirectory($name) {
+
+ throw new DAV\Exception\MethodNotAllowed('Creating collections in calendar objects is not allowed');
+
+ }
+
+ /**
+ * Creates a new file
+ *
+ * The contents of the new file must be a valid ICalendar string.
+ *
+ * @param string $name
+ * @param resource $calendarData
+ * @return string|null
+ */
+ public function createFile($name,$calendarData = null) {
+
+ if (is_resource($calendarData)) {
+ $calendarData = stream_get_contents($calendarData);
+ }
+ return $this->caldavBackend->createCalendarObject($this->calendarInfo['id'],$name,$calendarData);
+
+ }
+
+ /**
+ * Deletes the calendar.
+ *
+ * @return void
+ */
+ public function delete() {
+
+ $this->caldavBackend->deleteCalendar($this->calendarInfo['id']);
+
+ }
+
+ /**
+ * Renames the calendar. Note that most calendars use the
+ * {DAV:}displayname to display a name to display a name.
+ *
+ * @param string $newName
+ * @return void
+ */
+ public function setName($newName) {
+
+ throw new DAV\Exception\MethodNotAllowed('Renaming calendars is not yet supported');
+
+ }
+
+ /**
+ * Returns the last modification date as a unix timestamp.
+ *
+ * @return void
+ */
+ public function getLastModified() {
+
+ return null;
+
+ }
+
+ /**
+ * Returns the owner principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getOwner() {
+
+ return $this->calendarInfo['principaluri'];
+
+ }
+
+ /**
+ * Returns a group principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getGroup() {
+
+ return null;
+
+ }
+
+ /**
+ * Returns a list of ACE's for this node.
+ *
+ * Each ACE has the following properties:
+ * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
+ * currently the only supported privileges
+ * * 'principal', a url to the principal who owns the node
+ * * 'protected' (optional), indicating that this ACE is not allowed to
+ * be updated.
+ *
+ * @return array
+ */
+ public function getACL() {
+
+ return array(
+ array(
+ 'privilege' => '{DAV:}read',
+ 'principal' => $this->getOwner(),
+ 'protected' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}write',
+ 'principal' => $this->getOwner(),
+ 'protected' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}read',
+ 'principal' => $this->getOwner() . '/calendar-proxy-write',
+ 'protected' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}write',
+ 'principal' => $this->getOwner() . '/calendar-proxy-write',
+ 'protected' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}read',
+ 'principal' => $this->getOwner() . '/calendar-proxy-read',
+ 'protected' => true,
+ ),
+ array(
+ 'privilege' => '{' . Plugin::NS_CALDAV . '}read-free-busy',
+ 'principal' => '{DAV:}authenticated',
+ 'protected' => true,
+ ),
+
+ );
+
+ }
+
+ /**
+ * Updates the ACL
+ *
+ * This method will receive a list of new ACE's.
+ *
+ * @param array $acl
+ * @return void
+ */
+ public function setACL(array $acl) {
+
+ throw new DAV\Exception\MethodNotAllowed('Changing ACL is not yet supported');
+
+ }
+
+ /**
+ * Returns the list of supported privileges for this node.
+ *
+ * The returned data structure is a list of nested privileges.
+ * See \Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple
+ * standard structure.
+ *
+ * If null is returned from this method, the default privilege set is used,
+ * which is fine for most common usecases.
+ *
+ * @return array|null
+ */
+ public function getSupportedPrivilegeSet() {
+
+ $default = DAVACL\Plugin::getDefaultSupportedPrivilegeSet();
+
+ // We need to inject 'read-free-busy' in the tree, aggregated under
+ // {DAV:}read.
+ foreach($default['aggregates'] as &$agg) {
+
+ if ($agg['privilege'] !== '{DAV:}read') continue;
+
+ $agg['aggregates'][] = array(
+ 'privilege' => '{' . Plugin::NS_CALDAV . '}read-free-busy',
+ );
+
+ }
+ return $default;
+
+ }
+
+ /**
+ * Performs a calendar-query on the contents of this calendar.
+ *
+ * The calendar-query is defined in RFC4791 : CalDAV. Using the
+ * calendar-query it is possible for a client to request a specific set of
+ * object, based on contents of iCalendar properties, date-ranges and
+ * iCalendar component types (VTODO, VEVENT).
+ *
+ * This method should just return a list of (relative) urls that match this
+ * query.
+ *
+ * The list of filters are specified as an array. The exact array is
+ * documented by Sabre\CalDAV\CalendarQueryParser.
+ *
+ * @param array $filters
+ * @return array
+ */
+ public function calendarQuery(array $filters) {
+
+ return $this->caldavBackend->calendarQuery($this->calendarInfo['id'], $filters);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/CalendarObject.php b/app/Gdoo/Calendar/Sabre/CalDAV/CalendarObject.php
new file mode 100644
index 00000000..3a075810
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/CalendarObject.php
@@ -0,0 +1,279 @@
+caldavBackend = $caldavBackend;
+
+ if (!isset($objectData['calendarid'])) {
+ throw new \InvalidArgumentException('The objectData argument must contain a \'calendarid\' property');
+ }
+ if (!isset($objectData['uri'])) {
+ throw new \InvalidArgumentException('The objectData argument must contain an \'uri\' property');
+ }
+
+ $this->calendarInfo = $calendarInfo;
+ $this->objectData = $objectData;
+
+ }
+
+ /**
+ * Returns the uri for this object
+ *
+ * @return string
+ */
+ public function getName() {
+
+ return $this->objectData['uri'];
+
+ }
+
+ /**
+ * Returns the ICalendar-formatted object
+ *
+ * @return string
+ */
+ public function get() {
+
+ // Pre-populating the 'calendardata' is optional, if we don't have it
+ // already we fetch it from the backend.
+ if (!isset($this->objectData['calendardata'])) {
+ $this->objectData = $this->caldavBackend->getCalendarObject($this->objectData['calendarid'], $this->objectData['uri']);
+ }
+ return $this->objectData['calendardata'];
+
+ }
+
+ /**
+ * Updates the ICalendar-formatted object
+ *
+ * @param string|resource $calendarData
+ * @return string
+ */
+ public function put($calendarData) {
+
+ if (is_resource($calendarData)) {
+ $calendarData = stream_get_contents($calendarData);
+ }
+ $etag = $this->caldavBackend->updateCalendarObject($this->calendarInfo['id'],$this->objectData['uri'],$calendarData);
+ $this->objectData['calendardata'] = $calendarData;
+ $this->objectData['etag'] = $etag;
+
+ return $etag;
+
+ }
+
+ /**
+ * Deletes the calendar object
+ *
+ * @return void
+ */
+ public function delete() {
+
+ $this->caldavBackend->deleteCalendarObject($this->calendarInfo['id'],$this->objectData['uri']);
+
+ }
+
+ /**
+ * Returns the mime content-type
+ *
+ * @return string
+ */
+ public function getContentType() {
+
+ return 'text/calendar; charset=utf-8';
+
+ }
+
+ /**
+ * Returns an ETag for this object.
+ *
+ * The ETag is an arbitrary string, but MUST be surrounded by double-quotes.
+ *
+ * @return string
+ */
+ public function getETag() {
+
+ if (isset($this->objectData['etag'])) {
+ return $this->objectData['etag'];
+ } else {
+ return '"' . md5($this->get()). '"';
+ }
+
+ }
+
+ /**
+ * Returns the last modification date as a unix timestamp
+ *
+ * @return int
+ */
+ public function getLastModified() {
+
+ return $this->objectData['lastmodified'];
+
+ }
+
+ /**
+ * Returns the size of this object in bytes
+ *
+ * @return int
+ */
+ public function getSize() {
+
+ if (array_key_exists('size',$this->objectData)) {
+ return $this->objectData['size'];
+ } else {
+ return strlen($this->get());
+ }
+
+ }
+
+ /**
+ * Returns the owner principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getOwner() {
+
+ return $this->calendarInfo['principaluri'];
+
+ }
+
+ /**
+ * Returns a group principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getGroup() {
+
+ return null;
+
+ }
+
+ /**
+ * Returns a list of ACE's for this node.
+ *
+ * Each ACE has the following properties:
+ * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
+ * currently the only supported privileges
+ * * 'principal', a url to the principal who owns the node
+ * * 'protected' (optional), indicating that this ACE is not allowed to
+ * be updated.
+ *
+ * @return array
+ */
+ public function getACL() {
+
+ // An alternative acl may be specified in the object data.
+ if (isset($this->objectData['acl'])) {
+ return $this->objectData['acl'];
+ }
+
+ // The default ACL
+ return array(
+ array(
+ 'privilege' => '{DAV:}read',
+ 'principal' => $this->calendarInfo['principaluri'],
+ 'protected' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}write',
+ 'principal' => $this->calendarInfo['principaluri'],
+ 'protected' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}read',
+ 'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-write',
+ 'protected' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}write',
+ 'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-write',
+ 'protected' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}read',
+ 'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-read',
+ 'protected' => true,
+ ),
+
+ );
+
+ }
+
+ /**
+ * Updates the ACL
+ *
+ * This method will receive a list of new ACE's.
+ *
+ * @param array $acl
+ * @return void
+ */
+ public function setACL(array $acl) {
+
+ throw new \Sabre\DAV\Exception\MethodNotAllowed('Changing ACL is not yet supported');
+
+ }
+
+ /**
+ * Returns the list of supported privileges for this node.
+ *
+ * The returned data structure is a list of nested privileges.
+ * See \Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple
+ * standard structure.
+ *
+ * If null is returned from this method, the default privilege set is used,
+ * which is fine for most common usecases.
+ *
+ * @return array|null
+ */
+ public function getSupportedPrivilegeSet() {
+
+ return null;
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/CalendarQueryParser.php b/app/Gdoo/Calendar/Sabre/CalDAV/CalendarQueryParser.php
new file mode 100644
index 00000000..1b933983
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/CalendarQueryParser.php
@@ -0,0 +1,298 @@
+dom = $dom;
+ $this->xpath = new \DOMXPath($dom);
+ $this->xpath->registerNameSpace('cal',Plugin::NS_CALDAV);
+ $this->xpath->registerNameSpace('dav','urn:DAV');
+
+ }
+
+ /**
+ * Parses the request.
+ *
+ * @return void
+ */
+ public function parse() {
+
+ $filterNode = null;
+
+ $filter = $this->xpath->query('/cal:calendar-query/cal:filter');
+ if ($filter->length !== 1) {
+ throw new \Sabre\DAV\Exception\BadRequest('Only one filter element is allowed');
+ }
+
+ $compFilters = $this->parseCompFilters($filter->item(0));
+ if (count($compFilters)!==1) {
+ throw new \Sabre\DAV\Exception\BadRequest('There must be exactly 1 top-level comp-filter.');
+ }
+
+ $this->filters = $compFilters[0];
+ $this->requestedProperties = array_keys(\Sabre\DAV\XMLUtil::parseProperties($this->dom->firstChild));
+
+ $expand = $this->xpath->query('/cal:calendar-query/dav:prop/cal:calendar-data/cal:expand');
+ if ($expand->length>0) {
+ $this->expand = $this->parseExpand($expand->item(0));
+ }
+
+
+ }
+
+ /**
+ * Parses all the 'comp-filter' elements from a node
+ *
+ * @param \DOMElement $parentNode
+ * @return array
+ */
+ protected function parseCompFilters(\DOMElement $parentNode) {
+
+ $compFilterNodes = $this->xpath->query('cal:comp-filter', $parentNode);
+ $result = array();
+
+ for($ii=0; $ii < $compFilterNodes->length; $ii++) {
+
+ $compFilterNode = $compFilterNodes->item($ii);
+
+ $compFilter = array();
+ $compFilter['name'] = $compFilterNode->getAttribute('name');
+ $compFilter['is-not-defined'] = $this->xpath->query('cal:is-not-defined', $compFilterNode)->length>0;
+ $compFilter['comp-filters'] = $this->parseCompFilters($compFilterNode);
+ $compFilter['prop-filters'] = $this->parsePropFilters($compFilterNode);
+ $compFilter['time-range'] = $this->parseTimeRange($compFilterNode);
+
+ if ($compFilter['time-range'] && !in_array($compFilter['name'],array(
+ 'VEVENT',
+ 'VTODO',
+ 'VJOURNAL',
+ 'VFREEBUSY',
+ 'VALARM',
+ ))) {
+ throw new \Sabre\DAV\Exception\BadRequest('The time-range filter is not defined for the ' . $compFilter['name'] . ' component');
+ };
+
+ $result[] = $compFilter;
+
+ }
+
+ return $result;
+
+ }
+
+ /**
+ * Parses all the prop-filter elements from a node
+ *
+ * @param \DOMElement $parentNode
+ * @return array
+ */
+ protected function parsePropFilters(\DOMElement $parentNode) {
+
+ $propFilterNodes = $this->xpath->query('cal:prop-filter', $parentNode);
+ $result = array();
+
+ for ($ii=0; $ii < $propFilterNodes->length; $ii++) {
+
+ $propFilterNode = $propFilterNodes->item($ii);
+ $propFilter = array();
+ $propFilter['name'] = $propFilterNode->getAttribute('name');
+ $propFilter['is-not-defined'] = $this->xpath->query('cal:is-not-defined', $propFilterNode)->length>0;
+ $propFilter['param-filters'] = $this->parseParamFilters($propFilterNode);
+ $propFilter['text-match'] = $this->parseTextMatch($propFilterNode);
+ $propFilter['time-range'] = $this->parseTimeRange($propFilterNode);
+
+ $result[] = $propFilter;
+
+ }
+
+ return $result;
+
+ }
+
+ /**
+ * Parses the param-filter element
+ *
+ * @param \DOMElement $parentNode
+ * @return array
+ */
+ protected function parseParamFilters(\DOMElement $parentNode) {
+
+ $paramFilterNodes = $this->xpath->query('cal:param-filter', $parentNode);
+ $result = array();
+
+ for($ii=0;$ii<$paramFilterNodes->length;$ii++) {
+
+ $paramFilterNode = $paramFilterNodes->item($ii);
+ $paramFilter = array();
+ $paramFilter['name'] = $paramFilterNode->getAttribute('name');
+ $paramFilter['is-not-defined'] = $this->xpath->query('cal:is-not-defined', $paramFilterNode)->length>0;
+ $paramFilter['text-match'] = $this->parseTextMatch($paramFilterNode);
+
+ $result[] = $paramFilter;
+
+ }
+
+ return $result;
+
+ }
+
+ /**
+ * Parses the text-match element
+ *
+ * @param \DOMElement $parentNode
+ * @return array|null
+ */
+ protected function parseTextMatch(\DOMElement $parentNode) {
+
+ $textMatchNodes = $this->xpath->query('cal:text-match', $parentNode);
+
+ if ($textMatchNodes->length === 0)
+ return null;
+
+ $textMatchNode = $textMatchNodes->item(0);
+ $negateCondition = $textMatchNode->getAttribute('negate-condition');
+ $negateCondition = $negateCondition==='yes';
+ $collation = $textMatchNode->getAttribute('collation');
+ if (!$collation) $collation = 'i;ascii-casemap';
+
+ return array(
+ 'negate-condition' => $negateCondition,
+ 'collation' => $collation,
+ 'value' => $textMatchNode->nodeValue
+ );
+
+ }
+
+ /**
+ * Parses the time-range element
+ *
+ * @param \DOMElement $parentNode
+ * @return array|null
+ */
+ protected function parseTimeRange(\DOMElement $parentNode) {
+
+ $timeRangeNodes = $this->xpath->query('cal:time-range', $parentNode);
+ if ($timeRangeNodes->length === 0) {
+ return null;
+ }
+
+ $timeRangeNode = $timeRangeNodes->item(0);
+
+ if ($start = $timeRangeNode->getAttribute('start')) {
+ $start = VObject\DateTimeParser::parseDateTime($start);
+ } else {
+ $start = null;
+ }
+ if ($end = $timeRangeNode->getAttribute('end')) {
+ $end = VObject\DateTimeParser::parseDateTime($end);
+ } else {
+ $end = null;
+ }
+
+ if (!is_null($start) && !is_null($end) && $end <= $start) {
+ throw new \Sabre\DAV\Exception\BadRequest('The end-date must be larger than the start-date in the time-range filter');
+ }
+
+ return array(
+ 'start' => $start,
+ 'end' => $end,
+ );
+
+ }
+
+ /**
+ * Parses the CALDAV:expand element
+ *
+ * @param \DOMElement $parentNode
+ * @return void
+ */
+ protected function parseExpand(\DOMElement $parentNode) {
+
+ $start = $parentNode->getAttribute('start');
+ if(!$start) {
+ throw new \Sabre\DAV\Exception\BadRequest('The "start" attribute is required for the CALDAV:expand element');
+ }
+ $start = VObject\DateTimeParser::parseDateTime($start);
+
+ $end = $parentNode->getAttribute('end');
+ if(!$end) {
+ throw new \Sabre\DAV\Exception\BadRequest('The "end" attribute is required for the CALDAV:expand element');
+ }
+
+ $end = VObject\DateTimeParser::parseDateTime($end);
+
+ if ($end <= $start) {
+ throw new \Sabre\DAV\Exception\BadRequest('The end-date must be larger than the start-date in the expand element.');
+ }
+
+ return array(
+ 'start' => $start,
+ 'end' => $end,
+ );
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/CalendarQueryValidator.php b/app/Gdoo/Calendar/Sabre/CalDAV/CalendarQueryValidator.php
new file mode 100644
index 00000000..a5c2ed8e
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/CalendarQueryValidator.php
@@ -0,0 +1,392 @@
+name !== $filters['name']) {
+ return false;
+ }
+
+ return
+ $this->validateCompFilters($vObject, $filters['comp-filters']) &&
+ $this->validatePropFilters($vObject, $filters['prop-filters']);
+
+
+ }
+
+ /**
+ * This method checks the validity of comp-filters.
+ *
+ * A list of comp-filters needs to be specified. Also the parent of the
+ * component we're checking should be specified, not the component to check
+ * itself.
+ *
+ * @param VObject\Component $parent
+ * @param array $filters
+ * @return bool
+ */
+ protected function validateCompFilters(VObject\Component $parent, array $filters) {
+
+ foreach($filters as $filter) {
+
+ $isDefined = isset($parent->{$filter['name']});
+
+ if ($filter['is-not-defined']) {
+
+ if ($isDefined) {
+ return false;
+ } else {
+ continue;
+ }
+
+ }
+ if (!$isDefined) {
+ return false;
+ }
+
+ if ($filter['time-range']) {
+ foreach($parent->{$filter['name']} as $subComponent) {
+ if ($this->validateTimeRange($subComponent, $filter['time-range']['start'], $filter['time-range']['end'])) {
+ continue 2;
+ }
+ }
+ return false;
+ }
+
+ if (!$filter['comp-filters'] && !$filter['prop-filters']) {
+ continue;
+ }
+
+ // If there are sub-filters, we need to find at least one component
+ // for which the subfilters hold true.
+ foreach($parent->{$filter['name']} as $subComponent) {
+
+ if (
+ $this->validateCompFilters($subComponent, $filter['comp-filters']) &&
+ $this->validatePropFilters($subComponent, $filter['prop-filters'])) {
+ // We had a match, so this comp-filter succeeds
+ continue 2;
+ }
+
+ }
+
+ // If we got here it means there were sub-comp-filters or
+ // sub-prop-filters and there was no match. This means this filter
+ // needs to return false.
+ return false;
+
+ }
+
+ // If we got here it means we got through all comp-filters alive so the
+ // filters were all true.
+ return true;
+
+ }
+
+ /**
+ * This method checks the validity of prop-filters.
+ *
+ * A list of prop-filters needs to be specified. Also the parent of the
+ * property we're checking should be specified, not the property to check
+ * itself.
+ *
+ * @param VObject\Component $parent
+ * @param array $filters
+ * @return bool
+ */
+ protected function validatePropFilters(VObject\Component $parent, array $filters) {
+
+ foreach($filters as $filter) {
+
+ $isDefined = isset($parent->{$filter['name']});
+
+ if ($filter['is-not-defined']) {
+
+ if ($isDefined) {
+ return false;
+ } else {
+ continue;
+ }
+
+ }
+ if (!$isDefined) {
+ return false;
+ }
+
+ if ($filter['time-range']) {
+ foreach($parent->{$filter['name']} as $subComponent) {
+ if ($this->validateTimeRange($subComponent, $filter['time-range']['start'], $filter['time-range']['end'])) {
+ continue 2;
+ }
+ }
+ return false;
+ }
+
+ if (!$filter['param-filters'] && !$filter['text-match']) {
+ continue;
+ }
+
+ // If there are sub-filters, we need to find at least one property
+ // for which the subfilters hold true.
+ foreach($parent->{$filter['name']} as $subComponent) {
+
+ if(
+ $this->validateParamFilters($subComponent, $filter['param-filters']) &&
+ (!$filter['text-match'] || $this->validateTextMatch($subComponent, $filter['text-match']))
+ ) {
+ // We had a match, so this prop-filter succeeds
+ continue 2;
+ }
+
+ }
+
+ // If we got here it means there were sub-param-filters or
+ // text-match filters and there was no match. This means the
+ // filter needs to return false.
+ return false;
+
+ }
+
+ // If we got here it means we got through all prop-filters alive so the
+ // filters were all true.
+ return true;
+
+ }
+
+ /**
+ * This method checks the validity of param-filters.
+ *
+ * A list of param-filters needs to be specified. Also the parent of the
+ * parameter we're checking should be specified, not the parameter to check
+ * itself.
+ *
+ * @param VObject\Property $parent
+ * @param array $filters
+ * @return bool
+ */
+ protected function validateParamFilters(VObject\Property $parent, array $filters) {
+
+ foreach($filters as $filter) {
+
+ $isDefined = isset($parent[$filter['name']]);
+
+ if ($filter['is-not-defined']) {
+
+ if ($isDefined) {
+ return false;
+ } else {
+ continue;
+ }
+
+ }
+ if (!$isDefined) {
+ return false;
+ }
+
+ if (!$filter['text-match']) {
+ continue;
+ }
+
+ if (version_compare(VObject\Version::VERSION, '3.0.0beta1', '>=')) {
+
+ // If there are sub-filters, we need to find at least one parameter
+ // for which the subfilters hold true.
+ foreach($parent[$filter['name']]->getParts() as $subParam) {
+
+ if($this->validateTextMatch($subParam,$filter['text-match'])) {
+ // We had a match, so this param-filter succeeds
+ continue 2;
+ }
+
+ }
+
+ } else {
+
+ // If there are sub-filters, we need to find at least one parameter
+ // for which the subfilters hold true.
+ foreach($parent[$filter['name']] as $subParam) {
+
+ if($this->validateTextMatch($subParam,$filter['text-match'])) {
+ // We had a match, so this param-filter succeeds
+ continue 2;
+ }
+
+ }
+
+ }
+
+ // If we got here it means there was a text-match filter and there
+ // were no matches. This means the filter needs to return false.
+ return false;
+
+ }
+
+ // If we got here it means we got through all param-filters alive so the
+ // filters were all true.
+ return true;
+
+ }
+
+ /**
+ * This method checks the validity of a text-match.
+ *
+ * A single text-match should be specified as well as the specific property
+ * or parameter we need to validate.
+ *
+ * @param VObject\Node|string $check Value to check against.
+ * @param array $textMatch
+ * @return bool
+ */
+ protected function validateTextMatch($check, array $textMatch) {
+
+ if ($check instanceof VObject\Node) {
+ $check = (string)$check;
+ }
+
+ $isMatching = \Sabre\DAV\StringUtil::textMatch($check, $textMatch['value'], $textMatch['collation']);
+
+ return ($textMatch['negate-condition'] xor $isMatching);
+
+ }
+
+ /**
+ * Validates if a component matches the given time range.
+ *
+ * This is all based on the rules specified in rfc4791, which are quite
+ * complex.
+ *
+ * @param VObject\Node $component
+ * @param DateTime $start
+ * @param DateTime $end
+ * @return bool
+ */
+ protected function validateTimeRange(VObject\Node $component, $start, $end) {
+
+ if (is_null($start)) {
+ $start = new DateTime('1900-01-01');
+ }
+ if (is_null($end)) {
+ $end = new DateTime('3000-01-01');
+ }
+
+ switch($component->name) {
+
+ case 'VEVENT' :
+ case 'VTODO' :
+ case 'VJOURNAL' :
+
+ return $component->isInTimeRange($start, $end);
+
+ case 'VALARM' :
+
+ // If the valarm is wrapped in a recurring event, we need to
+ // expand the recursions, and validate each.
+ //
+ // Our datamodel doesn't easily allow us to do this straight
+ // in the VALARM component code, so this is a hack, and an
+ // expensive one too.
+ if ($component->parent->name === 'VEVENT' && $component->parent->RRULE) {
+
+ // Fire up the iterator!
+ $it = new VObject\RecurrenceIterator($component->parent->parent, (string)$component->parent->UID);
+ while($it->valid()) {
+ $expandedEvent = $it->getEventObject();
+
+ // We need to check from these expanded alarms, which
+ // one is the first to trigger. Based on this, we can
+ // determine if we can 'give up' expanding events.
+ $firstAlarm = null;
+ if ($expandedEvent->VALARM !== null) {
+ foreach($expandedEvent->VALARM as $expandedAlarm) {
+
+ $effectiveTrigger = $expandedAlarm->getEffectiveTriggerTime();
+ if ($expandedAlarm->isInTimeRange($start, $end)) {
+ return true;
+ }
+
+ if ((string)$expandedAlarm->TRIGGER['VALUE'] === 'DATE-TIME') {
+ // This is an alarm with a non-relative trigger
+ // time, likely created by a buggy client. The
+ // implication is that every alarm in this
+ // recurring event trigger at the exact same
+ // time. It doesn't make sense to traverse
+ // further.
+ } else {
+ // We store the first alarm as a means to
+ // figure out when we can stop traversing.
+ if (!$firstAlarm || $effectiveTrigger < $firstAlarm) {
+ $firstAlarm = $effectiveTrigger;
+ }
+ }
+ }
+ }
+ if (is_null($firstAlarm)) {
+ // No alarm was found.
+ //
+ // Or technically: No alarm that will change for
+ // every instance of the recurrence was found,
+ // which means we can assume there was no match.
+ return false;
+ }
+ if ($firstAlarm > $end) {
+ return false;
+ }
+ $it->next();
+ }
+ return false;
+ } else {
+ return $component->isInTimeRange($start, $end);
+ }
+
+ case 'VFREEBUSY' :
+ throw new \Sabre\DAV\Exception\NotImplemented('time-range filters are currently not supported on ' . $component->name . ' components');
+
+ case 'COMPLETED' :
+ case 'CREATED' :
+ case 'DTEND' :
+ case 'DTSTAMP' :
+ case 'DTSTART' :
+ case 'DUE' :
+ case 'LAST-MODIFIED' :
+ return ($start <= $component->getDateTime() && $end >= $component->getDateTime());
+
+
+
+ default :
+ throw new \Sabre\DAV\Exception\BadRequest('You cannot create a time-range filter on a ' . $component->name . ' component');
+
+ }
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/CalendarRootNode.php b/app/Gdoo/Calendar/Sabre/CalDAV/CalendarRootNode.php
new file mode 100644
index 00000000..905155a6
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/CalendarRootNode.php
@@ -0,0 +1,77 @@
+caldavBackend = $caldavBackend;
+
+ }
+
+ /**
+ * Returns the nodename
+ *
+ * We're overriding this, because the default will be the 'principalPrefix',
+ * and we want it to be Sabre\CalDAV\Plugin::CALENDAR_ROOT
+ *
+ * @return string
+ */
+ public function getName() {
+
+ return Plugin::CALENDAR_ROOT;
+
+ }
+
+ /**
+ * This method returns a node for a principal.
+ *
+ * The passed array contains principal information, and is guaranteed to
+ * at least contain a uri item. Other properties may or may not be
+ * supplied by the authentication backend.
+ *
+ * @param array $principal
+ * @return \Sabre\DAV\INode
+ */
+ public function getChildForPrincipal(array $principal) {
+
+ return new UserCalendars($this->caldavBackend, $principal);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Exception/InvalidComponentType.php b/app/Gdoo/Calendar/Sabre/CalDAV/Exception/InvalidComponentType.php
new file mode 100644
index 00000000..9c92672b
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Exception/InvalidComponentType.php
@@ -0,0 +1,35 @@
+ownerDocument;
+
+ $np = $doc->createElementNS(CalDAV\Plugin::NS_CALDAV,'cal:supported-calendar-component');
+ $errorNode->appendChild($np);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/ICSExportPlugin.php b/app/Gdoo/Calendar/Sabre/CalDAV/ICSExportPlugin.php
new file mode 100644
index 00000000..8e1c4f2a
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/ICSExportPlugin.php
@@ -0,0 +1,142 @@
+server = $server;
+ $this->server->subscribeEvent('beforeMethod',array($this,'beforeMethod'), 90);
+
+ }
+
+ /**
+ * 'beforeMethod' event handles. This event handles intercepts GET requests ending
+ * with ?export
+ *
+ * @param string $method
+ * @param string $uri
+ * @return bool
+ */
+ public function beforeMethod($method, $uri) {
+
+ if ($method!='GET') return;
+ if ($this->server->httpRequest->getQueryString()!='export') return;
+
+ // splitting uri
+ list($uri) = explode('?',$uri,2);
+
+ $node = $this->server->tree->getNodeForPath($uri);
+
+ if (!($node instanceof Calendar)) return;
+
+ // Checking ACL, if available.
+ if ($aclPlugin = $this->server->getPlugin('acl')) {
+ $aclPlugin->checkPrivileges($uri, '{DAV:}read');
+ }
+
+ $this->server->httpResponse->setHeader('Content-Type','text/calendar');
+ $this->server->httpResponse->sendStatus(200);
+
+ $nodes = $this->server->getPropertiesForPath($uri, array(
+ '{' . Plugin::NS_CALDAV . '}calendar-data',
+ ),1);
+
+ $this->server->httpResponse->sendBody($this->generateICS($nodes));
+
+ // Returning false to break the event chain
+ return false;
+
+ }
+
+ /**
+ * Merges all calendar objects, and builds one big ics export
+ *
+ * @param array $nodes
+ * @return string
+ */
+ public function generateICS(array $nodes) {
+
+ $calendar = new VObject\Component\VCalendar();
+ $calendar->version = '2.0';
+ if (DAV\Server::$exposeVersion) {
+ $calendar->prodid = '-//SabreDAV//SabreDAV ' . DAV\Version::VERSION . '//EN';
+ } else {
+ $calendar->prodid = '-//SabreDAV//SabreDAV//EN';
+ }
+ $calendar->calscale = 'GREGORIAN';
+
+ $collectedTimezones = array();
+
+ $timezones = array();
+ $objects = array();
+
+ foreach($nodes as $node) {
+
+ if (!isset($node[200]['{' . Plugin::NS_CALDAV . '}calendar-data'])) {
+ continue;
+ }
+ $nodeData = $node[200]['{' . Plugin::NS_CALDAV . '}calendar-data'];
+
+ $nodeComp = VObject\Reader::read($nodeData);
+
+ foreach($nodeComp->children() as $child) {
+
+ switch($child->name) {
+ case 'VEVENT' :
+ case 'VTODO' :
+ case 'VJOURNAL' :
+ $objects[] = $child;
+ break;
+
+ // VTIMEZONE is special, because we need to filter out the duplicates
+ case 'VTIMEZONE' :
+ // Naively just checking tzid.
+ if (in_array((string)$child->TZID, $collectedTimezones)) continue;
+
+ $timezones[] = $child;
+ $collectedTimezones[] = $child->TZID;
+ break;
+
+ }
+
+ }
+
+ }
+
+ foreach($timezones as $tz) $calendar->add($tz);
+ foreach($objects as $obj) $calendar->add($obj);
+
+ return $calendar->serialize();
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/ICalendar.php b/app/Gdoo/Calendar/Sabre/CalDAV/ICalendar.php
new file mode 100644
index 00000000..c56a81a1
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/ICalendar.php
@@ -0,0 +1,36 @@
+caldavBackend = $caldavBackend;
+ $this->principalUri = $principalUri;
+
+ }
+
+ /**
+ * Returns all notifications for a principal
+ *
+ * @return array
+ */
+ public function getChildren() {
+
+ $children = array();
+ $notifications = $this->caldavBackend->getNotificationsForPrincipal($this->principalUri);
+
+ foreach($notifications as $notification) {
+
+ $children[] = new Node(
+ $this->caldavBackend,
+ $this->principalUri,
+ $notification
+ );
+ }
+
+ return $children;
+
+ }
+
+ /**
+ * Returns the name of this object
+ *
+ * @return string
+ */
+ public function getName() {
+
+ return 'notifications';
+
+ }
+
+ /**
+ * Returns the owner principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getOwner() {
+
+ return $this->principalUri;
+
+ }
+
+ /**
+ * Returns a group principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getGroup() {
+
+ return null;
+
+ }
+
+ /**
+ * Returns a list of ACE's for this node.
+ *
+ * Each ACE has the following properties:
+ * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
+ * currently the only supported privileges
+ * * 'principal', a url to the principal who owns the node
+ * * 'protected' (optional), indicating that this ACE is not allowed to
+ * be updated.
+ *
+ * @return array
+ */
+ public function getACL() {
+
+ return array(
+ array(
+ 'principal' => $this->getOwner(),
+ 'privilege' => '{DAV:}read',
+ 'protected' => true,
+ ),
+ array(
+ 'principal' => $this->getOwner(),
+ 'privilege' => '{DAV:}write',
+ 'protected' => true,
+ )
+ );
+
+ }
+
+ /**
+ * Updates the ACL
+ *
+ * This method will receive a list of new ACE's as an array argument.
+ *
+ * @param array $acl
+ * @return void
+ */
+ public function setACL(array $acl) {
+
+ throw new DAV\Exception\NotImplemented('Updating ACLs is not implemented here');
+
+ }
+
+ /**
+ * Returns the list of supported privileges for this node.
+ *
+ * The returned data structure is a list of nested privileges.
+ * See Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple
+ * standard structure.
+ *
+ * If null is returned from this method, the default privilege set is used,
+ * which is fine for most common usecases.
+ *
+ * @return array|null
+ */
+ public function getSupportedPrivilegeSet() {
+
+ return null;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Notifications/ICollection.php b/app/Gdoo/Calendar/Sabre/CalDAV/Notifications/ICollection.php
new file mode 100644
index 00000000..99324648
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Notifications/ICollection.php
@@ -0,0 +1,24 @@
+caldavBackend = $caldavBackend;
+ $this->principalUri = $principalUri;
+ $this->notification = $notification;
+
+ }
+
+ /**
+ * Returns the path name for this notification
+ *
+ * @return id
+ */
+ public function getName() {
+
+ return $this->notification->getId() . '.xml';
+
+ }
+
+ /**
+ * Returns the etag for the notification.
+ *
+ * The etag must be surrounded by litteral double-quotes.
+ *
+ * @return string
+ */
+ public function getETag() {
+
+ return $this->notification->getETag();
+
+ }
+
+ /**
+ * This method must return an xml element, using the
+ * Sabre\CalDAV\Notifications\INotificationType classes.
+ *
+ * @return INotificationType
+ */
+ public function getNotificationType() {
+
+ return $this->notification;
+
+ }
+
+ /**
+ * Deletes this notification
+ *
+ * @return void
+ */
+ public function delete() {
+
+ $this->caldavBackend->deleteNotification($this->getOwner(), $this->notification);
+
+ }
+
+ /**
+ * Returns the owner principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getOwner() {
+
+ return $this->principalUri;
+
+ }
+
+ /**
+ * Returns a group principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getGroup() {
+
+ return null;
+
+ }
+
+ /**
+ * Returns a list of ACE's for this node.
+ *
+ * Each ACE has the following properties:
+ * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
+ * currently the only supported privileges
+ * * 'principal', a url to the principal who owns the node
+ * * 'protected' (optional), indicating that this ACE is not allowed to
+ * be updated.
+ *
+ * @return array
+ */
+ public function getACL() {
+
+ return array(
+ array(
+ 'principal' => $this->getOwner(),
+ 'privilege' => '{DAV:}read',
+ 'protected' => true,
+ ),
+ array(
+ 'principal' => $this->getOwner(),
+ 'privilege' => '{DAV:}write',
+ 'protected' => true,
+ )
+ );
+
+ }
+
+ /**
+ * Updates the ACL
+ *
+ * This method will receive a list of new ACE's as an array argument.
+ *
+ * @param array $acl
+ * @return void
+ */
+ public function setACL(array $acl) {
+
+ throw new DAV\Exception\NotImplemented('Updating ACLs is not implemented here');
+
+ }
+
+ /**
+ * Returns the list of supported privileges for this node.
+ *
+ * The returned data structure is a list of nested privileges.
+ * See Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple
+ * standard structure.
+ *
+ * If null is returned from this method, the default privilege set is used,
+ * which is fine for most common usecases.
+ *
+ * @return array|null
+ */
+ public function getSupportedPrivilegeSet() {
+
+ return null;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Notifications/Notification/Invite.php b/app/Gdoo/Calendar/Sabre/CalDAV/Notifications/Notification/Invite.php
new file mode 100644
index 00000000..72f82e2b
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Notifications/Notification/Invite.php
@@ -0,0 +1,324 @@
+$value) {
+ if (!property_exists($this, $key)) {
+ throw new \InvalidArgumentException('Unknown option: ' . $key);
+ }
+ $this->$key = $value;
+ }
+
+ }
+
+ /**
+ * Serializes the notification as a single property.
+ *
+ * You should usually just encode the single top-level element of the
+ * notification.
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $node
+ * @return void
+ */
+ public function serialize(DAV\Server $server, \DOMElement $node) {
+
+ $prop = $node->ownerDocument->createElement('cs:invite-notification');
+ $node->appendChild($prop);
+
+ }
+
+ /**
+ * This method serializes the entire notification, as it is used in the
+ * response body.
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $node
+ * @return void
+ */
+ public function serializeBody(DAV\Server $server, \DOMElement $node) {
+
+ $doc = $node->ownerDocument;
+
+ $dt = $doc->createElement('cs:dtstamp');
+ $this->dtStamp->setTimezone(new \DateTimezone('GMT'));
+ $dt->appendChild($doc->createTextNode($this->dtStamp->format('Ymd\\THis\\Z')));
+ $node->appendChild($dt);
+
+ $prop = $doc->createElement('cs:invite-notification');
+ $node->appendChild($prop);
+
+ $uid = $doc->createElement('cs:uid');
+ $uid->appendChild( $doc->createTextNode($this->id) );
+ $prop->appendChild($uid);
+
+ $href = $doc->createElement('d:href');
+ $href->appendChild( $doc->createTextNode( $this->href ) );
+ $prop->appendChild($href);
+
+ $nodeName = null;
+ switch($this->type) {
+
+ case SharingPlugin::STATUS_ACCEPTED :
+ $nodeName = 'cs:invite-accepted';
+ break;
+ case SharingPlugin::STATUS_DECLINED :
+ $nodeName = 'cs:invite-declined';
+ break;
+ case SharingPlugin::STATUS_DELETED :
+ $nodeName = 'cs:invite-deleted';
+ break;
+ case SharingPlugin::STATUS_NORESPONSE :
+ $nodeName = 'cs:invite-noresponse';
+ break;
+
+ }
+ $prop->appendChild(
+ $doc->createElement($nodeName)
+ );
+ $hostHref = $doc->createElement('d:href', $server->getBaseUri() . $this->hostUrl);
+ $hostUrl = $doc->createElement('cs:hosturl');
+ $hostUrl->appendChild($hostHref);
+ $prop->appendChild($hostUrl);
+
+ $access = $doc->createElement('cs:access');
+ if ($this->readOnly) {
+ $access->appendChild($doc->createElement('cs:read'));
+ } else {
+ $access->appendChild($doc->createElement('cs:read-write'));
+ }
+ $prop->appendChild($access);
+
+ $organizerUrl = $doc->createElement('cs:organizer');
+ // If the organizer contains a 'mailto:' part, it means it should be
+ // treated as absolute.
+ if (strtolower(substr($this->organizer,0,7))==='mailto:') {
+ $organizerHref = new DAV\Property\Href($this->organizer, false);
+ } else {
+ $organizerHref = new DAV\Property\Href($this->organizer, true);
+ }
+ $organizerHref->serialize($server, $organizerUrl);
+
+ if ($this->commonName) {
+ $commonName = $doc->createElement('cs:common-name');
+ $commonName->appendChild($doc->createTextNode($this->commonName));
+ $organizerUrl->appendChild($commonName);
+
+ $commonNameOld = $doc->createElement('cs:organizer-cn');
+ $commonNameOld->appendChild($doc->createTextNode($this->commonName));
+ $prop->appendChild($commonNameOld);
+
+ }
+ if ($this->firstName) {
+ $firstName = $doc->createElement('cs:first-name');
+ $firstName->appendChild($doc->createTextNode($this->firstName));
+ $organizerUrl->appendChild($firstName);
+
+ $firstNameOld = $doc->createElement('cs:organizer-first');
+ $firstNameOld->appendChild($doc->createTextNode($this->firstName));
+ $prop->appendChild($firstNameOld);
+ }
+ if ($this->lastName) {
+ $lastName = $doc->createElement('cs:last-name');
+ $lastName->appendChild($doc->createTextNode($this->lastName));
+ $organizerUrl->appendChild($lastName);
+
+ $lastNameOld = $doc->createElement('cs:organizer-last');
+ $lastNameOld->appendChild($doc->createTextNode($this->lastName));
+ $prop->appendChild($lastNameOld);
+ }
+ $prop->appendChild($organizerUrl);
+
+ if ($this->summary) {
+ $summary = $doc->createElement('cs:summary');
+ $summary->appendChild($doc->createTextNode($this->summary));
+ $prop->appendChild($summary);
+ }
+ if ($this->supportedComponents) {
+
+ $xcomp = $doc->createElement('cal:supported-calendar-component-set');
+ $this->supportedComponents->serialize($server, $xcomp);
+ $prop->appendChild($xcomp);
+
+ }
+
+ }
+
+ /**
+ * Returns a unique id for this notification
+ *
+ * This is just the base url. This should generally be some kind of unique
+ * id.
+ *
+ * @return string
+ */
+ public function getId() {
+
+ return $this->id;
+
+ }
+
+ /**
+ * Returns the ETag for this notification.
+ *
+ * The ETag must be surrounded by literal double-quotes.
+ *
+ * @return string
+ */
+ public function getETag() {
+
+ return $this->etag;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Notifications/Notification/InviteReply.php b/app/Gdoo/Calendar/Sabre/CalDAV/Notifications/Notification/InviteReply.php
new file mode 100644
index 00000000..a7ec277e
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Notifications/Notification/InviteReply.php
@@ -0,0 +1,218 @@
+$value) {
+ if (!property_exists($this, $key)) {
+ throw new \InvalidArgumentException('Unknown option: ' . $key);
+ }
+ $this->$key = $value;
+ }
+
+ }
+
+ /**
+ * Serializes the notification as a single property.
+ *
+ * You should usually just encode the single top-level element of the
+ * notification.
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $node
+ * @return void
+ */
+ public function serialize(DAV\Server $server, \DOMElement $node) {
+
+ $prop = $node->ownerDocument->createElement('cs:invite-reply');
+ $node->appendChild($prop);
+
+ }
+
+ /**
+ * This method serializes the entire notification, as it is used in the
+ * response body.
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $node
+ * @return void
+ */
+ public function serializeBody(DAV\Server $server, \DOMElement $node) {
+
+ $doc = $node->ownerDocument;
+
+ $dt = $doc->createElement('cs:dtstamp');
+ $this->dtStamp->setTimezone(new \DateTimezone('GMT'));
+ $dt->appendChild($doc->createTextNode($this->dtStamp->format('Ymd\\THis\\Z')));
+ $node->appendChild($dt);
+
+ $prop = $doc->createElement('cs:invite-reply');
+ $node->appendChild($prop);
+
+ $uid = $doc->createElement('cs:uid');
+ $uid->appendChild($doc->createTextNode($this->id));
+ $prop->appendChild($uid);
+
+ $inReplyTo = $doc->createElement('cs:in-reply-to');
+ $inReplyTo->appendChild( $doc->createTextNode($this->inReplyTo) );
+ $prop->appendChild($inReplyTo);
+
+ $href = $doc->createElement('d:href');
+ $href->appendChild( $doc->createTextNode($this->href) );
+ $prop->appendChild($href);
+
+ $nodeName = null;
+ switch($this->type) {
+
+ case SharingPlugin::STATUS_ACCEPTED :
+ $nodeName = 'cs:invite-accepted';
+ break;
+ case SharingPlugin::STATUS_DECLINED :
+ $nodeName = 'cs:invite-declined';
+ break;
+
+ }
+ $prop->appendChild(
+ $doc->createElement($nodeName)
+ );
+ $hostHref = $doc->createElement('d:href', $server->getBaseUri() . $this->hostUrl);
+ $hostUrl = $doc->createElement('cs:hosturl');
+ $hostUrl->appendChild($hostHref);
+ $prop->appendChild($hostUrl);
+
+ if ($this->summary) {
+ $summary = $doc->createElement('cs:summary');
+ $summary->appendChild($doc->createTextNode($this->summary));
+ $prop->appendChild($summary);
+ }
+
+ }
+
+ /**
+ * Returns a unique id for this notification
+ *
+ * This is just the base url. This should generally be some kind of unique
+ * id.
+ *
+ * @return string
+ */
+ public function getId() {
+
+ return $this->id;
+
+ }
+
+ /**
+ * Returns the ETag for this notification.
+ *
+ * The ETag must be surrounded by literal double-quotes.
+ *
+ * @return string
+ */
+ public function getETag() {
+
+ return $this->etag;
+
+ }
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Notifications/Notification/SystemStatus.php b/app/Gdoo/Calendar/Sabre/CalDAV/Notifications/Notification/SystemStatus.php
new file mode 100644
index 00000000..4c162c03
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Notifications/Notification/SystemStatus.php
@@ -0,0 +1,182 @@
+id = $id;
+ $this->type = $type;
+ $this->description = $description;
+ $this->href = $href;
+ $this->etag = $etag;
+
+ }
+
+ /**
+ * Serializes the notification as a single property.
+ *
+ * You should usually just encode the single top-level element of the
+ * notification.
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $node
+ * @return void
+ */
+ public function serialize(DAV\Server $server, \DOMElement $node) {
+
+ switch($this->type) {
+ case self::TYPE_LOW :
+ $type = 'low';
+ break;
+ case self::TYPE_MEDIUM :
+ $type = 'medium';
+ break;
+ default :
+ case self::TYPE_HIGH :
+ $type = 'high';
+ break;
+ }
+
+ $prop = $node->ownerDocument->createElement('cs:systemstatus');
+ $prop->setAttribute('type', $type);
+
+ $node->appendChild($prop);
+
+ }
+
+ /**
+ * This method serializes the entire notification, as it is used in the
+ * response body.
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $node
+ * @return void
+ */
+ public function serializeBody(DAV\Server $server, \DOMElement $node) {
+
+ switch($this->type) {
+ case self::TYPE_LOW :
+ $type = 'low';
+ break;
+ case self::TYPE_MEDIUM :
+ $type = 'medium';
+ break;
+ default :
+ case self::TYPE_HIGH :
+ $type = 'high';
+ break;
+ }
+
+ $prop = $node->ownerDocument->createElement('cs:systemstatus');
+ $prop->setAttribute('type', $type);
+
+ if ($this->description) {
+ $text = $node->ownerDocument->createTextNode($this->description);
+ $desc = $node->ownerDocument->createElement('cs:description');
+ $desc->appendChild($text);
+ $prop->appendChild($desc);
+ }
+ if ($this->href) {
+ $text = $node->ownerDocument->createTextNode($this->href);
+ $href = $node->ownerDocument->createElement('d:href');
+ $href->appendChild($text);
+ $prop->appendChild($href);
+ }
+
+ $node->appendChild($prop);
+
+ }
+
+ /**
+ * Returns a unique id for this notification
+ *
+ * This is just the base url. This should generally be some kind of unique
+ * id.
+ *
+ * @return string
+ */
+ public function getId() {
+
+ return $this->id;
+
+ }
+
+ /*
+ * Returns the ETag for this notification.
+ *
+ * The ETag must be surrounded by literal double-quotes.
+ *
+ * @return string
+ */
+ public function getETag() {
+
+ return $this->etag;
+
+ }
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Plugin.php b/app/Gdoo/Calendar/Sabre/CalDAV/Plugin.php
new file mode 100644
index 00000000..2dba561f
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Plugin.php
@@ -0,0 +1,1338 @@
+imipHandler = $imipHandler;
+
+ }
+
+ /**
+ * Use this method to tell the server this plugin defines additional
+ * HTTP methods.
+ *
+ * This method is passed a uri. It should only return HTTP methods that are
+ * available for the specified uri.
+ *
+ * @param string $uri
+ * @return array
+ */
+ public function getHTTPMethods($uri) {
+
+ // The MKCALENDAR is only available on unmapped uri's, whose
+ // parents extend IExtendedCollection
+ list($parent, $name) = DAV\URLUtil::splitPath($uri);
+
+ $node = $this->server->tree->getNodeForPath($parent);
+
+ if ($node instanceof DAV\IExtendedCollection) {
+ try {
+ $node->getChild($name);
+ } catch (DAV\Exception\NotFound $e) {
+ return array('MKCALENDAR');
+ }
+ }
+ return array();
+
+ }
+
+ /**
+ * Returns a list of features for the DAV: HTTP header.
+ *
+ * @return array
+ */
+ public function getFeatures() {
+
+ return array('calendar-access', 'calendar-proxy');
+
+ }
+
+ /**
+ * Returns a plugin name.
+ *
+ * Using this name other plugins will be able to access other plugins
+ * using DAV\Server::getPlugin
+ *
+ * @return string
+ */
+ public function getPluginName() {
+
+ return 'caldav';
+
+ }
+
+ /**
+ * Returns a list of reports this plugin supports.
+ *
+ * This will be used in the {DAV:}supported-report-set property.
+ * Note that you still need to subscribe to the 'report' event to actually
+ * implement them
+ *
+ * @param string $uri
+ * @return array
+ */
+ public function getSupportedReportSet($uri) {
+
+ $node = $this->server->tree->getNodeForPath($uri);
+
+ $reports = array();
+ if ($node instanceof ICalendar || $node instanceof ICalendarObject) {
+ $reports[] = '{' . self::NS_CALDAV . '}calendar-multiget';
+ $reports[] = '{' . self::NS_CALDAV . '}calendar-query';
+ }
+ if ($node instanceof ICalendar) {
+ $reports[] = '{' . self::NS_CALDAV . '}free-busy-query';
+ }
+ return $reports;
+
+ }
+
+ /**
+ * Initializes the plugin
+ *
+ * @param DAV\Server $server
+ * @return void
+ */
+ public function initialize(DAV\Server $server) {
+
+ $this->server = $server;
+
+ $server->subscribeEvent('unknownMethod',array($this,'unknownMethod'));
+ //$server->subscribeEvent('unknownMethod',array($this,'unknownMethod2'),1000);
+ $server->subscribeEvent('report',array($this,'report'));
+ $server->subscribeEvent('beforeGetProperties',array($this,'beforeGetProperties'));
+ $server->subscribeEvent('onHTMLActionsPanel', array($this,'htmlActionsPanel'));
+ $server->subscribeEvent('onBrowserPostAction', array($this,'browserPostAction'));
+ $server->subscribeEvent('beforeWriteContent', array($this, 'beforeWriteContent'));
+ $server->subscribeEvent('beforeCreateFile', array($this, 'beforeCreateFile'));
+ $server->subscribeEvent('beforeMethod', array($this,'beforeMethod'));
+
+ $server->xmlNamespaces[self::NS_CALDAV] = 'cal';
+ $server->xmlNamespaces[self::NS_CALENDARSERVER] = 'cs';
+
+ $server->propertyMap['{' . self::NS_CALDAV . '}supported-calendar-component-set'] = 'Sabre\\CalDAV\\Property\\SupportedCalendarComponentSet';
+ $server->propertyMap['{' . self::NS_CALDAV . '}schedule-calendar-transp'] = 'Sabre\\CalDAV\\Property\\ScheduleCalendarTransp';
+
+ $server->resourceTypeMapping['\\Sabre\\CalDAV\\ICalendar'] = '{urn:ietf:params:xml:ns:caldav}calendar';
+ $server->resourceTypeMapping['\\Sabre\\CalDAV\\Schedule\\IOutbox'] = '{urn:ietf:params:xml:ns:caldav}schedule-outbox';
+ $server->resourceTypeMapping['\\Sabre\\CalDAV\\Principal\\IProxyRead'] = '{http://calendarserver.org/ns/}calendar-proxy-read';
+ $server->resourceTypeMapping['\\Sabre\\CalDAV\\Principal\\IProxyWrite'] = '{http://calendarserver.org/ns/}calendar-proxy-write';
+ $server->resourceTypeMapping['\\Sabre\\CalDAV\\Notifications\\ICollection'] = '{' . self::NS_CALENDARSERVER . '}notification';
+
+ array_push($server->protectedProperties,
+
+ '{' . self::NS_CALDAV . '}supported-calendar-component-set',
+ '{' . self::NS_CALDAV . '}supported-calendar-data',
+ '{' . self::NS_CALDAV . '}max-resource-size',
+ '{' . self::NS_CALDAV . '}min-date-time',
+ '{' . self::NS_CALDAV . '}max-date-time',
+ '{' . self::NS_CALDAV . '}max-instances',
+ '{' . self::NS_CALDAV . '}max-attendees-per-instance',
+ '{' . self::NS_CALDAV . '}calendar-home-set',
+ '{' . self::NS_CALDAV . '}supported-collation-set',
+ '{' . self::NS_CALDAV . '}calendar-data',
+
+ // scheduling extension
+ '{' . self::NS_CALDAV . '}schedule-inbox-URL',
+ '{' . self::NS_CALDAV . '}schedule-outbox-URL',
+ '{' . self::NS_CALDAV . '}calendar-user-address-set',
+ '{' . self::NS_CALDAV . '}calendar-user-type',
+
+ // CalendarServer extensions
+ '{' . self::NS_CALENDARSERVER . '}getctag',
+ '{' . self::NS_CALENDARSERVER . '}calendar-proxy-read-for',
+ '{' . self::NS_CALENDARSERVER . '}calendar-proxy-write-for',
+ '{' . self::NS_CALENDARSERVER . '}notification-URL',
+ '{' . self::NS_CALENDARSERVER . '}notificationtype'
+
+ );
+ }
+
+ /**
+ * This function handles support for the MKCALENDAR method
+ *
+ * @param string $method
+ * @param string $uri
+ * @return bool
+ */
+ public function unknownMethod($method, $uri) {
+
+ switch ($method) {
+ case 'MKCALENDAR' :
+ $this->httpMkCalendar($uri);
+ // false is returned to stop the propagation of the
+ // unknownMethod event.
+ return false;
+ case 'POST' :
+
+ // Checking if this is a text/calendar content type
+ $contentType = $this->server->httpRequest->getHeader('Content-Type');
+ if (strpos($contentType, 'text/calendar')!==0) {
+ return;
+ }
+
+ // Checking if we're talking to an outbox
+ try {
+ $node = $this->server->tree->getNodeForPath($uri);
+ } catch (DAV\Exception\NotFound $e) {
+ return;
+ }
+ if (!$node instanceof Schedule\IOutbox)
+ return;
+
+ $this->outboxRequest($node, $uri);
+ return false;
+
+ }
+
+ }
+
+ /**
+ * This functions handles REPORT requests specific to CalDAV
+ *
+ * @param string $reportName
+ * @param \DOMNode $dom
+ * @return bool
+ */
+ public function report($reportName,$dom) {
+
+ switch($reportName) {
+ case '{'.self::NS_CALDAV.'}calendar-multiget' :
+ $this->calendarMultiGetReport($dom);
+ return false;
+ case '{'.self::NS_CALDAV.'}calendar-query' :
+ $this->calendarQueryReport($dom);
+ return false;
+ case '{'.self::NS_CALDAV.'}free-busy-query' :
+ $this->freeBusyQueryReport($dom);
+ return false;
+
+ }
+
+
+ }
+
+ /**
+ * This function handles the MKCALENDAR HTTP method, which creates
+ * a new calendar.
+ *
+ * @param string $uri
+ * @return void
+ */
+ public function httpMkCalendar($uri) {
+
+ // Due to unforgivable bugs in iCal, we're completely disabling MKCALENDAR support
+ // for clients matching iCal in the user agent
+ //$ua = $this->server->httpRequest->getHeader('User-Agent');
+ //if (strpos($ua,'iCal/')!==false) {
+ // throw new \Sabre\DAV\Exception\Forbidden('iCal has major bugs in it\'s RFC3744 support. Therefore we are left with no other choice but disabling this feature.');
+ //}
+
+ $body = $this->server->httpRequest->getBody(true);
+ $properties = array();
+
+ if ($body) {
+
+ $dom = DAV\XMLUtil::loadDOMDocument($body);
+
+ foreach($dom->firstChild->childNodes as $child) {
+
+ if (DAV\XMLUtil::toClarkNotation($child)!=='{DAV:}set') continue;
+ foreach(DAV\XMLUtil::parseProperties($child,$this->server->propertyMap) as $k=>$prop) {
+ $properties[$k] = $prop;
+ }
+
+ }
+ }
+
+ $resourceType = array('{DAV:}collection','{urn:ietf:params:xml:ns:caldav}calendar');
+
+ $this->server->createCollection($uri,$resourceType,$properties);
+
+ $this->server->httpResponse->sendStatus(201);
+ $this->server->httpResponse->setHeader('Content-Length',0);
+ }
+
+ /**
+ * beforeGetProperties
+ *
+ * This method handler is invoked before any after properties for a
+ * resource are fetched. This allows us to add in any CalDAV specific
+ * properties.
+ *
+ * @param string $path
+ * @param DAV\INode $node
+ * @param array $requestedProperties
+ * @param array $returnedProperties
+ * @return void
+ */
+ public function beforeGetProperties($path, DAV\INode $node, &$requestedProperties, &$returnedProperties) {
+
+ if ($node instanceof DAVACL\IPrincipal) {
+
+ // calendar-home-set property
+ $calHome = '{' . self::NS_CALDAV . '}calendar-home-set';
+ if (in_array($calHome,$requestedProperties)) {
+ $principalId = $node->getName();
+ $calendarHomePath = self::CALENDAR_ROOT . '/' . $principalId . '/';
+
+ unset($requestedProperties[array_search($calHome, $requestedProperties)]);
+ $returnedProperties[200][$calHome] = new DAV\Property\Href($calendarHomePath);
+
+ }
+
+ // schedule-outbox-URL property
+ $scheduleProp = '{' . self::NS_CALDAV . '}schedule-outbox-URL';
+ if (in_array($scheduleProp,$requestedProperties)) {
+ $principalId = $node->getName();
+ $outboxPath = self::CALENDAR_ROOT . '/' . $principalId . '/outbox';
+
+ unset($requestedProperties[array_search($scheduleProp, $requestedProperties)]);
+ $returnedProperties[200][$scheduleProp] = new DAV\Property\Href($outboxPath);
+
+ }
+
+ // calendar-user-address-set property
+ $calProp = '{' . self::NS_CALDAV . '}calendar-user-address-set';
+ if (in_array($calProp,$requestedProperties)) {
+
+ $addresses = $node->getAlternateUriSet();
+ $addresses[] = $this->server->getBaseUri() . DAV\URLUtil::encodePath($node->getPrincipalUrl() . '/');
+ unset($requestedProperties[array_search($calProp, $requestedProperties)]);
+ $returnedProperties[200][$calProp] = new DAV\Property\HrefList($addresses, false);
+
+ }
+
+ // These two properties are shortcuts for ical to easily find
+ // other principals this principal has access to.
+ $propRead = '{' . self::NS_CALENDARSERVER . '}calendar-proxy-read-for';
+ $propWrite = '{' . self::NS_CALENDARSERVER . '}calendar-proxy-write-for';
+ if (in_array($propRead,$requestedProperties) || in_array($propWrite,$requestedProperties)) {
+
+ $aclPlugin = $this->server->getPlugin('acl');
+ $membership = $aclPlugin->getPrincipalMembership($path);
+ $readList = array();
+ $writeList = array();
+
+ foreach($membership as $group) {
+
+ $groupNode = $this->server->tree->getNodeForPath($group);
+
+ // If the node is either ap proxy-read or proxy-write
+ // group, we grab the parent principal and add it to the
+ // list.
+ if ($groupNode instanceof Principal\IProxyRead) {
+ list($readList[]) = DAV\URLUtil::splitPath($group);
+ }
+ if ($groupNode instanceof Principal\IProxyWrite) {
+ list($writeList[]) = DAV\URLUtil::splitPath($group);
+ }
+
+ }
+ if (in_array($propRead,$requestedProperties)) {
+ unset($requestedProperties[$propRead]);
+ $returnedProperties[200][$propRead] = new DAV\Property\HrefList($readList);
+ }
+ if (in_array($propWrite,$requestedProperties)) {
+ unset($requestedProperties[$propWrite]);
+ $returnedProperties[200][$propWrite] = new DAV\Property\HrefList($writeList);
+ }
+
+ }
+
+ // notification-URL property
+ $notificationUrl = '{' . self::NS_CALENDARSERVER . '}notification-URL';
+ if (($index = array_search($notificationUrl, $requestedProperties)) !== false) {
+ $principalId = $node->getName();
+ $calendarHomePath = 'calendars/' . $principalId . '/notifications/';
+ unset($requestedProperties[$index]);
+ $returnedProperties[200][$notificationUrl] = new DAV\Property\Href($calendarHomePath);
+ }
+
+ } // instanceof IPrincipal
+
+ if ($node instanceof Notifications\INode) {
+
+ $propertyName = '{' . self::NS_CALENDARSERVER . '}notificationtype';
+ if (($index = array_search($propertyName, $requestedProperties)) !== false) {
+
+ $returnedProperties[200][$propertyName] =
+ $node->getNotificationType();
+
+ unset($requestedProperties[$index]);
+
+ }
+
+ } // instanceof Notifications_INode
+
+
+ if ($node instanceof ICalendarObject) {
+ // The calendar-data property is not supposed to be a 'real'
+ // property, but in large chunks of the spec it does act as such.
+ // Therefore we simply expose it as a property.
+ $calDataProp = '{' . Plugin::NS_CALDAV . '}calendar-data';
+ if (in_array($calDataProp, $requestedProperties)) {
+ unset($requestedProperties[$calDataProp]);
+ $val = $node->get();
+ if (is_resource($val))
+ $val = stream_get_contents($val);
+
+ // Taking out \r to not screw up the xml output
+ $returnedProperties[200][$calDataProp] = str_replace("\r","", $val);
+
+ }
+ }
+
+ }
+
+ /**
+ * This function handles the calendar-multiget REPORT.
+ *
+ * This report is used by the client to fetch the content of a series
+ * of urls. Effectively avoiding a lot of redundant requests.
+ *
+ * @param \DOMNode $dom
+ * @return void
+ */
+ public function calendarMultiGetReport($dom) {
+
+ $properties = array_keys(DAV\XMLUtil::parseProperties($dom->firstChild));
+ $hrefElems = $dom->getElementsByTagNameNS('urn:DAV','href');
+
+ $xpath = new \DOMXPath($dom);
+ $xpath->registerNameSpace('cal',Plugin::NS_CALDAV);
+ $xpath->registerNameSpace('dav','urn:DAV');
+
+ $expand = $xpath->query('/cal:calendar-multiget/dav:prop/cal:calendar-data/cal:expand');
+ if ($expand->length>0) {
+ $expandElem = $expand->item(0);
+ $start = $expandElem->getAttribute('start');
+ $end = $expandElem->getAttribute('end');
+ if(!$start || !$end) {
+ throw new DAV\Exception\BadRequest('The "start" and "end" attributes are required for the CALDAV:expand element');
+ }
+ $start = VObject\DateTimeParser::parseDateTime($start);
+ $end = VObject\DateTimeParser::parseDateTime($end);
+
+ if ($end <= $start) {
+ throw new DAV\Exception\BadRequest('The end-date must be larger than the start-date in the expand element.');
+ }
+
+ $expand = true;
+
+ } else {
+
+ $expand = false;
+
+ }
+
+ foreach($hrefElems as $elem) {
+ $uri = $this->server->calculateUri($elem->nodeValue);
+ list($objProps) = $this->server->getPropertiesForPath($uri,$properties);
+
+ if ($expand && isset($objProps[200]['{' . self::NS_CALDAV . '}calendar-data'])) {
+ $vObject = VObject\Reader::read($objProps[200]['{' . self::NS_CALDAV . '}calendar-data']);
+ $vObject->expand($start, $end);
+ $objProps[200]['{' . self::NS_CALDAV . '}calendar-data'] = $vObject->serialize();
+ }
+
+ $propertyList[]=$objProps;
+
+ }
+
+ $prefer = $this->server->getHTTPPRefer();
+
+ $this->server->httpResponse->sendStatus(207);
+ $this->server->httpResponse->setHeader('Content-Type','application/xml; charset=utf-8');
+ $this->server->httpResponse->setHeader('Vary','Brief,Prefer');
+ $this->server->httpResponse->sendBody($this->server->generateMultiStatus($propertyList, $prefer['return-minimal']));
+
+ }
+
+ /**
+ * This function handles the calendar-query REPORT
+ *
+ * This report is used by clients to request calendar objects based on
+ * complex conditions.
+ *
+ * @param \DOMNode $dom
+ * @return void
+ */
+ public function calendarQueryReport($dom) {
+
+ $parser = new CalendarQueryParser($dom);
+ $parser->parse();
+
+ $node = $this->server->tree->getNodeForPath($this->server->getRequestUri());
+ $depth = $this->server->getHTTPDepth(0);
+
+ // The default result is an empty array
+ $result = array();
+
+ // The calendarobject was requested directly. In this case we handle
+ // this locally.
+ if ($depth == 0 && $node instanceof ICalendarObject) {
+
+ $requestedCalendarData = true;
+ $requestedProperties = $parser->requestedProperties;
+
+ if (!in_array('{urn:ietf:params:xml:ns:caldav}calendar-data', $requestedProperties)) {
+
+ // We always retrieve calendar-data, as we need it for filtering.
+ $requestedProperties[] = '{urn:ietf:params:xml:ns:caldav}calendar-data';
+
+ // If calendar-data wasn't explicitly requested, we need to remove
+ // it after processing.
+ $requestedCalendarData = false;
+ }
+
+ $properties = $this->server->getPropertiesForPath(
+ $this->server->getRequestUri(),
+ $requestedProperties,
+ 0
+ );
+
+ // This array should have only 1 element, the first calendar
+ // object.
+ $properties = current($properties);
+
+ // If there wasn't any calendar-data returned somehow, we ignore
+ // this.
+ if (isset($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data'])) {
+
+ $validator = new CalendarQueryValidator();
+
+ $vObject = VObject\Reader::read($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data']);
+ if ($validator->validate($vObject,$parser->filters)) {
+
+ // If the client didn't require the calendar-data property,
+ // we won't give it back.
+ if (!$requestedCalendarData) {
+ unset($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data']);
+ } else {
+ if ($parser->expand) {
+ $vObject->expand($parser->expand['start'], $parser->expand['end']);
+ $properties[200]['{' . self::NS_CALDAV . '}calendar-data'] = $vObject->serialize();
+ }
+ }
+
+ $result = array($properties);
+
+ }
+
+ }
+
+ }
+ // If we're dealing with a calendar, the calendar itself is responsible
+ // for the calendar-query.
+ if ($node instanceof ICalendar && $depth = 1) {
+
+ $nodePaths = $node->calendarQuery($parser->filters);
+
+ foreach($nodePaths as $path) {
+
+ list($properties) =
+ $this->server->getPropertiesForPath($this->server->getRequestUri() . '/' . $path, $parser->requestedProperties);
+
+ if ($parser->expand) {
+ // We need to do some post-processing
+ $vObject = VObject\Reader::read($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data']);
+ $vObject->expand($parser->expand['start'], $parser->expand['end']);
+ $properties[200]['{' . self::NS_CALDAV . '}calendar-data'] = $vObject->serialize();
+ }
+
+ $result[] = $properties;
+
+ }
+
+ }
+
+ $prefer = $this->server->getHTTPPRefer();
+
+ $this->server->httpResponse->sendStatus(207);
+ $this->server->httpResponse->setHeader('Content-Type','application/xml; charset=utf-8');
+ $this->server->httpResponse->setHeader('Vary','Brief,Prefer');
+ $this->server->httpResponse->sendBody($this->server->generateMultiStatus($result, $prefer['return-minimal']));
+
+ }
+
+ /**
+ * This method is responsible for parsing the request and generating the
+ * response for the CALDAV:free-busy-query REPORT.
+ *
+ * @param \DOMNode $dom
+ * @return void
+ */
+ protected function freeBusyQueryReport(\DOMNode $dom) {
+
+ $start = null;
+ $end = null;
+
+ foreach($dom->firstChild->childNodes as $childNode) {
+
+ $clark = DAV\XMLUtil::toClarkNotation($childNode);
+ if ($clark == '{' . self::NS_CALDAV . '}time-range') {
+ $start = $childNode->getAttribute('start');
+ $end = $childNode->getAttribute('end');
+ break;
+ }
+
+ }
+ if ($start) {
+ $start = VObject\DateTimeParser::parseDateTime($start);
+ }
+ if ($end) {
+ $end = VObject\DateTimeParser::parseDateTime($end);
+ }
+
+ if (!$start && !$end) {
+ throw new DAV\Exception\BadRequest('The freebusy report must have a time-range filter');
+ }
+ $acl = $this->server->getPlugin('acl');
+
+ if (!$acl) {
+ throw new DAV\Exception('The ACL plugin must be loaded for free-busy queries to work');
+ }
+ $uri = $this->server->getRequestUri();
+ $acl->checkPrivileges($uri,'{' . self::NS_CALDAV . '}read-free-busy');
+
+ $calendar = $this->server->tree->getNodeForPath($uri);
+ if (!$calendar instanceof ICalendar) {
+ throw new DAV\Exception\NotImplemented('The free-busy-query REPORT is only implemented on calendars');
+ }
+
+ // Doing a calendar-query first, to make sure we get the most
+ // performance.
+ $urls = $calendar->calendarQuery(array(
+ 'name' => 'VCALENDAR',
+ 'comp-filters' => array(
+ array(
+ 'name' => 'VEVENT',
+ 'comp-filters' => array(),
+ 'prop-filters' => array(),
+ 'is-not-defined' => false,
+ 'time-range' => array(
+ 'start' => $start,
+ 'end' => $end,
+ ),
+ ),
+ ),
+ 'prop-filters' => array(),
+ 'is-not-defined' => false,
+ 'time-range' => null,
+ ));
+
+ $objects = array_map(function($url) use ($calendar) {
+ $obj = $calendar->getChild($url)->get();
+ return $obj;
+ }, $urls);
+
+ $generator = new VObject\FreeBusyGenerator();
+ $generator->setObjects($objects);
+ $generator->setTimeRange($start, $end);
+ $result = $generator->getResult();
+ $result = $result->serialize();
+
+ $this->server->httpResponse->sendStatus(200);
+ $this->server->httpResponse->setHeader('Content-Type', 'text/calendar');
+ $this->server->httpResponse->setHeader('Content-Length', strlen($result));
+ $this->server->httpResponse->sendBody($result);
+
+ }
+
+ /**
+ * This method is triggered before a file gets updated with new content.
+ *
+ * This plugin uses this method to ensure that CalDAV objects receive
+ * valid calendar data.
+ *
+ * @param string $path
+ * @param DAV\IFile $node
+ * @param resource $data
+ * @return void
+ */
+ public function beforeWriteContent($path, DAV\IFile $node, &$data) {
+
+ if (!$node instanceof ICalendarObject)
+ return;
+
+ $this->validateICalendar($data, $path);
+
+ }
+
+ /**
+ * This method is triggered before a new file is created.
+ *
+ * This plugin uses this method to ensure that newly created calendar
+ * objects contain valid calendar data.
+ *
+ * @param string $path
+ * @param resource $data
+ * @param DAV\ICollection $parentNode
+ * @return void
+ */
+ public function beforeCreateFile($path, &$data, DAV\ICollection $parentNode) {
+
+ if (!$parentNode instanceof Calendar)
+ return;
+
+ $this->validateICalendar($data, $path);
+
+ }
+
+ /**
+ * This event is triggered before any HTTP request is handled.
+ *
+ * We use this to intercept GET calls to notification nodes, and return the
+ * proper response.
+ *
+ * @param string $method
+ * @param string $path
+ * @return void
+ */
+ public function beforeMethod($method, $path) {
+
+ if ($method!=='GET') return;
+
+ try {
+ $node = $this->server->tree->getNodeForPath($path);
+ } catch (DAV\Exception\NotFound $e) {
+ return;
+ }
+
+ if (!$node instanceof Notifications\INode)
+ return;
+
+ if (!$this->server->checkPreconditions(true)) return false;
+ $dom = new \DOMDocument('1.0', 'UTF-8');
+
+ $dom->formatOutput = true;
+
+ $root = $dom->createElement('cs:notification');
+ foreach($this->server->xmlNamespaces as $namespace => $prefix) {
+ $root->setAttribute('xmlns:' . $prefix, $namespace);
+ }
+
+ $dom->appendChild($root);
+ $node->getNotificationType()->serializeBody($this->server, $root);
+
+ $this->server->httpResponse->setHeader('Content-Type','application/xml');
+ $this->server->httpResponse->setHeader('ETag',$node->getETag());
+ $this->server->httpResponse->sendStatus(200);
+ $this->server->httpResponse->sendBody($dom->saveXML());
+
+ return false;
+
+ }
+
+ /**
+ * Checks if the submitted iCalendar data is in fact, valid.
+ *
+ * An exception is thrown if it's not.
+ *
+ * @param resource|string $data
+ * @param string $path
+ * @return void
+ */
+ protected function validateICalendar(&$data, $path) {
+
+ // If it's a stream, we convert it to a string first.
+ if (is_resource($data)) {
+ $data = stream_get_contents($data);
+ }
+
+ // Converting the data to unicode, if needed.
+ $data = DAV\StringUtil::ensureUTF8($data);
+
+ try {
+
+ $vobj = VObject\Reader::read($data);
+
+ } catch (VObject\ParseException $e) {
+
+ throw new DAV\Exception\UnsupportedMediaType('This resource only supports valid iCalendar 2.0 data. Parse error: ' . $e->getMessage());
+
+ }
+
+ if ($vobj->name !== 'VCALENDAR') {
+ throw new DAV\Exception\UnsupportedMediaType('This collection can only support iCalendar objects.');
+ }
+
+ // Get the Supported Components for the target calendar
+ list($parentPath,$object) = DAV\URLUtil::splitPath($path);
+ $calendarProperties = $this->server->getProperties($parentPath,array('{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'));
+ $supportedComponents = $calendarProperties['{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set']->getValue();
+
+ $foundType = null;
+ $foundUID = null;
+ foreach($vobj->getComponents() as $component) {
+ switch($component->name) {
+ case 'VTIMEZONE' :
+ continue 2;
+ case 'VEVENT' :
+ case 'VTODO' :
+ case 'VJOURNAL' :
+ if (is_null($foundType)) {
+ $foundType = $component->name;
+ if (!in_array($foundType, $supportedComponents)) {
+ throw new Exception\InvalidComponentType('This calendar only supports ' . implode(', ', $supportedComponents) . '. We found a ' . $foundType);
+ }
+ if (!isset($component->UID)) {
+ throw new DAV\Exception\BadRequest('Every ' . $component->name . ' component must have an UID');
+ }
+ $foundUID = (string)$component->UID;
+ } else {
+ if ($foundType !== $component->name) {
+ throw new DAV\Exception\BadRequest('A calendar object must only contain 1 component. We found a ' . $component->name . ' as well as a ' . $foundType);
+ }
+ if ($foundUID !== (string)$component->UID) {
+ throw new DAV\Exception\BadRequest('Every ' . $component->name . ' in this object must have identical UIDs');
+ }
+ }
+ break;
+ default :
+ throw new DAV\Exception\BadRequest('You are not allowed to create components of type: ' . $component->name . ' here');
+
+ }
+ }
+ if (!$foundType)
+ throw new DAV\Exception\BadRequest('iCalendar object must contain at least 1 of VEVENT, VTODO or VJOURNAL');
+
+ }
+
+ /**
+ * This method handles POST requests to the schedule-outbox.
+ *
+ * Currently, two types of requests are support:
+ * * FREEBUSY requests from RFC 6638
+ * * Simple iTIP messages from draft-desruisseaux-caldav-sched-04
+ *
+ * The latter is from an expired early draft of the CalDAV scheduling
+ * extensions, but iCal depends on a feature from that spec, so we
+ * implement it.
+ *
+ * @param Schedule\IOutbox $outboxNode
+ * @param string $outboxUri
+ * @return void
+ */
+ public function outboxRequest(Schedule\IOutbox $outboxNode, $outboxUri) {
+
+ // Parsing the request body
+ try {
+ $vObject = VObject\Reader::read($this->server->httpRequest->getBody(true));
+ } catch (VObject\ParseException $e) {
+ throw new DAV\Exception\BadRequest('The request body must be a valid iCalendar object. Parse error: ' . $e->getMessage());
+ }
+
+ // The incoming iCalendar object must have a METHOD property, and a
+ // component. The combination of both determines what type of request
+ // this is.
+ $componentType = null;
+ foreach($vObject->getComponents() as $component) {
+ if ($component->name !== 'VTIMEZONE') {
+ $componentType = $component->name;
+ break;
+ }
+ }
+ if (is_null($componentType)) {
+ throw new DAV\Exception\BadRequest('We expected at least one VTODO, VJOURNAL, VFREEBUSY or VEVENT component');
+ }
+
+ // Validating the METHOD
+ $method = strtoupper((string)$vObject->METHOD);
+ if (!$method) {
+ throw new DAV\Exception\BadRequest('A METHOD property must be specified in iTIP messages');
+ }
+
+ // So we support two types of requests:
+ //
+ // REQUEST with a VFREEBUSY component
+ // REQUEST, REPLY, ADD, CANCEL on VEVENT components
+
+ $acl = $this->server->getPlugin('acl');
+
+ if ($componentType === 'VFREEBUSY' && $method === 'REQUEST') {
+
+ $acl && $acl->checkPrivileges($outboxUri,'{' . Plugin::NS_CALDAV . '}schedule-query-freebusy');
+ $this->handleFreeBusyRequest($outboxNode, $vObject);
+
+ } elseif ($componentType === 'VEVENT' && in_array($method, array('REQUEST','REPLY','ADD','CANCEL'))) {
+
+ $acl && $acl->checkPrivileges($outboxUri,'{' . Plugin::NS_CALDAV . '}schedule-post-vevent');
+ $this->handleEventNotification($outboxNode, $vObject);
+
+ } else {
+
+ throw new DAV\Exception\NotImplemented('SabreDAV supports only VFREEBUSY (REQUEST) and VEVENT (REQUEST, REPLY, ADD, CANCEL)');
+
+ }
+
+ }
+
+ /**
+ * This method handles the REQUEST, REPLY, ADD and CANCEL methods for
+ * VEVENT iTip messages.
+ *
+ * @return void
+ */
+ protected function handleEventNotification(Schedule\IOutbox $outboxNode, VObject\Component $vObject) {
+
+ $originator = $this->server->httpRequest->getHeader('Originator');
+ $recipients = $this->server->httpRequest->getHeader('Recipient');
+
+ if (!$originator) {
+ throw new DAV\Exception\BadRequest('The Originator: header must be specified when making POST requests');
+ }
+ if (!$recipients) {
+ throw new DAV\Exception\BadRequest('The Recipient: header must be specified when making POST requests');
+ }
+
+ $recipients = explode(',',$recipients);
+ foreach($recipients as $k=>$recipient) {
+
+ $recipient = trim($recipient);
+ if (!preg_match('/^mailto:(.*)@(.*)$/i', $recipient)) {
+ throw new DAV\Exception\BadRequest('Recipients must start with mailto: and must be valid email address');
+ }
+ $recipient = substr($recipient, 7);
+ $recipients[$k] = $recipient;
+ }
+
+ // We need to make sure that 'originator' matches one of the email
+ // addresses of the selected principal.
+ $principal = $outboxNode->getOwner();
+ $props = $this->server->getProperties($principal,array(
+ '{' . self::NS_CALDAV . '}calendar-user-address-set',
+ ));
+
+ $addresses = array();
+ if (isset($props['{' . self::NS_CALDAV . '}calendar-user-address-set'])) {
+ $addresses = $props['{' . self::NS_CALDAV . '}calendar-user-address-set']->getHrefs();
+ }
+
+ $found = false;
+ foreach($addresses as $address) {
+
+ // Trimming the / on both sides, just in case..
+ if (rtrim(strtolower($originator),'/') === rtrim(strtolower($address),'/')) {
+ $found = true;
+ break;
+ }
+
+ }
+
+ if (!$found) {
+ throw new DAV\Exception\Forbidden('The addresses specified in the Originator header did not match any addresses in the owners calendar-user-address-set header');
+ }
+
+ // If the Originator header was a url, and not a mailto: address..
+ // we're going to try to pull the mailto: from the vobject body.
+ if (strtolower(substr($originator,0,7)) !== 'mailto:') {
+ $originator = (string)$vObject->VEVENT->ORGANIZER;
+
+ }
+ if (strtolower(substr($originator,0,7)) !== 'mailto:') {
+ throw new DAV\Exception\Forbidden('Could not find mailto: address in both the Orignator header, and the ORGANIZER property in the VEVENT');
+ }
+ $originator = substr($originator,7);
+
+ $result = $this->iMIPMessage($originator, $recipients, $vObject, $principal);
+ $this->server->httpResponse->sendStatus(200);
+ $this->server->httpResponse->setHeader('Content-Type','application/xml');
+ $this->server->httpResponse->sendBody($this->generateScheduleResponse($result));
+
+ }
+
+ /**
+ * Sends an iMIP message by email.
+ *
+ * This method must return an array with status codes per recipient.
+ * This should look something like:
+ *
+ * array(
+ * 'user1@example.org' => '2.0;Success'
+ * )
+ *
+ * Formatting for this status code can be found at:
+ * https://tools.ietf.org/html/rfc5545#section-3.8.8.3
+ *
+ * A list of valid status codes can be found at:
+ * https://tools.ietf.org/html/rfc5546#section-3.6
+ *
+ * @param string $originator
+ * @param array $recipients
+ * @param VObject\Component $vObject
+ * @param string $principal Principal url
+ * @return array
+ */
+ protected function iMIPMessage($originator, array $recipients, VObject\Component $vObject, $principal) {
+
+ if (!$this->imipHandler) {
+ $resultStatus = '5.2;This server does not support this operation';
+ } else {
+ $this->imipHandler->sendMessage($originator, $recipients, $vObject, $principal);
+ $resultStatus = '2.0;Success';
+ }
+
+ $result = array();
+ foreach($recipients as $recipient) {
+ $result[$recipient] = $resultStatus;
+ }
+
+ return $result;
+
+ }
+
+ /**
+ * Generates a schedule-response XML body
+ *
+ * The recipients array is a key->value list, containing email addresses
+ * and iTip status codes. See the iMIPMessage method for a description of
+ * the value.
+ *
+ * @param array $recipients
+ * @return string
+ */
+ public function generateScheduleResponse(array $recipients) {
+
+ $dom = new \DOMDocument('1.0','utf-8');
+ $dom->formatOutput = true;
+ $xscheduleResponse = $dom->createElement('cal:schedule-response');
+ $dom->appendChild($xscheduleResponse);
+
+ foreach($this->server->xmlNamespaces as $namespace=>$prefix) {
+
+ $xscheduleResponse->setAttribute('xmlns:' . $prefix, $namespace);
+
+ }
+
+ foreach($recipients as $recipient=>$status) {
+ $xresponse = $dom->createElement('cal:response');
+
+ $xrecipient = $dom->createElement('cal:recipient');
+ $xrecipient->appendChild($dom->createTextNode($recipient));
+ $xresponse->appendChild($xrecipient);
+
+ $xrequestStatus = $dom->createElement('cal:request-status');
+ $xrequestStatus->appendChild($dom->createTextNode($status));
+ $xresponse->appendChild($xrequestStatus);
+
+ $xscheduleResponse->appendChild($xresponse);
+
+ }
+
+ return $dom->saveXML();
+
+ }
+
+ /**
+ * This method is responsible for parsing a free-busy query request and
+ * returning it's result.
+ *
+ * @param Schedule\IOutbox $outbox
+ * @param string $request
+ * @return string
+ */
+ protected function handleFreeBusyRequest(Schedule\IOutbox $outbox, VObject\Component $vObject) {
+
+ $vFreeBusy = $vObject->VFREEBUSY;
+ $organizer = $vFreeBusy->organizer;
+
+ $organizer = (string)$organizer;
+
+ // Validating if the organizer matches the owner of the inbox.
+ $owner = $outbox->getOwner();
+
+ $caldavNS = '{' . Plugin::NS_CALDAV . '}';
+
+ $uas = $caldavNS . 'calendar-user-address-set';
+ $props = $this->server->getProperties($owner,array($uas));
+
+ if (empty($props[$uas]) || !in_array($organizer, $props[$uas]->getHrefs())) {
+ throw new DAV\Exception\Forbidden('The organizer in the request did not match any of the addresses for the owner of this inbox');
+ }
+
+ if (!isset($vFreeBusy->ATTENDEE)) {
+ throw new DAV\Exception\BadRequest('You must at least specify 1 attendee');
+ }
+
+ $attendees = array();
+ foreach($vFreeBusy->ATTENDEE as $attendee) {
+ $attendees[]= (string)$attendee;
+ }
+
+
+ if (!isset($vFreeBusy->DTSTART) || !isset($vFreeBusy->DTEND)) {
+ throw new DAV\Exception\BadRequest('DTSTART and DTEND must both be specified');
+ }
+
+ $startRange = $vFreeBusy->DTSTART->getDateTime();
+ $endRange = $vFreeBusy->DTEND->getDateTime();
+
+ $results = array();
+ foreach($attendees as $attendee) {
+ $results[] = $this->getFreeBusyForEmail($attendee, $startRange, $endRange, $vObject);
+ }
+
+ $dom = new \DOMDocument('1.0','utf-8');
+ $dom->formatOutput = true;
+ $scheduleResponse = $dom->createElement('cal:schedule-response');
+ foreach($this->server->xmlNamespaces as $namespace=>$prefix) {
+
+ $scheduleResponse->setAttribute('xmlns:' . $prefix,$namespace);
+
+ }
+ $dom->appendChild($scheduleResponse);
+
+ foreach($results as $result) {
+ $response = $dom->createElement('cal:response');
+
+ $recipient = $dom->createElement('cal:recipient');
+ $recipientHref = $dom->createElement('d:href');
+
+ $recipientHref->appendChild($dom->createTextNode($result['href']));
+ $recipient->appendChild($recipientHref);
+ $response->appendChild($recipient);
+
+ $reqStatus = $dom->createElement('cal:request-status');
+ $reqStatus->appendChild($dom->createTextNode($result['request-status']));
+ $response->appendChild($reqStatus);
+
+ if (isset($result['calendar-data'])) {
+
+ $calendardata = $dom->createElement('cal:calendar-data');
+ $calendardata->appendChild($dom->createTextNode(str_replace("\r\n","\n",$result['calendar-data']->serialize())));
+ $response->appendChild($calendardata);
+
+ }
+ $scheduleResponse->appendChild($response);
+ }
+
+ $this->server->httpResponse->sendStatus(200);
+ $this->server->httpResponse->setHeader('Content-Type','application/xml');
+ $this->server->httpResponse->sendBody($dom->saveXML());
+
+ }
+
+ /**
+ * Returns free-busy information for a specific address. The returned
+ * data is an array containing the following properties:
+ *
+ * calendar-data : A VFREEBUSY VObject
+ * request-status : an iTip status code.
+ * href: The principal's email address, as requested
+ *
+ * The following request status codes may be returned:
+ * * 2.0;description
+ * * 3.7;description
+ *
+ * @param string $email address
+ * @param \DateTime $start
+ * @param \DateTime $end
+ * @param VObject\Component $request
+ * @return array
+ */
+ protected function getFreeBusyForEmail($email, \DateTime $start, \DateTime $end, VObject\Component $request) {
+
+ $caldavNS = '{' . Plugin::NS_CALDAV . '}';
+
+ $aclPlugin = $this->server->getPlugin('acl');
+ if (substr($email,0,7)==='mailto:') $email = substr($email,7);
+
+ $result = $aclPlugin->principalSearch(
+ array('{http://sabredav.org/ns}email-address' => $email),
+ array(
+ '{DAV:}principal-URL', $caldavNS . 'calendar-home-set',
+ '{http://sabredav.org/ns}email-address',
+ )
+ );
+
+ if (!count($result)) {
+ return array(
+ 'request-status' => '3.7;Could not find principal',
+ 'href' => 'mailto:' . $email,
+ );
+ }
+
+ if (!isset($result[0][200][$caldavNS . 'calendar-home-set'])) {
+ return array(
+ 'request-status' => '3.7;No calendar-home-set property found',
+ 'href' => 'mailto:' . $email,
+ );
+ }
+ $homeSet = $result[0][200][$caldavNS . 'calendar-home-set']->getHref();
+
+ // Grabbing the calendar list
+ $objects = array();
+ foreach($this->server->tree->getNodeForPath($homeSet)->getChildren() as $node) {
+ if (!$node instanceof ICalendar) {
+ continue;
+ }
+ $aclPlugin->checkPrivileges($homeSet . $node->getName() ,$caldavNS . 'read-free-busy');
+
+ // Getting the list of object uris within the time-range
+ $urls = $node->calendarQuery(array(
+ 'name' => 'VCALENDAR',
+ 'comp-filters' => array(
+ array(
+ 'name' => 'VEVENT',
+ 'comp-filters' => array(),
+ 'prop-filters' => array(),
+ 'is-not-defined' => false,
+ 'time-range' => array(
+ 'start' => $start,
+ 'end' => $end,
+ ),
+ ),
+ ),
+ 'prop-filters' => array(),
+ 'is-not-defined' => false,
+ 'time-range' => null,
+ ));
+
+ $calObjects = array_map(function($url) use ($node) {
+ $obj = $node->getChild($url)->get();
+ return $obj;
+ }, $urls);
+
+ $objects = array_merge($objects,$calObjects);
+
+ }
+
+ $vcalendar = new VObject\Component\VCalendar();
+ $vcalendar->VERSION = '2.0';
+ $vcalendar->METHOD = 'REPLY';
+ $vcalendar->CALSCALE = 'GREGORIAN';
+ $vcalendar->PRODID = '-//SabreDAV//SabreDAV ' . DAV\Version::VERSION . '//EN';
+
+ $generator = new VObject\FreeBusyGenerator();
+ $generator->setObjects($objects);
+ $generator->setTimeRange($start, $end);
+ $generator->setBaseObject($vcalendar);
+
+ $result = $generator->getResult();
+
+ $vcalendar->VFREEBUSY->ATTENDEE = 'mailto:' . $email;
+ $vcalendar->VFREEBUSY->UID = (string)$request->VFREEBUSY->UID;
+ $vcalendar->VFREEBUSY->ORGANIZER = clone $request->VFREEBUSY->ORGANIZER;
+
+ return array(
+ 'calendar-data' => $result,
+ 'request-status' => '2.0;Success',
+ 'href' => 'mailto:' . $email,
+ );
+ }
+
+ /**
+ * This method is used to generate HTML output for the
+ * DAV\Browser\Plugin. This allows us to generate an interface users
+ * can use to create new calendars.
+ *
+ * @param DAV\INode $node
+ * @param string $output
+ * @return bool
+ */
+ public function htmlActionsPanel(DAV\INode $node, &$output) {
+
+ if (!$node instanceof UserCalendars)
+ return;
+
+ $output.= '
+
';
+
+ return false;
+
+ }
+
+ /**
+ * This method allows us to intercept the 'mkcalendar' sabreAction. This
+ * action enables the user to create new calendars from the browser plugin.
+ *
+ * @param string $uri
+ * @param string $action
+ * @param array $postVars
+ * @return bool
+ */
+ public function browserPostAction($uri, $action, array $postVars) {
+
+ if ($action!=='mkcalendar')
+ return;
+
+ $resourceType = array('{DAV:}collection','{urn:ietf:params:xml:ns:caldav}calendar');
+ $properties = array();
+ if (isset($postVars['{DAV:}displayname'])) {
+ $properties['{DAV:}displayname'] = $postVars['{DAV:}displayname'];
+ }
+ $this->server->createCollection($uri . '/' . $postVars['name'],$resourceType,$properties);
+ return false;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Principal/Collection.php b/app/Gdoo/Calendar/Sabre/CalDAV/Principal/Collection.php
new file mode 100644
index 00000000..8de530bc
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Principal/Collection.php
@@ -0,0 +1,32 @@
+principalBackend, $principalInfo);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Principal/IProxyRead.php b/app/Gdoo/Calendar/Sabre/CalDAV/Principal/IProxyRead.php
new file mode 100644
index 00000000..7fabc00a
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Principal/IProxyRead.php
@@ -0,0 +1,19 @@
+principalInfo = $principalInfo;
+ $this->principalBackend = $principalBackend;
+
+ }
+
+ /**
+ * Returns this principals name.
+ *
+ * @return string
+ */
+ public function getName() {
+
+ return 'calendar-proxy-read';
+
+ }
+
+ /**
+ * Returns the last modification time
+ *
+ * @return null
+ */
+ public function getLastModified() {
+
+ return null;
+
+ }
+
+ /**
+ * Deletes the current node
+ *
+ * @throws DAV\Exception\Forbidden
+ * @return void
+ */
+ public function delete() {
+
+ throw new DAV\Exception\Forbidden('Permission denied to delete node');
+
+ }
+
+ /**
+ * Renames the node
+ *
+ * @throws DAV\Exception\Forbidden
+ * @param string $name The new name
+ * @return void
+ */
+ public function setName($name) {
+
+ throw new DAV\Exception\Forbidden('Permission denied to rename file');
+
+ }
+
+
+ /**
+ * Returns a list of alternative urls for a principal
+ *
+ * This can for example be an email address, or ldap url.
+ *
+ * @return array
+ */
+ public function getAlternateUriSet() {
+
+ return array();
+
+ }
+
+ /**
+ * Returns the full principal url
+ *
+ * @return string
+ */
+ public function getPrincipalUrl() {
+
+ return $this->principalInfo['uri'] . '/' . $this->getName();
+
+ }
+
+ /**
+ * Returns the list of group members
+ *
+ * If this principal is a group, this function should return
+ * all member principal uri's for the group.
+ *
+ * @return array
+ */
+ public function getGroupMemberSet() {
+
+ return $this->principalBackend->getGroupMemberSet($this->getPrincipalUrl());
+
+ }
+
+ /**
+ * Returns the list of groups this principal is member of
+ *
+ * If this principal is a member of a (list of) groups, this function
+ * should return a list of principal uri's for it's members.
+ *
+ * @return array
+ */
+ public function getGroupMembership() {
+
+ return $this->principalBackend->getGroupMembership($this->getPrincipalUrl());
+
+ }
+
+ /**
+ * Sets a list of group members
+ *
+ * If this principal is a group, this method sets all the group members.
+ * The list of members is always overwritten, never appended to.
+ *
+ * This method should throw an exception if the members could not be set.
+ *
+ * @param array $principals
+ * @return void
+ */
+ public function setGroupMemberSet(array $principals) {
+
+ $this->principalBackend->setGroupMemberSet($this->getPrincipalUrl(), $principals);
+
+ }
+
+ /**
+ * Returns the displayname
+ *
+ * This should be a human readable name for the principal.
+ * If none is available, return the nodename.
+ *
+ * @return string
+ */
+ public function getDisplayName() {
+
+ return $this->getName();
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Principal/ProxyWrite.php b/app/Gdoo/Calendar/Sabre/CalDAV/Principal/ProxyWrite.php
new file mode 100644
index 00000000..2a9c6630
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Principal/ProxyWrite.php
@@ -0,0 +1,180 @@
+principalInfo = $principalInfo;
+ $this->principalBackend = $principalBackend;
+
+ }
+
+ /**
+ * Returns this principals name.
+ *
+ * @return string
+ */
+ public function getName() {
+
+ return 'calendar-proxy-write';
+
+ }
+
+ /**
+ * Returns the last modification time
+ *
+ * @return null
+ */
+ public function getLastModified() {
+
+ return null;
+
+ }
+
+ /**
+ * Deletes the current node
+ *
+ * @throws DAV\Exception\Forbidden
+ * @return void
+ */
+ public function delete() {
+
+ throw new DAV\Exception\Forbidden('Permission denied to delete node');
+
+ }
+
+ /**
+ * Renames the node
+ *
+ * @throws DAV\Exception\Forbidden
+ * @param string $name The new name
+ * @return void
+ */
+ public function setName($name) {
+
+ throw new DAV\Exception\Forbidden('Permission denied to rename file');
+
+ }
+
+
+ /**
+ * Returns a list of alternative urls for a principal
+ *
+ * This can for example be an email address, or ldap url.
+ *
+ * @return array
+ */
+ public function getAlternateUriSet() {
+
+ return array();
+
+ }
+
+ /**
+ * Returns the full principal url
+ *
+ * @return string
+ */
+ public function getPrincipalUrl() {
+
+ return $this->principalInfo['uri'] . '/' . $this->getName();
+
+ }
+
+ /**
+ * Returns the list of group members
+ *
+ * If this principal is a group, this function should return
+ * all member principal uri's for the group.
+ *
+ * @return array
+ */
+ public function getGroupMemberSet() {
+
+ return $this->principalBackend->getGroupMemberSet($this->getPrincipalUrl());
+
+ }
+
+ /**
+ * Returns the list of groups this principal is member of
+ *
+ * If this principal is a member of a (list of) groups, this function
+ * should return a list of principal uri's for it's members.
+ *
+ * @return array
+ */
+ public function getGroupMembership() {
+
+ return $this->principalBackend->getGroupMembership($this->getPrincipalUrl());
+
+ }
+
+ /**
+ * Sets a list of group members
+ *
+ * If this principal is a group, this method sets all the group members.
+ * The list of members is always overwritten, never appended to.
+ *
+ * This method should throw an exception if the members could not be set.
+ *
+ * @param array $principals
+ * @return void
+ */
+ public function setGroupMemberSet(array $principals) {
+
+ $this->principalBackend->setGroupMemberSet($this->getPrincipalUrl(), $principals);
+
+ }
+
+ /**
+ * Returns the displayname
+ *
+ * This should be a human readable name for the principal.
+ * If none is available, return the nodename.
+ *
+ * @return string
+ */
+ public function getDisplayName() {
+
+ return $this->getName();
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Principal/User.php b/app/Gdoo/Calendar/Sabre/CalDAV/Principal/User.php
new file mode 100644
index 00000000..cdb04dd2
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Principal/User.php
@@ -0,0 +1,134 @@
+principalBackend->getPrincipalByPath($this->getPrincipalURL() . '/' . $name);
+ if (!$principal) {
+ throw new DAV\Exception\NotFound('Node with name ' . $name . ' was not found');
+ }
+ if ($name === 'calendar-proxy-read')
+ return new ProxyRead($this->principalBackend, $this->principalProperties);
+
+ if ($name === 'calendar-proxy-write')
+ return new ProxyWrite($this->principalBackend, $this->principalProperties);
+
+ throw new DAV\Exception\NotFound('Node with name ' . $name . ' was not found');
+
+ }
+
+ /**
+ * Returns an array with all the child nodes
+ *
+ * @return DAV\INode[]
+ */
+ public function getChildren() {
+
+ $r = array();
+ if ($this->principalBackend->getPrincipalByPath($this->getPrincipalURL() . '/calendar-proxy-read')) {
+ $r[] = new ProxyRead($this->principalBackend, $this->principalProperties);
+ }
+ if ($this->principalBackend->getPrincipalByPath($this->getPrincipalURL() . '/calendar-proxy-write')) {
+ $r[] = new ProxyWrite($this->principalBackend, $this->principalProperties);
+ }
+
+ return $r;
+
+ }
+
+ /**
+ * Returns whether or not the child node exists
+ *
+ * @param string $name
+ * @return bool
+ */
+ public function childExists($name) {
+
+ try {
+ $this->getChild($name);
+ return true;
+ } catch (DAV\Exception\NotFound $e) {
+ return false;
+ }
+
+ }
+
+ /**
+ * Returns a list of ACE's for this node.
+ *
+ * Each ACE has the following properties:
+ * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
+ * currently the only supported privileges
+ * * 'principal', a url to the principal who owns the node
+ * * 'protected' (optional), indicating that this ACE is not allowed to
+ * be updated.
+ *
+ * @return array
+ */
+ public function getACL() {
+
+ $acl = parent::getACL();
+ $acl[] = array(
+ 'privilege' => '{DAV:}read',
+ 'principal' => $this->principalProperties['uri'] . '/calendar-proxy-read',
+ 'protected' => true,
+ );
+ $acl[] = array(
+ 'privilege' => '{DAV:}read',
+ 'principal' => $this->principalProperties['uri'] . '/calendar-proxy-write',
+ 'protected' => true,
+ );
+ return $acl;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Property/AllowedSharingModes.php b/app/Gdoo/Calendar/Sabre/CalDAV/Property/AllowedSharingModes.php
new file mode 100644
index 00000000..08bf2448
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Property/AllowedSharingModes.php
@@ -0,0 +1,74 @@
+canBeShared = $canBeShared;
+ $this->canBePublished = $canBePublished;
+
+ }
+
+ /**
+ * Serializes the property in a DOMDocument
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $node
+ * @return void
+ */
+ public function serialize(DAV\Server $server, \DOMElement $node) {
+
+ $doc = $node->ownerDocument;
+ if ($this->canBeShared) {
+ $xcomp = $doc->createElement('cs:can-be-shared');
+ $node->appendChild($xcomp);
+ }
+ if ($this->canBePublished) {
+ $xcomp = $doc->createElement('cs:can-be-published');
+ $node->appendChild($xcomp);
+ }
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Property/Invite.php b/app/Gdoo/Calendar/Sabre/CalDAV/Property/Invite.php
new file mode 100644
index 00000000..7b91c335
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Property/Invite.php
@@ -0,0 +1,227 @@
+users = $users;
+ $this->organizer = $organizer;
+
+ }
+
+ /**
+ * Returns the list of users, as it was passed to the constructor.
+ *
+ * @return array
+ */
+ public function getValue() {
+
+ return $this->users;
+
+ }
+
+ /**
+ * Serializes the property in a DOMDocument
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $node
+ * @return void
+ */
+ public function serialize(DAV\Server $server,\DOMElement $node) {
+
+ $doc = $node->ownerDocument;
+
+ if (!is_null($this->organizer)) {
+
+ $xorganizer = $doc->createElement('cs:organizer');
+
+ $href = $doc->createElement('d:href');
+ $href->appendChild($doc->createTextNode($this->organizer['href']));
+ $xorganizer->appendChild($href);
+
+ if (isset($this->organizer['commonName']) && $this->organizer['commonName']) {
+ $commonName = $doc->createElement('cs:common-name');
+ $commonName->appendChild($doc->createTextNode($this->organizer['commonName']));
+ $xorganizer->appendChild($commonName);
+ }
+ if (isset($this->organizer['firstName']) && $this->organizer['firstName']) {
+ $firstName = $doc->createElement('cs:first-name');
+ $firstName->appendChild($doc->createTextNode($this->organizer['firstName']));
+ $xorganizer->appendChild($firstName);
+ }
+ if (isset($this->organizer['lastName']) && $this->organizer['lastName']) {
+ $lastName = $doc->createElement('cs:last-name');
+ $lastName->appendChild($doc->createTextNode($this->organizer['lastName']));
+ $xorganizer->appendChild($lastName);
+ }
+
+ $node->appendChild($xorganizer);
+
+
+ }
+
+ foreach($this->users as $user) {
+
+ $xuser = $doc->createElement('cs:user');
+
+ $href = $doc->createElement('d:href');
+ $href->appendChild($doc->createTextNode($user['href']));
+ $xuser->appendChild($href);
+
+ if (isset($user['commonName']) && $user['commonName']) {
+ $commonName = $doc->createElement('cs:common-name');
+ $commonName->appendChild($doc->createTextNode($user['commonName']));
+ $xuser->appendChild($commonName);
+ }
+
+ switch($user['status']) {
+
+ case SharingPlugin::STATUS_ACCEPTED :
+ $status = $doc->createElement('cs:invite-accepted');
+ $xuser->appendChild($status);
+ break;
+ case SharingPlugin::STATUS_DECLINED :
+ $status = $doc->createElement('cs:invite-declined');
+ $xuser->appendChild($status);
+ break;
+ case SharingPlugin::STATUS_NORESPONSE :
+ $status = $doc->createElement('cs:invite-noresponse');
+ $xuser->appendChild($status);
+ break;
+ case SharingPlugin::STATUS_INVALID :
+ $status = $doc->createElement('cs:invite-invalid');
+ $xuser->appendChild($status);
+ break;
+
+ }
+
+ $xaccess = $doc->createElement('cs:access');
+
+ if ($user['readOnly']) {
+ $xaccess->appendChild(
+ $doc->createElement('cs:read')
+ );
+ } else {
+ $xaccess->appendChild(
+ $doc->createElement('cs:read-write')
+ );
+ }
+ $xuser->appendChild($xaccess);
+
+ if (isset($user['summary']) && $user['summary']) {
+ $summary = $doc->createElement('cs:summary');
+ $summary->appendChild($doc->createTextNode($user['summary']));
+ $xuser->appendChild($summary);
+ }
+
+ $node->appendChild($xuser);
+
+ }
+
+
+ }
+
+ /**
+ * Unserializes the property.
+ *
+ * This static method should return a an instance of this object.
+ *
+ * @param \DOMElement $prop
+ * @return DAV\IProperty
+ */
+ static function unserialize(\DOMElement $prop) {
+
+ $xpath = new \DOMXPath($prop->ownerDocument);
+ $xpath->registerNamespace('cs', CalDAV\Plugin::NS_CALENDARSERVER);
+ $xpath->registerNamespace('d', 'urn:DAV');
+
+ $users = array();
+
+ foreach($xpath->query('cs:user', $prop) as $user) {
+
+ $status = null;
+ if ($xpath->evaluate('boolean(cs:invite-accepted)', $user)) {
+ $status = SharingPlugin::STATUS_ACCEPTED;
+ } elseif ($xpath->evaluate('boolean(cs:invite-declined)', $user)) {
+ $status = SharingPlugin::STATUS_DECLINED;
+ } elseif ($xpath->evaluate('boolean(cs:invite-noresponse)', $user)) {
+ $status = SharingPlugin::STATUS_NORESPONSE;
+ } elseif ($xpath->evaluate('boolean(cs:invite-invalid)', $user)) {
+ $status = SharingPlugin::STATUS_INVALID;
+ } else {
+ throw new DAV\Exception('Every cs:user property must have one of cs:invite-accepted, cs:invite-declined, cs:invite-noresponse or cs:invite-invalid');
+ }
+ $users[] = array(
+ 'href' => $xpath->evaluate('string(d:href)', $user),
+ 'commonName' => $xpath->evaluate('string(cs:common-name)', $user),
+ 'readOnly' => $xpath->evaluate('boolean(cs:access/cs:read)', $user),
+ 'summary' => $xpath->evaluate('string(cs:summary)', $user),
+ 'status' => $status,
+ );
+
+ }
+
+ return new self($users);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Property/ScheduleCalendarTransp.php b/app/Gdoo/Calendar/Sabre/CalDAV/Property/ScheduleCalendarTransp.php
new file mode 100644
index 00000000..00f54174
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Property/ScheduleCalendarTransp.php
@@ -0,0 +1,102 @@
+value = $value;
+
+ }
+
+ /**
+ * Returns the current value
+ *
+ * @return string
+ */
+ public function getValue() {
+
+ return $this->value;
+
+ }
+
+ /**
+ * Serializes the property in a DOMDocument
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $node
+ * @return void
+ */
+ public function serialize(DAV\Server $server,\DOMElement $node) {
+
+ $doc = $node->ownerDocument;
+ switch($this->value) {
+ case self::TRANSPARENT :
+ $xval = $doc->createElement('cal:transparent');
+ break;
+ case self::OPAQUE :
+ $xval = $doc->createElement('cal:opaque');
+ break;
+ }
+
+ $node->appendChild($xval);
+
+ }
+
+ /**
+ * Unserializes the DOMElement back into a Property class.
+ *
+ * @param \DOMElement $node
+ * @return ScheduleCalendarTransp
+ */
+ static function unserialize(\DOMElement $node) {
+
+ $value = null;
+ foreach($node->childNodes as $childNode) {
+ switch(DAV\XMLUtil::toClarkNotation($childNode)) {
+ case '{' . CalDAV\Plugin::NS_CALDAV . '}opaque' :
+ $value = self::OPAQUE;
+ break;
+ case '{' . CalDAV\Plugin::NS_CALDAV . '}transparent' :
+ $value = self::TRANSPARENT;
+ break;
+ }
+ }
+ if (is_null($value))
+ return null;
+
+ return new self($value);
+
+ }
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Property/SupportedCalendarComponentSet.php b/app/Gdoo/Calendar/Sabre/CalDAV/Property/SupportedCalendarComponentSet.php
new file mode 100644
index 00000000..d526bab1
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Property/SupportedCalendarComponentSet.php
@@ -0,0 +1,88 @@
+components = $components;
+
+ }
+
+ /**
+ * Returns the list of supported components
+ *
+ * @return array
+ */
+ public function getValue() {
+
+ return $this->components;
+
+ }
+
+ /**
+ * Serializes the property in a DOMDocument
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $node
+ * @return void
+ */
+ public function serialize(DAV\Server $server,\DOMElement $node) {
+
+ $doc = $node->ownerDocument;
+ foreach($this->components as $component) {
+
+ $xcomp = $doc->createElement('cal:comp');
+ $xcomp->setAttribute('name',$component);
+ $node->appendChild($xcomp);
+
+ }
+
+ }
+
+ /**
+ * Unserializes the DOMElement back into a Property class.
+ *
+ * @param \DOMElement $node
+ * @return Property_SupportedCalendarComponentSet
+ */
+ static function unserialize(\DOMElement $node) {
+
+ $components = array();
+ foreach($node->childNodes as $childNode) {
+ if (DAV\XMLUtil::toClarkNotation($childNode)==='{' . CalDAV\Plugin::NS_CALDAV . '}comp') {
+ $components[] = $childNode->getAttribute('name');
+ }
+ }
+ return new self($components);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Property/SupportedCalendarData.php b/app/Gdoo/Calendar/Sabre/CalDAV/Property/SupportedCalendarData.php
new file mode 100644
index 00000000..5c105f88
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Property/SupportedCalendarData.php
@@ -0,0 +1,40 @@
+ownerDocument;
+
+ $prefix = isset($server->xmlNamespaces[Plugin::NS_CALDAV])?$server->xmlNamespaces[Plugin::NS_CALDAV]:'cal';
+
+ $caldata = $doc->createElement($prefix . ':calendar-data');
+ $caldata->setAttribute('content-type','text/calendar');
+ $caldata->setAttribute('version','2.0');
+
+ $node->appendChild($caldata);
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Property/SupportedCollationSet.php b/app/Gdoo/Calendar/Sabre/CalDAV/Property/SupportedCollationSet.php
new file mode 100644
index 00000000..b05e376c
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Property/SupportedCollationSet.php
@@ -0,0 +1,45 @@
+ownerDocument;
+
+ $prefix = $node->lookupPrefix('urn:ietf:params:xml:ns:caldav');
+ if (!$prefix) $prefix = 'cal';
+
+ $node->appendChild(
+ $doc->createElement($prefix . ':supported-collation','i;ascii-casemap')
+ );
+ $node->appendChild(
+ $doc->createElement($prefix . ':supported-collation','i;octet')
+ );
+ $node->appendChild(
+ $doc->createElement($prefix . ':supported-collation','i;unicode-casemap')
+ );
+
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Schedule/IMip.php b/app/Gdoo/Calendar/Sabre/CalDAV/Schedule/IMip.php
new file mode 100644
index 00000000..8be0f5c8
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Schedule/IMip.php
@@ -0,0 +1,111 @@
+senderEmail = $senderEmail;
+
+ }
+
+ /**
+ * Sends one or more iTip messages through email.
+ *
+ * @param string $originator Originator Email
+ * @param array $recipients Array of email addresses
+ * @param VObject\Component $vObject
+ * @param string $principal Principal Url of the originator
+ * @return void
+ */
+ public function sendMessage($originator, array $recipients, VObject\Component $vObject, $principal) {
+
+ foreach($recipients as $recipient) {
+
+ $to = $recipient;
+ $replyTo = $originator;
+ $subject = 'SabreDAV iTIP message';
+
+ switch(strtoupper($vObject->METHOD)) {
+ case 'REPLY' :
+ $subject = 'Response for: ' . $vObject->VEVENT->SUMMARY;
+ break;
+ case 'REQUEST' :
+ $subject = 'Invitation for: ' .$vObject->VEVENT->SUMMARY;
+ break;
+ case 'CANCEL' :
+ $subject = 'Cancelled event: ' . $vObject->VEVENT->SUMMARY;
+ break;
+ }
+
+ $headers = array();
+ $headers[] = 'Reply-To: ' . $replyTo;
+ $headers[] = 'From: ' . $this->senderEmail;
+ $headers[] = 'Content-Type: text/calendar; method=' . (string)$vObject->method . '; charset=utf-8';
+ if (DAV\Server::$exposeVersion) {
+ $headers[] = 'X-Sabre-Version: ' . DAV\Version::VERSION . '-' . DAV\Version::STABILITY;
+ }
+
+ $vcalBody = $vObject->serialize();
+
+ $this->mail($to, $subject, $vcalBody, $headers);
+
+ }
+
+ }
+
+ // @codeCoverageIgnoreStart
+ // This is deemed untestable in a reasonable manner
+
+ /**
+ * This function is reponsible for sending the actual email.
+ *
+ * @param string $to Recipient email address
+ * @param string $subject Subject of the email
+ * @param string $body iCalendar body
+ * @param array $headers List of headers
+ * @return void
+ */
+ protected function mail($to, $subject, $body, array $headers) {
+
+
+ mail($to, $subject, $body, implode("\r\n", $headers));
+
+ }
+
+ // @codeCoverageIgnoreEnd
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Schedule/IOutbox.php b/app/Gdoo/Calendar/Sabre/CalDAV/Schedule/IOutbox.php
new file mode 100644
index 00000000..d8ce1729
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Schedule/IOutbox.php
@@ -0,0 +1,16 @@
+principalUri = $principalUri;
+
+ }
+
+ /**
+ * Returns the name of the node.
+ *
+ * This is used to generate the url.
+ *
+ * @return string
+ */
+ public function getName() {
+
+ return 'outbox';
+
+ }
+
+ /**
+ * Returns an array with all the child nodes
+ *
+ * @return \Sabre\DAV\INode[]
+ */
+ public function getChildren() {
+
+ return array();
+
+ }
+
+ /**
+ * Returns the owner principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getOwner() {
+
+ return $this->principalUri;
+
+ }
+
+ /**
+ * Returns a group principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getGroup() {
+
+ return null;
+
+ }
+
+ /**
+ * Returns a list of ACE's for this node.
+ *
+ * Each ACE has the following properties:
+ * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
+ * currently the only supported privileges
+ * * 'principal', a url to the principal who owns the node
+ * * 'protected' (optional), indicating that this ACE is not allowed to
+ * be updated.
+ *
+ * @return array
+ */
+ public function getACL() {
+
+ return array(
+ array(
+ 'privilege' => '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-query-freebusy',
+ 'principal' => $this->getOwner(),
+ 'protected' => true,
+ ),
+ array(
+ 'privilege' => '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-post-vevent',
+ 'principal' => $this->getOwner(),
+ 'protected' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}read',
+ 'principal' => $this->getOwner(),
+ 'protected' => true,
+ ),
+ );
+
+ }
+
+ /**
+ * Updates the ACL
+ *
+ * This method will receive a list of new ACE's.
+ *
+ * @param array $acl
+ * @return void
+ */
+ public function setACL(array $acl) {
+
+ throw new DAV\Exception\MethodNotAllowed('You\'re not allowed to update the ACL');
+
+ }
+
+ /**
+ * Returns the list of supported privileges for this node.
+ *
+ * The returned data structure is a list of nested privileges.
+ * See Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple
+ * standard structure.
+ *
+ * If null is returned from this method, the default privilege set is used,
+ * which is fine for most common usecases.
+ *
+ * @return array|null
+ */
+ public function getSupportedPrivilegeSet() {
+
+ $default = DAVACL\Plugin::getDefaultSupportedPrivilegeSet();
+ $default['aggregates'][] = array(
+ 'privilege' => '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-query-freebusy',
+ );
+ $default['aggregates'][] = array(
+ 'privilege' => '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-post-vevent',
+ );
+
+ return $default;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/ShareableCalendar.php b/app/Gdoo/Calendar/Sabre/CalDAV/ShareableCalendar.php
new file mode 100644
index 00000000..51ae8082
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/ShareableCalendar.php
@@ -0,0 +1,72 @@
+caldavBackend->updateShares($this->calendarInfo['id'], $add, $remove);
+
+ }
+
+ /**
+ * Returns the list of people whom this calendar is shared with.
+ *
+ * Every element in this array should have the following properties:
+ * * href - Often a mailto: address
+ * * commonName - Optional, for example a first + last name
+ * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
+ * * readOnly - boolean
+ * * summary - Optional, a description for the share
+ *
+ * @return array
+ */
+ public function getShares() {
+
+ return $this->caldavBackend->getShares($this->calendarInfo['id']);
+
+ }
+
+ /**
+ * Marks this calendar as published.
+ *
+ * Publishing a calendar should automatically create a read-only, public,
+ * subscribable calendar.
+ *
+ * @param bool $value
+ * @return void
+ */
+ public function setPublishStatus($value) {
+
+ $this->caldavBackend->setPublishStatus($this->calendarInfo['id'], $value);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/SharedCalendar.php b/app/Gdoo/Calendar/Sabre/CalDAV/SharedCalendar.php
new file mode 100644
index 00000000..021d7d23
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/SharedCalendar.php
@@ -0,0 +1,116 @@
+calendarInfo['{http://calendarserver.org/ns/}shared-url'];
+
+ }
+
+ /**
+ * Returns the owner principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getOwner() {
+
+ return $this->calendarInfo['{http://sabredav.org/ns}owner-principal'];
+
+ }
+
+ /**
+ * Returns a list of ACE's for this node.
+ *
+ * Each ACE has the following properties:
+ * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
+ * currently the only supported privileges
+ * * 'principal', a url to the principal who owns the node
+ * * 'protected' (optional), indicating that this ACE is not allowed to
+ * be updated.
+ *
+ * @return array
+ */
+ public function getACL() {
+
+ // The top-level ACL only contains access information for the true
+ // owner of the calendar, so we need to add the information for the
+ // sharee.
+ $acl = parent::getACL();
+ $acl[] = array(
+ 'privilege' => '{DAV:}read',
+ 'principal' => $this->calendarInfo['principaluri'],
+ 'protected' => true,
+ );
+ if (!$this->calendarInfo['{http://sabredav.org/ns}read-only']) {
+ $acl[] = array(
+ 'privilege' => '{DAV:}write',
+ 'principal' => $this->calendarInfo['principaluri'],
+ 'protected' => true,
+ );
+ }
+ return $acl;
+
+ }
+
+ /**
+ * Returns the list of people whom this calendar is shared with.
+ *
+ * Every element in this array should have the following properties:
+ * * href - Often a mailto: address
+ * * commonName - Optional, for example a first + last name
+ * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
+ * * readOnly - boolean
+ * * summary - Optional, a description for the share
+ *
+ * @return array
+ */
+ public function getShares() {
+
+ return $this->caldavBackend->getShares($this->calendarInfo['id']);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/SharingPlugin.php b/app/Gdoo/Calendar/Sabre/CalDAV/SharingPlugin.php
new file mode 100644
index 00000000..fe340b93
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/SharingPlugin.php
@@ -0,0 +1,526 @@
+server = $server;
+ $server->resourceTypeMapping['Sabre\\CalDAV\\ISharedCalendar'] = '{' . Plugin::NS_CALENDARSERVER . '}shared';
+
+ array_push(
+ $this->server->protectedProperties,
+ '{' . Plugin::NS_CALENDARSERVER . '}invite',
+ '{' . Plugin::NS_CALENDARSERVER . '}allowed-sharing-modes',
+ '{' . Plugin::NS_CALENDARSERVER . '}shared-url'
+ );
+
+ $this->server->subscribeEvent('beforeGetProperties', array($this, 'beforeGetProperties'));
+ $this->server->subscribeEvent('afterGetProperties', array($this, 'afterGetProperties'));
+ $this->server->subscribeEvent('updateProperties', array($this, 'updateProperties'));
+ $this->server->subscribeEvent('unknownMethod', array($this,'unknownMethod'));
+
+ }
+
+ /**
+ * This event is triggered when properties are requested for a certain
+ * node.
+ *
+ * This allows us to inject any properties early.
+ *
+ * @param string $path
+ * @param DAV\INode $node
+ * @param array $requestedProperties
+ * @param array $returnedProperties
+ * @return void
+ */
+ public function beforeGetProperties($path, DAV\INode $node, &$requestedProperties, &$returnedProperties) {
+
+ if ($node instanceof IShareableCalendar) {
+ if (($index = array_search('{' . Plugin::NS_CALENDARSERVER . '}invite', $requestedProperties))!==false) {
+
+ unset($requestedProperties[$index]);
+ $returnedProperties[200]['{' . Plugin::NS_CALENDARSERVER . '}invite'] =
+ new Property\Invite(
+ $node->getShares()
+ );
+
+ }
+
+ }
+
+ if ($node instanceof ISharedCalendar) {
+
+ if (($index = array_search('{' . Plugin::NS_CALENDARSERVER . '}shared-url', $requestedProperties))!==false) {
+
+ unset($requestedProperties[$index]);
+ $returnedProperties[200]['{' . Plugin::NS_CALENDARSERVER . '}shared-url'] =
+ new DAV\Property\Href(
+ $node->getSharedUrl()
+ );
+
+ }
+ // The 'invite' property is slightly different for the 'shared'
+ // instance of the calendar, as it also contains the owner
+ // information.
+ if (($index = array_search('{' . Plugin::NS_CALENDARSERVER . '}invite', $requestedProperties))!==false) {
+
+ unset($requestedProperties[$index]);
+
+ // Fetching owner information
+ $props = $this->server->getPropertiesForPath($node->getOwner(), array(
+ '{http://sabredav.org/ns}email-address',
+ '{DAV:}displayname',
+ ), 1);
+
+ $ownerInfo = array(
+ 'href' => $node->getOwner(),
+ );
+
+ if (isset($props[0][200])) {
+
+ // We're mapping the internal webdav properties to the
+ // elements caldav-sharing expects.
+ if (isset($props[0][200]['{http://sabredav.org/ns}email-address'])) {
+ $ownerInfo['href'] = 'mailto:' . $props[0][200]['{http://sabredav.org/ns}email-address'];
+ }
+ if (isset($props[0][200]['{DAV:}displayname'])) {
+ $ownerInfo['commonName'] = $props[0][200]['{DAV:}displayname'];
+ }
+
+ }
+
+ $returnedProperties[200]['{' . Plugin::NS_CALENDARSERVER . '}invite'] =
+ new Property\Invite(
+ $node->getShares(),
+ $ownerInfo
+ );
+
+ }
+
+
+ }
+
+ }
+
+ /**
+ * This method is triggered *after* all properties have been retrieved.
+ * This allows us to inject the correct resourcetype for calendars that
+ * have been shared.
+ *
+ * @param string $path
+ * @param array $properties
+ * @param DAV\INode $node
+ * @return void
+ */
+ public function afterGetProperties($path, &$properties, DAV\INode $node) {
+
+ if ($node instanceof IShareableCalendar) {
+ if (isset($properties[200]['{DAV:}resourcetype'])) {
+ if (count($node->getShares())>0) {
+ $properties[200]['{DAV:}resourcetype']->add(
+ '{' . Plugin::NS_CALENDARSERVER . '}shared-owner'
+ );
+ }
+ }
+ $propName = '{' . Plugin::NS_CALENDARSERVER . '}allowed-sharing-modes';
+ if (array_key_exists($propName, $properties[404])) {
+ unset($properties[404][$propName]);
+ $properties[200][$propName] = new Property\AllowedSharingModes(true,false);
+ }
+
+ }
+
+ }
+
+ /**
+ * This method is trigged when a user attempts to update a node's
+ * properties.
+ *
+ * A previous draft of the sharing spec stated that it was possible to use
+ * PROPPATCH to remove 'shared-owner' from the resourcetype, thus unsharing
+ * the calendar.
+ *
+ * Even though this is no longer in the current spec, we keep this around
+ * because OS X 10.7 may still make use of this feature.
+ *
+ * @param array $mutations
+ * @param array $result
+ * @param DAV\INode $node
+ * @return void
+ */
+ public function updateProperties(array &$mutations, array &$result, DAV\INode $node) {
+
+ if (!$node instanceof IShareableCalendar)
+ return;
+
+ if (!isset($mutations['{DAV:}resourcetype'])) {
+ return;
+ }
+
+ // Only doing something if shared-owner is indeed not in the list.
+ if($mutations['{DAV:}resourcetype']->is('{' . Plugin::NS_CALENDARSERVER . '}shared-owner')) return;
+
+ $shares = $node->getShares();
+ $remove = array();
+ foreach($shares as $share) {
+ $remove[] = $share['href'];
+ }
+ $node->updateShares(array(), $remove);
+
+ // We're marking this update as 200 OK
+ $result[200]['{DAV:}resourcetype'] = null;
+
+ // Removing it from the mutations list
+ unset($mutations['{DAV:}resourcetype']);
+
+ }
+
+ /**
+ * This event is triggered when the server didn't know how to handle a
+ * certain request.
+ *
+ * We intercept this to handle POST requests on calendars.
+ *
+ * @param string $method
+ * @param string $uri
+ * @return null|bool
+ */
+ public function unknownMethod($method, $uri) {
+
+ if ($method!=='POST') {
+ return;
+ }
+
+ // Only handling xml
+ $contentType = $this->server->httpRequest->getHeader('Content-Type');
+ if (strpos($contentType,'application/xml')===false && strpos($contentType,'text/xml')===false)
+ return;
+
+ // Making sure the node exists
+ try {
+ $node = $this->server->tree->getNodeForPath($uri);
+ } catch (DAV\Exception\NotFound $e) {
+ return;
+ }
+
+ $requestBody = $this->server->httpRequest->getBody(true);
+
+ // If this request handler could not deal with this POST request, it
+ // will return 'null' and other plugins get a chance to handle the
+ // request.
+ //
+ // However, we already requested the full body. This is a problem,
+ // because a body can only be read once. This is why we preemptively
+ // re-populated the request body with the existing data.
+ $this->server->httpRequest->setBody($requestBody);
+
+ $dom = DAV\XMLUtil::loadDOMDocument($requestBody);
+
+ $documentType = DAV\XMLUtil::toClarkNotation($dom->firstChild);
+
+ switch($documentType) {
+
+ // Dealing with the 'share' document, which modified invitees on a
+ // calendar.
+ case '{' . Plugin::NS_CALENDARSERVER . '}share' :
+
+ // We can only deal with IShareableCalendar objects
+ if (!$node instanceof IShareableCalendar) {
+ return;
+ }
+
+ // Getting ACL info
+ $acl = $this->server->getPlugin('acl');
+
+ // If there's no ACL support, we allow everything
+ if ($acl) {
+ $acl->checkPrivileges($uri, '{DAV:}write');
+ }
+
+ $mutations = $this->parseShareRequest($dom);
+
+ $node->updateShares($mutations[0], $mutations[1]);
+
+ $this->server->httpResponse->sendStatus(200);
+ // Adding this because sending a response body may cause issues,
+ // and I wanted some type of indicator the response was handled.
+ $this->server->httpResponse->setHeader('X-Sabre-Status', 'everything-went-well');
+
+ // Breaking the event chain
+ return false;
+
+ // The invite-reply document is sent when the user replies to an
+ // invitation of a calendar share.
+ case '{'. Plugin::NS_CALENDARSERVER.'}invite-reply' :
+
+ // This only works on the calendar-home-root node.
+ if (!$node instanceof UserCalendars) {
+ return;
+ }
+
+ // Getting ACL info
+ $acl = $this->server->getPlugin('acl');
+
+ // If there's no ACL support, we allow everything
+ if ($acl) {
+ $acl->checkPrivileges($uri, '{DAV:}write');
+ }
+
+ $message = $this->parseInviteReplyRequest($dom);
+
+ $url = $node->shareReply(
+ $message['href'],
+ $message['status'],
+ $message['calendarUri'],
+ $message['inReplyTo'],
+ $message['summary']
+ );
+
+ $this->server->httpResponse->sendStatus(200);
+ // Adding this because sending a response body may cause issues,
+ // and I wanted some type of indicator the response was handled.
+ $this->server->httpResponse->setHeader('X-Sabre-Status', 'everything-went-well');
+
+ if ($url) {
+ $dom = new \DOMDocument('1.0', 'UTF-8');
+ $dom->formatOutput = true;
+
+ $root = $dom->createElement('cs:shared-as');
+ foreach($this->server->xmlNamespaces as $namespace => $prefix) {
+ $root->setAttribute('xmlns:' . $prefix, $namespace);
+ }
+
+ $dom->appendChild($root);
+ $href = new DAV\Property\Href($url);
+
+ $href->serialize($this->server, $root);
+ $this->server->httpResponse->setHeader('Content-Type','application/xml');
+ $this->server->httpResponse->sendBody($dom->saveXML());
+
+ }
+
+ // Breaking the event chain
+ return false;
+
+ case '{' . Plugin::NS_CALENDARSERVER . '}publish-calendar' :
+
+ // We can only deal with IShareableCalendar objects
+ if (!$node instanceof IShareableCalendar) {
+ return;
+ }
+
+ // Getting ACL info
+ $acl = $this->server->getPlugin('acl');
+
+ // If there's no ACL support, we allow everything
+ if ($acl) {
+ $acl->checkPrivileges($uri, '{DAV:}write');
+ }
+
+ $node->setPublishStatus(true);
+
+ // iCloud sends back the 202, so we will too.
+ $this->server->httpResponse->sendStatus(202);
+
+ // Adding this because sending a response body may cause issues,
+ // and I wanted some type of indicator the response was handled.
+ $this->server->httpResponse->setHeader('X-Sabre-Status', 'everything-went-well');
+
+ // Breaking the event chain
+ return false;
+
+ case '{' . Plugin::NS_CALENDARSERVER . '}unpublish-calendar' :
+
+ // We can only deal with IShareableCalendar objects
+ if (!$node instanceof IShareableCalendar) {
+ return;
+ }
+
+ // Getting ACL info
+ $acl = $this->server->getPlugin('acl');
+
+ // If there's no ACL support, we allow everything
+ if ($acl) {
+ $acl->checkPrivileges($uri, '{DAV:}write');
+ }
+
+ $node->setPublishStatus(false);
+
+ $this->server->httpResponse->sendStatus(200);
+
+ // Adding this because sending a response body may cause issues,
+ // and I wanted some type of indicator the response was handled.
+ $this->server->httpResponse->setHeader('X-Sabre-Status', 'everything-went-well');
+
+ // Breaking the event chain
+ return false;
+
+ }
+
+
+
+ }
+
+ /**
+ * Parses the 'share' POST request.
+ *
+ * This method returns an array, containing two arrays.
+ * The first array is a list of new sharees. Every element is a struct
+ * containing a:
+ * * href element. (usually a mailto: address)
+ * * commonName element (often a first and lastname, but can also be
+ * false)
+ * * readOnly (true or false)
+ * * summary (A description of the share, can also be false)
+ *
+ * The second array is a list of sharees that are to be removed. This is
+ * just a simple array with 'hrefs'.
+ *
+ * @param \DOMDocument $dom
+ * @return array
+ */
+ protected function parseShareRequest(\DOMDocument $dom) {
+
+ $xpath = new \DOMXPath($dom);
+ $xpath->registerNamespace('cs', Plugin::NS_CALENDARSERVER);
+ $xpath->registerNamespace('d', 'urn:DAV');
+
+ $set = array();
+ $elems = $xpath->query('cs:set');
+
+ for($i=0; $i < $elems->length; $i++) {
+
+ $xset = $elems->item($i);
+ $set[] = array(
+ 'href' => $xpath->evaluate('string(d:href)', $xset),
+ 'commonName' => $xpath->evaluate('string(cs:common-name)', $xset),
+ 'summary' => $xpath->evaluate('string(cs:summary)', $xset),
+ 'readOnly' => $xpath->evaluate('boolean(cs:read)', $xset)!==false
+ );
+
+ }
+
+ $remove = array();
+ $elems = $xpath->query('cs:remove');
+
+ for($i=0; $i < $elems->length; $i++) {
+
+ $xremove = $elems->item($i);
+ $remove[] = $xpath->evaluate('string(d:href)', $xremove);
+
+ }
+
+ return array($set, $remove);
+
+ }
+
+ /**
+ * Parses the 'invite-reply' POST request.
+ *
+ * This method returns an array, containing the following properties:
+ * * href - The sharee who is replying
+ * * status - One of the self::STATUS_* constants
+ * * calendarUri - The url of the shared calendar
+ * * inReplyTo - The unique id of the share invitation.
+ * * summary - Optional description of the reply.
+ *
+ * @param \DOMDocument $dom
+ * @return array
+ */
+ protected function parseInviteReplyRequest(\DOMDocument $dom) {
+
+ $xpath = new \DOMXPath($dom);
+ $xpath->registerNamespace('cs', Plugin::NS_CALENDARSERVER);
+ $xpath->registerNamespace('d', 'urn:DAV');
+
+ $hostHref = $xpath->evaluate('string(cs:hosturl/d:href)');
+ if (!$hostHref) {
+ throw new DAV\Exception\BadRequest('The {' . Plugin::NS_CALENDARSERVER . '}hosturl/{DAV:}href element is required');
+ }
+
+ return array(
+ 'href' => $xpath->evaluate('string(d:href)'),
+ 'calendarUri' => $this->server->calculateUri($hostHref),
+ 'inReplyTo' => $xpath->evaluate('string(cs:in-reply-to)'),
+ 'summary' => $xpath->evaluate('string(cs:summary)'),
+ 'status' => $xpath->evaluate('boolean(cs:invite-accepted)')?self::STATUS_ACCEPTED:self::STATUS_DECLINED
+ );
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/UserCalendars.php b/app/Gdoo/Calendar/Sabre/CalDAV/UserCalendars.php
new file mode 100644
index 00000000..e2bc36f3
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/UserCalendars.php
@@ -0,0 +1,342 @@
+caldavBackend = $caldavBackend;
+ $this->principalInfo = $principalInfo;
+
+ }
+
+ /**
+ * Returns the name of this object
+ *
+ * @return string
+ */
+ public function getName() {
+
+ list(,$name) = DAV\URLUtil::splitPath($this->principalInfo['uri']);
+ return $name;
+
+ }
+
+ /**
+ * Updates the name of this object
+ *
+ * @param string $name
+ * @return void
+ */
+ public function setName($name) {
+
+ throw new DAV\Exception\Forbidden();
+
+ }
+
+ /**
+ * Deletes this object
+ *
+ * @return void
+ */
+ public function delete() {
+
+ throw new DAV\Exception\Forbidden();
+
+ }
+
+ /**
+ * Returns the last modification date
+ *
+ * @return int
+ */
+ public function getLastModified() {
+
+ return null;
+
+ }
+
+ /**
+ * Creates a new file under this object.
+ *
+ * This is currently not allowed
+ *
+ * @param string $filename
+ * @param resource $data
+ * @return void
+ */
+ public function createFile($filename, $data=null) {
+
+ throw new DAV\Exception\MethodNotAllowed('Creating new files in this collection is not supported');
+
+ }
+
+ /**
+ * Creates a new directory under this object.
+ *
+ * This is currently not allowed.
+ *
+ * @param string $filename
+ * @return void
+ */
+ public function createDirectory($filename) {
+
+ throw new DAV\Exception\MethodNotAllowed('Creating new collections in this collection is not supported');
+
+ }
+
+ /**
+ * Returns a single calendar, by name
+ *
+ * @param string $name
+ * @todo needs optimizing
+ * @return Calendar
+ */
+ public function getChild($name) {
+
+ foreach($this->getChildren() as $child) {
+ if ($name==$child->getName())
+ return $child;
+
+ }
+ throw new DAV\Exception\NotFound('Calendar with name \'' . $name . '\' could not be found');
+
+ }
+
+ /**
+ * Checks if a calendar exists.
+ *
+ * @param string $name
+ * @todo needs optimizing
+ * @return bool
+ */
+ public function childExists($name) {
+
+ foreach($this->getChildren() as $child) {
+ if ($name==$child->getName())
+ return true;
+
+ }
+ return false;
+
+ }
+
+ /**
+ * Returns a list of calendars
+ *
+ * @return array
+ */
+ public function getChildren() {
+
+ $calendars = $this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']);
+ $objs = array();
+ foreach($calendars as $calendar) {
+ if ($this->caldavBackend instanceof Backend\SharingSupport) {
+ if (isset($calendar['{http://calendarserver.org/ns/}shared-url'])) {
+ $objs[] = new SharedCalendar($this->caldavBackend, $calendar);
+ } else {
+ $objs[] = new ShareableCalendar($this->caldavBackend, $calendar);
+ }
+ } else {
+ $objs[] = new Calendar($this->caldavBackend, $calendar);
+ }
+ }
+ $objs[] = new Schedule\Outbox($this->principalInfo['uri']);
+
+ // We're adding a notifications node, if it's supported by the backend.
+ if ($this->caldavBackend instanceof Backend\NotificationSupport) {
+ $objs[] = new Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']);
+ }
+ return $objs;
+
+ }
+
+ /**
+ * Creates a new calendar
+ *
+ * @param string $name
+ * @param array $resourceType
+ * @param array $properties
+ * @return void
+ */
+ public function createExtendedCollection($name, array $resourceType, array $properties) {
+
+ $isCalendar = false;
+ foreach($resourceType as $rt) {
+ switch ($rt) {
+ case '{DAV:}collection' :
+ case '{http://calendarserver.org/ns/}shared-owner' :
+ // ignore
+ break;
+ case '{urn:ietf:params:xml:ns:caldav}calendar' :
+ $isCalendar = true;
+ break;
+ default :
+ throw new DAV\Exception\InvalidResourceType('Unknown resourceType: ' . $rt);
+ }
+ }
+ if (!$isCalendar) {
+ throw new DAV\Exception\InvalidResourceType('You can only create calendars in this collection');
+ }
+ $this->caldavBackend->createCalendar($this->principalInfo['uri'], $name, $properties);
+
+ }
+
+ /**
+ * Returns the owner principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getOwner() {
+
+ return $this->principalInfo['uri'];
+
+ }
+
+ /**
+ * Returns a group principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getGroup() {
+
+ return null;
+
+ }
+
+ /**
+ * Returns a list of ACE's for this node.
+ *
+ * Each ACE has the following properties:
+ * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
+ * currently the only supported privileges
+ * * 'principal', a url to the principal who owns the node
+ * * 'protected' (optional), indicating that this ACE is not allowed to
+ * be updated.
+ *
+ * @return array
+ */
+ public function getACL() {
+
+ return array(
+ array(
+ 'privilege' => '{DAV:}read',
+ 'principal' => $this->principalInfo['uri'],
+ 'protected' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}write',
+ 'principal' => $this->principalInfo['uri'],
+ 'protected' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}read',
+ 'principal' => $this->principalInfo['uri'] . '/calendar-proxy-write',
+ 'protected' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}write',
+ 'principal' => $this->principalInfo['uri'] . '/calendar-proxy-write',
+ 'protected' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}read',
+ 'principal' => $this->principalInfo['uri'] . '/calendar-proxy-read',
+ 'protected' => true,
+ ),
+
+ );
+
+ }
+
+ /**
+ * Updates the ACL
+ *
+ * This method will receive a list of new ACE's.
+ *
+ * @param array $acl
+ * @return void
+ */
+ public function setACL(array $acl) {
+
+ throw new DAV\Exception\MethodNotAllowed('Changing ACL is not yet supported');
+
+ }
+
+ /**
+ * Returns the list of supported privileges for this node.
+ *
+ * The returned data structure is a list of nested privileges.
+ * See Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple
+ * standard structure.
+ *
+ * If null is returned from this method, the default privilege set is used,
+ * which is fine for most common usecases.
+ *
+ * @return array|null
+ */
+ public function getSupportedPrivilegeSet() {
+
+ return null;
+
+ }
+
+ /**
+ * This method is called when a user replied to a request to share.
+ *
+ * This method should return the url of the newly created calendar if the
+ * share was accepted.
+ *
+ * @param string href The sharee who is replying (often a mailto: address)
+ * @param int status One of the SharingPlugin::STATUS_* constants
+ * @param string $calendarUri The url to the calendar thats being shared
+ * @param string $inReplyTo The unique id this message is a response to
+ * @param string $summary A description of the reply
+ * @return null|string
+ */
+ public function shareReply($href, $status, $calendarUri, $inReplyTo, $summary = null) {
+
+ if (!$this->caldavBackend instanceof Backend\SharingSupport) {
+ throw new DAV\Exception\NotImplemented('Sharing support is not implemented by this backend.');
+ }
+
+ return $this->caldavBackend->shareReply($href, $status, $calendarUri, $inReplyTo, $summary);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CalDAV/Version.php b/app/Gdoo/Calendar/Sabre/CalDAV/Version.php
new file mode 100644
index 00000000..971326ab
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CalDAV/Version.php
@@ -0,0 +1,24 @@
+carddavBackend = $carddavBackend;
+ $this->addressBookInfo = $addressBookInfo;
+
+ }
+
+ /**
+ * Returns the name of the addressbook
+ *
+ * @return string
+ */
+ public function getName() {
+
+ return $this->addressBookInfo['uri'];
+
+ }
+
+ /**
+ * Returns a card
+ *
+ * @param string $name
+ * @return \ICard
+ */
+ public function getChild($name) {
+
+ $obj = $this->carddavBackend->getCard($this->addressBookInfo['id'],$name);
+ if (!$obj) throw new DAV\Exception\NotFound('Card not found');
+ return new Card($this->carddavBackend,$this->addressBookInfo,$obj);
+
+ }
+
+ /**
+ * Returns the full list of cards
+ *
+ * @return array
+ */
+ public function getChildren() {
+
+ $objs = $this->carddavBackend->getCards($this->addressBookInfo['id']);
+ $children = array();
+ foreach($objs as $obj) {
+ $children[] = new Card($this->carddavBackend,$this->addressBookInfo,$obj);
+ }
+ return $children;
+
+ }
+
+ /**
+ * Creates a new directory
+ *
+ * We actually block this, as subdirectories are not allowed in addressbooks.
+ *
+ * @param string $name
+ * @return void
+ */
+ public function createDirectory($name) {
+
+ throw new DAV\Exception\MethodNotAllowed('Creating collections in addressbooks is not allowed');
+
+ }
+
+ /**
+ * Creates a new file
+ *
+ * The contents of the new file must be a valid VCARD.
+ *
+ * This method may return an ETag.
+ *
+ * @param string $name
+ * @param resource $vcardData
+ * @return string|null
+ */
+ public function createFile($name,$vcardData = null) {
+
+ if (is_resource($vcardData)) {
+ $vcardData = stream_get_contents($vcardData);
+ }
+ // Converting to UTF-8, if needed
+ $vcardData = DAV\StringUtil::ensureUTF8($vcardData);
+
+ return $this->carddavBackend->createCard($this->addressBookInfo['id'],$name,$vcardData);
+
+ }
+
+ /**
+ * Deletes the entire addressbook.
+ *
+ * @return void
+ */
+ public function delete() {
+
+ $this->carddavBackend->deleteAddressBook($this->addressBookInfo['id']);
+
+ }
+
+ /**
+ * Renames the addressbook
+ *
+ * @param string $newName
+ * @return void
+ */
+ public function setName($newName) {
+
+ throw new DAV\Exception\MethodNotAllowed('Renaming addressbooks is not yet supported');
+
+ }
+
+ /**
+ * Returns the last modification date as a unix timestamp.
+ *
+ * @return void
+ */
+ public function getLastModified() {
+
+ return null;
+
+ }
+
+ /**
+ * Updates properties on this node,
+ *
+ * The properties array uses the propertyName in clark-notation as key,
+ * and the array value for the property value. In the case a property
+ * should be deleted, the property value will be null.
+ *
+ * This method must be atomic. If one property cannot be changed, the
+ * entire operation must fail.
+ *
+ * If the operation was successful, true can be returned.
+ * If the operation failed, false can be returned.
+ *
+ * Deletion of a non-existent property is always successful.
+ *
+ * Lastly, it is optional to return detailed information about any
+ * failures. In this case an array should be returned with the following
+ * structure:
+ *
+ * array(
+ * 403 => array(
+ * '{DAV:}displayname' => null,
+ * ),
+ * 424 => array(
+ * '{DAV:}owner' => null,
+ * )
+ * )
+ *
+ * In this example it was forbidden to update {DAV:}displayname.
+ * (403 Forbidden), which in turn also caused {DAV:}owner to fail
+ * (424 Failed Dependency) because the request needs to be atomic.
+ *
+ * @param array $mutations
+ * @return bool|array
+ */
+ public function updateProperties($mutations) {
+
+ return $this->carddavBackend->updateAddressBook($this->addressBookInfo['id'], $mutations);
+
+ }
+
+ /**
+ * Returns a list of properties for this nodes.
+ *
+ * The properties list is a list of propertynames the client requested,
+ * encoded in clark-notation {xmlnamespace}tagname
+ *
+ * If the array is empty, it means 'all properties' were requested.
+ *
+ * @param array $properties
+ * @return array
+ */
+ public function getProperties($properties) {
+
+ $response = array();
+ foreach($properties as $propertyName) {
+
+ if (isset($this->addressBookInfo[$propertyName])) {
+
+ $response[$propertyName] = $this->addressBookInfo[$propertyName];
+
+ }
+
+ }
+
+ return $response;
+
+ }
+
+ /**
+ * Returns the owner principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getOwner() {
+
+ return $this->addressBookInfo['principaluri'];
+
+ }
+
+ /**
+ * Returns a group principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getGroup() {
+
+ return null;
+
+ }
+
+ /**
+ * Returns a list of ACE's for this node.
+ *
+ * Each ACE has the following properties:
+ * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
+ * currently the only supported privileges
+ * * 'principal', a url to the principal who owns the node
+ * * 'protected' (optional), indicating that this ACE is not allowed to
+ * be updated.
+ *
+ * @return array
+ */
+ public function getACL() {
+
+ return array(
+ array(
+ 'privilege' => '{DAV:}read',
+ 'principal' => $this->addressBookInfo['principaluri'],
+ 'protected' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}write',
+ 'principal' => $this->addressBookInfo['principaluri'],
+ 'protected' => true,
+ ),
+
+ );
+
+ }
+
+ /**
+ * Updates the ACL
+ *
+ * This method will receive a list of new ACE's.
+ *
+ * @param array $acl
+ * @return void
+ */
+ public function setACL(array $acl) {
+
+ throw new DAV\Exception\MethodNotAllowed('Changing ACL is not yet supported');
+
+ }
+
+ /**
+ * Returns the list of supported privileges for this node.
+ *
+ * The returned data structure is a list of nested privileges.
+ * See Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple
+ * standard structure.
+ *
+ * If null is returned from this method, the default privilege set is used,
+ * which is fine for most common usecases.
+ *
+ * @return array|null
+ */
+ public function getSupportedPrivilegeSet() {
+
+ return null;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CardDAV/AddressBookQueryParser.php b/app/Gdoo/Calendar/Sabre/CardDAV/AddressBookQueryParser.php
new file mode 100644
index 00000000..0fefba62
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CardDAV/AddressBookQueryParser.php
@@ -0,0 +1,221 @@
+dom = $dom;
+
+ $this->xpath = new \DOMXPath($dom);
+ $this->xpath->registerNameSpace('card',Plugin::NS_CARDDAV);
+
+ }
+
+ /**
+ * Parses the request.
+ *
+ * @return void
+ */
+ public function parse() {
+
+ $filterNode = null;
+
+ $limit = $this->xpath->evaluate('number(/card:addressbook-query/card:limit/card:nresults)');
+ if (is_nan($limit)) $limit = null;
+
+ $filter = $this->xpath->query('/card:addressbook-query/card:filter');
+
+ // According to the CardDAV spec there needs to be exactly 1 filter
+ // element. However, KDE 4.8.2 contains a bug that will encode 0 filter
+ // elements, so this is a workaround for that.
+ //
+ // See: https://bugs.kde.org/show_bug.cgi?id=300047
+ if ($filter->length === 0) {
+ $test = null;
+ $filter = null;
+ } elseif ($filter->length === 1) {
+ $filter = $filter->item(0);
+ $test = $this->xpath->evaluate('string(@test)', $filter);
+ } else {
+ throw new DAV\Exception\BadRequest('Only one filter element is allowed');
+ }
+
+ if (!$test) $test = self::TEST_ANYOF;
+ if ($test !== self::TEST_ANYOF && $test !== self::TEST_ALLOF) {
+ throw new DAV\Exception\BadRequest('The test attribute must either hold "anyof" or "allof"');
+ }
+
+ $propFilters = array();
+
+ $propFilterNodes = $this->xpath->query('card:prop-filter', $filter);
+ for($ii=0; $ii < $propFilterNodes->length; $ii++) {
+
+ $propFilters[] = $this->parsePropFilterNode($propFilterNodes->item($ii));
+
+
+ }
+
+ $this->filters = $propFilters;
+ $this->limit = $limit;
+ $this->requestedProperties = array_keys(DAV\XMLUtil::parseProperties($this->dom->firstChild));
+ $this->test = $test;
+
+ }
+
+ /**
+ * Parses the prop-filter xml element
+ *
+ * @param \DOMElement $propFilterNode
+ * @return array
+ */
+ protected function parsePropFilterNode(\DOMElement $propFilterNode) {
+
+ $propFilter = array();
+ $propFilter['name'] = $propFilterNode->getAttribute('name');
+ $propFilter['test'] = $propFilterNode->getAttribute('test');
+ if (!$propFilter['test']) $propFilter['test'] = 'anyof';
+
+ $propFilter['is-not-defined'] = $this->xpath->query('card:is-not-defined', $propFilterNode)->length>0;
+
+ $paramFilterNodes = $this->xpath->query('card:param-filter', $propFilterNode);
+
+ $propFilter['param-filters'] = array();
+
+
+ for($ii=0;$ii<$paramFilterNodes->length;$ii++) {
+
+ $propFilter['param-filters'][] = $this->parseParamFilterNode($paramFilterNodes->item($ii));
+
+ }
+ $propFilter['text-matches'] = array();
+ $textMatchNodes = $this->xpath->query('card:text-match', $propFilterNode);
+
+ for($ii=0;$ii<$textMatchNodes->length;$ii++) {
+
+ $propFilter['text-matches'][] = $this->parseTextMatchNode($textMatchNodes->item($ii));
+
+ }
+
+ return $propFilter;
+
+ }
+
+ /**
+ * Parses the param-filter element
+ *
+ * @param \DOMElement $paramFilterNode
+ * @return array
+ */
+ public function parseParamFilterNode(\DOMElement $paramFilterNode) {
+
+ $paramFilter = array();
+ $paramFilter['name'] = $paramFilterNode->getAttribute('name');
+ $paramFilter['is-not-defined'] = $this->xpath->query('card:is-not-defined', $paramFilterNode)->length>0;
+ $paramFilter['text-match'] = null;
+
+ $textMatch = $this->xpath->query('card:text-match', $paramFilterNode);
+ if ($textMatch->length>0) {
+ $paramFilter['text-match'] = $this->parseTextMatchNode($textMatch->item(0));
+ }
+
+ return $paramFilter;
+
+ }
+
+ /**
+ * Text match
+ *
+ * @param \DOMElement $textMatchNode
+ * @return array
+ */
+ public function parseTextMatchNode(\DOMElement $textMatchNode) {
+
+ $matchType = $textMatchNode->getAttribute('match-type');
+ if (!$matchType) $matchType = 'contains';
+
+ if (!in_array($matchType, array('contains', 'equals', 'starts-with', 'ends-with'))) {
+ throw new DAV\Exception\BadRequest('Unknown match-type: ' . $matchType);
+ }
+
+ $negateCondition = $textMatchNode->getAttribute('negate-condition');
+ $negateCondition = $negateCondition==='yes';
+ $collation = $textMatchNode->getAttribute('collation');
+ if (!$collation) $collation = 'i;unicode-casemap';
+
+ return array(
+ 'negate-condition' => $negateCondition,
+ 'collation' => $collation,
+ 'match-type' => $matchType,
+ 'value' => $textMatchNode->nodeValue
+ );
+
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CardDAV/AddressBookRoot.php b/app/Gdoo/Calendar/Sabre/CardDAV/AddressBookRoot.php
new file mode 100644
index 00000000..b58648b8
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CardDAV/AddressBookRoot.php
@@ -0,0 +1,80 @@
+carddavBackend = $carddavBackend;
+ parent::__construct($principalBackend, $principalPrefix);
+
+ }
+
+ /**
+ * Returns the name of the node
+ *
+ * @return string
+ */
+ public function getName() {
+
+ return Plugin::ADDRESSBOOK_ROOT;
+
+ }
+
+ /**
+ * This method returns a node for a principal.
+ *
+ * The passed array contains principal information, and is guaranteed to
+ * at least contain a uri item. Other properties may or may not be
+ * supplied by the authentication backend.
+ *
+ * @param array $principal
+ * @return \Sabre\DAV\INode
+ */
+ public function getChildForPrincipal(array $principal) {
+
+ return new UserAddressBooks($this->carddavBackend, $principal['uri']);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CardDAV/Backend/AbstractBackend.php b/app/Gdoo/Calendar/Sabre/CardDAV/Backend/AbstractBackend.php
new file mode 100644
index 00000000..f8addd0d
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CardDAV/Backend/AbstractBackend.php
@@ -0,0 +1,18 @@
+pdo = $pdo;
+ $this->addressBooksTableName = $addressBooksTableName;
+ $this->cardsTableName = $cardsTableName;
+
+ }
+
+ /**
+ * Returns the list of addressbooks for a specific user.
+ *
+ * @param string $principalUri
+ * @return array
+ */
+ public function getAddressBooksForUser($principalUri) {
+
+ $stmt = $this->pdo->prepare('SELECT id, uri, displayname, principaluri, description, ctag FROM '.$this->addressBooksTableName.' WHERE principaluri = ?');
+ $stmt->execute(array($principalUri));
+
+ $addressBooks = array();
+
+ foreach($stmt->fetchAll() as $row) {
+
+ $addressBooks[] = array(
+ 'id' => $row['id'],
+ 'uri' => $row['uri'],
+ 'principaluri' => $row['principaluri'],
+ '{DAV:}displayname' => $row['displayname'],
+ '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'],
+ '{http://calendarserver.org/ns/}getctag' => $row['ctag'],
+ '{' . CardDAV\Plugin::NS_CARDDAV . '}supported-address-data' =>
+ new CardDAV\Property\SupportedAddressData(),
+ );
+
+ }
+
+ return $addressBooks;
+
+ }
+
+
+ /**
+ * Updates an addressbook's properties
+ *
+ * See Sabre\DAV\IProperties for a description of the mutations array, as
+ * well as the return value.
+ *
+ * @param mixed $addressBookId
+ * @param array $mutations
+ * @see Sabre\DAV\IProperties::updateProperties
+ * @return bool|array
+ */
+ public function updateAddressBook($addressBookId, array $mutations) {
+
+ $updates = array();
+
+ foreach($mutations as $property=>$newValue) {
+
+ switch($property) {
+ case '{DAV:}displayname' :
+ $updates['displayname'] = $newValue;
+ break;
+ case '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' :
+ $updates['description'] = $newValue;
+ break;
+ default :
+ // If any unsupported values were being updated, we must
+ // let the entire request fail.
+ return false;
+ }
+
+ }
+
+ // No values are being updated?
+ if (!$updates) {
+ return false;
+ }
+
+ $query = 'UPDATE ' . $this->addressBooksTableName . ' SET ctag = ctag + 1 ';
+ foreach($updates as $key=>$value) {
+ $query.=', `' . $key . '` = :' . $key . ' ';
+ }
+ $query.=' WHERE id = :addressbookid';
+
+ $stmt = $this->pdo->prepare($query);
+ $updates['addressbookid'] = $addressBookId;
+
+ $stmt->execute($updates);
+
+ return true;
+
+ }
+
+ /**
+ * Creates a new address book
+ *
+ * @param string $principalUri
+ * @param string $url Just the 'basename' of the url.
+ * @param array $properties
+ * @return void
+ */
+ public function createAddressBook($principalUri, $url, array $properties) {
+
+ $values = array(
+ 'displayname' => null,
+ 'description' => null,
+ 'principaluri' => $principalUri,
+ 'uri' => $url,
+ );
+
+ foreach($properties as $property=>$newValue) {
+
+ switch($property) {
+ case '{DAV:}displayname' :
+ $values['displayname'] = $newValue;
+ break;
+ case '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' :
+ $values['description'] = $newValue;
+ break;
+ default :
+ throw new DAV\Exception\BadRequest('Unknown property: ' . $property);
+ }
+
+ }
+
+ $query = 'INSERT INTO ' . $this->addressBooksTableName . ' (uri, displayname, description, principaluri, ctag) VALUES (:uri, :displayname, :description, :principaluri, 1)';
+ $stmt = $this->pdo->prepare($query);
+ $stmt->execute($values);
+
+ }
+
+ /**
+ * Deletes an entire addressbook and all its contents
+ *
+ * @param int $addressBookId
+ * @return void
+ */
+ public function deleteAddressBook($addressBookId) {
+
+ $stmt = $this->pdo->prepare('DELETE FROM ' . $this->cardsTableName . ' WHERE addressbookid = ?');
+ $stmt->execute(array($addressBookId));
+
+ $stmt = $this->pdo->prepare('DELETE FROM ' . $this->addressBooksTableName . ' WHERE id = ?');
+ $stmt->execute(array($addressBookId));
+
+ }
+
+ /**
+ * Returns all cards for a specific addressbook id.
+ *
+ * This method should return the following properties for each card:
+ * * carddata - raw vcard data
+ * * uri - Some unique url
+ * * lastmodified - A unix timestamp
+ *
+ * It's recommended to also return the following properties:
+ * * etag - A unique etag. This must change every time the card changes.
+ * * size - The size of the card in bytes.
+ *
+ * If these last two properties are provided, less time will be spent
+ * calculating them. If they are specified, you can also ommit carddata.
+ * This may speed up certain requests, especially with large cards.
+ *
+ * @param mixed $addressbookId
+ * @return array
+ */
+ public function getCards($addressbookId) {
+
+ $stmt = $this->pdo->prepare('SELECT id, carddata, uri, lastmodified FROM ' . $this->cardsTableName . ' WHERE addressbookid = ?');
+ $stmt->execute(array($addressbookId));
+
+ return $stmt->fetchAll(\PDO::FETCH_ASSOC);
+
+
+ }
+
+ /**
+ * Returns a specfic card.
+ *
+ * The same set of properties must be returned as with getCards. The only
+ * exception is that 'carddata' is absolutely required.
+ *
+ * @param mixed $addressBookId
+ * @param string $cardUri
+ * @return array
+ */
+ public function getCard($addressBookId, $cardUri) {
+
+ $stmt = $this->pdo->prepare('SELECT id, carddata, uri, lastmodified FROM ' . $this->cardsTableName . ' WHERE addressbookid = ? AND uri = ? LIMIT 1');
+ $stmt->execute(array($addressBookId, $cardUri));
+
+ $result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
+
+ return (count($result)>0?$result[0]:false);
+
+ }
+
+ /**
+ * Creates a new card.
+ *
+ * The addressbook id will be passed as the first argument. This is the
+ * same id as it is returned from the getAddressbooksForUser method.
+ *
+ * The cardUri is a base uri, and doesn't include the full path. The
+ * cardData argument is the vcard body, and is passed as a string.
+ *
+ * It is possible to return an ETag from this method. This ETag is for the
+ * newly created resource, and must be enclosed with double quotes (that
+ * is, the string itself must contain the double quotes).
+ *
+ * You should only return the ETag if you store the carddata as-is. If a
+ * subsequent GET request on the same card does not have the same body,
+ * byte-by-byte and you did return an ETag here, clients tend to get
+ * confused.
+ *
+ * If you don't return an ETag, you can just return null.
+ *
+ * @param mixed $addressBookId
+ * @param string $cardUri
+ * @param string $cardData
+ * @return string|null
+ */
+ public function createCard($addressBookId, $cardUri, $cardData) {
+
+ $stmt = $this->pdo->prepare('INSERT INTO ' . $this->cardsTableName . ' (carddata, uri, lastmodified, addressbookid) VALUES (?, ?, ?, ?)');
+
+ $result = $stmt->execute(array($cardData, $cardUri, time(), $addressBookId));
+
+ $stmt2 = $this->pdo->prepare('UPDATE ' . $this->addressBooksTableName . ' SET ctag = ctag + 1 WHERE id = ?');
+ $stmt2->execute(array($addressBookId));
+
+ return '"' . md5($cardData) . '"';
+
+ }
+
+ /**
+ * Updates a card.
+ *
+ * The addressbook id will be passed as the first argument. This is the
+ * same id as it is returned from the getAddressbooksForUser method.
+ *
+ * The cardUri is a base uri, and doesn't include the full path. The
+ * cardData argument is the vcard body, and is passed as a string.
+ *
+ * It is possible to return an ETag from this method. This ETag should
+ * match that of the updated resource, and must be enclosed with double
+ * quotes (that is: the string itself must contain the actual quotes).
+ *
+ * You should only return the ETag if you store the carddata as-is. If a
+ * subsequent GET request on the same card does not have the same body,
+ * byte-by-byte and you did return an ETag here, clients tend to get
+ * confused.
+ *
+ * If you don't return an ETag, you can just return null.
+ *
+ * @param mixed $addressBookId
+ * @param string $cardUri
+ * @param string $cardData
+ * @return string|null
+ */
+ public function updateCard($addressBookId, $cardUri, $cardData) {
+
+ $stmt = $this->pdo->prepare('UPDATE ' . $this->cardsTableName . ' SET carddata = ?, lastmodified = ? WHERE uri = ? AND addressbookid =?');
+ $stmt->execute(array($cardData, time(), $cardUri, $addressBookId));
+
+ $stmt2 = $this->pdo->prepare('UPDATE ' . $this->addressBooksTableName . ' SET ctag = ctag + 1 WHERE id = ?');
+ $stmt2->execute(array($addressBookId));
+
+ return '"' . md5($cardData) . '"';
+
+ }
+
+ /**
+ * Deletes a card
+ *
+ * @param mixed $addressBookId
+ * @param string $cardUri
+ * @return bool
+ */
+ public function deleteCard($addressBookId, $cardUri) {
+
+ $stmt = $this->pdo->prepare('DELETE FROM ' . $this->cardsTableName . ' WHERE addressbookid = ? AND uri = ?');
+ $stmt->execute(array($addressBookId, $cardUri));
+
+ $stmt2 = $this->pdo->prepare('UPDATE ' . $this->addressBooksTableName . ' SET ctag = ctag + 1 WHERE id = ?');
+ $stmt2->execute(array($addressBookId));
+
+ return $stmt->rowCount()===1;
+
+ }
+}
diff --git a/app/Gdoo/Calendar/Sabre/CardDAV/Card.php b/app/Gdoo/Calendar/Sabre/CardDAV/Card.php
new file mode 100644
index 00000000..16834ea3
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CardDAV/Card.php
@@ -0,0 +1,260 @@
+carddavBackend = $carddavBackend;
+ $this->addressBookInfo = $addressBookInfo;
+ $this->cardData = $cardData;
+
+ }
+
+ /**
+ * Returns the uri for this object
+ *
+ * @return string
+ */
+ public function getName() {
+
+ return $this->cardData['uri'];
+
+ }
+
+ /**
+ * Returns the VCard-formatted object
+ *
+ * @return string
+ */
+ public function get() {
+
+ // Pre-populating 'carddata' is optional. If we don't yet have it
+ // already, we fetch it from the backend.
+ if (!isset($this->cardData['carddata'])) {
+ $this->cardData = $this->carddavBackend->getCard($this->addressBookInfo['id'], $this->cardData['uri']);
+ }
+ return $this->cardData['carddata'];
+
+ }
+
+ /**
+ * Updates the VCard-formatted object
+ *
+ * @param string $cardData
+ * @return string|null
+ */
+ public function put($cardData) {
+
+ if (is_resource($cardData))
+ $cardData = stream_get_contents($cardData);
+
+ // Converting to UTF-8, if needed
+ $cardData = DAV\StringUtil::ensureUTF8($cardData);
+
+ $etag = $this->carddavBackend->updateCard($this->addressBookInfo['id'],$this->cardData['uri'],$cardData);
+ $this->cardData['carddata'] = $cardData;
+ $this->cardData['etag'] = $etag;
+
+ return $etag;
+
+ }
+
+ /**
+ * Deletes the card
+ *
+ * @return void
+ */
+ public function delete() {
+
+ $this->carddavBackend->deleteCard($this->addressBookInfo['id'],$this->cardData['uri']);
+
+ }
+
+ /**
+ * Returns the mime content-type
+ *
+ * @return string
+ */
+ public function getContentType() {
+
+ return 'text/x-vcard; charset=utf-8';
+
+ }
+
+ /**
+ * Returns an ETag for this object
+ *
+ * @return string
+ */
+ public function getETag() {
+
+ if (isset($this->cardData['etag'])) {
+ return $this->cardData['etag'];
+ } else {
+ $data = $this->get();
+ if (is_string($data)) {
+ return '"' . md5($data) . '"';
+ } else {
+ // We refuse to calculate the md5 if it's a stream.
+ return null;
+ }
+ }
+
+ }
+
+ /**
+ * Returns the last modification date as a unix timestamp
+ *
+ * @return int
+ */
+ public function getLastModified() {
+
+ return isset($this->cardData['lastmodified'])?$this->cardData['lastmodified']:null;
+
+ }
+
+ /**
+ * Returns the size of this object in bytes
+ *
+ * @return int
+ */
+ public function getSize() {
+
+ if (array_key_exists('size', $this->cardData)) {
+ return $this->cardData['size'];
+ } else {
+ return strlen($this->get());
+ }
+
+ }
+
+ /**
+ * Returns the owner principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getOwner() {
+
+ return $this->addressBookInfo['principaluri'];
+
+ }
+
+ /**
+ * Returns a group principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getGroup() {
+
+ return null;
+
+ }
+
+ /**
+ * Returns a list of ACE's for this node.
+ *
+ * Each ACE has the following properties:
+ * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
+ * currently the only supported privileges
+ * * 'principal', a url to the principal who owns the node
+ * * 'protected' (optional), indicating that this ACE is not allowed to
+ * be updated.
+ *
+ * @return array
+ */
+ public function getACL() {
+
+ return array(
+ array(
+ 'privilege' => '{DAV:}read',
+ 'principal' => $this->addressBookInfo['principaluri'],
+ 'protected' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}write',
+ 'principal' => $this->addressBookInfo['principaluri'],
+ 'protected' => true,
+ ),
+ );
+
+ }
+
+ /**
+ * Updates the ACL
+ *
+ * This method will receive a list of new ACE's.
+ *
+ * @param array $acl
+ * @return void
+ */
+ public function setACL(array $acl) {
+
+ throw new DAV\Exception\MethodNotAllowed('Changing ACL is not yet supported');
+
+ }
+
+ /**
+ * Returns the list of supported privileges for this node.
+ *
+ * The returned data structure is a list of nested privileges.
+ * See Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple
+ * standard structure.
+ *
+ * If null is returned from this method, the default privilege set is used,
+ * which is fine for most common usecases.
+ *
+ * @return array|null
+ */
+ public function getSupportedPrivilegeSet() {
+
+ return null;
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/CardDAV/IAddressBook.php b/app/Gdoo/Calendar/Sabre/CardDAV/IAddressBook.php
new file mode 100644
index 00000000..af9e88d8
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CardDAV/IAddressBook.php
@@ -0,0 +1,20 @@
+subscribeEvent('beforeGetProperties', array($this, 'beforeGetProperties'));
+ $server->subscribeEvent('afterGetProperties', array($this, 'afterGetProperties'));
+ $server->subscribeEvent('updateProperties', array($this, 'updateProperties'));
+ $server->subscribeEvent('report', array($this,'report'));
+ $server->subscribeEvent('onHTMLActionsPanel', array($this,'htmlActionsPanel'));
+ $server->subscribeEvent('onBrowserPostAction', array($this,'browserPostAction'));
+ $server->subscribeEvent('beforeWriteContent', array($this, 'beforeWriteContent'));
+ $server->subscribeEvent('beforeCreateFile', array($this, 'beforeCreateFile'));
+
+ /* Namespaces */
+ $server->xmlNamespaces[self::NS_CARDDAV] = 'card';
+
+ /* Mapping Interfaces to {DAV:}resourcetype values */
+ $server->resourceTypeMapping['Sabre\\CardDAV\\IAddressBook'] = '{' . self::NS_CARDDAV . '}addressbook';
+ $server->resourceTypeMapping['Sabre\\CardDAV\\IDirectory'] = '{' . self::NS_CARDDAV . '}directory';
+
+ /* Adding properties that may never be changed */
+ $server->protectedProperties[] = '{' . self::NS_CARDDAV . '}supported-address-data';
+ $server->protectedProperties[] = '{' . self::NS_CARDDAV . '}max-resource-size';
+ $server->protectedProperties[] = '{' . self::NS_CARDDAV . '}addressbook-home-set';
+ $server->protectedProperties[] = '{' . self::NS_CARDDAV . '}supported-collation-set';
+
+ $server->propertyMap['{http://calendarserver.org/ns/}me-card'] = 'Sabre\\DAV\\Property\\Href';
+
+ $this->server = $server;
+
+ }
+
+ /**
+ * Returns a list of supported features.
+ *
+ * This is used in the DAV: header in the OPTIONS and PROPFIND requests.
+ *
+ * @return array
+ */
+ public function getFeatures() {
+
+ return array('addressbook');
+
+ }
+
+ /**
+ * Returns a list of reports this plugin supports.
+ *
+ * This will be used in the {DAV:}supported-report-set property.
+ * Note that you still need to subscribe to the 'report' event to actually
+ * implement them
+ *
+ * @param string $uri
+ * @return array
+ */
+ public function getSupportedReportSet($uri) {
+
+ $node = $this->server->tree->getNodeForPath($uri);
+ if ($node instanceof IAddressBook || $node instanceof ICard) {
+ return array(
+ '{' . self::NS_CARDDAV . '}addressbook-multiget',
+ '{' . self::NS_CARDDAV . '}addressbook-query',
+ );
+ }
+ return array();
+
+ }
+
+
+ /**
+ * Adds all CardDAV-specific properties
+ *
+ * @param string $path
+ * @param DAV\INode $node
+ * @param array $requestedProperties
+ * @param array $returnedProperties
+ * @return void
+ */
+ public function beforeGetProperties($path, DAV\INode $node, array &$requestedProperties, array &$returnedProperties) {
+
+ if ($node instanceof DAVACL\IPrincipal) {
+
+ // calendar-home-set property
+ $addHome = '{' . self::NS_CARDDAV . '}addressbook-home-set';
+ if (in_array($addHome,$requestedProperties)) {
+ $principalId = $node->getName();
+ $addressbookHomePath = self::ADDRESSBOOK_ROOT . '/' . $principalId . '/';
+ unset($requestedProperties[array_search($addHome, $requestedProperties)]);
+ $returnedProperties[200][$addHome] = new DAV\Property\Href($addressbookHomePath);
+ }
+
+ $directories = '{' . self::NS_CARDDAV . '}directory-gateway';
+ if ($this->directories && in_array($directories, $requestedProperties)) {
+ unset($requestedProperties[array_search($directories, $requestedProperties)]);
+ $returnedProperties[200][$directories] = new DAV\Property\HrefList($this->directories);
+ }
+
+ }
+
+ if ($node instanceof ICard) {
+
+ // The address-data property is not supposed to be a 'real'
+ // property, but in large chunks of the spec it does act as such.
+ // Therefore we simply expose it as a property.
+ $addressDataProp = '{' . self::NS_CARDDAV . '}address-data';
+ if (in_array($addressDataProp, $requestedProperties)) {
+ unset($requestedProperties[$addressDataProp]);
+ $val = $node->get();
+ if (is_resource($val))
+ $val = stream_get_contents($val);
+
+ $returnedProperties[200][$addressDataProp] = $val;
+
+ }
+ }
+
+ if ($node instanceof UserAddressBooks) {
+
+ $meCardProp = '{http://calendarserver.org/ns/}me-card';
+ if (in_array($meCardProp, $requestedProperties)) {
+
+ $props = $this->server->getProperties($node->getOwner(), array('{http://sabredav.org/ns}vcard-url'));
+ if (isset($props['{http://sabredav.org/ns}vcard-url'])) {
+
+ $returnedProperties[200][$meCardProp] = new DAV\Property\Href(
+ $props['{http://sabredav.org/ns}vcard-url']
+ );
+ $pos = array_search($meCardProp, $requestedProperties);
+ unset($requestedProperties[$pos]);
+
+ }
+
+ }
+
+ }
+
+ }
+
+ /**
+ * This event is triggered when a PROPPATCH method is executed
+ *
+ * @param array $mutations
+ * @param array $result
+ * @param DAV\INode $node
+ * @return bool
+ */
+ public function updateProperties(&$mutations, &$result, DAV\INode $node) {
+
+ if (!$node instanceof UserAddressBooks) {
+ return true;
+ }
+
+ $meCard = '{http://calendarserver.org/ns/}me-card';
+
+ // The only property we care about
+ if (!isset($mutations[$meCard]))
+ return true;
+
+ $value = $mutations[$meCard];
+ unset($mutations[$meCard]);
+
+ if ($value instanceof DAV\Property\IHref) {
+ $value = $value->getHref();
+ $value = $this->server->calculateUri($value);
+ } elseif (!is_null($value)) {
+ $result[400][$meCard] = null;
+ return false;
+ }
+
+ $innerResult = $this->server->updateProperties(
+ $node->getOwner(),
+ array(
+ '{http://sabredav.org/ns}vcard-url' => $value,
+ )
+ );
+
+ $closureResult = false;
+ foreach($innerResult as $status => $props) {
+ if (is_array($props) && array_key_exists('{http://sabredav.org/ns}vcard-url', $props)) {
+ $result[$status][$meCard] = null;
+ $closureResult = ($status>=200 && $status<300);
+ }
+
+ }
+
+ return $result;
+
+ }
+
+ /**
+ * This functions handles REPORT requests specific to CardDAV
+ *
+ * @param string $reportName
+ * @param \DOMNode $dom
+ * @return bool
+ */
+ public function report($reportName,$dom) {
+
+ switch($reportName) {
+ case '{'.self::NS_CARDDAV.'}addressbook-multiget' :
+ $this->addressbookMultiGetReport($dom);
+ return false;
+ case '{'.self::NS_CARDDAV.'}addressbook-query' :
+ $this->addressBookQueryReport($dom);
+ return false;
+ default :
+ return;
+
+ }
+
+
+ }
+
+ /**
+ * This function handles the addressbook-multiget REPORT.
+ *
+ * This report is used by the client to fetch the content of a series
+ * of urls. Effectively avoiding a lot of redundant requests.
+ *
+ * @param \DOMNode $dom
+ * @return void
+ */
+ public function addressbookMultiGetReport($dom) {
+
+ $properties = array_keys(DAV\XMLUtil::parseProperties($dom->firstChild));
+
+ $hrefElems = $dom->getElementsByTagNameNS('urn:DAV','href');
+ $propertyList = array();
+
+ foreach($hrefElems as $elem) {
+
+ $uri = $this->server->calculateUri($elem->nodeValue);
+ list($propertyList[]) = $this->server->getPropertiesForPath($uri,$properties);
+
+ }
+
+ $prefer = $this->server->getHTTPPRefer();
+
+ $this->server->httpResponse->sendStatus(207);
+ $this->server->httpResponse->setHeader('Content-Type','application/xml; charset=utf-8');
+ $this->server->httpResponse->setHeader('Vary','Brief,Prefer');
+ $this->server->httpResponse->sendBody($this->server->generateMultiStatus($propertyList, $prefer['return-minimal']));
+
+ }
+
+ /**
+ * This method is triggered before a file gets updated with new content.
+ *
+ * This plugin uses this method to ensure that Card nodes receive valid
+ * vcard data.
+ *
+ * @param string $path
+ * @param DAV\IFile $node
+ * @param resource $data
+ * @return void
+ */
+ public function beforeWriteContent($path, DAV\IFile $node, &$data) {
+
+ if (!$node instanceof ICard)
+ return;
+
+ $this->validateVCard($data);
+
+ }
+
+ /**
+ * This method is triggered before a new file is created.
+ *
+ * This plugin uses this method to ensure that Card nodes receive valid
+ * vcard data.
+ *
+ * @param string $path
+ * @param resource $data
+ * @param DAV\ICollection $parentNode
+ * @return void
+ */
+ public function beforeCreateFile($path, &$data, DAV\ICollection $parentNode) {
+
+ if (!$parentNode instanceof IAddressBook)
+ return;
+
+ $this->validateVCard($data);
+
+ }
+
+ /**
+ * Checks if the submitted iCalendar data is in fact, valid.
+ *
+ * An exception is thrown if it's not.
+ *
+ * @param resource|string $data
+ * @return void
+ */
+ protected function validateVCard(&$data) {
+
+ // If it's a stream, we convert it to a string first.
+ if (is_resource($data)) {
+ $data = stream_get_contents($data);
+ }
+
+ // Converting the data to unicode, if needed.
+ $data = DAV\StringUtil::ensureUTF8($data);
+
+ try {
+
+ $vobj = VObject\Reader::read($data);
+
+ } catch (VObject\ParseException $e) {
+
+ throw new DAV\Exception\UnsupportedMediaType('This resource only supports valid vcard data. Parse error: ' . $e->getMessage());
+
+ }
+
+ if ($vobj->name !== 'VCARD') {
+ throw new DAV\Exception\UnsupportedMediaType('This collection can only support vcard objects.');
+ }
+
+ if (!isset($vobj->UID)) {
+ // No UID in vcards is invalid, but we'll just add it in anyway.
+ $vobj->add('UID', DAV\UUIDUtil::getUUID());
+ $data = $vobj->serialize();
+ }
+
+ }
+
+
+ /**
+ * This function handles the addressbook-query REPORT
+ *
+ * This report is used by the client to filter an addressbook based on a
+ * complex query.
+ *
+ * @param \DOMNode $dom
+ * @return void
+ */
+ protected function addressbookQueryReport($dom) {
+
+ $query = new AddressBookQueryParser($dom);
+ $query->parse();
+
+ $depth = $this->server->getHTTPDepth(0);
+
+ if ($depth==0) {
+ $candidateNodes = array(
+ $this->server->tree->getNodeForPath($this->server->getRequestUri())
+ );
+ } else {
+ $candidateNodes = $this->server->tree->getChildren($this->server->getRequestUri());
+ }
+
+ $validNodes = array();
+ foreach($candidateNodes as $node) {
+
+ if (!$node instanceof ICard)
+ continue;
+
+ $blob = $node->get();
+ if (is_resource($blob)) {
+ $blob = stream_get_contents($blob);
+ }
+
+ if (!$this->validateFilters($blob, $query->filters, $query->test)) {
+ continue;
+ }
+
+ $validNodes[] = $node;
+
+ if ($query->limit && $query->limit <= count($validNodes)) {
+ // We hit the maximum number of items, we can stop now.
+ break;
+ }
+
+ }
+
+ $result = array();
+ foreach($validNodes as $validNode) {
+
+ if ($depth==0) {
+ $href = $this->server->getRequestUri();
+ } else {
+ $href = $this->server->getRequestUri() . '/' . $validNode->getName();
+ }
+
+ list($result[]) = $this->server->getPropertiesForPath($href, $query->requestedProperties, 0);
+
+ }
+
+ $prefer = $this->server->getHTTPPRefer();
+
+ $this->server->httpResponse->sendStatus(207);
+ $this->server->httpResponse->setHeader('Content-Type','application/xml; charset=utf-8');
+ $this->server->httpResponse->setHeader('Vary','Brief,Prefer');
+ $this->server->httpResponse->sendBody($this->server->generateMultiStatus($result, $prefer['return-minimal']));
+
+ }
+
+ /**
+ * Validates if a vcard makes it throught a list of filters.
+ *
+ * @param string $vcardData
+ * @param array $filters
+ * @param string $test anyof or allof (which means OR or AND)
+ * @return bool
+ */
+ public function validateFilters($vcardData, array $filters, $test) {
+
+ $vcard = VObject\Reader::read($vcardData);
+
+ if (!$filters) return true;
+
+ foreach($filters as $filter) {
+
+ $isDefined = isset($vcard->{$filter['name']});
+ if ($filter['is-not-defined']) {
+ if ($isDefined) {
+ $success = false;
+ } else {
+ $success = true;
+ }
+ } elseif ((!$filter['param-filters'] && !$filter['text-matches']) || !$isDefined) {
+
+ // We only need to check for existence
+ $success = $isDefined;
+
+ } else {
+
+ $vProperties = $vcard->select($filter['name']);
+
+ $results = array();
+ if ($filter['param-filters']) {
+ $results[] = $this->validateParamFilters($vProperties, $filter['param-filters'], $filter['test']);
+ }
+ if ($filter['text-matches']) {
+ $texts = array();
+ foreach($vProperties as $vProperty)
+ $texts[] = $vProperty->getValue();
+
+ $results[] = $this->validateTextMatches($texts, $filter['text-matches'], $filter['test']);
+ }
+
+ if (count($results)===1) {
+ $success = $results[0];
+ } else {
+ if ($filter['test'] === 'anyof') {
+ $success = $results[0] || $results[1];
+ } else {
+ $success = $results[0] && $results[1];
+ }
+ }
+
+ } // else
+
+ // There are two conditions where we can already determine whether
+ // or not this filter succeeds.
+ if ($test==='anyof' && $success) {
+ return true;
+ }
+ if ($test==='allof' && !$success) {
+ return false;
+ }
+
+ } // foreach
+
+ // If we got all the way here, it means we haven't been able to
+ // determine early if the test failed or not.
+ //
+ // This implies for 'anyof' that the test failed, and for 'allof' that
+ // we succeeded. Sounds weird, but makes sense.
+ return $test==='allof';
+
+ }
+
+ /**
+ * Validates if a param-filter can be applied to a specific property.
+ *
+ * @todo currently we're only validating the first parameter of the passed
+ * property. Any subsequence parameters with the same name are
+ * ignored.
+ * @param array $vProperties
+ * @param array $filters
+ * @param string $test
+ * @return bool
+ */
+ protected function validateParamFilters(array $vProperties, array $filters, $test) {
+
+ foreach($filters as $filter) {
+
+ $isDefined = false;
+ foreach($vProperties as $vProperty) {
+ $isDefined = isset($vProperty[$filter['name']]);
+ if ($isDefined) break;
+ }
+
+ if ($filter['is-not-defined']) {
+ if ($isDefined) {
+ $success = false;
+ } else {
+ $success = true;
+ }
+
+ // If there's no text-match, we can just check for existence
+ } elseif (!$filter['text-match'] || !$isDefined) {
+
+ $success = $isDefined;
+
+ } else {
+
+ $success = false;
+ foreach($vProperties as $vProperty) {
+ // If we got all the way here, we'll need to validate the
+ // text-match filter.
+ $success = DAV\StringUtil::textMatch($vProperty[$filter['name']]->getValue(), $filter['text-match']['value'], $filter['text-match']['collation'], $filter['text-match']['match-type']);
+ if ($success) break;
+ }
+ if ($filter['text-match']['negate-condition']) {
+ $success = !$success;
+ }
+
+ } // else
+
+ // There are two conditions where we can already determine whether
+ // or not this filter succeeds.
+ if ($test==='anyof' && $success) {
+ return true;
+ }
+ if ($test==='allof' && !$success) {
+ return false;
+ }
+
+ }
+
+ // If we got all the way here, it means we haven't been able to
+ // determine early if the test failed or not.
+ //
+ // This implies for 'anyof' that the test failed, and for 'allof' that
+ // we succeeded. Sounds weird, but makes sense.
+ return $test==='allof';
+
+ }
+
+ /**
+ * Validates if a text-filter can be applied to a specific property.
+ *
+ * @param array $texts
+ * @param array $filters
+ * @param string $test
+ * @return bool
+ */
+ protected function validateTextMatches(array $texts, array $filters, $test) {
+
+ foreach($filters as $filter) {
+
+ $success = false;
+ foreach($texts as $haystack) {
+ $success = DAV\StringUtil::textMatch($haystack, $filter['value'], $filter['collation'], $filter['match-type']);
+
+ // Breaking on the first match
+ if ($success) break;
+ }
+ if ($filter['negate-condition']) {
+ $success = !$success;
+ }
+
+ if ($success && $test==='anyof')
+ return true;
+
+ if (!$success && $test=='allof')
+ return false;
+
+
+ }
+
+ // If we got all the way here, it means we haven't been able to
+ // determine early if the test failed or not.
+ //
+ // This implies for 'anyof' that the test failed, and for 'allof' that
+ // we succeeded. Sounds weird, but makes sense.
+ return $test==='allof';
+
+ }
+
+ /**
+ * This event is triggered after webdav-properties have been retrieved.
+ *
+ * @return bool
+ */
+ public function afterGetProperties($uri, &$properties) {
+
+ // If the request was made using the SOGO connector, we must rewrite
+ // the content-type property. By default SabreDAV will send back
+ // text/x-vcard; charset=utf-8, but for SOGO we must strip that last
+ // part.
+ if (!isset($properties[200]['{DAV:}getcontenttype']))
+ return;
+
+ if (strpos($this->server->httpRequest->getHeader('User-Agent'),'Thunderbird')===false) {
+ return;
+ }
+
+ if (strpos($properties[200]['{DAV:}getcontenttype'],'text/x-vcard')===0) {
+ $properties[200]['{DAV:}getcontenttype'] = 'text/x-vcard';
+ }
+
+ }
+
+ /**
+ * This method is used to generate HTML output for the
+ * Sabre\DAV\Browser\Plugin. This allows us to generate an interface users
+ * can use to create new calendars.
+ *
+ * @param DAV\INode $node
+ * @param string $output
+ * @return bool
+ */
+ public function htmlActionsPanel(DAV\INode $node, &$output) {
+
+ if (!$node instanceof UserAddressBooks)
+ return;
+
+ $output.= '
+
';
+
+ return false;
+
+ }
+
+ /**
+ * This method allows us to intercept the 'mkcalendar' sabreAction. This
+ * action enables the user to create new calendars from the browser plugin.
+ *
+ * @param string $uri
+ * @param string $action
+ * @param array $postVars
+ * @return bool
+ */
+ public function browserPostAction($uri, $action, array $postVars) {
+
+ if ($action!=='mkaddressbook')
+ return;
+
+ $resourceType = array('{DAV:}collection','{urn:ietf:params:xml:ns:carddav}addressbook');
+ $properties = array();
+ if (isset($postVars['{DAV:}displayname'])) {
+ $properties['{DAV:}displayname'] = $postVars['{DAV:}displayname'];
+ }
+ $this->server->createCollection($uri . '/' . $postVars['name'],$resourceType,$properties);
+ return false;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CardDAV/Property/SupportedAddressData.php b/app/Gdoo/Calendar/Sabre/CardDAV/Property/SupportedAddressData.php
new file mode 100644
index 00000000..39433234
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CardDAV/Property/SupportedAddressData.php
@@ -0,0 +1,72 @@
+ 'text/vcard', 'version' => '3.0'),
+ // array('contentType' => 'text/vcard', 'version' => '4.0'),
+ );
+ }
+
+ $this->supportedData = $supportedData;
+
+ }
+
+ /**
+ * Serializes the property in a DOMDocument
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $node
+ * @return void
+ */
+ public function serialize(DAV\Server $server,\DOMElement $node) {
+
+ $doc = $node->ownerDocument;
+
+ $prefix =
+ isset($server->xmlNamespaces[CardDAV\Plugin::NS_CARDDAV]) ?
+ $server->xmlNamespaces[CardDAV\Plugin::NS_CARDDAV] :
+ 'card';
+
+ foreach($this->supportedData as $supported) {
+
+ $caldata = $doc->createElementNS(CardDAV\Plugin::NS_CARDDAV, $prefix . ':address-data-type');
+ $caldata->setAttribute('content-type',$supported['contentType']);
+ $caldata->setAttribute('version',$supported['version']);
+ $node->appendChild($caldata);
+
+ }
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CardDAV/UserAddressBooks.php b/app/Gdoo/Calendar/Sabre/CardDAV/UserAddressBooks.php
new file mode 100644
index 00000000..a9d4100b
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CardDAV/UserAddressBooks.php
@@ -0,0 +1,260 @@
+carddavBackend = $carddavBackend;
+ $this->principalUri = $principalUri;
+
+ }
+
+ /**
+ * Returns the name of this object
+ *
+ * @return string
+ */
+ public function getName() {
+
+ list(,$name) = DAV\URLUtil::splitPath($this->principalUri);
+ return $name;
+
+ }
+
+ /**
+ * Updates the name of this object
+ *
+ * @param string $name
+ * @return void
+ */
+ public function setName($name) {
+
+ throw new DAV\Exception\MethodNotAllowed();
+
+ }
+
+ /**
+ * Deletes this object
+ *
+ * @return void
+ */
+ public function delete() {
+
+ throw new DAV\Exception\MethodNotAllowed();
+
+ }
+
+ /**
+ * Returns the last modification date
+ *
+ * @return int
+ */
+ public function getLastModified() {
+
+ return null;
+
+ }
+
+ /**
+ * Creates a new file under this object.
+ *
+ * This is currently not allowed
+ *
+ * @param string $filename
+ * @param resource $data
+ * @return void
+ */
+ public function createFile($filename, $data=null) {
+
+ throw new DAV\Exception\MethodNotAllowed('Creating new files in this collection is not supported');
+
+ }
+
+ /**
+ * Creates a new directory under this object.
+ *
+ * This is currently not allowed.
+ *
+ * @param string $filename
+ * @return void
+ */
+ public function createDirectory($filename) {
+
+ throw new DAV\Exception\MethodNotAllowed('Creating new collections in this collection is not supported');
+
+ }
+
+ /**
+ * Returns a single calendar, by name
+ *
+ * @param string $name
+ * @todo needs optimizing
+ * @return \AddressBook
+ */
+ public function getChild($name) {
+
+ foreach($this->getChildren() as $child) {
+ if ($name==$child->getName())
+ return $child;
+
+ }
+ throw new DAV\Exception\NotFound('Addressbook with name \'' . $name . '\' could not be found');
+
+ }
+
+ /**
+ * Returns a list of addressbooks
+ *
+ * @return array
+ */
+ public function getChildren() {
+
+ $addressbooks = $this->carddavBackend->getAddressbooksForUser($this->principalUri);
+ $objs = array();
+ foreach($addressbooks as $addressbook) {
+ $objs[] = new AddressBook($this->carddavBackend, $addressbook);
+ }
+ return $objs;
+
+ }
+
+ /**
+ * Creates a new addressbook
+ *
+ * @param string $name
+ * @param array $resourceType
+ * @param array $properties
+ * @return void
+ */
+ public function createExtendedCollection($name, array $resourceType, array $properties) {
+
+ if (!in_array('{'.Plugin::NS_CARDDAV.'}addressbook',$resourceType) || count($resourceType)!==2) {
+ throw new DAV\Exception\InvalidResourceType('Unknown resourceType for this collection');
+ }
+ $this->carddavBackend->createAddressBook($this->principalUri, $name, $properties);
+
+ }
+
+ /**
+ * Returns the owner principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getOwner() {
+
+ return $this->principalUri;
+
+ }
+
+ /**
+ * Returns a group principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getGroup() {
+
+ return null;
+
+ }
+
+ /**
+ * Returns a list of ACE's for this node.
+ *
+ * Each ACE has the following properties:
+ * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
+ * currently the only supported privileges
+ * * 'principal', a url to the principal who owns the node
+ * * 'protected' (optional), indicating that this ACE is not allowed to
+ * be updated.
+ *
+ * @return array
+ */
+ public function getACL() {
+
+ return array(
+ array(
+ 'privilege' => '{DAV:}read',
+ 'principal' => $this->principalUri,
+ 'protected' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}write',
+ 'principal' => $this->principalUri,
+ 'protected' => true,
+ ),
+
+ );
+
+ }
+
+ /**
+ * Updates the ACL
+ *
+ * This method will receive a list of new ACE's.
+ *
+ * @param array $acl
+ * @return void
+ */
+ public function setACL(array $acl) {
+
+ throw new DAV\Exception\MethodNotAllowed('Changing ACL is not yet supported');
+
+ }
+
+ /**
+ * Returns the list of supported privileges for this node.
+ *
+ * The returned data structure is a list of nested privileges.
+ * See Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple
+ * standard structure.
+ *
+ * If null is returned from this method, the default privilege set is used,
+ * which is fine for most common usecases.
+ *
+ * @return array|null
+ */
+ public function getSupportedPrivilegeSet() {
+
+ return null;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CardDAV/VCFExportPlugin.php b/app/Gdoo/Calendar/Sabre/CardDAV/VCFExportPlugin.php
new file mode 100644
index 00000000..cf22f265
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CardDAV/VCFExportPlugin.php
@@ -0,0 +1,108 @@
+server = $server;
+ $this->server->subscribeEvent('beforeMethod',array($this,'beforeMethod'), 90);
+
+ }
+
+ /**
+ * 'beforeMethod' event handles. This event handles intercepts GET requests ending
+ * with ?export
+ *
+ * @param string $method
+ * @param string $uri
+ * @return bool
+ */
+ public function beforeMethod($method, $uri) {
+
+ if ($method!='GET') return;
+ if ($this->server->httpRequest->getQueryString()!='export') return;
+
+ // splitting uri
+ list($uri) = explode('?',$uri,2);
+
+ $node = $this->server->tree->getNodeForPath($uri);
+
+ if (!($node instanceof IAddressBook)) return;
+
+ // Checking ACL, if available.
+ if ($aclPlugin = $this->server->getPlugin('acl')) {
+ $aclPlugin->checkPrivileges($uri, '{DAV:}read');
+ }
+
+ $this->server->httpResponse->setHeader('Content-Type','text/directory');
+ $this->server->httpResponse->sendStatus(200);
+
+ $nodes = $this->server->getPropertiesForPath($uri, array(
+ '{' . Plugin::NS_CARDDAV . '}address-data',
+ ),1);
+
+ $this->server->httpResponse->sendBody($this->generateVCF($nodes));
+
+ // Returning false to break the event chain
+ return false;
+
+ }
+
+ /**
+ * Merges all vcard objects, and builds one big vcf export
+ *
+ * @param array $nodes
+ * @return string
+ */
+ public function generateVCF(array $nodes) {
+
+ $output = "";
+
+ foreach($nodes as $node) {
+
+ if (!isset($node[200]['{' . Plugin::NS_CARDDAV . '}address-data'])) {
+ continue;
+ }
+ $nodeData = $node[200]['{' . Plugin::NS_CARDDAV . '}address-data'];
+
+ // Parsing this node so VObject can clean up the output.
+ $output .=
+ VObject\Reader::read($nodeData)->serialize();
+
+ }
+
+ return $output;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/CardDAV/Version.php b/app/Gdoo/Calendar/Sabre/CardDAV/Version.php
new file mode 100644
index 00000000..8ba88ce4
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/CardDAV/Version.php
@@ -0,0 +1,26 @@
+ $username,
+ 'password' => $password
+ ];
+ if (\Auth::attempt($credentials)) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+ }
+
+ /**
+ * Returns information about the currently logged in username.
+ *
+ * If nobody is currently logged in, this method should return null.
+ *
+ * @return string|null
+ */
+ public function getCurrentUser()
+ {
+ if (\Auth::id() > 0) {
+ return \Auth::user()->username;
+ }
+ return null;
+ }
+}
diff --git a/app/Gdoo/Calendar/Sabre/Connector/CalDAV.php b/app/Gdoo/Calendar/Sabre/Connector/CalDAV.php
new file mode 100644
index 00000000..faf060b4
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/Connector/CalDAV.php
@@ -0,0 +1,649 @@
+ 'displayname',
+ '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description',
+ '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'timezone',
+ '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder',
+ '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor',
+ );
+
+ /**
+ * Returns a list of calendars for a principal.
+ *
+ * Every project is an array with the following keys:
+ * * id, a unique id that will be used by other functions to modify the
+ * calendar. This can be the same as the uri or a database key.
+ * * uri, which the basename of the uri with which the calendar is
+ * accessed.
+ * * principaluri. The owner of the calendar. Almost always the same as
+ * principalUri passed to this method.
+ *
+ * Furthermore it can contain webdav properties in clark notation. A very
+ * common one is '{DAV:}displayname'.
+ *
+ * @param string $principalUri
+ * @return array
+ */
+ public function getCalendarsForUser($principalUri)
+ {
+ $fields = array_values($this->propertyMap);
+ $fields[] = 'id';
+ $fields[] = 'uri';
+ $fields[] = 'ctag';
+ $fields[] = 'components';
+ $fields[] = 'principaluri';
+ $fields[] = 'transparent';
+
+ $rows = Calendar::where('principaluri', $principalUri)->get($fields);
+
+ foreach ($rows as $row) {
+ $components = array();
+ if ($row->components) {
+ $components = explode(',', $row->components);
+ }
+
+ $calendar = array(
+ 'id' => $row->id,
+ 'uri' => $row->uri,
+ 'principaluri' => $row->principaluri,
+ '{' . \Sabre\CalDAV\Plugin::NS_CALENDARSERVER . '}getctag' => $row->ctag?$row->ctag:'0',
+ '{' . \Sabre\CalDAV\Plugin::NS_CALDAV . '}supported-calendar-component-set' => new \Sabre\CalDAV\Property\SupportedCalendarComponentSet($components),
+ '{' . \Sabre\CalDAV\Plugin::NS_CALDAV . '}schedule-calendar-transp' => new \Sabre\CalDAV\Property\ScheduleCalendarTransp($row->transparent?'transparent':'opaque'),
+ );
+
+ foreach ($this->propertyMap as $xmlName => $dbName) {
+ $calendar[$xmlName] = $row[$dbName];
+ }
+ $calendars[] = $calendar;
+ }
+ return $calendars;
+ }
+
+ /**
+ * Creates a new calendar for a principal.
+ *
+ * If the creation was a success, an id must be returned that can be used to reference
+ * this calendar in other methods, such as updateCalendar
+ *
+ * @param string $principalUri
+ * @param string $calendarUri
+ * @param array $properties
+ * @return string
+ */
+ public function createCalendar($principalUri, $calendarUri, array $properties)
+ {
+ $fieldNames = array(
+ 'principaluri',
+ 'uri',
+ 'ctag',
+ 'transparent',
+ );
+ $values = array(
+ ':principaluri' => $principalUri,
+ ':uri' => $calendarUri,
+ ':ctag' => 1,
+ ':transparent' => 0,
+ );
+
+ $data = array(
+ 'principaluri' => $principalUri,
+ 'uri' => $calendarUri,
+ 'ctag' => 1,
+ 'transparent' => 0,
+ );
+
+ // Default value
+ $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set';
+ $fieldNames[] = 'components';
+ if (!isset($properties[$sccs])) {
+ $values[':components'] = 'VEVENT,VTODO';
+ } else {
+ if (!($properties[$sccs] instanceof \Sabre\CalDAV\Property\SupportedCalendarComponentSet)) {
+ throw new \Sabre\DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet');
+ }
+ $values[':components'] = implode(',', $properties[$sccs]->getValue());
+ }
+ $transp = '{' . \Sabre\CalDAV\Plugin::NS_CALDAV . '}schedule-calendar-transp';
+ if (isset($properties[$transp])) {
+ $values[':transparent'] = $properties[$transp]->getValue()==='transparent';
+ }
+
+ foreach ($this->propertyMap as $xmlName => $dbName) {
+ if (isset($properties[$xmlName])) {
+ $values[':' . $dbName] = $properties[$xmlName];
+ $fieldNames[] = $dbName;
+
+ $data[$dbName] = $properties[$xmlName];
+ }
+ }
+ return Calendar::insertGetId($data);
+ }
+
+ /**
+ * Updates properties for a calendar.
+ *
+ * The mutations array uses the propertyName in clark-notation as key,
+ * and the array value for the property value. In the case a property
+ * should be deleted, the property value will be null.
+ *
+ * This method must be atomic. If one property cannot be changed, the
+ * entire operation must fail.
+ *
+ * If the operation was successful, true can be returned.
+ * If the operation failed, false can be returned.
+ *
+ * Deletion of a non-existent property is always successful.
+ *
+ * Lastly, it is optional to return detailed information about any
+ * failures. In this case an array should be returned with the following
+ * structure:
+ *
+ * array(
+ * 403 => array(
+ * '{DAV:}displayname' => null,
+ * ),
+ * 424 => array(
+ * '{DAV:}owner' => null,
+ * )
+ * )
+ *
+ * In this example it was forbidden to update {DAV:}displayname.
+ * (403 Forbidden), which in turn also caused {DAV:}owner to fail
+ * (424 Failed Dependency) because the request needs to be atomic.
+ *
+ * @param string $calendarId
+ * @param array $mutations
+ * @return bool|array
+ */
+ public function updateCalendar($calendarId, array $mutations)
+ {
+ $newValues = array();
+ $result = array(
+ 200 => array(), // Ok
+ 403 => array(), // Forbidden
+ 424 => array(), // Failed Dependency
+ );
+
+ $hasError = false;
+
+ foreach ($mutations as $propertyName => $propertyValue) {
+ switch ($propertyName) {
+ case '{' . \Sabre\CalDAV\Plugin::NS_CALDAV . '}schedule-calendar-transp':
+ $fieldName = 'transparent';
+ $newValues[$fieldName] = $propertyValue->getValue()==='transparent';
+ break;
+ default:
+ // Checking the property map
+ if (!isset($this->propertyMap[$propertyName])) {
+ // We don't know about this property.
+ $hasError = true;
+ $result[403][$propertyName] = null;
+ unset($mutations[$propertyName]);
+ continue;
+ }
+
+ $fieldName = $this->propertyMap[$propertyName];
+ $newValues[$fieldName] = $propertyValue;
+ }
+ }
+
+ // If there were any errors we need to fail the request
+ if ($hasError) {
+ // Properties has the remaining properties
+ foreach ($mutations as $propertyName => $propertyValue) {
+ $result[424][$propertyName] = null;
+ }
+
+ // Removing unused statuscodes for cleanliness
+ foreach ($result as $status => $properties) {
+ if (is_array($properties) && count($properties)===0) {
+ unset($result[$status]);
+ }
+ }
+
+ return $result;
+ }
+
+ // Success
+
+ // Now we're generating the sql query.
+ Calendar::where('id', $calendarId)->update($newValues);
+ CalendarService::touchCalendar($calendarId);
+ return true;
+ }
+
+ /**
+ * Delete a calendar and all it's objects
+ *
+ * @param string $calendarId
+ * @return void
+ */
+ public function deleteCalendar($calendarId)
+ {
+ CalendarService::deleteCalendar($calendarId);
+ }
+
+ /**
+ * Returns all calendar objects within a calendar.
+ *
+ * Every item contains an array with the following keys:
+ * * id - unique identifier which will be used for subsequent updates
+ * * calendardata - The iCalendar-compatible calendar data
+ * * uri - a unique key which will be used to construct the uri. This can be any arbitrary string.
+ * * lastmodified - a timestamp of the last modification time
+ * * etag - An arbitrary string, surrounded by double-quotes. (e.g.:
+ * ' "abcdef"')
+ * * calendarid - The calendarid as it was passed to this function.
+ * * size - The size of the calendar objects, in bytes.
+ *
+ * Note that the etag is optional, but it's highly encouraged to return for
+ * speed reasons.
+ *
+ * The calendardata is also optional. If it's not returned
+ * 'getCalendarObject' will be called later, which *is* expected to return
+ * calendardata.
+ *
+ * If neither etag or size are specified, the calendardata will be
+ * used/fetched to determine these numbers. If both are specified the
+ * amount of times this is needed is reduced by a great degree.
+ *
+ * @param string $calendarId
+ * @return array
+ */
+ public function getCalendarObjects($calendarId)
+ {
+ $model = CalendarObject::select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size']);
+ if ($calendarId == 'share-events') {
+ $shared = ShareService::getItemsSourceBy(['event'], \Auth::id());
+ if ($shared->isEmpty()) {
+ return array();
+ }
+ $share_id = Arr::pluck($shared, 'source_id');
+ $rows = $model->whereIn('id', $share_id)->get();
+ foreach ($rows as $key => $row) {
+ $rows[$key]['calendarid'] = 'share-events';
+ }
+ } elseif ($calendarId > 0) {
+ $rows = $model->where('calendarid', $calendarId)->get();
+ }
+
+ $result = array();
+ foreach ($rows as $row) {
+ $result[] = array(
+ 'id' => $row->id,
+ 'uri' => $row->uri,
+ 'lastmodified' => $row->lastmodified,
+ 'etag' => '"' . $row->etag . '"',
+ 'calendarid' => $row->calendarid,
+ 'size' => (int)$row->size,
+ );
+ }
+ return $result;
+ }
+
+ /**
+ * Returns information from a single calendar object, based on it's object
+ * uri.
+ *
+ * The returned array must have the same keys as getCalendarObjects. The
+ * 'calendardata' object is required here though, while it's not required
+ * for getCalendarObjects.
+ *
+ * This method must return null if the object did not exist.
+ *
+ * @param string $calendarId
+ * @param string $objectUri
+ * @return array|null
+ */
+ public function getCalendarObject($calendarId, $objectUri)
+ {
+ $model = CalendarObject::where('uri', $objectUri);
+ if ($calendarId == 'share-events') {
+ $shared = ShareService::getItemsSourceBy(['event'], \Auth::id());
+ if ($shared->isEmpty()) {
+ return array();
+ }
+
+ $shareId = Arr::pluck($shared, 'source_id');
+ $shareUsersList = Arr::pluck($shared, 'name', 'source_id');
+
+ $row = $model->whereIn('id', $shareId)->first();
+ $row->calendarid = 'share-events';
+
+ $vcalendar = \Sabre\VObject\Reader::read($row->calendardata);
+ $vcalendar->VEvent->SUMMARY->value = '['.$shareUsersList[$row->id].']'.$vcalendar->VEvent->SUMMARY->value;
+ $row->calendardata = $vcalendar->serialize();
+ } elseif ($calendarId > 0) {
+ $row = $model->where('calendarid', $calendarId)->first();
+ }
+
+ if (empty($row)) {
+ return array();
+ }
+ return array(
+ 'id' => $row->id,
+ 'uri' => $row->uri,
+ 'lastmodified' => $row->lastmodified,
+ 'etag' => '"' . $row->etag . '"',
+ 'calendarid' => $row->calendarid,
+ 'size' => (int)$row->size,
+ 'calendardata' => $row->calendardata,
+ );
+ }
+
+ /**
+ * Creates a new calendar object.
+ *
+ * It is possible return an etag from this function, which will be used in
+ * the response to this PUT request. Note that the ETag must be surrounded
+ * by double-quotes.
+ *
+ * However, you should only really return this ETag if you don't mangle the
+ * calendar-data. If the result of a subsequent GET to this object is not
+ * the exact same as this request body, you should omit the ETag.
+ *
+ * @param mixed $calendarId
+ * @param string $objectUri
+ * @param string $calendarData
+ * @return string|null
+ */
+ public function createCalendarObject($calendarId, $objectUri, $calendarData)
+ {
+ $extraData = $this->getDenormalizedData($calendarData);
+ $data = array(
+ 'calendarid' => $calendarId,
+ 'uri' => $objectUri,
+ 'calendardata' => $calendarData,
+ 'lastmodified' => time(),
+ 'etag' => $extraData['etag'],
+ 'size' => $extraData['size'],
+ 'componenttype' => $extraData['componentType'],
+ 'firstoccurence' => $extraData['firstOccurence'],
+ 'lastoccurence' => $extraData['lastOccurence'],
+ );
+ CalendarObject::insert($data);
+ CalendarService::touchCalendar($calendarId);
+ return '"' . $extraData['etag'] . '"';
+ }
+
+ /**
+ * Updates an existing calendarobject, based on it's uri.
+ *
+ * It is possible return an etag from this function, which will be used in
+ * the response to this PUT request. Note that the ETag must be surrounded
+ * by double-quotes.
+ *
+ * However, you should only really return this ETag if you don't mangle the
+ * calendar-data. If the result of a subsequent GET to this object is not
+ * the exact same as this request body, you should omit the ETag.
+ *
+ * @param mixed $calendarId
+ * @param string $objectUri
+ * @param string $calendarData
+ * @return string|null
+ */
+ public function updateCalendarObject($calendarId, $objectUri, $calendarData)
+ {
+ $extraData = $this->getDenormalizedData($calendarData);
+ $update = array(
+ 'calendardata' => $calendarData,
+ 'lastmodified' => time(),
+ 'etag' => $extraData['etag'],
+ 'size' => $extraData['size'],
+ 'componenttype' => $extraData['componentType'],
+ 'firstoccurence' => $extraData['firstOccurence'],
+ 'lastoccurence' => $extraData['lastOccurence'],
+ );
+ CalendarObject::where('calendarid', $calendarId)->where('uri', $objectUri)->update($update);
+ CalendarService::touchCalendar($calendarId);
+ return '"' . $extraData['etag'] . '"';
+ }
+
+ /**
+ * Parses some information from calendar objects, used for optimized
+ * calendar-queries.
+ *
+ * Returns an array with the following keys:
+ * * etag
+ * * size
+ * * componentType
+ * * firstOccurence
+ * * lastOccurence
+ *
+ * @param string $calendarData
+ * @return array
+ */
+ protected function getDenormalizedData($calendarData)
+ {
+ $vObject = \Sabre\VObject\Reader::read($calendarData);
+ $componentType = null;
+ $component = null;
+ $firstOccurence = null;
+ $lastOccurence = null;
+ foreach ($vObject->getComponents() as $component) {
+ if ($component->name!=='VTIMEZONE') {
+ $componentType = $component->name;
+ break;
+ }
+ }
+ if (!$componentType) {
+ throw new \Sabre\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component');
+ }
+ if ($componentType === 'VEVENT') {
+ $firstOccurence = $component->DTSTART->getDateTime()->getTimeStamp();
+ // Finding the last occurence is a bit harder
+ if (!isset($component->RRULE)) {
+ if (isset($component->DTEND)) {
+ $lastOccurence = $component->DTEND->getDateTime()->getTimeStamp();
+ } elseif (isset($component->DURATION)) {
+ $endDate = clone $component->DTSTART->getDateTime();
+ $endDate->add(\Sabre\VObject\DateTimeParser::parse($component->DURATION->getValue()));
+ $lastOccurence = $endDate->getTimeStamp();
+ } elseif (!$component->DTSTART->hasTime()) {
+ $endDate = clone $component->DTSTART->getDateTime();
+ $endDate->modify('+1 day');
+ $lastOccurence = $endDate->getTimeStamp();
+ } else {
+ $lastOccurence = $firstOccurence;
+ }
+ } else {
+ $it = new \Sabre\VObject\RecurrenceIterator($vObject, (string)$component->UID);
+ $maxDate = new \DateTime(self::MAX_DATE);
+ if ($it->isInfinite()) {
+ $lastOccurence = $maxDate->getTimeStamp();
+ } else {
+ $end = $it->getDtEnd();
+ while ($it->valid() && $end < $maxDate) {
+ $end = $it->getDtEnd();
+ $it->next();
+ }
+ $lastOccurence = $end->getTimeStamp();
+ }
+ }
+ }
+
+ return array(
+ 'etag' => md5($calendarData),
+ 'size' => strlen($calendarData),
+ 'componentType' => $componentType,
+ 'firstOccurence' => $firstOccurence,
+ 'lastOccurence' => $lastOccurence,
+ );
+ }
+
+ /**
+ * Deletes an existing calendar object.
+ *
+ * @param string $calendarId
+ * @param string $objectUri
+ * @return void
+ */
+ public function deleteCalendarObject($calendarId, $objectUri)
+ {
+ CalendarObject::where('calendarid', $calendarId)->where('uri', $objectUri)->delete();
+ CalendarService::touchCalendar($calendarId);
+ }
+
+ /**
+ * Performs a calendar-query on the contents of this calendar.
+ *
+ * The calendar-query is defined in RFC4791 : CalDAV. Using the
+ * calendar-query it is possible for a client to request a specific set of
+ * object, based on contents of iCalendar properties, date-ranges and
+ * iCalendar component types (VTODO, VEVENT).
+ *
+ * This method should just return a list of (relative) urls that match this
+ * query.
+ *
+ * The list of filters are specified as an array. The exact array is
+ * documented by \Sabre\CalDAV\CalendarQueryParser.
+ *
+ * Note that it is extremely likely that getCalendarObject for every path
+ * returned from this method will be called almost immediately after. You
+ * may want to anticipate this to speed up these requests.
+ *
+ * This method provides a default implementation, which parses *all* the
+ * iCalendar objects in the specified calendar.
+ *
+ * This default may well be good enough for personal use, and calendars
+ * that aren't very large. But if you anticipate high usage, big calendars
+ * or high loads, you are strongly adviced to optimize certain paths.
+ *
+ * The best way to do so is override this method and to optimize
+ * specifically for 'common filters'.
+ *
+ * Requests that are extremely common are:
+ * * requests for just VEVENTS
+ * * requests for just VTODO
+ * * requests with a time-range-filter on a VEVENT.
+ *
+ * ..and combinations of these requests. It may not be worth it to try to
+ * handle every possible situation and just rely on the (relatively
+ * easy to use) CalendarQueryValidator to handle the rest.
+ *
+ * Note that especially time-range-filters may be difficult to parse. A
+ * time-range filter specified on a VEVENT must for instance also handle
+ * recurrence rules correctly.
+ * A good example of how to interprete all these filters can also simply
+ * be found in \Sabre\CalDAV\CalendarQueryFilter. This class is as correct
+ * as possible, so it gives you a good idea on what type of stuff you need
+ * to think of.
+ *
+ * This specific implementation (for the PDO) backend optimizes filters on
+ * specific components, and VEVENT time-ranges.
+ *
+ * @param string $calendarId
+ * @param array $filters
+ * @return array
+ */
+ public function calendarQuery($calendarId, array $filters)
+ {
+ $result = array();
+ $validator = new \Sabre\CalDAV\CalendarQueryValidator();
+
+ $componentType = null;
+ $requirePostFilter = true;
+ $timeRange = null;
+
+ // if no filters were specified, we don't need to filter after a query
+ if (!$filters['prop-filters'] && !$filters['comp-filters']) {
+ $requirePostFilter = false;
+ }
+
+ // Figuring out if there's a component filter
+ if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) {
+ $componentType = $filters['comp-filters'][0]['name'];
+
+ // Checking if we need post-filters
+ if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) {
+ $requirePostFilter = false;
+ }
+ // There was a time-range filter
+ if ($componentType == 'VEVENT' && isset($filters['comp-filters'][0]['time-range'])) {
+ $timeRange = $filters['comp-filters'][0]['time-range'];
+
+ // If start time OR the end time is not specified, we can do a
+ // 100% accurate mysql query.
+ if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) {
+ $requirePostFilter = false;
+ }
+ }
+ }
+
+ if ($requirePostFilter) {
+ $model = CalendarObject::select(array('uri', 'calendardata'));
+ } else {
+ $model = CalendarObject::select(array('uri'));
+ }
+
+ if ($calendarId == 'share-events') {
+ $shared = ShareService::getItemsSourceBy(['event'], \Auth::id());
+ $shareId = Arr::pluck($shared, 'source_id');
+ $model->whereIn('id', $shareId);
+ } else {
+ $model->where('calendarid', $calendarId);
+ }
+
+ if ($componentType) {
+ $model->where('componenttype', $componentType);
+ }
+
+ if ($timeRange && $timeRange['start']) {
+ $model->where('lastoccurence', '>', $timeRange['start']->getTimeStamp());
+ }
+ if ($timeRange && $timeRange['end']) {
+ $model->where('firstoccurence', '<', $timeRange['end']->getTimeStamp());
+ }
+ $rows = $model->get()->toArray();
+ foreach ($rows as $row) {
+ if ($requirePostFilter) {
+ if (!$this->validateFilterForObject($row, $filters)) {
+ continue;
+ }
+ }
+ $result[] = $row['uri'];
+ }
+ return $result;
+ }
+}
diff --git a/app/Gdoo/Calendar/Sabre/Connector/Principal.php b/app/Gdoo/Calendar/Sabre/Connector/Principal.php
new file mode 100644
index 00000000..1d862bcb
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/Connector/Principal.php
@@ -0,0 +1,406 @@
+ array(
+ 'dbField' => 'displayname',
+ ),
+
+ /**
+ * This property is actually used by the CardDAV plugin, where it gets
+ * mapped to {http://calendarserver.orgi/ns/}me-card.
+ *
+ * The reason we don't straight-up use that property, is because
+ * me-card is defined as a property on the users' addressbook
+ * collection.
+ */
+ '{http://sabredav.org/ns}vcard-url' => array(
+ 'dbField' => 'vcardurl',
+ ),
+ /**
+ * This is the users' primary email-address.
+ */
+ '{http://sabredav.org/ns}email-address' => array(
+ 'dbField' => 'email',
+ ),
+ );
+
+ /**
+ * Returns a list of principals based on a prefix.
+ *
+ * This prefix will often contain something like 'principals'. You are only
+ * expected to return principals that are in this base path.
+ *
+ * You are expected to return at least a 'uri' for every user, you can
+ * return any additional properties if you wish so. Common properties are:
+ * {DAV:}displayname
+ * {http://sabredav.org/ns}email-address - This is a custom SabreDAV
+ * field that's actualy injected in a number of other properties. If
+ * you have an email address, use this property.
+ *
+ * @param string $prefixPath
+ * @return array
+ */
+ public function getPrincipalsByPrefix($prefixPath)
+ {
+ if ($prefixPath == 'principals') {
+ $users = User::where('status', 1)
+ ->where('id', \Auth::id())
+ ->get(['id', 'username', 'name', 'email']);
+
+ foreach ($users as $user) {
+ $principals[] = array(
+ 'id' => $user['id'],
+ 'uri' => 'principals/'.$user['username'],
+ '{DAV:}displayname' => $user['name'],
+ '{http://sabredav.org/ns}email-address' => $user['email'],
+ );
+ }
+ }
+ return $principals;
+ }
+
+ /**
+ * Returns a specific principal, specified by it's path.
+ * The returned structure should be the exact same as from
+ * getPrincipalsByPrefix.
+ *
+ * @param string $path
+ * @return array
+ */
+ public function getPrincipalByPath($path)
+ {
+ list($prefix, $username) = explode('/', $path);
+
+ $user = User::where('username', $username)->first(['id', 'username', 'name', 'email']);
+
+ if ($prefix == 'principals') {
+ return array(
+ 'id' => $user['id'],
+ 'uri' => 'principals/'.$user['username'],
+ '{DAV:}displayname' => $user['name'],
+ '{http://sabredav.org/ns}email-address' => $user['email'],
+ );
+ }
+
+ $fields = array(
+ 'id',
+ 'uri',
+ );
+
+ foreach ($this->fieldMap as $key => $value) {
+ $fields[] = $value['dbField'];
+ }
+ $stmt = $this->pdo->prepare('SELECT '.implode(',', $fields).' FROM '. $this->tableName . ' WHERE uri = ?');
+ $stmt->execute(array($path));
+
+ $row = $stmt->fetch(\PDO::FETCH_ASSOC);
+ if (!$row) {
+ return;
+ }
+
+ $principal = array(
+ 'id' => $row['id'],
+ 'uri' => $row['uri'],
+ );
+ foreach ($this->fieldMap as $key => $value) {
+ if ($row[$value['dbField']]) {
+ $principal[$key] = $row[$value['dbField']];
+ }
+ }
+ return $principal;
+ }
+
+ /**
+ * Updates one ore more webdav properties on a principal.
+ *
+ * The list of mutations is supplied as an array. Each key in the array is
+ * a propertyname, such as {DAV:}displayname.
+ *
+ * Each value is the actual value to be updated. If a value is null, it
+ * must be deleted.
+ *
+ * This method should be atomic. It must either completely succeed, or
+ * completely fail. Success and failure can simply be returned as 'true' or
+ * 'false'.
+ *
+ * It is also possible to return detailed failure information. In that case
+ * an array such as this should be returned:
+ *
+ * array(
+ * 200 => array(
+ * '{DAV:}prop1' => null,
+ * ),
+ * 201 => array(
+ * '{DAV:}prop2' => null,
+ * ),
+ * 403 => array(
+ * '{DAV:}prop3' => null,
+ * ),
+ * 424 => array(
+ * '{DAV:}prop4' => null,
+ * ),
+ * );
+ *
+ * In this previous example prop1 was successfully updated or deleted, and
+ * prop2 was succesfully created.
+ *
+ * prop3 failed to update due to '403 Forbidden' and because of this prop4
+ * also could not be updated with '424 Failed dependency'.
+ *
+ * This last example was actually incorrect. While 200 and 201 could appear
+ * in 1 response, if there's any error (403) the other properties should
+ * always fail with 423 (failed dependency).
+ *
+ * But anyway, if you don't want to scratch your head over this, just
+ * return true or false.
+ *
+ * @param string $path
+ * @param array $mutations
+ * @return array|bool
+ */
+ public function updatePrincipal($path, $mutations)
+ {
+ $updateAble = array();
+ foreach ($mutations as $key => $value) {
+
+ // We are not aware of this field, we must fail.
+ if (!isset($this->fieldMap[$key])) {
+ $response = array(
+ 403 => array(
+ $key => null,
+ ),
+ 424 => array(),
+ );
+
+ // Adding the rest to the response as a 424
+ foreach ($mutations as $subKey => $subValue) {
+ if ($subKey !== $key) {
+ $response[424][$subKey] = null;
+ }
+ }
+ return $response;
+ }
+
+ $updateAble[$this->fieldMap[$key]['dbField']] = $value;
+ }
+
+ // No fields to update
+ $query = "UPDATE " . $this->tableName . " SET ";
+
+ $first = true;
+ foreach ($updateAble as $key => $value) {
+ if (!$first) {
+ $query.= ', ';
+ }
+ $first = false;
+ $query.= "$key = :$key ";
+ }
+ $query.='WHERE uri = :uri';
+ $stmt = $this->pdo->prepare($query);
+ $updateAble['uri'] = $path;
+ $stmt->execute($updateAble);
+
+ return true;
+ }
+
+ /**
+ * This method is used to search for principals matching a set of
+ * properties.
+ *
+ * This search is specifically used by RFC3744's principal-property-search
+ * REPORT. You should at least allow searching on
+ * http://sabredav.org/ns}email-address.
+ *
+ * The actual search should be a unicode-non-case-sensitive search. The
+ * keys in searchProperties are the WebDAV property names, while the values
+ * are the property values to search on.
+ *
+ * If multiple properties are being searched on, the search should be
+ * AND'ed.
+ *
+ * This method should simply return an array with full principal uri's.
+ *
+ * If somebody attempted to search on a property the backend does not
+ * support, you should simply return 0 results.
+ *
+ * You can also just return 0 results if you choose to not support
+ * searching at all, but keep in mind that this may stop certain features
+ * from working.
+ *
+ * @param string $prefixPath
+ * @param array $searchProperties
+ * @return array
+ */
+ public function searchPrincipals($prefixPath, array $searchProperties)
+ {
+ $query = 'SELECT uri FROM ' . $this->tableName . ' WHERE 1=1 ';
+ $values = array();
+ foreach ($searchProperties as $property => $value) {
+ switch ($property) {
+ case '{DAV:}displayname':
+ $query.=' AND displayname LIKE ?';
+ $values[] = '%' . $value . '%';
+ break;
+ case '{http://sabredav.org/ns}email-address':
+ $query.=' AND email LIKE ?';
+ $values[] = '%' . $value . '%';
+ break;
+ default:
+ // Unsupported property
+ return array();
+ }
+ }
+ $stmt = $this->pdo->prepare($query);
+ $stmt->execute($values);
+
+ $principals = array();
+ while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
+ // Checking if the principal is in the prefix
+ list($rowPrefix) = DAV\URLUtil::splitPath($row['uri']);
+ if ($rowPrefix !== $prefixPath) {
+ continue;
+ }
+
+ $principals[] = $row['uri'];
+ }
+
+ return $principals;
+ }
+
+ /**
+ * Returns the list of members for a group-principal
+ *
+ * @param string $principal
+ * @return array
+ */
+ public function getGroupMemberSet($principal)
+ {
+ $principal = $this->getPrincipalByPath($principal);
+ if (!$principal) {
+ throw new DAV\Exception('Principal not found');
+ }
+
+ $stmt = $this->pdo->prepare('SELECT principals.uri as uri FROM '.$this->groupMembersTableName.' AS groupmembers LEFT JOIN '.$this->tableName.' AS principals ON groupmembers.member_id = principals.id WHERE groupmembers.principal_id = ?');
+ $stmt->execute(array($principal['id']));
+
+ $result = array();
+ while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
+ $result[] = $row['uri'];
+ }
+ return $result;
+ }
+
+ /**
+ * Returns the list of groups a principal is a member of
+ *
+ * @param string $principal
+ * @return array
+ */
+ public function getGroupMembership($principal)
+ {
+ return array();
+
+ $principal = $this->getPrincipalByPath($principal);
+ if (!$principal) {
+ throw new DAV\Exception('Principal not found');
+ }
+
+ $stmt = $this->pdo->prepare('SELECT principals.uri as uri FROM '.$this->groupMembersTableName.' AS groupmembers LEFT JOIN '.$this->tableName.' AS principals ON groupmembers.principal_id = principals.id WHERE groupmembers.member_id = ?');
+ $stmt->execute(array($principal['id']));
+
+ $result = array();
+ while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
+ $result[] = $row['uri'];
+ }
+ return $result;
+ }
+
+ /**
+ * Updates the list of group members for a group principal.
+ *
+ * The principals should be passed as a list of uri's.
+ *
+ * @param string $principal
+ * @param array $members
+ * @return void
+ */
+ public function setGroupMemberSet($principal, array $members)
+ {
+ // Grabbing the list of principal id's.
+ $stmt = $this->pdo->prepare('SELECT id, uri FROM '.$this->tableName.' WHERE uri IN (? ' . str_repeat(', ? ', count($members)) . ');');
+ $stmt->execute(array_merge(array($principal), $members));
+
+ $memberIds = array();
+ $principalId = null;
+
+ while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
+ if ($row['uri'] == $principal) {
+ $principalId = $row['id'];
+ } else {
+ $memberIds[] = $row['id'];
+ }
+ }
+ if (!$principalId) {
+ throw new DAV\Exception('Principal not found');
+ }
+
+ // Wiping out old members
+ $stmt = $this->pdo->prepare('DELETE FROM '.$this->groupMembersTableName.' WHERE principal_id = ?;');
+ $stmt->execute(array($principalId));
+
+ foreach ($memberIds as $memberId) {
+ $stmt = $this->pdo->prepare('INSERT INTO '.$this->groupMembersTableName.' (principal_id, member_id) VALUES (?, ?);');
+ $stmt->execute(array($principalId, $memberId));
+ }
+ }
+}
diff --git a/app/Gdoo/Calendar/Sabre/Connector/Share/CalDAV.php b/app/Gdoo/Calendar/Sabre/Connector/Share/CalDAV.php
new file mode 100644
index 00000000..1a1b49b9
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/Connector/Share/CalDAV.php
@@ -0,0 +1,396 @@
+ '共享事件',
+ 'color' => '#999999',
+ );
+
+ /**
+ * Updates the list of shares.
+ *
+ * The first array is a list of people that are to be added to the
+ * calendar.
+ *
+ * Every element in the add array has the following properties:
+ * * href - A url. Usually a mailto: address
+ * * commonName - Usually a first and last name, or false
+ * * summary - A description of the share, can also be false
+ * * readOnly - A boolean value
+ *
+ * Every element in the remove array is just the address string.
+ *
+ * Note that if the calendar is currently marked as 'not shared' by and
+ * this method is called, the calendar should be 'upgraded' to a shared
+ * calendar.
+ *
+ * @param mixed $calendarId
+ * @param array $add
+ * @param array $remove
+ * @return void
+ */
+ public function updateShares($calendarId, array $add, array $remove)
+ {
+ $fields = array();
+ $fields[':calendarId'] = $calendarId;
+ // get the principal based on the supplied email address
+ $principal = $this->getPrincipalByEmail($add[0]['href']);
+ $fields[':member'] = $principal['id'];
+ $fields[':status'] = \Sabre\CalDAV\SharingPlugin::STATUS_NORESPONSE;
+ // check we have all the required fields
+ foreach ($this->sharesProperties as $field) {
+ if (isset($add[0][$field])) {
+ $fields[':'.$field] = $add[0][$field];
+ }
+ }
+ $stmt = $this->pdo->prepare("INSERT INTO ".$this->calendarSharesTableName." (".implode(', ', $this->sharesProperties).") VALUES (".implode(', ', array_keys($fields)).")");
+ $stmt->execute($fields);
+
+ // are we removing any shares?
+ if (count($remove)>0) {
+ $r_ids = array();
+ foreach ($remove as $r_mailto) {
+ // get the principalid
+ $r_principal = $this->getPrincipalByEmail($r_mailto);
+ $r_ids[] = $r_principal['id'];
+ }
+ $stmt = $this->pdo->prepare("DELETE FROM ".$this->calendarSharesTableName." WHERE MEMBER = ?");
+ $stmt->execute($r_ids);
+ }
+ }
+
+ /**
+ * Returns the list of people whom this calendar is shared with.
+ *
+ * Every element in this array should have the following properties:
+ * * href - Often a mailto: address
+ * * commonName - Optional, for example a first + last name
+ * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
+ * * readOnly - boolean
+ * * summary - Optional, a description for the share
+ *
+ * @return array
+ */
+ public function getShares($calendarId)
+ {
+
+ //$fields = implode(',', $this->sharesProperties);
+ //$shareRows = Share::getItemsSourceId('calendar',$calendarId);
+
+ //print_r($shareRows);
+ //$stmt = $this->pdo->prepare("SELECT * FROM ".$this->calendarSharesTableName." AS calendarShares LEFT JOIN ".$this->principalsTableName." AS principals ON calendarShares.member = principals.id WHERE calendarShares.calendarId = ? ORDER BY calendarShares.calendarId ASC");
+ //$stmt->execute(array($calendarId));
+
+ $shareRows = array();
+
+ $shares = array();
+ foreach ($shareRows as $row) {
+ //while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
+ $share = array(
+ 'calendarId' => $row['calendarId'],
+ 'principalPath' => $row['uri'],
+ 'readOnly' => $row['readonly'],
+ 'summary' => $row['summary'],
+ 'href' => $row['email'],
+ 'commonName' => $row['displayname'],
+ 'displayName' => $row['displayname'],
+ 'status' => $row['status']
+ /*
+ 'colour'=>$row['colour'],
+ 'displayName'=>$row['displayName'],
+ */
+ );
+ // map the status integer to a predefined constant
+ /*
+ switch($row['status']) {
+ case 1:
+ $share['status'] = 'STATUS_ACCEPTED';
+ break;
+ case 2:
+ $share['status'] = 'STATUS_DECLINED';
+ break;
+ case 3:
+ $share['status'] = 'STATUS_DELETED';
+ break;
+ case 4:
+ $share['status'] = 'STATUS_NORESPONSE';
+ break;
+ case 5:
+ $share['status'] = 'STATUS_INVALID';
+ break;
+ }*/
+ // add it to main array
+ $shares[] = $share;
+ }
+ return $shares;
+ }
+
+ /**
+ * Returns a list of calendars for a principal.
+ *
+ * Every project is an array with the following keys:
+ * * id, a unique id that will be used by other functions to modify the
+ * calendar. This can be the same as the uri or a database key.
+ * * uri, which the basename of the uri with which the calendar is
+ * accessed.
+ * * principaluri. The owner of the calendar. Almost always the same as
+ * principalUri passed to this method.
+ *
+ * Furthermore it can contain webdav properties in clark notation. A very
+ * common one is '{DAV:}displayname'.
+ *
+ * MODIFIED: THIS METHOD NOW NEEDS TO BE ABLE TO RETRIEVE SHARED CALENDARS
+ *
+ * @param string $principalUri
+ * @return array
+ */
+ public function getCalendarsForUser($principalUri)
+ {
+ // get the principal id
+ $principalBackend = $this->getPrincipalBackend();
+ $principal = $principalBackend->getPrincipalByPath($principalUri);
+
+ $fields = array_values($this->propertyMap);
+ $fields[] = 'id';
+ $fields[] = 'uri';
+ $fields[] = 'ctag';
+ $fields[] = 'components';
+ $fields[] = 'principaluri';
+ $fields[] = 'transparent';
+
+ $uri = 'principals/'.$principal['id'];
+
+ // Making fields a comma-delimited list
+ $rows = Calendar::where('principaluri', $uri)->get($fields);
+
+ foreach ($rows as $row) {
+ $components = array();
+ if ($row->components) {
+ $components = explode(',', $row->components);
+ }
+ $calendar = array(
+ 'id' => $row->id,
+ 'uri' => $row->uri,
+ 'principaluri' => $principalUri,
+ '{' . \Sabre\CalDAV\Plugin::NS_CALENDARSERVER . '}getctag' => $row->ctag ? $row->ctag : '0',
+ '{' . \Sabre\CalDAV\Plugin::NS_CALDAV . '}supported-calendar-component-set' => new \Sabre\CalDAV\Property\SupportedCalendarComponentSet($components),
+ '{' . \Sabre\CalDAV\Plugin::NS_CALDAV . '}schedule-calendar-transp' => new \Sabre\CalDAV\Property\ScheduleCalendarTransp($row->transparent ? 'transparent' : 'opaque'),
+ );
+ foreach ($this->propertyMap as $xmlName => $dbName) {
+ $calendar[$xmlName] = $row[$dbName];
+ }
+ $calendars[] = $calendar;
+ }
+
+ // now let's get any shared calendars
+ $shareFields = implode(', ', $this->sharesProperties);
+ $shareRows = ShareService::getItemsSourceBy(['calendar'], $principal['id']);
+
+ foreach ($shareRows as $shareRow) {
+ // get the original calendar
+ $calendarShareRow = CalendarService::getCalendar($shareRow['source_id'], false);
+
+ $shareComponents = array();
+ if ($calendarShareRow['components']) {
+ $shareComponents = explode(',', $calendarShareRow['components']);
+ }
+
+ $sharedCalendar = array(
+ 'id' => $calendarShareRow['id'],
+ 'uri' => $calendarShareRow['uri'],
+ 'principaluri' => $principalUri,
+ '{' . \Sabre\CalDAV\Plugin::NS_CALENDARSERVER . '}getctag' => $calendarShareRow['ctag']?$calendarShareRow['ctag']:'0',
+ '{' . \Sabre\CalDAV\Plugin::NS_CALDAV . '}supported-calendar-component-set' => new \Sabre\CalDAV\Property\SupportedCalendarComponentSet($shareComponents),
+ '{' . \Sabre\CalDAV\Plugin::NS_CALDAV . '}schedule-calendar-transp' => new \Sabre\CalDAV\Property\ScheduleCalendarTransp($calendarShareRow['transparent']?'transparent':'opaque'),
+ );
+
+ // some specific properies for shared calendars
+ $shareRow['displayname'] = '['.$shareRow['name'].']'.$calendarShareRow['displayname'];
+ $sharedCalendar['{http://calendarserver.org/ns/}shared-url'] = $calendarShareRow['uri'];
+ $sharedCalendar['{http://sabredav.org/ns}owner-principal'] = $calendarShareRow['principaluri'];
+ $sharedCalendar['{http://sabredav.org/ns}read-only'] = true;
+ $sharedCalendar['{http://calendarserver.org/ns/}summary'] = $shareRow['summary'];
+
+ foreach ($this->propertyMap as $xmlName => $dbName) {
+ if ($xmlName == '{DAV:}displayname') {
+ $sharedCalendar[$xmlName] = $shareRow['displayname'] == null ? $calendarShareRow['displayname'] : $shareRow['displayname'];
+ } elseif ($xmlName == '{http://apple.com/ns/ical/}calendar-color') {
+ $sharedCalendar[$xmlName] = $shareRow['colour'] == null ? $calendarShareRow['calendarcolor'] : $shareRow['color'];
+ } else {
+ $sharedCalendar[$xmlName] = $calendarShareRow[$dbName];
+ }
+ }
+ $calendars[] = $sharedCalendar;
+ }
+
+ // 获取分享记录
+ $shareSource = ShareService::getItemsSourceBy(['event'], Auth::id());
+ $sharedEvent = array(
+ 'id' => 'share-events',
+ 'uri' => 'share-events',
+ 'principaluri' => $principalUri,
+ '{'.\Sabre\CalDAV\Plugin::NS_CALENDARSERVER.'}getctag' => count($shareSource),
+ '{'.\Sabre\CalDAV\Plugin::NS_CALDAV.'}supported-calendar-component-set' => new \Sabre\CalDAV\Property\SupportedCalendarComponentSet(array('VEVENT')),
+ '{'.\Sabre\CalDAV\Plugin::NS_CALDAV.'}schedule-calendar-transp' => new \Sabre\CalDAV\Property\ScheduleCalendarTransp('opaque'),
+ );
+ $sharedEvent['{http://calendarserver.org/ns/}shared-url'] = 'share-events';
+ $sharedEvent['{http://sabredav.org/ns}owner-principal'] = 'principals/owner';
+ $sharedEvent['{http://sabredav.org/ns}read-only'] = true;
+ $sharedEvent['{DAV:}displayname'] = $this->sharedEvents['displayname'];
+ $calendars[] = $sharedEvent;
+
+ return $calendars;
+ }
+
+ /**
+ * This method is called when a user replied to a request to share.
+ *
+ * If the user chose to accept the share, this method should return the
+ * newly created calendar url.
+ *
+ * @param string href The sharee who is replying (often a mailto: address)
+ * @param int status One of the SharingPlugin::STATUS_* constants
+ * @param string $calendarUri The url to the calendar thats being shared
+ * @param string $inReplyTo The unique id this message is a response to
+ * @param string $summary A description of the reply
+ * @return null|string
+ */
+ public function shareReply($href, $status, $calendarUri, $inReplyTo, $summary = null)
+ {
+ }
+
+ /**
+ * Marks this calendar as published.
+ *
+ * Publishing a calendar should automatically create a read-only, public,
+ * subscribable calendar.
+ *
+ * @param bool $value
+ * @return void
+ */
+ public function setPublishStatus($calendarId, $value)
+ {
+ }
+
+ /**
+ * Returns a list of notifications for a given principal url.
+ *
+ * The returned array should only consist of implementations of
+ * \Sabre\CalDAV\Notifications\INotificationType.
+ *
+ * @param string $principalUri
+ * @return array
+ */
+ public function getNotificationsForPrincipal($principalUri)
+ {
+ // get ALL notifications for the user NB. Any read or out of date notifications should be already deleted.
+ $stmt = $this->pdo->prepare("SELECT * FROM ".$this->notificationsTableName." WHERE principaluri = ? ORDER BY dtstamp ASC");
+ $stmt->execute(array($principalUri));
+
+ $notifications = array();
+ while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
+ // we need to return the correct type of notification
+ switch ($row['notification']) {
+ case 'Invite':
+ $values = array();
+ // sort out the required data
+ if ($row['id']) {
+ $values['id'] = $row['id'];
+ }
+ if ($row['etag']) {
+ $values['etag'] = $row['etag'];
+ }
+ if ($row['principalUri']) {
+ $values['href'] = $row['principalUri'];
+ }
+ if ($row['dtstamp']) {
+ $values['dtstamp'] = $row['dtstamp'];
+ }
+ if ($row['type']) {
+ $values['type'] = $row['type'];
+ }
+ if ($row['readOnly']) {
+ $values['readOnly'] = $row['readOnly'];
+ }
+ if ($row['hostUrl']) {
+ $values['hostUrl'] = $row['hostUrl'];
+ }
+ if ($row['organizer']) {
+ $values['organizer'] = $row['organizer'];
+ }
+ if ($row['commonName']) {
+ $values['commonName'] = $row['commonName'];
+ }
+ if ($row['firstName']) {
+ $values['firstName'] = $row['firstName'];
+ }
+ if ($row['lastName']) {
+ $values['lastName'] = $row['lastName'];
+ }
+ if ($row['summary']) {
+ $values['summary'] = $row['summary'];
+ }
+ $notifications[] = new \Sabre\CalDAV\Notifications\Notification\Invite($values);
+ break;
+
+ case 'InviteReply':
+ break;
+ case 'SystemStatus':
+ break;
+ }
+ }
+ return $notifications;
+ }
+
+ /**
+ * This deletes a specific notifcation.
+ *
+ * This may be called by a client once it deems a notification handled.
+ *
+ * @param string $principalUri
+ * @param \Sabre\CalDAV\Notifications\INotificationType $notification
+ * @return void
+ */
+ public function deleteNotification($principalUri, \Sabre\CalDAV\Notifications\INotificationType $notification)
+ {
+ }
+
+ private function getPrincipalByEmail($email)
+ {
+ $principalBackend = $this->getPrincipalBackend();
+ $principalPath = $principalBackend->searchPrincipals('principals/users', array('{http://sabredav.org/ns}email-address'=>$email));
+ if ($principalPath == 0) {
+ throw new \Exception("Unknown email address");
+ }
+ // use the path to get the principal
+ return $principalBackend->getPrincipalByPath($principalPath);
+ }
+
+ private function getPrincipalBackend()
+ {
+ return new \Gdoo\Calendar\Sabre\Connector\Principal();
+ }
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Auth/Backend/AbstractBasic.php b/app/Gdoo/Calendar/Sabre/DAV/Auth/Backend/AbstractBasic.php
new file mode 100644
index 00000000..3ef99d15
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Auth/Backend/AbstractBasic.php
@@ -0,0 +1,87 @@
+currentUser;
+ }
+
+
+ /**
+ * Authenticates the user based on the current request.
+ *
+ * If authentication is successful, true must be returned.
+ * If authentication fails, an exception must be thrown.
+ *
+ * @param DAV\Server $server
+ * @param string $realm
+ * @throws DAV\Exception\NotAuthenticated
+ * @return bool
+ */
+ public function authenticate(DAV\Server $server, $realm) {
+
+ $auth = new HTTP\BasicAuth();
+ $auth->setHTTPRequest($server->httpRequest);
+ $auth->setHTTPResponse($server->httpResponse);
+ $auth->setRealm($realm);
+ $userpass = $auth->getUserPass();
+ if (!$userpass) {
+ $auth->requireLogin();
+ throw new DAV\Exception\NotAuthenticated('No basic authentication headers were found');
+ }
+
+ // Authenticates the user
+ if (!$this->validateUserPass($userpass[0],$userpass[1])) {
+ $auth->requireLogin();
+ throw new DAV\Exception\NotAuthenticated('Username or password does not match');
+ }
+ $this->currentUser = $userpass[0];
+ return true;
+ }
+
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Auth/Backend/AbstractDigest.php b/app/Gdoo/Calendar/Sabre/DAV/Auth/Backend/AbstractDigest.php
new file mode 100644
index 00000000..d5c23ce2
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Auth/Backend/AbstractDigest.php
@@ -0,0 +1,101 @@
+setHTTPRequest($server->httpRequest);
+ $digest->setHTTPResponse($server->httpResponse);
+
+ $digest->setRealm($realm);
+ $digest->init();
+
+ $username = $digest->getUsername();
+
+ // No username was given
+ if (!$username) {
+ $digest->requireLogin();
+ throw new DAV\Exception\NotAuthenticated('No digest authentication headers were found');
+ }
+
+ $hash = $this->getDigestHash($realm, $username);
+ // If this was false, the user account didn't exist
+ if ($hash===false || is_null($hash)) {
+ $digest->requireLogin();
+ throw new DAV\Exception\NotAuthenticated('The supplied username was not on file');
+ }
+ if (!is_string($hash)) {
+ throw new DAV\Exception('The returned value from getDigestHash must be a string or null');
+ }
+
+ // If this was false, the password or part of the hash was incorrect.
+ if (!$digest->validateA1($hash)) {
+ $digest->requireLogin();
+ throw new DAV\Exception\NotAuthenticated('Incorrect username');
+ }
+
+ $this->currentUser = $username;
+ return true;
+
+ }
+
+ /**
+ * Returns the currently logged in username.
+ *
+ * @return string|null
+ */
+ public function getCurrentUser() {
+
+ return $this->currentUser;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Auth/Backend/Apache.php b/app/Gdoo/Calendar/Sabre/DAV/Auth/Backend/Apache.php
new file mode 100644
index 00000000..cd674e18
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Auth/Backend/Apache.php
@@ -0,0 +1,63 @@
+httpRequest->getRawServerValue('REMOTE_USER');
+ if (is_null($remoteUser)) {
+ throw new DAV\Exception('We did not receive the $_SERVER[REMOTE_USER] property. This means that apache might have been misconfigured');
+ }
+
+ $this->remoteUser = $remoteUser;
+ return true;
+
+ }
+
+ /**
+ * Returns information about the currently logged in user.
+ *
+ * If nobody is currently logged in, this method should return null.
+ *
+ * @return array|null
+ */
+ public function getCurrentUser() {
+
+ return $this->remoteUser;
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Auth/Backend/BackendInterface.php b/app/Gdoo/Calendar/Sabre/DAV/Auth/Backend/BackendInterface.php
new file mode 100644
index 00000000..cb339537
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Auth/Backend/BackendInterface.php
@@ -0,0 +1,36 @@
+loadFile($filename);
+
+ }
+
+ /**
+ * Loads an htdigest-formatted file. This method can be called multiple times if
+ * more than 1 file is used.
+ *
+ * @param string $filename
+ * @return void
+ */
+ public function loadFile($filename) {
+
+ foreach(file($filename,FILE_IGNORE_NEW_LINES) as $line) {
+
+ if (substr_count($line, ":") !== 2)
+ throw new DAV\Exception('Malformed htdigest file. Every line should contain 2 colons');
+
+ list($username,$realm,$A1) = explode(':',$line);
+
+ if (!preg_match('/^[a-zA-Z0-9]{32}$/', $A1))
+ throw new DAV\Exception('Malformed htdigest file. Invalid md5 hash');
+
+ $this->users[$realm . ':' . $username] = $A1;
+
+ }
+
+ }
+
+ /**
+ * Returns a users' information
+ *
+ * @param string $realm
+ * @param string $username
+ * @return string
+ */
+ public function getDigestHash($realm, $username) {
+
+ return isset($this->users[$realm . ':' . $username])?$this->users[$realm . ':' . $username]:false;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Auth/Backend/PDO.php b/app/Gdoo/Calendar/Sabre/DAV/Auth/Backend/PDO.php
new file mode 100644
index 00000000..1a057e2f
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Auth/Backend/PDO.php
@@ -0,0 +1,65 @@
+pdo = $pdo;
+ $this->tableName = $tableName;
+
+ }
+
+ /**
+ * Returns the digest hash for a user.
+ *
+ * @param string $realm
+ * @param string $username
+ * @return string|null
+ */
+ public function getDigestHash($realm,$username) {
+
+ $stmt = $this->pdo->prepare('SELECT username, digesta1 FROM '.$this->tableName.' WHERE username = ?');
+ $stmt->execute(array($username));
+ $result = $stmt->fetchAll();
+
+ if (!count($result)) return;
+
+ return $result[0]['digesta1'];
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Auth/Plugin.php b/app/Gdoo/Calendar/Sabre/DAV/Auth/Plugin.php
new file mode 100644
index 00000000..ae3ab80d
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Auth/Plugin.php
@@ -0,0 +1,112 @@
+authBackend = $authBackend;
+ $this->realm = $realm;
+
+ }
+
+ /**
+ * Initializes the plugin. This function is automatically called by the server
+ *
+ * @param DAV\Server $server
+ * @return void
+ */
+ public function initialize(DAV\Server $server) {
+
+ $this->server = $server;
+ $this->server->subscribeEvent('beforeMethod',array($this,'beforeMethod'),10);
+
+ }
+
+ /**
+ * Returns a plugin name.
+ *
+ * Using this name other plugins will be able to access other plugins
+ * using DAV\Server::getPlugin
+ *
+ * @return string
+ */
+ public function getPluginName() {
+
+ return 'auth';
+
+ }
+
+ /**
+ * Returns the current users' principal uri.
+ *
+ * If nobody is logged in, this will return null.
+ *
+ * @return string|null
+ */
+ public function getCurrentUser() {
+
+ $userInfo = $this->authBackend->getCurrentUser();
+ if (!$userInfo) return null;
+
+ return $userInfo;
+
+ }
+
+ /**
+ * This method is called before any HTTP method and forces users to be authenticated
+ *
+ * @param string $method
+ * @param string $uri
+ * @throws Sabre\DAV\Exception\NotAuthenticated
+ * @return bool
+ */
+ public function beforeMethod($method, $uri) {
+
+ $this->authBackend->authenticate($this->server,$this->realm);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Browser/GuessContentType.php b/app/Gdoo/Calendar/Sabre/DAV/Browser/GuessContentType.php
new file mode 100644
index 00000000..639dfbdc
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Browser/GuessContentType.php
@@ -0,0 +1,99 @@
+ 'image/jpeg',
+ 'gif' => 'image/gif',
+ 'png' => 'image/png',
+
+ // groupware
+ 'ics' => 'text/calendar',
+ 'vcf' => 'text/x-vcard',
+
+ // text
+ 'txt' => 'text/plain',
+
+ );
+
+ /**
+ * Initializes the plugin
+ *
+ * @param DAV\Server $server
+ * @return void
+ */
+ public function initialize(DAV\Server $server) {
+
+ // Using a relatively low priority (200) to allow other extensions
+ // to set the content-type first.
+ $server->subscribeEvent('afterGetProperties',array($this,'afterGetProperties'),200);
+
+ }
+
+ /**
+ * Handler for teh afterGetProperties event
+ *
+ * @param string $path
+ * @param array $properties
+ * @return void
+ */
+ public function afterGetProperties($path, &$properties) {
+
+ if (array_key_exists('{DAV:}getcontenttype', $properties[404])) {
+
+ list(, $fileName) = DAV\URLUtil::splitPath($path);
+ $contentType = $this->getContentType($fileName);
+
+ if ($contentType) {
+ $properties[200]['{DAV:}getcontenttype'] = $contentType;
+ unset($properties[404]['{DAV:}getcontenttype']);
+ }
+
+ }
+
+ }
+
+ /**
+ * Simple method to return the contenttype
+ *
+ * @param string $fileName
+ * @return string
+ */
+ protected function getContentType($fileName) {
+
+ // Just grabbing the extension
+ $extension = strtolower(substr($fileName,strrpos($fileName,'.')+1));
+ if (isset($this->extensionMap[$extension]))
+ return $this->extensionMap[$extension];
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Browser/MapGetToPropFind.php b/app/Gdoo/Calendar/Sabre/DAV/Browser/MapGetToPropFind.php
new file mode 100644
index 00000000..b50d96f0
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Browser/MapGetToPropFind.php
@@ -0,0 +1,57 @@
+server = $server;
+ $this->server->subscribeEvent('beforeMethod',array($this,'httpGetInterceptor'));
+ }
+
+ /**
+ * This method intercepts GET requests to non-files, and changes it into an HTTP PROPFIND request
+ *
+ * @param string $method
+ * @param string $uri
+ * @return bool
+ */
+ public function httpGetInterceptor($method, $uri) {
+
+ if ($method!='GET') return true;
+
+ $node = $this->server->tree->getNodeForPath($uri);
+ if ($node instanceof DAV\IFile) return;
+
+ $this->server->invokeMethod('PROPFIND',$uri);
+ return false;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Browser/Plugin.php b/app/Gdoo/Calendar/Sabre/DAV/Browser/Plugin.php
new file mode 100644
index 00000000..f8baf84d
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Browser/Plugin.php
@@ -0,0 +1,491 @@
+ 'icons/file',
+ 'Sabre\\DAV\\ICollection' => 'icons/collection',
+ 'Sabre\\DAVACL\\IPrincipal' => 'icons/principal',
+ 'Sabre\\CalDAV\\ICalendar' => 'icons/calendar',
+ 'Sabre\\CardDAV\\IAddressBook' => 'icons/addressbook',
+ 'Sabre\\CardDAV\\ICard' => 'icons/card',
+ );
+
+ /**
+ * The file extension used for all icons
+ *
+ * @var string
+ */
+ public $iconExtension = '.png';
+
+ /**
+ * reference to server class
+ *
+ * @var Sabre\DAV\Server
+ */
+ protected $server;
+
+ /**
+ * enablePost turns on the 'actions' panel, which allows people to create
+ * folders and upload files straight from a browser.
+ *
+ * @var bool
+ */
+ protected $enablePost = true;
+
+ /**
+ * By default the browser plugin will generate a favicon and other images.
+ * To turn this off, set this property to false.
+ *
+ * @var bool
+ */
+ protected $enableAssets = true;
+
+ /**
+ * Creates the object.
+ *
+ * By default it will allow file creation and uploads.
+ * Specify the first argument as false to disable this
+ *
+ * @param bool $enablePost
+ * @param bool $enableAssets
+ */
+ public function __construct($enablePost=true, $enableAssets = true) {
+
+ $this->enablePost = $enablePost;
+ $this->enableAssets = $enableAssets;
+
+ }
+
+ /**
+ * Initializes the plugin and subscribes to events
+ *
+ * @param DAV\Server $server
+ * @return void
+ */
+ public function initialize(DAV\Server $server) {
+
+ $this->server = $server;
+ $this->server->subscribeEvent('beforeMethod',array($this,'httpGetInterceptor'));
+ $this->server->subscribeEvent('onHTMLActionsPanel', array($this, 'htmlActionsPanel'),200);
+ if ($this->enablePost) $this->server->subscribeEvent('unknownMethod',array($this,'httpPOSTHandler'));
+ }
+
+ /**
+ * This method intercepts GET requests to collections and returns the html
+ *
+ * @param string $method
+ * @param string $uri
+ * @return bool
+ */
+ public function httpGetInterceptor($method, $uri) {
+
+ if ($method !== 'GET') return true;
+
+ // We're not using straight-up $_GET, because we want everything to be
+ // unit testable.
+ $getVars = array();
+ parse_str($this->server->httpRequest->getQueryString(), $getVars);
+
+ if (isset($getVars['sabreAction']) && $getVars['sabreAction'] === 'asset' && isset($getVars['assetName'])) {
+ $this->serveAsset($getVars['assetName']);
+ return false;
+ }
+
+ try {
+ $node = $this->server->tree->getNodeForPath($uri);
+ } catch (DAV\Exception\NotFound $e) {
+ // We're simply stopping when the file isn't found to not interfere
+ // with other plugins.
+ return;
+ }
+ if ($node instanceof DAV\IFile)
+ return;
+
+ $this->server->httpResponse->sendStatus(200);
+ $this->server->httpResponse->setHeader('Content-Type','text/html; charset=utf-8');
+
+ $this->server->httpResponse->sendBody(
+ $this->generateDirectoryIndex($uri)
+ );
+
+ return false;
+
+ }
+
+ /**
+ * Handles POST requests for tree operations.
+ *
+ * @param string $method
+ * @param string $uri
+ * @return bool
+ */
+ public function httpPOSTHandler($method, $uri) {
+
+ if ($method!='POST') return;
+ $contentType = $this->server->httpRequest->getHeader('Content-Type');
+ list($contentType) = explode(';', $contentType);
+ if ($contentType !== 'application/x-www-form-urlencoded' &&
+ $contentType !== 'multipart/form-data') {
+ return;
+ }
+ $postVars = $this->server->httpRequest->getPostVars();
+
+ if (!isset($postVars['sabreAction']))
+ return;
+
+ if ($this->server->broadcastEvent('onBrowserPostAction', array($uri, $postVars['sabreAction'], $postVars))) {
+
+ switch($postVars['sabreAction']) {
+
+ case 'mkcol' :
+ if (isset($postVars['name']) && trim($postVars['name'])) {
+ // Using basename() because we won't allow slashes
+ list(, $folderName) = DAV\URLUtil::splitPath(trim($postVars['name']));
+ $this->server->createDirectory($uri . '/' . $folderName);
+ }
+ break;
+ case 'put' :
+ if ($_FILES) $file = current($_FILES);
+ else break;
+
+ list(, $newName) = DAV\URLUtil::splitPath(trim($file['name']));
+ if (isset($postVars['name']) && trim($postVars['name']))
+ $newName = trim($postVars['name']);
+
+ // Making sure we only have a 'basename' component
+ list(, $newName) = DAV\URLUtil::splitPath($newName);
+
+ if (is_uploaded_file($file['tmp_name'])) {
+ $this->server->createFile($uri . '/' . $newName, fopen($file['tmp_name'],'r'));
+ }
+ break;
+
+ }
+
+ }
+ $this->server->httpResponse->setHeader('Location',$this->server->httpRequest->getUri());
+ $this->server->httpResponse->sendStatus(302);
+ return false;
+
+ }
+
+ /**
+ * Escapes a string for html.
+ *
+ * @param string $value
+ * @return string
+ */
+ public function escapeHTML($value) {
+
+ return htmlspecialchars($value,ENT_QUOTES,'UTF-8');
+
+ }
+
+ /**
+ * Generates the html directory index for a given url
+ *
+ * @param string $path
+ * @return string
+ */
+ public function generateDirectoryIndex($path) {
+
+ $version = '';
+ if (DAV\Server::$exposeVersion) {
+ $version = DAV\Version::VERSION ."-". DAV\Version::STABILITY;
+ }
+
+ $html = "
+
+ Index for " . $this->escapeHTML($path) . "/ - SabreDAV " . $version . "
+
+ ";
+
+ if ($this->enableAssets) {
+ $html.='';
+ }
+
+ $html .= "
+
+
+ Generated by SabreDAV " . $version . " (c)2007-2015 http://sabre.io/
+
+ ";
+
+ return $html;
+
+ }
+
+ /**
+ * This method is used to generate the 'actions panel' output for
+ * collections.
+ *
+ * This specifically generates the interfaces for creating new files, and
+ * creating new directories.
+ *
+ * @param DAV\INode $node
+ * @param mixed $output
+ * @return void
+ */
+ public function htmlActionsPanel(DAV\INode $node, &$output) {
+
+ if (!$node instanceof DAV\ICollection)
+ return;
+
+ // We also know fairly certain that if an object is a non-extended
+ // SimpleCollection, we won't need to show the panel either.
+ if (get_class($node)==='Sabre\\DAV\\SimpleCollection')
+ return;
+
+ $output.= '
+
+
';
+
+ }
+
+ /**
+ * This method takes a path/name of an asset and turns it into url
+ * suiteable for http access.
+ *
+ * @param string $assetName
+ * @return string
+ */
+ protected function getAssetUrl($assetName) {
+
+ return $this->server->getBaseUri() . '?sabreAction=asset&assetName=' . urlencode($assetName);
+
+ }
+
+ /**
+ * This method returns a local pathname to an asset.
+ *
+ * @param string $assetName
+ * @return string
+ */
+ protected function getLocalAssetPath($assetName) {
+
+ $assetDir = __DIR__ . '/assets/';
+ $path = $assetDir . $assetName;
+
+ // Making sure people aren't trying to escape from the base path.
+ if (strpos(realpath($path), realpath($assetDir)) === 0) {
+ return $path;
+ }
+ throw new DAV\Exception\Forbidden('Path does not exist, or escaping from the base path was detected');
+ }
+
+ /**
+ * This method reads an asset from disk and generates a full http response.
+ *
+ * @param string $assetName
+ * @return void
+ */
+ protected function serveAsset($assetName) {
+
+ $assetPath = $this->getLocalAssetPath($assetName);
+ if (!file_exists($assetPath)) {
+ throw new DAV\Exception\NotFound('Could not find an asset with this name');
+ }
+ // Rudimentary mime type detection
+ switch(strtolower(substr($assetPath,strpos($assetPath,'.')+1))) {
+
+ case 'ico' :
+ $mime = 'image/vnd.microsoft.icon';
+ break;
+
+ case 'png' :
+ $mime = 'image/png';
+ break;
+
+ default:
+ $mime = 'application/octet-stream';
+ break;
+
+ }
+
+ $this->server->httpResponse->setHeader('Content-Type', $mime);
+ $this->server->httpResponse->setHeader('Content-Length', filesize($assetPath));
+ $this->server->httpResponse->setHeader('Cache-Control', 'public, max-age=1209600');
+ $this->server->httpResponse->sendStatus(200);
+ $this->server->httpResponse->sendBody(fopen($assetPath,'r'));
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Browser/assets/favicon.ico b/app/Gdoo/Calendar/Sabre/DAV/Browser/assets/favicon.ico
new file mode 100644
index 0000000000000000000000000000000000000000..2b2c10a22cc7a57c4dc5d7156f184448f2bee92b
GIT binary patch
literal 4286
zcmc&&O-NKx6uz%14NNBzA}E}JHil3^6a|4&P_&6!RGSD}1rgCAC@`9dTC_@vqNoT8
z0%;dQR0K_{iUM;bf#3*%o1h^C2N7@IH_nmc?Va~t5U6~f`_BE&`Of`&_n~tUev3uN
zziw!)bL*XR-2hy!51_yCgT8fb3s`VC=e=KcI5&)PGGQlpSAh?}1mK&Pg8c>z0Y`y$
zAT_6qJ%yV?|0!S$5WO@z3+`QD17OyXL4PyiM}RavtA7Tu7p)pn^p7Ks@m6m7)A}X$
z4Y+@;NrHYq_;V@RoZ|;69MPx!46Ftg*Tc~711C+J`JMuUfYwNBzXPB9sZm3WK9272
z&x|>@f_EO{b3cubqjOyc~J3I$d_lHIpN}q
z!{kjX{c{12XF=~Z$w$kazXHB!b53>u!rx}_$e&dD`xNgv+MR&p2yN1xb0>&9t@28Z
zV&5u#j_D=P9mI#){2s8@eGGj(?>gooo<%RT14>`VSZ&_l6GlGnan=^bemD56rRN{?
zSAqZD$i;oS9SF6#f5I`#^C&hW@13s_lc3LUl(PWmHcop2{vr^kO`kP(*4!m=3Hn3e#Oc!a2;iDn+FbXzcOHEQ
zbXZ)u93cj1WA=KS+M>jZ=oYyXq}1?ZdsjsX0A
zkJXCvi~cfO@2ffd7r^;>=SsL-3U%l5HRoEZ#0r%`7%&%
ziLTXJqU*JeXt3H5`AS#h(dpfl+`Ox|)*~QS%h&VO!d#)!>r3U5_YsDi2fY6Sd&vw%
literal 0
HcmV?d00001
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Browser/assets/icons/addressbook.png b/app/Gdoo/Calendar/Sabre/DAV/Browser/assets/icons/addressbook.png
new file mode 100644
index 0000000000000000000000000000000000000000..c9acc84172dad59708b6b298b310b8d29eeeb671
GIT binary patch
literal 7232
zcmeHMXH=70vktu%m8u{iNEJu|p@k-dEbh;oQp!
z004N*&5Ug6PsrAvofQCJ0J8kPi!O*#jGZWUL@!DZii8CiV2GYrpt(N^hqc9`Fu^B&
z$Li3XlkoOV6epx598L6BMs3+BQ~d+z-T;7(J~aS^_Qg_wo>&~7pbMI-s0IP?7+sK~
z8WMsGKw!P`W~WG4yHi&7=u^IEEeuFsk5h*Vrvvz7DJUS--;Y3sQ*}YxxN!P<>ophz
z+%}>3>Vm!{<%F~bB8Vg`5T>l6tfGX5sH+0iRFzfLRMb^qia-?zL=z0r0INcjpqg-q
z8XN`%e*b~=IDtAOj2GP2$mDxCx}*#8rceUlU~o`SkaCc!GLeJ>L$$QDzz`L%ii#55
zLWvwqprEKq1hUi?#5W8hEE!G02T<@t0&oixr=z>6WJ@7j?2K@s&Aduv@jf_Eq
zv3^*8EP+A>LzSW6o%VDlZ1Fg63i*c{f&86iI^SR_DuC_+0h6|E{^l9rO{5UX-o$`^
z_WYsV_TL%OJb;3R(c^9r`oovLEA)1
zN5s+d$KcTvEQUfv6TQ5!SY-@$JAofL!3_c_-b51Fnn=cPu}SA}i)5e<1`YqV)ot+`
z>jr+5Z_+o>55Gk<+z&;->4K&f4Xf4
z*@?Opg@UK}VRyv%Go$a__mho-f4Ygk@O1vF+EFt7eA{D5{^b9!NI%8a+1W>M#5WER
zMEbcxQ_Klo#O-7AcN@F`hGa~opfIFAkJbOyBk+{qpKERDlW4o4eu8d|CSvGq|Ls8h
z12~2BGjMyXpCge(pGp7dYwVB0|0n%X(x2Mxf_>}29Rr14jc@PhgNi;Q!9RxNw=(VM
z?f=Sho2~x}@($2{gX|#V*UNwD`ZY&8EdHfy2N}O!{!7=dIoe_IFI_vx`1SH%x_-^k
z4vYUp7w2EsEG&V3w+f&Lv``pA3oHbg@?#%M;1t1p-E2-|c|#H-_R1@JzcNBq&XcTyD{j<6UEo
zI0B^10yQ<)ne~ii-I(2&&uI#3*c>cE#4F`4kjf9Yf_ZrA9}M*e%!^mAR}E$zo9Zu3
zORdt;YtL*^N_oifn?F+u*|JG0gxkI3G?lUOvJIf`xLe&LM~@;G2ok9*GlYr@Go9q^
zEz`#}@G`i`7(r(J-?mH-)D<>Cgao26JlhL$0DkE{zbmf-K(&|l=DD*83q#uQGU
zBfS~GLtVKIm&%cnvH3DXpH6YX+nd@SWMv({*9pmf$($M!medu{2S+`*XCh6t-wjlCNsFC~wA
ztcbPif81a*Yr3ti?%Lg-nYeKf;M-OF)`;J@?PY=n6fA4v!4Y!-RK&GE!e|%E#rOE^
zoFOK;>>L^svjrm2h{sHx(ZWK2#aNC-Iyotq$HA~D8yWs;GH{Czl&W$vSX9co>CY_Jx^p$*HJ(W=11
z2PK+Q^PIpqE{Q$`e^qZ}9+2ghQRIQ~E)V}@K_6kS`C%KoxHO#-#55!NTr9BH
z$Gf;zV1y&|YokQx-X0?N;CZI65bn4U4me(v9v
zt~$4NQ0?3rdSimd1&!>(WtET&)ww0Fc3t+>j11iq;<6g?d`;6m(?o-v--hwhsT@)D
z0tIfwG=F`%g`|b}Fj5F8rtn%b=Q*N4c476cdco@zbJ{%maeVAa3Aft*j9lATg{8YasCtKb*)A6
z&p)mOy?*~>Rz>8ALTwmBci{43pegY-Rz
z@eqaDI>U++PdYkRyrs|SG9%{4tSuE@XNM|3erf14y>^dbo%`=<#-XIL;5_gIA^7h8{BBuh8QFCLWCMz
zjybw8t#D>`diqPK?x2S16YVFy=Uha*f^b!zgR4JJ1ZT}vGx=G3Onb-tVMfdR7D0dD
z`glZQZoz|4cc%WQut~kEYa)eZXSH#>_lm0s76*KCRHhC+4HUTh7P+wZx$aU#-1PwD
zSw(J?U7Ia5Q(8A&im4#q-#hZkl`FX)bHqF}g8`PDLh%P9%#Y3|7lfHZ&tmX5;&hWT
z<5fx_3!1T4E*<_n0Pc)>)%JSuOcLRfI-%tzZ;gP!lL*7d())_#@^@3z?2K^hXQMez
zmDt>^G}ZL42){nsmzQ~bTugJ`1R|Rsv0yNHf_;8B=k-ptG#j-&17x9xZl-tKyA+#6
ztpovzDJU_K53r$#T&zg?0a^a6-O5L(t?|buEi^7SuU@r^ia5%0=)partDxHpgbYVJ
zf7*m%AIoR4_ci_L>MqZ+*&Gus1R1Cu%z!(p(e54?)BHFeZReqE|p;9|xOx#=mi9BR2G+bFNE
z-l%Yn{d&pegCmMktsLdiG9F(%KkR?98FB?ye>(quR3lC}e?WDi>cbomAm@FBzd$g^
zV;Gwwu)@-|QaU>tzvM-TZ+$#1$T~4m^(w?f*!&T{%)ZSN@d_#2fI@5Rm8uEfOL0E93XfHpv@9<(lpR4mCW=Wvos2vP=RffHC{UD;WbfFVr5`q+#U0o}O{-IJS~eAv(asK`d^)1aHQ
ziy$S$GkI8i4v*0tgFSBcLh-pWdJsuP?pNabJmX@Z_2OKk=(*Z@o+mKR5yN;JSh0YO
z;_LHf?tcg;o|L=R`eA^auo>D~-ub>gnZ&aogPZNsR-M?G+g#spUS#6M*q%}`oxWOC
zeSONjTSg*?%f^N&jiG)pQcJJ-=+ayFwkTSC&8&4Qaq3x))DfvNqQv#nS&JAl^B7b(Tia2WaO$md0D6JDpTV=dCihh
zQ4IexnVGqpv4g8h+U=%}z6rv`flBeFYR&i{J@Lk9WM+$=Dcj=aFB+24EdQMu1Z35i3S>ORB({g=d7Sfc(FkLfW`1UO
z{)U`pu+|%A1{9(x2s#>kQGEY6mjQOyW35MuUrjoDTz&Cr=j$yJ%f!y>222$-wGR#^
zPN>LNhJplQ3fd3rD)m~H=?JTcNmm6(GWiC#@iqmIk{%gyHD^6lw7tt
zhMhcptW>Ps#OeK11)H|fL)fJ|=F`I?@r=te)Adow=3hu^^>w4s
zs9r~e!tV^N-`jWDP0kaCJGkt1WnrBrilEV4XHRx~iV?{UMKtsA@Ek2{pXlE0kR$8K
zvUgsr*wQ{^erT2u61=e2S;I*CS-d4bT4LYe&2Z2R+NOWD&k|R#};cqIY8^5>EQfcq87f$;c)(zU)I@Er+1@JoG9p
za9q{*!gAy(yOu~ku$Py95#Ec`?GJ;Omy3r6Q~g@+Je)1x%Tl~Q%D9&QWWh;Vh-wC-
zOV=yUyC*dJXWMyT-U3!~it`e2?`E=SeW8nCPUFMoJ(mdqi$ZMzi+PbsS?Ln`$rt&C
z22GC)MK|$t@NY4UO}@4o{^JomDd#w}qr&tsN2Ce$^FHtM``!2b+`s4fUDtck$-!DgP+AZK
z0*Tlh*ze5wM{NA~`9L5p$hHm%5Qz5?(ba?IVQ+`VQ$k@l0>vMI(L=*HQ6P{Jh8~8)
zhX6E)KM+VH8$;Q3O;8AtU<`HFwMW>8SpY%A12I&KFqB+kSui;S0W(Y0B7;3gb2=TCYf>=vNBJfmV7>&pw-N3~8
zQzB``P$*{}@?$x;FlS<55G~>-1v%mm<2V+=>9{bsHVgr$ZpOg>migav{v1re|BMZb
zq>?rlK)}NR5)cZIX%QR_?JaN);g%k>JK*m^!_hVaekS{qD1jV#1R|aW5NH%UB_IF*
zU<6>3i<67C=ahtiqv7^*GL4}eiw(38+FD4Yt2P3S&_ipZG!WWo1Y*-8h!Fvg#!~?t
zjY8e<><`ymfbgx+mWd>yi6e;^1yCWb(KsrB5*-mjG=gu~$(h;8+8q5zGlKsOb%TZQ
zpGy3R$&5t%E7L|%&?Fo=&=^YBA^-unND>Wd!Y*in*y9KQgi}Tw=LxT%tVlOA+`IvF
zJSj4QBM%Zlp+a0jaS=g8av&!t5Enxv1OFuS2kWNLzYE(C8xiRr4B-Eewz(P2ae;po
zYC^=^_85;xCr|hjlCTPp6P0W$PX1baQ$O{AY9F41TsJfXwMh
zR8I4mLhO%;Pg5k5nOR-Tdcx6XN4R2$6lk6VHpVUe3;E-_E^l
z;WvaTDoU-bF1J9GmD?cd>W@L*>GEeVKAk$;TWsH{xI5X?bzYQL47CPO7l5
z5ZvG24+e41nenL!Cz(oqPcyHbc%VVUvkR@m&uhl=QQqwp{9VZNc>ELdR;+XGIVS8i
z7X#ASnX~?4lH%(Av>N}mv_pJ5_ObB!%i%mNHT41lHFHmWO%BsNnV~Y)XJ=OOf-3fk
zwwSuw#$6q~oE2CBR_p;MG4d3O3TZC3b=)f
z?%4ef>b~PcF(TJb?iLkfuMHWDe+eKSFisLGms@6*-%SzSU4m|ZI*9vfV$35Z?P9s_
z5}2=Ib}^yc^_~Bd8Rpj|zjJ$K=iY!+uo)i|VX^x~iHZpVyDZP7x*X2twh$DKJKp)(
z2kw6GgE8L7esT)Qb>Tr(
z9~+tndeS6(A`%>gHQC0hnDpG4a`j{Aduz4YDCCG^iN3zz)g)Fyfx|L90rp1bdXl%Y
zyUK~Ei6NRKl;(cL^HNGscg=^^sLE%FyI~!%3h=7B
zzszfnFp9c81oKP0C;anLqlfT^WPY1Z@7{s4JZJc5don3}Xh*wKc9qr01boh0X+-zr
z-qf9*1w-|Y*FK@7JEW($S3f3=?#nlTGX>o
zt(ig!wA>)%ps~lJw{NHHdL&-|VuBEU;_^bKN){=e-{_*9Qhv9FU;F#;R|PkE4)WVM
z=HwJY51A>IesjE?+ILM(w!LRQOy5=DdDTD#_rgkCirx#5w|MSzRP~eG*YVei^U7Xc
z@2{#@#=_rszahJG@g*g&G=ccRN0CQLUk1ly`R!v#T*v|A`7e}>QcL5Ds}Y5&?%#%{
zzTCZX>D}3tp_2ZU=(^l05g}L9Q|&iO`OUnzx40nvcAq(>PhSw~;ph5%l=1mEf#$g7
z@?r@~+^U}K&dhgLQ=A8Rcl#fy6*uz<=qY-=drm~sPhpAAyl(2XCGD-PLIw-wlM1PL
zW}AeEFkz_4g;nyns7_l7;f!T0?t)?Ttnql>%J@p-XEwfP{r$*-6e+(pXH{FgGKt!o
zDC*O6Zc(6eCLuU4zDA7u{L-6OLap6}QbDP{FmVwZWURR%d
zA)jN{@_V&v=4G!I=!boXA}=H9HQX
za=Ol`$kPr_kk=(;)$e(AbsZcL%<9CZ9xHXVJ(>DdYTOfc9wxuSKQ2y+Kz5ef;}}j=
zm-RY!>Anw#Akxz&Xy5Mh+ZLT25<=s7lW`A0Df-2gvUYqeJ)%I0`IQ!l>D`Y^RCb^8
zynUl9z@@s{OY88>K3(Hz880-S-r9N#Nt77PFL(qtN)?V&kT3EcErwIw^NP{
zeNb4ET>U~W2{+9xQO-KaOq-TCxJcOEhB$M}C+RoSz8aPCSmmr9^>zRkDVLAUY<7|o@!c4td_gCN@+g!
zCSG5xp{c)j!CKiPdoR)~7CrtTtp=nuH4MO}^Hv#&f_JF8fr(3ko`nr6DHXwim#9i-
zcH?gJNB-vYRtT%6nZ*Phy@^U*31-w)(-{I|MfFTP&tt$|LzN7FwOz^9R|@3QHI-5G
zTG!6AgaQo(YUuzM#{h)@4JzPIgn!;bQ1U2WFyl8(ZqYWEu|Lq3YR_~Zwlau0bM
zVQ((GR&hm0E&l8dR*NMD^K#jJ+qp&Elk1>PBPOb#0?ebqQ4ulFIg;sj=3v3uvCm*T9iJ>!a>etUgsb!o&U
zspnei)pt2djm+
zC|Tti+{zZKG{1J%A>Zgn6zH_Lif%tEkaeT7L!
zR3$sXM`KHcZb3!$O!dLd!eK^6Mt8FI><G75RR_~_31AY4R>1)BBS#x|MpEfUkf*zmlzFecJsHpfVWoCLB
zyw@?Gt6=OnP7ynk$d&&31>XnubtAEnk{~MexewOfWvE@IZh|FmWMZPnEB3jjvC-Gh
zAPT@@o4o`FK^7-@8Z)wkX=wutKbHozw4qn=U0q$@7~t_;^UD|H^OlWg2f*WT&tJV_
zk|-2!nbR2=-rMDFl4F#U7}e1lJmkcFBVND25%a<>(FzG+E!|S9v=aFFR#24it$UAjtSmRU4DWYZp&MOpC4>2
VG`ly3;d~;1Y%Cr2-!R7}{u}gUbfEwM
literal 0
HcmV?d00001
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Browser/assets/icons/card.png b/app/Gdoo/Calendar/Sabre/DAV/Browser/assets/icons/card.png
new file mode 100644
index 0000000000000000000000000000000000000000..2ce954866d853edb737a7e281de221f7846846f6
GIT binary patch
literal 5695
zcmeHLdpMN&7ay0fZjDGHnIU_zPvqiXLJR^Cf~{zk;|Xg)J1@|U9t5%pOaNj{q6Y#nCn|vq-~j?D
zD!diI@|=%S+`T|A7iSESPSqnU+URkp44yXxg0qmazu
zo<=T67lthmOmU260&daU-HFkmL{k#n(n1o;!SDd607!sws9`h~hGP!r<6?O0#n%Wp
zjBf&ln!}fp@^W#7+0vN+%uvrj&p?-mG)BRUP_3L%N#^ii5M*Ew2sWFo$42SVnPh~%si`RfX@D>=(B)a^
zvZ81pful=fZCr#{!oUG6B9p=ZDRdfa5t9%|j{wc#aGoCa5u8L^#%4q?!}!P~A_52l
zr~nOQA@ue15rXzSCh!z;FvwbVqp?1+%;OuuAuxC@NCcB_^O+|jm=4le!F0yodoHW_
z{(>Q$7$DJ*7k81+WnbQ|i2P((APFI8!FT7^X({@0!Wd5=&q%(Ol
z>2H1Qs07MC={=aAwETiCb)djN;ZvN}EdGfu$-k~y0F8IIV)HIh
z5{E|(ArJ{WC!DoA=P{UeStb?<6;XwS#%;;LNbQ}{CM7#|?9Xh32Dh%x3TtD+4p}NX
z=P33;*+id>O0iGPc83m}%^N~*wua<4LyK_8p+k!J`?_)c}(WuGwGA@!ezR_oo+Od{B|q6241IjlJgdO
zt0-DJLWQ6S$lE}Psp!!rN*<=Kx7=v*sanVQU?~tKKQ9K)+6f(AnY>P8K1pf#TqeBS
z$Vqb#QNlAhwEXRph1E7nj+({eIjq7oeeFw^ARD9$ScuTq;*aYf_cHWln_$v*hrBQ&
zVybQRkK{?ER}xT2L#||ZYfIr%*xgm%ohRO9jr5sc06l^D+Jk(!J-KhFTSUGjL^WK!sA+18ml0HpUiCAi%da1ixGAcTjB}ST<6|c^O
zb;Vjdzop!GveBdU$c$xEG{o0xpU>7~^yrB?6!Iu*93$O?E8aIC^GhRcLjuKH(doGC
z>g>#i`DLb~d%7dmo@oN8I$V@l#0CH&zpB(caUa5C@Z_AA>t;`qo}S#BKDns84A&
zktlJp&Cn}0>mFqbanu~f=u7SW=ts7#i9_@eilO9@bm=aYe?khhFtg%k-MKB2QxH5&9dJ%BoUAv9<>+K
z&!i+MxZT|9@WNE%=1ECpD+IjSo$Fm`lAXsSzc$_`Q8D7beUrKS=9H#9tA5cz@-zSJ
ztyNdD&YeeuJ|~@lF^h(OGlz>j(wK~pb2<{61@xBK&W$9|T{>SKSO2bb(`g&OtP%DA
zSDX9ZwR_qkoml4}`3K`XTB<>#7D6*Wi)|UD<%YW&@pE`LJijg{Ef5S5@gJ>|$)n
zj%rk$h8I0@tT!%BTo{C!<@=lp`1OV<&F`siSjOE0BNma|_VK=(xxE|Ls0yV+7B-=O
z=m_3H7&6yJKijn16Ir=>-HiidZ=^#X(rR`nK>FoG
zN~@eDM8L5!cP+`l7kRHz1{K!D7eQik+D6=!^V?NCwRY
zd4*hGrygYw%hs9nJLmK_
z&D@&!ISrcJ+5EvCQkavY^)y$uLcCzGQXyPa;);)+Z%Jp=Bz8hax`~|In?Enk3BF@?
z&%35i)iK4Q_;pj@Wr^2gt08j@=Z9+iE4#Uqz4DfzNv<;}_ReO{+l-83CtIBIPdTy=p?FxkVsl{i?s8V`{<&J}
zd&Sef7bsM?L{moBE_j?(U<-m*@mDS{9d0Ww#M?RUh}QgLv-llgMDdZ)(OIJZMiEXAELrv
z`K@ze{)cl57I`gKme=kVBbx&L#O06>H(N9R#EEb$-IT@vZ7O7i3cHSe)Sxw-sPw@l~2F-b$rpmHb^+G_g^smmN*w
zLHndv&MEb0CKi{MkFs>@YPc5ma<1QH>m3Dc%7ndO2G8%|j+5UUAI9V)btp4?bEBy2
z8n(6fn3%Cp_vy5vLD9^&Ej}v)gcG2br#O4hDyNySR+ySvq&LeydAJ*jABP)&$qY^P
zW^2yc2^uAkCea|`C9#I5`xL!XU5M9nr!PFUcqO@;g4J)(rE4uS4W-RH!KrD>nonw?_3
z-{*F+?e3#yLgQT_RW;EA`%Bcm@0wRkKe2TLxc=m+MvcR^Hai_#>zD$46R(reH$M=t
z>)F<6JuzkG3uWEEzaGrDt?frTyKHh4IrgT}e6RdZPsT&%9hpZIj@^s&IrVr-6P=$U&LPIV
iv*os~CbdYlK_=OkBmrhgdFt(si=ij;pWxEzMhwDg6VRB9Bbk!O
z6tW@c#N~iA9v>u-KoWsq0uoKBtI5n{}YsCay6
zXecgpHIB;fWLq1y3O1NhBjA!bq@(BZNdo9Knhyh#3rPSis=3c|tZqI)w?*xxqqn
zEEbsuzbyyixw2XQLcw45La^gu2Pk;h~+gfLUcWpMrcU>L`Qr@4?^
zsuNNYCM1NckxX+eVll;tKr|we+=&D#flMVD8xV+80%6)C(2U8TGWakg9duh6)1MX80*Pk(rsS>Cv||GyLCBr&ySORpJLGTA=V}
zrm3P(10fE}94j(n!hTS2pb%>@c&Zw7f|xLflo3Ln7|V56h-ho4e_#8jQ-s&
zzd$I28_fSNvx09LcmJct?A`0Q3wm*e
z`r?VnFDmKiCBuXFJooSP4*}5gRtywBz3qeGUmH}Mtd5LTuaQb6O%3@sYx9blBsI;A
zlExZ^^pW9bCUwadsyU_q`ZA={G*Tl`6spRN&H3$BZ*Qcc;iN?*V?=h*$S;1fRzOvg
zjx40UKgPN3tm+bUKvGRcm|>Yy13P6EO366rn9RIDhtr4DCd@2ZL505eHF3AQ{I=N7
z_jjkWkMSlR7@ryWaG*i!S3!YTDG*XJ@b9UP)|cl+qwZ`%t<(=+x(`xU&|`0nk3JZ^
znm1clS#tZfLY}W5a~(Z*$qio`fl$hywQpZacC%KC^Qx;3SD2J7Z`+$cow$>C+IqvA
z?RR-)3IsXds1@arpAx(cVtXzr)_BcVd~V-bUKY>5^sNxQ(tKZ<tM9$5tdWz%ej0ez^ya?f6Uo&{%mXC{m5-D|#M})nH&YH-de-MQ_7o1-
z#CD#)h-GilV$|^-wK4g~F|==SXVAR;kpJOJ1QJh|oFtLRE%fX|
z6QYi#L~SE1SFVxVR-KodqG8#ke`e#DwxLZ(TjPcNUV+ccwwC;%P~eLCW!nG}$^T5*
z2KCN+25ld-C9`res#RIF2P5tNTFFQKZl)nOJ3ud(wWUs
zigV_31!`Y>wXYA;a?fF$(=%0GqwWNR=hK#(^eg1=iA+9BvdZsrMKSZFgBQm?$NQu`
z8ZI~ectr(JHJI%V2a*>8RF)v5JGOae_N$Wm${=X$hi-;9sN)kvM_4=Pfs=|
zS6fzbZGnxWj`*s@zJp#mRncNO44E8)p~G)x#}$pp#DF0V_ifP^|3taUb;iljLK-u=
zA6bT$8xsfkC2wEwO#~I!zKmC8__u1;#63TLz6lka$q6`+BpOakz5ZZrZ}=A#73X%3
z({QtIxVNXrh(9j^5Q#4!7NNWO(CB>_QSo7(eHhPOeUC
z?V^-SvGAl$U%2WnSuJ;3uuJhX*QK-%=kK-0b(|&s4!GIP?Z1h-aVs>vOUAkGm^gR36sr&_zj%NG!0qfsPSSWYtzWaqIns?Yya)Zez_}3HkrU=7y$!KPW
zZqo*AUyHJ*^w{93gBM1h74AJPqoQZOtK4%?yY{_CD1T+9OuhO?(MDGYTIw*KlH3>b
zk{(_!kE{DF^KfdIM`vPMhm!umMB}U?x0K|j^mm>bN1vDvM4ldQnvU{wMbGop6+;X3O=sODx{wXdGn!#Iowd2S3j=3v%!vAF1s^TonKOpMr4QroDy
zquS4he|@yd1iv=v5>ENHRZ|4FXT>M&JyMF^WMy~
zw<@-N9+-ID5_^>URu8zEs0$dnY|E(MuOEe5*(a%}q7^4R-GMnWm~Y3pugtL=B#07U
zXtoU}A4b&&Fjh&IkTl%00j+p_z%KoqL-6H
zsy0St$fopeoNbs^0c&p6~Uw>GXW;
zOnr}u;fk}!A*9*X0m+VuA=cNgd}=IGkJO6S`nCFi_>?27XD9XAi>;K9$g&uxo*Iv%
zN0vCW6uK=7Ymg@KEN`Tq0*&i*<@zl0-FwdV`f-&ABBYP9l
zmiYvvoml12_ApZyNX5#l7|GAe=qHVnpNUhum)mVcXL~&dyBYC$&+{kjvMrxjRlfHa
z=*~;8`n4bAQ>(4E_l(})VnJ}E-H#ZyGF=R`%OK2VpUn?#rMG@{Z-l*1a~S
z1#>P&^t~(6`?%86zGjT^@;pwC}Q1RrXH8pScuyuG^&BCf5knD@5o
zK&{USOy%2DN>RroTCx-MxT4oq7D$qmE~dBEt;$#{8h6o>mjZQNW$3K>`0h!3+Z^?0
zq#?|y0K3Zesv@7I_q+ol(sUCW*PZ(!&TCuIQtx%>IL6+g8JE!i^75e35Vq1nvVQz++X-2Na-jNuQTP-uom}bmOGfQTu9TlY;TkVum
z;*bjI!YPtVa*0h_yQ|zQicoe*?XuSpyE>ioJZC-6thN62f8YDQ|NH&__e{Kp`${d%
zxtag~Xt}yLd7@9U@`qCc01P;#b~*rHYxrzm#Mf;VgChv%45-EkfBHh`XNCAh=B`mkqWXc&RKp2cb
zpgc?{k}>2g!Wb?CeOG=a5x}t!M8G20D+xhgHxJNJEQLWDkz&aqTP*=;Hbm-Dstw)7
z0(29LKzoT4BvU~unY;v~EM-{PFsyCB&lkZ~6J$!cAq-Ea6`vW=5sMItAQA?N6cG_Y
zjIbh#r92XaPN$Q|R1%eHiAGq;6e0wYTZ&{RN{Dd`Cs@Xj@+Al#B~@ZV!Qya)MIfN_
z;KXtui6@^IipVA@M6%Dup%#+lkc31bl1b9B7}7VH|2yZ)U@m7eRuV21jxB)8A;Cg8
z3>G0Wl!G!3juMXRVfetoUI>JY1xzLf3&lKC9+%HSU@ju&h(khPn8=04xX@gN8(I=B
zgg{PcCX0YtOt&OcEU8pBh0Gw^Feo&0GKE1Vk9h<#xf}*Z3PXrks`Tu$YhLiC@zJ=6
zLcZ;4A%8P01=$ghlq-&q3HVHs(oS?{JZo$;k;Wu_gQ{fV{!@uBnCykf*G$TyFockZ
z$0Eorxo`*+E<^~n0~w{D8^nb{w2Tn?#xY)CBDY^Qc7x>{VYm#H2Zo5HpjQ|q3+0P=
zXb=yIb@J5*PS=!iUbbxqY3$^8Q#3I?(=#zGZ2%*j5aOr=U
zl}%_2`>w`Gl!+>Xh!`BN^VfjmqX}hWi}_Nxav|fp_Ww7$;*9ce(!pQ__#dSQzo+6W
zOaEaV5B=g4qEg1cp{E<|Eu_ijf(|Cz6D&e|k`!$|{n#~4XyclLIQt@A;t&MgelRfJ
zV_Z@5U{4t0DmK-^OaPeD3Y5mOm2iyP#3#ftzBTB^%W}zIv+pnj3gCZ19`*{>uKi
z(&CWpjmOsQtr2w2>c76os!MO>Zd~@+fphg(f}aGx)P_KmV|7e|d`h-{(CbSd9%vhF
zD-g`C@IGm~^*x?y6f)b$$f(kL+o!)U$40xV@ob+MB!+$Q;zep%A2)_S`lfgGV>2B9
z2hQ>-i5k`pru^}+Y(wx;#cNYXHhY%IDy=k10QI-Puy3raN~UJp(EI4je@<`VZ1?HVUK1>j=z9~
zl{0=f)Ri^teW7EQA8LJxp6vPl;CvP5(@r}mAB(9vX%?D)md|UCdeRjAi)V_}+@8Lp
z<--RKwdQm!dA#D~+`h&nBG9rObL#GJ`OLCY*<82y(=%=Ca^Kva>j<>o>W_nTc9+v$
zsNwXY*CZaHb&-yW@e%gyZIQ
z)%t;po`#$EpHyEF*Xd?z;`)+cyR-cdmmg~j?&S?J&*!SFN`tMJ>kYRSAD3;5aQ#Ii
zR{NI2+Xp~teVKTnKK^9hrkBZ>1kONW?f{S@C>#n|_`;ai=yQcbzlq
zS~HTLQzFs0=pWLDpVNzG7v0@QCT}%0UKDCq8QM1b+p+FOU4xc{)j9aq!g%3Ri@MHd
z3bpF1Bb03<&FQmrcKOn=%ih)$<$>b7F#jcPcR2cSf=#=#u@$u{ih+?cf|O@bZNnd7
zgKA!(%1y%tjCajQ(0yx{R}nH%;C6GMm|1x2rGl
zWlUuHy>q<6=1a1)o@~uoR0lQK{6UTpm3ma}uX76XE8A|^kq#>M#}dueH?m4DTuN4r
zx5~gRX5)G^25RslhBCNg*)i`y`%H2nt(LsUp>ijM#;*G4VZT~;Af@(#cA-jy_jK*I
zUOQv_b7x@Q&<^*cspF=MEX1k`>{O8WH66uqwwAFHxtI6d1*D>Wy$EXVlTxk4Wqu8f
z7pSbP89Cbr-Hi*kUw)Ki{CfEcgCxP`7w;Z!dA%oSTc)KECZ(XTSuFb{z@tEIWSp8j6-6W8K(M@5_M
zzM(ap>AgjFUdfp&hob@i^-&!S%)^__m?ce)TcZPr$q<4Xd?BHHSI3IyO0DU()TXl(gK1eqCMlT5UAa%-vKs*e
z{1f4!PyJ0*MUPbL)dU&7_9Lq@1gY)9cQZ`ZT7hdl{S1!vUE5%}_BJ6sJyyrpsY-CI
z*LF*lK$;-lW$ky-w+=Ms&LPicKhRG>`;bXD&CR(v|9MK|Ky|y%;EDyN%xC)aYr1&`JU!`)
zU`3-!RzOL*K6r~#TZ=CUG47?UtS$+c&%6n=q;a(zkC;{Ty<{g*)hWBOG^q9KmSxpt
ze|j9eKR3^y1z4}zR2x?ALSLU0aFj~G5oFA%A#gxM$>ZYQhTOKRZqbjE>~73kxI^Tg
zm}+D?;NE)t8bh(^J};-Md&W6t`*ob+{rStcW``Q%lQLXv%9Q_RU7g*X@*Fm7{~MA6
BY2g3>
literal 0
HcmV?d00001
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Browser/assets/icons/parent.png b/app/Gdoo/Calendar/Sabre/DAV/Browser/assets/icons/parent.png
new file mode 100644
index 0000000000000000000000000000000000000000..156fa64fd50f15d9e838326d42d68feba2c23c3b
GIT binary patch
literal 3474
zcmb7H2{=@1A3sx*ElZmr6{pFPFk2dCvP~gNlqE}xoS8YsL^ES%F!9N9n-C#MNxEne
zBBg~(j5Sk9R1{fSrMg$$D9Z93RJZPTzwddzInT^F?|J|K-|zSS{_p#Lo{8V=yg^Ap
zLjeE)C3`z-SL9BZ`pU@w01BKVoeu!$Cbqkm(93BfmBHPOgP2@8j1%qVAyEKeW+~!9
zi~v{&(qR^xV~!oHsK$b9ra9JgjT6C%w;uLq+lBFAw=idSMpyuY!o*ryD42<;2*7Sw
z2!W#AfgAxxEzMhwDg6VRB9Bbk!O
z6tW@c#N~iA9v>u-KoWsq0uoKBtI5n{}YsCay6
zXecgpHIB;fWLq1y3O1NhBjA!bq@(BZNdo9Knhyh#3rPSis=3c|tZqI)w?*xxqqn
zEEbsuzbyyixw2XQLcw45La^gu2Pk;h~+gfLUcWpMrcU>L`Qr@4?^
zsuNNYCM1NckxX+eVll;tKr|we+=&D#flMVD8xV+80%6)C(2U8TGWakg9duh6)1MX80*Pk(rsS>Cv||GyLCBr&ySORpJLGTA=V}
zrm3P(10fE}94j(n!hTS2pb%>@c&Zw7f|xLflo3Ln7|V56h-ho4e_#8jQ-s&
zzd$I28_fSNvx09LcmJct?A`0Q3wm*e
z`r?VnFDmKiCBuXFJooSP4*}5gRtywBz3qeGUmH}Mtd5LTuaQb6O%3@sYx9blBsI;A
zlExZ^^pW9bCUwadsyU_q`ZA={G*Tl`6spRN&H3$BZ*Qcc;iN?*V?=h*$S;1fRzOvg
zjx40UKgPN3tm+bUKvGRcm|>Yy13P6EO366rn9RIDhtr4DCd@2ZL505eHF3AQ{I=N7
z_jjkWkMSlR7@ryWaG*i!S3!YTDG*XJ@b9UP)|cl+qwZ`%t<(=+x(`xU&|`0nk3JZ^
znm1clS#tZfLY}W5a~(Z*$qio`fl$hywQpZacC%KC^Qx;3SD2J7Z`+$cow$>C+IqvA
z?RR-)3IsXds1@arpAx(cVtXzr)_BcVd~V-bUKY>5^sNxQ(tKZ<tM9$5tdWz%ej0ez^ya?f6Uo&{%mXC{m5-D|#M})nH&YH-de-MQ_7o1-
z#CD#)h-GilV$|^-wK4g~F|==SXVAR;kpJOJ1QJh|oFtLRE%fX|
z6QYi#L~SE1SFVxVR-KodqG8#ke`e#DwxLZ(TjPcNUV+ccwwC;%P~eLCW!nG}$^T5*
z2KCN+25ld-C9`res#RIF2P5tNTFFQKZl)nOJ3ud(wWUs
zigV_31!`Y>wXYA;a?fF$(=%0GqwWNR=hK#(^eg1=iA+9BvdZsrMKSZFgBQm?$NQu`
z8ZI~ectr(JHJI%V2a*>8RF)v5JGOae_N$Wm${=X$hi-;9sN)kvM_4=Pfs=|
zS6fzbZGnxWj`*s@zJp#mRncNO44E8)p~G)x#}$pp#DF0V_ifP^|3taUb;iljLK-u=
zA6bT$8xsfkC2wEwO#~I!zKmC8__u1;#63TLz6lka$q6`+BpOakz5ZZrZ}=A#73X%3
z({QtIxVNXrh(9j^5Q#4!7NNWO(CB>_QSo7(eHhPOeUC
z?V^-SvGAl$U%2WnSuJ;3uuJhX*QK-%=kK-0b(|&s4!GIP?Z1h-aVs>vOUAkGm^gR36sr&_zj%NG!0qfsPSSWYtzWaqIns?Yya)Zez_}3HkrU=7y$!KPW
zZqo*AUyHJ*^w{93gBM1h74AJPqoQZOtK4%?yY{_CD1T+9OuhO?(MDGYTIw*KlH3>b
zk{(_!kE{DF^KfdIM`vPMhm!umMB}U?x0K|j^mm>bN1vDvM4ldQnvU{wMbGop6+;X3O=sODx{wXdGn!#Iowd2S3j=3v%!vAF1s^TonKOpMr4QroDy
zquS4he|@yd1iv=v5>ENHRZ|4FXT>M&JyMF^WMy~
zw<@-N9+-ID5_^>URu8zEs0$dnY|E(MuOEe5*(a%}q7^4R-GMnWm~Y3pugtL=B#07U
zXtoU}A4b&&Fjh&IkTl%00j+p_z%KoqL-6H
zsy0St$fopeoNbs^0c&p6~Uw>GXW;
zOnr}u;fk}!A*9*X0m+VuA=cNgd}=IGkJO6S`nCFi_>?27XD9XAi>;K9$g&uxo*Iv%
zN0vCW6uK=7Ymg@KEN`Tq0*&i*<@zl0-FwdV`f-&ABBYP9l
zmiYvvoml12_ApZyNX5#l7|GAe=qHVnpNUhum)mVcXL~&dyBYC$&+{kjvMrxjRlfHa
z=*~;8`n4bAQ>(4E_l(})VnJ}E-H#ZyGF=R`%OK2VpUn?#rMG@{Z-l*1a~S
z1#>P&^t~(6`?%86zGjT^@;pwC}Q1RrXH8pScuyuG^&BCf5knD@5o
zK&{USOy%2DN>RroTCx-MxT4oq7D$qmE~dBEt;$#{8h6o>mjZQNW$3K>`0h!3+Z^?0
zq#?|y0K3Zesv@7I_q+ol(sUCW*PZ(!&TCuIQtx%>IL6+g8JE!i^75``Y6gr#$Vf>RpCKLvN
z^z@Wgh%9j~V?{(G(ZN9^8k~#+r8YQ0GFRda0Ax=A7o;UY2qpnyvN-P8;j`zl7#7_f
zyL?eFA(-m}C9?c7cu;u8(g<2c63vy4_4Lpn3rG@xWC#H|IEN
zMI=Xi%=;hKLjyzR(HW#L>f-m|B$7Ke5ka^lJU%Tg4VUJCgLzE6y{oG$oUVFczU!rZ_2oL0;H
z&5t^eUu9VPeU&*d$vSj%P9WQSobC=a=D*AN7q~%aTI07QFjZNbuuwkYoe>#hX
zKy(DA!3+ij;pmVof$5w`lvE@U=J7*dK1<4`ghMIG7&4tkn%b&NoMN5AMy8}Gkc;vsT7Ri^K?+A#O%>REy`Xn}4zK=*gQyluhl5<5v{5cF*c5FVjVNvKj
zUjYKrc^{6|f9ri%NcyL>VUkHCYp744htOcUr0u5;#NU7;yib8gKzV~|BzLPc$t6mCu;ISHTOg@8{`1v`W!_A;zE{UtDEr
zWmQzGgfqu3{F+s9ezhnZa2)Z9uFly_+2XbaSRA()h1LeQ%&&Ta2PA<^aue(oO@qP85~&ePYGTyx|%5fgoi~uQi^>
z^R~lr#QqcJ9`=+ib7xnz%$~dZ(#8E~<6(XZPiMH$_!Ml?JJ6rtbw!X2C~!EOMyL41
z-v0ivmTOnq+Am#F4`QP_Mhe0h8NZ5|?KO3W-!!Cjs8aOMy5sXFJX8AT#T`)B&>#fW
zxbD<`wpV0`12&Y+{k{Meex?3e$;QU!mcJ4v$8TxiUPS|M4XSc}dtiW{(Zr;vuD!$36|9bDk{df1+vF6yo{oS3n4FqdohLjw_`5~{?3hM@%4|Xg-TL<{!O54QXQz7JRVWFg}t9!8~JKOWQ
zXT$280Ugww0mhTe$9;WZJdLKd-;~kNxBIDXL_kvgLD9N(f|+G{RN;<4&mB8hEZn+a
z+3YxvY)Whl6NGL$-)81KwT}IPb3++5b4S$3MoP7|K-3wHW^b(cq+{x2V4&ZIg?(=#W?0>RtB8i22g=Mf#Bi7Z;Qg%I8uy
z*SJIR4Cq4fut#HiS#`znyk{DP`QOH`zcljnPSW~OyLiaeYxg{rZNc>=*5$S#6
z$DhrKGs_ges@%z*nI(Rk)O@Yryvm;X-SyNTRcbufHvzWR=#hhc1O460))F6`PN|DY
z3N-G<8bnv+8mVk<3D`e5IeDrxy2})>S8_tJLtXkDs_SdLBZDEVnwznb8v)mu!!o08
z7^@cU@7;4z%`E)kNXGcUL9#`|B&23EE;ehpb-DTai0G=h~gE@oaLKP&CrKmLvQXGSLUx*u87evamzB<=YVdFE$c6=%+IIj
zb(*XE*lGwo=q*;Z1ER)M`pb3R5s5^UVf$+Om}t;B)R!2drR3M%)xq=tY%(cF401Bm
znz2|^8m9-u7y=s&RCQ@I)%Zd4l*@;n%^BA+O8oYV;
zmSUuz`b>|k@p)ISH#d9PaR=L0-KLNK{u<^UE@8}_72?Kkaasu|BFt3t;CyiGfmo|8
z(kAw1)_s##8y6c|6xVLbRqmhnZ1@VU&hK0arg4ab{>^E|*JUl1MFe%lJw=>GLdz5O
zqOLY_RmOhP?FW+24ZmO?*^Hr+W!1tihruhm?RS;tp3u`h`8t|>`Ww6Qg=sQYTaFybZ9Z5Y@)k-y*mr@hLWqYfqA;VU2Q
zHBNnUR%_UBz^IQtcD4Ra(_LZl;oJ^`p5m8BPp(6#hwq6xvtIJ3L8A|>e|^?8zem00
zJ}v)`(cV4{T4cWWn<_^C={vqR@1<>k^WGgle2cM~Z}`oF!WYridplU=KYwT0mV}rO
zX5u#U!P=-pW9^}4eV(a$l|!cJ(pT5KcRxu8R2%DxtGdD2wM$d{a=lZP&BEZyfE7_c
zor&e>lw>hoN`E&ponu-*TOm7XrJC1)R{9#Rb!W65F4!5Ar}WPIboMORSS924n9T=H
zFK9bUq3sFy!&sxys8c8RRp1}>PijmWx~i8JZkQKto%Ps~doqk#*?Q^i*trmf%RqJU
z;#=VlXJ@*Ot@Tm7cQh70`TUgTilj9ygRTDMD_Wna-`nRr)Vk*kX+&p+_hoCb$go`z
z(mGzZL{lr@+q}G%)%W7iTIF2Zt6P@o>RvYSKjuC`!I)?+XJl_PxHBh4;f`g!!E>A4VP5$SQ5CLF_=0&A@lnwhleUz`(>k&GBZWCDT3kgv
cn$validSetting = $settings[$validSetting];
+ }
+ }
+
+ if (isset($settings['authType'])) {
+ $this->authType = $settings['authType'];
+ } else {
+ $this->authType = self::AUTH_BASIC | self::AUTH_DIGEST;
+ }
+
+ $this->propertyMap['{DAV:}resourcetype'] = 'Sabre\\DAV\\Property\\ResourceType';
+
+ }
+
+ /**
+ * Add trusted root certificates to the webdav client.
+ *
+ * The parameter certificates should be a absolute path to a file
+ * which contains all trusted certificates
+ *
+ * @param string $certificates
+ */
+ public function addTrustedCertificates($certificates) {
+ $this->trustedCertificates = $certificates;
+ }
+
+ /**
+ * Enables/disables SSL peer verification
+ *
+ * @param boolean $value
+ */
+ public function setVerifyPeer($value) {
+ $this->verifyPeer = $value;
+ }
+
+ /**
+ * Does a PROPFIND request
+ *
+ * The list of requested properties must be specified as an array, in clark
+ * notation.
+ *
+ * The returned array will contain a list of filenames as keys, and
+ * properties as values.
+ *
+ * The properties array will contain the list of properties. Only properties
+ * that are actually returned from the server (without error) will be
+ * returned, anything else is discarded.
+ *
+ * Depth should be either 0 or 1. A depth of 1 will cause a request to be
+ * made to the server to also return all child resources.
+ *
+ * @param string $url
+ * @param array $properties
+ * @param int $depth
+ * @return array
+ */
+ public function propFind($url, array $properties, $depth = 0) {
+
+ $body = '' . "\n";
+ $body.= '' . "\n";
+ $body.= ' ' . "\n";
+
+ foreach($properties as $property) {
+
+ list(
+ $namespace,
+ $elementName
+ ) = XMLUtil::parseClarkNotation($property);
+
+ if ($namespace === 'DAV:') {
+ $body.=' ' . "\n";
+ } else {
+ $body.=" \n";
+ }
+
+ }
+
+ $body.= ' ' . "\n";
+ $body.= '';
+
+ $response = $this->request('PROPFIND', $url, $body, array(
+ 'Depth' => $depth,
+ 'Content-Type' => 'application/xml'
+ ));
+
+ $result = $this->parseMultiStatus($response['body']);
+
+ // If depth was 0, we only return the top item
+ if ($depth===0) {
+ reset($result);
+ $result = current($result);
+ return isset($result[200])?$result[200]:array();
+ }
+
+ $newResult = array();
+ foreach($result as $href => $statusList) {
+
+ $newResult[$href] = isset($statusList[200])?$statusList[200]:array();
+
+ }
+
+ return $newResult;
+
+ }
+
+ /**
+ * Updates a list of properties on the server
+ *
+ * The list of properties must have clark-notation properties for the keys,
+ * and the actual (string) value for the value. If the value is null, an
+ * attempt is made to delete the property.
+ *
+ * @todo Must be building the request using the DOM, and does not yet
+ * support complex properties.
+ * @param string $url
+ * @param array $properties
+ * @return void
+ */
+ public function propPatch($url, array $properties) {
+
+ $body = '' . "\n";
+ $body.= '' . "\n";
+
+ foreach($properties as $propName => $propValue) {
+
+ list(
+ $namespace,
+ $elementName
+ ) = XMLUtil::parseClarkNotation($propName);
+
+ if ($propValue === null) {
+
+ $body.="\n";
+
+ if ($namespace === 'DAV:') {
+ $body.=' ' . "\n";
+ } else {
+ $body.=" \n";
+ }
+
+ $body.="\n";
+
+ } else {
+
+ $body.="\n";
+ if ($namespace === 'DAV:') {
+ $body.=' ';
+ } else {
+ $body.=" ";
+ }
+ // Shitty.. i know
+ $body.=htmlspecialchars($propValue, ENT_NOQUOTES, 'UTF-8');
+ if ($namespace === 'DAV:') {
+ $body.='' . "\n";
+ } else {
+ $body.="\n";
+ }
+ $body.="\n";
+
+ }
+
+ }
+
+ $body.= '';
+
+ $this->request('PROPPATCH', $url, $body, array(
+ 'Content-Type' => 'application/xml'
+ ));
+
+ }
+
+ /**
+ * Performs an HTTP options request
+ *
+ * This method returns all the features from the 'DAV:' header as an array.
+ * If there was no DAV header, or no contents this method will return an
+ * empty array.
+ *
+ * @return array
+ */
+ public function options() {
+
+ $result = $this->request('OPTIONS');
+ if (!isset($result['headers']['dav'])) {
+ return array();
+ }
+
+ $features = explode(',', $result['headers']['dav']);
+ foreach($features as &$v) {
+ $v = trim($v);
+ }
+ return $features;
+
+ }
+
+ /**
+ * Performs an actual HTTP request, and returns the result.
+ *
+ * If the specified url is relative, it will be expanded based on the base
+ * url.
+ *
+ * The returned array contains 3 keys:
+ * * body - the response body
+ * * httpCode - a HTTP code (200, 404, etc)
+ * * headers - a list of response http headers. The header names have
+ * been lowercased.
+ *
+ * @param string $method
+ * @param string $url
+ * @param string $body
+ * @param array $headers
+ * @return array
+ */
+ public function request($method, $url = '', $body = null, $headers = array()) {
+
+ $url = $this->getAbsoluteUrl($url);
+
+ $curlSettings = array(
+ CURLOPT_RETURNTRANSFER => true,
+ // Return headers as part of the response
+ CURLOPT_HEADER => true,
+
+ // For security we cast this to a string. If somehow an array could
+ // be passed here, it would be possible for an attacker to use @ to
+ // post local files.
+ CURLOPT_POSTFIELDS => (string)$body,
+ // Automatically follow redirects
+ CURLOPT_FOLLOWLOCATION => true,
+ CURLOPT_MAXREDIRS => 5,
+ CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
+ CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
+ );
+
+ if($this->verifyPeer !== null) {
+ $curlSettings[CURLOPT_SSL_VERIFYPEER] = $this->verifyPeer;
+ }
+
+ if($this->trustedCertificates) {
+ $curlSettings[CURLOPT_CAINFO] = $this->trustedCertificates;
+ }
+
+ switch ($method) {
+ case 'HEAD' :
+
+ // do not read body with HEAD requests (this is necessary because cURL does not ignore the body with HEAD
+ // requests when the Content-Length header is given - which in turn is perfectly valid according to HTTP
+ // specs...) cURL does unfortunately return an error in this case ("transfer closed transfer closed with
+ // ... bytes remaining to read") this can be circumvented by explicitly telling cURL to ignore the
+ // response body
+ $curlSettings[CURLOPT_NOBODY] = true;
+ $curlSettings[CURLOPT_CUSTOMREQUEST] = 'HEAD';
+ break;
+
+ default:
+ $curlSettings[CURLOPT_CUSTOMREQUEST] = $method;
+ break;
+
+ }
+
+ // Adding HTTP headers
+ $nHeaders = array();
+ foreach($headers as $key=>$value) {
+
+ $nHeaders[] = $key . ': ' . $value;
+
+ }
+ $curlSettings[CURLOPT_HTTPHEADER] = $nHeaders;
+
+ if ($this->proxy) {
+ $curlSettings[CURLOPT_PROXY] = $this->proxy;
+ }
+
+ if ($this->userName && $this->authType) {
+ $curlType = 0;
+ if ($this->authType & self::AUTH_BASIC) {
+ $curlType |= CURLAUTH_BASIC;
+ }
+ if ($this->authType & self::AUTH_DIGEST) {
+ $curlType |= CURLAUTH_DIGEST;
+ }
+ $curlSettings[CURLOPT_HTTPAUTH] = $curlType;
+ $curlSettings[CURLOPT_USERPWD] = $this->userName . ':' . $this->password;
+ }
+
+ list(
+ $response,
+ $curlInfo,
+ $curlErrNo,
+ $curlError
+ ) = $this->curlRequest($url, $curlSettings);
+
+ $headerBlob = substr($response, 0, $curlInfo['header_size']);
+ $response = substr($response, $curlInfo['header_size']);
+
+ // In the case of 100 Continue, or redirects we'll have multiple lists
+ // of headers for each separate HTTP response. We can easily split this
+ // because they are separated by \r\n\r\n
+ $headerBlob = explode("\r\n\r\n", trim($headerBlob, "\r\n"));
+
+ // We only care about the last set of headers
+ $headerBlob = $headerBlob[count($headerBlob)-1];
+
+ // Splitting headers
+ $headerBlob = explode("\r\n", $headerBlob);
+
+ $headers = array();
+ foreach($headerBlob as $header) {
+ $parts = explode(':', $header, 2);
+ if (count($parts)==2) {
+ $headers[strtolower(trim($parts[0]))] = trim($parts[1]);
+ }
+ }
+
+ $response = array(
+ 'body' => $response,
+ 'statusCode' => $curlInfo['http_code'],
+ 'headers' => $headers
+ );
+
+ if ($curlErrNo) {
+ throw new Exception('[CURL] Error while making request: ' . $curlError . ' (error code: ' . $curlErrNo . ')');
+ }
+
+ if ($response['statusCode']>=400) {
+ switch ($response['statusCode']) {
+ case 400 :
+ throw new Exception\BadRequest('Bad request');
+ case 401 :
+ throw new Exception\NotAuthenticated('Not authenticated');
+ case 402 :
+ throw new Exception\PaymentRequired('Payment required');
+ case 403 :
+ throw new Exception\Forbidden('Forbidden');
+ case 404:
+ throw new Exception\NotFound('Resource not found.');
+ case 405 :
+ throw new Exception\MethodNotAllowed('Method not allowed');
+ case 409 :
+ throw new Exception\Conflict('Conflict');
+ case 412 :
+ throw new Exception\PreconditionFailed('Precondition failed');
+ case 416 :
+ throw new Exception\RequestedRangeNotSatisfiable('Requested Range Not Satisfiable');
+ case 500 :
+ throw new Exception('Internal server error');
+ case 501 :
+ throw new Exception\NotImplemented('Not Implemented');
+ case 507 :
+ throw new Exception\InsufficientStorage('Insufficient storage');
+ default:
+ throw new Exception('HTTP error response. (errorcode ' . $response['statusCode'] . ')');
+ }
+ }
+
+ return $response;
+
+ }
+
+ /**
+ * Wrapper for all curl functions.
+ *
+ * The only reason this was split out in a separate method, is so it
+ * becomes easier to unittest.
+ *
+ * @param string $url
+ * @param array $settings
+ * @return array
+ */
+ // @codeCoverageIgnoreStart
+ protected function curlRequest($url, $settings) {
+
+ $curl = curl_init($url);
+ curl_setopt_array($curl, $settings);
+
+ return array(
+ curl_exec($curl),
+ curl_getinfo($curl),
+ curl_errno($curl),
+ curl_error($curl)
+ );
+
+ }
+ // @codeCoverageIgnoreEnd
+
+ /**
+ * Returns the full url based on the given url (which may be relative). All
+ * urls are expanded based on the base url as given by the server.
+ *
+ * @param string $url
+ * @return string
+ */
+ protected function getAbsoluteUrl($url) {
+
+ // If the url starts with http:// or https://, the url is already absolute.
+ if (preg_match('/^http(s?):\/\//', $url)) {
+ return $url;
+ }
+
+ // If the url starts with a slash, we must calculate the url based off
+ // the root of the base url.
+ if (strpos($url,'/') === 0) {
+ $parts = parse_url($this->baseUri);
+ return $parts['scheme'] . '://' . $parts['host'] . (isset($parts['port'])?':' . $parts['port']:'') . $url;
+ }
+
+ // Otherwise...
+ return $this->baseUri . $url;
+
+ }
+
+ /**
+ * Parses a WebDAV multistatus response body
+ *
+ * This method returns an array with the following structure
+ *
+ * array(
+ * 'url/to/resource' => array(
+ * '200' => array(
+ * '{DAV:}property1' => 'value1',
+ * '{DAV:}property2' => 'value2',
+ * ),
+ * '404' => array(
+ * '{DAV:}property1' => null,
+ * '{DAV:}property2' => null,
+ * ),
+ * )
+ * 'url/to/resource2' => array(
+ * .. etc ..
+ * )
+ * )
+ *
+ *
+ * @param string $body xml body
+ * @return array
+ */
+ public function parseMultiStatus($body) {
+
+ $body = XMLUtil::convertDAVNamespace($body);
+
+ // Fixes an XXE vulnerability on PHP versions older than 5.3.23 or
+ // 5.4.13.
+ $previous = libxml_disable_entity_loader(true);
+ $responseXML = simplexml_load_string($body, null, LIBXML_NOBLANKS | LIBXML_NOCDATA);
+ libxml_disable_entity_loader($previous);
+
+ if ($responseXML===false) {
+ throw new \InvalidArgumentException('The passed data is not valid XML');
+ }
+
+ $responseXML->registerXPathNamespace('d', 'urn:DAV');
+
+ $propResult = array();
+
+ foreach($responseXML->xpath('d:response') as $response) {
+ $response->registerXPathNamespace('d', 'urn:DAV');
+ $href = $response->xpath('d:href');
+ $href = (string)$href[0];
+
+ $properties = array();
+
+ foreach($response->xpath('d:propstat') as $propStat) {
+
+ $propStat->registerXPathNamespace('d', 'urn:DAV');
+ $status = $propStat->xpath('d:status');
+ list($httpVersion, $statusCode, $message) = explode(' ', (string)$status[0],3);
+
+ // Only using the propertymap for results with status 200.
+ $propertyMap = $statusCode==='200' ? $this->propertyMap : array();
+
+ $properties[$statusCode] = XMLUtil::parseProperties(dom_import_simplexml($propStat), $propertyMap);
+
+ }
+
+ $propResult[$href] = $properties;
+
+ }
+
+ return $propResult;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Collection.php b/app/Gdoo/Calendar/Sabre/DAV/Collection.php
new file mode 100644
index 00000000..caec0293
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Collection.php
@@ -0,0 +1,110 @@
+getChildren() as $child) {
+
+ if ($child->getName()==$name) return $child;
+
+ }
+ throw new Exception\NotFound('File not found: ' . $name);
+
+ }
+
+ /**
+ * Checks is a child-node exists.
+ *
+ * It is generally a good idea to try and override this. Usually it can be optimized.
+ *
+ * @param string $name
+ * @return bool
+ */
+ public function childExists($name) {
+
+ try {
+
+ $this->getChild($name);
+ return true;
+
+ } catch(Exception\NotFound $e) {
+
+ return false;
+
+ }
+
+ }
+
+ /**
+ * Creates a new file in the directory
+ *
+ * Data will either be supplied as a stream resource, or in certain cases
+ * as a string. Keep in mind that you may have to support either.
+ *
+ * After succesful creation of the file, you may choose to return the ETag
+ * of the new file here.
+ *
+ * The returned ETag must be surrounded by double-quotes (The quotes should
+ * be part of the actual string).
+ *
+ * If you cannot accurately determine the ETag, you should not return it.
+ * If you don't store the file exactly as-is (you're transforming it
+ * somehow) you should also not return an ETag.
+ *
+ * This means that if a subsequent GET to this new file does not exactly
+ * return the same contents of what was submitted here, you are strongly
+ * recommended to omit the ETag.
+ *
+ * @param string $name Name of the file
+ * @param resource|string $data Initial payload
+ * @return null|string
+ */
+ public function createFile($name, $data = null) {
+
+ throw new Exception\Forbidden('Permission denied to create file (filename ' . $name . ')');
+
+ }
+
+ /**
+ * Creates a new subdirectory
+ *
+ * @param string $name
+ * @throws Exception\Forbidden
+ * @return void
+ */
+ public function createDirectory($name) {
+
+ throw new Exception\Forbidden('Permission denied to create directory');
+
+ }
+
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Exception.php b/app/Gdoo/Calendar/Sabre/DAV/Exception.php
new file mode 100644
index 00000000..af47c36a
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Exception.php
@@ -0,0 +1,64 @@
+lock) {
+ $error = $errorNode->ownerDocument->createElementNS('DAV:','d:no-conflicting-lock');
+ $errorNode->appendChild($error);
+ if (!is_object($this->lock)) var_dump($this->lock);
+ $error->appendChild($errorNode->ownerDocument->createElementNS('DAV:','d:href',$this->lock->uri));
+ }
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Exception/FileNotFound.php b/app/Gdoo/Calendar/Sabre/DAV/Exception/FileNotFound.php
new file mode 100644
index 00000000..863302bb
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Exception/FileNotFound.php
@@ -0,0 +1,19 @@
+ownerDocument->createElementNS('DAV:','d:valid-resourcetype');
+ $errorNode->appendChild($error);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Exception/LengthRequired.php b/app/Gdoo/Calendar/Sabre/DAV/Exception/LengthRequired.php
new file mode 100644
index 00000000..d2a5a8e8
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Exception/LengthRequired.php
@@ -0,0 +1,30 @@
+message = 'The locktoken supplied does not match any locks on this entity';
+
+ }
+
+ /**
+ * This method allows the exception to include additional information into the WebDAV error response
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $errorNode
+ * @return void
+ */
+ public function serialize(DAV\Server $server,\DOMElement $errorNode) {
+
+ $error = $errorNode->ownerDocument->createElementNS('DAV:','d:lock-token-matches-request-uri');
+ $errorNode->appendChild($error);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Exception/Locked.php b/app/Gdoo/Calendar/Sabre/DAV/Exception/Locked.php
new file mode 100644
index 00000000..dfba856e
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Exception/Locked.php
@@ -0,0 +1,73 @@
+lock = $lock;
+
+ }
+
+ /**
+ * Returns the HTTP statuscode for this exception
+ *
+ * @return int
+ */
+ public function getHTTPCode() {
+
+ return 423;
+
+ }
+
+ /**
+ * This method allows the exception to include additional information into the WebDAV error response
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $errorNode
+ * @return void
+ */
+ public function serialize(DAV\Server $server,\DOMElement $errorNode) {
+
+ if ($this->lock) {
+ $error = $errorNode->ownerDocument->createElementNS('DAV:','d:lock-token-submitted');
+ $errorNode->appendChild($error);
+
+ $href = $errorNode->ownerDocument->createElementNS('DAV:','d:href');
+ $href->appendChild($errorNode->ownerDocument->createTextNode($this->lock->uri));
+ $error->appendChild(
+ $href
+ );
+ }
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Exception/MethodNotAllowed.php b/app/Gdoo/Calendar/Sabre/DAV/Exception/MethodNotAllowed.php
new file mode 100644
index 00000000..ad043290
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Exception/MethodNotAllowed.php
@@ -0,0 +1,45 @@
+getAllowedMethods($server->getRequestUri());
+
+ return array(
+ 'Allow' => strtoupper(implode(', ',$methods)),
+ );
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Exception/NotAuthenticated.php b/app/Gdoo/Calendar/Sabre/DAV/Exception/NotAuthenticated.php
new file mode 100644
index 00000000..26eb61d3
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Exception/NotAuthenticated.php
@@ -0,0 +1,30 @@
+header = $header;
+
+ }
+
+ /**
+ * Returns the HTTP statuscode for this exception
+ *
+ * @return int
+ */
+ public function getHTTPCode() {
+
+ return 412;
+
+ }
+
+ /**
+ * This method allows the exception to include additional information into the WebDAV error response
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $errorNode
+ * @return void
+ */
+ public function serialize(DAV\Server $server,\DOMElement $errorNode) {
+
+ if ($this->header) {
+ $prop = $errorNode->ownerDocument->createElement('s:header');
+ $prop->nodeValue = $this->header;
+ $errorNode->appendChild($prop);
+ }
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Exception/ReportNotSupported.php b/app/Gdoo/Calendar/Sabre/DAV/Exception/ReportNotSupported.php
new file mode 100644
index 00000000..2bfa3366
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Exception/ReportNotSupported.php
@@ -0,0 +1,32 @@
+ownerDocument->createElementNS('DAV:','d:supported-report');
+ $errorNode->appendChild($error);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Exception/RequestedRangeNotSatisfiable.php b/app/Gdoo/Calendar/Sabre/DAV/Exception/RequestedRangeNotSatisfiable.php
new file mode 100644
index 00000000..2651b1d3
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Exception/RequestedRangeNotSatisfiable.php
@@ -0,0 +1,31 @@
+
+ * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
+ * @license http://sabre.io/license/ Modified BSD License
+ */
+class ServiceUnavailable extends DAV\Exception {
+
+ /**
+ * Returns the HTTP statuscode for this exception
+ *
+ * @return int
+ */
+ public function getHTTPCode() {
+
+ return 503;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Exception/UnsupportedMediaType.php b/app/Gdoo/Calendar/Sabre/DAV/Exception/UnsupportedMediaType.php
new file mode 100644
index 00000000..990ca0cc
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Exception/UnsupportedMediaType.php
@@ -0,0 +1,28 @@
+path . '/' . $name;
+ file_put_contents($newPath,$data);
+
+ }
+
+ /**
+ * Creates a new subdirectory
+ *
+ * @param string $name
+ * @return void
+ */
+ public function createDirectory($name) {
+
+ $newPath = $this->path . '/' . $name;
+ mkdir($newPath);
+
+ }
+
+ /**
+ * Returns a specific child node, referenced by its name
+ *
+ * This method must throw DAV\Exception\NotFound if the node does not
+ * exist.
+ *
+ * @param string $name
+ * @throws DAV\Exception\NotFound
+ * @return DAV\INode
+ */
+ public function getChild($name) {
+
+ $path = $this->path . '/' . $name;
+
+ if (!file_exists($path)) throw new DAV\Exception\NotFound('File with name ' . $path . ' could not be located');
+
+ if (is_dir($path)) {
+
+ return new Directory($path);
+
+ } else {
+
+ return new File($path);
+
+ }
+
+ }
+
+ /**
+ * Returns an array with all the child nodes
+ *
+ * @return DAV\INode[]
+ */
+ public function getChildren() {
+
+ $nodes = array();
+ foreach(scandir($this->path) as $node) if($node!='.' && $node!='..') $nodes[] = $this->getChild($node);
+ return $nodes;
+
+ }
+
+ /**
+ * Checks if a child exists.
+ *
+ * @param string $name
+ * @return bool
+ */
+ public function childExists($name) {
+
+ $path = $this->path . '/' . $name;
+ return file_exists($path);
+
+ }
+
+ /**
+ * Deletes all files in this directory, and then itself
+ *
+ * @return void
+ */
+ public function delete() {
+
+ foreach($this->getChildren() as $child) $child->delete();
+ rmdir($this->path);
+
+ }
+
+ /**
+ * Returns available diskspace information
+ *
+ * @return array
+ */
+ public function getQuotaInfo() {
+
+ return array(
+ disk_total_space($this->path)-disk_free_space($this->path),
+ disk_free_space($this->path)
+ );
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/FS/File.php b/app/Gdoo/Calendar/Sabre/DAV/FS/File.php
new file mode 100644
index 00000000..c51635df
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/FS/File.php
@@ -0,0 +1,91 @@
+path,$data);
+
+ }
+
+ /**
+ * Returns the data
+ *
+ * @return string
+ */
+ public function get() {
+
+ return fopen($this->path,'r');
+
+ }
+
+ /**
+ * Delete the current file
+ *
+ * @return void
+ */
+ public function delete() {
+
+ unlink($this->path);
+
+ }
+
+ /**
+ * Returns the size of the node, in bytes
+ *
+ * @return int
+ */
+ public function getSize() {
+
+ return filesize($this->path);
+
+ }
+
+ /**
+ * Returns the ETag for a file
+ *
+ * An ETag is a unique identifier representing the current version of the file. If the file changes, the ETag MUST change.
+ * The ETag is an arbitrary string, but MUST be surrounded by double-quotes.
+ *
+ * Return null if the ETag can not effectively be determined
+ *
+ * @return mixed
+ */
+ public function getETag() {
+
+ return null;
+
+ }
+
+ /**
+ * Returns the mime-type for a file
+ *
+ * If null is returned, we'll assume application/octet-stream
+ *
+ * @return mixed
+ */
+ public function getContentType() {
+
+ return null;
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/FS/Node.php b/app/Gdoo/Calendar/Sabre/DAV/FS/Node.php
new file mode 100644
index 00000000..e3f2674e
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/FS/Node.php
@@ -0,0 +1,82 @@
+path = $path;
+
+ }
+
+
+
+ /**
+ * Returns the name of the node
+ *
+ * @return string
+ */
+ public function getName() {
+
+ list(, $name) = DAV\URLUtil::splitPath($this->path);
+ return $name;
+
+ }
+
+ /**
+ * Renames the node
+ *
+ * @param string $name The new name
+ * @return void
+ */
+ public function setName($name) {
+
+ list($parentPath, ) = DAV\URLUtil::splitPath($this->path);
+ list(, $newName) = DAV\URLUtil::splitPath($name);
+
+ $newPath = $parentPath . '/' . $newName;
+ rename($this->path,$newPath);
+
+ $this->path = $newPath;
+
+ }
+
+
+
+ /**
+ * Returns the last modification time, as a unix timestamp
+ *
+ * @return int
+ */
+ public function getLastModified() {
+
+ return filemtime($this->path);
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/FSExt/Directory.php b/app/Gdoo/Calendar/Sabre/DAV/FSExt/Directory.php
new file mode 100644
index 00000000..3774adb7
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/FSExt/Directory.php
@@ -0,0 +1,159 @@
+path . '/' . $name;
+ file_put_contents($newPath,$data);
+
+ return '"' . md5_file($newPath) . '"';
+
+ }
+
+ /**
+ * Creates a new subdirectory
+ *
+ * @param string $name
+ * @return void
+ */
+ public function createDirectory($name) {
+
+ // We're not allowing dots
+ if ($name=='.' || $name=='..') throw new DAV\Exception\Forbidden('Permission denied to . and ..');
+ $newPath = $this->path . '/' . $name;
+ mkdir($newPath);
+
+ }
+
+ /**
+ * Returns a specific child node, referenced by its name
+ *
+ * This method must throw Sabre\DAV\Exception\NotFound if the node does not
+ * exist.
+ *
+ * @param string $name
+ * @throws DAV\Exception\NotFound
+ * @return DAV\INode
+ */
+ public function getChild($name) {
+
+ $path = $this->path . '/' . $name;
+
+ if (!file_exists($path)) throw new DAV\Exception\NotFound('File could not be located');
+ if ($name=='.' || $name=='..') throw new DAV\Exception\Forbidden('Permission denied to . and ..');
+
+ if (is_dir($path)) {
+
+ return new Directory($path);
+
+ } else {
+
+ return new File($path);
+
+ }
+
+ }
+
+ /**
+ * Checks if a child exists.
+ *
+ * @param string $name
+ * @return bool
+ */
+ public function childExists($name) {
+
+ if ($name=='.' || $name=='..')
+ throw new DAV\Exception\Forbidden('Permission denied to . and ..');
+
+ $path = $this->path . '/' . $name;
+ return file_exists($path);
+
+ }
+
+ /**
+ * Returns an array with all the child nodes
+ *
+ * @return DAV\INode[]
+ */
+ public function getChildren() {
+
+ $nodes = array();
+ foreach(scandir($this->path) as $node) if($node!='.' && $node!='..' && $node!='.sabredav') $nodes[] = $this->getChild($node);
+ return $nodes;
+
+ }
+
+ /**
+ * Deletes all files in this directory, and then itself
+ *
+ * @return bool
+ */
+ public function delete() {
+
+ // Deleting all children
+ foreach($this->getChildren() as $child) $child->delete();
+
+ // Removing resource info, if its still around
+ if (file_exists($this->path . '/.sabredav')) unlink($this->path . '/.sabredav');
+
+ // Removing the directory itself
+ rmdir($this->path);
+
+ return parent::delete();
+
+ }
+
+ /**
+ * Returns available diskspace information
+ *
+ * @return array
+ */
+ public function getQuotaInfo() {
+
+ return array(
+ disk_total_space($this->path)-disk_free_space($this->path),
+ disk_free_space($this->path)
+ );
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/FSExt/File.php b/app/Gdoo/Calendar/Sabre/DAV/FSExt/File.php
new file mode 100644
index 00000000..1b7f0084
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/FSExt/File.php
@@ -0,0 +1,146 @@
+path,$data);
+ return '"' . md5_file($this->path) . '"';
+
+ }
+
+ /**
+ * Updates the file based on a range specification.
+ *
+ * The first argument is the data, which is either a readable stream
+ * resource or a string.
+ *
+ * The second argument is the type of update we're doing.
+ * This is either:
+ * * 1. append
+ * * 2. update based on a start byte
+ * * 3. update based on an end byte
+ *;
+ * The third argument is the start or end byte.
+ *
+ * After a successful put operation, you may choose to return an ETag. The
+ * etag must always be surrounded by double-quotes. These quotes must
+ * appear in the actual string you're returning.
+ *
+ * Clients may use the ETag from a PUT request to later on make sure that
+ * when they update the file, the contents haven't changed in the mean
+ * time.
+ *
+ * @param resource|string $data
+ * @param int $rangeType
+ * @param int $offset
+ * @return string|null
+ */
+ public function patch($data, $rangeType, $offset = null) {
+
+ switch($rangeType) {
+ case 1 :
+ $f = fopen($this->path, 'a');
+ break;
+ case 2 :
+ $f = fopen($this->path, 'c');
+ fseek($f,$offset);
+ break;
+ case 3 :
+ $f = fopen($this->path, 'c');
+ fseek($f, $offset, SEEK_END);
+ break;
+ }
+ if (is_string($data)) {
+ fwrite($f, $data);
+ } else {
+ stream_copy_to_stream($data,$f);
+ }
+ fclose($f);
+ return '"' . md5_file($this->path) . '"';
+
+ }
+
+ /**
+ * Returns the data
+ *
+ * @return resource
+ */
+ public function get() {
+
+ return fopen($this->path,'r');
+
+ }
+
+ /**
+ * Delete the current file
+ *
+ * @return bool
+ */
+ public function delete() {
+
+ unlink($this->path);
+ return parent::delete();
+
+ }
+
+ /**
+ * Returns the ETag for a file
+ *
+ * An ETag is a unique identifier representing the current version of the file. If the file changes, the ETag MUST change.
+ * The ETag is an arbitrary string, but MUST be surrounded by double-quotes.
+ *
+ * Return null if the ETag can not effectively be determined
+ *
+ * @return string|null
+ */
+ public function getETag() {
+
+ return '"' . md5_file($this->path). '"';
+
+ }
+
+ /**
+ * Returns the mime-type for a file
+ *
+ * If null is returned, we'll assume application/octet-stream
+ *
+ * @return string|null
+ */
+ public function getContentType() {
+
+ return null;
+
+ }
+
+ /**
+ * Returns the size of the file, in bytes
+ *
+ * @return int
+ */
+ public function getSize() {
+
+ return filesize($this->path);
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/FSExt/Node.php b/app/Gdoo/Calendar/Sabre/DAV/FSExt/Node.php
new file mode 100644
index 00000000..73467fff
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/FSExt/Node.php
@@ -0,0 +1,214 @@
+getResourceData();
+
+ foreach($properties as $propertyName=>$propertyValue) {
+
+ // If it was null, we need to delete the property
+ if (is_null($propertyValue)) {
+ if (isset($resourceData['properties'][$propertyName])) {
+ unset($resourceData['properties'][$propertyName]);
+ }
+ } else {
+ $resourceData['properties'][$propertyName] = $propertyValue;
+ }
+
+ }
+
+ $this->putResourceData($resourceData);
+ return true;
+ }
+
+ /**
+ * Returns a list of properties for this nodes.;
+ *
+ * The properties list is a list of propertynames the client requested, encoded as xmlnamespace#tagName, for example: http://www.example.org/namespace#author
+ * If the array is empty, all properties should be returned
+ *
+ * @param array $properties
+ * @return array
+ */
+ function getProperties($properties) {
+
+ $resourceData = $this->getResourceData();
+
+ // if the array was empty, we need to return everything
+ if (!$properties) return $resourceData['properties'];
+
+ $props = array();
+ foreach($properties as $property) {
+ if (isset($resourceData['properties'][$property])) $props[$property] = $resourceData['properties'][$property];
+ }
+
+ return $props;
+
+ }
+
+ /**
+ * Returns the path to the resource file
+ *
+ * @return string
+ */
+ protected function getResourceInfoPath() {
+
+ list($parentDir) = DAV\URLUtil::splitPath($this->path);
+ return $parentDir . '/.sabredav';
+
+ }
+
+ /**
+ * Returns all the stored resource information
+ *
+ * @return array
+ */
+ protected function getResourceData() {
+
+ $path = $this->getResourceInfoPath();
+ if (!file_exists($path)) return array('properties' => array());
+
+ // opening up the file, and creating a shared lock
+ $handle = fopen($path,'r');
+ flock($handle,LOCK_SH);
+ $data = '';
+
+ // Reading data until the eof
+ while(!feof($handle)) {
+ $data.=fread($handle,8192);
+ }
+
+ // We're all good
+ fclose($handle);
+
+ // Unserializing and checking if the resource file contains data for this file
+ $data = unserialize($data);
+ if (!isset($data[$this->getName()])) {
+ return array('properties' => array());
+ }
+
+ $data = $data[$this->getName()];
+ if (!isset($data['properties'])) $data['properties'] = array();
+ return $data;
+
+ }
+
+ /**
+ * Updates the resource information
+ *
+ * @param array $newData
+ * @return void
+ */
+ protected function putResourceData(array $newData) {
+
+ $path = $this->getResourceInfoPath();
+
+ // opening up the file, and creating a shared lock
+ $handle = fopen($path,'a+');
+ flock($handle,LOCK_EX);
+ $data = '';
+
+ rewind($handle);
+
+ // Reading data until the eof
+ while(!feof($handle)) {
+ $data.=fread($handle,8192);
+ }
+
+ // Unserializing and checking if the resource file contains data for this file
+ $data = unserialize($data);
+ $data[$this->getName()] = $newData;
+ ftruncate($handle,0);
+ rewind($handle);
+
+ fwrite($handle,serialize($data));
+ fclose($handle);
+
+ }
+
+ /**
+ * Renames the node
+ *
+ * @param string $name The new name
+ * @return void
+ */
+ public function setName($name) {
+
+ list($parentPath, ) = DAV\URLUtil::splitPath($this->path);
+ list(, $newName) = DAV\URLUtil::splitPath($name);
+ $newPath = $parentPath . '/' . $newName;
+
+ // We're deleting the existing resourcedata, and recreating it
+ // for the new path.
+ $resourceData = $this->getResourceData();
+ $this->deleteResourceData();
+
+ rename($this->path,$newPath);
+ $this->path = $newPath;
+ $this->putResourceData($resourceData);
+
+
+ }
+
+ /**
+ * @return bool
+ */
+ public function deleteResourceData() {
+
+ // When we're deleting this node, we also need to delete any resource information
+ $path = $this->getResourceInfoPath();
+ if (!file_exists($path)) return true;
+
+ // opening up the file, and creating a shared lock
+ $handle = fopen($path,'a+');
+ flock($handle,LOCK_EX);
+ $data = '';
+
+ rewind($handle);
+
+ // Reading data until the eof
+ while(!feof($handle)) {
+ $data.=fread($handle,8192);
+ }
+
+ // Unserializing and checking if the resource file contains data for this file
+ $data = unserialize($data);
+ if (isset($data[$this->getName()])) unset($data[$this->getName()]);
+ ftruncate($handle,0);
+ rewind($handle);
+ fwrite($handle,serialize($data));
+ fclose($handle);
+
+ return true;
+ }
+
+ public function delete() {
+
+ return $this->deleteResourceData();
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/File.php b/app/Gdoo/Calendar/Sabre/DAV/File.php
new file mode 100644
index 00000000..caf3a2cc
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/File.php
@@ -0,0 +1,85 @@
+ array(
+ * '{DAV:}displayname' => null,
+ * ),
+ * 424 => array(
+ * '{DAV:}owner' => null,
+ * )
+ * )
+ *
+ * In this example it was forbidden to update {DAV:}displayname.
+ * (403 Forbidden), which in turn also caused {DAV:}owner to fail
+ * (424 Failed Dependency) because the request needs to be atomic.
+ *
+ * @param array $mutations
+ * @return bool|array
+ */
+ function updateProperties($mutations);
+
+ /**
+ * Returns a list of properties for this nodes.
+ *
+ * The properties list is a list of propertynames the client requested,
+ * encoded in clark-notation {xmlnamespace}tagname
+ *
+ * If the array is empty, it means 'all properties' were requested.
+ *
+ * Note that it's fine to liberally give properties back, instead of
+ * conforming to the list of requested properties.
+ * The Server class will filter out the extra.
+ *
+ * @param array $properties
+ * @return void
+ */
+ function getProperties($properties);
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/IQuota.php b/app/Gdoo/Calendar/Sabre/DAV/IQuota.php
new file mode 100644
index 00000000..74b759cc
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/IQuota.php
@@ -0,0 +1,27 @@
+dataDir = $dataDir;
+
+ }
+
+ protected function getFileNameForUri($uri) {
+
+ return $this->dataDir . '/sabredav_' . md5($uri) . '.locks';
+
+ }
+
+
+ /**
+ * Returns a list of Sabre\DAV\Locks\LockInfo objects
+ *
+ * This method should return all the locks for a particular uri, including
+ * locks that might be set on a parent uri.
+ *
+ * If returnChildLocks is set to true, this method should also look for
+ * any locks in the subtree of the uri for locks.
+ *
+ * @param string $uri
+ * @param bool $returnChildLocks
+ * @return array
+ */
+ public function getLocks($uri, $returnChildLocks) {
+
+ $lockList = array();
+ $currentPath = '';
+
+ foreach(explode('/',$uri) as $uriPart) {
+
+ // weird algorithm that can probably be improved, but we're traversing the path top down
+ if ($currentPath) $currentPath.='/';
+ $currentPath.=$uriPart;
+
+ $uriLocks = $this->getData($currentPath);
+
+ foreach($uriLocks as $uriLock) {
+
+ // Unless we're on the leaf of the uri-tree we should ignore locks with depth 0
+ if($uri==$currentPath || $uriLock->depth!=0) {
+ $uriLock->uri = $currentPath;
+ $lockList[] = $uriLock;
+ }
+
+ }
+
+ }
+
+ // Checking if we can remove any of these locks
+ foreach($lockList as $k=>$lock) {
+ if (time() > $lock->timeout + $lock->created) unset($lockList[$k]);
+ }
+ return $lockList;
+
+ }
+
+ /**
+ * Locks a uri
+ *
+ * @param string $uri
+ * @param LockInfo $lockInfo
+ * @return bool
+ */
+ public function lock($uri, LockInfo $lockInfo) {
+
+ // We're making the lock timeout 30 minutes
+ $lockInfo->timeout = 1800;
+ $lockInfo->created = time();
+
+ $locks = $this->getLocks($uri,false);
+ foreach($locks as $k=>$lock) {
+ if ($lock->token == $lockInfo->token) unset($locks[$k]);
+ }
+ $locks[] = $lockInfo;
+ $this->putData($uri,$locks);
+ return true;
+
+ }
+
+ /**
+ * Removes a lock from a uri
+ *
+ * @param string $uri
+ * @param LockInfo $lockInfo
+ * @return bool
+ */
+ public function unlock($uri, LockInfo $lockInfo) {
+
+ $locks = $this->getLocks($uri,false);
+ foreach($locks as $k=>$lock) {
+
+ if ($lock->token == $lockInfo->token) {
+
+ unset($locks[$k]);
+ $this->putData($uri,$locks);
+ return true;
+
+ }
+ }
+ return false;
+
+ }
+
+ /**
+ * Returns the stored data for a uri
+ *
+ * @param string $uri
+ * @return array
+ */
+ protected function getData($uri) {
+
+ $path = $this->getFilenameForUri($uri);
+ if (!file_exists($path)) return array();
+
+ // opening up the file, and creating a shared lock
+ $handle = fopen($path,'r');
+ flock($handle,LOCK_SH);
+ $data = '';
+
+ // Reading data until the eof
+ while(!feof($handle)) {
+ $data.=fread($handle,8192);
+ }
+
+ // We're all good
+ fclose($handle);
+
+ // Unserializing and checking if the resource file contains data for this file
+ $data = unserialize($data);
+ if (!$data) return array();
+ return $data;
+
+ }
+
+ /**
+ * Updates the lock information
+ *
+ * @param string $uri
+ * @param array $newData
+ * @return void
+ */
+ protected function putData($uri,array $newData) {
+
+ $path = $this->getFileNameForUri($uri);
+
+ // opening up the file, and creating a shared lock
+ $handle = fopen($path,'a+');
+ flock($handle,LOCK_EX);
+ ftruncate($handle,0);
+ rewind($handle);
+
+ fwrite($handle,serialize($newData));
+ fclose($handle);
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Locks/Backend/File.php b/app/Gdoo/Calendar/Sabre/DAV/Locks/Backend/File.php
new file mode 100644
index 00000000..891aa21a
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Locks/Backend/File.php
@@ -0,0 +1,183 @@
+locksFile = $locksFile;
+
+ }
+
+ /**
+ * Returns a list of Sabre\DAV\Locks\LockInfo objects
+ *
+ * This method should return all the locks for a particular uri, including
+ * locks that might be set on a parent uri.
+ *
+ * If returnChildLocks is set to true, this method should also look for
+ * any locks in the subtree of the uri for locks.
+ *
+ * @param string $uri
+ * @param bool $returnChildLocks
+ * @return array
+ */
+ public function getLocks($uri, $returnChildLocks) {
+
+ $newLocks = array();
+
+ $locks = $this->getData();
+
+ foreach($locks as $lock) {
+
+ if ($lock->uri === $uri ||
+ //deep locks on parents
+ ($lock->depth!=0 && strpos($uri, $lock->uri . '/')===0) ||
+
+ // locks on children
+ ($returnChildLocks && (strpos($lock->uri, $uri . '/')===0)) ) {
+
+ $newLocks[] = $lock;
+
+ }
+
+ }
+
+ // Checking if we can remove any of these locks
+ foreach($newLocks as $k=>$lock) {
+ if (time() > $lock->timeout + $lock->created) unset($newLocks[$k]);
+ }
+ return $newLocks;
+
+ }
+
+ /**
+ * Locks a uri
+ *
+ * @param string $uri
+ * @param LockInfo $lockInfo
+ * @return bool
+ */
+ public function lock($uri, LockInfo $lockInfo) {
+
+ // We're making the lock timeout 30 minutes
+ $lockInfo->timeout = 1800;
+ $lockInfo->created = time();
+ $lockInfo->uri = $uri;
+
+ $locks = $this->getData();
+
+ foreach($locks as $k=>$lock) {
+ if (
+ ($lock->token == $lockInfo->token) ||
+ (time() > $lock->timeout + $lock->created)
+ ) {
+ unset($locks[$k]);
+ }
+ }
+ $locks[] = $lockInfo;
+ $this->putData($locks);
+ return true;
+
+ }
+
+ /**
+ * Removes a lock from a uri
+ *
+ * @param string $uri
+ * @param LockInfo $lockInfo
+ * @return bool
+ */
+ public function unlock($uri, LockInfo $lockInfo) {
+
+ $locks = $this->getData();
+ foreach($locks as $k=>$lock) {
+
+ if ($lock->token == $lockInfo->token) {
+
+ unset($locks[$k]);
+ $this->putData($locks);
+ return true;
+
+ }
+ }
+ return false;
+
+ }
+
+ /**
+ * Loads the lockdata from the filesystem.
+ *
+ * @return array
+ */
+ protected function getData() {
+
+ if (!file_exists($this->locksFile)) return array();
+
+ // opening up the file, and creating a shared lock
+ $handle = fopen($this->locksFile,'r');
+ flock($handle,LOCK_SH);
+
+ // Reading data until the eof
+ $data = stream_get_contents($handle);
+
+ // We're all good
+ fclose($handle);
+
+ // Unserializing and checking if the resource file contains data for this file
+ $data = unserialize($data);
+ if (!$data) return array();
+ return $data;
+
+ }
+
+ /**
+ * Saves the lockdata
+ *
+ * @param array $newData
+ * @return void
+ */
+ protected function putData(array $newData) {
+
+ // opening up the file, and creating an exclusive lock
+ $handle = fopen($this->locksFile,'a+');
+ flock($handle,LOCK_EX);
+
+ // We can only truncate and rewind once the lock is acquired.
+ ftruncate($handle,0);
+ rewind($handle);
+
+ fwrite($handle,serialize($newData));
+ fclose($handle);
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Locks/Backend/PDO.php b/app/Gdoo/Calendar/Sabre/DAV/Locks/Backend/PDO.php
new file mode 100644
index 00000000..36c01d3e
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Locks/Backend/PDO.php
@@ -0,0 +1,167 @@
+pdo = $pdo;
+ $this->tableName = $tableName;
+
+ }
+
+ /**
+ * Returns a list of Sabre\DAV\Locks\LockInfo objects
+ *
+ * This method should return all the locks for a particular uri, including
+ * locks that might be set on a parent uri.
+ *
+ * If returnChildLocks is set to true, this method should also look for
+ * any locks in the subtree of the uri for locks.
+ *
+ * @param string $uri
+ * @param bool $returnChildLocks
+ * @return array
+ */
+ public function getLocks($uri, $returnChildLocks) {
+
+ // NOTE: the following 10 lines or so could be easily replaced by
+ // pure sql. MySQL's non-standard string concatenation prevents us
+ // from doing this though.
+ $query = 'SELECT owner, token, timeout, created, scope, depth, uri FROM '.$this->tableName.' WHERE ((created + timeout) > CAST(? AS UNSIGNED INTEGER)) AND ((uri = ?)';
+ $params = array(time(),$uri);
+
+ // We need to check locks for every part in the uri.
+ $uriParts = explode('/',$uri);
+
+ // We already covered the last part of the uri
+ array_pop($uriParts);
+
+ $currentPath='';
+
+ foreach($uriParts as $part) {
+
+ if ($currentPath) $currentPath.='/';
+ $currentPath.=$part;
+
+ $query.=' OR (depth!=0 AND uri = ?)';
+ $params[] = $currentPath;
+
+ }
+
+ if ($returnChildLocks) {
+
+ $query.=' OR (uri LIKE ?)';
+ $params[] = $uri . '/%';
+
+ }
+ $query.=')';
+
+ $stmt = $this->pdo->prepare($query);
+ $stmt->execute($params);
+ $result = $stmt->fetchAll();
+
+ $lockList = array();
+ foreach($result as $row) {
+
+ $lockInfo = new LockInfo();
+ $lockInfo->owner = $row['owner'];
+ $lockInfo->token = $row['token'];
+ $lockInfo->timeout = $row['timeout'];
+ $lockInfo->created = $row['created'];
+ $lockInfo->scope = $row['scope'];
+ $lockInfo->depth = $row['depth'];
+ $lockInfo->uri = $row['uri'];
+ $lockList[] = $lockInfo;
+
+ }
+
+ return $lockList;
+
+ }
+
+ /**
+ * Locks a uri
+ *
+ * @param string $uri
+ * @param LockInfo $lockInfo
+ * @return bool
+ */
+ public function lock($uri, LockInfo $lockInfo) {
+
+ // We're making the lock timeout 30 minutes
+ $lockInfo->timeout = 30*60;
+ $lockInfo->created = time();
+ $lockInfo->uri = $uri;
+
+ $locks = $this->getLocks($uri,false);
+ $exists = false;
+ foreach($locks as $lock) {
+ if ($lock->token == $lockInfo->token) $exists = true;
+ }
+
+ if ($exists) {
+ $stmt = $this->pdo->prepare('UPDATE '.$this->tableName.' SET owner = ?, timeout = ?, scope = ?, depth = ?, uri = ?, created = ? WHERE token = ?');
+ $stmt->execute(array($lockInfo->owner,$lockInfo->timeout,$lockInfo->scope,$lockInfo->depth,$uri,$lockInfo->created,$lockInfo->token));
+ } else {
+ $stmt = $this->pdo->prepare('INSERT INTO '.$this->tableName.' (owner,timeout,scope,depth,uri,created,token) VALUES (?,?,?,?,?,?,?)');
+ $stmt->execute(array($lockInfo->owner,$lockInfo->timeout,$lockInfo->scope,$lockInfo->depth,$uri,$lockInfo->created,$lockInfo->token));
+ }
+
+ return true;
+
+ }
+
+
+
+ /**
+ * Removes a lock from a uri
+ *
+ * @param string $uri
+ * @param LockInfo $lockInfo
+ * @return bool
+ */
+ public function unlock($uri, LockInfo $lockInfo) {
+
+ $stmt = $this->pdo->prepare('DELETE FROM '.$this->tableName.' WHERE uri = ? AND token = ?');
+ $stmt->execute(array($uri,$lockInfo->token));
+
+ return $stmt->rowCount()===1;
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Locks/LockInfo.php b/app/Gdoo/Calendar/Sabre/DAV/Locks/LockInfo.php
new file mode 100644
index 00000000..ed3c0295
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Locks/LockInfo.php
@@ -0,0 +1,81 @@
+addPlugin($lockPlugin);
+ *
+ * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
+ * @author Evert Pot (http://evertpot.com/)
+ * @license http://sabre.io/license/ Modified BSD License
+ */
+class Plugin extends DAV\ServerPlugin {
+
+ /**
+ * locksBackend
+ *
+ * @var Backend\Backend\Interface
+ */
+ protected $locksBackend;
+
+ /**
+ * server
+ *
+ * @var Sabre\DAV\Server
+ */
+ protected $server;
+
+ /**
+ * __construct
+ *
+ * @param Backend\BackendInterface $locksBackend
+ */
+ public function __construct(Backend\BackendInterface $locksBackend = null) {
+
+ $this->locksBackend = $locksBackend;
+
+ }
+
+ /**
+ * Initializes the plugin
+ *
+ * This method is automatically called by the Server class after addPlugin.
+ *
+ * @param DAV\Server $server
+ * @return void
+ */
+ public function initialize(DAV\Server $server) {
+
+ $this->server = $server;
+ $server->subscribeEvent('unknownMethod',array($this,'unknownMethod'));
+ $server->subscribeEvent('beforeMethod',array($this,'beforeMethod'),50);
+ $server->subscribeEvent('afterGetProperties',array($this,'afterGetProperties'));
+
+ }
+
+ /**
+ * Returns a plugin name.
+ *
+ * Using this name other plugins will be able to access other plugins
+ * using Sabre\DAV\Server::getPlugin
+ *
+ * @return string
+ */
+ public function getPluginName() {
+
+ return 'locks';
+
+ }
+
+ /**
+ * This method is called by the Server if the user used an HTTP method
+ * the server didn't recognize.
+ *
+ * This plugin intercepts the LOCK and UNLOCK methods.
+ *
+ * @param string $method
+ * @param string $uri
+ * @return bool
+ */
+ public function unknownMethod($method, $uri) {
+
+ switch($method) {
+
+ case 'LOCK' : $this->httpLock($uri); return false;
+ case 'UNLOCK' : $this->httpUnlock($uri); return false;
+
+ }
+
+ }
+
+ /**
+ * This method is called after most properties have been found
+ * it allows us to add in any Lock-related properties
+ *
+ * @param string $path
+ * @param array $newProperties
+ * @return bool
+ */
+ public function afterGetProperties($path, &$newProperties) {
+
+ foreach($newProperties[404] as $propName=>$discard) {
+
+ switch($propName) {
+
+ case '{DAV:}supportedlock' :
+ $val = false;
+ if ($this->locksBackend) $val = true;
+ $newProperties[200][$propName] = new DAV\Property\SupportedLock($val);
+ unset($newProperties[404][$propName]);
+ break;
+
+ case '{DAV:}lockdiscovery' :
+ $newProperties[200][$propName] = new DAV\Property\LockDiscovery($this->getLocks($path));
+ unset($newProperties[404][$propName]);
+ break;
+
+ }
+
+
+ }
+ return true;
+
+ }
+
+
+ /**
+ * This method is called before the logic for any HTTP method is
+ * handled.
+ *
+ * This plugin uses that feature to intercept access to locked resources.
+ *
+ * @param string $method
+ * @param string $uri
+ * @return bool
+ */
+ public function beforeMethod($method, $uri) {
+
+ switch($method) {
+
+ case 'DELETE' :
+ $lastLock = null;
+ if (!$this->validateLock($uri,$lastLock, true))
+ throw new DAV\Exception\Locked($lastLock);
+ break;
+ case 'MKCOL' :
+ case 'PROPPATCH' :
+ case 'PUT' :
+ case 'PATCH' :
+ $lastLock = null;
+ if (!$this->validateLock($uri,$lastLock))
+ throw new DAV\Exception\Locked($lastLock);
+ break;
+ case 'MOVE' :
+ $lastLock = null;
+ if (!$this->validateLock(array(
+ $uri,
+ $this->server->calculateUri($this->server->httpRequest->getHeader('Destination')),
+ ),$lastLock, true))
+ throw new DAV\Exception\Locked($lastLock);
+ break;
+ case 'COPY' :
+ $lastLock = null;
+ if (!$this->validateLock(
+ $this->server->calculateUri($this->server->httpRequest->getHeader('Destination')),
+ $lastLock, true))
+ throw new DAV\Exception\Locked($lastLock);
+ break;
+ }
+
+ return true;
+
+ }
+
+ /**
+ * Use this method to tell the server this plugin defines additional
+ * HTTP methods.
+ *
+ * This method is passed a uri. It should only return HTTP methods that are
+ * available for the specified uri.
+ *
+ * @param string $uri
+ * @return array
+ */
+ public function getHTTPMethods($uri) {
+
+ if ($this->locksBackend)
+ return array('LOCK','UNLOCK');
+
+ return array();
+
+ }
+
+ /**
+ * Returns a list of features for the HTTP OPTIONS Dav: header.
+ *
+ * In this case this is only the number 2. The 2 in the Dav: header
+ * indicates the server supports locks.
+ *
+ * @return array
+ */
+ public function getFeatures() {
+
+ return array(2);
+
+ }
+
+ /**
+ * Returns all lock information on a particular uri
+ *
+ * This function should return an array with Sabre\DAV\Locks\LockInfo objects. If there are no locks on a file, return an empty array.
+ *
+ * Additionally there is also the possibility of locks on parent nodes, so we'll need to traverse every part of the tree
+ * If the $returnChildLocks argument is set to true, we'll also traverse all the children of the object
+ * for any possible locks and return those as well.
+ *
+ * @param string $uri
+ * @param bool $returnChildLocks
+ * @return array
+ */
+ public function getLocks($uri, $returnChildLocks = false) {
+
+ $lockList = array();
+
+ if ($this->locksBackend)
+ $lockList = array_merge($lockList,$this->locksBackend->getLocks($uri, $returnChildLocks));
+
+ return $lockList;
+
+ }
+
+ /**
+ * Locks an uri
+ *
+ * The WebDAV lock request can be operated to either create a new lock on a file, or to refresh an existing lock
+ * If a new lock is created, a full XML body should be supplied, containing information about the lock such as the type
+ * of lock (shared or exclusive) and the owner of the lock
+ *
+ * If a lock is to be refreshed, no body should be supplied and there should be a valid If header containing the lock
+ *
+ * Additionally, a lock can be requested for a non-existent file. In these case we're obligated to create an empty file as per RFC4918:S7.3
+ *
+ * @param string $uri
+ * @return void
+ */
+ protected function httpLock($uri) {
+
+ $lastLock = null;
+ if (!$this->validateLock($uri,$lastLock)) {
+
+ // If the existing lock was an exclusive lock, we need to fail
+ if (!$lastLock || $lastLock->scope == LockInfo::EXCLUSIVE) {
+ //var_dump($lastLock);
+ throw new DAV\Exception\ConflictingLock($lastLock);
+ }
+
+ }
+
+ if ($body = $this->server->httpRequest->getBody(true)) {
+ // This is a new lock request
+ $lockInfo = $this->parseLockRequest($body);
+ $lockInfo->depth = $this->server->getHTTPDepth();
+ $lockInfo->uri = $uri;
+ if($lastLock && $lockInfo->scope != LockInfo::SHARED) throw new DAV\Exception\ConflictingLock($lastLock);
+
+ } elseif ($lastLock) {
+
+ // This must have been a lock refresh
+ $lockInfo = $lastLock;
+
+ // The resource could have been locked through another uri.
+ if ($uri!=$lockInfo->uri) $uri = $lockInfo->uri;
+
+ } else {
+
+ // There was neither a lock refresh nor a new lock request
+ throw new DAV\Exception\BadRequest('An xml body is required for lock requests');
+
+ }
+
+ if ($timeout = $this->getTimeoutHeader()) $lockInfo->timeout = $timeout;
+
+ $newFile = false;
+
+ // If we got this far.. we should go check if this node actually exists. If this is not the case, we need to create it first
+ try {
+ $this->server->tree->getNodeForPath($uri);
+
+ // We need to call the beforeWriteContent event for RFC3744
+ // Edit: looks like this is not used, and causing problems now.
+ //
+ // See Issue 222
+ // $this->server->broadcastEvent('beforeWriteContent',array($uri));
+
+ } catch (DAV\Exception\NotFound $e) {
+
+ // It didn't, lets create it
+ $this->server->createFile($uri,fopen('php://memory','r'));
+ $newFile = true;
+
+ }
+
+ $this->lockNode($uri,$lockInfo);
+
+ $this->server->httpResponse->setHeader('Content-Type','application/xml; charset=utf-8');
+ $this->server->httpResponse->setHeader('Lock-Token','token . '>');
+ $this->server->httpResponse->sendStatus($newFile?201:200);
+ $this->server->httpResponse->sendBody($this->generateLockResponse($lockInfo));
+
+ }
+
+ /**
+ * Unlocks a uri
+ *
+ * This WebDAV method allows you to remove a lock from a node. The client should provide a valid locktoken through the Lock-token http header
+ * The server should return 204 (No content) on success
+ *
+ * @param string $uri
+ * @return void
+ */
+ protected function httpUnlock($uri) {
+
+ $lockToken = $this->server->httpRequest->getHeader('Lock-Token');
+
+ // If the locktoken header is not supplied, we need to throw a bad request exception
+ if (!$lockToken) throw new DAV\Exception\BadRequest('No lock token was supplied');
+
+ $locks = $this->getLocks($uri);
+
+ // Windows sometimes forgets to include < and > in the Lock-Token
+ // header
+ if ($lockToken[0]!=='<') $lockToken = '<' . $lockToken . '>';
+
+ foreach($locks as $lock) {
+
+ if ('token . '>' == $lockToken) {
+
+ $this->unlockNode($uri,$lock);
+ $this->server->httpResponse->setHeader('Content-Length','0');
+ $this->server->httpResponse->sendStatus(204);
+ return;
+
+ }
+
+ }
+
+ // If we got here, it means the locktoken was invalid
+ throw new DAV\Exception\LockTokenMatchesRequestUri();
+
+ }
+
+ /**
+ * Locks a uri
+ *
+ * All the locking information is supplied in the lockInfo object. The object has a suggested timeout, but this can be safely ignored
+ * It is important that if the existing timeout is ignored, the property is overwritten, as this needs to be sent back to the client
+ *
+ * @param string $uri
+ * @param LockInfo $lockInfo
+ * @return bool
+ */
+ public function lockNode($uri,LockInfo $lockInfo) {
+
+ if (!$this->server->broadcastEvent('beforeLock',array($uri,$lockInfo))) return;
+
+ if ($this->locksBackend) return $this->locksBackend->lock($uri,$lockInfo);
+ throw new DAV\Exception\MethodNotAllowed('Locking support is not enabled for this resource. No Locking backend was found so if you didn\'t expect this error, please check your configuration.');
+
+ }
+
+ /**
+ * Unlocks a uri
+ *
+ * This method removes a lock from a uri. It is assumed all the supplied information is correct and verified
+ *
+ * @param string $uri
+ * @param LockInfo $lockInfo
+ * @return bool
+ */
+ public function unlockNode($uri, LockInfo $lockInfo) {
+
+ if (!$this->server->broadcastEvent('beforeUnlock',array($uri,$lockInfo))) return;
+ if ($this->locksBackend) return $this->locksBackend->unlock($uri,$lockInfo);
+
+ }
+
+
+ /**
+ * Returns the contents of the HTTP Timeout header.
+ *
+ * The method formats the header into an integer.
+ *
+ * @return int
+ */
+ public function getTimeoutHeader() {
+
+ $header = $this->server->httpRequest->getHeader('Timeout');
+
+ if ($header) {
+
+ if (stripos($header,'second-')===0) $header = (int)(substr($header,7));
+ else if (strtolower($header)=='infinite') $header = LockInfo::TIMEOUT_INFINITE;
+ else throw new DAV\Exception\BadRequest('Invalid HTTP timeout header');
+
+ } else {
+
+ $header = 0;
+
+ }
+
+ return $header;
+
+ }
+
+ /**
+ * Generates the response for successful LOCK requests
+ *
+ * @param LockInfo $lockInfo
+ * @return string
+ */
+ protected function generateLockResponse(LockInfo $lockInfo) {
+
+ $dom = new \DOMDocument('1.0','utf-8');
+ $dom->formatOutput = true;
+
+ $prop = $dom->createElementNS('DAV:','d:prop');
+ $dom->appendChild($prop);
+
+ $lockDiscovery = $dom->createElementNS('DAV:','d:lockdiscovery');
+ $prop->appendChild($lockDiscovery);
+
+ $lockObj = new DAV\Property\LockDiscovery(array($lockInfo),true);
+ $lockObj->serialize($this->server,$lockDiscovery);
+
+ return $dom->saveXML();
+
+ }
+
+ /**
+ * validateLock should be called when a write operation is about to happen
+ * It will check if the requested url is locked, and see if the correct lock tokens are passed
+ *
+ * @param mixed $urls List of relevant urls. Can be an array, a string or nothing at all for the current request uri
+ * @param mixed $lastLock This variable will be populated with the last checked lock object (Sabre\DAV\Locks\LockInfo)
+ * @param bool $checkChildLocks If set to true, this function will also look for any locks set on child resources of the supplied urls. This is needed for for example deletion of entire trees.
+ * @return bool
+ */
+ protected function validateLock($urls = null,&$lastLock = null, $checkChildLocks = false) {
+
+ if (is_null($urls)) {
+ $urls = array($this->server->getRequestUri());
+ } elseif (is_string($urls)) {
+ $urls = array($urls);
+ } elseif (!is_array($urls)) {
+ throw new DAV\Exception('The urls parameter should either be null, a string or an array');
+ }
+
+ $conditions = $this->getIfConditions();
+
+ // We're going to loop through the urls and make sure all lock conditions are satisfied
+ foreach($urls as $url) {
+
+ $locks = $this->getLocks($url, $checkChildLocks);
+
+ // If there were no conditions, but there were locks, we fail
+ if (!$conditions && $locks) {
+ reset($locks);
+ $lastLock = current($locks);
+ return false;
+ }
+
+ // If there were no locks or conditions, we go to the next url
+ if (!$locks && !$conditions) continue;
+
+ foreach($conditions as $condition) {
+
+ if (!$condition['uri']) {
+ $conditionUri = $this->server->getRequestUri();
+ } else {
+ $conditionUri = $this->server->calculateUri($condition['uri']);
+ }
+
+ // If the condition has a url, and it isn't part of the affected url at all, check the next condition
+ if ($conditionUri && strpos($url,$conditionUri)!==0) continue;
+
+ // The tokens array contians arrays with 2 elements. 0=true/false for normal/not condition, 1=locktoken
+ // At least 1 condition has to be satisfied
+ foreach($condition['tokens'] as $conditionToken) {
+
+ $etagValid = true;
+ $lockValid = true;
+
+ // key 2 can contain an etag
+ if ($conditionToken[2]) {
+
+ $uri = $conditionUri?$conditionUri:$this->server->getRequestUri();
+ $node = $this->server->tree->getNodeForPath($uri);
+ $etagValid = $node instanceof DAV\IFile && $node->getETag()==$conditionToken[2];
+
+ }
+
+ // key 1 can contain a lock token
+ if ($conditionToken[1]) {
+
+ $lockValid = false;
+ // Match all the locks
+ foreach($locks as $lockIndex=>$lock) {
+
+ $lockToken = 'opaquelocktoken:' . $lock->token;
+
+ // Checking NOT
+ if (!$conditionToken[0] && $lockToken != $conditionToken[1]) {
+
+ // Condition valid, onto the next
+ $lockValid = true;
+ break;
+ }
+ if ($conditionToken[0] && $lockToken == $conditionToken[1]) {
+
+ $lastLock = $lock;
+ // Condition valid and lock matched
+ unset($locks[$lockIndex]);
+ $lockValid = true;
+ break;
+
+ }
+
+ }
+
+ }
+
+ // If, after checking both etags and locks they are stil valid,
+ // we can continue with the next condition.
+ if ($etagValid && $lockValid) continue 2;
+ }
+ // No conditions matched, so we fail
+ throw new DAV\Exception\PreconditionFailed('The tokens provided in the if header did not match','If');
+ }
+
+ // Conditions were met, we'll also need to check if all the locks are gone
+ if (count($locks)) {
+
+ reset($locks);
+
+ // There's still locks, we fail
+ $lastLock = current($locks);
+ return false;
+
+ }
+
+
+ }
+
+ // We got here, this means every condition was satisfied
+ return true;
+
+ }
+
+ /**
+ * This method is created to extract information from the WebDAV HTTP 'If:' header
+ *
+ * The If header can be quite complex, and has a bunch of features. We're using a regex to extract all relevant information
+ * The function will return an array, containing structs with the following keys
+ *
+ * * uri - the uri the condition applies to. If this is returned as an
+ * empty string, this implies it's referring to the request url.
+ * * tokens - The lock token. another 2 dimensional array containing 2 elements (0 = true/false.. If this is a negative condition its set to false, 1 = the actual token)
+ * * etag - an etag, if supplied
+ *
+ * @return array
+ */
+ public function getIfConditions() {
+
+ $header = $this->server->httpRequest->getHeader('If');
+ if (!$header) return array();
+
+ $matches = array();
+
+ $regex = '/(?:\<(?P.*?)\>\s)?\((?PNot\s)?(?:\<(?P[^\>]*)\>)?(?:\s?)(?:\[(?P[^\]]*)\])?\)/im';
+ preg_match_all($regex,$header,$matches,PREG_SET_ORDER);
+
+ $conditions = array();
+
+ foreach($matches as $match) {
+
+ $condition = array(
+ 'uri' => $match['uri'],
+ 'tokens' => array(
+ array($match['not']?0:1,$match['token'],isset($match['etag'])?$match['etag']:'')
+ ),
+ );
+
+ if (!$condition['uri'] && count($conditions)) $conditions[count($conditions)-1]['tokens'][] = array(
+ $match['not']?0:1,
+ $match['token'],
+ isset($match['etag'])?$match['etag']:''
+ );
+ else {
+ $conditions[] = $condition;
+ }
+
+ }
+
+ return $conditions;
+
+ }
+
+ /**
+ * Parses a webdav lock xml body, and returns a new Sabre\DAV\Locks\LockInfo object
+ *
+ * @param string $body
+ * @return DAV\Locks\LockInfo
+ */
+ protected function parseLockRequest($body) {
+
+ // Fixes an XXE vulnerability on PHP versions older than 5.3.23 or
+ // 5.4.13.
+ $previous = libxml_disable_entity_loader(true);
+
+
+ $xml = simplexml_load_string(
+ DAV\XMLUtil::convertDAVNamespace($body),
+ null,
+ LIBXML_NOWARNING);
+ libxml_disable_entity_loader($previous);
+
+ $xml->registerXPathNamespace('d','urn:DAV');
+ $lockInfo = new LockInfo();
+
+ $children = $xml->children("urn:DAV");
+ $lockInfo->owner = (string)$children->owner;
+
+ $lockInfo->token = DAV\UUIDUtil::getUUID();
+ $lockInfo->scope = count($xml->xpath('d:lockscope/d:exclusive'))>0 ? LockInfo::EXCLUSIVE : LockInfo::SHARED;
+
+ return $lockInfo;
+
+ }
+
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Mount/Plugin.php b/app/Gdoo/Calendar/Sabre/DAV/Mount/Plugin.php
new file mode 100644
index 00000000..71cbe6e2
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Mount/Plugin.php
@@ -0,0 +1,83 @@
+server = $server;
+ $this->server->subscribeEvent('beforeMethod',array($this,'beforeMethod'), 90);
+
+ }
+
+ /**
+ * 'beforeMethod' event handles. This event handles intercepts GET requests ending
+ * with ?mount
+ *
+ * @param string $method
+ * @param string $uri
+ * @return bool
+ */
+ public function beforeMethod($method, $uri) {
+
+ if ($method!='GET') return;
+ if ($this->server->httpRequest->getQueryString()!='mount') return;
+
+ $currentUri = $this->server->httpRequest->getAbsoluteUri();
+
+ // Stripping off everything after the ?
+ list($currentUri) = explode('?',$currentUri);
+
+ $this->davMount($currentUri);
+
+ // Returning false to break the event chain
+ return false;
+
+ }
+
+ /**
+ * Generates the davmount response
+ *
+ * @param string $uri absolute uri
+ * @return void
+ */
+ public function davMount($uri) {
+
+ $this->server->httpResponse->sendStatus(200);
+ $this->server->httpResponse->setHeader('Content-Type','application/davmount+xml');
+ ob_start();
+ echo '', "\n";
+ echo "\n";
+ echo " ", htmlspecialchars($uri, ENT_NOQUOTES, 'UTF-8'), "\n";
+ echo "";
+ $this->server->httpResponse->sendBody(ob_get_clean());
+
+ }
+
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Node.php b/app/Gdoo/Calendar/Sabre/DAV/Node.php
new file mode 100644
index 00000000..2a89856a
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Node.php
@@ -0,0 +1,55 @@
+rootNode = $rootNode;
+
+ }
+
+ /**
+ * Returns the INode object for the requested path
+ *
+ * @param string $path
+ * @return INode
+ */
+ public function getNodeForPath($path) {
+
+ $path = trim($path,'/');
+ if (isset($this->cache[$path])) return $this->cache[$path];
+
+ // Is it the root node?
+ if (!strlen($path)) {
+ return $this->rootNode;
+ }
+
+ // Attempting to fetch its parent
+ list($parentName, $baseName) = URLUtil::splitPath($path);
+
+ // If there was no parent, we must simply ask it from the root node.
+ if ($parentName==="") {
+ $node = $this->rootNode->getChild($baseName);
+ } else {
+ // Otherwise, we recursively grab the parent and ask him/her.
+ $parent = $this->getNodeForPath($parentName);
+
+ if (!($parent instanceof ICollection))
+ throw new Exception\NotFound('Could not find node at path: ' . $path);
+
+ $node = $parent->getChild($baseName);
+
+ }
+
+ $this->cache[$path] = $node;
+ return $node;
+
+ }
+
+ /**
+ * This function allows you to check if a node exists.
+ *
+ * @param string $path
+ * @return bool
+ */
+ public function nodeExists($path) {
+
+ try {
+
+ // The root always exists
+ if ($path==='') return true;
+
+ list($parent, $base) = URLUtil::splitPath($path);
+
+ $parentNode = $this->getNodeForPath($parent);
+ if (!$parentNode instanceof ICollection) return false;
+ return $parentNode->childExists($base);
+
+ } catch (Exception\NotFound $e) {
+
+ return false;
+
+ }
+
+ }
+
+ /**
+ * Returns a list of childnodes for a given path.
+ *
+ * @param string $path
+ * @return array
+ */
+ public function getChildren($path) {
+
+ $node = $this->getNodeForPath($path);
+ $children = $node->getChildren();
+ foreach($children as $child) {
+
+ $this->cache[trim($path,'/') . '/' . $child->getName()] = $child;
+
+ }
+ return $children;
+
+ }
+
+ /**
+ * This method is called with every tree update
+ *
+ * Examples of tree updates are:
+ * * node deletions
+ * * node creations
+ * * copy
+ * * move
+ * * renaming nodes
+ *
+ * If Tree classes implement a form of caching, this will allow
+ * them to make sure caches will be expired.
+ *
+ * If a path is passed, it is assumed that the entire subtree is dirty
+ *
+ * @param string $path
+ * @return void
+ */
+ public function markDirty($path) {
+
+ // We don't care enough about sub-paths
+ // flushing the entire cache
+ $path = trim($path,'/');
+ foreach($this->cache as $nodePath=>$node) {
+ if ($nodePath == $path || strpos($nodePath,$path.'/')===0)
+ unset($this->cache[$nodePath]);
+
+ }
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/PartialUpdate/IFile.php b/app/Gdoo/Calendar/Sabre/DAV/PartialUpdate/IFile.php
new file mode 100644
index 00000000..18db8495
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/PartialUpdate/IFile.php
@@ -0,0 +1,39 @@
+addPlugin($patchPlugin);
+ *
+ * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
+ * @author Jean-Tiare LE BIGOT (http://www.jtlebi.fr/)
+ * @license http://sabre.io/license/ Modified BSD License
+ */
+class Plugin extends DAV\ServerPlugin {
+
+ const RANGE_APPEND = 1;
+ const RANGE_START = 2;
+ const RANGE_END = 3;
+
+ /**
+ * Reference to server
+ *
+ * @var Sabre\DAV\Server
+ */
+ protected $server;
+
+ /**
+ * Initializes the plugin
+ *
+ * This method is automatically called by the Server class after addPlugin.
+ *
+ * @param DAV\Server $server
+ * @return void
+ */
+ public function initialize(DAV\Server $server) {
+
+ $this->server = $server;
+ $server->subscribeEvent('unknownMethod',array($this,'unknownMethod'));
+
+ }
+
+ /**
+ * Returns a plugin name.
+ *
+ * Using this name other plugins will be able to access other plugins
+ * using DAV\Server::getPlugin
+ *
+ * @return string
+ */
+ public function getPluginName() {
+
+ return 'partialupdate';
+
+ }
+
+ /**
+ * This method is called by the Server if the user used an HTTP method
+ * the server didn't recognize.
+ *
+ * This plugin intercepts the PATCH methods.
+ *
+ * @param string $method
+ * @param string $uri
+ * @return bool|null
+ */
+ public function unknownMethod($method, $uri) {
+
+ switch($method) {
+
+ case 'PATCH':
+ return $this->httpPatch($uri);
+
+ }
+
+ }
+
+ /**
+ * Use this method to tell the server this plugin defines additional
+ * HTTP methods.
+ *
+ * This method is passed a uri. It should only return HTTP methods that are
+ * available for the specified uri.
+ *
+ * We claim to support PATCH method (partial update) if and only if
+ * - the node exist
+ * - the node implements our partial update interface
+ *
+ * @param string $uri
+ * @return array
+ */
+ public function getHTTPMethods($uri) {
+
+ $tree = $this->server->tree;
+ if ($tree->nodeExists($uri)) {
+ $node = $tree->getNodeForPath($uri);
+ if ($node instanceof IFile || $node instanceof IPatchSupport) {
+ return array('PATCH');
+ }
+ }
+ return array();
+
+ }
+
+ /**
+ * Returns a list of features for the HTTP OPTIONS Dav: header.
+ *
+ * @return array
+ */
+ public function getFeatures() {
+
+ return array('sabredav-partialupdate');
+
+ }
+
+ /**
+ * Patch an uri
+ *
+ * The WebDAV patch request can be used to modify only a part of an
+ * existing resource. If the resource does not exist yet and the first
+ * offset is not 0, the request fails
+ *
+ * @param string $uri
+ * @return void
+ */
+ protected function httpPatch($uri) {
+
+ // Get the node. Will throw a 404 if not found
+ $node = $this->server->tree->getNodeForPath($uri);
+ if (!$node instanceof IFile && !$node instanceof IPatchSupport) {
+ throw new DAV\Exception\MethodNotAllowed('The target resource does not support the PATCH method.');
+ }
+
+ $range = $this->getHTTPUpdateRange();
+
+ if (!$range) {
+ throw new DAV\Exception\BadRequest('No valid "X-Update-Range" found in the headers');
+ }
+
+ $contentType = strtolower(
+ $this->server->httpRequest->getHeader('Content-Type')
+ );
+
+ if ($contentType != 'application/x-sabredav-partialupdate') {
+ throw new DAV\Exception\UnsupportedMediaType('Unknown Content-Type header "' . $contentType . '"');
+ }
+
+ $len = $this->server->httpRequest->getHeader('Content-Length');
+ if (!$len) throw new DAV\Exception\LengthRequired('A Content-Length header is required');
+
+ switch($range[0]) {
+ case self::RANGE_START :
+ // Calculate the end-range if it doesn't exist.
+ if (!$range[2]) {
+ $range[2] = $range[1] + $len - 1;
+ } else {
+ if ($range[2] < $range[1]) {
+ throw new DAV\Exception\RequestedRangeNotSatisfiable('The end offset (' . $range[2] . ') is lower than the start offset (' . $range[1] . ')');
+ }
+ if($range[2] - $range[1] + 1 != $len) {
+ throw new DAV\Exception\RequestedRangeNotSatisfiable('Actual data length (' . $len . ') is not consistent with begin (' . $range[1] . ') and end (' . $range[2] . ') offsets');
+ }
+ }
+ break;
+ }
+ // Checking If-None-Match and related headers.
+ if (!$this->server->checkPreconditions()) return;
+
+ if (!$this->server->broadcastEvent('beforeWriteContent',array($uri, $node, null)))
+ return;
+
+ $body = $this->server->httpRequest->getBody();
+
+
+ if ($node instanceof IPatchSupport) {
+ $etag = $node->patch($body, $range[0], isset($range[1])?$range[1]:null);
+ } else {
+ // The old interface
+ switch($range[0]) {
+ case self::RANGE_APPEND :
+ throw new DAV\Exception\NotImplemented('This node does not support the append syntax. Please upgrade it to IPatchSupport');
+ case self::RANGE_START :
+ $etag = $node->putRange($body, $range[1]);
+ break;
+ case self::RANGE_END :
+ throw new DAV\Exception\NotImplemented('This node does not support the end-range syntax. Please upgrade it to IPatchSupport');
+ break;
+ }
+ }
+
+ $this->server->broadcastEvent('afterWriteContent',array($uri, $node));
+
+ $this->server->httpResponse->setHeader('Content-Length','0');
+ if ($etag) $this->server->httpResponse->setHeader('ETag',$etag);
+ $this->server->httpResponse->sendStatus(204);
+
+ return false;
+
+ }
+
+ /**
+ * Returns the HTTP custom range update header
+ *
+ * This method returns null if there is no well-formed HTTP range request
+ * header. It returns array(1) if it was an append request, array(2,
+ * $start, $end) if it's a start and end range, lastly it's array(3,
+ * $endoffset) if the offset was negative, and should be calculated from
+ * the end of the file.
+ *
+ * Examples:
+ *
+ * null - invalid
+ * array(1) - append
+ * array(2,10,15) - update bytes 10, 11, 12, 13, 14, 15
+ * array(2,10,null) - update bytes 10 until the end of the patch body
+ * array(3,-5) - update from 5 bytes from the end of the file.
+ *
+ * @return array|null
+ */
+ public function getHTTPUpdateRange() {
+
+ $range = $this->server->httpRequest->getHeader('X-Update-Range');
+ if (is_null($range)) return null;
+
+ // Matching "Range: bytes=1234-5678: both numbers are optional
+
+ if (!preg_match('/^(append)|(?:bytes=([0-9]+)-([0-9]*))|(?:bytes=(-[0-9]+))$/i',$range,$matches)) return null;
+
+ if ($matches[1]==='append') {
+ return array(self::RANGE_APPEND);
+ } elseif (strlen($matches[2])>0) {
+ return array(self::RANGE_START, $matches[2], $matches[3]?:null);
+ } elseif ($matches[4]) {
+ return array(self::RANGE_END, $matches[4]);
+ } else {
+ return null;
+ }
+
+ }
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Property.php b/app/Gdoo/Calendar/Sabre/DAV/Property.php
new file mode 100644
index 00000000..b2d5eb24
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Property.php
@@ -0,0 +1,31 @@
+time = $time;
+ } elseif (is_int($time) || ctype_digit($time)) {
+ $this->time = new \DateTime('@' . $time);
+ } else {
+ $this->time = new \DateTime($time);
+ }
+
+ // Setting timezone to UTC
+ $this->time->setTimezone(new \DateTimeZone('UTC'));
+
+ }
+
+ /**
+ * serialize
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $prop
+ * @return void
+ */
+ public function serialize(DAV\Server $server, \DOMElement $prop) {
+
+ $doc = $prop->ownerDocument;
+ //$prop->setAttribute('xmlns:b','urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/');
+ //$prop->setAttribute('b:dt','dateTime.rfc1123');
+ $prop->nodeValue = HTTP\Util::toHTTPDate($this->time);
+
+ }
+
+ /**
+ * getTime
+ *
+ * @return \DateTime
+ */
+ public function getTime() {
+
+ return $this->time;
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Property/Href.php b/app/Gdoo/Calendar/Sabre/DAV/Property/Href.php
new file mode 100644
index 00000000..8f8147ba
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Property/Href.php
@@ -0,0 +1,99 @@
+href = $href;
+ $this->autoPrefix = $autoPrefix;
+
+ }
+
+ /**
+ * Returns the uri
+ *
+ * @return string
+ */
+ public function getHref() {
+
+ return $this->href;
+
+ }
+
+ /**
+ * Serializes this property.
+ *
+ * It will additionally prepend the href property with the server's base uri.
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $dom
+ * @return void
+ */
+ public function serialize(DAV\Server $server, \DOMElement $dom) {
+
+ $prefix = $server->xmlNamespaces['DAV:'];
+ $elem = $dom->ownerDocument->createElement($prefix . ':href');
+
+ if ($this->autoPrefix) {
+ $value = $server->getBaseUri() . DAV\URLUtil::encodePath($this->href);
+ } else {
+ $value = $this->href;
+ }
+ $elem->appendChild($dom->ownerDocument->createTextNode($value));
+
+ $dom->appendChild($elem);
+
+ }
+
+ /**
+ * Unserializes this property from a DOM Element
+ *
+ * This method returns an instance of this class.
+ * It will only decode {DAV:}href values. For non-compatible elements null will be returned.
+ *
+ * @param \DOMElement $dom
+ * @return DAV\Property\Href
+ */
+ static function unserialize(\DOMElement $dom) {
+
+ if ($dom->firstChild && DAV\XMLUtil::toClarkNotation($dom->firstChild)==='{DAV:}href') {
+ return new self($dom->firstChild->textContent,false);
+ }
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Property/HrefList.php b/app/Gdoo/Calendar/Sabre/DAV/Property/HrefList.php
new file mode 100644
index 00000000..ba8d9e46
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Property/HrefList.php
@@ -0,0 +1,105 @@
+hrefs = $hrefs;
+ $this->autoPrefix = $autoPrefix;
+
+ }
+
+ /**
+ * Returns the uris
+ *
+ * @return array
+ */
+ public function getHrefs() {
+
+ return $this->hrefs;
+
+ }
+
+ /**
+ * Serializes this property.
+ *
+ * It will additionally prepend the href property with the server's base uri.
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $dom
+ * @return void
+ */
+ public function serialize(DAV\Server $server,\DOMElement $dom) {
+
+ $prefix = $server->xmlNamespaces['DAV:'];
+
+ foreach($this->hrefs as $href) {
+
+ $elem = $dom->ownerDocument->createElement($prefix . ':href');
+ if ($this->autoPrefix) {
+ $value = $server->getBaseUri() . DAV\URLUtil::encodePath($href);
+ } else {
+ $value = $href;
+ }
+ $elem->appendChild($dom->ownerDocument->createTextNode($value));
+
+ $dom->appendChild($elem);
+ }
+
+ }
+
+ /**
+ * Unserializes this property from a DOM Element
+ *
+ * This method returns an instance of this class.
+ * It will only decode {DAV:}href values.
+ *
+ * @param \DOMElement $dom
+ * @return DAV\Property\HrefList
+ */
+ static function unserialize(\DOMElement $dom) {
+
+ $hrefs = array();
+ foreach($dom->childNodes as $child) {
+ if (DAV\XMLUtil::toClarkNotation($child)==='{DAV:}href') {
+ $hrefs[] = $child->textContent;
+ }
+ }
+ return new self($hrefs, false);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Property/IHref.php b/app/Gdoo/Calendar/Sabre/DAV/Property/IHref.php
new file mode 100644
index 00000000..00a7c98a
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Property/IHref.php
@@ -0,0 +1,25 @@
+locks = $locks;
+ $this->revealLockToken = $revealLockToken;
+
+ }
+
+ /**
+ * serialize
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $prop
+ * @return void
+ */
+ public function serialize(DAV\Server $server, \DOMElement $prop) {
+
+ $doc = $prop->ownerDocument;
+
+ foreach($this->locks as $lock) {
+
+ $activeLock = $doc->createElementNS('DAV:','d:activelock');
+ $prop->appendChild($activeLock);
+
+ $lockScope = $doc->createElementNS('DAV:','d:lockscope');
+ $activeLock->appendChild($lockScope);
+
+ $lockScope->appendChild($doc->createElementNS('DAV:','d:' . ($lock->scope==DAV\Locks\LockInfo::EXCLUSIVE?'exclusive':'shared')));
+
+ $lockType = $doc->createElementNS('DAV:','d:locktype');
+ $activeLock->appendChild($lockType);
+
+ $lockType->appendChild($doc->createElementNS('DAV:','d:write'));
+
+ /* {DAV:}lockroot */
+ if (!self::$hideLockRoot) {
+ $lockRoot = $doc->createElementNS('DAV:','d:lockroot');
+ $activeLock->appendChild($lockRoot);
+ $href = $doc->createElementNS('DAV:','d:href');
+ $href->appendChild($doc->createTextNode($server->getBaseUri() . $lock->uri));
+ $lockRoot->appendChild($href);
+ }
+
+ $activeLock->appendChild($doc->createElementNS('DAV:','d:depth',($lock->depth == DAV\Server::DEPTH_INFINITY?'infinity':$lock->depth)));
+ $activeLock->appendChild($doc->createElementNS('DAV:','d:timeout','Second-' . $lock->timeout));
+
+ if ($this->revealLockToken) {
+ $lockToken = $doc->createElementNS('DAV:','d:locktoken');
+ $activeLock->appendChild($lockToken);
+ $lockToken->appendChild($doc->createElementNS('DAV:','d:href','opaquelocktoken:' . $lock->token));
+ }
+
+ $activeLock->appendChild($doc->createElementNS('DAV:','d:owner',$lock->owner));
+
+ }
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Property/ResourceType.php b/app/Gdoo/Calendar/Sabre/DAV/Property/ResourceType.php
new file mode 100644
index 00000000..5bcf6e5d
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Property/ResourceType.php
@@ -0,0 +1,127 @@
+resourceType = array();
+ elseif ($resourceType === DAV\Server::NODE_DIRECTORY)
+ $this->resourceType = array('{DAV:}collection');
+ elseif (is_array($resourceType))
+ $this->resourceType = $resourceType;
+ else
+ $this->resourceType = array($resourceType);
+
+ }
+
+ /**
+ * serialize
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $prop
+ * @return void
+ */
+ public function serialize(DAV\Server $server, \DOMElement $prop) {
+
+ $propName = null;
+ $rt = $this->resourceType;
+
+ foreach($rt as $resourceType) {
+ if (preg_match('/^{([^}]*)}(.*)$/',$resourceType,$propName)) {
+
+ if (isset($server->xmlNamespaces[$propName[1]])) {
+ $prop->appendChild($prop->ownerDocument->createElement($server->xmlNamespaces[$propName[1]] . ':' . $propName[2]));
+ } else {
+ $prop->appendChild($prop->ownerDocument->createElementNS($propName[1],'custom:' . $propName[2]));
+ }
+
+ }
+ }
+
+ }
+
+ /**
+ * Returns the values in clark-notation
+ *
+ * For example array('{DAV:}collection')
+ *
+ * @return array
+ */
+ public function getValue() {
+
+ return $this->resourceType;
+
+ }
+
+ /**
+ * Checks if the principal contains a certain value
+ *
+ * @param string $type
+ * @return bool
+ */
+ public function is($type) {
+
+ return in_array($type, $this->resourceType);
+
+ }
+
+ /**
+ * Adds a resourcetype value to this property
+ *
+ * @param string $type
+ * @return void
+ */
+ public function add($type) {
+
+ $this->resourceType[] = $type;
+ $this->resourceType = array_unique($this->resourceType);
+
+ }
+
+ /**
+ * Unserializes a DOM element into a ResourceType property.
+ *
+ * @param \DOMElement $dom
+ * @return DAV\Property\ResourceType
+ */
+ static public function unserialize(\DOMElement $dom) {
+
+ $value = array();
+ foreach($dom->childNodes as $child) {
+
+ $value[] = DAV\XMLUtil::toClarkNotation($child);
+
+ }
+
+ return new self($value);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Property/Response.php b/app/Gdoo/Calendar/Sabre/DAV/Property/Response.php
new file mode 100644
index 00000000..973413a4
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Property/Response.php
@@ -0,0 +1,157 @@
+href = $href;
+ $this->responseProperties = $responseProperties;
+
+ }
+
+ /**
+ * Returns the url
+ *
+ * @return string
+ */
+ public function getHref() {
+
+ return $this->href;
+
+ }
+
+ /**
+ * Returns the property list
+ *
+ * @return array
+ */
+ public function getResponseProperties() {
+
+ return $this->responseProperties;
+
+ }
+
+ /**
+ * serialize
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $dom
+ * @return void
+ */
+ public function serialize(DAV\Server $server, \DOMElement $dom) {
+
+ $document = $dom->ownerDocument;
+ $properties = $this->responseProperties;
+
+ $xresponse = $document->createElement('d:response');
+ $dom->appendChild($xresponse);
+
+ $uri = DAV\URLUtil::encodePath($this->href);
+
+ // Adding the baseurl to the beginning of the url
+ $uri = $server->getBaseUri() . $uri;
+
+ $xresponse->appendChild($document->createElement('d:href',$uri));
+
+ // The properties variable is an array containing properties, grouped by
+ // HTTP status
+ foreach($properties as $httpStatus=>$propertyGroup) {
+
+ // The 'href' is also in this array, and it's special cased.
+ // We will ignore it
+ if ($httpStatus=='href') continue;
+
+ // If there are no properties in this group, we can also just carry on
+ if (!count($propertyGroup)) continue;
+
+ $xpropstat = $document->createElement('d:propstat');
+ $xresponse->appendChild($xpropstat);
+
+ $xprop = $document->createElement('d:prop');
+ $xpropstat->appendChild($xprop);
+
+ $nsList = $server->xmlNamespaces;
+
+ foreach($propertyGroup as $propertyName=>$propertyValue) {
+
+ $propName = null;
+ preg_match('/^{([^}]*)}(.*)$/',$propertyName,$propName);
+
+ // special case for empty namespaces
+ if ($propName[1]=='') {
+
+ $currentProperty = $document->createElement($propName[2]);
+ $xprop->appendChild($currentProperty);
+ $currentProperty->setAttribute('xmlns','');
+
+ } else {
+
+ if (!isset($nsList[$propName[1]])) {
+ $nsList[$propName[1]] = 'x' . count($nsList);
+ }
+
+ // If the namespace was defined in the top-level xml namespaces, it means
+ // there was already a namespace declaration, and we don't have to worry about it.
+ if (isset($server->xmlNamespaces[$propName[1]])) {
+ $currentProperty = $document->createElement($nsList[$propName[1]] . ':' . $propName[2]);
+ } else {
+ $currentProperty = $document->createElementNS($propName[1],$nsList[$propName[1]].':' . $propName[2]);
+ }
+ $xprop->appendChild($currentProperty);
+
+ }
+
+ if (is_scalar($propertyValue)) {
+ $text = $document->createTextNode($propertyValue);
+ $currentProperty->appendChild($text);
+ } elseif ($propertyValue instanceof DAV\PropertyInterface) {
+ $propertyValue->serialize($server,$currentProperty);
+ } elseif (!is_null($propertyValue)) {
+ throw new DAV\Exception('Unknown property value type: ' . gettype($propertyValue) . ' for property: ' . $propertyName);
+ }
+
+ }
+
+ $xpropstat->appendChild($document->createElement('d:status',$server->httpResponse->getStatusMessage($httpStatus)));
+
+ }
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Property/ResponseList.php b/app/Gdoo/Calendar/Sabre/DAV/Property/ResponseList.php
new file mode 100644
index 00000000..620c16ce
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Property/ResponseList.php
@@ -0,0 +1,59 @@
+responses = $responses;
+
+ }
+
+ /**
+ * serialize
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $dom
+ * @return void
+ */
+ public function serialize(DAV\Server $server,\DOMElement $dom) {
+
+ foreach($this->responses as $response) {
+ $response->serialize($server, $dom);
+ }
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Property/SupportedLock.php b/app/Gdoo/Calendar/Sabre/DAV/Property/SupportedLock.php
new file mode 100644
index 00000000..3898ecee
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Property/SupportedLock.php
@@ -0,0 +1,78 @@
+supportsLocks = $supportsLocks;
+
+ }
+
+ /**
+ * serialize
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $prop
+ * @return void
+ */
+ public function serialize(DAV\Server $server,\DOMElement $prop) {
+
+ $doc = $prop->ownerDocument;
+
+ if (!$this->supportsLocks) return null;
+
+ $lockEntry1 = $doc->createElement('d:lockentry');
+ $lockEntry2 = $doc->createElement('d:lockentry');
+
+ $prop->appendChild($lockEntry1);
+ $prop->appendChild($lockEntry2);
+
+ $lockScope1 = $doc->createElement('d:lockscope');
+ $lockScope2 = $doc->createElement('d:lockscope');
+ $lockType1 = $doc->createElement('d:locktype');
+ $lockType2 = $doc->createElement('d:locktype');
+
+ $lockEntry1->appendChild($lockScope1);
+ $lockEntry1->appendChild($lockType1);
+ $lockEntry2->appendChild($lockScope2);
+ $lockEntry2->appendChild($lockType2);
+
+ $lockScope1->appendChild($doc->createElement('d:exclusive'));
+ $lockScope2->appendChild($doc->createElement('d:shared'));
+
+ $lockType1->appendChild($doc->createElement('d:write'));
+ $lockType2->appendChild($doc->createElement('d:write'));
+
+ //$frag->appendXML('');
+ //$frag->appendXML('');
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Property/SupportedReportSet.php b/app/Gdoo/Calendar/Sabre/DAV/Property/SupportedReportSet.php
new file mode 100644
index 00000000..2df28026
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Property/SupportedReportSet.php
@@ -0,0 +1,111 @@
+addReport($reports);
+
+ }
+
+ /**
+ * Adds a report to this property
+ *
+ * The report must be a string in clark-notation.
+ * Multiple reports can be specified as an array.
+ *
+ * @param mixed $report
+ * @return void
+ */
+ public function addReport($report) {
+
+ if (!is_array($report)) $report = array($report);
+
+ foreach($report as $r) {
+
+ if (!preg_match('/^{([^}]*)}(.*)$/',$r))
+ throw new DAV\Exception('Reportname must be in clark-notation');
+
+ $this->reports[] = $r;
+
+ }
+
+ }
+
+ /**
+ * Returns the list of supported reports
+ *
+ * @return array
+ */
+ public function getValue() {
+
+ return $this->reports;
+
+ }
+
+ /**
+ * Serializes the node
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $prop
+ * @return void
+ */
+ public function serialize(DAV\Server $server, \DOMElement $prop) {
+
+ foreach($this->reports as $reportName) {
+
+ $supportedReport = $prop->ownerDocument->createElement('d:supported-report');
+ $prop->appendChild($supportedReport);
+
+ $report = $prop->ownerDocument->createElement('d:report');
+ $supportedReport->appendChild($report);
+
+ preg_match('/^{([^}]*)}(.*)$/',$reportName,$matches);
+
+ list(, $namespace, $element) = $matches;
+
+ $prefix = isset($server->xmlNamespaces[$namespace])?$server->xmlNamespaces[$namespace]:null;
+
+ if ($prefix) {
+ $report->appendChild($prop->ownerDocument->createElement($prefix . ':' . $element));
+ } else {
+ $report->appendChild($prop->ownerDocument->createElementNS($namespace, 'x:' . $element));
+ }
+
+ }
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/PropertyInterface.php b/app/Gdoo/Calendar/Sabre/DAV/PropertyInterface.php
new file mode 100644
index 00000000..31fa2f29
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/PropertyInterface.php
@@ -0,0 +1,21 @@
+ 'd',
+ 'http://sabredav.org/ns' => 's',
+ );
+
+ /**
+ * The propertymap can be used to map properties from
+ * requests to property classes.
+ *
+ * @var array
+ */
+ public $propertyMap = array(
+ '{DAV:}resourcetype' => 'Sabre\\DAV\\Property\\ResourceType',
+ );
+
+ public $protectedProperties = array(
+ // RFC4918
+ '{DAV:}getcontentlength',
+ '{DAV:}getetag',
+ '{DAV:}getlastmodified',
+ '{DAV:}lockdiscovery',
+ '{DAV:}supportedlock',
+
+ // RFC4331
+ '{DAV:}quota-available-bytes',
+ '{DAV:}quota-used-bytes',
+
+ // RFC3744
+ '{DAV:}supported-privilege-set',
+ '{DAV:}current-user-privilege-set',
+ '{DAV:}acl',
+ '{DAV:}acl-restrictions',
+ '{DAV:}inherited-acl-set',
+
+ );
+
+ /**
+ * This is a flag that allow or not showing file, line and code
+ * of the exception in the returned XML
+ *
+ * @var bool
+ */
+ public $debugExceptions = false;
+
+ /**
+ * This property allows you to automatically add the 'resourcetype' value
+ * based on a node's classname or interface.
+ *
+ * The preset ensures that {DAV:}collection is automaticlly added for nodes
+ * implementing Sabre\DAV\ICollection.
+ *
+ * @var array
+ */
+ public $resourceTypeMapping = array(
+ 'Sabre\\DAV\\ICollection' => '{DAV:}collection',
+ );
+
+ /**
+ * If this setting is turned off, SabreDAV's version number will be hidden
+ * from various places.
+ *
+ * Some people feel this is a good security measure.
+ *
+ * @var bool
+ */
+ static public $exposeVersion = true;
+
+ /**
+ * Sets up the server
+ *
+ * If a Sabre\DAV\Tree object is passed as an argument, it will
+ * use it as the directory tree. If a Sabre\DAV\INode is passed, it
+ * will create a Sabre\DAV\ObjectTree and use the node as the root.
+ *
+ * If nothing is passed, a Sabre\DAV\SimpleCollection is created in
+ * a Sabre\DAV\ObjectTree.
+ *
+ * If an array is passed, we automatically create a root node, and use
+ * the nodes in the array as top-level children.
+ *
+ * @param Tree|INode|array|null $treeOrNode The tree object
+ */
+ public function __construct($treeOrNode = null) {
+
+ if ($treeOrNode instanceof Tree) {
+ $this->tree = $treeOrNode;
+ } elseif ($treeOrNode instanceof INode) {
+ $this->tree = new ObjectTree($treeOrNode);
+ } elseif (is_array($treeOrNode)) {
+
+ // If it's an array, a list of nodes was passed, and we need to
+ // create the root node.
+ foreach($treeOrNode as $node) {
+ if (!($node instanceof INode)) {
+ throw new Exception('Invalid argument passed to constructor. If you\'re passing an array, all the values must implement Sabre\\DAV\\INode');
+ }
+ }
+
+ $root = new SimpleCollection('root', $treeOrNode);
+ $this->tree = new ObjectTree($root);
+
+ } elseif (is_null($treeOrNode)) {
+ $root = new SimpleCollection('root');
+ $this->tree = new ObjectTree($root);
+ } else {
+ throw new Exception('Invalid argument passed to constructor. Argument must either be an instance of Sabre\\DAV\\Tree, Sabre\\DAV\\INode, an array or null');
+ }
+ $this->httpResponse = new HTTP\Response();
+ $this->httpRequest = new HTTP\Request();
+
+ }
+
+ /**
+ * Starts the DAV Server
+ *
+ * @return void
+ */
+ public function exec() {
+
+ try {
+
+ // If nginx (pre-1.2) is used as a proxy server, and SabreDAV as an
+ // origin, we must make sure we send back HTTP/1.0 if this was
+ // requested.
+ // This is mainly because nginx doesn't support Chunked Transfer
+ // Encoding, and this forces the webserver SabreDAV is running on,
+ // to buffer entire responses to calculate Content-Length.
+ $this->httpResponse->defaultHttpVersion = $this->httpRequest->getHTTPVersion();
+
+ $this->invokeMethod($this->httpRequest->getMethod(), $this->getRequestUri());
+
+ } catch (Exception $e) {
+
+ try {
+ $this->broadcastEvent('exception', array($e));
+ } catch (Exception $ignore) {
+ }
+ $DOM = new \DOMDocument('1.0','utf-8');
+ $DOM->formatOutput = true;
+
+ $error = $DOM->createElementNS('DAV:','d:error');
+ $error->setAttribute('xmlns:s',self::NS_SABREDAV);
+ $DOM->appendChild($error);
+
+ $h = function($v) {
+
+ return htmlspecialchars($v, ENT_NOQUOTES, 'UTF-8');
+
+ };
+
+ $error->appendChild($DOM->createElement('s:exception',$h(get_class($e))));
+ $error->appendChild($DOM->createElement('s:message',$h($e->getMessage())));
+ if ($this->debugExceptions) {
+ $error->appendChild($DOM->createElement('s:file',$h($e->getFile())));
+ $error->appendChild($DOM->createElement('s:line',$h($e->getLine())));
+ $error->appendChild($DOM->createElement('s:code',$h($e->getCode())));
+ $error->appendChild($DOM->createElement('s:stacktrace',$h($e->getTraceAsString())));
+
+ }
+ if (self::$exposeVersion) {
+ $error->appendChild($DOM->createElement('s:sabredav-version',$h(Version::VERSION)));
+ }
+
+ if($e instanceof Exception) {
+
+ $httpCode = $e->getHTTPCode();
+ $e->serialize($this,$error);
+ $headers = $e->getHTTPHeaders($this);
+
+ } else {
+
+ $httpCode = 500;
+ $headers = array();
+
+ }
+ $headers['Content-Type'] = 'application/xml; charset=utf-8';
+
+ $this->httpResponse->sendStatus($httpCode);
+ $this->httpResponse->setHeaders($headers);
+ $this->httpResponse->sendBody($DOM->saveXML());
+
+ }
+
+ }
+
+ /**
+ * Sets the base server uri
+ *
+ * @param string $uri
+ * @return void
+ */
+ public function setBaseUri($uri) {
+
+ // If the baseUri does not end with a slash, we must add it
+ if ($uri[strlen($uri)-1]!=='/')
+ $uri.='/';
+
+ $this->baseUri = $uri;
+
+ }
+
+ /**
+ * Returns the base responding uri
+ *
+ * @return string
+ */
+ public function getBaseUri() {
+
+ if (is_null($this->baseUri)) $this->baseUri = $this->guessBaseUri();
+ return $this->baseUri;
+
+ }
+
+ /**
+ * This method attempts to detect the base uri.
+ * Only the PATH_INFO variable is considered.
+ *
+ * If this variable is not set, the root (/) is assumed.
+ *
+ * @return string
+ */
+ public function guessBaseUri() {
+
+ $pathInfo = $this->httpRequest->getRawServerValue('PATH_INFO');
+ $uri = $this->httpRequest->getRawServerValue('REQUEST_URI');
+
+ // If PATH_INFO is found, we can assume it's accurate.
+ if (!empty($pathInfo)) {
+
+ // We need to make sure we ignore the QUERY_STRING part
+ if ($pos = strpos($uri,'?'))
+ $uri = substr($uri,0,$pos);
+
+ // PATH_INFO is only set for urls, such as: /example.php/path
+ // in that case PATH_INFO contains '/path'.
+ // Note that REQUEST_URI is percent encoded, while PATH_INFO is
+ // not, Therefore they are only comparable if we first decode
+ // REQUEST_INFO as well.
+ $decodedUri = URLUtil::decodePath($uri);
+
+ // A simple sanity check:
+ if(substr($decodedUri,strlen($decodedUri)-strlen($pathInfo))===$pathInfo) {
+ $baseUri = substr($decodedUri,0,strlen($decodedUri)-strlen($pathInfo));
+ return rtrim($baseUri,'/') . '/';
+ }
+
+ throw new Exception('The REQUEST_URI ('. $uri . ') did not end with the contents of PATH_INFO (' . $pathInfo . '). This server might be misconfigured.');
+
+ }
+
+ // The last fallback is that we're just going to assume the server root.
+ return '/';
+
+ }
+
+ /**
+ * Adds a plugin to the server
+ *
+ * For more information, console the documentation of Sabre\DAV\ServerPlugin
+ *
+ * @param ServerPlugin $plugin
+ * @return void
+ */
+ public function addPlugin(ServerPlugin $plugin) {
+
+ $this->plugins[$plugin->getPluginName()] = $plugin;
+ $plugin->initialize($this);
+
+ }
+
+ /**
+ * Returns an initialized plugin by it's name.
+ *
+ * This function returns null if the plugin was not found.
+ *
+ * @param string $name
+ * @return ServerPlugin
+ */
+ public function getPlugin($name) {
+
+ if (isset($this->plugins[$name]))
+ return $this->plugins[$name];
+
+ // This is a fallback and deprecated.
+ foreach($this->plugins as $plugin) {
+ if (get_class($plugin)===$name) return $plugin;
+ }
+
+ return null;
+
+ }
+
+ /**
+ * Returns all plugins
+ *
+ * @return array
+ */
+ public function getPlugins() {
+
+ return $this->plugins;
+
+ }
+
+
+ /**
+ * Subscribe to an event.
+ *
+ * When the event is triggered, we'll call all the specified callbacks.
+ * It is possible to control the order of the callbacks through the
+ * priority argument.
+ *
+ * This is for example used to make sure that the authentication plugin
+ * is triggered before anything else. If it's not needed to change this
+ * number, it is recommended to ommit.
+ *
+ * @param string $event
+ * @param callback $callback
+ * @param int $priority
+ * @return void
+ */
+ public function subscribeEvent($event, $callback, $priority = 100) {
+
+ if (!isset($this->eventSubscriptions[$event])) {
+ $this->eventSubscriptions[$event] = array();
+ }
+ while(isset($this->eventSubscriptions[$event][$priority])) $priority++;
+ $this->eventSubscriptions[$event][$priority] = $callback;
+ ksort($this->eventSubscriptions[$event]);
+
+ }
+
+ /**
+ * Broadcasts an event
+ *
+ * This method will call all subscribers. If one of the subscribers returns false, the process stops.
+ *
+ * The arguments parameter will be sent to all subscribers
+ *
+ * @param string $eventName
+ * @param array $arguments
+ * @return bool
+ */
+ public function broadcastEvent($eventName,$arguments = array()) {
+
+ if (isset($this->eventSubscriptions[$eventName])) {
+
+ foreach($this->eventSubscriptions[$eventName] as $subscriber) {
+
+ $result = call_user_func_array($subscriber,$arguments);
+ if ($result===false) return false;
+
+ }
+
+ }
+
+ return true;
+
+ }
+
+ /**
+ * Handles a http request, and execute a method based on its name
+ *
+ * @param string $method
+ * @param string $uri
+ * @return void
+ */
+ public function invokeMethod($method, $uri) {
+
+ $method = strtoupper($method);
+
+ if (!$this->broadcastEvent('beforeMethod',array($method, $uri))) return;
+
+ // Make sure this is a HTTP method we support
+ $internalMethods = array(
+ 'OPTIONS',
+ 'GET',
+ 'HEAD',
+ 'DELETE',
+ 'PROPFIND',
+ 'MKCOL',
+ 'PUT',
+ 'PROPPATCH',
+ 'COPY',
+ 'MOVE',
+ 'REPORT'
+ );
+
+ if (in_array($method,$internalMethods)) {
+
+ call_user_func(array($this,'http' . $method), $uri);
+
+ } else {
+
+ if ($this->broadcastEvent('unknownMethod',array($method, $uri))) {
+ // Unsupported method
+ throw new Exception\NotImplemented('There was no handler found for this "' . $method . '" method');
+ }
+
+ }
+
+ }
+
+ // {{{ HTTP Method implementations
+
+ /**
+ * HTTP OPTIONS
+ *
+ * @param string $uri
+ * @return void
+ */
+ protected function httpOptions($uri) {
+
+ $methods = $this->getAllowedMethods($uri);
+
+ $this->httpResponse->setHeader('Allow',strtoupper(implode(', ',$methods)));
+ $features = array('1','3', 'extended-mkcol');
+
+ foreach($this->plugins as $plugin) $features = array_merge($features,$plugin->getFeatures());
+
+ $this->httpResponse->setHeader('DAV',implode(', ',$features));
+ $this->httpResponse->setHeader('MS-Author-Via','DAV');
+ $this->httpResponse->setHeader('Accept-Ranges','bytes');
+ if (self::$exposeVersion) {
+ $this->httpResponse->setHeader('X-Sabre-Version',Version::VERSION);
+ }
+ $this->httpResponse->setHeader('Content-Length',0);
+ $this->httpResponse->sendStatus(200);
+
+ }
+
+ /**
+ * HTTP GET
+ *
+ * This method simply fetches the contents of a uri, like normal
+ *
+ * @param string $uri
+ * @return bool
+ */
+ protected function httpGet($uri) {
+
+ $node = $this->tree->getNodeForPath($uri,0);
+
+ if (!$this->checkPreconditions(true)) return false;
+ if (!$node instanceof IFile) throw new Exception\NotImplemented('GET is only implemented on File objects');
+
+ $body = $node->get();
+
+ // Converting string into stream, if needed.
+ if (is_string($body)) {
+ $stream = fopen('php://temp','r+');
+ fwrite($stream,$body);
+ rewind($stream);
+ $body = $stream;
+ }
+
+ /*
+ * TODO: getetag, getlastmodified, getsize should also be used using
+ * this method
+ */
+ $httpHeaders = $this->getHTTPHeaders($uri);
+
+ /* ContentType needs to get a default, because many webservers will otherwise
+ * default to text/html, and we don't want this for security reasons.
+ */
+ if (!isset($httpHeaders['Content-Type'])) {
+ $httpHeaders['Content-Type'] = 'application/octet-stream';
+ }
+
+
+ if (isset($httpHeaders['Content-Length'])) {
+
+ $nodeSize = $httpHeaders['Content-Length'];
+
+ // Need to unset Content-Length, because we'll handle that during figuring out the range
+ unset($httpHeaders['Content-Length']);
+
+ } else {
+ $nodeSize = null;
+ }
+
+ $this->httpResponse->setHeaders($httpHeaders);
+
+ $range = $this->getHTTPRange();
+ $ifRange = $this->httpRequest->getHeader('If-Range');
+ $ignoreRangeHeader = false;
+
+ // If ifRange is set, and range is specified, we first need to check
+ // the precondition.
+ if ($nodeSize && $range && $ifRange) {
+
+ // if IfRange is parsable as a date we'll treat it as a DateTime
+ // otherwise, we must treat it as an etag.
+ try {
+ $ifRangeDate = new \DateTime($ifRange);
+
+ // It's a date. We must check if the entity is modified since
+ // the specified date.
+ if (!isset($httpHeaders['Last-Modified'])) $ignoreRangeHeader = true;
+ else {
+ $modified = new \DateTime($httpHeaders['Last-Modified']);
+ if($modified > $ifRangeDate) $ignoreRangeHeader = true;
+ }
+
+ } catch (\Exception $e) {
+
+ // It's an entity. We can do a simple comparison.
+ if (!isset($httpHeaders['ETag'])) $ignoreRangeHeader = true;
+ elseif ($httpHeaders['ETag']!==$ifRange) $ignoreRangeHeader = true;
+ }
+ }
+
+ // We're only going to support HTTP ranges if the backend provided a filesize
+ if (!$ignoreRangeHeader && $nodeSize && $range) {
+
+ // Determining the exact byte offsets
+ if (!is_null($range[0])) {
+
+ $start = $range[0];
+ $end = $range[1]?$range[1]:$nodeSize-1;
+ if($start >= $nodeSize)
+ throw new Exception\RequestedRangeNotSatisfiable('The start offset (' . $range[0] . ') exceeded the size of the entity (' . $nodeSize . ')');
+
+ if($end < $start) throw new Exception\RequestedRangeNotSatisfiable('The end offset (' . $range[1] . ') is lower than the start offset (' . $range[0] . ')');
+ if($end >= $nodeSize) $end = $nodeSize-1;
+
+ } else {
+
+ $start = $nodeSize-$range[1];
+ $end = $nodeSize-1;
+
+ if ($start<0) $start = 0;
+
+ }
+
+ // New read/write stream
+ $newStream = fopen('php://temp','r+');
+
+ // stream_copy_to_stream() has a bug/feature: the `whence` argument
+ // is interpreted as SEEK_SET (count from absolute offset 0), while
+ // for a stream it should be SEEK_CUR (count from current offset).
+ // If a stream is nonseekable, the function fails. So we *emulate*
+ // the correct behaviour with fseek():
+ if ($start > 0) {
+ if (($curOffs = ftell($body)) === false) $curOffs = 0;
+ fseek($body, $start - $curOffs, SEEK_CUR);
+ }
+ stream_copy_to_stream($body, $newStream, $end-$start+1);
+ rewind($newStream);
+
+ $this->httpResponse->setHeader('Content-Length', $end-$start+1);
+ $this->httpResponse->setHeader('Content-Range','bytes ' . $start . '-' . $end . '/' . $nodeSize);
+ $this->httpResponse->sendStatus(206);
+ $this->httpResponse->sendBody($newStream);
+
+
+ } else {
+
+ if ($nodeSize) $this->httpResponse->setHeader('Content-Length',$nodeSize);
+ $this->httpResponse->sendStatus(200);
+ $this->httpResponse->sendBody($body);
+
+ }
+
+ }
+
+ /**
+ * HTTP HEAD
+ *
+ * This method is normally used to take a peak at a url, and only get the HTTP response headers, without the body
+ * This is used by clients to determine if a remote file was changed, so they can use a local cached version, instead of downloading it again
+ *
+ * @param string $uri
+ * @return void
+ */
+ protected function httpHead($uri) {
+
+ $node = $this->tree->getNodeForPath($uri);
+ /* This information is only collection for File objects.
+ * Ideally we want to throw 405 Method Not Allowed for every
+ * non-file, but MS Office does not like this
+ */
+ if ($node instanceof IFile) {
+ $headers = $this->getHTTPHeaders($this->getRequestUri());
+ if (!isset($headers['Content-Type'])) {
+ $headers['Content-Type'] = 'application/octet-stream';
+ }
+ $this->httpResponse->setHeaders($headers);
+ }
+ $this->httpResponse->sendStatus(200);
+
+ }
+
+ /**
+ * HTTP Delete
+ *
+ * The HTTP delete method, deletes a given uri
+ *
+ * @param string $uri
+ * @return void
+ */
+ protected function httpDelete($uri) {
+
+ // Checking If-None-Match and related headers.
+ if (!$this->checkPreconditions()) return;
+
+ if (!$this->broadcastEvent('beforeUnbind',array($uri))) return;
+ $this->tree->delete($uri);
+ $this->broadcastEvent('afterUnbind',array($uri));
+
+ $this->httpResponse->sendStatus(204);
+ $this->httpResponse->setHeader('Content-Length','0');
+
+ }
+
+
+ /**
+ * WebDAV PROPFIND
+ *
+ * This WebDAV method requests information about an uri resource, or a list of resources
+ * If a client wants to receive the properties for a single resource it will add an HTTP Depth: header with a 0 value
+ * If the value is 1, it means that it also expects a list of sub-resources (e.g.: files in a directory)
+ *
+ * The request body contains an XML data structure that has a list of properties the client understands
+ * The response body is also an xml document, containing information about every uri resource and the requested properties
+ *
+ * It has to return a HTTP 207 Multi-status status code
+ *
+ * @param string $uri
+ * @return void
+ */
+ protected function httpPropfind($uri) {
+
+ $requestedProperties = $this->parsePropFindRequest($this->httpRequest->getBody(true));
+
+ $depth = $this->getHTTPDepth(1);
+ // The only two options for the depth of a propfind is 0 or 1
+ if ($depth!=0) $depth = 1;
+
+ $newProperties = $this->getPropertiesForPath($uri,$requestedProperties,$depth);
+
+ // This is a multi-status response
+ $this->httpResponse->sendStatus(207);
+ $this->httpResponse->setHeader('Content-Type','application/xml; charset=utf-8');
+ $this->httpResponse->setHeader('Vary','Brief,Prefer');
+
+ // Normally this header is only needed for OPTIONS responses, however..
+ // iCal seems to also depend on these being set for PROPFIND. Since
+ // this is not harmful, we'll add it.
+ $features = array('1','3', 'extended-mkcol');
+ foreach($this->plugins as $plugin) $features = array_merge($features,$plugin->getFeatures());
+ $this->httpResponse->setHeader('DAV',implode(', ',$features));
+
+ $prefer = $this->getHTTPPrefer();
+ $minimal = $prefer['return-minimal'];
+
+ $data = $this->generateMultiStatus($newProperties, $minimal);
+ $this->httpResponse->sendBody($data);
+
+ }
+
+ /**
+ * WebDAV PROPPATCH
+ *
+ * This method is called to update properties on a Node. The request is an XML body with all the mutations.
+ * In this XML body it is specified which properties should be set/updated and/or deleted
+ *
+ * @param string $uri
+ * @return void
+ */
+ protected function httpPropPatch($uri) {
+
+ $newProperties = $this->parsePropPatchRequest($this->httpRequest->getBody(true));
+
+ $result = $this->updateProperties($uri, $newProperties);
+
+ $prefer = $this->getHTTPPrefer();
+ $this->httpResponse->setHeader('Vary','Brief,Prefer');
+
+ if ($prefer['return-minimal']) {
+
+ // If return-minimal is specified, we only have to check if the
+ // request was succesful, and don't need to return the
+ // multi-status.
+ $ok = true;
+ foreach($result as $code=>$prop) {
+ if ((int)$code > 299) {
+ $ok = false;
+ }
+ }
+
+ if ($ok) {
+
+ $this->httpResponse->sendStatus(204);
+ return;
+
+ }
+
+ }
+
+ $this->httpResponse->sendStatus(207);
+ $this->httpResponse->setHeader('Content-Type','application/xml; charset=utf-8');
+
+ $this->httpResponse->sendBody(
+ $this->generateMultiStatus(array($result))
+ );
+
+ }
+
+ /**
+ * HTTP PUT method
+ *
+ * This HTTP method updates a file, or creates a new one.
+ *
+ * If a new resource was created, a 201 Created status code should be returned. If an existing resource is updated, it's a 204 No Content
+ *
+ * @param string $uri
+ * @return bool
+ */
+ protected function httpPut($uri) {
+
+ $body = $this->httpRequest->getBody();
+
+ // Intercepting Content-Range
+ if ($this->httpRequest->getHeader('Content-Range')) {
+ /**
+ Content-Range is dangerous for PUT requests: PUT per definition
+ stores a full resource. draft-ietf-httpbis-p2-semantics-15 says
+ in section 7.6:
+ An origin server SHOULD reject any PUT request that contains a
+ Content-Range header field, since it might be misinterpreted as
+ partial content (or might be partial content that is being mistakenly
+ PUT as a full representation). Partial content updates are possible
+ by targeting a separately identified resource with state that
+ overlaps a portion of the larger resource, or by using a different
+ method that has been specifically defined for partial updates (for
+ example, the PATCH method defined in [RFC5789]).
+ This clarifies RFC2616 section 9.6:
+ The recipient of the entity MUST NOT ignore any Content-*
+ (e.g. Content-Range) headers that it does not understand or implement
+ and MUST return a 501 (Not Implemented) response in such cases.
+ OTOH is a PUT request with a Content-Range currently the only way to
+ continue an aborted upload request and is supported by curl, mod_dav,
+ Tomcat and others. Since some clients do use this feature which results
+ in unexpected behaviour (cf PEAR::HTTP_WebDAV_Client 1.0.1), we reject
+ all PUT requests with a Content-Range for now.
+ */
+
+ throw new Exception\NotImplemented('PUT with Content-Range is not allowed.');
+ }
+
+ // Intercepting the Finder problem
+ if (($expected = $this->httpRequest->getHeader('X-Expected-Entity-Length')) && $expected > 0) {
+
+ /**
+ Many webservers will not cooperate well with Finder PUT requests,
+ because it uses 'Chunked' transfer encoding for the request body.
+
+ The symptom of this problem is that Finder sends files to the
+ server, but they arrive as 0-length files in PHP.
+
+ If we don't do anything, the user might think they are uploading
+ files successfully, but they end up empty on the server. Instead,
+ we throw back an error if we detect this.
+
+ The reason Finder uses Chunked, is because it thinks the files
+ might change as it's being uploaded, and therefore the
+ Content-Length can vary.
+
+ Instead it sends the X-Expected-Entity-Length header with the size
+ of the file at the very start of the request. If this header is set,
+ but we don't get a request body we will fail the request to
+ protect the end-user.
+ */
+
+ // Only reading first byte
+ $firstByte = fread($body,1);
+ if (strlen($firstByte)!==1) {
+ throw new Exception\Forbidden('This server is not compatible with OS/X finder. Consider using a different WebDAV client or webserver.');
+ }
+
+ // The body needs to stay intact, so we copy everything to a
+ // temporary stream.
+
+ $newBody = fopen('php://temp','r+');
+ fwrite($newBody,$firstByte);
+ stream_copy_to_stream($body, $newBody);
+ rewind($newBody);
+
+ $body = $newBody;
+
+ }
+
+ // Checking If-None-Match and related headers.
+ if (!$this->checkPreconditions()) return;
+
+ if ($this->tree->nodeExists($uri)) {
+
+ $node = $this->tree->getNodeForPath($uri);
+
+ // If the node is a collection, we'll deny it
+ if (!($node instanceof IFile)) throw new Exception\Conflict('PUT is not allowed on non-files.');
+ if (!$this->broadcastEvent('beforeWriteContent',array($uri, $node, &$body))) return false;
+
+ $etag = $node->put($body);
+
+ $this->broadcastEvent('afterWriteContent',array($uri, $node));
+
+ $this->httpResponse->setHeader('Content-Length','0');
+ if ($etag) $this->httpResponse->setHeader('ETag',$etag);
+ $this->httpResponse->sendStatus(204);
+
+ } else {
+
+ $etag = null;
+ // If we got here, the resource didn't exist yet.
+ if (!$this->createFile($this->getRequestUri(),$body,$etag)) {
+ // For one reason or another the file was not created.
+ return;
+ }
+
+ $this->httpResponse->setHeader('Content-Length','0');
+ if ($etag) $this->httpResponse->setHeader('ETag', $etag);
+ $this->httpResponse->sendStatus(201);
+
+ }
+
+ }
+
+
+ /**
+ * WebDAV MKCOL
+ *
+ * The MKCOL method is used to create a new collection (directory) on the server
+ *
+ * @param string $uri
+ * @return void
+ */
+ protected function httpMkcol($uri) {
+
+ $requestBody = $this->httpRequest->getBody(true);
+
+ if ($requestBody) {
+
+ $contentType = $this->httpRequest->getHeader('Content-Type');
+ if (strpos($contentType,'application/xml')!==0 && strpos($contentType,'text/xml')!==0) {
+
+ // We must throw 415 for unsupported mkcol bodies
+ throw new Exception\UnsupportedMediaType('The request body for the MKCOL request must have an xml Content-Type');
+
+ }
+
+ $dom = XMLUtil::loadDOMDocument($requestBody);
+ if (XMLUtil::toClarkNotation($dom->firstChild)!=='{DAV:}mkcol') {
+
+ // We must throw 415 for unsupported mkcol bodies
+ throw new Exception\UnsupportedMediaType('The request body for the MKCOL request must be a {DAV:}mkcol request construct.');
+
+ }
+
+ $properties = array();
+ foreach($dom->firstChild->childNodes as $childNode) {
+
+ if (XMLUtil::toClarkNotation($childNode)!=='{DAV:}set') continue;
+ $properties = array_merge($properties, XMLUtil::parseProperties($childNode, $this->propertyMap));
+
+ }
+ if (!isset($properties['{DAV:}resourcetype']))
+ throw new Exception\BadRequest('The mkcol request must include a {DAV:}resourcetype property');
+
+ $resourceType = $properties['{DAV:}resourcetype']->getValue();
+ unset($properties['{DAV:}resourcetype']);
+
+ } else {
+
+ $properties = array();
+ $resourceType = array('{DAV:}collection');
+
+ }
+
+ $result = $this->createCollection($uri, $resourceType, $properties);
+
+ if (is_array($result)) {
+ $this->httpResponse->sendStatus(207);
+ $this->httpResponse->setHeader('Content-Type','application/xml; charset=utf-8');
+
+ $this->httpResponse->sendBody(
+ $this->generateMultiStatus(array($result))
+ );
+
+ } else {
+ $this->httpResponse->setHeader('Content-Length','0');
+ $this->httpResponse->sendStatus(201);
+ }
+
+ }
+
+ /**
+ * WebDAV HTTP MOVE method
+ *
+ * This method moves one uri to a different uri. A lot of the actual request processing is done in getCopyMoveInfo
+ *
+ * @param string $uri
+ * @return bool
+ */
+ protected function httpMove($uri) {
+
+ $moveInfo = $this->getCopyAndMoveInfo();
+
+ // If the destination is part of the source tree, we must fail
+ if ($moveInfo['destination']==$uri)
+ throw new Exception\Forbidden('Source and destination uri are identical.');
+
+ if ($moveInfo['destinationExists']) {
+
+ if (!$this->broadcastEvent('beforeUnbind',array($moveInfo['destination']))) return false;
+ $this->tree->delete($moveInfo['destination']);
+ $this->broadcastEvent('afterUnbind',array($moveInfo['destination']));
+
+ }
+
+ if (!$this->broadcastEvent('beforeUnbind',array($uri))) return false;
+ if (!$this->broadcastEvent('beforeBind',array($moveInfo['destination']))) return false;
+ $this->tree->move($uri,$moveInfo['destination']);
+ $this->broadcastEvent('afterUnbind',array($uri));
+ $this->broadcastEvent('afterBind',array($moveInfo['destination']));
+
+ // If a resource was overwritten we should send a 204, otherwise a 201
+ $this->httpResponse->setHeader('Content-Length','0');
+ $this->httpResponse->sendStatus($moveInfo['destinationExists']?204:201);
+
+ }
+
+ /**
+ * WebDAV HTTP COPY method
+ *
+ * This method copies one uri to a different uri, and works much like the MOVE request
+ * A lot of the actual request processing is done in getCopyMoveInfo
+ *
+ * @param string $uri
+ * @return bool
+ */
+ protected function httpCopy($uri) {
+
+ $copyInfo = $this->getCopyAndMoveInfo();
+ // If the destination is part of the source tree, we must fail
+ if ($copyInfo['destination']==$uri)
+ throw new Exception\Forbidden('Source and destination uri are identical.');
+
+ if ($copyInfo['destinationExists']) {
+ if (!$this->broadcastEvent('beforeUnbind',array($copyInfo['destination']))) return false;
+ $this->tree->delete($copyInfo['destination']);
+
+ }
+ if (!$this->broadcastEvent('beforeBind',array($copyInfo['destination']))) return false;
+ $this->tree->copy($uri,$copyInfo['destination']);
+ $this->broadcastEvent('afterBind',array($copyInfo['destination']));
+
+ // If a resource was overwritten we should send a 204, otherwise a 201
+ $this->httpResponse->setHeader('Content-Length','0');
+ $this->httpResponse->sendStatus($copyInfo['destinationExists']?204:201);
+
+ }
+
+
+
+ /**
+ * HTTP REPORT method implementation
+ *
+ * Although the REPORT method is not part of the standard WebDAV spec (it's from rfc3253)
+ * It's used in a lot of extensions, so it made sense to implement it into the core.
+ *
+ * @param string $uri
+ * @return void
+ */
+ protected function httpReport($uri) {
+
+ $body = $this->httpRequest->getBody(true);
+ $dom = XMLUtil::loadDOMDocument($body);
+
+ $reportName = XMLUtil::toClarkNotation($dom->firstChild);
+
+ if ($this->broadcastEvent('report',array($reportName,$dom, $uri))) {
+
+ // If broadcastEvent returned true, it means the report was not supported
+ throw new Exception\ReportNotSupported();
+
+ }
+
+ }
+
+ // }}}
+ // {{{ HTTP/WebDAV protocol helpers
+
+ /**
+ * Returns an array with all the supported HTTP methods for a specific uri.
+ *
+ * @param string $uri
+ * @return array
+ */
+ public function getAllowedMethods($uri) {
+
+ $methods = array(
+ 'OPTIONS',
+ 'GET',
+ 'HEAD',
+ 'DELETE',
+ 'PROPFIND',
+ 'PUT',
+ 'PROPPATCH',
+ 'COPY',
+ 'MOVE',
+ 'REPORT'
+ );
+
+ // The MKCOL is only allowed on an unmapped uri
+ try {
+ $this->tree->getNodeForPath($uri);
+ } catch (Exception\NotFound $e) {
+ $methods[] = 'MKCOL';
+ }
+
+ // We're also checking if any of the plugins register any new methods
+ foreach($this->plugins as $plugin) $methods = array_merge($methods, $plugin->getHTTPMethods($uri));
+ array_unique($methods);
+
+ return $methods;
+
+ }
+
+ /**
+ * Gets the uri for the request, keeping the base uri into consideration
+ *
+ * @return string
+ */
+ public function getRequestUri() {
+
+ return $this->calculateUri($this->httpRequest->getUri());
+
+ }
+
+ /**
+ * Calculates the uri for a request, making sure that the base uri is stripped out
+ *
+ * @param string $uri
+ * @throws Exception\Forbidden A permission denied exception is thrown whenever there was an attempt to supply a uri outside of the base uri
+ * @return string
+ */
+ public function calculateUri($uri) {
+
+ if ($uri[0]!='/' && strpos($uri,'://')) {
+
+ $uri = parse_url($uri,PHP_URL_PATH);
+
+ }
+
+ $uri = str_replace('//','/',$uri);
+
+ if (strpos($uri,$this->getBaseUri())===0) {
+
+ return trim(URLUtil::decodePath(substr($uri,strlen($this->getBaseUri()))),'/');
+
+ // A special case, if the baseUri was accessed without a trailing
+ // slash, we'll accept it as well.
+ } elseif ($uri.'/' === $this->getBaseUri()) {
+
+ return '';
+
+ } else {
+
+ throw new Exception\Forbidden('Requested uri (' . $uri . ') is out of base uri (' . $this->getBaseUri() . ')');
+
+ }
+
+ }
+
+ /**
+ * Returns the HTTP depth header
+ *
+ * This method returns the contents of the HTTP depth request header. If the depth header was 'infinity' it will return the Sabre\DAV\Server::DEPTH_INFINITY object
+ * It is possible to supply a default depth value, which is used when the depth header has invalid content, or is completely non-existent
+ *
+ * @param mixed $default
+ * @return int
+ */
+ public function getHTTPDepth($default = self::DEPTH_INFINITY) {
+
+ // If its not set, we'll grab the default
+ $depth = $this->httpRequest->getHeader('Depth');
+
+ if (is_null($depth)) return $default;
+
+ if ($depth == 'infinity') return self::DEPTH_INFINITY;
+
+
+ // If its an unknown value. we'll grab the default
+ if (!ctype_digit($depth)) return $default;
+
+ return (int)$depth;
+
+ }
+
+ /**
+ * Returns the HTTP range header
+ *
+ * This method returns null if there is no well-formed HTTP range request
+ * header or array($start, $end).
+ *
+ * The first number is the offset of the first byte in the range.
+ * The second number is the offset of the last byte in the range.
+ *
+ * If the second offset is null, it should be treated as the offset of the last byte of the entity
+ * If the first offset is null, the second offset should be used to retrieve the last x bytes of the entity
+ *
+ * @return array|null
+ */
+ public function getHTTPRange() {
+
+ $range = $this->httpRequest->getHeader('range');
+ if (is_null($range)) return null;
+
+ // Matching "Range: bytes=1234-5678: both numbers are optional
+
+ if (!preg_match('/^bytes=([0-9]*)-([0-9]*)$/i',$range,$matches)) return null;
+
+ if ($matches[1]==='' && $matches[2]==='') return null;
+
+ return array(
+ $matches[1]!==''?$matches[1]:null,
+ $matches[2]!==''?$matches[2]:null,
+ );
+
+ }
+
+ /**
+ * Returns the HTTP Prefer header information.
+ *
+ * The prefer header is defined in:
+ * http://tools.ietf.org/html/draft-snell-http-prefer-14
+ *
+ * This method will return an array with options.
+ *
+ * Currently, the following options may be returned:
+ * array(
+ * 'return-asynch' => true,
+ * 'return-minimal' => true,
+ * 'return-representation' => true,
+ * 'wait' => 30,
+ * 'strict' => true,
+ * 'lenient' => true,
+ * )
+ *
+ * This method also supports the Brief header, and will also return
+ * 'return-minimal' if the brief header was set to 't'.
+ *
+ * For the boolean options, false will be returned if the headers are not
+ * specified. For the integer options it will be 'null'.
+ *
+ * @return array
+ */
+ public function getHTTPPrefer() {
+
+ $result = array(
+ 'return-asynch' => false,
+ 'return-minimal' => false,
+ 'return-representation' => false,
+ 'wait' => null,
+ 'strict' => false,
+ 'lenient' => false,
+ );
+
+ if ($prefer = $this->httpRequest->getHeader('Prefer')) {
+
+ $parameters = array_map('trim',
+ explode(',', $prefer)
+ );
+
+ foreach($parameters as $parameter) {
+
+ // Right now our regex only supports the tokens actually
+ // specified in the draft. We may need to expand this if new
+ // tokens get registered.
+ if(!preg_match('/^(?P[a-z0-9-]+)(?:=(?P[0-9]+))?$/', $parameter, $matches)) {
+ continue;
+ }
+
+ switch($matches['token']) {
+
+ case 'return-asynch' :
+ case 'return-minimal' :
+ case 'return-representation' :
+ case 'strict' :
+ case 'lenient' :
+ $result[$matches['token']] = true;
+ break;
+ case 'wait' :
+ $result[$matches['token']] = $matches['value'];
+ break;
+
+ }
+
+ }
+
+ }
+
+ if ($this->httpRequest->getHeader('Brief')=='t') {
+ $result['return-minimal'] = true;
+ }
+
+ return $result;
+
+ }
+
+
+ /**
+ * Returns information about Copy and Move requests
+ *
+ * This function is created to help getting information about the source and the destination for the
+ * WebDAV MOVE and COPY HTTP request. It also validates a lot of information and throws proper exceptions
+ *
+ * The returned value is an array with the following keys:
+ * * destination - Destination path
+ * * destinationExists - Whether or not the destination is an existing url (and should therefore be overwritten)
+ *
+ * @return array
+ */
+ public function getCopyAndMoveInfo() {
+
+ // Collecting the relevant HTTP headers
+ if (!$this->httpRequest->getHeader('Destination')) throw new Exception\BadRequest('The destination header was not supplied');
+ $destination = $this->calculateUri($this->httpRequest->getHeader('Destination'));
+ $overwrite = $this->httpRequest->getHeader('Overwrite');
+ if (!$overwrite) $overwrite = 'T';
+ if (strtoupper($overwrite)=='T') $overwrite = true;
+ elseif (strtoupper($overwrite)=='F') $overwrite = false;
+ // We need to throw a bad request exception, if the header was invalid
+ else throw new Exception\BadRequest('The HTTP Overwrite header should be either T or F');
+
+ list($destinationDir) = URLUtil::splitPath($destination);
+
+ try {
+ $destinationParent = $this->tree->getNodeForPath($destinationDir);
+ if (!($destinationParent instanceof ICollection)) throw new Exception\UnsupportedMediaType('The destination node is not a collection');
+ } catch (Exception\NotFound $e) {
+
+ // If the destination parent node is not found, we throw a 409
+ throw new Exception\Conflict('The destination node is not found');
+ }
+
+ try {
+
+ $destinationNode = $this->tree->getNodeForPath($destination);
+
+ // If this succeeded, it means the destination already exists
+ // we'll need to throw precondition failed in case overwrite is false
+ if (!$overwrite) throw new Exception\PreconditionFailed('The destination node already exists, and the overwrite header is set to false','Overwrite');
+
+ } catch (Exception\NotFound $e) {
+
+ // Destination didn't exist, we're all good
+ $destinationNode = false;
+
+
+
+ }
+
+ // These are the three relevant properties we need to return
+ return array(
+ 'destination' => $destination,
+ 'destinationExists' => $destinationNode==true,
+ 'destinationNode' => $destinationNode,
+ );
+
+ }
+
+ /**
+ * Returns a list of properties for a path
+ *
+ * This is a simplified version getPropertiesForPath.
+ * if you aren't interested in status codes, but you just
+ * want to have a flat list of properties. Use this method.
+ *
+ * @param string $path
+ * @param array $propertyNames
+ */
+ public function getProperties($path, $propertyNames) {
+
+ $result = $this->getPropertiesForPath($path,$propertyNames,0);
+ return $result[0][200];
+
+ }
+
+ /**
+ * A kid-friendly way to fetch properties for a node's children.
+ *
+ * The returned array will be indexed by the path of the of child node.
+ * Only properties that are actually found will be returned.
+ *
+ * The parent node will not be returned.
+ *
+ * @param string $path
+ * @param array $propertyNames
+ * @return array
+ */
+ public function getPropertiesForChildren($path, $propertyNames) {
+
+ $result = array();
+ foreach($this->getPropertiesForPath($path,$propertyNames,1) as $k=>$row) {
+
+ // Skipping the parent path
+ if ($k === 0) continue;
+
+ $result[$row['href']] = $row[200];
+
+ }
+ return $result;
+
+ }
+
+ /**
+ * Returns a list of HTTP headers for a particular resource
+ *
+ * The generated http headers are based on properties provided by the
+ * resource. The method basically provides a simple mapping between
+ * DAV property and HTTP header.
+ *
+ * The headers are intended to be used for HEAD and GET requests.
+ *
+ * @param string $path
+ * @return array
+ */
+ public function getHTTPHeaders($path) {
+
+ $propertyMap = array(
+ '{DAV:}getcontenttype' => 'Content-Type',
+ '{DAV:}getcontentlength' => 'Content-Length',
+ '{DAV:}getlastmodified' => 'Last-Modified',
+ '{DAV:}getetag' => 'ETag',
+ );
+
+ $properties = $this->getProperties($path,array_keys($propertyMap));
+
+ $headers = array();
+ foreach($propertyMap as $property=>$header) {
+ if (!isset($properties[$property])) continue;
+
+ if (is_scalar($properties[$property])) {
+ $headers[$header] = $properties[$property];
+
+ // GetLastModified gets special cased
+ } elseif ($properties[$property] instanceof Property\GetLastModified) {
+ $headers[$header] = HTTP\Util::toHTTPDate($properties[$property]->getTime());
+ }
+
+ }
+
+ return $headers;
+
+ }
+
+ /**
+ * Returns a list of properties for a given path
+ *
+ * The path that should be supplied should have the baseUrl stripped out
+ * The list of properties should be supplied in Clark notation. If the list is empty
+ * 'allprops' is assumed.
+ *
+ * If a depth of 1 is requested child elements will also be returned.
+ *
+ * @param string $path
+ * @param array $propertyNames
+ * @param int $depth
+ * @return array
+ */
+ public function getPropertiesForPath($path, $propertyNames = array(), $depth = 0) {
+
+ if ($depth!=0) $depth = 1;
+
+ $path = rtrim($path,'/');
+
+ // This event allows people to intercept these requests early on in the
+ // process.
+ //
+ // We're not doing anything with the result, but this can be helpful to
+ // pre-fetch certain expensive live properties.
+ $this->broadCastEvent('beforeGetPropertiesForPath', array($path, $propertyNames, $depth));
+
+ $returnPropertyList = array();
+
+ $parentNode = $this->tree->getNodeForPath($path);
+ $nodes = array(
+ $path => $parentNode
+ );
+ if ($depth==1 && $parentNode instanceof ICollection) {
+ foreach($this->tree->getChildren($path) as $childNode)
+ $nodes[$path . '/' . $childNode->getName()] = $childNode;
+ }
+
+ // If the propertyNames array is empty, it means all properties are requested.
+ // We shouldn't actually return everything we know though, and only return a
+ // sensible list.
+ $allProperties = count($propertyNames)==0;
+
+ foreach($nodes as $myPath=>$node) {
+
+ $currentPropertyNames = $propertyNames;
+
+ $newProperties = array(
+ '200' => array(),
+ '404' => array(),
+ );
+
+ if ($allProperties) {
+ // Default list of propertyNames, when all properties were requested.
+ $currentPropertyNames = array(
+ '{DAV:}getlastmodified',
+ '{DAV:}getcontentlength',
+ '{DAV:}resourcetype',
+ '{DAV:}quota-used-bytes',
+ '{DAV:}quota-available-bytes',
+ '{DAV:}getetag',
+ '{DAV:}getcontenttype',
+ );
+ }
+
+ // If the resourceType was not part of the list, we manually add it
+ // and mark it for removal. We need to know the resourcetype in order
+ // to make certain decisions about the entry.
+ // WebDAV dictates we should add a / and the end of href's for collections
+ $removeRT = false;
+ if (!in_array('{DAV:}resourcetype',$currentPropertyNames)) {
+ $currentPropertyNames[] = '{DAV:}resourcetype';
+ $removeRT = true;
+ }
+
+ $result = $this->broadcastEvent('beforeGetProperties',array($myPath, $node, &$currentPropertyNames, &$newProperties));
+ // If this method explicitly returned false, we must ignore this
+ // node as it is inaccessible.
+ if ($result===false) continue;
+
+ if (count($currentPropertyNames) > 0) {
+
+ if ($node instanceof IProperties) {
+ $nodeProperties = $node->getProperties($currentPropertyNames);
+
+ // The getProperties method may give us too much,
+ // properties, in case the implementor was lazy.
+ //
+ // So as we loop through this list, we will only take the
+ // properties that were actually requested and discard the
+ // rest.
+ foreach($currentPropertyNames as $k=>$currentPropertyName) {
+ if (isset($nodeProperties[$currentPropertyName])) {
+ unset($currentPropertyNames[$k]);
+ $newProperties[200][$currentPropertyName] = $nodeProperties[$currentPropertyName];
+ }
+ }
+
+ }
+
+ }
+
+ foreach($currentPropertyNames as $prop) {
+
+ if (isset($newProperties[200][$prop])) continue;
+
+ switch($prop) {
+ case '{DAV:}getlastmodified' : if ($node->getLastModified()) $newProperties[200][$prop] = new Property\GetLastModified($node->getLastModified()); break;
+ case '{DAV:}getcontentlength' :
+ if ($node instanceof IFile) {
+ $size = $node->getSize();
+ if (!is_null($size)) {
+ $newProperties[200][$prop] = (int)$node->getSize();
+ }
+ }
+ break;
+ case '{DAV:}quota-used-bytes' :
+ if ($node instanceof IQuota) {
+ $quotaInfo = $node->getQuotaInfo();
+ $newProperties[200][$prop] = $quotaInfo[0];
+ }
+ break;
+ case '{DAV:}quota-available-bytes' :
+ if ($node instanceof IQuota) {
+ $quotaInfo = $node->getQuotaInfo();
+ $newProperties[200][$prop] = $quotaInfo[1];
+ }
+ break;
+ case '{DAV:}getetag' : if ($node instanceof IFile && $etag = $node->getETag()) $newProperties[200][$prop] = $etag; break;
+ case '{DAV:}getcontenttype' : if ($node instanceof IFile && $ct = $node->getContentType()) $newProperties[200][$prop] = $ct; break;
+ case '{DAV:}supported-report-set' :
+ $reports = array();
+ foreach($this->plugins as $plugin) {
+ $reports = array_merge($reports, $plugin->getSupportedReportSet($myPath));
+ }
+ $newProperties[200][$prop] = new Property\SupportedReportSet($reports);
+ break;
+ case '{DAV:}resourcetype' :
+ $newProperties[200]['{DAV:}resourcetype'] = new Property\ResourceType();
+ foreach($this->resourceTypeMapping as $className => $resourceType) {
+ if ($node instanceof $className) $newProperties[200]['{DAV:}resourcetype']->add($resourceType);
+ }
+ break;
+
+ }
+
+ // If we were unable to find the property, we will list it as 404.
+ if (!$allProperties && !isset($newProperties[200][$prop])) $newProperties[404][$prop] = null;
+
+ }
+
+ $this->broadcastEvent('afterGetProperties',array(trim($myPath,'/'),&$newProperties, $node));
+
+ $newProperties['href'] = trim($myPath,'/');
+
+ // Its is a WebDAV recommendation to add a trailing slash to collectionnames.
+ // Apple's iCal also requires a trailing slash for principals (rfc 3744), though this is non-standard.
+ if ($myPath!='' && isset($newProperties[200]['{DAV:}resourcetype'])) {
+ $rt = $newProperties[200]['{DAV:}resourcetype'];
+ if ($rt->is('{DAV:}collection') || $rt->is('{DAV:}principal')) {
+ $newProperties['href'] .='/';
+ }
+ }
+
+ // If the resourcetype property was manually added to the requested property list,
+ // we will remove it again.
+ if ($removeRT) unset($newProperties[200]['{DAV:}resourcetype']);
+
+ $returnPropertyList[] = $newProperties;
+
+ }
+
+ return $returnPropertyList;
+
+ }
+
+ /**
+ * This method is invoked by sub-systems creating a new file.
+ *
+ * Currently this is done by HTTP PUT and HTTP LOCK (in the Locks_Plugin).
+ * It was important to get this done through a centralized function,
+ * allowing plugins to intercept this using the beforeCreateFile event.
+ *
+ * This method will return true if the file was actually created
+ *
+ * @param string $uri
+ * @param resource $data
+ * @param string $etag
+ * @return bool
+ */
+ public function createFile($uri,$data, &$etag = null) {
+
+ list($dir,$name) = URLUtil::splitPath($uri);
+
+ if (!$this->broadcastEvent('beforeBind',array($uri))) return false;
+
+ $parent = $this->tree->getNodeForPath($dir);
+ if (!$parent instanceof ICollection) {
+ throw new Exception\Conflict('Files can only be created as children of collections');
+ }
+
+ if (!$this->broadcastEvent('beforeCreateFile',array($uri, &$data, $parent))) return false;
+
+ $etag = $parent->createFile($name,$data);
+ $this->tree->markDirty($dir . '/' . $name);
+
+ $this->broadcastEvent('afterBind',array($uri));
+ $this->broadcastEvent('afterCreateFile',array($uri, $parent));
+
+ return true;
+ }
+
+ /**
+ * This method is invoked by sub-systems creating a new directory.
+ *
+ * @param string $uri
+ * @return void
+ */
+ public function createDirectory($uri) {
+
+ $this->createCollection($uri,array('{DAV:}collection'),array());
+
+ }
+
+ /**
+ * Use this method to create a new collection
+ *
+ * The {DAV:}resourcetype is specified using the resourceType array.
+ * At the very least it must contain {DAV:}collection.
+ *
+ * The properties array can contain a list of additional properties.
+ *
+ * @param string $uri The new uri
+ * @param array $resourceType The resourceType(s)
+ * @param array $properties A list of properties
+ * @return array|null
+ */
+ public function createCollection($uri, array $resourceType, array $properties) {
+
+ list($parentUri,$newName) = URLUtil::splitPath($uri);
+
+ // Making sure {DAV:}collection was specified as resourceType
+ if (!in_array('{DAV:}collection', $resourceType)) {
+ throw new Exception\InvalidResourceType('The resourceType for this collection must at least include {DAV:}collection');
+ }
+
+
+ // Making sure the parent exists
+ try {
+
+ $parent = $this->tree->getNodeForPath($parentUri);
+
+ } catch (Exception\NotFound $e) {
+
+ throw new Exception\Conflict('Parent node does not exist');
+
+ }
+
+ // Making sure the parent is a collection
+ if (!$parent instanceof ICollection) {
+ throw new Exception\Conflict('Parent node is not a collection');
+ }
+
+
+
+ // Making sure the child does not already exist
+ try {
+ $parent->getChild($newName);
+
+ // If we got here.. it means there's already a node on that url, and we need to throw a 405
+ throw new Exception\MethodNotAllowed('The resource you tried to create already exists');
+
+ } catch (Exception\NotFound $e) {
+ // This is correct
+ }
+
+
+ if (!$this->broadcastEvent('beforeBind',array($uri))) return;
+
+ // There are 2 modes of operation. The standard collection
+ // creates the directory, and then updates properties
+ // the extended collection can create it directly.
+ if ($parent instanceof IExtendedCollection) {
+
+ $parent->createExtendedCollection($newName, $resourceType, $properties);
+
+ } else {
+
+ // No special resourcetypes are supported
+ if (count($resourceType)>1) {
+ throw new Exception\InvalidResourceType('The {DAV:}resourcetype you specified is not supported here.');
+ }
+
+ $parent->createDirectory($newName);
+ $rollBack = false;
+ $exception = null;
+ $errorResult = null;
+
+ if (count($properties)>0) {
+
+ try {
+
+ $errorResult = $this->updateProperties($uri, $properties);
+ if (!isset($errorResult[200])) {
+ $rollBack = true;
+ }
+
+ } catch (Exception $e) {
+
+ $rollBack = true;
+ $exception = $e;
+
+ }
+
+ }
+
+ if ($rollBack) {
+ if (!$this->broadcastEvent('beforeUnbind',array($uri))) return;
+ $this->tree->delete($uri);
+
+ // Re-throwing exception
+ if ($exception) throw $exception;
+
+ return $errorResult;
+ }
+
+ }
+ $this->tree->markDirty($parentUri);
+ $this->broadcastEvent('afterBind',array($uri));
+
+ }
+
+ /**
+ * This method updates a resource's properties
+ *
+ * The properties array must be a list of properties. Array-keys are
+ * property names in clarknotation, array-values are it's values.
+ * If a property must be deleted, the value should be null.
+ *
+ * Note that this request should either completely succeed, or
+ * completely fail.
+ *
+ * The response is an array with statuscodes for keys, which in turn
+ * contain arrays with propertynames. This response can be used
+ * to generate a multistatus body.
+ *
+ * @param string $uri
+ * @param array $properties
+ * @return array
+ */
+ public function updateProperties($uri, array $properties) {
+
+ // we'll start by grabbing the node, this will throw the appropriate
+ // exceptions if it doesn't.
+ $node = $this->tree->getNodeForPath($uri);
+
+ $result = array(
+ 200 => array(),
+ 403 => array(),
+ 424 => array(),
+ );
+ $remainingProperties = $properties;
+ $hasError = false;
+
+ // Running through all properties to make sure none of them are protected
+ if (!$hasError) foreach($properties as $propertyName => $value) {
+ if(in_array($propertyName, $this->protectedProperties)) {
+ $result[403][$propertyName] = null;
+ unset($remainingProperties[$propertyName]);
+ $hasError = true;
+ }
+ }
+
+ if (!$hasError) {
+ // Allowing plugins to take care of property updating
+ $hasError = !$this->broadcastEvent('updateProperties',array(
+ &$remainingProperties,
+ &$result,
+ $node
+ ));
+ }
+
+ // If the node is not an instance of Sabre\DAV\IProperties, every
+ // property is 403 Forbidden
+ if (!$hasError && count($remainingProperties) && !($node instanceof IProperties)) {
+ $hasError = true;
+ foreach($properties as $propertyName=> $value) {
+ $result[403][$propertyName] = null;
+ }
+ $remainingProperties = array();
+ }
+
+ // Only if there were no errors we may attempt to update the resource
+ if (!$hasError) {
+
+ if (count($remainingProperties)>0) {
+
+ $updateResult = $node->updateProperties($remainingProperties);
+
+ if ($updateResult===true) {
+ // success
+ foreach($remainingProperties as $propertyName=>$value) {
+ $result[200][$propertyName] = null;
+ }
+
+ } elseif ($updateResult===false) {
+ // The node failed to update the properties for an
+ // unknown reason
+ foreach($remainingProperties as $propertyName=>$value) {
+ $result[403][$propertyName] = null;
+ }
+
+ } elseif (is_array($updateResult)) {
+
+ // The node has detailed update information
+ // We need to merge the results with the earlier results.
+ foreach($updateResult as $status => $props) {
+ if (is_array($props)) {
+ if (!isset($result[$status]))
+ $result[$status] = array();
+
+ $result[$status] = array_merge($result[$status], $updateResult[$status]);
+ }
+ }
+
+ } else {
+ throw new Exception('Invalid result from updateProperties');
+ }
+ $remainingProperties = array();
+ }
+
+ }
+
+ foreach($remainingProperties as $propertyName=>$value) {
+ // if there are remaining properties, it must mean
+ // there's a dependency failure
+ $result[424][$propertyName] = null;
+ }
+
+ // Removing empty array values
+ foreach($result as $status=>$props) {
+
+ if (count($props)===0) unset($result[$status]);
+
+ }
+ $result['href'] = $uri;
+ return $result;
+
+ }
+
+ /**
+ * This method checks the main HTTP preconditions.
+ *
+ * Currently these are:
+ * * If-Match
+ * * If-None-Match
+ * * If-Modified-Since
+ * * If-Unmodified-Since
+ *
+ * The method will return true if all preconditions are met
+ * The method will return false, or throw an exception if preconditions
+ * failed. If false is returned the operation should be aborted, and
+ * the appropriate HTTP response headers are already set.
+ *
+ * Normally this method will throw 412 Precondition Failed for failures
+ * related to If-None-Match, If-Match and If-Unmodified Since. It will
+ * set the status to 304 Not Modified for If-Modified_since.
+ *
+ * If the $handleAsGET argument is set to true, it will also return 304
+ * Not Modified for failure of the If-None-Match precondition. This is the
+ * desired behaviour for HTTP GET and HTTP HEAD requests.
+ *
+ * @param bool $handleAsGET
+ * @return bool
+ */
+ public function checkPreconditions($handleAsGET = false) {
+
+ $uri = $this->getRequestUri();
+ $node = null;
+ $lastMod = null;
+ $etag = null;
+
+ if ($ifMatch = $this->httpRequest->getHeader('If-Match')) {
+
+ // If-Match contains an entity tag. Only if the entity-tag
+ // matches we are allowed to make the request succeed.
+ // If the entity-tag is '*' we are only allowed to make the
+ // request succeed if a resource exists at that url.
+ try {
+ $node = $this->tree->getNodeForPath($uri);
+ } catch (Exception\NotFound $e) {
+ throw new Exception\PreconditionFailed('An If-Match header was specified and the resource did not exist','If-Match');
+ }
+
+ // Only need to check entity tags if they are not *
+ if ($ifMatch!=='*') {
+
+ // There can be multiple etags
+ $ifMatch = explode(',',$ifMatch);
+ $haveMatch = false;
+ foreach($ifMatch as $ifMatchItem) {
+
+ // Stripping any extra spaces
+ $ifMatchItem = trim($ifMatchItem,' ');
+
+ $etag = $node->getETag();
+ if ($etag===$ifMatchItem) {
+ $haveMatch = true;
+ } else {
+ // Evolution has a bug where it sometimes prepends the "
+ // with a \. This is our workaround.
+ if (str_replace('\\"','"', $ifMatchItem) === $etag) {
+ $haveMatch = true;
+ }
+ }
+
+ }
+ if (!$haveMatch) {
+ throw new Exception\PreconditionFailed('An If-Match header was specified, but none of the specified the ETags matched.','If-Match');
+ }
+ }
+ }
+
+ if ($ifNoneMatch = $this->httpRequest->getHeader('If-None-Match')) {
+
+ // The If-None-Match header contains an etag.
+ // Only if the ETag does not match the current ETag, the request will succeed
+ // The header can also contain *, in which case the request
+ // will only succeed if the entity does not exist at all.
+ $nodeExists = true;
+ if (!$node) {
+ try {
+ $node = $this->tree->getNodeForPath($uri);
+ } catch (Exception\NotFound $e) {
+ $nodeExists = false;
+ }
+ }
+ if ($nodeExists) {
+ $haveMatch = false;
+ if ($ifNoneMatch==='*') $haveMatch = true;
+ else {
+
+ // There might be multiple etags
+ $ifNoneMatch = explode(',', $ifNoneMatch);
+ $etag = $node->getETag();
+
+ foreach($ifNoneMatch as $ifNoneMatchItem) {
+
+ // Stripping any extra spaces
+ $ifNoneMatchItem = trim($ifNoneMatchItem,' ');
+
+ if ($etag===$ifNoneMatchItem) $haveMatch = true;
+
+ }
+
+ }
+
+ if ($haveMatch) {
+ if ($handleAsGET) {
+ $this->httpResponse->sendStatus(304);
+ return false;
+ } else {
+ throw new Exception\PreconditionFailed('An If-None-Match header was specified, but the ETag matched (or * was specified).','If-None-Match');
+ }
+ }
+ }
+
+ }
+
+ if (!$ifNoneMatch && ($ifModifiedSince = $this->httpRequest->getHeader('If-Modified-Since'))) {
+
+ // The If-Modified-Since header contains a date. We
+ // will only return the entity if it has been changed since
+ // that date. If it hasn't been changed, we return a 304
+ // header
+ // Note that this header only has to be checked if there was no If-None-Match header
+ // as per the HTTP spec.
+ $date = HTTP\Util::parseHTTPDate($ifModifiedSince);
+
+ if ($date) {
+ if (is_null($node)) {
+ $node = $this->tree->getNodeForPath($uri);
+ }
+ $lastMod = $node->getLastModified();
+ if ($lastMod) {
+ $lastMod = new \DateTime('@' . $lastMod);
+ if ($lastMod <= $date) {
+ $this->httpResponse->sendStatus(304);
+ $this->httpResponse->setHeader('Last-Modified', HTTP\Util::toHTTPDate($lastMod));
+ return false;
+ }
+ }
+ }
+ }
+
+ if ($ifUnmodifiedSince = $this->httpRequest->getHeader('If-Unmodified-Since')) {
+
+ // The If-Unmodified-Since will allow allow the request if the
+ // entity has not changed since the specified date.
+ $date = HTTP\Util::parseHTTPDate($ifUnmodifiedSince);
+
+ // We must only check the date if it's valid
+ if ($date) {
+ if (is_null($node)) {
+ $node = $this->tree->getNodeForPath($uri);
+ }
+ $lastMod = $node->getLastModified();
+ if ($lastMod) {
+ $lastMod = new \DateTime('@' . $lastMod);
+ if ($lastMod > $date) {
+ throw new Exception\PreconditionFailed('An If-Unmodified-Since header was specified, but the entity has been changed since the specified date.','If-Unmodified-Since');
+ }
+ }
+ }
+
+ }
+ return true;
+
+ }
+
+ // }}}
+ // {{{ XML Readers & Writers
+
+
+ /**
+ * Generates a WebDAV propfind response body based on a list of nodes.
+ *
+ * If 'strip404s' is set to true, all 404 responses will be removed.
+ *
+ * @param array $fileProperties The list with nodes
+ * @param bool strip404s
+ * @return string
+ */
+ public function generateMultiStatus(array $fileProperties, $strip404s = false) {
+
+ $dom = new \DOMDocument('1.0','utf-8');
+ //$dom->formatOutput = true;
+ $multiStatus = $dom->createElement('d:multistatus');
+ $dom->appendChild($multiStatus);
+
+ // Adding in default namespaces
+ foreach($this->xmlNamespaces as $namespace=>$prefix) {
+
+ $multiStatus->setAttribute('xmlns:' . $prefix,$namespace);
+
+ }
+
+ foreach($fileProperties as $entry) {
+
+ $href = $entry['href'];
+ unset($entry['href']);
+
+ if ($strip404s && isset($entry[404])) {
+ unset($entry[404]);
+ }
+
+ $response = new Property\Response($href,$entry);
+ $response->serialize($this,$multiStatus);
+
+ }
+
+ return $dom->saveXML();
+
+ }
+
+ /**
+ * This method parses a PropPatch request
+ *
+ * PropPatch changes the properties for a resource. This method
+ * returns a list of properties.
+ *
+ * The keys in the returned array contain the property name (e.g.: {DAV:}displayname,
+ * and the value contains the property value. If a property is to be removed the value
+ * will be null.
+ *
+ * @param string $body xml body
+ * @return array list of properties in need of updating or deletion
+ */
+ public function parsePropPatchRequest($body) {
+
+ //We'll need to change the DAV namespace declaration to something else in order to make it parsable
+ $dom = XMLUtil::loadDOMDocument($body);
+
+ $newProperties = array();
+
+ foreach($dom->firstChild->childNodes as $child) {
+
+ if ($child->nodeType !== XML_ELEMENT_NODE) continue;
+
+ $operation = XMLUtil::toClarkNotation($child);
+
+ if ($operation!=='{DAV:}set' && $operation!=='{DAV:}remove') continue;
+
+ $innerProperties = XMLUtil::parseProperties($child, $this->propertyMap);
+
+ foreach($innerProperties as $propertyName=>$propertyValue) {
+
+ if ($operation==='{DAV:}remove') {
+ $propertyValue = null;
+ }
+
+ $newProperties[$propertyName] = $propertyValue;
+
+ }
+
+ }
+
+ return $newProperties;
+
+ }
+
+ /**
+ * This method parses the PROPFIND request and returns its information
+ *
+ * This will either be a list of properties, or an empty array; in which case
+ * an {DAV:}allprop was requested.
+ *
+ * @param string $body
+ * @return array
+ */
+ public function parsePropFindRequest($body) {
+
+ // If the propfind body was empty, it means IE is requesting 'all' properties
+ if (!$body) return array();
+
+ $dom = XMLUtil::loadDOMDocument($body);
+ $elem = $dom->getElementsByTagNameNS('urn:DAV','propfind')->item(0);
+ return array_keys(XMLUtil::parseProperties($elem));
+
+ }
+
+ // }}}
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/ServerPlugin.php b/app/Gdoo/Calendar/Sabre/DAV/ServerPlugin.php
new file mode 100644
index 00000000..d232cfe4
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/ServerPlugin.php
@@ -0,0 +1,90 @@
+name = $name;
+ foreach($children as $child) {
+
+ if (!($child instanceof INode)) throw new Exception('Only instances of Sabre\DAV\INode are allowed to be passed in the children argument');
+ $this->addChild($child);
+
+ }
+
+ }
+
+ /**
+ * Adds a new childnode to this collection
+ *
+ * @param INode $child
+ * @return void
+ */
+ public function addChild(INode $child) {
+
+ $this->children[$child->getName()] = $child;
+
+ }
+
+ /**
+ * Returns the name of the collection
+ *
+ * @return string
+ */
+ public function getName() {
+
+ return $this->name;
+
+ }
+
+ /**
+ * Returns a child object, by its name.
+ *
+ * This method makes use of the getChildren method to grab all the child nodes, and compares the name.
+ * Generally its wise to override this, as this can usually be optimized
+ *
+ * This method must throw Sabre\DAV\Exception\NotFound if the node does not
+ * exist.
+ *
+ * @param string $name
+ * @throws Exception\NotFound
+ * @return INode
+ */
+ public function getChild($name) {
+
+ if (isset($this->children[$name])) return $this->children[$name];
+ throw new Exception\NotFound('File not found: ' . $name . ' in \'' . $this->getName() . '\'');
+
+ }
+
+ /**
+ * Returns a list of children for this collection
+ *
+ * @return array
+ */
+ public function getChildren() {
+
+ return array_values($this->children);
+
+ }
+
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/SimpleFile.php b/app/Gdoo/Calendar/Sabre/DAV/SimpleFile.php
new file mode 100644
index 00000000..ede3ff48
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/SimpleFile.php
@@ -0,0 +1,121 @@
+name = $name;
+ $this->contents = $contents;
+ $this->mimeType = $mimeType;
+
+ }
+
+ /**
+ * Returns the node name for this file.
+ *
+ * This name is used to construct the url.
+ *
+ * @return string
+ */
+ public function getName() {
+
+ return $this->name;
+
+ }
+
+ /**
+ * Returns the data
+ *
+ * This method may either return a string or a readable stream resource
+ *
+ * @return mixed
+ */
+ public function get() {
+
+ return $this->contents;
+
+ }
+
+ /**
+ * Returns the size of the file, in bytes.
+ *
+ * @return int
+ */
+ public function getSize() {
+
+ return strlen($this->contents);
+
+ }
+
+ /**
+ * Returns the ETag for a file
+ *
+ * An ETag is a unique identifier representing the current version of the file. If the file changes, the ETag MUST change.
+ * The ETag is an arbitrary string, but MUST be surrounded by double-quotes.
+ *
+ * Return null if the ETag can not effectively be determined
+ * @return string
+ */
+ public function getETag() {
+
+ return '"' . md5($this->contents) . '"';
+
+ }
+
+ /**
+ * Returns the mime-type for a file
+ *
+ * If null is returned, we'll assume application/octet-stream
+ * @return string
+ */
+ public function getContentType() {
+
+ return $this->mimeType;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/StringUtil.php b/app/Gdoo/Calendar/Sabre/DAV/StringUtil.php
new file mode 100644
index 00000000..3426dd44
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/StringUtil.php
@@ -0,0 +1,91 @@
+dataDir = $dataDir;
+
+ }
+
+ /**
+ * Initialize the plugin
+ *
+ * This is called automatically be the Server class after this plugin is
+ * added with Sabre\DAV\Server::addPlugin()
+ *
+ * @param Server $server
+ * @return void
+ */
+ public function initialize(Server $server) {
+
+ $this->server = $server;
+ $server->subscribeEvent('beforeMethod',array($this,'beforeMethod'));
+ $server->subscribeEvent('beforeCreateFile',array($this,'beforeCreateFile'));
+
+ }
+
+ /**
+ * This method is called before any HTTP method handler
+ *
+ * This method intercepts any GET, DELETE, PUT and PROPFIND calls to
+ * filenames that are known to match the 'temporary file' regex.
+ *
+ * @param string $method
+ * @param string $uri
+ * @return bool
+ */
+ public function beforeMethod($method, $uri) {
+
+ if (!$tempLocation = $this->isTempFile($uri))
+ return true;
+
+ switch($method) {
+ case 'GET' :
+ return $this->httpGet($tempLocation);
+ case 'PUT' :
+ return $this->httpPut($tempLocation);
+ case 'PROPFIND' :
+ return $this->httpPropfind($tempLocation, $uri);
+ case 'DELETE' :
+ return $this->httpDelete($tempLocation);
+ }
+ return true;
+
+ }
+
+ /**
+ * This method is invoked if some subsystem creates a new file.
+ *
+ * This is used to deal with HTTP LOCK requests which create a new
+ * file.
+ *
+ * @param string $uri
+ * @param resource $data
+ * @return bool
+ */
+ public function beforeCreateFile($uri,$data) {
+
+ if ($tempPath = $this->isTempFile($uri)) {
+
+ $hR = $this->server->httpResponse;
+ $hR->setHeader('X-Sabre-Temp','true');
+ file_put_contents($tempPath,$data);
+ return false;
+ }
+ return true;
+
+ }
+
+ /**
+ * This method will check if the url matches the temporary file pattern
+ * if it does, it will return an path based on $this->dataDir for the
+ * temporary file storage.
+ *
+ * @param string $path
+ * @return boolean|string
+ */
+ protected function isTempFile($path) {
+
+ // We're only interested in the basename.
+ list(, $tempPath) = URLUtil::splitPath($path);
+
+ foreach($this->temporaryFilePatterns as $tempFile) {
+
+ if (preg_match($tempFile,$tempPath)) {
+ return $this->getDataDir() . '/sabredav_' . md5($path) . '.tempfile';
+ }
+
+ }
+
+ return false;
+
+ }
+
+
+ /**
+ * This method handles the GET method for temporary files.
+ * If the file doesn't exist, it will return false which will kick in
+ * the regular system for the GET method.
+ *
+ * @param string $tempLocation
+ * @return bool
+ */
+ public function httpGet($tempLocation) {
+
+ if (!file_exists($tempLocation)) return true;
+
+ $hR = $this->server->httpResponse;
+ $hR->setHeader('Content-Type','application/octet-stream');
+ $hR->setHeader('Content-Length',filesize($tempLocation));
+ $hR->setHeader('X-Sabre-Temp','true');
+ $hR->sendStatus(200);
+ $hR->sendBody(fopen($tempLocation,'r'));
+ return false;
+
+ }
+
+ /**
+ * This method handles the PUT method.
+ *
+ * @param string $tempLocation
+ * @return bool
+ */
+ public function httpPut($tempLocation) {
+
+ $hR = $this->server->httpResponse;
+ $hR->setHeader('X-Sabre-Temp','true');
+
+ $newFile = !file_exists($tempLocation);
+
+ if (!$newFile && ($this->server->httpRequest->getHeader('If-None-Match'))) {
+ throw new Exception\PreconditionFailed('The resource already exists, and an If-None-Match header was supplied');
+ }
+
+ file_put_contents($tempLocation,$this->server->httpRequest->getBody());
+ $hR->sendStatus($newFile?201:200);
+ return false;
+
+ }
+
+ /**
+ * This method handles the DELETE method.
+ *
+ * If the file didn't exist, it will return false, which will make the
+ * standard HTTP DELETE handler kick in.
+ *
+ * @param string $tempLocation
+ * @return bool
+ */
+ public function httpDelete($tempLocation) {
+
+ if (!file_exists($tempLocation)) return true;
+
+ unlink($tempLocation);
+ $hR = $this->server->httpResponse;
+ $hR->setHeader('X-Sabre-Temp','true');
+ $hR->sendStatus(204);
+ return false;
+
+ }
+
+ /**
+ * This method handles the PROPFIND method.
+ *
+ * It's a very lazy method, it won't bother checking the request body
+ * for which properties were requested, and just sends back a default
+ * set of properties.
+ *
+ * @param string $tempLocation
+ * @param string $uri
+ * @return bool
+ */
+ public function httpPropfind($tempLocation, $uri) {
+
+ if (!file_exists($tempLocation)) return true;
+
+ $hR = $this->server->httpResponse;
+ $hR->setHeader('X-Sabre-Temp','true');
+ $hR->sendStatus(207);
+ $hR->setHeader('Content-Type','application/xml; charset=utf-8');
+
+ $this->server->parsePropFindRequest($this->server->httpRequest->getBody(true));
+
+ $properties = array(
+ 'href' => $uri,
+ 200 => array(
+ '{DAV:}getlastmodified' => new Property\GetLastModified(filemtime($tempLocation)),
+ '{DAV:}getcontentlength' => filesize($tempLocation),
+ '{DAV:}resourcetype' => new Property\ResourceType(null),
+ '{'.Server::NS_SABREDAV.'}tempFile' => true,
+
+ ),
+ );
+
+ $data = $this->server->generateMultiStatus(array($properties));
+ $hR->sendBody($data);
+ return false;
+
+ }
+
+
+ /**
+ * This method returns the directory where the temporary files should be stored.
+ *
+ * @return string
+ */
+ protected function getDataDir()
+ {
+ return $this->dataDir;
+ }
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Tree.php b/app/Gdoo/Calendar/Sabre/DAV/Tree.php
new file mode 100644
index 00000000..e097d3f8
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Tree.php
@@ -0,0 +1,193 @@
+getNodeForPath($path);
+ return true;
+
+ } catch (Exception\NotFound $e) {
+
+ return false;
+
+ }
+
+ }
+
+ /**
+ * Copies a file from path to another
+ *
+ * @param string $sourcePath The source location
+ * @param string $destinationPath The full destination path
+ * @return void
+ */
+ public function copy($sourcePath, $destinationPath) {
+
+ $sourceNode = $this->getNodeForPath($sourcePath);
+
+ // grab the dirname and basename components
+ list($destinationDir, $destinationName) = URLUtil::splitPath($destinationPath);
+
+ $destinationParent = $this->getNodeForPath($destinationDir);
+ $this->copyNode($sourceNode,$destinationParent,$destinationName);
+
+ $this->markDirty($destinationDir);
+
+ }
+
+ /**
+ * Moves a file from one location to another
+ *
+ * @param string $sourcePath The path to the file which should be moved
+ * @param string $destinationPath The full destination path, so not just the destination parent node
+ * @return int
+ */
+ public function move($sourcePath, $destinationPath) {
+
+ list($sourceDir, $sourceName) = URLUtil::splitPath($sourcePath);
+ list($destinationDir, $destinationName) = URLUtil::splitPath($destinationPath);
+
+ if ($sourceDir===$destinationDir) {
+ $renameable = $this->getNodeForPath($sourcePath);
+ $renameable->setName($destinationName);
+ } else {
+ $this->copy($sourcePath,$destinationPath);
+ $this->getNodeForPath($sourcePath)->delete();
+ }
+ $this->markDirty($sourceDir);
+ $this->markDirty($destinationDir);
+
+ }
+
+ /**
+ * Deletes a node from the tree
+ *
+ * @param string $path
+ * @return void
+ */
+ public function delete($path) {
+
+ $node = $this->getNodeForPath($path);
+ $node->delete();
+
+ list($parent) = URLUtil::splitPath($path);
+ $this->markDirty($parent);
+
+ }
+
+ /**
+ * Returns a list of childnodes for a given path.
+ *
+ * @param string $path
+ * @return array
+ */
+ public function getChildren($path) {
+
+ $node = $this->getNodeForPath($path);
+ return $node->getChildren();
+
+ }
+
+ /**
+ * This method is called with every tree update
+ *
+ * Examples of tree updates are:
+ * * node deletions
+ * * node creations
+ * * copy
+ * * move
+ * * renaming nodes
+ *
+ * If Tree classes implement a form of caching, this will allow
+ * them to make sure caches will be expired.
+ *
+ * If a path is passed, it is assumed that the entire subtree is dirty
+ *
+ * @param string $path
+ * @return void
+ */
+ public function markDirty($path) {
+
+
+ }
+
+ /**
+ * copyNode
+ *
+ * @param INode $source
+ * @param ICollection $destinationParent
+ * @param string $destinationName
+ * @return void
+ */
+ protected function copyNode(INode $source,ICollection $destinationParent,$destinationName = null) {
+
+ if (!$destinationName) $destinationName = $source->getName();
+
+ if ($source instanceof IFile) {
+
+ $data = $source->get();
+
+ // If the body was a string, we need to convert it to a stream
+ if (is_string($data)) {
+ $stream = fopen('php://temp','r+');
+ fwrite($stream,$data);
+ rewind($stream);
+ $data = $stream;
+ }
+ $destinationParent->createFile($destinationName,$data);
+ $destination = $destinationParent->getChild($destinationName);
+
+ } elseif ($source instanceof ICollection) {
+
+ $destinationParent->createDirectory($destinationName);
+
+ $destination = $destinationParent->getChild($destinationName);
+ foreach($source->getChildren() as $child) {
+
+ $this->copyNode($child,$destination);
+
+ }
+
+ }
+ if ($source instanceof IProperties && $destination instanceof IProperties) {
+
+ $props = $source->getProperties(array());
+ $destination->updateProperties($props);
+
+ }
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/Tree/Filesystem.php b/app/Gdoo/Calendar/Sabre/DAV/Tree/Filesystem.php
new file mode 100644
index 00000000..7e3f87dd
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/Tree/Filesystem.php
@@ -0,0 +1,133 @@
+basePath = $basePath;
+
+ }
+
+ /**
+ * Returns a new node for the given path
+ *
+ * @param string $path
+ * @return DAV\FS\Node
+ */
+ public function getNodeForPath($path) {
+
+ $realPath = $this->getRealPath($path);
+ if (!file_exists($realPath)) {
+ throw new DAV\Exception\NotFound('File at location ' . $realPath . ' not found');
+ }
+ if (is_dir($realPath)) {
+ return new DAV\FS\Directory($realPath);
+ } else {
+ return new DAV\FS\File($realPath);
+ }
+
+ }
+
+ /**
+ * Returns the real filesystem path for a webdav url.
+ *
+ * @param string $publicPath
+ * @return string
+ */
+ protected function getRealPath($publicPath) {
+
+ return rtrim($this->basePath,'/') . '/' . trim($publicPath,'/');
+
+ }
+
+ /**
+ * Copies a file or directory.
+ *
+ * This method must work recursively and delete the destination
+ * if it exists
+ *
+ * @param string $source
+ * @param string $destination
+ * @return void
+ */
+ public function copy($source,$destination) {
+
+ $source = $this->getRealPath($source);
+ $destination = $this->getRealPath($destination);
+ $this->realCopy($source,$destination);
+
+ }
+
+ /**
+ * Used by self::copy
+ *
+ * @param string $source
+ * @param string $destination
+ * @return void
+ */
+ protected function realCopy($source,$destination) {
+
+ if (is_file($source)) {
+ copy($source,$destination);
+ } else {
+ mkdir($destination);
+ foreach(scandir($source) as $subnode) {
+
+ if ($subnode=='.' || $subnode=='..') continue;
+ $this->realCopy($source.'/'.$subnode,$destination.'/'.$subnode);
+
+ }
+ }
+
+ }
+
+ /**
+ * Moves a file or directory recursively.
+ *
+ * If the destination exists, delete it first.
+ *
+ * @param string $source
+ * @param string $destination
+ * @return void
+ */
+ public function move($source,$destination) {
+
+ $source = $this->getRealPath($source);
+ $destination = $this->getRealPath($destination);
+ rename($source,$destination);
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAV/URLUtil.php b/app/Gdoo/Calendar/Sabre/DAV/URLUtil.php
new file mode 100644
index 00000000..739cf2c7
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAV/URLUtil.php
@@ -0,0 +1,124 @@
+
+ * will be returned as:
+ * {http://www.example.org}myelem
+ *
+ * This format is used throughout the SabreDAV sourcecode.
+ * Elements encoded with the urn:DAV namespace will
+ * be returned as if they were in the DAV: namespace. This is to avoid
+ * compatibility problems.
+ *
+ * This function will return null if a nodetype other than an Element is passed.
+ *
+ * @param \DOMNode $dom
+ * @return string
+ */
+ static function toClarkNotation(\DOMNode $dom) {
+
+ if ($dom->nodeType !== XML_ELEMENT_NODE) return null;
+
+ // Mapping back to the real namespace, in case it was dav
+ if ($dom->namespaceURI=='urn:DAV') $ns = 'DAV:'; else $ns = $dom->namespaceURI;
+
+ // Mapping to clark notation
+ return '{' . $ns . '}' . $dom->localName;
+
+ }
+
+ /**
+ * Parses a clark-notation string, and returns the namespace and element
+ * name components.
+ *
+ * If the string was invalid, it will throw an InvalidArgumentException.
+ *
+ * @param string $str
+ * @throws InvalidArgumentException
+ * @return array
+ */
+ static function parseClarkNotation($str) {
+
+ if (!preg_match('/^{([^}]*)}(.*)$/',$str,$matches)) {
+ throw new \InvalidArgumentException('\'' . $str . '\' is not a valid clark-notation formatted string');
+ }
+
+ return array(
+ $matches[1],
+ $matches[2]
+ );
+
+ }
+
+ /**
+ * This method takes an XML document (as string) and converts all instances of the
+ * DAV: namespace to urn:DAV
+ *
+ * This is unfortunately needed, because the DAV: namespace violates the xml namespaces
+ * spec, and causes the DOM to throw errors
+ *
+ * @param string $xmlDocument
+ * @return array|string|null
+ */
+ static function convertDAVNamespace($xmlDocument) {
+
+ // This is used to map the DAV: namespace to urn:DAV. This is needed, because the DAV:
+ // namespace is actually a violation of the XML namespaces specification, and will cause errors
+ return preg_replace("/xmlns(:[A-Za-z0-9_]*)?=(\"|\')DAV:(\\2)/","xmlns\\1=\\2urn:DAV\\2",$xmlDocument);
+
+ }
+
+ /**
+ * This method provides a generic way to load a DOMDocument for WebDAV use.
+ *
+ * This method throws a Sabre\DAV\Exception\BadRequest exception for any xml errors.
+ * It does not preserve whitespace, and it converts the DAV: namespace to urn:DAV.
+ *
+ * @param string $xml
+ * @throws Sabre\DAV\Exception\BadRequest
+ * @return DOMDocument
+ */
+ static function loadDOMDocument($xml) {
+
+ if (empty($xml))
+ throw new Exception\BadRequest('Empty XML document sent');
+
+ // The BitKinex client sends xml documents as UTF-16. PHP 5.3.1 (and presumably lower)
+ // does not support this, so we must intercept this and convert to UTF-8.
+ if (substr($xml,0,12) === "\x3c\x00\x3f\x00\x78\x00\x6d\x00\x6c\x00\x20\x00") {
+
+ // Note: the preceeding byte sequence is "]*)encoding="UTF-16"([^>]*)>|u','',$xml);
+
+ }
+
+ // Retaining old error setting
+ $oldErrorSetting = libxml_use_internal_errors(true);
+ // Fixes an XXE vulnerability on PHP versions older than 5.3.23 or
+ // 5.4.13.
+ $oldEntityLoaderSetting = libxml_disable_entity_loader(true);
+
+ // Clearing any previous errors
+ libxml_clear_errors();
+
+ $dom = new \DOMDocument();
+
+ // We don't generally care about any whitespace
+ $dom->preserveWhiteSpace = false;
+
+ $dom->loadXML(self::convertDAVNamespace($xml),LIBXML_NOWARNING | LIBXML_NOERROR);
+
+ if ($error = libxml_get_last_error()) {
+ libxml_clear_errors();
+ throw new Exception\BadRequest('The request body had an invalid XML body. (message: ' . $error->message . ', errorcode: ' . $error->code . ', line: ' . $error->line . ')');
+ }
+
+ // Restoring old mechanism for error handling
+ if ($oldErrorSetting===false) libxml_use_internal_errors(false);
+ if ($oldEntityLoaderSetting===false) libxml_disable_entity_loader(false);
+
+ return $dom;
+
+ }
+
+ /**
+ * Parses all WebDAV properties out of a DOM Element
+ *
+ * Generally WebDAV properties are enclosed in {DAV:}prop elements. This
+ * method helps by going through all these and pulling out the actual
+ * propertynames, making them array keys and making the property values,
+ * well.. the array values.
+ *
+ * If no value was given (self-closing element) null will be used as the
+ * value. This is used in for example PROPFIND requests.
+ *
+ * Complex values are supported through the propertyMap argument. The
+ * propertyMap should have the clark-notation properties as it's keys, and
+ * classnames as values.
+ *
+ * When any of these properties are found, the unserialize() method will be
+ * (statically) called. The result of this method is used as the value.
+ *
+ * @param \DOMElement $parentNode
+ * @param array $propertyMap
+ * @return array
+ */
+ static function parseProperties(\DOMElement $parentNode, array $propertyMap = array()) {
+
+ $propList = array();
+ foreach($parentNode->childNodes as $propNode) {
+
+ if (self::toClarkNotation($propNode)!=='{DAV:}prop') continue;
+
+ foreach($propNode->childNodes as $propNodeData) {
+
+ /* If there are no elements in here, we actually get 1 text node, this special case is dedicated to netdrive */
+ if ($propNodeData->nodeType != XML_ELEMENT_NODE) continue;
+
+ $propertyName = self::toClarkNotation($propNodeData);
+ if (isset($propertyMap[$propertyName])) {
+ $propList[$propertyName] = call_user_func(array($propertyMap[$propertyName],'unserialize'),$propNodeData);
+ } else {
+ $propList[$propertyName] = $propNodeData->textContent;
+ }
+ }
+
+
+ }
+ return $propList;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAVACL/AbstractPrincipalCollection.php b/app/Gdoo/Calendar/Sabre/DAVACL/AbstractPrincipalCollection.php
new file mode 100644
index 00000000..5903889c
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAVACL/AbstractPrincipalCollection.php
@@ -0,0 +1,155 @@
+principalPrefix = $principalPrefix;
+ $this->principalBackend = $principalBackend;
+
+ }
+
+ /**
+ * This method returns a node for a principal.
+ *
+ * The passed array contains principal information, and is guaranteed to
+ * at least contain a uri item. Other properties may or may not be
+ * supplied by the authentication backend.
+ *
+ * @param array $principalInfo
+ * @return IPrincipal
+ */
+ abstract function getChildForPrincipal(array $principalInfo);
+
+ /**
+ * Returns the name of this collection.
+ *
+ * @return string
+ */
+ public function getName() {
+
+ list(,$name) = DAV\URLUtil::splitPath($this->principalPrefix);
+ return $name;
+
+ }
+
+ /**
+ * Return the list of users
+ *
+ * @return array
+ */
+ public function getChildren() {
+
+ if ($this->disableListing)
+ throw new DAV\Exception\MethodNotAllowed('Listing members of this collection is disabled');
+
+ $children = array();
+ foreach($this->principalBackend->getPrincipalsByPrefix($this->principalPrefix) as $principalInfo) {
+
+ $children[] = $this->getChildForPrincipal($principalInfo);
+
+
+ }
+ return $children;
+
+ }
+
+ /**
+ * Returns a child object, by its name.
+ *
+ * @param string $name
+ * @throws DAV\Exception\NotFound
+ * @return IPrincipal
+ */
+ public function getChild($name) {
+
+ $principalInfo = $this->principalBackend->getPrincipalByPath($this->principalPrefix . '/' . $name);
+ if (!$principalInfo) throw new DAV\Exception\NotFound('Principal with name ' . $name . ' not found');
+ return $this->getChildForPrincipal($principalInfo);
+
+ }
+
+ /**
+ * This method is used to search for principals matching a set of
+ * properties.
+ *
+ * This search is specifically used by RFC3744's principal-property-search
+ * REPORT. You should at least allow searching on
+ * http://sabredav.org/ns}email-address.
+ *
+ * The actual search should be a unicode-non-case-sensitive search. The
+ * keys in searchProperties are the WebDAV property names, while the values
+ * are the property values to search on.
+ *
+ * If multiple properties are being searched on, the search should be
+ * AND'ed.
+ *
+ * This method should simply return a list of 'child names', which may be
+ * used to call $this->getChild in the future.
+ *
+ * @param array $searchProperties
+ * @return array
+ */
+ public function searchPrincipals(array $searchProperties) {
+
+ $result = $this->principalBackend->searchPrincipals($this->principalPrefix, $searchProperties);
+ $r = array();
+
+ foreach($result as $row) {
+ list(, $r[]) = DAV\URLUtil::splitPath($row);
+ }
+
+ return $r;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAVACL/Exception/AceConflict.php b/app/Gdoo/Calendar/Sabre/DAVACL/Exception/AceConflict.php
new file mode 100644
index 00000000..ee18adea
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAVACL/Exception/AceConflict.php
@@ -0,0 +1,35 @@
+ownerDocument;
+
+ $np = $doc->createElementNS('DAV:','d:no-ace-conflict');
+ $errorNode->appendChild($np);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAVACL/Exception/NeedPrivileges.php b/app/Gdoo/Calendar/Sabre/DAVACL/Exception/NeedPrivileges.php
new file mode 100644
index 00000000..7d9c8056
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAVACL/Exception/NeedPrivileges.php
@@ -0,0 +1,83 @@
+uri = $uri;
+ $this->privileges = $privileges;
+
+ parent::__construct('User did not have the required privileges (' . implode(',', $privileges) . ') for path "' . $uri . '"');
+
+ }
+
+ /**
+ * Adds in extra information in the xml response.
+ *
+ * This method adds the {DAV:}need-privileges element as defined in rfc3744
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $errorNode
+ * @return void
+ */
+ public function serialize(DAV\Server $server,\DOMElement $errorNode) {
+
+ $doc = $errorNode->ownerDocument;
+
+ $np = $doc->createElementNS('DAV:','d:need-privileges');
+ $errorNode->appendChild($np);
+
+ foreach($this->privileges as $privilege) {
+
+ $resource = $doc->createElementNS('DAV:','d:resource');
+ $np->appendChild($resource);
+
+ $resource->appendChild($doc->createElementNS('DAV:','d:href',$server->getBaseUri() . $this->uri));
+
+ $priv = $doc->createElementNS('DAV:','d:privilege');
+ $resource->appendChild($priv);
+
+ preg_match('/^{([^}]*)}(.*)$/',$privilege,$privilegeParts);
+ $priv->appendChild($doc->createElementNS($privilegeParts[1],'d:' . $privilegeParts[2]));
+
+
+ }
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/DAVACL/Exception/NoAbstract.php b/app/Gdoo/Calendar/Sabre/DAVACL/Exception/NoAbstract.php
new file mode 100644
index 00000000..4e1899aa
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAVACL/Exception/NoAbstract.php
@@ -0,0 +1,35 @@
+ownerDocument;
+
+ $np = $doc->createElementNS('DAV:','d:no-abstract');
+ $errorNode->appendChild($np);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAVACL/Exception/NotRecognizedPrincipal.php b/app/Gdoo/Calendar/Sabre/DAVACL/Exception/NotRecognizedPrincipal.php
new file mode 100644
index 00000000..d23fdc06
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAVACL/Exception/NotRecognizedPrincipal.php
@@ -0,0 +1,35 @@
+ownerDocument;
+
+ $np = $doc->createElementNS('DAV:','d:recognized-principal');
+ $errorNode->appendChild($np);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAVACL/Exception/NotSupportedPrivilege.php b/app/Gdoo/Calendar/Sabre/DAVACL/Exception/NotSupportedPrivilege.php
new file mode 100644
index 00000000..dd4cb00a
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAVACL/Exception/NotSupportedPrivilege.php
@@ -0,0 +1,35 @@
+ownerDocument;
+
+ $np = $doc->createElementNS('DAV:','d:not-supported-privilege');
+ $errorNode->appendChild($np);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAVACL/IACL.php b/app/Gdoo/Calendar/Sabre/DAVACL/IACL.php
new file mode 100644
index 00000000..92970c72
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAVACL/IACL.php
@@ -0,0 +1,74 @@
+getChild in the future.
+ *
+ * @param array $searchProperties
+ * @return array
+ */
+ function searchPrincipals(array $searchProperties);
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAVACL/Plugin.php b/app/Gdoo/Calendar/Sabre/DAVACL/Plugin.php
new file mode 100644
index 00000000..572a9d41
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAVACL/Plugin.php
@@ -0,0 +1,1402 @@
+ 'Display name',
+ '{http://sabredav.org/ns}email-address' => 'Email address',
+ );
+
+ /**
+ * Any principal uri's added here, will automatically be added to the list
+ * of ACL's. They will effectively receive {DAV:}all privileges, as a
+ * protected privilege.
+ *
+ * @var array
+ */
+ public $adminPrincipals = array();
+
+ /**
+ * Returns a list of features added by this plugin.
+ *
+ * This list is used in the response of a HTTP OPTIONS request.
+ *
+ * @return array
+ */
+ public function getFeatures() {
+
+ return array('access-control', 'calendarserver-principal-property-search');
+
+ }
+
+ /**
+ * Returns a list of available methods for a given url
+ *
+ * @param string $uri
+ * @return array
+ */
+ public function getMethods($uri) {
+
+ return array('ACL');
+
+ }
+
+ /**
+ * Returns a plugin name.
+ *
+ * Using this name other plugins will be able to access other plugins
+ * using Sabre\DAV\Server::getPlugin
+ *
+ * @return string
+ */
+ public function getPluginName() {
+
+ return 'acl';
+
+ }
+
+ /**
+ * Returns a list of reports this plugin supports.
+ *
+ * This will be used in the {DAV:}supported-report-set property.
+ * Note that you still need to subscribe to the 'report' event to actually
+ * implement them
+ *
+ * @param string $uri
+ * @return array
+ */
+ public function getSupportedReportSet($uri) {
+
+ return array(
+ '{DAV:}expand-property',
+ '{DAV:}principal-property-search',
+ '{DAV:}principal-search-property-set',
+ );
+
+ }
+
+
+ /**
+ * Checks if the current user has the specified privilege(s).
+ *
+ * You can specify a single privilege, or a list of privileges.
+ * This method will throw an exception if the privilege is not available
+ * and return true otherwise.
+ *
+ * @param string $uri
+ * @param array|string $privileges
+ * @param int $recursion
+ * @param bool $throwExceptions if set to false, this method won't throw exceptions.
+ * @throws Sabre\DAVACL\Exception\NeedPrivileges
+ * @return bool
+ */
+ public function checkPrivileges($uri, $privileges, $recursion = self::R_PARENT, $throwExceptions = true) {
+
+ if (!is_array($privileges)) $privileges = array($privileges);
+
+ $acl = $this->getCurrentUserPrivilegeSet($uri);
+
+ if (is_null($acl)) {
+ if ($this->allowAccessToNodesWithoutACL) {
+ return true;
+ } else {
+ if ($throwExceptions)
+ throw new Exception\NeedPrivileges($uri,$privileges);
+ else
+ return false;
+
+ }
+ }
+
+ $failed = array();
+ foreach($privileges as $priv) {
+
+ if (!in_array($priv, $acl)) {
+ $failed[] = $priv;
+ }
+
+ }
+
+ if ($failed) {
+ if ($throwExceptions)
+ throw new Exception\NeedPrivileges($uri,$failed);
+ else
+ return false;
+ }
+ return true;
+
+ }
+
+ /**
+ * Returns the standard users' principal.
+ *
+ * This is one authorative principal url for the current user.
+ * This method will return null if the user wasn't logged in.
+ *
+ * @return string|null
+ */
+ public function getCurrentUserPrincipal() {
+
+ $authPlugin = $this->server->getPlugin('auth');
+ if (is_null($authPlugin)) return null;
+ /** @var $authPlugin Sabre\DAV\Auth\Plugin */
+
+ $userName = $authPlugin->getCurrentUser();
+ if (!$userName) return null;
+
+ return $this->defaultUsernamePath . '/' . $userName;
+
+ }
+
+
+ /**
+ * Returns a list of principals that's associated to the current
+ * user, either directly or through group membership.
+ *
+ * @return array
+ */
+ public function getCurrentUserPrincipals() {
+
+ $currentUser = $this->getCurrentUserPrincipal();
+
+ if (is_null($currentUser)) return array();
+
+ return array_merge(
+ array($currentUser),
+ $this->getPrincipalMembership($currentUser)
+ );
+
+ }
+
+ /**
+ * This array holds a cache for all the principals that are associated with
+ * a single principal.
+ *
+ * @var array
+ */
+ protected $principalMembershipCache = array();
+
+
+ /**
+ * Returns all the principal groups the specified principal is a member of.
+ *
+ * @param string $principal
+ * @return array
+ */
+ public function getPrincipalMembership($mainPrincipal) {
+
+ // First check our cache
+ if (isset($this->principalMembershipCache[$mainPrincipal])) {
+ return $this->principalMembershipCache[$mainPrincipal];
+ }
+
+ $check = array($mainPrincipal);
+ $principals = array();
+
+ while(count($check)) {
+
+ $principal = array_shift($check);
+
+ $node = $this->server->tree->getNodeForPath($principal);
+ if ($node instanceof IPrincipal) {
+ foreach($node->getGroupMembership() as $groupMember) {
+
+ if (!in_array($groupMember, $principals)) {
+
+ $check[] = $groupMember;
+ $principals[] = $groupMember;
+
+ }
+
+ }
+
+ }
+
+ }
+
+ // Store the result in the cache
+ $this->principalMembershipCache[$mainPrincipal] = $principals;
+
+ return $principals;
+
+ }
+
+ /**
+ * Returns the supported privilege structure for this ACL plugin.
+ *
+ * See RFC3744 for more details. Currently we default on a simple,
+ * standard structure.
+ *
+ * You can either get the list of privileges by a uri (path) or by
+ * specifying a Node.
+ *
+ * @param string|DAV\INode $node
+ * @return array
+ */
+ public function getSupportedPrivilegeSet($node) {
+
+ if (is_string($node)) {
+ $node = $this->server->tree->getNodeForPath($node);
+ }
+
+ if ($node instanceof IACL) {
+ $result = $node->getSupportedPrivilegeSet();
+
+ if ($result)
+ return $result;
+ }
+
+ return self::getDefaultSupportedPrivilegeSet();
+
+ }
+
+ /**
+ * Returns a fairly standard set of privileges, which may be useful for
+ * other systems to use as a basis.
+ *
+ * @return array
+ */
+ static function getDefaultSupportedPrivilegeSet() {
+
+ return array(
+ 'privilege' => '{DAV:}all',
+ 'abstract' => true,
+ 'aggregates' => array(
+ array(
+ 'privilege' => '{DAV:}read',
+ 'aggregates' => array(
+ array(
+ 'privilege' => '{DAV:}read-acl',
+ 'abstract' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}read-current-user-privilege-set',
+ 'abstract' => true,
+ ),
+ ),
+ ), // {DAV:}read
+ array(
+ 'privilege' => '{DAV:}write',
+ 'aggregates' => array(
+ array(
+ 'privilege' => '{DAV:}write-acl',
+ 'abstract' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}write-properties',
+ 'abstract' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}write-content',
+ 'abstract' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}bind',
+ 'abstract' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}unbind',
+ 'abstract' => true,
+ ),
+ array(
+ 'privilege' => '{DAV:}unlock',
+ 'abstract' => true,
+ ),
+ ),
+ ), // {DAV:}write
+ ),
+ ); // {DAV:}all
+
+ }
+
+ /**
+ * Returns the supported privilege set as a flat list
+ *
+ * This is much easier to parse.
+ *
+ * The returned list will be index by privilege name.
+ * The value is a struct containing the following properties:
+ * - aggregates
+ * - abstract
+ * - concrete
+ *
+ * @param string|DAV\INode $node
+ * @return array
+ */
+ final public function getFlatPrivilegeSet($node) {
+
+ $privs = $this->getSupportedPrivilegeSet($node);
+
+ $flat = array();
+ $this->getFPSTraverse($privs, null, $flat);
+
+ return $flat;
+
+ }
+
+ /**
+ * Traverses the privilege set tree for reordering
+ *
+ * This function is solely used by getFlatPrivilegeSet, and would have been
+ * a closure if it wasn't for the fact I need to support PHP 5.2.
+ *
+ * @param array $priv
+ * @param $concrete
+ * @param array $flat
+ * @return void
+ */
+ final private function getFPSTraverse($priv, $concrete, &$flat) {
+
+ $myPriv = array(
+ 'privilege' => $priv['privilege'],
+ 'abstract' => isset($priv['abstract']) && $priv['abstract'],
+ 'aggregates' => array(),
+ 'concrete' => isset($priv['abstract']) && $priv['abstract']?$concrete:$priv['privilege'],
+ );
+
+ if (isset($priv['aggregates']))
+ foreach($priv['aggregates'] as $subPriv) $myPriv['aggregates'][] = $subPriv['privilege'];
+
+ $flat[$priv['privilege']] = $myPriv;
+
+ if (isset($priv['aggregates'])) {
+
+ foreach($priv['aggregates'] as $subPriv) {
+
+ $this->getFPSTraverse($subPriv, $myPriv['concrete'], $flat);
+
+ }
+
+ }
+
+ }
+
+ /**
+ * Returns the full ACL list.
+ *
+ * Either a uri or a DAV\INode may be passed.
+ *
+ * null will be returned if the node doesn't support ACLs.
+ *
+ * @param string|DAV\INode $node
+ * @return array
+ */
+ public function getACL($node) {
+
+ if (is_string($node)) {
+ $node = $this->server->tree->getNodeForPath($node);
+ }
+ if (!$node instanceof IACL) {
+ return null;
+ }
+ $acl = $node->getACL();
+ foreach($this->adminPrincipals as $adminPrincipal) {
+ $acl[] = array(
+ 'principal' => $adminPrincipal,
+ 'privilege' => '{DAV:}all',
+ 'protected' => true,
+ );
+ }
+ return $acl;
+
+ }
+
+ /**
+ * Returns a list of privileges the current user has
+ * on a particular node.
+ *
+ * Either a uri or a DAV\INode may be passed.
+ *
+ * null will be returned if the node doesn't support ACLs.
+ *
+ * @param string|DAV\INode $node
+ * @return array
+ */
+ public function getCurrentUserPrivilegeSet($node) {
+
+ if (is_string($node)) {
+ $node = $this->server->tree->getNodeForPath($node);
+ }
+
+ $acl = $this->getACL($node);
+
+ if (is_null($acl)) return null;
+
+ $principals = $this->getCurrentUserPrincipals();
+
+ $collected = array();
+
+ foreach($acl as $ace) {
+
+ $principal = $ace['principal'];
+
+ switch($principal) {
+
+ case '{DAV:}owner' :
+ $owner = $node->getOwner();
+ if ($owner && in_array($owner, $principals)) {
+ $collected[] = $ace;
+ }
+ break;
+
+
+ // 'all' matches for every user
+ case '{DAV:}all' :
+
+ // 'authenticated' matched for every user that's logged in.
+ // Since it's not possible to use ACL while not being logged
+ // in, this is also always true.
+ case '{DAV:}authenticated' :
+ $collected[] = $ace;
+ break;
+
+ // 'unauthenticated' can never occur either, so we simply
+ // ignore these.
+ case '{DAV:}unauthenticated' :
+ break;
+
+ default :
+ if (in_array($ace['principal'], $principals)) {
+ $collected[] = $ace;
+ }
+ break;
+
+ }
+
+
+ }
+
+ // Now we deduct all aggregated privileges.
+ $flat = $this->getFlatPrivilegeSet($node);
+
+ $collected2 = array();
+ while(count($collected)) {
+
+ $current = array_pop($collected);
+ $collected2[] = $current['privilege'];
+
+ foreach($flat[$current['privilege']]['aggregates'] as $subPriv) {
+ $collected2[] = $subPriv;
+ $collected[] = $flat[$subPriv];
+ }
+
+ }
+
+ return array_values(array_unique($collected2));
+
+ }
+
+ /**
+ * Principal property search
+ *
+ * This method can search for principals matching certain values in
+ * properties.
+ *
+ * This method will return a list of properties for the matched properties.
+ *
+ * @param array $searchProperties The properties to search on. This is a
+ * key-value list. The keys are property
+ * names, and the values the strings to
+ * match them on.
+ * @param array $requestedProperties This is the list of properties to
+ * return for every match.
+ * @param string $collectionUri The principal collection to search on.
+ * If this is ommitted, the standard
+ * principal collection-set will be used.
+ * @return array This method returns an array structure similar to
+ * Sabre\DAV\Server::getPropertiesForPath. Returned
+ * properties are index by a HTTP status code.
+ *
+ */
+ public function principalSearch(array $searchProperties, array $requestedProperties, $collectionUri = null) {
+
+ if (!is_null($collectionUri)) {
+ $uris = array($collectionUri);
+ } else {
+ $uris = $this->principalCollectionSet;
+ }
+
+ $lookupResults = array();
+ foreach($uris as $uri) {
+
+ $principalCollection = $this->server->tree->getNodeForPath($uri);
+ if (!$principalCollection instanceof IPrincipalCollection) {
+ // Not a principal collection, we're simply going to ignore
+ // this.
+ continue;
+ }
+
+ $results = $principalCollection->searchPrincipals($searchProperties);
+ foreach($results as $result) {
+ $lookupResults[] = rtrim($uri,'/') . '/' . $result;
+ }
+
+ }
+
+ $matches = array();
+
+ foreach($lookupResults as $lookupResult) {
+
+ list($matches[]) = $this->server->getPropertiesForPath($lookupResult, $requestedProperties, 0);
+
+ }
+
+ return $matches;
+
+ }
+
+ /**
+ * Sets up the plugin
+ *
+ * This method is automatically called by the server class.
+ *
+ * @param DAV\Server $server
+ * @return void
+ */
+ public function initialize(DAV\Server $server) {
+
+ $this->server = $server;
+ $server->subscribeEvent('beforeGetProperties',array($this,'beforeGetProperties'));
+
+ $server->subscribeEvent('beforeMethod', array($this,'beforeMethod'),20);
+ $server->subscribeEvent('beforeBind', array($this,'beforeBind'),20);
+ $server->subscribeEvent('beforeUnbind', array($this,'beforeUnbind'),20);
+ $server->subscribeEvent('updateProperties',array($this,'updateProperties'));
+ $server->subscribeEvent('beforeUnlock', array($this,'beforeUnlock'),20);
+ $server->subscribeEvent('report',array($this,'report'));
+ $server->subscribeEvent('unknownMethod', array($this, 'unknownMethod'));
+
+ array_push($server->protectedProperties,
+ '{DAV:}alternate-URI-set',
+ '{DAV:}principal-URL',
+ '{DAV:}group-membership',
+ '{DAV:}principal-collection-set',
+ '{DAV:}current-user-principal',
+ '{DAV:}supported-privilege-set',
+ '{DAV:}current-user-privilege-set',
+ '{DAV:}acl',
+ '{DAV:}acl-restrictions',
+ '{DAV:}inherited-acl-set',
+ '{DAV:}owner',
+ '{DAV:}group'
+ );
+
+ // Automatically mapping nodes implementing IPrincipal to the
+ // {DAV:}principal resourcetype.
+ $server->resourceTypeMapping['Sabre\\DAVACL\\IPrincipal'] = '{DAV:}principal';
+
+ // Mapping the group-member-set property to the HrefList property
+ // class.
+ $server->propertyMap['{DAV:}group-member-set'] = 'Sabre\\DAV\\Property\\HrefList';
+
+ }
+
+
+ /* {{{ Event handlers */
+
+ /**
+ * Triggered before any method is handled
+ *
+ * @param string $method
+ * @param string $uri
+ * @return void
+ */
+ public function beforeMethod($method, $uri) {
+
+ $exists = $this->server->tree->nodeExists($uri);
+
+ // If the node doesn't exists, none of these checks apply
+ if (!$exists) return;
+
+ switch($method) {
+
+ case 'GET' :
+ case 'HEAD' :
+ case 'OPTIONS' :
+ // For these 3 we only need to know if the node is readable.
+ $this->checkPrivileges($uri,'{DAV:}read');
+ break;
+
+ case 'PUT' :
+ case 'LOCK' :
+ case 'UNLOCK' :
+ // This method requires the write-content priv if the node
+ // already exists, and bind on the parent if the node is being
+ // created.
+ // The bind privilege is handled in the beforeBind event.
+ $this->checkPrivileges($uri,'{DAV:}write-content');
+ break;
+
+
+ case 'PROPPATCH' :
+ $this->checkPrivileges($uri,'{DAV:}write-properties');
+ break;
+
+ case 'ACL' :
+ $this->checkPrivileges($uri,'{DAV:}write-acl');
+ break;
+
+ case 'COPY' :
+ case 'MOVE' :
+ // Copy requires read privileges on the entire source tree.
+ // If the target exists write-content normally needs to be
+ // checked, however, we're deleting the node beforehand and
+ // creating a new one after, so this is handled by the
+ // beforeUnbind event.
+ //
+ // The creation of the new node is handled by the beforeBind
+ // event.
+ //
+ // If MOVE is used beforeUnbind will also be used to check if
+ // the sourcenode can be deleted.
+ $this->checkPrivileges($uri,'{DAV:}read',self::R_RECURSIVE);
+
+ break;
+
+ }
+
+ }
+
+ /**
+ * Triggered before a new node is created.
+ *
+ * This allows us to check permissions for any operation that creates a
+ * new node, such as PUT, MKCOL, MKCALENDAR, LOCK, COPY and MOVE.
+ *
+ * @param string $uri
+ * @return void
+ */
+ public function beforeBind($uri) {
+
+ list($parentUri,$nodeName) = DAV\URLUtil::splitPath($uri);
+ $this->checkPrivileges($parentUri,'{DAV:}bind');
+
+ }
+
+ /**
+ * Triggered before a node is deleted
+ *
+ * This allows us to check permissions for any operation that will delete
+ * an existing node.
+ *
+ * @param string $uri
+ * @return void
+ */
+ public function beforeUnbind($uri) {
+
+ list($parentUri,$nodeName) = DAV\URLUtil::splitPath($uri);
+ $this->checkPrivileges($parentUri,'{DAV:}unbind',self::R_RECURSIVEPARENTS);
+
+ }
+
+ /**
+ * Triggered before a node is unlocked.
+ *
+ * @param string $uri
+ * @param DAV\Locks\LockInfo $lock
+ * @TODO: not yet implemented
+ * @return void
+ */
+ public function beforeUnlock($uri, DAV\Locks\LockInfo $lock) {
+
+
+ }
+
+ /**
+ * Triggered before properties are looked up in specific nodes.
+ *
+ * @param string $uri
+ * @param DAV\INode $node
+ * @param array $requestedProperties
+ * @param array $returnedProperties
+ * @TODO really should be broken into multiple methods, or even a class.
+ * @return bool
+ */
+ public function beforeGetProperties($uri, DAV\INode $node, &$requestedProperties, &$returnedProperties) {
+
+ // Checking the read permission
+ if (!$this->checkPrivileges($uri,'{DAV:}read',self::R_PARENT,false)) {
+
+ // User is not allowed to read properties
+ if ($this->hideNodesFromListings) {
+ return false;
+ }
+
+ // Marking all requested properties as '403'.
+ foreach($requestedProperties as $key=>$requestedProperty) {
+ unset($requestedProperties[$key]);
+ $returnedProperties[403][$requestedProperty] = null;
+ }
+ return;
+
+ }
+
+ /* Adding principal properties */
+ if ($node instanceof IPrincipal) {
+
+ if (false !== ($index = array_search('{DAV:}alternate-URI-set', $requestedProperties))) {
+
+ unset($requestedProperties[$index]);
+ $returnedProperties[200]['{DAV:}alternate-URI-set'] = new DAV\Property\HrefList($node->getAlternateUriSet());
+
+ }
+ if (false !== ($index = array_search('{DAV:}principal-URL', $requestedProperties))) {
+
+ unset($requestedProperties[$index]);
+ $returnedProperties[200]['{DAV:}principal-URL'] = new DAV\Property\Href($node->getPrincipalUrl() . '/');
+
+ }
+ if (false !== ($index = array_search('{DAV:}group-member-set', $requestedProperties))) {
+
+ unset($requestedProperties[$index]);
+ $returnedProperties[200]['{DAV:}group-member-set'] = new DAV\Property\HrefList($node->getGroupMemberSet());
+
+ }
+ if (false !== ($index = array_search('{DAV:}group-membership', $requestedProperties))) {
+
+ unset($requestedProperties[$index]);
+ $returnedProperties[200]['{DAV:}group-membership'] = new DAV\Property\HrefList($node->getGroupMembership());
+
+ }
+
+ if (false !== ($index = array_search('{DAV:}displayname', $requestedProperties))) {
+
+ $returnedProperties[200]['{DAV:}displayname'] = $node->getDisplayName();
+
+ }
+
+ }
+ if (false !== ($index = array_search('{DAV:}principal-collection-set', $requestedProperties))) {
+
+ unset($requestedProperties[$index]);
+ $val = $this->principalCollectionSet;
+ // Ensuring all collections end with a slash
+ foreach($val as $k=>$v) $val[$k] = $v . '/';
+ $returnedProperties[200]['{DAV:}principal-collection-set'] = new DAV\Property\HrefList($val);
+
+ }
+ if (false !== ($index = array_search('{DAV:}current-user-principal', $requestedProperties))) {
+
+ unset($requestedProperties[$index]);
+ if ($url = $this->getCurrentUserPrincipal()) {
+ $returnedProperties[200]['{DAV:}current-user-principal'] = new Property\Principal(Property\Principal::HREF, $url . '/');
+ } else {
+ $returnedProperties[200]['{DAV:}current-user-principal'] = new Property\Principal(Property\Principal::UNAUTHENTICATED);
+ }
+
+ }
+ if (false !== ($index = array_search('{DAV:}supported-privilege-set', $requestedProperties))) {
+
+ unset($requestedProperties[$index]);
+ $returnedProperties[200]['{DAV:}supported-privilege-set'] = new Property\SupportedPrivilegeSet($this->getSupportedPrivilegeSet($node));
+
+ }
+ if (false !== ($index = array_search('{DAV:}current-user-privilege-set', $requestedProperties))) {
+
+ if (!$this->checkPrivileges($uri, '{DAV:}read-current-user-privilege-set', self::R_PARENT, false)) {
+ $returnedProperties[403]['{DAV:}current-user-privilege-set'] = null;
+ unset($requestedProperties[$index]);
+ } else {
+ $val = $this->getCurrentUserPrivilegeSet($node);
+ if (!is_null($val)) {
+ unset($requestedProperties[$index]);
+ $returnedProperties[200]['{DAV:}current-user-privilege-set'] = new Property\CurrentUserPrivilegeSet($val);
+ }
+ }
+
+ }
+
+ /* The ACL property contains all the permissions */
+ if (false !== ($index = array_search('{DAV:}acl', $requestedProperties))) {
+
+ if (!$this->checkPrivileges($uri, '{DAV:}read-acl', self::R_PARENT, false)) {
+
+ unset($requestedProperties[$index]);
+ $returnedProperties[403]['{DAV:}acl'] = null;
+
+ } else {
+
+ $acl = $this->getACL($node);
+ if (!is_null($acl)) {
+ unset($requestedProperties[$index]);
+ $returnedProperties[200]['{DAV:}acl'] = new Property\Acl($this->getACL($node));
+ }
+
+ }
+
+ }
+
+ /* The acl-restrictions property contains information on how privileges
+ * must behave.
+ */
+ if (false !== ($index = array_search('{DAV:}acl-restrictions', $requestedProperties))) {
+ unset($requestedProperties[$index]);
+ $returnedProperties[200]['{DAV:}acl-restrictions'] = new Property\AclRestrictions();
+ }
+
+ /* Adding ACL properties */
+ if ($node instanceof IACL) {
+
+ if (false !== ($index = array_search('{DAV:}owner', $requestedProperties))) {
+
+ unset($requestedProperties[$index]);
+ $returnedProperties[200]['{DAV:}owner'] = new DAV\Property\Href($node->getOwner() . '/');
+
+ }
+
+ }
+
+ }
+
+ /**
+ * This method intercepts PROPPATCH methods and make sure the
+ * group-member-set is updated correctly.
+ *
+ * @param array $propertyDelta
+ * @param array $result
+ * @param DAV\INode $node
+ * @return bool
+ */
+ public function updateProperties(&$propertyDelta, &$result, DAV\INode $node) {
+
+ if (!array_key_exists('{DAV:}group-member-set', $propertyDelta))
+ return;
+
+ if (is_null($propertyDelta['{DAV:}group-member-set'])) {
+ $memberSet = array();
+ } elseif ($propertyDelta['{DAV:}group-member-set'] instanceof DAV\Property\HrefList) {
+ $memberSet = array_map(
+ array($this->server,'calculateUri'),
+ $propertyDelta['{DAV:}group-member-set']->getHrefs()
+ );
+ } else {
+ throw new DAV\Exception('The group-member-set property MUST be an instance of Sabre\DAV\Property\HrefList or null');
+ }
+
+ if (!($node instanceof IPrincipal)) {
+ $result[403]['{DAV:}group-member-set'] = null;
+ unset($propertyDelta['{DAV:}group-member-set']);
+
+ // Returning false will stop the updateProperties process
+ return false;
+ }
+
+ $node->setGroupMemberSet($memberSet);
+ // We must also clear our cache, just in case
+
+ $this->principalMembershipCache = array();
+
+ $result[200]['{DAV:}group-member-set'] = null;
+ unset($propertyDelta['{DAV:}group-member-set']);
+
+ }
+
+ /**
+ * This method handles HTTP REPORT requests
+ *
+ * @param string $reportName
+ * @param \DOMNode $dom
+ * @return bool
+ */
+ public function report($reportName, $dom) {
+
+ switch($reportName) {
+
+ case '{DAV:}principal-property-search' :
+ $this->principalPropertySearchReport($dom);
+ return false;
+ case '{DAV:}principal-search-property-set' :
+ $this->principalSearchPropertySetReport($dom);
+ return false;
+ case '{DAV:}expand-property' :
+ $this->expandPropertyReport($dom);
+ return false;
+
+ }
+
+ }
+
+ /**
+ * This event is triggered for any HTTP method that is not known by the
+ * webserver.
+ *
+ * @param string $method
+ * @param string $uri
+ * @return bool
+ */
+ public function unknownMethod($method, $uri) {
+
+ if ($method!=='ACL') return;
+
+ $this->httpACL($uri);
+ return false;
+
+ }
+
+ /**
+ * This method is responsible for handling the 'ACL' event.
+ *
+ * @param string $uri
+ * @return void
+ */
+ public function httpACL($uri) {
+
+ $body = $this->server->httpRequest->getBody(true);
+ $dom = DAV\XMLUtil::loadDOMDocument($body);
+
+ $newAcl =
+ Property\Acl::unserialize($dom->firstChild)
+ ->getPrivileges();
+
+ // Normalizing urls
+ foreach($newAcl as $k=>$newAce) {
+ $newAcl[$k]['principal'] = $this->server->calculateUri($newAce['principal']);
+ }
+
+ $node = $this->server->tree->getNodeForPath($uri);
+
+ if (!($node instanceof IACL)) {
+ throw new DAV\Exception\MethodNotAllowed('This node does not support the ACL method');
+ }
+
+ $oldAcl = $this->getACL($node);
+
+ $supportedPrivileges = $this->getFlatPrivilegeSet($node);
+
+ /* Checking if protected principals from the existing principal set are
+ not overwritten. */
+ foreach($oldAcl as $oldAce) {
+
+ if (!isset($oldAce['protected']) || !$oldAce['protected']) continue;
+
+ $found = false;
+ foreach($newAcl as $newAce) {
+ if (
+ $newAce['privilege'] === $oldAce['privilege'] &&
+ $newAce['principal'] === $oldAce['principal'] &&
+ $newAce['protected']
+ )
+ $found = true;
+ }
+
+ if (!$found)
+ throw new Exception\AceConflict('This resource contained a protected {DAV:}ace, but this privilege did not occur in the ACL request');
+
+ }
+
+ foreach($newAcl as $newAce) {
+
+ // Do we recognize the privilege
+ if (!isset($supportedPrivileges[$newAce['privilege']])) {
+ throw new Exception\NotSupportedPrivilege('The privilege you specified (' . $newAce['privilege'] . ') is not recognized by this server');
+ }
+
+ if ($supportedPrivileges[$newAce['privilege']]['abstract']) {
+ throw new Exception\NoAbstract('The privilege you specified (' . $newAce['privilege'] . ') is an abstract privilege');
+ }
+
+ // Looking up the principal
+ try {
+ $principal = $this->server->tree->getNodeForPath($newAce['principal']);
+ } catch (DAV\Exception\NotFound $e) {
+ throw new Exception\NotRecognizedPrincipal('The specified principal (' . $newAce['principal'] . ') does not exist');
+ }
+ if (!($principal instanceof IPrincipal)) {
+ throw new Exception\NotRecognizedPrincipal('The specified uri (' . $newAce['principal'] . ') is not a principal');
+ }
+
+ }
+ $node->setACL($newAcl);
+
+ }
+
+ /* }}} */
+
+ /* Reports {{{ */
+
+ /**
+ * The expand-property report is defined in RFC3253 section 3-8.
+ *
+ * This report is very similar to a standard PROPFIND. The difference is
+ * that it has the additional ability to look at properties containing a
+ * {DAV:}href element, follow that property and grab additional elements
+ * there.
+ *
+ * Other rfc's, such as ACL rely on this report, so it made sense to put
+ * it in this plugin.
+ *
+ * @param \DOMElement $dom
+ * @return void
+ */
+ protected function expandPropertyReport($dom) {
+
+ $requestedProperties = $this->parseExpandPropertyReportRequest($dom->firstChild->firstChild);
+ $depth = $this->server->getHTTPDepth(0);
+ $requestUri = $this->server->getRequestUri();
+
+ $result = $this->expandProperties($requestUri,$requestedProperties,$depth);
+
+ $dom = new \DOMDocument('1.0','utf-8');
+ $dom->formatOutput = true;
+ $multiStatus = $dom->createElement('d:multistatus');
+ $dom->appendChild($multiStatus);
+
+ // Adding in default namespaces
+ foreach($this->server->xmlNamespaces as $namespace=>$prefix) {
+
+ $multiStatus->setAttribute('xmlns:' . $prefix,$namespace);
+
+ }
+
+ foreach($result as $response) {
+ $response->serialize($this->server, $multiStatus);
+ }
+
+ $xml = $dom->saveXML();
+ $this->server->httpResponse->setHeader('Content-Type','application/xml; charset=utf-8');
+ $this->server->httpResponse->sendStatus(207);
+ $this->server->httpResponse->sendBody($xml);
+
+ }
+
+ /**
+ * This method is used by expandPropertyReport to parse
+ * out the entire HTTP request.
+ *
+ * @param \DOMElement $node
+ * @return array
+ */
+ protected function parseExpandPropertyReportRequest($node) {
+
+ $requestedProperties = array();
+ do {
+
+ if (DAV\XMLUtil::toClarkNotation($node)!=='{DAV:}property') continue;
+
+ if ($node->firstChild) {
+
+ $children = $this->parseExpandPropertyReportRequest($node->firstChild);
+
+ } else {
+
+ $children = array();
+
+ }
+
+ $namespace = $node->getAttribute('namespace');
+ if (!$namespace) $namespace = 'DAV:';
+
+ $propName = '{'.$namespace.'}' . $node->getAttribute('name');
+ $requestedProperties[$propName] = $children;
+
+ } while ($node = $node->nextSibling);
+
+ return $requestedProperties;
+
+ }
+
+ /**
+ * This method expands all the properties and returns
+ * a list with property values
+ *
+ * @param array $path
+ * @param array $requestedProperties the list of required properties
+ * @param int $depth
+ * @return array
+ */
+ protected function expandProperties($path, array $requestedProperties, $depth) {
+
+ $foundProperties = $this->server->getPropertiesForPath($path, array_keys($requestedProperties), $depth);
+
+ $result = array();
+
+ foreach($foundProperties as $node) {
+
+ foreach($requestedProperties as $propertyName=>$childRequestedProperties) {
+
+ // We're only traversing if sub-properties were requested
+ if(count($childRequestedProperties)===0) continue;
+
+ // We only have to do the expansion if the property was found
+ // and it contains an href element.
+ if (!array_key_exists($propertyName,$node[200])) continue;
+
+ if ($node[200][$propertyName] instanceof DAV\Property\IHref) {
+ $hrefs = array($node[200][$propertyName]->getHref());
+ } elseif ($node[200][$propertyName] instanceof DAV\Property\HrefList) {
+ $hrefs = $node[200][$propertyName]->getHrefs();
+ }
+
+ $childProps = array();
+ foreach($hrefs as $href) {
+ $childProps = array_merge($childProps, $this->expandProperties($href, $childRequestedProperties, 0));
+ }
+ $node[200][$propertyName] = new DAV\Property\ResponseList($childProps);
+
+ }
+ $result[] = new DAV\Property\Response($node['href'], $node);
+
+ }
+
+ return $result;
+
+ }
+
+ /**
+ * principalSearchPropertySetReport
+ *
+ * This method responsible for handing the
+ * {DAV:}principal-search-property-set report. This report returns a list
+ * of properties the client may search on, using the
+ * {DAV:}principal-property-search report.
+ *
+ * @param \DOMDocument $dom
+ * @return void
+ */
+ protected function principalSearchPropertySetReport(\DOMDocument $dom) {
+
+ $httpDepth = $this->server->getHTTPDepth(0);
+ if ($httpDepth!==0) {
+ throw new DAV\Exception\BadRequest('This report is only defined when Depth: 0');
+ }
+
+ if ($dom->firstChild->hasChildNodes())
+ throw new DAV\Exception\BadRequest('The principal-search-property-set report element is not allowed to have child elements');
+
+ $dom = new \DOMDocument('1.0','utf-8');
+ $dom->formatOutput = true;
+ $root = $dom->createElement('d:principal-search-property-set');
+ $dom->appendChild($root);
+ // Adding in default namespaces
+ foreach($this->server->xmlNamespaces as $namespace=>$prefix) {
+
+ $root->setAttribute('xmlns:' . $prefix,$namespace);
+
+ }
+
+ $nsList = $this->server->xmlNamespaces;
+
+ foreach($this->principalSearchPropertySet as $propertyName=>$description) {
+
+ $psp = $dom->createElement('d:principal-search-property');
+ $root->appendChild($psp);
+
+ $prop = $dom->createElement('d:prop');
+ $psp->appendChild($prop);
+
+ $propName = null;
+ preg_match('/^{([^}]*)}(.*)$/',$propertyName,$propName);
+
+ $currentProperty = $dom->createElement($nsList[$propName[1]] . ':' . $propName[2]);
+ $prop->appendChild($currentProperty);
+
+ $descriptionElem = $dom->createElement('d:description');
+ $descriptionElem->setAttribute('xml:lang','en');
+ $descriptionElem->appendChild($dom->createTextNode($description));
+ $psp->appendChild($descriptionElem);
+
+
+ }
+
+ $this->server->httpResponse->setHeader('Content-Type','application/xml; charset=utf-8');
+ $this->server->httpResponse->sendStatus(200);
+ $this->server->httpResponse->sendBody($dom->saveXML());
+
+ }
+
+ /**
+ * principalPropertySearchReport
+ *
+ * This method is responsible for handing the
+ * {DAV:}principal-property-search report. This report can be used for
+ * clients to search for groups of principals, based on the value of one
+ * or more properties.
+ *
+ * @param \DOMDocument $dom
+ * @return void
+ */
+ protected function principalPropertySearchReport(\DOMDocument $dom) {
+
+ list($searchProperties, $requestedProperties, $applyToPrincipalCollectionSet) = $this->parsePrincipalPropertySearchReportRequest($dom);
+
+ $uri = null;
+ if (!$applyToPrincipalCollectionSet) {
+ $uri = $this->server->getRequestUri();
+ }
+ $result = $this->principalSearch($searchProperties, $requestedProperties, $uri);
+
+ $prefer = $this->server->getHTTPPRefer();
+
+ $this->server->httpResponse->sendStatus(207);
+ $this->server->httpResponse->setHeader('Content-Type','application/xml; charset=utf-8');
+ $this->server->httpResponse->setHeader('Vary','Brief,Prefer');
+ $this->server->httpResponse->sendBody($this->server->generateMultiStatus($result, $prefer['return-minimal']));
+
+ }
+
+ /**
+ * parsePrincipalPropertySearchReportRequest
+ *
+ * This method parses the request body from a
+ * {DAV:}principal-property-search report.
+ *
+ * This method returns an array with two elements:
+ * 1. an array with properties to search on, and their values
+ * 2. a list of propertyvalues that should be returned for the request.
+ *
+ * @param \DOMDocument $dom
+ * @return array
+ */
+ protected function parsePrincipalPropertySearchReportRequest($dom) {
+
+ $httpDepth = $this->server->getHTTPDepth(0);
+ if ($httpDepth!==0) {
+ throw new DAV\Exception\BadRequest('This report is only defined when Depth: 0');
+ }
+
+ $searchProperties = array();
+
+ $applyToPrincipalCollectionSet = false;
+
+ // Parsing the search request
+ foreach($dom->firstChild->childNodes as $searchNode) {
+
+ if (DAV\XMLUtil::toClarkNotation($searchNode) == '{DAV:}apply-to-principal-collection-set') {
+ $applyToPrincipalCollectionSet = true;
+ }
+
+ if (DAV\XMLUtil::toClarkNotation($searchNode)!=='{DAV:}property-search')
+ continue;
+
+ $propertyName = null;
+ $propertyValue = null;
+
+ foreach($searchNode->childNodes as $childNode) {
+
+ switch(DAV\XMLUtil::toClarkNotation($childNode)) {
+
+ case '{DAV:}prop' :
+ $property = DAV\XMLUtil::parseProperties($searchNode);
+ reset($property);
+ $propertyName = key($property);
+ break;
+
+ case '{DAV:}match' :
+ $propertyValue = $childNode->textContent;
+ break;
+
+ }
+
+
+ }
+
+ if (is_null($propertyName) || is_null($propertyValue))
+ throw new DAV\Exception\BadRequest('Invalid search request. propertyname: ' . $propertyName . '. propertvvalue: ' . $propertyValue);
+
+ $searchProperties[$propertyName] = $propertyValue;
+
+ }
+
+ return array($searchProperties, array_keys(DAV\XMLUtil::parseProperties($dom->firstChild)), $applyToPrincipalCollectionSet);
+
+ }
+
+
+ /* }}} */
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAVACL/Principal.php b/app/Gdoo/Calendar/Sabre/DAVACL/Principal.php
new file mode 100644
index 00000000..37dbd953
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAVACL/Principal.php
@@ -0,0 +1,281 @@
+principalBackend = $principalBackend;
+ $this->principalProperties = $principalProperties;
+
+ }
+
+ /**
+ * Returns the full principal url
+ *
+ * @return string
+ */
+ public function getPrincipalUrl() {
+
+ return $this->principalProperties['uri'];
+
+ }
+
+ /**
+ * Returns a list of alternative urls for a principal
+ *
+ * This can for example be an email address, or ldap url.
+ *
+ * @return array
+ */
+ public function getAlternateUriSet() {
+
+ $uris = array();
+ if (isset($this->principalProperties['{DAV:}alternate-URI-set'])) {
+
+ $uris = $this->principalProperties['{DAV:}alternate-URI-set'];
+
+ }
+
+ if (isset($this->principalProperties['{http://sabredav.org/ns}email-address'])) {
+ $uris[] = 'mailto:' . $this->principalProperties['{http://sabredav.org/ns}email-address'];
+ }
+
+ return array_unique($uris);
+
+ }
+
+ /**
+ * Returns the list of group members
+ *
+ * If this principal is a group, this function should return
+ * all member principal uri's for the group.
+ *
+ * @return array
+ */
+ public function getGroupMemberSet() {
+
+ return $this->principalBackend->getGroupMemberSet($this->principalProperties['uri']);
+
+ }
+
+ /**
+ * Returns the list of groups this principal is member of
+ *
+ * If this principal is a member of a (list of) groups, this function
+ * should return a list of principal uri's for it's members.
+ *
+ * @return array
+ */
+ public function getGroupMembership() {
+
+ return $this->principalBackend->getGroupMemberShip($this->principalProperties['uri']);
+
+ }
+
+
+ /**
+ * Sets a list of group members
+ *
+ * If this principal is a group, this method sets all the group members.
+ * The list of members is always overwritten, never appended to.
+ *
+ * This method should throw an exception if the members could not be set.
+ *
+ * @param array $groupMembers
+ * @return void
+ */
+ public function setGroupMemberSet(array $groupMembers) {
+
+ $this->principalBackend->setGroupMemberSet($this->principalProperties['uri'], $groupMembers);
+
+ }
+
+
+ /**
+ * Returns this principals name.
+ *
+ * @return string
+ */
+ public function getName() {
+
+ $uri = $this->principalProperties['uri'];
+ list(, $name) = DAV\URLUtil::splitPath($uri);
+ return $name;
+
+ }
+
+ /**
+ * Returns the name of the user
+ *
+ * @return string
+ */
+ public function getDisplayName() {
+
+ if (isset($this->principalProperties['{DAV:}displayname'])) {
+ return $this->principalProperties['{DAV:}displayname'];
+ } else {
+ return $this->getName();
+ }
+
+ }
+
+ /**
+ * Returns a list of properties
+ *
+ * @param array $requestedProperties
+ * @return array
+ */
+ public function getProperties($requestedProperties) {
+
+ $newProperties = array();
+ foreach($requestedProperties as $propName) {
+
+ if (isset($this->principalProperties[$propName])) {
+ $newProperties[$propName] = $this->principalProperties[$propName];
+ }
+
+ }
+
+ return $newProperties;
+
+ }
+
+ /**
+ * Updates this principals properties.
+ *
+ * @param array $mutations
+ * @see Sabre\DAV\IProperties::updateProperties
+ * @return bool|array
+ */
+ public function updateProperties($mutations) {
+
+ return $this->principalBackend->updatePrincipal($this->principalProperties['uri'], $mutations);
+
+ }
+
+ /**
+ * Returns the owner principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getOwner() {
+
+ return $this->principalProperties['uri'];
+
+
+ }
+
+ /**
+ * Returns a group principal
+ *
+ * This must be a url to a principal, or null if there's no owner
+ *
+ * @return string|null
+ */
+ public function getGroup() {
+
+ return null;
+
+ }
+
+ /**
+ * Returns a list of ACE's for this node.
+ *
+ * Each ACE has the following properties:
+ * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
+ * currently the only supported privileges
+ * * 'principal', a url to the principal who owns the node
+ * * 'protected' (optional), indicating that this ACE is not allowed to
+ * be updated.
+ *
+ * @return array
+ */
+ public function getACL() {
+
+ return array(
+ array(
+ 'privilege' => '{DAV:}read',
+ 'principal' => $this->getPrincipalUrl(),
+ 'protected' => true,
+ ),
+ );
+
+ }
+
+ /**
+ * Updates the ACL
+ *
+ * This method will receive a list of new ACE's.
+ *
+ * @param array $acl
+ * @return void
+ */
+ public function setACL(array $acl) {
+
+ throw new DAV\Exception\MethodNotAllowed('Updating ACLs is not allowed here');
+
+ }
+
+ /**
+ * Returns the list of supported privileges for this node.
+ *
+ * The returned data structure is a list of nested privileges.
+ * See Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple
+ * standard structure.
+ *
+ * If null is returned from this method, the default privilege set is used,
+ * which is fine for most common usecases.
+ *
+ * @return array|null
+ */
+ public function getSupportedPrivilegeSet() {
+
+ return null;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAVACL/PrincipalBackend/AbstractBackend.php b/app/Gdoo/Calendar/Sabre/DAVACL/PrincipalBackend/AbstractBackend.php
new file mode 100644
index 00000000..b898c790
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAVACL/PrincipalBackend/AbstractBackend.php
@@ -0,0 +1,18 @@
+ array(
+ * '{DAV:}prop1' => null,
+ * ),
+ * 201 => array(
+ * '{DAV:}prop2' => null,
+ * ),
+ * 403 => array(
+ * '{DAV:}prop3' => null,
+ * ),
+ * 424 => array(
+ * '{DAV:}prop4' => null,
+ * ),
+ * );
+ *
+ * In this previous example prop1 was successfully updated or deleted, and
+ * prop2 was succesfully created.
+ *
+ * prop3 failed to update due to '403 Forbidden' and because of this prop4
+ * also could not be updated with '424 Failed dependency'.
+ *
+ * This last example was actually incorrect. While 200 and 201 could appear
+ * in 1 response, if there's any error (403) the other properties should
+ * always fail with 423 (failed dependency).
+ *
+ * But anyway, if you don't want to scratch your head over this, just
+ * return true or false.
+ *
+ * @param string $path
+ * @param array $mutations
+ * @return array|bool
+ */
+ function updatePrincipal($path, $mutations);
+
+ /**
+ * This method is used to search for principals matching a set of
+ * properties.
+ *
+ * This search is specifically used by RFC3744's principal-property-search
+ * REPORT. You should at least allow searching on
+ * http://sabredav.org/ns}email-address.
+ *
+ * The actual search should be a unicode-non-case-sensitive search. The
+ * keys in searchProperties are the WebDAV property names, while the values
+ * are the property values to search on.
+ *
+ * If multiple properties are being searched on, the search should be
+ * AND'ed.
+ *
+ * This method should simply return an array with full principal uri's.
+ *
+ * If somebody attempted to search on a property the backend does not
+ * support, you should simply return 0 results.
+ *
+ * You can also just return 0 results if you choose to not support
+ * searching at all, but keep in mind that this may stop certain features
+ * from working.
+ *
+ * @param string $prefixPath
+ * @param array $searchProperties
+ * @return array
+ */
+ function searchPrincipals($prefixPath, array $searchProperties);
+
+ /**
+ * Returns the list of members for a group-principal
+ *
+ * @param string $principal
+ * @return array
+ */
+ function getGroupMemberSet($principal);
+
+ /**
+ * Returns the list of groups a principal is a member of
+ *
+ * @param string $principal
+ * @return array
+ */
+ function getGroupMembership($principal);
+
+ /**
+ * Updates the list of group members for a group principal.
+ *
+ * The principals should be passed as a list of uri's.
+ *
+ * @param string $principal
+ * @param array $members
+ * @return void
+ */
+ function setGroupMemberSet($principal, array $members);
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAVACL/PrincipalBackend/PDO.php b/app/Gdoo/Calendar/Sabre/DAVACL/PrincipalBackend/PDO.php
new file mode 100644
index 00000000..9390e816
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAVACL/PrincipalBackend/PDO.php
@@ -0,0 +1,428 @@
+ array(
+ 'dbField' => 'displayname',
+ ),
+
+ /**
+ * This property is actually used by the CardDAV plugin, where it gets
+ * mapped to {http://calendarserver.orgi/ns/}me-card.
+ *
+ * The reason we don't straight-up use that property, is because
+ * me-card is defined as a property on the users' addressbook
+ * collection.
+ */
+ '{http://sabredav.org/ns}vcard-url' => array(
+ 'dbField' => 'vcardurl',
+ ),
+ /**
+ * This is the users' primary email-address.
+ */
+ '{http://sabredav.org/ns}email-address' => array(
+ 'dbField' => 'email',
+ ),
+ );
+
+ /**
+ * Sets up the backend.
+ *
+ * @param PDO $pdo
+ * @param string $tableName
+ * @param string $groupMembersTableName
+ */
+ public function __construct(\PDO $pdo, $tableName = 'principals', $groupMembersTableName = 'groupmembers') {
+
+ $this->pdo = $pdo;
+ $this->tableName = $tableName;
+ $this->groupMembersTableName = $groupMembersTableName;
+
+ }
+
+
+ /**
+ * Returns a list of principals based on a prefix.
+ *
+ * This prefix will often contain something like 'principals'. You are only
+ * expected to return principals that are in this base path.
+ *
+ * You are expected to return at least a 'uri' for every user, you can
+ * return any additional properties if you wish so. Common properties are:
+ * {DAV:}displayname
+ * {http://sabredav.org/ns}email-address - This is a custom SabreDAV
+ * field that's actualy injected in a number of other properties. If
+ * you have an email address, use this property.
+ *
+ * @param string $prefixPath
+ * @return array
+ */
+ public function getPrincipalsByPrefix($prefixPath) {
+
+ $fields = array(
+ 'uri',
+ );
+
+ foreach($this->fieldMap as $key=>$value) {
+ $fields[] = $value['dbField'];
+ }
+ $result = $this->pdo->query('SELECT '.implode(',', $fields).' FROM '. $this->tableName);
+
+ $principals = array();
+
+ while($row = $result->fetch(\PDO::FETCH_ASSOC)) {
+
+ // Checking if the principal is in the prefix
+ list($rowPrefix) = DAV\URLUtil::splitPath($row['uri']);
+ if ($rowPrefix !== $prefixPath) continue;
+
+ $principal = array(
+ 'uri' => $row['uri'],
+ );
+ foreach($this->fieldMap as $key=>$value) {
+ if ($row[$value['dbField']]) {
+ $principal[$key] = $row[$value['dbField']];
+ }
+ }
+ $principals[] = $principal;
+
+ }
+
+ return $principals;
+
+ }
+
+ /**
+ * Returns a specific principal, specified by it's path.
+ * The returned structure should be the exact same as from
+ * getPrincipalsByPrefix.
+ *
+ * @param string $path
+ * @return array
+ */
+ public function getPrincipalByPath($path) {
+
+ $fields = array(
+ 'id',
+ 'uri',
+ );
+
+ foreach($this->fieldMap as $key=>$value) {
+ $fields[] = $value['dbField'];
+ }
+ $stmt = $this->pdo->prepare('SELECT '.implode(',', $fields).' FROM '. $this->tableName . ' WHERE uri = ?');
+ $stmt->execute(array($path));
+
+ $row = $stmt->fetch(\PDO::FETCH_ASSOC);
+ if (!$row) return;
+
+ $principal = array(
+ 'id' => $row['id'],
+ 'uri' => $row['uri'],
+ );
+ foreach($this->fieldMap as $key=>$value) {
+ if ($row[$value['dbField']]) {
+ $principal[$key] = $row[$value['dbField']];
+ }
+ }
+ return $principal;
+
+ }
+
+ /**
+ * Updates one ore more webdav properties on a principal.
+ *
+ * The list of mutations is supplied as an array. Each key in the array is
+ * a propertyname, such as {DAV:}displayname.
+ *
+ * Each value is the actual value to be updated. If a value is null, it
+ * must be deleted.
+ *
+ * This method should be atomic. It must either completely succeed, or
+ * completely fail. Success and failure can simply be returned as 'true' or
+ * 'false'.
+ *
+ * It is also possible to return detailed failure information. In that case
+ * an array such as this should be returned:
+ *
+ * array(
+ * 200 => array(
+ * '{DAV:}prop1' => null,
+ * ),
+ * 201 => array(
+ * '{DAV:}prop2' => null,
+ * ),
+ * 403 => array(
+ * '{DAV:}prop3' => null,
+ * ),
+ * 424 => array(
+ * '{DAV:}prop4' => null,
+ * ),
+ * );
+ *
+ * In this previous example prop1 was successfully updated or deleted, and
+ * prop2 was succesfully created.
+ *
+ * prop3 failed to update due to '403 Forbidden' and because of this prop4
+ * also could not be updated with '424 Failed dependency'.
+ *
+ * This last example was actually incorrect. While 200 and 201 could appear
+ * in 1 response, if there's any error (403) the other properties should
+ * always fail with 423 (failed dependency).
+ *
+ * But anyway, if you don't want to scratch your head over this, just
+ * return true or false.
+ *
+ * @param string $path
+ * @param array $mutations
+ * @return array|bool
+ */
+ public function updatePrincipal($path, $mutations) {
+
+ $updateAble = array();
+ foreach($mutations as $key=>$value) {
+
+ // We are not aware of this field, we must fail.
+ if (!isset($this->fieldMap[$key])) {
+
+ $response = array(
+ 403 => array(
+ $key => null,
+ ),
+ 424 => array(),
+ );
+
+ // Adding the rest to the response as a 424
+ foreach($mutations as $subKey=>$subValue) {
+ if ($subKey !== $key) {
+ $response[424][$subKey] = null;
+ }
+ }
+ return $response;
+ }
+
+ $updateAble[$this->fieldMap[$key]['dbField']] = $value;
+
+ }
+
+ // No fields to update
+ $query = "UPDATE " . $this->tableName . " SET ";
+
+ $first = true;
+ foreach($updateAble as $key => $value) {
+ if (!$first) {
+ $query.= ', ';
+ }
+ $first = false;
+ $query.= "$key = :$key ";
+ }
+ $query.='WHERE uri = :uri';
+ $stmt = $this->pdo->prepare($query);
+ $updateAble['uri'] = $path;
+ $stmt->execute($updateAble);
+
+ return true;
+
+ }
+
+ /**
+ * This method is used to search for principals matching a set of
+ * properties.
+ *
+ * This search is specifically used by RFC3744's principal-property-search
+ * REPORT. You should at least allow searching on
+ * http://sabredav.org/ns}email-address.
+ *
+ * The actual search should be a unicode-non-case-sensitive search. The
+ * keys in searchProperties are the WebDAV property names, while the values
+ * are the property values to search on.
+ *
+ * If multiple properties are being searched on, the search should be
+ * AND'ed.
+ *
+ * This method should simply return an array with full principal uri's.
+ *
+ * If somebody attempted to search on a property the backend does not
+ * support, you should simply return 0 results.
+ *
+ * You can also just return 0 results if you choose to not support
+ * searching at all, but keep in mind that this may stop certain features
+ * from working.
+ *
+ * @param string $prefixPath
+ * @param array $searchProperties
+ * @return array
+ */
+ public function searchPrincipals($prefixPath, array $searchProperties) {
+
+ $query = 'SELECT uri FROM ' . $this->tableName . ' WHERE 1=1 ';
+ $values = array();
+ foreach($searchProperties as $property => $value) {
+
+ switch($property) {
+
+ case '{DAV:}displayname' :
+ $query.=' AND displayname LIKE ?';
+ $values[] = '%' . $value . '%';
+ break;
+ case '{http://sabredav.org/ns}email-address' :
+ $query.=' AND email LIKE ?';
+ $values[] = '%' . $value . '%';
+ break;
+ default :
+ // Unsupported property
+ return array();
+
+ }
+
+ }
+ $stmt = $this->pdo->prepare($query);
+ $stmt->execute($values);
+
+ $principals = array();
+ while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
+
+ // Checking if the principal is in the prefix
+ list($rowPrefix) = DAV\URLUtil::splitPath($row['uri']);
+ if ($rowPrefix !== $prefixPath) continue;
+
+ $principals[] = $row['uri'];
+
+ }
+
+ return $principals;
+
+ }
+
+ /**
+ * Returns the list of members for a group-principal
+ *
+ * @param string $principal
+ * @return array
+ */
+ public function getGroupMemberSet($principal) {
+
+ $principal = $this->getPrincipalByPath($principal);
+ if (!$principal) throw new DAV\Exception('Principal not found');
+
+ $stmt = $this->pdo->prepare('SELECT principals.uri as uri FROM '.$this->groupMembersTableName.' AS groupmembers LEFT JOIN '.$this->tableName.' AS principals ON groupmembers.member_id = principals.id WHERE groupmembers.principal_id = ?');
+ $stmt->execute(array($principal['id']));
+
+ $result = array();
+ while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
+ $result[] = $row['uri'];
+ }
+ return $result;
+
+ }
+
+ /**
+ * Returns the list of groups a principal is a member of
+ *
+ * @param string $principal
+ * @return array
+ */
+ public function getGroupMembership($principal) {
+
+ $principal = $this->getPrincipalByPath($principal);
+ if (!$principal) throw new DAV\Exception('Principal not found');
+
+ $stmt = $this->pdo->prepare('SELECT principals.uri as uri FROM '.$this->groupMembersTableName.' AS groupmembers LEFT JOIN '.$this->tableName.' AS principals ON groupmembers.principal_id = principals.id WHERE groupmembers.member_id = ?');
+ $stmt->execute(array($principal['id']));
+
+ $result = array();
+ while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
+ $result[] = $row['uri'];
+ }
+ return $result;
+
+ }
+
+ /**
+ * Updates the list of group members for a group principal.
+ *
+ * The principals should be passed as a list of uri's.
+ *
+ * @param string $principal
+ * @param array $members
+ * @return void
+ */
+ public function setGroupMemberSet($principal, array $members) {
+
+ // Grabbing the list of principal id's.
+ $stmt = $this->pdo->prepare('SELECT id, uri FROM '.$this->tableName.' WHERE uri IN (? ' . str_repeat(', ? ', count($members)) . ');');
+ $stmt->execute(array_merge(array($principal), $members));
+
+ $memberIds = array();
+ $principalId = null;
+
+ while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
+ if ($row['uri'] == $principal) {
+ $principalId = $row['id'];
+ } else {
+ $memberIds[] = $row['id'];
+ }
+ }
+ if (!$principalId) throw new DAV\Exception('Principal not found');
+
+ // Wiping out old members
+ $stmt = $this->pdo->prepare('DELETE FROM '.$this->groupMembersTableName.' WHERE principal_id = ?;');
+ $stmt->execute(array($principalId));
+
+ foreach($memberIds as $memberId) {
+
+ $stmt = $this->pdo->prepare('INSERT INTO '.$this->groupMembersTableName.' (principal_id, member_id) VALUES (?, ?);');
+ $stmt->execute(array($principalId, $memberId));
+
+ }
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAVACL/PrincipalCollection.php b/app/Gdoo/Calendar/Sabre/DAVACL/PrincipalCollection.php
new file mode 100644
index 00000000..059284b2
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAVACL/PrincipalCollection.php
@@ -0,0 +1,33 @@
+principalBackend, $principal);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAVACL/Property/Acl.php b/app/Gdoo/Calendar/Sabre/DAVACL/Property/Acl.php
new file mode 100644
index 00000000..1afcc389
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAVACL/Property/Acl.php
@@ -0,0 +1,211 @@
+privileges = $privileges;
+ $this->prefixBaseUrl = $prefixBaseUrl;
+
+ }
+
+ /**
+ * Returns the list of privileges for this property
+ *
+ * @return array
+ */
+ public function getPrivileges() {
+
+ return $this->privileges;
+
+ }
+
+ /**
+ * Serializes the property into a DOMElement
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $node
+ * @return void
+ */
+ public function serialize(DAV\Server $server,\DOMElement $node) {
+
+ $doc = $node->ownerDocument;
+ foreach($this->privileges as $ace) {
+
+ $this->serializeAce($doc, $node, $ace, $server);
+
+ }
+
+ }
+
+ /**
+ * Unserializes the {DAV:}acl xml element.
+ *
+ * @param \DOMElement $dom
+ * @return Acl
+ */
+ static public function unserialize(\DOMElement $dom) {
+
+ $privileges = array();
+ $xaces = $dom->getElementsByTagNameNS('urn:DAV','ace');
+ for($ii=0; $ii < $xaces->length; $ii++) {
+
+ $xace = $xaces->item($ii);
+ $principal = $xace->getElementsByTagNameNS('urn:DAV','principal');
+ if ($principal->length !== 1) {
+ throw new DAV\Exception\BadRequest('Each {DAV:}ace element must have one {DAV:}principal element');
+ }
+ $principal = Principal::unserialize($principal->item(0));
+
+ switch($principal->getType()) {
+ case Principal::HREF :
+ $principal = $principal->getHref();
+ break;
+ case Principal::AUTHENTICATED :
+ $principal = '{DAV:}authenticated';
+ break;
+ case Principal::UNAUTHENTICATED :
+ $principal = '{DAV:}unauthenticated';
+ break;
+ case Principal::ALL :
+ $principal = '{DAV:}all';
+ break;
+
+ }
+
+ $protected = false;
+
+ if ($xace->getElementsByTagNameNS('urn:DAV','protected')->length > 0) {
+ $protected = true;
+ }
+
+ $grants = $xace->getElementsByTagNameNS('urn:DAV','grant');
+ if ($grants->length < 1) {
+ throw new DAV\Exception\NotImplemented('Every {DAV:}ace element must have a {DAV:}grant element. {DAV:}deny is not yet supported');
+ }
+ $grant = $grants->item(0);
+
+ $xprivs = $grant->getElementsByTagNameNS('urn:DAV','privilege');
+ for($jj=0; $jj<$xprivs->length; $jj++) {
+
+ $xpriv = $xprivs->item($jj);
+
+ $privilegeName = null;
+
+ for ($kk=0;$kk<$xpriv->childNodes->length;$kk++) {
+
+ $childNode = $xpriv->childNodes->item($kk);
+ if ($t = DAV\XMLUtil::toClarkNotation($childNode)) {
+ $privilegeName = $t;
+ break;
+ }
+ }
+ if (is_null($privilegeName)) {
+ throw new DAV\Exception\BadRequest('{DAV:}privilege elements must have a privilege element contained within them.');
+ }
+
+ $privileges[] = array(
+ 'principal' => $principal,
+ 'protected' => $protected,
+ 'privilege' => $privilegeName,
+ );
+
+ }
+
+ }
+
+ return new self($privileges);
+
+ }
+
+ /**
+ * Serializes a single access control entry.
+ *
+ * @param \DOMDocument $doc
+ * @param \DOMElement $node
+ * @param array $ace
+ * @param DAV\Server $server
+ * @return void
+ */
+ private function serializeAce($doc,$node,$ace, DAV\Server $server) {
+
+ $xace = $doc->createElementNS('DAV:','d:ace');
+ $node->appendChild($xace);
+
+ $principal = $doc->createElementNS('DAV:','d:principal');
+ $xace->appendChild($principal);
+ switch($ace['principal']) {
+ case '{DAV:}authenticated' :
+ $principal->appendChild($doc->createElementNS('DAV:','d:authenticated'));
+ break;
+ case '{DAV:}unauthenticated' :
+ $principal->appendChild($doc->createElementNS('DAV:','d:unauthenticated'));
+ break;
+ case '{DAV:}all' :
+ $principal->appendChild($doc->createElementNS('DAV:','d:all'));
+ break;
+ default:
+ $principal->appendChild($doc->createElementNS('DAV:','d:href',($this->prefixBaseUrl?$server->getBaseUri():'') . $ace['principal'] . '/'));
+ }
+
+ $grant = $doc->createElementNS('DAV:','d:grant');
+ $xace->appendChild($grant);
+
+ $privParts = null;
+
+ preg_match('/^{([^}]*)}(.*)$/',$ace['privilege'],$privParts);
+
+ $xprivilege = $doc->createElementNS('DAV:','d:privilege');
+ $grant->appendChild($xprivilege);
+
+ $xprivilege->appendChild($doc->createElementNS($privParts[1],'d:'.$privParts[2]));
+
+ if (isset($ace['protected']) && $ace['protected'])
+ $xace->appendChild($doc->createElement('d:protected'));
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAVACL/Property/AclRestrictions.php b/app/Gdoo/Calendar/Sabre/DAVACL/Property/AclRestrictions.php
new file mode 100644
index 00000000..6fcc4c1c
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAVACL/Property/AclRestrictions.php
@@ -0,0 +1,34 @@
+ownerDocument;
+
+ $elem->appendChild($doc->createElementNS('DAV:','d:grant-only'));
+ $elem->appendChild($doc->createElementNS('DAV:','d:no-invert'));
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAVACL/Property/CurrentUserPrivilegeSet.php b/app/Gdoo/Calendar/Sabre/DAVACL/Property/CurrentUserPrivilegeSet.php
new file mode 100644
index 00000000..69e04516
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAVACL/Property/CurrentUserPrivilegeSet.php
@@ -0,0 +1,124 @@
+privileges = $privileges;
+
+ }
+
+ /**
+ * Serializes the property in the DOM
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $node
+ * @return void
+ */
+ public function serialize(DAV\Server $server,\DOMElement $node) {
+
+ $doc = $node->ownerDocument;
+ foreach($this->privileges as $privName) {
+
+ $this->serializePriv($doc,$node,$privName);
+
+ }
+
+ }
+
+ /**
+ * Returns true or false, whether the specified principal appears in the
+ * list.
+ *
+ * @return bool
+ */
+ public function has($privilegeName) {
+
+ return in_array($privilegeName, $this->privileges);
+
+ }
+
+ /**
+ * Serializes one privilege
+ *
+ * @param \DOMDocument $doc
+ * @param \DOMElement $node
+ * @param string $privName
+ * @return void
+ */
+ protected function serializePriv($doc,$node,$privName) {
+
+ $xp = $doc->createElementNS('DAV:','d:privilege');
+ $node->appendChild($xp);
+
+ $privParts = null;
+ preg_match('/^{([^}]*)}(.*)$/',$privName,$privParts);
+
+ $xp->appendChild($doc->createElementNS($privParts[1],'d:'.$privParts[2]));
+
+ }
+
+ /**
+ * Unserializes the {DAV:}current-user-privilege-set element.
+ *
+ * @param DOMElement $node
+ * @return CurrentUserPrivilegeSet
+ */
+ static public function unserialize(\DOMElement $node) {
+
+ $result = array();
+
+ $xprivs = $node->getElementsByTagNameNS('urn:DAV','privilege');
+
+ for($jj=0; $jj<$xprivs->length; $jj++) {
+
+ $xpriv = $xprivs->item($jj);
+
+ $privilegeName = null;
+
+ for ($kk=0;$kk<$xpriv->childNodes->length;$kk++) {
+
+ $childNode = $xpriv->childNodes->item($kk);
+ if ($t = DAV\XMLUtil::toClarkNotation($childNode)) {
+ $privilegeName = $t;
+ break;
+ }
+ }
+
+ $result[] = $privilegeName;
+
+ }
+
+ return new self($result);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAVACL/Property/Principal.php b/app/Gdoo/Calendar/Sabre/DAVACL/Property/Principal.php
new file mode 100644
index 00000000..56de8456
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAVACL/Property/Principal.php
@@ -0,0 +1,161 @@
+type = $type;
+
+ if ($type===self::HREF && is_null($href)) {
+ throw new DAV\Exception('The href argument must be specified for the HREF principal type.');
+ }
+ $this->href = $href;
+
+ }
+
+ /**
+ * Returns the principal type
+ *
+ * @return int
+ */
+ public function getType() {
+
+ return $this->type;
+
+ }
+
+ /**
+ * Returns the principal uri.
+ *
+ * @return string
+ */
+ public function getHref() {
+
+ return $this->href;
+
+ }
+
+ /**
+ * Serializes the property into a DOMElement.
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $node
+ * @return void
+ */
+ public function serialize(DAV\Server $server, \DOMElement $node) {
+
+ $prefix = $server->xmlNamespaces['DAV:'];
+ switch($this->type) {
+
+ case self::UNAUTHENTICATED :
+ $node->appendChild(
+ $node->ownerDocument->createElement($prefix . ':unauthenticated')
+ );
+ break;
+ case self::AUTHENTICATED :
+ $node->appendChild(
+ $node->ownerDocument->createElement($prefix . ':authenticated')
+ );
+ break;
+ case self::HREF :
+ $href = $node->ownerDocument->createElement($prefix . ':href');
+ $href->nodeValue = $server->getBaseUri() . DAV\URLUtil::encodePath($this->href);
+ $node->appendChild($href);
+ break;
+
+ }
+
+ }
+
+ /**
+ * Deserializes a DOM element into a property object.
+ *
+ * @param \DOMElement $dom
+ * @return Principal
+ */
+ static public function unserialize(\DOMElement $dom) {
+
+ $parent = $dom->firstChild;
+ while(!DAV\XMLUtil::toClarkNotation($parent)) {
+ $parent = $parent->nextSibling;
+ }
+
+ switch(DAV\XMLUtil::toClarkNotation($parent)) {
+
+ case '{DAV:}unauthenticated' :
+ return new self(self::UNAUTHENTICATED);
+ case '{DAV:}authenticated' :
+ return new self(self::AUTHENTICATED);
+ case '{DAV:}href':
+ return new self(self::HREF, $parent->textContent);
+ case '{DAV:}all':
+ return new self(self::ALL);
+ default :
+ throw new DAV\Exception\BadRequest('Unexpected element (' . DAV\XMLUtil::toClarkNotation($parent) . '). Could not deserialize');
+
+ }
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAVACL/Property/SupportedPrivilegeSet.php b/app/Gdoo/Calendar/Sabre/DAVACL/Property/SupportedPrivilegeSet.php
new file mode 100644
index 00000000..ad717487
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAVACL/Property/SupportedPrivilegeSet.php
@@ -0,0 +1,94 @@
+privileges = $privileges;
+
+ }
+
+ /**
+ * Serializes the property into a domdocument.
+ *
+ * @param DAV\Server $server
+ * @param \DOMElement $node
+ * @return void
+ */
+ public function serialize(DAV\Server $server,\DOMElement $node) {
+
+ $doc = $node->ownerDocument;
+ $this->serializePriv($doc, $node, $this->privileges);
+
+ }
+
+ /**
+ * Serializes a property
+ *
+ * This is a recursive function.
+ *
+ * @param \DOMDocument $doc
+ * @param \DOMElement $node
+ * @param array $privilege
+ * @return void
+ */
+ private function serializePriv($doc,$node,$privilege) {
+
+ $xsp = $doc->createElementNS('DAV:','d:supported-privilege');
+ $node->appendChild($xsp);
+
+ $xp = $doc->createElementNS('DAV:','d:privilege');
+ $xsp->appendChild($xp);
+
+ $privParts = null;
+ preg_match('/^{([^}]*)}(.*)$/',$privilege['privilege'],$privParts);
+
+ $xp->appendChild($doc->createElementNS($privParts[1],'d:'.$privParts[2]));
+
+ if (isset($privilege['abstract']) && $privilege['abstract']) {
+ $xsp->appendChild($doc->createElementNS('DAV:','d:abstract'));
+ }
+
+ if (isset($privilege['description'])) {
+ $xsp->appendChild($doc->createElementNS('DAV:','d:description',$privilege['description']));
+ }
+
+ if (isset($privilege['aggregates'])) {
+ foreach($privilege['aggregates'] as $subPrivilege) {
+ $this->serializePriv($doc,$xsp,$subPrivilege);
+ }
+ }
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/DAVACL/Version.php b/app/Gdoo/Calendar/Sabre/DAVACL/Version.php
new file mode 100644
index 00000000..b2bacbdb
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/DAVACL/Version.php
@@ -0,0 +1,24 @@
+httpRequest->getHeader('Authorization');
+ $authHeader = explode(' ',$authHeader);
+
+ if ($authHeader[0]!='AWS' || !isset($authHeader[1])) {
+ $this->errorCode = self::ERR_NOAWSHEADER;
+ return false;
+ }
+
+ list($this->accessKey,$this->signature) = explode(':',$authHeader[1]);
+
+ return true;
+
+ }
+
+ /**
+ * Returns the username for the request
+ *
+ * @return string
+ */
+ public function getAccessKey() {
+
+ return $this->accessKey;
+
+ }
+
+ /**
+ * Validates the signature based on the secretKey
+ *
+ * @param string $secretKey
+ * @return bool
+ */
+ public function validate($secretKey) {
+
+ $contentMD5 = $this->httpRequest->getHeader('Content-MD5');
+
+ if ($contentMD5) {
+ // We need to validate the integrity of the request
+ $body = $this->httpRequest->getBody(true);
+ $this->httpRequest->setBody($body,true);
+
+ if ($contentMD5!=base64_encode(md5($body,true))) {
+ // content-md5 header did not match md5 signature of body
+ $this->errorCode = self::ERR_MD5CHECKSUMWRONG;
+ return false;
+ }
+
+ }
+
+ if (!$requestDate = $this->httpRequest->getHeader('x-amz-date'))
+ $requestDate = $this->httpRequest->getHeader('Date');
+
+ if (!$this->validateRFC2616Date($requestDate))
+ return false;
+
+ $amzHeaders = $this->getAmzHeaders();
+
+ $signature = base64_encode(
+ $this->hmacsha1($secretKey,
+ $this->httpRequest->getMethod() . "\n" .
+ $contentMD5 . "\n" .
+ $this->httpRequest->getHeader('Content-type') . "\n" .
+ $requestDate . "\n" .
+ $amzHeaders .
+ $this->httpRequest->getURI()
+ )
+ );
+
+ if ($this->signature != $signature) {
+
+ $this->errorCode = self::ERR_INVALIDSIGNATURE;
+ return false;
+
+ }
+
+ return true;
+
+ }
+
+
+ /**
+ * Returns an HTTP 401 header, forcing login
+ *
+ * This should be called when username and password are incorrect, or not supplied at all
+ *
+ * @return void
+ */
+ public function requireLogin() {
+
+ $this->httpResponse->setHeader('WWW-Authenticate','AWS');
+ $this->httpResponse->sendStatus(401);
+
+ }
+
+ /**
+ * Makes sure the supplied value is a valid RFC2616 date.
+ *
+ * If we would just use strtotime to get a valid timestamp, we have no way of checking if a
+ * user just supplied the word 'now' for the date header.
+ *
+ * This function also makes sure the Date header is within 15 minutes of the operating
+ * system date, to prevent replay attacks.
+ *
+ * @param string $dateHeader
+ * @return bool
+ */
+ protected function validateRFC2616Date($dateHeader) {
+
+ $date = Util::parseHTTPDate($dateHeader);
+
+ // Unknown format
+ if (!$date) {
+ $this->errorCode = self::ERR_INVALIDDATEFORMAT;
+ return false;
+ }
+
+ $min = new \DateTime('-15 minutes');
+ $max = new \DateTime('+15 minutes');
+
+ // We allow 15 minutes around the current date/time
+ if ($date > $max || $date < $min) {
+ $this->errorCode = self::ERR_REQUESTTIMESKEWED;
+ return false;
+ }
+
+ return $date;
+
+ }
+
+ /**
+ * Returns a list of AMZ headers
+ *
+ * @return string
+ */
+ protected function getAmzHeaders() {
+
+ $amzHeaders = array();
+ $headers = $this->httpRequest->getHeaders();
+ foreach($headers as $headerName => $headerValue) {
+ if (strpos(strtolower($headerName),'x-amz-')===0) {
+ $amzHeaders[strtolower($headerName)] = str_replace(array("\r\n"),array(' '),$headerValue) . "\n";
+ }
+ }
+ ksort($amzHeaders);
+
+ $headerStr = '';
+ foreach($amzHeaders as $h=>$v) {
+ $headerStr.=$h.':'.$v;
+ }
+
+ return $headerStr;
+
+ }
+
+ /**
+ * Generates an HMAC-SHA1 signature
+ *
+ * @param string $key
+ * @param string $message
+ * @return string
+ */
+ private function hmacsha1($key, $message) {
+
+ $blocksize=64;
+ if (strlen($key)>$blocksize)
+ $key=pack('H*', sha1($key));
+ $key=str_pad($key,$blocksize,chr(0x00));
+ $ipad=str_repeat(chr(0x36),$blocksize);
+ $opad=str_repeat(chr(0x5c),$blocksize);
+ $hmac = pack('H*',sha1(($key^$opad).pack('H*',sha1(($key^$ipad).$message))));
+ return $hmac;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/HTTP/AbstractAuth.php b/app/Gdoo/Calendar/Sabre/HTTP/AbstractAuth.php
new file mode 100644
index 00000000..b5583b87
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/HTTP/AbstractAuth.php
@@ -0,0 +1,111 @@
+httpResponse = new Response();
+ $this->httpRequest = new Request();
+
+ }
+
+ /**
+ * Sets an alternative HTTP response object
+ *
+ * @param Response $response
+ * @return void
+ */
+ public function setHTTPResponse(Response $response) {
+
+ $this->httpResponse = $response;
+
+ }
+
+ /**
+ * Sets an alternative HTTP request object
+ *
+ * @param Request $request
+ * @return void
+ */
+ public function setHTTPRequest(Request $request) {
+
+ $this->httpRequest = $request;
+
+ }
+
+
+ /**
+ * Sets the realm
+ *
+ * The realm is often displayed in authentication dialog boxes
+ * Commonly an application name displayed here
+ *
+ * @param string $realm
+ * @return void
+ */
+ public function setRealm($realm) {
+
+ $this->realm = $realm;
+
+ }
+
+ /**
+ * Returns the realm
+ *
+ * @return string
+ */
+ public function getRealm() {
+
+ return $this->realm;
+
+ }
+
+ /**
+ * Returns an HTTP 401 header, forcing login
+ *
+ * This should be called when username and password are incorrect, or not supplied at all
+ *
+ * @return void
+ */
+ abstract public function requireLogin();
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/HTTP/BasicAuth.php b/app/Gdoo/Calendar/Sabre/HTTP/BasicAuth.php
new file mode 100644
index 00000000..18fe1638
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/HTTP/BasicAuth.php
@@ -0,0 +1,67 @@
+httpRequest->getRawServerValue('PHP_AUTH_USER'))!==null && ($pass = $this->httpRequest->getRawServerValue('PHP_AUTH_PW'))!==null) {
+
+ return array($user,$pass);
+
+ }
+
+ // Most other webservers
+ $auth = $this->httpRequest->getHeader('Authorization');
+
+ // Apache could prefix environment variables with REDIRECT_ when urls
+ // are passed through mod_rewrite
+ if (!$auth) {
+ $auth = $this->httpRequest->getRawServerValue('REDIRECT_HTTP_AUTHORIZATION');
+ }
+
+ if (!$auth) return false;
+
+ if (strpos(strtolower($auth),'basic')!==0) return false;
+
+ return explode(':', base64_decode(substr($auth, 6)),2);
+
+ }
+
+ /**
+ * Returns an HTTP 401 header, forcing login
+ *
+ * This should be called when username and password are incorrect, or not supplied at all
+ *
+ * @return void
+ */
+ public function requireLogin() {
+
+ $this->httpResponse->setHeader('WWW-Authenticate','Basic realm="' . $this->realm . '"');
+ $this->httpResponse->sendStatus(401);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/HTTP/DigestAuth.php b/app/Gdoo/Calendar/Sabre/HTTP/DigestAuth.php
new file mode 100644
index 00000000..c1527393
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/HTTP/DigestAuth.php
@@ -0,0 +1,240 @@
+nonce = uniqid();
+ $this->opaque = md5($this->realm);
+ parent::__construct();
+
+ }
+
+ /**
+ * Gathers all information from the headers
+ *
+ * This method needs to be called prior to anything else.
+ *
+ * @return void
+ */
+ public function init() {
+
+ $digest = $this->getDigest();
+ $this->digestParts = $this->parseDigest($digest);
+
+ }
+
+ /**
+ * Sets the quality of protection value.
+ *
+ * Possible values are:
+ * Sabre\HTTP\DigestAuth::QOP_AUTH
+ * Sabre\HTTP\DigestAuth::QOP_AUTHINT
+ *
+ * Multiple values can be specified using logical OR.
+ *
+ * QOP_AUTHINT ensures integrity of the request body, but this is not
+ * supported by most HTTP clients. QOP_AUTHINT also requires the entire
+ * request body to be md5'ed, which can put strains on CPU and memory.
+ *
+ * @param int $qop
+ * @return void
+ */
+ public function setQOP($qop) {
+
+ $this->qop = $qop;
+
+ }
+
+ /**
+ * Validates the user.
+ *
+ * The A1 parameter should be md5($username . ':' . $realm . ':' . $password);
+ *
+ * @param string $A1
+ * @return bool
+ */
+ public function validateA1($A1) {
+
+ $this->A1 = $A1;
+ return $this->validate();
+
+ }
+
+ /**
+ * Validates authentication through a password. The actual password must be provided here.
+ * It is strongly recommended not store the password in plain-text and use validateA1 instead.
+ *
+ * @param string $password
+ * @return bool
+ */
+ public function validatePassword($password) {
+
+ $this->A1 = md5($this->digestParts['username'] . ':' . $this->realm . ':' . $password);
+ return $this->validate();
+
+ }
+
+ /**
+ * Returns the username for the request
+ *
+ * @return string
+ */
+ public function getUsername() {
+
+ return $this->digestParts['username'];
+
+ }
+
+ /**
+ * Validates the digest challenge
+ *
+ * @return bool
+ */
+ protected function validate() {
+
+ $A2 = $this->httpRequest->getMethod() . ':' . $this->digestParts['uri'];
+
+ if ($this->digestParts['qop']=='auth-int') {
+ // Making sure we support this qop value
+ if (!($this->qop & self::QOP_AUTHINT)) return false;
+ // We need to add an md5 of the entire request body to the A2 part of the hash
+ $body = $this->httpRequest->getBody(true);
+ $this->httpRequest->setBody($body,true);
+ $A2 .= ':' . md5($body);
+ } else {
+
+ // We need to make sure we support this qop value
+ if (!($this->qop & self::QOP_AUTH)) return false;
+ }
+
+ $A2 = md5($A2);
+
+ $validResponse = md5("{$this->A1}:{$this->digestParts['nonce']}:{$this->digestParts['nc']}:{$this->digestParts['cnonce']}:{$this->digestParts['qop']}:{$A2}");
+
+ return $this->digestParts['response']==$validResponse;
+
+
+ }
+
+ /**
+ * Returns an HTTP 401 header, forcing login
+ *
+ * This should be called when username and password are incorrect, or not supplied at all
+ *
+ * @return void
+ */
+ public function requireLogin() {
+
+ $qop = '';
+ switch($this->qop) {
+ case self::QOP_AUTH : $qop = 'auth'; break;
+ case self::QOP_AUTHINT : $qop = 'auth-int'; break;
+ case self::QOP_AUTH | self::QOP_AUTHINT : $qop = 'auth,auth-int'; break;
+ }
+
+ $this->httpResponse->setHeader('WWW-Authenticate','Digest realm="' . $this->realm . '",qop="'.$qop.'",nonce="' . $this->nonce . '",opaque="' . $this->opaque . '"');
+ $this->httpResponse->sendStatus(401);
+
+ }
+
+
+ /**
+ * This method returns the full digest string.
+ *
+ * It should be compatibile with mod_php format and other webservers.
+ *
+ * If the header could not be found, null will be returned
+ *
+ * @return mixed
+ */
+ public function getDigest() {
+
+ // mod_php
+ $digest = $this->httpRequest->getRawServerValue('PHP_AUTH_DIGEST');
+ if ($digest) return $digest;
+
+ // most other servers
+ $digest = $this->httpRequest->getHeader('Authorization');
+
+ // Apache could prefix environment variables with REDIRECT_ when urls
+ // are passed through mod_rewrite
+ if (!$digest) {
+ $digest = $this->httpRequest->getRawServerValue('REDIRECT_HTTP_AUTHORIZATION');
+ }
+
+ if ($digest && strpos(strtolower($digest),'digest')===0) {
+ return substr($digest,7);
+ } else {
+ return null;
+ }
+
+ }
+
+
+ /**
+ * Parses the different pieces of the digest string into an array.
+ *
+ * This method returns false if an incomplete digest was supplied
+ *
+ * @param string $digest
+ * @return mixed
+ */
+ protected function parseDigest($digest) {
+
+ // protect against missing data
+ $needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
+ $data = array();
+
+ preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', $digest, $matches, PREG_SET_ORDER);
+
+ foreach ($matches as $m) {
+ $data[$m[1]] = $m[2] ? $m[2] : $m[3];
+ unset($needed_parts[$m[1]]);
+ }
+
+ return $needed_parts ? false : $data;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/HTTP/Request.php b/app/Gdoo/Calendar/Sabre/HTTP/Request.php
new file mode 100644
index 00000000..e8baca04
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/HTTP/Request.php
@@ -0,0 +1,284 @@
+_SERVER = $serverData;
+ else $this->_SERVER =& $_SERVER;
+
+ if ($postData) $this->_POST = $postData;
+ else $this->_POST =& $_POST;
+
+ }
+
+ /**
+ * Returns the value for a specific http header.
+ *
+ * This method returns null if the header did not exist.
+ *
+ * @param string $name
+ * @return string
+ */
+ public function getHeader($name) {
+
+ $name = strtoupper(str_replace(array('-'),array('_'),$name));
+ if (isset($this->_SERVER['HTTP_' . $name])) {
+ return $this->_SERVER['HTTP_' . $name];
+ }
+
+ // There's a few headers that seem to end up in the top-level
+ // server array.
+ switch($name) {
+ case 'CONTENT_TYPE' :
+ case 'CONTENT_LENGTH' :
+ if (isset($this->_SERVER[$name])) {
+ return $this->_SERVER[$name];
+ }
+ break;
+
+ }
+ return;
+
+ }
+
+ /**
+ * Returns all (known) HTTP headers.
+ *
+ * All headers are converted to lower-case, and additionally all underscores
+ * are automatically converted to dashes
+ *
+ * @return array
+ */
+ public function getHeaders() {
+
+ $hdrs = array();
+ foreach($this->_SERVER as $key=>$value) {
+
+ switch($key) {
+ case 'CONTENT_LENGTH' :
+ case 'CONTENT_TYPE' :
+ $hdrs[strtolower(str_replace('_','-',$key))] = $value;
+ break;
+ default :
+ if (strpos($key,'HTTP_')===0) {
+ $hdrs[substr(strtolower(str_replace('_','-',$key)),5)] = $value;
+ }
+ break;
+ }
+
+ }
+
+ return $hdrs;
+
+ }
+
+ /**
+ * Returns the HTTP request method
+ *
+ * This is for example POST or GET
+ *
+ * @return string
+ */
+ public function getMethod() {
+
+ return $this->_SERVER['REQUEST_METHOD'];
+
+ }
+
+ /**
+ * Returns the requested uri
+ *
+ * @return string
+ */
+ public function getUri() {
+
+ return $this->_SERVER['REQUEST_URI'];
+
+ }
+
+ /**
+ * Will return protocol + the hostname + the uri
+ *
+ * @return string
+ */
+ public function getAbsoluteUri() {
+
+ // Checking if the request was made through HTTPS. The last in line is for IIS
+ $protocol = isset($this->_SERVER['HTTPS']) && ($this->_SERVER['HTTPS']) && ($this->_SERVER['HTTPS']!='off');
+ return ($protocol?'https':'http') . '://' . $this->getHeader('Host') . $this->getUri();
+
+ }
+
+ /**
+ * Returns everything after the ? from the current url
+ *
+ * @return string
+ */
+ public function getQueryString() {
+
+ return isset($this->_SERVER['QUERY_STRING'])?$this->_SERVER['QUERY_STRING']:'';
+
+ }
+
+ /**
+ * Returns the HTTP request body body
+ *
+ * This method returns a readable stream resource.
+ * If the asString parameter is set to true, a string is sent instead.
+ *
+ * @param bool $asString
+ * @return resource
+ */
+ public function getBody($asString = false) {
+
+ if (is_null($this->body)) {
+ if (!is_null(self::$defaultInputStream)) {
+ $this->body = self::$defaultInputStream;
+ } else {
+ $this->body = fopen('php://input','r');
+ self::$defaultInputStream = $this->body;
+ }
+ }
+ if ($asString) {
+ $body = stream_get_contents($this->body);
+ return $body;
+ } else {
+ return $this->body;
+ }
+
+ }
+
+ /**
+ * Sets the contents of the HTTP request body
+ *
+ * This method can either accept a string, or a readable stream resource.
+ *
+ * If the setAsDefaultInputStream is set to true, it means for this run of the
+ * script the supplied body will be used instead of php://input.
+ *
+ * @param mixed $body
+ * @param bool $setAsDefaultInputStream
+ * @return void
+ */
+ public function setBody($body,$setAsDefaultInputStream = false) {
+
+ if(is_resource($body)) {
+ $this->body = $body;
+ } else {
+
+ $stream = fopen('php://temp','r+');
+ fputs($stream,$body);
+ rewind($stream);
+ // String is assumed
+ $this->body = $stream;
+ }
+ if ($setAsDefaultInputStream) {
+ self::$defaultInputStream = $this->body;
+ }
+
+ }
+
+ /**
+ * Returns PHP's _POST variable.
+ *
+ * The reason this is in a method is so it can be subclassed and
+ * overridden.
+ *
+ * @return array
+ */
+ public function getPostVars() {
+
+ return $this->_POST;
+
+ }
+
+ /**
+ * Returns a specific item from the _SERVER array.
+ *
+ * Do not rely on this feature, it is for internal use only.
+ *
+ * @param string $field
+ * @return string
+ */
+ public function getRawServerValue($field) {
+
+ return isset($this->_SERVER[$field])?$this->_SERVER[$field]:null;
+
+ }
+
+ /**
+ * Returns the HTTP version specified within the request.
+ *
+ * @return string
+ */
+ public function getHTTPVersion() {
+
+ $protocol = $this->getRawServerValue('SERVER_PROTOCOL');
+ if ($protocol==='HTTP/1.0') {
+ return '1.0';
+ } else {
+ return '1.1';
+ }
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/HTTP/Response.php b/app/Gdoo/Calendar/Sabre/HTTP/Response.php
new file mode 100644
index 00000000..d53ddb41
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/HTTP/Response.php
@@ -0,0 +1,175 @@
+ 'Continue',
+ 101 => 'Switching Protocols',
+ 102 => 'Processing',
+ 200 => 'OK',
+ 201 => 'Created',
+ 202 => 'Accepted',
+ 203 => 'Non-Authorative Information',
+ 204 => 'No Content',
+ 205 => 'Reset Content',
+ 206 => 'Partial Content',
+ 207 => 'Multi-Status', // RFC 4918
+ 208 => 'Already Reported', // RFC 5842
+ 226 => 'IM Used', // RFC 3229
+ 300 => 'Multiple Choices',
+ 301 => 'Moved Permanently',
+ 302 => 'Found',
+ 303 => 'See Other',
+ 304 => 'Not Modified',
+ 305 => 'Use Proxy',
+ 306 => 'Reserved',
+ 307 => 'Temporary Redirect',
+ 400 => 'Bad request',
+ 401 => 'Unauthorized',
+ 402 => 'Payment Required',
+ 403 => 'Forbidden',
+ 404 => 'Not Found',
+ 405 => 'Method Not Allowed',
+ 406 => 'Not Acceptable',
+ 407 => 'Proxy Authentication Required',
+ 408 => 'Request Timeout',
+ 409 => 'Conflict',
+ 410 => 'Gone',
+ 411 => 'Length Required',
+ 412 => 'Precondition failed',
+ 413 => 'Request Entity Too Large',
+ 414 => 'Request-URI Too Long',
+ 415 => 'Unsupported Media Type',
+ 416 => 'Requested Range Not Satisfiable',
+ 417 => 'Expectation Failed',
+ 418 => 'I\'m a teapot', // RFC 2324
+ 422 => 'Unprocessable Entity', // RFC 4918
+ 423 => 'Locked', // RFC 4918
+ 424 => 'Failed Dependency', // RFC 4918
+ 426 => 'Upgrade required',
+ 428 => 'Precondition required', // draft-nottingham-http-new-status
+ 429 => 'Too Many Requests', // draft-nottingham-http-new-status
+ 431 => 'Request Header Fields Too Large', // draft-nottingham-http-new-status
+ 500 => 'Internal Server Error',
+ 501 => 'Not Implemented',
+ 502 => 'Bad Gateway',
+ 503 => 'Service Unavailable',
+ 504 => 'Gateway Timeout',
+ 505 => 'HTTP Version not supported',
+ 506 => 'Variant Also Negotiates',
+ 507 => 'Insufficient Storage', // RFC 4918
+ 508 => 'Loop Detected', // RFC 5842
+ 509 => 'Bandwidth Limit Exceeded', // non-standard
+ 510 => 'Not extended',
+ 511 => 'Network Authentication Required', // draft-nottingham-http-new-status
+ );
+
+ return 'HTTP/' . $httpVersion . ' ' . $code . ' ' . $msg[$code];
+
+ }
+
+ // @codeCoverageIgnoreStart
+ // We cannot reasonably test header() related methods.
+
+ /**
+ * Sends an HTTP status header to the client.
+ *
+ * @param int $code HTTP status code
+ * @return bool
+ */
+ public function sendStatus($code) {
+
+ if (!headers_sent())
+ return header($this->getStatusMessage($code, $this->defaultHttpVersion));
+ else return false;
+
+ }
+
+ /**
+ * Sets an HTTP header for the response
+ *
+ * @param string $name
+ * @param string $value
+ * @param bool $replace
+ * @return bool
+ */
+ public function setHeader($name, $value, $replace = true) {
+
+ $value = str_replace(array("\r","\n"),array('\r','\n'),$value);
+ if (!headers_sent())
+ return header($name . ': ' . $value, $replace);
+ else return false;
+
+
+ }
+ // @codeCoverageIgnoreEnd
+
+ /**
+ * Sets a bunch of HTTP Headers
+ *
+ * headersnames are specified as keys, value in the array value
+ *
+ * @param array $headers
+ * @return void
+ */
+ public function setHeaders(array $headers) {
+
+ foreach($headers as $key=>$value)
+ $this->setHeader($key, $value);
+
+ }
+
+ /**
+ * Sends the entire response body
+ *
+ * This method can accept either an open filestream, or a string.
+ *
+ * @param mixed $body
+ * @return void
+ */
+ public function sendBody($body) {
+
+ if (is_resource($body)) {
+
+ file_put_contents('php://output', $body);
+
+ } else {
+
+ // We assume a string
+ echo $body;
+
+ }
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/HTTP/Util.php b/app/Gdoo/Calendar/Sabre/HTTP/Util.php
new file mode 100644
index 00000000..f03643de
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/HTTP/Util.php
@@ -0,0 +1,82 @@
+= 0)
+ return new \DateTime('@' . $realDate, new \DateTimeZone('UTC'));
+
+ }
+
+ /**
+ * Transforms a DateTime object to HTTP's most common date format.
+ *
+ * We're serializing it as the RFC 1123 date, which, for HTTP must be
+ * specified as GMT.
+ *
+ * @param \DateTime $dateTime
+ * @return string
+ */
+ static function toHTTPDate(\DateTime $dateTime) {
+
+ // We need to clone it, as we don't want to affect the existing
+ // DateTime.
+ $dateTime = clone $dateTime;
+ $dateTime->setTimeZone(new \DateTimeZone('GMT'));
+ return $dateTime->format('D, d M Y H:i:s \G\M\T');
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/HTTP/Version.php b/app/Gdoo/Calendar/Sabre/HTTP/Version.php
new file mode 100644
index 00000000..5936b577
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/HTTP/Version.php
@@ -0,0 +1,24 @@
+ 'Sabre\\VObject\\Component\\VAlarm',
+ 'VCALENDAR' => 'Sabre\\VObject\\Component\\VCalendar',
+ 'VCARD' => 'Sabre\\VObject\\Component\\VCard',
+ 'VEVENT' => 'Sabre\\VObject\\Component\\VEvent',
+ 'VJOURNAL' => 'Sabre\\VObject\\Component\\VJournal',
+ 'VTODO' => 'Sabre\\VObject\\Component\\VTodo',
+ 'VFREEBUSY' => 'Sabre\\VObject\\Component\\VFreeBusy',
+ );
+
+ /**
+ * Creates the new component by name, but in addition will also see if
+ * there's a class mapped to the property name.
+ *
+ * @param string $name
+ * @param string $value
+ * @return Component
+ */
+ static public function create($name, $value = null) {
+
+ $name = strtoupper($name);
+
+ if (isset(self::$classMap[$name])) {
+ return new self::$classMap[$name]($name, $value);
+ } else {
+ return new self($name, $value);
+ }
+
+ }
+
+ /**
+ * Creates a new component.
+ *
+ * By default this object will iterate over its own children, but this can
+ * be overridden with the iterator argument
+ *
+ * @param string $name
+ * @param ElementList $iterator
+ */
+ public function __construct($name, ElementList $iterator = null) {
+
+ $this->name = strtoupper($name);
+ if (!is_null($iterator)) $this->iterator = $iterator;
+
+ }
+
+ /**
+ * Turns the object back into a serialized blob.
+ *
+ * @return string
+ */
+ public function serialize() {
+
+ $str = "BEGIN:" . $this->name . "\r\n";
+
+ /**
+ * Gives a component a 'score' for sorting purposes.
+ *
+ * This is solely used by the childrenSort method.
+ *
+ * A higher score means the item will be lower in the list.
+ * To avoid score collisions, each "score category" has a reasonable
+ * space to accomodate elements. The $key is added to the $score to
+ * preserve the original relative order of elements.
+ *
+ * @param int $key
+ * @param array $array
+ * @return int
+ */
+ $sortScore = function($key, $array) {
+
+ if ($array[$key] instanceof Component) {
+
+ // We want to encode VTIMEZONE first, this is a personal
+ // preference.
+ if ($array[$key]->name === 'VTIMEZONE') {
+ $score=300000000;
+ return $score+$key;
+ } else {
+ $score=400000000;
+ return $score+$key;
+ }
+ } else {
+ // Properties get encoded first
+ // VCARD version 4.0 wants the VERSION property to appear first
+ if ($array[$key] instanceof Property) {
+ if ($array[$key]->name === 'VERSION') {
+ $score=100000000;
+ return $score+$key;
+ } else {
+ // All other properties
+ $score=200000000;
+ return $score+$key;
+ }
+ }
+ }
+
+ };
+
+ $tmp = $this->children;
+ uksort($this->children, function($a, $b) use ($sortScore, $tmp) {
+
+ $sA = $sortScore($a, $tmp);
+ $sB = $sortScore($b, $tmp);
+
+ if ($sA === $sB) return 0;
+
+ return ($sA < $sB) ? -1 : 1;
+
+ });
+
+ foreach($this->children as $child) $str.=$child->serialize();
+ $str.= "END:" . $this->name . "\r\n";
+
+ return $str;
+
+ }
+
+ /**
+ * Adds a new component or element
+ *
+ * You can call this method with the following syntaxes:
+ *
+ * add(Node $node)
+ * add(string $name, $value, array $parameters = array())
+ *
+ * The first version adds an Element
+ * The second adds a property as a string.
+ *
+ * @param mixed $item
+ * @param mixed $itemValue
+ * @return void
+ */
+ public function add($item, $itemValue = null, array $parameters = array()) {
+
+ if ($item instanceof Node) {
+ if (!is_null($itemValue)) {
+ throw new \InvalidArgumentException('The second argument must not be specified, when passing a VObject Node');
+ }
+ $item->parent = $this;
+ $this->children[] = $item;
+ } elseif(is_string($item)) {
+
+ $item = Property::create($item,$itemValue, $parameters);
+ $item->parent = $this;
+ $this->children[] = $item;
+
+ } else {
+
+ throw new \InvalidArgumentException('The first argument must either be a \\Sabre\\VObject\\Node or a string');
+
+ }
+
+ }
+
+ /**
+ * Returns an iterable list of children
+ *
+ * @return ElementList
+ */
+ public function children() {
+
+ return new ElementList($this->children);
+
+ }
+
+ /**
+ * Returns an array with elements that match the specified name.
+ *
+ * This function is also aware of MIME-Directory groups (as they appear in
+ * vcards). This means that if a property is grouped as "HOME.EMAIL", it
+ * will also be returned when searching for just "EMAIL". If you want to
+ * search for a property in a specific group, you can select on the entire
+ * string ("HOME.EMAIL"). If you want to search on a specific property that
+ * has not been assigned a group, specify ".EMAIL".
+ *
+ * Keys are retained from the 'children' array, which may be confusing in
+ * certain cases.
+ *
+ * @param string $name
+ * @return array
+ */
+ public function select($name) {
+
+ $group = null;
+ $name = strtoupper($name);
+ if (strpos($name,'.')!==false) {
+ list($group,$name) = explode('.', $name, 2);
+ }
+
+ $result = array();
+ foreach($this->children as $key=>$child) {
+
+ if (
+ strtoupper($child->name) === $name &&
+ (is_null($group) || ( $child instanceof Property && strtoupper($child->group) === $group))
+ ) {
+
+ $result[$key] = $child;
+
+ }
+ }
+
+ reset($result);
+ return $result;
+
+ }
+
+ /**
+ * This method only returns a list of sub-components. Properties are
+ * ignored.
+ *
+ * @return array
+ */
+ public function getComponents() {
+
+ $result = array();
+ foreach($this->children as $child) {
+ if ($child instanceof Component) {
+ $result[] = $child;
+ }
+ }
+
+ return $result;
+
+ }
+
+ /**
+ * Validates the node for correctness.
+ *
+ * The following options are supported:
+ * - Node::REPAIR - If something is broken, and automatic repair may
+ * be attempted.
+ *
+ * An array is returned with warnings.
+ *
+ * Every item in the array has the following properties:
+ * * level - (number between 1 and 3 with severity information)
+ * * message - (human readable message)
+ * * node - (reference to the offending node)
+ *
+ * @param int $options
+ * @return array
+ */
+ public function validate($options = 0) {
+
+ $result = array();
+ foreach($this->children as $child) {
+ $result = array_merge($result, $child->validate($options));
+ }
+ return $result;
+
+ }
+
+ /* Magic property accessors {{{ */
+
+ /**
+ * Using 'get' you will either get a property or component,
+ *
+ * If there were no child-elements found with the specified name,
+ * null is returned.
+ *
+ * @param string $name
+ * @return Property
+ */
+ public function __get($name) {
+
+ $matches = $this->select($name);
+ if (count($matches)===0) {
+ return null;
+ } else {
+ $firstMatch = current($matches);
+ /** @var $firstMatch Property */
+ $firstMatch->setIterator(new ElementList(array_values($matches)));
+ return $firstMatch;
+ }
+
+ }
+
+ /**
+ * This method checks if a sub-element with the specified name exists.
+ *
+ * @param string $name
+ * @return bool
+ */
+ public function __isset($name) {
+
+ $matches = $this->select($name);
+ return count($matches)>0;
+
+ }
+
+ /**
+ * Using the setter method you can add properties or subcomponents
+ *
+ * You can either pass a Component, Property
+ * object, or a string to automatically create a Property.
+ *
+ * If the item already exists, it will be removed. If you want to add
+ * a new item with the same name, always use the add() method.
+ *
+ * @param string $name
+ * @param mixed $value
+ * @return void
+ */
+ public function __set($name, $value) {
+
+ $matches = $this->select($name);
+ $overWrite = count($matches)?key($matches):null;
+
+ if (is_null($value)) {
+ return;
+ }
+
+ if ($value instanceof Component || $value instanceof Property) {
+ $value->parent = $this;
+ if (!is_null($overWrite)) {
+ $this->children[$overWrite] = $value;
+ } else {
+ $this->children[] = $value;
+ }
+ } elseif (is_scalar($value)) {
+ $property = Property::create($name,$value);
+ $property->parent = $this;
+ if (!is_null($overWrite)) {
+ $this->children[$overWrite] = $property;
+ } else {
+ $this->children[] = $property;
+ }
+ } else {
+ throw new \InvalidArgumentException('You must pass a \\Sabre\\VObject\\Component, \\Sabre\\VObject\\Property or scalar type');
+ }
+
+ }
+
+ /**
+ * Removes all properties and components within this component.
+ *
+ * @param string $name
+ * @return void
+ */
+ public function __unset($name) {
+
+ $matches = $this->select($name);
+ foreach($matches as $k=>$child) {
+
+ unset($this->children[$k]);
+ $child->parent = null;
+
+ }
+
+ }
+
+ /* }}} */
+
+ /**
+ * This method is automatically called when the object is cloned.
+ * Specifically, this will ensure all child elements are also cloned.
+ *
+ * @return void
+ */
+ public function __clone() {
+
+ foreach($this->children as $key=>$child) {
+ $this->children[$key] = clone $child;
+ $this->children[$key]->parent = $this;
+ }
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/VObject/Component/VAlarm.php b/app/Gdoo/Calendar/Sabre/VObject/Component/VAlarm.php
new file mode 100644
index 00000000..bfe97c3a
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/Component/VAlarm.php
@@ -0,0 +1,108 @@
+TRIGGER;
+ if(!isset($trigger['VALUE']) || strtoupper($trigger['VALUE']) === 'DURATION') {
+ $triggerDuration = VObject\DateTimeParser::parseDuration($this->TRIGGER);
+ $related = (isset($trigger['RELATED']) && strtoupper($trigger['RELATED']) == 'END') ? 'END' : 'START';
+
+ $parentComponent = $this->parent;
+ if ($related === 'START') {
+
+ if ($parentComponent->name === 'VTODO') {
+ $propName = 'DUE';
+ } else {
+ $propName = 'DTSTART';
+ }
+
+ $effectiveTrigger = clone $parentComponent->$propName->getDateTime();
+ $effectiveTrigger->add($triggerDuration);
+ } else {
+ if ($parentComponent->name === 'VTODO') {
+ $endProp = 'DUE';
+ } elseif ($parentComponent->name === 'VEVENT') {
+ $endProp = 'DTEND';
+ } else {
+ throw new \LogicException('time-range filters on VALARM components are only supported when they are a child of VTODO or VEVENT');
+ }
+
+ if (isset($parentComponent->$endProp)) {
+ $effectiveTrigger = clone $parentComponent->$endProp->getDateTime();
+ $effectiveTrigger->add($triggerDuration);
+ } elseif (isset($parentComponent->DURATION)) {
+ $effectiveTrigger = clone $parentComponent->DTSTART->getDateTime();
+ $duration = VObject\DateTimeParser::parseDuration($parentComponent->DURATION);
+ $effectiveTrigger->add($duration);
+ $effectiveTrigger->add($triggerDuration);
+ } else {
+ $effectiveTrigger = clone $parentComponent->DTSTART->getDateTime();
+ $effectiveTrigger->add($triggerDuration);
+ }
+ }
+ } else {
+ $effectiveTrigger = $trigger->getDateTime();
+ }
+ return $effectiveTrigger;
+
+ }
+
+ /**
+ * Returns true or false depending on if the event falls in the specified
+ * time-range. This is used for filtering purposes.
+ *
+ * The rules used to determine if an event falls within the specified
+ * time-range is based on the CalDAV specification.
+ *
+ * @param \DateTime $start
+ * @param \DateTime $end
+ * @return bool
+ */
+ public function isInTimeRange(\DateTime $start, \DateTime $end) {
+
+ $effectiveTrigger = $this->getEffectiveTriggerTime();
+
+ if (isset($this->DURATION)) {
+ $duration = VObject\DateTimeParser::parseDuration($this->DURATION);
+ $repeat = (string)$this->repeat;
+ if (!$repeat) {
+ $repeat = 1;
+ }
+
+ $period = new \DatePeriod($effectiveTrigger, $duration, (int)$repeat);
+
+ foreach($period as $occurrence) {
+
+ if ($start <= $occurrence && $end > $occurrence) {
+ return true;
+ }
+ }
+ return false;
+ } else {
+ return ($start <= $effectiveTrigger && $end > $effectiveTrigger);
+ }
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/VObject/Component/VCalendar.php b/app/Gdoo/Calendar/Sabre/VObject/Component/VCalendar.php
new file mode 100644
index 00000000..77917a17
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/Component/VCalendar.php
@@ -0,0 +1,244 @@
+children as $component) {
+
+ if (!$component instanceof VObject\Component)
+ continue;
+
+ if (isset($component->{'RECURRENCE-ID'}))
+ continue;
+
+ if ($componentName && $component->name !== strtoupper($componentName))
+ continue;
+
+ if ($component->name === 'VTIMEZONE')
+ continue;
+
+ $components[] = $component;
+
+ }
+
+ return $components;
+
+ }
+
+ /**
+ * If this calendar object, has events with recurrence rules, this method
+ * can be used to expand the event into multiple sub-events.
+ *
+ * Each event will be stripped from it's recurrence information, and only
+ * the instances of the event in the specified timerange will be left
+ * alone.
+ *
+ * In addition, this method will cause timezone information to be stripped,
+ * and normalized to UTC.
+ *
+ * This method will alter the VCalendar. This cannot be reversed.
+ *
+ * This functionality is specifically used by the CalDAV standard. It is
+ * possible for clients to request expand events, if they are rather simple
+ * clients and do not have the possibility to calculate recurrences.
+ *
+ * @param DateTime $start
+ * @param DateTime $end
+ * @return void
+ */
+ public function expand(\DateTime $start, \DateTime $end) {
+
+ $newEvents = array();
+
+ foreach($this->select('VEVENT') as $key=>$vevent) {
+
+ if (isset($vevent->{'RECURRENCE-ID'})) {
+ unset($this->children[$key]);
+ continue;
+ }
+
+
+ if (!$vevent->rrule) {
+ unset($this->children[$key]);
+ if ($vevent->isInTimeRange($start, $end)) {
+ $newEvents[] = $vevent;
+ }
+ continue;
+ }
+
+ $uid = (string)$vevent->uid;
+ if (!$uid) {
+ throw new \LogicException('Event did not have a UID!');
+ }
+
+ $it = new VObject\RecurrenceIterator($this, $vevent->uid);
+ $it->fastForward($start);
+
+ while($it->valid() && $it->getDTStart() < $end) {
+
+ if ($it->getDTEnd() > $start) {
+
+ $newEvents[] = $it->getEventObject();
+
+ }
+ $it->next();
+
+ }
+ unset($this->children[$key]);
+
+ }
+
+ foreach($newEvents as $newEvent) {
+
+ foreach($newEvent->children as $child) {
+ if ($child instanceof VObject\Property\DateTime &&
+ $child->getDateType() == VObject\Property\DateTime::LOCALTZ) {
+ $child->setDateTime($child->getDateTime(),VObject\Property\DateTime::UTC);
+ }
+ }
+
+ $this->add($newEvent);
+
+ }
+
+ // Removing all VTIMEZONE components
+ unset($this->VTIMEZONE);
+
+ }
+
+ /**
+ * Validates the node for correctness.
+ * An array is returned with warnings.
+ *
+ * Every item in the array has the following properties:
+ * * level - (number between 1 and 3 with severity information)
+ * * message - (human readable message)
+ * * node - (reference to the offending node)
+ *
+ * @return array
+ */
+ /*
+ public function validate() {
+
+ $warnings = array();
+
+ $version = $this->select('VERSION');
+ if (count($version)!==1) {
+ $warnings[] = array(
+ 'level' => 1,
+ 'message' => 'The VERSION property must appear in the VCALENDAR component exactly 1 time',
+ 'node' => $this,
+ );
+ } else {
+ if ((string)$this->VERSION !== '2.0') {
+ $warnings[] = array(
+ 'level' => 1,
+ 'message' => 'Only iCalendar version 2.0 as defined in rfc5545 is supported.',
+ 'node' => $this,
+ );
+ }
+ }
+ $version = $this->select('PRODID');
+ if (count($version)!==1) {
+ $warnings[] = array(
+ 'level' => 2,
+ 'message' => 'The PRODID property must appear in the VCALENDAR component exactly 1 time',
+ 'node' => $this,
+ );
+ }
+ if (count($this->CALSCALE) > 1) {
+ $warnings[] = array(
+ 'level' => 2,
+ 'message' => 'The CALSCALE property must not be specified more than once.',
+ 'node' => $this,
+ );
+ }
+ if (count($this->METHOD) > 1) {
+ $warnings[] = array(
+ 'level' => 2,
+ 'message' => 'The METHOD property must not be specified more than once.',
+ 'node' => $this,
+ );
+ }
+
+ $allowedComponents = array(
+ 'VEVENT',
+ 'VTODO',
+ 'VJOURNAL',
+ 'VFREEBUSY',
+ 'VTIMEZONE',
+ );
+ $allowedProperties = array(
+ 'PRODID',
+ 'VERSION',
+ 'CALSCALE',
+ 'METHOD',
+ );
+ $componentsFound = 0;
+ foreach($this->children as $child) {
+ if($child instanceof Component) {
+ $componentsFound++;
+ if (!in_array($child->name, $allowedComponents)) {
+ $warnings[] = array(
+ 'level' => 1,
+ 'message' => 'The ' . $child->name . " component is not allowed in the VCALENDAR component",
+ 'node' => $this,
+ );
+ }
+ }
+ if ($child instanceof Property) {
+ if (!in_array($child->name, $allowedProperties)) {
+ $warnings[] = array(
+ 'level' => 2,
+ 'message' => 'The ' . $child->name . " property is not allowed in the VCALENDAR component",
+ 'node' => $this,
+ );
+ }
+ }
+ }
+
+ if ($componentsFound===0) {
+ $warnings[] = array(
+ 'level' => 1,
+ 'message' => 'An iCalendar object must have at least 1 component.',
+ 'node' => $this,
+ );
+ }
+
+ return array_merge(
+ $warnings,
+ parent::validate()
+ );
+
+ }
+ */
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/VObject/Component/VCard.php b/app/Gdoo/Calendar/Sabre/VObject/Component/VCard.php
new file mode 100644
index 00000000..ada90112
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/Component/VCard.php
@@ -0,0 +1,107 @@
+select('VERSION');
+ if (count($version)!==1) {
+ $warnings[] = array(
+ 'level' => 1,
+ 'message' => 'The VERSION property must appear in the VCARD component exactly 1 time',
+ 'node' => $this,
+ );
+ if ($options & self::REPAIR) {
+ $this->VERSION = self::DEFAULT_VERSION;
+ }
+ } else {
+ $version = (string)$this->VERSION;
+ if ($version!=='2.1' && $version!=='3.0' && $version!=='4.0') {
+ $warnings[] = array(
+ 'level' => 1,
+ 'message' => 'Only vcard version 4.0 (RFC6350), version 3.0 (RFC2426) or version 2.1 (icm-vcard-2.1) are supported.',
+ 'node' => $this,
+ );
+ if ($options & self::REPAIR) {
+ $this->VERSION = '4.0';
+ }
+ }
+
+ }
+ $fn = $this->select('FN');
+ if (count($fn)!==1) {
+ $warnings[] = array(
+ 'level' => 1,
+ 'message' => 'The FN property must appear in the VCARD component exactly 1 time',
+ 'node' => $this,
+ );
+ if (($options & self::REPAIR) && count($fn) === 0) {
+ // We're going to try to see if we can use the contents of the
+ // N property.
+ if (isset($this->N)) {
+ $value = explode(';', (string)$this->N);
+ if (isset($value[1]) && $value[1]) {
+ $this->FN = $value[1] . ' ' . $value[0];
+ } else {
+ $this->FN = $value[0];
+ }
+
+ // Otherwise, the ORG property may work
+ } elseif (isset($this->ORG)) {
+ $this->FN = (string)$this->ORG;
+ }
+
+ }
+ }
+
+ return array_merge(
+ parent::validate($options),
+ $warnings
+ );
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/VObject/Component/VEvent.php b/app/Gdoo/Calendar/Sabre/VObject/Component/VEvent.php
new file mode 100644
index 00000000..942c98b1
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/Component/VEvent.php
@@ -0,0 +1,70 @@
+RRULE) {
+ $it = new VObject\RecurrenceIterator($this);
+ $it->fastForward($start);
+
+ // We fast-forwarded to a spot where the end-time of the
+ // recurrence instance exceeded the start of the requested
+ // time-range.
+ //
+ // If the starttime of the recurrence did not exceed the
+ // end of the time range as well, we have a match.
+ return ($it->getDTStart() < $end && $it->getDTEnd() > $start);
+
+ }
+
+ $effectiveStart = $this->DTSTART->getDateTime();
+ if (isset($this->DTEND)) {
+
+ // The DTEND property is considered non inclusive. So for a 3 day
+ // event in july, dtstart and dtend would have to be July 1st and
+ // July 4th respectively.
+ //
+ // See:
+ // http://tools.ietf.org/html/rfc5545#page-54
+ $effectiveEnd = $this->DTEND->getDateTime();
+
+ } elseif (isset($this->DURATION)) {
+ $effectiveEnd = clone $effectiveStart;
+ $effectiveEnd->add( VObject\DateTimeParser::parseDuration($this->DURATION) );
+ } elseif ($this->DTSTART->getDateType() == VObject\Property\DateTime::DATE) {
+ $effectiveEnd = clone $effectiveStart;
+ $effectiveEnd->modify('+1 day');
+ } else {
+ $effectiveEnd = clone $effectiveStart;
+ }
+ return (
+ ($start <= $effectiveEnd) && ($end > $effectiveStart)
+ );
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/VObject/Component/VFreeBusy.php b/app/Gdoo/Calendar/Sabre/VObject/Component/VFreeBusy.php
new file mode 100644
index 00000000..e99cc95d
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/Component/VFreeBusy.php
@@ -0,0 +1,68 @@
+select('FREEBUSY') as $freebusy) {
+
+ // We are only interested in FBTYPE=BUSY (the default),
+ // FBTYPE=BUSY-TENTATIVE or FBTYPE=BUSY-UNAVAILABLE.
+ if (isset($freebusy['FBTYPE']) && strtoupper(substr((string)$freebusy['FBTYPE'],0,4))!=='BUSY') {
+ continue;
+ }
+
+ // The freebusy component can hold more than 1 value, separated by
+ // commas.
+ $periods = explode(',', (string)$freebusy);
+
+ foreach($periods as $period) {
+ // Every period is formatted as [start]/[end]. The start is an
+ // absolute UTC time, the end may be an absolute UTC time, or
+ // duration (relative) value.
+ list($busyStart, $busyEnd) = explode('/', $period);
+
+ $busyStart = VObject\DateTimeParser::parse($busyStart);
+ $busyEnd = VObject\DateTimeParser::parse($busyEnd);
+ if ($busyEnd instanceof \DateInterval) {
+ $tmp = clone $busyStart;
+ $tmp->add($busyEnd);
+ $busyEnd = $tmp;
+ }
+
+ if($start < $busyEnd && $end > $busyStart) {
+ return false;
+ }
+
+ }
+
+ }
+
+ return true;
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/VObject/Component/VJournal.php b/app/Gdoo/Calendar/Sabre/VObject/Component/VJournal.php
new file mode 100644
index 00000000..b9e39ca8
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/Component/VJournal.php
@@ -0,0 +1,46 @@
+DTSTART)?$this->DTSTART->getDateTime():null;
+ if ($dtstart) {
+ $effectiveEnd = clone $dtstart;
+ if ($this->DTSTART->getDateType() == VObject\Property\DateTime::DATE) {
+ $effectiveEnd->modify('+1 day');
+ }
+
+ return ($start <= $effectiveEnd && $end > $dtstart);
+
+ }
+ return false;
+
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/VObject/Component/VTodo.php b/app/Gdoo/Calendar/Sabre/VObject/Component/VTodo.php
new file mode 100644
index 00000000..c6cd73d0
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/Component/VTodo.php
@@ -0,0 +1,68 @@
+DTSTART)?$this->DTSTART->getDateTime():null;
+ $duration = isset($this->DURATION)?VObject\DateTimeParser::parseDuration($this->DURATION):null;
+ $due = isset($this->DUE)?$this->DUE->getDateTime():null;
+ $completed = isset($this->COMPLETED)?$this->COMPLETED->getDateTime():null;
+ $created = isset($this->CREATED)?$this->CREATED->getDateTime():null;
+
+ if ($dtstart) {
+ if ($duration) {
+ $effectiveEnd = clone $dtstart;
+ $effectiveEnd->add($duration);
+ return $start <= $effectiveEnd && $end > $dtstart;
+ } elseif ($due) {
+ return
+ ($start < $due || $start <= $dtstart) &&
+ ($end > $dtstart || $end >= $due);
+ } else {
+ return $start <= $dtstart && $end > $dtstart;
+ }
+ }
+ if ($due) {
+ return ($start < $due && $end >= $due);
+ }
+ if ($completed && $created) {
+ return
+ ($start <= $created || $start <= $completed) &&
+ ($end >= $created || $end >= $completed);
+ }
+ if ($completed) {
+ return ($start <= $completed && $end >= $completed);
+ }
+ if ($created) {
+ return ($end > $created);
+ }
+ return true;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/VObject/DateTimeParser.php b/app/Gdoo/Calendar/Sabre/VObject/DateTimeParser.php
new file mode 100644
index 00000000..03356d9d
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/DateTimeParser.php
@@ -0,0 +1,181 @@
+setTimeZone(new \DateTimeZone('UTC'));
+ return $date;
+
+ }
+
+ /**
+ * Parses an iCalendar (rfc5545) formatted date and returns a DateTime object
+ *
+ * @param string $date
+ * @return DateTime
+ */
+ static public function parseDate($date) {
+
+ // Format is YYYYMMDD
+ $result = preg_match('/^([1-4][0-9]{3})([0-1][0-9])([0-3][0-9])$/',$date,$matches);
+
+ if (!$result) {
+ throw new \LogicException('The supplied iCalendar date value is incorrect: ' . $date);
+ }
+
+ $date = new \DateTime($matches[1] . '-' . $matches[2] . '-' . $matches[3], new \DateTimeZone('UTC'));
+ return $date;
+
+ }
+
+ /**
+ * Parses an iCalendar (RFC5545) formatted duration value.
+ *
+ * This method will either return a DateTimeInterval object, or a string
+ * suitable for strtotime or DateTime::modify.
+ *
+ * @param string $duration
+ * @param bool $asString
+ * @return DateInterval|string
+ */
+ static public function parseDuration($duration, $asString = false) {
+
+ $result = preg_match('/^(?P\+|-)?P((?P\d+)W)?((?P\d+)D)?(T((?P\d+)H)?((?P\d+)M)?((?P\d+)S)?)?$/', $duration, $matches);
+ if (!$result) {
+ throw new \LogicException('The supplied iCalendar duration value is incorrect: ' . $duration);
+ }
+
+ if (!$asString) {
+ $invert = false;
+ if ($matches['plusminus']==='-') {
+ $invert = true;
+ }
+
+
+ $parts = array(
+ 'week',
+ 'day',
+ 'hour',
+ 'minute',
+ 'second',
+ );
+ foreach($parts as $part) {
+ $matches[$part] = isset($matches[$part])&&$matches[$part]?(int)$matches[$part]:0;
+ }
+
+
+ // We need to re-construct the $duration string, because weeks and
+ // days are not supported by DateInterval in the same string.
+ $duration = 'P';
+ $days = $matches['day'];
+ if ($matches['week']) {
+ $days+=$matches['week']*7;
+ }
+ if ($days)
+ $duration.=$days . 'D';
+
+ if ($matches['minute'] || $matches['second'] || $matches['hour']) {
+ $duration.='T';
+
+ if ($matches['hour'])
+ $duration.=$matches['hour'].'H';
+
+ if ($matches['minute'])
+ $duration.=$matches['minute'].'M';
+
+ if ($matches['second'])
+ $duration.=$matches['second'].'S';
+
+ }
+
+ if ($duration==='P') {
+ $duration = 'PT0S';
+ }
+ $iv = new \DateInterval($duration);
+ if ($invert) $iv->invert = true;
+
+ return $iv;
+
+ }
+
+
+
+ $parts = array(
+ 'week',
+ 'day',
+ 'hour',
+ 'minute',
+ 'second',
+ );
+
+ $newDur = '';
+ foreach($parts as $part) {
+ if (isset($matches[$part]) && $matches[$part]) {
+ $newDur.=' '.$matches[$part] . ' ' . $part . 's';
+ }
+ }
+
+ $newDur = ($matches['plusminus']==='-'?'-':'+') . trim($newDur);
+ if ($newDur === '+') { $newDur = '+0 seconds'; };
+ return $newDur;
+
+ }
+
+ /**
+ * Parses either a Date or DateTime, or Duration value.
+ *
+ * @param string $date
+ * @param DateTimeZone|string $referenceTZ
+ * @return DateTime|DateInterval
+ */
+ static public function parse($date, $referenceTZ = null) {
+
+ if ($date[0]==='P' || ($date[0]==='-' && $date[1]==='P')) {
+ return self::parseDuration($date);
+ } elseif (strlen($date)===8) {
+ return self::parseDate($date);
+ } else {
+ return self::parseDateTime($date, $referenceTZ);
+ }
+
+ }
+
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/VObject/Document.php b/app/Gdoo/Calendar/Sabre/VObject/Document.php
new file mode 100644
index 00000000..59f5a553
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/Document.php
@@ -0,0 +1,109 @@
+value syntax, in which case
+ * properties will automatically be created, or you can just pass a list of
+ * Component and Property object.
+ *
+ * @param string $name
+ * @param array $children
+ * @return Component
+ */
+ public function createComponent($name, array $children = array()) {
+
+ $component = Component::create($name);
+ foreach($children as $k=>$v) {
+
+ if ($v instanceof Node) {
+ $component->add($v);
+ } else {
+ $component->add($k, $v);
+ }
+
+ }
+ return $component;
+
+ }
+
+ /**
+ * Factory method for creating new properties
+ *
+ * This method automatically searches for the correct property class, based
+ * on its name.
+ *
+ * You can specify the parameters either in key=>value syntax, in which case
+ * parameters will automatically be created, or you can just pass a list of
+ * Parameter objects.
+ *
+ * @param string $name
+ * @param mixed $value
+ * @param array $parameters
+ * @return Property
+ */
+ public function createProperty($name, $value = null, array $parameters = array()) {
+
+ return Property::create($name, $value, $parameters);
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/VObject/ElementList.php b/app/Gdoo/Calendar/Sabre/VObject/ElementList.php
new file mode 100644
index 00000000..907aae85
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/ElementList.php
@@ -0,0 +1,172 @@
+vevent where there's multiple VEVENT objects.
+ *
+ * @copyright Copyright (C) 2011-2015 fruux GmbH (https://fruux.com/).
+ * @author Evert Pot (http://evertpot.com/)
+ * @license http://sabre.io/license/ Modified BSD License
+ */
+class ElementList implements \Iterator, \Countable, \ArrayAccess {
+
+ /**
+ * Inner elements
+ *
+ * @var array
+ */
+ protected $elements = array();
+
+ /**
+ * Creates the element list.
+ *
+ * @param array $elements
+ */
+ public function __construct(array $elements) {
+
+ $this->elements = $elements;
+
+ }
+
+ /* {{{ Iterator interface */
+
+ /**
+ * Current position
+ *
+ * @var int
+ */
+ private $key = 0;
+
+ /**
+ * Returns current item in iteration
+ *
+ * @return Element
+ */
+ public function current() {
+
+ return $this->elements[$this->key];
+
+ }
+
+ /**
+ * To the next item in the iterator
+ *
+ * @return void
+ */
+ public function next() {
+
+ $this->key++;
+
+ }
+
+ /**
+ * Returns the current iterator key
+ *
+ * @return int
+ */
+ public function key() {
+
+ return $this->key;
+
+ }
+
+ /**
+ * Returns true if the current position in the iterator is a valid one
+ *
+ * @return bool
+ */
+ public function valid() {
+
+ return isset($this->elements[$this->key]);
+
+ }
+
+ /**
+ * Rewinds the iterator
+ *
+ * @return void
+ */
+ public function rewind() {
+
+ $this->key = 0;
+
+ }
+
+ /* }}} */
+
+ /* {{{ Countable interface */
+
+ /**
+ * Returns the number of elements
+ *
+ * @return int
+ */
+ public function count() {
+
+ return count($this->elements);
+
+ }
+
+ /* }}} */
+
+ /* {{{ ArrayAccess Interface */
+
+
+ /**
+ * Checks if an item exists through ArrayAccess.
+ *
+ * @param int $offset
+ * @return bool
+ */
+ public function offsetExists($offset) {
+
+ return isset($this->elements[$offset]);
+
+ }
+
+ /**
+ * Gets an item through ArrayAccess.
+ *
+ * @param int $offset
+ * @return mixed
+ */
+ public function offsetGet($offset) {
+
+ return $this->elements[$offset];
+
+ }
+
+ /**
+ * Sets an item through ArrayAccess.
+ *
+ * @param int $offset
+ * @param mixed $value
+ * @return void
+ */
+ public function offsetSet($offset,$value) {
+
+ throw new \LogicException('You can not add new objects to an ElementList');
+
+ }
+
+ /**
+ * Sets an item through ArrayAccess.
+ *
+ * This method just forwards the request to the inner iterator
+ *
+ * @param int $offset
+ * @return void
+ */
+ public function offsetUnset($offset) {
+
+ throw new \LogicException('You can not remove objects from an ElementList');
+
+ }
+
+ /* }}} */
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/VObject/FreeBusyGenerator.php b/app/Gdoo/Calendar/Sabre/VObject/FreeBusyGenerator.php
new file mode 100644
index 00000000..bd081545
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/FreeBusyGenerator.php
@@ -0,0 +1,322 @@
+setTimeRange($start, $end);
+ }
+
+ if ($objects) {
+ $this->setObjects($objects);
+ }
+
+ }
+
+ /**
+ * Sets the VCALENDAR object.
+ *
+ * If this is set, it will not be generated for you. You are responsible
+ * for setting things like the METHOD, CALSCALE, VERSION, etc..
+ *
+ * The VFREEBUSY object will be automatically added though.
+ *
+ * @param Component $vcalendar
+ * @return void
+ */
+ public function setBaseObject(Component $vcalendar) {
+
+ $this->baseObject = $vcalendar;
+
+ }
+
+ /**
+ * Sets the input objects
+ *
+ * You must either specify a valendar object as a strong, or as the parse
+ * Component.
+ * It's also possible to specify multiple objects as an array.
+ *
+ * @param mixed $objects
+ * @return void
+ */
+ public function setObjects($objects) {
+
+ if (!is_array($objects)) {
+ $objects = array($objects);
+ }
+
+ $this->objects = array();
+ foreach($objects as $object) {
+
+ if (is_string($object)) {
+ $this->objects[] = Reader::read($object);
+ } elseif ($object instanceof Component) {
+ $this->objects[] = $object;
+ } else {
+ throw new \InvalidArgumentException('You can only pass strings or \\Sabre\\VObject\\Component arguments to setObjects');
+ }
+
+ }
+
+ }
+
+ /**
+ * Sets the time range
+ *
+ * Any freebusy object falling outside of this time range will be ignored.
+ *
+ * @param DateTime $start
+ * @param DateTime $end
+ * @return void
+ */
+ public function setTimeRange(\DateTime $start = null, \DateTime $end = null) {
+
+ $this->start = $start;
+ $this->end = $end;
+
+ }
+
+ /**
+ * Parses the input data and returns a correct VFREEBUSY object, wrapped in
+ * a VCALENDAR.
+ *
+ * @return Component
+ */
+ public function getResult() {
+
+ $busyTimes = array();
+
+ foreach($this->objects as $object) {
+
+ foreach($object->getBaseComponents() as $component) {
+
+ switch($component->name) {
+
+ case 'VEVENT' :
+
+ $FBTYPE = 'BUSY';
+ if (isset($component->TRANSP) && (strtoupper($component->TRANSP) === 'TRANSPARENT')) {
+ break;
+ }
+ if (isset($component->STATUS)) {
+ $status = strtoupper($component->STATUS);
+ if ($status==='CANCELLED') {
+ break;
+ }
+ if ($status==='TENTATIVE') {
+ $FBTYPE = 'BUSY-TENTATIVE';
+ }
+ }
+
+ $times = array();
+
+ if ($component->RRULE) {
+
+ $iterator = new RecurrenceIterator($object, (string)$component->uid);
+ if ($this->start) {
+ $iterator->fastForward($this->start);
+ }
+
+ $maxRecurrences = 200;
+
+ while($iterator->valid() && --$maxRecurrences) {
+
+ $startTime = $iterator->getDTStart();
+ if ($this->end && $startTime > $this->end) {
+ break;
+ }
+ $times[] = array(
+ $iterator->getDTStart(),
+ $iterator->getDTEnd(),
+ );
+
+ $iterator->next();
+
+ }
+
+ } else {
+
+ $startTime = $component->DTSTART->getDateTime();
+ if ($this->end && $startTime > $this->end) {
+ break;
+ }
+ $endTime = null;
+ if (isset($component->DTEND)) {
+ $endTime = $component->DTEND->getDateTime();
+ } elseif (isset($component->DURATION)) {
+ $duration = DateTimeParser::parseDuration((string)$component->DURATION);
+ $endTime = clone $startTime;
+ $endTime->add($duration);
+ } elseif ($component->DTSTART->getDateType() === Property\DateTime::DATE) {
+ $endTime = clone $startTime;
+ $endTime->modify('+1 day');
+ } else {
+ // The event had no duration (0 seconds)
+ break;
+ }
+
+ $times[] = array($startTime, $endTime);
+
+ }
+
+ foreach($times as $time) {
+
+ if ($this->end && $time[0] > $this->end) break;
+ if ($this->start && $time[1] < $this->start) break;
+
+ $busyTimes[] = array(
+ $time[0],
+ $time[1],
+ $FBTYPE,
+ );
+ }
+ break;
+
+ case 'VFREEBUSY' :
+ foreach($component->FREEBUSY as $freebusy) {
+
+ $fbType = isset($freebusy['FBTYPE'])?strtoupper($freebusy['FBTYPE']):'BUSY';
+
+ // Skipping intervals marked as 'free'
+ if ($fbType==='FREE')
+ continue;
+
+ $values = explode(',', $freebusy);
+ foreach($values as $value) {
+ list($startTime, $endTime) = explode('/', $value);
+ $startTime = DateTimeParser::parseDateTime($startTime);
+
+ if (substr($endTime,0,1)==='P' || substr($endTime,0,2)==='-P') {
+ $duration = DateTimeParser::parseDuration($endTime);
+ $endTime = clone $startTime;
+ $endTime->add($duration);
+ } else {
+ $endTime = DateTimeParser::parseDateTime($endTime);
+ }
+
+ if($this->start && $this->start > $endTime) continue;
+ if($this->end && $this->end < $startTime) continue;
+ $busyTimes[] = array(
+ $startTime,
+ $endTime,
+ $fbType
+ );
+
+ }
+
+
+ }
+ break;
+
+
+
+ }
+
+
+ }
+
+ }
+
+ if ($this->baseObject) {
+ $calendar = $this->baseObject;
+ } else {
+ $calendar = Component::create('VCALENDAR');
+ $calendar->version = '2.0';
+ $calendar->prodid = '-//Sabre//Sabre VObject ' . Version::VERSION . '//EN';
+ $calendar->calscale = 'GREGORIAN';
+ }
+
+ $vfreebusy = Component::create('VFREEBUSY');
+ $calendar->add($vfreebusy);
+
+ if ($this->start) {
+ $dtstart = Property::create('DTSTART');
+ $dtstart->setDateTime($this->start,Property\DateTime::UTC);
+ $vfreebusy->add($dtstart);
+ }
+ if ($this->end) {
+ $dtend = Property::create('DTEND');
+ $dtend->setDateTime($this->end,Property\DateTime::UTC);
+ $vfreebusy->add($dtend);
+ }
+ $dtstamp = Property::create('DTSTAMP');
+ $dtstamp->setDateTime(new \DateTime('now'), Property\DateTime::UTC);
+ $vfreebusy->add($dtstamp);
+
+ foreach($busyTimes as $busyTime) {
+
+ $busyTime[0]->setTimeZone(new \DateTimeZone('UTC'));
+ $busyTime[1]->setTimeZone(new \DateTimeZone('UTC'));
+
+ $prop = Property::create(
+ 'FREEBUSY',
+ $busyTime[0]->format('Ymd\\THis\\Z') . '/' . $busyTime[1]->format('Ymd\\THis\\Z')
+ );
+ $prop['FBTYPE'] = $busyTime[2];
+ $vfreebusy->add($prop);
+
+ }
+
+ return $calendar;
+
+ }
+
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/VObject/Node.php b/app/Gdoo/Calendar/Sabre/VObject/Node.php
new file mode 100644
index 00000000..6ff18c42
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/Node.php
@@ -0,0 +1,187 @@
+iterator))
+ return $this->iterator;
+
+ return new ElementList(array($this));
+
+ }
+
+ /**
+ * Sets the overridden iterator
+ *
+ * Note that this is not actually part of the iterator interface
+ *
+ * @param ElementList $iterator
+ * @return void
+ */
+ public function setIterator(ElementList $iterator) {
+
+ $this->iterator = $iterator;
+
+ }
+
+ /* }}} */
+
+ /* {{{ Countable interface */
+
+ /**
+ * Returns the number of elements
+ *
+ * @return int
+ */
+ public function count() {
+
+ $it = $this->getIterator();
+ return $it->count();
+
+ }
+
+ /* }}} */
+
+ /* {{{ ArrayAccess Interface */
+
+
+ /**
+ * Checks if an item exists through ArrayAccess.
+ *
+ * This method just forwards the request to the inner iterator
+ *
+ * @param int $offset
+ * @return bool
+ */
+ public function offsetExists($offset) {
+
+ $iterator = $this->getIterator();
+ return $iterator->offsetExists($offset);
+
+ }
+
+ /**
+ * Gets an item through ArrayAccess.
+ *
+ * This method just forwards the request to the inner iterator
+ *
+ * @param int $offset
+ * @return mixed
+ */
+ public function offsetGet($offset) {
+
+ $iterator = $this->getIterator();
+ return $iterator->offsetGet($offset);
+
+ }
+
+ /**
+ * Sets an item through ArrayAccess.
+ *
+ * This method just forwards the request to the inner iterator
+ *
+ * @param int $offset
+ * @param mixed $value
+ * @return void
+ */
+ public function offsetSet($offset,$value) {
+
+ $iterator = $this->getIterator();
+ $iterator->offsetSet($offset,$value);
+
+ // @codeCoverageIgnoreStart
+ //
+ // This method always throws an exception, so we ignore the closing
+ // brace
+ }
+ // @codeCoverageIgnoreEnd
+
+ /**
+ * Sets an item through ArrayAccess.
+ *
+ * This method just forwards the request to the inner iterator
+ *
+ * @param int $offset
+ * @return void
+ */
+ public function offsetUnset($offset) {
+
+ $iterator = $this->getIterator();
+ $iterator->offsetUnset($offset);
+
+ // @codeCoverageIgnoreStart
+ //
+ // This method always throws an exception, so we ignore the closing
+ // brace
+ }
+ // @codeCoverageIgnoreEnd
+
+ /* }}} */
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/VObject/Parameter.php b/app/Gdoo/Calendar/Sabre/VObject/Parameter.php
new file mode 100644
index 00000000..72aaf3cb
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/Parameter.php
@@ -0,0 +1,91 @@
+name = strtoupper($name);
+ $this->value = $value;
+
+ }
+
+ /**
+ * Returns the parameter's internal value.
+ *
+ * @return string
+ */
+ public function getValue() {
+
+ return $this->value;
+
+ }
+
+
+ /**
+ * Turns the object back into a serialized blob.
+ *
+ * @return string
+ */
+ public function serialize() {
+
+ if (is_null($this->value)) {
+ return $this->name;
+ }
+ $value = str_replace("\n", '\n', $this->value);
+ if (preg_match('#(?: [:;\\\\])#x', $value)) {
+ $value = '"' . $value . '"';
+ }
+ return $this->name . '=' . $value;
+
+ }
+
+ /**
+ * Called when this object is being cast to a string
+ *
+ * @return string
+ */
+ public function __toString() {
+
+ return $this->value;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/VObject/ParseException.php b/app/Gdoo/Calendar/Sabre/VObject/ParseException.php
new file mode 100644
index 00000000..a1dd47dd
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/ParseException.php
@@ -0,0 +1,12 @@
+ 'Sabre\\VObject\\Property\\DateTime',
+ 'CREATED' => 'Sabre\\VObject\\Property\\DateTime',
+ 'DTEND' => 'Sabre\\VObject\\Property\\DateTime',
+ 'DTSTAMP' => 'Sabre\\VObject\\Property\\DateTime',
+ 'DTSTART' => 'Sabre\\VObject\\Property\\DateTime',
+ 'DUE' => 'Sabre\\VObject\\Property\\DateTime',
+ 'EXDATE' => 'Sabre\\VObject\\Property\\MultiDateTime',
+ 'LAST-MODIFIED' => 'Sabre\\VObject\\Property\\DateTime',
+ 'RECURRENCE-ID' => 'Sabre\\VObject\\Property\\DateTime',
+ 'TRIGGER' => 'Sabre\\VObject\\Property\\DateTime',
+ 'N' => 'Sabre\\VObject\\Property\\Compound',
+ 'ORG' => 'Sabre\\VObject\\Property\\Compound',
+ 'ADR' => 'Sabre\\VObject\\Property\\Compound',
+ 'CATEGORIES' => 'Sabre\\VObject\\Property\\Compound',
+ );
+
+ /**
+ * Creates the new property by name, but in addition will also see if
+ * there's a class mapped to the property name.
+ *
+ * Parameters can be specified with the optional third argument. Parameters
+ * must be a key->value map of the parameter name, and value. If the value
+ * is specified as an array, it is assumed that multiple parameters with
+ * the same name should be added.
+ *
+ * @param string $name
+ * @param string $value
+ * @param array $parameters
+ * @return Property
+ */
+ static public function create($name, $value = null, array $parameters = array()) {
+
+ $name = strtoupper($name);
+ $shortName = $name;
+ $group = null;
+ if (strpos($shortName,'.')!==false) {
+ list($group, $shortName) = explode('.', $shortName);
+ }
+
+ if (isset(self::$classMap[$shortName])) {
+ return new self::$classMap[$shortName]($name, $value, $parameters);
+ } else {
+ return new self($name, $value, $parameters);
+ }
+
+ }
+
+ /**
+ * Creates a new property object
+ *
+ * Parameters can be specified with the optional third argument. Parameters
+ * must be a key->value map of the parameter name, and value. If the value
+ * is specified as an array, it is assumed that multiple parameters with
+ * the same name should be added.
+ *
+ * @param string $name
+ * @param string $value
+ * @param array $parameters
+ */
+ public function __construct($name, $value = null, array $parameters = array()) {
+
+ if (!is_scalar($value) && !is_null($value)) {
+ throw new \InvalidArgumentException('The value argument must be scalar or null');
+ }
+
+ $name = strtoupper($name);
+ $group = null;
+ if (strpos($name,'.')!==false) {
+ list($group, $name) = explode('.', $name);
+ }
+ $this->name = $name;
+ $this->group = $group;
+ $this->setValue($value);
+
+ foreach($parameters as $paramName => $paramValues) {
+
+ if (!is_array($paramValues)) {
+ $paramValues = array($paramValues);
+ }
+
+ foreach($paramValues as $paramValue) {
+ $this->add($paramName, $paramValue);
+ }
+
+ }
+
+ }
+
+ /**
+ * Updates the internal value
+ *
+ * @param string $value
+ * @return void
+ */
+ public function setValue($value) {
+
+ $this->value = $value;
+
+ }
+
+ /**
+ * Returns the internal value
+ *
+ * @param string $value
+ * @return string
+ */
+ public function getValue() {
+
+ return $this->value;
+
+ }
+
+ /**
+ * Turns the object back into a serialized blob.
+ *
+ * @return string
+ */
+ public function serialize() {
+
+ $str = $this->name;
+ if ($this->group) $str = $this->group . '.' . $this->name;
+
+ foreach($this->parameters as $param) {
+
+ $str.=';' . $param->serialize();
+
+ }
+
+ $src = array(
+ '\\',
+ "\n",
+ "\r",
+ );
+ $out = array(
+ '\\\\',
+ '\n',
+ '',
+ );
+
+ // avoid double-escaping of \, and \; from Compound properties
+ if (method_exists($this, 'setParts')) {
+ $src[] = '\\\\,';
+ $out[] = '\\,';
+ $src[] = '\\\\;';
+ $out[] = '\\;';
+ }
+
+ $str.=':' . str_replace($src, $out, $this->value);
+
+ $out = '';
+ while(strlen($str)>0) {
+ if (strlen($str)>75) {
+ $out.= mb_strcut($str,0,75,'utf-8') . "\r\n";
+ $str = ' ' . mb_strcut($str,75,strlen($str),'utf-8');
+ } else {
+ $out.=$str . "\r\n";
+ $str='';
+ break;
+ }
+ }
+
+ return $out;
+
+ }
+
+ /**
+ * Adds a new componenten or element
+ *
+ * You can call this method with the following syntaxes:
+ *
+ * add(Parameter $element)
+ * add(string $name, $value)
+ *
+ * The first version adds an Parameter
+ * The second adds a property as a string.
+ *
+ * @param mixed $item
+ * @param mixed $itemValue
+ * @return void
+ */
+ public function add($item, $itemValue = null) {
+
+ if ($item instanceof Parameter) {
+ if (!is_null($itemValue)) {
+ throw new \InvalidArgumentException('The second argument must not be specified, when passing a VObject');
+ }
+ $item->parent = $this;
+ $this->parameters[] = $item;
+ } elseif(is_string($item)) {
+
+ $parameter = new Parameter($item,$itemValue);
+ $parameter->parent = $this;
+ $this->parameters[] = $parameter;
+
+ } else {
+
+ throw new \InvalidArgumentException('The first argument must either be a Node a string');
+
+ }
+
+ }
+
+ /* ArrayAccess interface {{{ */
+
+ /**
+ * Checks if an array element exists
+ *
+ * @param mixed $name
+ * @return bool
+ */
+ public function offsetExists($name) {
+
+ if (is_int($name)) return parent::offsetExists($name);
+
+ $name = strtoupper($name);
+
+ foreach($this->parameters as $parameter) {
+ if ($parameter->name == $name) return true;
+ }
+ return false;
+
+ }
+
+ /**
+ * Returns a parameter, or parameter list.
+ *
+ * @param string $name
+ * @return Node
+ */
+ public function offsetGet($name) {
+
+ if (is_int($name)) return parent::offsetGet($name);
+ $name = strtoupper($name);
+
+ $result = array();
+ foreach($this->parameters as $parameter) {
+ if ($parameter->name == $name)
+ $result[] = $parameter;
+ }
+
+ if (count($result)===0) {
+ return null;
+ } elseif (count($result)===1) {
+ return $result[0];
+ } else {
+ $result[0]->setIterator(new ElementList($result));
+ return $result[0];
+ }
+
+ }
+
+ /**
+ * Creates a new parameter
+ *
+ * @param string $name
+ * @param mixed $value
+ * @return void
+ */
+ public function offsetSet($name, $value) {
+
+ if (is_int($name)) parent::offsetSet($name, $value);
+
+ if (is_scalar($value)) {
+ if (!is_string($name))
+ throw new \InvalidArgumentException('A parameter name must be specified. This means you cannot use the $array[]="string" to add parameters.');
+
+ $this->offsetUnset($name);
+ $parameter = new Parameter($name, $value);
+ $parameter->parent = $this;
+ $this->parameters[] = $parameter;
+
+ } elseif ($value instanceof Parameter) {
+ if (!is_null($name))
+ throw new \InvalidArgumentException('Don\'t specify a parameter name if you\'re passing a \\Sabre\\VObject\\Parameter. Add using $array[]=$parameterObject.');
+
+ $value->parent = $this;
+ $this->parameters[] = $value;
+ } else {
+ throw new \InvalidArgumentException('You can only add parameters to the property object');
+ }
+
+ }
+
+ /**
+ * Removes one or more parameters with the specified name
+ *
+ * @param string $name
+ * @return void
+ */
+ public function offsetUnset($name) {
+
+ if (is_int($name)) parent::offsetUnset($name);
+ $name = strtoupper($name);
+
+ foreach($this->parameters as $key=>$parameter) {
+ if ($parameter->name == $name) {
+ $parameter->parent = null;
+ unset($this->parameters[$key]);
+ }
+
+ }
+
+ }
+
+ /* }}} */
+
+ /**
+ * Called when this object is being cast to a string
+ *
+ * @return string
+ */
+ public function __toString() {
+
+ return (string)$this->value;
+
+ }
+
+ /**
+ * This method is automatically called when the object is cloned.
+ * Specifically, this will ensure all child elements are also cloned.
+ *
+ * @return void
+ */
+ public function __clone() {
+
+ foreach($this->parameters as $key=>$child) {
+ $this->parameters[$key] = clone $child;
+ $this->parameters[$key]->parent = $this;
+ }
+
+ }
+
+ /**
+ * Validates the node for correctness.
+ *
+ * The following options are supported:
+ * - Node::REPAIR - If something is broken, and automatic repair may
+ * be attempted.
+ *
+ * An array is returned with warnings.
+ *
+ * Every item in the array has the following properties:
+ * * level - (number between 1 and 3 with severity information)
+ * * message - (human readable message)
+ * * node - (reference to the offending node)
+ *
+ * @param int $options
+ * @return array
+ */
+ public function validate($options = 0) {
+
+ $warnings = array();
+
+ // Checking if our value is UTF-8
+ if (!StringUtil::isUTF8($this->value)) {
+ $warnings[] = array(
+ 'level' => 1,
+ 'message' => 'Property is not valid UTF-8!',
+ 'node' => $this,
+ );
+ if ($options & self::REPAIR) {
+ $this->value = StringUtil::convertToUTF8($this->value);
+ }
+ }
+
+ // Checking if the propertyname does not contain any invalid bytes.
+ if (!preg_match('/^([A-Z0-9-]+)$/', $this->name)) {
+ $warnings[] = array(
+ 'level' => 1,
+ 'message' => 'The propertyname: ' . $this->name . ' contains invalid characters. Only A-Z, 0-9 and - are allowed',
+ 'node' => $this,
+ );
+ if ($options & self::REPAIR) {
+ // Uppercasing and converting underscores to dashes.
+ $this->name = strtoupper(
+ str_replace('_', '-', $this->name)
+ );
+ // Removing every other invalid character
+ $this->name = preg_replace('/([^A-Z0-9-])/u', '', $this->name);
+
+ }
+
+ }
+
+ // Validating inner parameters
+ foreach($this->parameters as $param) {
+ $warnings = array_merge($warnings, $param->validate($options));
+ }
+
+ return $warnings;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/VObject/Property/Compound.php b/app/Gdoo/Calendar/Sabre/VObject/Property/Compound.php
new file mode 100644
index 00000000..10b53f64
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/Property/Compound.php
@@ -0,0 +1,125 @@
+ ';',
+ 'ADR' => ';',
+ 'ORG' => ';',
+ 'CATEGORIES' => ',',
+ );
+
+ /**
+ * The currently used delimiter.
+ *
+ * @var string
+ */
+ protected $delimiter = null;
+
+ /**
+ * Get a compound value as an array.
+ *
+ * @param $name string
+ * @return array
+ */
+ public function getParts() {
+
+ if (is_null($this->value)) {
+ return array();
+ }
+
+ $delimiter = $this->getDelimiter();
+
+ // split by any $delimiter which is NOT prefixed by a slash.
+ // Note that this is not a a perfect solution. If a value is prefixed
+ // by two slashes, it should actually be split anyway.
+ //
+ // Hopefully we can fix this better in a future version, where we can
+ // break compatibility a bit.
+ $compoundValues = preg_split("/(?value);
+
+ // remove slashes from any semicolon and comma left escaped in the single values
+ $compoundValues = array_map(
+ function($val) {
+ return strtr($val, array('\,' => ',', '\;' => ';'));
+ }, $compoundValues);
+
+ return $compoundValues;
+
+ }
+
+ /**
+ * Returns the delimiter for this property.
+ *
+ * @return string
+ */
+ public function getDelimiter() {
+
+ if (!$this->delimiter) {
+ if (isset(self::$delimiterMap[$this->name])) {
+ $this->delimiter = self::$delimiterMap[$this->name];
+ } else {
+ // To be a bit future proof, we are going to default the
+ // delimiter to ;
+ $this->delimiter = ';';
+ }
+ }
+ return $this->delimiter;
+
+ }
+
+ /**
+ * Set a compound value as an array.
+ *
+ *
+ * @param $name string
+ * @return array
+ */
+ public function setParts(array $values) {
+
+ // add slashes to all semicolons and commas in the single values
+ $values = array_map(
+ function($val) {
+ return strtr($val, array(',' => '\,', ';' => '\;'));
+ }, $values);
+
+ $this->setValue(
+ implode($this->getDelimiter(), $values)
+ );
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/VObject/Property/DateTime.php b/app/Gdoo/Calendar/Sabre/VObject/Property/DateTime.php
new file mode 100644
index 00000000..ab83b518
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/Property/DateTime.php
@@ -0,0 +1,245 @@
+setValue($dt->format('Ymd\\THis'));
+ $this->offsetUnset('VALUE');
+ $this->offsetUnset('TZID');
+ $this->offsetSet('VALUE','DATE-TIME');
+ break;
+ case self::UTC :
+ $dt->setTimeZone(new \DateTimeZone('UTC'));
+ $this->setValue($dt->format('Ymd\\THis\\Z'));
+ $this->offsetUnset('VALUE');
+ $this->offsetUnset('TZID');
+ $this->offsetSet('VALUE','DATE-TIME');
+ break;
+ case self::LOCALTZ :
+ $this->setValue($dt->format('Ymd\\THis'));
+ $this->offsetUnset('VALUE');
+ $this->offsetUnset('TZID');
+ $this->offsetSet('VALUE','DATE-TIME');
+ $this->offsetSet('TZID', $dt->getTimeZone()->getName());
+ break;
+ case self::DATE :
+ $this->setValue($dt->format('Ymd'));
+ $this->offsetUnset('VALUE');
+ $this->offsetUnset('TZID');
+ $this->offsetSet('VALUE','DATE');
+ break;
+ default :
+ throw new \InvalidArgumentException('You must pass a valid dateType constant');
+
+ }
+ $this->dateTime = $dt;
+ $this->dateType = $dateType;
+
+ }
+
+ /**
+ * Returns the current DateTime value.
+ *
+ * If no value was set, this method returns null.
+ *
+ * @return \DateTime|null
+ */
+ public function getDateTime() {
+
+ if ($this->dateTime)
+ return $this->dateTime;
+
+ list(
+ $this->dateType,
+ $this->dateTime
+ ) = self::parseData($this->value, $this);
+ return $this->dateTime;
+
+ }
+
+ /**
+ * Returns the type of Date format.
+ *
+ * This method returns one of the format constants. If no date was set,
+ * this method will return null.
+ *
+ * @return int|null
+ */
+ public function getDateType() {
+
+ if ($this->dateType)
+ return $this->dateType;
+
+ list(
+ $this->dateType,
+ $this->dateTime,
+ ) = self::parseData($this->value, $this);
+ return $this->dateType;
+
+ }
+
+ /**
+ * This method will return true, if the property had a date and a time, as
+ * opposed to only a date.
+ *
+ * @return bool
+ */
+ public function hasTime() {
+
+ return $this->getDateType()!==self::DATE;
+
+ }
+
+ /**
+ * Parses the internal data structure to figure out what the current date
+ * and time is.
+ *
+ * The returned array contains two elements:
+ * 1. A 'DateType' constant (as defined on this class), or null.
+ * 2. A DateTime object (or null)
+ *
+ * @param string|null $propertyValue The string to parse (yymmdd or
+ * ymmddThhmmss, etc..)
+ * @param \Sabre\VObject\Property|null $property The instance of the
+ * property we're parsing.
+ * @return array
+ */
+ static public function parseData($propertyValue, VObject\Property $property = null) {
+
+ if (is_null($propertyValue)) {
+ return array(null, null);
+ }
+
+ $date = '(?P[1-2][0-9]{3})(?P[0-1][0-9])(?P[0-3][0-9])';
+ $time = '(?P[0-2][0-9])(?P[0-5][0-9])(?P[0-5][0-9])';
+ $regex = "/^$date(T$time(?PZ)?)?$/";
+
+ if (!preg_match($regex, $propertyValue, $matches)) {
+ throw new \InvalidArgumentException($propertyValue . ' is not a valid \DateTime or Date string');
+ }
+
+ if (!isset($matches['hour'])) {
+ // Date-only
+ return array(
+ self::DATE,
+ new \DateTime($matches['year'] . '-' . $matches['month'] . '-' . $matches['date'] . ' 00:00:00', new \DateTimeZone('UTC')),
+ );
+ }
+
+ $dateStr =
+ $matches['year'] .'-' .
+ $matches['month'] . '-' .
+ $matches['date'] . ' ' .
+ $matches['hour'] . ':' .
+ $matches['minute'] . ':' .
+ $matches['second'];
+
+ if (isset($matches['isutc'])) {
+ $dt = new \DateTime($dateStr,new \DateTimeZone('UTC'));
+ $dt->setTimeZone(new \DateTimeZone('UTC'));
+ return array(
+ self::UTC,
+ $dt
+ );
+ }
+
+ // Finding the timezone.
+ $tzid = $property['TZID'];
+ if (!$tzid) {
+ // This was a floating time string. This implies we use the
+ // timezone from date_default_timezone_set / date.timezone ini
+ // setting.
+ return array(
+ self::LOCAL,
+ new \DateTime($dateStr)
+ );
+ }
+
+ // To look up the timezone, we must first find the VCALENDAR component.
+ $root = $property;
+ while($root->parent) {
+ $root = $root->parent;
+ }
+ if ($root->name === 'VCALENDAR') {
+ $tz = VObject\TimeZoneUtil::getTimeZone((string)$tzid, $root);
+ } else {
+ $tz = VObject\TimeZoneUtil::getTimeZone((string)$tzid);
+ }
+
+ $dt = new \DateTime($dateStr, $tz);
+ $dt->setTimeZone($tz);
+
+ return array(
+ self::LOCALTZ,
+ $dt
+ );
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/VObject/Property/MultiDateTime.php b/app/Gdoo/Calendar/Sabre/VObject/Property/MultiDateTime.php
new file mode 100644
index 00000000..816a703a
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/Property/MultiDateTime.php
@@ -0,0 +1,180 @@
+offsetUnset('VALUE');
+ $this->offsetUnset('TZID');
+ switch($dateType) {
+
+ case DateTime::LOCAL :
+ $val = array();
+ foreach($dt as $i) {
+ $val[] = $i->format('Ymd\\THis');
+ }
+ $this->setValue(implode(',',$val));
+ $this->offsetSet('VALUE','DATE-TIME');
+ break;
+ case DateTime::UTC :
+ $val = array();
+ foreach($dt as $i) {
+ $i->setTimeZone(new \DateTimeZone('UTC'));
+ $val[] = $i->format('Ymd\\THis\\Z');
+ }
+ $this->setValue(implode(',',$val));
+ $this->offsetSet('VALUE','DATE-TIME');
+ break;
+ case DateTime::LOCALTZ :
+ $val = array();
+ foreach($dt as $i) {
+ $val[] = $i->format('Ymd\\THis');
+ }
+ $this->setValue(implode(',',$val));
+ $this->offsetSet('VALUE','DATE-TIME');
+ $this->offsetSet('TZID', $dt[0]->getTimeZone()->getName());
+ break;
+ case DateTime::DATE :
+ $val = array();
+ foreach($dt as $i) {
+ $val[] = $i->format('Ymd');
+ }
+ $this->setValue(implode(',',$val));
+ $this->offsetSet('VALUE','DATE');
+ break;
+ default :
+ throw new \InvalidArgumentException('You must pass a valid dateType constant');
+
+ }
+ $this->dateTimes = $dt;
+ $this->dateType = $dateType;
+
+ }
+
+ /**
+ * Returns the current DateTime value.
+ *
+ * If no value was set, this method returns null.
+ *
+ * @return array|null
+ */
+ public function getDateTimes() {
+
+ if ($this->dateTimes)
+ return $this->dateTimes;
+
+ $dts = array();
+
+ if (!$this->value) {
+ $this->dateTimes = null;
+ $this->dateType = null;
+ return null;
+ }
+
+ foreach(explode(',',$this->value) as $val) {
+ list(
+ $type,
+ $dt
+ ) = DateTime::parseData($val, $this);
+ $dts[] = $dt;
+ $this->dateType = $type;
+ }
+ $this->dateTimes = $dts;
+ return $this->dateTimes;
+
+ }
+
+ /**
+ * Returns the type of Date format.
+ *
+ * This method returns one of the format constants. If no date was set,
+ * this method will return null.
+ *
+ * @return int|null
+ */
+ public function getDateType() {
+
+ if ($this->dateType)
+ return $this->dateType;
+
+ if (!$this->value) {
+ $this->dateTimes = null;
+ $this->dateType = null;
+ return null;
+ }
+
+ $dts = array();
+ foreach(explode(',',$this->value) as $val) {
+ list(
+ $type,
+ $dt
+ ) = DateTime::parseData($val, $this);
+ $dts[] = $dt;
+ $this->dateType = $type;
+ }
+ $this->dateTimes = $dts;
+ return $this->dateType;
+
+ }
+
+ /**
+ * This method will return true, if the property had a date and a time, as
+ * opposed to only a date.
+ *
+ * @return bool
+ */
+ public function hasTime() {
+
+ return $this->getDateType()!==DateTime::DATE;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/VObject/Reader.php b/app/Gdoo/Calendar/Sabre/VObject/Reader.php
new file mode 100644
index 00000000..1b339c14
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/Reader.php
@@ -0,0 +1,223 @@
+add($parsedLine);
+
+ if ($nextLine===false)
+ throw new ParseException('Invalid VObject. Document ended prematurely.');
+
+ }
+
+ // Checking component name of the 'END:' line.
+ if (substr($nextLine,4)!==$obj->name) {
+ throw new ParseException('Invalid VObject, expected: "END:' . $obj->name . '" got: "' . $nextLine . '"');
+ }
+ next($lines);
+
+ return $obj;
+
+ }
+
+ // Properties
+ //$result = preg_match('/(?P[A-Z0-9-]+)(?:;(?P^(?([^:^\"]|\"([^\"]*)\")*))?";
+ $regex = "/^(?P$token)$parameters:(?P.*)$/i";
+
+ $result = preg_match($regex,$line,$matches);
+
+ if (!$result) {
+ if ($options & self::OPTION_IGNORE_INVALID_LINES) {
+ return null;
+ } else {
+ throw new ParseException('Invalid VObject, line ' . ($lineNr+1) . ' did not follow the icalendar/vcard format');
+ }
+ }
+
+ $propertyName = strtoupper($matches['name']);
+ $propertyValue = preg_replace_callback('#(\\\\(\\\\|N|n))#',function($matches) {
+ if ($matches[2]==='n' || $matches[2]==='N') {
+ return "\n";
+ } else {
+ return $matches[2];
+ }
+ }, $matches['value']);
+
+ $obj = Property::create($propertyName, $propertyValue);
+
+ if ($matches['parameters']) {
+
+ foreach(self::readParameters($matches['parameters']) as $param) {
+ $obj->add($param);
+ }
+
+ }
+
+ return $obj;
+
+
+ }
+
+ /**
+ * Reads a parameter list from a property
+ *
+ * This method returns an array of Parameter
+ *
+ * @param string $parameters
+ * @return array
+ */
+ static private function readParameters($parameters) {
+
+ $token = '[A-Z0-9-]+';
+
+ $paramValue = '(?P[^\"^;]*|"[^"]*")';
+
+ $regex = "/(?<=^|;)(?P$token)(=$paramValue(?=$|;))?/i";
+ preg_match_all($regex, $parameters, $matches, PREG_SET_ORDER);
+
+ $params = array();
+ foreach($matches as $match) {
+
+ if (!isset($match['paramValue'])) {
+
+ $value = null;
+
+ } else {
+
+ $value = $match['paramValue'];
+
+ if (isset($value[0]) && $value[0]==='"') {
+ // Stripping quotes, if needed
+ $value = substr($value,1,strlen($value)-2);
+ }
+
+ $value = preg_replace_callback('#(\\\\(\\\\|N|n|;|,))#',function($matches) {
+ if ($matches[2]==='n' || $matches[2]==='N') {
+ return "\n";
+ } else {
+ return $matches[2];
+ }
+ }, $value);
+
+ }
+
+ $params[] = new Parameter($match['paramName'], $value);
+
+ }
+
+ return $params;
+
+ }
+
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/VObject/RecurrenceIterator.php b/app/Gdoo/Calendar/Sabre/VObject/RecurrenceIterator.php
new file mode 100644
index 00000000..00cd2435
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/RecurrenceIterator.php
@@ -0,0 +1,1145 @@
+ 0,
+ 'MO' => 1,
+ 'TU' => 2,
+ 'WE' => 3,
+ 'TH' => 4,
+ 'FR' => 5,
+ 'SA' => 6,
+ );
+
+ /**
+ * Mappings between the day number and english day name.
+ *
+ * @var array
+ */
+ private $dayNames = array(
+ 0 => 'Sunday',
+ 1 => 'Monday',
+ 2 => 'Tuesday',
+ 3 => 'Wednesday',
+ 4 => 'Thursday',
+ 5 => 'Friday',
+ 6 => 'Saturday',
+ );
+
+ /**
+ * If the current iteration of the event is an overriden event, this
+ * property will hold the VObject
+ *
+ * @var Component
+ */
+ private $currentOverriddenEvent;
+
+ /**
+ * This property may contain the date of the next not-overridden event.
+ * This date is calculated sometimes a bit early, before overridden events
+ * are evaluated.
+ *
+ * @var DateTime
+ */
+ private $nextDate;
+
+ /**
+ * This counts the number of overridden events we've handled so far
+ *
+ * @var int
+ */
+ private $handledOverridden = 0;
+
+ /**
+ * Creates the iterator
+ *
+ * You should pass a VCALENDAR component, as well as the UID of the event
+ * we're going to traverse.
+ *
+ * @param Component $vcal
+ * @param string|null $uid
+ */
+ public function __construct(Component $vcal, $uid=null) {
+
+ if (is_null($uid)) {
+ if ($vcal->name === 'VCALENDAR') {
+ throw new \InvalidArgumentException('If you pass a VCALENDAR object, you must pass a uid argument as well');
+ }
+ $components = array($vcal);
+ $uid = (string)$vcal->uid;
+ } else {
+ $components = $vcal->select('VEVENT');
+ }
+ foreach($components as $component) {
+ if ((string)$component->uid == $uid) {
+ if (isset($component->{'RECURRENCE-ID'})) {
+ $this->overriddenEvents[$component->DTSTART->getDateTime()->getTimeStamp()] = $component;
+ $this->overriddenDates[] = $component->{'RECURRENCE-ID'}->getDateTime();
+ } else {
+ $this->baseEvent = $component;
+ }
+ }
+ }
+
+ ksort($this->overriddenEvents);
+
+ if (!$this->baseEvent) {
+ throw new \InvalidArgumentException('Could not find a base event with uid: ' . $uid);
+ }
+
+ $this->startDate = clone $this->baseEvent->DTSTART->getDateTime();
+
+ $this->endDate = null;
+ if (isset($this->baseEvent->DTEND)) {
+ $this->endDate = clone $this->baseEvent->DTEND->getDateTime();
+ } else {
+ $this->endDate = clone $this->startDate;
+ if (isset($this->baseEvent->DURATION)) {
+ $this->endDate->add(DateTimeParser::parse($this->baseEvent->DURATION->value));
+ } elseif ($this->baseEvent->DTSTART->getDateType()===Property\DateTime::DATE) {
+ $this->endDate->modify('+1 day');
+ }
+ }
+ $this->currentDate = clone $this->startDate;
+
+ $rrule = (string)$this->baseEvent->RRULE;
+
+ $parts = explode(';', $rrule);
+
+ // If no rrule was specified, we create a default setting
+ if (!$rrule) {
+ $this->frequency = 'daily';
+ $this->count = 1;
+ } else foreach($parts as $part) {
+
+ list($key, $value) = explode('=', $part, 2);
+
+ switch(strtoupper($key)) {
+
+ case 'FREQ' :
+ if (!in_array(
+ strtolower($value),
+ array('secondly','minutely','hourly','daily','weekly','monthly','yearly')
+ )) {
+ throw new \InvalidArgumentException('Unknown value for FREQ=' . strtoupper($value));
+
+ }
+ $this->frequency = strtolower($value);
+ break;
+
+ case 'UNTIL' :
+ $this->until = DateTimeParser::parse($value);
+
+ // In some cases events are generated with an UNTIL=
+ // parameter before the actual start of the event.
+ //
+ // Not sure why this is happening. We assume that the
+ // intention was that the event only recurs once.
+ //
+ // So we are modifying the parameter so our code doesn't
+ // break.
+ if($this->until < $this->baseEvent->DTSTART->getDateTime()) {
+ $this->until = $this->baseEvent->DTSTART->getDateTime();
+ }
+ break;
+
+ case 'COUNT' :
+ $this->count = (int)$value;
+ break;
+
+ case 'INTERVAL' :
+ $this->interval = (int)$value;
+ $this->interval = $this->interval == 0 ? 1 : $this->interval;
+ if ($this->interval < 1) {
+ throw new \InvalidArgumentException('INTERVAL in RRULE must be a positive integer!');
+ }
+ break;
+
+ case 'BYSECOND' :
+ $this->bySecond = explode(',', $value);
+ break;
+
+ case 'BYMINUTE' :
+ $this->byMinute = explode(',', $value);
+ break;
+
+ case 'BYHOUR' :
+ $this->byHour = explode(',', $value);
+ break;
+
+ case 'BYDAY' :
+ $this->byDay = explode(',', strtoupper($value));
+ break;
+
+ case 'BYMONTHDAY' :
+ $this->byMonthDay = explode(',', $value);
+ break;
+
+ case 'BYYEARDAY' :
+ $this->byYearDay = explode(',', $value);
+ break;
+
+ case 'BYWEEKNO' :
+ $this->byWeekNo = explode(',', $value);
+ break;
+
+ case 'BYMONTH' :
+ $this->byMonth = explode(',', $value);
+ break;
+
+ case 'BYSETPOS' :
+ $this->bySetPos = explode(',', $value);
+ break;
+
+ case 'WKST' :
+ $this->weekStart = strtoupper($value);
+ break;
+
+ }
+
+ }
+
+ // Parsing exception dates
+ if (isset($this->baseEvent->EXDATE)) {
+ foreach($this->baseEvent->EXDATE as $exDate) {
+
+ foreach(explode(',', (string)$exDate) as $exceptionDate) {
+
+ $this->exceptionDates[] =
+ DateTimeParser::parse($exceptionDate, $this->startDate->getTimeZone());
+
+ }
+
+ }
+
+ }
+
+ }
+
+ /**
+ * Returns the current item in the list
+ *
+ * @return DateTime
+ */
+ public function current() {
+
+ if (!$this->valid()) return null;
+ return clone $this->currentDate;
+
+ }
+
+ /**
+ * This method returns the startdate for the current iteration of the
+ * event.
+ *
+ * @return DateTime
+ */
+ public function getDtStart() {
+
+ if (!$this->valid()) return null;
+ return clone $this->currentDate;
+
+ }
+
+ /**
+ * This method returns the enddate for the current iteration of the
+ * event.
+ *
+ * @return DateTime
+ */
+ public function getDtEnd() {
+
+ if (!$this->valid()) return null;
+ $dtEnd = clone $this->currentDate;
+ $dtEnd->add( $this->startDate->diff( $this->endDate ) );
+ return clone $dtEnd;
+
+ }
+
+ /**
+ * Returns a VEVENT object with the updated start and end date.
+ *
+ * Any recurrence information is removed, and this function may return an
+ * 'overridden' event instead.
+ *
+ * This method always returns a cloned instance.
+ *
+ * @return Component\VEvent
+ */
+ public function getEventObject() {
+
+ if ($this->currentOverriddenEvent) {
+ return clone $this->currentOverriddenEvent;
+ }
+ $event = clone $this->baseEvent;
+ unset($event->RRULE);
+ unset($event->EXDATE);
+ unset($event->RDATE);
+ unset($event->EXRULE);
+
+ $event->DTSTART->setDateTime($this->getDTStart(), $event->DTSTART->getDateType());
+ if (isset($event->DTEND)) {
+ $event->DTEND->setDateTime($this->getDtEnd(), $event->DTSTART->getDateType());
+ }
+ if ($this->counter > 0) {
+ $event->{'RECURRENCE-ID'} = (string)$event->DTSTART;
+ }
+
+ return $event;
+
+ }
+
+ /**
+ * Returns the current item number
+ *
+ * @return int
+ */
+ public function key() {
+
+ return $this->counter;
+
+ }
+
+ /**
+ * Whether or not there is a 'next item'
+ *
+ * @return bool
+ */
+ public function valid() {
+
+ if (!is_null($this->count)) {
+ return $this->counter < $this->count;
+ }
+ if (!is_null($this->until) && $this->currentDate > $this->until) {
+
+ // Need to make sure there's no overridden events past the
+ // until date.
+ foreach($this->overriddenEvents as $overriddenEvent) {
+
+ if ($overriddenEvent->DTSTART->getDateTime() >= $this->currentDate) {
+
+ return true;
+ }
+ }
+ return false;
+ }
+ return true;
+
+ }
+
+ /**
+ * Resets the iterator
+ *
+ * @return void
+ */
+ public function rewind() {
+
+ $this->currentDate = clone $this->startDate;
+ $this->counter = 0;
+
+ }
+
+ /**
+ * This method allows you to quickly go to the next occurrence after the
+ * specified date.
+ *
+ * Note that this checks the current 'endDate', not the 'stardDate'. This
+ * means that if you forward to January 1st, the iterator will stop at the
+ * first event that ends *after* January 1st.
+ *
+ * @param DateTime $dt
+ * @return void
+ */
+ public function fastForward(\DateTime $dt) {
+
+ while($this->valid() && $this->getDTEnd() <= $dt) {
+ $this->next();
+ }
+
+ }
+
+ /**
+ * Returns true if this recurring event never ends.
+ *
+ * @return bool
+ */
+ public function isInfinite() {
+
+ return !$this->count && !$this->until;
+
+ }
+
+ /**
+ * Goes on to the next iteration
+ *
+ * @return void
+ */
+ public function next() {
+
+ $previousStamp = $this->currentDate->getTimeStamp();
+
+ // Finding the next overridden event in line, and storing that for
+ // later use.
+ $overriddenEvent = null;
+ $overriddenDate = null;
+ $this->currentOverriddenEvent = null;
+
+ foreach($this->overriddenEvents as $index=>$event) {
+ if ($index > $previousStamp) {
+ $overriddenEvent = $event;
+ $overriddenDate = clone $event->DTSTART->getDateTime();
+ break;
+ }
+ }
+
+ // If we have a stored 'next date', we will use that.
+ if ($this->nextDate) {
+ if (!$overriddenDate || $this->nextDate < $overriddenDate) {
+ $this->currentDate = $this->nextDate;
+ $currentStamp = $this->currentDate->getTimeStamp();
+ $this->nextDate = null;
+ } else {
+ $this->currentDate = clone $overriddenDate;
+ $this->currentOverriddenEvent = $overriddenEvent;
+ }
+ $this->counter++;
+ return;
+ }
+
+ while(true) {
+
+ // Otherwise, we find the next event in the normal RRULE
+ // sequence.
+ switch($this->frequency) {
+
+ case 'hourly' :
+ $this->nextHourly();
+ break;
+
+ case 'daily' :
+ $this->nextDaily();
+ break;
+
+ case 'weekly' :
+ $this->nextWeekly();
+ break;
+
+ case 'monthly' :
+ $this->nextMonthly();
+ break;
+
+ case 'yearly' :
+ $this->nextYearly();
+ break;
+
+ }
+ $currentStamp = $this->currentDate->getTimeStamp();
+
+
+ // Checking exception dates
+ foreach($this->exceptionDates as $exceptionDate) {
+ if ($this->currentDate == $exceptionDate) {
+ $this->counter++;
+ continue 2;
+ }
+ }
+ foreach($this->overriddenDates as $check) {
+ if ($this->currentDate == $check) {
+ continue 2;
+ }
+ }
+ break;
+
+ }
+
+
+
+ // Is the date we have actually higher than the next overiddenEvent?
+ if ($overriddenDate && $this->currentDate > $overriddenDate) {
+ $this->nextDate = clone $this->currentDate;
+ $this->currentDate = clone $overriddenDate;
+ $this->currentOverriddenEvent = $overriddenEvent;
+ $this->handledOverridden++;
+ }
+ $this->counter++;
+
+
+ /*
+ * If we have overridden events left in the queue, but our counter is
+ * running out, we should grab one of those.
+ */
+ if (!is_null($overriddenEvent) && !is_null($this->count) && count($this->overriddenEvents) - $this->handledOverridden >= ($this->count - $this->counter)) {
+
+ $this->currentOverriddenEvent = $overriddenEvent;
+ $this->currentDate = clone $overriddenDate;
+ $this->handledOverridden++;
+
+ }
+
+ }
+
+ /**
+ * Does the processing for advancing the iterator for hourly frequency.
+ *
+ * @return void
+ */
+ protected function nextHourly() {
+
+ if (!$this->byHour) {
+ $this->currentDate->modify('+' . $this->interval . ' hours');
+ return;
+ }
+ }
+
+ /**
+ * Does the processing for advancing the iterator for daily frequency.
+ *
+ * @return void
+ */
+ protected function nextDaily() {
+
+ if (!$this->byHour && !$this->byDay) {
+ $this->currentDate->modify('+' . $this->interval . ' days');
+ return;
+ }
+
+ if (isset($this->byHour)) {
+ $recurrenceHours = $this->getHours();
+ }
+
+ if (isset($this->byDay)) {
+ $recurrenceDays = $this->getDays();
+ }
+
+ do {
+
+ if ($this->byHour) {
+ if ($this->currentDate->format('G') == '23') {
+ // to obey the interval rule
+ $this->currentDate->modify('+' . $this->interval-1 . ' days');
+ }
+
+ $this->currentDate->modify('+1 hours');
+
+ } else {
+ $this->currentDate->modify('+' . $this->interval . ' days');
+
+ }
+
+ // Current day of the week
+ $currentDay = $this->currentDate->format('w');
+
+ // Current hour of the day
+ $currentHour = $this->currentDate->format('G');
+
+ } while (($this->byDay && !in_array($currentDay, $recurrenceDays)) || ($this->byHour && !in_array($currentHour, $recurrenceHours)));
+
+ }
+
+ /**
+ * Does the processing for advancing the iterator for weekly frequency.
+ *
+ * @return void
+ */
+ protected function nextWeekly() {
+
+ if (!$this->byHour && !$this->byDay) {
+ $this->currentDate->modify('+' . $this->interval . ' weeks');
+ return;
+ }
+
+ if ($this->byHour) {
+ $recurrenceHours = $this->getHours();
+ }
+
+ if ($this->byDay) {
+ $recurrenceDays = $this->getDays();
+ }
+
+ // First day of the week:
+ $firstDay = $this->dayMap[$this->weekStart];
+
+ do {
+
+ if ($this->byHour) {
+ $this->currentDate->modify('+1 hours');
+ } else {
+ $this->currentDate->modify('+1 days');
+ }
+
+ // Current day of the week
+ $currentDay = (int) $this->currentDate->format('w');
+
+ // Current hour of the day
+ $currentHour = (int) $this->currentDate->format('G');
+
+ // We need to roll over to the next week
+ if ($currentDay === $firstDay && (!$this->byHour || $currentHour == '0')) {
+ $this->currentDate->modify('+' . $this->interval-1 . ' weeks');
+
+ // We need to go to the first day of this week, but only if we
+ // are not already on this first day of this week.
+ if($this->currentDate->format('w') != $firstDay) {
+ $this->currentDate->modify('last ' . $this->dayNames[$this->dayMap[$this->weekStart]]);
+ }
+ }
+
+ // We have a match
+ } while (($this->byDay && !in_array($currentDay, $recurrenceDays)) || ($this->byHour && !in_array($currentHour, $recurrenceHours)));
+ }
+
+ /**
+ * Does the processing for advancing the iterator for monthly frequency.
+ *
+ * @return void
+ */
+ protected function nextMonthly() {
+
+ $currentDayOfMonth = $this->currentDate->format('j');
+ if (!$this->byMonthDay && !$this->byDay) {
+
+ // If the current day is higher than the 28th, rollover can
+ // occur to the next month. We Must skip these invalid
+ // entries.
+ if ($currentDayOfMonth < 29) {
+ $this->currentDate->modify('+' . $this->interval . ' months');
+ } else {
+ $increase = 0;
+ do {
+ $increase++;
+ $tempDate = clone $this->currentDate;
+ $tempDate->modify('+ ' . ($this->interval*$increase) . ' months');
+ } while ($tempDate->format('j') != $currentDayOfMonth);
+ $this->currentDate = $tempDate;
+ }
+ return;
+ }
+
+ while(true) {
+
+ $occurrences = $this->getMonthlyOccurrences();
+
+ foreach($occurrences as $occurrence) {
+
+ // The first occurrence thats higher than the current
+ // day of the month wins.
+ if ($occurrence > $currentDayOfMonth) {
+ break 2;
+ }
+
+ }
+
+ // If we made it all the way here, it means there were no
+ // valid occurrences, and we need to advance to the next
+ // month.
+ $this->currentDate->modify('first day of this month');
+ $this->currentDate->modify('+ ' . $this->interval . ' months');
+
+ // This goes to 0 because we need to start counting at hte
+ // beginning.
+ $currentDayOfMonth = 0;
+
+ }
+
+ $this->currentDate->setDate($this->currentDate->format('Y'), $this->currentDate->format('n'), $occurrence);
+
+ }
+
+ /**
+ * Does the processing for advancing the iterator for yearly frequency.
+ *
+ * @return void
+ */
+ protected function nextYearly() {
+
+ $currentMonth = $this->currentDate->format('n');
+ $currentYear = $this->currentDate->format('Y');
+ $currentDayOfMonth = $this->currentDate->format('j');
+
+ // No sub-rules, so we just advance by year
+ if (!$this->byMonth) {
+
+ // Unless it was a leap day!
+ if ($currentMonth==2 && $currentDayOfMonth==29) {
+
+ $counter = 0;
+ do {
+ $counter++;
+ // Here we increase the year count by the interval, until
+ // we hit a date that's also in a leap year.
+ //
+ // We could just find the next interval that's dividable by
+ // 4, but that would ignore the rule that there's no leap
+ // year every year that's dividable by a 100, but not by
+ // 400. (1800, 1900, 2100). So we just rely on the datetime
+ // functions instead.
+ $nextDate = clone $this->currentDate;
+ $nextDate->modify('+ ' . ($this->interval*$counter) . ' years');
+ } while ($nextDate->format('n')!=2);
+ $this->currentDate = $nextDate;
+
+ return;
+
+ }
+
+ // The easiest form
+ $this->currentDate->modify('+' . $this->interval . ' years');
+ return;
+
+ }
+
+ $currentMonth = $this->currentDate->format('n');
+ $currentYear = $this->currentDate->format('Y');
+ $currentDayOfMonth = $this->currentDate->format('j');
+
+ $advancedToNewMonth = false;
+
+ // If we got a byDay or getMonthDay filter, we must first expand
+ // further.
+ if ($this->byDay || $this->byMonthDay) {
+
+ while(true) {
+
+ $occurrences = $this->getMonthlyOccurrences();
+
+ foreach($occurrences as $occurrence) {
+
+ // The first occurrence that's higher than the current
+ // day of the month wins.
+ // If we advanced to the next month or year, the first
+ // occurrence is always correct.
+ if ($occurrence > $currentDayOfMonth || $advancedToNewMonth) {
+ break 2;
+ }
+
+ }
+
+ // If we made it here, it means we need to advance to
+ // the next month or year.
+ $currentDayOfMonth = 1;
+ $advancedToNewMonth = true;
+ do {
+
+ $currentMonth++;
+ if ($currentMonth>12) {
+ $currentYear+=$this->interval;
+ $currentMonth = 1;
+ }
+ } while (!in_array($currentMonth, $this->byMonth));
+
+ $this->currentDate->setDate($currentYear, $currentMonth, $currentDayOfMonth);
+
+ }
+
+ // If we made it here, it means we got a valid occurrence
+ $this->currentDate->setDate($currentYear, $currentMonth, $occurrence);
+ return;
+
+ } else {
+
+ // These are the 'byMonth' rules, if there are no byDay or
+ // byMonthDay sub-rules.
+ do {
+
+ $currentMonth++;
+ if ($currentMonth>12) {
+ $currentYear+=$this->interval;
+ $currentMonth = 1;
+ }
+ } while (!in_array($currentMonth, $this->byMonth));
+ $this->currentDate->setDate($currentYear, $currentMonth, $currentDayOfMonth);
+
+ return;
+
+ }
+
+ }
+
+ /**
+ * Returns all the occurrences for a monthly frequency with a 'byDay' or
+ * 'byMonthDay' expansion for the current month.
+ *
+ * The returned list is an array of integers with the day of month (1-31).
+ *
+ * @return array
+ */
+ protected function getMonthlyOccurrences() {
+
+ $startDate = clone $this->currentDate;
+
+ $byDayResults = array();
+
+ // Our strategy is to simply go through the byDays, advance the date to
+ // that point and add it to the results.
+ if ($this->byDay) foreach($this->byDay as $day) {
+
+ $dayName = $this->dayNames[$this->dayMap[substr($day,-2)]];
+
+ // Dayname will be something like 'wednesday'. Now we need to find
+ // all wednesdays in this month.
+ $dayHits = array();
+
+ $checkDate = clone $startDate;
+ $checkDate->modify('first day of this month');
+ $checkDate->modify($dayName);
+
+ do {
+ $dayHits[] = $checkDate->format('j');
+ $checkDate->modify('next ' . $dayName);
+ } while ($checkDate->format('n') === $startDate->format('n'));
+
+ // So now we have 'all wednesdays' for month. It is however
+ // possible that the user only really wanted the 1st, 2nd or last
+ // wednesday.
+ if (strlen($day)>2) {
+ $offset = (int)substr($day,0,-2);
+
+ if ($offset>0) {
+ // It is possible that the day does not exist, such as a
+ // 5th or 6th wednesday of the month.
+ if (isset($dayHits[$offset-1])) {
+ $byDayResults[] = $dayHits[$offset-1];
+ }
+ } else {
+
+ // if it was negative we count from the end of the array
+ $byDayResults[] = $dayHits[count($dayHits) + $offset];
+ }
+ } else {
+ // There was no counter (first, second, last wednesdays), so we
+ // just need to add the all to the list).
+ $byDayResults = array_merge($byDayResults, $dayHits);
+
+ }
+
+ }
+
+ $byMonthDayResults = array();
+ if ($this->byMonthDay) foreach($this->byMonthDay as $monthDay) {
+
+ // Removing values that are out of range for this month
+ if ($monthDay > $startDate->format('t') ||
+ $monthDay < 0-$startDate->format('t')) {
+ continue;
+ }
+ if ($monthDay>0) {
+ $byMonthDayResults[] = $monthDay;
+ } else {
+ // Negative values
+ $byMonthDayResults[] = $startDate->format('t') + 1 + $monthDay;
+ }
+ }
+
+ // If there was just byDay or just byMonthDay, they just specify our
+ // (almost) final list. If both were provided, then byDay limits the
+ // list.
+ if ($this->byMonthDay && $this->byDay) {
+ $result = array_intersect($byMonthDayResults, $byDayResults);
+ } elseif ($this->byMonthDay) {
+ $result = $byMonthDayResults;
+ } else {
+ $result = $byDayResults;
+ }
+ $result = array_unique($result);
+ sort($result, SORT_NUMERIC);
+
+ // The last thing that needs checking is the BYSETPOS. If it's set, it
+ // means only certain items in the set survive the filter.
+ if (!$this->bySetPos) {
+ return $result;
+ }
+
+ $filteredResult = array();
+ foreach($this->bySetPos as $setPos) {
+
+ if ($setPos<0) {
+ $setPos = count($result)-($setPos+1);
+ }
+ if (isset($result[$setPos-1])) {
+ $filteredResult[] = $result[$setPos-1];
+ }
+ }
+
+ sort($filteredResult, SORT_NUMERIC);
+ return $filteredResult;
+
+ }
+
+ protected function getHours()
+ {
+ $recurrenceHours = array();
+ foreach($this->byHour as $byHour) {
+ $recurrenceHours[] = $byHour;
+ }
+
+ return $recurrenceHours;
+ }
+
+ protected function getDays()
+ {
+ $recurrenceDays = array();
+ foreach($this->byDay as $byDay) {
+
+ // The day may be preceeded with a positive (+n) or
+ // negative (-n) integer. However, this does not make
+ // sense in 'weekly' so we ignore it here.
+ $recurrenceDays[] = $this->dayMap[substr($byDay,-2)];
+
+ }
+
+ return $recurrenceDays;
+ }
+}
+
diff --git a/app/Gdoo/Calendar/Sabre/VObject/Splitter/ICalendar.php b/app/Gdoo/Calendar/Sabre/VObject/Splitter/ICalendar.php
new file mode 100644
index 00000000..b909bf00
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/Splitter/ICalendar.php
@@ -0,0 +1,111 @@
+children as $component) {
+ if (!$component instanceof VObject\Component) {
+ continue;
+ }
+
+ // Get all timezones
+ if ($component->name === 'VTIMEZONE') {
+ $this->vtimezones[(string)$component->TZID] = $component;
+ continue;
+ }
+
+ // Get component UID for recurring Events search
+ if($component->UID) {
+ $uid = (string)$component->UID;
+ } else {
+ // Generating a random UID
+ $uid = sha1(microtime()) . '-vobjectimport';
+ }
+
+ // Take care of recurring events
+ if (!array_key_exists($uid, $this->objects)) {
+ $this->objects[$uid] = VObject\Component::create('VCALENDAR');
+ }
+
+ $this->objects[$uid]->add(clone $component);
+ }
+
+ }
+
+ /**
+ * Every time getNext() is called, a new object will be parsed, until we
+ * hit the end of the stream.
+ *
+ * When the end is reached, null will be returned.
+ *
+ * @return Sabre\VObject\Component|null
+ */
+ public function getNext() {
+
+ if($object=array_shift($this->objects)) {
+
+ // create our baseobject
+ $object->version = '2.0';
+ $object->prodid = '-//Sabre//Sabre VObject ' . VObject\Version::VERSION . '//EN';
+ $object->calscale = 'GREGORIAN';
+
+ // add vtimezone information to obj (if we have it)
+ foreach ($this->vtimezones as $vtimezone) {
+ $object->add($vtimezone);
+ }
+
+ return $object;
+
+ } else {
+
+ return null;
+
+ }
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/VObject/Splitter/SplitterInterface.php b/app/Gdoo/Calendar/Sabre/VObject/Splitter/SplitterInterface.php
new file mode 100644
index 00000000..d4543333
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/Splitter/SplitterInterface.php
@@ -0,0 +1,39 @@
+input = $input;
+
+ }
+
+ /**
+ * Every time getNext() is called, a new object will be parsed, until we
+ * hit the end of the stream.
+ *
+ * When the end is reached, null will be returned.
+ *
+ * @return Sabre\VObject\Component|null
+ */
+ public function getNext() {
+
+ $vcard = '';
+
+ do {
+
+ if (feof($this->input)) {
+ return false;
+ }
+
+ $line = fgets($this->input);
+ $vcard .= $line;
+
+ } while(strtoupper(substr($line,0,4))!=="END:");
+
+ $object = VObject\Reader::read($vcard);
+
+ if($object->name !== 'VCARD') {
+ throw new \InvalidArgumentException("Thats no vCard!", 1);
+ }
+
+ return $object;
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/VObject/StringUtil.php b/app/Gdoo/Calendar/Sabre/VObject/StringUtil.php
new file mode 100644
index 00000000..e81a77c1
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/StringUtil.php
@@ -0,0 +1,61 @@
+'Australia/Darwin',
+ 'AUS Eastern Standard Time'=>'Australia/Sydney',
+ 'Afghanistan Standard Time'=>'Asia/Kabul',
+ 'Alaskan Standard Time'=>'America/Anchorage',
+ 'Arab Standard Time'=>'Asia/Riyadh',
+ 'Arabian Standard Time'=>'Asia/Dubai',
+ 'Arabic Standard Time'=>'Asia/Baghdad',
+ 'Argentina Standard Time'=>'America/Buenos_Aires',
+ 'Armenian Standard Time'=>'Asia/Yerevan',
+ 'Atlantic Standard Time'=>'America/Halifax',
+ 'Azerbaijan Standard Time'=>'Asia/Baku',
+ 'Azores Standard Time'=>'Atlantic/Azores',
+ 'Bangladesh Standard Time'=>'Asia/Dhaka',
+ 'Canada Central Standard Time'=>'America/Regina',
+ 'Cape Verde Standard Time'=>'Atlantic/Cape_Verde',
+ 'Caucasus Standard Time'=>'Asia/Yerevan',
+ 'Cen. Australia Standard Time'=>'Australia/Adelaide',
+ 'Central America Standard Time'=>'America/Guatemala',
+ 'Central Asia Standard Time'=>'Asia/Almaty',
+ 'Central Brazilian Standard Time'=>'America/Cuiaba',
+ 'Central Europe Standard Time'=>'Europe/Budapest',
+ 'Central European Standard Time'=>'Europe/Warsaw',
+ 'Central Pacific Standard Time'=>'Pacific/Guadalcanal',
+ 'Central Standard Time'=>'America/Chicago',
+ 'Central Standard Time (Mexico)'=>'America/Mexico_City',
+ 'China Standard Time'=>'Asia/Shanghai',
+ 'Dateline Standard Time'=>'Etc/GMT+12',
+ 'E. Africa Standard Time'=>'Africa/Nairobi',
+ 'E. Australia Standard Time'=>'Australia/Brisbane',
+ 'E. Europe Standard Time'=>'Europe/Minsk',
+ 'E. South America Standard Time'=>'America/Sao_Paulo',
+ 'Eastern Standard Time'=>'America/New_York',
+ 'Egypt Standard Time'=>'Africa/Cairo',
+ 'Ekaterinburg Standard Time'=>'Asia/Yekaterinburg',
+ 'FLE Standard Time'=>'Europe/Kiev',
+ 'Fiji Standard Time'=>'Pacific/Fiji',
+ 'GMT Standard Time'=>'Europe/London',
+ 'GTB Standard Time'=>'Europe/Istanbul',
+ 'Georgian Standard Time'=>'Asia/Tbilisi',
+ 'Greenland Standard Time'=>'America/Godthab',
+ 'Greenwich Standard Time'=>'Atlantic/Reykjavik',
+ 'Hawaiian Standard Time'=>'Pacific/Honolulu',
+ 'India Standard Time'=>'Asia/Calcutta',
+ 'Iran Standard Time'=>'Asia/Tehran',
+ 'Israel Standard Time'=>'Asia/Jerusalem',
+ 'Jordan Standard Time'=>'Asia/Amman',
+ 'Kamchatka Standard Time'=>'Asia/Kamchatka',
+ 'Korea Standard Time'=>'Asia/Seoul',
+ 'Magadan Standard Time'=>'Asia/Magadan',
+ 'Mauritius Standard Time'=>'Indian/Mauritius',
+ 'Mexico Standard Time'=>'America/Mexico_City',
+ 'Mexico Standard Time 2'=>'America/Chihuahua',
+ 'Mid-Atlantic Standard Time'=>'Etc/GMT-2',
+ 'Middle East Standard Time'=>'Asia/Beirut',
+ 'Montevideo Standard Time'=>'America/Montevideo',
+ 'Morocco Standard Time'=>'Africa/Casablanca',
+ 'Mountain Standard Time'=>'America/Denver',
+ 'Mountain Standard Time (Mexico)'=>'America/Chihuahua',
+ 'Myanmar Standard Time'=>'Asia/Rangoon',
+ 'N. Central Asia Standard Time'=>'Asia/Novosibirsk',
+ 'Namibia Standard Time'=>'Africa/Windhoek',
+ 'Nepal Standard Time'=>'Asia/Katmandu',
+ 'New Zealand Standard Time'=>'Pacific/Auckland',
+ 'Newfoundland Standard Time'=>'America/St_Johns',
+ 'North Asia East Standard Time'=>'Asia/Irkutsk',
+ 'North Asia Standard Time'=>'Asia/Krasnoyarsk',
+ 'Pacific SA Standard Time'=>'America/Santiago',
+ 'Pacific Standard Time'=>'America/Los_Angeles',
+ 'Pacific Standard Time (Mexico)'=>'America/Santa_Isabel',
+ 'Pakistan Standard Time'=>'Asia/Karachi',
+ 'Paraguay Standard Time'=>'America/Asuncion',
+ 'Romance Standard Time'=>'Europe/Paris',
+ 'Russian Standard Time'=>'Europe/Moscow',
+ 'SA Eastern Standard Time'=>'America/Cayenne',
+ 'SA Pacific Standard Time'=>'America/Bogota',
+ 'SA Western Standard Time'=>'America/La_Paz',
+ 'SE Asia Standard Time'=>'Asia/Bangkok',
+ 'Samoa Standard Time'=>'Pacific/Apia',
+ 'Singapore Standard Time'=>'Asia/Singapore',
+ 'South Africa Standard Time'=>'Africa/Johannesburg',
+ 'Sri Lanka Standard Time'=>'Asia/Colombo',
+ 'Syria Standard Time'=>'Asia/Damascus',
+ 'Taipei Standard Time'=>'Asia/Taipei',
+ 'Tasmania Standard Time'=>'Australia/Hobart',
+ 'Tokyo Standard Time'=>'Asia/Tokyo',
+ 'Tonga Standard Time'=>'Pacific/Tongatapu',
+ 'US Eastern Standard Time'=>'America/Indianapolis',
+ 'US Mountain Standard Time'=>'America/Phoenix',
+ 'UTC+12'=>'Etc/GMT-12',
+ 'UTC-02'=>'Etc/GMT+2',
+ 'UTC-11'=>'Etc/GMT+11',
+ 'Ulaanbaatar Standard Time'=>'Asia/Ulaanbaatar',
+ 'Venezuela Standard Time'=>'America/Caracas',
+ 'Vladivostok Standard Time'=>'Asia/Vladivostok',
+ 'W. Australia Standard Time'=>'Australia/Perth',
+ 'W. Central Africa Standard Time'=>'Africa/Lagos',
+ 'W. Europe Standard Time'=>'Europe/Berlin',
+ 'West Asia Standard Time'=>'Asia/Tashkent',
+ 'West Pacific Standard Time'=>'Pacific/Port_Moresby',
+ 'Yakutsk Standard Time'=>'Asia/Yakutsk',
+
+ // Microsoft exchange timezones
+ // Source:
+ // http://msdn.microsoft.com/en-us/library/ms988620%28v=exchg.65%29.aspx
+ //
+ // Correct timezones deduced with help from:
+ // http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
+ 'Universal Coordinated Time' => 'UTC',
+ 'Casablanca, Monrovia' => 'Africa/Casablanca',
+ 'Greenwich Mean Time: Dublin, Edinburgh, Lisbon, London' => 'Europe/Lisbon',
+ 'Greenwich Mean Time; Dublin, Edinburgh, London' => 'Europe/London',
+ 'Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna' => 'Europe/Berlin',
+ 'Belgrade, Pozsony, Budapest, Ljubljana, Prague' => 'Europe/Prague',
+ 'Brussels, Copenhagen, Madrid, Paris' => 'Europe/Paris',
+ 'Paris, Madrid, Brussels, Copenhagen' => 'Europe/Paris',
+ 'Prague, Central Europe' => 'Europe/Prague',
+ 'Sarajevo, Skopje, Sofija, Vilnius, Warsaw, Zagreb' => 'Europe/Sarajevo',
+ 'West Central Africa' => 'Africa/Luanda', // This was a best guess
+ 'Athens, Istanbul, Minsk' => 'Europe/Athens',
+ 'Bucharest' => 'Europe/Bucharest',
+ 'Cairo' => 'Africa/Cairo',
+ 'Harare, Pretoria' => 'Africa/Harare',
+ 'Helsinki, Riga, Tallinn' => 'Europe/Helsinki',
+ 'Israel, Jerusalem Standard Time' => 'Asia/Jerusalem',
+ 'Baghdad' => 'Asia/Baghdad',
+ 'Arab, Kuwait, Riyadh' => 'Asia/Kuwait',
+ 'Moscow, St. Petersburg, Volgograd' => 'Europe/Moscow',
+ 'East Africa, Nairobi' => 'Africa/Nairobi',
+ 'Tehran' => 'Asia/Tehran',
+ 'Abu Dhabi, Muscat' => 'Asia/Muscat', // Best guess
+ 'Baku, Tbilisi, Yerevan' => 'Asia/Baku',
+ 'Kabul' => 'Asia/Kabul',
+ 'Ekaterinburg' => 'Asia/Yekaterinburg',
+ 'Islamabad, Karachi, Tashkent' => 'Asia/Karachi',
+ 'Kolkata, Chennai, Mumbai, New Delhi, India Standard Time' => 'Asia/Calcutta',
+ 'Kathmandu, Nepal' => 'Asia/Kathmandu',
+ 'Almaty, Novosibirsk, North Central Asia' => 'Asia/Almaty',
+ 'Astana, Dhaka' => 'Asia/Dhaka',
+ 'Sri Jayawardenepura, Sri Lanka' => 'Asia/Colombo',
+ 'Rangoon' => 'Asia/Rangoon',
+ 'Bangkok, Hanoi, Jakarta' => 'Asia/Bangkok',
+ 'Krasnoyarsk' => 'Asia/Krasnoyarsk',
+ 'Beijing, Chongqing, Hong Kong SAR, Urumqi' => 'Asia/Shanghai',
+ 'Irkutsk, Ulaan Bataar' => 'Asia/Irkutsk',
+ 'Kuala Lumpur, Singapore' => 'Asia/Singapore',
+ 'Perth, Western Australia' => 'Australia/Perth',
+ 'Taipei' => 'Asia/Taipei',
+ 'Osaka, Sapporo, Tokyo' => 'Asia/Tokyo',
+ 'Seoul, Korea Standard time' => 'Asia/Seoul',
+ 'Yakutsk' => 'Asia/Yakutsk',
+ 'Adelaide, Central Australia' => 'Australia/Adelaide',
+ 'Darwin' => 'Australia/Darwin',
+ 'Brisbane, East Australia' => 'Australia/Brisbane',
+ 'Canberra, Melbourne, Sydney, Hobart (year 2000 only)' => 'Australia/Sydney',
+ 'Guam, Port Moresby' => 'Pacific/Guam',
+ 'Hobart, Tasmania' => 'Australia/Hobart',
+ 'Vladivostok' => 'Asia/Vladivostok',
+ 'Magadan, Solomon Is., New Caledonia' => 'Asia/Magadan',
+ 'Auckland, Wellington' => 'Pacific/Auckland',
+ 'Fiji Islands, Kamchatka, Marshall Is.' => 'Pacific/Fiji',
+ 'Nuku\'alofa, Tonga' => 'Pacific/Tongatapu',
+ 'Azores' => 'Atlantic/Azores',
+ 'Cape Verde Is.' => 'Atlantic/Cape_Verde',
+ 'Mid-Atlantic' => 'America/Noronha',
+ 'Brasilia' => 'America/Sao_Paulo', // Best guess
+ 'Buenos Aires' => 'America/Argentina/Buenos_Aires',
+ 'Greenland' => 'America/Godthab',
+ 'Newfoundland' => 'America/St_Johns',
+ 'Atlantic Time (Canada)' => 'America/Halifax',
+ 'Caracas, La Paz' => 'America/Caracas',
+ 'Santiago' => 'America/Santiago',
+ 'Bogota, Lima, Quito' => 'America/Bogota',
+ 'Eastern Time (US & Canada)' => 'America/New_York',
+ 'Indiana (East)' => 'America/Indiana/Indianapolis',
+ 'Central America' => 'America/Guatemala',
+ 'Central Time (US & Canada)' => 'America/Chicago',
+ 'Mexico City, Tegucigalpa' => 'America/Mexico_City',
+ 'Saskatchewan' => 'America/Edmonton',
+ 'Arizona' => 'America/Phoenix',
+ 'Mountain Time (US & Canada)' => 'America/Denver', // Best guess
+ 'Pacific Time (US & Canada); Tijuana' => 'America/Los_Angeles', // Best guess
+ 'Alaska' => 'America/Anchorage',
+ 'Hawaii' => 'Pacific/Honolulu',
+ 'Midway Island, Samoa' => 'Pacific/Midway',
+ 'Eniwetok, Kwajalein, Dateline Time' => 'Pacific/Kwajalein',
+
+ // The following list are timezone names that could be generated by
+ // Lotus / Domino
+ 'Dateline' => 'Etc/GMT-12',
+ 'Samoa' => 'Pacific/Apia',
+ 'Hawaiian' => 'Pacific/Honolulu',
+ 'Alaskan' => 'America/Anchorage',
+ 'Pacific' => 'America/Los_Angeles',
+ 'Pacific Standard Time' => 'America/Los_Angeles',
+ 'Mexico Standard Time 2' => 'America/Chihuahua',
+ 'Mountain' => 'America/Denver',
+ 'Mountain Standard Time' => 'America/Chihuahua',
+ 'US Mountain' => 'America/Phoenix',
+ 'Canada Central' => 'America/Edmonton',
+ 'Central America' => 'America/Guatemala',
+ 'Central' => 'America/Chicago',
+ 'Central Standard Time' => 'America/Mexico_City',
+ 'Mexico' => 'America/Mexico_City',
+ 'Eastern' => 'America/New_York',
+ 'SA Pacific' => 'America/Bogota',
+ 'US Eastern' => 'America/Indiana/Indianapolis',
+ 'Venezuela' => 'America/Caracas',
+ 'Atlantic' => 'America/Halifax',
+ 'Central Brazilian' => 'America/Manaus',
+ 'Pacific SA' => 'America/Santiago',
+ 'SA Western' => 'America/La_Paz',
+ 'Newfoundland' => 'America/St_Johns',
+ 'Argentina' => 'America/Argentina/Buenos_Aires',
+ 'E. South America' => 'America/Belem',
+ 'Greenland' => 'America/Godthab',
+ 'Montevideo' => 'America/Montevideo',
+ 'SA Eastern' => 'America/Belem',
+ 'Mid-Atlantic' => 'Etc/GMT-2',
+ 'Azores' => 'Atlantic/Azores',
+ 'Cape Verde' => 'Atlantic/Cape_Verde',
+ 'Greenwich' => 'Atlantic/Reykjavik', // No I'm serious.. Greenwich is not GMT.
+ 'Morocco' => 'Africa/Casablanca',
+ 'Central Europe' => 'Europe/Prague',
+ 'Central European' => 'Europe/Sarajevo',
+ 'Romance' => 'Europe/Paris',
+ 'W. Central Africa' => 'Africa/Lagos', // Best guess
+ 'W. Europe' => 'Europe/Amsterdam',
+ 'E. Europe' => 'Europe/Minsk',
+ 'Egypt' => 'Africa/Cairo',
+ 'FLE' => 'Europe/Helsinki',
+ 'GTB' => 'Europe/Athens',
+ 'Israel' => 'Asia/Jerusalem',
+ 'Jordan' => 'Asia/Amman',
+ 'Middle East' => 'Asia/Beirut',
+ 'Namibia' => 'Africa/Windhoek',
+ 'South Africa' => 'Africa/Harare',
+ 'Arab' => 'Asia/Kuwait',
+ 'Arabic' => 'Asia/Baghdad',
+ 'E. Africa' => 'Africa/Nairobi',
+ 'Georgian' => 'Asia/Tbilisi',
+ 'Russian' => 'Europe/Moscow',
+ 'Iran' => 'Asia/Tehran',
+ 'Arabian' => 'Asia/Muscat',
+ 'Armenian' => 'Asia/Yerevan',
+ 'Azerbijan' => 'Asia/Baku',
+ 'Caucasus' => 'Asia/Yerevan',
+ 'Mauritius' => 'Indian/Mauritius',
+ 'Afghanistan' => 'Asia/Kabul',
+ 'Ekaterinburg' => 'Asia/Yekaterinburg',
+ 'Pakistan' => 'Asia/Karachi',
+ 'West Asia' => 'Asia/Tashkent',
+ 'India' => 'Asia/Calcutta',
+ 'Sri Lanka' => 'Asia/Colombo',
+ 'Nepal' => 'Asia/Kathmandu',
+ 'Central Asia' => 'Asia/Dhaka',
+ 'N. Central Asia' => 'Asia/Almaty',
+ 'Myanmar' => 'Asia/Rangoon',
+ 'North Asia' => 'Asia/Krasnoyarsk',
+ 'SE Asia' => 'Asia/Bangkok',
+ 'China' => 'Asia/Shanghai',
+ 'North Asia East' => 'Asia/Irkutsk',
+ 'Singapore' => 'Asia/Singapore',
+ 'Taipei' => 'Asia/Taipei',
+ 'W. Australia' => 'Australia/Perth',
+ 'Korea' => 'Asia/Seoul',
+ 'Tokyo' => 'Asia/Tokyo',
+ 'Yakutsk' => 'Asia/Yakutsk',
+ 'AUS Central' => 'Australia/Darwin',
+ 'Cen. Australia' => 'Australia/Adelaide',
+ 'AUS Eastern' => 'Australia/Sydney',
+ 'E. Australia' => 'Australia/Brisbane',
+ 'Tasmania' => 'Australia/Hobart',
+ 'Vladivostok' => 'Asia/Vladivostok',
+ 'West Pacific' => 'Pacific/Guam',
+ 'Central Pacific' => 'Asia/Magadan',
+ 'Fiji' => 'Pacific/Fiji',
+ 'New Zealand' => 'Pacific/Auckland',
+ 'Tonga' => 'Pacific/Tongatapu',
+
+ // PHP 5.5.10 failed on a few timezones that were valid before. We're
+ // normalizing them here.
+ 'CST6CDT' => 'America/Chicago',
+ 'Cuba' => 'America/Havana',
+ 'Egypt' => 'Africa/Cairo',
+ 'Eire' => 'Europe/Dublin',
+ 'EST5EDT' => 'America/New_York',
+ 'Factory' => 'UTC',
+ 'GB-Eire' => 'Europe/London',
+ 'GMT0' => 'UTC',
+ 'Greenwich' => 'UTC',
+ 'Hongkong' => 'Asia/Hong_Kong',
+ 'Iceland' => 'Atlantic/Reykjavik',
+ 'Iran' => 'Asia/Tehran',
+ 'Israel' => 'Asia/Jerusalem',
+ 'Jamaica' => 'America/Jamaica',
+ 'Japan' => 'Asia/Tokyo',
+ 'Kwajalein' => 'Pacific/Kwajalein',
+ 'Libya' => 'Africa/Tripoli',
+ 'MST7MDT' => 'America/Denver',
+ 'Navajo' => 'America/Denver',
+ 'NZ-CHAT' => 'Pacific/Chatham',
+ 'Poland' => 'Europe/Warsaw',
+ 'Portugal' => 'Europe/Lisbon',
+ 'PST8PDT' => 'America/Los_Angeles',
+ 'Singapore' => 'Asia/Singapore',
+ 'Turkey' => 'Europe/Istanbul',
+ 'Universal' => 'UTC',
+ 'W-SU' => 'Europe/Moscow',
+ );
+
+ /**
+ * List of microsoft exchange timezone ids.
+ *
+ * Source: http://msdn.microsoft.com/en-us/library/aa563018(loband).aspx
+ */
+ public static $microsoftExchangeMap = array(
+ 0 => 'UTC',
+ 31 => 'Africa/Casablanca',
+
+ // Insanely, id #2 is used for both Europe/Lisbon, and Europe/Sarajevo.
+ // I'm not even kidding.. We handle this special case in the
+ // getTimeZone method.
+ 2 => 'Europe/Lisbon',
+ 1 => 'Europe/London',
+ 4 => 'Europe/Berlin',
+ 6 => 'Europe/Prague',
+ 3 => 'Europe/Paris',
+ 69 => 'Africa/Luanda', // This was a best guess
+ 7 => 'Europe/Athens',
+ 5 => 'Europe/Bucharest',
+ 49 => 'Africa/Cairo',
+ 50 => 'Africa/Harare',
+ 59 => 'Europe/Helsinki',
+ 27 => 'Asia/Jerusalem',
+ 26 => 'Asia/Baghdad',
+ 74 => 'Asia/Kuwait',
+ 51 => 'Europe/Moscow',
+ 56 => 'Africa/Nairobi',
+ 25 => 'Asia/Tehran',
+ 24 => 'Asia/Muscat', // Best guess
+ 54 => 'Asia/Baku',
+ 48 => 'Asia/Kabul',
+ 58 => 'Asia/Yekaterinburg',
+ 47 => 'Asia/Karachi',
+ 23 => 'Asia/Calcutta',
+ 62 => 'Asia/Kathmandu',
+ 46 => 'Asia/Almaty',
+ 71 => 'Asia/Dhaka',
+ 66 => 'Asia/Colombo',
+ 61 => 'Asia/Rangoon',
+ 22 => 'Asia/Bangkok',
+ 64 => 'Asia/Krasnoyarsk',
+ 45 => 'Asia/Shanghai',
+ 63 => 'Asia/Irkutsk',
+ 21 => 'Asia/Singapore',
+ 73 => 'Australia/Perth',
+ 75 => 'Asia/Taipei',
+ 20 => 'Asia/Tokyo',
+ 72 => 'Asia/Seoul',
+ 70 => 'Asia/Yakutsk',
+ 19 => 'Australia/Adelaide',
+ 44 => 'Australia/Darwin',
+ 18 => 'Australia/Brisbane',
+ 76 => 'Australia/Sydney',
+ 43 => 'Pacific/Guam',
+ 42 => 'Australia/Hobart',
+ 68 => 'Asia/Vladivostok',
+ 41 => 'Asia/Magadan',
+ 17 => 'Pacific/Auckland',
+ 40 => 'Pacific/Fiji',
+ 67 => 'Pacific/Tongatapu',
+ 29 => 'Atlantic/Azores',
+ 53 => 'Atlantic/Cape_Verde',
+ 30 => 'America/Noronha',
+ 8 => 'America/Sao_Paulo', // Best guess
+ 32 => 'America/Argentina/Buenos_Aires',
+ 60 => 'America/Godthab',
+ 28 => 'America/St_Johns',
+ 9 => 'America/Halifax',
+ 33 => 'America/Caracas',
+ 65 => 'America/Santiago',
+ 35 => 'America/Bogota',
+ 10 => 'America/New_York',
+ 34 => 'America/Indiana/Indianapolis',
+ 55 => 'America/Guatemala',
+ 11 => 'America/Chicago',
+ 37 => 'America/Mexico_City',
+ 36 => 'America/Edmonton',
+ 38 => 'America/Phoenix',
+ 12 => 'America/Denver', // Best guess
+ 13 => 'America/Los_Angeles', // Best guess
+ 14 => 'America/Anchorage',
+ 15 => 'Pacific/Honolulu',
+ 16 => 'Pacific/Midway',
+ 39 => 'Pacific/Kwajalein',
+ );
+
+ /**
+ * This method will try to find out the correct timezone for an iCalendar
+ * date-time value.
+ *
+ * You must pass the contents of the TZID parameter, as well as the full
+ * calendar.
+ *
+ * If the lookup fails, this method will return the default PHP timezone
+ * (as configured using date_default_timezone_set, or the date.timezone ini
+ * setting).
+ *
+ * Alternatively, if $failIfUncertain is set to true, it will throw an
+ * exception if we cannot accurately determine the timezone.
+ *
+ * @param string $tzid
+ * @param Sabre\VObject\Component $vcalendar
+ * @return DateTimeZone
+ */
+ static public function getTimeZone($tzid, Component $vcalendar = null, $failIfUncertain = false) {
+
+ // First we will just see if the tzid is a support timezone identifier.
+ //
+ // The only exception is if the timezone starts with (. This is to
+ // handle cases where certain microsoft products generate timezone
+ // identifiers that for instance look like:
+ //
+ // (GMT+01.00) Sarajevo/Warsaw/Zagreb
+ //
+ // Since PHP 5.5.10, the first bit will be used as the timezone and
+ // this method will return just GMT+01:00. This is wrong, because it
+ // doesn't take DST into account.
+ if ($tzid[0]!=='(') {
+ try {
+ return new \DateTimeZone($tzid);
+ } catch (\Exception $e) {
+ }
+ }
+
+ // Next, we check if the tzid is somewhere in our tzid map.
+ if (isset(self::$map[$tzid])) {
+ return new \DateTimeZone(self::$map[$tzid]);
+ }
+
+ // Maybe the author was hyper-lazy and just included an offset. We
+ // support it, but we aren't happy about it.
+ //
+ // Note that the path in the source will never be taken from PHP 5.5.10
+ // onwards. PHP 5.5.10 supports the "GMT+0100" style of format, so it
+ // already gets returned early in this function. Once we drop support
+ // for versions under PHP 5.5.10, this bit can be taken out of the
+ // source.
+ if (preg_match('/^GMT(\+|-)([0-9]{4})$/', $tzid, $matches)) {
+ return new \DateTimeZone('Etc/GMT' . $matches[1] . ltrim(substr($matches[2],0,2),'0'));
+ }
+
+ if ($vcalendar) {
+
+ // If that didn't work, we will scan VTIMEZONE objects
+ foreach($vcalendar->select('VTIMEZONE') as $vtimezone) {
+
+ if ((string)$vtimezone->TZID === $tzid) {
+
+ // Some clients add 'X-LIC-LOCATION' with the olson name.
+ if (isset($vtimezone->{'X-LIC-LOCATION'})) {
+
+ $lic = (string)$vtimezone->{'X-LIC-LOCATION'};
+
+ // Libical generators may specify strings like
+ // "SystemV/EST5EDT". For those we must remove the
+ // SystemV part.
+ if (substr($lic,0,8)==='SystemV/') {
+ $lic = substr($lic,8);
+ }
+
+ return self::getTimeZone($lic, null, $failIfUncertain);
+
+ }
+ // Microsoft may add a magic number, which we also have an
+ // answer for.
+ if (isset($vtimezone->{'X-MICROSOFT-CDO-TZID'})) {
+ $cdoId = (int)$vtimezone->{'X-MICROSOFT-CDO-TZID'}->value;
+
+ // 2 can mean both Europe/Lisbon and Europe/Sarajevo.
+ if ($cdoId===2 && strpos((string)$vtimezone->TZID, 'Sarajevo')!==false) {
+ return new \DateTimeZone('Europe/Sarajevo');
+ }
+
+ if (isset(self::$microsoftExchangeMap[$cdoId])) {
+ return new \DateTimeZone(self::$microsoftExchangeMap[$cdoId]);
+ }
+ }
+
+ }
+
+ }
+
+ }
+
+ if ($failIfUncertain) {
+ throw new \InvalidArgumentException('We were unable to determine the correct PHP timezone for tzid: ' . $tzid);
+ }
+
+ // If we got all the way here, we default to UTC.
+ return new \DateTimeZone(date_default_timezone_get());
+
+ }
+
+}
diff --git a/app/Gdoo/Calendar/Sabre/VObject/Version.php b/app/Gdoo/Calendar/Sabre/VObject/Version.php
new file mode 100644
index 00000000..58bc01cb
--- /dev/null
+++ b/app/Gdoo/Calendar/Sabre/VObject/Version.php
@@ -0,0 +1,24 @@
+ $data['id'],
+ 'title' => $data['title'],
+ 'allday' => $data['allday'],
+ 'calendar' => $data['calendar'],
+ ];
+
+ if ($data['allday']) {
+ foreach ($ranges as $date) {
+ $item['date'] = $date->format($format);
+ $items[] = $item;
+ }
+ } else {
+ $_start = $start->format('H:i');
+ $_end = $end->format('H:i');
+
+ $count = count($ranges);
+
+ foreach ($ranges as $i => $date) {
+ $now = $date->format($format);
+
+ $item['date'] = $now;
+
+ if ($i == 0) {
+ $item['start'] = $_start;
+ $item['end'] = $count == 1 ? $_end : '23:59';
+ } else {
+ $item['start'] = '00:00';
+ $item['end'] = $count -1 == $i ? $_end : '23:59';
+ }
+ $items[] = $item;
+ }
+ }
+ return $items;
+ }
+}
diff --git a/app/Gdoo/Calendar/Services/CalendarService.php b/app/Gdoo/Calendar/Services/CalendarService.php
new file mode 100644
index 00000000..8dee0c19
--- /dev/null
+++ b/app/Gdoo/Calendar/Services/CalendarService.php
@@ -0,0 +1,1176 @@
+
+ * Copyright (c) 2012 Bart Visscher
+ * Copyright (c) 2012 Georg Ehrke
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+/**
+ * This class manages our calendar objects
+ */
+
+use Auth;
+use App\Support\VObject;
+use Gdoo\Index\Services\AttachmentService;
+use Gdoo\Calendar\Models\Calendar;
+use Gdoo\Calendar\Models\CalendarObject;
+
+class CalendarService
+{
+ /**
+ * @brief timezone of the user
+ */
+ public static $tz;
+
+ /**
+ * We need to specify a max date, because we need to stop *somewhere*
+ *
+ * On 32 bit system the maximum for a signed integer is 2147483647, so
+ * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results
+ * in 2038-01-19 to avoid problems when the date is converted
+ * to a unix timestamp.
+ */
+ const MAX_DATE = '2038-01-01';
+
+ /**
+ * @return (string) $timezone as set by user or the default timezone
+ */
+ public static function getTimezone()
+ {
+ self::$tz = date_default_timezone_get();
+ return self::$tz;
+ }
+
+ public static function getCalendars($userId, $active = false)
+ {
+ $model = Calendar::where('userid', $userId);
+ if ($active) {
+ $model->where('active', $active);
+ }
+
+ // 如果用户没有日历则添加一个默认的
+ if ($model->count() == 0) {
+ Calendar::addDefaultCalendar($userId);
+ }
+
+ $rows = $model->get();
+
+ return $rows;
+ }
+
+ public static function getCalendar($id, $security = true)
+ {
+ $calendar = Calendar::find($id);
+
+ // FIXME: Correct arguments to just check for permissions
+ if ($security === true) {
+ if (Auth::id() == $calendar['userid']) {
+ return $calendar;
+ } else {
+ return false;
+ }
+ }
+ return $calendar;
+ }
+
+ public static function setCalendarActive($id, $active)
+ {
+ Calendar::where('id', $id)->update(array('active'=>$active));
+ }
+
+ public static function addCalendar($userid, $name, $components='VEVENT,VTODO,VJOURNAL', $timezone=null, $order=0, $color=null)
+ {
+ $uri = self::createURI();
+ $data = array(
+ 'userid' => $userid,
+ 'displayname' => $name,
+ 'principaluri' => 'principals/'.Auth::user()->id,
+ 'uri' => $uri,
+ 'ctag' => 1,
+ 'calendarorder' => $order,
+ 'calendarcolor' => $color,
+ 'timezone' => $timezone,
+ 'components' => $components
+ );
+ return Calendar::insertGetId($data);
+ }
+
+ public static function editCalendar($id, $name = null, $components = null, $timezone = null, $order = null, $color = null)
+ {
+ $calendar = self::getCalendar($id, false);
+ if ($calendar['userid'] != Auth::id()) {
+ throw new \Exception('您没有权限编辑此日历。');
+ }
+
+ if (is_null($name)) {
+ $name = $calendar['displayname'];
+ }
+ if (is_null($components)) {
+ $components = $calendar['components'];
+ }
+ if (is_null($timezone)) {
+ $timezone = $calendar['timezone'];
+ }
+ if (is_null($order)) {
+ $order = $calendar['calendarorder'];
+ }
+ if (is_null($color)) {
+ $color = $calendar['calendarcolor'];
+ }
+
+ $data = array(
+ 'displayname' => $name,
+ 'calendarorder' => $order,
+ 'calendarcolor' => $color,
+ 'timezone' => $timezone,
+ 'components' => $components,
+ );
+ Calendar::where('id', $id)->update($data);
+ self::touchCalendar($id);
+ return $id;
+ }
+
+ /**
+ * @brief gets the userid from a principal path
+ * @return string
+ */
+ public static function extractUserID($principaluri)
+ {
+ list($prefix, $userid) = \Sabre\DAV\URLUtil::splitPath($principaluri);
+ return $userid;
+ }
+
+ public static function addDefaultCalendar($userid = 0)
+ {
+ if ($userid == 0) {
+ $userid = Auth::id();
+ }
+ $id = self::addCalendar($userid, '默认日历');
+ return true;
+ }
+
+ /**
+ * @brief removes a calendar
+ * @param integer $id
+ * @return boolean
+ */
+ public static function deleteCalendar($id)
+ {
+ $calendar = self::getCalendar($id, false);
+
+ if ($calendar['userid'] != Auth::id()) {
+ throw new \Exception('您没有权限删除此日历。');
+ }
+ $events = CalendarObject::where('calendarid', $id)->get();
+ if (sizeof($events)) {
+ foreach ($events as $event) {
+ AttachmentService::remove($event->attachment);
+ }
+ CalendarObject::where('calendarid', $id)->delete();
+ }
+ Calendar::where('id', $id)->delete();
+ return true;
+ }
+
+ public static function createURI()
+ {
+ return substr(md5(rand().time()), 0, 10);
+ }
+
+ /**
+ * @brief Returns all objects of a calendar between $start and $end
+ * @param integer $id
+ * @param DateTime $start
+ * @param DateTime $end
+ * @return array
+ *
+ * The objects are associative arrays. You'll find the original vObject
+ * in ['calendardata']
+ */
+ public static function getRangeEvents($id, $start, $end, $shared = false)
+ {
+ $start = strtotime($start);
+ $end = strtotime($end);
+
+ /*
+ $model = CalendarObject::where(function ($q) use ($start, $end) {
+ $q->where('rrule', 0);
+ $q->whereBetween('firstoccurence', [$start, $end]);
+ $q->orWhereBetween('lastoccurence', [$start, $end]);
+ })->orWhere(function ($q) use ($start, $end) {
+ $q->where('rrule', 1)->where('firstoccurence', '<=', $end);
+ });
+ */
+ $model = CalendarObject::whereRaw('(
+ (firstoccurence between '.$start.' and '.$end.' or lastoccurence between '.$start.' and '.$end.')
+ or (rrule = 1 and firstoccurence <= '.$end.')
+ )');
+
+ // 获取分享事件
+ if ($shared == true) {
+ $model->whereIn('id', $id);
+ } elseif (count($id)) {
+ $model->whereIn('calendarid', $id);
+ }
+ $events = self::getRruleEvents($model->get(), $start, $end);
+
+ return $events;
+ }
+
+ public static function getRruleEvents($rows, $start, $end)
+ {
+ $result = [];
+ foreach ($rows as $key => $row) {
+ $vcalendar = \Sabre\VObject\Reader::read($row->calendardata);
+
+ $output = [];
+
+ if ($vcalendar->name === 'VEVENT') {
+ $vevent = $vcalendar;
+ } elseif (isset($vcalendar->VEVENT)) {
+ $vevent = $vcalendar->VEVENT;
+ } else {
+ return $output;
+ }
+
+ $allday = ($vcalendar->VEVENT->DTSTART->getDateType() == \Sabre\VObject\Property\DateTime::DATE) ? true : false;
+
+ $output = array(
+ 'id' => (int)$row->id,
+ 'calendarid' => (int)$row->calendarid,
+ 'title' => (isset($vevent->SUMMARY) && $vevent->SUMMARY->value) ? strtr($vevent->SUMMARY->value, array('\,' => ',', '\;' => ';')) : 'unnamed',
+ 'description' => (isset($vevent->DESCRIPTION) && $vevent->DESCRIPTION->value) ? strtr($vevent->DESCRIPTION->value, array('\,' => ',', '\;' => ';')) : '',
+ 'location' => (isset($vevent->LOCATION) && $vevent->LOCATION->value) ? strtr($vevent->LOCATION->value, array('\,' => ',', '\;' => ';')) : '',
+ 'lastmodified' => $row->lastmodified,
+ 'allDay' => $allday,
+ );
+
+ if ($vcalendar->VEVENT->RRULE) {
+ $start_dt = \DateTime::createFromFormat('U', $start);
+ $end_dt = \DateTime::createFromFormat('U', $end);
+ $vcalendar->expand($start_dt, $end_dt);
+ }
+
+ foreach ($vcalendar->getComponents() as $vevent) {
+ if (!($vevent instanceof \Sabre\VObject\Component\VEvent)) {
+ continue;
+ }
+ $rrule = self::generateStartEndDate($vevent->DTSTART, self::getDTEndFromVEvent($vevent), $allday, self::$tz);
+ $result[] = array_merge($output, $rrule);
+ }
+ }
+ return $result;
+ }
+
+ /**
+ * @brief Returns all objects of a calendar
+ * @param integer $id
+ * @return array
+ *
+ * The objects are associative arrays. You'll find the original vObject in
+ * ['calendardata']
+ */
+ public static function getEvent($id)
+ {
+ return CalendarObject::find($id);
+ }
+
+ /**
+ * @brief Adds an object
+ * @param integer $id
+ * @param integer $calendardata
+ * @param string $attachment
+ * @return integer
+ */
+ public static function add($id, $calendardata, $attachment = null)
+ {
+ $calendar = self::getCalendar($id);
+
+ if ($calendar['userid'] != Auth::id()) {
+ throw new \Exception('您没有权限添加事件到此日历。');
+ }
+
+ $extraData = self::getDenormalizedData($calendardata);
+ $uri = self::createURI().'.ics';
+ $data = array(
+ 'calendarid' => $id,
+ 'uri' => $uri,
+ 'calendardata' => $calendardata,
+ 'lastmodified' => time(),
+ 'attachment' => $attachment,
+ 'rrule' => $extraData['rrule'],
+ 'etag' => $extraData['etag'],
+ 'size' => $extraData['size'],
+ 'componenttype' => $extraData['componentType'],
+ 'firstoccurence' => $extraData['firstOccurence'],
+ 'lastoccurence' => $extraData['lastOccurence'],
+ );
+ $objectId = CalendarObject::insertGetId($data);
+ self::touchCalendar($id);
+ return $objectId;
+ }
+
+ /**
+ * @brief edits an object
+ * @param integer $id id of object
+ * @param string $data object
+ * @return boolean
+ */
+ public static function edit($id, $calendardata, $attachment = null)
+ {
+ $event = self::getEvent($id);
+ $calendar = self::getCalendar($event['calendarid']);
+
+ if ($calendar['userid'] != Auth::id()) {
+ throw new \Exception('您没有权限编辑此事件。');
+ }
+
+ if (empty($attachment)) {
+ $attachment = $event['attachment'];
+ }
+
+ $extraData = self::getDenormalizedData($calendardata);
+ $data = array(
+ 'calendardata' => $calendardata,
+ 'lastmodified' => time(),
+ 'attachment' => $attachment,
+ 'rrule' => $extraData['rrule'],
+ 'etag' => $extraData['etag'],
+ 'size' => $extraData['size'],
+ 'componenttype' => $extraData['componentType'],
+ 'firstoccurence' => $extraData['firstOccurence'],
+ 'lastoccurence' => $extraData['lastOccurence'],
+ );
+ CalendarObject::where('id', $id)->update($data);
+ self::touchCalendar($event['calendarid']);
+ return true;
+ }
+
+ /**
+ * @brief deletes an object
+ * @param integer $id id of object
+ * @return boolean
+ */
+ public static function remove($id)
+ {
+ $event = self::getEvent($id);
+ $calendar = self::getCalendar($event['calendarid']);
+
+ if ($calendar['userid'] != Auth::id()) {
+ throw new \Exception('您没有权限删除此事件。');
+ }
+
+ AttachmentService::remove($event['attachment']);
+ CalendarObject::where('id', $id)->delete();
+ self::touchCalendar($event['calendarid']);
+ return true;
+ }
+
+ public static function moveToCalendar($id, $calendarid)
+ {
+ $calendar = self::getCalendar($calendarid);
+ if ($calendar['userid'] != Auth::id()) {
+ throw new \Exception('您没有权限添加事件到此日历。');
+ }
+ CalendarObject::where('id', $id)->update(array('calendarid'=>$calendarid));
+ self::touchCalendar($calendarid);
+ return true;
+ }
+
+ /**
+ * Parses some information from calendar objects, used for optimized
+ * calendar-queries.
+ *
+ * Returns an array with the following keys:
+ * * etag
+ * * size
+ * * componentType
+ * * firstOccurence
+ * * lastOccurence
+ *
+ * @param string $calendarData
+ * @return array
+ */
+ public static function getDenormalizedData($calendarData)
+ {
+ $vObject = \Sabre\VObject\Reader::read($calendarData);
+
+ $componentType = null;
+ $component = null;
+ $firstOccurence = null;
+ $rrule = 0;
+
+ foreach ($vObject->getComponents() as $component) {
+ if ($component->name !== 'VTIMEZONE') {
+ $componentType = $component->name;
+ break;
+ }
+ }
+
+ if (!$componentType) {
+ throw new \Sabre\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component');
+ }
+
+ if ($componentType === 'VEVENT') {
+ $firstOccurence = $component->DTSTART->getDateTime()->getTimeStamp();
+ // Finding the last occurence is a bit harder
+ if (!isset($component->RRULE)) {
+ if (isset($component->DTEND)) {
+ $lastOccurence = $component->DTEND->getDateTime()->getTimeStamp();
+ } elseif (isset($component->DURATION)) {
+ $endDate = clone $component->DTSTART->getDateTime();
+ $endDate->add(\VObject\DateTimeParser::parse($component->DURATION->getValue()));
+ $lastOccurence = $endDate->getTimeStamp();
+ } elseif (!$component->DTSTART->hasTime()) {
+ $endDate = clone $component->DTSTART->getDateTime();
+ $endDate->modify('+1 day');
+ $lastOccurence = $endDate->getTimeStamp();
+ } else {
+ $lastOccurence = $firstOccurence;
+ }
+ } else {
+ $it = new \Sabre\VObject\RecurrenceIterator($vObject, (string)$component->UID);
+ $maxDate = new \DateTime(self::MAX_DATE);
+ if ($it->isInfinite()) {
+ $lastOccurence = $maxDate->getTimeStamp();
+ } else {
+ $end = $it->getDtEnd();
+ while ($it->valid() && $end < $maxDate) {
+ $end = $it->getDtEnd();
+ $it->next();
+ }
+ $lastOccurence = $end->getTimeStamp();
+ }
+ $rrule = 1;
+ }
+ }
+
+ return array(
+ 'etag' => md5($calendarData),
+ 'size' => strlen($calendarData),
+ 'rrule' => $rrule,
+ 'componentType' => $componentType,
+ 'firstOccurence' => $firstOccurence,
+ 'lastOccurence' => $lastOccurence,
+ );
+ }
+
+ /**
+ * @brief returns the DTEND of an $vevent object
+ * @param object $vevent vevent object
+ * @return object
+ */
+ public static function getDTEndFromVEvent($vevent)
+ {
+ if ($vevent->DTEND) {
+ $dtend = $vevent->DTEND;
+ } else {
+ $dtend = clone $vevent->DTSTART;
+ // clone creates a shallow copy, also clone DateTime
+ $dtend->setDateTime(clone $dtend->getDateTime(), $dtend->getDateType());
+
+ if ($vevent->DURATION) {
+ $duration = strval($vevent->DURATION);
+ $invert = 0;
+ if ($duration[0] == '-') {
+ $duration = substr($duration, 1);
+ $invert = 1;
+ }
+ if ($duration[0] == '+') {
+ $duration = substr($duration, 1);
+ }
+ $interval = new \DateInterval($duration);
+ $interval->invert = $invert;
+ $dtend->getDateTime()->add($interval);
+ }
+ }
+ return $dtend;
+ }
+
+ /**
+ * @brief Remove all properties which should not be exported for the AccessClass Confidential
+ * @param string $id Event ID
+ * @param VObject $vobject Sabre VObject
+ * @return object
+ */
+ public static function cleanByAccessClass($id, $vobject)
+ {
+ // Do not clean your own calendar
+ if (self::getowner($id) === Auth::id()) {
+ return $vobject;
+ }
+
+ if (isset($vobject->VEVENT)) {
+ $velement = $vobject->VEVENT;
+ } elseif (isset($vobject->VJOURNAL)) {
+ $velement = $vobject->VJOURNAL;
+ } elseif (isset($vobject->VTODO)) {
+ $velement = $vobject->VTODO;
+ }
+
+ if (isset($velement->CLASS) && $velement->CLASS->value == 'CONFIDENTIAL') {
+ foreach ($velement->children as &$property) {
+ switch ($property->name) {
+ case 'CREATED':
+ case 'DTSTART':
+ case 'RRULE':
+ case 'DURATION':
+ case 'DTEND':
+ case 'CLASS':
+ case 'UID':
+ break;
+ case 'SUMMARY':
+ $property->value = 'Busy';
+ break;
+ default:
+ $velement->__unset($property->name);
+ unset($property);
+ break;
+ }
+ }
+ }
+ return $vobject;
+ }
+
+ /**
+ * @brief Get the permissions determined by the access class of an event/todo/journal
+ * @param VObject $vobject Sabre VObject
+ * @return (int) $permissions - CRUDS permissions
+ * @see OCP\Share
+ */
+ public static function getAccessClassPermissions($vobject)
+ {
+ if (isset($vobject->VEVENT)) {
+ $velement = $vobject->VEVENT;
+ } elseif (isset($vobject->VJOURNAL)) {
+ $velement = $vobject->VJOURNAL;
+ } elseif (isset($vobject->VTODO)) {
+ $velement = $vobject->VTODO;
+ }
+ $accessclass = $velement->getAsString('CLASS');
+ return static::getAccessClassPermissions($accessclass);
+ }
+
+ /**
+ * @brief returns the options for the access class of an event
+ * @return array - valid inputs for the access class of an event
+ */
+ public static function getAccessClassOptions()
+ {
+ return array(
+ 'PUBLIC' => '共享',
+ 'PRIVATE' => '私人',
+ 'CONFIDENTIAL' => '保密',
+ );
+ }
+
+ /**
+ * @brief returns the options for the repeat rule of an repeating event
+ * @return array - valid inputs for the repeat rule of an repeating event
+ */
+ public static function getValarmOptions()
+ {
+ return array(
+ 'time' => array(
+ 'PT0S' => '事件发生时',
+ '-PT5M' => '5分钟前',
+ '-PT15M' => '15分钟前',
+ '-PT30M' => '30分钟前',
+ '-PT1H' => '1小时前',
+ '-PT2H' => '2小时前',
+ '-P1D' => '1天前',
+ '-P2D' => '2天前',
+ '-P1W' => '1周前'
+ ),
+ 'day' => array(
+ 'PT9H' => '事件发生当天(9:00)',
+ '-PT15H' => '1天前(9:00)',
+ '-P1DT15H' => '2天前(9:00)',
+ '-P6DT15H' => '1周前'
+ )
+ );
+ }
+
+ /**
+ * @brief returns the options for the repeat rule of an repeating event
+ * @return array - valid inputs for the repeat rule of an repeating event
+ */
+ public static function getRepeatOptions()
+ {
+ return array(
+ 'doesnotrepeat' => ' - ',
+ 'daily' => '每天',
+ 'weekly' => '每周',
+ 'weekday' => '每个工作日',
+ 'biweekly' => '每两周',
+ 'monthly' => '每月',
+ 'yearly' => '每年'
+ );
+ }
+
+ /**
+ * @brief returns the options for the end of an repeating event
+ * @return array - valid inputs for the end of an repeating events
+ */
+ public static function getEndOptions()
+ {
+ return array(
+ 'never' => '从不',
+ 'count' => '根据发生次',
+ 'date' => '根据日期'
+ );
+ }
+
+ /**
+ * @brief returns the options for an monthly repeating event
+ * @return array - valid inputs for monthly repeating events
+ */
+ public static function getMonthOptions()
+ {
+ return array(
+ 'monthday' => '根据月天',
+ 'weekday' => '根据星期'
+ );
+ }
+
+ /**
+ * @brief returns the options for an weekly repeating event
+ * @return array - valid inputs for weekly repeating events
+ */
+ public static function getWeeklyOptions()
+ {
+ return array(
+ 'MO' => '星期一',
+ 'TU' => '星期二',
+ 'WE' => '星期三',
+ 'TH' => '星期四',
+ 'FR' => '星期五',
+ 'SA' => '星期六',
+ 'SU' => '星期日'
+ );
+ }
+
+ /**
+ * @brief returns the options for an monthly repeating event which occurs on specific weeks of the month
+ * @return array - valid inputs for monthly repeating events
+ */
+ public static function getWeekofMonth()
+ {
+ return array(
+ 'auto' => '事件每月发生的周数',
+ '1' => '首先',
+ '2' => '其次',
+ '3' => '第三',
+ '4' => '第四',
+ '5' => '第五',
+ '-1' => '最后'
+ );
+ }
+
+ /**
+ * @brief returns the options for an yearly repeating event which occurs on specific days of the year
+ * @return array - valid inputs for yearly repeating events
+ */
+ public static function getByYearDayOptions()
+ {
+ $return = array();
+ foreach (range(1, 366) as $num) {
+ $return[(string)$num] = (string)$num;
+ }
+ return $return;
+ }
+
+ /**
+ * @brief returns the options for an yearly or monthly repeating event which occurs on specific days of the month
+ * @return array - valid inputs for yearly or monthly repeating events
+ */
+ public static function getByMonthDayOptions()
+ {
+ $return = array();
+ foreach (range(1, 31) as $num) {
+ $return[(string)$num] = (string)$num;
+ }
+ return $return;
+ }
+
+ /**
+ * @brief returns the options for an yearly repeating event which occurs on specific month of the year
+ * @return array - valid inputs for yearly repeating events
+ */
+ public static function getByMonthOptions()
+ {
+ return array(
+ '1' => '一月',
+ '2' => '二月',
+ '3' => '三月',
+ '4' => '四月',
+ '5' => '五月',
+ '6' => '六月',
+ '7' => '七月',
+ '8' => '八月',
+ '9' => '九月',
+ '10' => '十月',
+ '11' => '十一月',
+ '12' => '十二月'
+ );
+ }
+
+ /**
+ * @brief returns the options for an yearly repeating event
+ * @return array - valid inputs for yearly repeating events
+ */
+ public static function getYearOptions()
+ {
+ return array(
+ 'bydate' => '根据时间日期',
+ 'byyearday' => '根据年数',
+ 'byweekno' => '根据周数',
+ 'bydaymonth' => '根据天和月'
+ );
+ }
+
+ /**
+ * @brief returns the options for an yearly repeating event which occurs on specific week numbers of the year
+ * @return array - valid inputs for yearly repeating events
+ */
+ public static function getByWeekNoOptions()
+ {
+ return range(1, 52);
+ }
+
+ /**
+ * @brief validates a request
+ * @param array $request
+ * @return mixed (array / boolean)
+ */
+ public static function validateRequest($request)
+ {
+ $errnum = 0;
+ $errarr = array('title'=>'false', 'cal'=>'false', 'from'=>'false', 'fromtime'=>'false', 'to'=>'false', 'totime'=>'false', 'endbeforestart'=>'false');
+ if ($request['title'] == '') {
+ $errarr['title'] = 'true';
+ $errnum++;
+ }
+
+ list($fromyear, $frommonth, $fromday) = explode('-', $request['from']);
+ if (!checkdate($frommonth, $fromday, $fromyear)) {
+ $errarr['from'] = 'true';
+ $errnum++;
+ }
+ $allday = isset($request['allday']);
+ if (!$allday && self::checkTime(urldecode($request['fromtime']))) {
+ $errarr['fromtime'] = 'true';
+ $errnum++;
+ }
+
+ list($toyear, $tomonth, $today) = explode('-', $request['to']);
+ if (!checkdate($tomonth, $today, $toyear)) {
+ $errarr['to'] = 'true';
+ $errnum++;
+ }
+ if ($request['repeat'] != 'doesnotrepeat') {
+ if (is_nan($request['interval']) && $request['interval'] != '') {
+ $errarr['interval'] = 'true';
+ $errnum++;
+ }
+ if (array_key_exists('repeat', $request) && !array_key_exists($request['repeat'], self::getRepeatOptions())) {
+ $errarr['repeat'] = 'true';
+ $errnum++;
+ }
+ if (array_key_exists('advanced_month_select', $request) && !array_key_exists($request['advanced_month_select'], self::getMonthOptions())) {
+ $errarr['advanced_month_select'] = 'true';
+ $errnum++;
+ }
+ if (array_key_exists('advanced_year_select', $request) && !array_key_exists($request['advanced_year_select'], self::getYearOptions())) {
+ $errarr['advanced_year_select'] = 'true';
+ $errnum++;
+ }
+ if (array_key_exists('weekofmonthoptions', $request) && !array_key_exists($request['weekofmonthoptions'], self::getWeekofMonth())) {
+ $errarr['weekofmonthoptions'] = 'true';
+ $errnum++;
+ }
+ if ($request['end'] != 'never') {
+ if (!array_key_exists($request['end'], self::getEndOptions())) {
+ $errarr['end'] = 'true';
+ $errnum++;
+ }
+ if ($request['end'] == 'count' && is_nan($request['byoccurrences'])) {
+ $errarr['byoccurrences'] = 'true';
+ $errnum++;
+ }
+ if ($request['end'] == 'date') {
+ list($bydate_year, $bydate_month, $bydate_day) = explode('-', $request['bydate']);
+ if (!checkdate($bydate_month, $bydate_day, $bydate_year)) {
+ $errarr['bydate'] = 'true';
+ $errnum++;
+ }
+ }
+ }
+ if (array_key_exists('weeklyoptions', $request)) {
+ foreach ($request['weeklyoptions'] as $option) {
+ if (!array_key_exists($option, self::getWeeklyOptions())) {
+ $errarr['weeklyoptions'] = 'true';
+ $errnum++;
+ }
+ }
+ }
+ if (array_key_exists('byyearday', $request)) {
+ foreach ($request['byyearday'] as $option) {
+ if (!array_key_exists($option, self::getByYearDayOptions())) {
+ $errarr['byyearday'] = 'true';
+ $errnum++;
+ }
+ }
+ }
+ if (array_key_exists('weekofmonthoptions', $request)) {
+ if (is_nan((double)$request['weekofmonthoptions'])) {
+ $errarr['weekofmonthoptions'] = 'true';
+ $errnum++;
+ }
+ }
+ if (array_key_exists('bymonth', $request)) {
+ foreach ($request['bymonth'] as $option) {
+ if (!array_key_exists($option, self::getByMonthOptions())) {
+ $errarr['bymonth'] = 'true';
+ $errnum++;
+ }
+ }
+ }
+ if (array_key_exists('byweekno', $request)) {
+ foreach ($request['byweekno'] as $option) {
+ if (!array_key_exists($option, self::getByWeekNoOptions())) {
+ $errarr['byweekno'] = 'true';
+ $errnum++;
+ }
+ }
+ }
+ if (array_key_exists('bymonthday', $request)) {
+ foreach ($request['bymonthday'] as $option) {
+ if (!array_key_exists($option, self::getByMonthDayOptions())) {
+ $errarr['bymonthday'] = 'true';
+ $errnum++;
+ }
+ }
+ }
+ }
+ if (!$allday && self::checkTime(urldecode($request['totime']))) {
+ $errarr['totime'] = 'true';
+ $errnum++;
+ }
+ if ($today < $fromday && $frommonth == $tomonth && $fromyear == $toyear) {
+ $errarr['endbeforestart'] = 'true';
+ $errnum++;
+ }
+ if ($today == $fromday && $frommonth > $tomonth && $fromyear == $toyear) {
+ $errarr['endbeforestart'] = 'true';
+ $errnum++;
+ }
+ if ($today == $fromday && $frommonth == $tomonth && $fromyear > $toyear) {
+ $errarr['endbeforestart'] = 'true';
+ $errnum++;
+ }
+ if (!$allday && $fromday == $today && $frommonth == $tomonth && $fromyear == $toyear) {
+ list($tohours, $tominutes) = explode(':', $request['totime']);
+ list($fromhours, $fromminutes) = explode(':', $request['fromtime']);
+ if ($tohours < $fromhours) {
+ $errarr['endbeforestart'] = 'true';
+ $errnum++;
+ }
+ if ($tohours == $fromhours && $tominutes < $fromminutes) {
+ $errarr['endbeforestart'] = 'true';
+ $errnum++;
+ }
+ }
+ if ($errnum) {
+ return $errarr;
+ }
+ return false;
+ }
+
+ /**
+ * @brief validates time
+ * @param string $time
+ * @return boolean
+ */
+ protected static function checkTime($time)
+ {
+ if (strpos($time, ':') === false) {
+ return true;
+ }
+ list($hours, $minutes) = explode(':', $time);
+ return empty($time) || $hours < 0 || $hours > 24 || $minutes < 0 || $minutes > 60;
+ }
+
+ /**
+ * @brief creates an VCalendar Object from the request data
+ * @param array $request
+ * @return object created $vcalendar
+ */
+ public static function createVCalendarFromRequest($request)
+ {
+ $vcalendar = new VObject('VCALENDAR');
+ $vcalendar->add('PRODID', 'gdoo.com Calendar');
+ $vcalendar->add('VERSION', '2.0');
+
+ $vevent = new VObject('VEVENT');
+ $vcalendar->add($vevent);
+ $vevent->setDateTime('CREATED', 'now', \Sabre\VObject\Property\DateTime::UTC);
+ $vevent->setUID();
+
+ return self::updateVCalendarFromRequest($request, $vcalendar);
+ }
+
+ /**
+ * @brief updates an VCalendar Object from the request data
+ * @param array $request
+ * @param object $vcalendar
+ * @return object updated $vcalendar
+ */
+ public static function updateVCalendarFromRequest($request, $vobject)
+ {
+ $accessclass = $request["accessclass"];
+ $title = $request["title"];
+ $location = $request["location"];
+ $categories = $request["categories"];
+ $allday = isset($request["allday"]);
+
+ $from = $request["from"];
+ $to = $request["to"];
+
+ if (!$allday) {
+ $fromtime = $request['fromtime'];
+ $totime = $request['totime'];
+ }
+ $vevent = $vobject->VEVENT;
+ $description = $request["description"];
+ $repeat = $request["repeat"];
+ if ($repeat != 'doesnotrepeat') {
+ $rrule = '';
+ $interval = $request['interval'];
+ $end = $request['end'];
+ $byoccurrences = $request['byoccurrences'];
+
+ switch ($repeat) {
+ case 'daily':
+ $rrule .= 'FREQ=DAILY';
+ break;
+ case 'weekly':
+ $rrule .= 'FREQ=WEEKLY';
+ if (array_key_exists('weeklyoptions', $request)) {
+ $byday = '';
+ foreach ($request['weeklyoptions'] as $days) {
+ if ($byday == '') {
+ $byday .= $days;
+ } else {
+ $byday .= ',' .$days;
+ }
+ }
+ $rrule .= ';BYDAY=' . $byday;
+ }
+ break;
+ case 'weekday':
+ $rrule .= 'FREQ=WEEKLY';
+ $rrule .= ';BYDAY=MO,TU,WE,TH,FR';
+ break;
+ case 'biweekly':
+ $rrule .= 'FREQ=WEEKLY';
+ $interval = $interval * 2;
+ break;
+ case 'monthly':
+ $rrule .= 'FREQ=MONTHLY';
+ if ($request['advanced_month_select'] == 'monthday') {
+ break;
+ } elseif ($request['advanced_month_select'] == 'weekday') {
+ if ($request['weekofmonthoptions'] == 'auto') {
+ list($_year, $_month, $_day) = explode('-', $from);
+ $weekofmonth = floor($_day/7);
+ } else {
+ $weekofmonth = $request['weekofmonthoptions'];
+ }
+ $byday = '';
+ foreach ($request['weeklyoptions'] as $day) {
+ if ($byday == '') {
+ $byday .= $weekofmonth . $day;
+ } else {
+ $byday .= ',' . $weekofmonth . $day;
+ }
+ }
+ if ($byday == '') {
+ $byday = 'MO,TU,WE,TH,FR,SA,SU';
+ }
+ $rrule .= ';BYDAY=' . $byday;
+ }
+ break;
+ case 'yearly':
+ $rrule .= 'FREQ=YEARLY';
+ if ($request['advanced_year_select'] == 'bydate') {
+ } elseif ($request['advanced_year_select'] == 'byyearday') {
+ list($_year, $_month, $_day) = explode('-', $from);
+ $byyearday = date('z', mktime(0, 0, 0, $_month, $_day, $_year)) + 1;
+ if (array_key_exists('byyearday', $request)) {
+ foreach ($request['byyearday'] as $yearday) {
+ $byyearday .= ',' . $yearday;
+ }
+ }
+ $rrule .= ';BYYEARDAY=' . $byyearday;
+ } elseif ($request['advanced_year_select'] == 'byweekno') {
+ list($_year, $_month, $_day) = explode('-', $from);
+ $rrule .= ';BYDAY=' . strtoupper(substr(date('l', mktime(0, 0, 0, $_month, $_day, $_year)), 0, 2));
+ $byweekno = '';
+ foreach ($request['byweekno'] as $weekno) {
+ if ($byweekno == '') {
+ $byweekno = $weekno;
+ } else {
+ $byweekno .= ',' . $weekno;
+ }
+ }
+ $rrule .= ';BYWEEKNO=' . $byweekno;
+ } elseif ($request['advanced_year_select'] == 'bydaymonth') {
+ if (array_key_exists('weeklyoptions', $request)) {
+ $byday = '';
+ foreach ($request['weeklyoptions'] as $day) {
+ if ($byday == '') {
+ $byday .= $day;
+ } else {
+ $byday .= ',' . $day;
+ }
+ }
+ $rrule .= ';BYDAY=' . $byday;
+ }
+ if (array_key_exists('bymonth', $request)) {
+ $bymonth = '';
+ foreach ($request['bymonth'] as $month) {
+ if ($bymonth == '') {
+ $bymonth .= $month;
+ } else {
+ $bymonth .= ',' . $month;
+ }
+ }
+ $rrule .= ';BYMONTH=' . $bymonth;
+ }
+ if (array_key_exists('bymonthday', $request)) {
+ $bymonthday = '';
+ foreach ($request['bymonthday'] as $monthday) {
+ if ($bymonthday == '') {
+ $bymonthday .= $monthday;
+ } else {
+ $bymonthday .= ',' . $monthday;
+ }
+ }
+ $rrule .= ';BYMONTHDAY=' . $bymonthday;
+ }
+ }
+ break;
+ default:
+ break;
+ }
+ if ($interval != '') {
+ $rrule .= ';INTERVAL=' . $interval;
+ }
+ if ($end == 'count') {
+ $rrule .= ';COUNT=' . $byoccurrences;
+ }
+ if ($end == 'date') {
+ list($bydate_year, $bydate_month, $bydate_day) = explode('-', $request['bydate']);
+
+ $until = $bydate_year . $bydate_month . $bydate_day;
+ if ($allday) {
+ $until = $until;
+ } else {
+ $until = $until.'T155959Z';
+ }
+ $rrule .= ';UNTIL=' . $until;
+ }
+ $repeat = "true";
+ } else {
+ $rrule = '';
+ $repeat = "false";
+ }
+ $vevent->setString('RRULE', $rrule);
+
+ $vevent->setDateTime('LAST-MODIFIED', 'now', \Sabre\VObject\Property\DateTime::UTC);
+ $vevent->setDateTime('DTSTAMP', 'now', \Sabre\VObject\Property\DateTime::UTC);
+ $vevent->setString('SUMMARY', $title);
+
+ unset($vevent->VALARM);
+
+ $valarm = \Sabre\VObject\Component::create('VALARM');
+ $valarm->ACTION = 'DTSTART';
+ $valarm->SUMMARY = 'Alarm notification';
+
+ if ($allday) {
+ $start = new \DateTime($from);
+ $end = new \DateTime($to.' +1 day');
+ $vevent->setDateTime('DTSTART', $start, \Sabre\VObject\Property\DateTime::DATE);
+ $vevent->setDateTime('DTEND', $end, \Sabre\VObject\Property\DateTime::DATE);
+ $valarm->TRIGGER = $request['valarm_day'];
+ } else {
+ $timezone = new \DateTimeZone(self::$tz);
+ $start = new \DateTime($from.' '.$fromtime, $timezone);
+ $end = new \DateTime($to.' '.$totime, $timezone);
+ $vevent->setDateTime('DTSTART', $start, \Sabre\VObject\Property\DateTime::LOCALTZ);
+ $vevent->setDateTime('DTEND', $end, \Sabre\VObject\Property\DateTime::LOCALTZ);
+ $valarm->TRIGGER = $request['valarm_time'];
+ }
+ $vevent->add($valarm);
+
+ unset($vevent->DURATION);
+
+ $vevent->setString('CLASS', $accessclass);
+ $vevent->setString('LOCATION', $location);
+ $vevent->setString('DESCRIPTION', $description);
+ $vevent->setString('CATEGORIES', $categories);
+
+ return $vobject;
+ }
+
+ /**
+ * @brief Updates ctag for calendar
+ * @param integer $id
+ * @return boolean
+ */
+ public static function touchCalendar($id)
+ {
+ $calendar = Calendar::find($id);
+ $calendar->ctag + 1;
+ $calendar->save();
+ return true;
+ }
+
+ /**
+ * @brief converts the start_dt and end_dt to a new timezone
+ * @param object $dtstart
+ * @param object $dtend
+ * @param boolean $allday
+ * @param string $tz
+ * @return array
+ */
+ public static function generateStartEndDate($dtstart, $dtend, $allday, $tz)
+ {
+ $start_dt = $dtstart->getDateTime();
+ $end_dt = $dtend->getDateTime();
+ $return = [];
+
+ if ($allday) {
+ $return['start'] = $start_dt->format('Y-m-d');
+ //$end_dt->modify('-1 minute');
+ while ($start_dt >= $end_dt) {
+ $end_dt->modify('+1 day');
+ }
+ $return['end'] = $end_dt->format('Y-m-d');
+ } else {
+ if ($dtstart->getDateType() !== \Sabre\VObject\Property\DateTime::LOCAL) {
+ $start_dt->setTimezone(new \DateTimeZone($tz));
+ $end_dt->setTimezone(new \DateTimeZone($tz));
+ }
+ $return['start'] = $start_dt->format('Y-m-d H:i:s');
+ $return['end'] = $end_dt->format('Y-m-d H:i:s');
+ }
+ return $return;
+ }
+}
+CalendarService::getTimezone();
\ No newline at end of file
diff --git a/app/Gdoo/Calendar/config.php b/app/Gdoo/Calendar/config.php
new file mode 100644
index 00000000..cfeb6fe6
--- /dev/null
+++ b/app/Gdoo/Calendar/config.php
@@ -0,0 +1,64 @@
+ "日程管理",
+ "version" => "1.0",
+ "description" => "日历日程安排,下属日历查看,支持流行的caldav协议。",
+ 'menus' => [
+ ['name' => '工作', 'id' => 'work'],
+ ['name' => '日程管理', 'id' => 'calendar_calendar_index', 'parent' => 'work', 'url' => 'calendar/calendar/index'],
+ ],
+ "controllers" => [
+ "calendar" => [
+ "name" => "日历",
+ "actions" => [
+ "index" => [
+ "name" => "列表"
+ ],
+ "calendar" => [
+ "name" => "读取日历"
+ ],
+ "active" => [
+ "name" => "活动日历"
+ ],
+ "view" => [
+ "name" => "查看"
+ ],
+ "add" => [
+ "name" => "新建"
+ ],
+ "delete" => [
+ "name" => "删除"
+ ],
+ "help" => [
+ "name" => "帮助"
+ ]
+ ]
+ ],
+ "event" => [
+ "name" => "事件",
+ "actions" => [
+ "index" => [
+ "name" => "列表"
+ ],
+ "resize" => [
+ "name" => "调整事件"
+ ],
+ "move" => [
+ "name" => "移动事件"
+ ],
+ "view" => [
+ "name" => "查看"
+ ],
+ "add" => [
+ "name" => "新建"
+ ],
+ "edit" => [
+ "name" => "编辑"
+ ],
+ "delete" => [
+ "name" => "删除"
+ ]
+ ]
+ ]
+ ]
+];
diff --git a/app/Gdoo/Calendar/views/calendar/add.blade.php b/app/Gdoo/Calendar/views/calendar/add.blade.php
new file mode 100644
index 00000000..af2c7bea
--- /dev/null
+++ b/app/Gdoo/Calendar/views/calendar/add.blade.php
@@ -0,0 +1,44 @@
+
+
+
diff --git a/app/Gdoo/Calendar/views/calendar/help.blade.php b/app/Gdoo/Calendar/views/calendar/help.blade.php
new file mode 100644
index 00000000..257954b5
--- /dev/null
+++ b/app/Gdoo/Calendar/views/calendar/help.blade.php
@@ -0,0 +1,22 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/business/index.blade.php b/app/Gdoo/Customer/views/business/index.blade.php
new file mode 100644
index 00000000..6688d298
--- /dev/null
+++ b/app/Gdoo/Customer/views/business/index.blade.php
@@ -0,0 +1,73 @@
+
+
+
+ @include('business/query')
+
+
+
+
+
+
diff --git a/app/Gdoo/Customer/views/business/query.blade.php b/app/Gdoo/Customer/views/business/query.blade.php
new file mode 100644
index 00000000..e59e4afa
--- /dev/null
+++ b/app/Gdoo/Customer/views/business/query.blade.php
@@ -0,0 +1,29 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/complaint/create.blade.php b/app/Gdoo/Customer/views/complaint/create.blade.php
new file mode 100644
index 00000000..0d031496
--- /dev/null
+++ b/app/Gdoo/Customer/views/complaint/create.blade.php
@@ -0,0 +1,32 @@
+
+
+
+
+ {{$form['btn']}}
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/complaint/index.blade.php b/app/Gdoo/Customer/views/complaint/index.blade.php
new file mode 100644
index 00000000..71c011cb
--- /dev/null
+++ b/app/Gdoo/Customer/views/complaint/index.blade.php
@@ -0,0 +1,60 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/complaint/print.blade.php b/app/Gdoo/Customer/views/complaint/print.blade.php
new file mode 100644
index 00000000..ca2d71da
--- /dev/null
+++ b/app/Gdoo/Customer/views/complaint/print.blade.php
@@ -0,0 +1 @@
+{{$form['tpl']}}
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/complaint/progress.blade.php b/app/Gdoo/Customer/views/complaint/progress.blade.php
new file mode 100644
index 00000000..c2097abb
--- /dev/null
+++ b/app/Gdoo/Customer/views/complaint/progress.blade.php
@@ -0,0 +1,131 @@
+
+ @include('headers')
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/contact/create.blade.php b/app/Gdoo/Customer/views/contact/create.blade.php
new file mode 100644
index 00000000..90bd8375
--- /dev/null
+++ b/app/Gdoo/Customer/views/contact/create.blade.php
@@ -0,0 +1,12 @@
+
+
+
+
+ {{$form['btn']}}
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/contact/dialog.blade.php b/app/Gdoo/Customer/views/contact/dialog.blade.php
new file mode 100644
index 00000000..15127bc9
--- /dev/null
+++ b/app/Gdoo/Customer/views/contact/dialog.blade.php
@@ -0,0 +1,116 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/contact/index.blade.php b/app/Gdoo/Customer/views/contact/index.blade.php
new file mode 100644
index 00000000..94953cd2
--- /dev/null
+++ b/app/Gdoo/Customer/views/contact/index.blade.php
@@ -0,0 +1,59 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/contact/show.blade.php b/app/Gdoo/Customer/views/contact/show.blade.php
new file mode 100644
index 00000000..12dcdb0e
--- /dev/null
+++ b/app/Gdoo/Customer/views/contact/show.blade.php
@@ -0,0 +1,14 @@
+
+
+
+
+
职位
+
+
+ {{$header['tpl']}}
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/customer/create.blade.php b/app/Gdoo/Customer/views/customer/create.blade.php
new file mode 100644
index 00000000..31adf0d1
--- /dev/null
+++ b/app/Gdoo/Customer/views/customer/create.blade.php
@@ -0,0 +1,42 @@
+
+
+
+
+
+ {{$form['btn']}}
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/customer/dialog.blade.php b/app/Gdoo/Customer/views/customer/dialog.blade.php
new file mode 100644
index 00000000..07690d66
--- /dev/null
+++ b/app/Gdoo/Customer/views/customer/dialog.blade.php
@@ -0,0 +1,132 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/customer/index.blade.php b/app/Gdoo/Customer/views/customer/index.blade.php
new file mode 100644
index 00000000..0dbbe0fc
--- /dev/null
+++ b/app/Gdoo/Customer/views/customer/index.blade.php
@@ -0,0 +1,88 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/customer/priceEdit.blade.php b/app/Gdoo/Customer/views/customer/priceEdit.blade.php
new file mode 100644
index 00000000..b6db23e4
--- /dev/null
+++ b/app/Gdoo/Customer/views/customer/priceEdit.blade.php
@@ -0,0 +1,15 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/customerApply/create.blade.php b/app/Gdoo/Customer/views/customerApply/create.blade.php
new file mode 100644
index 00000000..45f95a3a
--- /dev/null
+++ b/app/Gdoo/Customer/views/customerApply/create.blade.php
@@ -0,0 +1,34 @@
+
+
+
+
+ {{$form['btn']}}
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/customerApply/index.blade.php b/app/Gdoo/Customer/views/customerApply/index.blade.php
new file mode 100644
index 00000000..628b1154
--- /dev/null
+++ b/app/Gdoo/Customer/views/customerApply/index.blade.php
@@ -0,0 +1,60 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/customerClass/create.blade.php b/app/Gdoo/Customer/views/customerClass/create.blade.php
new file mode 100644
index 00000000..e74512c0
--- /dev/null
+++ b/app/Gdoo/Customer/views/customerClass/create.blade.php
@@ -0,0 +1,3 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/customerClass/dialog.blade.php b/app/Gdoo/Customer/views/customerClass/dialog.blade.php
new file mode 100644
index 00000000..1db92e72
--- /dev/null
+++ b/app/Gdoo/Customer/views/customerClass/dialog.blade.php
@@ -0,0 +1,122 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/customerClass/index.blade.php b/app/Gdoo/Customer/views/customerClass/index.blade.php
new file mode 100644
index 00000000..7987177c
--- /dev/null
+++ b/app/Gdoo/Customer/views/customerClass/index.blade.php
@@ -0,0 +1,64 @@
+{{$header["js"]}}
+
+ @include('headers')
+
+
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/deliveryAddress/create.blade.php b/app/Gdoo/Customer/views/deliveryAddress/create.blade.php
new file mode 100644
index 00000000..fb589523
--- /dev/null
+++ b/app/Gdoo/Customer/views/deliveryAddress/create.blade.php
@@ -0,0 +1,4 @@
+
+
diff --git a/app/Gdoo/Customer/views/deliveryAddress/dialog.blade.php b/app/Gdoo/Customer/views/deliveryAddress/dialog.blade.php
new file mode 100644
index 00000000..15127bc9
--- /dev/null
+++ b/app/Gdoo/Customer/views/deliveryAddress/dialog.blade.php
@@ -0,0 +1,116 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/deliveryAddress/index.blade.php b/app/Gdoo/Customer/views/deliveryAddress/index.blade.php
new file mode 100644
index 00000000..592b9903
--- /dev/null
+++ b/app/Gdoo/Customer/views/deliveryAddress/index.blade.php
@@ -0,0 +1,59 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/deliveryAddress/show.blade.php b/app/Gdoo/Customer/views/deliveryAddress/show.blade.php
new file mode 100644
index 00000000..12dcdb0e
--- /dev/null
+++ b/app/Gdoo/Customer/views/deliveryAddress/show.blade.php
@@ -0,0 +1,14 @@
+
+
+
+
+
职位
+
+
+ {{$header['tpl']}}
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/price/create.blade.php b/app/Gdoo/Customer/views/price/create.blade.php
new file mode 100644
index 00000000..57d96909
--- /dev/null
+++ b/app/Gdoo/Customer/views/price/create.blade.php
@@ -0,0 +1,138 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/price/index.blade.php b/app/Gdoo/Customer/views/price/index.blade.php
new file mode 100644
index 00000000..180275af
--- /dev/null
+++ b/app/Gdoo/Customer/views/price/index.blade.php
@@ -0,0 +1,60 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/price/referCustomer.blade.php b/app/Gdoo/Customer/views/price/referCustomer.blade.php
new file mode 100644
index 00000000..d2dfaa12
--- /dev/null
+++ b/app/Gdoo/Customer/views/price/referCustomer.blade.php
@@ -0,0 +1,74 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/reconcile/query.blade.php b/app/Gdoo/Customer/views/reconcile/query.blade.php
new file mode 100644
index 00000000..2e6b606d
--- /dev/null
+++ b/app/Gdoo/Customer/views/reconcile/query.blade.php
@@ -0,0 +1,129 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/region/create.blade.php b/app/Gdoo/Customer/views/region/create.blade.php
new file mode 100644
index 00000000..4ad41f62
--- /dev/null
+++ b/app/Gdoo/Customer/views/region/create.blade.php
@@ -0,0 +1,3 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/region/dialog.blade.php b/app/Gdoo/Customer/views/region/dialog.blade.php
new file mode 100644
index 00000000..88e69f40
--- /dev/null
+++ b/app/Gdoo/Customer/views/region/dialog.blade.php
@@ -0,0 +1,134 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/region/index.blade.php b/app/Gdoo/Customer/views/region/index.blade.php
new file mode 100644
index 00000000..e5c41407
--- /dev/null
+++ b/app/Gdoo/Customer/views/region/index.blade.php
@@ -0,0 +1,78 @@
+{{$header["js"]}}
+
+ @include('headers')
+
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/regionTask/create.blade.php b/app/Gdoo/Customer/views/regionTask/create.blade.php
new file mode 100644
index 00000000..fbe27337
--- /dev/null
+++ b/app/Gdoo/Customer/views/regionTask/create.blade.php
@@ -0,0 +1,30 @@
+
+
+
+
+ {{$form['btn']}}
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/regionTask/index.blade.php b/app/Gdoo/Customer/views/regionTask/index.blade.php
new file mode 100644
index 00000000..628b1154
--- /dev/null
+++ b/app/Gdoo/Customer/views/regionTask/index.blade.php
@@ -0,0 +1,60 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/regionTask/progress.blade.php b/app/Gdoo/Customer/views/regionTask/progress.blade.php
new file mode 100644
index 00000000..55e35ee7
--- /dev/null
+++ b/app/Gdoo/Customer/views/regionTask/progress.blade.php
@@ -0,0 +1,117 @@
+
+ @include('headers')
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/task/create.blade.php b/app/Gdoo/Customer/views/task/create.blade.php
new file mode 100644
index 00000000..e158c9e3
--- /dev/null
+++ b/app/Gdoo/Customer/views/task/create.blade.php
@@ -0,0 +1,77 @@
+
+
+
+
+ {{$form['btn']}}
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/task/index.blade.php b/app/Gdoo/Customer/views/task/index.blade.php
new file mode 100644
index 00000000..628b1154
--- /dev/null
+++ b/app/Gdoo/Customer/views/task/index.blade.php
@@ -0,0 +1,60 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/task/progress.blade.php b/app/Gdoo/Customer/views/task/progress.blade.php
new file mode 100644
index 00000000..95dfacd4
--- /dev/null
+++ b/app/Gdoo/Customer/views/task/progress.blade.php
@@ -0,0 +1,132 @@
+
+ @include('headers')
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/tax/create.blade.php b/app/Gdoo/Customer/views/tax/create.blade.php
new file mode 100644
index 00000000..a47f0da4
--- /dev/null
+++ b/app/Gdoo/Customer/views/tax/create.blade.php
@@ -0,0 +1,32 @@
+
+
+
+
+ {{$form['btn']}}
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/tax/dialog.blade.php b/app/Gdoo/Customer/views/tax/dialog.blade.php
new file mode 100644
index 00000000..cb43d971
--- /dev/null
+++ b/app/Gdoo/Customer/views/tax/dialog.blade.php
@@ -0,0 +1,135 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/tax/index.blade.php b/app/Gdoo/Customer/views/tax/index.blade.php
new file mode 100644
index 00000000..060b7eae
--- /dev/null
+++ b/app/Gdoo/Customer/views/tax/index.blade.php
@@ -0,0 +1,60 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/type/create.blade.php b/app/Gdoo/Customer/views/type/create.blade.php
new file mode 100644
index 00000000..99c09353
--- /dev/null
+++ b/app/Gdoo/Customer/views/type/create.blade.php
@@ -0,0 +1,3 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/type/index.blade.php b/app/Gdoo/Customer/views/type/index.blade.php
new file mode 100644
index 00000000..d6cbfa5f
--- /dev/null
+++ b/app/Gdoo/Customer/views/type/index.blade.php
@@ -0,0 +1,57 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Customer/views/widget/birthday.blade.php b/app/Gdoo/Customer/views/widget/birthday.blade.php
new file mode 100644
index 00000000..998be17e
--- /dev/null
+++ b/app/Gdoo/Customer/views/widget/birthday.blade.php
@@ -0,0 +1,31 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/CustomerCost/views/adjust/index.blade.php b/app/Gdoo/CustomerCost/views/adjust/index.blade.php
new file mode 100644
index 00000000..da91f784
--- /dev/null
+++ b/app/Gdoo/CustomerCost/views/adjust/index.blade.php
@@ -0,0 +1,57 @@
+{{$header["js"]}}
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/CustomerCost/views/category/create.blade.php b/app/Gdoo/CustomerCost/views/category/create.blade.php
new file mode 100644
index 00000000..db7f6d80
--- /dev/null
+++ b/app/Gdoo/CustomerCost/views/category/create.blade.php
@@ -0,0 +1,3 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/CustomerCost/views/category/dialog.blade.php b/app/Gdoo/CustomerCost/views/category/dialog.blade.php
new file mode 100644
index 00000000..a6181bd3
--- /dev/null
+++ b/app/Gdoo/CustomerCost/views/category/dialog.blade.php
@@ -0,0 +1,113 @@
+
+
+
+
+
+
名称
+
ID
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/CustomerCost/views/category/index.blade.php b/app/Gdoo/CustomerCost/views/category/index.blade.php
new file mode 100644
index 00000000..d6cbfa5f
--- /dev/null
+++ b/app/Gdoo/CustomerCost/views/category/index.blade.php
@@ -0,0 +1,57 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/CustomerCost/views/compen/create.blade.php b/app/Gdoo/CustomerCost/views/compen/create.blade.php
new file mode 100644
index 00000000..dbdb83ef
--- /dev/null
+++ b/app/Gdoo/CustomerCost/views/compen/create.blade.php
@@ -0,0 +1,89 @@
+
+
+
+
+ {{$form['btn']}}
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/CustomerCost/views/compen/index.blade.php b/app/Gdoo/CustomerCost/views/compen/index.blade.php
new file mode 100644
index 00000000..da91f784
--- /dev/null
+++ b/app/Gdoo/CustomerCost/views/compen/index.blade.php
@@ -0,0 +1,57 @@
+{{$header["js"]}}
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/CustomerCost/views/cost/close.blade.php b/app/Gdoo/CustomerCost/views/cost/close.blade.php
new file mode 100644
index 00000000..e429d86b
--- /dev/null
+++ b/app/Gdoo/CustomerCost/views/cost/close.blade.php
@@ -0,0 +1,6 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/CustomerCost/views/cost/create.blade.php b/app/Gdoo/CustomerCost/views/cost/create.blade.php
new file mode 100644
index 00000000..ad4f27ee
--- /dev/null
+++ b/app/Gdoo/CustomerCost/views/cost/create.blade.php
@@ -0,0 +1,36 @@
+
+
+
+
+ {{$form['btn']}}
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/CustomerCost/views/cost/dialog.blade.php b/app/Gdoo/CustomerCost/views/cost/dialog.blade.php
new file mode 100644
index 00000000..2fc06862
--- /dev/null
+++ b/app/Gdoo/CustomerCost/views/cost/dialog.blade.php
@@ -0,0 +1,122 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/CustomerCost/views/cost/index.blade.php b/app/Gdoo/CustomerCost/views/cost/index.blade.php
new file mode 100644
index 00000000..7ba5395c
--- /dev/null
+++ b/app/Gdoo/CustomerCost/views/cost/index.blade.php
@@ -0,0 +1,107 @@
+{{$header["js"]}}
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/CustomerCost/views/cost/serviceSaleOrder.blade.php b/app/Gdoo/CustomerCost/views/cost/serviceSaleOrder.blade.php
new file mode 100644
index 00000000..4e0d662c
--- /dev/null
+++ b/app/Gdoo/CustomerCost/views/cost/serviceSaleOrder.blade.php
@@ -0,0 +1,57 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/CustomerCost/views/rebate/create.blade.php b/app/Gdoo/CustomerCost/views/rebate/create.blade.php
new file mode 100644
index 00000000..9c297afd
--- /dev/null
+++ b/app/Gdoo/CustomerCost/views/rebate/create.blade.php
@@ -0,0 +1,90 @@
+
+
+
+
+ {{$form['btn']}}
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/CustomerCost/views/rebate/index.blade.php b/app/Gdoo/CustomerCost/views/rebate/index.blade.php
new file mode 100644
index 00000000..da91f784
--- /dev/null
+++ b/app/Gdoo/CustomerCost/views/rebate/index.blade.php
@@ -0,0 +1,57 @@
+{{$header["js"]}}
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/CustomerCost/views/report/saleOrderDetail.blade.php b/app/Gdoo/CustomerCost/views/report/saleOrderDetail.blade.php
new file mode 100644
index 00000000..cd92db4b
--- /dev/null
+++ b/app/Gdoo/CustomerCost/views/report/saleOrderDetail.blade.php
@@ -0,0 +1,105 @@
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/File/views/file/edit.blade.php b/app/Gdoo/File/views/file/edit.blade.php
new file mode 100644
index 00000000..59672b50
--- /dev/null
+++ b/app/Gdoo/File/views/file/edit.blade.php
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
资料上传
+
+
+
+
+
+ @include('attachment/add')
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/File/views/file/folder.blade.php b/app/Gdoo/File/views/file/folder.blade.php
new file mode 100644
index 00000000..82105e91
--- /dev/null
+++ b/app/Gdoo/File/views/file/folder.blade.php
@@ -0,0 +1,14 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/File/views/file/index.blade.1.php b/app/Gdoo/File/views/file/index.blade.1.php
new file mode 100644
index 00000000..defeab8e
--- /dev/null
+++ b/app/Gdoo/File/views/file/index.blade.1.php
@@ -0,0 +1,31 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/File/views/file/index.blade.php b/app/Gdoo/File/views/file/index.blade.php
new file mode 100644
index 00000000..3f87013c
--- /dev/null
+++ b/app/Gdoo/File/views/file/index.blade.php
@@ -0,0 +1,150 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/File/views/file/receivedata.blade.php b/app/Gdoo/File/views/file/receivedata.blade.php
new file mode 100644
index 00000000..62521bb2
--- /dev/null
+++ b/app/Gdoo/File/views/file/receivedata.blade.php
@@ -0,0 +1,78 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/File/views/file/share.blade.php b/app/Gdoo/File/views/file/share.blade.php
new file mode 100644
index 00000000..c47e14a0
--- /dev/null
+++ b/app/Gdoo/File/views/file/share.blade.php
@@ -0,0 +1,93 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/File/views/file/sharing.blade.php b/app/Gdoo/File/views/file/sharing.blade.php
new file mode 100644
index 00000000..a4b61e3e
--- /dev/null
+++ b/app/Gdoo/File/views/file/sharing.blade.php
@@ -0,0 +1,12 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/File/views/file/upload.blade.php b/app/Gdoo/File/views/file/upload.blade.php
new file mode 100644
index 00000000..967dc634
--- /dev/null
+++ b/app/Gdoo/File/views/file/upload.blade.php
@@ -0,0 +1,52 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/File/views/inspectReport/create.blade.php b/app/Gdoo/File/views/inspectReport/create.blade.php
new file mode 100644
index 00000000..b614d90d
--- /dev/null
+++ b/app/Gdoo/File/views/inspectReport/create.blade.php
@@ -0,0 +1,47 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/File/views/inspectReport/index.blade.php b/app/Gdoo/File/views/inspectReport/index.blade.php
new file mode 100644
index 00000000..e6d70536
--- /dev/null
+++ b/app/Gdoo/File/views/inspectReport/index.blade.php
@@ -0,0 +1,95 @@
+{{$header["js"]}}
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/File/views/widget/index.blade.php b/app/Gdoo/File/views/widget/index.blade.php
new file mode 100644
index 00000000..e6954f9d
--- /dev/null
+++ b/app/Gdoo/File/views/widget/index.blade.php
@@ -0,0 +1,24 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Forum/views/post/add.blade.php b/app/Gdoo/Forum/views/post/add.blade.php
new file mode 100644
index 00000000..c976f841
--- /dev/null
+++ b/app/Gdoo/Forum/views/post/add.blade.php
@@ -0,0 +1,28 @@
+
+
+
+
+
+
+
+
diff --git a/app/Gdoo/Forum/views/post/comment.blade.php b/app/Gdoo/Forum/views/post/comment.blade.php
new file mode 100644
index 00000000..aa2df5b7
--- /dev/null
+++ b/app/Gdoo/Forum/views/post/comment.blade.php
@@ -0,0 +1,22 @@
+
+
+
+
+
+
diff --git a/app/Gdoo/Forum/views/post/forum.blade.php b/app/Gdoo/Forum/views/post/forum.blade.php
new file mode 100644
index 00000000..fbc7e22f
--- /dev/null
+++ b/app/Gdoo/Forum/views/post/forum.blade.php
@@ -0,0 +1,68 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Index/views/dashboard/quickMenu.blade.php b/app/Gdoo/Index/views/dashboard/quickMenu.blade.php
new file mode 100644
index 00000000..164a2896
--- /dev/null
+++ b/app/Gdoo/Index/views/dashboard/quickMenu.blade.php
@@ -0,0 +1,72 @@
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Index/views/dashboard/settingInfo.blade.php b/app/Gdoo/Index/views/dashboard/settingInfo.blade.php
new file mode 100644
index 00000000..68627be0
--- /dev/null
+++ b/app/Gdoo/Index/views/dashboard/settingInfo.blade.php
@@ -0,0 +1,71 @@
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Index/views/dashboard/settingWidget.blade.php b/app/Gdoo/Index/views/dashboard/settingWidget.blade.php
new file mode 100644
index 00000000..ca7c58fd
--- /dev/null
+++ b/app/Gdoo/Index/views/dashboard/settingWidget.blade.php
@@ -0,0 +1,53 @@
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Index/views/demo/vouch.blade.php b/app/Gdoo/Index/views/demo/vouch.blade.php
new file mode 100644
index 00000000..6afd18f4
--- /dev/null
+++ b/app/Gdoo/Index/views/demo/vouch.blade.php
@@ -0,0 +1,228 @@
+
+
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Model/views/field/create.blade.php b/app/Gdoo/Model/views/field/create.blade.php
new file mode 100644
index 00000000..f32beb5b
--- /dev/null
+++ b/app/Gdoo/Model/views/field/create.blade.php
@@ -0,0 +1,311 @@
+
+
+
+
模型名称
+
+
+
+
+
父节模型
+
+
+
+
+
+
+
模型类型
+
+
+
+
+
+
+
关联外键
+
+
+
+
+
数据表名
+
id > 0) readonly @endif>
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Model/views/model/index.blade.php b/app/Gdoo/Model/views/model/index.blade.php
new file mode 100644
index 00000000..184b97b7
--- /dev/null
+++ b/app/Gdoo/Model/views/model/index.blade.php
@@ -0,0 +1,92 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Model/views/module/edit.blade.php b/app/Gdoo/Model/views/module/edit.blade.php
new file mode 100644
index 00000000..71eca222
--- /dev/null
+++ b/app/Gdoo/Model/views/module/edit.blade.php
@@ -0,0 +1,60 @@
+
+
+
+
+
名称
+
+
+
+
+
+
邮箱帐号
+
+
+
+
+
+
邮箱密码
+
+
+
+
+
+
SMTP服务器
+
+
+
+
+
+
服务器端口
+
+
+
+
+
+
连接方式
+
+
+
+
+
+
状态
+
+
+
+
+
+
排序
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Model/views/module/index.blade.php b/app/Gdoo/Model/views/module/index.blade.php
new file mode 100644
index 00000000..8f909f64
--- /dev/null
+++ b/app/Gdoo/Model/views/module/index.blade.php
@@ -0,0 +1,100 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Model/views/permission/create.blade.php b/app/Gdoo/Model/views/permission/create.blade.php
new file mode 100644
index 00000000..9c9f5d9d
--- /dev/null
+++ b/app/Gdoo/Model/views/permission/create.blade.php
@@ -0,0 +1,147 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Model/views/permission/index.blade.php b/app/Gdoo/Model/views/permission/index.blade.php
new file mode 100644
index 00000000..a14eeafb
--- /dev/null
+++ b/app/Gdoo/Model/views/permission/index.blade.php
@@ -0,0 +1,41 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Model/views/process/log.blade.php b/app/Gdoo/Model/views/process/log.blade.php
new file mode 100644
index 00000000..6a57ff65
--- /dev/null
+++ b/app/Gdoo/Model/views/process/log.blade.php
@@ -0,0 +1,52 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Model/views/process/turn.blade.php b/app/Gdoo/Model/views/process/turn.blade.php
new file mode 100644
index 00000000..8997b6f6
--- /dev/null
+++ b/app/Gdoo/Model/views/process/turn.blade.php
@@ -0,0 +1,72 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Model/views/step/condition.php b/app/Gdoo/Model/views/step/condition.php
new file mode 100644
index 00000000..6b98772c
--- /dev/null
+++ b/app/Gdoo/Model/views/step/condition.php
@@ -0,0 +1,145 @@
+
+
+
+
+
+
+
+
转入: name; ?>
+
+
+
左括号
+
字段
+
条件
+
值
+
函数
+
右括号
+
逻辑
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Model/views/step/create.blade.php b/app/Gdoo/Model/views/step/create.blade.php
new file mode 100644
index 00000000..863b46f9
--- /dev/null
+++ b/app/Gdoo/Model/views/step/create.blade.php
@@ -0,0 +1,173 @@
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Model/views/step/index.blade.php b/app/Gdoo/Model/views/step/index.blade.php
new file mode 100644
index 00000000..bae62bb5
--- /dev/null
+++ b/app/Gdoo/Model/views/step/index.blade.php
@@ -0,0 +1,49 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Model/views/todo/widget.blade.php b/app/Gdoo/Model/views/todo/widget.blade.php
new file mode 100644
index 00000000..ef7280b5
--- /dev/null
+++ b/app/Gdoo/Model/views/todo/widget.blade.php
@@ -0,0 +1,102 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/logistics/dialog.blade.php b/app/Gdoo/Order/views/logistics/dialog.blade.php
new file mode 100644
index 00000000..d9e86f84
--- /dev/null
+++ b/app/Gdoo/Order/views/logistics/dialog.blade.php
@@ -0,0 +1,153 @@
+
+
+
+
+ @include('searchForm')
+
+
+
+
+
+
+
+
公司名称
+
法人代表
+
ID
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/logistics/index.blade.php b/app/Gdoo/Order/views/logistics/index.blade.php
new file mode 100644
index 00000000..125f3866
--- /dev/null
+++ b/app/Gdoo/Order/views/logistics/index.blade.php
@@ -0,0 +1,59 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/order/bank.blade.php b/app/Gdoo/Order/views/order/bank.blade.php
new file mode 100644
index 00000000..23e04b60
--- /dev/null
+++ b/app/Gdoo/Order/views/order/bank.blade.php
@@ -0,0 +1,33 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/order/create.blade.php b/app/Gdoo/Order/views/order/create.blade.php
new file mode 100644
index 00000000..7eb9f421
--- /dev/null
+++ b/app/Gdoo/Order/views/order/create.blade.php
@@ -0,0 +1,435 @@
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/order/delivery.blade.php b/app/Gdoo/Order/views/order/delivery.blade.php
new file mode 100644
index 00000000..e4b39f29
--- /dev/null
+++ b/app/Gdoo/Order/views/order/delivery.blade.php
@@ -0,0 +1,153 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/order/deliveryEdit.blade.php b/app/Gdoo/Order/views/order/deliveryEdit.blade.php
new file mode 100644
index 00000000..4e4c785e
--- /dev/null
+++ b/app/Gdoo/Order/views/order/deliveryEdit.blade.php
@@ -0,0 +1,9 @@
+
+
+
+
运费付款方式
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/order/deliveryPlan.blade.php b/app/Gdoo/Order/views/order/deliveryPlan.blade.php
new file mode 100644
index 00000000..61369ce3
--- /dev/null
+++ b/app/Gdoo/Order/views/order/deliveryPlan.blade.php
@@ -0,0 +1,62 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/order/detail.blade.php b/app/Gdoo/Order/views/order/detail.blade.php
new file mode 100644
index 00000000..3dfe25fc
--- /dev/null
+++ b/app/Gdoo/Order/views/order/detail.blade.php
@@ -0,0 +1,62 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/order/dialog.blade.php b/app/Gdoo/Order/views/order/dialog.blade.php
new file mode 100644
index 00000000..21c4855f
--- /dev/null
+++ b/app/Gdoo/Order/views/order/dialog.blade.php
@@ -0,0 +1,125 @@
+
+
+ @include('searchForm3')
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/order/index.blade.php b/app/Gdoo/Order/views/order/index.blade.php
new file mode 100644
index 00000000..cd9a19bc
--- /dev/null
+++ b/app/Gdoo/Order/views/order/index.blade.php
@@ -0,0 +1,61 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/order/logisticsPlan.blade.php b/app/Gdoo/Order/views/order/logisticsPlan.blade.php
new file mode 100644
index 00000000..d8db7eda
--- /dev/null
+++ b/app/Gdoo/Order/views/order/logisticsPlan.blade.php
@@ -0,0 +1,59 @@
+
+
+
+
+
预计发货日期
+
+
+
+
+
短途承运人
+
+
+
+
+
+
+
短途车牌号
+
+
+
+
+
物流公司
+
+
+
+
+
+
+
物流联系电话
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/order/print.blade.php b/app/Gdoo/Order/views/order/print.blade.php
new file mode 100644
index 00000000..ca2d71da
--- /dev/null
+++ b/app/Gdoo/Order/views/order/print.blade.php
@@ -0,0 +1 @@
+{{$form['tpl']}}
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/order/print/121.blade.php b/app/Gdoo/Order/views/order/print/121.blade.php
new file mode 100644
index 00000000..ebe9edb3
--- /dev/null
+++ b/app/Gdoo/Order/views/order/print/121.blade.php
@@ -0,0 +1,188 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/order/reference.blade.php b/app/Gdoo/Order/views/order/reference.blade.php
new file mode 100644
index 00000000..2d81a877
--- /dev/null
+++ b/app/Gdoo/Order/views/order/reference.blade.php
@@ -0,0 +1,122 @@
+
+
+
+
+
+ @include('searchForm3')
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/order/serviceCancelOrder.blade.php b/app/Gdoo/Order/views/order/serviceCancelOrder.blade.php
new file mode 100644
index 00000000..87dde57e
--- /dev/null
+++ b/app/Gdoo/Order/views/order/serviceCancelOrder.blade.php
@@ -0,0 +1,119 @@
+
+
+
+
+
+ @include('searchForm3')
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/order/serviceDelivery.blade.php b/app/Gdoo/Order/views/order/serviceDelivery.blade.php
new file mode 100644
index 00000000..fdbf8903
--- /dev/null
+++ b/app/Gdoo/Order/views/order/serviceDelivery.blade.php
@@ -0,0 +1,128 @@
+
+
+
+
+
+ @include('searchForm3')
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/order/serviceNotDelivery.blade.php b/app/Gdoo/Order/views/order/serviceNotDelivery.blade.php
new file mode 100644
index 00000000..e8641d4d
--- /dev/null
+++ b/app/Gdoo/Order/views/order/serviceNotDelivery.blade.php
@@ -0,0 +1,117 @@
+
+
+
+
+
+ @include('searchForm3')
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/order/servicePromotion.blade.php b/app/Gdoo/Order/views/order/servicePromotion.blade.php
new file mode 100644
index 00000000..672700a5
--- /dev/null
+++ b/app/Gdoo/Order/views/order/servicePromotion.blade.php
@@ -0,0 +1,174 @@
+
+
+ @include('searchForm6')
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/plan/export_sale.blade.php b/app/Gdoo/Order/views/plan/export_sale.blade.php
new file mode 100644
index 00000000..cfd56f2f
--- /dev/null
+++ b/app/Gdoo/Order/views/plan/export_sale.blade.php
@@ -0,0 +1,110 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/plan/index.blade.php b/app/Gdoo/Order/views/plan/index.blade.php
new file mode 100644
index 00000000..3692ec55
--- /dev/null
+++ b/app/Gdoo/Order/views/plan/index.blade.php
@@ -0,0 +1,108 @@
+
+ @include('headers')
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/plan/produce.blade.php b/app/Gdoo/Order/views/plan/produce.blade.php
new file mode 100644
index 00000000..4ba681b8
--- /dev/null
+++ b/app/Gdoo/Order/views/plan/produce.blade.php
@@ -0,0 +1,219 @@
+
+ @include('headers')
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/plan/producePlan.blade.php b/app/Gdoo/Order/views/plan/producePlan.blade.php
new file mode 100644
index 00000000..e6b8e59d
--- /dev/null
+++ b/app/Gdoo/Order/views/plan/producePlan.blade.php
@@ -0,0 +1,44 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/report/category.blade.php b/app/Gdoo/Order/views/report/category.blade.php
new file mode 100644
index 00000000..b1c73ad8
--- /dev/null
+++ b/app/Gdoo/Order/views/report/category.blade.php
@@ -0,0 +1,187 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/report/city.blade.php b/app/Gdoo/Order/views/report/city.blade.php
new file mode 100644
index 00000000..15be7e1d
--- /dev/null
+++ b/app/Gdoo/Order/views/report/city.blade.php
@@ -0,0 +1,133 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/report/citydata.blade.php b/app/Gdoo/Order/views/report/citydata.blade.php
new file mode 100644
index 00000000..40b6599d
--- /dev/null
+++ b/app/Gdoo/Order/views/report/citydata.blade.php
@@ -0,0 +1,77 @@
+
+
+
+
+
{{$year}}年 ({{$circle['name']}}) 客户圈月份销售分析
+
+
+
+
+
+
+
+
+
+
月份
+
+ @if(count($categorys)) @foreach($categorys as $k => $v)
+
{{$k}}
+ @endforeach @endif
+
+
总销售额
+
促销费比
+
+
消费促销
+
渠道促销
+
经销促销
+
+ @if(count($single['money'])) @foreach($single['money'] as $key => $value)
+
+
{{$key}}月
+
+ @if(count($categorys)) @foreach($categorys as $k => $v)
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/report/clientdata.blade.php b/app/Gdoo/Order/views/report/clientdata.blade.php
new file mode 100644
index 00000000..311f4f98
--- /dev/null
+++ b/app/Gdoo/Order/views/report/clientdata.blade.php
@@ -0,0 +1,65 @@
+
+
+ @if(count($single['all']))
+ @foreach($single['all'] as $key => $value)
+
+
{{$i + 1}}
+
{{$clients[$key]['area']}}
+
{{$clients[$key]['circle_name']}}
+
{{$clients[$key]['client_id']}}
+
{{$single['name']}} - {{$single['spec']}}
+
{{$value}}
+
+
+ @endforeach
+ @endif
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/report/index.blade.php b/app/Gdoo/Order/views/report/index.blade.php
new file mode 100644
index 00000000..d2e2c716
--- /dev/null
+++ b/app/Gdoo/Order/views/report/index.blade.php
@@ -0,0 +1,128 @@
+
+
+
+
+@endif
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/report/newclient.blade.php b/app/Gdoo/Order/views/report/newclient.blade.php
new file mode 100644
index 00000000..7e8fd35d
--- /dev/null
+++ b/app/Gdoo/Order/views/report/newclient.blade.php
@@ -0,0 +1,52 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/report/promotion.blade.php b/app/Gdoo/Order/views/report/promotion.blade.php
new file mode 100644
index 00000000..eba373df
--- /dev/null
+++ b/app/Gdoo/Order/views/report/promotion.blade.php
@@ -0,0 +1,28 @@
+
+
+
+
+
步骤
+
开始时间
+
结束时间
+
促销对象
+
促销目标
+
促销类别
+
促销单品
+
促销方法
+
我司支持方式
+
兑现凭据
+
预估费用
+
实际兑现费用
+
+ @foreach($promotions as $key => $value)
+
+ @foreach($value as $k => $v)
+
{{$v}}
+ @endforeach
+
+ @endforeach
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/report/ranking.blade.php b/app/Gdoo/Order/views/report/ranking.blade.php
new file mode 100644
index 00000000..9a531031
--- /dev/null
+++ b/app/Gdoo/Order/views/report/ranking.blade.php
@@ -0,0 +1,315 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/report/receivable.blade.php b/app/Gdoo/Order/views/report/receivable.blade.php
new file mode 100644
index 00000000..1e82c84d
--- /dev/null
+++ b/app/Gdoo/Order/views/report/receivable.blade.php
@@ -0,0 +1,45 @@
+
+
+
+ @if(Auth::user()->role->name != 'client')
+
+
+ @include('data/select')
+
+
+ 年份
+
+
+
+
+ @endif
+
+
+
+
+
客户名称
+
客户代码
+ @for($i=1; $i <= 12; $i++)
+
{{$i}}月(¥)
+ @endfor
+
合计(¥)
+
+
+ @foreach($rows as $row)
+
+
{{$row['customer_name']}}
+
{{$row['customer_code']}}
+ @for($i=1; $i <= 12; $i++)
+
+
@number($row['task'][$i] * 10000)
+
@number($row['money'][$i])
+
+ @endfor
+
+
@number(array_sum($row['task']) * 10000)
+
@number(array_sum($row['money']))
+
+
+ @endforeach
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/report/select.blade.php b/app/Gdoo/Order/views/report/select.blade.php
new file mode 100644
index 00000000..d842aaf8
--- /dev/null
+++ b/app/Gdoo/Order/views/report/select.blade.php
@@ -0,0 +1,56 @@
+
+
+
+
+
+
+
+
+@if(isset($select['query']['customer_type']))
+
+@endif
+
+@if($select['query']['date1'])
+
+日期
+
+@endif
+@if($select['query']['date2'])
+-
+
+@endif
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/report/single.blade.php b/app/Gdoo/Order/views/report/single.blade.php
new file mode 100644
index 00000000..245fb368
--- /dev/null
+++ b/app/Gdoo/Order/views/report/single.blade.php
@@ -0,0 +1,90 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/report/stockmonth.blade.php b/app/Gdoo/Order/views/report/stockmonth.blade.php
new file mode 100644
index 00000000..762825b2
--- /dev/null
+++ b/app/Gdoo/Order/views/report/stockmonth.blade.php
@@ -0,0 +1,42 @@
+
+
+
+ @if(count($rows))
+ @foreach($rows as $row)
+
+
{{$n}}
+
{{$row['code']}}
+
{{$row['name']}}
+
{{$customer_type[$row['type_id']]['name']}}
+
@number($data[$year1][$row['id']], 2)
+
@number($data[$year2][$row['id']], 2)
+
+
+ @endforeach
+ @endif
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/sampleApply/create.blade.php b/app/Gdoo/Order/views/sampleApply/create.blade.php
new file mode 100644
index 00000000..b950f4d3
--- /dev/null
+++ b/app/Gdoo/Order/views/sampleApply/create.blade.php
@@ -0,0 +1,37 @@
+
+
+
+
+ {{$form['btn']}}
+
+
+
+ {{$form['tpl']}}
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/sampleApply/detail.blade.php b/app/Gdoo/Order/views/sampleApply/detail.blade.php
new file mode 100644
index 00000000..875193a0
--- /dev/null
+++ b/app/Gdoo/Order/views/sampleApply/detail.blade.php
@@ -0,0 +1,80 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/sampleApply/index.blade.php b/app/Gdoo/Order/views/sampleApply/index.blade.php
new file mode 100644
index 00000000..875193a0
--- /dev/null
+++ b/app/Gdoo/Order/views/sampleApply/index.blade.php
@@ -0,0 +1,80 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/sampleApply/print.blade.php b/app/Gdoo/Order/views/sampleApply/print.blade.php
new file mode 100644
index 00000000..ca2d71da
--- /dev/null
+++ b/app/Gdoo/Order/views/sampleApply/print.blade.php
@@ -0,0 +1 @@
+{{$form['tpl']}}
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/sampleApply/serviceDelivery.blade.php b/app/Gdoo/Order/views/sampleApply/serviceDelivery.blade.php
new file mode 100644
index 00000000..88a8041a
--- /dev/null
+++ b/app/Gdoo/Order/views/sampleApply/serviceDelivery.blade.php
@@ -0,0 +1,116 @@
+
+
+
+
+
+ @include('searchForm3')
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/type/create.blade.php b/app/Gdoo/Order/views/type/create.blade.php
new file mode 100644
index 00000000..a41d6aeb
--- /dev/null
+++ b/app/Gdoo/Order/views/type/create.blade.php
@@ -0,0 +1,3 @@
+
+ {{$form['tpl']}}
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/type/dialog.blade.php b/app/Gdoo/Order/views/type/dialog.blade.php
new file mode 100644
index 00000000..ff8ed941
--- /dev/null
+++ b/app/Gdoo/Order/views/type/dialog.blade.php
@@ -0,0 +1,130 @@
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/widget/goods.blade.php b/app/Gdoo/Order/views/widget/goods.blade.php
new file mode 100644
index 00000000..a9ca2a29
--- /dev/null
+++ b/app/Gdoo/Order/views/widget/goods.blade.php
@@ -0,0 +1,38 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Order/views/widget/index.blade.php b/app/Gdoo/Order/views/widget/index.blade.php
new file mode 100644
index 00000000..199f0496
--- /dev/null
+++ b/app/Gdoo/Order/views/widget/index.blade.php
@@ -0,0 +1,31 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Produce/Controllers/MaterialController.php b/app/Gdoo/Produce/Controllers/MaterialController.php
new file mode 100644
index 00000000..a84544ef
--- /dev/null
+++ b/app/Gdoo/Produce/Controllers/MaterialController.php
@@ -0,0 +1,333 @@
+ 'product_material',
+ 'referer' => 1,
+ 'search' => ['by' => ''],
+ ]);
+
+ $cols = $header['cols'];
+
+ $cols['actions']['options'] = [[
+ 'name' => '编辑',
+ 'action' => 'edit',
+ 'display' => $this->access['edit'],
+ ]];
+
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table'])->setBy($header);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+ $model->orderBy($header['sort'], $header['order']);
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ $model->select($header['select']);
+ $rows = $model->paginate($query['limit'])->appends($query);
+ $items = Grid::dataFilters($rows, $header);
+ return $items->toJson();
+ }
+
+ $header['buttons'] = [
+ ['name' => '删除', 'icon' => 'fa-remove', 'action' => 'delete', 'display' => $this->access['delete']],
+ ['name' => '导出', 'icon' => 'fa-share', 'action' => 'export', 'display' => 1],
+ ];
+
+ $header['left_buttons'] = [
+ ['name' => '配料', 'color' => 'default', 'icon' => 'fa-file-text-o', 'action' => 'config', 'display' => 1],
+ ];
+
+ $header['cols'] = $cols;
+ $header['tabs'] = Material::$tabs;
+ $header['bys'] = Material::$bys;
+ $header['js'] = Grid::js($header);
+
+ return $this->display([
+ 'header' => $header,
+ ]);
+ }
+
+ // 新建
+ public function createAction($action = 'edit')
+ {
+ $id = (int)Request::get('id');
+ $form = Form::make(['code' => 'product_material', 'id' => $id, 'action' => $action]);
+ return $this->render([
+ 'form' => $form,
+ ], 'create');
+ }
+
+ // 创建
+ public function editAction()
+ {
+ return $this->createAction('edit');
+ }
+
+ // 配方
+ public function configAction()
+ {
+ $id = (int)Request::get('id');
+ if (Request::method() == 'POST') {
+ $rows = DB::table('product_formula')->where('material_id', $id)
+ ->leftJoin('product', 'product.id', '=', 'product_formula.product_id')
+ ->leftJoin('product_unit', 'product_unit.id', '=', 'product.unit_id')
+ ->get([
+ 'product.name as product_name',
+ 'product.code as product_code',
+ 'product.spec as product_spec',
+ 'product_unit.name as product_unit',
+ 'product_formula.*'
+ ]);
+ return $this->json($rows, true);
+ }
+ return $this->display(['id' => $id]);
+ }
+
+ // 用料计划
+ public function planAction()
+ {
+ $search = search_form([
+ 'advanced' => 0,
+ ], [
+ ['form_type' => 'date', 'name' => '计划时间', 'field' => 'date', 'options' => []],
+ ['form_type' => 'dialog', 'name' => '生产车间', 'field' => 'department_id', 'options' => [
+ 'url' => 'user/department/dialog',
+ ]],
+ ['form_type' => 'select', 'name' => '重算计算', 'field' => 'is_recalc', 'options' => [
+ ['id' => 1, 'name' => '是'],
+ ['id' => 0, 'name' => '否']
+ ]],
+ ], 'model');
+
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $date = $query['search_0'];
+ $department_id = $query['search_1'];
+ $is_recalc = $query['search_2'];
+ $rows = [];
+ if ($date && $department_id) {
+ $rows = ProduceService::getMaterialPlanDay($date, $department_id, $is_recalc);
+ $rowspan = $rowspan2 = [];
+ foreach($rows as $row) {
+ $rowspan[$row['product_id']] ++;
+ $rowspan2[$row['product_id'].'_'.$row['category_name']] ++;
+ }
+ foreach($rows as &$row) {
+ $a = $rowspan[$row['product_id']];
+ $b = $rowspan2[$row['product_id'].'_'.$row['category_name']];
+ if ($a) {
+ $row['rowspan'] = $rowspan[$row['product_id']];
+ $row['rowspan_end'] = count($rowspan) == 1 ? 1 : 0;
+ unset($rowspan[$row['product_id']]);
+ }
+ if ($b) {
+ $row['rowspan2_end'] = count($rowspan2) == 1 ? 0 : 0;
+ $row['rowspan2'] = $rowspan2[$row['product_id'].'_'.$row['category_name']];
+ unset($rowspan2[$row['product_id'].'_'.$row['category_name']]);
+ }
+
+ }
+ }
+ return $this->json($rows, true);
+ }
+
+ $search['table'] = 'material_plan';
+ return $this->display([
+ 'search' => $search,
+ 'query' => $query,
+ ]);
+ }
+
+ // 用料计划产品
+ public function planProductAction()
+ {
+ $search = search_form([], [], 'model');
+ $query = $search['query'];
+ if (Request::method() == 'POST') {
+ $date = $query['date'];
+ $department_id = (int)$query['department_id'];
+ $product_id = (int)$query['product_id'];
+
+ $rows = [];
+ if ($date) {
+ $sql = "select a.date,
+ a.code,
+ a.dept_id,
+ a.product_id,
+ b.name as product_name,
+ b.spec as product_spec,
+ c.name as product_unit,
+ a.product_num,
+ a.material_id,
+ d.category as category_name,
+ d.name as material_name,
+ a.material_num,
+ a.total_num
+ ,a.creator_id,
+ a.creator_name,
+ a.create_date,
+ a.remark
+ from material_plan_day a
+ left join product b on a.product_id = b.id
+ left join product_unit AS c ON b.unit_id = c.id
+ left join product_material d on a.Material_Id = d.Id
+ where a.date = '".$date."' and a.Dept_Id = $department_id and a.Product_Id = $product_id";
+ $rows = DB::select($sql);
+ }
+ return $this->json($rows, true);
+ }
+ return $this->render([
+ 'search' => $search,
+ 'query' => $query,
+ ]);
+ }
+
+ // 用料计划总量
+ public function planTotalAction()
+ {
+ $search = search_form([], [], 'model');
+ $query = $search['query'];
+ if (Request::method() == 'POST') {
+
+ $date = $query['date'];
+ $department_id = (int)$query['department_id'];
+
+ $rows = [];
+ if ($date) {
+ $sql = "select a.date,
+ a.dept_id,
+ a.material_id,
+ d.category as category_name,
+ d.name as material_name,
+ sum(a.material_num) material_num,
+ sum(a.total_num)total_num
+ from material_plan_day a
+ left join product b on a.product_id = b.id
+ left join product_unit AS c ON b.unit_id = c.id
+ left join product_material d on a.Material_Id = d.Id
+ where a.date = '$date' and a.Dept_Id = ".$department_id."
+ group by a.Date, a.Dept_Id, a.Material_Id, d.category, d.Name";
+ $rows = DB::select($sql);
+ }
+ return $this->json($rows, true);
+ }
+ return $this->render([
+ 'search' => $search,
+ 'query' => $query,
+ ]);
+ }
+
+ // 配方保存
+ public function configSaveAction()
+ {
+ $gets = Request::all();
+ $id = $gets['id'];
+ if (empty($id)) {
+ return $this->json('原辅料编号不能为空');
+ }
+
+ $data = $gets['product_formula'];
+
+ // 新增或者修改
+ foreach((array)$data['rows'] as $_row) {
+ $_row['material_id'] = $id;
+ $row = Formula::findOrNew($_row['id']);
+ $row->fill($_row)->save();
+ }
+
+ // 删除记录
+ foreach((array)$data['deleteds'] as $row) {
+ if ($row['id'] > 0) {
+ Formula::where('id', $row['id'])->delete();
+ }
+ }
+ return $this->json('配方保存成功。', true);
+ }
+
+ // 删除
+ public function deleteAction()
+ {
+ if (Request::method() == 'POST') {
+ $ids = Request::get('id');
+ return Form::remove(['code' => 'product_material', 'ids' => $ids]);
+ }
+ }
+
+ /**
+ * 弹出层信息
+ */
+ public function dialogAction()
+ {
+ $search = search_form([
+ 'advanced' => '',
+ 'prefix' => '',
+ 'offset' => '',
+ 'sort' => '',
+ 'order' => '',
+ 'limit' => '',
+ ], [
+ ['text','logistics.name','名称'],
+ ]);
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table('logistics');
+ // 排序方式
+ if ($query['sort'] && $query['order']) {
+ $model->orderBy('logistics.'.$query['sort'], $query['order']);
+ }
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ $model->selectRaw("logistics.*");
+
+ if ($query['limit']) {
+ $rows = $model->paginate($query['limit']);
+ } else {
+ $rows['total'] = $model->count();
+ $rows['data'] = $model->get();
+ }
+ return response()->json($rows);
+ }
+
+ return $this->render([
+ 'search' => $search,
+ 'query' => $query,
+ ]);
+ }
+}
diff --git a/app/Gdoo/Produce/Controllers/PlanController.php b/app/Gdoo/Produce/Controllers/PlanController.php
new file mode 100644
index 00000000..001fc787
--- /dev/null
+++ b/app/Gdoo/Produce/Controllers/PlanController.php
@@ -0,0 +1,222 @@
+ 'produce_plan',
+ 'referer' => 1,
+ 'search' => ['by' => ''],
+ ]);
+
+ $cols = $header['cols'];
+
+ $cols['actions']['options'] = [[
+ 'name' => '编辑',
+ 'action' => 'edit',
+ 'display' => $this->access['edit'],
+ ]];
+
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table'])->setBy($header);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+ $model->orderBy($header['sort'], $header['order']);
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ $model->select($header['select']);
+ $rows = $model->paginate($query['limit'])->appends($query);
+ $items = Grid::dataFilters($rows, $header);
+ return $items->toJson();
+ }
+
+ $header['buttons'] = [
+ //['name' => '删除', 'icon' => 'fa-remove', 'action' => 'delete', 'display' => $this->access['delete']],
+ ['name' => '导出', 'icon' => 'fa-share', 'action' => 'export', 'display' => 1],
+ ];
+
+ $header['cols'] = $cols;
+ $header['tabs'] = Plan::$tabs;
+ $header['bys'] = Plan::$bys;
+ $header['js'] = Grid::js($header);
+
+ return $this->display([
+ 'header' => $header,
+ ]);
+ }
+
+ // 生产计划导出
+ public function planExportAction()
+ {
+ $search = search_form([
+ 'advanced' => 0,
+ ], [
+ ['form_type' => 'date', 'name' => '计划时间', 'field' => 'date', 'options' => []
+ ],
+ ], 'model');
+
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $date = $query['search_0'];
+ $rows = [];
+ if ($date) {
+
+ $departments = DB::table('produce_plan_data')
+ ->leftJoin('produce_plan', 'produce_plan.id', '=', 'produce_plan_data.plan_id')
+ ->leftJoin('department', 'department.id', '=', 'produce_plan_data.department_id')
+ ->where('produce_plan.date', $date)
+ ->where('department.id', '>', 0)
+ ->groupBy('department.id', 'department.name')
+ ->get(['department.id','department.name']);
+
+ foreach($departments as $index => $department) {
+ $sql = "
+ SELECT p.name as product_name, p.spec as product_spec, pu.name as product_unit, d.plan_num, d.remark, d.batch_sn, m.date
+ FROM produce_plan_data d
+ left join produce_plan m on m.id = d.plan_id
+ left join product p on d.product_id = p.id
+ left join product_unit pu on pu.id = p.unit_id
+ WHERE m.id = d.plan_id
+ AND isnull(d.department_id, 0) = ?
+ AND d.plan_num <> 0
+ AND d.plan_num IS NOT NULL
+ AND m.date = ? AND m.type = 1";
+ $items = DB::select($sql, [$department['id'], $date]);
+
+ if ($index > 0) {
+ $rows[] = [];
+ }
+
+ $rows[] = [
+ 'product_name' => '序号:'.($index + 1),
+ 'product_spec' => '',
+ 'product_unit' => '生产车间',
+ 'plan_num' => $department['name'],
+ 'quantity' => '',
+ 'remark' => '',
+ 'date' => ''
+ ];
+
+ $rows[] = [
+ 'product_name' => '成品品种',
+ 'product_spec' => '规格',
+ 'product_unit' => '单位',
+ 'plan_num' => '计划数量',
+ 'quantity' => '生产数量',
+ 'remark' => '备注',
+ 'date' => '计划日期'
+ ];
+
+ $plan_num = 0;
+ foreach($items as $item) {
+ $plan_num += $item['plan_num'];
+ $rows[] = [
+ 'product_name' => $item['product_name'],
+ 'product_spec' => $item['product_spec'],
+ 'product_unit' => $item['product_unit'],
+ 'plan_num' => $item['plan_num'],
+ 'remark' => $item['batch_sn'],
+ 'date' => $item['date'],
+ ];
+ }
+ $rows[] = [
+ 'product_name' => '总量',
+ 'product_spec' => '',
+ 'product_unit' => '',
+ 'plan_num' => $plan_num,
+ 'remark' => '',
+ 'date' => ''
+ ];
+ }
+ }
+ return $this->json($rows, true);
+ }
+ $search['table'] = 'material_plan';
+ return $this->display([
+ 'search' => $search,
+ 'query' => $query,
+ ]);
+ }
+
+ // 新建
+ public function createAction($action = 'edit')
+ {
+ $id = (int)Request::get('id');
+ $form = Form::make(['code' => 'produce_plan', 'id' => $id, 'action' => $action]);
+ return $this->display([
+ 'form' => $form,
+ ], 'create');
+ }
+
+ // 审核
+ public function auditAction()
+ {
+ return $this->createAction('edit');
+ }
+
+ // 创建
+ public function editAction()
+ {
+ return $this->createAction('edit');
+ }
+
+ // 创建
+ public function showAction()
+ {
+ return $this->createAction('show');
+ }
+
+ // 参照订单计划
+ public function orderPlanAction()
+ {
+ $plan_date = Request::get('plan_date');
+ $rows = [];
+ if ($plan_date) {
+ $rows = ProduceService::getProducePlanQuantity($plan_date);
+ if (empty($rows)) {
+ return $this->json($plan_date.'无营销计划和外贸订单。');
+ } else {
+ return $this->json($rows, true);
+ }
+ }
+ return $this->json($rows, true);
+ }
+
+ // 删除
+ public function deleteAction()
+ {
+ if (Request::method() == 'POST') {
+ $ids = Request::get('id');
+ return Form::remove(['code' => 'produce_plan', 'ids' => $ids]);
+ }
+ }
+}
diff --git a/app/Gdoo/Produce/Hooks/PlanDataHook.php b/app/Gdoo/Produce/Hooks/PlanDataHook.php
new file mode 100644
index 00000000..d1c0cabc
--- /dev/null
+++ b/app/Gdoo/Produce/Hooks/PlanDataHook.php
@@ -0,0 +1,13 @@
+orderBy('product_id_product.code', 'asc');
+ $params['q'] = $q;
+ return $params;
+ }
+}
diff --git a/app/Gdoo/Produce/Models/Formula.php b/app/Gdoo/Produce/Models/Formula.php
new file mode 100644
index 00000000..8e60851e
--- /dev/null
+++ b/app/Gdoo/Produce/Models/Formula.php
@@ -0,0 +1,26 @@
+ 'tab',
+ 'items' => [
+ ['value' => 'material.index', 'url' => 'produce/material/index', 'name' => '原辅料档案'],
+ ]
+ ];
+
+ public static $bys = [
+ 'name' => 'by',
+ 'items' => [
+ ['value' => '', 'name' => '全部'],
+ ['value' => 'divider'],
+ ['value' => 'day', 'name' => '今日创建'],
+ ['value' => 'week', 'name' => '本周创建'],
+ ['value' => 'month', 'name' => '本月创建'],
+ ]
+ ];
+}
diff --git a/app/Gdoo/Produce/Models/Material.php b/app/Gdoo/Produce/Models/Material.php
new file mode 100644
index 00000000..d6cb3738
--- /dev/null
+++ b/app/Gdoo/Produce/Models/Material.php
@@ -0,0 +1,26 @@
+ 'tab',
+ 'items' => [
+ ['value' => 'material.index', 'url' => 'produce/material/index', 'name' => '原辅料档案'],
+ ]
+ ];
+
+ public static $bys = [
+ 'name' => 'by',
+ 'items' => [
+ ['value' => '', 'name' => '全部'],
+ ['value' => 'divider'],
+ ['value' => 'day', 'name' => '今日创建'],
+ ['value' => 'week', 'name' => '本周创建'],
+ ['value' => 'month', 'name' => '本月创建'],
+ ]
+ ];
+}
diff --git a/app/Gdoo/Produce/Models/Plan.php b/app/Gdoo/Produce/Models/Plan.php
new file mode 100644
index 00000000..7ba5e6fe
--- /dev/null
+++ b/app/Gdoo/Produce/Models/Plan.php
@@ -0,0 +1,26 @@
+ 'tab',
+ 'items' => [
+ ['value' => 'plan.index', 'url' => 'produce/plan/index', 'name' => '生产计划单'],
+ ]
+ ];
+
+ public static $bys = [
+ 'name' => 'by',
+ 'items' => [
+ ['value' => '', 'name' => '全部'],
+ ['value' => 'divider'],
+ ['value' => 'day', 'name' => '今日创建'],
+ ['value' => 'week', 'name' => '本周创建'],
+ ['value' => 'month', 'name' => '本月创建'],
+ ]
+ ];
+}
diff --git a/app/Gdoo/Produce/Services/ProduceService.php b/app/Gdoo/Produce/Services/ProduceService.php
new file mode 100644
index 00000000..9e41864d
--- /dev/null
+++ b/app/Gdoo/Produce/Services/ProduceService.php
@@ -0,0 +1,1074 @@
+user();
+
+ $department_id = (int)$department_id;
+ $isCalAgain = (int)$isCalAgain;
+
+ $rows = [];
+
+ if ($date) {
+ if ($isCalAgain) {
+ // 删除先有计划
+ DB::delete("delete FROM material_plan_day where date = '$date' and Dept_Id=$department_id");
+
+ // 获取生产计划
+ $items = DB::select("
+ SELECT
+ d.product_id,
+ d.plan_num,
+ c.quantity as material_num,
+ c.material_id,
+ d.department_id,
+ case when ISNULL(c.ratio, 1) = 0 then 0 else ISNULL(d.plan_num, 0) * ISNULL(c.quantity, 0) / ISNULL(c.ratio, 1) end as total_num
+ FROM produce_plan m, produce_plan_data d
+ left join(
+ select a.product_id, a.material_id, a.quantity, b.ratio
+ from product_formula a left join product_material b on a.material_id = b.id
+ ) c on c.product_id = d.product_id
+ WHERE m.id = d.plan_id
+ AND isnull(department_id, 0) = $department_id
+ AND plan_num <> 0
+ AND plan_num IS NOT NULL
+ AND m.date = '$date'
+ ");
+
+ foreach($items as $item) {
+ DB::table("material_plan_day")->insert([
+ 'date' => $date,
+ 'code' => '',
+ 'dept_id' => $department_id,
+ 'product_id' => $item['product_id'],
+ 'product_num' => $item['plan_num'],
+ 'material_id' => $item['material_id'],
+ 'material_num' => $item['material_num'],
+ 'total_num' => $item['total_num'],
+ 'creator_id' => $user['id'],
+ 'creator_name' => $user['name'],
+ 'create_date' => date('Y-m-d H:i:s'),
+ 'remark' => '',
+ ]);
+ }
+ }
+
+ $rows = DB::select("
+ select a.id, a.date,
+ a.dept_id,
+ e.name as dept_name,
+ a.product_id,
+ b.name as product_name,
+ b.spec as product_spec,
+ c.name as product_unit,
+ a.product_num,
+ a.material_id,
+ d.category as category_name,
+ d.name as material_name,
+ a.material_num,
+ a.total_num,
+ a.creator_id,
+ a.creator_name,
+ a.create_date,
+ a.remark
+ from material_plan_day a
+ left join product b on a.product_id = b.id
+ left join product_unit AS c ON b.unit_id = c.id
+ left join product_material d on a.Material_Id = d.id
+ left join department e on a.Dept_Id = e.id
+ where a.date = '$date' and a.Dept_Id = $department_id
+ ");
+ }
+ return $rows;
+ }
+
+ public static function getPreShipDate($order_id, $date)
+ {
+ $order_id = (int)$order_id;
+
+ $sql = "a.product_id, sum(a.ky_num) stock_num
+ from (".StockService::getStockSelectSql().") a, (select product_id from customer_order_data where order_id = $order_id group by product_id) b
+ where a.product_id = b.product_id and (a.warehouse_name like '%成品%' or a.warehouse_code ='07') and a.warehouse_code <> '25'
+ group by a.product_id";
+
+ $stock = DB::query()->selectRaw($sql)->pluck('stock_num', 'product_id');
+
+ $sql = "product_id, SUM(plan_num) plan_num from(
+ SELECT d.product_id,
+ SUM(d.delivery_quantity)-ISNULL(SUM(i.num), 0)-ISNULL(SUM(r.num), 0) as plan_num
+ FROM customer_order_data AS d
+ LEFT JOIN customer_order AS m ON m.id = d.order_id
+ LEFT JOIN (select dd.sale_data_id, SUM(dd.quantity) num, SUM(dd.money) money, sum(dd.other_money) other_money
+ from stock_delivery_data dd, stock_delivery mm
+ where mm.id = dd.delivery_id
+ GROUP BY dd.sale_data_id
+ ) as i ON i.sale_data_id = d.id
+
+ LEFT JOIN (select dd.sale_data_id, SUM(dd.quantity) num from stock_allocation_data dd, stock_allocation mm
+ where mm.id = dd.allocation_id
+ GROUP BY dd.sale_data_id
+ ) as r ON r.sale_data_id = d.id
+
+ where ISNULL(d.use_close, 0) = 0 and m.status > 0";
+
+ if ($date) {
+ $sql .= " AND m.plan_delivery_dt <= '$date'";
+ }
+
+ $sql .= " and d.product_id in (select product_id from customer_order_data where order_id = $order_id)
+ and d.order_id <> $order_id
+ GROUP BY m.id, d.product_id, m.status
+ HAVING SUM(d.delivery_quantity)-ISNULL(SUM(i.num),0) - ISNULL(SUM(r.num),0) > 0
+ ) aa
+ group by product_id";
+
+ $plan = DB::query()->selectRaw($sql)->pluck('plan_num', 'product_id');
+
+ // 获取三天的生产计划
+ $start_dt = date('Y-m-d');
+ $end_dt = date('Y-m-d', strtotime('+2 day'));
+
+ $sql = "a.product_id, b.date, sum(a.plan_num) plan_num
+ from produce_plan_data a
+ left join produce_plan b on b.id = a.plan_id
+ left join customer_order_data c on c.product_id = a.product_id
+ where b.date BETWEEN ? and ? and a.product_id = c.product_id and c.order_id = $order_id
+ group by a.product_id, b.date";
+ $rows = DB::query()->selectRaw($sql, [$start_dt, $end_dt])->get();
+ $days = [];
+
+ $dates = date_range($start_dt, $end_dt);
+ foreach($dates as $index => $date) {
+ foreach($rows as $row) {
+ if ($date == $row['date']) {
+ $days[$index][$row['product_id']] += $row['plan_num'];
+ }
+ }
+ }
+
+ $sql = "order_id, product_id, p.name as product_name, p.spec as product_spec, SUM(delivery_quantity) num
+ from customer_order_data
+ left join product as p on p.id = product_id
+ where order_id = $order_id
+ group by order_id ,product_id, p.name, p.spec
+ ";
+ $rows = DB::query()->selectRaw($sql)->get();
+
+ $rows->transform(function($row) use ($stock, $plan, $days) {
+ $product_id = $row['product_id'];
+ $row['product_num'] = $stock[$product_id] - $plan[$product_id];
+ $row['need_num'] = $row['num'] - ($stock[$product_id] - $plan[$product_id]);
+
+ $row['day1'] = $days[0][$product_id];
+ $row['day2'] = $days[1][$product_id];
+ $row['day3'] = $days[2][$product_id];
+ return $row;
+ });
+
+ return $rows;
+ }
+
+ public static function getProducePlanQuantity($date) {
+ // 计划日期
+ $end_dt = $date;
+ // 计划日期两天前
+ $start_dt = date('Y-m-d', strtotime($date.' -2 day'));
+
+ $now_month = date('Y-m', strtotime($date));
+ $last_month = date('Y-m', strtotime($date.' -1 month'));
+
+ // 查询未发内销订单
+ $sql[] = "
+ select a.product_id,
+ sum(wf_num) as wf_num,
+ sum(wf_num_ydk) as wf_num_ydk,
+ sum(wf_num_wdk) as wf_num_wdk,
+ 0 as fhjh_num,
+ 0 as stock_num,
+ 0 as ljrk_num,
+ 0 as syfh_num,
+ 0 yxjh_num,
+ 0 as yxjh_num1,
+ 0 as yxjh_num2,
+ 0 as plan_num1,
+ 0 as plan_num2,
+ null as batch_sn
+ from (
+ select d.product_id,
+ SUM(d.delivery_quantity) - ISNULL(SUM(sd.quantity), 0) - ISNULL(SUM(sa.quantity), 0) as wf_num,
+ CASE WHEN ISNULL(m.pay_dt, null) <> null or m.status = 1 THEN SUM(d.delivery_quantity) - ISNULL(SUM(sd.quantity), 0)-ISNULL(SUM(sa.quantity), 0) ELSE 0 END as wf_num_ydk,
+ CASE WHEN ISNULL(m.pay_dt, null) = null or m.status <> 0 THEN SUM(d.delivery_quantity) - ISNULL(SUM(sd.quantity), 0)-ISNULL(SUM(sa.quantity), 0) ELSE 0 END as wf_num_wdk
+ from customer_order_data d
+ left join customer_order as m ON m.id = d.order_id
+
+ LEFT JOIN (select sdd.sale_data_id, SUM(sdd.quantity) quantity
+ from stock_delivery_data sdd, stock_delivery sd
+ where sd.id = sdd.delivery_id
+ GROUP BY sdd.sale_data_id
+ ) as sd ON sd.sale_data_id = d.id
+
+ LEFT JOIN (select sad.sale_data_id, SUM(sad.quantity) quantity
+ from stock_allocation_data sad, stock_allocation sa
+ where sa.id = sad.allocation_id
+ GROUP BY sad.sale_data_id
+ ) as sa ON sa.sale_data_id = d.id
+
+ LEFT JOIN product p on d.product_id = p.id
+ LEFT JOIN product_category pc on pc.id = p.category_id
+ where ISNULL(d.use_close, 0) = 0
+ and m.status > 0
+ AND LEFT(pc.name, 2) <> '外销'
+
+ and m.created_at > 1585065600
+
+ GROUP BY d.product_id, ISNULL(m.pay_dt, null), m.status
+ HAVING SUM(d.delivery_quantity) - ISNULL(SUM(sd.quantity), 0) - ISNULL(SUM(sa.quantity),0) <> 0
+ ) as a
+
+ GROUP BY a.product_id
+ ";
+
+ // 内销库存
+ $sql[] = "
+ select product_id,
+ 0 as wf_num,
+ 0 as wf_num_ydk,
+ 0 as wf_num_wdk,
+ 0 as fhjh_num,
+ SUM(ISNULL(ky_num, 0)) as stock_num,
+ 0 as ljrk_num,
+ 0 as syfh_num,
+ 0 yxjh_num,
+ 0 as yxjh_num1,
+ 0 as yxjh_num2,
+ 0 as plan_num1,
+ 0 as plan_num2,
+ null as batch_sn
+ from (".StockService::getStockSelectSql().") kc
+ LEFT JOIN product_category pc on pc.id = category_id
+ WHERE product_type = 1 and warehouse_name LIKE '%成品%' and left(pc.name, 2) <> '外销' and warehouse_code <> '25'
+ GROUP by product_id
+ HAVING sum(ky_num) <> 0";
+
+ // 外销库存
+ $sql[] = "
+ select product_id,
+ 0 as wf_num,
+ 0 as wf_num_ydk,
+ 0 as wf_num_wdk,
+ 0 as fhjh_num,
+ SUM(ISNULL(ky_num, 0)) as stock_num,
+ 0 as ljrk_num,
+ 0 as syfh_num,
+ 0 yxjh_num,
+ 0 as yxjh_num1,
+ 0 as yxjh_num2,
+ 0 as plan_num1,
+ 0 as plan_num2,
+ batch_sn
+ from (".StockService::getStockSelectSql().") ss
+ LEFT JOIN product_category pc on pc.id = category_id
+ WHERE product_type = 1 and warehouse_name LIKE '%成品%' and left(pc.name, 2) = '外销' and warehouse_code <> '25'
+ GROUP by product_id, batch_sn
+ HAVING sum(ky_num) <> 0";
+
+ // 查询未发外贸订单
+ $sql[] = "
+ select a.product_id,
+ sum(wf_num) as wf_num,
+ sum(wf_num_ydk) as wf_num_ydk,
+ sum(wf_num_wdk) as wf_num_wdk,
+ 0 as fhjh_num,
+ 0 as stock_num,
+ sum(wxrk_num) as ljrk_num,
+ 0 as syfh_num,
+ 0 as yxjh_num,
+ 0 as yxjh_num1,
+ 0 as yxjh_num2,
+ 0 as plan_num1,
+ 0 as plan_num2,
+ a.batch_sn
+ from (
+ select d.product_id, ISNULL(d.batch_sn, null) as batch_sn,
+ SUM(ISNULL(y.wxrk_num, 0)) as wxrk_num,
+ SUM(d.delivery_quantity)-ISNULL(SUM(sd.quantity),0) - ISNULL(SUM(sa.quantity), 0) as wf_num,
+ -- 获取已打款和审核生效未发货的订单
+ CASE WHEN ISNULL(m.pay_dt, null) <> null or m.status = 1 THEN SUM(d.delivery_quantity) - ISNULL(SUM(sd.quantity),0) - ISNULL(SUM(sa.quantity), 0) ELSE 0 END as wf_num_ydk,
+ -- 获取未打款未审核通过的订单
+ CASE WHEN ISNULL(m.pay_dt, null) = null and m.status <> 1 THEN SUM(d.delivery_quantity) - ISNULL(SUM(sd.quantity),0) - ISNULL(SUM(sa.quantity), 0) ELSE 0 END as wf_num_wdk
+ from customer_order_data d
+ left join customer_order as m ON m.id = d.order_id
+
+ -- 获取发货单
+ LEFT JOIN (select sdd.sale_data_id, SUM(sdd.quantity) quantity
+ from stock_delivery_data sdd, stock_delivery sd
+ where sd.id = sdd.delivery_id
+ GROUP BY sdd.sale_data_id
+ ) as sd ON sd.sale_data_id = d.id
+
+ -- 获取调拨单
+ LEFT JOIN (select sad.sale_data_id, SUM(sad.quantity) quantity
+ from stock_allocation_data sad, stock_allocation sa
+ where sa.id = sad.allocation_id
+ GROUP BY sad.sale_data_id
+ ) as sa ON sa.sale_data_id = d.id
+
+ -- 外销入库
+ LEFT JOIN (select dd.batch_sn, dd.product_id, SUM(dd.quantity) wxrk_num
+ from stock_record10_data dd, stock_record10 mm
+ where mm.id = dd.record10_id
+ GROUP BY dd.batch_sn, dd.product_id
+ ) as y ON y.product_id = d.product_id AND y.batch_sn = d.batch_sn
+
+ LEFT JOIN product p on d.product_id = p.id
+ LEFT JOIN product_category pc on pc.id = p.category_id
+ where ISNULL(d.use_close,0) = 0
+ AND m.status > 0
+ and LEFT(pc.name, 2) = '外销'
+
+ and m.created_at > 1585065600
+
+ GROUP BY d.product_id, d.batch_sn, ISNULL(m.pay_dt, null), m.status
+ HAVING SUM(d.delivery_quantity) - ISNULL(SUM(sd.quantity), 0) - ISNULL(SUM(sa.quantity), 0) <> 0
+ ) as a
+
+ GROUP BY a.product_id, a.batch_sn
+ ";
+
+ // 内销发货计划
+ $sql[] = "
+ SELECT d.product_id,
+ 0 as wf_num,
+ 0 as wf_num_ydk,
+ 0 as wf_num_wdk,
+ SUM(d.delivery_quantity) as fhjh_num,
+ 0 as stock_num,
+ 0 as ljrk_num,
+ 0 as syfh_num,
+ 0 as yxjh_num,
+ 0 as yxjh_num1,
+ 0 as yxjh_num2,
+ 0 as plan_num1,
+ 0 as plan_num2,
+ null as batch_sn
+ FROM customer_order_data AS d
+ LEFT JOIN customer_order AS m ON m.id = d.order_id
+ LEFT JOIN product p on d.product_id = p.id
+ LEFT JOIN product_category pc on pc.id = p.category_id
+ where m.plan_delivery_dt BETWEEN '$start_dt' and '$end_dt'
+ AND ISNULL(d.use_close, 0) = 0 AND left(pc.name, 2) <> '外销'
+ GROUP BY d.product_id
+ ";
+
+ // 外销发货计划
+ $sql[] = "
+ SELECT d.product_id,
+ 0 as wf_num,
+ 0 as wf_num_ydk,
+ 0 as wf_num_wdk,
+ SUM(d.delivery_quantity) as fhjh_num,
+ 0 as stock_num,
+ 0 as ljrk_num,
+ 0 as syfh_num,
+ 0 as yxjh_num,
+ 0 as yxjh_num1,
+ 0 as yxjh_num2,
+ 0 as plan_num1,
+ 0 as plan_num2,
+ ISNULL(d.batch_sn, null) as batch_sn
+ FROM customer_order_data AS d
+ LEFT JOIN customer_order AS m ON m.id = d.order_id
+ LEFT JOIN product p on d.product_id = p.id
+ LEFT JOIN product_category pc on pc.id = p.category_id
+ where m.plan_delivery_dt BETWEEN '$start_dt' and '$end_dt'
+ AND ISNULL(d.use_close, 0) = 0 AND left(pc.name, 2) = '外销'
+ GROUP BY d.product_id, d.batch_sn";
+
+ // 营销计划数量
+ // 获取按天计算时间差的sql片段
+ $sql_day_diff = sql_day_diff('d.date', $end_dt);
+ $sql[] = "
+ SELECT product_id,
+ 0 as wf_num,
+ 0 as wf_num_ydk,
+ 0 as wf_num_wdk,
+ 0 as fhjh_num,
+ 0 as stock_num,
+ 0 as ljrk_num,
+ 0 as syfh_num,
+ sum(yxjh_num) as yxjh_num,
+ sum(yxjh_num1) as yxjh_num1,
+ sum(yxjh_num2) as yxjh_num2,
+ 0 as plan_num1,
+ 0 as plan_num2,
+ null as batch_sn
+ FROM (
+ SELECT d.product_id,
+ -- 后天营销
+ sum(CASE WHEN ".$sql_day_diff." = 0 THEN ISNULL(d.quantity, 0) ELSE 0 END) as yxjh_num,
+ -- 明天营销
+ sum(CASE WHEN ".$sql_day_diff." = 1 THEN ISNULL(d.quantity, 0) ELSE 0 END) as yxjh_num1,
+ -- 今天营销
+ sum(CASE WHEN ".$sql_day_diff." = 2 THEN ISNULL(d.quantity, 0) ELSE 0 END) as yxjh_num2
+ FROM produce_data AS d
+ where d.date BETWEEN '$start_dt' and '$end_dt'
+ GROUP BY d.product_id, d.date
+ ) as a
+ GROUP BY a.product_id";
+
+ // 生产计划数量
+ // 获取按天计算时间差的sql片段
+ $sql_day_diff = sql_day_diff('m.date', $end_dt);
+ $sql[] = "
+ SELECT product_id,
+ 0 as wf_num,
+ 0 as wf_num_ydk,
+ 0 as wf_num_wdk,
+ 0 as fhjh_num,
+ 0 as stock_num,
+ 0 as ljrk_num,
+ 0 as syfh_num,
+ 0 as yxjh_num,
+ 0 as yxjh_num1,
+ 0 as yxjh_num2,
+ sum(plan_num1) as plan_num1,
+ sum(plan_num2) as plan_num2,
+ batch_sn
+ FROM (
+ SELECT d.product_id, ISNULL(d.batch_sn, null) as batch_sn,
+ -- 明天计划数量
+ sum(CASE WHEN ".$sql_day_diff." = 1 THEN ISNULL(d.plan_num, 0) ELSE 0 END) as plan_num1,
+ -- 今天计划数量
+ sum(CASE WHEN ".$sql_day_diff." = 2 THEN ISNULL(d.plan_num, 0) ELSE 0 END) as plan_num2
+ FROM produce_plan m, produce_plan_data AS d
+ where m.date BETWEEN '$start_dt' and '$end_dt'
+ and m.id = d.plan_id
+ and m.status = 1
+ GROUP BY m.date, d.product_id, d.batch_sn
+ ) as a
+ GROUP BY a.product_id, a.batch_sn";
+
+ // 入库数量
+ $sql[] = "
+ SELECT
+ d.product_id,
+ 0 as wf_num,
+ 0 as wf_num_ydk,
+ 0 as wf_num_wdk,
+ 0 as fhjh_num,
+ 0 as stock_num,
+ SUM(d.quantity) as ljrk_num,
+ 0 as syfh_num,
+ 0 as yxjh_num,
+ 0 as yxjh_num1,
+ 0 as yxjh_num2,
+ 0 as plan_num1,
+ 0 as plan_num2,
+ null as batch_sn
+ FROM stock_record10 AS m
+ INNER JOIN stock_record10_data AS d ON m.id = d.record10_id
+ LEFT JOIN product p on d.product_id = p.id
+ LEFT JOIN product_category pc on pc.id = p.category_id
+ where ".sql_year_month('m.invoice_dt')." = '$now_month'
+ and left(pc.name, 2) <> '外销'
+ GROUP BY d.product_id";
+
+ // 上月发货数量
+ $sql[] = "
+ SELECT d.product_id,
+ 0 as wf_num,
+ 0 as wf_num_ydk,
+ 0 as wf_num_wdk,
+ 0 as fhjh_num,
+ 0 as stock_num,
+ 0 as ljrk_num,
+ SUM(d.quantity) as syfh_num,
+ 0 as yxjh_num,
+ 0 as yxjh_num1,
+ 0 as yxjh_num2,
+ 0 as plan_num1,
+ 0 as plan_num2,
+ null as batch_sn
+ from stock_delivery as m
+ left join stock_delivery_data as d on m.id = d.delivery_id
+ LEFT JOIN product p on d.product_id = p.id
+ LEFT JOIN product_category pc on pc.id = p.category_id
+ where ".sql_year_month('m.invoice_dt')." = '$last_month'
+ and left(pc.name, 2) <> '外销'
+ group by d.product_id";
+
+ $sql = "select p.department_id, dep.name department_name, dep.name department_id_name,p.category_id,
+ p.id product_id, p.code product_code,p.name product_name,p.spec product_spec, u.name product_unit,
+ sum(wf_num) dphz_num,
+ sum(yxjh_num) as yxjh_num,
+ sum(yxjh_num1) as yxjh_num1,
+ sum(yxjh_num2) as yxjh_num2,
+ sum(ljrk_num) as ljrk_num,
+ sum(plan_num1) as plan_num1,
+ sum(plan_num2) as plan_num2,
+ sum(fhjh_num) as fhjh_num,
+ sum(stock_num) as stock_num,
+ ISNULL(sum(wf_num),0) - ISNULL(sum(stock_num),0) as xqzc_num,
+ ISNULL(sum(wf_num_ydk),0) - ISNULL(sum(stock_num),0) as dkzc_num,
+ sum(syfh_num) as syfh_num, batch_sn
+ from product p
+ left join (".join(' UNION ALL ', $sql).") as temp on p.id = temp.product_id
+ LEFT JOIN product_category pc on pc.id = p.category_id
+ LEFT join product_unit AS u ON p.unit_id = u.id
+ LEFT join department dep ON p.department_id = dep.id
+ where p.product_type = 1 and pc.type = 1 and p.code != ''
+ group by p.department_id, dep.name, p.category_id, pc.name, p.id, p.code, p.name, p.spec, u.name, batch_sn
+ order by p.code asc";
+ return DB::select($sql);
+ }
+
+ /**
+ * 生产计划(营销)
+ * @start_dt
+ * @end_dt
+ * @warehouse_id
+ * @product_category_id
+ * @ny --内销1 外销2
+ */
+
+ public static function getPlanDetail($start_dt, $end_dt, $warehouse_id, $product_category_id, $ny)
+ {
+ $warehouse_id = (int)$warehouse_id;
+ $product_category_id = (int)$product_category_id;
+ $ny = (int)$ny;
+
+ // 产品编码
+ $category_code = DB::table('product_category')->where('id', $product_category_id)->value('code');
+ $last_month = date('Y-m', strtotime($start_dt.' -1 month'));
+
+ // 内销订单
+ $sql = "
+ SELECT product_id,
+ '' batch_sn,
+ null invoice_dt,
+ 0 wfhjh_num,
+ 0 fhjh_num,
+ 0 sale_plan_num,
+ 0 pro_plan_num,
+ 0 pro_bg_num,
+ 0 rk_num,
+ sum(wf_num) wf_num,
+ sum(wf_num_ydk) wf_num_ydk,
+ sum(wf_num_wdk) wf_num_wdk,
+ 0 waitin_num,
+ 0 kc_num,
+ 0 syfh_num
+ from (
+ SELECT d.product_id,
+ SUM(d.delivery_quantity)-ISNULL(SUM(i.quantity),0)-ISNULL(SUM(r.quantity),0) as wf_num,
+ CASE WHEN ISNULL(m.pay_dt, null) <> null or m.status = 1 then SUM(d.delivery_quantity)-ISNULL(SUM(i.quantity),0)-ISNULL(SUM(r.quantity),0) ELSE 0 end as wf_num_ydk,
+ CASE WHEN ISNULL(m.pay_dt, null) = null or m.status <> 0 then SUM(d.delivery_quantity)-ISNULL(SUM(i.quantity),0)-ISNULL(SUM(r.quantity),0) ELSE 0 end as wf_num_wdk
+ FROM customer_order AS m
+ INNER JOIN customer_order_data AS d ON m.id = d.order_id
+ LEFT OUTER JOIN(
+ select dd.sale_data_id, SUM(dd.quantity) quantity, SUM(dd.money) money, sum(dd.other_money) other_money
+ from stock_delivery_data dd, stock_delivery mm
+ where mm.id = dd.delivery_id
+ GROUP BY dd.sale_data_id
+ ) as i ON i.sale_data_id = d.id
+ LEFT OUTER JOIN (
+ select dd.sale_data_id, SUM(dd.quantity) quantity
+ from stock_allocation_data dd, stock_allocation mm
+ where mm.id = dd.allocation_id
+ GROUP BY dd.sale_data_id
+ ) as r ON r.sale_data_id = d.id
+
+ LEFT JOIN product p on d.product_id = p.id
+ LEFT JOIN product_category pc on pc.id = p.category_id
+
+ where ISNULL(d.use_close, 0) = 0
+ and m.status > 0
+ and m.created_at > 1585065600
+ AND LEFT(pc.name, 2) <> '外销'
+ GROUP BY d.product_id, ISNULL(m.pay_dt, null), m.status
+ HAVING SUM(d.delivery_quantity) - ISNULL(SUM(i.quantity),0) - ISNULL(SUM(r.quantity),0) <> 0
+ ) as a
+ GROUP BY product_id
+
+ UNION ALL
+
+ --未发外销订单
+ SELECT product_id,
+ batch_sn,
+ null invoice_dt,
+ 0 wfhjh_num,
+ 0 fhjh_num,
+ 0 sale_plan_num,
+ 0 pro_plan_num,
+ 0 pro_bg_num,
+ 0 rk_num,
+ sum(wf_num) as wf_num,
+ sum(wf_num_ydk) as wf_num_ydk,
+ sum(wf_num_wdk) as wf_num_wdk,
+ 0 waitin_num,
+ 0 kc_num,
+ 0 syfh_num
+ from (
+ SELECT d.product_id, d.batch_sn,
+ SUM(ISNULL(y.wxrk_num, 0)) as wxrk_num,
+ SUM(d.delivery_quantity)-ISNULL(SUM(i.quantity),0)-ISNULL(SUM(r.quantity),0) as wf_num,
+ CASE WHEN ISNULL(m.pay_dt, null) <> null or m.status = 1 then SUM(d.delivery_quantity)-ISNULL(SUM(i.quantity),0)-ISNULL(SUM(r.quantity),0) ELSE 0 end as wf_num_ydk,
+ CASE WHEN ISNULL(m.pay_dt, null) = null or m.status <> 0 then SUM(d.delivery_quantity)-ISNULL(SUM(i.quantity),0)-ISNULL(SUM(r.quantity),0) ELSE 0 end as wf_num_wdk
+ FROM customer_order AS m
+ INNER JOIN customer_order_data AS d ON m.id = d.order_id
+
+ LEFT JOIN (select dd.sale_data_id,SUM(dd.quantity) quantity, SUM(dd.money) money, sum(dd.other_money) other_money
+ from stock_delivery_data dd, stock_delivery mm
+ where mm.id = dd.delivery_id
+ GROUP BY dd.sale_data_id
+ ) as i ON i.sale_data_id=d.id
+
+ LEFT JOIN (select dd.sale_data_id,SUM(dd.quantity) quantity
+ from stock_allocation_data dd, stock_allocation mm
+ where mm.id = dd.allocation_id
+ GROUP BY dd.sale_data_id
+ ) as r ON r.sale_data_id = d.id
+
+ -- 外销入库
+ LEFT JOIN (select dd.batch_sn, dd.product_id, SUM(dd.quantity) wxrk_num
+ from stock_record10_data dd, stock_record10 mm
+ where mm.id = dd.record10_id
+ GROUP BY dd.batch_sn, dd.product_id
+ ) as y ON y.product_id = d.product_id AND y.batch_sn = d.batch_sn
+
+ LEFT JOIN product p on d.product_id = p.id
+ LEFT JOIN product_category pc on pc.id = p.category_id
+
+ where ISNULL(d.use_close,0) = 0
+ and m.created_at > 1585065600
+ AND m.status > 0
+ and LEFT(pc.name, 2) = '外销'
+ GROUP BY d.product_id, d.batch_sn, ISNULL(m.pay_dt, null), m.status
+ HAVING SUM(d.delivery_quantity)-ISNULL(SUM(i.quantity),0)-ISNULL(SUM(r.quantity),0) <> 0
+ ) as b
+ GROUP BY product_id, batch_sn
+
+ UNION ALL
+
+ --内销库存(包含物料)
+ SELECT product_id,
+ '' batch_sn,
+ null invoice_dt,
+ 0 wfhjh_num,
+ 0 fhjh_num,
+ 0 sale_plan_num,
+ 0 pro_plan_num,
+ 0 pro_bg_num,
+ 0 rk_num,
+ 0 wf_num,
+ 0 wf_num_ydk,
+ 0 wf_num_wdk,
+ 0 waitin_num,
+ sum(kc.ky_num) as kc_num,
+ 0 syfh_num
+ from (".StockService::getStockSelectSql().") kc
+ LEFT JOIN product p on kc.product_id = p.id
+ LEFT JOIN product_category pc on pc.id = p.category_id
+ WHERE (p.product_type=1 or p.material_type > 0) and
+ (kc.warehouse_name LIKE '%成品%' or kc.warehouse_code = '07') and kc.warehouse_code <> '25'
+ and ($warehouse_id = 0 OR kc.warehouse_id = $warehouse_id)
+ AND LEFT(pc.name, 2) <> '外销'
+ GROUP by kc.product_id
+ HAVING sum(kc.ky_num) <> 0
+
+ UNION ALL
+
+ --外销库存
+ SELECT
+ kc.product_id,
+ kc.batch_sn,
+ null invoice_dt,
+ 0 wfhjh_num,
+ 0 fhjh_num,
+ 0 sale_plan_num,
+ 0 pro_plan_num,
+ 0 pro_bg_num,
+ 0 rk_num,
+ 0 wf_num,
+ 0 wf_num_ydk,
+ 0 wf_num_wdk,
+ 0 waitin_num,
+ sum(kc.ky_num) as kc_num,
+ 0 syfh_num
+ from (".StockService::getStockSelectSql().") kc
+ LEFT JOIN product p on kc.product_id = p.id
+ LEFT JOIN product_category pc on pc.id = p.category_id
+ WHERE p.product_type=1 and kc.warehouse_name LIKE '%成品%' and kc.warehouse_code <> '25'
+ AND ($warehouse_id = 0 OR kc.warehouse_id = $warehouse_id)
+ AND LEFT(pc.name, 2) = '外销'
+ GROUP by kc.product_id, kc.batch_sn
+ HAVING sum(kc.ky_num) <> 0
+
+ UNION ALL
+
+ --川南库存
+ SELECT
+ kc.product_id,
+ '' batch_sn,
+ null invoice_dt,
+ 0 wfhjh_num,
+ 0 fhjh_num,
+ 0 sale_plan_num,
+ 0 pro_plan_num,
+ 0 pro_bg_num,
+ 0 rk_num,
+ 0 wf_num,
+ 0 wf_num_ydk,
+ 0 wf_num_wdk,
+ sum(kc.ky_num) as waitin_num,
+ 0 kc_num,
+ 0 syfh_num
+ from (".StockService::getStockSelectSql().") kc
+ LEFT JOIN product p on kc.product_id = p.id
+ WHERE p.product_type = 1 and kc.warehouse_name LIKE '%川南%'
+ and ($warehouse_id = 0 OR kc.warehouse_id = $warehouse_id)
+ GROUP by kc.product_id
+ HAVING sum(kc.ky_num) <> 0
+
+ UNION ALL
+
+ --上月发货
+ SELECT
+ d.product_id,
+ '' batch_sn,
+ null invoice_dt,
+ 0 wfhjh_num,
+ 0 fhjh_num,
+ 0 sale_plan_num,
+ 0 pro_plan_num,
+ 0 pro_bg_num,
+ 0 rk_num,
+ 0 wf_num,
+ 0 wf_num_ydk,
+ 0 wf_num_wdk,
+ 0 waitin_num,
+ 0 kc_num,
+ sum(d.quantity) as syfh_num
+ from stock_delivery_data d
+ LEFT JOIN stock_delivery m ON d.delivery_id = m.id
+ LEFT JOIN product p on d.product_id = p.id
+ WHERE m.id = d.delivery_id
+ AND ".sql_year_month('m.invoice_dt')." = '$last_month'
+ GROUP by d.product_id
+ HAVING sum(d.quantity) <> 0
+
+ UNION ALL
+
+ --内销发货计划
+ SELECT
+ d.product_id,
+ '' batch_sn,
+ m.plan_delivery_dt as invoice_dt,
+ 0 wfhjh_num,
+ SUM(d.delivery_quantity) as fhjh_num,
+ 0 sale_plan_num,
+ 0 pro_plan_num,
+ 0 pro_bg_num,
+ 0 rk_num,
+ 0 wf_num,
+ 0 wf_num_ydk,
+ 0 wf_num_wdk,
+ 0 waitin_num,
+ 0 kc_num,
+ 0 syfh_num
+ FROM customer_order_data AS d
+ LEFT JOIN customer_order AS m ON m.id = d.order_id
+ LEFT JOIN product p on d.product_id = p.id
+ LEFT JOIN product_category pc on pc.id = p.category_id
+ where m.plan_delivery_dt between '$start_dt' and '$end_dt'
+ AND ISNULL(d.use_close, 0) = 0 and m.status > 0 AND left(pc.name, 2) <> '外销'
+ GROUP BY m.plan_delivery_dt, d.product_id
+
+ UNION ALL
+
+ --外销发货计划
+ SELECT d.product_id,
+ d.batch_sn,
+ m.plan_delivery_dt as invoice_dt,
+ 0 wfhjh_num,
+ SUM(d.delivery_quantity) as fhjh_num,
+ 0 sale_plan_num,
+ 0 pro_plan_num,
+ 0 pro_bg_num,
+ 0 rk_num,
+ 0 wf_num,
+ 0 wf_num_ydk,
+ 0 wf_num_wdk,
+ 0 waitin_num,
+ 0 kc_num,
+ 0 syfh_num
+ FROM customer_order_data AS d
+ left JOIN customer_order AS m ON m.id = d.order_id
+ LEFT JOIN product p on d.product_id = p.id
+ LEFT JOIN product_category pc on pc.id = p.category_id
+ where m.plan_delivery_dt between '$start_dt' and '$end_dt'
+ AND ISNULL(d.use_close, 0) = 0 and m.status > 0 and left(pc.name, 2) = '外销'
+ GROUP BY m.plan_delivery_dt, d.product_id, d.batch_sn
+
+ UNION ALL
+
+ --发货计划未发
+ SELECT product_id,
+ '' batch_sn,
+ invoice_dt,
+ SUM(wfhjh_num) as wfhjh_num,
+ 0 fhjh_num,
+ 0 sale_plan_num,
+ 0 pro_plan_num,
+ 0 pro_bg_num,
+ 0 rk_num,
+ 0 wf_num,
+ 0 wf_num_ydk,
+ 0 wf_num_wdk,
+ 0 waitin_num,
+ 0 kc_num,
+ 0 syfh_num
+ FROM (
+ SELECT d.product_id,
+ m.plan_delivery_dt as invoice_dt,
+ ISNULL(SUM(d.delivery_quantity), 0) - ISNULL(SUM(i.quantity), 0) - ISNULL(SUM(r.quantity), 0) as wfhjh_num
+ FROM customer_order_data AS d
+ LEFT JOIN customer_order AS m ON m.id = d.order_id
+
+ LEFT JOIN (select dd.sale_data_id, SUM(dd.quantity) quantity
+ from stock_delivery_data dd, stock_delivery mm
+ where mm.id = dd.delivery_id
+ GROUP BY dd.sale_data_id
+ ) as i ON i.sale_data_id = d.id
+
+ LEFT JOIN (select dd.sale_data_id, SUM(dd.quantity) quantity
+ from stock_allocation_data dd, stock_allocation mm
+ where mm.id = dd.allocation_id
+ GROUP BY dd.sale_data_id
+ ) as r ON r.sale_data_id = d.id
+
+ LEFT JOIN product p on d.product_id = p.id
+ LEFT JOIN product_category pc on pc.id = p.category_id
+
+ where m.plan_delivery_dt between '$start_dt' and '$end_dt'
+ AND ISNULL(d.use_close, 0) = 0 and m.status > 0 and left(pc.name, 2) <> '外销'
+ GROUP BY m.plan_delivery_dt, d.id, d.product_id, i.quantity, r.quantity
+ having ISNULL(SUM(d.delivery_quantity), 0) - ISNULL(SUM(i.quantity), 0) - ISNULL(SUM(r.quantity), 0) <> 0
+ ) as c
+ GROUP BY invoice_dt, product_id
+
+ UNION ALL
+
+ --发货计划未发
+ SELECT d.product_id,
+ '' batch_sn,
+ null invoice_dt,
+ SUM(wfhjh_num) as wfhjh_num,
+ 0 fhjh_num,
+ 0 sale_plan_num,
+ 0 pro_plan_num,
+ 0 pro_bg_num,
+ 0 rk_num,
+ 0 wf_num,
+ 0 wf_num_ydk,
+ 0 wf_num_wdk,
+ 0 as waitin_num,
+ 0 as kc_num,
+ 0 as syfh_num
+ FROM (
+ SELECT d.product_id,
+ m.plan_delivery_dt as invoice_dt,
+ isnull(SUM(d.delivery_quantity), 0) - isnull(SUM(i.quantity), 0)-isnull(SUM(r.quantity), 0) as wfhjh_num
+ FROM customer_order_data AS d
+ left JOIN customer_order AS m ON m.id = d.order_id
+
+ LEFT JOIN (select dd.sale_data_id, SUM(dd.quantity) quantity
+ from stock_delivery_data dd, stock_delivery mm
+ where mm.id = dd.delivery_id
+ GROUP BY dd.sale_data_id
+ ) as i ON i.sale_data_id = d.id
+
+ LEFT JOIN (select dd.sale_data_id, SUM(dd.quantity) quantity
+ from stock_allocation_data dd, stock_allocation mm
+ where mm.id = dd.allocation_id
+ GROUP BY dd.sale_data_id
+ ) as r ON r.sale_data_id = d.id
+
+ LEFT JOIN product p on d.product_id = p.id
+ LEFT JOIN product_category pc on pc.id = p.category_id
+
+ where m.plan_delivery_dt between '$start_dt' and '$end_dt'
+ AND ISNULL(d.use_close,0) = 0 and m.status > 0 and left(pc.name, 2) = '外销'
+ GROUP BY m.plan_delivery_dt, d.id, d.product_id, i.quantity, r.quantity
+ having isnull(SUM(d.delivery_quantity), 0) - isnull(SUM(i.quantity), 0) - isnull(SUM(r.quantity), 0) <> 0
+ ) as d
+ GROUP BY invoice_dt, product_id
+
+ UNION ALL
+
+ --营销计划数量
+ SELECT d.product_id,
+ '' batch_sn,
+ d.date as invoice_dt,
+ 0 wfhjh_num,
+ 0 fhjh_num,
+ SUM(d.quantity) sale_plan_num,
+ 0 pro_plan_num,
+ 0 pro_bg_num,
+ 0 rk_num,
+ 0 wf_num,
+ 0 wf_num_ydk,
+ 0 wf_num_wdk,
+ 0 as waitin_num,
+ 0 as kc_num,
+ 0 as syfh_num
+ FROM produce_data AS d
+ where d.date between '$start_dt' and '$end_dt'
+ GROUP BY d.date, d.product_id
+
+ UNION ALL
+
+ --生产计划数量
+ SELECT d.product_id,
+ d.batch_sn,
+ m.date as invoice_dt,
+ 0 as wfhjh_num,
+ 0 as fhjh_num,
+ 0 sale_plan_num,
+ SUM(d.plan_num) pro_plan_num,
+ 0 pro_bg_num,
+ 0 rk_num,
+ 0 wf_num,
+ 0 wf_num_ydk,
+ 0 wf_num_wdk,
+ 0 as waitin_num,
+ 0 as kc_num,
+ 0 as syfh_num
+ FROM produce_plan m, produce_plan_data AS d
+ where m.date between '$start_dt' and '$end_dt'
+ and m.id = d.plan_id
+ and m.status = 1
+ and plan_num <> 0
+ GROUP BY m.date, d.product_id, d.batch_sn
+
+ UNION ALL
+
+ --生产计划变更数量
+ SELECT d.product_id,
+ d.batch_sn,
+ m.date as invoice_dt,
+ 0 as wfhjh_num,
+ 0 as fhjh_num,
+ 0 sale_plan_num,
+ 0 pro_plan_num,
+ SUM(d.plan_num) pro_bg_num,
+ 0 rk_num,
+ 0 wf_num,
+ 0 wf_num_ydk,
+ 0 wf_num_wdk,
+ 0 as waitin_num,
+ 0 as kc_num,
+ 0 as syfh_num
+ FROM produce_plan m, produce_plan_data AS d
+ where m.date between '$start_dt' and '$end_dt'
+ and m.id = d.plan_id and m.type = 2
+ and m.status = 1
+ and plan_num <> 0
+ GROUP BY m.date, d.product_id, d.batch_sn
+
+ UNION ALL
+
+ --入库数量
+ SELECT d.product_id,
+ '' batch_sn,
+ m.invoice_dt,
+ 0 as wfhjh_num,
+ 0 as fhjh_num,
+ 0 sale_plan_num,
+ 0 pro_plan_num,
+ 0 pro_bg_num,
+ SUM(d.quantity) rk_num,
+ 0 wf_num,
+ 0 wf_num_ydk,
+ 0 wf_num_wdk,
+ 0 as waitin_num,
+ 0 as kc_num,
+ 0 as syfh_num
+ FROM stock_record10_data AS d
+ LEFT JOIN stock_record10 AS m ON m.id = d.record10_id
+ LEFT JOIN product p on d.product_id = p.id
+ LEFT JOIN product_category pc on pc.id = p.category_id
+ where m.invoice_dt between '$start_dt' and '$end_dt'
+ and left(pc.name,2) <> '外销'
+ GROUP BY m.invoice_dt, d.product_id";
+
+ $dates = date_range($start_dt, $end_dt);
+ $sql_col = [];
+ foreach ($dates as $date) {
+ $_date = str_replace('-', '_', $date);
+ $sql_col[] = "sum(case when invoice_dt='$date' then wfhjh_num else 0 end) as wfhjh_num_$_date";
+ $sql_col[] = "sum(case when invoice_dt='$date' then fhjh_num else 0 end) as fhjh_num_$_date";
+ $sql_col[] = "sum(case when invoice_dt='$date' then sale_plan_num else 0 end) as sale_plan_num_$_date";
+ $sql_col[] = "sum(case when invoice_dt='$date' then pro_plan_num else 0 end) as produce_plan_num_$_date";
+ $sql_col[] = "sum(case when invoice_dt='$date' then pro_bg_num else 0 end) as produce_bg_num_$_date";
+ $sql_col[] = "sum(case when invoice_dt='$date' then rk_num else 0 end) as rk_num_$_date";
+ }
+
+ $sql2[] = "select p.id,
+ pc.id as category_id,
+ pc.code as category_code,
+ pc.name as category_name,
+ p.id as product_id,
+ p.code as product_code,
+ p.name as product_name,
+ p.spec as product_spec,
+ Concat(p.name,' ', isnull(p.spec,'')) as product_name_spec,
+ isnull(batch_sn,'') as batch_sn,
+ pu.name as product_unit,
+ sum(wf_num) dphz_num,
+ sum(wf_num_ydk) ydk_num,
+ sum(wf_num_wdk) wdk_num,
+ sum(kc_num) kc_num,
+ ISNULL(sum(wf_num), 0) - ISNULL(sum(kc_num), 0) as xqzc_num,
+ ISNULL(sum(wf_num_ydk) ,0) as yhk_num,
+ ISNULL(sum(wf_num_ydk), 0) - ISNULL(sum(kc_num), 0) as kfzc_num,
+ ISNULL(sum(wfhjh_num), 0) - ISNULL(sum(kc_num), 0) as kfjh_num,
+ ISNULL(sum(syfh_num), 0) syfh_num,
+ ".join("\n,", $sql_col)."
+ from product as p
+ LEFT JOIN (".$sql.") as temp on temp.product_id = p.id
+ LEFT JOIN product_category pc on pc.id = p.category_id
+ LEFT JOIN product_unit pu on pu.id = p.unit_id
+ where pc.type = 1 and p.status = 1";
+
+ if ($category_code) {
+ $sql2[] = "and pc.code like '$category_code%";
+ }
+
+ if ($ny == 1) {
+ $sql2[] = "and pc.name not like '外销%'";
+ } else if($ny == 2) {
+ $sql2[] = "and pc.name like '外销%'";
+ }
+
+ $sql2[] = "group by pc.id,pc.name,pc.code,p.id,p.code,p.name,isnull(batch_sn,''),p.spec,pu.name
+ order by p.code";
+ $rows = DB::select(join(' ', $sql2));
+ return $rows;
+ }
+}
\ No newline at end of file
diff --git a/app/Gdoo/Produce/config.php b/app/Gdoo/Produce/config.php
new file mode 100644
index 00000000..62d2b739
--- /dev/null
+++ b/app/Gdoo/Produce/config.php
@@ -0,0 +1,74 @@
+ "生产管理",
+ "version" => "1.0",
+ "description" => "原辅料档案。",
+ 'dialogs' => [
+ ],
+ "listens" => [
+ 'produce_plan_data' => 'Gdoo\Produce\Hooks\PlanDataHook',
+ ],
+ "controllers" => [
+ "material" => [
+ "name" => "原辅料档案",
+ "actions" => [
+ "index" => [
+ "name" => "列表"
+ ],
+ "show" => [
+ "name" => "显示"
+ ],
+ "create" => [
+ "name" => "新建"
+ ],
+ "edit" => [
+ "name" => "编辑"
+ ],
+ "delete" => [
+ "name" => "删除"
+ ],
+ "plan" => [
+ "name" => "用料计划"
+ ],
+ ]
+ ],
+ "plan" => [
+ "name" => "生产计划单",
+ "actions" => [
+ "index" => [
+ "name" => "列表",
+ ],
+ "count" => [
+ "name" => "统计",
+ ],
+ "audit" => [
+ "name" => "审核"
+ ],
+ "recall" => [
+ "name" => "撤回"
+ ],
+ "abort" => [
+ "name" => "弃审"
+ ],
+ "show" => [
+ "name" => "显示"
+ ],
+ "create" => [
+ "name" => "新建"
+ ],
+ "edit" => [
+ "name" => "编辑"
+ ],
+ "print" => [
+ "name" => "打印"
+ ],
+ "delete" => [
+ "name" => "删除"
+ ],
+ "planExport" => [
+ "name" => "生产计划导出"
+ ]
+ ]
+ ],
+ ]
+];
diff --git a/app/Gdoo/Produce/views/material/config.blade.php b/app/Gdoo/Produce/views/material/config.blade.php
new file mode 100644
index 00000000..ef217111
--- /dev/null
+++ b/app/Gdoo/Produce/views/material/config.blade.php
@@ -0,0 +1,100 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Produce/views/material/create.blade.php b/app/Gdoo/Produce/views/material/create.blade.php
new file mode 100644
index 00000000..8b3ae47e
--- /dev/null
+++ b/app/Gdoo/Produce/views/material/create.blade.php
@@ -0,0 +1,4 @@
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Produce/views/material/plan.blade.php b/app/Gdoo/Produce/views/material/plan.blade.php
new file mode 100644
index 00000000..9aad063c
--- /dev/null
+++ b/app/Gdoo/Produce/views/material/plan.blade.php
@@ -0,0 +1,183 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Produce/views/material/planProduct.blade.php b/app/Gdoo/Produce/views/material/planProduct.blade.php
new file mode 100644
index 00000000..04793440
--- /dev/null
+++ b/app/Gdoo/Produce/views/material/planProduct.blade.php
@@ -0,0 +1,35 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Produce/views/material/planTotal.blade.php b/app/Gdoo/Produce/views/material/planTotal.blade.php
new file mode 100644
index 00000000..d88dc1a1
--- /dev/null
+++ b/app/Gdoo/Produce/views/material/planTotal.blade.php
@@ -0,0 +1,43 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Produce/views/plan/config.blade.php b/app/Gdoo/Produce/views/plan/config.blade.php
new file mode 100644
index 00000000..6551591d
--- /dev/null
+++ b/app/Gdoo/Produce/views/plan/config.blade.php
@@ -0,0 +1,91 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Produce/views/plan/create.blade.php b/app/Gdoo/Produce/views/plan/create.blade.php
new file mode 100644
index 00000000..d9033700
--- /dev/null
+++ b/app/Gdoo/Produce/views/plan/create.blade.php
@@ -0,0 +1,96 @@
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Produce/views/plan/index.blade.php b/app/Gdoo/Produce/views/plan/index.blade.php
new file mode 100644
index 00000000..c99f5bcd
--- /dev/null
+++ b/app/Gdoo/Produce/views/plan/index.blade.php
@@ -0,0 +1,61 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Produce/views/plan/planExport.blade.php b/app/Gdoo/Produce/views/plan/planExport.blade.php
new file mode 100644
index 00000000..cf6a795d
--- /dev/null
+++ b/app/Gdoo/Produce/views/plan/planExport.blade.php
@@ -0,0 +1,129 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Product/views/category/dialog.blade.php b/app/Gdoo/Product/views/category/dialog.blade.php
new file mode 100644
index 00000000..16474744
--- /dev/null
+++ b/app/Gdoo/Product/views/category/dialog.blade.php
@@ -0,0 +1,135 @@
+
+
+ @include('searchForm6')
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Product/views/category/index.blade.php b/app/Gdoo/Product/views/category/index.blade.php
new file mode 100644
index 00000000..4601deb1
--- /dev/null
+++ b/app/Gdoo/Product/views/category/index.blade.php
@@ -0,0 +1,78 @@
+{{$header["js"]}}
+
+ @include('headers')
+
+
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Product/views/product/create.blade.php b/app/Gdoo/Product/views/product/create.blade.php
new file mode 100644
index 00000000..90bd8375
--- /dev/null
+++ b/app/Gdoo/Product/views/product/create.blade.php
@@ -0,0 +1,12 @@
+
+
+
+
+ {{$form['btn']}}
+
+
+
+ {{$form['tpl']}}
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Product/views/product/dialog.blade.php b/app/Gdoo/Product/views/product/dialog.blade.php
new file mode 100644
index 00000000..cca4bce2
--- /dev/null
+++ b/app/Gdoo/Product/views/product/dialog.blade.php
@@ -0,0 +1,191 @@
+
+
+ @include('searchForm3')
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Product/views/product/index.blade.php b/app/Gdoo/Product/views/product/index.blade.php
new file mode 100644
index 00000000..5ce9d99e
--- /dev/null
+++ b/app/Gdoo/Product/views/product/index.blade.php
@@ -0,0 +1,164 @@
+{{$header["js"]}}
+
+
+
+ @include('headers')
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Product/views/product/serviceCustomer.blade.php b/app/Gdoo/Product/views/product/serviceCustomer.blade.php
new file mode 100644
index 00000000..b549e3fa
--- /dev/null
+++ b/app/Gdoo/Product/views/product/serviceCustomer.blade.php
@@ -0,0 +1,195 @@
+
+
+ @include('searchForm3')
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Product/views/unit/create.blade.php b/app/Gdoo/Product/views/unit/create.blade.php
new file mode 100644
index 00000000..db7f6d80
--- /dev/null
+++ b/app/Gdoo/Product/views/unit/create.blade.php
@@ -0,0 +1,3 @@
+
+ {{$form['tpl']}}
+
\ No newline at end of file
diff --git a/app/Gdoo/Product/views/unit/dialog.blade.php b/app/Gdoo/Product/views/unit/dialog.blade.php
new file mode 100644
index 00000000..cd521f14
--- /dev/null
+++ b/app/Gdoo/Product/views/unit/dialog.blade.php
@@ -0,0 +1,121 @@
+
+
+
+ @include('searchForm')
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Product/views/unit/index.blade.php b/app/Gdoo/Product/views/unit/index.blade.php
new file mode 100644
index 00000000..d6cbfa5f
--- /dev/null
+++ b/app/Gdoo/Product/views/unit/index.blade.php
@@ -0,0 +1,57 @@
+{{$header["js"]}}
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Project/views/project/add.blade.php b/app/Gdoo/Project/views/project/add.blade.php
new file mode 100644
index 00000000..c817fc6a
--- /dev/null
+++ b/app/Gdoo/Project/views/project/add.blade.php
@@ -0,0 +1,48 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Project/views/project/edit.blade.php b/app/Gdoo/Project/views/project/edit.blade.php
new file mode 100644
index 00000000..6e5edbad
--- /dev/null
+++ b/app/Gdoo/Project/views/project/edit.blade.php
@@ -0,0 +1,49 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Project/views/project/index.blade.php b/app/Gdoo/Project/views/project/index.blade.php
new file mode 100644
index 00000000..388146ef
--- /dev/null
+++ b/app/Gdoo/Project/views/project/index.blade.php
@@ -0,0 +1,56 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Project/views/project/query.blade.php b/app/Gdoo/Project/views/project/query.blade.php
new file mode 100644
index 00000000..9d42d19d
--- /dev/null
+++ b/app/Gdoo/Project/views/project/query.blade.php
@@ -0,0 +1,20 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Project/views/project/show.blade.php b/app/Gdoo/Project/views/project/show.blade.php
new file mode 100644
index 00000000..e69de29b
diff --git a/app/Gdoo/Project/views/task/add.blade.php b/app/Gdoo/Project/views/task/add.blade.php
new file mode 100644
index 00000000..35eedd71
--- /dev/null
+++ b/app/Gdoo/Project/views/task/add.blade.php
@@ -0,0 +1,84 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Project/views/task/edit.blade.php b/app/Gdoo/Project/views/task/edit.blade.php
new file mode 100644
index 00000000..45cee2d1
--- /dev/null
+++ b/app/Gdoo/Project/views/task/edit.blade.php
@@ -0,0 +1,234 @@
+
+
+
+
+
+@include('task/index/js')
\ No newline at end of file
diff --git a/app/Gdoo/Project/views/task/index/index.blade.php b/app/Gdoo/Project/views/task/index/index.blade.php
new file mode 100644
index 00000000..d4addb86
--- /dev/null
+++ b/app/Gdoo/Project/views/task/index/index.blade.php
@@ -0,0 +1,150 @@
+
+
+
+
+@include('task/index/js')
\ No newline at end of file
diff --git a/app/Gdoo/Project/views/task/index/js.blade.php b/app/Gdoo/Project/views/task/index/js.blade.php
new file mode 100644
index 00000000..11cddc14
--- /dev/null
+++ b/app/Gdoo/Project/views/task/index/js.blade.php
@@ -0,0 +1,198 @@
+
\ No newline at end of file
diff --git a/app/Gdoo/Project/views/task/item/add.blade.php b/app/Gdoo/Project/views/task/item/add.blade.php
new file mode 100644
index 00000000..376330af
--- /dev/null
+++ b/app/Gdoo/Project/views/task/item/add.blade.php
@@ -0,0 +1,34 @@
+
+
+
+
+
+
+
+
+
名称
+
+
+
+
+
+
+
备注
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Project/views/task/item/edit.blade.php b/app/Gdoo/Project/views/task/item/edit.blade.php
new file mode 100644
index 00000000..ee173067
--- /dev/null
+++ b/app/Gdoo/Project/views/task/item/edit.blade.php
@@ -0,0 +1,42 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Project/views/task/show.blade.php b/app/Gdoo/Project/views/task/show.blade.php
new file mode 100644
index 00000000..0f63f8b0
--- /dev/null
+++ b/app/Gdoo/Project/views/task/show.blade.php
@@ -0,0 +1,106 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Project/views/widget/index.blade.php b/app/Gdoo/Project/views/widget/index.blade.php
new file mode 100644
index 00000000..9a892e4b
--- /dev/null
+++ b/app/Gdoo/Project/views/widget/index.blade.php
@@ -0,0 +1,32 @@
+
+
+
+
标题
+
发布时间
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Project/views/widget/info.blade.php b/app/Gdoo/Project/views/widget/info.blade.php
new file mode 100644
index 00000000..dd73358d
--- /dev/null
+++ b/app/Gdoo/Project/views/widget/info.blade.php
@@ -0,0 +1,15 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Promotion/views/material/index.blade.php b/app/Gdoo/Promotion/views/material/index.blade.php
new file mode 100644
index 00000000..aae1f93e
--- /dev/null
+++ b/app/Gdoo/Promotion/views/material/index.blade.php
@@ -0,0 +1,82 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Promotion/views/material/query.blade.php b/app/Gdoo/Promotion/views/material/query.blade.php
new file mode 100644
index 00000000..5fe28ad3
--- /dev/null
+++ b/app/Gdoo/Promotion/views/material/query.blade.php
@@ -0,0 +1,30 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Promotion/views/material/show.blade.php b/app/Gdoo/Promotion/views/material/show.blade.php
new file mode 100644
index 00000000..4c9732f4
--- /dev/null
+++ b/app/Gdoo/Promotion/views/material/show.blade.php
@@ -0,0 +1,19 @@
+
+
+
{{$row['name']}}
+ 位置: {{$row['location']}}
+
+
+
+
+ @foreach($row['images'] as $image)
+
+
+
+
+
+ @endforeach
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Promotion/views/promotion/create.blade.php b/app/Gdoo/Promotion/views/promotion/create.blade.php
new file mode 100644
index 00000000..4b2502e3
--- /dev/null
+++ b/app/Gdoo/Promotion/views/promotion/create.blade.php
@@ -0,0 +1,181 @@
+@if(is_weixin())
+
+
+@endif
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Promotion/views/promotion/dialog.blade.php b/app/Gdoo/Promotion/views/promotion/dialog.blade.php
new file mode 100644
index 00000000..8414f27d
--- /dev/null
+++ b/app/Gdoo/Promotion/views/promotion/dialog.blade.php
@@ -0,0 +1,187 @@
+
+
+
+
+
+ @include('searchForm3')
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Promotion/views/promotion/index.blade.php b/app/Gdoo/Promotion/views/promotion/index.blade.php
new file mode 100644
index 00000000..a0d33cea
--- /dev/null
+++ b/app/Gdoo/Promotion/views/promotion/index.blade.php
@@ -0,0 +1,82 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Promotion/views/promotion/print.blade.php b/app/Gdoo/Promotion/views/promotion/print.blade.php
new file mode 100644
index 00000000..ca2d71da
--- /dev/null
+++ b/app/Gdoo/Promotion/views/promotion/print.blade.php
@@ -0,0 +1 @@
+{{$form['tpl']}}
\ No newline at end of file
diff --git a/app/Gdoo/Promotion/views/promotion/product.blade.php b/app/Gdoo/Promotion/views/promotion/product.blade.php
new file mode 100644
index 00000000..abffa7d7
--- /dev/null
+++ b/app/Gdoo/Promotion/views/promotion/product.blade.php
@@ -0,0 +1,33 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Promotion/views/promotion/serviceSaleOrder.blade.php b/app/Gdoo/Promotion/views/promotion/serviceSaleOrder.blade.php
new file mode 100644
index 00000000..812deac8
--- /dev/null
+++ b/app/Gdoo/Promotion/views/promotion/serviceSaleOrder.blade.php
@@ -0,0 +1,122 @@
+
+
+
+
+
+ @include('searchForm3')
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Promotion/views/review/create.blade.php b/app/Gdoo/Promotion/views/review/create.blade.php
new file mode 100644
index 00000000..a468582f
--- /dev/null
+++ b/app/Gdoo/Promotion/views/review/create.blade.php
@@ -0,0 +1,141 @@
+
+
+
+
+ {{$form['btn']}}
+
+
+
+ {{$form['tpl']}}
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Promotion/views/review/feeDetail.blade.php b/app/Gdoo/Promotion/views/review/feeDetail.blade.php
new file mode 100644
index 00000000..c977add1
--- /dev/null
+++ b/app/Gdoo/Promotion/views/review/feeDetail.blade.php
@@ -0,0 +1,34 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Promotion/views/review/index.blade.php b/app/Gdoo/Promotion/views/review/index.blade.php
new file mode 100644
index 00000000..4745fd80
--- /dev/null
+++ b/app/Gdoo/Promotion/views/review/index.blade.php
@@ -0,0 +1,59 @@
+{{$header["js"]}}
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Purchase/views/order/index.blade.php b/app/Gdoo/Purchase/views/order/index.blade.php
new file mode 100644
index 00000000..c99f5bcd
--- /dev/null
+++ b/app/Gdoo/Purchase/views/order/index.blade.php
@@ -0,0 +1,61 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Purchase/views/order/serviceRecord01.blade.php b/app/Gdoo/Purchase/views/order/serviceRecord01.blade.php
new file mode 100644
index 00000000..3a9b0a53
--- /dev/null
+++ b/app/Gdoo/Purchase/views/order/serviceRecord01.blade.php
@@ -0,0 +1,124 @@
+
+
+
+
+
+ @include('searchForm3')
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Purchase/views/supplier/create.blade.php b/app/Gdoo/Purchase/views/supplier/create.blade.php
new file mode 100644
index 00000000..3598aea0
--- /dev/null
+++ b/app/Gdoo/Purchase/views/supplier/create.blade.php
@@ -0,0 +1,3 @@
+
+ {{$form['tpl']}}
+
\ No newline at end of file
diff --git a/app/Gdoo/Purchase/views/supplier/dialog.blade.php b/app/Gdoo/Purchase/views/supplier/dialog.blade.php
new file mode 100644
index 00000000..23106ddf
--- /dev/null
+++ b/app/Gdoo/Purchase/views/supplier/dialog.blade.php
@@ -0,0 +1,131 @@
+
+
+ @include('searchForm3')
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Purchase/views/supplier/index.blade.php b/app/Gdoo/Purchase/views/supplier/index.blade.php
new file mode 100644
index 00000000..00a0fa14
--- /dev/null
+++ b/app/Gdoo/Purchase/views/supplier/index.blade.php
@@ -0,0 +1,61 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/StiReport/Controllers/StiReportController.php b/app/Gdoo/StiReport/Controllers/StiReportController.php
new file mode 100644
index 00000000..1a4799a2
--- /dev/null
+++ b/app/Gdoo/StiReport/Controllers/StiReportController.php
@@ -0,0 +1,50 @@
+where('id', $template_id)->first();
+ // 报表名称
+ $report_name = "{$template['code']}";
+ $report_file = '';
+ if (is_file(public_path().'/reports/'.$report_name.'.mrt')) {
+ $report_file = $report_name;
+ }
+ return $this->render(['report_name' => $report_name, 'report_file' => $report_file]);
+ }
+
+ public function viewerAction()
+ {
+ return $this->render();
+ }
+
+ public function renderAction()
+ {
+ return $this->render();
+ }
+
+ public function saveReportAction()
+ {
+ $gets = Request::all();
+ $fileName = $gets['fileName'];
+ file_put_contents(public_path().'/reports/'.$fileName.".mrt", $gets['data']);
+ $success = ['success' => true, 'msg' => "保存成功:".$fileName];
+ return $success;
+ }
+
+ public function licenseAction()
+ {
+ echo "6vJhGtLLLz2GNviWmUTrhSqnOItdDwjBylQzQcAOiHlrzAZzmWmSnQQ4gKFiZ4LJpJv//QjFVXxcHAVbzZfXjyOGPmj/m+BEjr2Z14dWeqLFNGF74GELbTTKs2+Le/9cDIWdGNnOpEK2aGdYllauMPLQsiScC521JIEYSdOspiRHSLcegksxfNedJjyIjGlfI2YrddBRWGiO+uWOHE5oz9hLG8VPBSRo60KmgkscM5X+7+aQ+6vzKKOC2XB+e6BMQC5qNVBUblfGQR2EjNLZKmSJtvek7IbG/OK+XP0j2bwicyJUGC0pyLHqctr3BpcO/gA5LoVfuwqYG3klL//owBkObPPhJV1HD6XsHL0GDryssJFaDCQIyXMrOn7hNQNkEIyx+AJDNgf5XfxPgEgFsRhYCPYq7ccutg2by8duOxbF3xH0gL/uAQN275COXJBV3W62DSLM+o8azChG+Z7y0dF9f4whZ/SKD4DwNPUWK7osEPVwl5BY+0lkdqd67fatlrlc0QU/ZX9f5QcTKfl5ljuNc+kcqxmd9NND6Xzrw9gFsFqIWqqVo++DdoAZFStXMkOp/nTNBQMRA100k3vi2SbbiHq/gVimrQecUhWG0qU5zcemtVGDMs1ruXsoHX8pYX/rMJHH09qCWllVyBykkTLourYEig9g5fhKDYRV05aC0cWsbxR2nj9TH3SLmG4P2Px7uJsq6iOsnIHWuBMwk8oF7xPEugjw+x8lkjVVoV8WWBSdjIxGh4LviZXBEJm9FTJzYcnEHMZRh0uVE1g8crC+TfRVii7dcdZzeQklzyNY+0Q1/hRaIUs+mNPRiqG6YqEv3f+yG4ncxzkCWZDvXPox87y61jbg6Dg73X1RAwwvbIXuJVANbaDOefUELPmpz4SIpHx8zpLSmn1H1u0PolbsimLigcGw2bJQeuU++OBU74vJJde3JdoO6IOfmUJkoxprdszyknLm+zWgnC+jjaCtEZZuOIJqyuVPoqHRiFkqNjbddkvGMmj/4+2D6BdYQot9sEOW7iCgV4SvZ/efC0NlRX+Z+6PODwKJiO+Sen5aAlsJcL2jIUSAjgyS+7im7XTGlYKuRL59EQjA5HArO1ikJ0P/2pk4u91z2J8GRvTPu5BZUI9M0BLGLAVCFMte4JQCOr+f785RgjerSNCSgN4Mfa5+jDQAKTAVAO5tqT/SBEm0M5U1EylQ/fbseKt+dQ1/VzqlQ9SH14jtI0J97ACqk9SBt9xpTgBnJrBSTnnY21l2zWS7/2k5U9LPDJn0Lm32ueoDRFaM4JeK1HoSi2HvOYy1V1hU5pCe893QsBE/HOVp4UWu9lfiEWunHEEdPZOUPgc131KwJrM4K3DYiBbXl442TgbNLfz5IBnAw1NVabMXXyx2LOi6x35xw1YLMRYNWYE9QpocBhoFQtStd2OUZ5CqvxhXf+VaLK3hmm1GvlqpUK6LIDd3eyuQK4f0E7+zVSBaV6eSDI9YJC42Ee+Br8AByGYLRaFISpDculGt2nqwFL6cwltv1Xy11frJR2KqbR8sd6dI0V69XnwBziRzJq1SyAZd9bzClYSpA3ZYPN9ghdaHA+GZak0IYMokWLi6oYquOCRoy8f0sEQM2Uhw2x/E9tgyNoLZhDhrk805/VCsThI5fHn0YWVnmQZTrGkOwnoqLw3VHb7akUmNnjMlk/tD59bR2lgD+fnNuNsBYDDjJpg+fKmgf9araTPEIpuuanp53e6xodRYKIj4o4+39DrPK10eR4CDfSh5UShvnCZz+V0FAkIkoM92U1JTU59P4M4pzc8PswmS1rGTRaZMUrTYrjeGCHC9Hl0CTIR1/rQAx8iIcC3yVNCeiTJAmKMCl830O4GpEfduNHQgDrlsJC4q6RA7J2kUzW2WQvKFKH3bRH1hOc6LZK4DmwMGzXMKDKOxK0dzld2/ImRN6DbPacV/4d0HK06qBOFEgUJqXhMpV1JjsXVvmx/m2LCRgkD5vPEwcuiWtWde7tISLCEg6hjAV9+Hx6zOWpozg7aZMtikT+43uWakRkU/H+ITIGhqxuQhkZkmIddWrjD5lJtdUOSa0FWu969EDp4XB8dmUKSwyrkgOHZu6DutFW5ArtqhNejthWt/sV1FkSbvdd26zn1fSO4pDa4pDmcSo+l/4DChZbEyICc7IQrPjVuRUlVGuAVksZTBX+VYIip8LsJSFLHo7Dnn4QT3qDNIh8aAcY3fnHhph4G5ekbvGOw3+m1qqs8t0m89vdK7k8nJTw==";
+ }
+}
diff --git a/app/Gdoo/StiReport/Services/StiReportService.php b/app/Gdoo/StiReport/Services/StiReportService.php
new file mode 100644
index 00000000..dc3a4fda
--- /dev/null
+++ b/app/Gdoo/StiReport/Services/StiReportService.php
@@ -0,0 +1,8 @@
+
+
+
+
+ Report Designer - {{$setting['title']}}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/app/Gdoo/StiReport/views/stiReport/render.blade.php b/app/Gdoo/StiReport/views/stiReport/render.blade.php
new file mode 100644
index 00000000..cd57a29c
--- /dev/null
+++ b/app/Gdoo/StiReport/views/stiReport/render.blade.php
@@ -0,0 +1,35 @@
+
+
+
+
+ 打印预览
+
+
+
+
+
+
+
+
+
diff --git a/app/Gdoo/StiReport/views/stiReport/viewer.blade.php b/app/Gdoo/StiReport/views/stiReport/viewer.blade.php
new file mode 100644
index 00000000..24998982
--- /dev/null
+++ b/app/Gdoo/StiReport/views/stiReport/viewer.blade.php
@@ -0,0 +1,66 @@
+
+
+
+
+ Report Viewer - {{$setting['title']}}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/app/Gdoo/Stock/Controllers/AllocationController.php b/app/Gdoo/Stock/Controllers/AllocationController.php
new file mode 100644
index 00000000..632d926d
--- /dev/null
+++ b/app/Gdoo/Stock/Controllers/AllocationController.php
@@ -0,0 +1,220 @@
+ 'stock_allocation',
+ 'referer' => 1,
+ 'search' => ['by' => ''],
+ ]);
+
+ $cols = $header['cols'];
+
+ $cols['actions']['options'] = [[
+ 'name' => '显示',
+ 'action' => 'show',
+ 'display' => $this->access['show'],
+ ]];
+
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table'])->setBy($header);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+ $model->orderBy($header['sort'], $header['order']);
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ $model->select($header['select']);
+ $rows = $model->paginate($query['limit'])->appends($query);
+ $items = Grid::dataFilters($rows, $header);
+ return $items->toJson();
+ }
+
+ $header['buttons'] = [
+ //['name' => '删除', 'icon' => 'fa-remove', 'action' => 'delete', 'display' => $this->access['delete']],
+ ['name' => '导出', 'icon' => 'fa-share', 'action' => 'export', 'display' => 1],
+ ];
+
+ $header['cols'] = $cols;
+ $header['tabs'] = Allocation::$tabs;
+ $header['bys'] = Allocation::$bys;
+ $header['js'] = Grid::js($header);
+
+ return $this->display([
+ 'header' => $header,
+ ]);
+ }
+
+ // 新建
+ public function createAction($action = 'edit')
+ {
+ $id = (int) Request::get('id');
+ $header['action'] = $action;
+ $header['code'] = 'stock_allocation';
+ $header['id'] = $id;
+
+ $form = Form::make($header);
+ $tpl = $action == 'print' ? 'print' : 'create';
+ return $this->display([
+ 'form' => $form,
+ ], $tpl);
+ }
+
+ // 编辑
+ public function editAction()
+ {
+ return $this->createAction();
+ }
+
+ // 审核
+ public function auditAction()
+ {
+ return $this->createAction('audit');
+ }
+
+ // 显示
+ public function showAction()
+ {
+ return $this->createAction('show');
+ }
+
+ // 打印
+ public function printAction()
+ {
+ $id = Request::get('id');
+ $template_id = Request::get('template_id');
+
+ $this->layout = 'layouts.print3';
+ $master = DB::table('stock_allocation as m')
+ ->where('m.id', $id)
+ ->leftJoin('warehouse as wo', 'wo.id', '=', 'm.out_warehouse_id')
+ ->leftJoin('warehouse as wi', 'wi.id', '=', 'm.in_warehouse_id')
+ ->selectRaw('m.*, wi.name as in_warehouse_name, wo.name as out_warehouse_name, wi.code as in_warehouse_code, wo.code as out_warehouse_code')
+ ->first();
+
+ $rows = DB::table('stock_allocation_data as d')
+ ->leftJoin('stock_allocation as m', 'm.id', '=', 'd.allocation_id')
+ ->leftJoin('product as p', 'p.id', '=', 'd.product_id')
+ ->leftJoin('product_unit as pu', 'pu.id', '=', 'p.unit_id')
+ ->where('m.id', $id)
+ ->selectRaw('
+ d.*,
+ p.name as product_name,
+ p.code as product_code,
+ p.spec as product_spec,
+ pu.name as product_unit
+ ')
+ ->orderBy('p.code', 'asc')
+ ->get();
+
+ $form = [
+ 'template' => DB::table('model_template')->where('id', $template_id)->first()
+ ];
+
+ if ($master['in_warehouse_code'] == 27 || $master['in_warehouse_code'] == 21) {
+ $warehouse_by = '李志全';
+ } else {
+ $warehouse_by = '万海英';
+ }
+
+ $tpl = $this->display([
+ 'master' => $master,
+ 'rows' => $rows,
+ 'form' => $form,
+ 'warehouse_by' => $warehouse_by,
+ ], 'print/'.$template_id);
+ return $tpl;
+ }
+
+ // 选择库存
+ public function stockSelectAction()
+ {
+ $search = search_form(['advanced' => ''], [
+ ['form_type' => 'text', 'name' => '产品名称', 'field' => 'name'],
+ ['form_type' => 'text', 'name' => '产品编码', 'field' => 'code']
+ ], 'model');
+ $query = $search['query'];
+ if (Request::method() == 'POST') {
+ $fields = [];
+ foreach($search['forms']['field'] as $i => $field) {
+ $fields[$field] = $search['forms']['search'][$i];
+ }
+ if($fields['name']) {
+ $query['value'] = $fields['name'];
+ }
+ if($fields['code']) {
+ $query['value'] = $fields['code'];
+ }
+
+ $rows = StockService::getBatchSelectZY($query['warehouse_id'], '', $query['value']);
+ return ['data' => $rows];
+ }
+ return $this->render([
+ 'search' => $search,
+ ]);
+ }
+
+ // 物流信息
+ public function logisticsAction()
+ {
+ if (Request::method() == 'POST') {
+ $gets = Request::get('stock_allocation');
+ $id = $gets['id'];
+ $gets['freight_created_dt'] = date('Y-m-d H:i:s');
+ $gets['freight_created_by'] = auth()->user()->name;
+ DB::table('stock_allocation')->where('id', $id)->update($gets);
+ return $this->json('物流信息提交成功。', true);
+ }
+ $file = base_path().'/addons/'.ucfirst(Request::module()).'/views/'.Request::controller().'/'.Request::action().'.html';
+ $id = Request::get('id');
+ $row = Allocation::find($id);
+ $freight_quantity = floatval($row['freight_quantity']);
+
+ if ($freight_quantity == 0) {
+ $count = DB::table('stock_allocation_data')
+ ->where('allocation_id', $id)->selectRaw('sum(total_weight) as weight, sum(quantity) as quantity')->first();
+ $weight = number_format($count['weight'] / 1000, 2);
+ $quantity = number_format($count['quantity'], 2);
+ $row['freight_quantity'] = $quantity;
+ $row['freight_weight'] = $weight;
+ }
+ $form = Form::make1(['table' => 'stock_allocation', 'file' => $file, 'row' => $row]);
+ return $form;
+ }
+
+ // 删除
+ public function deleteAction()
+ {
+ if (Request::method() == 'POST') {
+ $ids = Request::get('id');
+ return Form::remove(['code' => 'stock_allocation', 'ids' => $ids]);
+ }
+ }
+}
diff --git a/app/Gdoo/Stock/Controllers/CancelController.php b/app/Gdoo/Stock/Controllers/CancelController.php
new file mode 100644
index 00000000..5e65ab8e
--- /dev/null
+++ b/app/Gdoo/Stock/Controllers/CancelController.php
@@ -0,0 +1,185 @@
+ 'stock_cancel',
+ 'referer' => 1,
+ 'search' => ['by' => ''],
+ ]);
+
+ $cols = $header['cols'];
+
+ $cols['actions']['options'] = [[
+ 'name' => '显示',
+ 'action' => 'show',
+ 'display' => $this->access['show'],
+ ]];
+
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table'])->setBy($header);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+ $model->orderBy($header['sort'], $header['order']);
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ $model->select($header['select']);
+ $rows = $model->paginate($query['limit'])->appends($query);
+ $items = Grid::dataFilters($rows, $header);
+ return $items->toJson();
+ }
+
+ $header['buttons'] = [
+ //['name' => '删除', 'icon' => 'fa-remove', 'action' => 'delete', 'display' => $this->access['delete']],
+ ['name' => '导出', 'icon' => 'fa-share', 'action' => 'export', 'display' => 1],
+ ];
+
+ $header['left_buttons'] = [
+ ['name' => '批量编辑', 'color' => 'default', 'icon' => 'fa-pencil-square-o', 'action' => 'batchEdit', 'display' => $this->access['batchEdit']],
+ ];
+
+ $header['cols'] = $cols;
+ $header['tabs'] = Cancel::$tabs;
+ $header['bys'] = Cancel::$bys;
+ $header['js'] = Grid::js($header);
+
+ return $this->display([
+ 'header' => $header,
+ ]);
+ }
+
+ // 新建
+ public function createAction($action = 'edit')
+ {
+ $id = (int) Request::get('id');
+ $header['action'] = $action;
+ $header['code'] = 'stock_cancel';
+ $header['id'] = $id;
+
+ $form = Form::make($header);
+ $tpl = $action == 'print' ? 'print' : 'create';
+ return $this->display([
+ 'form' => $form,
+ ], $tpl);
+ }
+
+ // 编辑
+ public function editAction()
+ {
+ return $this->createAction();
+ }
+
+ // 审核
+ public function auditAction()
+ {
+ return $this->createAction('audit');
+ }
+
+ // 显示
+ public function showAction()
+ {
+ return $this->createAction('show');
+ }
+
+ // 打印
+ public function printAction()
+ {
+ $id = Request::get('id');
+ $template_id = Request::get('template_id');
+
+ $this->layout = 'layouts.print3';
+ $master = DB::table('stock_cancel as sd')
+ ->leftJoin('customer as c', 'c.id', '=', 'sd.customer_id')
+ ->leftJoin('customer_tax as ct', 'ct.id', '=', 'sd.tax_id')
+ ->leftJoin('sale_type as st', 'st.id', '=', 'sd.type_id')
+ ->selectRaw('sd.*, ct.name as tax_name, c.name as customer_name, st.name as type_name')
+ ->where('sd.id', $id)
+ ->first();
+
+ $model = DB::table('stock_cancel_data as sdd')
+ ->leftJoin('stock_cancel as sd', 'sd.id', '=', 'sdd.cancel_id')
+ ->leftJoin('product as p', 'p.id', '=', 'sdd.product_id')
+ ->leftJoin('product_unit as pu', 'pu.id', '=', 'p.unit_id')
+ ->leftJoin('customer_order_type as cot', 'cot.id', '=', 'sdd.type_id')
+ ->leftJoin('warehouse as w', 'w.id', '=', 'sdd.warehouse_id')
+ ->where('sdd.cancel_id', $id);
+
+ $rows = $model->selectRaw("
+ sdd.*,
+ p.name as product_name,
+ p.spec as product_spec,
+ cot.name as type_name,
+ pu.name as product_unit,
+ p.material_type,
+ p.product_type
+ ")
+ ->orderBy('p.code', 'asc')
+ ->get();
+
+ $form = [
+ 'template' => DB::table('model_template')->where('id', $template_id)->first()
+ ];
+
+ return $this->display([
+ 'master' => $master,
+ 'rows' => $rows,
+ 'form' => $form,
+ ], 'print/'.$template_id);
+ }
+
+ // 批量编辑
+ public function batchEditAction()
+ {
+ $gets = Request::all();
+ if (Request::method() == 'POST') {
+ $ids = explode(',', $gets['ids']);
+ DB::table('stock_cancel')->whereIn('id', $ids)->update([
+ $gets['field'] => $gets['search_0'],
+ ]);
+ return $this->json('修改完成。', true);
+ }
+ $header = Grid::batchEdit([
+ 'code' => 'stock_cancel',
+ 'columns' => ['customer_id', 'tax_id'],
+ ]);
+ return view('batchEdit', [
+ 'gets' => $gets,
+ 'header' => $header
+ ]);
+ }
+
+ // 删除
+ public function deleteAction()
+ {
+ if (Request::method() == 'POST') {
+ $ids = Request::get('id');
+ return Form::remove(['code' => 'stock_cancel', 'ids' => $ids]);
+ }
+ }
+}
diff --git a/app/Gdoo/Stock/Controllers/CategoryController.php b/app/Gdoo/Stock/Controllers/CategoryController.php
new file mode 100644
index 00000000..3453cab9
--- /dev/null
+++ b/app/Gdoo/Stock/Controllers/CategoryController.php
@@ -0,0 +1,134 @@
+ 'stock_type',
+ 'referer' => 1,
+ 'search' => ['by' => ''],
+ ]);
+
+ $cols = $header['cols'];
+
+ $cols['actions']['options'] = [[
+ 'name' => '编辑',
+ 'action' => 'edit',
+ 'display' => $this->access['edit'],
+ ]];
+
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table'])->setBy($header);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+ $model->orderBy($header['sort'], $header['order'])
+ ->orderBy('id', 'desc');
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ $model->select($header['select']);
+ $rows = $model->paginate($query['limit'])->appends($query);
+ $items = Grid::dataFilters($rows, $header);
+ return $items->toJson();
+ }
+
+ $header['buttons'] = [
+ ['name' => '删除', 'icon' => 'fa-remove', 'action' => 'delete', 'display' => $this->access['delete']],
+ ['name' => '导出', 'icon' => 'fa-share', 'action' => 'export', 'display' => 1],
+ ];
+ $header['cols'] = $cols;
+ $header['tabs'] = StockCategory::$tabs;
+ $header['bys'] = StockCategory::$bys;
+ $header['js'] = Grid::js($header);
+
+ return $this->display([
+ 'header' => $header,
+ ]);
+ }
+
+ // 新建客户联系人
+ public function createAction()
+ {
+ $id = (int)Request::get('id');
+ $form = Form::make(['code' => 'stock_type', 'id' => $id]);
+ return $this->render([
+ 'form' => $form,
+ ], 'create');
+ }
+
+ // 创建客户联系人
+ public function editAction()
+ {
+ return $this->createAction();
+ }
+
+ /**
+ * 弹出层信息
+ */
+ public function dialogAction()
+ {
+ $header = Grid::header([
+ 'code' => 'stock_type',
+ ]);
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table']);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+ $model->where('stock_type.status', 1)
+ ->orderBy('stock_type.sort', 'asc');
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ $model->select($header['select']);
+
+ $rows = $model->paginate($query['limit']);
+ $items = Grid::dataFilters($rows, $header, function($item) {
+ return $item;
+ });
+ return response()->json($items);
+ }
+
+ return $this->render([
+ 'search' => $search,
+ ], 'dialog');
+ }
+
+ // 删除
+ public function deleteAction()
+ {
+ if (Request::method() == 'POST') {
+ $ids = Request::get('id');
+ return Form::remove(['code' => 'stock_type', 'ids' => $ids]);
+ }
+ }
+}
\ No newline at end of file
diff --git a/app/Gdoo/Stock/Controllers/DeliveryController.php b/app/Gdoo/Stock/Controllers/DeliveryController.php
new file mode 100644
index 00000000..e193d788
--- /dev/null
+++ b/app/Gdoo/Stock/Controllers/DeliveryController.php
@@ -0,0 +1,470 @@
+ 'stock_delivery',
+ 'referer' => 1,
+ 'search' => ['by' => ''],
+ ]);
+
+ $cols = $header['cols'];
+
+ $cols['actions']['options'] = [[
+ 'name' => '显示',
+ 'action' => 'show',
+ 'display' => $this->access['show'],
+ ]];
+
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table'])->setBy($header);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+ $model->orderBy($header['sort'], $header['order']);
+
+ // 过滤库管角色 辅料:30, 成品:31
+ if (auth()->user()->role_id == 30) {
+ $model->where('stock_delivery.type_id', 2);
+ }
+ if (auth()->user()->role_id == 31) {
+ $model->where('stock_delivery.type_id', '<>', 2);
+ }
+
+ // 发货统计
+ $model->leftJoin(DB::raw('(select SUM(ISNULL(d.quantity, 0)) total_quantity, d.delivery_id
+ FROM stock_delivery_data as d
+ GROUP BY d.delivery_id
+ ) sdd
+ '), 'stock_delivery.id', '=', 'sdd.delivery_id');
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ // 客户权限
+ $region = regionCustomer('customer_id_customer');
+ if ($region['authorise']) {
+ foreach ($region['whereIn'] as $key => $where) {
+ $model->whereIn($key, $where);
+ }
+ }
+
+ $model->select($header['select']);
+ $model->addSelect(DB::raw('sdd.total_quantity'));
+
+ $rows = $model->paginate($query['limit'])->appends($query);
+ $items = Grid::dataFilters($rows, $header);
+ return $items->toJson();
+ }
+
+ $header['buttons'] = [
+ ['name' => '导出', 'icon' => 'fa-share', 'action' => 'export', 'display' => 1],
+ ];
+
+ $header['left_buttons'] = [
+ ['name' => '批量编辑', 'color' => 'default', 'icon' => 'fa-pencil-square-o', 'action' => 'batchEdit', 'display' => $this->access['batchEdit']],
+ ];
+
+ $header['cols'] = $cols;
+ $header['tabs'] = Delivery::$tabs;
+ $header['bys'] = Delivery::$bys;
+ $header['js'] = Grid::js($header);
+
+ return $this->display([
+ 'header' => $header,
+ ]);
+ }
+
+ // 明细列表
+ public function detailAction()
+ {
+ $header = Grid::header([
+ 'code' => 'stock_delivery',
+ 'referer' => 1,
+ 'template_id' => 71,
+ 'search' => ['by' => ''],
+ ]);
+
+ $cols = $header['cols'];
+
+ $cols['actions']['options'] = [[
+ 'name' => '显示',
+ 'action' => 'show',
+ 'display' => $this->access['show'],
+ ]];
+
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table'])->setBy($header);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+ $model->orderBy($header['sort'], $header['order']);
+
+ // 过滤库管角色 辅料:30, 成品:31
+ if (auth()->user()->role_id == 30) {
+ $model->where('stock_delivery.type_id', 2);
+ }
+ if (auth()->user()->role_id == 31) {
+ $model->where('stock_delivery.type_id', '<>', 2);
+ }
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ // 客户权限
+ $region = regionCustomer('customer_id_customer');
+ if ($region['authorise']) {
+ foreach ($region['whereIn'] as $key => $where) {
+ $model->whereIn($key, $where);
+ }
+ }
+
+ $model->select($header['select']);
+ $rows = $model->paginate($query['limit'])->appends($query);
+ $items = Grid::dataFilters($rows, $header);
+ return $items->toJson();
+ }
+
+ $header['buttons'] = [
+ ['name' => '导出', 'icon' => 'fa-share', 'action' => 'export', 'display' => 1],
+ ];
+
+ $header['cols'] = $cols;
+ $header['tabs'] = Delivery::$tabs2;
+ $header['bys'] = Delivery::$bys;
+ $header['js'] = Grid::js($header);
+
+ return $this->display([
+ 'header' => $header,
+ ]);
+ }
+
+ // 自动保存
+ public function autoSaveAction()
+ {
+ $gets = Request::all();
+ $master = $gets['master'];
+ $keys = AES::decrypt($master['key'], config('app.key'));
+ list($bill_id, $id) = explode('.', $keys);
+ $bill = Bill::find($bill_id);
+
+ // 发货日期为空
+ if (empty($gets['stock_delivery']['invoice_dt'])) {
+ $gets['stock_delivery']['invoice_dt'] = date('Y-m-d');
+ }
+
+ $models = ModelService::getModels($bill->model_id);
+ if (Request::method() == 'POST') {
+ $rows = $gets['stock_delivery_data']['rows'];
+ $product_ids = [];
+ foreach($rows as $row) {
+ $product_ids[$row['product_id']] = $row['product_id'];
+ }
+
+ // 获取产品列表
+ $vars2 = DB::table('product')->whereIn('id', $product_ids)->get()->keyBy('id');
+ $materiels = $products = [];
+ foreach($rows as $row) {
+ $product = $vars2[$row['product_id']];
+ if ($product['material_type'] > 0) {
+ $materiels[] = $row;
+ } else {
+ $products[] = $row;
+ }
+ }
+
+ $print_ids = [];
+ if (count($products) > 0) {
+ $gets['stock_delivery']['type_id'] = 1;
+ $gets['stock_delivery_data']['rows'] = $products;
+ $id = Form::store($bill, $models, $gets, 0);
+ $print_ids[] = $id;
+ }
+
+ if (count($materiels) > 0) {
+ $gets['stock_delivery']['type_id'] = 2;
+ $gets['stock_delivery_data']['rows'] = $materiels;
+ $id = Form::store($bill, $models, $gets, 0);
+ $print_ids[] = $id;
+ }
+
+ foreach($print_ids as $print_id) {
+ DB::table('stock_delivery')->where('id', $print_id)->update(['print_master_id' => $print_ids[0]]);
+ }
+
+ // 自动保存发货单返回数据
+ $url = url($master['uri'].'/show', ['id' => $id, 'client' => $master['client']]);
+ return $this->json($bill['name'].'保存成功', $url);
+ }
+ return $this->createAction('audit');
+ }
+
+ // 新建
+ public function createAction($action = 'edit')
+ {
+ $id = (int) Request::get('id');
+ $header['action'] = $action;
+ $header['code'] = 'stock_delivery';
+ $header['id'] = $id;
+
+ // 客户权限
+ $header['region'] = ['field' => 'customer_id'];
+ $header['authorise'] = ['action' => 'index', 'field' => 'created_id'];
+
+ $header['select'] = '
+ product_id_product.weight,
+ product_id_product.weight * stock_delivery_data.quantity as total_weight
+ ';
+
+ $form = Form::make($header);
+ $tpl = $action == 'print' ? 'print' : 'create';
+ return $this->display([
+ 'form' => $form,
+ ], $tpl);
+ }
+
+ // 编辑
+ public function editAction()
+ {
+ return $this->createAction();
+ }
+
+ // 审核
+ public function auditAction()
+ {
+ return $this->createAction('audit');
+ }
+
+ // 显示
+ public function showAction()
+ {
+ return $this->createAction('show');
+ }
+
+ // 批量编辑
+ public function batchEditAction()
+ {
+ $gets = Request::all();
+ if (Request::method() == 'POST') {
+ $ids = explode(',', $gets['ids']);
+ DB::table('stock_delivery')->whereIn('id', $ids)->update([
+ $gets['field'] => $gets['search_0'],
+ ]);
+ return $this->json('修改完成。', true);
+ }
+ $header = Grid::batchEdit([
+ 'code' => 'stock_delivery',
+ 'columns' => ['customer_id', 'tax_id'],
+ ]);
+ return view('batchEdit', [
+ 'gets' => $gets,
+ 'header' => $header
+ ]);
+ }
+
+ // 打印
+ public function printAction()
+ {
+ $id = Request::get('id');
+ $template_id = Request::get('template_id');
+
+ $master = DB::table('stock_delivery as sd')
+ ->leftJoin('customer as c', 'c.id', '=', 'sd.customer_id')
+ ->leftJoin('customer_tax as ct', 'ct.id', '=', 'sd.tax_id')
+ ->leftJoin('sale_type as st', 'st.id', '=', 'sd.type_id')
+ ->selectRaw('sd.*, ct.name as tax_name, c.name as customer_name, st.name as type_name')
+ ->where('sd.id', $id)
+ ->first();
+
+ $model = DB::table('stock_delivery_data as sdd')
+ ->leftJoin('stock_delivery as sd', 'sd.id', '=', 'sdd.delivery_id')
+ ->leftJoin('product as p', 'p.id', '=', 'sdd.product_id')
+ ->leftJoin('product_unit as pu', 'pu.id', '=', 'p.unit_id')
+ ->leftJoin('customer_order_type as cot', 'cot.id', '=', 'sdd.type_id')
+ ->leftJoin('warehouse as w', 'w.id', '=', 'sdd.warehouse_id');
+
+ if ($template_id == 112) {
+ $model->where('sd.print_master_id', $master['print_master_id']);
+ } else {
+ $model->where('sdd.delivery_id', $id);
+ }
+
+ $model->whereRaw("p.code <> '99001'");
+
+ $rows = $model->selectRaw("
+ sdd.*,
+ p.name as product_name,
+ p.spec as product_spec,
+ cot.name as type_name,
+ pu.name as product_unit,
+ p.material_type,
+ p.product_type,
+ SUBSTRING(batch_sn, 3, 4) as batch_sn,
+ case when right(w.name, 4) = '不满件库' then 'B' else '' end warehouse_type
+ ")
+ ->orderBy('p.code', 'asc')
+ ->get();
+
+ $money = DB::table('stock_delivery_data as sdd')
+ ->leftJoin('product as p', 'p.id', '=', 'sdd.product_id')
+ ->where('sdd.delivery_id', $id)
+ ->whereRaw("p.code = '99001'")
+ ->sum("money");
+
+ $form = [
+ 'template' => DB::table('model_template')->where('id', $template_id)->first()
+ ];
+
+ if ($template_id == 87) {
+ $this->layout = 'layouts.print_stiReport';
+ return $this->display([
+ 'master' => $master,
+ 'money' => $money,
+ 'rows' => $rows,
+ 'form' => $form,
+ ], 'print/'.$template_id);
+ } else {
+ $this->layout = 'layouts.print2';
+ print_prince($this->display([
+ 'master' => $master,
+ 'money' => $money,
+ 'rows' => $rows,
+ 'form' => $form,
+ ], 'print/'.$template_id));
+ }
+ }
+
+ // 物流信息
+ public function logisticsAction()
+ {
+ if (Request::method() == 'POST') {
+ $gets = Request::get('stock_delivery');
+ $id = $gets['id'];
+ $gets['freight_created_dt'] = date('Y-m-d H:i:s');
+ $gets['freight_created_by'] = auth()->user()->name;
+ DB::table('stock_delivery')->where('id', $id)->update($gets);
+ return $this->json('物流信息提交成功。', true);
+ }
+ $file = base_path().'/addons/'.ucfirst(Request::module()).'/views/'.Request::controller().'/'.Request::action().'.xml';
+ $id = Request::get('id');
+ $row = Delivery::find($id);
+ $freight_quantity = floatval($row['freight_quantity']);
+
+ if ($freight_quantity == 0) {
+ $count = DB::table('stock_delivery_data')
+ ->where('delivery_id', $id)->selectRaw('sum(total_weight) as weight, sum(quantity) as quantity')->first();
+ $weight = intval($count['weight'] / 100);
+ $weight = number_format($weight / 10, 1);
+ $quantity = $count['quantity'];
+ $row['freight_quantity'] = $quantity;
+ $row['freight_weight'] = $weight;
+ }
+ $form = Form::make1(['table' => 'stock_delivery', 'file' => $file, 'row' => $row]);
+ return $form;
+ }
+
+ // 获取库存(不含不满件)
+ public function getBatchSelectAction()
+ {
+ $search = search_form(['advanced' => ''], [
+ ['form_type' => 'text', 'name' => '产品名称', 'field' => 'name'],
+ ['form_type' => 'text', 'name' => '产品编码', 'field' => 'code']
+ ], 'model');
+ $query = $search['query'];
+ if (Request::method() == 'POST') {
+ $rows = StockService::getBatchSelect($query['warehouse_id'], $query['product_id'], $query['value'], $query['customer_id']);
+ return ['data' => $rows];
+ }
+ return $this->render([
+ 'search' => $search,
+ ]);
+ }
+
+ // 获取库存(直营)
+ public function getBatchSelectZYAction()
+ {
+ $search = search_form(['advanced' => ''], [
+ ['form_type' => 'text', 'name' => '产品名称', 'field' => 'name'],
+ ['form_type' => 'text', 'name' => '产品编码', 'field' => 'code']
+ ], 'model');
+ $query = $search['query'];
+ if (Request::method() == 'POST') {
+ $rows = StockService::getBatchSelectZY($query['warehouse_id'], $query['product_id'], $query['value']);
+ return ['data' => $rows];
+ }
+ return $this->render([
+ 'search' => $search,
+ ], 'getBatchSelect');
+ }
+
+ // 获取库存(全部)
+ public function getBatchSelectAllAction()
+ {
+ $search = search_form(['advanced' => ''], [
+ ['form_type' => 'text', 'name' => '产品名称', 'field' => 'name'],
+ ['form_type' => 'text', 'name' => '产品编码', 'field' => 'code']
+ ], 'model');
+ $query = $search['query'];
+ if (Request::method() == 'POST') {
+ $rows = StockService::getBatchSelectAll($query['warehouse_id'], $query['product_id'], $query['value'], 0);
+ return ['data' => $rows];
+ }
+ return $this->render([
+ 'search' => $search,
+ ], 'getBatchSelect');
+ }
+
+ // 删除
+ public function deleteAction()
+ {
+ if (Request::method() == 'POST') {
+ $ids = Request::get('id');
+ return Form::remove(['code' => 'stock_delivery', 'ids' => $ids]);
+ }
+ }
+}
diff --git a/app/Gdoo/Stock/Controllers/DirectController.php b/app/Gdoo/Stock/Controllers/DirectController.php
new file mode 100644
index 00000000..987668c7
--- /dev/null
+++ b/app/Gdoo/Stock/Controllers/DirectController.php
@@ -0,0 +1,247 @@
+ 'stock_direct',
+ 'referer' => 1,
+ 'search' => ['by' => ''],
+ ]);
+
+ $cols = $header['cols'];
+
+ $cols['actions']['options'] = [[
+ 'name' => '显示',
+ 'action' => 'show',
+ 'display' => $this->access['show'],
+ ]];
+
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table'])->setBy($header);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+ $model->orderBy($header['sort'], $header['order']);
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ $model->select($header['select']);
+ $rows = $model->paginate($query['limit'])->appends($query);
+ $items = Grid::dataFilters($rows, $header);
+ return $items->toJson();
+ }
+
+ $header['buttons'] = [
+ ['name' => '导出', 'icon' => 'fa-share', 'action' => 'export', 'display' => 1],
+ ];
+
+ $header['cols'] = $cols;
+ $header['tabs'] = Direct::$tabs;
+ $header['bys'] = Direct::$bys;
+ $header['js'] = Grid::js($header);
+
+ return $this->display([
+ 'header' => $header,
+ ]);
+ }
+
+ // 新建
+ public function createAction($action = 'edit')
+ {
+ $id = (int) Request::get('id');
+ $header['action'] = $action;
+ $header['code'] = 'stock_direct';
+ $header['id'] = $id;
+
+ $form = Form::make($header);
+ $tpl = $action == 'print' ? 'print' : 'create';
+ return $this->display([
+ 'form' => $form,
+ ], $tpl);
+ }
+
+ // 编辑
+ public function editAction()
+ {
+ return $this->createAction();
+ }
+
+ // 审核
+ public function auditAction()
+ {
+ return $this->createAction('audit');
+ }
+
+ // 显示
+ public function showAction()
+ {
+ return $this->createAction('show');
+ }
+
+ // 打印
+ public function printAction()
+ {
+ $id = Request::get('id');
+ $template_id = Request::get('template_id');
+
+ $this->layout = 'layouts.print3';
+
+ $master = DB::table('stock_direct as sd')
+ ->leftJoin('customer as c', 'c.id', '=', 'sd.customer_id')
+ ->leftJoin('customer_tax as ct', 'ct.id', '=', 'sd.tax_id')
+ ->leftJoin('sale_type as st', 'st.id', '=', 'sd.type_id')
+ ->selectRaw('sd.*, ct.name as tax_name, c.name as customer_name, st.name as type_name')
+ ->where('sd.id', $id)
+ ->first();
+
+ $model = DB::table('stock_direct_data as sdd')
+ ->leftJoin('stock_direct as sd', 'sd.id', '=', 'sdd.direct_id')
+ ->leftJoin('product as p', 'p.id', '=', 'sdd.product_id')
+ ->leftJoin('product_unit as pu', 'pu.id', '=', 'p.unit_id')
+ ->leftJoin('customer_order_type as cot', 'cot.id', '=', 'sdd.type_id')
+ ->leftJoin('warehouse as w', 'w.id', '=', 'sdd.warehouse_id');
+
+ $model->where('sdd.direct_id', $id);
+
+ $model->whereRaw("p.code <> '99001'");
+
+ $rows = $model->selectRaw("
+ sdd.*,
+ p.name as product_name,
+ p.spec as product_spec,
+ cot.name as type_name,
+ pu.name as product_unit,
+ p.material_type,
+ p.product_type,
+ SUBSTRING(batch_sn, 3, 4) as batch_sn,
+ case when right(w.name, 4) = '不满件库' then 'B' else '' end warehouse_type
+ ")
+ ->orderBy('p.code', 'asc')
+ ->get();
+
+ $money = DB::table('stock_direct_data as sdd')
+ ->leftJoin('product as p', 'p.id', '=', 'sdd.product_id')
+ ->where('sdd.direct_id', $id)
+ ->whereRaw("p.code = '99001'")
+ ->sum("money");
+
+ $form = [
+ 'template' => DB::table('model_template')->where('id', $template_id)->first()
+ ];
+
+ return $this->display([
+ 'master' => $master,
+ 'money' => $money,
+ 'rows' => $rows,
+ 'form' => $form,
+ ], 'print/'.$template_id);
+ }
+
+ public function importExcelAction()
+ {
+ if (Request::method() == 'POST') {
+ $customer_id = Request::get('customer_id');
+ $file = Request::file('file');
+ if ($file->isValid()) {
+ $types = DB::table('customer_order_type')->get()->keyBy('name');
+ $customer = DB::table('customer')->where('id', $customer_id)->first();
+ $products = DB::table('customer_price')
+ ->leftJoin('product', 'product.id', '=', 'customer_price.product_id')
+ ->leftJoin('product_unit', 'product_unit.id', '=', 'product.unit_id')
+ ->where('customer_id', $customer_id)
+ ->selectRaw('
+ product.*,
+ customer_price.price,
+ product_unit.name as unit_name,
+ product.price'.$customer['type_id'].' as product_price
+ ')
+ ->get()->keyBy('code');
+
+ /*
+ [0] => 类型
+ [1] => 产品编码
+ [2] => 数量
+ [3] => 单价
+ [4] => 备注
+ */
+ $rows = readExcel($file->getPathName(), $file->getClientOriginalExtension());
+ $items = [];
+ foreach($rows as $i => $row) {
+ if ($i > 1) {
+ $type = $types[$row[0]];
+ if (empty($type)) {
+ return $this->json('产品编码'.$row[1].':类型('.$row[0].')不存在。');
+ }
+ $product = $products[$row[1]];
+ if (empty($product)) {
+ return $this->json('产品编码'.$row[1].'在客户销售价格中不存在。');
+ }
+
+ if (floatval($row[3]) <> 0) {
+ $price = $row[3];
+ } else {
+ $price = floatval($product['price']) == 0 ? $product['product_price'] : $product['price'];
+ }
+
+ $quantity = $row[2];
+ $item = [
+ 'type_id' => $type['id'],
+ 'type_id_name' => $type['name'],
+ 'product_id' => $product['id'],
+ 'product_code' => $product['code'],
+ 'product_name' => $product['name'],
+ 'product_spec' => $product['spec'],
+ 'product_barcode' => $product['barcode'],
+ 'product_unit' => $product['unit_name'],
+ 'price' => $price,
+ 'quantity' => $quantity,
+ 'money' => $price * $quantity,
+ 'weight' => $product['weight'],
+ 'total_weight' => $product['weight'] * $quantity,
+ 'remark' => $row[3],
+ ];
+ if ($type['id'] == 2) {
+ $item['other_money'] = $item['money'];
+ }
+ $items[] = $item;
+ }
+ }
+ return $this->json($items, true);
+ }
+ }
+ return view('importExcel');
+ }
+
+ // 删除
+ public function deleteAction()
+ {
+ if (Request::method() == 'POST') {
+ $ids = Request::get('id');
+ return Form::remove(['code' => 'stock_direct', 'ids' => $ids]);
+ }
+ }
+}
diff --git a/app/Gdoo/Stock/Controllers/LocationController.php b/app/Gdoo/Stock/Controllers/LocationController.php
new file mode 100644
index 00000000..5b7bdfd0
--- /dev/null
+++ b/app/Gdoo/Stock/Controllers/LocationController.php
@@ -0,0 +1,188 @@
+ 'warehouse_location',
+ 'referer' => 1,
+ 'search' => ['by' => ''],
+ ]);
+
+ $cols = $header['cols'];
+
+ $cols['actions']['options'] = [[
+ 'name' => '编辑',
+ 'action' => 'edit',
+ 'display' => $this->access['edit'],
+ ]];
+
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table'])->setBy($header);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+ $model->orderBy($header['sort'], $header['order'])
+ ->orderBy('id', 'desc');
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ $model->select($header['select']);
+ $rows = $model->paginate($query['limit'])->appends($query);
+ $items = Grid::dataFilters($rows, $header);
+ return $items->toJson();
+ }
+
+ $header['buttons'] = [
+ //['name' => '删除', 'icon' => 'fa-remove', 'action' => 'delete', 'display' => $this->access['delete']],
+ ['name' => '导出', 'icon' => 'fa-share', 'action' => 'export', 'display' => 1],
+ ];
+ $header['cols'] = $cols;
+ $header['tabs'] = WarehouseLocation::$tabs;
+ $header['bys'] = WarehouseLocation::$bys;
+ $header['js'] = Grid::js($header);
+
+ return $this->display([
+ 'header' => $header,
+ ]);
+ }
+
+ // 新建客户联系人
+ public function createAction($action = 'edit')
+ {
+ $id = (int)Request::get('id');
+ $form = Form::make(['code' => 'warehouse_location', 'id' => $id, 'action' => $action]);
+ return $this->render([
+ 'form' => $form,
+ ], 'create');
+ }
+
+ // 创建客户联系人
+ public function editAction()
+ {
+ return $this->createAction();
+ }
+
+ /**
+ * 弹出层信息
+ */
+ public function dialogAction()
+ {
+ $header = Grid::header([
+ 'code' => 'warehouse_location',
+ ]);
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table']);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+
+ $model->where('warehouse_location.warehouse_id', $query['warehouse_id'])
+ ->orderBy('warehouse_location.sort', 'asc');
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+ $model->select($header['select']);
+
+ $rows = $model->paginate($query['limit']);
+ $items = Grid::dataFilters($rows, $header, function($item) {
+ return $item;
+ });
+ return response()->json($items);
+ }
+
+ return $this->render([
+ 'search' => $search,
+ ], 'dialog');
+ }
+
+ /**
+ * 获取现存量
+ */
+ public function dialog2Action()
+ {
+ $header = Grid::header([
+ 'code' => 'warehouse_location',
+ ]);
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table']);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+ $model->where('warehouse_location.status', 1)
+ ->orderBy('warehouse_location.sort', 'asc');
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ $model->select($header['select']);
+
+ $rows = $model->paginate($query['limit']);
+ $items = Grid::dataFilters($rows, $header, function($item) {
+ return $item;
+ });
+ return response()->json($items);
+ }
+
+ return $this->render([
+ 'search' => $search,
+ ], 'dialog2');
+ }
+
+ /**
+ * 获取仓库货位
+ */
+ public function serviceWarehouseAction()
+ {
+ $warehouse_id = Request::get('warehouse_id');
+ if (Request::method() == 'POST') {
+ $model = DB::table('warehouse_location')
+ ->where('warehouse_location.warehouse_id', $warehouse_id)
+ ->where('warehouse_location.status', 1)
+ ->orderBy('warehouse_location.sort', 'asc');
+ return response()->json($model->get());
+ }
+ }
+
+ // 删除
+ public function deleteAction()
+ {
+ if (Request::method() == 'POST') {
+ $ids = Request::get('id');
+ return Form::remove(['code' => 'warehouse_location', 'ids' => $ids]);
+ }
+ }
+}
diff --git a/app/Gdoo/Stock/Controllers/Record01Controller.php b/app/Gdoo/Stock/Controllers/Record01Controller.php
new file mode 100644
index 00000000..76f8967b
--- /dev/null
+++ b/app/Gdoo/Stock/Controllers/Record01Controller.php
@@ -0,0 +1,115 @@
+ 'stock_record01',
+ 'referer' => 1,
+ 'search' => ['by' => ''],
+ ]);
+
+ $cols = $header['cols'];
+
+ $cols['actions']['options'] = [[
+ 'name' => '显示',
+ 'action' => 'show',
+ 'display' => $this->access['show'],
+ ]];
+
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table'])->setBy($header);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+ $model->orderBy($header['sort'], $header['order']);
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ $model->select($header['select']);
+ $rows = $model->paginate($query['limit'])->appends($query);
+ $items = Grid::dataFilters($rows, $header);
+ return $items->toJson();
+ }
+
+ $header['buttons'] = [
+ //['name' => '删除', 'icon' => 'fa-remove', 'action' => 'delete', 'display' => $this->access['delete']],
+ ['name' => '导出', 'icon' => 'fa-share', 'action' => 'export', 'display' => 1],
+ ];
+
+ $header['cols'] = $cols;
+ $header['tabs'] = Record01::$tabs;
+ $header['bys'] = Record01::$bys;
+ $header['js'] = Grid::js($header);
+
+ return $this->display([
+ 'header' => $header,
+ ]);
+ }
+
+ // 新建
+ public function createAction($action = 'edit')
+ {
+ $id = (int) Request::get('id');
+ $header['action'] = $action;
+ $header['code'] = 'stock_record01';
+ $header['id'] = $id;
+
+ $form = Form::make($header);
+ $tpl = $action == 'print' ? 'print' : 'create';
+ return $this->display([
+ 'form' => $form,
+ ], $tpl);
+ }
+
+ // 编辑
+ public function editAction()
+ {
+ return $this->createAction();
+ }
+
+ // 显示
+ public function showAction()
+ {
+ return $this->createAction('show');
+ }
+
+ // 打印
+ public function printAction()
+ {
+ $this->layout = 'layouts.print2';
+ print_prince($this->createAction('print'));
+ }
+
+ // 删除
+ public function deleteAction()
+ {
+ if (Request::method() == 'POST') {
+ $ids = Request::get('id');
+ return Form::remove(['code' => 'stock_record01', 'ids' => $ids]);
+ }
+ }
+}
\ No newline at end of file
diff --git a/app/Gdoo/Stock/Controllers/Record08Controller.php b/app/Gdoo/Stock/Controllers/Record08Controller.php
new file mode 100644
index 00000000..abd82703
--- /dev/null
+++ b/app/Gdoo/Stock/Controllers/Record08Controller.php
@@ -0,0 +1,166 @@
+ 'stock_record08',
+ 'referer' => 1,
+ 'search' => ['by' => ''],
+ ]);
+
+ $cols = $header['cols'];
+
+ $cols['actions']['options'] = [[
+ 'name' => '显示',
+ 'action' => 'show',
+ 'display' => $this->access['show'],
+ ]];
+
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table'])->setBy($header);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+ $model->orderBy($header['sort'], $header['order']);
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ $model->select($header['select']);
+ $rows = $model->paginate($query['limit'])->appends($query);
+ $items = Grid::dataFilters($rows, $header);
+ return $items->toJson();
+ }
+
+ $header['buttons'] = [
+ //['name' => '删除', 'icon' => 'fa-remove', 'action' => 'delete', 'display' => $this->access['delete']],
+ ['name' => '导出', 'icon' => 'fa-share', 'action' => 'export', 'display' => 1],
+ ];
+
+ $header['cols'] = $cols;
+ $header['tabs'] = Record08::$tabs;
+ $header['bys'] = Record08::$bys;
+ $header['js'] = Grid::js($header);
+
+ return $this->display([
+ 'header' => $header,
+ ]);
+ }
+
+ // 新建
+ public function createAction($action = 'edit')
+ {
+ $id = (int) Request::get('id');
+ $header['action'] = $action;
+ $header['code'] = 'stock_record08';
+ $header['id'] = $id;
+
+ $form = Form::make($header);
+ $tpl = $action == 'print' ? 'print' : 'create';
+ return $this->display([
+ 'form' => $form,
+ ], $tpl);
+ }
+
+ // 编辑
+ public function editAction()
+ {
+ return $this->createAction();
+ }
+
+ // 显示
+ public function showAction()
+ {
+ return $this->createAction('show');
+ }
+
+ // 打印
+ public function printAction()
+ {
+ $this->layout = 'layouts.print2';
+ print_prince($this->createAction('print'));
+ }
+
+ public function importExcelAction()
+ {
+ if (Request::method() == 'POST') {
+ $file = Request::file('file');
+
+ if ($file->isValid()) {
+ $products = DB::table('product')
+ ->leftJoin('product_unit', 'product_unit.id', '=', 'product.unit_id')
+ ->selectRaw('
+ product.*,
+ product_unit.name as unit_name
+ ')
+ ->get()
+ ->keyBy('code');
+ /*
+ [0] => 存货编码
+ [1] => 存货名称
+ [2] => 规格型号
+ [3] => 批次
+ [4] => 数量
+ */
+ $rows = readExcel($file->getPathName(), $file->getClientOriginalExtension());
+ $items = [];
+ foreach($rows as $i => $row) {
+ if ($i > 1) {
+ if ($row[0]) {
+ $product = $products[$row[0]];
+ if (empty($product)) {
+ return $this->json('产品编码'.$product[0].':产品('.$product[1].')不存在。');
+ }
+ $batch_sn = $row[3];
+ $quantity = $row[4];
+ $item = [
+ 'product_id' => $product['id'],
+ 'product_code' => $product['code'],
+ 'product_name' => $product['name'],
+ 'product_spec' => $product['spec'],
+ 'product_unit' => $product['unit_name'],
+ 'quantity' => $quantity,
+ 'batch_sn' => $batch_sn,
+ ];
+ $items[] = $item;
+ }
+ }
+ }
+ return $this->json($items, true);
+ }
+ }
+ return view('importExcel');
+ }
+
+ // 删除
+ public function deleteAction()
+ {
+ if (Request::method() == 'POST') {
+ $ids = Request::get('id');
+ return Form::remove(['code' => 'stock_record08', 'ids' => $ids]);
+ }
+ }
+}
diff --git a/app/Gdoo/Stock/Controllers/Record09Controller.php b/app/Gdoo/Stock/Controllers/Record09Controller.php
new file mode 100644
index 00000000..aa8aae7b
--- /dev/null
+++ b/app/Gdoo/Stock/Controllers/Record09Controller.php
@@ -0,0 +1,157 @@
+ 'stock_record09',
+ 'referer' => 1,
+ 'search' => ['by' => ''],
+ ]);
+
+ $cols = $header['cols'];
+
+ $cols['actions']['options'] = [[
+ 'name' => '显示',
+ 'action' => 'show',
+ 'display' => $this->access['show'],
+ ]];
+
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table'])->setBy($header);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+ $model->orderBy($header['sort'], $header['order']);
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ $model->select($header['select']);
+ $rows = $model->paginate($query['limit'])->appends($query);
+ $items = Grid::dataFilters($rows, $header);
+ return $items->toJson();
+ }
+
+ $header['buttons'] = [
+ //['name' => '删除', 'icon' => 'fa-remove', 'action' => 'delete', 'display' => $this->access['delete']],
+ ['name' => '导出', 'icon' => 'fa-share', 'action' => 'export', 'display' => 1],
+ ];
+
+ $header['cols'] = $cols;
+ $header['tabs'] = Record09::$tabs;
+ $header['bys'] = Record09::$bys;
+ $header['js'] = Grid::js($header);
+
+ return $this->display([
+ 'header' => $header,
+ ]);
+ }
+
+ // 新建
+ public function createAction($action = 'edit')
+ {
+ $id = (int) Request::get('id');
+ $header['action'] = $action;
+ $header['code'] = 'stock_record09';
+ $header['id'] = $id;
+
+ $form = Form::make($header);
+ $tpl = $action == 'print' ? 'print' : 'create';
+ return $this->display([
+ 'form' => $form,
+ ], $tpl);
+ }
+
+ // 编辑
+ public function editAction()
+ {
+ return $this->createAction();
+ }
+
+ // 审核
+ public function auditAction()
+ {
+ return $this->createAction();
+ }
+
+ // 显示
+ public function showAction()
+ {
+ return $this->createAction('show');
+ }
+
+ // 打印
+ public function printAction()
+ {
+ $id = Request::get('id');
+ $template_id = Request::get('template_id');
+
+ $this->layout = 'layouts.print3';
+ $master = DB::table('stock_record09 as m')->where('m.id', $id)
+ ->leftJoin('stock_type as st', 'st.id', '=', 'm.type_id')
+ ->leftJoin('department', 'department.id', '=', 'm.department_id')
+ ->leftJoin('warehouse', 'warehouse.id', '=', 'm.warehouse_id')
+ ->selectRaw('m.*, st.name as type_name, warehouse.name as warehouse_name, department.name as department_name')
+ ->first();
+
+ $rows = DB::table('stock_record09_data as d')
+ ->leftJoin('stock_record09 as m', 'm.id', '=', 'd.record09_id')
+ ->leftJoin('product as p', 'p.id', '=', 'd.product_id')
+ ->leftJoin('product_unit as pu', 'pu.id', '=', 'p.unit_id')
+ ->leftJoin('stock_type as st', 'st.id', '=', 'm.type_id')
+ ->where('m.id', $id)
+ ->selectRaw('
+ d.*,
+ p.name as product_name,
+ p.code as product_code,
+ p.spec as product_spec,
+ st.name as type_name,
+ pu.name as product_unit
+ ')
+ ->orderBy('p.code', 'asc')
+ ->get();
+
+ $form = [
+ 'template' => DB::table('model_template')->where('id', $template_id)->first()
+ ];
+
+ $tpl = $this->display([
+ 'master' => $master,
+ 'rows' => $rows,
+ 'form' => $form,
+ ], 'print/'.$template_id);
+ return $tpl;
+ }
+
+ // 删除
+ public function deleteAction()
+ {
+ if (Request::method() == 'POST') {
+ $ids = Request::get('id');
+ return Form::remove(['code' => 'stock_record09', 'ids' => $ids]);
+ }
+ }
+}
diff --git a/app/Gdoo/Stock/Controllers/Record10Controller.php b/app/Gdoo/Stock/Controllers/Record10Controller.php
new file mode 100644
index 00000000..63020b30
--- /dev/null
+++ b/app/Gdoo/Stock/Controllers/Record10Controller.php
@@ -0,0 +1,275 @@
+ 'stock_record10',
+ 'referer' => 1,
+ 'search' => ['by' => ''],
+ ]);
+
+ $cols = $header['cols'];
+
+ $cols['actions']['options'] = [[
+ 'name' => '显示',
+ 'action' => 'show',
+ 'display' => $this->access['show'],
+ ]];
+
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table'])->setBy($header);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+ $model->orderBy($header['sort'], $header['order']);
+
+ // 川南库管登录
+ if (auth()->id() == 2177) {
+ $model->whereIn('stock_record10.warehouse_id', [20001, 20047]);
+ } else {
+ $model->whereNotIn('stock_record10.warehouse_id', [20001, 20047]);
+ }
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ $model->select($header['select']);
+ $rows = $model->paginate($query['limit'])->appends($query);
+ $items = Grid::dataFilters($rows, $header);
+ return $items->toJson();
+ }
+
+ $header['buttons'] = [
+ //['name' => '删除', 'icon' => 'fa-remove', 'action' => 'delete', 'display' => $this->access['delete']],
+ ['name' => '导出', 'icon' => 'fa-share', 'action' => 'export', 'display' => 1],
+ ];
+
+ $header['cols'] = $cols;
+ $header['tabs'] = Record10::$tabs;
+ $header['bys'] = Record10::$bys;
+ $header['js'] = Grid::js($header);
+
+ return $this->display([
+ 'header' => $header,
+ ]);
+ }
+
+ // 新建
+ public function createAction($action = 'edit')
+ {
+ $id = (int) Request::get('id');
+ $header['action'] = $action;
+ $header['code'] = 'stock_record10';
+ $header['id'] = $id;
+
+ $form = Form::make($header);
+
+ $tpl = $action == 'print' ? 'print' : 'create';
+ return $this->display([
+ 'form' => $form,
+ ], $tpl);
+ }
+
+ // 编辑
+ public function editAction()
+ {
+ return $this->createAction();
+ }
+
+ // 审核
+ public function auditAction()
+ {
+ return $this->createAction('audit');
+ }
+
+ // 显示
+ public function showAction()
+ {
+ return $this->createAction('show');
+ }
+
+ // 打印
+ public function print2Action()
+ {
+ $this->layout = 'layouts.print2';
+ $view = $this->createAction('print');
+ $viewData = $view->getData();
+ print_prince($this->createAction('print'));
+ }
+
+ // 显示促销
+ public function printAction()
+ {
+ $id = Request::get('id');
+ $template_id = Request::get('template_id');
+ if ($template_id == 117) {
+
+ $this->layout = 'layouts.print3';
+
+ $master = DB::table('stock_record10 as m')->where('m.id', $id)
+ ->leftJoin('stock_type as st', 'st.id', '=', 'm.type_id')
+ ->leftJoin('department', 'department.id', '=', 'm.department_id')
+ ->leftJoin('warehouse', 'warehouse.id', '=', 'm.warehouse_id')
+ ->selectRaw('m.*, st.name as type_name, warehouse.name as warehouse_name, department.name as department_name')
+ ->first();
+
+ $rows = DB::table('stock_record10_data as d')
+ ->leftJoin('stock_record10 as m', 'm.id', '=', 'd.record10_id')
+ ->leftJoin('product as p', 'p.id', '=', 'd.product_id')
+ ->leftJoin('product_unit as pu', 'pu.id', '=', 'p.unit_id')
+ ->leftJoin('stock_type as st', 'st.id', '=', 'm.type_id')
+ ->where('m.id', $id)
+ ->selectRaw('
+ d.*,
+ p.name as product_name,
+ p.code as product_code,
+ p.spec as product_spec,
+ st.name as type_name,
+ pu.name as product_unit
+ ')
+ ->get();
+
+ $form = [
+ 'template' => DB::table('model_template')->where('id', $template_id)->first()
+ ];
+
+ $tpl = $this->display([
+ 'master' => $master,
+ 'rows' => $rows,
+ 'form' => $form,
+ ], 'print/'.$template_id);
+ return $tpl;
+
+ } else {
+ $this->layout = 'layouts.print2';
+ $tpl = $this->createAction('print');
+ print_prince($tpl);
+ }
+ }
+
+ // 显示促销
+ public function print3Action()
+ {
+ $this->layout = 'layouts.print2';
+ $id = Request::get('id');
+ $template_id = Request::get('template_id');
+ if ($template_id == 117) {
+
+ $this->layout = 'layouts.print2';
+
+ $master = DB::table('stock_record10 as m')->where('m.id', $id)
+ ->leftJoin('stock_type as st', 'st.id', '=', 'm.type_id')
+ ->leftJoin('department', 'department.id', '=', 'm.department_id')
+ ->leftJoin('warehouse', 'warehouse.id', '=', 'm.warehouse_id')
+ ->selectRaw('m.*, st.name as type_name, warehouse.name as warehouse_name, department.name as department_name')
+ ->first();
+
+ $rows = DB::table('stock_record10_data as d')
+ ->leftJoin('stock_record10 as m', 'm.id', '=', 'd.record10_id')
+ ->leftJoin('product as p', 'p.id', '=', 'd.product_id')
+ ->leftJoin('product_unit as pu', 'pu.id', '=', 'p.unit_id')
+ ->leftJoin('stock_type as st', 'st.id', '=', 'm.type_id')
+ ->where('m.id', $id)
+ ->selectRaw('
+ d.*,
+ p.name as product_name,
+ p.code as product_code,
+ p.spec as product_spec,
+ st.name as type_name,
+ pu.name as product_unit
+ ')
+ ->get();
+
+ $form = [
+ 'template' => DB::table('model_template')->where('id', $template_id)->first()
+ ];
+
+ $template = "report.fr3";
+ $ver = 3.0;
+
+ $id = (int) Request::get('id');
+ $header['action'] = 'print';
+ $header['code'] = 'stock_record10';
+ $header['id'] = $id;
+ $form = Form::make($header);
+
+ $Tables = [];
+ foreach($form['prints'] as $print) {
+ $fields = [];
+ foreach($print['fields'] as $field) {
+ $type = 'str';
+ $size = 255;
+ if ($field['type'] == 'INT' || $field['type'] == 'TINYINT') {
+ $type = 'int';
+ $size = 0;
+ }
+ if ($field['type'] == 'DATE') {
+ $type = 'str';
+ }
+ if ($field['type'] == 'DECIMAL') {
+ $type = 'float';
+ $size = 0;
+ }
+ $fields[] = ["type" => $type, "size" => $size, "name" => $field['field'], "required" => false];
+ }
+ $Tables[] = [
+ 'Name' => $print['name'],
+ 'Cols' => $fields,
+ 'Data' => $print['data'],
+ ];
+ }
+ $jsonObject = [
+ "template" => $template,
+ "ver" => $ver,
+ "Tables" => $Tables,
+ ];
+
+ $jsonStr = json_encode($jsonObject);
+
+
+ $tpl = $this->display([
+ 'master' => $master,
+ 'jsonStr' => $jsonStr,
+ 'rows' => $rows,
+ 'form' => $form,
+ ], 'print3/'.$template_id);
+ return $tpl;
+
+ } else {
+ $tpl = $this->createAction('print');
+ print_prince($tpl);
+ }
+ }
+
+ // 删除
+ public function deleteAction()
+ {
+ if (Request::method() == 'POST') {
+ $ids = Request::get('id');
+ return Form::remove(['code' => 'stock_record10', 'ids' => $ids]);
+ }
+ }
+}
diff --git a/app/Gdoo/Stock/Controllers/Record11Controller.php b/app/Gdoo/Stock/Controllers/Record11Controller.php
new file mode 100644
index 00000000..3dd2e5ad
--- /dev/null
+++ b/app/Gdoo/Stock/Controllers/Record11Controller.php
@@ -0,0 +1,158 @@
+ 'stock_record11',
+ 'referer' => 1,
+ 'search' => ['by' => ''],
+ ]);
+
+ $cols = $header['cols'];
+
+ $cols['actions']['options'] = [[
+ 'name' => '显示',
+ 'action' => 'show',
+ 'display' => $this->access['show'],
+ ]];
+
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table'])->setBy($header);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+ $model->orderBy($header['sort'], $header['order']);
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ $model->select($header['select']);
+ $rows = $model->paginate($query['limit'])->appends($query);
+ $items = Grid::dataFilters($rows, $header);
+ return $items->toJson();
+ }
+
+ $header['buttons'] = [
+ ['name' => '导出', 'icon' => 'fa-share', 'action' => 'export', 'display' => 1],
+ ];
+
+ $header['cols'] = $cols;
+ $header['tabs'] = Record11::$tabs;
+ $header['bys'] = Record11::$bys;
+ $header['js'] = Grid::js($header);
+
+ return $this->display([
+ 'header' => $header,
+ ]);
+ }
+
+ // 新建
+ public function createAction($action = 'edit')
+ {
+ $id = (int) Request::get('id');
+ $header['action'] = $action;
+ $header['code'] = 'stock_record11';
+ $header['id'] = $id;
+
+ $form = Form::make($header);
+ $tpl = $action == 'print' ? 'print' : 'create';
+ return $this->display([
+ 'form' => $form,
+ ], $tpl);
+ }
+
+ // 编辑
+ public function editAction()
+ {
+ return $this->createAction();
+ }
+
+ // 显示
+ public function showAction()
+ {
+ return $this->createAction('show');
+ }
+
+ // 打印
+ public function printAction()
+ {
+ $id = Request::get('id');
+ $template_id = Request::get('template_id');
+ if ($template_id == 115) {
+
+ $this->layout = 'layouts.print3';
+
+ $master = DB::table('stock_record11 as m')->where('m.id', $id)
+ ->leftJoin('stock_type as st', 'st.id', '=', 'm.type_id')
+ ->leftJoin('department', 'department.id', '=', 'm.department_id')
+ ->leftJoin('warehouse', 'warehouse.id', '=', 'm.warehouse_id')
+ ->selectRaw('m.*, st.name as type_name, warehouse.name as warehouse_name, department.name as department_name')
+ ->first();
+
+ $rows = DB::table('stock_record11_data as d')
+ ->leftJoin('stock_record11 as m', 'm.id', '=', 'd.record11_id')
+ ->leftJoin('product as p', 'p.id', '=', 'd.product_id')
+ ->leftJoin('product_unit as pu', 'pu.id', '=', 'p.unit_id')
+ ->leftJoin('stock_type as st', 'st.id', '=', 'm.type_id')
+ ->where('m.id', $id)
+ ->selectRaw('
+ d.*,
+ p.name as product_name,
+ p.code as product_code,
+ p.spec as product_spec,
+ st.name as type_name,
+ pu.name as product_unit
+ ')
+ ->orderBy('p.code', 'asc')
+ ->get();
+
+ $form = [
+ 'template' => DB::table('model_template')->where('id', $template_id)->first()
+ ];
+
+ $tpl = $this->display([
+ 'master' => $master,
+ 'rows' => $rows,
+ 'form' => $form,
+ ], 'print/'.$template_id);
+ return $tpl;
+
+ } else {
+ $this->layout = 'layouts.print2';
+ $tpl = $this->createAction('print');
+ print_prince($tpl);
+ }
+ }
+
+ // 删除
+ public function deleteAction()
+ {
+ if (Request::method() == 'POST') {
+ $ids = Request::get('id');
+ return Form::remove(['code' => 'stock_record11', 'ids' => $ids]);
+ }
+ }
+}
diff --git a/app/Gdoo/Stock/Controllers/ReportController.php b/app/Gdoo/Stock/Controllers/ReportController.php
new file mode 100644
index 00000000..6a247dcb
--- /dev/null
+++ b/app/Gdoo/Stock/Controllers/ReportController.php
@@ -0,0 +1,183 @@
+ 0,
+ ], [
+ ['form_type' => 'dialog', 'name' => '仓库', 'field' => 'warehouse_id', 'options' => ['url' => 'stock/warehouse/dialog', 'query' => ['multi'=>0]]],
+ ['form_type' => 'dialog', 'name' => '产品', 'field' => 'product_id', 'options' => ['url' => 'product/product/dialog', 'query' => ['multi'=>0]]],
+ ['form_type' => 'text', 'name' => '批号', 'field' => 'batch_sn', 'options' => []],
+ ['form_type' => 'select', 'name' => '内销/外销', 'field' => 'type', 'options' => [['id'=>'内销','name'=>'内销'],['id'=>'外贸','name'=>'外贸']]],
+ //['form_type' => 'select', 'name' => '是否统计批号', 'field' => 'batch', 'value' => 0, 'options' => [['id'=>1,'name'=>'是'],['id'=>0,'name'=>'否']]],
+ ['form_type' => 'select', 'name' => '包含不满件库', 'field' => 'bmj', 'value' => 0, 'options' => [['id'=>1,'name'=>'是'],['id'=>0,'name'=>'否']]],
+ ['form_type' => 'date2', 'name' => '单据日期', 'field' => 'date', 'value' => [$sdate, $edate], 'options' => []],
+ ], 'model');
+
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $fields = [];
+ foreach($search['forms']['field'] as $i => $field) {
+ $fields[$field] = $search['forms']['search'][$i];
+ }
+ $rows = [];
+ if ($query['filter'] == 1) {
+ $rows = StockService::reportOrderStockDetail(
+ $fields['warehouse_id'],
+ $fields['product_id'],
+ $fields['batch_sn'],
+ $fields['type'],
+ $fields['date'][0],
+ $fields['date'][1],
+ auth()->id(),
+ $fields['bmj']
+ );
+ $QmNum = 0;
+ foreach($rows as $i => $row) {
+ if ($row['bill_name'] == '期初') {
+ $QmNum = (float)$row['qm_num'];
+ } else {
+ $QmNum = ((float)$row['rk_num'] - (float)$row['ck_num']) + $QmNum;
+ }
+ $row['qm_num'] = $QmNum;
+ $row['id'] = $i + 1;
+ $rows[$i] = $row;
+ }
+ }
+ return $this->json($rows, true);
+ }
+ $search['table'] = 'material_plan';
+ return $this->display([
+ 'search' => $search,
+ 'query' => $query,
+ ]);
+ }
+
+ // 库存汇总表
+ public function stockTotalAction()
+ {
+ $search = search_form([
+ 'advanced' => 0,
+ ], [
+ ['form_type' => 'dialog', 'name' => '仓库', 'field' => 'warehouse_id', 'options' => ['url' => 'stock/warehouse/dialog', 'query' => ['multi'=>0]]],
+ ['form_type' => 'text', 'name' => '存货编码', 'field' => 'product_code', 'options' => []],
+ ['form_type' => 'select', 'name' => '内销/外销', 'field' => 'type', 'options' => [['id'=>'1','name'=>'内销'],['id'=>'2','name'=>'外贸']]],
+ ['form_type' => 'date2', 'name' => '生产日期', 'field' => 'date', 'value' => [], 'options' => []],
+ ['form_type' => 'select', 'name' => '统计批号', 'field' => 'batch', 'value' => 1, 'options' => [['id'=>1,'name'=>'是'],['id'=>0,'name'=>'否']]],
+ ['form_type' => 'select', 'name' => '包含不满件库', 'field' => 'bmj', 'value' => 0, 'options' => [['id'=>1,'name'=>'是'],['id'=>0,'name'=>'否']]],
+
+ ], 'model');
+
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $fields = [];
+ foreach($search['forms']['field'] as $i => $field) {
+ $fields[$field] = $search['forms']['search'][$i];
+ }
+ $rows = [];
+ if ($query['filter'] == 1) {
+ /*
+ $rows = DB::select('EXEC P_ReportOrderStockTotal ?,?,?,?,?,?,?,?', [
+ $fields['warehouse_id'],
+ $fields['product_code'],
+ $fields['type'],
+ $fields['date'][0],
+ $fields['date'][1],
+ auth()->id(),
+ $fields['batch'],
+ $fields['bmj'],
+ ]);
+ */
+ $rows = StockService::reportOrderStockTotal(
+ $fields['warehouse_id'],
+ $fields['product_code'],
+ $fields['type'],
+ $fields['date'][0],
+ $fields['date'][1],
+ auth()->id(),
+ $fields['batch'],
+ $fields['bmj']
+ );
+ }
+ return $this->json($rows, true);
+ }
+ $search['table'] = 'material_plan';
+ return $this->display([
+ 'search' => $search,
+ 'query' => $query,
+ ]);
+ }
+
+ // 进销存库存汇总表
+ public function stockInOutAction()
+ {
+ $sdate = date('Y-m-01');
+ $edate = date('Y-m-d');
+ $search = search_form([
+ 'advanced' => 0,
+ ], [
+ ['form_type' => 'dialog', 'name' => '仓库', 'field' => 'warehouse_id', 'options' => ['url' => 'stock/warehouse/dialog', 'query' => ['multi'=>0]]],
+ ['form_type' => 'dialog', 'name' => '产品', 'field' => 'product_id', 'options' => ['url' => 'product/product/dialog', 'query' => ['multi'=>0]]],
+ ['form_type' => 'text', 'name' => '批号', 'field' => 'batch_sn', 'options' => []],
+ ['form_type' => 'select', 'name' => '内销/外销', 'field' => 'type', 'options' => [['id'=>'内销','name'=>'内销'],['id'=>'外贸','name'=>'外贸']]],
+ ['form_type' => 'select', 'name' => '统计批号', 'field' => 'batch', 'value' => 0, 'options' => [['id'=>1,'name'=>'是'],['id'=>0,'name'=>'否']]],
+ ['form_type' => 'select', 'name' => '包含不满件库', 'field' => 'bmj', 'value' => 0, 'options' => [['id'=>1,'name'=>'是'],['id'=>0,'name'=>'否']]],
+ ['form_type' => 'date2', 'name' => '单据日期', 'field' => 'date', 'value' => [$sdate, $edate], 'options' => []],
+ ], 'model');
+
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $fields = [];
+ foreach($search['forms']['field'] as $i => $field) {
+ $fields[$field] = $search['forms']['search'][$i];
+ }
+
+ $rows = [];
+ if ($query['filter'] == 1) {
+ $rows = StockService::reportOrderStockInOut(
+ $fields['warehouse_id'],
+ $fields['product_id'],
+ $fields['batch_sn'],
+ $fields['type'],
+ $fields['date'][0],
+ $fields['date'][1],
+ auth()->id(),
+ $fields['batch'],
+ $fields['bmj']
+ );
+ }
+ return $this->json($rows, true);
+ }
+ $search['table'] = 'material_plan';
+ return $this->display([
+ 'search' => $search,
+ 'query' => $query,
+ ]);
+ }
+}
diff --git a/app/Gdoo/Stock/Controllers/TypeController.php b/app/Gdoo/Stock/Controllers/TypeController.php
new file mode 100644
index 00000000..e582cc8f
--- /dev/null
+++ b/app/Gdoo/Stock/Controllers/TypeController.php
@@ -0,0 +1,134 @@
+ 'sale_type',
+ 'referer' => 1,
+ 'search' => ['by' => ''],
+ ]);
+
+ $cols = $header['cols'];
+
+ $cols['actions']['options'] = [[
+ 'name' => '编辑',
+ 'action' => 'edit',
+ 'display' => $this->access['edit'],
+ ]];
+
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table'])->setBy($header);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+ $model->orderBy($header['sort'], $header['order'])
+ ->orderBy('id', 'desc');
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ $model->select($header['select']);
+ $rows = $model->paginate($query['limit'])->appends($query);
+ $items = Grid::dataFilters($rows, $header);
+ return $items->toJson();
+ }
+
+ $header['buttons'] = [
+ ['name' => '删除', 'icon' => 'fa-remove', 'action' => 'delete', 'display' => $this->access['delete']],
+ ['name' => '导出', 'icon' => 'fa-share', 'action' => 'export', 'display' => 1],
+ ];
+ $header['cols'] = $cols;
+ $header['tabs'] = StockType::$tabs;
+ $header['bys'] = StockType::$bys;
+ $header['js'] = Grid::js($header);
+
+ return $this->display([
+ 'header' => $header,
+ ]);
+ }
+
+ // 新建客户联系人
+ public function createAction()
+ {
+ $id = (int)Request::get('id');
+ $form = Form::make(['code' => 'sale_type', 'id' => $id]);
+ return $this->render([
+ 'form' => $form,
+ ], 'create');
+ }
+
+ // 创建客户联系人
+ public function editAction()
+ {
+ return $this->createAction();
+ }
+
+ /**
+ * 弹出层信息
+ */
+ public function dialogAction()
+ {
+ $header = Grid::header([
+ 'code' => 'sale_type',
+ ]);
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table']);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+ $model->where('sale_type.status', 1)
+ ->orderBy('sale_type.sort', 'asc');
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ $model->select($header['select']);
+
+ $rows = $model->paginate($query['limit']);
+ $items = Grid::dataFilters($rows, $header, function($item) {
+ return $item;
+ });
+ return response()->json($items);
+ }
+
+ return $this->render([
+ 'search' => $search,
+ ], 'dialog');
+ }
+
+ // 删除
+ public function deleteAction()
+ {
+ if (Request::method() == 'POST') {
+ $ids = Request::get('id');
+ return Form::remove(['code' => 'sale_type', 'ids' => $ids]);
+ }
+ }
+}
\ No newline at end of file
diff --git a/app/Gdoo/Stock/Controllers/WarehouseController.php b/app/Gdoo/Stock/Controllers/WarehouseController.php
new file mode 100644
index 00000000..7f008fd2
--- /dev/null
+++ b/app/Gdoo/Stock/Controllers/WarehouseController.php
@@ -0,0 +1,177 @@
+ 'warehouse',
+ 'referer' => 1,
+ 'search' => ['by' => ''],
+ ]);
+
+ $cols = $header['cols'];
+
+ $cols['actions']['options'] = [[
+ 'name' => '编辑',
+ 'action' => 'edit',
+ 'display' => $this->access['edit'],
+ ]];
+
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table'])->setBy($header);
+ foreach ($header['join'] as $join) {
+ $model->leftJoin($join[0], $join[1], $join[2], $join[3]);
+ }
+ $model->orderBy($header['sort'], $header['order'])
+ ->orderBy('id', 'desc');
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ $model->select($header['select']);
+ $rows = $model->paginate($query['limit'])->appends($query);
+ $items = Grid::dataFilters($rows, $header);
+ return $items->toJson();
+ }
+
+ $header['buttons'] = [
+ ['name' => '删除', 'icon' => 'fa-remove', 'action' => 'delete', 'display' => $this->access['delete']],
+ ['name' => '导出', 'icon' => 'fa-share', 'action' => 'export', 'display' => 1],
+ ];
+ $header['cols'] = $cols;
+ $header['tabs'] = Warehouse::$tabs;
+ $header['bys'] = Warehouse::$bys;
+ $header['js'] = Grid::js($header);
+
+ return $this->display([
+ 'header' => $header,
+ ]);
+ }
+
+ // 新建客户联系人
+ public function createAction()
+ {
+ $id = (int)Request::get('id');
+ $form = Form::make(['code' => 'warehouse', 'id' => $id]);
+ return $this->render([
+ 'form' => $form,
+ ], 'create');
+ }
+
+ // 创建客户联系人
+ public function editAction()
+ {
+ return $this->createAction();
+ }
+
+ /**
+ * 弹出层信息
+ */
+ public function dialogAction()
+ {
+ $header = Grid::header([
+ 'code' => 'warehouse',
+ ]);
+ $search = $header['search_form'];
+ $query = $search['query'];
+
+ if (Request::method() == 'POST') {
+ $model = DB::table($header['table']);
+ $model->leftJoin('user_warehouse', 'user_warehouse.warehouse_id', '=', 'warehouse.id')
+ ->where('user_warehouse.user_id', auth()->id());
+
+ $model->where('warehouse.status', 1)
+ ->orderBy('warehouse.id', 'asc');
+
+ foreach ($search['where'] as $where) {
+ if ($where['active']) {
+ $model->search($where);
+ }
+ }
+
+ $model->select($header['select']);
+
+ $rows = $model->paginate($query['limit']);
+
+ $_locations = DB::table('warehouse_location')->get();
+ $locations = [];
+ foreach ($_locations as $_location) {
+ $locations[$_location['warehouse_id']][] = $_location;
+ }
+
+ $items = Grid::dataFilters($rows, $header, function($item) use($locations) {
+ $item['pos'] = (array)$locations[$item['id']];
+ return $item;
+ });
+ return response()->json($items);
+ }
+
+ return $this->render([
+ 'search' => $search,
+ ], 'dialog');
+ }
+
+ /**
+ * 权限设置
+ */
+ public function permissionAction()
+ {
+ $gets = Request::all();
+ if (Request::method() == 'POST') {
+ $user_id = $gets['user_id'];
+ $rows = $gets['rows'];
+ $users = DB::table('user_warehouse')
+ ->where('user_id', $user_id)
+ ->pluck('id', 'warehouse_id');
+ foreach($rows as $row) {
+ if (empty($users[$row['id']])) {
+ DB::table('user_warehouse')->insert([
+ 'user_id' => $user_id,
+ 'warehouse_id' => $row['id']
+ ]);
+ } else {
+ unset($users[$row['id']]);
+ }
+ }
+ foreach($users as $warehouse_id) {
+ DB::table('user_warehouse')->where('id', $warehouse_id)->delete();
+ }
+ return $this->json('仓库权限设置成功。', true);
+ }
+ $rows = DB::table('warehouse')->orderBy('id', 'asc')->get(['id', 'code', 'name']);
+ $users = DB::table('user_warehouse')->where('user_id', $gets['user_id'])->pluck('id', 'warehouse_id');
+ return $this->render([
+ 'rows' => $rows,
+ 'users' => $users,
+ ]);
+ }
+
+ // 删除
+ public function deleteAction()
+ {
+ if (Request::method() == 'POST') {
+ $ids = Request::get('id');
+ return Form::remove(['code' => 'warehouse', 'ids' => $ids]);
+ }
+ }
+}
diff --git a/app/Gdoo/Stock/Hooks/AllocationHook.php b/app/Gdoo/Stock/Hooks/AllocationHook.php
new file mode 100644
index 00000000..a3bfa001
--- /dev/null
+++ b/app/Gdoo/Stock/Hooks/AllocationHook.php
@@ -0,0 +1,72 @@
+leftJoin('warehouse as w', 'w.id', '=', 'sa.in_warehouse_id')
+ ->leftJoin('warehouse as w2', 'w2.id', '=', 'sa.out_warehouse_id')
+ ->leftJoin('department as d', 'd.id', '=', 'sa.in_department_id')
+ ->leftJoin('department as d2', 'd2.id', '=', 'sa.out_department_id')
+ ->leftJoin('stock_type as st', 'st.id', '=', 'sa.in_type_id')
+ ->leftJoin('stock_type as st2', 'st2.id', '=', 'sa.out_type_id')
+ ->where('sa.id', $id)
+ ->selectRaw('
+ sa.*,
+ st.code as in_type_code,
+ st2.code as out_type_code,
+ d.code as in_department_code,
+ d2.code as out_department_code,
+ w.code as in_warehouse_code,
+ w2.code as out_warehouse_code
+ ')
+ ->first();
+
+ $rows = DB::table('stock_allocation_data')
+ ->leftJoin('product', 'product.id', '=', 'stock_allocation_data.product_id')
+ ->where('stock_allocation_data.allocation_id', $id)
+ ->get(['stock_allocation_data.*', 'product.code as product_code']);
+ // 同步数据到yonyou
+ $ret = plugin_sync_api('postTransVouch', ['master' => $master, 'rows' => $rows]);
+ if ($ret['success'] == true) {
+ return $params;
+ }
+ abort_error($ret['msg']);
+ }
+
+ public function onBeforeAbort($params) {
+ $id = $params['id'];
+ $master = DB::table('stock_allocation')->where('id', $id)->first();
+ // 检查用友单据是否存在
+ $ret = plugin_sync_api('getVouchExist', ['table' => 'TransVouch', 'field' => 'cTVCode', 'value' => $master['sn']]);
+ if ($ret['msg'] > 0) {
+ abort_error('用友存在其他入库单['.$master['sn'].']无法弃审。');
+ }
+ return $params;
+ }
+
+ public function onAfterStore($params) {
+ return $params;
+ }
+
+ public function onBeforeDelete($params) {
+ return $params;
+ }
+
+}
diff --git a/app/Gdoo/Stock/Hooks/CancelHook.php b/app/Gdoo/Stock/Hooks/CancelHook.php
new file mode 100644
index 00000000..d33c2076
--- /dev/null
+++ b/app/Gdoo/Stock/Hooks/CancelHook.php
@@ -0,0 +1,142 @@
+ $data) {
+ if ($data['table'] == 'stock_cancel_data') {
+ foreach($data['data'] as $j => $row) {
+ if ($row['batch_sn']) {
+ $batch_sn = substr($row['batch_sn'], 0, 6);
+ $sn = str_split($batch_sn, 2);
+ $row['batch_date'] = date("Y-m-d", mktime(0, 0, 0, $sn[1], $sn[2], $sn[0]));
+ }
+ if ($row['quantity'] >= 0) {
+ abort_error('产品编码['.$row['product_code'].']数量必须是负数。');
+ }
+ $data['data'][$j] = $row;
+ }
+ $datas[$i] = $data;
+ }
+ }
+ $params['datas'] = $datas;
+ return $params;
+ }
+
+ public function onBeforeAudit($params) {
+ $id = $params['id'];
+ $master = DB::table('stock_cancel')
+ ->leftJoin('customer', 'customer.id', '=', 'stock_cancel.customer_id')
+ ->leftJoin('customer_tax', 'customer_tax.id', '=', 'stock_cancel.tax_id')
+ ->leftJoin('customer_region', 'customer_region.id', '=', 'customer.region_id')
+ ->leftJoin('department', 'department.id', '=', 'customer_tax.department_id')
+ ->leftJoin('sale_type', 'sale_type.id', '=', 'stock_cancel.type_id')
+ ->where('stock_cancel.id', $id)
+ ->first([
+ 'stock_cancel.*',
+ 'sale_type.code as sale_code',
+ 'department.code as department_code',
+ 'customer_tax.code as customer_code',
+ 'customer.region_id',
+ 'customer_region.owner_user_id as salesman_id',
+ 'customer.region2_id',
+ 'customer.region3_id'
+ ]);
+
+ $sql = "select d.id,d.type_id,d.price,d.quantity,d.money,d.other_money,
+ d.batch_sn,
+ d.poscode,
+ d.remark,
+ d.product_id,
+ d.warehouse_id,
+ product.code as product_code,
+ product.name as product_name,
+ d.total_weight,
+ warehouse.code as warehouse_code,
+
+ null as fee_category_name,
+ null as fee_category_id,
+ null as fee_src_type_id,
+ null as fee_src_sn,
+ null as fee_src_id,
+ null as promotion_sn,
+ null as row_index
+
+ from stock_cancel_data as d
+ left Join product on product.id = d.product_id
+ left Join warehouse on warehouse.id = d.warehouse_id
+ where d.cancel_id = ".$id."
+ and product.code <> '99001'
+
+ union
+
+ select t.* from (
+ select d.id,
+ null as type_id,
+ null as price,
+ null as quantity,
+ SUM(d.money) OVER(PARTITION BY product.code) as money,
+ SUM(d.other_money) OVER(PARTITION BY product.code) as other_money,
+ d.batch_sn,
+ d.poscode,
+ d.remark,
+ null as product_id,
+ null as warehouse_id,
+ product.code as product_code,
+ product.name as product_name,
+ null as total_weight,
+ null as warehouse_code,
+ ccc.name as fee_category_name,
+ d.fee_category_id as fee_category_id,
+ d.fee_src_type_id as fee_src_type_id,
+ d.fee_src_sn as fee_src_sn,
+ d.fee_src_id as fee_src_id,
+ d.promotion_sn as promotion_sn,
+ row_number() over(partition by product.code order by d.id desc) row_index
+ from stock_cancel_data as d
+ left Join product on product.id = d.product_id
+ left Join customer_cost_category as ccc on ccc.id = d.fee_category_id
+ where d.cancel_id = ".$id." and product.code = '99001'
+ ) t where t.row_index = 1";
+ $rows = DB::select($sql);
+
+ // 同步数据到yonyou
+ $ret = plugin_sync_api('postCancelOrder', ['master' => $master, 'rows' => $rows]);
+ if ($ret['success'] == true) {
+ return $params;
+ }
+ abort_error($ret['msg']);
+ }
+
+ public function onBeforeAbort($params) {
+ $id = $params['id'];
+ $master = DB::table('stock_cancel')->where('id', $id)->first();
+ // 检查用友单据是否存在
+ $ret = plugin_sync_api('getVouchExist', ['table' => 'DispatchList', 'field' => 'cDLCode', 'value' => $master['sn']]);
+ if ($ret['msg'] > 0) {
+ abort_error('用友存在退货申请['.$master['sn'].']无法弃审。');
+ }
+ return $params;
+ }
+
+ public function onAfterStore($params) {
+ return $params;
+ }
+
+ public function onBeforeDelete($params) {
+ return $params;
+ }
+}
diff --git a/app/Gdoo/Stock/Hooks/DeliveryDataHook.php b/app/Gdoo/Stock/Hooks/DeliveryDataHook.php
new file mode 100644
index 00000000..682e5253
--- /dev/null
+++ b/app/Gdoo/Stock/Hooks/DeliveryDataHook.php
@@ -0,0 +1,23 @@
+leftJoin('customer_order_type as cot', 'cot.id', '=', 'stock_delivery_data.type_id')
+ ->orderBy('cot.sort', 'asc')
+ ->orderBy('product_id_product.code', 'asc')
+ ->orderBy('stock_delivery_data.id', 'asc');
+
+ $params['q'] = $q;
+ return $params;
+ }
+
+ public function onAfterForm($arguments) {
+ return $arguments;
+ }
+}
diff --git a/app/Gdoo/Stock/Hooks/DeliveryHook.php b/app/Gdoo/Stock/Hooks/DeliveryHook.php
new file mode 100644
index 00000000..f4939686
--- /dev/null
+++ b/app/Gdoo/Stock/Hooks/DeliveryHook.php
@@ -0,0 +1,161 @@
+", $error));
+ }
+ }
+ }
+ }
+ return $params;
+ }
+
+ public function onBeforeAudit($params) {
+ $id = $params['id'];
+ $master = DB::table('stock_delivery')
+ ->leftJoin('customer', 'customer.id', '=', 'stock_delivery.customer_id')
+ ->leftJoin('customer_tax', 'customer_tax.id', '=', 'stock_delivery.tax_id')
+ ->leftJoin('customer_region', 'customer_region.id', '=', 'customer.region_id')
+ ->leftJoin('department', 'department.id', '=', 'customer_tax.department_id')
+ ->leftJoin('sale_type', 'sale_type.id', '=', 'stock_delivery.type_id')
+ ->where('stock_delivery.id', $id)
+ ->first([
+ 'stock_delivery.*',
+ 'sale_type.code as sale_code',
+ 'department.code as department_code',
+ 'customer_tax.code as customer_code',
+ 'customer.region_id',
+ 'customer_region.owner_user_id as salesman_id',
+ 'customer.region2_id',
+ 'customer.region3_id'
+ ]);
+
+ $sql = "select sdd.id,sdd.type_id,sdd.price,sdd.quantity,sdd.money,sdd.other_money,
+ sdd.batch_sn,
+ sdd.poscode,
+ sdd.remark,
+ sdd.product_id,
+ sdd.warehouse_id,
+ product.code as product_code,
+ product.name as product_name,
+ sdd.total_weight,
+ warehouse.code as warehouse_code,
+
+ null as fee_category_name,
+ null as fee_category_id,
+ null as fee_src_type_id,
+ null as fee_src_sn,
+ null as fee_src_id,
+ null as promotion_sn,
+ null as row_index
+
+ from stock_delivery_data as sdd
+ left Join product on product.id = sdd.product_id
+ left Join warehouse on warehouse.id = sdd.warehouse_id
+ where sdd.delivery_id = ".$id."
+ and product.code <> '99001'
+
+ union
+
+ select t.* from (
+ select sdd.id,
+ null as type_id,
+ null as price,
+ null as quantity,
+ SUM(sdd.money) OVER(PARTITION BY product.code) as money,
+ SUM(sdd.other_money) OVER(PARTITION BY product.code) as other_money,
+ sdd.batch_sn,
+ sdd.poscode,
+ sdd.remark,
+ null as product_id,
+ null as warehouse_id,
+ product.code as product_code,
+ product.name as product_name,
+ null as total_weight,
+ null as warehouse_code,
+ ccc.name as fee_category_name,
+ sdd.fee_category_id as fee_category_id,
+ sdd.fee_src_type_id as fee_src_type_id,
+ sdd.fee_src_sn as fee_src_sn,
+ sdd.fee_src_id as fee_src_id,
+ sdd.promotion_sn as promotion_sn,
+ row_number() over(partition by product.code order by sdd.id desc) row_index
+ from stock_delivery_data as sdd
+ left Join product on product.id = sdd.product_id
+ left Join customer_cost_category as ccc on ccc.id = sdd.fee_category_id
+ where sdd.delivery_id = ".$id." and product.code = '99001'
+ ) t where t.row_index = 1";
+ $rows = DB::select($sql);
+
+ // 检查库存
+ foreach($rows as $row) {
+ if ($row['product_code'] == '99001') {
+ continue;
+ }
+ // 检查库存
+ $exec = StockService::verfyInvoiceBatch($row['warehouse_id'], $row['product_id'], $row['batch_sn'], $row['poscode'], $row['id'], 0, 0);
+ if ($exec[0]['ky_num'] < $row['quantity']) {
+ abort_error('存货编码为['.$row['product_code'].']的存货库存不足。');
+ }
+ }
+
+ // 同步数据到yonyou
+ $ret = plugin_sync_api('postDelivery', ['master' => $master, 'rows' => $rows]);
+ if ($ret['success'] == true) {
+ return $params;
+ }
+ abort_error($ret['msg']);
+ }
+
+ public function onBeforeAbort($params) {
+ $id = $params['id'];
+ $master = DB::table('stock_delivery')->where('id', $id)->first();
+ // 检查用友单据是否存在
+ $ret = plugin_sync_api('getVouchExist', ['table' => 'DispatchList', 'field' => 'cDLCode', 'value' => $master['sn']]);
+ if ($ret['msg'] > 0) {
+ abort_error('用友存在发货单['.$master['sn'].']无法弃审。');
+ }
+ return $params;
+ }
+
+ public function onAfterStore($params) {
+ return $params;
+ }
+
+ public function onBeforeDelete($params) {
+ return $params;
+ }
+
+}
diff --git a/app/Gdoo/Stock/Hooks/DirectHook.php b/app/Gdoo/Stock/Hooks/DirectHook.php
new file mode 100644
index 00000000..6b6f1606
--- /dev/null
+++ b/app/Gdoo/Stock/Hooks/DirectHook.php
@@ -0,0 +1,149 @@
+find($master['warehouse_id']);
+
+ $datas = $params['datas'];
+ foreach($datas as $data) {
+ if ($data['table'] == 'stock_direct_data') {
+ foreach($data['data'] as $row) {
+ if ($row['product_id'] == '20226') {
+ continue;
+ }
+ // 检查库存
+ $exec = StockService::verfyInvoiceBatch($master['warehouse_id'], $row['product_id'], $row['batch_sn'], $row['poscode'], 0, 0, $row['id']);
+ if ($exec[0]['ky_num'] < $row['quantity']) {
+ $error = [];
+ $error[] = '存货编码为:'.$row['product_code'];
+ $error[] = '仓库名称为:'.$warehouse['name'];
+ $error[] = '批次为:'.$row['batch_sn'];
+ $error[] = '货位为:'.$row['poscode'];
+ $error[] = '发货数量:'.$row['quantity'];
+ $error[] = '可用量为:'.$exec[0]['ky_num'];
+ abort_error(join(" ", $error));
+ }
+ }
+ }
+ }
+ return $params;
+ }
+
+ public function onBeforeAudit($params) {
+ $id = $params['id'];
+ $master = DB::table('stock_direct')
+ ->leftJoin('customer', 'customer.id', '=', 'stock_direct.customer_id')
+ ->leftJoin('customer_tax', 'customer_tax.id', '=', 'stock_direct.tax_id')
+ ->leftJoin('customer_region', 'customer_region.id', '=', 'customer.region_id')
+ ->leftJoin('department', 'department.id', '=', 'customer_tax.department_id')
+ ->leftJoin('warehouse', 'warehouse.id', '=', 'stock_direct.warehouse_id')
+ ->leftJoin('sale_type', 'sale_type.id', '=', 'stock_direct.type_id')
+ ->where('stock_direct.id', $id)
+ ->first([
+ 'stock_direct.*',
+ 'sale_type.code as sale_code',
+ 'department.code as department_code',
+ 'customer_tax.code as customer_code',
+ 'warehouse.code as warehouse_code',
+ 'customer.region_id',
+ 'customer_region.owner_user_id as salesman_id',
+ 'customer.region2_id',
+ 'customer.region3_id'
+ ]);
+
+ $sql = DB::table('stock_direct_data')
+ ->leftJoin('product', 'product.id', '=', 'stock_direct_data.product_id')
+ ->where('stock_direct_data.direct_id', $id)
+ ->where('product.code', '<>', '99001')
+ ->selectRaw('
+ stock_direct_data.id,
+ stock_direct_data.type_id,
+ stock_direct_data.price,
+ stock_direct_data.quantity,
+ stock_direct_data.money,
+ stock_direct_data.other_money,
+ stock_direct_data.batch_sn,
+ stock_direct_data.poscode,
+ stock_direct_data.remark,
+ stock_direct_data.product_id,
+ product.code as product_code,
+ product.name as product_name,
+ stock_direct_data.total_weight
+ ');
+
+ $rows = DB::table('stock_direct_data')
+ ->leftJoin('product', 'product.id', '=', 'stock_direct_data.product_id')
+ ->where('stock_direct_data.direct_id', $id)
+ ->where('product.code', '99001')
+ ->selectRaw('
+ max(stock_direct_data.id) as id,
+ null as type_id,
+ null as price,
+ null as quantity,
+ sum(stock_direct_data.money) as money,
+ sum(stock_direct_data.other_money) as other_money,
+ stock_direct_data.batch_sn,
+ stock_direct_data.poscode,
+ stock_direct_data.remark,
+ null as product_id,
+ product.code as product_code,
+ product.name as product_name,
+ null as total_weight
+ ')
+ ->groupBy('product.name', 'product.code', 'stock_direct_data.batch_sn', 'stock_direct_data.poscode', 'stock_direct_data.remark')
+ ->union($sql)->get();
+
+ // 检查库存
+ foreach($rows as $row) {
+ if ($row['product_code'] == '99001') {
+ continue;
+ }
+ $exec = StockService::verfyInvoiceBatch($master['warehouse_id'], $row['product_id'], $row['batch_sn'], $row['poscode'], 0, 0, $row['id']);
+ if ($exec[0]['ky_num'] < $row['quantity']) {
+ abort_error('存货编码为['.$row['product_code'].']的存货库存不足。');
+ }
+ }
+
+ // 同步数据到yonyou
+ $ret = plugin_sync_api('postDeliveryZY', ['master' => $master, 'rows' => $rows]);
+ if ($ret['success'] == true) {
+ return $params;
+ }
+ abort_error($ret['msg']);
+ }
+
+ public function onBeforeAbort($params) {
+ $id = $params['id'];
+ $master = DB::table('stock_direct')->where('id', $id)->first();
+ // 检查用友单据是否存在
+ $ret = plugin_sync_api('getVouchExist', ['table' => 'DispatchList', 'field' => 'cDLCode', 'value' => $master['sn']]);
+ if ($ret['msg'] > 0) {
+ abort_error('用友存在发货单(直营)['.$master['sn'].']无法弃审。');
+ }
+ return $params;
+ }
+
+ public function onAfterStore($params) {
+ return $params;
+ }
+
+ public function onBeforeDelete($params) {
+ return $params;
+ }
+}
diff --git a/app/Gdoo/Stock/Hooks/Record01Hook.php b/app/Gdoo/Stock/Hooks/Record01Hook.php
new file mode 100644
index 00000000..e25c488d
--- /dev/null
+++ b/app/Gdoo/Stock/Hooks/Record01Hook.php
@@ -0,0 +1,67 @@
+leftJoin('warehouse', 'warehouse.id', '=', 'stock_record01.warehouse_id')
+ ->leftJoin('department', 'department.id', '=', 'stock_record01.department_id')
+ ->leftJoin('stock_type', 'stock_type.id', '=', 'stock_record01.type_id')
+ ->leftJoin('supplier', 'supplier.id', '=', 'stock_record01.supplier_id')
+ ->where('stock_record01.id', $id)
+ ->first([
+ 'stock_record01.*',
+ 'department.code as department_code',
+ 'stock_type.code as type_code',
+ 'supplier.code as supplier_code',
+ 'warehouse.code as warehouse_code',
+ ]);
+
+ $rows = DB::table('stock_record01_data')
+ ->leftJoin('product', 'product.id', '=', 'stock_record01_data.product_id')
+ ->where('stock_record01_data.record01_id', $id)
+ ->get(['stock_record01_data.*', 'product.code as product_code']);
+ // 同步数据到yonyou
+ $ret = plugin_sync_api('postRecord01', ['master' => $master, 'rows' => $rows]);
+ if ($ret['success'] == true) {
+ return $params;
+ }
+ abort_error($ret['msg']);
+ }
+
+ public function onBeforeAbort($params) {
+ $id = $params['id'];
+ $master = DB::table('stock_record01')->where('id', $id)->first();
+ // 检查用友单据是否存在
+ $ret = plugin_sync_api('getVouchExist', ['table' => 'Rdrecord01', 'field' => 'cCode', 'value' => $master['sn']]);
+ if ($ret['msg'] > 0) {
+ abort_error('用友存在采购单['.$master['sn'].']无法弃审。');
+ }
+ return $params;
+ }
+
+ public function onAfterStore($params) {
+ return $params;
+ }
+
+ public function onBeforeDelete($params) {
+ return $params;
+ }
+
+}
diff --git a/app/Gdoo/Stock/Hooks/Record08Hook.php b/app/Gdoo/Stock/Hooks/Record08Hook.php
new file mode 100644
index 00000000..b3ecefe9
--- /dev/null
+++ b/app/Gdoo/Stock/Hooks/Record08Hook.php
@@ -0,0 +1,76 @@
+ $data) {
+ if ($data['table'] == 'stock_record08_data') {
+ foreach($data['data'] as $j => $row) {
+ if ($row['batch_sn']) {
+ $batch_sn = substr($row['batch_sn'], 0, 6);
+ $sn = str_split($batch_sn, 2);
+ $row['batch_date'] = date("Y-m-d", mktime(0, 0, 0, $sn[1], $sn[2], $sn[0]));
+ }
+ $data['data'][$j] = $row;
+ }
+ $datas[$i] = $data;
+ }
+ }
+ $params['datas'] = $datas;
+ return $params;
+ }
+
+ public function onBeforeAudit($params) {
+ $id = $params['id'];
+ $master = DB::table('stock_record08')
+ ->leftJoin('warehouse', 'warehouse.id', '=', 'stock_record08.warehouse_id')
+ ->leftJoin('department', 'department.id', '=', 'stock_record08.department_id')
+ ->leftJoin('stock_type', 'stock_type.id', '=', 'stock_record08.type_id')
+ ->where('stock_record08.id', $id)
+ ->first(['stock_record08.*', 'stock_type.code as type_code', 'department.code as department_code', 'warehouse.code as warehouse_code']);
+
+ $rows = DB::table('stock_record08_data')
+ ->leftJoin('product', 'product.id', '=', 'stock_record08_data.product_id')
+ ->where('stock_record08_data.record08_id', $id)
+ ->get(['stock_record08_data.*', 'product.code as product_code']);
+ // 同步数据到yonyou
+ $ret = plugin_sync_api('postRecord08', ['master' => $master, 'rows' => $rows]);
+ if ($ret['success'] == true) {
+ return $params;
+ }
+ abort_error($ret['msg']);
+ }
+
+ public function onBeforeAbort($params) {
+ $id = $params['id'];
+ $master = DB::table('stock_record08')->where('id', $id)->first();
+ // 检查用友单据是否存在
+ $ret = plugin_sync_api('getVouchExist', ['table' => 'Rdrecord08', 'field' => 'cCode', 'value' => $master['sn']]);
+ if ($ret['msg'] > 0) {
+ abort_error('用友存在其他入库单['.$master['sn'].']无法弃审。');
+ }
+ return $params;
+ }
+
+ public function onAfterStore($params) {
+ return $params;
+ }
+
+ public function onBeforeDelete($params) {
+ return $params;
+ }
+
+}
diff --git a/app/Gdoo/Stock/Hooks/Record09Hook.php b/app/Gdoo/Stock/Hooks/Record09Hook.php
new file mode 100644
index 00000000..705d58e8
--- /dev/null
+++ b/app/Gdoo/Stock/Hooks/Record09Hook.php
@@ -0,0 +1,114 @@
+find($master['warehouse_id']);
+
+ $datas = $params['datas'];
+ foreach($datas as $data) {
+ if ($data['table'] == 'stock_record09_data') {
+ foreach($data['data'] as $row) {
+ // 检查库存
+ $exec = StockService::verfyInvoiceBatch($master['warehouse_id'], $row['product_id'], $row['batch_sn'], $row['poscode'], 0, $row['id'], 0);
+ if ($exec[0]['ky_num'] < $row['quantity']) {
+ $error = [];
+ $error[] = '存货编码为:'.$row['product_code'];
+ $error[] = '仓库名称为:'.$warehouse['name'];
+ $error[] = '批次为:'.$row['batch_sn'];
+ $error[] = '货位为:'.$row['poscode'];
+ $error[] = '发货数量:'.$row['quantity'];
+ $error[] = '可用量为:'.$exec[0]['ky_num'];
+ abort_error(join(" ", $error));
+ }
+ }
+ }
+ }
+ return $params;
+ }
+
+ public function onBeforeAudit($params) {
+ $id = $params['id'];
+
+ $master = DB::table('stock_record09')
+ ->leftJoin('warehouse', 'warehouse.id', '=', 'stock_record09.warehouse_id')
+ ->leftJoin('department', 'department.id', '=', 'stock_record09.department_id')
+ ->leftJoin('stock_type', 'stock_type.id', '=', 'stock_record09.type_id')
+ ->where('stock_record09.id', $id)
+ ->first(['stock_record09.*', 'stock_type.code as type_code', 'department.code as department_code', 'warehouse.code as warehouse_code']);
+
+ $rows = DB::table('stock_record09_data')
+ ->leftJoin('product', 'product.id', '=', 'stock_record09_data.product_id')
+ ->where('stock_record09_data.record09_id', $id)
+ ->selectRaw('
+ stock_record09_data.*,
+ product.code as product_code,
+ product.weight * stock_record09_data.quantity as total_weight
+ ')
+ ->get();
+ $master['total_weight'] = $rows->sum('total_weight');
+
+ // 检查库存
+ foreach($rows as $row) {
+ $exec = StockService::verfyInvoiceBatch($master['warehouse_id'], $row['product_id'], $row['batch_sn'], $row['poscode'], 0, $row['id'], 0);
+ if ($exec[0]['ky_num'] < $row['quantity']) {
+ abort_error('存货编码为['.$row['product_code'].']的存货库存不足。');
+ }
+ }
+
+ if ($master['type_id'] == 2) {
+ $post_type = 'postSampleDelivery';
+ } else {
+ $post_type = 'postRecord09';
+ }
+ // 同步数据到yonyou
+ $ret = plugin_sync_api($post_type, ['master' => $master, 'rows' => $rows]);
+ if ($ret['success'] == true) {
+ return $params;
+ }
+ abort_error($ret['msg']);
+ }
+
+ public function onBeforeAbort($params) {
+ $id = $params['id'];
+
+ $master = DB::table('stock_record09')->where('id', $id)->first();
+ // 检查用友单据是否存在
+ if ($master['type_id'] == 2) {
+ $ret = plugin_sync_api('getVouchExist', ['table' => 'DispatchList', 'field' => 'cDLCode', 'value' => $master['sn']]);
+ if ($ret['msg'] > 0) {
+ abort_error('用友存在样品申请单['.$master['sn'].']无法弃审。');
+ }
+ } else {
+ $ret = plugin_sync_api('getVouchExist', ['table' => 'Rdrecord09', 'field' => 'cCode', 'value' => $master['sn']]);
+ if ($ret['msg'] > 0) {
+ abort_error('用友存在其他出库单['.$master['sn'].']无法弃审。');
+ }
+ }
+ return $params;
+ }
+
+ public function onAfterStore($params) {
+ return $params;
+ }
+
+ public function onBeforeDelete($params) {
+ return $params;
+ }
+
+}
diff --git a/app/Gdoo/Stock/Hooks/Record10DataHook.php b/app/Gdoo/Stock/Hooks/Record10DataHook.php
new file mode 100644
index 00000000..0eacb71c
--- /dev/null
+++ b/app/Gdoo/Stock/Hooks/Record10DataHook.php
@@ -0,0 +1,21 @@
+orderBy('stock_record10_data.batch_sn', 'asc')
+ ->orderBy('product_id_product.code', 'asc');
+
+ $params['q'] = $q;
+ return $params;
+ }
+
+ public function onAfterForm($arguments) {
+ return $arguments;
+ }
+}
diff --git a/app/Gdoo/Stock/Hooks/Record10Hook.php b/app/Gdoo/Stock/Hooks/Record10Hook.php
new file mode 100644
index 00000000..c1489705
--- /dev/null
+++ b/app/Gdoo/Stock/Hooks/Record10Hook.php
@@ -0,0 +1,94 @@
+id() == 2177) {
+ $params['rule'] = $params['rule'].'11';
+ } else {
+ $params['rule'] = $params['rule'].'10';
+ }
+ return $params;
+ }
+
+ public function onBeforePage($params) {
+ // 川南库管登录
+ if (auth()->id() == 2177) {
+ $params['q']->whereIn('stock_record10.warehouse_id', [20001, 20047]);
+ } else {
+ $params['q']->whereNotIn('stock_record10.warehouse_id', [20001, 20047]);
+ }
+ return $params;
+ }
+
+ public function onBeforeStore($params) {
+ $datas = $params['datas'];
+ // 处理生产日期
+ foreach($datas as $i => $data) {
+ if ($data['table'] == 'stock_record10_data') {
+ foreach($data['data'] as $j => $row) {
+ if ($row['batch_sn']) {
+ $batch_sn = substr($row['batch_sn'], 0, 6);
+ $sn = str_split($batch_sn, 2);
+ $row['batch_date'] = date("Y-m-d", mktime(0, 0, 0, $sn[1], $sn[2], $sn[0]));
+ }
+ $data['data'][$j] = $row;
+ }
+ $datas[$i] = $data;
+ }
+ }
+ $params['datas'] = $datas;
+ return $params;
+ }
+
+ public function onBeforeAudit($params) {
+ $id = $params['id'];
+
+ $master = DB::table('stock_record10')
+ ->leftJoin('warehouse', 'warehouse.id', '=', 'stock_record10.warehouse_id')
+ ->leftJoin('department', 'department.id', '=', 'stock_record10.department_id')
+ ->leftJoin('stock_type', 'stock_type.id', '=', 'stock_record10.type_id')
+ ->where('stock_record10.id', $id)
+ ->first(['stock_record10.*', 'stock_type.code as type_code', 'department.code as department_code', 'warehouse.code as warehouse_code']);
+
+ $rows = DB::table('stock_record10_data')
+ ->leftJoin('product', 'product.id', '=', 'stock_record10_data.product_id')
+ ->where('stock_record10_data.record10_id', $id)
+ ->get(['stock_record10_data.*', 'product.code as product_code']);
+ // 同步数据到yonyou
+ $ret = plugin_sync_api('postRecord10', ['master' => $master, 'rows' => $rows]);
+ if ($ret['success'] == true) {
+ return $params;
+ }
+
+ abort_error($ret['msg']);
+ }
+
+ public function onBeforeAbort($params) {
+ $id = $params['id'];
+ $master = DB::table('stock_record10')->where('id', $id)->first();
+ // 检查用友单据是否存在
+ $ret = plugin_sync_api('getVouchExist', ['table' => 'Rdrecord10', 'field' => 'cCode', 'value' => $master['sn']]);
+ if ($ret['msg'] > 0) {
+ abort_error('用友存在产成品入库单['.$master['sn'].']无法弃审。');
+ }
+ return $params;
+ }
+
+ public function onAfterStore($params) {
+ return $params;
+ }
+
+ public function onBeforeDelete($params) {
+ return $params;
+ }
+
+}
diff --git a/app/Gdoo/Stock/Hooks/Record11Hook.php b/app/Gdoo/Stock/Hooks/Record11Hook.php
new file mode 100644
index 00000000..b1ef637d
--- /dev/null
+++ b/app/Gdoo/Stock/Hooks/Record11Hook.php
@@ -0,0 +1,77 @@
+ $data) {
+ if ($data['table'] == 'stock_record11_data') {
+ foreach($data['data'] as $j => $row) {
+ if ($row['batch_sn']) {
+ $batch_sn = substr($row['batch_sn'], 0, 6);
+ $sn = str_split($batch_sn, 2);
+ $row['batch_date'] = date("Y-m-d", mktime(0, 0, 0, $sn[1], $sn[2], $sn[0]));
+ }
+ $data['data'][$j] = $row;
+ }
+ $datas[$i] = $data;
+ }
+ }
+ $params['datas'] = $datas;
+ return $params;
+ }
+
+ public function onBeforeAudit($params) {
+ $id = $params['id'];
+ $master = DB::table('stock_record11')
+ ->leftJoin('warehouse', 'warehouse.id', '=', 'stock_record11.warehouse_id')
+ ->leftJoin('department', 'department.id', '=', 'stock_record11.department_id')
+ ->leftJoin('stock_type', 'stock_type.id', '=', 'stock_record11.category_id')
+ ->where('stock_record11.id', $id)
+ ->first(['stock_record11.*', 'stock_type.code as type_code', 'department.code as department_code', 'warehouse.code as warehouse_code']);
+
+ $rows = DB::table('stock_record11_data')
+ ->leftJoin('product', 'product.id', '=', 'stock_record11_data.product_id')
+ ->where('stock_record11_data.record11_id', $id)
+ ->get(['stock_record11_data.*', 'product.code as product_code']);
+ // 同步数据到yonyou
+ $ret = plugin_sync_api('postRecord11', ['master' => $master, 'rows' => $rows]);
+
+ if ($ret['success'] == true) {
+ return $params;
+ }
+ abort_error($ret['msg']);
+ }
+
+ public function onBeforeAbort($params) {
+ $id = $params['id'];
+ $master = DB::table('stock_record11')->where('id', $id)->first();
+ // 检查用友单据是否存在
+ $ret = plugin_sync_api('getVouchExist', ['table' => 'Rdrecord11', 'field' => 'cCode', 'value' => $master['sn']]);
+ if ($ret['msg'] > 0) {
+ abort_error('用友存在原材料出库单['.$master['sn'].']无法弃审。');
+ }
+ return $params;
+ }
+
+ public function onAfterStore($params) {
+ return $params;
+ }
+
+ public function onBeforeDelete($params) {
+ return $params;
+ }
+
+}
diff --git a/app/Gdoo/Stock/Models/Allocation.php b/app/Gdoo/Stock/Models/Allocation.php
new file mode 100644
index 00000000..a19e875e
--- /dev/null
+++ b/app/Gdoo/Stock/Models/Allocation.php
@@ -0,0 +1,35 @@
+ 'tab',
+ 'items' => [
+ ['value' => 'allocation', 'url' => 'stock/allocation/index', 'name' => '产成品调拨单'],
+ ]
+ ];
+
+ public static $bys = [
+ 'name' => 'by',
+ 'items' => [
+ ['value' => '', 'name' => '全部'],
+ ['value' => 'enabled', 'name' => '启用'],
+ ['value' => 'disabled', 'name' => '禁用'],
+ ['value' => 'divider'],
+ ['value' => 'day', 'name' => '今日创建'],
+ ['value' => 'week', 'name' => '本周创建'],
+ ['value' => 'month', 'name' => '本月创建'],
+ ]
+ ];
+
+ public function warehouse($query)
+ {
+ return $this->belongsTo('Gdoo\Stock\Models\Warehouse');
+ }
+
+}
diff --git a/app/Gdoo/Stock/Models/Cancel.php b/app/Gdoo/Stock/Models/Cancel.php
new file mode 100644
index 00000000..1df6e0fc
--- /dev/null
+++ b/app/Gdoo/Stock/Models/Cancel.php
@@ -0,0 +1,35 @@
+ 'tab',
+ 'items' => [
+ ['value' => 'cancel', 'url' => 'stock/cancel/index', 'name' => '退货申请'],
+ ]
+ ];
+
+ public static $bys = [
+ 'name' => 'by',
+ 'items' => [
+ ['value' => '', 'name' => '全部'],
+ ['value' => 'enabled', 'name' => '启用'],
+ ['value' => 'disabled', 'name' => '禁用'],
+ ['value' => 'divider'],
+ ['value' => 'day', 'name' => '今日创建'],
+ ['value' => 'week', 'name' => '本周创建'],
+ ['value' => 'month', 'name' => '本月创建'],
+ ]
+ ];
+
+ public function warehouse($query)
+ {
+ return $this->belongsTo('Gdoo\Stock\Models\Warehouse');
+ }
+
+}
diff --git a/app/Gdoo/Stock/Models/Delivery.php b/app/Gdoo/Stock/Models/Delivery.php
new file mode 100644
index 00000000..6a6f7c73
--- /dev/null
+++ b/app/Gdoo/Stock/Models/Delivery.php
@@ -0,0 +1,42 @@
+ 'tab',
+ 'items' => [
+ ['value' => 'delivery', 'url' => 'stock/delivery/index', 'name' => '发货单'],
+ ]
+ ];
+
+ public static $tabs2 = [
+ 'name' => 'tab',
+ 'items' => [
+ ['value' => 'delivery', 'url' => 'stock/delivery/detail', 'name' => '发货明细'],
+ ]
+ ];
+
+ public static $bys = [
+ 'name' => 'by',
+ 'items' => [
+ ['value' => '', 'name' => '全部'],
+ ['value' => 'enabled', 'name' => '启用'],
+ ['value' => 'disabled', 'name' => '禁用'],
+ ['value' => 'divider'],
+ ['value' => 'day', 'name' => '今日创建'],
+ ['value' => 'week', 'name' => '本周创建'],
+ ['value' => 'month', 'name' => '本月创建'],
+ ]
+ ];
+
+ public function warehouse($query)
+ {
+ return $this->belongsTo('Gdoo\Stock\Models\Warehouse');
+ }
+
+}
diff --git a/app/Gdoo/Stock/Models/Direct.php b/app/Gdoo/Stock/Models/Direct.php
new file mode 100644
index 00000000..0d22ed3f
--- /dev/null
+++ b/app/Gdoo/Stock/Models/Direct.php
@@ -0,0 +1,35 @@
+ 'tab',
+ 'items' => [
+ ['value' => 'direct', 'url' => 'stock/direct/index', 'name' => '发货单(直营)'],
+ ]
+ ];
+
+ public static $bys = [
+ 'name' => 'by',
+ 'items' => [
+ ['value' => '', 'name' => '全部'],
+ ['value' => 'enabled', 'name' => '启用'],
+ ['value' => 'disabled', 'name' => '禁用'],
+ ['value' => 'divider'],
+ ['value' => 'day', 'name' => '今日创建'],
+ ['value' => 'week', 'name' => '本周创建'],
+ ['value' => 'month', 'name' => '本月创建'],
+ ]
+ ];
+
+ public function warehouse($query)
+ {
+ return $this->belongsTo('Gdoo\Stock\Models\Warehouse');
+ }
+
+}
diff --git a/app/Gdoo/Stock/Models/Record01.php b/app/Gdoo/Stock/Models/Record01.php
new file mode 100644
index 00000000..100df16d
--- /dev/null
+++ b/app/Gdoo/Stock/Models/Record01.php
@@ -0,0 +1,35 @@
+ 'tab',
+ 'items' => [
+ ['value' => 'record01', 'url' => 'stock/record01/index', 'name' => '采购入库单'],
+ ]
+ ];
+
+ public static $bys = [
+ 'name' => 'by',
+ 'items' => [
+ ['value' => '', 'name' => '全部'],
+ ['value' => 'enabled', 'name' => '启用'],
+ ['value' => 'disabled', 'name' => '禁用'],
+ ['value' => 'divider'],
+ ['value' => 'day', 'name' => '今日创建'],
+ ['value' => 'week', 'name' => '本周创建'],
+ ['value' => 'month', 'name' => '本月创建'],
+ ]
+ ];
+
+ public function warehouse($query)
+ {
+ return $this->belongsTo('Gdoo\Stock\Models\Warehouse');
+ }
+
+}
diff --git a/app/Gdoo/Stock/Models/Record08.php b/app/Gdoo/Stock/Models/Record08.php
new file mode 100644
index 00000000..80f06805
--- /dev/null
+++ b/app/Gdoo/Stock/Models/Record08.php
@@ -0,0 +1,35 @@
+ 'tab',
+ 'items' => [
+ ['value' => 'record08', 'url' => 'stock/record08/index', 'name' => '其他入库单'],
+ ]
+ ];
+
+ public static $bys = [
+ 'name' => 'by',
+ 'items' => [
+ ['value' => '', 'name' => '全部'],
+ ['value' => 'enabled', 'name' => '启用'],
+ ['value' => 'disabled', 'name' => '禁用'],
+ ['value' => 'divider'],
+ ['value' => 'day', 'name' => '今日创建'],
+ ['value' => 'week', 'name' => '本周创建'],
+ ['value' => 'month', 'name' => '本月创建'],
+ ]
+ ];
+
+ public function warehouse($query)
+ {
+ return $this->belongsTo('Gdoo\Stock\Models\Warehouse');
+ }
+
+}
diff --git a/app/Gdoo/Stock/Models/Record09.php b/app/Gdoo/Stock/Models/Record09.php
new file mode 100644
index 00000000..678d98a3
--- /dev/null
+++ b/app/Gdoo/Stock/Models/Record09.php
@@ -0,0 +1,35 @@
+ 'tab',
+ 'items' => [
+ ['value' => 'record09', 'url' => 'stock/record09/index', 'name' => '其他出库单'],
+ ]
+ ];
+
+ public static $bys = [
+ 'name' => 'by',
+ 'items' => [
+ ['value' => '', 'name' => '全部'],
+ ['value' => 'enabled', 'name' => '启用'],
+ ['value' => 'disabled', 'name' => '禁用'],
+ ['value' => 'divider'],
+ ['value' => 'day', 'name' => '今日创建'],
+ ['value' => 'week', 'name' => '本周创建'],
+ ['value' => 'month', 'name' => '本月创建'],
+ ]
+ ];
+
+ public function warehouse($query)
+ {
+ return $this->belongsTo('Gdoo\Stock\Models\Warehouse');
+ }
+
+}
diff --git a/app/Gdoo/Stock/Models/Record10.php b/app/Gdoo/Stock/Models/Record10.php
new file mode 100644
index 00000000..6ce47f34
--- /dev/null
+++ b/app/Gdoo/Stock/Models/Record10.php
@@ -0,0 +1,35 @@
+ 'tab',
+ 'items' => [
+ ['value' => 'record10', 'url' => 'stock/record10/index', 'name' => '产成品入库单'],
+ ]
+ ];
+
+ public static $bys = [
+ 'name' => 'by',
+ 'items' => [
+ ['value' => '', 'name' => '全部'],
+ ['value' => 'enabled', 'name' => '启用'],
+ ['value' => 'disabled', 'name' => '禁用'],
+ ['value' => 'divider'],
+ ['value' => 'day', 'name' => '今日创建'],
+ ['value' => 'week', 'name' => '本周创建'],
+ ['value' => 'month', 'name' => '本月创建'],
+ ]
+ ];
+
+ public function warehouse($query)
+ {
+ return $this->belongsTo('Gdoo\Stock\Models\Warehouse');
+ }
+
+}
diff --git a/app/Gdoo/Stock/Models/Record11.php b/app/Gdoo/Stock/Models/Record11.php
new file mode 100644
index 00000000..7ab2575e
--- /dev/null
+++ b/app/Gdoo/Stock/Models/Record11.php
@@ -0,0 +1,35 @@
+ 'tab',
+ 'items' => [
+ ['value' => 'record11', 'url' => 'stock/record11/index', 'name' => '原材料出库单'],
+ ]
+ ];
+
+ public static $bys = [
+ 'name' => 'by',
+ 'items' => [
+ ['value' => '', 'name' => '全部'],
+ ['value' => 'enabled', 'name' => '启用'],
+ ['value' => 'disabled', 'name' => '禁用'],
+ ['value' => 'divider'],
+ ['value' => 'day', 'name' => '今日创建'],
+ ['value' => 'week', 'name' => '本周创建'],
+ ['value' => 'month', 'name' => '本月创建'],
+ ]
+ ];
+
+ public function warehouse()
+ {
+ return $this->belongsTo('Gdoo\Stock\Models\Warehouse');
+ }
+
+}
diff --git a/app/Gdoo/Stock/Models/StockCategory.php b/app/Gdoo/Stock/Models/StockCategory.php
new file mode 100644
index 00000000..d391b57e
--- /dev/null
+++ b/app/Gdoo/Stock/Models/StockCategory.php
@@ -0,0 +1,28 @@
+ 'tab',
+ 'items' => [
+ ['value' => 'category', 'url' => 'stock/category/index', 'name' => '库存类别'],
+ ]
+ ];
+
+ public static $bys = [
+ 'name' => 'by',
+ 'items' => [
+ ['value' => '', 'name' => '全部'],
+ ['value' => 'enabled', 'name' => '启用'],
+ ['value' => 'disabled', 'name' => '禁用'],
+ ['value' => 'divider'],
+ ['value' => 'day', 'name' => '今日创建'],
+ ['value' => 'week', 'name' => '本周创建'],
+ ['value' => 'month', 'name' => '本月创建'],
+ ]
+ ];
+}
diff --git a/app/Gdoo/Stock/Models/StockType.php b/app/Gdoo/Stock/Models/StockType.php
new file mode 100644
index 00000000..614fb8a8
--- /dev/null
+++ b/app/Gdoo/Stock/Models/StockType.php
@@ -0,0 +1,28 @@
+ 'tab',
+ 'items' => [
+ ['value' => 'type', 'url' => 'stock/type/index', 'name' => '销售类型'],
+ ]
+ ];
+
+ public static $bys = [
+ 'name' => 'by',
+ 'items' => [
+ ['value' => '', 'name' => '全部'],
+ ['value' => 'enabled', 'name' => '启用'],
+ ['value' => 'disabled', 'name' => '禁用'],
+ ['value' => 'divider'],
+ ['value' => 'day', 'name' => '今日创建'],
+ ['value' => 'week', 'name' => '本周创建'],
+ ['value' => 'month', 'name' => '本月创建'],
+ ]
+ ];
+}
diff --git a/app/Gdoo/Stock/Models/Warehouse.php b/app/Gdoo/Stock/Models/Warehouse.php
new file mode 100644
index 00000000..2996e1f8
--- /dev/null
+++ b/app/Gdoo/Stock/Models/Warehouse.php
@@ -0,0 +1,32 @@
+ 'tab',
+ 'items' => [
+ ['value' => 'stock', 'url' => 'stock/warehouse/index', 'name' => '仓库档案'],
+ ]
+ ];
+
+ public static $bys = [
+ 'name' => 'by',
+ 'items' => [
+ ['value' => '', 'name' => '全部'],
+ ['value' => 'divider'],
+ ['value' => 'day', 'name' => '今日创建'],
+ ['value' => 'week', 'name' => '本周创建'],
+ ['value' => 'month', 'name' => '本月创建'],
+ ]
+ ];
+
+ public function user()
+ {
+ return $this->belongsTo('Gdoo\User\Models\User');
+ }
+
+}
diff --git a/app/Gdoo/Stock/Models/WarehouseLocation.php b/app/Gdoo/Stock/Models/WarehouseLocation.php
new file mode 100644
index 00000000..60f95cf5
--- /dev/null
+++ b/app/Gdoo/Stock/Models/WarehouseLocation.php
@@ -0,0 +1,26 @@
+ 'tab',
+ 'items' => [
+ ['value' => 'location', 'url' => 'stock/location/index', 'name' => '仓库货位'],
+ ]
+ ];
+
+ public static $bys = [
+ 'name' => 'by',
+ 'items' => [
+ ['value' => '', 'name' => '全部'],
+ ['value' => 'divider'],
+ ['value' => 'day', 'name' => '今日创建'],
+ ['value' => 'week', 'name' => '本周创建'],
+ ['value' => 'month', 'name' => '本月创建'],
+ ]
+ ];
+}
diff --git a/app/Gdoo/Stock/Services/StockService.php b/app/Gdoo/Stock/Services/StockService.php
new file mode 100644
index 00000000..4251cdef
--- /dev/null
+++ b/app/Gdoo/Stock/Services/StockService.php
@@ -0,0 +1,1621 @@
+ 0) {
+ $count = DB::table('customer')
+ ->where('id', $customer_id)
+ ->whereRaw("RIGHT(name, 1) = 'A'")
+ ->count();
+ }
+
+ $model = DB::query()->selectRaw('* FROM('.static::getStockSelectSql().') ss')
+ ->whereRaw("ISNULL(ky_num, 0) > 0");
+
+ // 获取产品id
+ if ($product_ids) {
+ $model->whereIn('product_id', explode(',', $product_ids));
+ }
+
+ if ($value) {
+ $v = join("','", explode(',', $value));
+ $model->whereRaw("(
+ product_code LIKE '%{$value}%'
+ OR product_name LIKE '%{$value}%'
+ OR batch_sn IN('{$v}')
+ )");
+ }
+
+ // 只取成品库和物料库,不包括成品待检验库
+ $model->whereRaw("
+ (warehouse_name LIKE '%成品%' or warehouse_code LIKE '07')
+ and warehouse_code <> '25' -- 成品待检验库
+ ");
+
+ // 取仓库
+ if ($warehouse_id > 0) {
+ $model->where("warehouse_id", $warehouse_id);
+ }
+
+ // 是否取不满件
+ if ($count > 0) {
+ $model->whereRaw("
+ warehouse_code <> '28' -- 味聚特辣酱不满件库
+ and warehouse_code <> '23' -- 成品泡菜不满件库
+ ");
+ }
+
+ $model->orderByRaw('warehouse_code asc, product_code ASC, batch_sn ASC');
+ return $model->get();
+ }
+
+ /**
+ * 商品批次选择
+ *
+ * @warehouse_id 仓库ID
+ * @product_ids 存货档案ID
+ * @value 存货档案编码, 存货档案名称、批次、地区标
+ * @customer_id 客户ID
+ */
+ public static function getBatchSelectAll($warehouse_id = 0, $product_id = 0, $value = '', $customer_id = 0)
+ {
+ $model = DB::query()->selectRaw('* FROM('.static::getStockSelectSql().') ss')
+ ->whereRaw("ISNULL(ky_num, 0) > 0");
+
+ // 获取产品id
+ if ($product_id) {
+ $model->where('product_id', $product_id);
+ }
+
+ if ($value) {
+ $model->whereRaw("(
+ product_code LIKE '%{$value}%'
+ OR product_name LIKE '%{$value}%'
+ OR batch_sn LIKE '%{$value}%'
+ )");
+ }
+
+ // 取仓库
+ if ($warehouse_id > 0) {
+ $model->where("warehouse_id", $warehouse_id);
+ }
+
+ $model->orderByRaw('warehouse_code asc, product_code ASC, batch_sn ASC');
+ return $model->get();
+ }
+
+ /**
+ * 商品批次选择(直营)
+ *
+ * @warehouse_id 仓库ID
+ * @product_ids 存货档案ID
+ * @value 存货档案编码, 存货档案名称、批次、地区标
+ * @customer_id 客户ID
+ */
+ public static function getBatchSelectZY($warehouse_id = 0, $product_ids = '', $value = '')
+ {
+ $model = DB::query()->selectRaw('* FROM('.static::getStockSelectSql().') ss')
+ ->whereRaw("ISNULL(ky_num, 0) > 0");
+
+ // 获取产品id
+ if ($product_ids) {
+ $model->whereIn('product_id', explode(',', $product_ids));
+ }
+
+ if ($value) {
+ $v = join("','", explode(',', $value));
+ $model->whereRaw("(
+ product_code LIKE '%{$value}%'
+ OR product_name LIKE '%{$value}%'
+ OR batch_sn IN('{$v}')
+ )");
+ }
+
+ // 取仓库
+ if ($warehouse_id > 0) {
+ $model->where("warehouse_id", $warehouse_id);
+ }
+
+ $model->orderByRaw('product_code ASC, batch_sn');
+ return $model->get();
+ }
+
+ /**
+ * 进出存汇总表
+ *
+ * @warehouse_id 仓库ID
+ * @product_code 存货编码
+ * @NY 内销1 外贸2
+ * @made_start_dt 生产日期起始日期
+ * @made_end_dt 生产日期截至日期
+ * @user_id 用户id
+ * @SFPH 是否显示批号
+ * @HBBMJ 是否合并不满件
+ */
+ public static function reportOrderStockTotal($warehouse_id = 0, $product_code = '', $ny = '', $made_start_dt = '', $made_end_dt = '', $user_id = 0, $SFPH = 0, $HBBMJ = 0)
+ {
+ $warehouse_id = (int)$warehouse_id;
+ $SFPH = (int)$SFPH;
+ $HBBMJ = (int)$HBBMJ;
+
+ $sql = [];
+ $sql[] = "select warehouse_code,product_code,batch_sn,batch_date,poscode,posname,warehouse_name,
+ product_name,product_spec,unit_name,product_id,warehouse_id,SUM(Num) num,SUM(ky_Num) kynum,SUM(fh_Num) fhnum,SUM(Ck_Num) cknum,SUM(Rk_Num) rknum,SUM(Max_Num) maxnum
+ from (
+ SELECT case when {$HBBMJ}=1 AND warehouse_code='23' THEN '10'
+ when {$HBBMJ}=1 AND warehouse_code='27' THEN '21'
+ when {$HBBMJ}=1 AND warehouse_code='28' THEN '11' ELSE warehouse_code end AS warehouse_code,
+ rd.product_code,batch_sn,batch_date,
+ case when {$HBBMJ}=1 and poscode='91' THEN '99'
+ when {$HBBMJ}=1 and poscode='92' THEN '98'
+ when {$HBBMJ}=1 and poscode='93' THEN '98' ELSE poscode end AS poscode,
+ case when {$HBBMJ}=1 and posname='不满件' THEN '小菜'
+ when {$HBBMJ}=1 and posname='川南辣酱不满件' THEN '辣酱'
+ when {$HBBMJ}=1 and posname='味聚特辣酱不满件' THEN '辣酱' ELSE posname end as posname,
+ case when {$HBBMJ}=1 and warehouse_name='成品不满件库' THEN '成品库小菜'
+ when {$HBBMJ}=1 and warehouse_name='川南辣酱不满件库' THEN '川南酱库'
+ when {$HBBMJ}=1 and warehouse_name='味聚特辣酱不满件库' THEN '成品库酱' ELSE warehouse_name end AS warehouse_name, rd.product_name,rd.product_spec,rd.unit_name,rd.product_id,
+ CASE WHEN {$HBBMJ}=1 AND warehouse_id=20005 THEN 139
+ WHEN {$HBBMJ}=1 AND warehouse_id=20047 THEN 20001
+ WHEN {$HBBMJ}=1 AND warehouse_id=20048 THEN 140 ELSE warehouse_id end AS warehouse_id,Num,ky_Num,fh_Num,Ck_Num,Rk_Num,Max_Num
+ FROM (".StockService::getStockSelectSql().") rd
+ left join (
+ select i.product_id, i.product_name,i.product_spec,i.unit_name,invc.NY, i.product_code from (
+ select id, case substring(name, 0, 3) when '外销' then 2 else 1 end as NY
+ from product_category) as invc right join (
+ SELECT a.id as product_id, a.name as product_name,a.spec as product_spec, b.name as unit_name, a.code as product_code, a.category_id
+ FROM product a
+ LEFT JOIN product_unit b ON a.unit_id = b.id) i on i.category_id = invc.id
+ ) as ic on rd.product_id = ic.product_id
+ WHERE (ISNULL(ky_Num,0) <> 0 OR ISNULL(fh_Num,0) <> 0 or Ck_Num<>0 or Rk_Num<>0)";
+
+ if ($ny) {
+ $sql[] = "and (ic.NY = {$ny})";
+ }
+
+ if ($made_start_dt && $made_end_dt) {
+ $sql[] = "and (batch_date >= '$made_start_dt') and (batch_date <= '$made_end_dt')";
+ }
+
+ $sql[] = "AND (({$warehouse_id} = 0 OR warehouse_id = {$warehouse_id} OR (
+ ({$HBBMJ}=1 and warehouse_id = 20005 AND {$warehouse_id} = 139) OR ({$HBBMJ} = 1 and warehouse_id = 20047 AND {$warehouse_id} = 20001) OR ({$HBBMJ} = 1 and warehouse_id = 20048 AND {$warehouse_id} = 140))
+ ) and warehouse_id in (
+ SELECT uwh.warehouse_id FROM user_warehouse uwh LEFT JOIN warehouse wh ON uwh.warehouse_id = wh.id where uwh.user_id = {$user_id})
+ )";
+
+ if ($product_code) {
+ $sql[] = "and (rd.product_code LIKE '%{$product_code}%')";
+ }
+
+ $sql[] = ") as a
+ GROUP BY warehouse_code,product_code,batch_sn,batch_date,poscode,posname,warehouse_name,
+ product_name,product_spec,unit_name,product_id,warehouse_id
+ order by warehouse_code desc,product_code,batch_sn";
+
+ return DB::select(join(" ", $sql));
+ }
+
+ /**
+ * 进销存汇总表
+ *
+ * @warehouse_id 仓库ID
+ * @product_id 存货ID
+ * @batch_sn 批号
+ * @ny 内销 外销,''-全部
+ * @start_dt 起始日期
+ * @end_dt 截至日期
+ * @user_id 用户ID
+ * @SFPH 是否显示批号
+ * @HBBMJ 是否合并不满件
+ */
+ public static function reportOrderStockInOut($warehouse_id = 0, $product_id = 0, $batch_sn = '', $ny = '', $start_dt = '', $end_dt = '', $user_id = 0, $SFPH = 0, $HBBMJ = 0)
+ {
+ $warehouse_id = (int)$warehouse_id;
+ $product_id = (int)$product_id;
+ $SFPH = (int)$SFPH;
+ $HBBMJ = (int)$HBBMJ;
+
+ $invoice_dt = sql_year_month_day('m.invoice_dt');
+
+ // 期初
+ $sqlqc = "
+ warehouse_id,
+ product_id,
+ isnull(poscode,'') as poscode,
+ isnull(posname,'') as posname,
+ isnull(batch_sn,'') as batch_sn,
+ batch_date,
+ 0 RkNum,
+ 0 Rknum_Sc,
+ 0 Rknum_Qt,
+ 0 Rknum_Qr,
+ 0 Rknum_Th,
+ 0 Rknum_No,
+ 0 Cknum,
+ 0 Cknum_Fh,
+ 0 Cknum_Zy,
+ 0 Cknum_Qt,
+ 0 Cknum_Dc,
+ 0 Cknum_No,
+ SUM(Num) QcNum
+ from (
+ SELECT SUM(d.quantity) AS num, m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,batch_date
+ FROM stock_record10 m
+ LEFT JOIN stock_record10_data d ON m.id = d.record10_id
+ LEFT JOIN product p on p.id=d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".sql_year_month_day('m.invoice_dt')." < '$start_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' ='' OR (pc.name like '%外销%' AND '$ny' = '外销') OR (pc.name not like '%内销%' AND '$ny' = '内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id = $user_id)
+ GROUP BY m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,batch_date
+
+ UNION ALL
+
+ SELECT SUM(d.quantity) AS RkNum, m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date
+ FROM stock_record08 m
+ LEFT JOIN stock_record08_data d ON m.id = d.record08_id
+ LEFT JOIN product p on p.id=d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".sql_year_month_day('m.invoice_dt')." < '$start_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny' = '外销') OR (pc.name not like '%内销%' AND '$ny' = '内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,batch_date
+
+ UNION ALL
+
+ SELECT SUM(0-d.quantity),
+ m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date
+
+ FROM stock_record09 m
+ LEFT JOIN stock_record09_data d ON m.id = d.record09_id
+ LEFT JOIN product p on p.id=d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".sql_year_month_day('m.invoice_dt')." < '$start_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' ='' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,batch_date
+
+ UNION ALL
+
+ SELECT SUM(0-d.quantity),
+ m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date
+ FROM stock_record11 m
+ LEFT JOIN stock_record11_data d ON m.id = d.record11_id
+ LEFT JOIN product p on p.id=d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".sql_year_month_day('m.invoice_dt')." < '$start_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' ='' OR d.batch_sn = '$batch_sn')
+ and ('$ny' ='' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,batch_date
+
+ UNION ALL
+
+ SELECT SUM(0-d.quantity) AS RkNum, out_warehouse_id,d.product_id,d.batch_sn,d.out_poscode,d.out_posname,d.batch_date
+ FROM stock_allocation m
+ LEFT JOIN stock_allocation_data d ON m.id = d.allocation_id
+ LEFT JOIN product p on p.id=d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".sql_year_month_day("case when m.delivery_dt <> null then m.delivery_dt else m.invoice_dt end")." < '$start_dt'
+
+ and ($warehouse_id = 0 OR m.out_warehouse_id = $warehouse_id OR (
+ ($HBBMJ=1 AND m.out_warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.out_warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.out_warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.out_warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY m.out_warehouse_id,d.product_id,d.batch_sn,d.out_poscode,d.out_posname,batch_date
+
+ UNION ALL
+
+ SELECT SUM(d.quantity) AS RkNum, in_warehouse_id,d.product_id,d.batch_sn,d.in_poscode,d.in_posname,d.batch_date
+ FROM stock_allocation m
+ LEFT JOIN stock_allocation_data d ON m.id = d.allocation_id
+ LEFT JOIN product p on p.id=d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".sql_year_month_day("case when m.delivery_dt <> null then m.delivery_dt else m.invoice_dt end")." < '$start_dt'
+
+ and ($warehouse_id = 0 OR m.in_warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.in_warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.in_warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.in_warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' ='' OR (pc.name like '%外销%' AND '$ny' = '外销') OR (pc.name not like '%内销%' AND '$ny' = '内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.in_warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY m.in_warehouse_id,d.product_id,d.batch_sn,d.in_poscode,d.in_posname,batch_date
+
+ UNION ALL
+
+ -- 发货数量
+ SELECT
+ SUM(0-d.quantity) Cknum,
+ d.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date
+ FROM stock_delivery_data d
+ LEFT JOIN stock_delivery m on m.id = d.delivery_id
+ LEFT JOIN product p on p.id=d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".sql_year_month_day('m.invoice_dt')." < '$start_dt'
+
+ and ($warehouse_id = 0 OR d.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND d.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND d.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND d.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' ='' OR (pc.name like '%外销%' AND '$ny' = '外销') OR (pc.name not like '%内销%' AND '$ny' = '内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and d.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY d.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date
+
+ UNION ALL
+
+ SELECT
+ SUM(0-d.quantity) Cknum, m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date
+ FROM stock_direct_data d
+ LEFT JOIN stock_direct m on m.id = d.direct_id
+ LEFT JOIN product p on p.id=d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".sql_year_month_day('m.invoice_dt')." < '$start_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id = $warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' ='' OR (pc.name like '%外销%' AND '$ny' = '外销') OR (pc.name not like '%内销%' AND '$ny' = '内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date
+
+ UNION ALL
+
+ -- 退货数量
+ SELECT SUM(0-d.quantity) AS RkNum, d.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date
+ FROM stock_cancel_data d
+ left join stock_cancel m on m.id = d.cancel_id
+ LEFT JOIN product p on p.id=d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL and m.status = 1
+ AND ".sql_year_month_day('m.invoice_dt')." < '$start_dt'
+
+ and ($warehouse_id = 0 OR d.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND d.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND d.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND d.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' ='' OR (pc.name like '%外销%' AND '$ny' = '外销') OR (pc.name not like '%内销%' AND '$ny' = '内销'))
+ and ($product_id = 0 OR d.product_id=$product_id)
+ and d.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY d.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date
+ ) as a
+ group by warehouse_id, product_id, isnull(batch_sn,''), isnull(poscode,''), isnull(posname,''), batch_date
+ having SUM(Num) <> 0";
+
+ $sqlfs = "
+ warehouse_id,
+ product_id,
+ isnull(poscode,'') as poscode,
+ isnull(posname,'') as posname,
+ isnull(batch_sn,'') as batch_sn,
+ batch_date,
+ SUM(RkNum) RkNum,
+ SUM(Rknum_Sc) Rknum_Sc,
+ SUM(Rknum_Qt) Rknum_Qt,
+ SUM(Rknum_Qr) Rknum_Qr,
+ SUM(Rknum_Th) Rknum_Th,
+ SUM(Rknum_No) Rknum_No,
+ SUM(Cknum) Cknum,
+ SUM(Cknum_Fh) Cknum_Fh,
+ SUM(Cknum_Zy) Cknum_Zy,
+ SUM(Cknum_Qt) Cknum_Qt,
+ SUM(Cknum_Dc) Cknum_Dc,
+ SUM(Cknum_No) Cknum_No,
+ 0 QcNum
+ from (
+ SELECT SUM(d.quantity) AS RkNum,
+ SUM(d.quantity) Rknum_Sc,
+ 0 Rknum_Qt,
+ 0 Rknum_Qr,
+ 0 Rknum_Th,
+ SUM(ISNULL(case when ISNULL(m.status, 0) <> 1 then d.quantity ELSE 0 end,0)) Rknum_No,
+ 0 Cknum,
+ 0 Cknum_Fh,
+ 0 Cknum_Zy,
+ 0 Cknum_Qt,
+ 0 Cknum_Dc,
+ 0 Cknum_No,
+ m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,batch_date
+ FROM stock_record10 m
+ LEFT JOIN stock_record10_data d ON m.ID = d.record10_id
+
+ LEFT JOIN product p on p.id=d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".$invoice_dt." >= '$start_dt'
+ AND ".$invoice_dt." <= '$end_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id = $user_id)
+ GROUP BY m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,batch_date
+
+ UNION ALL
+
+ SELECT SUM(d.quantity) AS RkNum,
+ 0 Rknum_Sc,
+ SUM(d.quantity) Rknum_Qt,
+ 0 Rknum_Qr,
+ 0 Rknum_Th,
+ SUM(ISNULL(case when ISNULL(m.status,0) <> 1 then d.quantity ELSE 0 end, 0)) Rknum_No,
+ 0 Cknum,
+ 0 Cknum_Fh,
+ 0 Cknum_Zy,
+ 0 Cknum_Qt,
+ 0 Cknum_Dc,
+ 0 Cknum_No,
+ m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,batch_date
+ FROM stock_record08 m
+ LEFT JOIN stock_record08_data d ON m.id = d.record08_id
+
+ LEFT JOIN product p on p.id=d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".$invoice_dt." >= '$start_dt'
+ AND ".$invoice_dt." <= '$end_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn'='' OR d.batch_sn = '$batch_sn')
+ and ('$ny'='' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销') )
+ and ($product_id = 0 OR d.product_id=$product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,batch_date
+
+ UNION ALL
+
+ SELECT 0 AS RkNum,
+ 0 Rknum_Sc,
+ 0 Rknum_Qt,
+ 0 Rknum_Qr,
+ 0 Rknum_Th,
+ 0 Rknum_No,
+ SUM(d.quantity) Cknum,
+ 0 Cknum_Fh,
+ 0 Cknum_Zy,
+ SUM(d.quantity) Cknum_Qt,
+ 0 Cknum_Dc,
+ SUM(ISNULL(case when ISNULL(m.status,0) <> 1 then d.quantity ELSE 0 end,0)) Cknum_No,
+
+ m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,batch_date
+
+ FROM stock_record09 m
+ LEFT JOIN stock_record09_data d ON m.id = d.record09_id
+
+ LEFT JOIN product p on p.id=d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".$invoice_dt." >= '$start_dt'
+ AND ".$invoice_dt." <= '$end_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,batch_date
+
+ UNION ALL
+
+ SELECT 0 AS RkNum,
+ 0 Rknum_Sc,
+ 0 Rknum_Qt,
+ 0 Rknum_Qr,
+ 0 Rknum_Th,
+ 0 Rknum_No,
+ SUM(d.quantity) Cknum,
+ 0 Cknum_Fh,
+ 0 Cknum_Zy,
+ 0 Cknum_Qt,
+ SUM(d.quantity) Cknum_Dc,
+ SUM(ISNULL(case when ISNULL(m.status,0) <> 1 then d.quantity ELSE 0 end,0)) Cknum_No,
+ m.out_warehouse_id,d.product_id,d.batch_sn,d.out_poscode,d.out_posname,batch_date
+
+ FROM stock_allocation m
+ LEFT JOIN stock_allocation_data d ON m.id = d.allocation_id
+ LEFT JOIN product p on p.id=d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE ".sql_year_month_day("case when m.delivery_dt <> null then m.delivery_dt else m.invoice_dt end")." >= '$start_dt'
+ AND ".sql_year_month_day("case when m.delivery_dt <> null then m.delivery_dt else m.invoice_dt end")." <= '$end_dt'
+
+ and ($warehouse_id = 0 OR m.out_warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.out_warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.out_warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.out_warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.out_warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY m.out_warehouse_id,d.product_id,d.batch_sn,d.out_poscode,d.out_posname,batch_date
+
+ UNION ALL
+
+ SELECT SUM(d.quantity) AS RkNum,
+ 0 Rknum_Sc,
+ 0 Rknum_Qt,
+ SUM(d.quantity) Rknum_Qr,
+ 0 Rknum_Th,
+ SUM(ISNULL(case when ISNULL(m.status,0) <> 1 then d.quantity ELSE 0 end,0)) Rknum_No,
+ 0 Cknum,
+ 0 Cknum_Fh,
+ 0 Cknum_Zy,
+ 0 Cknum_Qt,
+ 0 Cknum_Dc,
+ 0 Cknum_No,
+ m.in_warehouse_id,d.product_id,d.batch_sn,d.in_poscode,d.in_posname,batch_date
+ FROM stock_allocation m
+ LEFT JOIN stock_allocation_data d ON m.id = d.allocation_id
+ LEFT JOIN product p on p.id=d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE ".sql_year_month_day("case when m.delivery_dt <> null then m.delivery_dt else m.invoice_dt end")." >= '$start_dt'
+ AND ".sql_year_month_day("case when m.delivery_dt <> null then m.delivery_dt else m.invoice_dt end")." <= '$end_dt'
+ --WHERE CONVERT(varchar(10), case when isnull(m.delivery_dt, '') <> '' then m.delivery_dt else m.invoice_dt end, 121)>=@SdDate
+ --AND CONVERT(varchar(10), case when isnull(m.delivery_dt, '') <> '' then m.delivery_dt else m.invoice_dt end, 121)<=@EdDate
+
+ and ($warehouse_id = 0 OR m.in_warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.in_warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.in_warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.in_warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.in_warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY m.in_warehouse_id,d.product_id,d.batch_sn,d.in_poscode,d.in_posname,batch_date
+
+ UNION ALL
+
+ -- 发货数量
+ SELECT 0 AS RkNum,
+ 0 Rknum_Sc,
+ 0 Rknum_Qt,
+ 0 Rknum_Qr,
+ 0 Rknum_Th,
+ 0 Rknum_No,
+ SUM(d.quantity) Cknum,
+ SUM(d.quantity) Cknum_Fh,
+ 0 Cknum_Zy,
+ 0 Cknum_Qt,
+ 0 Cknum_Dc,
+ SUM(ISNULL(case when ISNULL(m.status,0) <> 1 then d.quantity ELSE 0 end,0)) Cknum_No,
+ d.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,batch_date
+ FROM stock_delivery_data d
+ LEFT JOIN stock_delivery m on m.id = d.delivery_id
+ LEFT JOIN product p on p.id=d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".$invoice_dt." >= '$start_dt'
+ AND ".$invoice_dt." <= '$end_dt'
+
+ and ($warehouse_id = 0 OR d.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND d.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND d.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND d.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销') )
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and d.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY d.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,batch_date
+
+ UNION ALL
+
+ SELECT 0 AS RkNum,
+ 0 Rknum_Sc,
+ 0 Rknum_Qt,
+ 0 Rknum_Qr,
+ 0 Rknum_Th,
+ 0 Rknum_No,
+ SUM(d.quantity) Cknum,
+ 0 Cknum_Fh,
+ SUM(d.quantity) Cknum_Zy,
+ 0 Cknum_Qt,
+ 0 Cknum_Dc,
+ SUM(ISNULL(case when ISNULL(m.status, 0) <> 1 then d.quantity ELSE 0 end,0)) Cknum_No,
+ m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,batch_date
+ FROM stock_direct_data d
+ LEFT JOIN stock_direct m on m.id = d.direct_id
+ LEFT JOIN product p on p.id=d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".$invoice_dt." >= '$start_dt'
+ AND ".$invoice_dt." <= '$end_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,batch_date
+
+ UNION ALL
+
+ -- 退货数量
+ SELECT 0 AS RkNum,
+ 0 Rknum_Sc,
+ 0 Rknum_Qt,
+ 0 Rknum_Qr,
+ 0 Rknum_Th,
+ 0 Rknum_No,
+ SUM(d.quantity) Cknum,
+ SUM(d.quantity) Cknum_Fh,
+ 0 Cknum_Zy,
+ 0 Cknum_Qt,
+ 0 Cknum_Dc,
+ SUM(ISNULL(case when ISNULL(m.status,0) <> 1 then d.quantity ELSE 0 end,0)) Cknum_No,
+ d.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,batch_date
+ FROM stock_cancel_data d
+ left join stock_cancel m on m.id = d.cancel_id
+ LEFT JOIN product p on p.id=d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL and m.status = 1
+ AND ".$invoice_dt." >= '$start_dt'
+ AND ".$invoice_dt." <= '$end_dt'
+
+ and ($warehouse_id = 0 OR d.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND d.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND d.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND d.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id=$product_id)
+ and d.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY d.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,batch_date
+ ) as b
+ group by warehouse_id,product_id,isnull(poscode,''),isnull(posname,''),isnull(batch_sn,''),batch_date";
+
+ $sql[] = "
+ w.id as warehouse_id,
+ w.code as warehouse_code,
+ w.name as warehouse_name,
+ c.product_id,
+ i.code as product_code,
+ i.name as product_name,
+ i.spec as product_spec,
+ g.name as product_unit,
+ isnull(c.poscode,'') as poscode,
+ isnull(c.posname,'') as posname,";
+
+ if ($SFPH == 1) {
+ $sql[] = "isnull(c.batch_sn,'') as batch_sn, c.batch_date,";
+ }
+
+ $sql[] = "sum(QcNum) as qc_num,
+ SUM(RkNum) rk_num,
+ SUM(Rknum_Sc) rk_num_sc,
+ SUM(Rknum_Qt) rk_num_qt,
+ SUM(Rknum_Qr) rk_num_qr,
+ SUM(Rknum_Th) rk_num_th,
+ SUM(Rknum_No) rk_num_no,
+ SUM(Cknum) ck_num,
+ SUM(Cknum_Fh) ck_num_fh,
+ SUM(Cknum_Zy) ck_num_zy,
+ SUM(Cknum_Qt) ck_num_qt,
+ SUM(Cknum_Dc) ck_num_dc,
+ SUM(Cknum_No) ck_num_no,
+ ISNULL(sum(QcNum),0)+ISNULL(SUM(RkNum),0)-isnull(SUM(CkNum),0) as qm_num
+ from (
+ SELECT ".$sqlfs."
+ union all
+ SELECT ".$sqlqc."
+ ) as c, product i,product_unit g,warehouse w
+ where i.id = c.product_id and g.id = i.unit_id AND w.id=c.warehouse_id
+ group by w.id,w.code,w.name,c.product_id,i.code,i.name,i.spec,g.name,isnull(c.poscode,''),isnull(c.posname,'')
+ ";
+
+ if ($SFPH == 1) {
+ $sql[] = ",isnull(c.batch_sn,''),c.batch_date";
+ }
+
+ $sql[] = "order by w.code,i.code";
+
+ if ($SFPH == 1) {
+ $sql[] = ", isnull(c.batch_sn,'')";
+ }
+ $rows = DB::query()->selectRaw(join(" ", $sql))->get();
+ return $rows;
+ }
+
+ /**
+ * 库存明细表
+ *
+ * @warehouse_id 仓库ID
+ * @product_id 存货ID
+ * @batch_sn 批号
+ * @ny 内销 外销,''-全部
+ * @start_dt 起始日期
+ * @end_dt 截至日期
+ * @user_id 用户ID
+ * @HBBMJ 是否合并不满件库
+ */
+ public static function reportOrderStockDetail($warehouse_id = 0, $product_id = 0, $batch_sn = '', $ny = '', $start_dt = '', $end_dt = '', $user_id = 0, $HBBMJ = 0)
+ {
+ $warehouse_id = (int)$warehouse_id;
+ $product_id = (int)$product_id;
+ $HBBMJ = (int)$HBBMJ;
+ $user_id = (int)$user_id;
+
+ // 期初
+ $sql = "isnull(SUM(Num), 0) as qcnum
+ from (
+ SELECT SUM(d.quantity) AS Num, m.warehouse_id, d.product_id, d.batch_sn, d.poscode, d.posname, batch_date
+ FROM stock_record10 m
+ LEFT JOIN stock_record10_data d ON m.id = d.record10_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".sql_year_month_day('m.invoice_dt')." < '$start_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny' = '外销') OR (pc.name not like '%内销%' AND '$ny' = '内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date
+
+ UNION ALL
+
+ -- 原材料入库
+ SELECT SUM(d.quantity) AS Num, m.warehouse_id, d.product_id, d.batch_sn, d.poscode, d.posname, batch_date
+ FROM stock_record01 m
+ LEFT JOIN stock_record01_data d ON m.id = d.record01_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".sql_year_month_day('m.invoice_dt')." < '$start_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny' = '外销') OR (pc.name not like '%内销%' AND '$ny' = '内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date
+
+ UNION ALL
+
+ SELECT SUM(d.quantity) AS RkNum,
+ m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date
+ FROM stock_record08 m
+ LEFT JOIN stock_record08_data d ON m.id = d.record08_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".sql_year_month_day('m.invoice_dt')." < '$start_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date
+
+ UNION ALL
+
+ SELECT SUM(0-d.quantity) as Num,
+ m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date
+ FROM stock_record09 m
+ LEFT JOIN stock_record09_data d ON m.id = d.record09_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".sql_year_month_day('m.invoice_dt')." < '$start_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date
+
+ UNION ALL
+
+ -- 原材料出库
+ SELECT SUM(0-d.quantity) as Num,
+ m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date
+ FROM stock_record11 m
+ LEFT JOIN stock_record11_data d ON m.id = d.record11_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".sql_year_month_day('m.invoice_dt')." < '$start_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id= $product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id = $user_id)
+ GROUP BY m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date
+
+ UNION ALL
+
+ -- 调拨出
+ SELECT SUM(0-d.quantity) AS RkNum,
+ m.out_warehouse_id,d.product_id,batch_sn,d.out_poscode,d.out_posname,d.batch_date
+ FROM stock_allocation m
+ LEFT JOIN stock_allocation_data d ON m.id = d.allocation_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE ".sql_year_month_day("case when m.delivery_dt <> null then m.delivery_dt else m.invoice_dt end")." < '$start_dt'
+
+ and ($warehouse_id = 0 OR m.out_warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.out_warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.out_warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.out_warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.out_warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY d.product_id,m.out_warehouse_id,d.batch_sn,d.out_poscode,d.out_posname,d.batch_date
+
+ UNION ALL
+
+ SELECT SUM(d.quantity) AS RkNum,
+ m.in_warehouse_id,d.product_id,d.batch_sn,d.in_poscode,d.in_posname,d.batch_date
+ FROM stock_allocation m
+ LEFT JOIN stock_allocation_data d ON m.id = d.allocation_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE ".sql_year_month_day("case when m.delivery_dt <> null then m.delivery_dt else m.invoice_dt end")." < '$start_dt'
+
+ and ($warehouse_id = 0 OR m.in_warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.in_warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.in_warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.in_warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny'='外销')OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.in_warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY d.product_id,m.in_warehouse_id,d.batch_sn,d.in_poscode,d.in_posname,d.batch_date
+
+ UNION ALL
+
+ -- 发货数量
+ SELECT
+ SUM(0-d.quantity) Cknum,
+ d.warehouse_id,d.product_id, d.batch_sn,d.poscode,d.posname,d.batch_date
+ FROM stock_delivery_data d
+ LEFT JOIN stock_delivery m on m.id = d.delivery_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".sql_year_month_day('m.invoice_dt')." < '$start_dt'
+
+ and ($warehouse_id = 0 OR d.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND d.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND d.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND d.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id=$product_id)
+ and d.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY d.product_id,d.batch_sn,d.warehouse_id,d.poscode,d.posname,d.batch_date
+
+ UNION ALL
+
+ -- 直营发货
+ SELECT
+ SUM(0-d.quantity) Cknum,
+ m.warehouse_id,d.product_id, d.batch_sn,d.poscode,d.posname,d.batch_date
+ FROM stock_direct_data d LEFT JOIN stock_direct m on m.id = d.direct_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".sql_year_month_day('m.invoice_dt')." < '$start_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id=$product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id = $user_id)
+ GROUP BY d.product_id,d.batch_sn,m.warehouse_id,d.poscode,d.posname,d.batch_date
+
+ UNION ALL
+
+ -- 退货数量
+ SELECT SUM(0-d.quantity) AS RkNum,
+ d.warehouse_id,d.product_id, d.batch_sn,d.poscode,d.posname,d.batch_date
+ FROM stock_cancel_data d left join stock_cancel m on m.id = d.cancel_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL and m.status = 1
+ AND ".sql_year_month_day('m.invoice_dt')." < '$start_dt'
+
+ and ($warehouse_id = 0 OR d.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND d.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND d.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND d.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id=$product_id)
+ and d.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+ GROUP BY d.product_id,d.batch_sn,d.warehouse_id,d.poscode,d.posname,d.batch_date
+ ) as a
+ having SUM(Num) <> 0";
+ $qcnum = DB::query()->selectRaw($sql)->value('qcnum');
+
+ $invoice_dt = sql_year_month_day('m.invoice_dt');
+ $sql = "
+ c.product_id,
+ c.warehouse_id,
+ c.id,
+ c.sn,
+ c.invoice_dt,
+ w.code as warehouse_code,
+ w.name as warehouse_name,
+ p.code as product_code,
+ p.name as product_name,
+ p.spec as product_spec,
+ g.name as unit_name,
+ isnull(batch_sn,'') as batch_sn,
+ isnull(c.poscode,'') as poscode,
+ isnull(c.posname,'') as posname,
+ isnull(batch_date, null) batch_date,
+ c.bill_type,
+ c.bill_name,
+ c.RkNum as rk_num,
+ c.CkNum as ck_num,
+ 0 as qm_num
+ from (
+ SELECT d.quantity AS RkNum, 0 CkNum,
+ m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date,
+ 'stock_record10' as bill_type,'产成品入库' as bill_name, m.id, m.sn,
+ m.invoice_dt
+ FROM stock_record10 m
+ LEFT JOIN stock_record10_data d ON m.id = d.record10_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+
+ WHERE d.product_id IS NOT NULL
+ AND ".$invoice_dt." >= '$start_dt'
+ AND ".$invoice_dt." <= '$end_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id=$product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+
+ UNION ALL
+
+ SELECT d.quantity AS RkNum, 0 CkNum,
+ m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date,
+ 'stock_record08' as bill_type,'其他入库' as bill_name,m.id,m.sn,
+ m.invoice_dt
+ FROM stock_record08 m
+ LEFT JOIN stock_record08_data d ON m.id = d.record08_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".$invoice_dt." >= '$start_dt'
+ AND ".$invoice_dt." <= '$end_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny'='外销') OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id=$product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id=$user_id)
+
+ UNION ALL
+
+ SELECT d.quantity AS RkNum, 0 CkNum,
+ m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date,
+ 'stock_record01' as bill_type,'原材料入库' as bill_name,m.id,m.sn,
+ m.invoice_dt
+ FROM stock_record01 m
+ LEFT JOIN stock_record01_data d ON m.id = d.record01_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".$invoice_dt." >= '$start_dt'
+ AND ".$invoice_dt." <= '$end_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny' = '外销') OR (pc.name not like '%内销%' AND '$ny' = '内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id = $user_id)
+
+ UNION ALL
+
+ SELECT 0 AS RkNum, d.quantity CkNum,
+ m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date,
+ 'stock_record09' as bill_type,'其他出库' as bill_name,m.id,m.sn,m.invoice_dt
+ FROM stock_record09 m
+ LEFT JOIN stock_record09_data d ON m.id = d.record09_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".$invoice_dt." >= '$start_dt'
+ AND ".$invoice_dt." <= '$end_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id = $warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id = 139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id = 20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id = 140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny' = '外销')OR (pc.name not like '%内销%' AND '$ny' = '内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id = $user_id)
+
+ UNION ALL
+
+ SELECT 0 AS RkNum, d.quantity CkNum,
+ m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date,
+ 'stock_record11' as bill_type,'原材料出库' as bill_name,m.id,m.sn,m.invoice_dt
+ FROM stock_record11 m
+ LEFT JOIN stock_record11_data d ON m.id = d.record11_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".$invoice_dt." >= '$start_dt'
+ AND ".$invoice_dt." <= '$end_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id = $warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id = 139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id = 20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id = 140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny' = '外销') OR (pc.name not like '%内销%' AND '$ny' = '内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id = $user_id)
+
+ UNION ALL
+
+ SELECT 0 AS RkNum,
+ d.quantity Cknum,
+ m.out_warehouse_id,d.product_id,d.batch_sn,d.out_poscode,d.out_posname,d.batch_date,
+ 'stock_allocation' as bill_type,'产成品调拨' as bill_name, m.id, m.sn, m.invoice_dt
+ FROM stock_allocation m
+ LEFT JOIN stock_allocation_data d ON m.id = d.allocation_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+
+ where ".sql_year_month_day("case when m.delivery_dt <> null then m.delivery_dt else m.invoice_dt end")." >= '$start_dt'
+ AND ".sql_year_month_day("case when m.delivery_dt <> null then m.delivery_dt else m.invoice_dt end")." <= '$end_dt'
+
+ and ($warehouse_id = 0 OR m.out_warehouse_id = $warehouse_id OR (
+ ($HBBMJ=1 AND m.out_warehouse_id=20005 AND $warehouse_id = 139)
+ OR ($HBBMJ=1 AND m.out_warehouse_id=20047 AND $warehouse_id = 20001)
+ OR ($HBBMJ=1 AND m.out_warehouse_id=20048 AND $warehouse_id = 140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny' = '外销') OR (pc.name not like '%内销%' AND '$ny' = '内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.out_warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id = $user_id)
+
+ UNION ALL
+
+ SELECT d.quantity AS RkNum,
+ 0 Cknum,
+ m.in_warehouse_id,d.product_id,d.batch_sn,d.in_poscode,d.in_posname,d.batch_date,
+ 'stock_allocation' as bill_type,'产成品调拨' as bill_name, m.id,m.sn, m.invoice_dt
+
+ FROM stock_allocation m
+ LEFT JOIN stock_allocation_data d ON m.id = d.allocation_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+
+ where ".sql_year_month_day("case when m.delivery_dt <> null then m.delivery_dt else m.invoice_dt end")." >= '$start_dt'
+ AND ".sql_year_month_day("case when m.delivery_dt <> null then m.delivery_dt else m.invoice_dt end")." <= '$end_dt'
+
+ --and (ISNULL(@cWhID,'')='' OR m.in_warehouse_id = @cWhID OR ($HBBMJ=1 AND m.in_warehouse_id=20005))
+
+ and ($warehouse_id = 0 OR m.in_warehouse_id = $warehouse_id OR (
+ ($HBBMJ=1 AND m.in_warehouse_id = 20005 AND $warehouse_id = 139)
+ OR ($HBBMJ=1 AND m.in_warehouse_id = 20047 AND $warehouse_id = 20001)
+ OR ($HBBMJ=1 AND m.in_warehouse_id = 20048 AND $warehouse_id = 140)
+ ))
+
+ --and ('$batch_sn' = '' OR d.cBatch = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny' = '外销') OR (pc.name not like '%内销%' AND '$ny' = '内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.in_warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id = $user_id)
+
+ UNION ALL
+
+ -- 发货数量
+
+ SELECT 0 AS RkNum,
+ d.quantity Cknum,
+ d.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date,
+ 'stock_delivery' as bill_type,'发货' as bill_name, m.id, m.sn,
+ m.invoice_dt
+ FROM stock_delivery_data d
+ LEFT JOIN stock_delivery m on m.id = d.delivery_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".$invoice_dt." >= '$start_dt'
+ AND ".$invoice_dt." <= '$end_dt'
+
+ and ($warehouse_id = 0 OR d.warehouse_id = $warehouse_id OR (
+ ($HBBMJ=1 AND d.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND d.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND d.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny' = '外销')OR (pc.name not like '%内销%' AND '$ny'='内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and d.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id = $user_id)
+
+ UNION ALL
+
+ SELECT 0 AS RkNum,
+ d.quantity Cknum,
+ m.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date,
+ 'stock_direct' as bill_type,'发货(直营)' as bill_name,m.id,m.sn,
+ m.invoice_dt
+ FROM stock_direct_data d
+ LEFT JOIN stock_direct m on m.id = d.direct_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".$invoice_dt." >= '$start_dt'
+ AND ".$invoice_dt." <= '$end_dt'
+
+ and ($warehouse_id = 0 OR m.warehouse_id = $warehouse_id OR (
+ ($HBBMJ=1 AND m.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND m.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND m.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny' = '外销') OR (pc.name not like '%内销%' AND '$ny' = '内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and m.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id = $user_id)
+
+ UNION ALL
+
+ -- 退货数量
+ SELECT 0 AS RkNum,
+ d.quantity Cknum,
+ d.warehouse_id,d.product_id,d.batch_sn,d.poscode,d.posname,d.batch_date,
+ 'stock_cancel' as bill_type,'退货申请' as bill_name,m.id,m.sn,
+ m.invoice_dt
+ FROM stock_cancel_data d
+ left join stock_cancel m on m.id = d.cancel_id
+ LEFT JOIN product p on p.id = d.product_id
+ LEFT JOIN product_category pc on p.category_id = pc.id
+ WHERE d.product_id IS NOT NULL
+ AND ".$invoice_dt." >= '$start_dt'
+ AND ".$invoice_dt." <= '$end_dt'
+
+ and ($warehouse_id = 0 OR d.warehouse_id=$warehouse_id OR (
+ ($HBBMJ=1 AND d.warehouse_id=20005 AND $warehouse_id=139)
+ OR ($HBBMJ=1 AND d.warehouse_id=20047 AND $warehouse_id=20001)
+ OR ($HBBMJ=1 AND d.warehouse_id=20048 AND $warehouse_id=140)
+ ))
+
+ and ('$batch_sn' = '' OR d.batch_sn = '$batch_sn')
+ and ('$ny' = '' OR (pc.name like '%外销%' AND '$ny' = '外销') OR (pc.name not like '%内销%' AND '$ny' = '内销'))
+ and ($product_id = 0 OR d.product_id = $product_id)
+ and d.warehouse_id in (SELECT uwh.warehouse_id FROM user_warehouse uwh where user_id = $user_id)
+ ) as c, product p, product_unit g, warehouse w
+
+ where p.id = c.product_id and g.id = p.unit_id and w.id = c.warehouse_id order by c.invoice_dt, c.RkNum";
+ $rows = DB::query()->selectRaw($sql)->get();
+ // 插入初期
+ $rows->prepend(['bill_name' => '期初', 'qm_num' => $qcnum]);
+ return $rows;
+ }
+
+ /**
+ * 校验单据批次
+ * @warehouse_id 仓库编码
+ * @product_id 存货编码
+ * @batch_sn 批次
+ * @poscode 货位
+ * @delivery_data_id 发货单表体ID
+ * @sample_data_id 样品单表体ID
+ * @direct_data_id 直营单表体ID
+ */
+ public static function verfyInvoiceBatch($warehouse_id, $product_id, $batch_sn, $poscode, $delivery_data_id = 0, $sample_data_id = 0, $direct_data_id = 0)
+ {
+ $warehouse_id = (int)$warehouse_id;
+ $product_id = (int)$product_id;
+ $delivery_data_id = (int)$delivery_data_id;
+ $sample_data_id = (int)$sample_data_id;
+ $direct_data_id = (int)$direct_data_id;
+
+ return DB::select("
+ select sum(CAST((ROUND(ISNULL(a.num,0) - ISNULL(a.fh_num,0) ,4)) AS decimal(20,4))) as ky_num
+ FROM (
+ -- 合并库存、出入库数量
+ -- 产成品入库单
+ SELECT SUM(ISNULL(num, 0)) num, sum(isnull(fh_num, 0)) as fh_num, product_id, warehouse_id, batch_sn,poscode
+ FROM (
+ SELECT SUM(ISNULL(d.quantity, 0)) num, 0 as fh_num, m.warehouse_id, d.product_id, d.batch_sn, d.poscode
+ FROM stock_record10_data d
+ LEFT JOIN stock_record10 m ON m.id = d.record10_id
+ WHERE d.product_id IS NOT NULL
+ GROUP BY m.warehouse_id,d.product_id,d.batch_sn,d.poscode
+
+ UNION ALL
+
+ -- 采购入库单
+ SELECT SUM(ISNULL(d.quantity, 0)) num,
+ NULL AS fh_num, m.warehouse_id, d.product_id, d.batch_sn, d.poscode
+ FROM stock_record01_data d
+ LEFT JOIN stock_record01 m ON m.id = d.record01_id
+ WHERE d.product_id IS NOT NULL
+ GROUP BY m.warehouse_id, d.product_id, d.batch_sn, d.poscode
+
+ UNION ALL
+
+ -- 其他入库
+ SELECT SUM(ISNULL(d.quantity,0)) num, 0 fh_num,warehouse_id,d.product_id,d.batch_sn,poscode
+ FROM stock_record08 m
+ LEFT JOIN stock_record08_data d ON m.id = d.record08_id
+ WHERE d.product_id IS NOT NULL
+ GROUP BY warehouse_id,d.product_id,d.batch_sn,poscode
+
+ UNION ALL
+
+ -- 其他出库单
+ SELECT 0 - SUM(ISNULL(d.quantity,0)) num,0 fh_num,warehouse_id,d.product_id,d.batch_sn,poscode
+ FROM stock_record09 m
+ LEFT JOIN stock_record09_data d ON m.id = d.record09_id
+ WHERE d.product_id IS NOT NULL and d.id <> $sample_data_id
+ GROUP BY warehouse_id,d.product_id,d.batch_sn,poscode
+
+ UNION ALL
+
+ -- 调拨出库
+ SELECT 0 - SUM(ISNULL(quantity,0)) AS num,0 fh_num,out_warehouse_id,product_id,batch_sn,out_poscode
+ FROM stock_allocation m
+ LEFT JOIN stock_allocation_data d ON m.id = d.allocation_id
+ GROUP BY product_id, out_warehouse_id, batch_sn, out_poscode
+
+ UNION ALL
+
+ -- 调拨入库
+ SELECT SUM(ISNULL(quantity,0)) AS num, 0 fh_num, in_warehouse_id, product_id, batch_sn, in_poscode
+ FROM stock_allocation m
+ LEFT JOIN stock_allocation_data d ON m.id = d.allocation_id
+ GROUP BY product_id,in_warehouse_id,batch_sn,in_poscode
+
+ UNION ALL
+
+ -- 发货数量
+ SELECT 0 - SUM(ISNULL(case when ISNULL(m.status, 0) = 1 then d.quantity ELSE 0 end,0)) num,
+ SUM(ISNULL(case when ISNULL(m.status, 0) <> 1 then d.quantity ELSE 0 end, 0)) fh_num,
+ d.warehouse_id,d.product_id,d.batch_sn,poscode
+ FROM stock_delivery_data d,stock_delivery m
+ WHERE m.id = d.delivery_id and d.id <> $delivery_data_id
+ GROUP BY d.product_id,d.batch_sn,d.warehouse_id,poscode
+
+ union all
+
+ -- 直营发货数量
+ SELECT 0 - SUM(ISNULL(case when ISNULL(m.status, 0) = 1 then d.quantity ELSE 0 end,0)) num,
+ SUM(ISNULL(case when ISNULL(m.status, 0) <> 1 then d.quantity ELSE 0 end, 0)) fh_num,
+ m.warehouse_id,d.product_id,d.batch_sn,d.poscode
+ FROM stock_direct_data d,stock_direct m
+ WHERE m.id = d.direct_id and d.id <> $direct_data_id
+ GROUP BY d.product_id,d.batch_sn,m.warehouse_id,d.poscode
+
+ union all
+
+ -- 退货数量
+ SELECT 0 - SUM(ISNULL(case when ISNULL(m.status, 0) = 1 then d.quantity ELSE 0 end,0)) num,
+ SUM(ISNULL(case when ISNULL(m.status,0) <> 1 then d.quantity ELSE 0 end,0)) fh_num,
+ d.warehouse_id,d.product_id,d.batch_sn,poscode
+ FROM stock_cancel_data d,stock_cancel m
+ WHERE m.id = d.cancel_id AND m.status = 1
+ GROUP BY d.product_id,d.batch_sn,d.warehouse_id,poscode
+
+ ) a1
+ GROUP BY product_id,warehouse_id,batch_sn,poscode
+ HAVING SUM(ISNULL(num, 0)) <> 0 OR sum(isnull(fh_num, 0)) <> 0
+ ) a
+ WHERE a.warehouse_id = $warehouse_id
+ AND a.product_id = $product_id
+ AND (ISNULL('$batch_sn', '') = '' or a.batch_sn = '$batch_sn')
+ AND (ISNULL('$poscode', '') = '' or a.poscode = '$poscode')
+ ");
+ }
+
+ /**
+ * 查询库存
+ */
+ public static function getStockSelectSql()
+ {
+ return "
+ SELECT w.code AS warehouse_code, p.code AS product_code, a.batch_sn AS batch_sn, a.batch_date,
+ a.poscode AS poscode, a.posname AS posname, w.type as warehouse_type, w.name AS warehouse_name, p.name AS product_name, p.product_type,
+ p.spec AS product_spec, u.id AS unit_id, u.name AS product_unit, u.name AS unit_name, p.category_id, p.id AS product_id, w.id AS warehouse_id,
+ round(a.Num, 4) AS num,
+ round(isnull(a.Num, 0) - isnull(a.FhNum, 0) - isnull(a.Cknum, 0) + isnull(a.Rknum, 0), 4) AS ky_num,
+ round(isnull(a.FhNum, 0), 4) AS fh_num,
+ round(isnull(a.Cknum, 0), 4) AS ck_num,
+ round(isnull(a.Rknum, 0), 4) AS rk_num,
+ round(isnull(a.Num, 0) - isnull(a.FhNum, 0), 4) AS max_num
+ FROM (
+ SELECT SUM(isnull(a1.Num, 0)) AS Num,
+ SUM(isnull(a1.FhNum, 0)) AS FhNum,
+ SUM(isnull(a1.Rknum, 0)) AS Rknum,
+ SUM(isnull(a1.Cknum, 0)) AS Cknum,
+ a1.product_id, a1.warehouse_id, a1.batch_sn, a1.batch_date,
+ CASE WHEN a1.poscode = '' THEN NULL ELSE a1.poscode END AS poscode,
+ CASE WHEN a1.posname = '' THEN NULL ELSE a1.posname END AS posname
+ FROM (
+ -- 采购入库
+ SELECT SUM(isnull(CASE WHEN m.status = 1 THEN d.quantity ELSE 0 END, 0)) AS Num,
+ NULL AS FhNum,
+ SUM(isnull(CASE WHEN m.status <> 1 THEN d.quantity ELSE 0 END, 0)) AS Rknum,
+ 0 AS Cknum, m.warehouse_id, d.product_id, d.batch_sn, d.batch_date, d.poscode, d.posname
+ FROM stock_record01 m
+ LEFT JOIN stock_record01_data d ON m.id = d.record01_id
+ WHERE d.product_id IS NOT NULL
+ GROUP BY d.product_id, m.warehouse_id, d.batch_sn, d.batch_date, d.poscode, d.posname
+
+ UNION ALL
+
+ -- 原材料出库
+ SELECT
+ 0 - SUM(isnull(CASE WHEN m.status = 1 THEN d.quantity ELSE 0 END, 0)) AS Num,
+ 0 AS FhNum, 0 AS Rknum, SUM(isnull(CASE WHEN m.status <> 1 THEN d.quantity ELSE 0 END, 0)) AS Cknum,
+ m.warehouse_id, d.product_id, d.batch_sn, d.batch_date, d.poscode, d.posname
+ FROM stock_record11 m
+ LEFT JOIN stock_record11_data d ON m.id = d.record11_id
+ WHERE d.product_id IS NOT NULL
+ GROUP BY d.product_id, m.warehouse_id, d.batch_sn, d.batch_date, d.poscode, d.posname
+
+ UNION ALL
+
+ SELECT SUM(isnull(CASE WHEN m.status = 1 THEN d.quantity ELSE 0 END, 0)) AS Num,
+ 0 AS FhNum,
+ SUM(isnull(CASE WHEN m.status <> 1 THEN d.quantity ELSE 0 END, 0)) AS Rknum,
+ 0 AS Cknum, m.warehouse_id, d.product_id, d.batch_sn, d.batch_date, d.poscode, d.posname
+ FROM stock_record10 m
+ LEFT JOIN stock_record10_data d ON m.id = d.record10_id
+ WHERE d.product_id IS NOT NULL
+ GROUP BY d.product_id, m.warehouse_id, d.batch_sn, d.batch_date, d.poscode, d.posname
+
+ UNION ALL
+
+ SELECT SUM(isnull(CASE WHEN m.status = 1 THEN d.quantity ELSE 0 END, 0)) AS Num,
+ 0 AS FhNum,
+ SUM(isnull(CASE WHEN m.status <> 1 THEN d.quantity ELSE 0 END, 0)) AS Rknum,
+ 0 AS Cknum, m.warehouse_id, d.product_id, d.batch_sn, d.batch_date, d.poscode, d.posname
+ FROM stock_record08 m
+ LEFT JOIN stock_record08_data d ON m.id = d.record08_id
+ WHERE d.product_id IS NOT NULL
+ GROUP BY d.product_id, m.warehouse_id, d.batch_sn, d.batch_date, d.poscode, d.posname
+
+ UNION ALL
+
+ SELECT
+ 0 - SUM(isnull(CASE WHEN m.status = 1 THEN d.quantity ELSE 0 END, 0)) AS Num,
+ 0 AS FhNum, 0 AS Rknum, SUM(isnull(CASE WHEN m.status <> 1 THEN d.quantity ELSE 0 END, 0)) AS Cknum,
+ m.warehouse_id, d.product_id, d.batch_sn, d.batch_date, d.poscode, d.posname
+ FROM stock_record09 m
+ LEFT JOIN stock_record09_data d ON m.id = d.record09_id
+ WHERE d.product_id IS NOT NULL
+ GROUP BY d.product_id, m.warehouse_id, d.batch_sn, d.batch_date, d.poscode, d.posname
+
+ UNION ALL
+
+ SELECT
+ 0 - SUM(isnull(CASE WHEN m.status = 1 THEN d.quantity ELSE 0 END, 0)) AS Num,
+ 0 AS FhNum, 0 AS Rknum,
+ SUM(isnull(CASE WHEN m.status <> 1 THEN d.quantity ELSE 0 END, 0)) AS Cknum,
+ m.out_warehouse_id, d.product_id, d.batch_sn, d.batch_date, d.out_poscode poscode, d.out_posname posname
+ FROM stock_allocation m
+ LEFT JOIN stock_allocation_data d ON m.id = d.allocation_id
+ GROUP BY d.product_id, m.out_warehouse_id, d.batch_sn, d.batch_date, d.out_poscode, d.out_posname
+
+ UNION ALL
+
+ SELECT SUM(isnull(CASE WHEN m.status = 1 THEN d.quantity ELSE 0 END, 0)) AS Num, NULL AS FhNum,
+ SUM(isnull(CASE WHEN m.status <> 1 THEN d.quantity ELSE 0 END, 0)) AS Rknum,
+ 0 AS Cknum, m.in_warehouse_id, d.product_id, d.batch_sn, d.batch_date, d.in_poscode poscode, d.in_posname
+ FROM stock_allocation m
+ LEFT JOIN stock_allocation_data d ON m.id = d.allocation_id
+ GROUP BY d.product_id, m.in_warehouse_id, d.batch_sn, d.batch_date, d.in_poscode, d.in_posname
+
+ UNION ALL
+
+ SELECT
+ 0 - SUM(isnull(CASE WHEN m.status = 1 THEN d.quantity ELSE 0 END, 0)) AS Num,
+ SUM(isnull(CASE WHEN m.status <> 1 THEN d.quantity ELSE 0 END, 0)) AS FhNum,
+ NULL AS Rknum, NULL AS Cknum, d.warehouse_id, d.product_id, d.batch_sn, d.batch_date, d.poscode, d.posname
+ FROM stock_delivery_data d
+ JOIN stock_delivery m ON m.id = d.delivery_id
+ GROUP BY d.product_id, d.warehouse_id, d.batch_sn, d.batch_date, d.poscode, d.posname
+
+ UNION ALL
+
+ SELECT
+ 0 - SUM(isnull(CASE WHEN m.status = 1 THEN d.quantity ELSE 0 END, 0)) AS Num,
+ SUM(isnull(CASE WHEN m.status <> 1 THEN d.quantity ELSE 0 END, 0)) AS FhNum,
+ 0 AS Rknum, 0 AS Cknum, m.warehouse_id, d.product_id, d.batch_sn, d.batch_date, d.poscode, d.posname
+ FROM stock_direct_data d
+ JOIN stock_direct m ON m.id = d.direct_id
+ GROUP BY d.product_id, m.warehouse_id, d.batch_sn, d.batch_date, d.poscode, d.posname
+
+ UNION ALL
+
+ SELECT
+ 0 - SUM(isnull(CASE WHEN m.status = 1 THEN d.quantity ELSE 0 END, 0)) AS Num,
+ 0 AS FhNum,
+ 0 - SUM(isnull(CASE WHEN m.status <> 1 THEN d.quantity ELSE 0 END, 0)) AS Rknum,
+ NULL AS Cknum, d.warehouse_id, d.product_id, d.batch_sn, d.batch_date, d.poscode, d.posname
+ FROM stock_cancel_data d
+ JOIN stock_cancel m
+ ON m.id = d.cancel_id AND m.status = 1
+ GROUP BY d.product_id, d.warehouse_id, d.batch_sn, d.batch_date, d.poscode, d.posname
+ ) a1
+
+ GROUP BY a1.product_id, a1.warehouse_id, a1.batch_sn, a1.batch_date,
+ CASE WHEN a1.poscode = '' THEN NULL ELSE a1.poscode END,
+ CASE WHEN a1.posname = '' THEN NULL ELSE a1.posname END
+
+ HAVING SUM(isnull(a1.Num, 0)) <> 0 OR SUM(isnull(a1.FhNum, 0)) <> 0 OR SUM(isnull(a1.Cknum, 0)) <> 0 OR SUM(isnull(a1.Rknum, 0)) <> 0
+ ) a
+
+ LEFT JOIN product p ON a.product_id = p.id
+ LEFT JOIN product_unit u ON p.unit_id = u.id
+ LEFT JOIN warehouse w ON a.warehouse_id = w.id";
+ }
+}
\ No newline at end of file
diff --git a/app/Gdoo/Stock/config.php b/app/Gdoo/Stock/config.php
new file mode 100644
index 00000000..9af1f5f0
--- /dev/null
+++ b/app/Gdoo/Stock/config.php
@@ -0,0 +1,419 @@
+ "库存管理",
+ "version" => "1.0",
+ "description" => "产品列表,产品类别,库存类型,仓库类别,库存管理,仓库列表。",
+ "listens" => [
+ 'stock_delivery' => 'Gdoo\Stock\Hooks\DeliveryHook',
+ 'stock_delivery_data' => 'Gdoo\Stock\Hooks\DeliveryDataHook',
+ 'stock_record11' => 'Gdoo\Stock\Hooks\Record11Hook',
+
+ 'stock_record10' => 'Gdoo\Stock\Hooks\Record10Hook',
+ 'stock_record10_data' => 'Gdoo\Stock\Hooks\Record10DataHook',
+
+ 'stock_record09' => 'Gdoo\Stock\Hooks\Record09Hook',
+ 'stock_record08' => 'Gdoo\Stock\Hooks\Record08Hook',
+ 'stock_record01' => 'Gdoo\Stock\Hooks\Record01Hook',
+ 'stock_direct' => 'Gdoo\Stock\Hooks\DirectHook',
+ 'stock_cancel' => 'Gdoo\Stock\Hooks\CancelHook',
+ 'stock_allocation' => 'Gdoo\Stock\Hooks\AllocationHook',
+ ],
+ 'dialogs' => [
+ 'warehouse' => [
+ 'name' => '仓库',
+ 'model' => 'Gdoo\Stock\Models\Warehouse::Dialog',
+ 'url' => 'stock/warehouse/dialog',
+ ],
+ 'location_batch' => [
+ 'name' => '库存数量',
+ 'model' => 'Gdoo\Stock\Models\WarehouseLocation::Dialog',
+ 'url' => 'stock/location/dialog2',
+ ],
+ 'warehouse_location' => [
+ 'name' => '仓库货位',
+ 'model' => 'Gdoo\Stock\Models\WarehouseLocation::Dialog',
+ 'url' => 'stock/location/dialog',
+ ],
+ ],
+ "controllers" => [
+ "delivery" => [
+ "name" => "发货单",
+ "actions" => [
+ "index" => [
+ "name" => "列表",
+ ],
+ "detail" => [
+ "name" => "明细列表",
+ ],
+ "show" => [
+ "name" => "查看"
+ ],
+ "create" => [
+ "name" => "新建"
+ ],
+ "edit" => [
+ "name" => "编辑"
+ ],
+ "audit" => [
+ "name" => "审核"
+ ],
+ "recall" => [
+ "name" => "撤回"
+ ],
+ "abort" => [
+ "name" => "弃审"
+ ],
+ "print" => [
+ "name" => "打印"
+ ],
+ "delete" => [
+ "name" => "删除"
+ ],
+ "batchEdit" => [
+ "name" => "批量编辑"
+ ],
+ ]
+ ],
+ "direct" => [
+ "name" => "发货单(直营)",
+ "actions" => [
+ "index" => [
+ "name" => "列表",
+ ],
+ "show" => [
+ "name" => "查看"
+ ],
+ "create" => [
+ "name" => "新建"
+ ],
+ "edit" => [
+ "name" => "编辑"
+ ],
+ "audit" => [
+ "name" => "审核"
+ ],
+ "recall" => [
+ "name" => "撤回"
+ ],
+ "abort" => [
+ "name" => "弃审"
+ ],
+ "print" => [
+ "name" => "打印"
+ ],
+ "delete" => [
+ "name" => "删除"
+ ],
+ ]
+ ],
+ "allocation" => [
+ "name" => "产成品调拨单",
+ "actions" => [
+ "index" => [
+ "name" => "列表",
+ ],
+ "show" => [
+ "name" => "查看"
+ ],
+ "create" => [
+ "name" => "新建"
+ ],
+ "edit" => [
+ "name" => "编辑"
+ ],
+ "audit" => [
+ "name" => "审核"
+ ],
+ "recall" => [
+ "name" => "撤回"
+ ],
+ "abort" => [
+ "name" => "弃审"
+ ],
+ "print" => [
+ "name" => "打印"
+ ],
+ "delete" => [
+ "name" => "删除"
+ ],
+ ]
+ ],
+ "cancel" => [
+ "name" => "退货申请",
+ "actions" => [
+ "index" => [
+ "name" => "列表",
+ ],
+ "show" => [
+ "name" => "查看"
+ ],
+ "create" => [
+ "name" => "新建"
+ ],
+ "edit" => [
+ "name" => "编辑"
+ ],
+ "audit" => [
+ "name" => "审核"
+ ],
+ "recall" => [
+ "name" => "撤回"
+ ],
+ "abort" => [
+ "name" => "弃审"
+ ],
+ "print" => [
+ "name" => "打印"
+ ],
+ "delete" => [
+ "name" => "删除"
+ ],
+ "batchEdit" => [
+ "name" => "批量编辑"
+ ],
+ ]
+ ],
+ "record01" => [
+ "name" => "采购入库单",
+ "actions" => [
+ "index" => [
+ "name" => "列表",
+ ],
+ "show" => [
+ "name" => "查看"
+ ],
+ "create" => [
+ "name" => "新建"
+ ],
+ "edit" => [
+ "name" => "编辑"
+ ],
+ "audit" => [
+ "name" => "审核"
+ ],
+ "recall" => [
+ "name" => "撤回"
+ ],
+ "abort" => [
+ "name" => "弃审"
+ ],
+ "print" => [
+ "name" => "打印"
+ ],
+ "delete" => [
+ "name" => "删除"
+ ],
+ ]
+ ],
+ "record10" => [
+ "name" => "产成品入库单",
+ "actions" => [
+ "index" => [
+ "name" => "列表",
+ ],
+ "show" => [
+ "name" => "查看"
+ ],
+ "create" => [
+ "name" => "新建"
+ ],
+ "edit" => [
+ "name" => "编辑"
+ ],
+ "audit" => [
+ "name" => "审核"
+ ],
+ "recall" => [
+ "name" => "撤回"
+ ],
+ "abort" => [
+ "name" => "弃审"
+ ],
+ "print" => [
+ "name" => "打印"
+ ],
+ "delete" => [
+ "name" => "删除"
+ ],
+ ]
+ ],
+ "record08" => [
+ "name" => "其他入库单",
+ "actions" => [
+ "index" => [
+ "name" => "列表",
+ ],
+ "show" => [
+ "name" => "查看"
+ ],
+ "create" => [
+ "name" => "新建"
+ ],
+ "edit" => [
+ "name" => "编辑"
+ ],
+ "audit" => [
+ "name" => "审核"
+ ],
+ "recall" => [
+ "name" => "撤回"
+ ],
+ "abort" => [
+ "name" => "弃审"
+ ],
+ "print" => [
+ "name" => "打印"
+ ],
+ "delete" => [
+ "name" => "删除"
+ ],
+ ]
+ ],
+ "record09" => [
+ "name" => "其他出库单",
+ "actions" => [
+ "index" => [
+ "name" => "列表",
+ ],
+ "show" => [
+ "name" => "查看"
+ ],
+ "create" => [
+ "name" => "新建"
+ ],
+ "edit" => [
+ "name" => "编辑"
+ ],
+ "audit" => [
+ "name" => "审核"
+ ],
+ "recall" => [
+ "name" => "撤回"
+ ],
+ "abort" => [
+ "name" => "弃审"
+ ],
+ "print" => [
+ "name" => "打印"
+ ],
+ "delete" => [
+ "name" => "删除"
+ ],
+ ]
+ ],
+ "record11" => [
+ "name" => "原材料出库单",
+ "actions" => [
+ "index" => [
+ "name" => "列表",
+ ],
+ "show" => [
+ "name" => "查看"
+ ],
+ "create" => [
+ "name" => "新建"
+ ],
+ "edit" => [
+ "name" => "编辑"
+ ],
+ "audit" => [
+ "name" => "审核"
+ ],
+ "recall" => [
+ "name" => "撤回"
+ ],
+ "abort" => [
+ "name" => "弃审"
+ ],
+ "print" => [
+ "name" => "打印"
+ ],
+ "delete" => [
+ "name" => "删除"
+ ],
+ ]
+ ],
+ "warehouse" => [
+ "name" => "仓库档案",
+ "actions" => [
+ "index" => [
+ "name" => "列表"
+ ],
+ "create" => [
+ "name" => "新建"
+ ],
+ "edit" => [
+ "name" => "编辑"
+ ],
+ "delete" => [
+ "name" => "删除"
+ ]
+ ]
+ ],
+ "location" => [
+ "name" => "仓库货位",
+ "actions" => [
+ "index" => [
+ "name" => "列表"
+ ],
+ "create" => [
+ "name" => "新建"
+ ],
+ "edit" => [
+ "name" => "编辑"
+ ],
+ "delete" => [
+ "name" => "删除"
+ ]
+ ]
+ ],
+ "type" => [
+ "name" => "库存类型",
+ "actions" => [
+ "index" => [
+ "name" => "列表"
+ ],
+ "create" => [
+ "name" => "新建"
+ ],
+ "edit" => [
+ "name" => "编辑"
+ ],
+ "delete" => [
+ "name" => "删除"
+ ]
+ ]
+ ],
+ "category" => [
+ "name" => "库存类别",
+ "actions" => [
+ "index" => [
+ "name" => "列表"
+ ],
+ "create" => [
+ "name" => "新建"
+ ],
+ "edit" => [
+ "name" => "编辑"
+ ],
+ "delete" => [
+ "name" => "删除"
+ ]
+ ]
+ ],
+ "report" => [
+ "name" => "报表",
+ "actions" => [
+ "stockDetail" => [
+ "name" => "库存明细表"
+ ],
+ "stockTotal" => [
+ "name" => "库存汇总表"
+ ],
+ "stockInOut" => [
+ "name" => "进销存汇总表"
+ ],
+ ]
+ ]
+ ]
+];
diff --git a/app/Gdoo/Stock/views/allocation/create.blade.php b/app/Gdoo/Stock/views/allocation/create.blade.php
new file mode 100644
index 00000000..942c9f25
--- /dev/null
+++ b/app/Gdoo/Stock/views/allocation/create.blade.php
@@ -0,0 +1,451 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/allocation/index.blade.php b/app/Gdoo/Stock/views/allocation/index.blade.php
new file mode 100644
index 00000000..19c2dd6d
--- /dev/null
+++ b/app/Gdoo/Stock/views/allocation/index.blade.php
@@ -0,0 +1,60 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/allocation/logistics.html b/app/Gdoo/Stock/views/allocation/logistics.html
new file mode 100644
index 00000000..afb3935c
--- /dev/null
+++ b/app/Gdoo/Stock/views/allocation/logistics.html
@@ -0,0 +1,105 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/allocation/print.blade.php b/app/Gdoo/Stock/views/allocation/print.blade.php
new file mode 100644
index 00000000..ca2d71da
--- /dev/null
+++ b/app/Gdoo/Stock/views/allocation/print.blade.php
@@ -0,0 +1 @@
+{{$form['tpl']}}
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/allocation/print/118.blade.php b/app/Gdoo/Stock/views/allocation/print/118.blade.php
new file mode 100644
index 00000000..29947821
--- /dev/null
+++ b/app/Gdoo/Stock/views/allocation/print/118.blade.php
@@ -0,0 +1,108 @@
+
+
{{$setting['print_title']}}产成品调拨单
+
+
+
+
单据编号:{{$master['sn']}}
+
调拨日期:{{$master['invoice_dt']}}
+
转出仓库:{{$master['out_warehouse_name']}}
+
+
+
转入仓库:{{$master['in_warehouse_name']}}
+
备注:{{$master['remark']}}
+
+
+
+
+
+
+
+
+
+
产品编码
+
产品名称
+
规格型号
+
单位
+
数量
+
批号
+
货位
+
+
+
+@foreach($rows as $row)
+
+
{{$row['product_code']}}
+
{{$row['product_name']}}
+
{{$row['product_spec']}}
+
{{$row['product_unit']}}
+
@number($row['quantity'], 2)
+
{{$row['batch_sn']}}
+
{{$row['posname']}}
+
+@endforeach
+
+
+
合计
+
+
+
+
###
+
+
+
+
+
+
+
+
+
+
+
+
制单人:{{$master['created_by']}}
+
库管员:{{$warehouse_by}}
+
第##页,##页
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/allocation/print/125.blade.php b/app/Gdoo/Stock/views/allocation/print/125.blade.php
new file mode 100644
index 00000000..fe635301
--- /dev/null
+++ b/app/Gdoo/Stock/views/allocation/print/125.blade.php
@@ -0,0 +1,127 @@
+
+
{{$setting['print_title']}}产成品调拨单
+
+
+
+
单据编号:{{$master['sn']}}
+
单据日期:{{$master['invoice_dt']}}
+
发货日期:{{$master['delivery_dt']}}
+
+
+
转入仓库:{{$master['in_warehouse_name']}}
+
转出仓库:{{$master['out_warehouse_name']}}
+
+
+
+
备注:{{$master['remark']}}
+
+
+
+
+
+
+
+
+
+
+
产品编码
+
产品名称
+
规格型号
+
单位
+
数量
+
批号
+
货位
+
+
+
+@foreach($rows as $row)
+
+
{{$row['product_code']}}
+
{{$row['product_name']}}
+
{{$row['product_spec']}}
+
{{$row['product_unit']}}
+
@number($row['quantity'], 2)
+
{{$row['batch_sn']}}
+
{{$row['posname']}}
+
+@endforeach
+
+
+
合计
+
+
+
+
###
+
+
+
+
+
+
+
+
+
+
+
+
制单人:{{$master['created_by']}}
+
会计:李彩
+
发货:
+
仓管:
+
+
+
+
+
+
+
+
第##页,##页
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/allocation/reference.blade.php b/app/Gdoo/Stock/views/allocation/reference.blade.php
new file mode 100644
index 00000000..564b172a
--- /dev/null
+++ b/app/Gdoo/Stock/views/allocation/reference.blade.php
@@ -0,0 +1,109 @@
+
+
+
+
+
+ @include('searchForm3')
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/allocation/stockSelect.blade.php b/app/Gdoo/Stock/views/allocation/stockSelect.blade.php
new file mode 100644
index 00000000..ef409c85
--- /dev/null
+++ b/app/Gdoo/Stock/views/allocation/stockSelect.blade.php
@@ -0,0 +1,81 @@
+
+
+ @include('searchForm3')
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/cancel/create.blade.php b/app/Gdoo/Stock/views/cancel/create.blade.php
new file mode 100644
index 00000000..395af3ca
--- /dev/null
+++ b/app/Gdoo/Stock/views/cancel/create.blade.php
@@ -0,0 +1,189 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/cancel/index.blade.php b/app/Gdoo/Stock/views/cancel/index.blade.php
new file mode 100644
index 00000000..4745fd80
--- /dev/null
+++ b/app/Gdoo/Stock/views/cancel/index.blade.php
@@ -0,0 +1,59 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/cancel/print.blade.php b/app/Gdoo/Stock/views/cancel/print.blade.php
new file mode 100644
index 00000000..ca2d71da
--- /dev/null
+++ b/app/Gdoo/Stock/views/cancel/print.blade.php
@@ -0,0 +1 @@
+{{$form['tpl']}}
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/cancel/print/120.blade.php b/app/Gdoo/Stock/views/cancel/print/120.blade.php
new file mode 100644
index 00000000..17390019
--- /dev/null
+++ b/app/Gdoo/Stock/views/cancel/print/120.blade.php
@@ -0,0 +1,144 @@
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/cancel/reference.blade.php b/app/Gdoo/Stock/views/cancel/reference.blade.php
new file mode 100644
index 00000000..5703aa61
--- /dev/null
+++ b/app/Gdoo/Stock/views/cancel/reference.blade.php
@@ -0,0 +1,109 @@
+
+
+
+
+
+ @include('searchForm3')
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/category/create.blade.php b/app/Gdoo/Stock/views/category/create.blade.php
new file mode 100644
index 00000000..7878b060
--- /dev/null
+++ b/app/Gdoo/Stock/views/category/create.blade.php
@@ -0,0 +1,3 @@
+
+ {{$form['tpl']}}
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/category/dialog.blade.php b/app/Gdoo/Stock/views/category/dialog.blade.php
new file mode 100644
index 00000000..4ed8c29a
--- /dev/null
+++ b/app/Gdoo/Stock/views/category/dialog.blade.php
@@ -0,0 +1,123 @@
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/delivery/create.blade.php b/app/Gdoo/Stock/views/delivery/create.blade.php
new file mode 100644
index 00000000..b81570d8
--- /dev/null
+++ b/app/Gdoo/Stock/views/delivery/create.blade.php
@@ -0,0 +1,485 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/delivery/detail.blade.php b/app/Gdoo/Stock/views/delivery/detail.blade.php
new file mode 100644
index 00000000..19c2dd6d
--- /dev/null
+++ b/app/Gdoo/Stock/views/delivery/detail.blade.php
@@ -0,0 +1,60 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/delivery/getBatchSelect.blade.php b/app/Gdoo/Stock/views/delivery/getBatchSelect.blade.php
new file mode 100644
index 00000000..244b4d61
--- /dev/null
+++ b/app/Gdoo/Stock/views/delivery/getBatchSelect.blade.php
@@ -0,0 +1,131 @@
+
+
+ @include('searchForm3')
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/delivery/index.blade.php b/app/Gdoo/Stock/views/delivery/index.blade.php
new file mode 100644
index 00000000..f2681a50
--- /dev/null
+++ b/app/Gdoo/Stock/views/delivery/index.blade.php
@@ -0,0 +1,60 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/delivery/logistics.xml b/app/Gdoo/Stock/views/delivery/logistics.xml
new file mode 100644
index 00000000..9044765f
--- /dev/null
+++ b/app/Gdoo/Stock/views/delivery/logistics.xml
@@ -0,0 +1,87 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/delivery/print.blade.php b/app/Gdoo/Stock/views/delivery/print.blade.php
new file mode 100644
index 00000000..ca2d71da
--- /dev/null
+++ b/app/Gdoo/Stock/views/delivery/print.blade.php
@@ -0,0 +1 @@
+{{$form['tpl']}}
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/delivery/print/112.blade.php b/app/Gdoo/Stock/views/delivery/print/112.blade.php
new file mode 100644
index 00000000..11a2b03e
--- /dev/null
+++ b/app/Gdoo/Stock/views/delivery/print/112.blade.php
@@ -0,0 +1,203 @@
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/delivery/print/87.blade.php b/app/Gdoo/Stock/views/delivery/print/87.blade.php
new file mode 100644
index 00000000..fda19ac7
--- /dev/null
+++ b/app/Gdoo/Stock/views/delivery/print/87.blade.php
@@ -0,0 +1,163 @@
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/delivery/reference.blade.php b/app/Gdoo/Stock/views/delivery/reference.blade.php
new file mode 100644
index 00000000..564b172a
--- /dev/null
+++ b/app/Gdoo/Stock/views/delivery/reference.blade.php
@@ -0,0 +1,109 @@
+
+
+
+
+
+ @include('searchForm3')
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/direct/create.blade.php b/app/Gdoo/Stock/views/direct/create.blade.php
new file mode 100644
index 00000000..7e6e84a9
--- /dev/null
+++ b/app/Gdoo/Stock/views/direct/create.blade.php
@@ -0,0 +1,411 @@
+
+
+
+
+ {{$form['btn']}}
+
+
+
+ {{$form['tpl']}}
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/direct/index.blade.php b/app/Gdoo/Stock/views/direct/index.blade.php
new file mode 100644
index 00000000..19c2dd6d
--- /dev/null
+++ b/app/Gdoo/Stock/views/direct/index.blade.php
@@ -0,0 +1,60 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/direct/print.blade.php b/app/Gdoo/Stock/views/direct/print.blade.php
new file mode 100644
index 00000000..ca2d71da
--- /dev/null
+++ b/app/Gdoo/Stock/views/direct/print.blade.php
@@ -0,0 +1 @@
+{{$form['tpl']}}
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/direct/print/126.blade.php b/app/Gdoo/Stock/views/direct/print/126.blade.php
new file mode 100644
index 00000000..88a3a33b
--- /dev/null
+++ b/app/Gdoo/Stock/views/direct/print/126.blade.php
@@ -0,0 +1,163 @@
+
+
{{$setting['print_title']}}发货单
+
+
+
+
客户名称:{{$master['tax_name']}}
+
发货日期:{{$master['invoice_dt']}}
+
+
+
单据编号:{{$master['sn']}}
+
销售类型:{{$master['type_name']}}
+
+
+
备注:{{$master['remark']}}
+
+
+
+
+
+
+
+
+
+
+
产品名称
+
规格型号
+
单位
+
数量
+
批次
+
B
+
单价
+
金额
+
重量(kg)
+
+
+
+ @foreach($rows as $row)
+
+
+ {{$row['product_name']}}
+
+
+ {{$row['product_spec']}}
+
+
+ {{$row['product_unit']}}
+
+
+ {{floatval($row['quantity'])}}
+
+
+ {{$row['batch_sn']}}
+
+
+ {{$row['warehouse_type']}}
+
+
+ @number($row['price'], 2)
+
+
+ @number($row['money'], 2)
+
+
+ @number($row['total_weight'], 2)
+
+
+ @endforeach
+
+ @if($money < 0)
+
+
折扣额
+
+
+
+
+
+
+
@number($money, 2)
+
+
+ @endif
+
+
+
合计
+
+
+
{{floatval($rows->sum('quantity'))}}
+
+
+
+
@number($rows->sum('money') + $money, 2)
+
@number($rows->sum('total_weight'), 2)
+
+
+
+
+
+
+
+
+
+
制单:{{$master['created_by']}}
+
财务:李彩
+
发货:
+
仓管:
+
+
+
+
+
+
第##页,##页
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/direct/reference.blade.php b/app/Gdoo/Stock/views/direct/reference.blade.php
new file mode 100644
index 00000000..5703aa61
--- /dev/null
+++ b/app/Gdoo/Stock/views/direct/reference.blade.php
@@ -0,0 +1,109 @@
+
+
+
+
+
+ @include('searchForm3')
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/location/create.blade.php b/app/Gdoo/Stock/views/location/create.blade.php
new file mode 100644
index 00000000..db7f6d80
--- /dev/null
+++ b/app/Gdoo/Stock/views/location/create.blade.php
@@ -0,0 +1,3 @@
+
+ {{$form['tpl']}}
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/location/dialog.blade.php b/app/Gdoo/Stock/views/location/dialog.blade.php
new file mode 100644
index 00000000..40aa50e3
--- /dev/null
+++ b/app/Gdoo/Stock/views/location/dialog.blade.php
@@ -0,0 +1,122 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/location/index.blade.php b/app/Gdoo/Stock/views/location/index.blade.php
new file mode 100644
index 00000000..d6cbfa5f
--- /dev/null
+++ b/app/Gdoo/Stock/views/location/index.blade.php
@@ -0,0 +1,57 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record01/create.blade.php b/app/Gdoo/Stock/views/record01/create.blade.php
new file mode 100644
index 00000000..b7dfd9a0
--- /dev/null
+++ b/app/Gdoo/Stock/views/record01/create.blade.php
@@ -0,0 +1,130 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record01/index.blade.php b/app/Gdoo/Stock/views/record01/index.blade.php
new file mode 100644
index 00000000..4745fd80
--- /dev/null
+++ b/app/Gdoo/Stock/views/record01/index.blade.php
@@ -0,0 +1,59 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record01/print.blade.php b/app/Gdoo/Stock/views/record01/print.blade.php
new file mode 100644
index 00000000..ca2d71da
--- /dev/null
+++ b/app/Gdoo/Stock/views/record01/print.blade.php
@@ -0,0 +1 @@
+{{$form['tpl']}}
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record08/create.blade.php b/app/Gdoo/Stock/views/record08/create.blade.php
new file mode 100644
index 00000000..4ab913da
--- /dev/null
+++ b/app/Gdoo/Stock/views/record08/create.blade.php
@@ -0,0 +1,122 @@
+
+
+
+
+ {{$form['btn']}}
+
+
+
+ {{$form['tpl']}}
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record08/index.blade.php b/app/Gdoo/Stock/views/record08/index.blade.php
new file mode 100644
index 00000000..4745fd80
--- /dev/null
+++ b/app/Gdoo/Stock/views/record08/index.blade.php
@@ -0,0 +1,59 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record08/print.blade.php b/app/Gdoo/Stock/views/record08/print.blade.php
new file mode 100644
index 00000000..ca2d71da
--- /dev/null
+++ b/app/Gdoo/Stock/views/record08/print.blade.php
@@ -0,0 +1 @@
+{{$form['tpl']}}
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record08/print/117.blade.php b/app/Gdoo/Stock/views/record08/print/117.blade.php
new file mode 100644
index 00000000..ea479c73
--- /dev/null
+++ b/app/Gdoo/Stock/views/record08/print/117.blade.php
@@ -0,0 +1,87 @@
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record08/reference.blade.php b/app/Gdoo/Stock/views/record08/reference.blade.php
new file mode 100644
index 00000000..564b172a
--- /dev/null
+++ b/app/Gdoo/Stock/views/record08/reference.blade.php
@@ -0,0 +1,109 @@
+
+
+
+
+
+ @include('searchForm3')
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record09/create.blade.php b/app/Gdoo/Stock/views/record09/create.blade.php
new file mode 100644
index 00000000..702fa29d
--- /dev/null
+++ b/app/Gdoo/Stock/views/record09/create.blade.php
@@ -0,0 +1,295 @@
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record09/index.blade.php b/app/Gdoo/Stock/views/record09/index.blade.php
new file mode 100644
index 00000000..4745fd80
--- /dev/null
+++ b/app/Gdoo/Stock/views/record09/index.blade.php
@@ -0,0 +1,59 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record09/print.blade.php b/app/Gdoo/Stock/views/record09/print.blade.php
new file mode 100644
index 00000000..ca2d71da
--- /dev/null
+++ b/app/Gdoo/Stock/views/record09/print.blade.php
@@ -0,0 +1 @@
+{{$form['tpl']}}
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record09/print/115.blade.php b/app/Gdoo/Stock/views/record09/print/115.blade.php
new file mode 100644
index 00000000..4a1c6e1a
--- /dev/null
+++ b/app/Gdoo/Stock/views/record09/print/115.blade.php
@@ -0,0 +1,87 @@
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record09/reference.blade.php b/app/Gdoo/Stock/views/record09/reference.blade.php
new file mode 100644
index 00000000..564b172a
--- /dev/null
+++ b/app/Gdoo/Stock/views/record09/reference.blade.php
@@ -0,0 +1,109 @@
+
+
+
+
+
+ @include('searchForm3')
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record10/create.blade.php b/app/Gdoo/Stock/views/record10/create.blade.php
new file mode 100644
index 00000000..5cb0b156
--- /dev/null
+++ b/app/Gdoo/Stock/views/record10/create.blade.php
@@ -0,0 +1,103 @@
+
+
+
+
+ {{$form['btn']}}
+
+
+
+ {{$form['tpl']}}
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record10/index.blade.php b/app/Gdoo/Stock/views/record10/index.blade.php
new file mode 100644
index 00000000..4745fd80
--- /dev/null
+++ b/app/Gdoo/Stock/views/record10/index.blade.php
@@ -0,0 +1,59 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record10/print.blade.php b/app/Gdoo/Stock/views/record10/print.blade.php
new file mode 100644
index 00000000..e23ef6dd
--- /dev/null
+++ b/app/Gdoo/Stock/views/record10/print.blade.php
@@ -0,0 +1,26 @@
+
+
+{{$form['tpl']}}
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record10/print/117.blade.php b/app/Gdoo/Stock/views/record10/print/117.blade.php
new file mode 100644
index 00000000..a0e0d145
--- /dev/null
+++ b/app/Gdoo/Stock/views/record10/print/117.blade.php
@@ -0,0 +1,87 @@
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record10/print3/117.blade.php b/app/Gdoo/Stock/views/record10/print3/117.blade.php
new file mode 100644
index 00000000..19d07a1f
--- /dev/null
+++ b/app/Gdoo/Stock/views/record10/print3/117.blade.php
@@ -0,0 +1,45 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record10/reference.blade.php b/app/Gdoo/Stock/views/record10/reference.blade.php
new file mode 100644
index 00000000..564b172a
--- /dev/null
+++ b/app/Gdoo/Stock/views/record10/reference.blade.php
@@ -0,0 +1,109 @@
+
+
+
+
+
+ @include('searchForm3')
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record11/create.blade.php b/app/Gdoo/Stock/views/record11/create.blade.php
new file mode 100644
index 00000000..f5c9332d
--- /dev/null
+++ b/app/Gdoo/Stock/views/record11/create.blade.php
@@ -0,0 +1,128 @@
+
+
+
+
+ {{$form['btn']}}
+
+
+
+
+ {{$form['tpl']}}
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record11/index.blade.php b/app/Gdoo/Stock/views/record11/index.blade.php
new file mode 100644
index 00000000..4745fd80
--- /dev/null
+++ b/app/Gdoo/Stock/views/record11/index.blade.php
@@ -0,0 +1,59 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record11/print.blade.php b/app/Gdoo/Stock/views/record11/print.blade.php
new file mode 100644
index 00000000..ca2d71da
--- /dev/null
+++ b/app/Gdoo/Stock/views/record11/print.blade.php
@@ -0,0 +1 @@
+{{$form['tpl']}}
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/record11/reference.blade.php b/app/Gdoo/Stock/views/record11/reference.blade.php
new file mode 100644
index 00000000..564b172a
--- /dev/null
+++ b/app/Gdoo/Stock/views/record11/reference.blade.php
@@ -0,0 +1,109 @@
+
+
+
+
+
+ @include('searchForm3')
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/report/stockDetail.blade.php b/app/Gdoo/Stock/views/report/stockDetail.blade.php
new file mode 100644
index 00000000..606aa458
--- /dev/null
+++ b/app/Gdoo/Stock/views/report/stockDetail.blade.php
@@ -0,0 +1,137 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/report/stockInOut.blade.php b/app/Gdoo/Stock/views/report/stockInOut.blade.php
new file mode 100644
index 00000000..a1c1a529
--- /dev/null
+++ b/app/Gdoo/Stock/views/report/stockInOut.blade.php
@@ -0,0 +1,147 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/report/stockTotal.blade.php b/app/Gdoo/Stock/views/report/stockTotal.blade.php
new file mode 100644
index 00000000..35fd22cc
--- /dev/null
+++ b/app/Gdoo/Stock/views/report/stockTotal.blade.php
@@ -0,0 +1,144 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/type/create.blade.php b/app/Gdoo/Stock/views/type/create.blade.php
new file mode 100644
index 00000000..ed99e357
--- /dev/null
+++ b/app/Gdoo/Stock/views/type/create.blade.php
@@ -0,0 +1,3 @@
+
+ {{$form['tpl']}}
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/type/dialog.blade.php b/app/Gdoo/Stock/views/type/dialog.blade.php
new file mode 100644
index 00000000..7de72639
--- /dev/null
+++ b/app/Gdoo/Stock/views/type/dialog.blade.php
@@ -0,0 +1,122 @@
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/warehouse/create.blade.php b/app/Gdoo/Stock/views/warehouse/create.blade.php
new file mode 100644
index 00000000..89fca55b
--- /dev/null
+++ b/app/Gdoo/Stock/views/warehouse/create.blade.php
@@ -0,0 +1,3 @@
+
+ {{$form['tpl']}}
+
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/warehouse/dialog.blade.php b/app/Gdoo/Stock/views/warehouse/dialog.blade.php
new file mode 100644
index 00000000..40aa50e3
--- /dev/null
+++ b/app/Gdoo/Stock/views/warehouse/dialog.blade.php
@@ -0,0 +1,122 @@
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/Stock/views/warehouse/permission.blade.php b/app/Gdoo/Stock/views/warehouse/permission.blade.php
new file mode 100644
index 00000000..1c92515c
--- /dev/null
+++ b/app/Gdoo/Stock/views/warehouse/permission.blade.php
@@ -0,0 +1,43 @@
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/System/views/mail/edit.blade.php b/app/Gdoo/System/views/mail/edit.blade.php
new file mode 100644
index 00000000..71eca222
--- /dev/null
+++ b/app/Gdoo/System/views/mail/edit.blade.php
@@ -0,0 +1,60 @@
+
+
+
+
+
名称
+
+
+
+
+
+
邮箱帐号
+
+
+
+
+
+
邮箱密码
+
+
+
+
+
+
SMTP服务器
+
+
+
+
+
+
服务器端口
+
+
+
+
+
+
连接方式
+
+
+
+
+
+
状态
+
+
+
+
+
+
排序
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/System/views/mail/index.blade.php b/app/Gdoo/System/views/mail/index.blade.php
new file mode 100644
index 00000000..6c845749
--- /dev/null
+++ b/app/Gdoo/System/views/mail/index.blade.php
@@ -0,0 +1,83 @@
+{{$header["js"]}}
+
+
+ @include('headers')
+
+
+
+
+
+@include('footers')
\ No newline at end of file
diff --git a/app/Gdoo/System/views/mail/test.blade.php b/app/Gdoo/System/views/mail/test.blade.php
new file mode 100644
index 00000000..140edb99
--- /dev/null
+++ b/app/Gdoo/System/views/mail/test.blade.php
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/System/views/media/create.blade.php b/app/Gdoo/System/views/media/create.blade.php
new file mode 100644
index 00000000..30f8ea30
--- /dev/null
+++ b/app/Gdoo/System/views/media/create.blade.php
@@ -0,0 +1,62 @@
+
+
+
+@endverbatim
+
+
\ No newline at end of file
diff --git a/app/Gdoo/System/views/media/folder.blade.php b/app/Gdoo/System/views/media/folder.blade.php
new file mode 100644
index 00000000..04cecfae
--- /dev/null
+++ b/app/Gdoo/System/views/media/folder.blade.php
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/Gdoo/System/views/media/qrcode.blade.php b/app/Gdoo/System/views/media/qrcode.blade.php
new file mode 100644
index 00000000..dbf7cf66
--- /dev/null
+++ b/app/Gdoo/System/views/media/qrcode.blade.php
@@ -0,0 +1,108 @@
+
+
+