Question: how to add database connection as extension for inserting link as external link #643
-
Hello, sorry for noob question. i read "how to create custom extension" but a have problem to configuration <?php
namespace League\CommonMark\Extension\PhpMyadmin;
use League\CommonMark\ConfigurableEnvironmentInterface;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Extension\ExtensionInterface;
class PhpMyadminExtension implements ExtensionInterface
{
public function register(ConfigurableEnvironmentInterface $environment)
{
$environment->addInlineRenderer(PhpMyadmin::class, new PhpMyadminRenderer(
(string) $environment->getConfig('PhpMyadmin/host', 'localhost'),
(string) $environment->getConfig('PhpMyadmin/db_user', 'root'),
(string) $environment->getConfig('PhpMyadmin/db_pass', ''),
(string) $environment->getConfig('PhpMyadmin/db_name', '')
));
}
} PhpMyadminRender <?php
namespace League\CommonMark\Extension\PhpMyadmin;
use League\CommonMark\ConfigurableEnvironmentInterface;
use League\CommonMark\ElementRendererInterface;
use League\CommonMark\Inline\Renderer\InlineRendererInterface;
Class PhpMyadminRenderer implements InlineRendererInterface {
/** @var getConfig */
Private $servername;
Private $username;
Private $password;
Private $database;
/** @var EnvironmentInterface */
private $environment;
/**
* @param EnvironmentInterface $environment
*/
public function __construct(string $servername, string $username, string $password, string $database)
{
$this->servername = $servername;
$this->username = $username;
$this->password = $password;
$this->database = $database;
}
protected function connect() {
/** @var connection */
$con = new mysqli($this->servername, $this->username, $this->password, $this->database);
if($con->connect_error){
die("Connection failed: [{$conn->connect_error}]");
}
}
}
?> i want to add database connection for this use League\CommonMark\Extension\PhpMyadmin\PhpmadminRenderer;
final class LinkRenderer implements InlineRendererInterface, ConfigurationAwareInterface
{
.....
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
{
if (!($inline instanceof Link)) {
throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline));
}
$attrs = $inline->getData('attributes', []);
$forbidUnsafeLinks = !$this->config->get('allow_unsafe_links');
if (!($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($inline->getUrl()))) {
$Punycode = new PunyCodeRenderer();
$uri = $Punycode->encode($inline->getUrl());
$attrs['href'] = $this->check_Link($uri);
}
if (isset($inline->data['title'])) {
$attrs['title'] = $inline->data['title'];
}
if (isset($attrs['target']) && $attrs['target'] === '_blank' && !isset($attrs['rel'])) {
$attrs['rel'] = 'noopener noreferrer';
}
return new HtmlElement('a', $attrs, $htmlRenderer->renderInlines($inline->children()));
}
public function check_Link(string $uri){
$result = $this->connect()->query("SELECT * FROM external_link where url ='{$uri}'");
if (preg_match($this->REGEX, $uri)) {
if (!preg_match($this->whitelistedDomain, $uri)) {
if ($result->num_rows == 1) {
$data = $result->fetch_assoc();
return "/redirect.php?hash={$data['hash']}&url={$data['url']}";
}else{
return $this->insert_Link($uri);
}
}else{
return $uri;
}
}else{
return $uri;
}
}
public function insert_Link(string $uri){
$hash = substr(hash('sha256', md5(random_bytes(20))), 0, 20);
$this->connect()->query("INSERT external_link(`url`,`hash`,`date`) VALUES ('{$uri}','{$hash}',current_date)");
return $this->check_Link($uri);
} Error:
|
Beta Was this translation helpful? Give feedback.
Answered by
colinodell
Aug 23, 2020
Replies: 1 comment
-
The fatal error you're receiving states that your |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
colinodell
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The fatal error you're receiving states that your
PhpMyadminRenderer
class is missing therender
method (which is required by theInlineRendererInterface
you are implementing. Once you define that method the error will go away :)