Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.90 KB | None | 0 0
  1. <?php
  2.  
  3. require_once(__DIR__ . "/../Warehouse.php");
  4.  
  5. call_user_func(function() {
  6.     if(!Authenticate::isLoggedIn()) {
  7.         throw new Exception('Permission denied');
  8.     }
  9.  
  10.     $methods = array(
  11.         'getOptions' => function($request) {
  12.             $type = $request['type'];
  13.             $sql  = array();
  14.  
  15.             if(!is_array($type)) {
  16.                 $type = [$type];
  17.             }
  18.  
  19.             $linked_usernames = Authenticate::getLinkedUsernames(null, true);
  20.             $usernames        = implode("','", $linked_usernames);
  21.             $user_where       = ["1=1"];
  22.             $student_where    = ["1=1"];
  23.  
  24.             if(User('PROFILE') !== 'admin') {
  25.                 // Disallow all except linked users
  26.                 $user_where[]    = "1=2";
  27.                 $student_where[] = "1=2";
  28.             }
  29.             else {
  30.                 $user_class    = $_SESSION['USER_CLASS'];
  31.                 $user_class_id = $_SESSION['USER_CLASS_ID'];
  32.                 $user          = new $user_class($user_class_id);
  33.  
  34.                 if(!$user->isSuperUser(Package::is('SIS'))) {
  35.                     if(!Permissions::hasPermission('SIS:UserLoginAs')) {
  36.                         $user_where[]    = "1=2";
  37.                         $student_where[] = "1=2";
  38.                     }
  39.                     else {
  40.                         $school          = UserSchool();
  41.                         $user_where[]    = "concat(',',ue.schools,',') LIKE concat('%',{$school},'%'";
  42.                         $student_where[] = "enrollment.school = {$school}";
  43.                     }
  44.                 }
  45.             }
  46.  
  47.             $user_where    = "(" . implode(" AND ", $user_where) . ")";
  48.             $student_where = "(" . implode(" AND ", $student_where) . ")";
  49.  
  50.             //options -rayb
  51.             $options = [];
  52.  
  53.             // Add users
  54.             if(in_array('user', $type)) {
  55.               $tmp = "
  56.                  SELECT
  57.                      u.username as value,
  58.                      CONCAT(u.last_name, ', ', u.first_name, ' ', u.middle_name, ' - ', u.staff_id) as text,
  59.                                            ue.profiles
  60.                  FROM
  61.                      users u
  62.                  INNER JOIN user_enrollment ue
  63.                                        ON ue.staff_id = u.staff_id
  64.                  WHERE
  65.                      '{$today}' BETWEEN COALESCE(ue.start_date, '1800-01-01') AND COALESCE(ue.end_date, '9999-01-01') AND
  66.                      u.username IS NOT NULL AND
  67.                      u.first_name IS NOT NULL AND
  68.                      u.last_name IS NOT NULL AND
  69.                      (u.username IN ('{$usernames}') OR {$user_where})
  70.  
  71.              ";
  72.  
  73.  
  74.               $target = Database::get($tmp);
  75.  
  76.                             if($target) {
  77.                                 $staffInfo = [];
  78.                                 foreach($target as $record) {
  79.                                     $profile_ids = explode(',',$record['PROFILES']);
  80.                                     foreach ($profile_ids as $index => $id) {
  81.                                         if(empty($id)) {
  82.                                             unset($profile_ids[$index]);
  83.                                         }
  84.                                     }
  85.  
  86.                                     $staffInfo[] = [
  87.                                         'value' => $record['VALUE'],
  88.                                         'text' => $record['TEXT'],
  89.                                         'profile_ids' => $profile_ids
  90.                                     ];
  91.                                 }
  92.                             }
  93.  
  94.                             $tmp = Database::get(
  95.                                 "SELECT id,title FROM user_profiles
  96.                            ");
  97.  
  98.                             $profiles = [];
  99.                             foreach($tmp as $profile) {
  100.                                 $profiles[$profile['ID']] = $profile['TITLE'];
  101.                             }
  102.  
  103.                             if($staffInfo) {
  104.                                 foreach($staffInfo as $staffMember) {
  105.                                     $profileTitles = [];
  106.                                     foreach($staffMember['profile_ids'] as $profile_id) {
  107.                                         if($profiles[$profile_id]) {
  108.                                             $profileTitles[] = $profiles[$profile_id];
  109.                                         }
  110.                                     }
  111.                                     $profileTitles = implode(', ', $profileTitles);
  112.                                     if($profileTitles) {
  113.                                         $staffMember['text'] .= " ({$profileTitles})";
  114.                                         $options[$staffMember['value']] = $staffMember['text'];
  115.                                     } else {
  116.                                         $staffMember['text'] .= " (Unknown)";
  117.                                         $options[$staffMember['value']] = $staffMember['text'];
  118.                                     }
  119.                                 }
  120.                             }
  121.             }
  122.  
  123.               // Add students
  124.               if(in_array('student', $type)) {
  125.                   $syear = UserSYear();
  126.                   $date  = date('Y-m-d');
  127.  
  128.                   $studentSql = "
  129.                      SELECT
  130.                          username as value,
  131.                          CONCAT(last_name, ', ', first_name, ' ', middle_name, ' - ', student_id, ' (student)') as text
  132.                      FROM
  133.                          students
  134.                      JOIN
  135.                          student_enrollment enrollment
  136.                      ON
  137.                          enrollment.student_id = students.student_id
  138.                      WHERE
  139.                          username IS NOT NULL AND
  140.                          first_name IS NOT NULL AND
  141.                          last_name IS NOT NULL AND
  142.                          enrollment.syear = '{$syear}' AND
  143.                          '{$date}' BETWEEN COALESCE(enrollment.start_date, '{$date}') AND COALESCE(enrollment.end_date, '{$date}') AND
  144.                          (username IN ('{$usernames}') OR {$student_where})
  145.                  ";
  146.  
  147.                             $tmp = Database::get($studentSql);
  148.                             foreach($tmp as $record) {
  149.                                 $options[$record['VALUE']] = $record['TEXT'];
  150.                             }
  151.               }
  152.  
  153.               return array(
  154.                   'options' => $options,
  155.                   'current' => Authenticate::getCurrentUsername(),
  156.               );
  157.           },
  158.  
  159.         'login' => function($request) {
  160.             session_start();
  161.  
  162.             $username    = strtolower($request['username']);
  163.             $error       = null;
  164.             $tmp_session = $_SESSION;
  165.             $type        = Authenticate::getLoginType();
  166.  
  167.             try {
  168.                 if(User('PROFILE') !== 'admin') {
  169.                     $linked_usernames = array_map('strtolower', Authenticate::getLinkedUsernames(null, true));
  170.  
  171.                     if(!in_array($username, $linked_usernames)) {
  172.                         throw new Exception('Permission denied');
  173.                     }
  174.                 }
  175.  
  176.                 Authenticate::login($type, $username, true);
  177.             }
  178.             catch(Exception $e) {
  179.                 $error    = $e->getMessage();
  180.                 $_SESSION = $tmp_session;
  181.             }
  182.  
  183.             return [
  184.                 'error'  => $error,
  185.                 'result' => empty($error)
  186.             ];
  187.         },
  188.     );
  189.  
  190.     $data   = $_POST['data'];
  191.     $action = $data['action'];
  192.  
  193.     if(empty($methods[$action])) {
  194.         throw new Exception("Invalid action: {$action}");
  195.     }
  196.  
  197.     $response = call_user_func_array($methods[$action], [$data]);
  198.  
  199.     echo json_encode($response);
  200.  
  201.     exit;
  202. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement