1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-05 19:42:38 +02:00
Oinktube/plugin/Cache/Objects/CachesInDB.php
Daniel Neto b92016cffd Update
2023-06-14 21:17:32 -03:00

279 lines
7.2 KiB
PHP

<?php
require_once dirname(__FILE__) . '/../../../videos/configuration.php';
class CachesInDB extends ObjectYPT
{
public static $loggedType_NOT_LOGGED = 'n';
public static $loggedType_LOGGED = 'l';
public static $loggedType_ADMIN = 'a';
public static $prefix = 'ypt_cache_';
protected $id;
protected $content;
protected $domain;
protected $ishttps;
protected $loggedType;
protected $user_location;
protected $expires;
protected $timezone;
protected $name;
public static function getSearchFieldsNames()
{
return ['domain', 'ishttps', 'user_location', 'timezone', 'name'];
}
public static function getTableName()
{
return 'CachesInDB';
}
public function setId($id)
{
$this->id = intval($id);
}
public function setContent($content)
{
$content = self::encodeContent($content);
$this->content = $content;
}
public function setDomain($domain)
{
$this->domain = $domain;
}
public function setIshttps($ishttps)
{
$this->ishttps = $ishttps;
}
public function setLoggedType($loggedType)
{
$this->loggedType = $loggedType;
}
public function setUser_location($user_location)
{
$this->user_location = $user_location;
}
public function setExpires($expires)
{
$this->expires = $expires;
}
public function setTimezone($timezone)
{
$this->timezone = $timezone;
}
public function setName($name)
{
$this->name = $name;
}
public function getId()
{
return intval($this->id);
}
public function getContent()
{
$this->content = self::decodeContent($this->content);
return $this->content;
}
public function getDomain()
{
return $this->domain;
}
public function getIshttps()
{
return $this->ishttps;
}
public function getLoggedType()
{
return $this->loggedType;
}
public function getUser_location()
{
return $this->user_location;
}
public function getExpires()
{
return $this->expires;
}
public function getTimezone()
{
return $this->timezone;
}
public function getName()
{
return $this->name;
}
static function hashName($name){
if(preg_match('/^hashName_/', $name)){
return $name;
}
return 'hashName_'.preg_replace('/[^0-9a-z]/i', '_', $name);
}
public static function _getCache($name, $domain, $ishttps, $user_location, $loggedType, $ignoreMetadata=false)
{
global $global;
$name = self::hashName($name);
$sql = "SELECT * FROM " . static::getTableName() . " WHERE name = ? ";
$formats = 's';
$values = [$name];
$sql .= " AND ishttps = ? AND domain = ? AND user_location = ? ";
$formats = 'ssss';
$values = [$name, $ishttps, $domain, $user_location];
if(empty($ignoreMetadata)){
$sql .= " AND loggedType = ? ";
$formats .= 'i';
$values[] = $loggedType;
}
$sql .= " ORDER BY id DESC LIMIT 1";
//_error_log(json_encode(array($sql, $values )));
// I had to add this because the about from customize plugin was not loading on the about page http://127.0.0.1/AVideo/about
$res = sqlDAL::readSql($sql, $formats, $values);
$data = sqlDAL::fetchAssoc($res);
sqlDAL::close($res);
if ($res) {
if (!empty($data) && !empty($data['content'])) {
$data['content'] = self::decodeContent($data['content']);
}
$row = $data;
} else {
$row = false;
}
return $row;
}
public static function _setCache($name, $value, $domain, $ishttps, $user_location, $loggedType)
{
if (!is_string($value)) {
$value = _json_encode($value);
}
if (empty($value)) {
return false;
}
$row = self::_getCache($name, $domain, $ishttps, $user_location, $loggedType);
if (!empty($row)) {
$c = new CachesInDB($row['id']);
} else {
$c = new CachesInDB(0);
}
$name = self::hashName($name);
$c->setContent($value);
$c->setName($name);
$c->setDomain($domain);
$c->setIshttps($ishttps);
$c->setUser_location($user_location);
$c->setLoggedType($loggedType);
$c->setExpires(date('Y-m-d H:i:s', strtotime('+ 1 month')));
return $c->save();
}
public static function _deleteCache($name)
{
global $global;
if (empty($name)) {
return false;
}
if (!static::isTableInstalled()) {
return false;
}
$name = self::hashName($name);
$sql = "DELETE FROM " . static::getTableName() . " ";
$sql .= " WHERE name = ?";
$global['lastQuery'] = $sql;
//_error_log("Delete Query: ".$sql);
return sqlDAL::writeSql($sql, "s", [$name]);
}
public static function _deleteCacheStartingWith($name)
{
global $global;
if (empty($name)) {
return false;
}
if (!static::isTableInstalled()) {
return false;
}
$name = self::hashName($name);
//$sql = "DELETE FROM " . static::getTableName() . " ";
//$sql .= " WHERE name LIKE '{$name}%'";
$sql = "DELETE FROM CachesInDB WHERE MATCH(name) AGAINST('{$name}*' IN BOOLEAN MODE);";
$global['lastQuery'] = $sql;
//_error_log("Delete Query: ".$sql);
return sqlDAL::writeSql($sql);
}
public static function _deleteCacheWith($name)
{
global $global;
if (empty($name)) {
return false;
}
if (!static::isTableInstalled()) {
return false;
}
$name = self::hashName($name);
$name = str_replace('hashName_', '', $name);
$sql = "DELETE FROM " . static::getTableName() . " ";
$sql .= " WHERE name LIKE '%{$name}%'";
$global['lastQuery'] = $sql;
//_error_log("Delete Query: ".$sql);
return sqlDAL::writeSql($sql);
}
public static function _deleteAllCache()
{
global $global;
if (!static::isTableInstalled()) {
return false;
}
$sql = "TRUNCATE TABLE " . static::getTableName() . " ";
$global['lastQuery'] = $sql;
//_error_log("Delete Query: ".$sql);
return sqlDAL::writeSql($sql);
}
public static function encodeContent($content)
{
if (!is_string($content)) {
$content = _json_encode($content);
}
$prefix = substr($content, 0, 10);
if ($prefix!== CachesInDB::$prefix) {
$base64 = base64_encode($content);
$content = CachesInDB::$prefix.$base64;
}
return $content;
}
public static function decodeContent($content)
{
$prefix = substr($content, 0, 10);
if ($prefix === CachesInDB::$prefix) {
$content = str_replace(CachesInDB::$prefix, '', $content);
$content = base64_decode($content);
}
return $content;
}
}