Your IP : 216.73.216.55


Current Path : /home/g/i/t/giteleslfp/www/administrator/components/com_templateck/controllers/
Upload File :
Current File : /home/g/i/t/giteleslfp/www/administrator/components/com_templateck/controllers/pixabay.php

<?php
// No direct access
defined('_JEXEC') or die;

use Templatecreatorck\CKController;
use Templatecreatorck\CKfile;
use Templatecreatorck\CKfolder;
use Templatecreatorck\CKFof;

require_once TEMPLATECREATORCK_PATH . '/helpers/ckcontroller.php';

class TemplateckControllerPixabay extends CKController {

	/**
	 * Upload the image
	 * 
	 * @return json : result = boolean on success, file = the image filename
	 */
	public function upload() {
		// security check
		CKFof::checkAjaxToken();

		$url = $this->input->get('image_url', '', 'url');
		
		// $url = 'https://pixabay.com/get/57e6d04b4b5bb114a6da837ec32f2a7f1038dbed5b59724e7c_1280.jpg';
		$destFolder = JPATH_ROOT . '/images/pixabay/';
		$fileName = TCK_File::getName($url);
		$filePath = $destFolder . $fileName;

		// create the destination folder if not exists
		if (! file_exists($destFolder)) {
			$result = TCK_Folder::create($destFolder);
			if (! $result) {
				echo '{"status" : "0", "file" : "", "message" : "Error on folder creation"}';
				exit();
			}
		}

		// get the file from url
		set_time_limit(0);
		try {
			$file = file_get_contents(urldecode($url));
		} catch (Exception $e) {
			echo 'Exception : ',  $e->getMessage(), "\n";
			exit;
		}

		if (file_exists($filePath)) {
			$result = true;
		} else {
			// store the file locally
			$result = file_put_contents($filePath, $file);
			if (! $result) {
				echo '{"status" : "0", "file" : "", "message" : "Error on file creation"}';
				exit();
			}

			$fileArray = array( 
				"name" => $fileName 
				,"type" =>  "image/png" 
				,"tmp_name" => $filePath 
				,"error" => 0
				,"size" => filesize($filePath)
				,"filepath" => $destFolder
			);
			// Trigger the onContentBeforeSave event.
			$fileObj = new JObject($fileArray);
			$result = CKFof::triggerEvent('onContentBeforeSave', array('com_media.file', &$fileObj, true));

			if (in_array(false, $result, true))
			{
				// There are some errors in the plugins
				echo '{"status" : "0", "message" : "' . JText::plural('COM_MEDIA_ERROR_BEFORE_SAVE', count($errors = $object_file->getErrors()), implode('<br />', $errors)) . '"}';
				exit;
			}

		}

		echo '{"status" : "1", "file" : "' . 'images/pixabay/' . $fileName . '"}';
		exit;
	}
}