| Current Path : /home/zieirix/www/plugins/system/jaosmap/ |
| Current File : /home/zieirix/www/plugins/system/jaosmap/jaosmap.php |
<?php
/**
* ------------------------------------------------------------------------
* JA Open Street Map
* ------------------------------------------------------------------------
* Copyright (C) 2004-2018 J.O.O.M Solutions Co., Ltd. All Rights Reserved.
* @license - Copyrighted Commercial Software
* Author: J.O.O.M Solutions Co., Ltd
* Websites: http://www.joomlart.com - http://www.joomlancers.com
* This file may not be redistributed in whole or significant part.
* ------------------------------------------------------------------------
*/
// no direct access
defined('_JEXEC') or die('Restricted access');
jimport('joomla.plugin.plugin');
/**
*
* JA LEAFLET MAP PLUGIN SYSTEM CLASS
* @author JoomlArt
*
*/
class plgSystemJaosmap extends JPlugin
{
protected $_plgCodeNew = "#{jaosmap(.*?)}\s*{/jaosmap}#i";
protected $_plgCode = "#{jaosmap(.*?)}#i";
protected $mapSetting = array();
protected $mapId = null;
/**
*
* Construct JA Googla Map
* @param object $subject
* @param object $config
*/
function __construct(&$subject, $config)
{
$mainframe = JFactory::getApplication();
parent::__construct($subject, $config);
$this->plugin = JPluginHelper::getPlugin('system', 'jaosmap');
$this->plgParams = new JRegistry();
$this->plgParams->loadString($this->plugin->params);
}
function onBeforeRender() {
$app = JFactory::getApplication();
if ($app->isAdmin()) {
return;
}
JHtml::_('jquery.framework');
JHtml::_('behavior.core');
$doc = JFactory::getDocument();
$doc->addScriptOptions('jaosmap', array('juri_root' => JUri::root()));
$doc->addStyleSheet(Juri::root() . 'plugins/system/jaosmap/assets/leaflet/leaflet.css');
$doc->addStyleSheet(Juri::root() . 'plugins/system/jaosmap/assets/leaflet-routing-machine/leaflet-routing-machine.css');
$doc->addStyleSheet("https://api.tiles.mapbox.com/mapbox-gl-js/v0.35.1/mapbox-gl.css");
$doc->addScript(Juri::root() . 'plugins/system/jaosmap/assets/leaflet/leaflet.js');
$doc->addScript(Juri::root() . 'plugins/system/jaosmap/assets/leaflet-routing-machine/leaflet-routing-machine.min.js');
$doc->addScript("https://api.tiles.mapbox.com/mapbox-gl-js/v0.35.1/mapbox-gl.js");
$doc->addScript(JUri::root() . 'plugins/system/jaosmap/assets/mapbox-gl-leaflet-master/leaflet-mapbox-gl.js');
$doc->addScript(JUri::root() . 'plugins/system/jaosmap/assets/jaosmap.js');
}
static function get_web_page( $url )
{
$user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
$options = array(
CURLOPT_CUSTOMREQUEST => "GET", //set request type post or get
CURLOPT_POST => false, //set to GET
CURLOPT_USERAGENT => $user_agent, //set user agent
// CURLOPT_COOKIEFILE =>"cookie.txt", //set cookie file
// CURLOPT_COOKIEJAR =>"cookie.txt", //set cookie jar
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
function onAfterRender()
{
$app = JFactory::getApplication();
if ($app->isAdmin()) {
return;
}
$body = $app->getBody();
$plgParams = $this->plgParams;
$disable_map = $plgParams->get('disable_map', 0);
if ($disable_map) {
$body = $this->removeCode($body);
$app->setBody($body);
return;
}
if (!preg_match($this->_plgCodeNew, $body) && !preg_match($this->_plgCode, $body)) {
return;
}
//ignore short-code that placed in text/editor field
$pattern = '#value\s*=\s*"[^"]*?{jaosmap.*?}(\s*{/jaosmap})?[^"]*?"#';
$pattern2 = '#<textarea[^>]*?>[\s\S]*?{jaosmap.*?}(\s*{/jaosmap})?[\s\S]*?</textarea>#';
$body = preg_replace_callback($pattern, array($this, 'escapeMap'), $body);
$body = preg_replace_callback($pattern2, array($this, 'escapeMap'), $body);
//generate map
$body = preg_replace_callback($this->_plgCodeNew, array($this, 'genMap'), $body);
$body = preg_replace_callback($this->_plgCode, array($this, 'genMap'), $body);
//restore short codes
$body = str_replace(array('{[jaosmap]', '{/[jaosmap]}'), array('{jaosmap', '{/jaosmap}'), $body);
$app->setBody($body);
}
function escapeMap($matches) {
return str_replace(array('{jaosmap', '{/jaosmap}'), array('{[jaosmap]', '{/[jaosmap]}'), $matches[0]);
}
function genMap($matches) {
static $mapid = 0;
$mapid++;
$this->mapId = $mapid;
$this->mapSetting = $this->parseAttributes($matches[0]);
$output = $this->loadLayout($this->plugin, 'default');
return $output;
}
/**
* @ref JUtility::parseAttributes
*/
protected function parseAttributes($string)
{
$attr = array();
$retarray = array();
$string = preg_replace("/\\\\'/", '__QUOTE__', $string);
// Let's grab all the key/value pairs using a regular expression
preg_match_all("/([\w:-]+)[\s]?=[\s]?'([^']*)'/i", $string, $attr);
if (is_array($attr))
{
$numPairs = count($attr[1]);
for ($i = 0; $i < $numPairs; $i++)
{
$retarray[$attr[1][$i]] = str_replace('__QUOTE__', "\'", $attr[2][$i]);
}
}
return $retarray;
}
/**
*
* Remove map code tag
* @param string $content
* @return string
*/
function removeCode($content)
{
return preg_replace($this->_plgCodeNew, '', $content);
return preg_replace($this->_plgCode, '', $content);
}
/**
*
* Load content into layout
* @param object $plugin
* @param string $layout
* @return string
*/
function loadLayout($plugin, $layout = 'default')
{
$layout_path = JPluginHelper::getLayoutPath('system', 'jaosmap');
if ($layout_path) {
ob_start();
require $layout_path;
$content = ob_get_contents();
ob_end_clean();
return $content;
}
return '';
}
/*
* AJAX Call to transfer location address to lat long.
* @param array all address without latlong.
*/
function onAjaxJaosmap()
{
$latlong = [];
$jinput = JFactory::getApplication()->input;
$address = $jinput->get('address', array(), 'ARRAY');
foreach ($address AS $k => $add) {
$data = plgSystemJaosmap::get_web_page('https://nominatim.openstreetmap.org/search?q='.urlencode($add).'&format=json&addressdetails=0');
$content = json_decode($data['content']);
if (!empty($content[0]) && !empty($content[0]->lat) && !empty($content[0]->lon)) {
$latlong[$k] = [$content[0]->lat, $content[0]->lon];
}
}
return $latlong;
}
}