merak.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * Merakchange password backend
  4. *
  5. * @author Edwin van Elk <Edwin@eve-software.com>
  6. * @version $Id$
  7. * @package plugins
  8. * @subpackage change_password
  9. */
  10. /**
  11. * Config vars
  12. */
  13. global $merak_url, $merak_selfpage, $merak_action;
  14. // The Merak Server
  15. $merak_url = "http://localhost:32000/";
  16. $merak_selfpage = "self.html";
  17. $merak_action = "self_edit";
  18. // get overrides from config.
  19. if ( isset($cpw_merak) && is_array($cpw_merak) && !empty($cpw_merak) ) {
  20. foreach ( $cpw_merak as $key => $value ) {
  21. if ( isset(${'merak_'.$key}) )
  22. ${'merak_'.$key} = $value;
  23. }
  24. }
  25. global $squirrelmail_plugin_hooks;
  26. $squirrelmail_plugin_hooks['change_password_dochange']['merak'] =
  27. 'cpw_merak_dochange';
  28. $squirrelmail_plugin_hooks['change_password_init']['merak'] =
  29. 'cpw_merak_init';
  30. /**
  31. * Check if php install has all required extensions.
  32. */
  33. function cpw_merak_init() {
  34. global $color;
  35. /**
  36. * If SM_PATH isn't defined, define it. Required to include files.
  37. * @ignore
  38. */
  39. if (!defined('SM_PATH')) {
  40. define('SM_PATH','../../../');
  41. }
  42. // load error_box() function
  43. include_once(SM_PATH . 'functions/display_messages.php');
  44. if (!function_exists('curl_init')) {
  45. // user_error('Curl module NOT available!', E_USER_ERROR);
  46. error_box(_("PHP Curl extension is NOT available! Unable to change password!"),$color);
  47. // close html and stop script execution
  48. echo "</body></html>\n";
  49. exit();
  50. }
  51. }
  52. /**
  53. * This is the function that is specific to your backend. It takes
  54. * the current password (as supplied by the user) and the desired
  55. * new password. It will return an array of messages. If everything
  56. * was successful, the array will be empty. Else, it will contain
  57. * the errormessage(s).
  58. * Constants to be used for these messages:
  59. * CPW_CURRENT_NOMATCH -> "Your current password is not correct."
  60. * CPW_INVALID_PW -> "Your new password contains invalid characters."
  61. *
  62. * @param array data The username/currentpw/newpw data.
  63. * @return array Array of error messages.
  64. */
  65. function cpw_merak_dochange($data)
  66. {
  67. // unfortunately, we can only pass one parameter to a hook function,
  68. // so we have to pass it as an array.
  69. $username = $data['username'];
  70. $curpw = $data['curpw'];
  71. $newpw = $data['newpw'];
  72. $msgs = array();
  73. global $merak_url, $merak_selfpage, $merak_action;
  74. $ch = curl_init();
  75. curl_setopt ($ch, CURLOPT_URL, $merak_url . $merak_selfpage);
  76. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  77. curl_setopt ($ch, CURLOPT_TIMEOUT, 10);
  78. curl_setopt ($ch, CURLOPT_USERPWD, "$username:$curpw");
  79. curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
  80. $result = curl_exec ($ch);
  81. curl_close ($ch);
  82. if (strpos($result, "401 Access denied") <> 0) {
  83. array_push($msgs, _("Cannot change password! (Is user 'Self Configurable User' ?) (401)"));
  84. return $msgs;
  85. }
  86. // Get URL from: <FORM METHOD="POST" ACTION="success.html?id=a9375ee5e445775e871d5e1401a963aa">
  87. $str = stristr($result, "<FORM");
  88. $str = substr($str, 0, strpos($str, ">") + 1);
  89. $str = stristr($str, "ACTION=");
  90. $str = substr(stristr($str, "\""),1);
  91. $str = substr($str, 0, strpos($str, "\""));
  92. // Extra check to see if the result contains 'html'
  93. if (!stristr($str, "html")) {
  94. array_push($msgs, _("Cannot change password!") . " (1)" );
  95. return $msgs;
  96. }
  97. $newurl = $merak_url . $str;
  98. // Get useraddr from: $useraddr = <INPUT TYPE="HIDDEN" NAME="usraddr" VALUE="mail@hostname.com">
  99. $str = stristr($result, "usraddr");
  100. $str = substr($str, 0, strpos($str, ">") + 1);
  101. $str = stristr($str, "VALUE=");
  102. $str = substr(stristr($str, "\""),1);
  103. $str = substr($str, 0, strpos($str, "\""));
  104. // Extra check to see if the result contains '@'
  105. if (!stristr($str, "@")) {
  106. array_push($msgs, _("Cannot change password!") . " (2)" );
  107. return $msgs;
  108. }
  109. $useraddr = $str;
  110. //Include (almost) all input fields from screen
  111. $contents2 = $result;
  112. $tag = stristr($contents2, "<INPUT");
  113. while ($tag) {
  114. $contents2 = stristr($contents2, "<INPUT");
  115. $tag = substr($contents2, 0, strpos($contents2, ">") + 1);
  116. if (GetSub($tag, "TYPE") == "TEXT" ||
  117. GetSub($tag, "TYPE") == "HIDDEN" ||
  118. GetSub($tag, "TYPE") == "PASSWORD") {
  119. $tags[GetSub($tag, "NAME")] = GetSub($tag, "VALUE");
  120. }
  121. if ((GetSub($tag, "TYPE") == "RADIO" ||
  122. GetSub($tag, "TYPE") == "CHECKBOX") &&
  123. IsChecked($tag)) {
  124. $tags[GetSub($tag, "NAME")] = GetSub($tag, "VALUE");
  125. }
  126. $contents2 = substr($contents2, 1);
  127. }
  128. $tags["action"] = $merak_action;
  129. $tags["usraddr"] = $useraddr;
  130. $tags["usr_pass"] = $newpw;
  131. $tags["usr_conf"] = $newpw;
  132. $str2 = "";
  133. foreach ($tags as $key => $value) {
  134. $str2 .= $key . "=" . urlencode($value) . "&";
  135. }
  136. $str2 = trim($str2, "&");
  137. // Change password!
  138. $ch = curl_init();
  139. curl_setopt ($ch, CURLOPT_URL, $newurl);
  140. curl_setopt ($ch, CURLOPT_POST, 1);
  141. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  142. curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
  143. curl_setopt ($ch, CURLOPT_POSTFIELDS, $str2);
  144. $result=curl_exec ($ch);
  145. curl_close ($ch);
  146. if (strpos($result, "Failure") <> 0) {
  147. array_push($msgs, _("Cannot change password!") . " (3)");
  148. return $msgs;
  149. }
  150. return $msgs;
  151. }
  152. function GetSub($tag, $type) {
  153. $str = stristr($tag, $type . "=");
  154. $str = substr($str, strlen($type) + 1);
  155. $str = trim($str, '"');
  156. if (!strpos($str, " ") === false) {
  157. $str = substr($str, 0, strpos($str, " "));
  158. $str = trim($str, '"');
  159. }
  160. if (!(strpos($str, '"') === false)) {
  161. $str = substr($str, 0, strpos($str, '"'));
  162. }
  163. $str = trim($str, '>');
  164. return $str;
  165. }
  166. function IsChecked($tag) {
  167. if (!(strpos(strtolower($tag), 'checked') === false)) {
  168. return true;
  169. }
  170. return false;
  171. }