1<?php
2/**
3 * DokuWiki Plugin Issuelinks (Helper Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <dokuwiki@cosmocode.de>
7 */
8
9class helper_plugin_issuelinks_util extends DokuWiki_Plugin
10{
11
12    /**
13     * Parse the link header received by DokuHTTPClient
14     *
15     * @param string $linkHeader
16     *
17     * @return array
18     */
19    public function parseHTTPLinkHeaders($linkHeader)
20    {
21        $links = explode(',', $linkHeader);
22        $linkarray = [];
23        foreach ($links as $linkstring) {
24            list($linktarget, $linkrel) = explode(';', $linkstring);
25            $linkrel = substr(trim($linkrel), strlen('rel="'), -1);
26            $linktarget = trim($linktarget, '<> ');
27            $linkarray[$linkrel] = $linktarget;
28        }
29        return $linkarray;
30    }
31
32    /**
33     * @param string $page_id
34     * @param int    $revision
35     *
36     * @return string
37     */
38    public function getDiffUrl($page_id, $revision = 0)
39    {
40        if (empty($revision)) {
41            $currentRevision = filemtime(wikiFN($page_id));
42            $url = wl(
43                $page_id,
44                [
45                    "rev" => $currentRevision,
46                    "do" => "diff",
47                ],
48                true
49            );
50        } else {
51            $changelog = new PageChangelog($page_id);
52            $previousRevision = $changelog->getRelativeRevision($revision, -1);
53            $url = wl(
54                $page_id,
55                [
56                    "do" => "diff",
57                    "rev2[0]" => $revision,
58                    "rev2[1]" => $previousRevision,
59                    "difftype" => "sidebyside",
60                ],
61                true
62            );
63        }
64        return $url;
65    }
66
67    /**
68     * Show an error message for the execption, add trace if $conf['allowdebug']
69     *
70     * @param Exception $e
71     */
72    public function reportException(Exception $e)
73    {
74        msg(hsc($e->getMessage()), -1, $e->getLine(), $e->getFile());
75        global $conf;
76        if ($conf['allowdebug']) {
77            msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
78        }
79    }
80
81    /**
82     * @param int   $code
83     * @param mixed $msg
84     */
85    public function sendResponse($code, $msg)
86    {
87        header('Content-Type: application/json');
88        if ((int)$code === 204) {
89            http_status(200);
90        } else {
91            http_status($code);
92        }
93        global $MSG;
94        echo json_encode(['data' => $msg, 'msg' => $MSG]);
95    }
96
97
98    /**
99     * Check whether a number or string is a valid unix timestamp
100     *
101     * Adapted from http://stackoverflow.com/a/2524761/3293343
102     *
103     * @param string|int $timestamp
104     *
105     * @return bool
106     */
107    public function isValidTimeStamp($timestamp)
108    {
109        return ((string)(int)$timestamp === (string)$timestamp);
110    }
111}
112