xref: /plugin/davcal/vendor/sabre/http/examples/basicauth.php (revision a1a3b6794e0e143a4a8b51d3185ce2d339be61ab)
1*a1a3b679SAndreas Boehler<?php
2*a1a3b679SAndreas Boehler
3*a1a3b679SAndreas Boehler/**
4*a1a3b679SAndreas Boehler * This example shows how to do Basic authentication.
5*a1a3b679SAndreas Boehler * *
6*a1a3b679SAndreas Boehler * @copyright Copyright (C) 2009-2015 fruux GmbH (https://fruux.com/).
7*a1a3b679SAndreas Boehler * @author Evert Pot (http://evertpot.com/)
8*a1a3b679SAndreas Boehler * @license http://sabre.io/license/ Modified BSD License
9*a1a3b679SAndreas Boehler */
10*a1a3b679SAndreas Boehler$userList = [
11*a1a3b679SAndreas Boehler    "user1" => "password",
12*a1a3b679SAndreas Boehler    "user2" => "password",
13*a1a3b679SAndreas Boehler];
14*a1a3b679SAndreas Boehler
15*a1a3b679SAndreas Boehleruse Sabre\HTTP\Sapi;
16*a1a3b679SAndreas Boehleruse Sabre\HTTP\Response;
17*a1a3b679SAndreas Boehleruse Sabre\HTTP\Auth;
18*a1a3b679SAndreas Boehler
19*a1a3b679SAndreas Boehler// Find the autoloader
20*a1a3b679SAndreas Boehler$paths = [
21*a1a3b679SAndreas Boehler    __DIR__ . '/../vendor/autoload.php',
22*a1a3b679SAndreas Boehler    __DIR__ . '/../../../autoload.php',
23*a1a3b679SAndreas Boehler    __DIR__ . '/vendor/autoload.php',
24*a1a3b679SAndreas Boehler
25*a1a3b679SAndreas Boehler];
26*a1a3b679SAndreas Boehler
27*a1a3b679SAndreas Boehlerforeach ($paths as $path) {
28*a1a3b679SAndreas Boehler    if (file_exists($path)) {
29*a1a3b679SAndreas Boehler        include $path;
30*a1a3b679SAndreas Boehler        break;
31*a1a3b679SAndreas Boehler    }
32*a1a3b679SAndreas Boehler}
33*a1a3b679SAndreas Boehler
34*a1a3b679SAndreas Boehler$request = Sapi::getRequest();
35*a1a3b679SAndreas Boehler$response = new Response();
36*a1a3b679SAndreas Boehler
37*a1a3b679SAndreas Boehler$basicAuth = new Auth\Basic("Locked down area", $request, $response);
38*a1a3b679SAndreas Boehlerif (!$userPass = $basicAuth->getCredentials()) {
39*a1a3b679SAndreas Boehler
40*a1a3b679SAndreas Boehler    // No username or password given
41*a1a3b679SAndreas Boehler    $basicAuth->requireLogin();
42*a1a3b679SAndreas Boehler
43*a1a3b679SAndreas Boehler} elseif (!isset($userList[$userPass[0]]) || $userList[$userPass[0]] !== $userPass[1]) {
44*a1a3b679SAndreas Boehler
45*a1a3b679SAndreas Boehler    // Username or password are incorrect
46*a1a3b679SAndreas Boehler    $basicAuth->requireLogin();
47*a1a3b679SAndreas Boehler} else {
48*a1a3b679SAndreas Boehler
49*a1a3b679SAndreas Boehler    // Success !
50*a1a3b679SAndreas Boehler    $response->setBody('You are logged in!');
51*a1a3b679SAndreas Boehler
52*a1a3b679SAndreas Boehler}
53*a1a3b679SAndreas Boehler
54*a1a3b679SAndreas Boehler// Sending the response
55*a1a3b679SAndreas BoehlerSapi::sendResponse($response);
56