Issues (150)

src/app/controllers/page.php (13 issues)

1
<?php
2
if (!function_exists('getallheaders'))  {
3
    function getallheaders() {
4
        if (!is_array($_SERVER)) {
5
            return array();
6
        }
7
8
        $headers = array();
9
        foreach ($_SERVER as $name => $value) {
10
            if (substr($name, 0, 5) == 'HTTP_') {
11
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
12
            }
13
        }
14
        return $headers;
15
    }
16
}
17
class page extends Controller {
18
19
    function __construct() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
20
        $this->load_library("http_lib", "http");
21
        $this->load_library("auth_lib", "auth");
22
23
        $this->load_model("jugaad_model");
24
        $this->load_model("template_model");
25
        $this->load_model("perms_model");
26
27
        $this->user = $this->auth->get_user() ?: "";
0 ignored issues
show
Bug Best Practice introduced by
The property user does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug Best Practice introduced by
The property auth does not exist on page. Did you maybe forget to declare it?
Loading history...
28
    }
29
30
    function show($api = null) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
31
        $path = func_get_args();
32
33
        if ($api == "api") {
34
            $path = array_slice($path, 1);
35
        }
36
37
        $file_id = $this->jugaad_model->get_path_id($path);
0 ignored issues
show
Bug Best Practice introduced by
The property jugaad_model does not exist on page. Did you maybe forget to declare it?
Loading history...
38
        $file_type = $this->jugaad_model->get_file_type($file_id);
39
40
        if (end($path) == 'index') {
41
            $this->http->redirect(
0 ignored issues
show
Bug Best Practice introduced by
The property http does not exist on page. Did you maybe forget to declare it?
Loading history...
42
                locale_base_url() . implode("/", array_slice($path, 0, -1)) . "/"
43
            );
44
        }
45
46
        // Check if index exists
47
        if ($file_type == 'directory') {
48
            $file_id = $this->jugaad_model->get_slug_id($file_id, 'index');
49
        }
50
51
        if ($file_id === false) {
52
            $this->http->response_code(404);
53
        }
54
55
        $file = $this->jugaad_model->get_file($file_id);
56
57
        $file["user_can"] = $this->perms_model->get_permissions($file_id, $this->user);
0 ignored issues
show
Bug Best Practice introduced by
The property perms_model does not exist on page. Did you maybe forget to declare it?
Loading history...
58
59
        if (!$file["user_can"]["read_file"]) {
60
            $this->http->response_code(404);
61
        }
62
63
        $template_meta = $this->template_model->get_meta($file["template"]);
0 ignored issues
show
Bug Best Practice introduced by
The property template_model does not exist on page. Did you maybe forget to declare it?
Loading history...
64
        if ($template_meta === false) {
65
            $this->http->response_code(404);
66
        }
67
68
        if ($api == "api") {
69
            foreach ($template_meta as $name => $meta) {
70
                if (isset($meta["inApi"]) && $meta["inApi"] === false) {
71
                    unset($template_meta[$name]);
72
                }
73
            }
74
75
            $latest_version = $this->jugaad_model->get_latest_version_id_with_external_data($file_id, $template_meta, $this->user);
76
77
            if (!empty($_GET["prev_id"]) && $_GET["prev_id"] == $latest_version) {
78
                $this->http->response_code(304, false);
79
                exit();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
80
            }
81
82
            $data = $this->jugaad_model->get_file_data($file_id, $template_meta, $this->user, true);
83
84
            header('Content-Type: application/json; charset=UTF-8');
85
            echo json_encode([
86
                "version_id" => $latest_version,
87
                "page_data" => $data
88
            ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
89
        } else {
90
            $header = getallheaders();
91
            $is_ajax = isset($header["X-Ajax-Request"]) && $header["X-Ajax-Request"];
92
93
            if ($is_ajax) {
94
                foreach ($template_meta as $name => $meta) {
95
                    if (isset($meta["inAjax"]) && $meta["inAjax"] === false) {
96
                        unset($template_meta[$name]);
97
                    }
98
                }
99
            }
100
101
            $data = $this->jugaad_model->get_file_data($file_id, $template_meta, $this->user, true);
102
103
            $data["is_ajax"] = $is_ajax;
104
105
            $data["is_authenticated"] = $this->auth->is_authenticated();
0 ignored issues
show
Bug Best Practice introduced by
The property auth does not exist on page. Did you maybe forget to declare it?
Loading history...
106
            $data["user_nick"] = $this->auth->get_user();
107
108
            $view_name = $this->template_model->get_view_name($file["template"]);
109
110
            $data["page_slug"] = implode('__', $path);
111
112
            $this->load_view($view_name, $data);
113
        }
114
    }
115
116
    function locale_dump() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
117
        $strings = [];
118
119
        $files = $this->jugaad_model->get_file_data(0, [
0 ignored issues
show
Bug Best Practice introduced by
The property jugaad_model does not exist on page. Did you maybe forget to declare it?
Loading history...
120
            'files' => [
121
                'type' => 'external',
122
                'path' => '**',
123
                'data' => []
124
            ]
125
        ], $this->user);
126
127
        $files = $files['files'];
128
129
        foreach ($files as $file) {
130
            $template_meta = $this->template_model->get_meta($file["template"]);
0 ignored issues
show
Bug Best Practice introduced by
The property template_model does not exist on page. Did you maybe forget to declare it?
Loading history...
131
132
            if ($template_meta === false) {
133
                continue;
134
            }
135
136
            $translatable_types = ['text', 'longtext'];
137
138
            foreach ($template_meta as $name => $meta) {
139
                if (!(in_array($meta['type'], $translatable_types)
140
                    || ($meta['type'] == 'list' && in_array($meta['listType'], $translatable_types)))
141
                ) {
142
                    unset($template_meta[$name]);
143
                }
144
            }
145
146
            $file_id = $this->jugaad_model->get_path_id(explode('/', $file['path']));
147
            $data = $this->jugaad_model->get_file_data($file_id, $template_meta, $this->user, true);
148
149
            foreach ($data as $value) {
150
                if (!$value) continue;
151
                if (is_array($value)) {
152
                    foreach ($value as $val) {
153
                        $strings[] = $val;
154
                    }
155
                } else {
156
                    $strings[] = $value;
157
                }
158
            }
159
        }
160
161
        header('Content-Type: text/plain; charset=UTF-8');
162
163
        echo "<?php
164
/**
165
 * Save this code in src/locale/locale_dump.php
166
 * After saving this scan src/ with poedit to start translating!
167
 */
168
169
";
170
        foreach ($strings as $string) {
171
            echo '__("' . addslashes($string) . '");' . "\n";
172
        }
173
    }
174
175
}
176