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 = cString::getPartOfString($result, 0, cString::getStringLength($result) - 1);
110:
111:
112: return cSecurity::toInteger($result . 0);
113: }
114: }
115: }
116:
117: 118: 119:
120: class PimPlugin extends Item {
121:
122: 123: 124:
125: protected $_error;
126:
127: 128: 129: 130: 131:
132: public function __construct($id = false) {
133: $cfg = cRegistry::getConfig();
134: parent::__construct($cfg['tab']['plugins'], 'idplugin');
135: $this->_error = '';
136: if ($id !== false) {
137: $this->loadByPrimaryKey($id);
138: }
139: }
140:
141: 142: 143: 144: 145: 146: 147:
148: public function setField($name, $value, $bSafe = true) {
149: switch ($name) {
150: case 'idclient':
151: $value = (int) $value;
152: break;
153: case 'active':
154: $value = (int) $value;
155: break;
156: }
157:
158: return parent::setField($name, $value, $bSafe);
159: }
160:
161: 162: 163: 164: 165: 166: 167:
168: public function checkDependedFromOtherPlugins($newOrder) {
169: $cfg = cRegistry::getConfig();
170: $pluginsDir = $cfg['path']['contenido'] . $cfg['path']['plugins'];
171:
172:
173: $pimPluginColl = new PimPluginCollection();
174: $pimPluginColl->setWhere('idplugin', $this->get("idplugin"));
175: $pimPluginColl->query();
176: $pimPluginSql = $pimPluginColl->next();
177: $uuidBase = $pimPluginSql->get('uuid');
178:
179:
180: $pimPluginColl->resetQuery();
181:
182:
183: $dirs = cDirHandler::read($pluginsDir);
184: foreach ($dirs as $dirname) {
185:
186:
187: if (!cFileHandler::exists($pluginsDir . $dirname . DIRECTORY_SEPARATOR . "plugin.xml")) {
188: continue;
189: }
190:
191:
192: $tempXmlContent = cFileHandler::read($pluginsDir . $dirname . DIRECTORY_SEPARATOR . "plugin.xml");
193:
194:
195: $tempXml = simplexml_load_string($tempXmlContent);
196:
197: $dependenciesCount = count($tempXml->dependencies);
198: for ($i = 0; $i < $dependenciesCount; $i++) {
199:
200:
201: $depend = cSecurity::escapeString($tempXml->dependencies->depend[$i]);
202:
203:
204: if ($depend == "") {
205: continue;
206: }
207:
208:
209: foreach ($tempXml->dependencies->depend[$i]->attributes() as $key => $value) {
210:
211:
212: if ($key == "uuid") {
213:
214: $uuidTemp = cSecurity::escapeString($value);
215:
216: if ($uuidBase === $uuidTemp) {
217:
218:
219: $pimPluginColl->setWhere('uuid', $tempXml->general->uuid);
220: $pimPluginColl->setWhere('active', '1');
221: $pimPluginColl->query();
222:
223: if ($pimPluginColl->count() == 0) {
224: continue;
225: }
226:
227: $result = $pimPluginColl->next();
228:
229: if ($newOrder == $result->get('executionorder')) {
230: return false;
231: }
232: }
233: }
234: }
235: }
236: }
237:
238: return true;
239: }
240:
241: 242: 243: 244: 245: 246: 247:
248: public function checkDependenciesToOtherPlugins($newOrder) {
249: $cfg = cRegistry::getConfig();
250: $pluginsDir = $cfg['path']['contenido'] . $cfg['path']['plugins'];
251:
252:
253: $pimPluginColl = new PimPluginCollection();
254: $pimPluginColl->setWhere('idplugin', $this->get("idplugin"));
255: $pimPluginColl->query();
256: $pimPluginSql = $pimPluginColl->next();
257: $folderBase = $pimPluginSql->get('folder');
258: $uuidBase = $pimPluginSql->get('uuid');
259:
260:
261: $pimPluginColl->resetQuery();
262:
263:
264: if (!cFileHandler::exists($pluginsDir . $folderBase . DIRECTORY_SEPARATOR . "plugin.xml")) {
265: return true;
266: }
267:
268:
269: $tempXmlContent = cFileHandler::read($pluginsDir . $folderBase . DIRECTORY_SEPARATOR . "plugin.xml");
270:
271:
272: $tempXml = simplexml_load_string($tempXmlContent);
273:
274:
275: $dependenciesBase = array();
276:
277: $dependenciesCount = count($tempXml->dependencies);
278: for ($i = 0; $i < $dependenciesCount; $i++) {
279:
280: foreach ($tempXml->dependencies->depend[$i]->attributes() as $key => $value) {
281: $dependenciesBase[] = cSecurity::escapeString($value);
282: }
283:
284: }
285:
286:
287: $dirs = cDirHandler::read($pluginsDir);
288: foreach ($dirs as $dirname) {
289:
290:
291: if (!cFileHandler::exists($pluginsDir . $dirname . DIRECTORY_SEPARATOR . "plugin.xml")) {
292: continue;
293: }
294:
295:
296: $tempXmlContent = cFileHandler::read($pluginsDir . $dirname . DIRECTORY_SEPARATOR . "plugin.xml");
297:
298:
299: $tempXml = simplexml_load_string($tempXmlContent);
300:
301: if (in_array($tempXml->general->uuid, $dependenciesBase) === true) {
302:
303: $pimPluginColl->setWhere('uuid', $tempXml->general->uuid);
304: $pimPluginColl->query();
305: $result = $pimPluginColl->next();
306:
307: if ($newOrder == $result->get('executionorder')) {
308: return false;
309: }
310: }
311: }
312:
313: return true;
314: }
315:
316: 317: 318: 319: 320: 321:
322: public function updateExecOrder($newOrder) {
323:
324: $dependendFromOtherPlugins = $this->checkDependedFromOtherPlugins($newOrder);
325: $dependenciesToOtherPlugins = $this->checkDependenciesToOtherPlugins($newOrder);
326:
327: if ($dependendFromOtherPlugins === false || $dependenciesToOtherPlugins === false) {
328: return false;
329: }
330:
331: $oldOrder = $this->get('executionorder');
332: $idplugin = $this->get("idplugin");
333:
334: $this->set('executionorder', $newOrder);
335: $this->store();
336:
337:
338: $pluginColl = new PimPluginCollection();
339: $pluginColl->select('executionorder >= "' . min($newOrder, $oldOrder) . '" AND executionorder <= "' . max($newOrder, $oldOrder) . '" AND idplugin != "' . $idplugin . '"', NULL, 'executionorder');
340:
341: while ($plugin = $pluginColl->next()) {
342: if ($newOrder < $oldOrder) {
343: $plugin->set("executionorder", $plugin->get("executionorder") + 1);
344: $plugin->store();
345: } else if ($oldOrder < $newOrder) {
346: $plugin->set("executionorder", $plugin->get("executionorder") - 1);
347: $plugin->store();
348: }
349: }
350:
351: return true;
352: }
353:
354: 355: 356: 357: 358: 359:
360: public function isPluginAvailable($pluginname) {
361: return $this->loadByMany(array(
362: 'idclient' => cRegistry::getClientId(),
363: 'name' => $pluginname,
364: 'active' => 1
365: ));
366: }
367: }
368: