Repository navigation
Expand file tree
/
Copy pathPluginConfig.class.php
More file actions
150 lines (133 loc) · 5.03 KB
/
Copy pathPluginConfig.class.php
File metadata and controls
150 lines (133 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php defined('IN_MANAGER_MODE') or die('<h1>ERROR:</h1><p>Please use the MODx Content Manager instead of accessing this file directly.</p>');
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//
// PluginConfig class
// for MODx Evolution CMF
//
// Reads the configuration string of a MODx plugin from the database and makes
// it usable as an array
//
// @version 1.0
// @license http://www.gnu.org/copyleft/gpl.html GNU Public License (GPL)
// @author sam (sam@gmx-topmail.de)
// @www https://github.com/Sammyboy/TreeSelectTV-plugin
//
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
class PluginConfig {
/**
* Initialize the PluginConfig class
*
* @param $pluginText: Identification string inside of the plugin code
*/
function PluginConfig($pluginText) {
$this->pluginInfo = $this->getInfo($pluginText);
$this->config = $this->getConfig();
}
/**
* Returns plugin informations: id, name, version, author, license, properties
*
* @return array or boolean
*/
function getInfo($pluginText) {
global $modx;
$table = $modx->getFullTableName('site_plugins');
$fields = "id,name,".
"SUBSTR(plugincode, INSTR(plugincode, '@version'), 200) as version,".
"SUBSTR(plugincode, INSTR(plugincode, '@author'), 200) as author,".
"SUBSTR(plugincode, INSTR(plugincode, '@license'), 200) as license,".
"properties";
$where = "plugincode LIKE '%{$pluginText}%'";
$query = $modx->db->select($fields, $table, $where);
$output = $modx->db->makeArray($query);
if (!count($output)) return false;
foreach ($output[0] as $key => $val) {
if ($val[0] == "@")
$output[0][$key] = trim(str_replace("@".$key, '', substr($val, 0, strpos($val, "\n"))));
}
return $output[0];
}
/**
* Deletes an option by name
*
* @return boolean
*/
function deleteOption($name = "") {
if (!strlen($name) || !isset($this->config['type'][$name])) return false;
foreach (array_keys($this->config) as $i) {
if (isset($this->config[$i][$name])) unset($this->config[$i][$name]);
}
return true;
}
/**
* Overwrites an option by name
*
* @return boolean
*/
function setOption($name, $text, $type = "string", $value = null, $options = null) {
if (!strlen($name)) return false;
$this->config['text'][$name] = $text;
$this->config['type'][$name] = $type;
$this->config['values'][$name] = $value;
$this->config['options'][$name] = $options;
return true;
}
/**
* Reads a plugin configuration string and returns it as an array
*
* @return array
*/
function getConfig($config_string = "") {
$opt = array();
if (!strlen($config_string)) $config_string = $this->pluginInfo['properties'];
$configs = explode("&", $config_string);
if (!count($configs)) return false;
foreach ($configs as $config) {
if (!strlen($config)) continue;
list($key, $values) = explode("=", trim($config));
$values = explode(";", $values);
$opt['text'][$key] = $values[0];
$opt['type'][$key] = $values[1];
if ($opt['type'][$key] == 'list') {
$opt['options'][$key] = explode(",", $values[2]);
$opt['values'][$key] = $values[3];
} else $opt['values'][$key] = $values[2];
}
return $opt;
}
/**
* Converts a config-array into a string
*
* @return string
*/
function setConfigString($config = "") {
if (!is_array($config)) $config = isset($this->config) ? $this->config : null;
if (!isset($config) || !is_array($config)) return false;
$output = "";
foreach ($config['text'] as $i => $text) {
$output .= (strlen($output) ? " " : "").
"&{$i}={$text};{$config['type'][$i]};".
(($config['type'][$i] == "list" && isset($config['options'][$i])) ?
(is_array($config['options'][$i]) ?
implode(",", $config['options'][$i])
: $config['options'][$i]).";"
: "").
$config['values'][$i];
}
return $output;
}
/**
* Saves new properties to database
*
* @return boolean
*/
function saveProperties($string = "", $id = 0) {
global $modx;
if ($id === 0) $id = $this->pluginInfo['id'];
if (!isset($id) || !strlen($string)) return false;
$table = $modx->getFullTableName('site_plugins');
$string = $modx->db->escape($string);
$result = $modx->db->update('properties="'.$string.'"', $table, 'id="'.$id.'"');
return $result;
}
}
?>