summaryrefslogtreecommitdiff
path: root/plugins/htpasswd/htpasswd.module
blob: 6e1c9d2c08c90c8f5df780ec8dc31f9f7dbb2f20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php

function htpasswd_init($config) {
  $data = [NULL => []];
  $file = $config['file'];
  if (!is_array($file)) {
    $file = [NULL => $file];
  }
  foreach ($file as $d => $f) {
    $data[$d] = htpasswd_load($f);
  }

  // Load the plugin.
  require_once __DIR__ . '/BridgeHtpasswd.php';
  require_once __DIR__ . '/htpasswd.inc';
  return new BridgeHtpasswd($data, $config);
}

function htpasswd_load($file) {
  $data = [];
  if (file_exists($file) && is_readable($file)) {
    $lines = explode("\n", trim(file_get_contents($file)));
    foreach ($lines as $line) {
      list($user, $password) = explode(":", trim($line), 2);
      $data[$user] = $password;
    }
  }
  return $data;
}