summaryrefslogtreecommitdiffstats
path: root/admin/survey/classes/class.HashUrl.php
blob: 1c4e536e867ed73db9ca0464eebdd168cbe9ee9e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php 

class HashUrl 
{

	private $_anketa;
	private $_hash_length = 8;
	private $_hash_page = 'data';
	
	const PAGE_DATA = 'data';
	const PAGE_ANALYSIS = 'analysis';
	
	public function __construct($anketa = null)
	{
		global $lang;
		try {
			if (!empty($anketa) && (int)$anketa > 0) 
			{
				$this->_anketa = $anketa;
			}
			else 
			{
				throw new Exception($lang['srv_urlLinks_exception_sid']);
			}
		} catch (Exception $e) {
			die( $e->getMessage().' Exiting script!');
		}
		
		return $this;
	}

	public function hashExists($_hash)
	{

		if (!empty($_hash))
		{
			$str = "SELECT hash FROM srv_hash_url WHERE anketa='$this->_anketa' AND hash='$_hash'";
			$qry = sisplet_query($str);
			return mysqli_num_rows($qry);
		}
		return false;
	}
	
	public function getProperties($_hash)
	{

		if (!empty($_hash))
		{
			$str = "SELECT properties FROM srv_hash_url WHERE anketa='$this->_anketa' AND hash='$_hash'";
			
			$qry = sisplet_query($str);
			list($properties) = mysqli_fetch_row($qry);
			$_properties = unserialize($properties);
			if (is_array($_properties))
			{
				return $_properties;
			}
		}
		
		return array();
	}

	public function saveProperty($hash, $properties = array())
	{
		global $global_user_id;
		if (!empty($hash))
		{
			$_properties = serialize($properties);
			$str = "SELECT h.hash, h.properties, h.comment, h.page, h.add_date, u.email FROM srv_hash_url as h LEFT JOIN users AS u ON h.add_uid = u.id WHERE anketa='$this->_anketa'";
			
			$str = "INSERT INTO srv_hash_url (hash,anketa,properties,page,add_date,add_uid) VALUES"
				  ." ('{$hash}','{$this->_anketa}','{$_properties}','{$this->_hash_page}', NOW(), {$global_user_id})"
				  ." ON DUPLICATE KEY UPDATE properties = '".$_properties."'";
			$updated = sisplet_query($str);
			sisplet_query('COMMIT');
		}
		else
		{
			die($lang['srv_urlLinks_error_save']);
		}
		return $this;
	}
	
	public function getNewHash()
	{
		$hashs_in_db = array();
		$str = "SELECT hash FROM srv_hash_url WHERE anketa='$this->_anketa'";
		$qry = sisplet_query($str);
		while (list($hash_in_db) = mysqli_fetch_row($qry))
		{
			$hashs_in_db[] = $hash_in_db;
		}
		
		do 
		{
			$newHash = $this->generateHash();
		}
		while (in_array($newHash,$hashs_in_db));
		if (!empty($newHash) && $newHash != '')
		{
			return $newHash;
		}
		else
		{
			die('Can\'t generate new hash!');
		}
	}

	private function generateHash()
	{
		return substr(strtoupper(hash('md5', uniqid() )),0,$this->_hash_length);
	}
	
	public function getSurveyHashes()
	{
		$result = array();
		$str = "SELECT h.hash, h.properties, h.comment, h.refresh, h.access_password, h.page, DATE_FORMAT(h.add_date,'".STP_CALENDAR_DATE_FORMAT."') as add_date, DATE_FORMAT(h.add_date,'%H:%i') as add_date, u.email FROM srv_hash_url as h LEFT JOIN users AS u ON h.add_uid = u.id WHERE anketa='$this->_anketa' ORDER BY h.add_date DESC";
		$qry = sisplet_query($str);
		while ( list($hash,$properties,$comment,$refresh,$access_password, $page, $add_date, $add_time, $email) = mysqli_fetch_row($qry))
		{
			$result[] = array('hash'=>$hash,'properties'=>unserialize($properties), 'comment'=>$comment, 'refresh'=>$refresh, 'access_password'=>$access_password,
						'page'=>$page, 'add_date'=>$add_date, 'add_time'=>$add_time, 'email'=>$email);
		}
		
		return $result;
		
	}
	
	public function updateComment($hash,$comment)
	{
		$str = "UPDATE srv_hash_url SET comment='$comment' WHERE anketa='$this->_anketa' AND hash='$hash'";
		sisplet_query($str);
	}
        
        public function updateRefresh($hash,$refresh)
	{
		$str = "UPDATE srv_hash_url SET refresh='$refresh' WHERE anketa='$this->_anketa' AND hash='$hash'";
		sisplet_query($str);
	}
        
        public function updateAccessPassword($hash,$pass)
	{
		$str = "UPDATE srv_hash_url SET access_password='$pass' WHERE anketa='$this->_anketa' AND hash='$hash'";
		sisplet_query($str);
	}
	
	public function deleteLink($hash)
	{
		$str = "DELETE FROM srv_hash_url WHERE anketa='$this->_anketa' AND hash='$hash'";
		sisplet_query($str);
	}
	
	public function setPage($string)
	{
		if ($string == HashUrl::PAGE_ANALYSIS)
		{
			$this->_hash_page = HashUrl::PAGE_ANALYSIS; 
		}
		else
		{
			$this->_hash_page = HashUrl::PAGE_DATA;
		}
	}
        
        /**
         * Check if hashlink access password matches
         * @param type $hash - haslink id
         * @param type $pass - access password
         * @return boolean
         */
        public function CheckHashAccessPass($hash, $pass) {
            $sql = sisplet_query("SELECT access_password AS pass FROM srv_hash_url WHERE hash = '$hash'");
            if($sql){
                $row = mysqli_fetch_array($sql);
                if($row['pass'] == $pass)
                    return true;
                else
                    return false;
            }
            return false;   
	}
        
        /**
         * Check if hashlink access password exists
         * @return boolean
         */
        public function IsHashAccessPass($hash) {
            $sql = sisplet_query("SELECT access_password AS pass FROM srv_hash_url WHERE hash = '$hash'");
            if($sql){
                $row = mysqli_fetch_array($sql);
                if($row['pass'] == '' || $row['pass'] == 'NULL')
                    return false;
                else
                    return true;
            }
            return false;   
	}
        
        /**
         * Check if hashlink refresh is on
         * @param type $hash - haslink id
         * @return boolean
         */
        public function IsHashRefresh($hash) {
            $sql = sisplet_query("SELECT refresh FROM srv_hash_url WHERE hash = '$hash'");
            if($sql){
                $row = mysqli_fetch_array($sql);
                if($row['refresh'] == '1')
                    return true;
                else
                    return false;
            }
            return false;   
	}
        
        /**
         * Display from for password to access public link
         * @global type $lang
         * @param type $hash - hash id
         */
        public function HashlinkAccessPasswordForm($hash){
            global $lang, $site_url, $lang_admin; 

            header('Cache-Control: no-cache');
            header('Pragma: no-cache');
            
            echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
            echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">';
            echo '<head>';
            echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
            //echo '<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />';
            echo '<script type="text/javascript" src="'.$site_url.'admin/survey/script/js-lang.php?lang='.($lang_admin==1?'si':'en').'"></script>';
            if ($_GET['mode'] != 'old') {
			echo '<script type="text/javascript" src="'.$site_url.'admin/survey/minify/g=jsnew"></script>'."\n";
		} else {
			echo '<script type="text/javascript" src="'.$site_url.'admin/survey/minify/g=js"></script>'."\n";
		}
            echo '<link type="text/css" href="'.$site_url.'admin/survey/minify/g=css" media="screen" rel="stylesheet" />';
            echo '<link type="text/css" href="'.$site_url.'admin/survey/minify/g=cssPrint" media="print" rel="stylesheet" />';
            echo '<style>';
            echo '.container {margin-bottom:45px;} #navigationBottom {width: 100%; background-color: #f2f2f2; border-top: 1px solid gray; height:25px; padding: 10px 30px 10px 0px !important; position: fixed; bottom: 0; left: 0; right: 0; z-index: 1000;}';
            echo '</style>';
            echo '<!--[if lt IE 7]>';
            echo '<link rel="stylesheet" href="<?=$site_url?>admin/survey/css/ie6hacks.css" type="text/css" />';
            echo '<![endif]-->';
            echo '<!--[if IE 7]>';
            echo '<link rel="stylesheet" href="<?=$site_url?>admin/survey/css/ie7hacks.css" type="text/css" />';
            echo '<![endif]-->';
            echo '<!--[if IE 8]>';
            echo '<link rel="stylesheet" href="<?=$site_url?>admin/survey/css/ie8hacks.css" type="text/css" />';
            echo '<![endif]-->';
            echo '<style>';
            echo '.container {margin-bottom:45px;} #navigationBottom {width: 100%; background-color: #f2f2f2; border-top: 1px solid gray; height:25px; padding: 10px 30px 10px 0px !important; position: fixed; bottom: 0; left: 0; right: 0; z-index: 1000;}';
            echo '</style>';
            echo '</head>'."\n";
            echo '<body id="arch_body" >'."\n";
            echo '<div id="arch_body_div">';
            
            echo '<br><div style="float:left"><fieldset>';
            echo '<legend>' . $lang['srv_analiza_archive_access'] . '</legend>';

            echo '<form name="archive_access_pass_form" id="archive_access_pass_form" method="post" action="'.$site_url.'podatki/'.$this->_anketa.'/'.$hash.'/">';
            //echo '<input type="hidden" name="archive_id" value="' . $aid . '">';

            //user insertet wrong password
            if(isset($_SESSION['hashlink_access'][$hash]) && $_SESSION['hashlink_access'][$hash] == '0')
                echo '<i class="red" id="archive_access_wrong_pass_warning">' . $lang['srv_analiza_archive_access_wrong_pass'] . '</i><br>';

            echo '<br>'.$lang['srv_analiza_archive_access_password_label'].': ';
            echo '<input type="password" name="hashlink_access_pass" id="hashlink_access_pass" maxlength="25" value="" /><br><br>';

            echo '<span class="spaceRight floatLeft"><div class="buttonwrapper">'
            . '<a class="ovalbutton ovalbutton_orange btn_savesettings" href="#" onclick="$(\'#archive_access_pass_form\').submit();">';
            echo $lang['srv_analiza_archive_access_button'];
            echo '</a></div></span><br><br>';
            echo '</form></fieldset></div>';

            #izpišemo še zaključek html
            echo '</div>'."\n";
            echo '</div>'."\n";
            echo '</body>'."\n";
            echo '</html>';
        }
        
        /**
         * Just for acces with password
         */
        function checkHashlinkAccessSessionValues($hash){
            if(isset($_POST['hashlink_access_pass'])){
                if($this->CheckHashAccessPass($hash, $_POST['hashlink_access_pass']))
                    $_SESSION['hashlink_access'][$hash] = '1';
                else
                    $_SESSION['hashlink_access'][$hash] = '0';
            }
        }
}