Completed
Push — master ( 7701ed...c3eede )
by Patrick
03:23
created

index.php ➔ hasUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
1
<?php
2
require_once('class.FlipREST.php');
3
require_once('class.AuthProvider.php');
4
5 View Code Duplication
if($_SERVER['REQUEST_URI'][0] == '/' && $_SERVER['REQUEST_URI'][1] == '/')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
6
{
7
    $_SERVER['REQUEST_URI'] = substr($_SERVER['REQUEST_URI'], 1);
8
}
9
10
require('login.php');
11
require('users.php');
12
require('pending_users.php');
13
require('sessions.php');
14
require('areas.php');
15
require('groups.php');
16
require('aws.php');
17
18
$app = new FlipREST();
19
$app->get('(/)', 'service_root');
20
$app->get('/\$metadata', 'metadata');
21
$app->post('/login', 'login');
22
$app->post('/logout', 'logout');
23
$app->group('/users', 'users');
24
$app->group('/groups', 'groups');
25
$app->group('/zip', 'postalcode');
26
$app->group('/pending_users', 'pending_users');
27
$app->group('/sessions', 'sessions');
28
$app->group('/areas', 'areas');
29
$app->group('/aws', 'aws');
30
$app->get('/leads', 'leads');
31
$app->post('/leads', 'addLead');
32
33
function hasUser($app)
34
{
35
    return ($app->user || $app->isLocal);
36
}
37
38
function isAdmin($app)
39
{
40
    return ($app->isLocal || $app->user->isInGroupNamed('LDAPAdmins'));
41
}
42
43
function service_root()
44
{
45
    global $app;
46
    $res = array();
47
    $res['@odata.context'] = $app->request->getUrl().$app->request->getRootUri().'/$metadata';
48
    $res['value'] = array(
49
        array('name'=>'users', 'kind'=>'EntitySet', 'url'=>'users')
50
        //array('name'=>'groups', 'kind'=>'EntitySet', 'url'=>'groups'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
89% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
51
        //array('name'=>'pending_users', 'kind'=>'EntitySet', 'url'=>'pending_users'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
89% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
        //array('name'=>'sessions', 'kind'=>'EntitySet', 'url'=>'sessions'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
89% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
53
        //array('name'=>'areas', 'kind'=>'EntitySet', 'url'=>'areas'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
89% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
54
        //array('name'=>'leads', 'kind'=>'EntitySet', 'url'=>'leads')
0 ignored issues
show
Unused Code Comprehensibility introduced by
88% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
55
    );
56
    echo json_encode($res);
57
}
58
59
function metadata()
60
{
61
    global $app;
62
    $app->fmt = 'passthru';
63
    $app->response->headers->set('Content-Type', 'application/xml;charset=utf-8');
64
    echo '
65
        <edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
66
            <edmx:DataServices>
67
                <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="BurningFlipside.Profiles">
68
                    <EntityType Name="User">
69
                        <Key>
70
                            <PropertyRef Name="uid"/>
71
                        </Key>
72
                        <Property Name="uid" Type="Edm.String" Nullable="false">
73
                            <Annotation Term="Org.OData.Core.V1.Permissions">
74
                                <EnumMember>Org.OData.Core.V1.Permission/Read</EnumMember>
75
                            </Annotation>
76
                        </Property>
77
                        <Property Name="displayName" Type="Edm.String"/>
78
                        <Property Name="mail" Type="Edm.String" Nullable="false">
79
                        </Property>
80
                    </EntityType>
81
                    <EntitySet Name="Users" EntityType="BurningFlipside.Profiles.User">
82
                        <NavigationPropertyBinding Path="users" Target="Users"/>
83
                        <Annotation Term="Org.OData.Core.V1.ResourcePath" String="users"/>
84
                        <Annotation Term="Org.OData.Capabilities.V1.NavigationRestrictions">
85
                            <Record>
86
                                <PropertyValue Property="Navigability">
87
                                    <EnumMember>Org.OData.Capabilities.V1.NavigationType/None</EnumMember>
88
                                </PropertyValue>
89
                            </Record>
90
                        </Annotation>
91
                        <Annotation Term="Org.OData.Capabilities.V1.SearchRestrictions">
92
                            <Record>
93
                                <PropertyValue Property="Searchable" Bool="true"/>
94
                                <PropertyValue Property="UnsupportedExpressions">
95
                                    <EnumMember>Org.OData.Capabilities.V1.SearchExpressions/none</EnumMember>
96
                                </PropertyValue>
97
                            </Record>
98
                        </Annotation>
99
                    </EntitySet>
100
                    <Singleton Name="Me" Type="BurningFlipside.Profiles.User">
101
                        <Annotation Term="Org.OData.Core.V1.ResourcePath" String="me"/>
102
                    </Singleton>
103
                </Schema>
104
            </edmx:DataServices>
105
        </edmx:Edmx>
106
    ';
107
}
108
109
function validate_post_code()
110
{
111
    global $app;
112
    $obj = $app->request->params();
113
    if($obj === null || count($obj) === 0)
114
    {
115
        $body = $app->request->getBody();
116
        $obj  = json_decode($body);
117
        $array = array('c' => $obj->c, 'postalCode'=>$obj->postalCode);
118
        $obj = $array;
119
    }
120
    if($obj['c'] == 'US')
121
    {
122
        if(preg_match("/^([0-9]{5})(-[0-9]{4})?$/i", $obj['postalCode']))
123
        {
124
            $contents = file_get_contents('http://ziptasticapi.com/'.$obj['postalCode']);
125
            $resp = json_decode($contents);
126
            if(isset($resp->error))
127
            {
128
                json_encode($resp->error);
129
            }
130
            else
131
            {
132
                json_encode(true);
133
            }
134
        }
135
        else
136
        {
137
            json_encode('Invalid Zip Code!');
138
        }
139
    }
140
    else
141
    {
142
        json_encode(true);
143
    }
144
}
145
146
function leads()
147
{
148
    global $app;
149
    if(!$app->user)
150
    {
151
        throw new Exception('Must be logged in', ACCESS_DENIED);
152
    }
153
    if(!$app->user->isInGroupNamed('Leads') && !$app->user->isInGroupNamed('CC') && !$app->user->isInGroupNamed('AFs'))
154
    {
155
        throw new Exception('Must be Lead', ACCESS_DENIED);
156
    }
157
    $params = $app->request->params();
158
    $auth = AuthProvider::getInstance();
159
    $leads = array();
160
    if(!isset($params['type']))
161
    {
162
        $leadGroup = $auth->getGroupByName('Leads');
163
        $aarGroup  = $auth->getGroupByName('AAR');
164
        $afGroup   = $auth->getGroupByName('AFs');
165
        $ccGroup   = $auth->getGroupByName('CC');
166
        $leads     = array_merge($leads, $leadGroup->members(true, false));
167
        $leads     = array_merge($leads, $aarGroup->members(true, false));
168
        $leads     = array_merge($leads, $afGroup->members(true, false));
169
        $leads     = array_merge($leads, $ccGroup->members(true, false));
170
    }
171
    else
172
    {
173
        switch($params['type'])
174
        {
175 View Code Duplication
            case 'aar':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
176
                $aarGroup  = $auth->getGroupByName('AAR');
177
                $leads     = array_merge($leads, $aarGroup->members(true, false));
178
                break;
179
            case 'af':
180
                $afGroup   = $auth->getGroupByName('AFs');
181
                $leads     = array_merge($leads, $afGroup->members(true, false));
182
                break;
183
            case 'cc':
184
                $ccGroup   = $auth->getGroupByName('CC');
185
                $leads     = array_merge($leads, $ccGroup->members(true, false));
186
                break;
187 View Code Duplication
            case 'lead':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
188
                $leadGroup = $auth->getGroupByName('Leads');
189
                $leads     = array_merge($leads, $leadGroup->members(true, false));
190
                break;
191
            default:
192
                $filter    = new \Data\Filter('ou eq '.$params['type']);
193
                $leads     = $auth->getUsersByFilter($filter);
194
                break;
195
        }
196
    }
197
    if($app->odata->select !== false)
198
    {
199
        $select = array_flip($app->odata->select);
200
        $count = count($leads);
201
        for($i = 0; $i < $count; $i++)
202
        {
203
            $leads[$i] = array_intersect_key($leads[$i]->jsonSerialize(), $select);
204
        }
205
    }
206
    echo json_encode($leads);
207
}
208
209
function postalcode()
210
{
211
    global $app;
212
    $app->post('', 'validate_post_code');
213
}
214
215 View Code Duplication
function addLead()
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
216
{
217
    global $app;
218
    if(!$app->user)
219
    {
220
        throw new Exception('Must be logged in', ACCESS_DENIED);
221
    }
222
    if(!$app->user->isInGroupNamed('LDAPAdmins'))
223
    {
224
        throw new Exception('Must be LDAPAdmins', ACCESS_DENIED);
225
    }
226
    $body = $app->request->getBody();
227
    $obj  = json_decode($body);
228
    $data_set = DataSetFactory::getDataSetByName('profiles');
229
    $data_table = $data_set['position'];
230
    $ret = $data_table->create($obj);
231
    echo json_encode($ret);
232
}
233
234
$app->run();
235
?>
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...
236