· 7 years ago · Jan 09, 2019, 02:38 PM
1<?php
2
3if(isset($_GET["id"])){
4 include 'db_connection.php';
5 include 'helpers.php';
6
7 // Get the Client IP
8 $ip = $_SERVER['REMOTE_ADDR'];
9 // Get id param as string
10 $key = htmlspecialchars($_GET["id"]);
11 $key = strtoupper($key);
12 // Check if id param is bad
13 if(!preg_match("/^([A-Z0-9]){23}+$/", $key))
14 return Redirect('errorPage', 'loso zadaden key: ili ne e tocno 23karakteri ili sodrzi nesto drugo od bukvi i brojki');
15 // Open conection with db
16 $conn = OpenCon();
17 // If Table Exists
18 if($conn->query("DESCRIBE cache")) {
19 $response = checkRewardsUrl($key);
20 IF(!isset($response->subscriberId)){
21 return Redirect('errorPage', 'No valid id in url');
22 }
23
24 //Set the parameters returned from WS
25 $subscriberId = $response->subscriberId;
26 $msisdn = $response->msisdn;
27 $offer1 = $response->offer1;
28 $offer2 = $response->offer2;
29 $offer3 = $response->offer3;
30 $validTo = $response->validTo;
31 $pos = strrpos($validTo, "T");
32 $validTo = substr($validTo, 0, $pos);
33 // Check if is still valid date
34 if(date("Y-m-d") > $validTo){
35 return Redirect('errorPage', 'the current id is not anymore valid');
36 }
37 // SQL to check if existing client
38 $sql = "SELECT count(*) as `test`
39 FROM `cache`
40 WHERE UPPER(`key`)='" . strtoupper($response->subscriberId) . "'";
41 // Check if query is successfully executed
42 $result = $conn->query($sql) or die($conn->error);
43 // Fetch the returned rows
44 $row = $result->fetch_assoc();
45 IF($row['test']==1){
46 echo "existing client";
47 }
48 else{
49 echo "new client";
50
51 // sql to insert record when first time page loaded
52 $sql = "INSERT INTO `cache`(`key`,`offer1`,`offer2`,`offer3`,`valid_to`)
53 VALUES('" . $subscriberId . "','" . $offer1 . "','" . $offer2 . "','" . $offer3 . "','" . $validTo . "')";
54 if($conn->query($sql) === FALSE){
55 die("error inserting into table cache " . $conn->error);
56 }
57 }
58 echo "table exists";
59 }
60 // ELSE IF Table Does Not Exists - First Page Run
61 else {
62 $response = checkRewardsUrl($key);
63 IF(isset($response->subscriberId)){
64 // sql to create cache table
65 $sql = "CREATE TABLE cache (
66 `id` INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
67 `key` VARCHAR(50),
68 `offer1` VARCHAR(50),
69 `offer2` VARCHAR(50),
70 `offer3` VARCHAR(50),
71 `valid_to` DATE
72 )";
73 // Check if cache table is successfully created
74 if ($conn->query($sql) === FALSE) {
75 die("error creating table " . $conn->error);
76 }
77 // sql to crete companies table
78 $sql = "CREATE TABLE companies (
79 `company_id` INT(6) PRIMARY KEY,
80 `company_name` VARCHAR(100),
81 `description` VARCHAR(500),
82 `img_src` VARCHAR(500)
83 )";
84
85 // Check if companies table is successfully created
86 if ($conn->query($sql) === FALSE) {
87 die("error creating table " . $conn->error);
88 }
89
90 $companies = ['TEHNOMARKET' => 'ova e tehnomarket',
91 'NEPTUN' => 'ova e neptun',
92 'AMC' => 'ova e amc'];
93 $i = 1;
94 foreach($companies as $key => $value){
95 // sql to insert records when first time page loaded
96 $sql = "INSERT INTO `companies`(`company_id`,`company_name`,`description`,`img_src`)
97 VALUES('" . $i . "','" . $key . "','" . $value . "','C:\\Users\\vasko.dojchinov\\Desktop\\importLogs\\errorExample.png')";
98 if($conn->query($sql) === FALSE){
99 die("error inserting into table cache " . $conn->error);
100 }
101 $i++;
102 }
103 //Set the parameters returned from WS
104 $subscriberId = $response->subscriberId;
105 $msisdn = $response->msisdn;
106 $offer1 = $response->offer1;
107 $offer2 = $response->offer2;
108 $offer3 = $response->offer3;
109 $validTo = $response->validTo;
110 $pos = strrpos($validTo, "T");
111 $validTo = substr($validTo, 0, $pos);
112 // sql to insert record when first time page loaded
113 $sql = "INSERT INTO `cache`(`key`,`offer1`,`offer2`,`offer3`,`valid_to`)
114 VALUES('" . $subscriberId . "','" . $offer1 . "','" . $offer2 . "','" . $offer3 . "','" . $validTo . "')";
115 if($conn->query($sql) === FALSE){
116 die("error inserting into table cache " . $conn->error);
117 }
118 }
119 else {
120 die("No valid id in url");
121 }
122 }
123 CloseCon($conn);
124}
125 else {
126 die("Error");
127 }
128?>
129<html>
130<head>
131</head>
132<body>
133
134<h1> Test Connection <?=$subscriberId?></h1>
135
136</body>
137</html>