Skip to content

Commit

Permalink
fixing ical export issues
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelfolaron committed Sep 21, 2024
1 parent 8a13a1c commit 673296f
Show file tree
Hide file tree
Showing 8 changed files with 3,397 additions and 2,222 deletions.
2 changes: 2 additions & 0 deletions .idea/leantime-oss.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/phpspec.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 23 additions & 13 deletions app/Domain/Calendar/Controllers/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

use Leantime\Core\Configuration\Environment;
use Leantime\Core\Controller\Controller;
use Leantime\Domain\Calendar\Services\Calendar;
use Leantime\Domain\Setting\Repositories\Setting as SettingRepository;
use phpDocumentor\Reflection\Exception;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -21,6 +23,8 @@ class Export extends Controller
private Environment $config;
private SettingRepository $settingsRepo;

private Calendar $calendarService;

/**
* init - initialize private variables
*
Expand All @@ -29,10 +33,15 @@ class Export extends Controller
*
* @return void
*/
public function init(Environment $config, SettingRepository $settingsRepo): void
public function init(
Environment $config,
SettingRepository $settingsRepo,
Calendar $calendarService
): void
{
$this->config = $config;
$this->settingsRepo = $settingsRepo;
$this->calendarService = $calendarService;
}

/**
Expand All @@ -45,28 +54,29 @@ public function init(Environment $config, SettingRepository $settingsRepo): void
public function run(): Response
{
if (isset($_GET['remove'])) {
$this->settingsRepo->deleteSetting("usersettings." . session("userdata.id") . ".icalSecret");

$this->settingsRepo->deleteSetting("usersettings." . session("userdata.id") . ".icalSecret");
$this->tpl->setNotification("notifications.ical_removed_success", "success");

}

//Add Post handling
if (isset($_POST['generateUrl'])) {
$uuid = Uuid::uuid4();
$icalHash = $uuid->toString();

$this->settingsRepo->saveSetting("usersettings." . session("userdata.id") . ".icalSecret", $icalHash);
try {
$this->calendarService->generateIcalHash();
$this->tpl->setNotification("notifications.ical_success", "success");
}catch(\Exception $e) {
$this->tpl->setNotification("There was a problem generating the ical hash", "error");
}

$this->tpl->setNotification("notifications.ical_success", "success");
}

$icalHash = $this->settingsRepo->getSetting("usersettings." . session("userdata.id") . ".icalSecret");
$userHash = hash('sha1', session("userdata.id") . $this->config->sessionpassword);

if (!$icalHash) {
$icalUrl = "";
} else {
$icalUrl = BASE_URL . "/calendar/ical/" . $icalHash . "_" . $userHash;
$icalUrl = "";
try {
$icalUrl = $this->calendarService->getICalUrl();
}catch(\Exception $e) {
$this->tpl->setNotification("Could not find ical URL", "error");
}

//Add delete handling
Expand Down
20 changes: 14 additions & 6 deletions app/Domain/Calendar/Controllers/Ical.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,27 @@ public function init(Calendar $calendarService): void
*/
public function run(): RedirectResponse|Response
{
$calId = $_GET['id'];

$calId = $_GET['id'] ?? "";
$idParts = explode("_", $calId);

if (count($idParts) != 2) {
return Frontcontroller::redirect(BASE_URL . "/errors/404");
}

$calendar = $this->calendarService->getIcalByHash($idParts[1], $idParts[0]);
try {

$calendar = $this->calendarService->getIcalByHash($idParts[1], $idParts[0]);

return new Response($calendar->get(), 200, [
'Content-Type' => 'text/calendar; charset=utf-8',
'Content-Disposition' => 'attachment; filename="leantime-calendar.ics"',
]);

}catch(\Exception $e) {
return Frontcontroller::redirect(BASE_URL . "/errors/404");
}


return new Response($calendar->get(), 200, [
'Content-Type' => 'text/calendar; charset=utf-8',
'Content-Disposition' => 'attachment; filename="leantime-calendar.ics"',
]);
}
}
58 changes: 55 additions & 3 deletions app/Domain/Calendar/Services/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

namespace Leantime\Domain\Calendar\Services;

use Illuminate\Support\Str;
use Leantime\Core\Configuration\Environment;
use Leantime\Core\Exceptions\MissingParameterException;
use Leantime\Core\Language as LanguageCore;
use Leantime\Core\Support\FromFormat;
use Leantime\Domain\Auth\Models\Roles;
use Leantime\Domain\Auth\Services\Auth;
use Leantime\Domain\Calendar\Repositories\Calendar as CalendarRepository;
use Leantime\Domain\Setting\Repositories\Setting;
use Leantime\Domain\Tickets\Services\Tickets;
use phpDocumentor\Reflection\Exception;
use Ramsey\Uuid\Uuid;
use Spatie\IcalendarGenerator\Components\Calendar as IcalCalendar;
use Spatie\IcalendarGenerator\Components\Event as IcalEvent;
use Spatie\IcalendarGenerator\Enums\Display;
Expand All @@ -22,15 +27,26 @@ class Calendar
private CalendarRepository $calendarRepo;
private LanguageCore $language;

private Setting $settingsRepo;

private Environment $config;

/**
* @param CalendarRepository $calendarRepo
* @param LanguageCore $language
*
*/
public function __construct(CalendarRepository $calendarRepo, LanguageCore $language)
public function __construct(
CalendarRepository $calendarRepo,
LanguageCore $language,
Setting $settingsRepo,
Environment $config,
)
{
$this->calendarRepo = $calendarRepo;
$this->language = $language;
$this->settingsRepo = $settingsRepo;
$this->config = $config;
}

/**
Expand Down Expand Up @@ -261,18 +277,24 @@ public function getIcalByHash(string $userHash, string $calHash): IcalCalendar

$calendarEvents = $this->calendarRepo->getCalendarBySecretHash($userHash, $calHash);

$eventObjects = [];
if(!$calendarEvents) {
throw new Exception("Calendar could not be retrieved");
}

$eventObjects = [];
//Create array of event objects for ical generator
foreach ($calendarEvents as $event) {

try {

$description = str_replace("\r\n", "\\n", strip_tags($event['description']));

$currentEvent = IcalEvent::create()
->image(BASE_URL . '/dist/images/favicon.png', 'image/png', Display::badge())
->startsAt(dtHelper()->parseDbDateTime($event['dateFrom'])->setToUserTimezone())
->endsAt(dtHelper()->parseDbDateTime($event['dateTo'])->setToUserTimezone())
->name($event['title'])
->description($event['description'] ?? '')
->description($description)
->uniqueIdentifier($event['id'])
->url($event['url'] ?? '');

Expand Down Expand Up @@ -412,6 +434,36 @@ public function getCalendar(int $userId): array
return $newValues;
}

public function getICalUrl() {

if(empty(session("userdata.id"))) {
throw new \Exception("Session id is not set.");
}

$userHash = hash('sha1', session("userdata.id") . $this->config->sessionPassword);
$icalHash = $this->settingsRepo->getSetting("usersettings." . session("userdata.id") . ".icalSecret");

if(empty($icalHash)) {
throw new \Exception("User has no ical hash");
}

return BASE_URL . "/calendar/ical/" . $icalHash . "_" . $userHash;

}

public function generateIcalHash() {

if(empty(session("userdata.id"))) {
throw new \Exception("Session id is not set.");
}

$uuid = Uuid::uuid4();
$icalHash = $uuid->toString();

$this->settingsRepo->saveSetting("usersettings." . session("userdata.id") . ".icalSecret", $icalHash);

}

/**
* Generates an event array for fullcalendar.io frontend.
*
Expand Down
2 changes: 1 addition & 1 deletion app/Domain/Users/Repositories/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function getUserBySha($hash): array|false

$stmn = $this->db->database->prepare($sql);
$stmn->bindValue(':hash', $hash, PDO::PARAM_STR);
$stmn->bindValue(':sessionSecret', $this->config->sessionpassword, PDO::PARAM_STR);
$stmn->bindValue(':sessionSecret', $this->config->sessionPassword, PDO::PARAM_STR);

$stmn->execute();
$values = $stmn->fetch();
Expand Down
2 changes: 1 addition & 1 deletion app/Views/Templates/sections/header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<meta name="description" content="{{ $sitename }}">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-touch-fullscreen" content="yes">
<meta name="theme-color" content="{{ $primaryColor }}">
<meta name="color-scheme" content="{{ $themeColorMode }}">
Expand Down
Loading

0 comments on commit 673296f

Please sign in to comment.