Your IP : 216.73.216.54


Current Path : /var/www/html/components/com_jvld/helpers/
Upload File :
Current File : /var/www/html/components/com_jvld/helpers/jvld.util.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');

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

abstract class JvldFendModel extends JModelForm
{
	public $cfg;
	public $user;
	public $input;
	public $pop;
	private $properties = array();

	public function __construct($config = array())
	{
		parent::__construct($config);

		$this->cfg = JvldCfg::getInstance();
		$this->user = JFactory::getUser();
		$this->input = JFactory::getApplication()->input;
		$this->pop = $this->input->getInt('pop', 0);

		$app = JFactory::getApplication('site');
	}

	function __get($property)
	{
		return $this->properties[$property];
	}

	function __set($property, $value)
	{
		$this->properties[$property] = $value;
	}

	function __isset($property)
	{
		if (isset($this->properties[$property]))
			return true;

		return false;
	}

	protected function preprocessForm(JForm $form, $data, $group = 'content')
	{
		// Captcha preprocessing
		$this->preProcessCaptcha($form);

		parent::preprocessForm($form, $data, $group);
	}

	private function preProcessCaptcha($form)
	{
		$show_captcha = -1;

		if (($this->cfg->get('captcha') === '0') || ($this->cfg->get('captcha') === 0))
		{
			$show_captcha = 0;
		}
		else
		{
			$who = $this->cfg->get('captcha_who');
			$show_captcha = ($who) ? 1 : (($this->user->get('id')) ? 0 : 1);
		}

		if ($show_captcha)
		{
			JvldLog::log(__METHOD__.' - Captcha is to be shown and captcha selected is: '.$this->cfg->get('captcha'));
		}
		else
		{
			JvldLog::log(__METHOD__.' - Captcha is not to be shown');
			$form->removeField('captcha');
		}
	}

	public function preProcess($session_store=true, $addn_session_params=array())
	{
		$data  = JFactory::getApplication()->input->post->get('jform', array(), 'array');
		if (!count($data))
			return;

		$form = $this->getForm($data, false);

		$validData = $this->validate($form, $data);
		if ($validData === false)
		{
			$msg = '';
			$errors = $this->getErrors();
			for ($i=0;$i<count($errors);$i++)
				$msg .= $errors[$i]->getMessage()."<br />";

			throw new Exception($msg);
		}

		foreach ($validData as $var=>$val)
			$this->{$var} = $val;

		if ($session_store == true)
		{
			if (count($addn_session_params))
				$validData = array_merge($validData, $addn_session_params);

			JvldSession::clear(array('valid_data'), 'addlink');
			JvldSession::set('valid_data', $validData, 'addlink');
		}
	}
}

abstract class JvldSiteUtil
{
	public static function showAlerts()
	{
		$cfg = JvldCfg::getInstance();

		$dir_status = $cfg->get('dir_online');
		if (!$dir_status)
		{
			echo '<div class="jvalert jvalert-error">'.$cfg->get("dir_offline_msg").'</div>';
			return 1;
		}

		return 0;
	}

	public static function getRelatedArticles($keywordlist)
    {
        $cfg = JvldCfg::getInstance();
        if ($cfg->get('relart_enabled') == '')
        	return array();

	    if ($cfg->get('relart_enabled') == 'all')
	    {
	    	$rows = array();

	    	$comp_arrays = array('com_content', 'com_k2', 'com_easyblog', 'com_cobalt');
		    for ($i=0;$i<count($comp_arrays);$i++)
		    {
			    $obj = JvldArticlesExtensionsFactory::create($comp_arrays[$i]);
			    if ($obj != null)
			    {
			    	$tmp_array = $obj->getRelatedArticles($keywordlist);
				    if (count($tmp_array))
				        $rows = array_merge($rows, $tmp_array);
			    }
		    }

		    return $rows;
	    }
	    else
	    {
		    $obj = JvldArticlesExtensionsFactory::create($cfg->get('relart_enabled'));
		    if ($obj == null)
			    return array();

		    return $obj->getRelatedArticles($keywordlist);
	    }
    }

	public static function isCategoryVisible($id)
    {
    	$nodes = JvldCategoryinfo::getPathToRoot($id);
        for ($i=0;$i<count($nodes);$i++)
        {
            $visibility = JvldDb::getValue("select published from #__categories where id = '".(int)$nodes[$i]->id."'");
            if (!$visibility)
                return 0;
        }
        return 1;
    }

	public static function getRSSLinksForHomepage(&$feed_titles, &$feed_urls)
    {
    	$cfg = JvldCfg::getInstance();

    	if (is_array($cfg->get('rss_homepage')))
	    {
	    	if (!count($cfg->get('rss_homepage')))
	    		return 0;

		    $rss_homepage = (array)$cfg->get('rss_homepage');
	    }
	    else
	    {
		    if ($cfg->get('rss_homepage') == '')
			    return 0;

		    $rss_homepage = json_decode($cfg->get('rss_homepage'), true);
	    }

    	for ($i=0;$i<count($rss_homepage);$i++)
    	{
    		switch ($rss_homepage[$i])
    		{
    			case 1:
    			{
    				$feed_titles[] = JText::_('COM_JVLD_LINKS_RECENT');
    				$feed_urls[] = JRoute::_(JvldPostinit::getRoute('showlinks', 'recent', 0).'&format=feed&type=rss');
    				break;
    			}
    			case 2:
    			{
    				if ($cfg->get('hitstracking_en'))
    				{
    					$feed_titles[] = JText::_('COM_JVLD_LINKS_POPULAR');
    					$feed_urls[] = JRoute::_(JvldPostinit::getRoute('showlinks', 'popular', 0).'&format=feed&type=rss');
    				}
    				break;
    			}
    			case 3:
    			{
    				if ($cfg->get('rating_system') != 'none')
    				{
    					$feed_titles[] = JText::_('COM_JVLD_LINKS_HRATED');
    					$feed_urls[] = JRoute::_(JvldPostinit::getRoute('showlinks', 'highlyrated', 0).'&format=feed&type=rss');
    				}
    				break;
    			}
    			case 4:
    			{
    				$feed_titles[] = JText::_('COM_JVLD_LINKS_RANDOM');
    				$feed_urls[] = JRoute::_(JvldPostinit::getRoute('showlinks', 'random', 0).'&format=feed&type=rss');
    				break;
    			}
    			case 5:
    			{
    				if ($cfg->get('felinks_en'))
    				{
    					$feed_titles[] = JText::_('COM_JVLD_LINKS_FEATURED');
    					$feed_urls[] = JRoute::_(JvldPostinit::getRoute('featured', 0, 0).'&format=feed&type=rss');
    				}
    				break;
    			}
    			case 6:
    			{
    				if ($cfg->get('like_en'))
    				{
    					$feed_titles[] = JText::_('COM_JVLD_LINKS_MLIKED');
    					$feed_urls[] = JRoute::_(JvldPostinit::getRoute('showlinks', 'mostliked', 0).'&format=feed&type=rss');
    				}
    				break;
    			}
    			case 7:
    			{
    				$feed_titles[] = JText::_('COM_JVLD_LINKS_MVIEWED');
    				$feed_urls[] = JRoute::_(JvldPostinit::getRoute('showlinks', 'mostviewed', 0).'&format=feed&type=rss');
    				break;
    			}
    			case 8:
    			{
				    if ($cfg->get('rating_system') != 'none')
    				{
    					$feed_titles[] = JText::_('COM_JVLD_LINKS_MVOTED');
    					$feed_urls[] = JRoute::_(JvldPostinit::getRoute('showlinks', 'mostvoted', 0).'&format=feed&type=rss');
    				}
    				break;
    			}
				case 9:
				{
					$feed_titles[] = JText::_('COM_JVLD_LINKS_TODAY');
					$feed_urls[] = JRoute::_(JvldPostinit::getRoute('showlinks', 'today', 0).'&format=feed&type=rss');
					break;
				}
    			default: break;
    		}
    	}

    	return 1;
    }

	public static function getFormValues($keys, $form='jform')
	{
		$input = JFactory::getApplication()->input;
		$results = $input->getArray(array('jform' => $keys));
		return $results[$form];
	}

    public static function cleanupString($str)
    {
    	return htmlspecialchars($str, ENT_COMPAT, 'UTF-8');
    }

	public static function sendEmailNotifications($obj, $owpaid_id)
	{
		$cfg = JvldCfg::getInstance();

		$lid = $obj->getInfo('id');
		$status = $obj->getInfo('link_status');
		$purl = JvldPostinit::getLinkUrlorTitleForDisplay($obj->getInfo('partner_url'), $obj->getInfo('partner_title'));
		$pemail = $obj->getInfo('partner_email');
		$partner_linkloc_url = JvldUtil::getPartnerLinkAtHome($obj->getPrimaryCategory());
		$partner_detail_url = JvldInit::getSEFUrlForXternal(JvldPostinit::getRoute('detailpageurl', $lid));
		$spam = ($obj->getInfo('spam_score') > $cfg->get('spam_min_score')) ? 1 : 0;

		if ($status == _JVLD_LINKSTATUS_ESTABLISHED)
		{
			$em = new JvldEmail('NP_LINK_APPROVAL');
			$em->setTag('{PARTNER-URL}', $purl);
			$em->setTag('{PARTNER-LINKLOC-AT-HOME}', $partner_linkloc_url);
			$em->setTag('{PARTNER-DETAILPAGE-HOME}', $partner_detail_url);
			$em->send($pemail);

			$em = new JvldEmail('FNA_LINK_AUTOAPPROVED');
			$em->setTag('{PARTNER-URL}', $purl);
			$em->setTag('{PARTNER-LINKLOC-AT-HOME}', $partner_linkloc_url);
			$em->setTag('{PARTNER-DETAILPAGE-HOME}', $partner_detail_url);
			$em->setTag('{PAID-LINK-ALERT}', JvldUtil::getPaidOneWayLinkEmailAlertText($owpaid_id, JText::_("COM_JVLD_ADDLINK_EMAIL_ALERT_1")));
			$em->send($cfg->get('site_email'));
		}
		else if ($status == _JVLD_LINKSTATUS_WPENDING)
		{
			$paid_link_alert = JvldUtil::getPaidOneWayLinkEmailAlertText($owpaid_id, JText::_("COM_JVLD_ADDLINK_EMAIL_ALERT_1"));

			if ($cfg->get('link_apprem'))
			{
				$apnowstr = JvldPostinit::generateKey(32);
				JvldDb::update("update `#__jvld_links` set `emstr` = '".JvldSecure::defendSQL($apnowstr)."' where `id` = '".(int)$lid."'");

				$approve_url = JvldInit::getSEFUrlForXternal(JvldPostinit::getRoute('emailappr', $apnowstr, 0));
				$reject_url = JvldInit::getSEFUrlForXternal(JvldPostinit::getRoute('emailrejt', $apnowstr, 0));
				$delete_url = JvldInit::getSEFUrlForXternal(JvldPostinit::getRoute('emaildel', $apnowstr, 0));

				$em = new JvldEmail('FNA_INCLUSION_REQUEST');
				$em->setTag('{PARTNER-EMAIL}', $pemail);
				$em->setTag('{PARTNER-URL}', $purl);
				$em->setTag('{PARTNER-LINKLOC-AT-HOME}', $partner_linkloc_url);
				$em->setTag('{PARTNER-DETAILPAGE-HOME}', $partner_detail_url);
				$em->setTag('{SPAM-ALERT}', (($spam) ? JText::_("COM_JVLD_ADDLINK_EMAIL_ALERT_2")."\r\n\r\n" : ""));
				$em->setTag('{PAID-LINK-ALERT}', $paid_link_alert);
				$em->setTag('{EMAIL-APPROVE-ACTION}', JText::sprintf("COM_JVLD_EMAIL_EMAIL_ACTION_TXT", $approve_url, $reject_url, $delete_url));
				$em->send($cfg->get('site_email'));
			}
			else
			{
				$em = new JvldEmail('FNA_INCLUSION_REQUEST');
				$em->setTag('{PARTNER-EMAIL}', $pemail);
				$em->setTag('{PARTNER-URL}', $purl);
				$em->setTag('{PARTNER-LINKLOC-AT-HOME}', $partner_linkloc_url);
				$em->setTag('{PARTNER-DETAILPAGE-HOME}', $partner_detail_url);
				$em->setTag('{SPAM-ALERT}', (($spam) ? JText::_("COM_JVLD_ADDLINK_EMAIL_ALERT_2")."\r\n\r\n" : ""));
				$em->setTag('{PAID-LINK-ALERT}', $paid_link_alert);
				$em->setTag('{EMAIL-APPROVE-ACTION}', "");
				$em->send($cfg->get('site_email'));
			}
		}
	}

	public static function setReturnPage($return_page = "")
	{
		if ($return_page == "")
			$return_page = JvldPostinit::getRoute("home");

		$app = JFactory::getApplication();
		$app->setUserState("com_jvld.return", base64_encode($return_page));
	}

	public static function getReturnPage()
	{
		$app = JFactory::getApplication();
		$return_page = $app->getUserState("com_jvld.return");

		if (($return_page == null) || (!JUri::isInternal(base64_decode($return_page))) || ($return_page == ''))
			return JvldPostinit::getRoute("home");

		return base64_decode($return_page);
	}

	public static function getSiteInfo($url)
	{
		try
		{
			if ($url == '')
				throw new Exception("Empty URL");

			$content = file_get_contents($url);
			@preg_match("/<title>(.*)<\/title>/i", $content, $match);
			$title = \Joomla\String\StringHelper::trim(strip_tags(@$match[1]));

			$tags     = get_meta_tags($url);
			$keywords = (isset($tags['keywords'])) ? \Joomla\String\StringHelper::trim(strip_tags($tags['keywords'])) : '';
			$metadesc = (isset($tags['description'])) ? \Joomla\String\StringHelper::trim(strip_tags($tags['description'])) : '';

			return array('title' => $title, 'keywords' => $keywords, 'metadesc' => $metadesc);
		}
		catch (Exception $ex)
		{
			JvldLog::log("Error in getSiteInfo for [".$url."]: ".$ex->getMessage());
			return array('title'=>'', 'keywords'=>'', 'metadesc'=>'');
		}
	}

	public static function getCurrentUri()
	{
		$url = JUri::getInstance()->toString();
		JvldLog::log("getCurrentUri: ".$url);

		return $url;
	}


	public static function checkForListingRenewability($linkplan, $expiration_date, $link_status, $link_owner_uid)
	{
		// it is a paid, non-recurring, and active listing with no permanent expiration, and not already expired and expiring in max N days from it's expiration date. user must be logged in and must be the owner of the listing to renew

		$cfg = JvldCfg::getInstance();
		$user = JFactory::getUser();

		if (!$user->get('id'))
			throw new Exception(JText::_('COM_JVLD_ERR_LOGIN_RENEW_LISTING'));

		if ($user->get('id') != $link_owner_uid)
			throw new Exception(JText::_('COM_JVLD_SETFE_NOT_OWNER'));

		if (!$linkplan->isPaid())
			throw new Exception(JText::_('COM_JVLD_ERR_NOTPAID_RENEW_LISTING'));

		if ($linkplan->isRecurring())
			throw new Exception(JText::_('COM_JVLD_ERR_RECUR_RENEW_LISTING'));

		if ($link_status != _JVLD_LINKSTATUS_ESTABLISHED)
			throw new Exception(JText::_('COM_JVLD_ERR_NOTACTIVE_RENEW_LISTING'));

		if ($expiration_date == JvldDate::getMySQLNullDateTime())
			throw new Exception(JText::_('COM_JVLD_ERR_NOEXPDATE_RENEW_LISTING'));

		$time_to_expire_secs = strtotime($expiration_date) - time();
		if ($time_to_expire_secs <= 0)
			throw new Exception(JText::_('COM_JVLD_ERR_EXPIRED_RENEW_LISTING'));

		if ($time_to_expire_secs > (24 * 3600 * $cfg->get('renew_days_from_expiry')))
			throw new Exception(JText::sprintf('COM_JVLD_ERR_NOTTIMEYET_RENEW_LISTING', $cfg->get('renew_days_from_expiry')));

		return;
	}
}