aboutsummaryrefslogtreecommitdiff
path: root/src/phorkie/Repository.php
blob: 6dec015538b788e5b556fbbef66a45bf2302d76c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace phorkie;


class Repository
{
    /**
     * Repository ID (number in repositories directory)
     *
     * @var integer
     */
    public $id;

    /**
     * Full path to the git repository
     *
     * @var string
     */
    public $repoDir;

    /**
     * Load Repository data from GET-Request
     *
     * @return void
     *
     * @throws Exception When something is wrong
     */
    public function loadFromRequest()
    {
        if (!isset($_GET['id'])) {
            throw new Exception_Input('Paste ID missing');
        }
        if (!is_numeric($_GET['id'])) {
            throw new Exception_Input('Paste ID not numeric');
        }
        $this->id = (int)$_GET['id'];

        $repoDir = $GLOBALS['phorkie']['cfg']['repos'] . '/' . $this->id;
        if (!is_dir($repoDir)) {
            throw new Exception_NotFound('Paste not found');
        }
        $this->repoDir = $repoDir;
    }

    public function loadById($id)
    {
        if (!is_numeric($id)) {
            throw new Exception_Input('Paste ID not numeric');
        }
        $this->id = (int)$id;

        $repoDir = $GLOBALS['phorkie']['cfg']['repos'] . '/' . $this->id;
        if (!is_dir($repoDir)) {
            throw new Exception_NotFound('Paste not found');
        }
        $this->repoDir = $repoDir;
    }

    public function getVc()
    {
        return new \VersionControl_Git($this->repoDir);
    }

    /**
     * Loads the list of files in this repository
     *
     * @return File[] Array of files
     */
    public function getFiles()
    {
        $files = glob($this->repoDir . '/*');
        $arFiles = array();
        foreach ($files as $path) {
            $arFiles[] = new File($path, $this);
        }
        return $arFiles;
    }

    public function getFileByName($name, $bHasToExist = true)
    {
        $base = basename($name);
        if ($base != $name) {
            throw new Exception('No directories supported for now');
        }
        if ($name == '') {
            throw new Exception_Input('Empty file name given');
        }
        $path = $this->repoDir . '/' . $base;
        if ($bHasToExist && !is_readable($path)) {
            throw new Exception_Input('File does not exist');
        }
        return new File($path, $this);
    }

    public function hasFile($name)
    {
        try {
            $this->getFileByName($name);
        } catch (Exception $e) {
            return false;
        }
        return true;
    }

    /**
     * Permanently deletes the paste repository without any way to get
     * it back.
     *
     * @return boolean True if all went well, false if not
     */
    public function delete()
    {
        return Tools::recursiveDelete($this->repoDir);
    }

    public function getDescription()
    {
        if (!is_readable($this->repoDir . '/.git/description')) {
            return null;
        }
        return file_get_contents($this->repoDir . '/.git/description');
    }

    public function setDescription($description)
    {
        file_put_contents($this->repoDir . '/.git/description', $description);
    }

    /**
     * Get a link to the repository
     *
     * @param string $type Link type. Supported are:
     *                     - "edit"
     *                     - "display"
     *                     - "fork"
     *
     * @return string
     */
    public function getLink($type)
    {
        if ($type == 'edit') {
            return '/' . $this->id . '/edit';
        } else if ($type == 'display') {
            return '/' . $this->id;
        } else if ($type == 'fork') {
            return '/' . $this->id . '/fork';
        } else if ($type == 'delete') {
            return '/' . $this->id . '/delete';
        } else if ($type == 'delete-confirm') {
            return '/' . $this->id . '/delete/confirm';
        }
        throw new Exception('Unknown link type');
    }

    public function getCloneURL($public = true)
    {
        $var = $public ? 'public' : 'private';
        if (isset($GLOBALS['phorkie']['cfg']['git'][$var])) {
            return $GLOBALS['phorkie']['cfg']['git'][$var] . $this->id . '/.git';
        }
        return null;
    }
}

?>