aboutsummaryrefslogtreecommitdiffstats
path: root/htdocs/application/controllers/Api.php
blob: 74aac2279a502e2bc13c2299ae2d1880ed8edba2 (plain) (blame)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

include_once 'application/controllers/Main.php';

class Api extends Main
{

    public function __construct()
    {
        parent::__construct();

        if (config_item('disable_api')) {
            die("The API has been disabled\n");
        }

        // if ldap is configured and no api token is configured, fail the request
        if ((config_item('require_auth') == true) && (config_item('apikey') == '')) {
            die("API key not configured");
        }

    }

    public function index()
    {
        $languages = $this->languages->get_languages();
        $languages = array_keys($languages);
        $languages = implode(', ', $languages);
        $data['languages'] = $languages;
        $this->load->view('api_help', $data);
    }

    public function create()
    {

        if (config_item('apikey') != $this->input->get('apikey') && config_item('soft_api') == false) {
            die("Invalid API key\n");
        }
        $this->load->model('pastes');
        $this->load->library('form_validation'); //needed by parent class

        if (!$this->input->post('text')) {
            $data['msg'] = 'Error: Missing paste text';
            $this->load->view('view/api', $data);
        } else {

            if (!$this->input->post('lang')) {
                $_POST['lang'] = 'text';
            }
            $_POST['code'] = $this->input->post('text');

            if ($this->config->item('private_only')) {
                $_POST['private'] = 1;
            }

            //validations

            if (!$this->_valid_ip()) {
                die("You are not allowed to paste\n");
            }

            if (config_item('soft_api') == true && (config_item('apikey') == $this->input->get('apikey'))) {

                //pass

            } else {

                if (!$this->_blockwords_check()) {
                    die("Your paste contains blocked words\n");
                }
            }

            if (!$this->input->post('expire')) {
                $_POST['expire'] = config_item('default_expiration');
            }

            //create paste
            $paste_url = $this->pastes->createPaste();
            $data['msg'] = base_url() . $paste_url;
            $this->load->view('view/api', $data);
        }
    }

    public function paste()
    {

        if (config_item('apikey') != $this->input->get('apikey')) {
            die("Invalid API key\n");
        }

        if (config_item('private_only')) {
            show_404();
        }
        $this->load->model('pastes');
        $check = $this->pastes->checkPaste(3);

        if ($check) {
            $data = $this->pastes->getPaste(3);
        } else {
            $data = array(
                'message' => 'Not found',
            );
        }
        echo json_encode($data);
    }

    public function random()
    {

        if (config_item('apikey') != $this->input->get('apikey')) {
            die("Invalid API key\n");
        }

        if (config_item('private_only')) {
            show_404();
        }
        $this->load->model('pastes');
        $data = $this->pastes->random_paste();
        echo json_encode($data);
    }

    public function recent()
    {

        if (config_item('apikey') != $this->input->get('apikey')) {
            die("Invalid API key\n");
        }

        if (config_item('private_only')) {
            show_404();
        }
        $this->load->model('pastes');
        $pastes = $this->pastes->getLists('api/recent');
        $pastes = $pastes['pastes'];
        $data = array();
        foreach ($pastes as $paste) {
            $data[] = array(
                'pid' => $paste['pid'],
                'title' => $paste['title'],
                'name' => $paste['name'],
                'created' => $paste['created'],
                'lang' => $paste['lang'],
            );
        }
        echo json_encode($data);
    }

    public function trending()
    {

        if (config_item('apikey') != $this->input->get('apikey')) {
            die("Invalid API key\n");
        }

        if (config_item('private_only')) {
            show_404();
        }
        $this->load->model('pastes');
        $pastes = $this->pastes->getTrends('api/trending', 2);
        $pastes = $pastes['pastes'];
        $data = array();
        foreach ($pastes as $paste) {
            $data[] = array(
                'pid' => $paste['pid'],
                'title' => $paste['title'],
                'name' => $paste['name'],
                'created' => $paste['created'],
                'lang' => $paste['lang'],
                'hits' => $paste['hits'],
            );
        }
        echo json_encode($data);
    }

    public function langs()
    {
        if (config_item('apikey') != $this->input->get('apikey')) {
            die("Invalid API key\n");
        }

        $languages = $this->languages->get_languages();
        echo json_encode($languages);
    }
}

© 2014-2024 Faster IT GmbH | imprint | privacy policy