diff --git a/.gitignore b/.gitignore index 371ba1b..0fb6d93 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ ffmpeg-*/ alltube-release.zip coverage/ bower_components/ +config.yml diff --git a/Gruntfile.js b/Gruntfile.js index 91ef587..ef153d9 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -29,7 +29,7 @@ module.exports = function (grunt) { }, phpcs: { php: { - src: ['*.php'] + src: ['*.php', 'classes/*.php', 'controllers/*.php'] }, tests: { src: ['tests/*.php'] @@ -51,7 +51,7 @@ module.exports = function (grunt) { options: { archive: 'alltube-release.zip' }, - src: ['*.php', '!config.php', 'dist/**', 'fonts/**', '.htaccess', 'img/**', 'js/**', 'LICENSE', 'README.md', 'robots.txt', 'sitemap.xml', 'templates/**', 'templates_c/', 'vendor/**', 'classes/**', 'controllers/**', 'bower_components/**'] + src: ['*.php', '!config.yml', 'dist/**', 'fonts/**', '.htaccess', 'img/**', 'js/**', 'LICENSE', 'README.md', 'robots.txt', 'sitemap.xml', 'templates/**', 'templates_c/', 'vendor/**', 'classes/**', 'controllers/**', 'bower_components/**'] } } } diff --git a/README.md b/README.md index 76fd9f9..ea68dbf 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ You should also ensure that the *templates_c* folder has the right permissions: If you want to use a custom config, you need to create a config file: - cp config.example.php config.php + cp config.example.yml config.yml ##License @@ -33,7 +33,7 @@ __Please use a different name and logo if you run it on a public server.__ ##Other dependencies You need [avconv](https://libav.org/avconv.html) and [rtmpdump](http://rtmpdump.mplayerhq.hu/) in order to enable conversions. -If you don't want to enable conversions, you can disable it in *config.php*. +If you don't want to enable conversions, you can disable it in *config.yml*. On Debian-based systems: diff --git a/classes/Config.php b/classes/Config.php new file mode 100644 index 0000000..15dc952 --- /dev/null +++ b/classes/Config.php @@ -0,0 +1,54 @@ + + * @license GNU General Public License http://www.gnu.org/licenses/gpl.html + * @link http://rudloff.pro + * */ +namespace Alltube; +use Symfony\Component\Yaml\Yaml; +/** + * Class to manage config parameters + * + * PHP Version 5.3.10 + * + * @category Youtube-dl + * @package Youtubedl + * @author Pierre Rudloff + * @license GNU General Public License http://www.gnu.org/licenses/gpl.html + * @link http://rudloff.pro + * */ +Class Config +{ + private static $_instance; + + public $youtubedl = __DIR__.'/../vendor/rg3/youtube-dl/youtube_dl/__main__.py'; + public $python = '/usr/bin/python'; + public $params = '--no-playlist --no-warnings -f best'; + public $convert = false; + public $avconv = __DIR__.'/ffmpeg/ffmpeg'; + + private function __construct() { + $yaml = Yaml::parse(__DIR__.'/../config.yml'); + foreach ($yaml as $param=>$value) { + if (isset($this->$param)) { + $this->$param = $value; + } + } + if (getenv('CONVERT')) { + $this->convert = getenv('CONVERT'); + } + } + + public static function getInstance() { + if(is_null(self::$_instance)) { + self::$_instance = new Config(); + } + return self::$_instance; + } +} diff --git a/classes/VideoDownload.php b/classes/VideoDownload.php index 5c362e7..3d18990 100644 --- a/classes/VideoDownload.php +++ b/classes/VideoDownload.php @@ -1,7 +1,6 @@ python.' '.$config->youtubedl.' --dump-user-agent', $version ); return $version[0]; @@ -47,8 +46,9 @@ Class VideoDownload * */ static function listExtractors() { + $config = Config::getInstance(); exec( - PYTHON.' '.YOUTUBE_DL.' --list-extractors', + $config->python.' '.$config->youtubedl.' --list-extractors', $extractors ); return $extractors; @@ -64,7 +64,8 @@ Class VideoDownload * */ static function getFilename($url, $format=null) { - $cmd=PYTHON.' '.YOUTUBE_DL; + $config = Config::getInstance(); + $cmd=$config->python.' '.$config->youtubedl; if (isset($format)) { $cmd .= ' -f '.escapeshellarg($format); } @@ -86,7 +87,8 @@ Class VideoDownload * */ static function getJSON($url, $format=null) { - $cmd=PYTHON.' '.YOUTUBE_DL.' '.PARAMS; + $config = Config::getInstance(); + $cmd=$config->python.' '.$config->youtubedl.' '.$config->params; if (isset($format)) { $cmd .= ' -f '.escapeshellarg($format); } @@ -111,7 +113,8 @@ Class VideoDownload * */ static function getURL($url, $format=null) { - $cmd=PYTHON.' '.YOUTUBE_DL.' '.PARAMS; + $config = Config::getInstance(); + $cmd=$config->python.' '.$config->youtubedl.' '.$config->params; if (isset($format)) { $cmd .= ' -f '.escapeshellarg($format); } diff --git a/common.php b/common.php deleted file mode 100644 index ad61333..0000000 --- a/common.php +++ /dev/null @@ -1,21 +0,0 @@ - - * @author Olivier Haquette - * @license GNU General Public License http://www.gnu.org/licenses/gpl.html - * @link http://rudloff.pro - * */ -require_once __DIR__.'/vendor/autoload.php'; -if (is_file('config.php')) { - include_once 'config.php'; -} else { - include_once 'config.example.php'; -} -define('FILENAME', basename($_SERVER["SCRIPT_FILENAME"])); diff --git a/composer.json b/composer.json index eb0d20f..110348e 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,8 @@ "rg3/youtube-dl": "2015.10.24", "slim/slim": "~2.6.2", "slim/views": "~0.1.3", - "rudloff/smarty-plugin-noscheme": "~0.1.0" + "rudloff/smarty-plugin-noscheme": "~0.1.0", + "symfony/yaml": "~2.7.6" }, "require-dev": { "symfony/var-dumper": "~2.7.6" diff --git a/composer.lock b/composer.lock index ac1d90a..2d0cd81 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "82f7ed8800ea2323a9c82063c52a5aec", - "content-hash": "d0cc534e5934271396a1b66d8d0ff51b", + "hash": "a7e5944a818030d017d39d13b9ec0ffd", + "content-hash": "2d61af9410d3e5f69fa0d6a956210a83", "packages": [ { "name": "jeremykendall/php-domain-parser", @@ -380,6 +380,52 @@ "templating" ], "time": "2015-06-18 00:55:59" + }, + { + "name": "symfony/yaml", + "version": "v2.7.6", + "source": { + "type": "git", + "url": "https://github.com/symfony/yaml.git", + "reference": "eca9019c88fbe250164affd107bc8057771f3f4d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/yaml/zipball/eca9019c88fbe250164affd107bc8057771f3f4d", + "reference": "eca9019c88fbe250164affd107bc8057771f3f4d", + "shasum": "" + }, + "require": { + "php": ">=5.3.9" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.7-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Yaml\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Yaml Component", + "homepage": "https://symfony.com", + "time": "2015-10-11 09:39:48" } ], "packages-dev": [ diff --git a/config.example.php b/config.example.php deleted file mode 100644 index f26c490..0000000 --- a/config.example.php +++ /dev/null @@ -1,24 +0,0 @@ - - * @license GNU General Public License http://www.gnu.org/licenses/gpl.html - * @link http://rudloff.pro - * */ -define('YOUTUBE_DL', __DIR__.'/vendor/rg3/youtube-dl/youtube_dl/__main__.py'); -define('PYTHON', '/usr/bin/python'); -define('PARAMS', '--no-playlist --no-warnings -f best'); -if (getenv('CONVERT')) { - define('CONVERT', getenv('CONVERT')); -} else { - define('CONVERT', false); -} -define('AVCONV', __DIR__.'/ffmpeg/ffmpeg'); -define('MAINTENANCE', false); -define('DISABLED', false); diff --git a/config.example.yml b/config.example.yml new file mode 100644 index 0000000..7dd9940 --- /dev/null +++ b/config.example.yml @@ -0,0 +1,5 @@ +youtubedl: vendor/rg3/youtube-dl/youtube_dl/__main__.py +python: /usr/bin/python +params: --no-playlist --no-warnings -f best +convert: false +avconv: ffmpeg/ffmpeg diff --git a/controllers/FrontController.php b/controllers/FrontController.php index 2d0cf96..09e54dc 100644 --- a/controllers/FrontController.php +++ b/controllers/FrontController.php @@ -1,10 +1,12 @@ render( 'head.tpl', array( @@ -17,7 +19,7 @@ class FrontController { $app->render( 'index.tpl', array( - 'convert'=>CONVERT + 'convert'=>$config->convert ) ); $app->render('footer.tpl'); @@ -44,6 +46,7 @@ class FrontController { static function video() { global $app; + $config = Config::getInstance(); if (isset($_GET["url"])) { if (isset($_GET['audio'])) { try { @@ -71,7 +74,7 @@ class FrontController { header("Content-Type: audio/mpeg"); passthru( '/usr/bin/rtmpdump -q -r '.escapeshellarg($video->url). - ' | '.AVCONV.' -v quiet -i - -f mp3 -vn pipe:1' + ' | '.$config->avconv.' -v quiet -i - -f mp3 -vn pipe:1' ); exit; } else { @@ -90,7 +93,7 @@ class FrontController { passthru( 'curl --user-agent '.escapeshellarg($UA). ' '.escapeshellarg($video->url). - ' | '.AVCONV.' -v quiet -i - -f mp3 -vn pipe:1' + ' | '.$config->avconv.' -v quiet -i - -f mp3 -vn pipe:1' ); exit; } diff --git a/index.php b/index.php index 3deadb5..181ffa0 100644 --- a/index.php +++ b/index.php @@ -12,8 +12,8 @@ * @license GNU General Public License http://www.gnu.org/licenses/gpl.html * @link http://rudloff.pro * */ +require_once __DIR__.'/vendor/autoload.php'; use Alltube\VideoDownload; -require_once 'common.php'; $app = new \Slim\Slim( array( diff --git a/phpunit.xml b/phpunit.xml index 32420be..0288925 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,7 +1,7 @@ - + - download.php + classes/ diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php new file mode 100644 index 0000000..2cbd47e --- /dev/null +++ b/tests/ConfigTest.php @@ -0,0 +1,34 @@ + + * @license GNU General Public License http://www.gnu.org/licenses/gpl.html + * @link http://rudloff.pro + * */ +use Alltube\Config; + +/** + * Unit tests for the Config class + * + * PHP Version 5.3.10 + * + * @category Youtube-dl + * @package Youtubedl + * @author Pierre Rudloff + * @license GNU General Public License http://www.gnu.org/licenses/gpl.html + * @link http://rudloff.pro + * */ +class ConfigTest extends PHPUnit_Framework_TestCase +{ + public function testGetInstance() + { + putenv('CONVERT=1'); + $config = Config::getInstance(); + $this->assertEquals($config->convert, true); + } +} diff --git a/tests/VideoDownloadTest.php b/tests/VideoDownloadTest.php index 364ca00..fc84cf7 100644 --- a/tests/VideoDownloadTest.php +++ b/tests/VideoDownloadTest.php @@ -11,7 +11,6 @@ * @link http://rudloff.pro * */ use Alltube\VideoDownload; -require_once __DIR__.'/../common.php'; /** * Unit tests for the VideoDownload class