/** * Mocks BeeHub_Auth to make it show as if a certain user is logged in * * @param string $path The path to the user * @return void */ protected function setCurrentUser($path) { $user = new \BeeHub_User($path); $auth = $this->getMock('\\BeeHub\\tests\\BeeHub_Auth', array('current_user'), array(new \SimpleSAML_Auth_Simple('BeeHub'))); $auth->expects($this->any())->method('current_user')->will($this->returnValue($user)); \BeeHub::setAuth($auth); }
/** * Send an e-mail * @param array $recipients An array of the recipients. The key represents the e-mail address, the value is the displayname * @param type $subject The subject of the message * @param type $message The message body * @return void */ public function email($recipients, $subject, $message) { $beehubConfig = \BeeHub::config(); $config = $beehubConfig['email']; $mail = new Mail\Message(); $mail->setBody($message)->addTo($recipients)->setSubject($subject)->setFrom($config['sender_address'], $config['sender_name'])->setEncoding('UTF-8'); $this->emailer->send($mail); }
/** * Because we can't be sure we're using PHP 5.4 or higher, we can't use traits. * Instead, we use this global function to do the general setup for tests * * @return void */ function setUp() { reset_SERVER(); \DAV::$REGISTRY = new \BeeHub_Registry(); \DAV::$LOCKPROVIDER = new \BeeHub_Lock_Provider(); \DAV::$ACLPROVIDER = new \BeeHub_ACL_Provider(); \BeeHub::setAuth(new BeeHub_Auth(new \SimpleSAML_Auth_Simple('BeeHub'))); }
public function testMethod_HEAD() { $user = new \BeeHub_User('/system/users/john'); $auth = $this->getMock('\\BeeHub\\tests\\BeeHub_Auth', array('current_user'), array(new \SimpleSAML_Auth_Simple('BeeHub'))); $auth->expects($this->any())->method('current_user')->will($this->returnValue($user)); \BeeHub::setAuth($auth); $headers = $this->obj->method_HEAD(); $this->assertSame('no-cache', $headers['Cache-Control']); }
public function method_GET() { $this->assert(BeeHub::PRIV_READ_CONTENT); // You can request the POST authentication code through the system // collection with a 'POST_auth_code' query field set. However, this is only // allowed when using HTTPS if (isset($_GET['POST_auth_code'])) { return BeeHub::getAuth()->getPostAuthCode(); } $this->include_view(); }
public function testUser_prop_getlastmodified() { // We touch the file (updating the 'last modified' timestamp) and reload // the resource. The difference between the getlastmodified property and // the current time should not be greater than 1 second. Else either the // property is not loaded correctly or it takes too long to load the // resource. This last case is actually not an error in // user_prop_getlastmodified, but a problem nevertheless touch(\BeeHub::localPath('/foo/file.txt')); $file = new \BeeHub_MongoResource('/foo/file.txt'); $this->assertLessThan(1, $file->user_prop_getlastmodified() - time()); }
public function testWheel() { $obj = new \BeeHub_ACL_Provider(); $this->setCurrentUser('/system/users/john'); $this->assertTrue($obj->wheel(), 'BeeHub_ACL_Provider::wheel() should return true, because John is a member of the admin group'); $config = \BeeHub::config(); $adminGroup = new \BeeHub_Group($config['namespace']['admin_group']); $adminGroup->change_memberships(array('jane'), \BeeHub_Group::USER_ACCEPT); $adminGroup->change_memberships(array('jane'), \BeeHub_Group::ADMIN_ACCEPT); $this->setCurrentUser('/system/users/jane'); $this->assertTrue($obj->wheel(), 'BeeHub_ACL_Provider::wheel() should return true, because Jane is also a member of the admin group'); $this->setCurrentUser('/system/users/johny'); $this->assertFalse($obj->wheel(), 'BeeHub_ACL_Provider::wheel() should return true, because Johny is not a member of the admin group'); }
public function check_password_reset_code($reset_code) { $collection = BeeHub::getNoSQL()->users; $document = $collection->findOne(array('name' => $this->name)); if ($document['password_reset_code'] === $reset_code && $document['password_reset_expiration'] > time()) { unset($document['password_reset_code'], $document['password_reset_expiration']); $saveResult = $collection->save($document); if (!$saveResult['ok']) { throw new DAV_Status(DAV::HTTP_INTERNAL_SERVER_ERROR); } return true; } else { return false; } }
// We need SimpleSamlPHP require_once BeeHub::$CONFIG['environment']['simplesamlphp'] . 'lib' . DIRECTORY_SEPARATOR . '_autoload.php'; if (isset($_SERVER['HTTP_ORIGIN']) && !empty($_SERVER['HTTP_ORIGIN']) && parse_url($_SERVER['HTTP_ORIGIN'], PHP_URL_HOST) != $_SERVER['SERVER_NAME']) { die('Cross Origin Resourc Sharing prohibited!'); } DAV::$PROTECTED_PROPERTIES[DAV::PROP_GROUP_MEMBER_SET] = true; DAV::$ACL_PROPERTIES[BeeHub::PROP_SPONSOR] = 'sponsor'; DAV::addSupported_Properties(BeeHub::PROP_SPONSOR, 'sponsor'); BeeHub::handle_method_spoofing(); DAV::$REGISTRY = BeeHub_Registry::inst(); DAV::$LOCKPROVIDER = BeeHub_Lock_Provider::inst(); DAV::$ACLPROVIDER = BeeHub_ACL_Provider::inst(); DAV::$UNAUTHORIZED = array(BeeHub::getAuth(), 'unauthorized'); // In case of POST requests, we can already check the POST authentication code if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!BeeHub::getAuth()->checkPostAuthCode()) { throw new DAV_Status(DAV::HTTP_FORBIDDEN, 'POST authentication code (POST_auth_code) was incorrect. The correct code can be obtained with a GET request to /system/?POST_auth_code'); } } // Prepare test environments if needed if (APPLICATION_ENV === BeeHub::ENVIRONMENT_TEST && isset($_GET['test'])) { if (substr($_SERVER['REQUEST_URI'], 0, 19) !== '/foo/client_tests/?') { header('Location: /foo/client_tests/?' . $_SERVER['QUERY_STRING']); die; } define('RUN_CLIENT_TESTS', true); } else { define('RUN_CLIENT_TESTS', false); } // If we want to run the client tests, load the test configuration and reset the storage backend (of the test environment) if (APPLICATION_ENV === BeeHub::ENVIRONMENT_TEST) {
if ($tempfile === false) { print "WRONG\n"; $notGood = true; } else { \unlink($tempfile); print "ok\n"; } // If we encountered an error, abort now! if ($notGood) { \header('HTTP/1.1 500 Internal Server Error'); \ob_end_flush(); print "Your PHP configuration is not correct.\n"; exit; } try { $db = \BeeHub::getNoSQL(); } catch (DAV_Status $exception) { \header('HTTP/1.1 500 Internal Server Error'); \ob_end_flush(); print "\nFailed to connect to MongoDB\n"; exit; } $collections = $db->listCollections(); if (\count($collections) > 0) { \header('HTTP/1.1 500 Internal Server Error'); \ob_end_flush(); print "MongoDB database already contains collections. Cannot initialise the database.\n"; exit; } $datadir = new \DirectoryIterator($config['environment']['datadir']); $hasChildren = false;
/** * This method renews file .../js/principals.js * @TODO make sure that .../js/principals.js is overwritable by a `rename`; consider not writing it to a location inside the document root for security reasons */ public static function update_principals_json() { $json = array(); foreach (array('users', 'groups', 'sponsors') as $thing) { $collection = BeeHub::getNoSQL()->selectCollection($thing); $resultSet = $collection->find(array(), array('name' => true, 'displayname' => true)); $things = array(); foreach ($resultSet as $row) { $things[$row['name']] = $row['displayname']; } $json[$thing] = $things; } $local_js_path = dirname(dirname(__FILE__)) . '/public' . BeeHub::JAVASCRIPT_PATH; $filename = tempnam($local_js_path, 'tmp_principals'); file_put_contents($filename, 'nl.sara.beehub.principals = ' . json_encode($json) . ';'); rename($filename, $local_js_path . DIRECTORY_SEPARATOR . 'principals.js'); chmod($local_js_path . 'principals.js', 0664); }
/** * Gets the getcontenttype property * * @return string The content-type */ public function user_prop_getcontenttype() { return BeeHub::best_xhtml_type() . '; charset="utf-8"'; }
$i = $i + 1; } ?> </div> </div> <!-- End join tab --> <!-- Create tab --> <br/> <div id="bh-gss-panel-create" class="tab-pane fade"> <form id="bh-gss-create-form" class="form-horizontal" action="<?php echo BeeHub::SPONSORS_PATH; ?> " method="post"> <input type="hidden" name="POST_auth_code" value="<?php echo DAV::xmlescape(BeeHub::getAuth()->getPostAuthCode()); ?> " /> <div class="control-group"> <label class="control-label" for="bh-gss-name">Sponsor name</label> <div class="controls"> <input type="text" id="bh-gss-name" name="sponsor_name" required> </div> </div> <div class="control-group"> <label class="control-label" for="bh-gss-display-name">Display name</label> <div class="controls"> <input type="text" id="bh-gss-display-name" name="displayname" required> </div> </div> <div class="control-group">
public function testUser() { $user = $this->getMock('BeeHub_User', array('init_props'), array('/system/users/test_user')); $user->expects($this->any())->method('init_props'); $registryMock = $this->getMock('BeeHub_Registry', array('resource')); $registryMock->expects($this->once())->method('resource')->will($this->returnValue($user)); \DAV::$REGISTRY = $registryMock; $this->assertSame($user, \BeeHub::user('/system/users/test_user'), 'BeeHub::user() should return a user if the path is correct'); $registryMockNull = $this->getMock('BeeHub_Registry', array('resource')); $registryMockNull->expects($this->once())->method('resource')->will($this->returnValue(null)); \DAV::$REGISTRY = $registryMockNull; $this->setExpectedException('DAV_Status'); \BeeHub::group('/system/users/test_user', null, \DAV::HTTP_FORBIDDEN); }
public function testMethod_POST_DeleteMember() { $_POST['delete_members'] = array('/system/users/jane'); $headers = array(); $emailer = $this->getMock('\\BeeHub_Emailer', array('email')); $emailer->expects($this->once())->method('email'); \BeeHub::setEmailer($emailer); $foo = new \BeeHub_Group('/system/groups/foo'); $foo->change_memberships(array('jane'), \BeeHub_Group::USER_ACCEPT); $foo->change_memberships(array('jane'), \BeeHub_Group::ADMIN_ACCEPT); $expectedMemberships = array('/system/users/jane', '/system/users/john'); sort($expectedMemberships); $returnedMemberships = $foo->user_prop_group_member_set(); sort($returnedMemberships); $this->assertSame($expectedMemberships, $returnedMemberships); $this->setCurrentUser('/system/users/john'); $foo->method_POST($headers); $this->assertSame(array('/system/users/john'), $foo->user_prop_group_member_set()); $this->setCurrentUser('/system/users/jane'); $this->assertFalse($foo->is_invited()); $this->assertFalse($foo->is_requested()); $this->assertFalse($foo->is_member()); $this->assertFalse($foo->is_admin()); \BeeHub::setEmailer(new \BeeHub_Emailer()); }
protected function init_members() { $collection = BeeHub::getNoSQL()->groups; $this->members = $collection->find(array(), array('name'))->sort(array('displayname' => 1)); }
/** * @return DirectoryIterator */ private function dir() { if (is_null($this->dir)) { $collection = BeeHub::getNoSQL()->files; $unslashifiedPath = DAV::unslashify($this->path); while (substr($unslashifiedPath, 0, 1) === '/') { $unslashifiedPath = substr($unslashifiedPath, 1); } if (!empty($unslashifiedPath)) { $query = array('depth' => substr_count($unslashifiedPath, '/') + 2, 'path' => array('$regex' => '^' . preg_quote($unslashifiedPath) . '/[^/]*$')); } else { $query = array('depth' => 1); } $allChildren = $collection->find($query); $this->dir = array(); foreach ($allChildren as $document) { $child = basename($document['path']); if (isset($document['collection']) && $document['collection']) { $child .= '/'; } if (!DAV::$REGISTRY->resource($document)->isVisible()) { DAV::$REGISTRY->forget($this->path . $child); } else { $this->dir[] = $child; } } } return $this->dir; }
public function testMethod_COPYWithoutCollectionSponsor() { $bar = new \BeeHub_Directory('/bar/'); $bar->set_acl(array(new \DAVACL_Element_ace('/system/users/jane', false, array(\DAVACL::PRIV_READ, \DAVACL::PRIV_WRITE), false))); $this->setCurrentUser('/system/users/jane'); $this->obj->method_COPY('/bar/directory/'); $newDirectory = \DAV::$REGISTRY->resource('/bar/directory/'); $this->assertNull($newDirectory->user_prop_getetag()); $this->assertSame('/system/users/jane', $newDirectory->user_prop_owner()); $this->assertSame(\BeeHub::getAuth()->current_user()->user_prop_sponsor(), $newDirectory->user_prop_sponsor()); $this->assertSame(array(), $newDirectory->user_prop_acl_internal()); $this->assertSame($this->obj->user_prop('test_namespace test_property'), $newDirectory->user_prop('test_namespace test_property')); }
/** * Gets all members who have a certain property set * @param string $prop The property which should be set on the member * @return array An array with all paths to members who have the property set */ public function get_members_with_prop($prop) { $collection = BeeHub::getNoSQL()->files; $unslashifiedPath = DAV::unslashify($this->path); while (substr($unslashifiedPath, 0, 1) === '/') { $unslashifiedPath = substr($unslashifiedPath, 1); } if ($unslashifiedPath === '') { $queryArray = array('depth' => array('$gt' => 0), 'props.' . $prop => array('$exists' => true)); } else { $queryArray = array('depth' => array('$gt' => substr_count($unslashifiedPath, '/') + 1), 'path' => array('$regex' => '^' . preg_quote(DAV::slashify($unslashifiedPath)) . '.*'), 'props.' . $prop => array('$exists' => true)); } $results = $collection->find($queryArray, array('path' => 1, 'props.' . $prop => 1)); $returnVal = array(); foreach ($results as $result) { $returnVal[$result['path']] = $result['props'][$prop]; } return $returnVal; }
/** * Determine the most appropriate content type for a file * * @param string $path The (webDAV) path to the file * @return string The (MIME) content type */ public static function determineContentType($path) { if (is_null(self::$extensions)) { self::$extensions = array('doc' => 'application/msword', 'dot' => 'application/msword', 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', 'pot' => 'application/vnd.ms-powerpoint', 'pps' => 'application/vnd.ms-powerpoint', 'ppt' => 'application/vnd.ms-powerpoint', 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template', 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'wp' => 'application/wordperfect', 'wp5' => 'application/wordperfect', 'wp6' => 'application/wordperfect', 'wpd' => 'application/wordperfect', 'xls' => 'application/vnd.ms-excel', 'xlt' => 'application/vnd.ms-excel', 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template'); } $contentType = null; $filename = basename($path); $lastDot = strrpos($filename, '.'); if ($lastDot !== false) { $extension = substr($filename, $lastDot + 1); if (array_key_exists($extension, self::$extensions)) { $contentType = self::$extensions[$extension]; } } if (is_null($contentType)) { $finfo = new finfo(FILEINFO_MIME); $contentType = $finfo->file(self::localPath($path)); } if ($contentType === false) { return null; } return $contentType; }
public function testMethod_POST() { if (!setUpStorageBackend()) { $this->markTestSkipped('No storage backend specified; all tests depending on the storage backend are skipped'); return; } $_POST['user_name'] = 'jdoe'; $_POST['displayname'] = 'J Doe'; $_POST['email'] = "*****@*****.**"; $_POST['password'] = '******'; $headers = array(); $obj = $this->getMock('\\BeeHub_Users', array('include_view'), array('/system/users/')); $obj->expects($this->any())->method('include_view')->with($this->equalTo('new_user_confirmation'), $this->equalTo(array('email_address' => $_POST['email']))); $emailer = $this->getMock('\\BeeHub_Emailer', array('email')); $emailer->expects($this->once())->method('email'); \BeeHub::setEmailer($emailer); $this->expectOutputRegex('/html/'); $this->obj->method_POST($headers); $user = new \BeeHub_User('/system/users/jdoe'); $this->assertSame($_POST['displayname'], $user->user_prop(\DAV::PROP_DISPLAYNAME)); $this->assertTrue($user->check_password($_POST['password'])); $userFolder = \DAV::$REGISTRY->resource('/home/' . $_POST['user_name']); $beehubConfig = \BeeHub::config(); $this->assertSame($user->path, $userFolder->user_prop(\DAV::PROP_OWNER)); \BeeHub::setEmailer(new \BeeHub_Emailer()); }
/** * Checks whether the user submitted a correct POST authentication code and sets a new code when authentication succeeded or too many attempts have been done. * * Using this method instead of checking it yourself. This to ensure the * following: * - Enforce a consistent API (always the same POST field: POST_auth_code) * - Refresh the code after a successful check * - Refresh the code after five failed attempts * * @see getPostAuthCode() * @api * @return boolean True of the code was correct, false otherwise */ public function checkPostAuthCode() { $postField = 'POST_auth_code'; // The key of the $_SESSION array field with the number of failed attempts to check a POST authentication code $postAuthAttempts = 'POST_auth_attempts'; BeeHub::startSession(); if (!isset($_SESSION[$postAuthAttempts])) { $_SESSION[$postAuthAttempts] = 0; } if (!isset($_POST[$postField]) || empty($_POST[$postField]) || $_POST[$postField] !== $this->getPostAuthCode()) { $_SESSION[$postAuthAttempts]++; if ($_SESSION[$postAuthAttempts] >= 500) { // After 500 failed attempts, we unset the key, so a new one should be generated. This is to prevent brute force attempts. unset($_SESSION[self::$SESSION_KEY]); $_SESSION[$postAuthAttempts] = 0; } return false; } $_SESSION[$postAuthAttempts] = 0; return true; }
/** * @return mysqli * @throws DAV_Status */ public static function mysqli() { if (!self::$mysqli instanceof mysqli) { $config = BeeHub::config(); self::$mysqli = new mysqli($config['mysql']['host'], $config['mysql']['username'], $config['mysql']['password'], $config['mysql']['database']); if (!self::$mysqli) { self::$mysqli = null; throw new BeeHub_MySQL(mysqli_connect_error(), mysqli_connect_errno()); } self::$mysqli->set_charset('utf8'); #$charset = $mysqli->get_charset(); #DAV::debug($charset); } return self::$mysqli; }
function loadTestConfig() { $configFile = \dirname(__FILE__) . \DIRECTORY_SEPARATOR . 'config.ini'; if (!\file_exists($configFile)) { print 'No configuration file exists. Please copy ' . \dirname(__DIR__) . \DIRECTORY_SEPARATOR . 'config_example.ini to ' . $configFile . " and edit it to set the right configuration options\n"; die(1); } \BeeHub::loadConfig($configFile); \BeeHub::changeConfigField('namespace', 'admin_group', '/system/groups/admin'); }
/** * @param string $path */ public function resource($path) { if (is_array($path)) { $document = $path; $path = '/' . $document['path']; } else { $document = null; } $path = DAV::unslashify($path); $systemPath = DAV::unslashify(BeeHub::SYSTEM_PATH); $usersPath = DAV::unslashify(BeeHub::USERS_PATH); $groupsPath = DAV::unslashify(BeeHub::GROUPS_PATH); $sponsorsPath = DAV::unslashify(BeeHub::SPONSORS_PATH); if (isset($this->resourceCache[$path])) { return $this->resourceCache[$path]; } $localPath = BeeHub::localPath($path); $retval = null; if ($path === '/') { $retval = new BeeHub_Directory($path); } elseif ($path === $systemPath) { $retval = new BeeHub_System_Collection($path); } elseif (substr($path, 0, strlen($usersPath)) === $usersPath) { if ($path === $usersPath) { $retval = new BeeHub_Users($path); } else { try { $retval = new BeeHub_User($path); } catch (Exception $e) { } } } elseif (substr($path, 0, strlen($groupsPath)) === $groupsPath) { if ($path === $groupsPath) { $retval = new BeeHub_Groups($path); } else { try { $retval = new BeeHub_Group($path); } catch (Exception $e) { } } } elseif (substr($path, 0, strlen($sponsorsPath)) === $sponsorsPath) { if ($path === $sponsorsPath) { $retval = new BeeHub_Sponsors($path); } else { try { $retval = new BeeHub_Sponsor($path); } catch (Exception $e) { } } } else { $unslashifiedPath = $path; if (substr($unslashifiedPath, 0, 1) === '/') { $unslashifiedPath = substr($unslashifiedPath, 1); } $collection = BeeHub::getNoSQL()->files; if (!is_array($document)) { $document = $collection->findOne(array('path' => $unslashifiedPath)); } if (!is_null($document)) { if (isset($document['collection']) && $document['collection']) { $retval = new BeeHub_Directory($document); } else { $retval = new BeeHub_File($document); } } else { return null; } } return $this->resourceCache[$path] = $retval; }
public function testUser_prop_owner() { $resource = $this->getMock('\\BeeHub_Resource', array('init_props', 'user_prop_acl_internal', 'user_prop'), array($_SERVER['REQUEST_URI'])); $resource->expects($this->once())->method('user_prop')->with(\DAV::PROP_OWNER)->will($this->returnValue('/system/users/jane')); $this->assertSame('/system/users/jane', $resource->user_prop_owner()); $ownerlessResource = $this->getMock('\\BeeHub_Resource', array('init_props', 'user_prop_acl_internal'), array($_SERVER['REQUEST_URI'])); $config = \BeeHub::config(); $this->assertNull($ownerlessResource->user_prop_owner(), 'All resources have owners. If none is specified, then it defaults to null'); }
public function testStorePropertiesNewEmail() { $emailer = $this->getMock('\\BeeHub_Emailer', array('email')); $emailer->expects($this->once())->method('email'); \BeeHub::setEmailer($emailer); $user = new \BeeHub_User('/system/users/john'); $user->method_PROPPATCH(\BeeHub::PROP_EMAIL, '*****@*****.**'); $user->storeProperties(); \BeeHub::setEmailer(new \BeeHub_Emailer()); }
<li><a href="<?php echo DAV::getPath() . '?logout=yes'; ?> ">Log out from SURFconext</a></li> <?php } else { ?> <li><a href="<?php echo BeeHub::urlbase(true) . DAV::getPath() . '?login=conext'; ?> ">With SURFconext</a></li> <?php } ?> <li><a href="<?php echo BeeHub::urlbase(true) . '/system/password_reset.php'; ?> ">I forgot my password</a></li> </ul> </li> <?php } ?> <li class="beehub-spacer-surfsara-logo visible-desktop"></li> </ul> </div> </div> </div> </div> <div class="beehub-spacer-navbar-fixed-top visible-desktop"></div> <a href="http://www.surfsara.nl/"><img src="/system/img/surfsara.png" class="surfsara-logo visible-desktop" alt="A service by SURFsara" /></a>
public function is_requested($user = null) { $this->init_props(); if (is_null($user)) { $user = BeeHub::getAuth()->current_user(); } elseif (!$user instanceof BeeHub_User) { $user = BeeHub::user($user); } return ($tmp = @$this->users[$user->path]) && !$tmp['is_accepted']; }
/** * @return boolean is the current user an administrator? */ public function wheel() { $user = BeeHub::getAuth()->current_user(); return !is_null($user) && in_array(BeeHub::$CONFIG['namespace']['admin_group'], $user->user_prop_group_membership()); }