Completed
Push — master ( 5a053e...8eeafe )
by Terrence
10:16
created

index-functions.php ➔ printLogOff()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 85

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 16
nop 0
dl 0
loc 85
rs 8.0161
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file contains functions called by index-site.php. The index-site.php
5
 * file should include this file with the following statement at the top:
6
 *
7
 * require_once __DIR__ . '/index-functions.php';
8
 */
9
10
use CILogon\Service\Util;
11
use CILogon\Service\Content;
12
use CILogon\Service\Loggit;
13
14
/**
15
 * printLogonPage
16
 *
17
 * This function prints out the HTML for the main cilogon.org page.
18
 * Explanatory text is shown as well as a button to log in to an IdP
19
 * and get rerouted to the Shibboleth protected service script, or the
20
 * OpenID script.
21
 *
22
 * @param bool $clearcookies True if the Shibboleth cookies and session
23
 *        variables  should be cleared out before displaying the page.
24
 *        Defaults to false.
25
 */
26
function printLogonPage($clearcookies = false)
0 ignored issues
show
Best Practice introduced by
The function printLogonPage() has been defined more than once; this definition is ignored, only the first definition in authorize/index-functions.php (L25-121) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
27
{
28
    if ($clearcookies) {
29
        Util::removeShibCookies();
30
        Util::unsetAllUserSessionVars();
31
        Util::getSkin()->init(true);  // Clear cilogon_skin var; check for forced skin
32
    }
33
34
    $log = new Loggit();
35
    $log->info('Welcome page hit.');
36
37
    Util::setSessionVar('stage', 'logon'); // For Show/Hide Help button clicks
38
39
    Content::printHeader('Welcome To The CILogon Service');
40
41
    echo '
42
    <div class="boxed">
43
    ';
44
45
    Content::printHelpButton();
46
    Content::printWAYF();
47
48
    echo '
49
    </div> <!-- End boxed -->
50
    ';
51
    Content::printFooter();
52
}
53
54
/**
55
 * printMainPage
56
 *
57
 * This function prints out the HTML for the main page where the user
58
 * can download a certificate.
59
 */
60
function printMainPage()
0 ignored issues
show
Best Practice introduced by
The function printMainPage() has been defined more than once; this definition is ignored, only the first definition in authorize/index-functions.php (L192-289) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
61
{
62
    $log = new Loggit();
63
    $log->info('Get And Use Certificate page hit.');
64
65
    Util::setSessionVar('stage', 'main'); // For Show/Hide Help button clicks
66
67
    Content::printHeader('Get Your Certificate');
68
69
    // CIL-626 Allow browser 'reload page' by adding CSRF to the PHP session
70
    Util::setSessionVar('submit', 'Proceed');
71
    Util::getCsrf()->setTheSession();
72
73
    echo '
74
    <div class="boxed">
75
    ';
76
77
    Content::printHelpButton();
78
    printCertInfo();
79
    printGetCertificate();
80
    Content::printTwoFactorBox();
81
    printLogOff();
82
83
    echo '
84
    </div> <!-- boxed -->
85
    ';
86
    Content::printFooter();
87
}
88
89
/**
90
 * printCertInfo
91
 *
92
 * This function prints the certificate information table at the top
93
 * of the main page.
94
 */
95
function printCertInfo()
96
{
97
    $dn = Util::getSessionVar('dn');
98
    $dn = Content::reformatDN(preg_replace('/\s+email=.+$/', '', $dn));
99
100
    echo '
101
    <table class="certinfo">
102
      <tr>
103
        <th>Certificate&nbsp;Subject:</th>
104
        <td>' , Util::htmlent($dn) , '</td>
105
      </tr>
106
      <tr>
107
        <th>Identity&nbsp;Provider:</th>
108
        <td>' , Util::getSessionVar('idpname') , '</td>
109
      </tr>
110
      <tr>
111
        <th><a target="_blank"
112
        href="http://ca.cilogon.org/loa">Level&nbsp;of&nbsp;Assurance:</a></th>
113
        <td>
114
    ';
115
116
    $loa = Util::getSessionVar('loa');
117
    if ($loa == 'openid') {
118
        echo '<a href="http://ca.cilogon.org/policy/openid"
119
              target="_blank">OpenID</a>';
120
    } elseif ($loa == 'http://incommonfederation.org/assurance/silver') {
121
        echo '<a href="http://ca.cilogon.org/policy/silver"
122
              target="_blank">Silver</a>';
123
    } else {
124
        echo '<a href="http://ca.cilogon.org/policy/basic"
125
              target="_blank">Basic</a>';
126
    }
127
    echo '
128
        </td>
129
      </tr>
130
    </table>
131
    ';
132
}
133
134
/**
135
 * printGetCertificate
136
 *
137
 * This function prints the 'Get New Certificate' box on the main page.
138
 * If the 'p12' PHP session variable is valid, it is read and a link for the
139
 * usercred.p12 file is presented to the user.
140
 */
141
function printGetCertificate()
142
{
143
    // Check if PKCS12 downloading is disabled. If so, print out message.
144
    $skin = Util::getSkin();
145
    $disabled = $skin->getConfigOption('pkcs12', 'disabled');
146
    if ((!is_null($disabled)) && ((int)$disabled == 1)) {
147
        $disabledmsg = $skin->getConfigOption(
148
            'pkcs12',
149
            'disabledmessage'
150
        );
151
        if (!is_null($disabledmsg)) {
152
            $disabledmsg = trim(html_entity_decode($disabledmsg));
153
        }
154
        if (strlen($disabledmsg) == 0) {
155
            $disabledmsg = "Downloading PKCS12 certificates is " .
156
                "restricted. Please try another method or log on " .
157
                "with a different Identity Provider.";
158
        }
159
160
        echo '<div class="p12actionbox"><p>
161
             ', $disabledmsg , '
162
             </p></div> <!-- p12actionbox -->';
163
    } else { // PKCS12 downloading is okay
164
        $downloadcerttext = "Clicking this button will generate a link " .
165
            "to a new certificate, which you can download to your local " .
166
            "computer. The certificate is valid for up to 13 months.";
167
        $p12linktext = "Left-click this link to import the certificate " .
168
            "into your broswer / operating system. (Firefox users see " .
169
            "the FAQ.) Right-click this link and select 'Save As...' to " .
170
            "save the certificate to your desktop.";
171
        $passwordtext1 = 'Enter a password of at least 12 characters to " .
172
            "protect your certificate.';
173
        $passwordtext2 = 'Re-enter your password to verify.';
174
175
        validateP12();
176
        $p12expire = '';
177
        $p12link = '';
178
        $p12 = Util::getSessionVar('p12');
179
        if (preg_match('/([^\s]*)\s(.*)/', $p12, $match)) {
180
            $p12expire = $match[1];
181
            $p12link = $match[2];
182
        }
183
184
        if ((strlen($p12link) > 0) && (strlen($p12expire) > 0)) {
185
            $p12link = '<a href="' . $p12link .
186
                '">&raquo; Click Here To Download Your Certificate &laquo;</a>';
187
        }
188
        if ((strlen($p12expire) > 0) && ($p12expire > 0)) {
189
            $expire = $p12expire - time();
190
            $minutes = floor($expire % 3600 / 60);
191
            $seconds = $expire % 60;
192
            $p12expire = 'Link Expires: ' .
193
                sprintf("%02dm:%02ds", $minutes, $seconds);
194
        } else {
195
            $p12expire = '';
196
        }
197
198
        $p12lifetime = Util::getSessionVar('p12lifetime');
199
        if ((strlen($p12lifetime) == 0) || ($p12lifetime == 0)) {
200
            $p12lifetime = Util::getCookieVar('p12lifetime');
201
        }
202
        $p12multiplier = Util::getSessionVar('p12multiplier');
203
        if ((strlen($p12multiplier) == 0) || ($p12multiplier == 0)) {
204
            $p12multiplier = Util::getCookieVar('p12multiplier');
205
        }
206
207
        // Try to read the skin's intiallifetime if not yet set
208
        if ((strlen($p12lifetime) == 0) || ($p12lifetime <= 0)) {
209
            // See if the skin specified an initial value
210
            $skinlife = $skin->getConfigOption('pkcs12', 'initiallifetime', 'number');
211
            $skinmult = $skin->getConfigOption('pkcs12', 'initiallifetime', 'multiplier');
212
            if (
213
                (!is_null($skinlife)) && (!is_null($skinmult)) &&
214
                ((int)$skinlife > 0) && ((int)$skinmult > 0)
215
            ) {
216
                $p12lifetime = (int)$skinlife;
217
                $p12multiplier = (int)$skinmult;
218
            } else {
219
                $p12lifetime = 13;      // Default to 13 months
220
                $p12multiplier = 732;
221
            }
222
        }
223
        if ((strlen($p12multiplier) == 0) || ($p12multiplier <= 0)) {
224
            $p12multiplier = 732;   // Default to months
225
            if ($p12lifetime > 13) {
226
                $p12lifetime = 13;
227
            }
228
        }
229
230
        // Make sure lifetime is within [minlifetime,maxlifetime]
231
        list($minlifetime, $maxlifetime) =
232
            Content::getMinMaxLifetimes('pkcs12', 9516);
233
        if (($p12lifetime * $p12multiplier) < $minlifetime) {
234
            $p12lifetime = $minlifetime;
235
            $p12multiplier = 1; // In hours
236
        } elseif (($p12lifetime * $p12multiplier) > $maxlifetime) {
237
            $p12lifetime = $maxlifetime;
238
            $p12multiplier = 1; // In hours
239
        }
240
241
        $lifetimetext = "Specify the certificate lifetime. Acceptable range " .
242
                        "is between $minlifetime and $maxlifetime hours" .
243
                        (($maxlifetime > 732) ?
244
                            " ( = " . round(($maxlifetime / 732), 2) . " months)." :
245
                            "."
246
                        );
247
248
        echo '
249
        <div class="p12actionbox"';
250
251
        if (Util::getSessionVar('showhelp') == 'on') {
252
            echo ' style="width:92%;"';
253
        }
254
255
        echo '>
256
        <table class="helptable">
257
        <tr>
258
        <td class="actioncell">
259
        ';
260
261
        Content::printFormHead();
262
263
        echo '
264
          <fieldset>
265
          ';
266
267
        $p12error = Util::getSessionVar('p12error');
268
        if (strlen($p12error) > 0) {
269
            echo "<p class=\"logonerror\">$p12error</p>";
270
            Util::unsetSessionVar('p12error');
271
        }
272
273
        echo '
274
          <p>
275
          Password Protect Your New Certificate:
276
          </p>
277
278
          <p>
279
          <label for="password1" class="helpcursor" title="' ,
280
          $passwordtext1 , '">Enter A Password:</label>
281
          <input type="password" name="password1" id="password1"
282
          size="22" title="' , $passwordtext1 , '" onkeyup="checkPassword()"/>
283
          <img src="/images/blankIcon.png" width="14" height="14" alt=""
284
          id="pw1icon"/>
285
          </p>
286
287
          <p>
288
          <label for="password2" class="helpcursor" title="' ,
289
          $passwordtext2 , '">Confirm Password:</label>
290
          <input type="password" name="password2" id="password2"
291
          size="22" title="' , $passwordtext2 , '" onkeyup="checkPassword()"/>
292
          <img src="/images/blankIcon.png" width="14" height="14" alt=""
293
          id="pw2icon"/>
294
          </p>
295
296
          <p class="p12certificatelifetime">
297
          <label for="p12lifetime" title="' , $lifetimetext ,
298
          '" class="helpcursor">Certificate Lifetime:</label>
299
          <input type="text" name="p12lifetime" id="p12lifetime"
300
          title="', $lifetimetext ,
301
          '" class="helpcursor" value="' , $p12lifetime ,
302
          '" size="8" maxlength="8"/>
303
          <select title="' , $lifetimetext ,
304
          '" class="helpcursor" id="p12multiplier" name="p12multiplier">
305
          <option value="1"' ,
306
              (($p12multiplier == 1) ? ' selected="selected"' : '') ,
307
              '>hours</option>
308
          <option value="24"' ,
309
              (($p12multiplier == 24) ? ' selected="selected"' : '') ,
310
              '>days</option>
311
          <option value="732"' ,
312
              (($p12multiplier == 732) ? ' selected="selected"' : '') ,
313
              '>months</option>
314
          </select>
315
          <img src="/images/blankIcon.png" width="14" height="14" alt=""/>
316
          </p>
317
318
          <p>
319
          <input type="submit" name="submit" class="submit helpcursor"
320
          title="' , $downloadcerttext , '" value="Get New Certificate"
321
          onclick="showHourglass(\'p12\')"/>
322
          <img src="/images/hourglass.gif" width="32" height="32" alt=""
323
          class="hourglass" id="p12hourglass"/>
324
          </p>
325
326
          <p id="p12value" class="helpcursor" title="' ,
327
              $p12linktext , '">' , $p12link , '</p>
328
          <p id="p12expire">' , $p12expire , '</p>
329
330
          </fieldset>
331
          </form>
332
        </td>
333
        ';
334
335
        if (Util::getSessionVar('showhelp') == 'on') {
336
            echo '
337
            <td class="helpcell">
338
            <div>
339
            <p>
340
            In order to get a new certificate, please enter a password of at
341
            least 12 characters in length.  This password protects the private
342
            key of the certificate and is different from your identity provider
343
            password.  You must enter the password twice for verification.
344
            </p>
345
            <p>
346
            After entering a password, click the "Get New Certificate" button to
347
            generate a new link.  Right-click on this link to download the
348
            certificate to your computer.  The certificate is valid for up to 13
349
            months.
350
            </p>
351
            </div>
352
            </td>
353
            ';
354
        }
355
356
        echo '
357
        </tr>
358
        </table>
359
        </div> <!-- p12actionbox -->
360
        ';
361
    }
362
}
363
364
/**
365
 * printLogOff
366
 *
367
 * This function prints the Log Off boxes at the bottom of the main page.
368
 */
369
function printLogOff()
370
{
371
    $logofftext = 'End your CILogon session and return to the welcome page. ' .
372
                  'Note that this will not log you out at ' .
373
                  Util::getSessionVar('idpname') . '.';
374
375
    $showhelp = Util::getSessionVar('showhelp');
376
377
    echo '
378
    <div class="logoffactionbox"';
379
380
    if ($showhelp == 'on') {
381
        echo ' style="width:92%;"';
382
    }
383
384
    echo '>
385
    <table class="helptable">
386
    <tr>
387
    <td class="actioncell">
388
    ';
389
390
    Content::printFormHead();
391
392
    echo '
393
      <p>
394
      <input type="submit" name="submit" class="submit helpcursor"
395
      title="' , $logofftext , '" value="Log Off" />
396
      </p>
397
    </form>
398
    </td>
399
    ';
400
401
    if ($showhelp == 'on') {
402
        echo '
403
        <td class="helpcell">
404
        <div>
405
        <p>
406
        This button will log you off of the CILogon Service. In order to log
407
        out from your identity provider, you must either quit your browser
408
        or manually clear your browser\'s cookies.
409
        </p>
410
        </div>
411
        </td>
412
        ';
413
    }
414
415
    echo '
416
    </tr>
417
    </table>
418
    </div> <!-- logoffactionbox -->
419
420
    <div class="logofftextbox"';
421
422
    if ($showhelp == 'on') {
423
        echo ' style="width:92%;"';
424
    }
425
426
    echo '>
427
    <table class="helptable">
428
    <tr>
429
    <td class="actioncell">
430
      <p>To log off, please quit your browser.<p>
431
    </td>
432
    ';
433
434
    if ($showhelp == 'on') {
435
        echo '
436
        <td class="helpcell">
437
        <div>
438
        <p>
439
        Quitting your browser clears all session cookies which logs you out
440
        from your identity provider.  Alternatively, you can manually clear
441
        your browser\'s cookies.
442
        </p>
443
        </div>
444
        </td>
445
        ';
446
    }
447
448
    echo '
449
    </tr>
450
    </table>
451
    </div> <!-- logofftextbox -->
452
    ';
453
}
454
455
/**
456
 * validateP12
457
 *
458
 * This function is called just before the 'Download your certificate'
459
 * link is printed out to HTML. It checks to see if the p12 is still
460
 * valid time-wise. If not, then it unsets the PHP session variable
461
 * 'p12'.
462
 */
463
function validateP12()
464
{
465
    $p12link = '';
466
    $p12expire = '';
467
    $p12 = Util::getSessionVar('p12');
468
    if (preg_match('/([^\s]*)\s(.*)/', $p12, $match)) {
469
        $p12expire = $match[1];
470
        $p12link = $match[2];
471
    }
472
473
    // Verify that the p12expire and p12link values are valid.
474
    if (
475
        (strlen($p12expire) == 0) ||
476
        ($p12expire == 0) ||
477
        (time() > $p12expire) ||
478
        (strlen($p12link) == 0)
479
    ) {
480
        Util::unsetSessionVar('p12');
481
    }
482
}
483