1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
15:
16: 17: 18: 19: 20: 21: 22:
23: class PimPluginCollection extends ItemCollection {
24:
25: 26: 27: 28: 29:
30: public function __construct() {
31: global $cfg;
32: parent::__construct($cfg['tab']['plugins'], 'idplugin');
33: $this->_setItemClass('PimPlugin');
34: }
35:
36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52:
53: public function create($name, $description, $author, $copyright, $mail, $website, $version, $foldername, $uuId, $active, $execOrder = 0) {
54: global $client;
55:
56: $nextId = $this->_getNextId();
57:
58:
59: $item = $this->createNewItem($nextId);
60: $item->set('idclient', $client);
61: $item->set('name', $name);
62: $item->set('description', $description);
63: $item->set('author', $author);
64: $item->set('copyright', $copyright);
65: $item->set('mail', $mail);
66: $item->set('website', $website);
67: $item->set('version', $version);
68: $item->set('folder', $foldername);
69: $item->set('uuid', $uuId);
70: $item->set('installed', date("Y-m-d H:i:s"), false);
71: $item->set('active', $active);
72:
73:
74: if ($execOrder == 0) {
75: $this->select();
76: $execOrder = $this->count();
77: }
78: $item->set("executionorder", $execOrder);
79:
80: $item->store();
81:
82: return $item;
83: }
84:
85: 86: 87: 88: 89:
90: protected function _getNextId() {
91: $cfg = cRegistry::getConfig();
92:
93: $sql = 'SELECT MAX(idplugin) AS id FROM ' . $cfg['tab']['plugins'];
94: $this->db->query($sql);
95:
96: if ($this->db->nextRecord()) {
97:
98: $result = $this->db->f('id');
99:
100:
101: if ($result < 10000) {
102: $result = 10000;
103: }
104:
105:
106: $result = $result + 10;
107:
108:
109: $result = substr($result, 0, strlen($result) - 1);
110:
111:
112: return cSecurity::toInteger($result . 0);
113: }
114: }
115:
116: }
117:
118: 119: 120:
121: class PimPlugin extends Item {
122:
123: 124: 125:
126: protected $_error;
127:
128: 129: 130: 131: 132:
133: public function __construct($id = false) {
134: $cfg = cRegistry::getConfig();
135: parent::__construct($cfg['tab']['plugins'], 'idplugin');
136: $this->_error = '';
137: if ($id !== false) {
138: $this->loadByPrimaryKey($id);
139: }
140: }
141:
142: 143: 144: 145: 146: 147: 148:
149: public function setField($name, $value, $bSafe = true) {
150: switch ($name) {
151: case 'idclient':
152: $value = (int) $value;
153: break;
154: case 'active':
155: $value = (int) $value;
156: break;
157: }
158:
159: return parent::setField($name, $value, $bSafe);
160: }
161:
162: 163: 164: 165: 166: 167: 168:
169: public function checkDependedFromOtherPlugins($newOrder) {
170: $cfg = cRegistry::getConfig();
171: $pluginsDir = $cfg['path']['contenido'] . $cfg['path']['plugins'];
172:
173:
174: $pimPluginColl = new PimPluginCollection();
175: $pimPluginColl->setWhere('idplugin', $this->get("idplugin"));
176: $pimPluginColl->query();
177: $pimPluginSql = $pimPluginColl->next();
178: $uuidBase = $pimPluginSql->get('uuid');
179:
180:
181: $pimPluginColl->resetQuery();
182:
183:
184: $dirs = cDirHandler::read($pluginsDir);
185: foreach ($dirs as $dirname) {
186:
187:
188: if (!cFileHandler::exists($pluginsDir . $dirname . DIRECTORY_SEPARATOR . "plugin.xml")) {
189: continue;
190: }
191:
192:
193: $tempXmlContent = cFileHandler::read($pluginsDir . $dirname . DIRECTORY_SEPARATOR . "plugin.xml");
194:
195:
196: $tempXml = simplexml_load_string($tempXmlContent);
197:
198: $dependenciesCount = count($tempXml->dependencies);
199: for ($i = 0; $i < $dependenciesCount; $i++) {
200:
201:
202: $depend = cSecurity::escapeString($tempXml->dependencies->depend[$i]);
203:
204:
205: if ($depend == "") {
206: continue;
207: }
208:
209:
210: foreach ($tempXml->dependencies->depend[$i]->attributes() as $key => $value) {
211:
212:
213: if ($key == "uuid") {
214:
215: $uuidTemp = cSecurity::escapeString($value);
216:
217: if ($uuidBase === $uuidTemp) {
218:
219:
220: $pimPluginColl->setWhere('uuid', $tempXml->general->uuid);
221: $pimPluginColl->setWhere('active', '1');
222: $pimPluginColl->query();
223:
224: if ($pimPluginColl->count() == 0) {
225: continue;
226: }
227:
228: $result = $pimPluginColl->next();
229:
230: if ($newOrder == $result->get('executionorder')) {
231: return false;
232: }
233: }
234: }
235: }
236: }
237: }
238:
239: return true;
240: }
241:
242: 243: 244: 245: 246: 247: 248:
249: public function checkDependenciesToOtherPlugins($newOrder) {
250: $cfg = cRegistry::getConfig();
251: $pluginsDir = $cfg['path']['contenido'] . $cfg['path']['plugins'];
252:
253:
254: $pimPluginColl = new PimPluginCollection();
255: $pimPluginColl->setWhere('idplugin', $this->get("idplugin"));
256: $pimPluginColl->query();
257: $pimPluginSql = $pimPluginColl->next();
258: $folderBase = $pimPluginSql->get('folder');
259: $uuidBase = $pimPluginSql->get('uuid');
260:
261:
262: $pimPluginColl->resetQuery();
263:
264:
265: if (!cFileHandler::exists($pluginsDir . $folderBase . DIRECTORY_SEPARATOR . "plugin.xml")) {
266: return true;
267: }
268:
269:
270: $tempXmlContent = cFileHandler::read($pluginsDir . $folderBase . DIRECTORY_SEPARATOR . "plugin.xml");
271:
272:
273: $tempXml = simplexml_load_string($tempXmlContent);
274:
275:
276: $dependenciesBase = array();
277:
278: $dependenciesCount = count($tempXml->dependencies);
279: for ($i = 0; $i < $dependenciesCount; $i++) {
280:
281: foreach ($tempXml->dependencies->depend[$i]->attributes() as $key => $value) {
282: $dependenciesBase[] = cSecurity::escapeString($value);
283: }
284:
285: }
286:
287:
288: $dirs = cDirHandler::read($pluginsDir);
289: foreach ($dirs as $dirname) {
290:
291:
292: if (!cFileHandler::exists($pluginsDir . $dirname . DIRECTORY_SEPARATOR . "plugin.xml")) {
293: continue;
294: }
295:
296:
297: $tempXmlContent = cFileHandler::read($pluginsDir . $dirname . DIRECTORY_SEPARATOR . "plugin.xml");
298:
299:
300: $tempXml = simplexml_load_string($tempXmlContent);
301:
302: if (in_array($tempXml->general->uuid, $dependenciesBase) === true) {
303:
304: $pimPluginColl->setWhere('uuid', $tempXml->general->uuid);
305: $pimPluginColl->query();
306: $result = $pimPluginColl->next();
307:
308: if ($newOrder == $result->get('executionorder')) {
309: return false;
310: }
311: }
312: }
313:
314: return true;
315: }
316:
317: 318: 319: 320: 321: 322:
323: public function updateExecOrder($newOrder) {
324:
325: $dependendFromOtherPlugins = $this->checkDependedFromOtherPlugins($newOrder);
326: $dependenciesToOtherPlugins = $this->checkDependenciesToOtherPlugins($newOrder);
327:
328: if ($dependendFromOtherPlugins === false || $dependenciesToOtherPlugins === false) {
329: return false;
330: }
331:
332: $oldOrder = $this->get('executionorder');
333: $idplugin = $this->get("idplugin");
334:
335: $this->set('executionorder', $newOrder);
336: $this->store();
337:
338:
339: $pluginColl = new PimPluginCollection();
340: $pluginColl->select('executionorder >= "' . min($newOrder, $oldOrder) . '" AND executionorder <= "' . max($newOrder, $oldOrder) . '" AND idplugin != "' . $idplugin . '"', NULL, 'executionorder');
341:
342: while ($plugin = $pluginColl->next()) {
343: if ($newOrder < $oldOrder) {
344: $plugin->set("executionorder", $plugin->get("executionorder") + 1);
345: $plugin->store();
346: } else if ($oldOrder < $newOrder) {
347: $plugin->set("executionorder", $plugin->get("executionorder") - 1);
348: $plugin->store();
349: }
350: }
351:
352: return true;
353: }
354: }
355: