<?php
namespace App\Helpers\Files;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\File\File;
use Gregwar\Image\Image;
class FileHelper
{
public function __construct(EntityManagerInterface $entityManager)
{
$this->em = $entityManager;
}
public function upload(?\Symfony\Component\HttpFoundation\File\File $file = null, ?string $path = null, $filename = null)
{
// Missing details?
if(!$file && !$path && !$filename)
throw new \Exception("Invalid file path given.");
// Directory not created?
if(!is_dir($path))
mkdir($path);
// Let's move it
$file->move($path, $filename);
return true;
}
public function move($path = null, $newPath = null)
{
return true;
}
public function delete($path = null)
{
if(!$path)
throw new \Exception("Invalid path!");
// Remove first slash
$path = ltrim($path, '/');
$realpath = realpath($path);
$currentDirectory = getcwd() . '/files';
$currentDirectoryLength = strlen($currentDirectory);
// Compare string
$subString = substr($realpath, 0, $currentDirectoryLength);
// Dangerous match!
if($subString != $currentDirectory)
throw new \Exception("You do not have permission to access this directory");
if(is_dir($path))
{
$path = rtrim($path, '/\\');
$i = new \DirectoryIterator($path);
foreach($i as $f)
{
if($f->isFile())
{
unlink($f->getRealPath());
}
else if(!$f->isDot() && $f->isDir())
{
$this->delete($f->getRealPath());
}
}
rmdir($path);
return true;
}
if(file_exists($path))
{
unlink($path);
return true;
}
}
public function slugifyFilename($filename = null)
{
// No filename
if(!$filename)
return null;
// Slugify the name
return preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $filename);
}
public function getFriendlyName($filename = null)
{
// No filename
if(!$filename)
return null;
// Return and remove extension.
return preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename);
}
public function thumbnail($image, $width = 200, $height = 200, $type = 'zoomcrop', $bgColour = 'FFFFFF')
{
$image = trim($image, '/');
// Split up path
$filename = basename($image);
$directory = trim(dirname($image), '/');
// check if file exists
if(!file_exists($image))
return "/assets/admin/img/noimage.png";
// Split up by extension
$bits = explode('.', $filename);
if(count($bits) == 1)
return "/assets/admin/img/noimage.png";
$name = $bits[0];
$extension = $bits[1];
if($type == "resize")
$nameType = "r";
else
$nameType = "zc";
// Get new name
$newName = $name . '-' . $nameType . '-' . $width . 'x' . $height . '.' . $extension;
// Does it already exist?
if(is_file($directory . '/' . $newName))
{
return '/' . $directory . '/' . $newName;
}
else
{
if($type == "resize")
{
// Resize it
Image::open($image)
->resize($width, $height, $bgColour, 0, 0)
->save($directory . '/' . $newName);
}
else
{
// default zoomcrop
Image::open($image)
->zoomCrop($width, $height, 'FFFFFF', 0, 0)
->save($directory . '/' . $newName);
}
// return image
return '/' . $directory . '/' . $newName;
}
}
public function generatePdfFromUrl(string $url, string $saveLocation, array $options = []): ?string
{
try
{
// Curl setup
$ch = curl_init("https://www.pdfcommand.com/generate");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
"key" => "8952573e-f689-11ee-a6df-f69977c54390",
"type" => "url",
"url" => $url,
"options" => $options
]);
// Execute it
$response = curl_exec($ch);
// Grab the HTTP code
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close curl session
curl_close($ch);
// Problem
if($httpcode != 200)
return null;
// Grab the file
$fc = file_get_contents($response);
// Write it
file_put_contents($saveLocation, $fc);
return $saveLocation;
}
catch(\Exception $e)
{
return null;
}
}
public function generatePdfFromHtml(string $html, string $saveLocation, array $options = []): ?string
{
try
{
// Does the save location directory not exist?
if(!is_dir(dirname($saveLocation)))
mkdir(dirname($saveLocation), 0777, true);
// Add prefixes to options
foreach($options AS $k => $v)
{
unset($options[$k]);
$options["opt-" . $k] = $v;
}
// Curl setup
$ch = curl_init("https://www.pdfcommand.com/generate");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array_merge([
"key" => "8952573e-f689-11ee-a6df-f69977c54390",
"type" => "html",
"html" => $html
], $options));
// Execute it
$response = curl_exec($ch);
// Grab the HTTP code
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close curl session
curl_close($ch);
// Problem
if($httpcode != 200)
return null;
// Grab the file
$fc = file_get_contents($response);
// Write it
file_put_contents($saveLocation, $fc);
return $saveLocation;
}
catch(\Exception $e)
{
dump($e);
return null;
}
}
}