Completed
Push — master ( ced03c...d460d6 )
by Patrick
03:17
created

index.php ➔ getLeadsWithParams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 18
rs 9.4285
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 hasLeadAccess($app)
44
{
45
    return ($app->user->isInGroupNamed('Leads') || $app->user->isInGroupNamed('CC') || $app->user->isInGroupNamed('AFs'));
46
}
47
48
function service_root()
49
{
50
    global $app;
51
    $res = array();
52
    $res['@odata.context'] = $app->request->getUrl().$app->request->getRootUri().'/$metadata';
53
    $res['value'] = array(
54
        array('name'=>'users', 'kind'=>'EntitySet', 'url'=>'users')
55
        //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...
56
        //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...
57
        //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...
58
        //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...
59
        //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...
60
    );
61
    echo json_encode($res);
62
}
63
64
function metadata()
65
{
66
    global $app;
67
    $app->fmt = 'passthru';
68
    $app->response->headers->set('Content-Type', 'application/xml;charset=utf-8');
69
    echo '
70
        <edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
71
            <edmx:DataServices>
72
                <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="BurningFlipside.Profiles">
73
                    <EntityType Name="User">
74
                        <Key>
75
                            <PropertyRef Name="uid"/>
76
                        </Key>
77
                        <Property Name="uid" Type="Edm.String" Nullable="false">
78
                            <Annotation Term="Org.OData.Core.V1.Permissions">
79
                                <EnumMember>Org.OData.Core.V1.Permission/Read</EnumMember>
80
                            </Annotation>
81
                        </Property>
82
                        <Property Name="displayName" Type="Edm.String"/>
83
                        <Property Name="mail" Type="Edm.String" Nullable="false">
84
                        </Property>
85
                    </EntityType>
86
                    <EntitySet Name="Users" EntityType="BurningFlipside.Profiles.User">
87
                        <NavigationPropertyBinding Path="users" Target="Users"/>
88
                        <Annotation Term="Org.OData.Core.V1.ResourcePath" String="users"/>
89
                        <Annotation Term="Org.OData.Capabilities.V1.NavigationRestrictions">
90
                            <Record>
91
                                <PropertyValue Property="Navigability">
92
                                    <EnumMember>Org.OData.Capabilities.V1.NavigationType/None</EnumMember>
93
                                </PropertyValue>
94
                            </Record>
95
                        </Annotation>
96
                        <Annotation Term="Org.OData.Capabilities.V1.SearchRestrictions">
97
                            <Record>
98
                                <PropertyValue Property="Searchable" Bool="true"/>
99
                                <PropertyValue Property="UnsupportedExpressions">
100
                                    <EnumMember>Org.OData.Capabilities.V1.SearchExpressions/none</EnumMember>
101
                                </PropertyValue>
102
                            </Record>
103
                        </Annotation>
104
                    </EntitySet>
105
                    <Singleton Name="Me" Type="BurningFlipside.Profiles.User">
106
                        <Annotation Term="Org.OData.Core.V1.ResourcePath" String="me"/>
107
                    </Singleton>
108
                </Schema>
109
            </edmx:DataServices>
110
        </edmx:Edmx>
111
    ';
112
}
113
114
function validate_post_code()
115
{
116
    global $app;
117
    $obj = $app->request->params();
118
    if($obj === null || count($obj) === 0)
119
    {
120
        $body = $app->request->getBody();
121
        $obj  = json_decode($body);
122
        $array = array('c' => $obj->c, 'postalCode'=>$obj->postalCode);
123
        $obj = $array;
124
    }
125
    if($obj['c'] == 'US')
126
    {
127
        if(preg_match("/^([0-9]{5})(-[0-9]{4})?$/i", $obj['postalCode']))
128
        {
129
            $contents = file_get_contents('http://ziptasticapi.com/'.$obj['postalCode']);
130
            $resp = json_decode($contents);
131
            if(isset($resp->error))
132
            {
133
                json_encode($resp->error);
134
            }
135
            else
136
            {
137
                json_encode(true);
138
            }
139
        }
140
        else
141
        {
142
            json_encode('Invalid Zip Code!');
143
        }
144
    }
145
    else
146
    {
147
        json_encode(true);
148
    }
149
}
150
151
function getLeadsByType($type, $auth)
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
152
{
153
    switch($params['type'])
0 ignored issues
show
Bug introduced by
The variable $params does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
154
    {
155
        case 'aar':
156
            $aarGroup = $auth->getGroupByName('AAR');
157
            return $aarGroup->members(true, false);
158
        case 'af':
159
            $afGroup = $auth->getGroupByName('AFs');
160
            return $afGroup->members(true, false);
161
        case 'cc':
162
            $ccGroup = $auth->getGroupByName('CC');
0 ignored issues
show
Unused Code introduced by
$ccGroup is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
163
            return $ccGrnup->members(true, false);
0 ignored issues
show
Bug introduced by
The variable $ccGrnup does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
164
        case 'lead':
165
            $leadGroup = $auth->getGroupByName('Leads');
166
            return $leadGroup->members(true, false);
167
        default:
168
            $filter = new \Data\Filter('ou eq '.$params['type']);
169
            return $auth->getUsersByFilter($filter);
170
    }
171
}
172
173
function getLeadsWithParams($params)
174
{
175
    $auth = AuthProvider::getInstance();
176
    if(isset($params['type']))
177
    {
178
        return getLeadsByType($params['type'], $auth);
179
    }
180
    $leads = array();
181
    $leadGroup = $auth->getGroupByName('Leads');
182
    $aarGroup  = $auth->getGroupByName('AAR');
183
    $afGroup   = $auth->getGroupByName('AFs');
184
    $ccGroup   = $auth->getGroupByName('CC');
185
    $leads     = array_merge($leads, $leadGroup->members(true, false));
186
    $leads     = array_merge($leads, $aarGroup->members(true, false));
187
    $leads     = array_merge($leads, $afGroup->members(true, false));
188
    $leads     = array_merge($leads, $ccGroup->members(true, false));
189
    return $leads;
190
}
191
192
function leads()
193
{
194
    global $app;
195
    if(!$app->user)
196
    {
197
        throw new Exception('Must be logged in', ACCESS_DENIED);
198
    }
199
    if(!hasLeadAccess($app))
200
    {
201
        throw new Exception('Must be Lead', ACCESS_DENIED);
202
    }
203
    $params = $app->request->params();
204
    $leads = getLeadsWithParams($params);
205
    if($app->odata->select !== false)
206
    {
207
        $leads = $app->odata->filterArrayPerSelect($leads);
208
    }
209
    echo json_encode($leads);
210
}
211
212
function postalcode()
213
{
214
    global $app;
215
    $app->post('', 'validate_post_code');
216
}
217
218 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...
219
{
220
    global $app;
221
    if(!$app->user)
222
    {
223
        throw new Exception('Must be logged in', ACCESS_DENIED);
224
    }
225
    if(!$app->user->isInGroupNamed('LDAPAdmins'))
226
    {
227
        throw new Exception('Must be LDAPAdmins', ACCESS_DENIED);
228
    }
229
    $body = $app->request->getBody();
230
    $obj  = json_decode($body);
231
    $data_set = DataSetFactory::getDataSetByName('profiles');
232
    $data_table = $data_set['position'];
233
    $ret = $data_table->create($obj);
234
    echo json_encode($ret);
235
}
236
237
$app->run();
238
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
239