daily-scripture-module/helper.php

549 lines
11 KiB
PHP
Raw Normal View History

2022-01-04 15:41:07 +00:00
<?php
2023-07-26 09:21:40 +00:00
/*----------------------------------------------------------------------------------| io.vdm.dev |----/
Vast Development Method
/-------------------------------------------------------------------------------------------------------/
2022-01-04 15:41:07 +00:00
2023-07-26 09:21:40 +00:00
@package getBible.net
2022-01-04 15:41:07 +00:00
2023-07-26 09:21:40 +00:00
@created 3rd December, 2015
@author Llewellyn van der Merwe <https://getbible.net>
@git Get Bible <https://git.vdm.dev/getBible>
@github Get Bible <https://github.com/getBible>
@support Get Bible <https://git.vdm.dev/getBible/support>
@copyright Copyright (C) 2015. All Rights Reserved
@license GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
2022-01-04 15:41:07 +00:00
2023-07-26 09:21:40 +00:00
/------------------------------------------------------------------------------------------------------*/
2022-01-04 15:41:07 +00:00
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
use Joomla\Registry\Registry;
2022-01-04 15:41:07 +00:00
class ModDailyScriptureHelper
{
2022-01-05 19:40:59 +00:00
/**
* Params
*
* @var Registry
* @since 1.0
*/
protected Registry $params;
/**
* The Translation Version
*
* @var string
* @since 1.1
*/
protected string $version;
2022-01-05 19:40:59 +00:00
2022-01-04 15:41:07 +00:00
/**
* Scripture
*
2022-01-07 15:39:57 +00:00
* @var mixed
2022-01-05 19:40:59 +00:00
* @since 1.0
*/
protected $scripture = null;
/**
* Telegram Scripture
2022-01-05 19:40:59 +00:00
*
* @var string
* @since 1.1
2022-01-05 19:40:59 +00:00
*/
protected string $telegram = '';
2022-01-05 19:40:59 +00:00
/**
* Telegram Comments
*
* @var string
* @since 1.1
*/
protected string $comments = '';
/**
* Type
2022-01-05 19:40:59 +00:00
*
* @var int
2022-01-04 15:41:07 +00:00
* @since 1.0
*/
protected int $type;
2022-01-05 19:40:59 +00:00
/**
* The Telegram Post ID
2022-01-05 19:40:59 +00:00
*
* @var int
* @since 1.1
2022-01-05 19:40:59 +00:00
*/
protected int $id = 0;
2022-01-04 15:41:07 +00:00
/**
* Constructor.
*
2022-01-07 15:39:57 +00:00
* @param Registry|null $params the module settings
2022-01-04 15:41:07 +00:00
*
* @since 1.0
*/
public function __construct(Registry $params = null)
{
2022-01-07 15:39:57 +00:00
// we must have the params or we cant continue
if ($params)
2022-01-05 19:40:59 +00:00
{
2022-01-07 15:39:57 +00:00
// set the global params
$this->params = $params;
2022-01-05 19:40:59 +00:00
// get the version
2022-01-07 15:39:57 +00:00
$this->type = $params->get('type', 1);
// get the version
$this->version = $params->get('version', 'kjv');
2022-01-07 15:39:57 +00:00
// implementation type = 1 = gitHub
if ($this->type == 1)
{
// the link to the scripture for the day
$path = "https://raw.githubusercontent.com/trueChristian/daily-scripture/master/scripture/{$this->version}/README.json";
2022-01-07 15:39:57 +00:00
// get the scripture object
$this->scripture = $this->getFileContents($path);
}
}
}
/**
* get the Telegram Post ID
*
* @return int
* @since 1.1
*/
protected function getId(): int
{
if ($this->id == 0)
{
// the link to the scripture for the day
$path = "https://raw.githubusercontent.com/trueChristian/daily-scripture/master/scripture/{$this->version}/README.tg.id";
// get the scripture object
$id = trim($this->getFileContents($path, false));
// make sure we have a number here
if (is_numeric($id))
2022-01-07 15:39:57 +00:00
{
$this->id = (int) $id;
2022-01-07 15:39:57 +00:00
}
2022-01-05 19:40:59 +00:00
}
return $this->id;
2022-01-04 15:41:07 +00:00
}
/**
* Waco method to get an scripture value
*
* @param mixed $name Name of the value to retrieve.
*
* @return mixed The request value
* @since 1.0
*/
public function __get($key)
2022-01-04 15:41:07 +00:00
{
if ($this->type == 1 && $this->checkScripture($key))
2022-01-04 15:41:07 +00:00
{
return $this->scripture->{$key};
2022-01-04 15:41:07 +00:00
}
2023-11-10 08:28:35 +00:00
elseif ($this->type == 1 && $key === 'local_link')
{
return $this->getLocalLink();
}
elseif ($this->type == 2 && $key === 'telegram')
2022-01-05 19:40:59 +00:00
{
return $this->getTelegram();
2022-01-05 19:40:59 +00:00
}
elseif ($key === 'comments')
{
return $this->getComments();
}
2022-01-04 15:41:07 +00:00
return null;
}
2023-11-10 08:28:35 +00:00
/**
* get the local link
*
* @return string|null
*
* @since 1.2
*/
protected function getLocalLink(): ?string
{
2023-11-10 08:52:15 +00:00
$link = $this->params->get('local_link');
if (empty($link))
2023-11-10 08:28:35 +00:00
{
return null;
}
return "$link/{$this->version}/{$this->book}/{$this->chapter}/{$this->verse}";
}
2022-01-05 19:40:59 +00:00
/**
* get the Telegram script
*
* @return string|null
*
* @since 1.1
*/
protected function getTelegram(): ?string
{
if (empty($this->telegram))
{
$this->setTelegram();
}
return $this->checkString($this->telegram) ? $this->telegram : null;
}
/**
* set the Telegram script
*
2022-01-07 15:39:57 +00:00
* @return void
2022-01-05 19:40:59 +00:00
*
* @since 1.0
*/
protected function setTelegram()
{
// validate the ID
if (($id = $this->getId()) > 0)
2022-01-05 19:40:59 +00:00
{
// get the color
$color = $this->getColor();
// get the dark theme
$dark = $this->getDarkTheme();
// get the width
$width = $this->params->get('width', 100);
2022-01-05 19:40:59 +00:00
// get the userpic
$userpic = $this->getUserPic();
// set the script
$this->telegram = "<script async src=\"https://telegram.org/js/telegram-widget.js?22\" data-telegram-post=\"daily_scripture/$id\" data-width=\"$width%\"${color}${userpic}${dark}></script>";
}
}
/**
* get the Telegram Comment script
*
* @return string|null
*
* @since 1.1
*/
protected function getComments(): ?string
{
if (empty($this->comments))
{
$this->setComments();
}
return $this->checkString($this->comments) ? $this->comments : null;
}
/**
* set the Telegram script
*
* @return void
*
* @since 1.1
*/
protected function setComments()
{
// should we add comments
if (($id = $this->getId()) > 0 && $this->params->get('show_comments', 0) == 1)
{
// get the color
$color = $this->getColor();
2022-01-05 19:40:59 +00:00
// get the dark theme
$dark = $this->getDarkTheme();
// get comment limit
$limit = $this->params->get('comments_limit', 5);
// get comment Height
$height = $this->getCommentHeight();
// get color ful switch
$colorful = $this->getCommentColorful();
2022-01-05 19:40:59 +00:00
// set the script
$this->comments = "<script async src=\"https://telegram.org/js/telegram-widget.js?22\" data-telegram-discussion=\"daily_scripture/$id\" data-comments-limit=\"$limit\"${colorful}${height}${color}${dark}></script>";
2022-01-05 19:40:59 +00:00
}
}
/**
* get the comment height
*
* @return string height value
2022-01-05 19:40:59 +00:00
*
* @since 1.1
*/
protected function getCommentHeight()
{
if (($height = $this->params->get('comments_height')) > 300)
{
return " data-height=\"$height\"";
}
return '';
}
/**
* get the comment color ful switch
2022-01-07 15:39:57 +00:00
*
* @return string height value
2022-01-05 19:40:59 +00:00
*
* @since 1.1
2022-01-05 19:40:59 +00:00
*/
protected function getCommentColorful()
2022-01-05 19:40:59 +00:00
{
if (($colorful = $this->params->get('comments_colorful', 0)) == 1)
{
return " data-colorful=\"1\"";
}
return '';
2022-01-05 19:40:59 +00:00
}
/**
* get the color
*
* @return string The telegram script
*
* @since 1.0
*/
protected function getColor()
{
// get the color
$color = $this->params->get('color', 1);
// convert to color
switch($color)
{
case 2:
// Cyan
$color = '13B4C6';
$dark_color = '39C4E8';
break;
case 3:
// Green
$color = '29B127';
$dark_color = '72E350';
break;
case 4:
// Yellow
$color = 'CA9C0E';
$dark_color = 'F0B138';
break;
case 5:
// Red
$color = 'E22F38';
$dark_color = 'F95C54';
break;
case 6:
// White
$color = '343638';
$dark_color = 'FFFFFF';
break;
case 7:
// custom color
$color = strtoupper(trim($this->params->get('custom_color', 'F646A4'), '#'));
$dark_color = null;
break;
default:
// default
$color = null;
$dark_color = null;
break;
}
// load colors if set
if ($color)
{
$color = " data-color=\"$color\"";
// load dark color if set
if ($dark_color)
{
$color = "$color data-dark-color=\"$dark_color\"";
}
return $color;
}
return '';
}
/**
* get the user pic state
*
* @return string data-userpic value
*
* @since 1.0
*/
protected function getUserPic()
{
// get the author_photo
$author_photo = $this->params->get('author_photo', 1);
// convert to userpic
switch($author_photo)
{
case 2:
// Always show
$userpic = 'true';
break;
case 3:
// Always hide
$userpic = 'false';
break;
default:
// Auto
$userpic = null;
break;
}
// load userpic if set
if ($userpic)
{
$userpic = " data-userpic=\"$userpic\"";
return $userpic;
}
return '';
}
/**
* get the dark theme state
*
* @return string data-dark value
*
* @since 1.0
*/
protected function getDarkTheme()
{
// get the theme
$theme = $this->params->get('theme', 1);
// only load if dark theme is set
if ($theme == 2)
{
return " data-dark=\"1\"";
}
return '';
}
2022-01-04 15:41:07 +00:00
/**
* get the file content
*
* @input string $path The path to get remotely
*
* @returns mixed on success
2022-01-04 15:41:07 +00:00
*
* @since 1.0
*/
2022-01-05 19:40:59 +00:00
protected function getFileContents($path, $json = true)
2022-01-04 15:41:07 +00:00
{
// use basic file get content for now
if (($content = @file_get_contents($path)) !== FALSE)
{
// return if found
2022-01-05 19:40:59 +00:00
if ($json)
{
if ($this->checkJson($content))
{
return json_decode($content);
}
}
elseif ($this->checkString($content))
2022-01-04 15:41:07 +00:00
{
2022-01-05 19:40:59 +00:00
return $content;
2022-01-04 15:41:07 +00:00
}
}
// use curl if available
elseif (function_exists('curl_version'))
{
// start curl
$ch = curl_init();
// set the options
$options = array();
$options[CURLOPT_URL] = $path;
$options[CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
$options[CURLOPT_RETURNTRANSFER] = TRUE;
$options[CURLOPT_SSL_VERIFYPEER] = FALSE;
// load the options
curl_setopt_array($ch, $options);
// get the content
$content = curl_exec($ch);
// close the connection
curl_close($ch);
// return if found
2022-01-05 19:40:59 +00:00
if ($json)
{
if ($this->checkJson($content))
{
return json_decode($content);
}
}
elseif ($this->checkString($content))
2022-01-04 15:41:07 +00:00
{
2022-01-05 19:40:59 +00:00
return $content;
2022-01-04 15:41:07 +00:00
}
}
return null;
2022-01-04 15:41:07 +00:00
}
/**
* Check if have an json string
*
* @input string The json string to check
*
* @returns bool true on success
*
* @since 1.0
*/
protected function checkJson($string)
{
if ($this->checkString($string))
{
json_decode($string);
return (json_last_error() === JSON_ERROR_NONE);
}
return false;
}
/**
* Check if have a string with a length
*
* @input string The string to check
*
* @returns bool true on success
*
* @since 1.0
*/
protected function checkString($string)
{
if (isset($string) && is_string($string) && strlen($string) > 0)
{
return true;
}
return false;
}
/**
* Check if we have an scripture object with value
*
* @input key The key being requested
*
* @returns bool true on success
*
* @since 1.0
*/
protected function checkScripture($key)
{
if (isset($this->scripture) && is_object($this->scripture) && isset($this->scripture->{$key}))
{
return true;
}
return false;
}
}