Advertisement
epheterson

TXT TAG - functions.php

Dec 9th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.07 KB | None | 0 0
  1. <?php
  2.  
  3. function connectDB(){
  4.     $server="localhost";
  5.     $user="root";
  6.  
  7.  
  8.     // Enable for TXT TAG Live
  9.     $password="L0ckTh1$@8";
  10.     $database="good_tt";
  11.  
  12.  
  13.     // Enable for TXT TAG Dev
  14.     //$password="CHUPra7e";
  15.     ///$database="good_tt_dev";
  16.  
  17.     $db = new mysqli($server,$user,$password,$database);
  18.     if ($db->connect_errno) {
  19.         printf("Connect failed: %s\n", $mysqli->connect_error);
  20.         exit();
  21.     }
  22.  
  23.     return $db;
  24. }
  25.  
  26. function getStats($db)
  27. {
  28.     $q = 'SELECT COUNT(*) FROM txts';
  29.     $r=mysqli_query($db,$q) or die(mysqli_error($db)." Q=".$q);
  30.     $r = mysqli_fetch_assoc($r);
  31.     $count["msgs"] = $r['COUNT(*)'];
  32.  
  33.     $q = 'SELECT COUNT(*) FROM tags';
  34.     $r=mysqli_query($db,$q) or die(mysqli_error($db)." Q=".$q);
  35.     $r = mysqli_fetch_assoc($r);
  36.     $count["tags"] = $r['COUNT(*)'];
  37.  
  38.     return $count;
  39. }
  40.  
  41. function printForm($action){
  42. // Actions include: text, view, claim, remind, leave, contact
  43. ?>
  44.     <form action="<?php echo "./includes/process.php"; ?>" method="post">
  45.     <input type="hidden" name="action" value="<? echo $action ?>" />
  46.     <input type="hidden" name="page_instance_id" value="<?php echo end($_SESSION['page_instance_ids']) ?>" />
  47.     <span class="confirmation-field"><input type="text" name="robot" value="" /></span>
  48.  
  49.     <? if ($action != "remind" && $action != "contact") {?>
  50.     <p><select id="state" name="state" size="1">
  51.     <option value="AL">Alabama</option>
  52.     <option value="AK">Alaska</option>
  53.     <option value="AZ">Arizona</option>
  54.     <option value="AR">Arkansas</option>
  55.     <option value="CA">California</option>
  56.     <option value="CO">Colorado</option>
  57.     <option value="CT">Connecticut</option>
  58.     <option value="DE">Delaware</option>
  59.     <option value="FL" selected>Florida</option>
  60.     <option value="GA">Georgia</option>
  61.     <option value="HI">Hawaii</option>
  62.     <option value="ID">Idaho</option>
  63.     <option value="IL">Illinois</option>
  64.     <option value="IN">Indiana</option>
  65.     <option value="IA">Iowa</option>
  66.     <option value="KS">Kansas</option>
  67.     <option value="KY">Kentucky</option>
  68.     <option value="LA">Louisiana</option>
  69.     <option value="ME">Maine</option>
  70.     <option value="MD">Maryland</option>
  71.     <option value="MA">Massachusetts</option>
  72.     <option value="MI">Michigan</option>
  73.     <option value="MN">Minnesota</option>
  74.     <option value="MS">Mississippi</option>
  75.     <option value="MO">Missouri</option>
  76.     <option value="MT">Montana</option>
  77.     <option value="NE">Nebraska</option>
  78.     <option value="NV">Nevada</option>
  79.     <option value="NH">New Hampshire</option>
  80.     <option value="NJ">New Jersey</option>
  81.     <option value="NM">New Mexico</option>
  82.     <option value="NY">New York</option>
  83.     <option value="NC">North Carolina</option>
  84.     <option value="ND">North Dakota</option>
  85.     <option value="OH">Ohio</option>
  86.     <option value="OK">Oklahoma</option>
  87.     <option value="OR">Oregon</option>
  88.     <option value="PA">Pennsylvania</option>
  89.     <option value="RI">Rhode Island</option>
  90.     <option value="SC">South Carolina</option>
  91.     <option value="SD">South Dakota</option>
  92.     <option value="TN">Tennessee</option>
  93.     <option value="TX">Texas</option>
  94.     <option value="UT">Utah</option>
  95.     <option value="VT">Vermont</option>
  96.     <option value="VA">Virginia</option>
  97.     <option value="WA">Washington</option>
  98.     <option value="DC">Washington D.C.</option>
  99.     <option value="WV">West Virginia</option>
  100.     <option value="WI">Wisconsin</option>
  101.     <option value="WY">Wyoming</option></select></p>
  102.  
  103.     <p><input type="text" size="10" name="plate" id="plate" style="text-align:center;" placeholder="ADGT09" /></p>
  104.     <? } ?>
  105.     <? if ($action == "text"){ ?>
  106.     <p><input size="50" name="text" style="text-align:center;" placeholder="I really dig your car!"></p>
  107.     <? } ?>
  108.  
  109.     <? if ($action == "contact") { ?>
  110.     <p><input type='text' name='Name' size=20 style="text-align:center;" placeholder="John Smith" /></p>
  111.     <? } ?>
  112.  
  113.     <? if ($action == "claim" || $action == "leave" || $action == "remind" || $action == "contact"){ ?>
  114.         <p><input type="text" name="email" id="mail" size="30" style="text-align:center;"  placeholder="[email protected]"></p>
  115.     <? } ?>
  116.  
  117.     <? if ($action == "contact") { ?>
  118.     <p><textarea name='text' rows=5 cols=40 placeholder="I just wanted to say..." /></textarea></p>
  119.     <? } ?>
  120.  
  121.     <? if ($action == "claim"){ ?>
  122.         <p><input type="checkbox" name="custom" id="custom" value="false">
  123.         <label for="custom"> Only receive TXT TAG's preset messages</label><br><i>Not yet functional.</i></p>
  124.         <p><input type="checkbox" name="public" id="public" value="true" checked>
  125.         <label for="public"> Continue to list my messages publicly</label></p>
  126.         <p><input type="checkbox" name="share" id="share" value="true" checked>
  127.         <label for="share"> Share my messages with similar tags.<br><b><i>Highly Recommended.</i></b></label></p>
  128.     <? } ?>
  129.  
  130.     <? if ($action == "claim" || $action == "leave" || $action == "contact"){
  131.         $first = rand(1,5);
  132.         $second = rand(1,10-$first);
  133.         $answer = $first + $second;
  134.         ?>
  135.         <p><? echo $first .' + '.$second.' = ' ?>
  136.         <input type=text name='Guess' size=2 placeholder="?" style="text-align:center;" >
  137.         <input type=hidden name='Answer' value=<? echo $answer; ?>></p>
  138.     <? } ?>
  139.  
  140.     <p><input type="submit" value="Submit"></p>
  141.     </form>
  142.  
  143.     <script type="text/javascript" >
  144.     <? if ($action != "remind") { ?>
  145.     document.getElementById('state').value = geoplugin_region();
  146.     retrieve_zip("example_callback"); // Alert the User's Zipcode
  147.     document.getElementById('plate').focus();
  148.     <? } else { ?>
  149.     document.getElementById('mail').focus();
  150.     <? } ?>
  151.     </script>
  152. <?php
  153. return;
  154. }
  155.  
  156. function effective_tag($plate) {
  157.     // http://www.deadlyroads.com/licenseplates.shtml
  158.     // Untouched: C, E, F, R, Y
  159.     // Remaining: Untouched, 0, 1, 2, 4, 5, 8, H, V
  160.  
  161.     $plate = str_replace('O', '0', $plate);
  162.     $plate = str_replace('Q', '0', $plate);
  163.     $plate = str_replace('G', '0', $plate);
  164.     $plate = str_replace('D', '0', $plate);
  165.  
  166.     $plate = str_replace('I', '1', $plate);
  167.     $plate = str_replace('L', '1', $plate);
  168.     $plate = str_replace('T', '1', $plate);
  169.     $plate = str_replace('J', '1', $plate);
  170.  
  171.     $plate = str_replace('Z', '2', $plate);
  172.     $plate = str_replace('7', '2', $plate); // Because 7 -> Z -> 2
  173.  
  174.     $plate = str_replace('A', '4', $plate);
  175.  
  176.     $plate = str_replace('6', '5', $plate);
  177.     $plate = str_replace('S', '5', $plate);
  178.  
  179.     $plate = str_replace('B', '8', $plate);
  180.     $plate = str_replace('9', '8', $plate);
  181.     $plate = str_replace('P', '8', $plate);
  182.     $plate = str_replace('R', '8', $plate);
  183.  
  184.     $plate = str_replace('M', 'H', $plate);
  185.     $plate = str_replace('N', 'H', $plate);
  186.     $plate = str_replace('W', 'H', $plate);
  187.  
  188.     $plate = str_replace('U', 'V', $plate);
  189.     $plate = str_replace('X', 'V', $plate);
  190.     $plate = str_replace('K', 'V', $plate); // Because K -> X -> V
  191.  
  192.     return $plate;
  193. }
  194.  
  195. function txtTag($db,$email,$state,$plate,$text)
  196. {
  197.     $plat3 = prettyPlate($plate);
  198.     // Format the message to prevent an SQL or HTML attack...
  199.     $t3xt = $db->real_escape_string($text);
  200.  
  201.     $subject = "Message for $state $plat3";
  202.     sendMsg($email, $subject, "<h2>$text</h2>");
  203.  
  204.     $q="INSERT INTO log (state, tag, msg, email) VALUES ('$state','$plate','$t3xt','$email')";
  205.     $r=mysqli_query($db,$q) or die(mysqli_error($db)." Q=".$q);
  206.  
  207.     $q="UPDATE tags SET numMsg = numMsg + 1 WHERE state='$state' AND tag='$plate'";
  208.     $r=mysqli_query($db,$q) or die(mysqli_error($db)." Q=".$q);
  209.  
  210.     return;
  211. }
  212.  
  213. function txtEffectiveTag($db,$email,$state,$plate_original,$plate_similar,$text)
  214. {
  215.     $plat3 = prettyPlate($plate_similar);
  216.     // Format the message to prevent an SQL or HTML attack...
  217.     $t3xt = $db->real_escape_string($text);
  218.  
  219.     $subject = "Message (kinda) for $state $plat3";
  220.     sendMsg($email, $subject, "<p>You're receiving this message because your tag ($plate_similar) is kinda sorta similar to $plate_original.</p><h2>$text</h2>");
  221.  
  222.     $q="INSERT INTO log (state, tag, msg, email) VALUES ('$state','$plate_similar','$t3xt','$email')";
  223.     $r=mysqli_query($db,$q) or die(mysqli_error($db)." Q=".$q);
  224.  
  225.     $q="UPDATE tags SET numMsg = numMsg + 1 WHERE state='$state' AND tag='$plate_similar'";
  226.     $r=mysqli_query($db,$q) or die(mysqli_error($db)." Q=".$q);
  227.  
  228.     return;
  229. }
  230.  
  231. function sendMsg($email, $subject, $message){
  232.  
  233.     // Remove this once mail works again
  234.     //return;
  235.  
  236.     $message = "<html><body><center><p><a href=\"http://www.txttag.me\" alt=\"TXT TAG\"><img src=\"http://www.txttag.me/images/txttag.png\"></a></p><hr><p>$message</p><hr><p>http://www.TXTTAG.me</p><p><small><a href='http://txttag.me/leave.php'>You can unsubscribe at any time.</a></small></p></center></body></html>";
  237.  
  238.     $headers  = 'MIME-Version: 1.0' . "\r\n";
  239.     $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  240.     $headers .= 'From: "TXT TAG" <[email protected]>' . "\r\n";
  241.     if(!mail($email,$subject,$message,$headers,'[email protected]')) {
  242.         echo "ERROR! The message did not send properly, please alert [email protected] about this error! Our apologies.";
  243.     }
  244. }
  245.  
  246. function justClaimed($db,$email,$state,$plate,$r)
  247. {
  248.     $plat3 = prettyPlate($plate);
  249.     $num = $r->num_rows;
  250.  
  251.     ob_start();
  252.     printMsgs($r);
  253.     $msg = ob_get_clean();
  254.  
  255.     $subject = "Old messages for $state $plat3";
  256.     sendMsg($email, $subject, $msg, "allow");
  257.  
  258.     $q="UPDATE tags SET numMsg = numMsg + $num WHERE state='$state' AND tag='$plate'";
  259.     $r=mysqli_query($db,$q) or die(mysqli_error($db)." Q=".$q);
  260.  
  261.     $q="INSERT INTO log (state, tag, msg, email) VALUES ('$state','$plate','TXT TAG: Sent all existing messages.','$email')";
  262.     $r=mysqli_query($db,$q) or die(mysqli_error($db)." Q=".$q);
  263.     return;
  264. }
  265.  
  266. function fbLikeBox() { ?>
  267.     <div class="fb-like-box" data-href="http://www.facebook.com/txttag" data-width="500" data-show-faces="true" data-stream="true" data-header="false"></div>
  268. <? }
  269.  
  270. function fbFacePile() { ?>
  271.     <div class="fb-like" data-href="http://www.facebook.com/txttag" data-send="true" data-width="465" data-show-faces="true" data-font="segoe ui"></div>
  272. <? }
  273.  
  274. function validEmail( $email ){
  275.     return filter_var( $email, FILTER_VALIDATE_EMAIL );
  276. }
  277.  
  278. function prettyPlate($plate)
  279. {
  280.     switch(strlen($plate))
  281.     {
  282.     case 6:
  283.         $plat3 = substr($plate, 0, 3) . " " . substr($plate, 3);
  284.         break;
  285.     case 8:
  286.         $plat3 = substr($plate, 0, 4) . " " . substr($plate, 4);
  287.         break;
  288.     default:
  289.         $plat3 = $plate;
  290.     }
  291.     return $plat3;
  292. }
  293.  
  294. function printMsgs($r) {
  295.     while($row = $r->fetch_array())
  296.         { $rows[] = $row; }
  297.  
  298.     foreach($rows as $row)
  299.     {
  300.         $date = date('l, F j, Y @ g:i A', strtotime($row['date']) );
  301.         echo "<p><b><i>" . $date . "</i></b><br>" . $row['msg'] . "</p>";
  302.     }
  303.     return;
  304. }
  305.  
  306. function xmlIt($xml) {
  307.  
  308.   // add marker linefeeds to aid the pretty-tokeniser (adds a linefeed between all tag-end boundaries)
  309.   $xml = preg_replace('/(>)(<)(\/*)/', "$1\n$2$3", $xml);
  310.  
  311.   // now indent the tags
  312.   $token      = strtok($xml, "\n");
  313.   $result     = ''; // holds formatted version as it is built
  314.   $pad        = 0; // initial indent
  315.   $matches    = array(); // returns from preg_matches()
  316.  
  317.   // scan each line and adjust indent based on opening/closing tags
  318.   while ($token !== false) :
  319.  
  320.     // test for the various tag states
  321.  
  322.     // 1. open and closing tags on same line - no change
  323.     if (preg_match('/.+<\/\w[^>]*>$/', $token, $matches)) :
  324.       $indent=0;
  325.     // 2. closing tag - outdent now
  326.     elseif (preg_match('/^<\/\w/', $token, $matches)) :
  327.       $pad--;
  328.     // 3. opening tag - don't pad this one, only subsequent tags
  329.     elseif (preg_match('/^<\w[^>]*[^\/]>.*$/', $token, $matches)) :
  330.       $indent=1;
  331.     // 4. no indentation needed
  332.     else :
  333.       $indent = 0;
  334.     endif;
  335.  
  336.     // pad the line with the required number of leading spaces
  337.     $line    = str_pad($token, strlen($token)+$pad, ' ', STR_PAD_LEFT);
  338.     $result .= $line . "\n"; // add to the cumulative result, with linefeed
  339.     $token   = strtok("\n"); // get the next token
  340.     $pad    += $indent; // update the pad size for subsequent lines
  341.   endwhile;
  342.  
  343.   return $result;
  344. }
  345.  
  346. // The following two functions were provided by Matthew Carroll
  347. // They are for returning machine-readable JSON responses
  348.  
  349. function returnError($msg)
  350. {
  351.     $response = array("success" => false,"message" => $msg);
  352.     echo json_encode($response);
  353.     exit();
  354. }
  355.  
  356. function returnSuccess($msg, $data)
  357. {
  358.     $response = array("success" => true,"message" => $msg,"data" => $data);
  359.     echo json_encode($response);
  360.     exit();
  361. }
  362.  
  363.  
  364. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement