<?php
/*=======================================================================
 *
 * inc_WEB.inc.php
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/
 


// Insert database parameter per mandator
function insertParametersPerMandator ($x_md_id, $x_hq_id, $hqMnemonic = "", $hqPrefix = "") {
	
   if (!existsEntry("parameter",array("par_key","MAXIMUM_LOGIN_TRIALS","md_id",$x_md_id,"hq_id","0","emp_id","0"))) :
      insertStmt("parameter", array("par_key", "MAXIMUM_LOGIN_TRIALS","md_id",$x_md_id, "hq_id", "0", "emp_id", "0", "par_value", "50"));
   endif;
   
   if (!existsEntry("parameter",array("par_key","AT_EID_EDITABLE","md_id",$x_md_id,"hq_id","0","emp_id","0"))) :
      insertStmt("parameter", array("par_key", "AT_EID_EDITABLE","md_id",$x_md_id, "hq_id", "0", "emp_id", "0", "par_value", "1"));
   endif;
   insertStmt("parameter", array("par_key", "AT_EID_EDITABLE","md_id",$x_md_id, "hq_id", $x_hq_id, "emp_id", "0", "par_value", "1"));
   
   if (!existsEntry("parameter",array("par_key","AT_EID_GENERATION","md_id",$x_md_id,"hq_id","0","emp_id","0"))) :
      insertStmt("parameter", array("par_key", "AT_EID_GENERATION","md_id",$x_md_id, "hq_id", "0", "emp_id", "0", "par_value", $hqPrefix . "890000"));
   endif;
   insertStmt("parameter", array("par_key", "AT_EID_GENERATION","md_id",$x_md_id, "hq_id", $x_hq_id, "emp_id", "0", "par_value", $hqPrefix . $hqMnemonic . "890000"));

   if (!existsEntry("parameter",array("par_key","AT_EID_PREFIX","md_id",$x_md_id,"hq_id","0","emp_id","0"))) :
      insertStmt("parameter", array("par_key", "AT_EID_PREFIX","md_id",$x_md_id, "hq_id", "0", "emp_id", "0", "par_value", "AT"));
   endif;
   insertStmt("parameter", array("par_key", "AT_EID_PREFIX","md_id",$x_md_id, "hq_id", $x_hq_id, "emp_id", "0", "par_value", "AT"));
   
   if (!existsEntry("parameter",array("par_key","CR_EID_EDITABLE","md_id",$x_md_id,"hq_id","0","emp_id","0"))) :
      insertStmt("parameter", array("par_key", "CR_EID_EDITABLE","md_id",$x_md_id, "hq_id", "0", "emp_id", "0", "par_value", "1"));
   endif;
   insertStmt("parameter", array("par_key", "CR_EID_EDITABLE","md_id",$x_md_id, "hq_id", $x_hq_id, "emp_id", "0", "par_value", "1"));

   if (!existsEntry("parameter",array("par_key","CR_EID_GENERATION","md_id",$x_md_id,"hq_id","0","emp_id","0"))) :
      insertStmt("parameter", array("par_key", "CR_EID_GENERATION","md_id",$x_md_id, "hq_id", "0", "emp_id", "0", "par_value", $hqPrefix . "89999"));
   endif;
   insertStmt("parameter", array("par_key", "CR_EID_GENERATION","md_id",$x_md_id, "hq_id", $x_hq_id, "emp_id", "0", "par_value", $hqPrefix . $hqMnemonic . "89999"));

   if (!existsEntry("parameter",array("par_key","CS_EID_EDITABLE","md_id",$x_md_id,"hq_id","0","emp_id","0"))) :
      insertStmt("parameter", array("par_key", "CS_EID_EDITABLE","md_id",$x_md_id, "hq_id", "0", "emp_id", "0", "par_value", "1"));
   endif;
   insertStmt("parameter", array("par_key", "CS_EID_EDITABLE","md_id",$x_md_id, "hq_id", $x_hq_id, "emp_id", "0", "par_value", "1"));

   if (!existsEntry("parameter",array("par_key","CS_EID_GENERATION","md_id",$x_md_id,"hq_id","0","emp_id","0"))) :
      insertStmt("parameter", array("par_key", "CS_EID_GENERATION","md_id",$x_md_id, "hq_id", "0", "emp_id", "0", "par_value", $hqPrefix . "89999"));
   endif;
   insertStmt("parameter", array("par_key", "CS_EID_GENERATION","md_id",$x_md_id, "hq_id", $x_hq_id, "emp_id", "0", "par_value", $hqPrefix . $hqMnemonic . "89999"));

   if (!existsEntry("parameter",array("par_key","MASK_COURIER_EID_LENGTH","md_id",$x_md_id,"hq_id","0","emp_id","0"))) :
      insertStmt("parameter", array("par_key", "MASK_COURIER_EID_LENGTH","md_id",$x_md_id, "hq_id", "0", "emp_id", "0", "par_value", "6"));
   endif;
   insertStmt("parameter", array("par_key", "MASK_COURIER_EID_LENGTH","md_id",$x_md_id, "hq_id", $x_hq_id, "emp_id", "0", "par_value", "6"));

   if (!existsEntry("parameter",array("par_key","MASK_CUSTOMER_EID_LENGTH","md_id",$x_md_id,"hq_id","0","emp_id","0"))) :
      insertStmt("parameter", array("par_key", "MASK_CUSTOMER_EID_LENGTH","md_id",$x_md_id, "hq_id", "0", "emp_id", "0", "par_value", "6"));
   endif;
   insertStmt("parameter", array("par_key", "MASK_CUSTOMER_EID_LENGTH","md_id",$x_md_id, "hq_id", $x_hq_id, "emp_id", "0", "par_value", "6"));

   if (!existsEntry("parameter",array("par_key","SEARCH_CSCSCLIST_CMPCOMP_CSEID","md_id",$x_md_id,"hq_id","0","emp_id","0"))) :
      insertStmt("parameter", array("par_key", "SEARCH_CSCSCLIST_CMPCOMP_CSEID","md_id",$x_md_id, "hq_id", "0", "emp_id", "0", "par_value", "1"));
   endif;
   insertStmt("parameter", array("par_key", "SEARCH_CSCSCLIST_CMPCOMP_CSEID","md_id",$x_md_id, "hq_id", $x_hq_id, "emp_id", "0", "par_value", "1"));

   if (!existsEntry("parameter",array("par_key","MASK_CR_MOBILE_PDA_EDIT","md_id",$x_md_id,"hq_id","0","emp_id","0"))) :
      insertStmt("parameter", array("par_key", "MASK_CR_MOBILE_PDA_EDIT","md_id",$x_md_id, "hq_id", "0", "emp_id", "0", "par_value", "1"));
   endif;

   insertStmt("parameter", array("par_key", "MASK_CR_PARENT_HIDE","md_id",$x_md_id, "hq_id", $x_hq_id, "emp_id", "0", "par_value", "1"));
   insertStmt("parameter", array("par_key", "CR_SID_STATUS","md_id",$x_md_id, "hq_id", $x_hq_id, "emp_id", "0", "par_value", "0"));
   insertStmt("parameter", array("par_key", "MG_STATUS","md_id",$x_md_id, "hq_id", $x_hq_id, "emp_id", "0", "par_value", "0"));
}


// Activate WEB and generate mandator
function webRegister ($cmpComp, $userName, $userFirstname, $userEmail, $userMobile, $userAccount, $userPassword, $cmpComp2 = "", $hqName = "", $hqMnemonic = "", $hqId = "", $hqPrefix = "", $logoFile = "", $logoWidth = "", $logoHeight = "", $insertAdmin = "", $globalUniqueDbInstanceNo = "") {
	global $db, $PHP_SELF;
//	global $dbname, $dblogin, $dbpassword;

  // Check global switch for web registration to be enabled
  $webRegistrationEnabled = getParameterValue("0", "GLOBAL_WEB_REGISTRATION_ENABLED", "0", "0");
  if ($webRegistrationEnabled != "1") : die(); endif;
  
  $retArray = array("001","<err_no>001</err_no>\n","<err_desc>" . getLngt("Registrierung fehlgeschlagen!") . "</err_desc>\n");  // Init only
/*  
  if ($appKey != "" && $appId != "") :
     // Get database instance connection data of metaobject
     $constExtDbInst = getExternalMetaDbInst();
     $db_conn = getDbConnectionSpecial($constExtDbInst, $dbname, $dblogin, $dbpassword);
     $tmpSqlQuery = "SELECT mo_id, usr_id FROM meta_object.applicationuser WHERE app_id = '" . $appId . "' AND appusr_code = '" . $appKey . "' AND appusr_activated = '0'";
     $result = $db_conn->query($tmpSqlQuery);
     while ($row = $result->fetch_assoc()):
           $retArray = array("0", $row["usr_id"], $row["mo_id"]);
           // Set activation flag
           $tmpSqlQuery = "UPDATE meta_object.applicationuser SET appusr_activated = '1' WHERE app_id = '" . $appId . "' AND appusr_code = '" . $appKey . "'";
           $res = $db_conn->query($tmpSqlQuery);
           if (DB::isError($res)) : die ("$PHP_SELF: " . $res->getMessage()); endif;
     endwhile;
     $result->free();
  endif;
*/
  if ($userName != "" && $userFirstname != "" && $userEmail != "" && $userMobile != "") :

     // Get global IMPORT.UPLOAD path of the instance
     $importUploadPath = getParameterValue("0", "MD_IMPORT_UPLOAD_PATH", "0", "0");
     if ($importUploadPath == "") : $importUploadPath = "../import/upload/"; endif;

     // Generate unique directory name
     do {
     	  $x_md_doc_dirname = md5(rand(0, getrandmax()));
        $tmpMdId = getFieldValueFromId("mandator", "md_doc_path", $importUploadPath . $x_md_doc_dirname . "/", "md_id");
     } while ($tmpMdId != "");

     // Generate unique user account
     $userAccount = trim($userAccount);
     do {
     	  $tmpUserAccount = $userAccount;
     	  $tmpUsrId = "0";
     	  if ($tmpUserAccount != "") :
     	     $tmpUsrId = getFieldValueFromId("user", "usr_account", $tmpUserAccount, "usr_id");
     	     if ($tmpUsrId != "") :
     	        $tmpUserAccount .= rand(10000, 99999);
     	     endif;
     	  else :
     	     $tmpUserAccount = rand(0, getrandmax());
     	  endif;
     	  $tmpUsrId = getFieldValueFromId("user", "usr_account", $tmpUserAccount, "usr_id");
     } while ($tmpUsrId != "");
     $userAccount = $tmpUserAccount;

     // Generate user password
     $userPassword = trim($userPassword);
     if ($userPassword == "") : $userPassword = rand(0, getrandmax()); endif;
     
     if ($hqName == "") : $hqName = "101"; endif;
     if ($hqMnemonic == "") : $hqMnemonic = "101"; endif;

     // Insert company
     $sqlquery = "INSERT INTO company (cmp_comp, cmp_comp2, ad_id, cmp_hsno, cmp_iln, cmp_tax_idno, tx_id, br_id, cmp_bank, cmp_bankno, cmp_bankacc, cmp_bankmode, cmp_postage, cmp_inv2hq, cmp_authenticated, cmp_keyuser, cmp_publickey, cmp_defaultkey, cmp_logo, cmp_logo_width, cmp_logo_height, cmp_remark, cmp_match, cmp_match2, cmp_new_date, cmp_modify_status, cmp_export_time, cmp_visible) VALUES " 
                                   . "('" . $cmpComp . "', '- " . $cmpComp2 . " -', 0, '', NULL, NULL, 0, 0, '', '', '0', 0, '0.00', 0, 0, NULL, NULL, NULL, '" . $logoFile . "', '" . $logoWidth . "', '" . $logoHeight . "', '', '', NULL, '0000-00-00 00:00:00', 0, '0000-00-00 00:00:00','1')";
     $result = $db->query($sqlquery);
     if (DB::isError($result)) die ("$PHP_SELF: <br>$sqlquery<br>" . $result->getMessage());
     $cmp_id_last = getLastInsertId();

     // Insert mandator
     $x_md_name = $x_md_doc_dirname;
     insertStmt("mandator", array("md_name", $x_md_name, "md_doc_path", $importUploadPath . $x_md_doc_dirname . "/"));
     $x_md_id = getLastInsertId();

     // Insert headquarter
     $hqVhtDefault = getVhtId("2", "car_01");
     $hqIsInserted = false;
     if ($hqId != "") :
        $tmpCmpIdHq = getFieldValueFromId("headquarters", "hq_id", $hqId, "cmp_id");
        if ($tmpCmpIdHq == "") :
           insertStmt("headquarters", array("hq_id", $hqId, "hq_name", $hqName, "hq_mnemonic", $hqMnemonic, "cmp_id", $cmp_id_last, 
                                 "hq_cr_prov", "0.0", "hq_prov", "0.0", "hq_workmode", "2",
                                 "hq_distancecalc", "0", "hq_vht_default", $hqVhtDefault, "hq_invmode", "1"));
           $x_hq_id = $hqId;
           $hqIsInserted = true;
        endif;
     endif;
     if (!$hqIsInserted) :
        insertStmt("headquarters", array("hq_name", $hqName, "hq_mnemonic", $hqMnemonic, "cmp_id", $cmp_id_last, 
                                         "hq_cr_prov", "0.0", "hq_prov", "0.0", "hq_workmode", "2",
                                         "hq_distancecalc", "0", "hq_vht_default", $hqVhtDefault, "hq_invmode", "1"));
        $x_hq_id = getLastInsertId();
     endif;

     // Insert mandatorheadquarters
     insertStmt("mandatorheadquarters", array("md_id", $x_md_id, "hq_id", $x_hq_id));


     // Insert user "admin" for every mandator
     if ($insertAdmin == "1") :

        $adminPassword = "7348910256x%";
        insertStmt("user", array("hq_id", $x_hq_id, "usr_type", "1", "usr_name", "Administrator " . $hqMnemonic, "usr_account", "admin_" . $x_md_id, "usr_password", $adminPassword));
        $usr_id_last = getLastInsertId();

        // Set crypted Password
        $sqlStmtPwd = "UPDATE user SET usr_password = PASSWORD('" . $adminPassword . "') WHERE usr_id = '" . $usr_id_last . "'";
        $res = $db->query($sqlStmtPwd);
        if (DB::isError($res)) : die ("$PHP_SELF: " . $res->getMessage()); endif;

        insertStmt("employee", array("usr_id", $usr_id_last, "csc_id", "0", "emp_rights", "11111111111111111111111111111111111111111111111111", "emp_headquarters", $x_hq_id));
        $emp_id_last = getLastInsertId();

        updateStmt("headquarters", "hq_id", $x_hq_id, array("hq_admin", $emp_id_last));

        // Set ALL rights in "employeerights"
        $rgtArray = getColVectorFromDB2Array("rights","rgt_type","emp","rgt_id","rgt_id");
        $rgtArrayLen = count($rgtArray);
        for ($i = 0; $i < $rgtArrayLen; $i++) :
            insertStmt("employeerights", array("emp_id", $emp_id_last, "hq_id", $x_hq_id, "rgt_id", $rgtArray[$i]));
        endfor;
     endif;

     // Insert new user requested
     insertStmt("user", array("hq_id", $x_hq_id, "usr_type", "1", "usr_name", $userName, "usr_firstname", $userFirstname, "usr_email", $userEmail, "usr_phone", $userMobile, "usr_account", $userAccount, "usr_password", $userPassword));
     $usr_id_last = getLastInsertId();

     // Set crypted Password
     $sqlStmtPwd = "UPDATE user SET usr_password = PASSWORD('" . $userPassword . "') WHERE usr_id = '" . $usr_id_last . "'";
     $res = $db->query($sqlStmtPwd);
     if (DB::isError($res)) : die ("$PHP_SELF: " . $res->getMessage()); endif;

     insertStmt("employee", array("usr_id", $usr_id_last, "csc_id", "0", "emp_rights", "11111111111111101111111111111111111111111111111111", "emp_headquarters", $x_hq_id));
     $emp_id_last = getLastInsertId();

     if ($insertAdmin != "1") :
        updateStmt("headquarters", "hq_id", $x_hq_id, array("hq_admin", $emp_id_last));
     endif;

     // Set ALL rights in "employeerights"
     $rgtArray = getColVectorFromDB2Array("rights","rgt_type","emp","rgt_id","rgt_id");
     $rgtArrayLen = count($rgtArray);
     for ($i = 0; $i < $rgtArrayLen; $i++) :
         insertStmt("employeerights", array("emp_id", $emp_id_last, "hq_id", $x_hq_id, "rgt_id", $rgtArray[$i]));
     endfor;
     
     // Insert database parameter
     insertParametersPerMandator($x_md_id, $x_hq_id, $hqMnemonic, $hqPrefix);
     
     $retArray = array("0", $usr_id_last, $x_hq_id);
     
     // Insert user into "metaobjects"
     // ........
  endif;
   
  return $retArray;
}
?>