]> git.donarmstrong.com Git - roundcube.git/blob - plugins/password/drivers/cpanel.php
Imported Upstream version 0.3.1
[roundcube.git] / plugins / password / drivers / cpanel.php
1 <?php
2
3 /**
4  * cPanel Password Driver
5  *
6  * Driver that adds functionality to change the users cPanel password.
7  * The cPanel PHP API code has been taken from: http://www.phpclasses.org/browse/package/3534.html
8  *
9  * This driver has been tested with Hostmonster hosting and seems to work fine.
10
11  *
12  * @version 1.0
13  * @author Fulvio Venturelli <fulvio@venturelli.org>
14  */
15
16 class HTTP
17 {
18         function HTTP($host, $username, $password, $port, $ssl, $theme)
19         {
20                 $this->ssl = $ssl ? 'ssl://' : '';
21                 $this->username = $username;
22                 $this->password = $password;
23                 $this->theme = $theme;
24                 $this->auth = base64_encode($username . ':' . $password);
25                 $this->port = $port;
26                 $this->host = $host;
27                 $this->path = '/frontend/' . $theme . '/';
28         }
29
30         function getData($url, $data = '')
31         {
32                 $url = $this->path . $url;
33                 if(is_array($data))
34                 {
35                         $url = $url . '?';
36                         foreach($data as $key=>$value)
37                         {
38                                 $url .= urlencode($key) . '=' . urlencode($value) . '&';
39                         }
40                         $url = substr($url, 0, -1);
41                 }
42                 $response = '';
43                 $fp = fsockopen($this->ssl . $this->host, $this->port);
44                 if(!$fp)
45                 {
46                         return false;
47                 }
48                 $out = 'GET ' . $url . ' HTTP/1.0' . "\r\n";
49                 $out .= 'Authorization: Basic ' . $this->auth . "\r\n";
50                 $out .= 'Connection: Close' . "\r\n\r\n";
51                 fwrite($fp, $out);
52                 while (!feof($fp))
53                 {
54                         $response .= @fgets($fp);
55                 }
56                 fclose($fp);
57                 return $response;
58         }
59 }
60
61
62 class emailAccount
63 {          
64         function emailAccount($host, $username, $password, $port, $ssl, $theme, $address)
65         {
66                 $this->HTTP = new HTTP($host, $username, $password, $port, $ssl, $theme);
67                 if(strpos($address, '@'))
68                 {
69                         list($this->email, $this->domain) = explode('@', $address);
70                 }
71                 else
72                 {
73                         list($this->email, $this->domain) = array($address, '');
74                 }
75         }
76
77  /*
78   * Change email account password
79   *
80   * Returns true on success or false on failure.
81   * @param string $password email account password
82   * @return bool
83   */
84         function setPassword($password)
85         {
86                 $data['email'] = $this->email;
87                 $data['domain'] = $this->domain;
88                 $data['password'] = $password;
89                 $response = $this->HTTP->getData('mail/dopasswdpop.html', $data);
90                 if(strpos($response, 'success') && !strpos($response, 'failure'))
91                 {
92                         return true;
93                 }
94                 return false;
95         }
96 }
97
98
99 function password_save($curpas, $newpass)
100 {
101     $rcmail = rcmail::get_instance();
102
103     // Create a cPanel email object
104     $cPanel = new emailAccount($rcmail->config->get('password_cpanel_host'),
105         $rcmail->config->get('password_cpanel_username'),
106         $rcmail->config->get('password_cpanel_password'),
107         $rcmail->config->get('password_cpanel_port'),
108         $rcmail->config->get('password_cpanel_ssl'),
109         $rcmail->config->get('password_cpanel_theme'),
110         $_SESSION['username'] );
111
112     if ($cPanel->setPassword($newpass)){
113         return PASSWORD_SUCCESS;
114     }
115     else
116     {
117        return PASSWORD_ERROR;
118     }
119 }
120
121 ?>