1: <?php
2: /**
3: * Implements the Accounts API for servers with unix accounts on the localhost
4: * machine (same machine as the web server). Should work for local unix
5: * accounts, nis/nis+ accounts, or any PAM oriented accounts that appear as
6: * local accounts on the local machine. The exception is the quota support.
7: * See that routine for additional comments.
8: *
9: * Copyright 2002-2012 Horde LLC (http://www.horde.org/)
10: *
11: * See the enclosed file COPYING for license information (LGPL). If you
12: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
13: *
14: * @author Eric Jon Rostetter <eric.rostetter@physics.utexas.edu>
15: * @author Jan Schneider <jan@horde.org>
16: * @package Horde
17: */
18: class Horde_Block_Account_Localhost extends Horde_Block_Account_Base
19: {
20: /**
21: * User information hash.
22: *
23: * @var array
24: */
25: protected $_information;
26:
27: /**
28: * Constructor.
29: *
30: * @param array $params Hash containing connection parameters.
31: */
32: public function __construct($params = array())
33: {
34: $params = array_merge(
35: array('quota_path' => 'quota',
36: 'grep_path' => 'grep'),
37: $params);
38: parent::__construct($params);
39: }
40:
41: /**
42: * Returns the user account from the posix information.
43: *
44: * @return array A hash with complete account details.
45: *
46: * @throws Horde_Exception if posix extension is missing.
47: */
48: protected function _getAccount()
49: {
50: if (!isset($this->_information)) {
51: // This won't work if we don't have posix extensions.
52: if (!Horde_Util::extensionExists('posix')) {
53: throw new Horde_Exception(_("POSIX extension is missing"));
54: }
55: $user = Horde_String::lower($this->getUsername());
56: $this->_information = posix_getpwnam($user);
57: }
58: return $this->_information;
59: }
60:
61: /**
62: * Returns the user's quota for servers with a unix quota command.
63: *
64: * This may require a modified "quota" command that allows the httpd
65: * server account to get quotas for other users... It requires that your
66: * web server and user server be the same server or at least have shared
67: * authentication and file servers (e.g. via NIS/NFS). And last, it (as
68: * written) requires the posix php extensions.
69: *
70: * If your quota command wraps the output onto two lines, then this module
71: * will only work if you have a grep which supports the -A switch, and you
72: * append an -A1 switch to your grep_path (e.g. '/bin/grep -A1').
73: *
74: * @return array A quota array, elements are used bytes and limit bytes.
75: *
76: * @throws Horde_Exception if posix extension is missing.
77: */
78: public function getQuota()
79: {
80: $information = $this->_getAccount();
81: $homedir = $information['dir'];
82:
83: // If we want mount point translations, then translate the login dir
84: // name to a mount point. If not, then simply parse out the device
85: // name from the login directory, and use that instead.
86: if ($this->_params['translateMountPoint'] &&
87: file_exists($this->_params['translationTable'])) {
88: $sysTab = File_Fstab::singleton($this->_params['translationTable']);
89: do {
90: $entry = $sysTab->getEntryForPath($homedir);
91: $homedir = dirname($homedir);
92: if ($homedir == '.' || empty($homedir)) {
93: $homedir = '/';
94: }
95: } while (is_a($entry, 'PEAR_Error'));
96: $mountPoint = $entry->device;
97: } else {
98: $homedir = explode('/', $homedir);
99: $mountPoint = '/' . $homedir[1];
100: }
101:
102: $cmdline = sprintf('%s -u %s 2>&1 | %s %s',
103: $this->_params['quota_path'],
104: $this->getUserName(),
105: $this->_params['grep_path'],
106: $mountPoint);
107: $junk = exec($cmdline, $quota_data, $return_code);
108: if ($return_code == 0 && !empty($quota_data[0])) {
109: // In case of quota output wrapping on two lines, we concat the
110: // second line of results, if any, here.
111: if (!empty($quota_data[1])) {
112: $quota_data[0] .= $quota_data[1];
113: }
114: // Now parse out the quota info and return it.
115: $quota = preg_split('/\s+/', trim($quota_data[0]));
116: return array('used' => $quota[1] * 1024, 'limit' => $quota[2] * 1024);
117: }
118:
119: return array();
120: }
121:
122: /**
123: * Returns the user's full name.
124: *
125: * @return string The user's full name.
126: *
127: * @throws Horde_Exception if posix extension is missing.
128: */
129: public function getFullname()
130: {
131: $information = $this->_getAccount();
132: $gecos_array = explode(',', $information['gecos']);
133: return $gecos_array[0];
134: }
135:
136: /**
137: * Returns the user's home (login) directory.
138: *
139: * @return string The user's directory.
140: *
141: * @throws Horde_Exception if posix extension is missing.
142: */
143: public function getHome()
144: {
145: $information = $this->_getAccount();
146: return $information['dir'];
147: }
148:
149: /**
150: * Returns the user's default shell.
151: *
152: * @return string The user's shell.
153: *
154: * @throws Horde_Exception if posix extension is missing.
155: */
156: public function getShell()
157: {
158: $information = $this->_getAccount();
159: return $information['shell'];
160: }
161: }
162: