1<?php
2/**
3 * Helper Component for the youTrack-Links Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Dominic Pöllath <dominic.poellath@ils-gmbh.net>
7 */
8
9if(!defined('DOKU_INC')) die();
10
11class helper_plugin_youtracklinks extends DokuWiki_Plugin
12{
13
14
15    // Method: POST, PUT, GET etc
16    // Data: array("param" => "value") ==> index.php?param=value
17    function CallAPI($method, $url, $data = false, $cookiejar = false)
18    {
19        if (!function_exists('curl_init')) {
20            throw new Exception("You have to install curl first.");
21        }
22
23
24        $curl = curl_init();
25        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 1);
26        curl_setopt($curl, CURLOPT_TIMEOUT, 1);
27        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
28
29        switch ($method)
30        {
31            case "POST":
32                curl_setopt($curl, CURLOPT_POST, 1);
33                if ($data)
34                    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
35                break;
36            case "PUT":
37                curl_setopt($curl, CURLOPT_PUT, 1);
38                break;
39            default:
40                if ($data)
41                    $url = sprintf("%s?%s", $url, http_build_query($data));
42        }
43
44
45        curl_setopt($curl, CURLOPT_URL, $url);
46
47
48        // Optional Authentication:
49        // curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
50        // curl_setopt($curl, CURLOPT_USERPWD, "username:password");
51        // curl_setopt($curl, CURLOPT_COOKIEJAR,  $ckfile);
52        if ($cookiejar) {
53            curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiejar);
54            curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiejar);
55        }
56
57        $content = curl_exec($curl);
58        curl_close($curl);
59        return $content ? $content : null;
60    }
61
62
63
64    /**
65     * getter function for the youTrack url without ending slash
66     */
67    function getYouTrackURL() {
68        $url = $this->getConf('youtrack_external_url');
69        return substr($url, -1) === '/' ? substr($url, 0, strlen($url)-1) : $url;
70    }
71
72
73    /**
74     * function to perform login
75     */
76    function loginYouTrack($url, $cookiejar) {
77        $content = $this->CallAPI("POST", "$url/rest/user/login",
78            array("login" => $this->getConf('login_user'),
79                "password" => $this->getConf('login_pass')), $cookiejar);
80
81        return (simplexml_load_string($content) == "ok");
82    }
83
84    /**
85     * function to fetch issue details from YouTrack
86     */
87
88    function fetchYouTrackIssue($id) {
89
90        if ($id == null || $id == "")
91            return null;
92        $url = $this->getYouTrackURL();
93        if ($url == '')
94            return null;
95
96        $cookiejar = false;
97        // check if should login
98        if ($this->getConf('should_login')) {
99            $cookiejar = tempnam (sys_get_temp_dir(), "CURLCOOKIE");
100            // do login
101            if (!$this->loginYouTrack($url, $cookiejar)) {
102                throw new Exception("Logindata not correct, or REST login is not enabled.");
103            }
104        }
105        // fetch issue
106        $xml = $this->CallAPI("GET", $url . "/rest/issue/$id", false, $cookiejar);
107        if ($xml == null || $xml == '')
108            return null;
109        // clean jar
110        if ($cookiejar)
111            unlink($cookiejar) or die("Can't unlink $cookiejar");
112        return simplexml_load_string($xml);
113
114    }
115
116    /**
117     * function to generate html link to the issue with name of issue
118     */
119    function generateLinkForIssue($issue) {
120
121        if ($issue == null || $issue['0'] == "You are not logged in.") {
122            return null;
123        }
124
125        $url = $this->getYouTrackURL();
126        $target = $this->getConf('open_in_new_window') ? '_blank' : '_self';
127
128        $resolved = false;
129
130        $id = $issue->attributes()->id;
131        foreach($issue as $key => $field) {
132            if ($field->attributes()->name == 'summary') {
133                $summary = $field->value;
134                break;
135            }
136            if ($field->attributes()->name == 'resolved') {
137                $resolved = true;
138            }
139        }
140
141        if (!$resolved)
142            foreach($issue as $key => $field) {
143                if ($field->attributes()->name == 'resolved') {
144                    $resolved = true;
145                    break;
146                }
147            }
148
149        return "<span" . ($resolved ? " style='text-decoration: line-through;'" : "") . "><a target='$target' href='$url/issue/$id'><b>$id</b> $summary</a></span> ";
150    }
151
152
153}