File: Procopen.php

package info (click to toggle)
php-horde-passwd 5.0.7-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 3,412 kB
  • sloc: php: 2,017; xml: 1,136; javascript: 29; makefile: 18; sh: 3
file content (68 lines) | stat: -rw-r--r-- 2,050 bytes parent folder | download | duplicates (3)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
 * Copyright 2004-2017 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (GPL). If you
 * did not receive this file, see http://www.horde.org/licenses/gpl.
 *
 * @category  Horde
 * @copyright 2004-2017 Horde LLC
 * @license   http://www.horde.org/licenses/gpl GPL
 * @package   Passwd
 */

/**
 * A procopen implementation of the passwd system.
 *
 * Any script or program can be supplied as the 'program' attribute value of
 * the params argument.  The username, old password and new password are
 * written to the stdin of the process and then the stdout and stderr of the
 * process are read and combined with the exit status value and returned to
 * the caller if the status code is not 0.
 *
 * @author    Samuel Nicolary <sam@nicolary.org>
 * @category  Horde
 * @copyright 2004-2017 Horde LLC
 * @license   http://www.horde.org/licenses/gpl GPL
 * @package   Passwd
 */
class Passwd_Driver_Procopen extends Passwd_Driver
{
    /**
     */
    protected function _changePassword($user, $oldpass, $newpass)
    {
        $descr = array(
            0 => array('pipe', 'r'),
            1 => array('pipe', 'w'),
            2 => array('pipe', 'w')
        );
        $output = '';

        $process = @proc_open($this->_params['program'], $descr, $pipes);
        if (is_resource($process)) {
            fwrite($pipes[0], "$user\n");
            fwrite($pipes[0], "$oldpass\n");
            fwrite($pipes[0], "$newpass\n");
            fclose($pipes[0]);
            while (!feof($pipes[1])) {
                $output .= fgets($pipes[1], 1024);
            }
            fclose($pipes[1]);
            while (!feof($pipes[2])) {
                $output .= fgets($pipes[2], 1024);
            }
            fclose($pipes[2]);
            $return_value = proc_close($process);
        } else {
            $return_value = -1;
        }

        $output .= " (Exit Status: $return_value)";

        if ($return_value != 0) {
            throw new Passwd_Exception($output);
        }
    }

}