PageRenderTime 120ms CodeModel.GetById 38ms RepoModel.GetById 2ms app.codeStats 0ms

/tags/0.2-final/webapp/php/trunk/public_html/addPersonResult.php

https://github.com/shanti/olio
PHP | 153 lines | 107 code | 20 blank | 26 comment | 15 complexity | f60c0187df53dddf13e03343217c76ea MD5 | raw file
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. require_once("../etc/config.php");
  20. $connection = DBConnection::getWriteInstance();
  21. // 1. Get data from submission page.
  22. $username=$_POST['add_user_name'];
  23. $pwd =$_POST['psword'];
  24. $summary=$_POST['summary'];
  25. $fname=$_POST['first_name'];
  26. $lname=$_POST['last_name'];
  27. $email=$_POST['email'];
  28. $strt1= $_POST['street1'];
  29. $street2= $_POST['street2'];
  30. $cty = $_POST['city'];
  31. $street1=str_replace(" ","+",$strt1);
  32. $city = str_replace(" ","+",$cty);
  33. $state=$_POST['state'];
  34. $zip=$_POST['zip'];
  35. $country=$_POST['country'];
  36. $telephone=$_POST['telephone'];
  37. $timezone=$_POST['timezone'];
  38. $image_name= basename($_FILES['user_image']['name']);
  39. // 2. Get coordinates of the address.
  40. $geocode = new Geocoder($street1, $city, $state, $zip);
  41. // 3. Insert address and get the address id, or update the address.
  42. if(isset($_POST['addpersonsubmit'])) {
  43. $insertaddr = "insert into ADDRESS (street1, street2, city, state, zip, country, latitude, longitude) ".
  44. "values ('$strt1', '$street2', '$cty', '$state', '$zip', '$country', ".
  45. "'$geocode->latitude', '$geocode->longitude')";
  46. $connection->beginTransaction();
  47. $connection->exec($insertaddr);
  48. $cq = "select last_insert_id()";
  49. $idres = $connection->query($cq);
  50. while($idres->next()) {
  51. $addrid = $idres->get(1);
  52. }
  53. unset($idres);
  54. // At least temporary place holder for the image.
  55. $modified_image_name = Olio::$config['includes'] . "userphotomissing.gif";
  56. $imagethumb = Olio::$config['includes'] . "userphotomissing.gif";
  57. $insertsql = "insert into PERSON (username, password, firstname, lastname,".
  58. " email, telephone, imageurl, imagethumburl, summary,".
  59. " timezone,"."ADDRESS_addressid) values('$username', '$pwd',".
  60. " '$fname', '$lname', '$email', '$telephone',".
  61. " '$modified_image_name', '$imagethumb', '$summary',".
  62. " '$timezone', '$addrid')";
  63. $insertresult = $connection->exec($insertsql);
  64. $idres = $connection->query($cq);
  65. while ($idres->next()) {
  66. $userid = $idres->get(1);
  67. }
  68. } else if (isset($_POST['addpersonsubmitupdate'])) {
  69. if ($summary == "" ) {
  70. $sumquery = "select summary from PERSON where username='$username' ";
  71. $sumresult = $connection->query($sumquery);
  72. while ($sumresult->next()) {
  73. $summary = $sumresult->get(1);
  74. }
  75. unset($sumresult);
  76. }
  77. $insertaddr = "insert into ADDRESS (street1, street2, city, state, zip, country, latitude, longitude) ".
  78. "values ('$strt1', '$street2', '$cty', '$state', '$zip', '$country', ".
  79. "'$geocode->latitude', '$geocode->longitude')";
  80. $connection->exec($insertaddr);
  81. $cq = "select last_insert_id()";
  82. $idres = $connection->query($cq);
  83. while($idres->next()) {
  84. $addrid = $idres->get(1);
  85. }
  86. unset($idres);
  87. $updatesql ="update PERSON set password='$pwd', firstname='$fname', lastname='$lname', email='$email', telephone='$telephone',";
  88. if ($summary != "") {
  89. $updatesql .= " summary='$summary',";
  90. }
  91. $updatesql .= " timezone='$timezone', ADDRESS_addressid='$addrid' where username='$username' ";
  92. $connection->exec($updatesql);
  93. $userid_query = "select userid from PERSON where username='$username'";
  94. $idres = $connection->query($userid_query);
  95. while ($idres->next()) {
  96. $userid = $idres.get(1);
  97. }
  98. unset($idres);
  99. }
  100. // 4. End the DB transaction here.
  101. $connection->commit();
  102. if ($image_name != "") {
  103. // 5. Determine the image id.
  104. $pos=strpos($image_name,'.');
  105. $img_ext = substr($image_name,$pos,strlen($image_name));
  106. $modified_image_name = "P".$userid.$img_ext;
  107. $resourcedir = '/tmp/';
  108. $imagethumb = "P".$userid."T".$img_ext;
  109. $user_image_location = $resourcedir . $modified_image_name;
  110. if (!move_uploaded_file($_FILES['user_image']['tmp_name'], $user_image_location)) {
  111. throw new Exception("Error moving uploaded file to $user_image_location");
  112. }
  113. // 6. Generate the thumbnails.
  114. $thumb_location = $resourcedir . $imagethumb;
  115. ImageUtil::createThumb($user_image_location, $thumb_location, 120, 120);
  116. // 7. Store the image.
  117. $fs = FileSystem::getInstance();
  118. if (!$fs->create($user_image_location, "NO_OP", "NO_OP")) {
  119. error_log("Error copying image " . $user_image_location);
  120. }
  121. if (!$fs->create($thumb_location, "NO_OP", "NO_OP")) {
  122. error_log("Error copying thumb " . $thumb_location);
  123. }
  124. unlink($user_image_location);
  125. unlink($thumb_location);
  126. // 8. Update the DB.
  127. $updateimage = "update PERSON set imageurl = '$modified_image_name', ".
  128. "imagethumburl = '$imagethumb' ".
  129. "where userid = '$userid'";
  130. $updated = $connection->exec($updateimage);
  131. }
  132. header("Location:users.php?username=".$username);
  133. ?>