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