PageRenderTime 62ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/pmfv2-1-0-alpha-1/modules/user/TrackingDAO.php

https://github.com/redbugz/rootstech2013
PHP | 259 lines | 190 code | 43 blank | 26 comment | 34 complexity | 72afd0039a1480c1c8997758a499e492 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause
  1. <?php
  2. class TrackingDAO extends MyFamilyDAO {
  3. function updateEmail($newemail) {
  4. global $tblprefix, $pdo;
  5. $id = $this->getId();
  6. $stmt = $pdo->prepare("UPDATE ".$tblprefix."users SET email = ? WHERE id = ?");
  7. $stmt->bindParam(1, $newemail, PDO::PARAM_STR);
  8. $stmt->bindParam(2, $id, PDO::PARAM_STR);
  9. $stmt->execute();
  10. //Tracking is allowed by unregistered users hence this
  11. $stmt = $pdo->prepare("UPDATE ".$tblprefix."tracking SET email = ? WHERE email = ?");
  12. $stmt->bindParam(1, $newemail, PDO::PARAM_STR);
  13. $stmt->bindParam(2, $_SESSION["email"], PDO::PARAM_STR);
  14. $stmt->execute();
  15. $_SESSION["email"] = $newemail;
  16. }
  17. // function: delete_expired
  18. // deletes timedout requests from database
  19. function delete_expired() {
  20. global $tblprefix, $pdo;
  21. // clear out subscription requests
  22. $dquery = "DELETE FROM ".$tblprefix."tracking WHERE expires < NOW() and expires IS NOT NULL AND `action` = 'sub'";
  23. if ($pdo->exec($dquery) === FALSE) {
  24. die(print_r($pdo->errorInfo(), true));
  25. }
  26. // clear out unsubscribe requests
  27. $dquery = "UPDATE ".$tblprefix."tracking SET `key` = '', `expires` = NULL WHERE expires < NOW() and expires IS NOT NULL AND `action` = 'unsub'";
  28. if ($pdo->exec($dquery) === FALSE) {
  29. die(print_r($pdo->errorInfo(), true));
  30. }
  31. } // end of delete_expired()
  32. function trackByRegistered($person, $email) {
  33. global $tblprefix, $pdo;
  34. $query = "INSERT INTO ".$tblprefix."tracking (person_id, email) VALUES (".$pdo->quote($person).", ".$pdo->quote($email).")";
  35. if ($pdo->exec($query) === FALSE) {
  36. die(print_r($pdo->errorInfo(), true));
  37. }
  38. }
  39. function untrackByRegistered($person, $email) {
  40. global $tblprefix, $pdo;
  41. $query = "DELETE FROM ".$tblprefix."tracking WHERE person_id = ".$pdo->quote($person)." AND email = ".$pdo->quote($email);
  42. if ($pdo->exec($query) === FALSE) {
  43. die(print_r($pdo->errorInfo(), true));
  44. }
  45. }
  46. function trackByUnregistered($person, $name, $newkey, $email) {
  47. global $tblprefix, $pdo, $eSubBody, $eSubSubject;
  48. // insert into database
  49. $iquery = "INSERT INTO ".$tblprefix."tracking (person_id, email, `key`, `action`, expires) VALUES ('".$person."', '".$email."', '".$newkey."', 'sub', DATE_ADD(NOW(), INTERVAL 24 HOUR))";
  50. $iresult = mysql_query($iquery);
  51. // if we get this error then already tracking
  52. if (mysql_errno() == 1062) {
  53. $ret = 1;
  54. } else {
  55. $this->mailSubscriber($eSubBody, $name, $newkey, $eSubSubject, $email);
  56. $ret = 0;
  57. }
  58. return ($ret);
  59. }
  60. function untrackByUnregistered($person, $name, $newkey, $email) {
  61. global $tblprefix, $pdo, $eSubSubject, $eUnSubBody;
  62. $ret = 1;
  63. $uquery = "UPDATE ".$tblprefix."tracking SET `key` = '".$newkey."', `expires` = DATE_ADD(NOW(), INTERVAL 24 HOUR), `action` = 'unsub' WHERE person_id = '".$person."' AND email = '".$email."'";
  64. if (($numrows = $pdo->exec($uquery)) === FALSE) {
  65. die(print_r($pdo->errorInfo(), true));
  66. }
  67. if ($numrows != 0) {
  68. $this->mailSubscriber($eUnSubBody, $name, $newkey, $eSubSubject, $email);
  69. $ret = 0;
  70. }
  71. return ($ret);
  72. }
  73. function isTracked($email, $person) {
  74. global $tblprefix, $pdo;
  75. $query = "SELECT * FROM ".$tblprefix."tracking WHERE email = ".$pdo->quote($email)." AND person_id = ".$pdo->quote($person);
  76. if (($result = $pdo->query($query)) == FALSE) {
  77. die(print_r($pdo->errorInfo(), true));
  78. }
  79. if ($result->rowCount() == 0) {
  80. $ret = false;
  81. } else {
  82. $ret = true;
  83. }
  84. return ($ret);
  85. }
  86. function processTrackingAction($key, $action) {
  87. global $tblprefix, $pdo;
  88. $ret = 0;
  89. // find out what we're supposed to do
  90. $kquery = "SELECT * FROM ".$tblprefix."tracking WHERE `key` = ".$pdo->quote($key);
  91. foreach ($pdo->query($kquery) as $krow) {
  92. $action = $krow["action"];
  93. }
  94. // sub or un?
  95. if ($action == "sub") {
  96. // check we have key and action it
  97. $pquery = "UPDATE ".$tblprefix."tracking SET `key` = '', expires = NULL WHERE `key` = ".$pdo->quote($key);
  98. if (($numrows = $pdo->exec($pquery)) === FALSE) {
  99. die(print_r($pdo->errorInfo(), true));
  100. }
  101. if ($numrows != 0) {
  102. // You are now monitoring this person
  103. $ret = 1;
  104. } else {
  105. // Theres been a problem
  106. $ret = -1;
  107. }
  108. } elseif ($action == "unsub") {
  109. $uquery = "DELETE FROM ".$tblprefix."tracking WHERE `key` = ".$pdo->quote($key)." AND `action` = 'unsub'";
  110. if (($numrows = $pdo->exec($uquery)) === FALSE) {
  111. die(print_r($pdo->errorInfo(), true));
  112. }
  113. if ($numrows != 0) {
  114. // You are now not monitoring this person
  115. $ret = 2;
  116. } else {
  117. // Theres been a problem
  118. $ret = -1;
  119. }
  120. } else {
  121. $ret = -1;
  122. }
  123. return ($ret);
  124. }
  125. function trackPerson($person) {
  126. global $tblprefix;
  127. global $err_person;
  128. global $eTrackSubject;
  129. global $eTrackBodyTop;
  130. global $eTrackBodyBottom;
  131. global $currentRequest;
  132. global $pdo;
  133. $config = Config::getInstance();
  134. $tquery = "SELECT ".$tblprefix."people.person_id, email FROM ".$tblprefix."people, ".$tblprefix."tracking WHERE ".
  135. $tblprefix."people.person_id = ".$tblprefix."tracking.person_id AND ".$tblprefix."people.person_id = ?".
  136. " AND `key` = '' AND expires IS NULL";
  137. $stmt = $pdo->prepare($tquery);
  138. $stmt->bindParam(1, $person->person_id, PDO::PARAM_INT);
  139. if ($stmt->execute() === FALSE) {
  140. die($err_person);
  141. }
  142. while ($trow = $stmt->fetch(PDO::FETCH_ASSOC)) {
  143. $subject=str_replace("$1", $person->getDisplayName(), $eTrackSubject);
  144. $body = str_replace("$1", $person->getDisplayName(), $eTrackBodyTop);
  145. $body = str_replace("$2", $config->absurl, $body);
  146. $body = str_replace("$3", $currentRequest->name, $body);
  147. $body .= $config->absurl."people.php?person=".$person->person_id."\n\n";
  148. $body .= $eTrackBodyBottom;
  149. $body .= $config->absurl."track.php?person=".$person->person_id."&action=unsub&email=".$trow["email"]."&name=".urlencode($person->name->getDisplayName())."\n";
  150. $this->mailSubscriber($body, $person->getDisplayName(), '', $subject, $trow["email"]);
  151. }
  152. $stmt->closeCursor();
  153. } // eod of track_person()
  154. // function: bb_person($person)
  155. // send a big brother email on all changes
  156. function bbPerson($person, $action = "updated") {
  157. global $tblprefix;
  158. global $err_person;
  159. global $eBBSubject;
  160. global $eTrackBodyTop;
  161. global $eBBBottom;
  162. global $currentRequest;
  163. $config = Config::getInstance();
  164. // Give a subject line
  165. $subject = str_replace("$1", $person->getDisplayName(), $eBBSubject);
  166. // Flesh out the body
  167. $body = str_replace("$1", $person->getDisplayName(), $eTrackBodyTop);
  168. $body = str_replace("$2", $config->absurl, $body);
  169. $body = str_replace("$3", $currentRequest->name, $body);
  170. $body .= $config->absurl."people.php?person=".$person->person_id."\n\n";
  171. $body .= $eBBBottom;
  172. // Fire of the Big Brother email
  173. $this->mailSubscriber($body, $person->getDisplayName(), '', $subject, $config->email);
  174. } // end of bb_person()
  175. // function: stamppeeps
  176. // timestamp a particular person for last updated
  177. function stamppeeps($person) {
  178. // declare globals used within
  179. global $tblprefix, $currentRequest, $pdo;
  180. $config = Config::getInstance();
  181. // update the updated column
  182. $query = "UPDATE ".$tblprefix."people SET updated = NOW(), editor_id=".$currentRequest->id." WHERE person_id = '".$person->person_id."'";
  183. $result = $pdo->exec($query);
  184. // If we allow tracking by email
  185. if ($config->tracking)
  186. $this->trackPerson($person);
  187. // If Big Brother is watching
  188. if ($config->bbtracking)
  189. $this->bbPerson($person);
  190. } // end of stamppeeps()
  191. function mailSubscriber($body, $name, $newkey, $subject, $email) {
  192. $config = Config::getInstance();
  193. $body = str_replace("$1", $name, $body);
  194. if ($newkey != '') {
  195. $body .= $config->absurl."track.php?key=".$newkey."\n";
  196. }
  197. $email = $config->email;
  198. $mail = new PHPMailer();
  199. $mail->IsSMTP();
  200. $mail->SMTPAuth = true;
  201. // SMTP username
  202. $mail->Host = $config->smtp_host;
  203. $mail->Username = $config->smtp_user;
  204. $mail->Password = $config->smtp_password;
  205. $mail->From=$config->trackemail;
  206. $mail->AddAddress($email,'');
  207. $mail->Subject=$subject;
  208. $mail->Body=$body;
  209. if(!$mail->Send()) {
  210. echo "Message could not be sent. <p>";
  211. echo "Mailer Error: " . $mail->ErrorInfo;
  212. exit;
  213. }
  214. }
  215. }
  216. ?>