Your IP : 216.73.216.54


Current Path : /var/www/html/plugins/jvextensions/jvldarticle/
Upload File :
Current File : /var/www/html/plugins/jvextensions/jvldarticle/jvldarticle.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 plgJvextensionsJvldarticle extends JPlugin
{
    var $extension = 'com_content';

    var $uid = 0;
    var $featured = 1;
    var $efields = array();
    var $tborph = 1;
    var $numchar_desc = 0;

	var $content_catid = 0;
	var $content_imgtype = 0;

	var $k2_catid = 0;
	var $k2_imgtype = 0;
    var $k2_tag_add = 0;

	var $easyblog_catid = 0;
	var $easyblog_imgtype = 0;
    var $easyblog_tag_add = 0;

	var $linkdelaction = 0;
	var $txt_viewatdir = '';

    function __construct(&$subject, $params)
    {
        parent::__construct($subject, $params);

        if (!defined('_JVLD_INSTALLED'))
        	return;

        JvldPostinit::loadLanguageFile();

        $this->extension = $this->params->get('extension', 'com_content');
        $this->uid = (int)$this->params->get('uid', 0);
	    $this->efields = $this->params->get('efields', array());
        $this->featured = (int)$this->params->get('featured', 1);
        $this->numchar_desc = (int)$this->params->get('numchar_desc', 0);
        $this->tborph = (int)$this->params->get('tborph', 1);

	    $this->content_catid = (int)$this->params->get('content_catid', 0);
	    $this->content_imgtype = (int)$this->params->get('content_imgtype', 0);

	    $this->k2_catid = (int)$this->params->get('k2_catid', 0);
	    $this->k2_imgtype = (int)$this->params->get('k2_imgtype', 0);
	    $this->k2_tag_add = (int)$this->params->get('k2_tag_add', 0);

	    $this->easyblog_catid = (int)$this->params->get('easyblog_catid', 0);
	    $this->easyblog_imgtype = (int)$this->params->get('easyblog_imgtype', 0);
        $this->easyblog_tag_add = (int)$this->params->get('easyblog_tag_add', 0);

		$this->linkdelaction = (int)$this->params->get('linkdelaction', 0);
	    $this->txt_viewatdir = strip_tags($this->params->get('txt_viewatdir', ''));
    }

    public function OnLinkPublish($lid)
    {
        if (!defined('_JVLD_INSTALLED'))
        	return;

        try
        {
        	JvldLog::log('JvldArticle: Joomla article creation started');

	        $imgtype = \Joomla\String\StringHelper::substr($this->extension, 4).'_imgtype';
	        $imgtype_val = $this->{$imgtype};

	        $catidtype = \Joomla\String\StringHelper::substr($this->extension, 4).'_catid';
	        $catid_val = $this->{$catidtype};

			if (!$catid_val)
				throw new Exception('JvldArticle: Failed - Destination category is not chosen');

			$link = JvldDb::getRow("select * from #__jvld_links where id = ".(int)$lid);
			if (!$link)
				throw new Exception('JvldArticle: Invalid link');

			$cfg = JvldCfg::getInstance();

        	$profile = JvldDb::getRow("select title, description, banner from #__jvld_linkprof where lid = ".(int)$lid." order by RAND() limit 0, 1");
        	if (!$profile)
        		throw new Exception("JvldArticle: No random text profile could be found");

        	$row = JvldDb::getRow("select id from #__content where title = '".JvldSecure::defendSQL($profile->title)."' and catid = '".(int)$catid_val."'");
        	if ($row)
        		throw new Exception("JvldArticle: Article with this title already exists in the chosen category");

        	$linkurl = JvldPostinit::getRoute('detailpageurl', $link->id, 0);

        	$created = $link->link_published_on;

        	// if no author is set in article plugin, then use the listing author
	        $created_by = ($this->uid) ? $this->uid : ($link->uid ? $link->uid : JFactory::getUser()->get('id'));

			if (!$created_by)
				throw new Exception("JvldArticle: Author for the article is 0. Pls check the config and set a default author when guest adds a link");

        	$pictag = '';
        	switch ($this->tborph)
        	{
        		case 1:
        		{
        			if ($cfg->get('pgallery_en'))
        			{
        				$picarr = JvldPostinit::getRandomPhotoOfLink($lid, 'image');
        				if (count($picarr))
				        {
					        if (JFile::exists(_JVLD_PHOTOS_ABSPATH . DIRECTORY_SEPARATOR . $picarr['pic']))
						        $pictag = '<img src="' . JvldFrameworkHelper::getApprImage($picarr['pic'], _JVLD_PHOTOS_URLPATH) . '" alt="' . htmlspecialchars($picarr['title']) . '"/>';
				        }
        			}
        			break;
        		}
        		case 2:
        		{
			        $thumbObj = new JvldPreviewsCache($link->partner_url, _JVLD_CONTEXT_LINKDET, true);
			        $picture = $thumbObj->getPreview();

			        if ($picture == '')
			        {
			        	JvldLog::log("JvldArticle: No thumbnail available");
			        	break;
			        }

			        if (\Joomla\String\StringHelper::stristr($picture, "-imgwait") !== FALSE)
			        {
				        JvldLog::log("JvldArticle: Thumbnail returned is an imgwait");
				        break;
			        }

			        $pictag = '<img src="'.$picture.'" alt="'.htmlspecialchars($profile->title).'" />';
        			break;
        		}
        		default: break;
        	}

	        // Get the extra field values and append to description
	        require_once(_JVLD_ABSPATH_ADMIN.DIRECTORY_SEPARATOR.'libraries'.DIRECTORY_SEPARATOR.'class.extrafields.php');

	        $fnames = $fvalues = $flabels = array();
	        for ($i = 0; $i < count($this->efields); $i++)
	        {
		        $field = new JvldField($this->efields[$i]);
		        $fval  = "field" . $this->efields[$i];

		        array_push($fnames, $field->getName());
		        array_push($fvalues, \Joomla\String\StringHelper::trim(stripslashes($link->{$fval})));
		        array_push($flabels, $field->showLabel());
	        }

	        $description_t = '';

	        if ($profile->description != '')
	        	$description_t .= ($this->numchar_desc) ? \Joomla\String\StringHelper::substr($profile->description, 0, $this->numchar_desc).'...' : $profile->description;

	        if ($profile->banner != '')
		        $description_t .= '<img src="'.JvldFrameworkHelper::getApprImage($profile->banner, _JVLD_PROFBANNERS_URLPATH).'" alt="'.htmlspecialchars($profile->title).'" />';

	        $tmpl = new JvldTemplateFile('plg_article');
	        $tmpl->pictag = (!$imgtype_val) ? $pictag : '';
	        $tmpl->description = $description_t;
	        $tmpl->fnames = $fnames;
	        $tmpl->fvalues = $fvalues;
	        $tmpl->flabels = $flabels;
	        $tmpl->txt_viewatdir = $this->txt_viewatdir;
	        $tmpl->linkurl = $linkurl;
	        $tmpl->title = $profile->title;
	        $description = $tmpl->getContents();

	        // Get the image url from the pictag
	        $imgurl = ($pictag != '') ? $this->getImageFromHtml($pictag) : ''; // empty or url

	        // create the article
	        $id_component = $this->createArticle($profile->title, $description, $catid_val, $created, $created_by, $link->metakeys, $link->access, $imgtype_val, $imgurl);

			// Set link associations
			if ($id_component)
			{
				JvldPostinit::setLink3PDAssociation($lid, $this->extension, $id_component);
				JvldLog::log("Link-Article association has been set successfully");
			}
			else
			{
				JvldLog::log("Link-Article association could not be set as article ID returned from createArticle is 0");
			}
        }
        catch (Exception $ex)
        {
        	JvldLog::log('JvldArticle Error: '.$ex->getMessage());
        }

        return;
    }

    private function createArticle($title, $description, $catid, $created, $created_by, $metakeys, $access, $imgtype, $imgurl)
    {
		$obj = JvldArticlesExtensionsFactory::create($this->extension);
		if ($obj == null)
			return 0;

		$params = array(
        				'title' => $title,
        				'description' => $description,
        				'created' => $created,
        				'created_by' => $created_by,
        				'metakeys' => $metakeys,
        				'access' => $access,
                        'featured' => $this->featured,
                        'catid' => $catid,
                        'k2_add_tag' => $this->k2_tag_add,
        				'easyblog_add_tag' => $this->easyblog_tag_add,
				        'imgtype' => $imgtype,
				        'imgurl' => $imgurl,
                    );
        return $obj->createArticle($params);
    }

	public function OnLinkDelete($id)
	{
		if ($this->linkdelaction)
		{
			if (!defined('_JVLD_INSTALLED'))
				return;

			$obj = JvldArticlesExtensionsFactory::create($this->extension);
			if ($obj != null)
			{
				$linkassoc = JvldDb::getRow("select * from #__jvld_3pdassoc where lid = ".(int)$id);

				$name = "id_" . $this->extension;
				$obj->deleteArticle($linkassoc->{$name});
			}
		}
	}

	private function getImageFromHtml($html)
	{
		JvldLog::log("html - ".htmlentities($html));

		if (preg_match_all("/<img(.*?)>/is", $html, $matches))
		{
			$imgtag = '<img '.$matches[1][0].'>';
			$imgval = $this->getTagParam("src", $imgtag);

			// urls can start with http:// or https:// or // or directly the image name with or without the path
			if ((substr($imgval, 0, 7) == "http://") || (substr($imgval, 0, 8) == "https://"))
			{
				JvldLog::log("1 - ".$imgval);
				return $imgval;
			}

			if (substr($imgval, 0, 2) == "//")
			{
				$imgval = \Joomla\String\StringHelper::substr($imgval, 2);
				JvldLog::log("2 - ".$imgval);
				return (_JVLD_IS_SSL) ? "https://".$imgval : "http://".$imgval;
			}

			return JUri::root().$imgval;
		}

		JvldLog::log("3 - None");
		return '';
	}

	private function getTagParam($param, $tag)
	{
		return (preg_match('/'.preg_quote($param).'=([\'"])?((?(1).+?|[^\s>]+))(?(1)\1)/is', $tag, $match)) ? urldecode($match[2]) : '';
	}
}