Utils.php 7.37 KB
Newer Older
1
2
3
4
5
6
7
<?php
/**
 * This software is governed by the CeCILL-B license. If a copy of this license
 * is not distributed with this file, you can obtain one at
 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
 *
 * Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
8
 * Authors of Framadate/OpenSondage: Framasoft (https://github.com/framasoft)
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * =============================
 *
 * Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
 * ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
 *
 * Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
 * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
 */
namespace Framadate;

Thomas Citharel's avatar
Thomas Citharel committed
21
22
use Parsedown;

23
class Utils {
24
25
26
    /**
     * @return string Server name
     */
27
    public static function get_server_name() {
28
        $scheme = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')) ? 'https' : 'http';
29
        $port = in_array($_SERVER['SERVER_PORT'], ['80', '443'], true) ? '' : ':' . $_SERVER['SERVER_PORT'];
Olivier PEREZ's avatar
Olivier PEREZ committed
30
31
        $dirname = dirname($_SERVER['SCRIPT_NAME']);
        $dirname = $dirname === '\\' ? '/' : $dirname . '/';
32
        $dirname = str_replace('/admin', '', $dirname);
33
        $dirname = str_replace('/action', '', $dirname);
34
        $server_name = (defined('APP_URL') ? APP_URL : $_SERVER['SERVER_NAME']) . $port . $dirname;
35

Olivier PEREZ's avatar
Olivier PEREZ committed
36
        return $scheme . '://' . preg_replace('#//+#', '/', $server_name);
37
38
    }

39
40
41
42
    /**
     * @param string $title
     * @deprecated
     */
43
    public static function print_header($title = '') {
Olivier PEREZ's avatar
Olivier PEREZ committed
44
        global $locale;
45
46

        echo '<!DOCTYPE html>
Olivier PEREZ's avatar
Olivier PEREZ committed
47
    <html lang="' . $locale . '">
48
    <head>
49
        <meta charset="utf-8" />';
50

51
        if (!empty($title)) {
52
53
54
            echo '<title>' . stripslashes($title) . ' - ' . NOMAPPLICATION . '</title>';
        } else {
            echo '<title>' . NOMAPPLICATION . '</title>';
55
56
57
        }

        echo '
58
59
60
61
62
        <link rel="stylesheet" href="' . self::get_server_name() . 'css/bootstrap.min.css" />
        <link rel="stylesheet" href="' . self::get_server_name() . 'css/datepicker3.css" />
        <link rel="stylesheet" href="' . self::get_server_name() . 'css/style.css" />
        <link rel="stylesheet" href="' . self::get_server_name() . 'css/frama.css" />
        <link rel="stylesheet" href="' . self::get_server_name() . 'css/print.css" media="print" />
Thomas Citharel's avatar
Thomas Citharel committed
63
        <script type="text/javascript" src="' . self::get_server_name() . 'js/jquery-1.12.4.min.js"></script>
64
        <script type="text/javascript" src="' . self::get_server_name() . 'js/bootstrap.min.js"></script>
65
        <script type="text/javascript" src="' . self::get_server_name() . 'js/bootstrap-datepicker.js"></script>';
66
        if ('en' !== $locale) {
67
68
69
70
        	   echo '
        <script type="text/javascript" src="' . self::get_server_name() . 'js/locales/bootstrap-datepicker.' . $locale . '.js"></script>';
        }
        echo '
71
        <script type="text/javascript" src="' . self::get_server_name() . 'js/core.js"></script>';
72
        if (is_file($_SERVER['DOCUMENT_ROOT'] . "/nav/nav.js")) {
73
74
75
76
77
78
            echo '<script src="/nav/nav.js" id="nav_js" type="text/javascript" charset="utf-8"></script><!-- /Framanav -->';
        }

        echo '
    </head>
    <body>
FramaJosephK's avatar
FramaJosephK committed
79
    <div class="container ombre">';
80
81
82
    }

    /**
Antonin's avatar
Antonin committed
83
84
85
86
     * Function allowing to generate poll's url
     * @param   string $id The poll's id
     * @param   bool $admin True to generate an admin URL, false for a public one
     * @param   string $vote_id (optional) The vote's unique id
Thomas Citharel's avatar
Thomas Citharel committed
87
88
     * @param null $action
     * @param null $action_value
Antonin's avatar
Antonin committed
89
     * @return  string The poll's URL.
90
     */
91
    public static function getUrlSondage($id, $admin = false, $vote_id = '', $action = null, $action_value = null) {
92
        // URL-Encode $action_value
Thomas Citharel's avatar
Thomas Citharel committed
93
        $action_value = $action_value ? Utils::base64url_encode($action_value) : null;
94

95
96
        if (URL_PROPRE) {
            if ($admin === true) {
97
                $url = self::get_server_name() . $id . '/admin';
98
            } else {
Olivier PEREZ's avatar
Olivier PEREZ committed
99
                $url = self::get_server_name() . $id;
100
            }
101
            if ($vote_id !== '') {
Olivier PEREZ's avatar
Olivier PEREZ committed
102
                $url .= '/vote/' . $vote_id . "#edit";
103
104
            } elseif ($action) {
                if ($action_value) {
105
106
107
108
                    $url .= '/action/' . $action . '/' . $action_value;
                } else {
                    $url .= '/action/' . $action;
                }
109
            }
110
111
        } else {
            if ($admin === true) {
112
                $url = self::get_server_name() . 'adminstuds.php?poll=' . $id;
113
            } else {
114
115
                $url = self::get_server_name() . 'studs.php?poll=' . $id;
            }
116
            if ($vote_id !== '') {
Olivier PEREZ's avatar
Olivier PEREZ committed
117
                $url .= '&vote=' . $vote_id . "#edit";
Thomas Citharel's avatar
Thomas Citharel committed
118
119
            } elseif ($action)  {
                if ($action_value) {
120
121
122
123
                    $url .= '&' . $action . "=" . $action_value;
                } else {
                    $url .= '&' . $action . "=";
                }
124
            }
125
126
127
128
129
        }

        return $url;
    }

Olivier PEREZ's avatar
Olivier PEREZ committed
130
131
    /**
     * This method pretty prints an object to the page framed by pre tags.
132
     *
133
     * @param mixed $object The object to print.
Olivier PEREZ's avatar
Olivier PEREZ committed
134
     */
135
    public static function debug($object) {
Olivier PEREZ's avatar
Olivier PEREZ committed
136
137
138
139
        echo '<pre>';
        print_r($object);
        echo '</pre>';
    }
140
141
142
143

    public static function table($tableName) {
        return TABLENAME_PREFIX . $tableName;
    }
144

Antonin's avatar
Antonin committed
145
    public static function markdown($md, $clear=false, $line=true) {
Thomas Citharel's avatar
Thomas Citharel committed
146
        $parseDown = new Parsedown();
147

Antonin's avatar
Antonin committed
148
        $parseDown
Antonin's avatar
Antonin committed
149
            ->setMarkupEscaped(true)
Antonin's avatar
Antonin committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
            ->setBreaksEnabled(true)
            ->setUrlsLinked(false);

        if ($line) {
            $html  = $parseDown->line($md);
        } else {
            $md = preg_replace_callback(
                '#( ){2,}#',
                function ($m) {
                    return str_repeat('&nbsp;', strlen($m[0]));
                },
                $md
            );
            $html  = $parseDown->text($md);
164
165
        }

Antonin's avatar
Antonin committed
166
        $text = strip_tags($html);
167

168
169
170
        return $clear ? $text : $html;
    }

171
172
173
174
    public static function htmlEscape($html) {
        return htmlentities($html, ENT_HTML5 | ENT_QUOTES);
    }

Antonin's avatar
Antonin committed
175
    public static function htmlMailEscape($html) {
Antonin's avatar
Antonin committed
176
        return htmlspecialchars($html, ENT_HTML5 | ENT_QUOTES);
Antonin's avatar
Antonin committed
177
178
    }

179
180
181
182
    public static function csvEscape($text) {
        $escaped = str_replace('"', '""', $text);
        $escaped = str_replace("\r\n", '', $escaped);
        $escaped = str_replace("\n", '', $escaped);
183
        $escaped = preg_replace("/^(=|\+|\-|\@)/", "'$1", $escaped);
184
185
186
187
188
189
190
191
192

        return '"' . $escaped . '"';
    }

    public static function cleanFilename($title) {
        $cleaned = preg_replace('[^a-zA-Z0-9._-]', '_', $title);
        $cleaned = preg_replace(' {2,}', ' ', $cleaned);

        return $cleaned;
193
    }
194

195
    public static function fromPostOrDefault($postKey, $default = '') {
196
        return !empty($_POST[$postKey]) ? $_POST[$postKey] : $default;
197
    }
198
199
200
201
202
203

    public static function base64url_encode($input) {
        return rtrim(strtr(base64_encode($input), '+/', '-_'), '=');
    }

    public static function base64url_decode($input) {
204
        return base64_decode(str_pad(strtr($input, '-_', '+/'), strlen($input) % 4, '=', STR_PAD_RIGHT), true);
205
    }
206
}