SessionCookie.php 1.83 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 4/30/17
 * Time: 1:45 PM
 */

namespace qfq;

require_once(__DIR__ . '/../Constants.php');

13
14
15
16
/**
 * Class SessionCookie
 * @package qfq
 */
17
18
class SessionCookie {

19
20
21
    /**
     * @var string
     */
22
23
    private $pathFileNameCookie = '';

24
25
26
27
28
    /**
     * @var bool
     */
    private $cleanTempFiles = true;

29
30
31
    /**
     * Copy all current cookies to a temporary file.
     *
32
     * @param array $config
Carsten  Rose's avatar
Carsten Rose committed
33
     *
34
35
     * @throws CodeException
     */
36
    public function __construct(array $config) {
37
38
        $lines = '';

39
        $this->cleanTempFiles = !Support::findInSet(SYSTEM_SHOW_DEBUG_INFO_DOWNLOAD, $config[SYSTEM_SHOW_DEBUG_INFO]);
40

41
        $urlParts = parse_url($config[SYSTEM_BASE_URL]);
42
43
44
        $domain = $urlParts['host'];
        $path = $urlParts['path'];

45
46
        // $_COOKIES[]
        if (false === ($this->pathFileNameCookie = tempnam(sys_get_temp_dir(), SESSION_COOKIE_PREFEIX))) {
47
48
49
50
51
52
53
54
55
56
57
58
59
            throw new CodeException('Error creating output file.', ERROR_IO_CREATE_FILE);
        }

        foreach ($_COOKIE as $name => $value) {
            // e.g.: SANDBOXSESSION=a83f1o69jbv12932q54hmgphk6; domain=qfq.math.uzh.ch; path=/;
            $lines .= $name . "=" . $value . "; domain=$domain; path=$path;\n";
        }

        file_put_contents($this->pathFileNameCookie, $lines, FILE_APPEND);
    }

    /**
     * Unlink current cookie file
60
     * @throws UserFormException
61
62
     */
    public function __destruct() {
63
64
65

        if ($this->cleanTempFiles) {
            if (file_exists($this->pathFileNameCookie)) {
66
                HelperFile::unlink($this->pathFileNameCookie);
67
68
                $this->pathFileNameCookie = '';
            }
69
70
71
72
73
74
75
76
77
78
        }
    }

    /**
     * @return string PathFilename of cookie file
     */
    public function getFile() {

        return $this->pathFileNameCookie;
    }
79

80
}