merak.php 5.7 KB

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