Newer
Older
ubFramework / Portal / docroot / admin / accounts / add_user.php
@Christopher W. Olsen Christopher W. Olsen on 10 Dec 2017 9 KB Cleaning Up Making It A Sub Module
<?php

/******************************************************************************************
 Copyright (c) 2013, 2014 Christopher W. Olsen <cwolsen@ubixtechnologies.com>
 All rights reserved.

 Redistribution and use in source and binary forms, with or without modification, are
 permitted provided that the following conditions are met:

 Redistributions of source code must retain the above copyright notice, this list of
 conditions, the following disclaimer and the list of authors.  Redistributions in binary
 form must reproduce the above copyright notice, this list of conditions, the following
 disclaimer and the list of authors in the documentation and/or other materials provided
 with the distribution. Neither the name of the uBix Cube Project nor the names of its
 contributors may be used to endorse or promote products derived from this software
 without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 $Id: add_user.php 931 2017-09-07 19:18:33Z reddawg $

 *****************************************************************************************/

  /* Main include info */
  ini_set('include_path',$_SERVER['DOCUMENT_ROOT'] . "/include/");
  include("config.php");

  /* Assign the page title */
  $data['tpl']->assign("PAGE_TITLE","Admin: Accounts");

  /* Main defines */
  $data['tpl']->define(array("index" => "admin/index/index.html"));
  $data['tpl']->define(array("menu"  => "admin/accounts/menu.html"));
  $data['tpl']->define(array("body"  => "admin/accounts/add_user.html"));

  $data['session']->Validate('A_G', 1);

  $form_data = $data['data'];

  $message = "";

  if (isset($form_data['up'])) {
  	
    $mapFields = array("account_number" => 9, "first_name" => 1, "last_name" => 1, "email" => 7);

    /* Verify Fields Data */
    foreach ($mapFields as $key => $val)
      if (strlen($form_data[$key]) < $val)
  	$message .= "<h5>$key Needs To Be Atleast $val Characters</h5>";

    if ($form_data['status'] == "")
      $message .= "Invalid Account Status<br />\n";

    if ($form_data['account_type'] == "") 
      $message .= "Invalid Account Type<br />\n";
    
    if ($message == "") {
      $master_acl = array_merge($data['session']->userACL, $data['session']->adminACL);

      $s_acl = "";

      foreach ($master_acl as $key => $val) {
        if ($form_data[$key] == 1) {
          $m_acl[$key][0] = 1;
          $s_acl .= $key . ",";
        }
        else
          $m_acl[$key][0] = 0;
      }
    
      $password = $data['ubf']->generatePassword(8);

      $query = "INSERT INTO users (email, password, first_name, last_name, account_number, session_acl, master_acl, status, account_type, created, last_updated) VALUES('" . $form_data['email'] . "', '" . hash("SHA256", $password) . "', '" . $form_data['first_name'] . "', '" . $form_data['last_name'] . "', '" . $form_data['account_number'] . "', '" . $s_acl . "', '" . json_encode($m_acl) . "', 1, 5, " . time() . ", " . time() . ")";

      if ($data['ubix_db']->query($query) === true) {
        $message .= "Account Added.<br />";
      $e_message = "Welcome aboard " . $data['data']['first_name'] . ",<br>\n<br>\n";
      $e_message .= "Thank you for choosing PSC Tech as your reliable IT partner for " . $data['data']['company'] . ". As we continue to activate your services you will receive updates letting you know that they are available along with basic instructions on how to use them.<br>\n<br>\n";
      $e_message .= "Please keep the following information for your records as it will help expedite all support requests.<br>\n<br>\n";
      $e_message .= "Account Number: " . $data['data']['account_number'] . "<br>\n";
      $e_message .= "Account Manager: Douglas Goldstein<br>\n";
      $e_message .= "Account Manager E-Mail: dgoldstein@PSCTech.us<br>\n";
      $e_message .= "Account Manager Direct Line: 516-423-0666<br>\n";
      $e_message .= "Service Line: 844-842-UBIX (8249)<br>\n<br>\n";
      $e_message .= "Portal Information:<br>\n<br>\n";
      $e_message .= "Portal Address: <a href=\"https://manage.ubixtechnologies.net/\">https://manage.ubixtechnologies.net</a><br>\n";
      $e_message .= "Portal Username: " . $data['data']['email'] . "<br>\n";
      $e_message .= "Portal Temporary Password: " . $password . "<br>\n<br>\n";
      $e_message .= "If you have any questions concerning your new services please contact your account manager. If you need to request support please call our service line or e-mail support@ubixtechnologies.com.<br>\n<br>\n";
      
      $recipient = $data['data']['first_name'] . " " . $data['data']['last_name'] . " <" . $data['data']['email'] . ">";
      
      foreach ($mapFields as $key => $val)
      	$data['data'][$key] = "";
      
      $data['ubf']->sendMail("Welcome to PSC Tech", $e_message, $recipient, "sales@ubixtechnologies.net", "newaccount@ubixtechnologies.net");
    }
    else
      $message .= "Error Adding Account.<br />";
    }
  }

  if (!isset($form_data['status']) || $form_data['status'] == "")
    $status = "<option value=\"\">Please Select</option><option value=\"2\">Active</option><option value=\"0\">Inactive</option><option value=\"1\">Pending</option>";
  else if ($form_data['status'] == 0)
    $status = "<option value=\"2\">Active</option><option value=\"0\" selected>Inactive</option><option value=\"1\">Pending</option>";
  else if ($form_data['status'] == 1)
    $status = "<option value=\"2\">Active</option><option value=\"0\">Inactive</option><option value=\"1\" selected>Pending</option>";
  else if ($form_data['status'] == 2)
    $status = "<option value=\"2\" selected>Active</option><option value=\"0\">Inactive</option><option value=\"1\">Pending</option>";

  if (!isset($form_data['account_type']) || $form_data['account_type'] == "")
    $aType = "<option value=\"\">Please Select</option><option value=\"0\">Primary</option><option value=\"1\">Sub</option>";
  else if ($form_data['account_type'] == 0)
    $aType = "<option value=\"0\" selected>Primary</option><option value=\"1\">Sub</option>";
  else
    $aType = "<option value=\"0\">Primary</option><option value=\"1\" selected>Sub</option>";

  $query = "SELECT account_number, company FROM accounts ORDER BY company";
  $aRes   = $data['ubix_db']->query($query);

  $accounts = "<option>Please Select</option>\n";

  while ($aData = $aRes->fetch_assoc()) {
    if ($form_data['account_number'] == $aData['account_number'])
      $accounts .= "<option value=\"" . $aData['account_number'] . "\" selected>" . $aData['account_number'] . " - " . $aData['company'] . "</option>\n";
    else
      $accounts .= "<option value=\"" . $aData['account_number'] . "\">" . $aData['account_number'] . " - " . $aData['company'] . "</option>\n";
  }
  $aRes->free();

  $user_acl = "";

  foreach ($data['session']->userACL as $key => $val) {
    if ($form_data[$key] == 1)
      $user_acl .= "<tr><td>" . $val[0] . "</td><td><input type=\"radio\" name=\"data[$key]\" value=\"1\" checked> Enabled <input type=\"radio\" name=\"data[$key]\" value=\"0\"> Disabled</td></tr>\n";
    else
      $user_acl .= "<tr><td>" . $val[0] . "</td><td><input type=\"radio\" name=\"data[$key]\" value=\"1\"> Enabled <input type=\"radio\" name=\"data[$key]\" value=\"0\" checked> Disabled</td></tr>\n";
  }

  $admin_acl = "";

  foreach ($data['session']->adminACL as $key => $val) {
    if ($data['session']->cACL[$key][0] == 0)
      $aD = "disabled";
    else
      $aD = "";

    if ($form_data[$key] == 1) {
      if ($aD != "")
        $admin_acl .= "<tr><td>" . $val[0] . "</td><td><input type=\"radio\" name=\"data[$key]\" value=\"1\" checked> Enabled <input type=\"radio\" name=\"data[$key]\" value=\"0\" $aD> Disabled</td></tr>\n";
      else
        $admin_acl .= "<tr><td>" . $val[0] . "</td><td><input type=\"radio\" name=\"data[$key]\" value=\"1\" checked> Enabled <input type=\"radio\" name=\"data[$key]\" value=\"0\"> Disabled</td></tr>\n";
    }
    else
      if ($aD != "")
        $admin_acl .= "<tr><td>" . $val[0] . "</td><td><input type=\"radio\" name=\"data[$key]\" value=\"1\" $aD> Enabled <input type=\"radio\" name=\"data[$key]\" value=\"0\" checked> Disabled</td></tr>\n";
      else
        $admin_acl .= "<tr><td>" . $val[0] . "</td><td><input type=\"radio\" name=\"data[$key]\" value=\"1\"> Enabled <input type=\"radio\" name=\"data[$key]\" value=\"0\" checked> Disabled</td></tr>\n";
  }

  $data['tpl']->assign("CREATED", date("m/d/Y H:i:s", time()));
  $data['tpl']->assign("STATUS", $status);
  $data['tpl']->assign("ACCOUNT_TYPE", $aType);
  $data['tpl']->assign("ACCOUNTS", $accounts);
  $data['tpl']->assign("EMAIL", $form_data['email']);
  $data['tpl']->assign("FIRST_NAME", $form_data['first_name']);
  $data['tpl']->assign("LAST_NAME", $form_data['last_name']);
  $data['tpl']->assign("USER_ACL", $user_acl);
  $data['tpl']->assign("ADMIN_ACL", $admin_acl);

  $data['tpl']->assign("MESSAGE", $message);


  /* Parse and print */
  $data['tpl']->parse("MENU", array("menu"));
  $data['tpl']->parse("BODY", array("body"));
  $data['tpl']->parse("INDEX", array("index"));
  $data['tpl']->FastPrint("INDEX");
  
?>