1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | User Tags - a plugin for Piwigo | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2010-2013 Nicolas Roudaire http://www.nikrou.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | This program is free software; you can redistribute it and/or modify | |
---|
8 | // | it under the terms of the GNU General Public License version2 as | |
---|
9 | // | published by the Free Software Foundation | |
---|
10 | // | | |
---|
11 | // | This program is distributed in the hope that it will be useful, but | |
---|
12 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
13 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
14 | // | General Public License for more details. | |
---|
15 | // | | |
---|
16 | // | You should have received a copy of the GNU General Public License | |
---|
17 | // | along with this program; if not, write to the Free Software | |
---|
18 | // | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
---|
19 | // | MA 02110-1301 USA. | |
---|
20 | // +-----------------------------------------------------------------------+ |
---|
21 | |
---|
22 | class t4u_Config |
---|
23 | { |
---|
24 | private $config = array(); |
---|
25 | protected static $instance; |
---|
26 | |
---|
27 | public function __construct($plugin_dir, $plugin_name) { |
---|
28 | $this->plugin_dir = $plugin_dir; |
---|
29 | $this->plugin_name = $plugin_name; |
---|
30 | |
---|
31 | if (!file_exists($this->get_config_file_dir())) { |
---|
32 | mkgetdir($this->get_config_file_dir()); |
---|
33 | } |
---|
34 | |
---|
35 | if (!file_exists($this->get_config_filename())) { |
---|
36 | $this->setDefaults(); |
---|
37 | $this->save_config(); |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | public static function getInstance() { |
---|
42 | if (!isset(self::$instance)) { |
---|
43 | self::$instance = new t4u_Config(T4U_PLUGIN_ROOT, T4U_PLUGIN_NAME); |
---|
44 | } |
---|
45 | return self::$instance; |
---|
46 | } |
---|
47 | |
---|
48 | |
---|
49 | public function load_config() { |
---|
50 | $x = file_get_contents($this->get_config_filename()); |
---|
51 | if ($x!==false) { |
---|
52 | $c = unserialize($x); |
---|
53 | $this->config = $c; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | public function save_config() { |
---|
58 | file_put_contents($this->get_config_filename(), serialize($this->config)); |
---|
59 | } |
---|
60 | |
---|
61 | private function get_config_file_dir() { |
---|
62 | return $GLOBALS['conf']['data_location'].'/plugins/'; |
---|
63 | } |
---|
64 | |
---|
65 | private function get_config_filename() { |
---|
66 | return $this->get_config_file_dir().basename($this->plugin_dir).'.dat'; |
---|
67 | } |
---|
68 | |
---|
69 | public function __set($key, $value) { |
---|
70 | $this->config[$key] = $value; |
---|
71 | } |
---|
72 | |
---|
73 | public function __get($key) { |
---|
74 | return isset($this->config[$key])?$this->config[$key]:null; |
---|
75 | } |
---|
76 | |
---|
77 | public function setPermission($permission, $value) { |
---|
78 | $this->config['permissions'][$permission] = $value; |
---|
79 | } |
---|
80 | |
---|
81 | public function getPermission($permission) { |
---|
82 | return isset($this->config['permissions'][$permission])?$this->config['permissions'][$permission]:null; |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | public function hasPermission($permission='add') { |
---|
87 | return |
---|
88 | (($this->getPermission($permission)!='') |
---|
89 | and is_autorize_status(get_access_type_status($this->getPermission($permission)))); |
---|
90 | } |
---|
91 | |
---|
92 | public function plugin_admin_menu($menu) { |
---|
93 | array_push($menu, |
---|
94 | array('NAME' => $this->plugin_name, |
---|
95 | 'URL' => get_admin_plugin_menu_link(T4U_PLUGIN_ROOT .'/admin.php') |
---|
96 | ) |
---|
97 | ); |
---|
98 | return $menu; |
---|
99 | } |
---|
100 | |
---|
101 | public function get_admin_help($help_content, $page) { |
---|
102 | return load_language('help/'.$page.'.html', |
---|
103 | $this->plugin_dir .'/', |
---|
104 | array('return'=>true) |
---|
105 | ); |
---|
106 | } |
---|
107 | |
---|
108 | public function getActionUrl($action, $method='POST') { |
---|
109 | $ws = get_root_url(); |
---|
110 | $ws .= 'ws.php?format=json&method=user_tags.tags.list'; |
---|
111 | |
---|
112 | return $ws; |
---|
113 | } |
---|
114 | |
---|
115 | private function setDefaults() { |
---|
116 | include_once $this->plugin_dir.'/include/default_values.inc.php'; |
---|
117 | |
---|
118 | foreach ($default_values as $key => $value) { |
---|
119 | if (empty($this->config[$key])) { |
---|
120 | $this->config[$key] = $value; |
---|
121 | } |
---|
122 | } |
---|
123 | } |
---|
124 | } |
---|
125 | ?> |
---|