user.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. #
  5. # The contents of this file are subject to the Mozilla Public License Version
  6. # 1.1 (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. # http://www.mozilla.org/MPL/
  9. #
  10. # Software distributed under the License is distributed on an "AS IS" basis,
  11. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. # for the specific language governing rights and limitations under the
  13. # License.
  14. #
  15. # The Initial Developer of the Original Code is balu
  16. #
  17. # Portions created by the Initial Developer are Copyright (C) 2012
  18. # the Initial Developer. All Rights Reserved.
  19. #
  20. # Contributor(s):
  21. #
  22. # Alternatively, the contents of this file may be used under the terms of
  23. # either the GNU General Public License Version 2 or later (the "GPL"), or
  24. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  25. # in which case the provisions of the GPL or the LGPL are applicable instead
  26. # of those above. If you wish to allow use of your version of this file only
  27. # under the terms of either the GPL or the LGPL, and not to allow others to
  28. # use your version of this file under the terms of the MPL, indicate your
  29. # decision by deleting the provisions above and replace them with the notice
  30. # and other provisions required by the GPL or the LGPL. If you do not delete
  31. # the provisions above, a recipient may use your version of this file under
  32. # the terms of any one of the MPL, the GPL or the LGPL.
  33. #
  34. # ***** END LICENSE BLOCK *****
  35. /*
  36. ## DESCRIPTION: Implementation of user api v1.0
  37. ##
  38. ## AUTHOR: balu
  39. ##
  40. ## DATE: 20.02.2012
  41. ##
  42. ## VERSION: 0.1
  43. */
  44. require_once 'weave_utils.php';
  45. if(!$include) //file should only be used in context of index.php
  46. {
  47. log_error("include error");
  48. report_problem('Function not found', 404);
  49. }
  50. require_once "settings.php";
  51. // basic path extraction and validation. No point in going on if these are missing
  52. $path = '/';
  53. if (!empty($_SERVER['PATH_INFO']))
  54. $path = $_SERVER['PATH_INFO'];
  55. else if (!empty($_SERVER['ORIG_PATH_INFO']))
  56. $path = $_SERVER['ORIG_PATH_INFO'];
  57. else if (!empty($_SERVER["REQUEST_URI"]))
  58. {
  59. // improved path handling to prevent invalid server url error message in Firefox
  60. log_error("experimental path");
  61. // this is kind of an experimental try, i needed it so i build it,
  62. // but that doesent mean that it does work... well it works for me
  63. // and it shouldnt break anything...
  64. $path = $_SERVER["REQUEST_URI"];
  65. $lastfolder = substr(FSYNCMS_ROOT,strrpos(FSYNCMS_ROOT, "/",-2));
  66. $path = substr($path, (strpos($path,$lastfolder) + strlen($lastfolder)-1)); // chop the lead slash
  67. if(strpos($path,'?') != false)
  68. $path = substr($path, 0, strpos($path,'?')); // remove php arguments
  69. log_error("path_exp:".$path);
  70. }
  71. else
  72. {
  73. log_error("user.php: No path found");
  74. report_problem("No path found", 404);
  75. }
  76. $path = substr($path, 1); #chop the lead slash
  77. // split path into parts and make sure that all values are properly initialized
  78. list($preinstr, $version, $username, $function, $collection, $id) = array_pad(explode('/', $path.'///'), 6, '');
  79. log_error("Pfad:".$path);
  80. if( $preinstr != 'user' && $preinstr != 'misc' )
  81. report_problem('Function not found', 404);
  82. if ($version != '1.0')
  83. report_problem('Function not found', 404);
  84. //if captcha
  85. if(($preinstr =='misc') && ($_SERVER['REQUEST_METHOD'] == 'GET') && ($username =='captcha_html'))
  86. {
  87. if(ENABLE_REGISTER)
  88. exit("Fill in the details and click next.");
  89. else
  90. exit("Registration is currently closed, sorry.");
  91. }
  92. //probably no need but...
  93. header("Content-type: application/json");
  94. //if ($function != "info" && $function != "storage")
  95. // report_problem(WEAVE_ERROR_FUNCTION_NOT_SUPPORTED, 400);
  96. if (!validate_username($username))
  97. {
  98. log_error( "invalid user");
  99. report_problem(WEAVE_ERROR_INVALID_USERNAME, 400);
  100. }
  101. #user passes preliminaries, connections made, onto actually getting the data
  102. try
  103. {
  104. if ($_SERVER['REQUEST_METHOD'] == 'GET')
  105. {
  106. $db = new WeaveStorage($username);
  107. log_error("user.php: GET");
  108. if($function == 'node' && $collection == 'weave') //client fragt node an
  109. {
  110. // reply node server for user
  111. //to be compatible with users how use /index.php/ in their path
  112. /*$index ="https://";
  113. if (!isset($_SERVER['HTTPS']))
  114. $index = "http://";
  115. $index .= $_SERVER['SERVER_NAME']. dirname($_SERVER['SCRIPT_NAME']) . "/";
  116. if(strpos($_SERVER['REQUEST_URI'],'index.php') !== 0)
  117. $index .= "index.php/";
  118. */
  119. // modification to support iPhone/iPod Touch devices
  120. // check http://www.rfkd.de/?p=974 for further details
  121. $port = parse_url(FSYNCMS_ROOT, PHP_URL_PORT);
  122. $url_port = '';
  123. // If the url has a port
  124. if ($port != '')
  125. $url_port = ":" . $port;
  126. // New URL
  127. $parsed_url = parse_url(FSYNCMS_ROOT, PHP_URL_HOST) . $url_port . parse_url(FSYNCMS_ROOT, PHP_URL_PATH);
  128. if (isset($_SERVER['HTTPS'])) {
  129. exit("https://" . $parsed_url);
  130. } else {
  131. // allow http requests because use of self-signed certificates
  132. // on iPhone/iPod Touch devices doesn't work
  133. exit("http://" . $parsed_url);
  134. }
  135. }
  136. else if($function == 'password_reset')
  137. {
  138. //email mit neuem pw senden
  139. /*
  140. Possible errors:
  141. 503: problems with looking up the user or sending the email
  142. 400: 12 (No email address on file)
  143. 400: 3 (Incorrect or missing username)
  144. 400: 2 (Incorrect or missing captcha)
  145. */
  146. report_problem(WEAVE_ERROR_NO_EMAIL, 400);
  147. }
  148. //node/weave
  149. else if($function == '' && $collection == '' && $id =='') //frage nach freiem usernamen
  150. //User exists
  151. {
  152. //$db = new WeaveStorage($username);
  153. if(exists_user($db))
  154. exit(json_encode(1));
  155. else
  156. exit(json_encode(0));
  157. }
  158. else
  159. report_problem(WEAVE_ERROR_INVALID_PROTOCOL, 400);
  160. }
  161. else if($_SERVER['REQUEST_METHOD'] == 'PUT')
  162. {
  163. if(ENABLE_REGISTER)
  164. {
  165. $db = new WeaveStorage(null);
  166. //Requests that an account be created for username.
  167. /*
  168. The JSON payload should include
  169. Field Description
  170. password The password to be associated with the account.
  171. email Email address associated with the account
  172. captcha-challenge The challenge string from the captcha (see miscellaneous functions below)
  173. captcha-response The response to the captcha. Only required if WEAVE_REGISTER_USE_CAPTCHA is set
  174. */
  175. log_error("PUT");
  176. $data = get_json();
  177. log_error(print_r($data,true));
  178. //werte vorhanden
  179. if($data == NULL)
  180. report_problem(WEAVE_ERROR_JSON_PARSE, 400);
  181. $name = $username;
  182. $pwd = fix_utf8_encoding($data['password']);
  183. $email = $data['email'];
  184. if($email == '')
  185. {
  186. log_error('create user datenfehler');
  187. report_problem(WEAVE_ERROR_NO_EMAIL, 400);
  188. }
  189. else if ( $pwd == '' )
  190. {
  191. log_error('create user datenfehler');
  192. report_problem(WEAVE_ERROR_MISSING_PASSWORD, 400);
  193. }
  194. if($name == '' || $pwd == '' || $email == '')
  195. {
  196. log_error('create user datenfehler');
  197. report_problem(WEAVE_ERROR_JSON_PARSE, 400);
  198. }
  199. log_error("create user ".$name." pw : ".$pwd);
  200. try{
  201. if ($db->create_user($name, $pwd))
  202. {
  203. log_error("successfully created user");
  204. exit(json_encode(strtolower($name)));
  205. }
  206. else
  207. {
  208. log_error("create user failed");
  209. report_problem(WEAVE_ERROR_NO_OVERWRITE, 503);
  210. }
  211. }
  212. catch(Exception $e)
  213. {
  214. log_error("db exception create user");
  215. header("X-Weave-Backoff: 1800");
  216. report_problem($e->getMessage(), $e->getCode());
  217. }
  218. }
  219. else
  220. {
  221. log_error("register not enabled");
  222. report_problem(WEAVE_ERROR_FUNCTION_NOT_SUPPORTED,400);
  223. }
  224. } // ende put
  225. else if($_SERVER['REQUEST_METHOD'] == 'POST')
  226. {
  227. if($username == '')
  228. {
  229. log_error("user.php : Post no username");
  230. report_problem(WEAVE_ERROR_INVALID_USERNAME, 400);
  231. }
  232. $db = new WeaveStorage($username);
  233. log_error("user.php: POST");
  234. if($function == "password")
  235. {
  236. #Auth the user
  237. verify_user($username, $db);
  238. $new_pwd = get_phpinput();
  239. log_error("user.php: POST password ");
  240. //to do
  241. // change pw in db
  242. $hash = WeaveHashFactory::factory();
  243. if($db->change_password($hash->hash($new_pwd)))
  244. exit("success");
  245. else
  246. report_problem(WEAVE_ERROR_INVALID_PROTOCOL, 503); //server db messed up somehow
  247. // return success
  248. // report_problem(7, 400);
  249. }
  250. else if($function == "email")
  251. {
  252. //change email adr
  253. }
  254. else if($function == "delete_account")
  255. {
  256. //remove account and data from sync server
  257. // 1. verify user auth, needs $auth_pw passed to function.
  258. if($auth_pw == '')
  259. {
  260. log_error("user.php : delete account, no auth password given");
  261. report_problem(WEAVE_ERROR_MISSING_PASSWORD, 400);
  262. }
  263. try
  264. {
  265. $existingHash = $db->get_password_hash(); //passes $username internally
  266. $hash = WeaveHashFactory::factory();
  267. if ( ! $hash->verify(fix_utf8_encoding($auth_pw), $existingHash) )
  268. {
  269. log_error("Auth failed 2 {");
  270. log_error(" User pw: ". $auth_user ."|".$auth_pw ."|md5:". md5($auth_pw) ."|fix:". fix_utf8_encoding($auth_pw) ."|fix md5 ". md5(fix_utf8_encoding($auth_pw)));
  271. log_error(" Url_user: ".$url_user);
  272. log_error(" Existing hash: ".$existingHash);
  273. log_error("}");
  274. report_problem('Authentication failed', '401');
  275. } else {
  276. // 2. get collections + data and remove data
  277. // 3. remove user account
  278. }
  279. }
  280. catch(Exception $e)
  281. {
  282. header("X-Weave-Backoff: 1800");
  283. log_error($e->getMessage(), $e->getCode());
  284. report_problem($e->getMessage(), $e->getCode());
  285. }
  286. }
  287. else
  288. {
  289. report_problem(WEAVE_ERROR_INVALID_PROTOCOL, 400);
  290. }
  291. // exit('success');
  292. }
  293. }
  294. catch(Exception $e)
  295. {
  296. report_problem($e->getMessage(), $e->getCode());
  297. }
  298. #The datasets we might be dealing with here are too large for sticking it all into an array, so
  299. #we need to define a direct-output method for the storage class to use. If we start producing multiples
  300. #(unlikely), we can put them in their own class.
  301. #include_once "WBOJsonOutput.php";
  302. ?>