merak.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. // NO NEED TO CHANGE ANYTHING BELOW THIS LINE
  19. global $squirrelmail_plugin_hooks;
  20. $squirrelmail_plugin_hooks['change_password_dochange']['merak'] =
  21. 'cpw_merak_dochange';
  22. /**
  23. * This is the function that is specific to your backend. It takes
  24. * the current password (as supplied by the user) and the desired
  25. * new password. It will return an array of messages. If everything
  26. * was successful, the array will be empty. Else, it will contain
  27. * the errormessage(s).
  28. * Constants to be used for these messages:
  29. * CPW_CURRENT_NOMATCH -> "Your current password is not correct."
  30. * CPW_INVALID_PW -> "Your new password contains invalid characters."
  31. *
  32. * @param array data The username/currentpw/newpw data.
  33. * @return array Array of error messages.
  34. */
  35. function cpw_merak_dochange($data)
  36. {
  37. // unfortunately, we can only pass one parameter to a hook function,
  38. // so we have to pass it as an array.
  39. $username = $data['username'];
  40. $curpw = $data['curpw'];
  41. $newpw = $data['newpw'];
  42. $msgs = array();
  43. global $merak_url, $merak_selfpage, $merak_action, $use_ssl_for_password_change, $debug;
  44. if (!function_exists('curl_init')) {
  45. // user_error('Curl module NOT available!', E_USER_ERROR);
  46. array_push($msgs, _("Curl module NOT available! Unable to change password!"));
  47. return $msgs;
  48. }
  49. $ch = curl_init();
  50. curl_setopt ($ch, CURLOPT_URL, $merak_url . $merak_selfpage);
  51. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  52. curl_setopt ($ch, CURLOPT_TIMEOUT, 10);
  53. curl_setopt ($ch, CURLOPT_USERPWD, "$username:$curpw");
  54. curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
  55. $result = curl_exec ($ch);
  56. curl_close ($ch);
  57. if (strpos($result, "401 Access denied") <> 0) {
  58. array_push($msgs, _("Cannot change password! (Is user 'Self Configurable User' ?) (401)"));
  59. return $msgs;
  60. }
  61. // Get URL from: <FORM METHOD="POST" ACTION="success.html?id=a9375ee5e445775e871d5e1401a963aa">
  62. $str = stristr($result, "<FORM");
  63. $str = substr($str, 0, strpos($str, ">") + 1);
  64. $str = stristr($str, "ACTION=");
  65. $str = substr(stristr($str, "\""),1);
  66. $str = substr($str, 0, strpos($str, "\""));
  67. // Extra check to see if the result contains 'html'
  68. if (!stristr($str, "html")) {
  69. array_push($msgs, _("Cannot change password!") . " (1)" );
  70. return $msgs;
  71. }
  72. $newurl = $merak_url . $str;
  73. // Get useraddr from: $useraddr = <INPUT TYPE="HIDDEN" NAME="usraddr" VALUE="mail@hostname.com">
  74. $str = stristr($result, "usraddr");
  75. $str = substr($str, 0, strpos($str, ">") + 1);
  76. $str = stristr($str, "VALUE=");
  77. $str = substr(stristr($str, "\""),1);
  78. $str = substr($str, 0, strpos($str, "\""));
  79. // Extra check to see if the result contains '@'
  80. if (!stristr($str, "@")) {
  81. array_push($msgs, _("Cannot change password!") . " (2)" );
  82. return $msgs;
  83. }
  84. $useraddr = $str;
  85. //Include (almost) all input fields from screen
  86. $contents2 = $result;
  87. $tag = stristr($contents2, "<INPUT");
  88. while ($tag) {
  89. $contents2 = stristr($contents2, "<INPUT");
  90. $tag = substr($contents2, 0, strpos($contents2, ">") + 1);
  91. if (GetSub($tag, "TYPE") == "TEXT" ||
  92. GetSub($tag, "TYPE") == "HIDDEN" ||
  93. GetSub($tag, "TYPE") == "PASSWORD") {
  94. $tags[GetSub($tag, "NAME")] = GetSub($tag, "VALUE");
  95. }
  96. if ((GetSub($tag, "TYPE") == "RADIO" ||
  97. GetSub($tag, "TYPE") == "CHECKBOX") &&
  98. IsChecked($tag)) {
  99. $tags[GetSub($tag, "NAME")] = GetSub($tag, "VALUE");
  100. }
  101. $contents2 = substr($contents2, 1);
  102. }
  103. $tags["action"] = $merak_action;
  104. $tags["usraddr"] = $useraddr;
  105. $tags["usr_pass"] = $newpw;
  106. $tags["usr_conf"] = $newpw;
  107. $str2 = "";
  108. foreach ($tags as $key => $value) {
  109. $str2 .= $key . "=" . urlencode($value) . "&";
  110. }
  111. $str2 = trim($str2, "&");
  112. // Change password!
  113. $ch = curl_init();
  114. curl_setopt ($ch, CURLOPT_URL, $newurl);
  115. curl_setopt ($ch, CURLOPT_POST, 1);
  116. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  117. curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
  118. curl_setopt ($ch, CURLOPT_POSTFIELDS, $str2);
  119. $result=curl_exec ($ch);
  120. curl_close ($ch);
  121. if (strpos($result, "Failure") <> 0) {
  122. array_push($msgs, _("Cannot change password!") . " (3)");
  123. return $msgs;
  124. }
  125. return $msgs;
  126. }
  127. function GetSub($tag, $type) {
  128. $str = stristr($tag, $type . "=");
  129. $str = substr($str, strlen($type) + 1);
  130. $str = trim($str, '"');
  131. if (!strpos($str, " ") === false) {
  132. $str = substr($str, 0, strpos($str, " "));
  133. $str = trim($str, '"');
  134. }
  135. if (!(strpos($str, '"') === false)) {
  136. $str = substr($str, 0, strpos($str, '"'));
  137. }
  138. $str = trim($str, '>');
  139. return $str;
  140. }
  141. function IsChecked($tag) {
  142. if (!(strpos(strtolower($tag), 'checked') === false)) {
  143. return true;
  144. }
  145. return false;
  146. }