1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8define('SF_CACHE', 48); # in hours
9
10function wikiplugin_sf_info()
11{
12	return [
13		'name' => tra('SourceForge'),
14		'documentation' => 'PluginSF',
15		'description' => tra('Creates a link to SourceForge tracker items'),
16		'prefs' => [ 'wikiplugin_sf' ],
17		'body' => tra('text'),
18		'iconname' => 'link',
19		'introduced' => 1,
20		'params' => [
21			'groupid' => [
22				'required' => true,
23				'name' => tra('Group ID'),
24				'description' => tra('SourceForge project ID (shows as group_id in the URL of a tracker item'),
25				'since' => '1',
26				'filter' => 'digits',
27				'default' => '',
28			],
29			'trackerid' => [
30				'required' => true,
31				'name' => tra('Tracker ID'),
32				'description' => tra('SourceForge tracker ID (shows as atid in the URL of a tracker item'),
33				'since' => '1',
34				'filter' => 'digits',
35				'default' => '',
36				'profile_reference' => 'tracker',
37			],
38			'itemid' => [
39				'required' => true,
40				'name' => tra('Item ID'),
41				'description' => tra('SourceForge item ID (shows as aid in the URL of a tracker item'),
42				'since' => '1',
43				'filter' => 'digits',
44				'default' => '',
45				'profile_reference' => 'tracker_item',
46			],
47			'title' => [
48				'required' => false,
49				'name' => tra('Link title'),
50				'description' => tra('First part of link tooltip identifying the type of tracker item (bug, feature
51					request, patch or support request).'),
52				'filter' => 'alpha',
53				'default' => 'Item',
54				'since' => 7.0,
55			],
56			],
57	];
58}
59
60function get_artifact_label($gid, $atid, $aid, $reload = false)
61{
62	$agent = $_SERVER['HTTP_USER_AGENT'];
63	$cachefile = "temp/sftrackers.cache.$gid.$atid.$aid";
64	$cachelimit = time() - 60 * 60 * SF_CACHE;
65	$url = 'http://sourceforge.net/tracker/index.php?func=detail&aid=' . $aid . '&group_id=' . $gid . '&atid=' . $atid;
66	if (! is_file($cachefile)) {
67		$reload = true;
68	}
69	$back = false;
70	if ($reload or (filemtime($cachefile) < $cachelimit)) {
71		$ch = curl_init($url);
72		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
73		curl_setopt($ch, CURLOPT_USERAGENT, $agent);
74		curl_setopt($ch, CURLOPT_REFERER, $url);
75		$buffer = curl_exec($ch);
76		curl_close($ch);
77		if (preg_match("/<title>[^-]*-([^<]*)<\/title>/i", $buffer, $match)) {
78			$fp = fopen($cachefile, "wb");
79			fputs($fp, $match[1]);
80			fclose($fp);
81		} elseif (is_file($cachefile)) {
82			$fp = fopen($cachefile, "rb");
83			$back = fgets($fp);
84			fclose($fp);
85		}
86	} else {
87		$fp = fopen($cachefile, "rb");
88		$back = fgets($fp, 4096);
89		fclose($fp);
90	}
91	return $back;
92}
93
94function wikiplugin_sf($data, $params)
95{
96	if (function_exists('curl_init')) {
97		if (empty($params['itemid']) || empty($params['groupid']) || empty($params['trackerid'])) {
98			return 'Plugin SF failed. One or more of the following parameters are missing: groupid, trackerid or itemid.';
99		}
100		$title = empty($params['title']) ? 'Item' : $params['title'];
101		$label = get_artifact_label($params['groupid'], $params['trackerid'], $params['itemid']);
102		$back = '<a href="http://sourceforge.net/tracker/index.php?func=detail&aid=' . $params['itemid']
103					. '&group_id=' . $params['groupid'] . '&atid=' . $params['trackerid']
104					. '" target="_blank" title="' . $title . ' ' . $params['itemid']
105					. '" class="wiki external" rel="external nofollow">' . $label . '</a>';
106	} else {
107		$back = 'Plugin SF failed: The php-curl module must be loaded to use this plugin.';
108	}
109	return $back;
110}
111