<?php
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED);
session_start();
if((($_SERVER['SERVER_ADDR']==$_SESSION['demo_server_ip']) && ($_SERVER['REMOTE_ADDR']!=$_SESSION['demo_developer_ip']) && ($_SESSION['id_user']==$_SESSION['demo_user_id'])) || ($_SESSION['sml_si']!=session_id())) {
    //DEMO CHECK
    die();
}
require('spreadsheet-reader/php-excel-reader/excel_reader2.php');
require('spreadsheet-reader/SpreadsheetReader.php');
require_once("../../db/connection.php");
ini_set('memory_limit','256M');
ini_set('max_execution_time', 240);
ini_set('max_input_time', 240);
if (!file_exists(dirname(__FILE__).'/../tmp/')) {
    mkdir(dirname(__FILE__).'/../tmp/', 0775);
}
if(!isset($_SESSION['id_map_sel'])) {
    echo "ERROR: retry later.";
    exit;
}
$id_map = $_SESSION['id_map_sel'];
$array_exist_markers = array();
$query = "SELECT id FROM sml_markers WHERE id_map=$id_map;";
$result = $mysqli->query($query);
if($result) {
    if($result->num_rows>0) {
        while($row=$result->fetch_array(MYSQLI_ASSOC)) {
            $id_m = $row['id'];
            array_push($array_exist_markers,$id_m);
        }
    }
}
$array_exist_categories = array();
$query = "SELECT id FROM sml_categories WHERE id_map=$id_map;";
$result = $mysqli->query($query);
if($result) {
    if($result->num_rows>0) {
        while($row=$result->fetch_array(MYSQLI_ASSOC)) {
            $id_c = $row['id'];
            array_push($array_exist_categories,$id_c);
        }
    }
}
$count = 0;
$count_i = 0;
$count_insert = 0;
$count_update = 0;
$error = '';
if(isset($_FILES) && !empty($_FILES['file']['name'])){
    $allowed_ext = array('csv','xls','xlsx');
    $filename = $_FILES['file']['name'];
    $ext = explode('.',$filename);
    $ext = strtolower(end($ext));
    if(in_array($ext,$allowed_ext)){
        $fname = "markers_".time().".$ext";
        $moved = move_uploaded_file($_FILES['file']['tmp_name'],dirname(__FILE__).'/../tmp/'.$fname);
        if($moved) {
            try {
                $Reader = new SpreadsheetReader(dirname(__FILE__).'/../tmp/'.$fname);
            } catch (Exception $e) {
                echo 'ERROR: error parsing file.';
                exit;
            }
            $first_line = false;
            foreach ($Reader as $Row) {
                if(!$first_line) {
                    if(count($Row)<107) {
                        echo 'ERROR: the file should have at least 107 columns, the uploaded file has '.count($Row).' columns!';
                        exit;
                    }
                    $first_line = true;
                }
                if($Row[0]=='id') continue;
                $i = 0;
                $id = $Row[$i++];
                $lat = str_replace(",",".",$Row[$i++]);
                $lon = str_replace(",",".",$Row[$i++]);
                if(!is_numeric($lat)) {
                    $lat = "";
                }
                if(!is_numeric($lon)) {
                    $lon = "";
                }
                $name = strip_tags($Row[$i++]);
                if(empty($name)) { continue; }
                $street = strip_tags($Row[$i++]);
                $city = strip_tags($Row[$i++]);
                $postal_code = strip_tags($Row[$i++]);
                $country = strip_tags($Row[$i++]);
                $state = strip_tags($Row[$i++]);
                $county = strip_tags($Row[$i++]);
                $website = strip_tags($Row[$i++]);
                $website_caption = strip_tags($Row[$i++]);
                $email = strip_tags($Row[$i++]);
                $phone = strip_tags($Row[$i++]);
                $whatsapp = strip_tags($Row[$i++]);
                $hours = $Row[$i++];
                $description = $Row[$i++];
                $icon = $Row[$i++];
                $icon_label = $Row[$i++];
                $icon_label_size = $Row[$i++];
                if($icon_label_size=='') $icon_label_size=12;
                $icon_label_color = $Row[$i++];
                $id_icon_library = $Row[$i++];
                if($id_icon_library=='') $id_icon_library=0;
                $active = $Row[$i++];
                if($active=='') $active=1;
                $view_directions = $Row[$i++];
                if($view_directions=='') $view_directions=1;
                $view_street_view = $Row[$i++];
                if($view_street_view=='') $view_street_view=1;
                $view_review = $Row[$i++];
                if($view_review=='') $view_review=1;
                $view_share = $Row[$i++];
                if($view_share=='') $view_share=1;
                $order = $Row[$i++];
                if($order=='') $order=0;
                $color_hex = $Row[$i++];
                if($color_hex=='') $color_hex='';
                $icon_color_hex = $Row[$i++];
                if($icon_color_hex=='') $icon_color_hex='#ffffff';
                $color_hex_hover = $Row[$i++];
                if($color_hex_hover=='') $color_hex_hover='';
                $icon_color_hex_hover = $Row[$i++];
                if($icon_color_hex_hover=='') $icon_color_hex_hover='';
                $marker_size = $Row[$i++];
                if($marker_size=='') $marker_size=0;
                $field_e1 = convertUrlsToLinks($Row[$i++]);
                $field_e1_name = $Row[$i++];
                $field_e1_icon = $Row[$i++];
                $field_e2 = convertUrlsToLinks($Row[$i++]);
                $field_e2_name = $Row[$i++];
                $field_e2_icon = $Row[$i++];
                $field_e3 = convertUrlsToLinks($Row[$i++]);
                $field_e3_name = $Row[$i++];
                $field_e3_icon = $Row[$i++];
                $field_e4 = convertUrlsToLinks($Row[$i++]);
                $field_e4_name = $Row[$i++];
                $field_e4_icon = $Row[$i++];
                $field_e5 = convertUrlsToLinks($Row[$i++]);
                $field_e5_name = $Row[$i++];
                $field_e5_icon = $Row[$i++];
                $field_e6 = convertUrlsToLinks($Row[$i++]);
                $field_e6_name = $Row[$i++];
                $field_e6_icon = $Row[$i++];
                $field_e7 = convertUrlsToLinks($Row[$i++]);
                $field_e7_name = $Row[$i++];
                $field_e7_icon = $Row[$i++];
                $field_e8 = convertUrlsToLinks($Row[$i++]);
                $field_e8_name = $Row[$i++];
                $field_e8_icon = $Row[$i++];
                $field_e9 = convertUrlsToLinks($Row[$i++]);
                $field_e9_name = $Row[$i++];
                $field_e9_icon = $Row[$i++];
                $field_e10 = convertUrlsToLinks($Row[$i++]);
                $field_e10_name = $Row[$i++];
                $field_e10_icon = $Row[$i++];
                $field_e11 = convertUrlsToLinks($Row[$i++]);
                $field_e11_name = $Row[$i++];
                $field_e11_icon = $Row[$i++];
                $field_e12 = convertUrlsToLinks($Row[$i++]);
                $field_e12_name = $Row[$i++];
                $field_e12_icon = $Row[$i++];
                $field_e13 = convertUrlsToLinks($Row[$i++]);
                $field_e13_name = $Row[$i++];
                $field_e13_icon = $Row[$i++];
                $field_e14 = convertUrlsToLinks($Row[$i++]);
                $field_e14_name = $Row[$i++];
                $field_e14_icon = $Row[$i++];
                $field_e15 = convertUrlsToLinks($Row[$i++]);
                $field_e15_name = $Row[$i++];
                $field_e15_icon = $Row[$i++];
                $field_e16 = convertUrlsToLinks($Row[$i++]);
                $field_e16_name = $Row[$i++];
                $field_e16_icon = $Row[$i++];
                $field_e17 = convertUrlsToLinks($Row[$i++]);
                $field_e17_name = $Row[$i++];
                $field_e17_icon = $Row[$i++];
                $field_e18 = convertUrlsToLinks($Row[$i++]);
                $field_e18_name = $Row[$i++];
                $field_e18_icon = $Row[$i++];
                $field_e19 = convertUrlsToLinks($Row[$i++]);
                $field_e19_name = $Row[$i++];
                $field_e19_icon = $Row[$i++];
                $field_e20 = convertUrlsToLinks($Row[$i++]);
                $field_e20_name = $Row[$i++];
                $field_e20_icon = $Row[$i++];
                $button_b1 = $Row[$i++];
                $button_b1_icon = $Row[$i++];
                $button_b1_title = $Row[$i++];
                $min_zoom_level = $Row[$i++];
                $geofence_radius = $Row[$i++];
                $geofence_color = $Row[$i++];
                $id_categories = $Row[$i++];
                $open_sheet = $Row[$i++];
                $view_popup = $Row[$i++];
                $popup_image_height = $Row[$i++];
                $popup_background = $Row[$i++];
                $popup_color = $Row[$i++];
                if($min_zoom_level=='') $min_zoom_level=0;
                if($min_zoom_level<0) $min_zoom_level=0;
                if($min_zoom_level>20) $min_zoom_level=20;
                if($geofence_radius=='') $geofence_radius=0;
                if($geofence_color=='') $geofence_color='rgba(0,0,255,0.1)';
                if($open_sheet=='') $open_sheet=1;
                if($view_popup=='') $view_popup=1;
                if($popup_image_height=='') $popup_image_height=60;
                if($popup_background=='') $popup_background='#ffffff';
                if($popup_color=='') $popup_color='#000000';
                $visible_map = $Row[$i++];
                $visible_search = $Row[$i++];
                if($visible_map=='') $visible_map=1;
                if($visible_search=='') $visible_search=1;
                $count++;
                if(!in_array($id,$array_exist_markers)) {
                    $query = "INSERT IGNORE INTO sml_markers(id_map,name,street,city,postal_code,country,state,county,website,website_caption,phone,email,whatsapp,hours,description,icon,icon_label,icon_label_size,icon_label_color,lat,lon,active,view_directions,view_street_view,view_review,view_share,`order`,id_icon_library,marker_size,color_hex,icon_color_hex,extra_field_value_1,extra_field_value_2,extra_field_value_3,extra_field_value_4,extra_field_value_5,extra_field_value_6,extra_field_value_7,extra_field_value_8,extra_field_value_9,extra_field_value_10,extra_field_value_11,extra_field_value_12,extra_field_value_13,extra_field_value_14,extra_field_value_15,extra_field_value_16,extra_field_value_17,extra_field_value_18,extra_field_value_19,extra_field_value_20,extra_field_icon_1,extra_field_icon_2,extra_field_icon_3,extra_field_icon_4,extra_field_icon_5,extra_field_icon_6,extra_field_icon_7,extra_field_icon_8,extra_field_icon_9,extra_field_icon_10,extra_field_icon_11,extra_field_icon_12,extra_field_icon_13,extra_field_icon_14,extra_field_icon_15,extra_field_icon_16,extra_field_icon_17,extra_field_icon_18,extra_field_icon_19,extra_field_icon_20,extra_button_value_1,extra_button_icon_1,extra_button_title_1,min_zoom_level,geofence_radius,geofence_color,open_sheet,view_popup,popup_image_height,popup_background,popup_color,color_hex_hover,icon_color_hex_hover,visible_map,visible_search,extra_field_name_1,extra_field_name_2,extra_field_name_3,extra_field_name_4,extra_field_name_5,extra_field_name_6,extra_field_name_7,extra_field_name_8,extra_field_name_9,extra_field_name_10,extra_field_name_11,extra_field_name_12,extra_field_name_13,extra_field_name_14,extra_field_name_15,extra_field_name_16,extra_field_name_17,extra_field_name_18,extra_field_name_19,extra_field_name_20) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
                    if($smt = $mysqli->prepare($query)) {
                        $smt->bind_param('issssssssssssssssisssiiiiiiidsssssssssssssssssssssssssssssssssssssssssssssiisiiissssiissssssssssssssssssss',$id_map,$name,$street,$city,$postal_code,$country,$state,$county,$website,$website_caption,$phone,$email,$whatsapp,$hours,$description,$icon,$icon_label,$icon_label_size,$icon_label_color,$lat,$lon,$active,$view_directions,$view_street_view,$view_review,$view_share,$order,$id_icon_library,$marker_size,$color_hex,$icon_color_hex,$field_e1,$field_e2,$field_e3,$field_e4,$field_e5,$field_e6,$field_e7,$field_e8,$field_e9,$field_e10,$field_e11,$field_e12,$field_e13,$field_e14,$field_e15,$field_e16,$field_e17,$field_e18,$field_e19,$field_e20,$field_e1_icon,$field_e2_icon,$field_e3_icon,$field_e4_icon,$field_e5_icon,$field_e6_icon,$field_e7_icon,$field_e8_icon,$field_e9_icon,$field_e10_icon,$field_e11_icon,$field_e12_icon,$field_e13_icon,$field_e14_icon,$field_e15_icon,$field_e16_icon,$field_e17_icon,$field_e18_icon,$field_e19_icon,$field_e20_icon,$button_b1,$button_b1_icon,$button_b1_title,$min_zoom_level,$geofence_radius,$geofence_color,$open_sheet,$view_popup,$popup_image_height,$popup_background,$popup_color,$color_hex_hover,$icon_color_hex_hover,$visible_map,$visible_search,$field_e1_name,$field_e2_name,$field_e3_name,$field_e4_name,$field_e5_name,$field_e6_name,$field_e7_name,$field_e8_name,$field_e9_name,$field_e10_name,$field_e11_name,$field_e12_name,$field_e13_name,$field_e14_name,$field_e15_name,$field_e16_name,$field_e17_name,$field_e18_name,$field_e19_name,$field_e20_name);
                        $result = $smt->execute();
                        if($result) {
                            $id_insert = $mysqli->insert_id;
                            if(!empty($id_categories)) {
                                $array_cat = explode(",",$id_categories);
                                foreach ($array_cat as $id_cat) {
                                    if(in_array($id_cat,$array_exist_categories)) {
                                        $mysqli->query("INSERT INTO sml_markers_categories_assoc(id_marker,id_category) VALUES($id_insert,$id_cat)");
                                    }
                                }
                            }
                            if ($mysqli->affected_rows > 0) {
                                $count_insert++;
                            }
                            $count_i++;
                        } else {
                            echo 'ERROR: '.$mysqli->error;
                            exit;
                        }
                    } else {
                        echo 'ERROR: '.$mysqli->error;
                        exit;
                    }
                } else {
                    $query_t = "SELECT * FROM sml_markers WHERE id=$id LIMIT 1;";
                    $result_t = $mysqli->query($query_t);
                    $curr_data = [];
                    if($result_t) {
                        $curr_data = $result_t->fetch_array(MYSQLI_ASSOC);
                    }
                    $fields_to_check = [
                        'lat' => $lat, 'lon' => $lon, 'name' => $name, 'street' => $street,
                        'city' => $city, 'county' => $county, 'state' => $state, 'postal_code' => $postal_code,
                        'country' => $country, 'website' => $website, 'website_caption' => $website_caption,
                        'email' => $email, 'phone' => $phone, 'whatsapp' => $whatsapp, 'hours' => $hours, 'description' => $description,
                        'extra_field_name_1' => $field_e1_name, 'extra_field_value_1' => $field_e1,
                        'extra_field_name_2' => $field_e2_name, 'extra_field_value_2' => $field_e2,
                        'extra_field_name_3' => $field_e3_name, 'extra_field_value_3' => $field_e3,
                        'extra_field_name_4' => $field_e4_name, 'extra_field_value_4' => $field_e4,
                        'extra_field_name_5' => $field_e5_name, 'extra_field_value_5' => $field_e5,
                        'extra_field_name_6' => $field_e6_name, 'extra_field_value_6' => $field_e6,
                        'extra_field_name_7' => $field_e7_name, 'extra_field_value_7' => $field_e7,
                        'extra_field_name_8' => $field_e8_name, 'extra_field_value_8' => $field_e8,
                        'extra_field_name_9' => $field_e9_name, 'extra_field_value_9' => $field_e9,
                        'extra_field_name_10' => $field_e10_name, 'extra_field_value_10' => $field_e10,
                        'extra_field_name_11' => $field_e11_name, 'extra_field_value_11' => $field_e11,
                        'extra_field_name_12' => $field_e12_name, 'extra_field_value_12' => $field_e12,
                        'extra_field_name_13' => $field_e13_name, 'extra_field_value_13' => $field_e13,
                        'extra_field_name_14' => $field_e14_name, 'extra_field_value_14' => $field_e14,
                        'extra_field_name_15' => $field_e15_name, 'extra_field_value_15' => $field_e15,
                        'extra_field_name_16' => $field_e16_name, 'extra_field_value_16' => $field_e16,
                        'extra_field_name_17' => $field_e17_name, 'extra_field_value_17' => $field_e17,
                        'extra_field_name_18' => $field_e18_name, 'extra_field_value_18' => $field_e18,
                        'extra_field_name_19' => $field_e19_name, 'extra_field_value_19' => $field_e19,
                        'extra_field_name_20' => $field_e20_name, 'extra_field_value_20' => $field_e20,
                        'extra_button_value_1' => $button_b1, 'extra_button_title_1' => $button_b1_title
                    ];
                    $needs_update_date = false;
                    foreach($fields_to_check as $db_col => $new_val) {
                        $old_val = isset($curr_data[$db_col]) ? $curr_data[$db_col] : '';
                        $old_str = trim((string)$old_val);
                        $new_str = trim((string)$new_val);
                        if ($old_str === $new_str) continue;
                        $old_str = str_replace(["\r\n", "\r"], "\n", $old_str);
                        $new_str = str_replace(["\r\n", "\r"], "\n", $new_str);
                        if ($old_str === $new_str) continue;
                        $old_str_strip = stripslashes($old_str);
                        $new_str_strip = stripslashes($new_str);
                        if ($old_str_strip === $new_str_strip) continue;
                        if (html_entity_decode($old_str_strip, ENT_QUOTES) === html_entity_decode($new_str_strip, ENT_QUOTES)) continue;
                        if (is_numeric($old_str_strip) && is_numeric($new_str_strip)) {
                            if ((float)$old_str_strip == (float)$new_str_strip) continue;
                        }
                        if (empty($old_str_strip) && empty($new_str_strip)) continue;
                        $old_html = preg_replace('/<br\s*\/?>/i', '<br>', $old_str_strip);
                        $new_html = preg_replace('/<br\s*\/?>/i', '<br>', $new_str_strip);
                        $old_html = trim(preg_replace('/\s+/', ' ', $old_html));
                        $new_html = trim(preg_replace('/\s+/', ' ', $new_html));
                        if ($old_html === $new_html) continue;
                        $needs_update_date = true;
                        break;
                    }
                    $query = "UPDATE sml_markers SET name=?,street=?,city=?,postal_code=?,country=?,state=?,county=?,website=?,website_caption=?,phone=?,whatsapp=?,email=?,hours=?,description=?,lat=?,lon=?,icon=?,icon_label=?,icon_label_size=?,icon_label_color=?,active=?,view_directions=?,view_street_view=?,view_review=?,view_share=?,color_hex=?,icon_color_hex=?,marker_size=?,id_icon_library=?,extra_field_icon_1=?,extra_field_icon_2=?,extra_field_icon_3=?,extra_field_icon_4=?,extra_field_icon_5=?,extra_field_icon_6=?,extra_field_icon_7=?,extra_field_icon_8=?,extra_field_icon_9=?,extra_field_icon_10=?,extra_field_icon_11=?,extra_field_icon_12=?,extra_field_icon_13=?,extra_field_icon_14=?,extra_field_icon_15=?,extra_field_icon_16=?,extra_field_icon_17=?,extra_field_icon_18=?,extra_field_icon_19=?,extra_field_icon_20=?,extra_field_value_1=?,extra_field_value_2=?,extra_field_value_3=?,extra_field_value_4=?,extra_field_value_5=?,extra_field_value_6=?,extra_field_value_7=?,extra_field_value_8=?,extra_field_value_9=?,extra_field_value_10=?,extra_field_value_11=?,extra_field_value_12=?,extra_field_value_13=?,extra_field_value_14=?,extra_field_value_15=?,extra_field_value_16=?,extra_field_value_17=?,extra_field_value_18=?,extra_field_value_19=?,extra_field_value_20=?,extra_button_icon_1=?,extra_button_title_1=?,extra_button_value_1=?,`order`=?,min_zoom_level=?,geofence_radius=?,geofence_color=?,open_sheet=?,view_popup=?,popup_image_height=?,popup_background=?,popup_color=?,color_hex_hover=?,icon_color_hex_hover=?,visible_map=?,visible_search=?,extra_field_name_1=?,extra_field_name_2=?,extra_field_name_3=?,extra_field_name_4=?,extra_field_name_5=?,extra_field_name_6=?,extra_field_name_7=?,extra_field_name_8=?,extra_field_name_9=?,extra_field_name_10=?,extra_field_name_11=?,extra_field_name_12=?,extra_field_name_13=?,extra_field_name_14=?,extra_field_name_15=?,extra_field_name_16=?,extra_field_name_17=?,extra_field_name_18=?,extra_field_name_19=?,extra_field_name_20=? WHERE id=? AND id_map=?;";
                    if($smt = $mysqli->prepare($query)) {
                        $smt->bind_param('ssssssssssssssssssisiiiiissdisssssssssssssssssssssssssssssssssssssssssssiiisiiissssiissssssssssssssssssssii',$name,$street,$city,$postal_code,$country,$state,$county,$website,$website_caption,$phone,$whatsapp,$email,$hours,$description,$lat,$lon,$icon,$icon_label,$icon_label_size,$icon_label_color,$active,$view_directions,$view_street_view,$view_review,$view_share,$color_hex,$icon_color_hex,$marker_size,$id_icon_library,$field_e1_icon,$field_e2_icon,$field_e3_icon,$field_e4_icon,$field_e5_icon,$field_e6_icon,$field_e7_icon,$field_e8_icon,$field_e9_icon,$field_e10_icon,$field_e11_icon,$field_e12_icon,$field_e13_icon,$field_e14_icon,$field_e15_icon,$field_e16_icon,$field_e17_icon,$field_e18_icon,$field_e19_icon,$field_e20_icon,$field_e1,$field_e2,$field_e3,$field_e4,$field_e5,$field_e6,$field_e7,$field_e8,$field_e9,$field_e10,$field_e11,$field_e12,$field_e13,$field_e14,$field_e15,$field_e16,$field_e17,$field_e18,$field_e19,$field_e20,$button_b1_icon,$button_b1_title,$button_b1,$order,$min_zoom_level,$geofence_radius,$geofence_color,$open_sheet,$view_popup,$popup_image_height,$popup_background,$popup_color,$color_hex_hover,$icon_color_hex_hover,$visible_map,$visible_search,$field_e1_name,$field_e2_name,$field_e3_name,$field_e4_name,$field_e5_name,$field_e6_name,$field_e7_name,$field_e8_name,$field_e9_name,$field_e10_name,$field_e11_name,$field_e12_name,$field_e13_name,$field_e14_name,$field_e15_name,$field_e16_name,$field_e17_name,$field_e18_name,$field_e19_name,$field_e20_name,$id,$id_map);
                        $result = $smt->execute();
                        if($result) {
                            if($mysqli->affected_rows==1 || $needs_update_date) {
                                if ($needs_update_date) {
                                    $now = date('Y-m-d H:i:s');
                                    $mysqli->query("UPDATE sml_markers SET update_date='$now' WHERE id=$id;");
                                }
                                $mysqli->query("DELETE FROM sml_markers_categories_assoc WHERE id_marker=$id;");
                                if(!empty($id_categories)) {
                                    $array_cat = explode(",",$id_categories);
                                    foreach ($array_cat as $id_cat) {
                                        if(in_array($id_cat,$array_exist_categories)) {
                                            $mysqli->query("INSERT INTO sml_markers_categories_assoc(id_marker,id_category) VALUES($id,$id_cat)");
                                        }
                                    }
                                }
                                $count_update++;
                                $count_i++;
                            }
                        } else {
                            echo 'ERROR: '.$mysqli->error;
                            exit;
                        }
                    } else {
                        echo 'ERROR: '.$mysqli->error;
                        exit;
                    }
                }
            }
            echo "Imported <b>".$count_i."</b> (".$count_insert." inserted, ".$count_update." updated) of ".$count. " markers.";
        } else {
            echo 'ERROR: code:'.$_FILES["file"]["error"];
        }
    } else {
        echo 'ERROR: Only csv, xls and xlsx files are supported';
    }
}else{
    echo 'ERROR: file not provided';
}
exit;

function convertUrlsToLinks($text) {
    $pattern = '/\b((http(s)?:\/\/)|(www\.))([a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3})(:[0-9]{1,5})?(\/[^\s<]*)?\b/i';
    $callback = function ($matches) use ($text) {
        $url = $matches[0];
        if (preg_match('/<a[^>]*href=["\'].*' . preg_quote($url, '/') . '.*["\'][^>]*>/i', $text)) {
            return $url;
        }
        if (!preg_match('/^(http(s)?:\/\/)/', $url)) {
            $url = 'http://' . $url;
        }
        return '<a href="' . $url . '" target="_blank">' . $matches[0] . '</a>';
    };
    return preg_replace_callback($pattern, $callback, $text);
}