2015-05-22 12:36:26 -04:00
|
|
|
<?php
|
2015-06-16 11:11:35 -04:00
|
|
|
/**
|
|
|
|
* Base for base models
|
|
|
|
*/
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* Common base for all Models
|
|
|
|
*/
|
2015-05-22 12:36:26 -04:00
|
|
|
class BaseModel {
|
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* The global configuration object
|
|
|
|
* @var object $config
|
|
|
|
*/
|
2015-06-09 18:18:53 -04:00
|
|
|
protected $config;
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2015-05-22 12:36:26 -04:00
|
|
|
public function __construct()
|
|
|
|
{
|
2015-06-09 18:18:53 -04:00
|
|
|
global $config;
|
|
|
|
$this->config = $config;
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
2015-06-09 11:54:42 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the path of the cached version of the image. Create the cached image
|
|
|
|
* if the file does not already exist
|
|
|
|
*
|
|
|
|
* @param string $api_path - The original image url
|
|
|
|
* @param string $series_slug - The part of the url with the series name, becomes the image name
|
|
|
|
* @param string $type - Anime or Manga, controls cache path
|
|
|
|
* @return string - the frontend path for the cached image
|
|
|
|
*/
|
|
|
|
public function get_cached_image($api_path, $series_slug, $type="anime")
|
|
|
|
{
|
|
|
|
$path_parts = explode('?', basename($api_path));
|
|
|
|
$path = current($path_parts);
|
|
|
|
$ext_parts = explode('.', $path);
|
|
|
|
$ext = end($ext_parts);
|
2015-06-16 11:11:35 -04:00
|
|
|
|
|
|
|
// Workaround for some broken extensions
|
|
|
|
if ($ext == "jjpg") $ext = "jpg";
|
|
|
|
|
|
|
|
// Failsafe for weird urls
|
|
|
|
if (strlen($ext) > 3) return $api_path;
|
2015-06-09 11:54:42 -04:00
|
|
|
|
|
|
|
$cached_image = "{$series_slug}.{$ext}";
|
2015-06-09 18:18:53 -04:00
|
|
|
$cached_path = "{$this->config->img_cache_path}/{$type}/{$cached_image}";
|
2015-06-09 11:54:42 -04:00
|
|
|
|
|
|
|
// Cache the file if it doesn't already exist
|
|
|
|
if ( ! file_exists($cached_path))
|
|
|
|
{
|
|
|
|
if (ini_get('allow_url_fopen'))
|
|
|
|
{
|
|
|
|
copy($api_path, $cached_path);
|
|
|
|
}
|
|
|
|
elseif (function_exists('curl_init'))
|
|
|
|
{
|
|
|
|
$ch = curl_init($api_path);
|
|
|
|
$fp = fopen($cached_path, 'wb');
|
|
|
|
curl_setopt_array($ch, [
|
|
|
|
CURLOPT_FILE => $fp,
|
|
|
|
CURLOPT_HEADER => 0
|
|
|
|
]);
|
|
|
|
curl_exec($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
fclose($ch);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception("Couldn't cache images because they couldn't be downloaded.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-09 18:18:53 -04:00
|
|
|
return "/public/images/{$type}/{$cached_image}";
|
2015-06-09 11:54:42 -04:00
|
|
|
}
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
// End of BaseModel.php
|