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