Completed
Push — master ( d2b147...d8f226 )
by Terrence
12:00
created

index-functions.php ➔ printCertInfo()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 38
rs 9.312
c 0
b 0
f 0
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 (L22-118) 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 (L189-286) 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
    Content::printCertInfo();
0 ignored issues
show
Bug introduced by
The method printCertInfo() does not seem to exist on object<CILogon\Service\Content>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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