1<?php
2require_once("ZoteroConfigException.php");
3
4abstract class ZoteroConfig
5{
6	const FILE_ZOTERO_IDS = "ZoteroIds.txt";
7	const FILE_ZOTERO_ENTRIES = "ZoteroEntries.txt";
8	const ENTRY_URL = "http://www.zotero.org/USERNAME/items/ENTRYID";
9	const ENTRIES_URL = "https://api.zotero.org/users/USERID/items?content=json&itemType=-attachment&key=ZOTEROKEY";
10
11	protected $config = array();
12
13	public function __construct()
14	{
15		if (!$this->usernameIsValid())
16		{
17			throw new ZoteroConfigException("Invalid Zotero username in config file.");
18		}
19
20		if ($this->autoupdateIsActivated() && !$this->keyIsValid())
21		{
22			throw new ZoteroConfigException("Invalid Zotero key in config file.");
23		}
24
25		if (!isset($this->config['WikiOutput']['parentheses']) || $this->config['WikiOutput']['parentheses'] == "")
26		{
27			$this->config['WikiOutput']['parentheses'] = ",";
28		}
29
30		if (!isset($this->config['SourceEntries']['authorFormat']) || $this->config['SourceEntries']['authorFormat'] == "")
31		{
32			$this->config['SourceEntries']['authorFormat'] = "LASTNAME, FIRSTNAME";
33		}
34		if (!isset($this->config['WikiOutput']['titleFormat']) || $this->config['WikiOutput']['titleFormat'] == "")
35		{
36			$this->config['WikiOutput']['titleFormat'] = "AUTHOR: TITLE (DATE)";
37		}
38	}
39
40	private function usernameIsValid()
41	{
42		return isset($this->config['ZoteroAccess']['username']) && $this->config['ZoteroAccess']['username'] != "" && $this->config['ZoteroAccess']['username'] != "YOURUSERNAME";
43	}
44
45	private function keyIsValid()
46	{
47		return isset($this->config['ZoteroAccess']['key']) && $this->config['ZoteroAccess']['key'] != "" && $this->config['ZoteroAccess']['key'] != "YOURZOTEROKEY";
48	}
49
50	private function autoupdateIsActivated()
51	{
52		return isset($this->config['ZoteroAccess']['autoupdate']) && $this->config['ZoteroAccess']['autoupdate'] == true;
53	}
54
55	public function getConfig($category, $key)
56	{
57		$value = @$this->config[$category][$key];
58		return $value;
59	}
60
61	public function getCachePage()
62	{
63		$cachePage = $this->config['SourceEntries']['cachePage'];
64		if ($cachePage != "")
65		{
66			$wikiPagesDir = dirname(__FILE__) . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR . "pages" . DIRECTORY_SEPARATOR;
67			if (strstr($cachePage, ":"))
68			{
69				$parts = explode(":", $cachePage);
70				$cachePage = $parts[0] . DIRECTORY_SEPARATOR . $parts[1];
71			}
72			$cachePage = realpath($wikiPagesDir) . DIRECTORY_SEPARATOR . $cachePage . ".txt";
73		}
74		else
75		{
76			$cachePage = self::FILE_ZOTERO_ENTRIES;
77		}
78		return $cachePage;
79	}
80
81	public function getUrlForEntry(ZoteroEntry $entry)
82	{
83		$url = str_replace("USERNAME", $this->getConfig("ZoteroAccess", "username"), self::ENTRY_URL);
84		$url = str_replace("ENTRYID", $entry->getZoteroId(), $url);
85		return $url;
86	}
87
88	public function getUrlForEntries()
89	{
90		$url = str_replace("USERID", $this->getConfig("ZoteroAccess", "userid"), self::ENTRIES_URL);
91		$url = str_replace("ZOTEROKEY", $this->getConfig("ZoteroAccess", "key"), $url);
92		return $url;
93	}
94}
95?>
96