Skip to content

Commit

Permalink
Improve load Meta data
Browse files Browse the repository at this point in the history
  • Loading branch information
sonvnn committed Dec 10, 2024
1 parent f67caa6 commit 1ddf311
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 3 deletions.
13 changes: 10 additions & 3 deletions framework/library/astroid/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,16 @@ public function addMeta()
$og_description = $this->article->params->get('astroid_og_desc', '');
}
$images = json_decode($this->article->images);
if (isset($images->image_intro) && !empty($images->image_intro)) {
$og_image = Uri::base() . htmlspecialchars($images->image_intro, ENT_COMPAT, 'UTF-8');
if (isset($images->image_fulltext) && !empty($images->image_fulltext)) {
$img = Helper::cleanImageUrl($images->image_fulltext);
$og_image = Uri::base() . htmlspecialchars($img->url, ENT_COMPAT, 'UTF-8');
} elseif (isset($images->image_intro) && !empty($images->image_intro)) {
$img = Helper::cleanImageUrl($images->image_intro);
$og_image = Uri::base() . htmlspecialchars($img->url, ENT_COMPAT, 'UTF-8');
} else {
$og_image = '';
}

if (!empty($this->article->params->get('astroid_og_image', ''))) {
$og_image = Uri::base() . $this->article->params->get('astroid_og_image', '');
}
Expand Down Expand Up @@ -141,7 +148,7 @@ public function addMeta()
}
$meta = implode('', $meta);
if (!empty($meta)) {
$document = Factory::getDocument();
$document = Factory::getApplication()->getDocument();
$document->addCustomTag($meta);
}
}
Expand Down
43 changes: 43 additions & 0 deletions framework/library/astroid/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,49 @@ public static function clearJoomlaCache()
$app->getDispatcher()->dispatch('onAfterPurge', new AfterPurgeEvent('onAfterPurge'));
}

public static function cleanImageUrl($url): object
{
$obj = new \stdClass();

$obj->attributes = [
'width' => 0,
'height' => 0,
];

if ($url === null) {
$url = '';
}

$mediaUri = new Uri($url);

// Old image URL format
if ($mediaUri->hasVar('joomla_image_height')) {
$height = (int) $mediaUri->getVar('joomla_image_height');
$width = (int) $mediaUri->getVar('joomla_image_width');

$mediaUri->delVar('joomla_image_height');
$mediaUri->delVar('joomla_image_width');
} else {
// New Image URL format
$fragmentUri = new Uri($mediaUri->getFragment());
$width = (int) $fragmentUri->getVar('width', 0);
$height = (int) $fragmentUri->getVar('height', 0);
}

if ($width > 0) {
$obj->attributes['width'] = $width;
}

if ($height > 0) {
$obj->attributes['height'] = $height;
}

$mediaUri->setFragment('');
$obj->url = $mediaUri->toString();

return $obj;
}

public static function getFileHash($file)
{
$content = file_get_contents($file);
Expand Down
69 changes: 69 additions & 0 deletions framework/library/astroid/Helper/Captcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/**
* @package Astroid Framework
* @author Astroid Framework Team https://astroidframe.work
* @copyright Copyright (C) 2023 AstroidFrame.work.
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or Later
*/

namespace Astroid\Helper;

use Astroid\Helper;
use Joomla\CMS\Factory;

defined('_JEXEC') or die;

class Captcha {
public static function loadCaptcha($context = '') {
if (empty($context)) {
return '';
}
$app = Factory::getApplication();
$value1 = rand(1,100);
$value2 = rand(1,100);
$app->setUserState( $context.'.value1', $value1 );
$app->setUserState( $context.'.value2', $value2 );
return '<div class="'.$context.'">'.($value1 . ' + ' . $value2 .' = ?').'</div><div class="'.$context.'-result"><input type="text" name="'.$context.'" class="form-control" placeholder="'.($value1 . ' + ' . $value2 .' = ?').'"></div>';
}

public static function getCaptcha($context = '') {
$app = Factory::getApplication();
$value1 = intval($app->getUserState( $context.'.value1' ));
$value2 = intval($app->getUserState( $context.'.value2' ));
$value_result = intval($app->input->get($context, 0, 'ALNUM'));
return ( $value1 + $value2 == $value_result );
}

public static function verifyGoogleCaptcha($gRecaptchaResponse, $secretKey = '') {
if (empty($gRecaptchaResponse)) {
return false;
}
if (empty($secretKey)) {
$pluginParams = Helper::getPluginParams();
$secretKey = $pluginParams->get('g_secret_key', '');
}
if (empty($secretKey)) {
return false;
}
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = [
'secret' => $secretKey,
'response' => $gRecaptchaResponse
];

$options = [
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
],
];

$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result, true);

return isset($response['success']) && $response['success'] === true;
}
}

0 comments on commit 1ddf311

Please sign in to comment.