Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.48 KB | None | 0 0
  1. <?php
  2. //error_reporting(E_ALL ^ E_DEPRECATED);
  3. /**
  4. * File to handle all API requests
  5. * Accepts GET and POST
  6. *
  7. * Each request will be identified by TAG
  8. * Response will be JSON data
  9.  
  10. /**
  11. * check for POST request
  12. */
  13. if (isset($_POST['tag']) && $_POST['tag'] != '') {
  14. // get tag
  15. $tag = $_POST['tag'];
  16.  
  17. // include DB_function
  18. require_once 'DB_Functions.php';
  19. $db = new DB_Functions();
  20.  
  21. // response Array
  22. $response = array("tag" => $tag, "error" => FALSE);
  23.  
  24. // checking tag
  25. if ($tag == 'login') {
  26. // Request type is check Login
  27. $mobile = $_POST['mobile'];
  28. $password = $_POST['password'];
  29.  
  30. // check for user
  31. $user = $db->getUserByMobileAndPassword($mobile, $password);
  32. if ($user != false) {
  33. // user found
  34. $response["error"] = FALSE;
  35. $response["userID"] = $user["userID"];
  36. echo json_encode($response);
  37. } else {
  38. // user not found
  39. // echo json with error = 1
  40. $response["error"] = TRUE;
  41. $response["error_msg"] = "Incorrect mobile or password!";
  42. echo json_encode($response);
  43. }
  44. } else if ($tag == 'register') {
  45. // Request type is Register new user
  46. $firstname = $_POST['firstname'];
  47. $lastname = $_POST['lastname'];
  48. $mobile = $_POST['mobile']
  49. $email = $_POST['email'];
  50. $password = $_POST['password'];
  51.  
  52. // check if user is already existed
  53. if ($db->isUserExisted($mobile)) {
  54. // user is already existed - error response
  55. $response["error"] = TRUE;
  56. $response["error_msg"] = "User already existed";
  57. echo json_encode($response);
  58. } else {
  59. // store user
  60. //Create new userID
  61. require_once 'userIDGenerator.php';
  62. $userID = createUniqueUserID($mobile);
  63. $user = $db->storeUser($userID,$firstname,$lastname,$mobile,$email,$password);
  64. if ($user) {
  65. // user stored successfully
  66. $response["error"] = FALSE;
  67. $response["userID"] = $user["userID"];
  68. echo json_encode($response);
  69. } else {
  70. // user failed to store
  71. $response["error"] = TRUE;
  72. $response["error_msg"] = "Error occured in Registartion";
  73. echo json_encode($response);
  74. }
  75. }
  76. } else {
  77. // user failed to store
  78. $response["error"] = TRUE;
  79. $response["error_msg"] = "Unknown 'tag' value. It should be either 'login' or 'register'";
  80. echo json_encode($response);
  81. }
  82. } else {
  83. //TODO
  84. echo("Server reached | Not POST Method");
  85. }
  86.  
  87. ?>
  88.  
  89. public class RegistrationActivity extends AppCompatActivity {
  90. //Input variables from layout
  91. EditText tb_firstname;
  92. EditText tb_lastname;
  93. EditText tb_mobile;
  94. EditText tb_email;
  95. EditText tb_password;
  96. EditText tb_confirm_password;
  97. CheckBox checkBox;
  98. TextView tv_checkBox;
  99. Button btn_signup;
  100.  
  101. // Progress dialog
  102. private ProgressDialog pDialog;
  103.  
  104. //Create user object from input data
  105. User user;
  106.  
  107.  
  108.  
  109. @Override
  110. protected void onCreate(Bundle savedInstanceState) {
  111. super.onCreate(savedInstanceState);
  112. setContentView(R.layout.activity_registration);
  113.  
  114. //Initialize layout variables
  115. tb_firstname = (EditText) findViewById(R.id.tb_firstname);
  116. tb_lastname = (EditText) findViewById(R.id.tb_last_name);
  117. tb_mobile = (EditText) findViewById(R.id.tb_mobile);
  118. tb_email = (EditText) findViewById(R.id.tb_email);
  119. tb_password = (EditText) findViewById(R.id.tb_password);
  120. tb_confirm_password = (EditText) findViewById(R.id.tb_confirm_password);
  121. checkBox = (CheckBox)findViewById(R.id.checkBox);
  122. tv_checkBox = (TextView)findViewById(R.id.tv_checkbox);
  123. btn_signup = (Button)findViewById(R.id.btn_signup);
  124.  
  125. pDialog = new ProgressDialog(this);
  126. pDialog.setCancelable(false);
  127.  
  128. //Add necessary validation after input from user
  129. tb_firstname.addTextChangedListener(new TextWatcher() {
  130. @Override
  131. public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  132.  
  133. }
  134.  
  135. @Override
  136. public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  137.  
  138. }
  139.  
  140. @Override
  141. public void afterTextChanged(Editable editable) {
  142. String firstName = tb_firstname.getText().toString();
  143. if(!firstName.isEmpty()){
  144. if(!isAlpha(firstName)){
  145. // contains a number
  146. //tb_firstname.setText("");
  147. tb_firstname.setError("Alphabets allowed only");
  148. }
  149. }
  150. }
  151. });
  152. tb_lastname.addTextChangedListener(new TextWatcher() {
  153. @Override
  154. public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  155.  
  156. }
  157.  
  158. @Override
  159. public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  160.  
  161. }
  162.  
  163. @Override
  164. public void afterTextChanged(Editable editable) {
  165. String lastName = tb_lastname.getText().toString();
  166. if(!lastName.isEmpty()){
  167. if(!isAlpha(lastName)){
  168. // contains a number
  169. //tb_lastname.setText("");
  170. tb_lastname.setError("Alphabets allowed only");
  171. }
  172. }
  173. }
  174. });
  175. tb_mobile.addTextChangedListener(new TextWatcher() {
  176. @Override
  177. public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  178.  
  179. }
  180.  
  181. @Override
  182. public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  183. String mobile = tb_mobile.getText().toString();
  184. if(!mobile.isEmpty()){
  185. if(!isValidMobile(mobile)){
  186. // contains a number
  187. //tb_mobile.setText("");
  188. tb_mobile.setError("Invalid");
  189. }
  190. }
  191. }
  192.  
  193. @Override
  194. public void afterTextChanged(Editable editable) {
  195.  
  196. }
  197. });
  198. tb_email.addTextChangedListener(new TextWatcher() {
  199. @Override
  200. public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  201.  
  202. }
  203.  
  204. @Override
  205. public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  206.  
  207. }
  208.  
  209. @Override
  210. public void afterTextChanged(Editable editable) {
  211. String email = tb_email.getText().toString();
  212. if(!email.isEmpty()){
  213. if(!isValidEmail(email)){
  214. // contains a number
  215. // tb_email.setText("");
  216. tb_email.setError("Invalid");
  217. }
  218. }
  219. }
  220. });
  221. tb_confirm_password.addTextChangedListener(new TextWatcher() {
  222. @Override
  223. public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  224.  
  225. }
  226.  
  227. @Override
  228. public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  229.  
  230. }
  231.  
  232. @Override
  233. public void afterTextChanged(Editable editable) {
  234. String pass = tb_password.getText().toString();
  235. String confirmed_pass = tb_confirm_password.getText().toString();
  236. if(!pass.isEmpty() && !confirmed_pass.isEmpty()){
  237. if(!pass.contentEquals(confirmed_pass)){
  238. // contains a number
  239. tb_confirm_password.setError("Passwords Don't Match!");
  240. }
  241. }
  242. }
  243. });
  244.  
  245.  
  246. tv_checkBox.setOnClickListener(new View.OnClickListener() {
  247. @Override
  248. public void onClick(View view) {
  249. if(view == tv_checkBox){
  250. //Display t&C in dialog fragment
  251. }
  252. }
  253. });
  254.  
  255.  
  256. btn_signup.setOnClickListener(new View.OnClickListener() {
  257. @Override
  258. public void onClick(View view) {
  259. if(view == btn_signup){
  260.  
  261. if(checkBox.isChecked()) {
  262. //Register User
  263. //Store user inputs in user variable
  264. user = new User();
  265. user.setFirstname(tb_firstname.getText().toString());
  266. user.setLastname(tb_lastname.getText().toString());
  267. user.setMobile(tb_mobile.getText().toString());
  268. user.setEmail(tb_email.getText().toString());
  269. user.setPassword(tb_password.getText().toString());
  270. if(isConnectedToInternet(RegistrationActivity.this)) {
  271. Register(user);
  272. }else{
  273. //Toast : "Network not available"
  274. Toast toast=Toast.makeText(getApplicationContext(),"Network not available", Toast.LENGTH_SHORT);
  275. toast.show();
  276. }
  277. }
  278. else{
  279. //Toast : "Terms and conditions not agreed"
  280. Toast toast=Toast.makeText(getApplicationContext(),"Terms and conditions not agreed", Toast.LENGTH_SHORT);
  281. toast.show();
  282. }
  283. }
  284. }
  285. });
  286.  
  287. }
  288.  
  289. private void Register(final User user) {
  290. // Tag used to cancel the request
  291. String tag_string_req = "req_register";
  292. pDialog.setMessage("Registering ...");
  293. showDialog();
  294.  
  295. StringRequest strReq = new StringRequest(Request.Method.POST,
  296. "http://paaniman.com/android_locker/userHandlerApi.php", new Response.Listener<String>() {
  297.  
  298. @Override
  299. public void onResponse(String response) {
  300. hideDialog();
  301.  
  302. try {
  303. JSONObject jObj = new JSONObject(response);
  304. boolean error = jObj.getBoolean("error");
  305. if (!error) {
  306. pDialog.setMessage("Initializing ...");
  307. showDialog();
  308. //Store data locally
  309. SharedPreferences pref = getApplicationContext().getSharedPreferences(getResources().getString(R.string.SP_app_data), MODE_PRIVATE);
  310. SharedPreferences.Editor editor = pref.edit();
  311.  
  312. editor.putBoolean(getResources().getString(R.string.app_user_registered_flag),true);
  313. editor.apply();
  314.  
  315. pref = getApplicationContext().getSharedPreferences(getResources().getString(R.string.SP_user_data), MODE_PRIVATE);
  316. editor = pref.edit();
  317.  
  318. editor.putString(getString(R.string.user_userID),jObj.getString("userID"));
  319. editor.putString(getString(R.string.user_first_name),user.getFirstname());
  320. editor.putString(getString(R.string.user_last_name),user.getLastname());
  321. editor.putString(getString(R.string.user_mobile),user.getMobile());
  322. editor.putString(getString(R.string.user_email),user.getEmail());
  323. editor.putString(getString(R.string.user_password),user.getPassword());
  324.  
  325. editor.apply();
  326.  
  327.  
  328. Thread timer= new Thread()
  329. {
  330. public void run()
  331. {
  332. try
  333. {
  334. //Display for 3 seconds
  335. sleep(2000);
  336. hideDialog();
  337. }
  338. catch (InterruptedException e)
  339. {
  340. // TODO: handle exception
  341. e.printStackTrace();
  342. }
  343. finally
  344. {
  345. Intent intent = new Intent(
  346. RegistrationActivity.this,
  347. LocationActivity.class);
  348. startActivity(intent);
  349. finish();
  350. }
  351. }
  352. };
  353. timer.start();
  354.  
  355. } else {
  356. String errorMsg = jObj.getString("error_msg");
  357. Toast.makeText(getApplicationContext(),
  358. errorMsg, Toast.LENGTH_LONG).show();
  359. }
  360. } catch (JSONException e) {
  361. e.printStackTrace();
  362. }
  363.  
  364. }
  365. }, new Response.ErrorListener() {
  366.  
  367. @Override
  368. public void onErrorResponse(VolleyError error) {
  369. Toast.makeText(getApplicationContext(),
  370. error.getMessage(), Toast.LENGTH_LONG).show();
  371. hideDialog();
  372. }
  373. }) {
  374.  
  375. @Override
  376. protected Map<String, String> getParams() {
  377. // Posting params to register url
  378. Map<String, String> params = new HashMap<String, String>();
  379. params.put("tag", "register");
  380. params.put("firstname", user.getFirstname());
  381. params.put("lastname", user.getLastname());
  382. params.put("mobile",user.getMobile());
  383. params.put("email",user.getEmail());
  384. params.put("password", user.getPassword());
  385.  
  386. return params;
  387. }
  388.  
  389. };
  390.  
  391. AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
  392.  
  393. }
  394.  
  395. @Override
  396. protected void onResume(){
  397. super.onResume();
  398. }
  399.  
  400. @Override
  401. protected void onPause(){
  402. super.onPause();
  403. }
  404.  
  405. private void showDialog() {
  406. if (!pDialog.isShowing())
  407. pDialog.show();
  408. }
  409.  
  410. private void hideDialog() {
  411. if (pDialog.isShowing())
  412. pDialog.dismiss();
  413. }
  414.  
  415. public boolean isAlpha(String name) {
  416. return name.matches("[a-zA-Z]+");
  417. }
  418. private boolean isValidMobile(String phone)
  419. {
  420. if(phone.length()==10) {
  421. return true;
  422. }else{
  423. return false;
  424. }
  425. }
  426. private boolean isValidEmail(String email)
  427. {
  428. return Patterns.EMAIL_ADDRESS.matcher(email).matches();
  429. }
  430.  
  431.  
  432. public static boolean isConnectedToInternet(Context context)
  433. {
  434. // Check intenet connectivity
  435. boolean connected = false;
  436. try
  437. {
  438. ConnectivityManager conMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
  439.  
  440. connected = ( conMgr.getActiveNetworkInfo() != null &&
  441. conMgr.getActiveNetworkInfo().isAvailable() &&
  442. conMgr.getActiveNetworkInfo().isConnected() );
  443. }catch (Exception e)
  444. {
  445. return false;
  446. }
  447.  
  448. return connected;
  449.  
  450. }
  451.  
  452.  
  453. }
  454.  
  455. 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: org.json.JSONException: End of input at character 0 of
  456. 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
  457. 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at org.json.JSONTokener.nextValue(JSONTokener.java:97)
  458. 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at org.json.JSONObject.<init>(JSONObject.java:156)
  459. 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at org.json.JSONObject.<init>(JSONObject.java:173)
  460. 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at com.chaoslabs.paaniman.RegistrationActivity$8.onResponse(RegistrationActivity.java:252)
  461. 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at com.chaoslabs.paaniman.RegistrationActivity$8.onResponse(RegistrationActivity.java:245)
  462. 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
  463. 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
  464. 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
  465. 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
  466. 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
  467. 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at android.os.Looper.loop(Looper.java:135)
  468. 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5237)
  469. 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at java.lang.reflect.Method.invoke(Native Method)
  470. 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
  471. 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
  472. 07-27 13:48:21.907 10181-10181/com.chaoslabs.paaniman W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement