includes/sessions.php
changeset 1101 30d8bb88572d
parent 1089 16a1e8626dd9
child 1132 05fe0039d952
equal deleted inserted replaced
1100:aead4e1ce5df 1101:30d8bb88572d
   424       $db->_die('The error seems to have occurred somewhere in the session management code.');
   424       $db->_die('The error seems to have occurred somewhere in the session management code.');
   425     }
   425     }
   426     return $result;
   426     return $result;
   427   }
   427   }
   428   
   428   
       
   429   /**
       
   430    * Returns true if we're currently on a page that shouldn't be blocked even if we have an inactive or banned account
       
   431    * @param bool strict - if true, whitelist of pages is even stricter (Login, Logout and CSS only). if false (default), admin access is allowed, assuming other factors allow it
       
   432    * @return bool
       
   433    */
       
   434   
       
   435   function on_critical_page($strict = false)
       
   436   {
       
   437     global $title;
       
   438     list($page_id, $namespace) = RenderMan::strToPageID($title);
       
   439     list($page_id) = explode('/', $page_id);
       
   440     
       
   441     if ( $strict )
       
   442     {
       
   443       return $namespace == 'Special' && in_array($page_id, array('CSS', 'Login', 'Logout'));
       
   444     }
       
   445     else
       
   446     {
       
   447       return $namespace == 'Admin' || ($namespace == 'Special' && in_array($page_id, array('CSS', 'Login', 'Logout', 'Administration')));
       
   448     }
       
   449   }
       
   450   
   429   # Session restoration and permissions
   451   # Session restoration and permissions
   430   
   452   
   431   /**
   453   /**
   432    * Initializes the basic state of things, including most user prefs, login data, cookie stuff
   454    * Initializes the basic state of things, including most user prefs, login data, cookie stuff
   433    */
   455    */
   450       {
   472       {
   451         $userdata = $this->validate_session($_COOKIE['sid']);
   473         $userdata = $this->validate_session($_COOKIE['sid']);
   452       }
   474       }
   453       if ( is_array($userdata) )
   475       if ( is_array($userdata) )
   454       {
   476       {
   455         $data = RenderMan::strToPageID($paths->get_pageid_from_url());
       
   456         
       
   457         if(!$this->compat && $userdata['account_active'] != 1 && $data[1] != 'Special' && $data[1] != 'Admin')
       
   458         {
       
   459           $this->show_inactive_error($userdata);
       
   460         }
       
   461         
       
   462         $this->sid = $_COOKIE['sid'];
   477         $this->sid = $_COOKIE['sid'];
   463         $this->user_logged_in = true;
   478         $this->user_logged_in = true;
   464         $this->user_id =       intval($userdata['user_id']);
   479         $this->user_id =       intval($userdata['user_id']);
   465         $this->username =      $userdata['username'];
   480         $this->username =      $userdata['username'];
   466         $this->user_level =    intval($userdata['user_level']);
   481         $this->user_level =    intval($userdata['user_level']);
   564     }
   579     }
   565     
   580     
   566     // make sure we aren't banned
   581     // make sure we aren't banned
   567     $this->check_banlist();
   582     $this->check_banlist();
   568     
   583     
       
   584     // make sure the account is active
       
   585     if ( !$this->compat && $this->user_logged_in && $userdata['account_active'] != 1 && !$this->on_critical_page() )
       
   586     {
       
   587       $this->show_inactive_error($userdata);
       
   588     }
       
   589     
   569     // Printable page view? Probably the wrong place to control
   590     // Printable page view? Probably the wrong place to control
   570     // it but $template is pretty dumb, it will just about always
   591     // it but $template is pretty dumb, it will just about always
   571     // do what you ask it to do, which isn't always what we want
   592     // do what you ask it to do, which isn't always what we want
   572     if ( isset ( $_GET['printable'] ) )
   593     if ( isset ( $_GET['printable'] ) )
   573     {
   594     {
  1496   function show_inactive_error($userdata)
  1517   function show_inactive_error($userdata)
  1497   {
  1518   {
  1498     global $db, $session, $paths, $template, $plugins; // Common objects
  1519     global $db, $session, $paths, $template, $plugins; // Common objects
  1499     global $lang;
  1520     global $lang;
  1500     
  1521     
       
  1522     global $title;
       
  1523     $paths->init($title);
       
  1524     
  1501     $language = intval(getConfig('default_language'));
  1525     $language = intval(getConfig('default_language'));
  1502     $lang = new Language($language);
  1526     $lang = new Language($language);
  1503     @setlocale(LC_ALL, $lang->lang_code);
  1527     @setlocale(LC_ALL, $lang->lang_code);
  1504     
  1528     
  1505     $a = getConfig('account_activation');
  1529     $a = getConfig('account_activation');
  1561                    <p><input type="submit" name="logout" value="' . $lang->get('user_login_noact_btn_log_out') . '" /></p>
  1585                    <p><input type="submit" name="logout" value="' . $lang->get('user_login_noact_btn_log_out') . '" /></p>
  1562                  </form>';
  1586                  </form>';
  1563       }
  1587       }
  1564     }
  1588     }
  1565     
  1589     
  1566     die_semicritical($lang->get('user_login_noact_title'), '<p>' . $lang->get('user_login_noact_msg_intro') . ' '.$solution.'</p>' . $form);
  1590     global $output;
       
  1591     $output = new Output_HTML();
       
  1592     $output->set_title($lang->get('user_login_noact_title'));
       
  1593     die_friendly($lang->get('user_login_noact_title'), '<p>' . $lang->get('user_login_noact_msg_intro') . ' '.$solution.'</p>' . $form);
  1567   }
  1594   }
  1568   
  1595   
  1569   /**
  1596   /**
  1570    * Appends the high-privilege session key to the URL if we are authorized to do high-privilege stuff
  1597    * Appends the high-privilege session key to the URL if we are authorized to do high-privilege stuff
  1571    * @param string $url The URL to add session data to
  1598    * @param string $url The URL to add session data to
  1777   function check_banlist()
  1804   function check_banlist()
  1778   {
  1805   {
  1779     global $db, $session, $paths, $template, $plugins; // Common objects
  1806     global $db, $session, $paths, $template, $plugins; // Common objects
  1780     global $lang;
  1807     global $lang;
  1781     
  1808     
  1782     $col_reason = ( $this->compat ) ? '"No reason entered (session manager is in compatibility mode)" AS reason' : 'reason';
  1809     $col_reason = ( $this->compat ) ? '\'No reason available (session manager is in compatibility mode)\' AS reason' : 'reason';
       
  1810     $remote_addr = ( strstr($_SERVER['REMOTE_ADDR'], ':') ) ? expand_ipv6_address($_SERVER['REMOTE_ADDR']) : $_SERVER['REMOTE_ADDR'];
       
  1811     
  1783     $banned = false;
  1812     $banned = false;
  1784     if ( $this->user_logged_in )
  1813     if ( $this->user_logged_in )
  1785     {
  1814     {
  1786       // check by IP, email, and username
  1815       // check by IP, email, and username
  1787       if ( ENANO_DBLAYER == 'MYSQL' )
  1816       if ( ENANO_DBLAYER == 'MYSQL' )
  1817             $regexp = parse_ip_range_regex($ban_value);
  1846             $regexp = parse_ip_range_regex($ban_value);
  1818             if ( !$regexp )
  1847             if ( !$regexp )
  1819             {
  1848             {
  1820               continue;
  1849               continue;
  1821             }
  1850             }
  1822             if ( preg_match("/$regexp/", $_SERVER['REMOTE_ADDR']) )
  1851             if ( preg_match("/$regexp/", $remote_addr) )
  1823             {
  1852             {
  1824               $reason = $reason_temp;
  1853               $reason = $reason_temp;
  1825               $banned = true;
  1854               $banned = true;
  1826             }
  1855             }
  1827           }
  1856           }
  1860           if ( $ban_type == BAN_IP && $is_regex != 1 )
  1889           if ( $ban_type == BAN_IP && $is_regex != 1 )
  1861           {
  1890           {
  1862             // check range
  1891             // check range
  1863             $regexp = parse_ip_range_regex($ban_value);
  1892             $regexp = parse_ip_range_regex($ban_value);
  1864             if ( !$regexp )
  1893             if ( !$regexp )
       
  1894             {
       
  1895               die("bad regexp for $ban_value");
  1865               continue;
  1896               continue;
  1866             if ( preg_match("/$regexp/", $_SERVER['REMOTE_ADDR']) )
  1897             }
       
  1898             if ( preg_match("/$regexp/", $remote_addr) )
  1867             {
  1899             {
  1868               $reason = $reason_temp;
  1900               $reason = $reason_temp;
  1869               $banned = true;
  1901               $banned = true;
  1870             }
  1902             }
  1871           }
  1903           }
  1877           }
  1909           }
  1878         }
  1910         }
  1879       }
  1911       }
  1880       $db->free_result();
  1912       $db->free_result();
  1881     }
  1913     }
  1882     if ( $banned && $paths->get_pageid_from_url() != $paths->nslist['Special'].'CSS' )
  1914     if ( $banned && !$this->on_critical_page(true) )
  1883     {
  1915     {
  1884       // This guy is banned - kill the session, kill the database connection, bail out, and be pretty about it
  1916       // This guy is banned - kill the session, kill the database connection, bail out, and be pretty about it
  1885       die_semicritical($lang->get('user_ban_msg_title'), '<p>' . $lang->get('user_ban_msg_body') . '</p><div class="error-box"><b>' . $lang->get('user_ban_lbl_reason') . '</b><br />' . $reason . '</div>');
  1917       die_semicritical($lang->get('user_ban_msg_title'), '<p>' . $lang->get('user_ban_msg_body') . '</p><div class="error-box"><b>' . $lang->get('user_ban_lbl_reason') . '</b><br />' . $reason . '</div>');
  1886       exit;
  1918       exit;
  1887     }
  1919     }