Completed
Push — master ( 85359b...604927 )
by mains
13s
created

jodel-web.php ➔ isUserAdmin()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 2
nop 0
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 40 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
include 'php/DatabaseConnect.php';
4
include 'php/Location.php';
5
include 'php/JodelAccount.php';
6
include 'php/Jodel.php';
7
include 'php/View.php';
8
9
include 'php/Requests/AbstractRequest.php';
10
include 'php/Requests/CreateUser.php';
11
include 'php/Requests/GetPosts.php';
12
include 'php/Requests/GetKarma.php';
13
include 'php/Requests/UpdateLocation.php';
14
include 'php/Requests/Upvote.php';
15
include 'php/Requests/Downvote.php';
16
include 'php/Requests/GetPostDetails.php';
17
include 'php/Requests/SendJodel.php';
18
include 'php/Requests/GetCaptcha.php';
19
include 'php/Requests/PostCaptcha.php';
20
include 'php/Requests/GetUserConfig.php';
21
include 'php/Requests/GetChannel.php';
22
23
require_once 'php/Requests/libary/Requests.php';
24
Requests::register_autoloader();
25
26
$config = parse_ini_file('config/config.ini.php');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
27
$baseUrl = $config['Url'];
28
29
$location = new Location();
30
$location->setLat($config['default_lat']);
31
$location->setLng($config['default_lng']);
32
$location->setCityName($config['default_location']);
33
//What is dude doing with my Server?
34
if($_SERVER['REMOTE_ADDR'] == '94.231.103.52')
35
{
36
	echo('You are flooting my Server! Pls enable Cookies in your script and contact me: [email protected]');
37
	die();
38
}
39
40
function isUserBot()
0 ignored issues
show
Coding Style introduced by
isUserBot uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
41
{
42
    preg_match('/bot|spider|google|twitter/i', $_SERVER['HTTP_USER_AGENT'], $matches);
43
44
    return (isset($matches[0])) ? true : false;
45
}
46
47
function configPropertyExists($config, $property)
48
{
49
    if(!array_key_exists($property, $config) || !isset($config[$property]) || $config[$property] == '' || $config[$property] == 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
0 ignored issues
show
Coding Style introduced by
The if-else statement can be simplified to return !(!array_key_exis...xxxxxxxxxxxxxxxxxxxx');.
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 170 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
50
    {
51
        return FALSE;
52
    }
53
    else
54
    {
55
        return TRUE;
56
    }
57
}
58
function isUserAdmin() {
0 ignored issues
show
Coding Style introduced by
isUserAdmin uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
59
	global $config;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
60
	if (isset($_COOKIE['JodelAdminPassword']) && configPropertyExists($config, 'pw') && $config['pw'] == $_COOKIE['JodelAdminPassword'])
0 ignored issues
show
Coding Style introduced by
The if-else statement can be simplified to return isset($_COOKIE['J...['JodelAdminPassword'];.
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
61
	{
62
		return TRUE;
63
	}
64
	else
65
	{
66
		return FALSE;
67
	}
68
}
69
70
function isDeviceUidInDatabase($deviceUid)
71
{
72
    $db = new DatabaseConnect();  
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $db. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
73
    $result = $db->query("SELECT * FROM accounts WHERE device_uid='" . $deviceUid  . "'");
74
75
    if ($result->num_rows > 0)
0 ignored issues
show
Coding Style introduced by
The if-else statement can be simplified to return $result->num_rows > 0;.
Loading history...
76
    {
77
        return TRUE;
78
    }
79
    else
80
    {
81
        return FALSE;
82
    }
83
}
84
85
	//Check if it's a Spider or Google Bot
86
	if(configPropertyExists($config, 'botDeviceUid') && isUserBot())
87
	{
88
		error_log('Spider or Bot checked in!');
89
		
90
		$jodelAccountForView = new JodelAccount($config['botDeviceUid'], TRUE);
91
	}
92
	else
93
	{
94
		if(!isset($_COOKIE['JodelDeviceId']) || !isDeviceUidInDatabase($_COOKIE['JodelDeviceId']))
95
		{
96
			$jodelAccountForView = new JodelAccount();
97
			setcookie('JodelDeviceId', $jodelAccountForView->deviceUid, time()+60*60*24*365*10);
98
			error_log('Created account with JodelDeviceId:' . $jodelAccountForView->deviceUid .  ' for [' . $_SERVER ['HTTP_USER_AGENT'] . ']');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
99
			
100
		}
101
		else
102
		{
103
			$jodelAccountForView = new JodelAccount($_COOKIE['JodelDeviceId']);
104
		}
105
		
106
	}
107
	
108
	if(configPropertyExists($config, 'karmaDeviceUid'))
109
    {
110
    	$jodelAccountForKarma = new JodelAccount($config['karmaDeviceUid']);
111
    }
112
    else
113
    {
114
    	error_log("No Karma deviceUid set in config file");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal No Karma deviceUid set in config file does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
115
		$jodelAccountForKarma = new JodelAccount($deviceUid);
116
    }
117
118
	/*
119
	 * Cunstruct View
120
	 */
121
122
	$hashtag = '';
123
124
	if(isset($_GET['search']))
125
	{
126
		if(substr($_GET['search'], 0, 1) === "#")
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal # does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
127
		{
128
			if(strrpos($_GET['search'], ' ') == NULL)
129
			{
130
				$hashtag = substr($_GET['search'], 1);
131
			}
132
			else
133
			{
134
				$hashtag = substr($_GET['search'], 1, strrpos($_GET['search'], ' '));
135
136
				$city = substr($_GET['search'],
137
						strrpos($_GET['search'], ' ') + 1,
138
						strlen($_GET['search']));
139
140
				if($city != NULL && $city != '')
141
				{
142
					$_GET['city'] = $city;
143
				}
144
			}
145
		}
146
		else
147
		{
148
			$_GET['city'] = $_GET['search'];
149
		}
150
	}
151
	$hashtag = trim($hashtag);
152
153
	if($hashtag == '')
154
	{
155
		if(isset($_GET['hashtag']))
156
		{
157
			$hashtag = $_GET['hashtag'];
158
		}
159
		else
160
		{
161
			$hashtag = '#all';
162
		}
163
	}
164
	
165
	//Set Location
166
	if(isset($_GET['city']) && !$jodelAccountForView->locationEquals($_GET['city']))
167
	{
168
		$cityName = $jodelAccountForView->setLocation();
169
	}
170
	else
171
	{
172
		$cityName = $jodelAccountForView->location->getCityName();
173
	}
174
175
176
	
177
	if(isset($_GET['view']))
178
	{
179
		switch ($_GET['view']) {
180
			case 'discussed':
181
				$view = 'discussed';
182
				break;
183
			
184
			case 'popular':
185
				$view = 'popular';
186
				break;
187
188
			default:
189
				$view = 'combo';
190
				break;
191
		}
192
	}
193
	else
194
	{
195
		$view = 'combo';
196
	}
197
198
	if(isset($_GET['postId']))
199
	{
200
		$view = new View($baseUrl, 'DE', $cityName, $hashtag, $view, $_GET['postId']);
201
	}
202
	else
203
	{
204
		$view = new View($baseUrl, 'DE', $cityName, $hashtag, $view);
205
	}
206
	
207
	$newPositionStatus = '';
208
	if($hashtag != '#all')
209
	{
210
		$newPositionStatus = '#' . $hashtag . ' ';
211
	}
212
	$newPositionStatus .= $cityName;
213
214
	//Verify Account
215
	if(isset($_GET['solution']) && isset($_GET['key']) && isset($_GET['deviceUid']))
216
	{
217
		$jodelAccountForVerify = new JodelAccount($_GET['deviceUid']);
218
		$jodelAccountForVerify->verifyCaptcha();
219
	}
220
	
221
	//Vote
222
	if(isset($_GET['vote']) && isset($_GET['postId']))
223
	{
224
		$jodelAccountForKarma->votePostId($_GET['postId'], $_GET['vote']);
225
		if(isset($_GET['getPostDetails']) && isset($_GET['getPostDetails']))
226
        {
227
            header('Location: index.php?getPostDetails=true&postId=' . htmlspecialchars($_GET['postId_parent']) . '#postId-' . htmlspecialchars($_GET['postId']));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 162 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
228
        }
229
        else
230
        {
231
            header("Location: index.php#postId-" . htmlspecialchars($_GET['postId']));
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Location: index.php#postId- does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
232
        }   
233
        die();
234
	}
235
	
236
	//SendJodel
237
	if(isset($_POST['message']))
238
	{
239
		$jodelAccountForKarma->sendJodel($jodelAccountForView->location, $view);
240
	}
241
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...