Skip to content

Instantly share code, notes, and snippets.

@treetop1500
Last active September 26, 2016 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save treetop1500/c877342e8ccf15362af9248348e32ed3 to your computer and use it in GitHub Desktop.
Save treetop1500/c877342e8ccf15362af9248348e32ed3 to your computer and use it in GitHub Desktop.
Froala Image Receiver Controller
<?php
/**
* Created by PhpStorm.
* User: grayloon
* Date: 9/3/16
* Time: 7:39 AM
*
* Set these routes up!!
* # froala receive route for file uploads
* froala_receiver:
* path: /froala-receiver
* defaults: { _controller: ContentBundle:Froala:receive }
*/
namespace ContentBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class FroalaController extends Controller
{
private $allowable_file_types;
private $allowable_image_extensions;
private $allowable_file_extensions;
private $uploads_dir;
public function indexAction()
{
return $this->render('ContentBundle::froala_index.html.twig', array(
// ...
));
}
public function receiveAction(Request $request)
{
$this->setUploadsDir($this->container->getParameter('froala_uploads_dir'));
$this->setAllowableFileTypes($this->container->getParameter('froala_allowable_mime_types'));
$this->setAllowableImageExtensions($this->container->getParameter('froala_allowable_image_extensions'));
$this->setAllowableFileExtensions($this->container->getParameter('froala_allowable_file_extensions'));
///////////////////////////////////////////////////////
$txt = $request->request->all();
if($txt['id'] == 'admin_image') {
$allowedExts = $this->getAllowableImageExtensions();
//$myfile = fopen("people.txt", "w") or die("Unable to open file!");
//fwrite($myfile, date('U') . ' - image');
//fclose($myfile);
} else {
$allowedExts = $this->getAllowableFileExtensions();
//$myfile = fopen("people.txt", "w") or die("Unable to open file!");
//fwrite($myfile, date('U') . ' - file');
//fclose($myfile);
}
///////////////////////////////////////////////////////////
// Get filename.
$temp = explode(".", $_FILES["file"]["name"]);
// Get extension.
$extension = end($temp);
// Do not use $_FILES["file"]["type"] as it can be easily forged.
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES["file"]["tmp_name"]);
if (in_array($mime,$this->getAllowableFileTypes())
&& in_array(strtolower($extension), $allowedExts)) {
// Generate new random name.
$name = sha1(microtime()) . "." . $extension;
// Save file in the uploads folder.
move_uploaded_file($_FILES["file"]["tmp_name"], $this->container->getParameter('kernel.root_dir') . "/../web/uploads/froala-editor/" . $name);
$response = new \stdClass();
$response->link = "/uploads/froala-editor/" . $name;
return $this->json($response);
} else {
return $this->json(null);
}
}
/**
* @return mixed
*/
public function getAllowableFileTypes()
{
return $this->allowable_file_types;
}
/**
* @param mixed $allowable_file_types
*/
public function setAllowableFileTypes($allowable_file_types)
{
$this->allowable_file_types = $allowable_file_types;
}
/**
* @return mixed
*/
public function getUploadsDir()
{
return $this->uploads_dir;
}
/**
* @param mixed $uploads_dir
*/
public function setUploadsDir($uploads_dir)
{
$this->uploads_dir = $uploads_dir;
}
/**
* @return mixed
*/
public function getAllowableImageExtensions()
{
return $this->allowable_image_extensions;
}
/**
* @param mixed $allowable_image_extensions
*/
public function setAllowableImageExtensions($allowable_image_extensions)
{
$this->allowable_image_extensions = $allowable_image_extensions;
}
/**
* @return mixed
*/
public function getAllowableFileExtensions()
{
return $this->allowable_file_extensions;
}
/**
* @param mixed $allowable_file_extensions
*/
public function setAllowableFileExtensions($allowable_file_extensions)
{
$this->allowable_file_extensions = $allowable_file_extensions;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment