PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/qmigo_sendsms.php

https://github.com/deviltry/qmigo
PHP | 266 lines | 153 code | 56 blank | 57 comment | 10 complexity | a8951e632c4fde4c7629589c8d218808 MD5 | raw file
  1. <?php
  2. // Get our DB info
  3. require "info.php";
  4. // Include the PHP TwilioRest library
  5. require "twilio.php";
  6. ini_set('display_errors', true);
  7. ini_set('display_startup_errors', true);
  8. error_reporting(E_ALL);
  9. # Connect to the database.
  10. #########################################################
  11. $connection = mysql_connect($mySqlHostname, $mySqlUsername, $mySqlPassword);
  12. if (!$connection)
  13. {
  14. die("Error " . mysql_errno() . " : " . mysql_error());
  15. }
  16. # Select the DB
  17. $db_selected = mysql_select_db($mySqlDatabase, $connection);
  18. if (!$db_selected)
  19. {
  20. die("Error " . mysql_errno() . " : " . mysql_error());
  21. }
  22. $time = time(); //server time
  23. //$timeUnix = strtotime($time); // server time in Unix Time
  24. # 1. Generate All the Offers in the SQL Query that are not past current time, haven't been sent
  25. #####################################################################
  26. $SqlStatement = "SELECT * FROM `qmigo_offers` WHERE `offer_time` >= '". $time ."' AND sms_sent = FALSE";
  27. # Get the LATEST OFFER INFO query on the database through the connection
  28. $result = mysql_query($SqlStatement,$connection);
  29. if (!$result)
  30. {
  31. die("Error " . mysql_errno() . " : " . mysql_error());
  32. }
  33. if ($row = mysql_fetch_array($result,MYSQL_ASSOC))
  34. {
  35. $offer_expire = $row['offer_expire']; //offer expire time
  36. }
  37. else
  38. {
  39. print "sorry, no offers";
  40. exit;
  41. }
  42. # Only Offers Not SMS-d come up in SQL search , don't pass old offers
  43. #######################################################################
  44. $offer_name = ""; // SELECTS LATEST OFFER BASED ON WHAT's IN DATABASE
  45. echo "Server Time: " . $time;
  46. $expiredTimeUnix = strtotime($offer_expire);
  47. echo "$offer_expire: Expire " . $expiredTimeUnix;
  48. # Only Offers Not SMS-d come up in SQL search
  49. # Cron Job runs script 1x a minute
  50. #########################################################
  51. $SqlStatement1 = "SELECT * FROM `qmigo_offers` WHERE `offer_time` >= '". $time ."' AND `offer_expire` >= '". $expiredTimeUnix ."' AND sms_sent = FALSE";
  52. # Get the LATEST OFFER INFO query on the database through the connection
  53. $result1 = mysql_query($SqlStatement1,$connection);
  54. if (!$result1)
  55. {
  56. die("Error " . mysql_errno() . " : " . mysql_error());
  57. }
  58. if ($row1 = mysql_fetch_array($result1,MYSQL_ASSOC))
  59. {
  60. $offer_id = $row1['id'];
  61. $offer_name = $row1['offer']; // name of offer
  62. $offer_expire = $row1['offer_expire']; //offer expire row
  63. $start_time = $row1['offer_time']; //offer start-time row
  64. # Run the LATEST VENDOR INFO + OFFER INFO query on the database connection
  65. $SqlStatement2 = "Select o.id, o.offer, unix_timestamp(o.offer_expire) AS offer_expire, v.venue, v.venue_streetaddress, v.venue_city, v.venue_state, v.venue_zipcode,
  66. v.venue_phone FROM qmigo_offers o, qmigo_vendors v WHERE v.id = o.vendor_id AND o.id = $offer_id LIMIT 1";
  67. // PICK SPECIFIC OFFER AND PICK SPECIFIC VENDOR FROM THAT OFFER
  68. $result2 = mysql_query($SqlStatement2,$connection);
  69. if ($row2 = mysql_fetch_array($result2, MYSQL_ASSOC))
  70. {
  71. $venue = $row2['venue'];
  72. }
  73. // load all users in database // sql query
  74. $SqlStatement3 = "SELECT id, firstname, phone, hasguests FROM qmigo_members";
  75. $result3 = mysql_query($SqlStatement3,$connection);
  76. print "what's up?";
  77. while ($row3 = mysql_fetch_array($result3, MYSQL_ASSOC))
  78. {
  79. $member_id = $row3['id'];
  80. $member_firstname = $row3['firstname'];
  81. $phone = $row3['phone'];
  82. $hasguests = $row3['hasguests'];
  83. // Troubleshooting Code Printouts
  84. // print("START NEW LOOP: <br />");
  85. // print($member_id);
  86. // print("<br />");
  87. // print($offer_id);
  88. // print("<br />");
  89. $SqlStatement4 = "INSERT into qmigo_status (member_id, offer_id, QR_status) VALUES ($member_id, $offer_id, 0);";
  90. mysql_query($SqlStatement4,$connection) or die(mysql_error());
  91. //set QR status to 0.
  92. $status_id = mysql_insert_id(); // passing this value into guest interaction
  93. $qmigo_url = "http://www.qmigo.com/mobileoffer.php?id=". $member_id . "&o=" . $offer_id;
  94. // Call the Twilio function to get the members blasted.
  95. sendTextToMe($offer_name, $member_firstname, $venue, $start_time, $phone, $qmigo_url);
  96. //troubleshoot printout
  97. echo "check for guests";
  98. if($hasguests == 1)
  99. {
  100. echo "do the guest stuff";
  101. // Select all the guests that a member will add.
  102. // for each guest, send them the sms.
  103. // interaction only happens with the guests.
  104. // Brian needs to make sure all his contacts are going into the correct tables when offer is created.
  105. // When brian's guest info is added in.
  106. // Need to do SELECT statement and get them updated...
  107. $SqlStatement5 = "SELECT id, firstname, guest_phone, offer_id FROM qmigo_guests WHERE offer_id = " . $offer_id;
  108. $result5 = mysql_query($SqlStatement5,$connection);
  109. //troubleshoot printout
  110. print "running this sql, sqlstatement5: " . $SqlStatement5 . "\n\n";
  111. while ($row5 = mysql_fetch_array($result5, MYSQL_ASSOC))
  112. {
  113. //troubleshoot printout
  114. print "in loop:" . print_r($row5) . "\n\n";
  115. $guest_id = $row5['id'];
  116. $guest_firstname = $row5['firstname'];
  117. $guest_phone = $row5['guest_phone'];
  118. $offer_id = $row5['offer_id'];
  119. $member_id;
  120. $member_firstname;
  121. // mysql_query($SqlStatement5,$connection) or die(mysql_error());
  122. // //set QR status to 0.
  123. $qmigo_guest_url = "http://www.qmigo.com/mobileoffer.php?gid=". $guest_id . "&o=" . $offer_id;
  124. //
  125. sendTextToGuests($offer_name, $guest_firstname, $member_firstname, $venue, $start_time, $guest_phone,
  126. $qmigo_guest_url);
  127. //$qmigo_guest_url
  128. // Call the Twilio function to get the associated guests blasted.
  129. }
  130. }
  131. }
  132. }
  133. // otherwise no offers available
  134. else
  135. {
  136. echo "No new QMIGO offers! Nothing to see, move on!";
  137. }
  138. /* TWILIO INFO */
  139. /*
  140. SMS PHONENUMBER: 646-580-6934
  141. call or sms this number to test
  142. this url will be requested when somebody sends an SMS to this phone number
  143. */
  144. /*
  145. ---------------------
  146. SEND TEXT TO MEMBERS
  147. ---------------------
  148. */
  149. function sendTextToMe($member_firstname, $offer_name, $venue, $startTime, $phone, $drinkURL)
  150. {
  151. global $offer_id;
  152. global $connection;
  153. // The message to send // twilio limits to The text of the message you want to send, limited to 160 characters.//
  154. $sMessage = "Redeem: " . $member_firstname ."! A". $offer_name . " " ." at " . $venue . " " . " Visit: ". $drinkURL ;
  155. // Twilio REST API version
  156. $ApiVersion = "2010-04-01";
  157. // Set our AccountSid and AuthToken
  158. $AccountSid = "ACb26211f0fbf7301895430c95d3be5598";
  159. $AuthToken = "46194a715d2737d415646d168c90fc3c";
  160. // Instantiate a new Twilio Rest Client
  161. $client = new TwilioRestClient($AccountSid, $AuthToken);
  162. // make an associative array of all members linked to their phone #s
  163. $people = array(
  164. // "2128675309"=>"Cindy",
  165. // somehow output the database phone numbers => name
  166. $phone => $member_firstname
  167. );
  168. // Iterate over all our server admins
  169. foreach ($people as $number => $name)
  170. {
  171. // Send a new outgoinging SMS by POST'ing to the SMS resource */
  172. // YYY-YYY-YYYY must be a Twilio validated phone number
  173. $response = $client->request("/$ApiVersion/Accounts/$AccountSid/SMS/Messages",
  174. "POST", array(
  175. "To" => $number,
  176. "From" => "646-580-6934",
  177. "Body" => $sMessage
  178. //$sMessage = "Redeem: " . $member_firstname ."! ". $offerName ." at " . $venueName . " Visit: ". $drinkURL ;
  179. ));
  180. if($response->IsError)
  181. {
  182. echo "Error: {$response->ErrorMessage}";
  183. }
  184. // if no errors
  185. else
  186. {
  187. echo "Repeating SMS Message: Redeem Offer: " . $member_firstname ."! " . $offer_name." at " . $venue . ". ";
  188. //print $offer_id;
  189. }
  190. // Change socialdrinkster_offers -> sms_sent status to 1 (sent).
  191. $SqlStatement = "Update qmigo_offers SET sms_sent = 1 WHERE id = $offer_id";
  192. $result = mysql_query($SqlStatement,$connection);
  193. }
  194. }
  195. /*
  196. ---------------------
  197. SEND TEXT TO GUESTS
  198. ---------------------
  199. */
  200. ?>