Sindbad~EG File Manager

Current Path : /var/www/ispania.gr/components/com_rsdirectory/models/
Upload File :
Current File : /var/www/ispania.gr/components/com_rsdirectory/models/claim.php

<?php
/**
 * @package	RSDirectory!
 * @copyright	(c) 2013 - 2021 RSJoomla!
 * @link		https://www.rsjoomla.com
 * @license	GNU General Public License http://www.gnu.org/licenses/gpl-3.0.en.html
 */

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

/**
 * Contact model.
 */
class RSDirectoryModelClaim extends JModelAdmin
{
    /**
     * The entry from which the contact form was sent.
     *
     * @access protected
     *
     * @var object
     */
    protected $entry;

    /**
     * Submitted data.
     *
     * @access protected
     *
     * @var object
     */
    protected $data;

    /**
     * Method to get a table object, load it if necessary.
     *
     * @access public
     *
     * @param string $type
     * @param string $prefix
     * @param array $config
     *
     * @return object
     */
    public function getTable( $type = 'Claim', $prefix = 'RSDirectoryTable', $config = array() )
    {
        return JTable::getInstance($type, $prefix, $config);
    }

    /**
     * Method for getting the form from the model.
     *
     * @access public
     *
     * @param array $data
     * @param bool $loadData
     *
     * @return mixed
     */
    public function getForm( $data = array(), $loadData = true )
    {
        // Get the form.
        $form = $this->loadForm( 'com_rsdirectory.claim', 'claim', array('control' => 'jform', 'load_data' => $loadData) );
        $config = RSDirectoryConfig::getInstance();

        $user = JFactory::getUser();
        // if the user is logged in remove these fields
        if (!empty($user->id)) {
            $form->removeField('name');
            $form->removeField('email');
        }

        if (!$config->get('use_claim_phone_number')) {
            $form->removeField('phone_number');
        } else {
            if (!$config->get('required_claim_phone_number')) {
                $form->setFieldAttribute('phone_number', 'required', 'false');
            }
        }

        if ( empty($form) )
            return false;

        return $form;
    }

    /**
     * Method to get the data that should be injected in the form.
     *
     * @access protected
     *
     * @return array
     */
    protected function loadFormData()
    {
        return JFactory::getApplication()->getUserState('com_rsdirectory.edit.claim.data');
    }

    protected function matchUser($email)
    {
        static $matchUser = array();

        if (!isset($matchUser[$email]))
        {
            if (!empty($email))
            {
                $db = JFactory::getDbo();
                $query = $db->getQuery(true)
                    ->select($db->qn('id'))
                    ->from($db->qn('#__users'))
                    ->where($db->qn('email') . ' = ' . $db->q($email));
                $db->setQuery($query);

                if ($id = $db->loadResult())
                {
                    $user = JTable::getInstance('User', 'JTable', array());
                    $user->load($id);

                    $matchUser[$email] = $user;
                }
                else
                {
                    $matchUser[$email] = false;
                }
            }
            else
            {
                $matchUser[$email] = false;
            }
        }

        return $matchUser[$email];
    }

    /**
     * Validate form data.
     *
     * @access public
     *
     * @param object $form The form to validate against.
     * @param array $data The data to validate.
     * @param string $group The name of the field group to validate.
     *
     * @return mixed
     */
    public function validate($form, $data, $group = null)
    {
        $app = JFactory::getApplication();
        $user = JFactory::getUser();
        $logged = false;

        if ($user->id)
        {
            $data['name'] = $user->name;
            $data['email'] = $user->email;
            $logged = true;
        }
        else
        {
            // try to match a current user to the email entered
            $matched_user = $this->matchUser($data['email']);

            if ($matched_user !== false && $matched_user->id)
            {
                $data['name'] = $matched_user->name;
                $data['email'] = $matched_user->email;
                $user = $matched_user;
            }
        }

        $return = parent::validate($form, $data, $group);

        $entry_id = $app->input->getInt('entry_id');

        if ( !$entry_id || !( $this->entry = RSDirectoryHelper::getEntry($entry_id) ) )
        {
            $this->setError( JText::_('COM_RSDIRECTORY_INVALID_ENTRY_SPECIFIED') );
            $return = false;
        }

        // check to see if the entry is not already claimed
        $claim = $this->getTable();
        if ($claim->load(array('entry_id' => $this->entry->id)))
        {
            $denied_for_user = (empty($claim->approved) && !empty($claim->denied) && $claim->to_user_id == $user->id && !empty($claim->to_user_id));
            if (
                ($this->entry->user_id == $claim->to_user_id && $claim->approved == 1) ||
                (empty($claim->approved) && empty($claim->denied)) ||
                $denied_for_user
            )
            {
                $err_msg = JText::_('COM_RSDIRECTORY_ENTRY_ALREADY_CLAIMED');
                if ($denied_for_user)
                {
                    $err_msg = JText::_('COM_RSDIRECTORY_ENTRY_DENIED_CLAIM');
                }

                $this->setError( $err_msg );
                $return = false;
            }
        }

        // if the user is not logged in or a match is not found return the error
        if (!empty($this->entry->claim_listing_credits) && !$user->id)
        {
            $this->setError( JText::_('COM_RSDIRECTORY_USER_MUST_BE_LOGGED_IN') );
            $return = false;
        }


        // check to see if the entry has credits for the claim  and the user who claims it also has sufficient credits
        $user_credits = RSDirectoryCredits::getUserCredits($user->id);
        if (!empty($this->entry->claim_listing_credits) && $user_credits < $this->entry->claim_listing_credits)
        {
            if ($logged)
            {
                $error_string = 'COM_RSDIRECTORY_ENTRY_CLAIM_CREDIT_INSUFFICIENT_CREDITS';
            }
            else
            {
                $error_string = 'COM_RSDIRECTORY_ENTRY_CLAIM_CREDIT_INSUFFICIENT_CREDITS_NOT_LOGGED';
            }

            $this->setError( JText::sprintf($error_string,$user_credits, RSDirectoryRoute::getURL('credits', 'default', array('entry_id'=>$entry_id,'claim'=>'1'))));
            $return = false;
        }

        $config = RSDirectoryConfig::getInstance();

        if ($config->get('use_consent_checkbox_on_contact_author_form', 0) && empty($data['consent'])) {
            $this->setError( JText::_('COM_RSDIRECTORY_CONSENT_DEFAULT_ERROR') );
            $return = false;
        }

        if ( RSDirectoryHelper::checkUserPermission('claim_captcha') )
        {

            if ( $config->get('captcha_type') == 'built_in' )
            {
                require_once JPATH_ADMINISTRATOR . '/components/com_rsdirectory/helpers/captcha/captcha.php';

                // Initialize the CAPTCHA object.
                $captcha = new RsdirectoryCaptcha('claim');
                $captcha->case_sensitive = $config->get('captcha_case_sensitive');

                // Validate the CAPTCHA value.
                if ( empty($data['captcha']) || !$captcha->check( trim($data['captcha']) ) )
                {
                    $this->setError( JText::_('COM_RSDIRECTORY_CAPTCHA_ERROR') );
                    $return = false;
                }
            }
            else
            {
                try {
                    $response = isset($data['g_recaptcha_response']) ? $data['g_recaptcha_response'] : '';
                    $ip		  = $app->input->server->get('REMOTE_ADDR');
                    $secretKey= $config->get('recaptcha_new_secret_key');

                    $http = JHttpFactory::getHttp();
                    if ($request = $http->get('https://www.google.com/recaptcha/api/siteverify?secret='.urlencode($secretKey).'&response='.urlencode($response).'&remoteip='.urlencode($ip))) {
                        $json = json_decode($request->body);
                    }
                } catch (Exception $e) {
                    $this->setError( $e->getMessage() );
                    $return = false;
                }

                if (!$json->success) {
                    $this->setError( JText::_('COM_RSDIRECTORY_CAPTCHA_ERROR') );
                    $return = false;
                }
            }
        }

        return $return;
    }

    /**
     * Send email.
     *
     * @access public
     *
     * @param array $data
     *
     * @return bool
     */
    public function save($data)
    {
        // Do a few checks.
        if (!$data || !$this->entry)
        {
            return false;
        }

        $user = JFactory::getUser();

        // check for permissions
        if (!RSDirectoryHelper::checkUserPermission('can_claim_available_entries', $user->id))
        {
            $this->setError(JText::_('COM_RSDIRECTORY_CLAIM_LISTING_FAILED_NO_PERMISSION'));
            return false;
        }

        $logged = false;

        if ($user->id)
        {
            $data['name'] = $user->name;
            $data['email'] = $user->email;
            $data['to_user_id'] = $user->id;
            $logged = true;
        }
        else
        {
            // try to match a current user to the email entered
            $matched_user = $this->matchUser($data['email']);

            if ($matched_user !== false && $matched_user->id)
            {
                $data['name'] = $matched_user->name;
                $data['email'] = $matched_user->email;
                $data['to_user_id'] = $matched_user->id;
                $user = $matched_user;
            }
        }

        if ($this->entry->user_id == $user->id)
        {
            $this->setError(JText::_('COM_RSDIRECTORY_CLAIM_LISTING_FAILED_SAME_USER'));
            return false;
        }

        if (empty($user->id) && RSDirectoryConfig::getInstance()->get('use_claim_create_user', 1))
        {
            require_once JPATH_ADMINISTRATOR . '/components/com_rsdirectory/helpers/registration.php';
            $reg = new RSDirectoryRegistration;
            $reg->setData( array(
                'name' => $data['name'],
                'email' => $data['email']
            ) );

            if ( $reg->register() )
            {
                $data['to_user_id'] = $reg->getUserId();

                // Remember the id of the newly registered user.
                JFactory::getApplication()->setUserState('com_rsdirectory.registration.user.id', $data['to_user_id']);
            }
            else
            {
                $reg_errors = $reg->getErrors();

                if ($reg_errors)
                {
                    foreach ($reg_errors as $reg_error)
                    {
                        $this->setError($reg_error);
                    }
                }
                else
                {
                    $this->setError( JText::_('COM_RSDIRECTORY_CLAIM_LISTING_FAILED_USER_REGISTRATION') );
                }

                return false;
            }
        }

        $data['entry_id'] = $this->entry->id;
        $data['from_user_id'] = $this->entry->user_id;

        $jdate = new JDate;
        $data['date_claimed'] =  $jdate->toSql();

        if (empty($this->entry->claim_listing_credits))
        {
            $data['paid'] = 1;
        }
        else if ($user->id)
        {
            $entry_credits_objects = array(
                (object)array(
                    'object_type' => 'claim_entry',
                    'object_id' => 0,
                    'credits' => (int) $this->entry->claim_listing_credits,
                ),
            );

            $user_credits = RSDirectoryCredits::getUserCredits($user->id);
            $checkCredits = RSDirectoryCredits::checkUserCredits($this->entry->claim_listing_credits, $user->id);

            if (!$checkCredits)
            {
                if ($logged)
                {
                    $error_string = 'COM_RSDIRECTORY_ENTRY_CLAIM_CREDIT_INSUFFICIENT_CREDITS';
                }
                else
                {
                    $error_string = 'COM_RSDIRECTORY_ENTRY_CLAIM_CREDIT_INSUFFICIENT_CREDITS_NOT_LOGGED';
                }

                $this->setError( JText::sprintf($error_string,$user_credits, RSDirectoryRoute::getURL('credits', 'default', array('entry_id'=>$this->entry->id,'claim'=>'1'))));
                return false;
            }

            $entry_credits_objects = RSDirectoryCredits::cleanEntryCredits($entry_credits_objects);

            if ( !RSDirectoryCredits::addEntryCreditsObjects($this->entry->id, $entry_credits_objects, $user->id) )
            {
                $this->setError(JText::_('COM_RSDIRECTORY_CLAIM_ERROR_CHARGING_CREDITS'));
                return false;
            }

            $data['paid'] = 1;
            // save the spent credits
            $data['claim_credits'] = $this->entry->claim_listing_credits;
        }

        $claim = $this->getTable();

        $return = false;
        try
        {
            $return = $claim->save($data);

            if ($return)
            {
                require_once JPATH_ADMINISTRATOR . '/components/com_rsdirectory/helpers/placeholders.php';

	            // Get the RSDirectory! config object.
	            $config = RSDirectoryConfig::getInstance();

                $this->data = (object)$data;
                $this->entry = RSDirectoryHelper::getEntryData($this->entry);
                $form = $this->entry->form;

                // Process placeholders.
                $from_name = $this->processPlaceholders($form->claim_from_name);
	            $reply_to_email = $this->processPlaceholders($form->claim_from_email);
                $to_name = $this->processPlaceholders($form->claim_to_name);
                $to_email = $this->processPlaceholders($form->claim_to_email);
                $cc = $this->processPlaceholders($form->claim_cc);
                $bcc = $this->processPlaceholders($form->claim_bcc);
                $subject = $this->processPlaceholders($form->claim_subject);
                $message = $this->processPlaceholders($form->claim_message);

                // Get mailer.
                $mailer = JFactory::getMailer();

                // Set sender.
	            $mailer->setSender( array($config->get('from_email'), $from_name ));

	            // Set Reply to
	            $mailer->addReplyTo($reply_to_email);

                // Add recipient(s).
                $mailer->addRecipient(
                    explode(',', $to_email),
                    explode(',', $to_name)
                );

                // Set CC.
                if ($cc)
                {
                    $mailer->addCC( explode(',', $cc) );
                }

                // Set BCC.
                if ($bcc)
                {
                    $mailer->addBCC( explode(',', $bcc) );
                }

                // Set subject.
                $mailer->setSubject($subject);

                // Set body.
                $mailer->setBody($message);

                // Send as HTML.
                $mailer->isHtml($form->claim_send_html);

                if ( !empty($mailer->ErrorInfo) )
                {
                    $this->setError($mailer->ErrorInfo);
                    return false;
                }

                // Unset the contact form data.
                JFactory::getApplication()->setUserState('com_rsdirectory.edit.claim.data', null);

                try
                {
                    $result = $mailer->Send();

                    if (is_a($result, 'JException'))
                    {
                        throw new Exception($result->getMessage());
                    }
                }
                catch (Exception $e)
                {
                    $this->setError($e->getMessage());
                }

                // Unset the contact form data.
                JFactory::getApplication()->setUserState('com_rsdirectory.edit.claim.data', null);
            }
        }
        catch (Exception $e)
        {
            $this->setError($e->getMessage());
        }

        return $return;
    }
    /**
     * Process placeholders.
     *
     * @access protected
     *
     * @param string $text
     *
     * @return string
     */
    protected function processPlaceholders($text)
    {
        // Process the regular placeholders.
        $text = RSDirectoryPlaceholders::getInstance($text, $this->entry->form->fields, $this->entry, $this->entry->form)
            ->setParams('email')
            ->process();

        // Process the contact specific placeholders.
        $replace = array(
            '{claim.name}',
            '{claim.email}',
            '{claim.message}',
            '{claim.phone_number}',
        );

        $replace_with = array(
            strip_tags($this->data->name),
            strip_tags($this->data->email),
            strip_tags($this->data->message),
            (!empty($this->data->phone_number) ? strip_tags($this->data->phone_number) : '')
        );
		
		$text = (string) $text;
        $text = str_replace($replace,$replace_with, $text);

        return $text;
    }
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists