Guest User

Untitled

a guest
Jan 6th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.69 KB | None | 0 0
  1. <?php
  2. mysql_connect("localhost","kodknatte_root","xxx");
  3. mysql_select_db("kodknatte_derp");
  4. require_once('idna_convert.class.php');
  5. class Search {
  6.     private $query;
  7.    
  8.     private $searchBar;
  9.     private $startDate;
  10.     private $endDate;
  11.     private $minmax;
  12.     private $length;
  13.     public $obj;
  14.    
  15.     private $error_handler = array();
  16.    
  17.     public function __construct($query, $searchBar = "", $startDate = "", $endDate = "", $minmax = "", $length = 0) {
  18.         $this->query = $query;
  19.         $this->searchBar = trim($searchBar);
  20.         $this->startDate = $startDate;
  21.         $this->endDate = $endDate;
  22.         $this->minmax = $minmax;
  23.         $this->length = $length;
  24.        
  25.         $this->db_host = "localhost";
  26.         $this->db_user = "kodknatte_root";
  27.         $this->db_pass = "xxx";
  28.         $this->db_name = "kodknatte_derp";
  29.        
  30.         $this->link = false;
  31.         $obj = new idna_convert(array('idn_version' => 2008));
  32.     }
  33.    
  34.     public function decode_idn($domain) {
  35.         return $obj->decode($domain);
  36.     }
  37.    
  38.     public function handleError($error) {
  39.         if(is_string($error)) {
  40.             $this->error_handler[] = $error;
  41.         }
  42.     }
  43.  
  44.     public function showErrors() {
  45.         if(is_array($this->error_handler)) {
  46.             for($i=0;$i<count($this->error_handler);$i++) {
  47.                 echo $this->error_handler[$i]."<br />";
  48.             }
  49.         }
  50.     }
  51.    
  52.     public function writeDate($date) {
  53.         $dated = date("Y-m-d");
  54.         if(date("Y-m-d") > $date)
  55.             return "<span style='color:green;font-weight:bold;'>Ledig!</span>";
  56.         elseif(date("Y-m-d") == $date)
  57.             return "<span style='color:green;font-weight:bold;'>Idag!</span>";
  58.         elseif(date("Y-m-d",strtotime("+30 days",strtotime($dated))) < $date)
  59.             return "<span style='color:red'>".$date."</span>";
  60.         else
  61.             return $date;
  62.     }
  63.    
  64.     public function printRecords() {
  65.         if($this->doQuery() && count($this->error_handler) == 0) {
  66.             $query = $this->doQuery();
  67.             echo "<table style='width:300px'>";
  68.             while($r = mysql_fetch_array($query)) {
  69.                 echo "<tr><td align='left'>".$this->decode_idn($r['name'])."</td><td align='right'>".$this->writeDate($r['date'])."</td></tr>";
  70.             }
  71.             echo "<table>";
  72.         }
  73.     }
  74.    
  75.     public function doQuery() {
  76.         $this->link = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
  77.         if(!$this->link) {
  78.             $this->handleError(mysql_error());
  79.             return false;
  80.         }
  81.         else
  82.         {
  83.             $link_db = mysql_select_db($this->db_name, $this->link);
  84.             if(!$link_db) {
  85.                 $this->handleError(mysql_error());
  86.                 return false;
  87.             }
  88.             else
  89.             {
  90.                 $query = mysql_query($this->query);
  91.                 if(!$query) {
  92.                     $this->handleError(mysql_error(). $this->query);
  93.                     return false;
  94.                 }
  95.                 else
  96.                     return $query;
  97.             }  
  98.         }
  99.     }
  100.    
  101.     public function buildQuery() {
  102.         if($this->setKeywords() == false && count($this->error_handler) == 0) {
  103.             if($this->setDate() == false) {
  104.                 $this->query = $this->query." date = '".date("Y-m-d")."'";
  105.                 return true;
  106.             }
  107.             else
  108.             {
  109.                 $this->query = $this->query." ".$this->setDate();
  110.                 return true;
  111.             }
  112.         }
  113.         elseif($this->setKeywords() && count($this->error_handler) == 0) {
  114.             if($this->setDate()) {
  115.                 $this->query = $this->query." (".$this->setKeywords().") AND (".$this->setDate().")";
  116.                 return true;
  117.             }
  118.             else
  119.             {
  120.                 $this->query = $this->query." ".$this->setKeywords();
  121.                 return true;
  122.             }
  123.         }
  124.     }
  125.    
  126.     public function setKeywords() {
  127.         if(strlen($this->searchBar) == 0) {
  128.             return false;
  129.         }
  130.         else
  131.         {
  132.             $keywords = explode(" ", $this->searchBar);
  133.             if(count($keywords) < 2) {
  134.                 if(strlen($keywords[0]) >= 2)  
  135.                     return "name LIKE '%".$keywords[0]."%'";
  136.                 else {
  137.                     handleError("Sökordet måste vara minst 2 tecken långt");
  138.                     return false;
  139.                 }
  140.             }
  141.             else
  142.             {
  143.                 if(preg_match('/\s/', $this->searchBar)) {
  144.                     $query = "";
  145.                     for($i=0;$i<count($keywords);$i++) {
  146.                         if(strlen($keywords[$i]) < 2) {
  147.                             handleError("Sökord måste vara minst 2 tecken långa");
  148.                             return false;
  149.                         }
  150.                         else
  151.                         {
  152.                             $query .= "name LIKE '%".trim($keywords[$i])."%' OR ";
  153.                         }
  154.                     }
  155.                     return substr($query,0,(strlen($query)-3));
  156.                 }
  157.             }
  158.         }
  159.     }
  160.    
  161.     public function setDate() {
  162.         if(strlen(trim($this->startDate)) > 0 && strlen(trim($this->endDate)) > 0) {
  163.             if($this->startDate < $this->endDate) {
  164.                 if(!preg_match("#^((19|20)?[0-9]{2}[- /.](0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01]))*$#", $this->startDate) && !preg_match("#^((19|20)?[0-9]{2}[- /.](0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01]))*$#", $this->endDate)) {
  165.                     handleError("Datumen är i fel format");
  166.                     return false;
  167.                 }
  168.                 else
  169.                 {
  170.                     $query = "date >= '{$this->startDate}' AND date <= '{$this->endDate}'";
  171.                     return $query;
  172.                 }
  173.             }
  174.             else
  175.             {
  176.                 $this->handleError("Startdatumet kan inte vara mindre än slutdatumet");
  177.                 return false;
  178.             }
  179.         }
  180.         elseif(strlen(trim($this->startDate)) > 0 && strlen(trim($this->endDate)) == 0) {
  181.             if(!preg_match("#^((19|20)?[0-9]{2}[- /.](0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01]))*$#", $this->startDate)) {
  182.                 handleError("Start-datumet är i fel format");
  183.                 return false;
  184.             }
  185.             else
  186.             {
  187.                 $query = "date >= '{$this->startDate}'";
  188.                 return $query;
  189.             }
  190.         }
  191.         elseif(strlen(trim($this->startDate)) == 0 && strlen(trim($this->endDate)) > 0) {
  192.             if(!preg_match("#^((19|20)?[0-9]{2}[- /.](0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01]))*$#", $this->startDate)) {
  193.                 handleError("Slut-datumet är i fel format");
  194.                 return false;
  195.             }
  196.             else
  197.             {
  198.                 $query = "date <= '{$this->endDate}'";
  199.                 return $query;
  200.             }
  201.         }
  202.         elseif(strlen(trim($this->startDate)) == 0 && strlen(trim($this->endDate)) == 0) {
  203.             return false;
  204.         }
  205.     }
  206. }
  207. $obj = new Search("SELECT * FROM domains WHERE", "", "2011-11-02", "2011-11-09", "min", 6);
  208. $obj->buildQuery();
  209. $obj->printRecords();
  210. $obj->showErrors();
  211. ?>
Add Comment
Please, Sign In to add comment