Skip to content

Commit

Permalink
WIP work on CONNECT feature see #409
Browse files Browse the repository at this point in the history
+ CONNECT can display login or logout
- %CONNECT% cannot use ?id param anymore
+ added a %USER% inclusion for fun
  • Loading branch information
vincent-peugnet committed Oct 13, 2024
1 parent d2a741d commit ab835a7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 35 deletions.
48 changes: 42 additions & 6 deletions app/class/Servicepostprocess.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wcms;

use FTP\Connection;
use JsonException;

/**
Expand All @@ -20,12 +21,16 @@ class Servicepostprocess

public const VISIT_COUNT = '%VISITCOUNT%';
public const EDIT_COUNT = '%EDITCOUNT%';
public const AFF_COUNT = '%DISPLAYCOUNT%';
public const DISPLAY_COUNT = '%DISPLAYCOUNT%';
public const CONNECT = '%CONNECT%';
public const USER = '%USER%';

public const COUNTERS = [
public const POST_PROCESS_CODES = [
self::VISIT_COUNT,
self::EDIT_COUNT,
self::AFF_COUNT,
self::DISPLAY_COUNT,
self::CONNECT,
self::USER,
];

public function __construct(Page $page, User $user)
Expand Down Expand Up @@ -71,14 +76,19 @@ private function replace(string $text): string
$displaycount = $this->page->displaycount();

$replacements = [
self::VISIT_COUNT => "<span class=\"counter visitcount\">$visitcount</span>",
self::EDIT_COUNT => "<span class=\"counter editcount\">$editcount</span>",
self::AFF_COUNT => "<span class=\"counter displaycount\">$displaycount</span>",
self::VISIT_COUNT => "<span class=\"counter visitcount\">$visitcount</span>",
self::EDIT_COUNT => "<span class=\"counter editcount\">$editcount</span>",
self::DISPLAY_COUNT => "<span class=\"counter displaycount\">$displaycount</span>",
self::CONNECT => $this->connect(),
self::USER => $this->user->name(),
];
return strtr($text, $replacements);
}

/**
* Datas about given page and user in JSON
* To be printed with every pages, so it can be used in JS
*
* @return string JSON encoded w global, pages and user datas
* @throws JsonException If JSON encoding failed
*/
Expand All @@ -101,4 +111,30 @@ private function wobj(Page $page, User $user): string
];
return json_encode($wdatas, JSON_THROW_ON_ERROR);
}



/**
* Generate a login or logout form
*
* @return string HTML code
*/
protected function connect(): string
{

$form = "<form action=\"!co\" method=\"post\">\n";

if (!$this->user->isvisitor()) {
$form .= '<input type="submit" name="log" value="logout">';
} else {
$form .= '<input type="text" name="user" id="loginuser" autofocus placeholder="user" required>';
$form .= '<input type="password" name="pass" id="loginpass" placeholder="password" required>';
$form .= '<input type="submit" name="log" value="login" id="button">';
}
$pageid = $this->page->id();
$form .= '<input type="hidden" name="route" value="pageread">';
$form .= "<input type=\"hidden\" name=\"id\" value=\"$pageid\">";
$form .= '</form>';
return $form;
}
}
30 changes: 1 addition & 29 deletions app/class/Servicerender.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ protected function winclusions($text)
$text = $this->pageoptmap($text);
$text = $this->randomopt($text);
$text = $this->authors($text);
$text = $this->authenticate($text);
return $text;
}

Expand Down Expand Up @@ -851,7 +850,7 @@ protected function authors(string $text): string
*/
protected function checkpostprocessaction(string $text): bool
{
$counterpaterns = Servicepostprocess::COUNTERS;
$counterpaterns = Servicepostprocess::POST_PROCESS_CODES;
$pattern = implode('|', $counterpaterns);
return boolval(preg_match("#($pattern)#", $text));
}
Expand All @@ -872,33 +871,6 @@ protected function everylink(string $text, int $limit): string
return $text;
}



/**
* @param string $text content to analyse and replace
*
* @return string text ouput
*/
protected function authenticate(string $text): string
{
$id = $this->page->id();
$regex = '~\%CONNECT(\?dir=([a-zA-Z0-9-_]+))?\%~';
$text = preg_replace_callback($regex, function ($matches) use ($id) {
if (isset($matches[2])) {
$id = $matches[2];
}
$form = '<form action="' . Model::dirtopath('!co') . '" method="post">
<input type="text" name="user" id="loginuser" autofocus placeholder="user" required>
<input type="password" name="pass" id="loginpass" placeholder="password" required>
<input type="hidden" name="route" value="pageread">
<input type="hidden" name="id" value="' . $id . '">
<input type="submit" name="log" value="login" id="button">
</form>';
return $form;
}, $text);
return $text;
}

/**
* Render an user as a <span> HTML element
* A HTML link is added inside if user have a specified URL property
Expand Down

0 comments on commit ab835a7

Please sign in to comment.