Your IP : 216.73.216.54


Current Path : /var/www/html/components/com_jvld/models/
Upload File :
Current File : /var/www/html/components/com_jvld/models/editfile.php

<?php
/**
 * @version		$Id$
 * @package		JV-LinkDirectory
 * @subpackage	com_jvld
 * @copyright	Copyright 2008-2013 JV-Extensions. All rights reserved
 * @license		GNU General Public License version 3 or later
 * @author		JV-Extensions
 * @link		http://www.jv-extensions.com
 */

// No direct access
defined('_JEXEC') or die('Restricted access');

class JvldModelEditfile extends JvldFendModel
{
	public function getForm($data = array(), $loadData = true)
	{
		$form = $this->loadForm('com_jvld.editfile', 'editfile', array('control' => 'jform', 'load_data' => $loadData), true);
		if (empty($form))
			return false;

		return $form;
	}

	public function getItem()
	{
		$lid = JFactory::getApplication()->input->getInt('lid', 0);
		$id = JFactory::getApplication()->input->getInt('id', 0);

		if ($id)
			return JvldDb::getRow("select * from #__jvld_files where id = ".(int)$id." and lid = ".(int)$lid);
		else
			return null;
	}

	protected function loadFormData()
	{
		$item = $this->getItem();
		if ($item)
		{
			; // nothing to do
		}

		return $item;
	}

	public function process()
	{
		try
		{
			parent::preProcess();

			if (!$this->cfg->get('files_en'))
				throw new Exception(JText::_("COM_JVLD_FEATURED_DISABLED"));

			if (!$this->lid)
				throw new Exception(JText::_("COM_JVLD_ERR_INVALID_REQ"));

			if ($this->title == '')
				throw new Exception(JText::_("COM_JVLD_ERR_INSUFFINFO"));

			$link = new JvldLinkinfo($this->lid);
			if ($link->getInfo('uid') != $this->user->get('id'))
				throw new Exception(JText::_("COM_JVLD_UNAUTHORIZED"));

			$tnow = JvldDate::getDateTimeInUTCInMySqlFormat();

			$linkplan = new JvldLinkPlan();
			$linkplan->setPlanCode($link->getInfo('oway_plan'));

			$maxfcnt = $linkplan->getMaxFiles();

			$myfile = array();
			$upfile = JvldFileManager::upload('upname', _J_ABSPATH_TMP.DIRECTORY_SEPARATOR.'jvldfiles', array('allowed_extns'=>$this->cfg->get('files_extens'), 'maxsize'=>$this->cfg->get('files_maxsize'), 'genprefix'=>1, 'err_on_nofileupload'=>0), $myfile);

			if ($this->id)
			{
				// existing file being edited for listing. file can be uploaded or need not be too.
				if ($upfile == '')
				{
					// file was not uploaded. it is okay
					JvldDb::update("update #__jvld_files set title = '".JvldSecure::defendSQL($this->title)."', description = '".JvldSecure::defendSQL($this->description)."', version = '".JvldSecure::defendSQL($this->version)."' where id = ".(int)$this->id);
				}
				else
				{
					$clsname = "JvldStorage" . \Joomla\String\StringHelper::ucfirst($this->cfg->get('files_location_type'));

					// file has been updated. So delete the older file from storage first.
					$ofile = JvldDb::getRow("select * from #__jvld_files where id = ".(int)$this->id);

					JvldUtil::deleteFileInStorage(
						new $clsname([
							'srcfile' => $this->cfg->get('files_location') . DIRECTORY_SEPARATOR . $ofile->obname,
							'region'  => $this->cfg->get('files_s3_region'),
							'bucket'  => $this->cfg->get('files_s3_bucket'),
							'key'     => ($this->cfg->get('files_s3_folder') != '') ? $this->cfg->get('files_s3_folder') . '/' . $ofile->obname : $ofile->obname,
						])
					);

					JvldUtil::copyFileToStorage(
						new $clsname([
							'srcfile'  => _J_ABSPATH_TMP . DIRECTORY_SEPARATOR . 'jvldfiles' . DIRECTORY_SEPARATOR . $upfile,
							'destfile' => $this->cfg->get('files_location') . DIRECTORY_SEPARATOR . $upfile,
							'region'   => $this->cfg->get('files_s3_region'),
							'bucket'   => $this->cfg->get('files_s3_bucket'),
							'key'      => ($this->cfg->get('files_s3_folder') != '') ? $this->cfg->get('files_s3_folder') . '/' . $upfile : $upfile,
						])
					);

					$csum = md5_file(_J_ABSPATH_TMP.DIRECTORY_SEPARATOR.'jvldfiles'.DIRECTORY_SEPARATOR.$upfile);
					JvldDb::update("update #__jvld_files set title = '".JvldSecure::defendSQL($this->title)."', description = '".JvldSecure::defendSQL($this->description)."', version = '".JvldSecure::defendSQL($this->version)."', upname = '".JvldSecure::defendSQL($myfile['name'])."', obname = '".JvldSecure::defendSQL($upfile)."', fsize = '".JvldSecure::defendSQL(round($myfile['size']/1024, 2))."', uploaded_on = '".JvldSecure::defendSQL($tnow)."', checksum = '".JvldSecure::defendSQL($csum)."' where id = ".(int)$this->id);
				}

				// get text
				$oktext = JText::_('COM_JVLD_FILE_EDITED_OK');
			}
			else
			{
				// new file for listing.

				// check for max file count breach.
				$files = JvldPostinit::getLinkFiles($this->lid, -1);
				if (count($files) >= $maxfcnt)
					throw new Exception(JText::_("COM_JVLD_FILE_LIMIT_REACHED"));

				// file must be uploaded.
				if ($upfile == '')
					throw new Exception(JText::_("COM_JVLD_ERR_UPLOAD_NOFILE"));

				$clsname = "JvldStorage" . \Joomla\String\StringHelper::ucfirst($this->cfg->get('files_location_type'));
				JvldUtil::copyFileToStorage(
					new $clsname([
						'srcfile'  => _J_ABSPATH_TMP . DIRECTORY_SEPARATOR . 'jvldfiles' . DIRECTORY_SEPARATOR . $upfile,
						'destfile' => $this->cfg->get('files_location') . DIRECTORY_SEPARATOR . $upfile,
						'region'   => $this->cfg->get('files_s3_region'),
						'bucket'   => $this->cfg->get('files_s3_bucket'),
						'key'      => ($this->cfg->get('files_s3_folder') != '') ? $this->cfg->get('files_s3_folder') . '/' . $upfile : $upfile,
					])
				);

				// add the file to db
				$csum = md5_file(_J_ABSPATH_TMP.DIRECTORY_SEPARATOR.'jvldfiles'.DIRECTORY_SEPARATOR.$upfile);
				$fid = JvldDb::update("insert into #__jvld_files (lid, title, description, upname, obname, version, fsize, num_downloads, uploaded_on, checksum, published, access) values('".(int)$this->lid."', '".JvldSecure::defendSQL($this->title)."', '".JvldSecure::defendSQL($this->description)."', '".JvldSecure::defendSQL($myfile['name'])."', '".JvldSecure::defendSQL($upfile)."', '".JvldSecure::defendSQL($this->version)."', '".JvldSecure::defendSQL(round($myfile['size']/1024, 2))."', '0', '".JvldSecure::defendSQL($tnow)."', '".JvldSecure::defendSQL($csum)."', '1', '".JvldSecure::defendSQL($this->cfg->get('files_fe_access'))."')");

				// Trigger OnNewFileUpload plugin
				JvldInit::triggerPlugins('OnNewFileUpload', array($fid));

				// get text
				$oktext = JText::_('COM_JVLD_FILE_ADDED_OK');
			}

			JvldLinkinfo::updateLinkUpdateDate($this->lid);

			JvldMsg::display($oktext, _JVLD_NEXT_PAGE_GO_TO_RETURN_URL);
			return;
		}
		catch (Exception $ex)
		{
			JvldMsg::display($ex->getMessage(), _JVLD_NEXT_PAGE_GO_BACK);
			return;
		}
	}
}