Skip to content

Commit

Permalink
feature: vk video & rutube
Browse files Browse the repository at this point in the history
  • Loading branch information
Gemorroj committed Sep 29, 2024
1 parent aee16d9 commit 524d02d
Show file tree
Hide file tree
Showing 8 changed files with 582 additions and 305 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"require-dev": {
"phpunit/phpunit": "^9.6",
"friendsofphp/php-cs-fixer": "^3.46"
"friendsofphp/php-cs-fixer": "^3.64"
},
"authors": [
{
Expand Down
6 changes: 5 additions & 1 deletion src/Tag/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ class Code extends TagAbstract
{
public const BEHAVIOUR = 'pre';

// Альтернативные названия языков и их трансляция в обозначения GeSHi
/**
* Альтернативные названия языков и их трансляция в обозначения GeSHi.
*
* @var array<string, string>
*/
public array $langSynonym = [
'algol' => 'algol86',
'c++' => 'cpp',
Expand Down
97 changes: 97 additions & 0 deletions src/Tag/Rutube.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/******************************************************************************
* *
* Copyright (C) 2006-2007 Dmitriy Skorobogatov dima@pc.uz *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
* *
******************************************************************************/

namespace Xbbcode\Tag;

use Xbbcode\Attributes;

/**
* Class Rutube
* Класс для тега [rutube].
*/
class Rutube extends TagAbstract
{
public int $width = 560;
public int $height = 315;
public const BEHAVIOUR = 'img';

protected function getSrc(): string
{
$src = $this->attributes['src'] ?? '';

if (!$src) {
$src = $this->getTreeText();
}

// https://rutube.ru/video/e21f509b303c3672897cfcf85098ae80/ or https://rutube.ru/video/e21f509b303c3672897cfcf85098ae80/?r=wd
// to
// https://rutube.ru/play/embed/e21f509b303c3672897cfcf85098ae80/
$parse = \parse_url($src);

if (isset($parse['path'])) {
$id = \substr($parse['path'], \strlen('/video/'));
$id = \strtok($id, '/');
} else {
$id = '';
}

return $id ? '//rutube.ru/play/embed/'.\rawurlencode($id) : '';
}

protected function getAttributes(): Attributes
{
$attr = new Attributes();

$attr->set('frameborder', '0');
$attr->set('allowfullscreen', 'allowfullscreen');
$attr->set('width', (string) $this->width);
$attr->set('height', (string) $this->height);
$attr->set('allow', 'autoplay; encrypted-media; fullscreen; picture-in-picture;');

$src = $this->getSrc();
if ($src) {
$attr->set('src', $src);
}

if (isset($this->attributes['width'])) {
if ($this->isValidSize($this->attributes['width'])) {
$attr->set('width', $this->attributes['width']);
}
}

if (isset($this->attributes['height'])) {
if ($this->isValidSize($this->attributes['height'])) {
$attr->set('height', $this->attributes['height']);
}
}

return $attr;
}

/**
* Return html code.
*/
public function __toString(): string
{
return '<iframe '.$this->getAttributes().'></iframe>';
}
}
103 changes: 103 additions & 0 deletions src/Tag/VkVideo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

/******************************************************************************
* *
* Copyright (C) 2006-2007 Dmitriy Skorobogatov dima@pc.uz *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
* *
******************************************************************************/

namespace Xbbcode\Tag;

use Xbbcode\Attributes;

/**
* Class VK Video
* Класс для тега [vk].
*/
class VkVideo extends TagAbstract
{
public const BEHAVIOUR = 'img';
public int $width = 560;
public int $height = 315;

protected function getSrc(): string
{
$src = $this->attributes['src'] ?? '';

if (!$src) {
$src = $this->getTreeText();
}

// https://vk.com/video-177129873_456239415 or https://vk.com/video?z=video-177129873_456239415%2Fpl_cat_trends or https://m.vk.com/video-177129873_456239415?from=video
// to
// https://vk.com/video_ext.php?id=456239415
$parse = \parse_url($src);
$path = '';
if (isset($parse['query'])) {
\parse_str($parse['query'], $query);
if (isset($query['z'])) {
$path = $query['z'];
}
}
if (!$path && isset($parse['path'])) {
$path = $parse['path'];
}

$id = \substr($path, \strpos($path, '_') + 1);
$id = \strtok($id, '/');

return $id ? '//vk.com/video_ext.php?id='.\rawurlencode($id) : '';
}

protected function getAttributes(): Attributes
{
$attr = new Attributes();

$attr->set('frameborder', '0');
$attr->set('allowfullscreen', 'allowfullscreen');
$attr->set('width', (string) $this->width);
$attr->set('height', (string) $this->height);
$attr->set('allow', 'autoplay; encrypted-media; fullscreen; picture-in-picture;');

$src = $this->getSrc();
if ($src) {
$attr->set('src', $src);
}

if (isset($this->attributes['width'])) {
if ($this->isValidSize($this->attributes['width'])) {
$attr->set('width', $this->attributes['width']);
}
}

if (isset($this->attributes['height'])) {
if ($this->isValidSize($this->attributes['height'])) {
$attr->set('height', $this->attributes['height']);
}
}

return $attr;
}

/**
* Return html code.
*/
public function __toString(): string
{
return '<iframe '.$this->getAttributes().'></iframe>';
}
}
6 changes: 4 additions & 2 deletions src/Tag/Youtube.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
class Youtube extends TagAbstract
{
public const BEHAVIOUR = 'img';
public int $width = 560;
public int $height = 315;

protected function getSrc(): string
{
Expand All @@ -57,8 +59,8 @@ protected function getAttributes(): Attributes

$attr->set('frameborder', '0');
$attr->set('allowfullscreen', 'allowfullscreen');
$attr->set('width', '560');
$attr->set('height', '315');
$attr->set('width', (string) $this->width);
$attr->set('height', (string) $this->height);

$src = $this->getSrc();
if ($src) {
Expand Down
Loading

0 comments on commit 524d02d

Please sign in to comment.