/**
  * Test login & logout
  * @test
  */
 public function test_login_logout()
 {
     CurrentUser::login("testuser", "testpassword");
     $this->assertEquals("testuser", $_SESSION['login']);
     $this->assertNull($_SESSION['token']);
     $this->assertNotNull(CurrentUser::$account);
     $this->assertEquals("testuser", CurrentUser::$account->login);
     $this->assertFalse(CurrentUser::$admin);
     CurrentUser::logout();
     //TODO: Failure because I do require_once. Autoload may solve the issue
     $this->assertNull($_SESSION['login']);
     $this->assertNull(CurrentUser::$account);
     $this->assertNull($_SESSION['token']);
     $this->assertFalse(CurrentUser::$admin);
 }
 /**
  * test view function
  * @test
  * @depends test_generate_key
  */
 public function test_view()
 {
     //prepare
     self::login_as_admin();
     self::delete_tokens_file();
     $paths = array();
     $keys = array();
     $paths[1] = Settings::$photos_dir . "/tokenfolder";
     $paths[2] = Settings::$photos_dir . "/tokenfolder2";
     $paths[3] = Settings::$photos_dir . "/tokenfolder/subfolder";
     for ($i = 1; $i <= 3; $i++) {
         if (!file_exists($paths[$i])) {
             mkdir($paths[$i]);
         }
         $keys[$i] = Guesttoken::generate_key();
         GuestToken::create($paths[$i], $keys[$i]);
     }
     CurrentUser::logout();
     //test
     for ($i = 1; $i <= 3; $i++) {
         $this->assertTrue(GuestToken::view($keys[$i], $paths[$i]));
     }
     $this->assertFalse(GuestToken::view($keys[3], $paths[1]));
     $this->assertFalse(GuestToken::view($keys[3], $paths[2]));
     $this->assertFalse(GuestToken::view($keys[2], $paths[1]));
     $this->assertTrue(GuestToken::view($keys[1], $paths[3]));
 }
Beispiel #3
0
 /**
  * Retrieves info for the current user account
  *
  * @author Thibaud Rohmer
  */
 public static function init()
 {
     CurrentUser::$accounts_file = Settings::$conf_dir . "/accounts.xml";
     CurrentUser::$groups_file = Settings::$conf_dir . "/groups.xml";
     /// Set path
     if (isset($_GET['f'])) {
         CurrentUser::$path = stripslashes(File::r2a($_GET['f']));
         if (isset($_GET['p'])) {
             switch ($_GET['p']) {
                 case 'n':
                     CurrentUser::$path = File::next(CurrentUser::$path);
                     break;
                 case 'p':
                     CurrentUser::$path = File::prev(CurrentUser::$path);
                     break;
             }
         }
     } else {
         /// Path not defined in URL
         CurrentUser::$path = Settings::$photos_dir;
     }
     /// Set CurrentUser account
     if (isset($_SESSION['login'])) {
         self::$account = new Account($_SESSION['login']);
         // groups sometimes can be null
         $groups = self::$account->groups === NULL ? array() : self::$account->groups;
         self::$admin = in_array("root", $groups);
         self::$uploader = in_array("uploaders", $groups);
     }
     /// Set action (needed for page layout)
     if (isset($_GET['t'])) {
         switch ($_GET['t']) {
             case "Page":
             case "Img":
             case "Thb":
                 CurrentUser::$action = $_GET['t'];
                 break;
             case "Big":
             case "BDl":
             case "Zip":
                 if (!Settings::$nodownload) {
                     CurrentUser::$action = $_GET['t'];
                 }
                 break;
             case "Reg":
                 if (isset($_POST['login']) && isset($_POST['password'])) {
                     if (!Account::create($_POST['login'], $_POST['password'], $_POST['verif'])) {
                         echo "Error creating account.";
                     }
                 }
             case "Log":
                 if (isset($_SESSION['login'])) {
                     CurrentUser::logout();
                     echo "logged out";
                     break;
                 }
                 if (isset($_POST['login']) && isset($_POST['password'])) {
                     try {
                         if (!CurrentUser::login($_POST['login'], $_POST['password'])) {
                             echo "Wrong password";
                         }
                     } catch (Exception $e) {
                         echo "Account not found";
                     }
                 }
                 if (!isset(CurrentUser::$account)) {
                     CurrentUser::$action = $_GET['t'];
                 }
                 break;
             case "Acc":
                 if (isset($_POST['old_password'])) {
                     Account::edit($_POST['login'], $_POST['old_password'], $_POST['password'], $_POST['name'], $_POST['email']);
                 }
                 CurrentUser::$action = "Acc";
                 break;
             case "Adm":
                 if (CurrentUser::$admin) {
                     CurrentUser::$action = "Adm";
                 }
                 break;
             case "Com":
                 Comments::add(CurrentUser::$path, $_POST['content'], $_POST['login']);
                 break;
             case "Rig":
                 Judge::edit(CurrentUser::$path, $_POST['users'], $_POST['groups'], true);
                 CurrentUser::$action = "Judge";
                 break;
             case "Pub":
                 Judge::edit(CurrentUser::$path);
                 CurrentUser::$action = "Judge";
                 break;
             case "Pri":
                 Judge::edit(CurrentUser::$path, array(), array(), true);
                 CurrentUser::$action = "Judge";
                 break;
             case "Inf":
                 CurrentUser::$action = "Inf";
                 break;
             case "Fs":
                 if (is_file(CurrentUser::$path)) {
                     CurrentUser::$action = "Fs";
                 }
                 break;
             default:
                 CurrentUser::$action = "Page";
                 break;
         }
     } else {
         CurrentUser::$action = "Page";
     }
     if (isset($_GET['a']) && CurrentUser::$action != "Adm") {
         if (CurrentUser::$admin || CurrentUser::$uploader) {
             new Admin();
         }
     }
     if (isset($_GET['j'])) {
         CurrentUser::$action = "JS";
     }
     /// Set default action
     if (!isset(CurrentUser::$action)) {
         CurrentUser::$action = "Page";
     }
     /// Throw exception if accounts file is missing
     if (!file_exists(CurrentUser::$accounts_file)) {
         throw new Exception("Accounts file missing", 69);
     }
     /// Create Group File if it doesn't exist
     if (!file_exists(CurrentUser::$groups_file)) {
         Group::create_group_file();
     }
     if (isset(CurrentUser::$account)) {
         CurrentUser::$admin = in_array("root", CurrentUser::$account->groups);
     }
 }