Completed
Push — master ( 032e4b...251c57 )
by Patrick
02:59
created

index.php ➔ getLeadsByType()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 21
rs 8.7624
1
<?php
2
require_once('class.FlipREST.php');
3
require_once('class.AuthProvider.php');
4
5
if($_SERVER['REQUEST_URI'][0] == '/' && $_SERVER['REQUEST_URI'][1] == '/')
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 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...
147
{
148
    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...
149
    {
150
        case 'aar':
151
            $aarGroup = $auth->getGroupByName('AAR');
152
            return $aarGroup->members(true, false);
153
        case 'af':
154
            $afGroup = $auth->getGroupByName('AFs');
155
            return $afGroup->members(true, false);
156
        case 'cc':
157
            $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...
158
            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...
159
        case 'lead':
160
            $leadGroup = $auth->getGroupByName('Leads');
161
            return $leadGroup->members(true, false);
162
        default:
163
            $filter = new \Data\Filter('ou eq '.$params['type']);
164
            return $auth->getUsersByFilter($filter);
165
    }
166
}
167
168
function leads()
169
{
170
    global $app;
171
    if(!$app->user)
172
    {
173
        throw new Exception('Must be logged in', ACCESS_DENIED);
174
    }
175
    if(!$app->user->isInGroupNamed('Leads') && !$app->user->isInGroupNamed('CC') && !$app->user->isInGroupNamed('AFs'))
176
    {
177
        throw new Exception('Must be Lead', ACCESS_DENIED);
178
    }
179
    $params = $app->request->params();
180
    $auth = AuthProvider::getInstance();
181
    $leads = array();
182
    if(!isset($params['type']))
183
    {
184
        $leadGroup = $auth->getGroupByName('Leads');
185
        $aarGroup  = $auth->getGroupByName('AAR');
186
        $afGroup   = $auth->getGroupByName('AFs');
187
        $ccGroup   = $auth->getGroupByName('CC');
188
        $leads     = array_merge($leads, $leadGroup->members(true, false));
189
        $leads     = array_merge($leads, $aarGroup->members(true, false));
190
        $leads     = array_merge($leads, $afGroup->members(true, false));
191
        $leads     = array_merge($leads, $ccGroup->members(true, false));
192
    }
193
    else
194
    {
195
        $leads = getLeadsByType($params['type'], $auth);
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
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
236