<?php
session_name('CAKEPHP');
session_start();
error_reporting(0);

if (empty($_SESSION['ACGCompanyId'])) { echo 'checkVal'; exit; }

$linkName = isset($_POST['linkName']) ? trim($_POST['linkName']) : '';
if (empty($linkName)) { echo 'checkVal'; exit; }

$con = mysqli_connect("127.0.0.1", "root", "", "herp_amch");
if (!$con) { echo 'checkVal'; exit; }

$safe  = mysqli_real_escape_string($con, $linkName);
$hroot = 'http://' . $_SERVER['HTTP_HOST'] . '/git/PHP_564/mimhans-2.0/';
$herp  = 'http://' . $_SERVER['HTTP_HOST'] . '/git/PHP_564/mimhans-3.0/herp/';

// Check HMIS forms first
$sql = mysqli_query($con, "SELECT url FROM forms WHERE lebel = '{$safe}' LIMIT 1");
if ($sql && mysqli_num_rows($sql) > 0) {
    $row = mysqli_fetch_assoc($sql);
    echo $hroot . $row['url'];
    exit;
}

// Check HERP ERP modules_form
$sql2 = mysqli_query($con,
    "SELECT frm_url, id AS frmid, moduleid AS modid
     FROM modules_form
     WHERE frm_name = '{$safe}' AND status = 'Y'
     LIMIT 1"
);
if ($sql2 && mysqli_num_rows($sql2) > 0) {
    $row = mysqli_fetch_assoc($sql2);
    echo $herp . 'index.php?route=' . ltrim($row['frm_url'], '/')
       . '&modid=' . intval($row['modid'])
       . '&frmid=' . intval($row['frmid']);
    exit;
}

echo 'checkVal';
