Advertisement
Guest User

adduser.php

a guest
Oct 31st, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.47 KB | None | 0 0
  1. <?php
  2. /*
  3. UserSpice 4
  4. An Open Source PHP User Management System
  5. by the UserSpice Team at http://UserSpice.com
  6.  
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. ?>
  21. <?php require_once '../users/init.php'; ?>
  22. <?php require_once $abs_us_root.$us_url_root.'users/includes/header.php'; ?>
  23. <?php require_once $abs_us_root.$us_url_root.'users/includes/navigation.php'; ?>
  24.  
  25. <?php if (!securePage($_SERVER['PHP_SELF'])){die();} ?>
  26. <?php
  27. //PHP Goes Here!
  28. $errors = $successes = [];
  29. $form_valid=TRUE;
  30. $permOpsQ = $db->query("SELECT * FROM permissions");
  31. $permOps = $permOpsQ->results();
  32. // dnd($permOps);
  33.  
  34. //Forms posted
  35. if (!empty($_POST)) {
  36.   //Manually Add User
  37.   if(!empty($_POST['addUser'])) {
  38.     $join_date = date("Y-m-d H:i:s");
  39.     $username = Input::get('username');
  40.     $fname = Input::get('fname');
  41.     $lname = Input::get('lname');
  42.     $email = Input::get('email');
  43.     $token = $_POST['csrf'];
  44.  
  45.     if(!Token::check($token)){
  46.       die('Token doesn\'t match!');
  47.     }
  48.  
  49.     $form_valid=FALSE; // assume the worst
  50.     $validation = new Validate();
  51.     $validation->check($_POST,array(
  52.       'username' => array(
  53.       'display' => 'Username',
  54.       'required' => true,
  55.       'min' => 2,
  56.       'max' => 35,
  57.       'unique' => 'users',
  58.       ),
  59.       'fname' => array(
  60.       'display' => 'First Name',
  61.       'required' => true,
  62.       'min' => 2,
  63.       'max' => 35,
  64.       ),
  65.       'lname' => array(
  66.       'display' => 'Last Name',
  67.       'required' => true,
  68.       'min' => 2,
  69.       'max' => 35,
  70.       ),
  71.       'email' => array(
  72.       'display' => 'Email',
  73.       'required' => true,
  74.       'valid_email' => true,
  75.       'unique' => 'users',
  76.       ),
  77.       'password' => array(
  78.       'display' => 'Password',
  79.       'required' => true,
  80.       'min' => 6,
  81.       'max' => 25,
  82.       ),
  83.       'confirm' => array(
  84.       'display' => 'Confirm Password',
  85.       'required' => true,
  86.       'matches' => 'password',
  87.       ),
  88.     ));
  89.     if($validation->passed()) {
  90.         $form_valid=TRUE;
  91.       try {
  92.         // echo "Trying to create user";
  93.         $fields=array(
  94.           'username' => Input::get('username'),
  95.           'fname' => Input::get('fname'),
  96.           'lname' => Input::get('lname'),
  97.           'email' => Input::get('email'),
  98.           'password' =>
  99.           password_hash(Input::get('password'), PASSWORD_BCRYPT, array('cost' => 12)),
  100.           'permissions' => 1,
  101.           'account_owner' => 1,
  102.           'stripe_cust_id' => '',
  103.           'join_date' => $join_date,
  104.           'company' => Input::get('company'),
  105.           'email_verified' => 1,
  106.           'active' => 1,
  107.           'vericode' => 111111,
  108.         );
  109.         $db->insert('users',$fields);
  110.         $theNewId=$db->lastId();
  111.         // bold($theNewId);
  112.         $perm = Input::get('perm');
  113.         $addNewPermission = array('user_id' => $theNewId, 'permission_id' => $perm);
  114.         $db->insert('user_permission_matches',$addNewPermission);
  115.         $db->insert('profiles',['user_id'=>$theNewId, 'bio'=>'This is your bio']);
  116.  
  117.         if($perm != 1){
  118.           $addNewPermission2 = array('user_id' => $theNewId, 'permission_id' => 1);
  119.           $db->insert('user_permission_matches',$addNewPermission2);
  120.         }
  121.  
  122.         $successes[] = lang("ACCOUNT_USER_ADDED");
  123.  
  124.       } catch (Exception $e) {
  125.         die($e->getMessage());
  126.       }
  127.  
  128.     }
  129.   }
  130. }
  131.  
  132. $highestPermQ = $db->query("SELECT * FROM user_permission_matches WHERE user_id = ? ORDER BY permission_id DESC",array($user->data()->id));
  133. $highestPerm = $highestPermQ->first();
  134. $highest = $highestPerm->permission_id;
  135. $availableQ = $db->query("SELECT * FROM permissions WHERE id !=2 AND id < ?",array($highest));
  136. $available = $availableQ->results();
  137.  
  138. ?>
  139. <div id="page-wrapper">
  140.  
  141.   <div class="container">
  142.  
  143.     <!-- Page Heading -->
  144.     <div class="row">
  145.  
  146.         <div class="col-xs-12 col-md-6">
  147.         <h1>User Management - Add User</h1>
  148.         <a href="<?=$us_url_root?>users/admin_users.php"><h4>User Management</h4></a>
  149.       </div>
  150.  
  151.         </div>
  152.  
  153.  
  154.                  <div class="row">
  155.              <div class="col-md-12">
  156.           <?php echo resultBlock($errors,$successes);
  157.                 ?>
  158.  
  159.                              <hr />
  160.                <div class="row">
  161.                <div class="col-xs-12">
  162.                <?php
  163.                if (!$form_valid && Input::exists()){
  164.                 echo display_errors($validation->errors());
  165.                }
  166.                ?>
  167.  
  168.                <form class="form-signup" action="adduser.php" method="POST" id="payment-form">
  169.  
  170.                 <div class="well well-sm">
  171.                 <h4 class="form-signin-heading"> Access Level:
  172.                 <select name="perm">
  173.                   <?php foreach($available as $a){ ?>
  174.                     <option value="<?=$a->id?>"><?=$a->name?></option>
  175.                   <?php } ?>
  176.                   </select>
  177.                   </h4>
  178.  
  179.                 <div class="form-group">
  180.                   <div class="col-xs-2">
  181.                     <input  class="form-control" type="text" name="username" id="username" placeholder="Username" value="<?php if (!$form_valid && !empty($_POST)){ echo $username;} ?>" required autofocus>
  182. </div>
  183.                   <div class="col-xs-2">
  184.                     <input type="text" class="form-control" id="fname" name="fname" placeholder="First Name" value="<?php if (!$form_valid && !empty($_POST)){ echo $fname;} ?>" required>
  185. </div>
  186.                   <div class="col-xs-2">
  187.                     <input type="text" class="form-control" id="lname" name="lname" placeholder="Last Name" value="<?php if (!$form_valid && !empty($_POST)){ echo $lname;} ?>" required>
  188. </div>
  189.                   <div class="col-xs-2">
  190.                     <input  class="form-control" type="text" name="email" id="email" placeholder="Email Address" value="<?php if (!$form_valid && !empty($_POST)){ echo $email;} ?>" required >
  191. </div>
  192.                   <div class="col-xs-2">
  193.                     <input  class="form-control" type="password" name="password" id="password" placeholder="Password" required aria-describedby="passwordhelp">
  194. </div>
  195.                   <div class="col-xs-2">
  196.                     <input  type="password" id="confirm" name="confirm" class="form-control" placeholder="Confirm Password" required >
  197. </div>
  198.                 </div>
  199.  
  200.                 <br /><br />
  201.                 <input type="hidden" value="<?=Token::generate();?>" name="csrf">
  202.                 <input class='btn btn-primary' type='submit' name='addUser' value='Manually Add User' />
  203.               </div>
  204.                </form>
  205.                </div>
  206.                </div>
  207.  
  208.  
  209.   </div>
  210. </div>
  211.  
  212.  
  213.     <!-- End of main content section -->
  214.  
  215. <?php require_once $abs_us_root.$us_url_root.'users/includes/page_footer.php'; // the final html footer copyright row + the external js calls ?>
  216.  
  217.     <!-- Place any per-page javascript here -->
  218. <script src="js/search.js" charset="utf-8"></script>
  219.  
  220. <?php require_once $abs_us_root.$us_url_root.'users/includes/html_footer.php'; // currently just the closing /body and /html ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement