Skip to content

Commit

Permalink
Prevent FeedFactory from throwing a "Undefined index: ssl_check.disab…
Browse files Browse the repository at this point in the history
…led" error for anybody upgrading, and reformat some stuff.
  • Loading branch information
koenhoeijmakers committed Oct 5, 2018
1 parent eb369fe commit 758659c
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 78 deletions.
102 changes: 66 additions & 36 deletions src/FeedsFactory.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
<?php namespace willvincent\Feeds;

use SimplePie;
use Illuminate\Support\Arr;
use simplePie;

class FeedsFactory
{

/**
* The config.
*
* @var array
*/
protected $config;

protected $simplepie;
/**
* @var SimplePie
*/
protected $simplePie;

/**
* FeedsFactory constructor.
*
* @param $config
*/
public function __construct($config)
Expand All @@ -18,71 +28,91 @@ public function __construct($config)
}

/**
* @param array $feed_url RSS URL
* @param int $limit items returned per-feed with multifeeds
* @param bool|false $force_feed
*
* @return SimplePie
* @param array $feedUrl RSS URL
* @param int $limit items returned per-feed with multifeeds
* @param bool $forceFeed
* @param null $options
* @return simplePie
*/
public function make($feed_url = [], $limit = 0, $force_feed = false, $options = null)
public function make($feedUrl = [], $limit = 0, $forceFeed = false, $options = null)
{
$this->simplepie = new SimplePie();
$this->simplePie = new simplePie();
$this->configure();
$this->simplepie->set_feed_url($feed_url);
$this->simplepie->set_item_limit($limit);
if ($force_feed === true) {
$this->simplepie->force_feed(true);
$this->simplePie->set_feed_url($feedUrl);
$this->simplePie->set_item_limit($limit);

if ($forceFeed === true) {
$this->simplePie->force_feed(true);
}
if (!$this->config['strip_html_tags.disabled'] && !empty($this->config['strip_html_tags.tags']) && is_array($this->config['strip_html_tags.tags'])) {
$this->simplepie->strip_htmltags($this->config['strip_html_tags.tags']);

$stripHtmlTags = Arr::get($this->config, 'strip_html_tags.disabled', false);

if (! $stripHtmlTags && ! empty($this->config['strip_html_tags.tags']) && is_array($this->config['strip_html_tags.tags'])) {
$this->simplePie->strip_htmltags($this->config['strip_html_tags.tags']);
} else {
$this->simplepie->strip_htmltags(false);
$this->simplePie->strip_htmltags(false);
}
if (!$this->config['strip_attribute.disabled'] && !empty($this->config['strip_attribute.tags']) && is_array($this->config['strip_attribute.tags'])) {
$this->simplepie->strip_attributes($this->config['strip_attribute.tags']);

if (! $stripHtmlTags && ! empty($this->config['strip_attribute.tags']) && is_array($this->config['strip_attribute.tags'])) {
$this->simplePie->strip_attributes($this->config['strip_attribute.tags']);
} else {
$this->simplepie->strip_attributes(false);
$this->simplePie->strip_attributes(false);
}

if (isset($this->config['curl.timeout']) && is_int($this->config['curl.timeout'])) {
$this->simplepie->set_timeout($this->config['curl.timeout']);
$this->simplePie->set_timeout($this->config['curl.timeout']);
}

if (isset($options) && is_array($options)) {
if (isset($options['curl.options']) && is_array($options['curl.options'])) {
$this->simplepie->set_curl_options($this->simplepie->curl_options + $options['curl.options']);
$this->simplePie->set_curl_options($this->simplePie->curl_options + $options['curl.options']);
}

if (isset($options['strip_html_tags.tags']) && is_array($options['strip_html_tags.tags'])) {
$this->simplepie->strip_htmltags($options['strip_html_tags.tags']);
$this->simplePie->strip_htmltags($options['strip_html_tags.tags']);
}

if (isset($options['strip_attribute.tags']) && is_array($options['strip_attribute.tags'])) {
$this->simplepie->strip_attributes($options['strip_attribute.tags']);
$this->simplePie->strip_attributes($options['strip_attribute.tags']);
}

if (isset($options['curl.timeout']) && is_int($options['curl.timeout'])) {
$this->simplepie->set_timeout($options['curl.timeout']);
$this->simplePie->set_timeout($options['curl.timeout']);
}
}
$this->simplepie->init();

return $this->simplepie;
$this->simplePie->init();

return $this->simplePie;
}

/**
* Configure SimplePie.
*
* @return void
*/
protected function configure()
{
$curloptions = [];
$curlOptions = [];

if ($this->config['cache.disabled']) {
$this->simplepie->enable_cache(false);
$this->simplePie->enable_cache(false);
} else {
$this->simplepie->set_cache_location($this->config['cache.location']);
$this->simplepie->set_cache_duration($this->config['cache.life']);
$this->simplePie->set_cache_location($this->config['cache.location']);
$this->simplePie->set_cache_duration($this->config['cache.life']);
}

if (isset($this->config['curl.options']) && is_array($this->config['curl.options'])) {
$curloptions += $this->config['curl.options'];
$curlOptions += $this->config['curl.options'];
}

if ($this->config['ssl_check.disabled']) {
$curloptions[CURLOPT_SSL_VERIFYHOST] = false;
$curloptions[CURLOPT_SSL_VERIFYPEER] = false;
$curlOptions[CURLOPT_SSL_VERIFYHOST] = false;
$curlOptions[CURLOPT_SSL_VERIFYPEER] = false;
}
if (is_array($curloptions)) {
$this->simplepie->set_curl_options($curloptions);

if (is_array($curlOptions)) {
$this->simplePie->set_curl_options($curlOptions);
}
}
}
90 changes: 48 additions & 42 deletions src/FeedsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
<?php namespace willvincent\Feeds;

use Illuminate\Support\ServiceProvider;
use willvincent\Feeds\FeedsFactory;

class FeedsServiceProvider extends ServiceProvider {

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;

public function boot() {
$this->publishes([
__DIR__ . '/config/feeds.php' => config_path('feeds.php'),
]);
}

/**
* Register the service provider.
*
* @return void
*/
public function register() {
$this->app->singleton('Feeds', function () {
$config = config('feeds');

if (!$config) {
throw new \RunTimeException('Feeds configuration not found. Please run `php artisan vendor:publish`');
}

return new FeedsFactory($config);
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides() {
return ['Feeds'];
}

class FeedsServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__ . '/config/feeds.php' => config_path('feeds.php'),
]);
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('Feeds', function () {
$config = config('feeds');

if (! $config) {
throw new \RunTimeException('Feeds configuration not found. Please run `php artisan vendor:publish`');
}

return new FeedsFactory($config);
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['Feeds'];
}
}

0 comments on commit 758659c

Please sign in to comment.