Your IP : 216.73.216.215


Current Path : /home/sunflowe/www2/j15/administrator/components/com_easycaptcha/models/
Upload File :
Current File : /home/sunflowe/www2/j15/administrator/components/com_easycaptcha/models/captcha.php

<?php
/**
 * @version $Id: captcha.php 636 2008-03-18 09:01:06Z snipersister $
 * @package    EasyCaptcha
 * @link http://www.easy-joomla.org
 * @license    GNU/GPL
 */
// no direct access

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );

class EasyCaptchaModelCaptcha extends JModel
{
var $_data = null;
var $_id = null;	
	 /*
	 * Constructor that retrieves the ID from the request
	 *
	 * @access    public
	 * @return    void
	 */
	function __construct()
	{
	    parent::__construct();
	
	    $array = JRequest::getVar('cid',  0, '', 'array');
	    $this->setId((int)$array[0]);
	}
	
	/**
 	* Method to set the identifier
 	*
 	* @access    public
 	* @param    int identifier
 	* @return    void
 	*/
	function setId($id)
	{
    	// Set id and wipe data
    	$this->_id        = $id;
    	$this->_data    = null;
	}
	
	/**
 	* Method to get a captcha
 	* @return object with data
 	*/
	function getData()
	{
    	global $mainframe;
    	
    	// Load the data
    	if(empty( $this->_data )) {
       	 $query = ' SELECT * FROM #__easycaptchas '.
       	         '  WHERE id = '.$this->_id;
       	 $this->_db->setQuery( $query );
       	 $this->_data = $this->_db->loadObject();
    	}
    	if (!$this->_data) {
    	    $this->_data = $this->getTable();
    	    $this->_data->id = 0;
    	}    	    
    	return $this->_data;
	}
	
	/**
	 * Method to store a record
 	*
 	* @access    public
 	* @return    boolean    True on success
 	*/
	function store($data)
	{    	
    	$row =& $this->getTable();

    	// Bind the form fields to the hello table
    	if (!$row->bind($data)) {
       	 	$this->setError($this->_db->getErrorMsg());
        	return false;
    	}
		
		// Make sure the hello record is valid
	    if (!$row->check()) {
	        $this->setError($this->_db->getErrorMsg());
	        return false;
	    }
		
   		// Store the entry to the database
    	if (!$row->store()) {
        	$this->setError($this->_db->getErrorMsg());
        	return false;
    	}

    	return true;
	}
	
	function uninstall()
	{
	    global $mainframe;

		// Initialize variables
		$failed = array ();

		$id = $this->_id;

		// Get a database connector
		$db =& JFactory::getDBO();

		// Get an installer object for the extension type
		jimport('joomla.installer.installer');
		$installer = & JInstaller::getInstance();
		
		//set the adapter
		require_once JPATH_COMPONENT.DS.'libraries'.DS.'captchaadapter.php';
		$Pluginadapter = new CaptchaAdapter($installer);
		$installer->setAdapter('captcha', $Pluginadapter );
		
		// Uninstall the chosen extensions

		$id		= trim( $id );
		$result	= $installer->uninstall('captcha', $id);

		// Build an array of extensions that failed to uninstall
		if ($result === false) {
			$failed[] = $id;
		}
		

		if (count($failed)) {
			// There was an error in uninstalling the package
			$msg = JText::sprintf('UNINSTALLEXT', JText::_('Captcha'), JText::_('Error'));
			$result = false;
		} else {
			// Package uninstalled sucessfully
			$msg = JText::sprintf('UNINSTALLEXT', JText::_('Captcha'), JText::_('Success'));
			$result = true;
		}

		$mainframe->enqueueMessage($msg);

		return $result;
	}

	function publish() {
		$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );
	    $row =& $this->getTable();
	
	        if (!$row->publish( $cids )) {
	            $this->setError( $row->getError() );
	            return false;
	        }
	
	    return true;
	}
	
	function install()
	{
		global $mainframe;
		
		//get the package
		$package = $this->_getPackageFromUpload();

		// Was the package unpacked?
		if (!$package) {
			return false;
		}

		// Get an installer instance
		jimport('joomla.installer.installer');
		$installer =& JInstaller::getInstance();
		
		//set the adapter
		require_once JPATH_COMPONENT.DS.'libraries'.DS.'captchaadapter.php';
		$Pluginadapter = new CaptchaAdapter($installer);
		$installer->setAdapter('captcha', $Pluginadapter );
		
		
		// Install the package
		if($package['type'] != "captcha")
		{
			$msg = JText::sprintf('INSTALLEXT', JText::_('Captcha'), JText::_('Please provide a captcha package'));
			$result = false;
		}
		elseif (!$installer->install($package['dir'])) {
			// There was an error installing the package
			$msg = JText::sprintf('INSTALLEXT', JText::_('Captcha'), JText::_('Error'));
			$result = false;
		} else {
			// Package installed sucessfully
			$msg = JText::sprintf('INSTALLEXT', JText::_('Captcha'), JText::_('Success'));
			$result = true;
		}
		
		$mainframe->enqueueMessage($msg);
		
		// Cleanup the install files
		if (!is_file($package['packagefile'])) {
			$config =& JFactory::getConfig();
			$package['packagefile'] = $config->getValue('config.tmp_path').DS.$package['packagefile'];
		}
	
		JInstallerHelper::cleanupInstall($package['packagefile'], $package['extractdir']);
		return $result;
	}
	
	function _getPackageFromUpload()
	{
		// Get the uploaded file information
		$userfile = JRequest::getVar('install_package', null, 'files', 'array' );

		// Make sure that file uploads are enabled in php
		if (!(bool) ini_get('file_uploads')) {
			JError::raiseWarning('SOME_ERROR_CODE', JText::_('WARNINSTALLFILE'));
			return false;
		}

		// Make sure that zlib is loaded so that the package can be unpacked
		if (!extension_loaded('zlib')) {
			JError::raiseWarning('SOME_ERROR_CODE', JText::_('WARNINSTALLZLIB'));
			return false;
		}

		// If there is no uploaded file, we have a problem...
		if (!is_array($userfile) ) {
			JError::raiseWarning('SOME_ERROR_CODE', JText::_('No file selected'));
			return false;
		}

		// Check if there was a problem uploading the file.
		if ( $userfile['error'] || $userfile['size'] < 1 )
		{
			JError::raiseWarning('SOME_ERROR_CODE', JText::_('WARNINSTALLUPLOADERROR'));
			return false;
		}

		// Build the appropriate paths
		$config =& JFactory::getConfig();
		$tmp_dest 	= $config->getValue('config.tmp_path').DS.$userfile['name'];
		$tmp_src	= $userfile['tmp_name'];

		// Move uploaded file
		jimport('joomla.filesystem.file');
		$uploaded = JFile::upload($tmp_src, $tmp_dest);

		// Unpack the downloaded package file
		jimport('joomla.installer.helper');
		$package = JInstallerHelper::unpack($tmp_dest);

		return $package;
	}
}