PageRenderTime 27ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/models/plugins.php

https://github.com/widgetfactory/jce-admin
PHP | 361 lines | 227 code | 85 blank | 49 comment | 48 complexity | 3b186a152c657573f77de994378c4c77 MD5 | raw file
  1. <?php
  2. /**
  3. * @package JCE
  4. * @copyright Copyright (c) 2009-2015 Ryan Demmer. All rights reserved.
  5. * @license GNU/GPL 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  6. * JCE is free software. This version may have been modified pursuant
  7. * to the GNU General Public License, and as distributed it includes or
  8. * is derivative of works licensed under the GNU General Public License or
  9. * other free or open source software licenses.
  10. */
  11. defined('_JEXEC') or die('RESTRICTED');
  12. // load base model
  13. require_once (dirname(__FILE__) . '/model.php');
  14. class WFModelPlugins extends WFModel {
  15. public function getCommands() {
  16. //$xml = JFactory::getXMLParser('Simple');
  17. $file = dirname(__FILE__) . '/commands.xml';
  18. $xml = WFXMLElement::load($file);
  19. $commands = array();
  20. if ($xml) {
  21. //$elements = WFXMLHelper::getElements($xml, 'commands');
  22. foreach ($xml->children() as $command) {
  23. $name = (string) $command->name;
  24. if ($name) {
  25. $commands[$name] = new StdClass();
  26. foreach ($command->children() as $item) {
  27. $key = $item->getName();
  28. $value = (string) $item;
  29. $commands[$name]->$key = $value;
  30. }
  31. $commands[$name]->type = 'command';
  32. }
  33. }
  34. }
  35. return $commands;
  36. }
  37. public function getPlugins() {
  38. jimport('joomla.filesystem.folder');
  39. $plugins = array();
  40. // get core xml
  41. $xml = WFXMLElement::load(dirname(__FILE__) . '/plugins.xml');
  42. if ($xml) {
  43. foreach ($xml->children() as $plugin) {
  44. $name = (string) $plugin->name;
  45. if ($name) {
  46. $plugins[$name] = new StdClass();
  47. foreach ($plugin->children() as $item) {
  48. $key = $item->getName();
  49. $value = (string) $item;
  50. if (is_numeric($value)) {
  51. $value = (int) $value;
  52. }
  53. $plugins[$name]->$key = $value;
  54. }
  55. $plugins[$name]->type = 'plugin';
  56. $plugins[$name]->path = str_replace(JPATH_SITE, '', WF_EDITOR_PLUGINS) . '/' . $name;
  57. }
  58. }
  59. }
  60. unset($xml);
  61. // get all Plugins
  62. $folders = JFolder::folders(WF_EDITOR_PLUGINS, '.', false, true, array_merge(array('.svn', 'CVS'), array_keys($plugins)));
  63. foreach ($folders as $folder) {
  64. $name = basename($folder);
  65. $file = $folder . '/' . $name . '.xml';
  66. if (is_file($file)) {
  67. $xml = WFXMLElement::load($folder . '/' . $name . '.xml');
  68. if ($xml) {
  69. if ((string) $xml->getName() != 'extension' && (string) $xml->getName() != 'install') {
  70. continue;
  71. }
  72. $params = $xml->params;
  73. if (!isset($plugins[$name])) {
  74. $plugins[$name] = new StdClass();
  75. $plugins[$name]->name = $name;
  76. $plugins[$name]->title = (string) $xml->name;
  77. $plugins[$name]->icon = (string) $xml->icon;
  78. $editable = (int) $xml->attributes()->editable;
  79. $plugins[$name]->editable = $editable ? $editable : ($params && count($params->children()) ? 1 : 0);
  80. $row = (int) $xml->attributes()->row;
  81. $plugins[$name]->row = $row ? $row : 4;
  82. $plugins[$name]->core = (int) $xml->attributes()->core ? 1 : 0;
  83. }
  84. // relative path
  85. $plugins[$name]->path = str_replace(JPATH_SITE, '', $folder);
  86. $plugins[$name]->author = (string) $xml->author;
  87. $plugins[$name]->version = (string) $xml->version;
  88. $plugins[$name]->creationdate = (string) $xml->creationDate;
  89. $plugins[$name]->description = (string) $xml->description;
  90. $plugins[$name]->authorUrl = (string) $xml->authorUrl;
  91. $plugins[$name]->type = 'plugin';
  92. }
  93. }
  94. }
  95. return $plugins;
  96. }
  97. /**
  98. * Get a plugin's extensions
  99. * @param object $plugin
  100. * @return
  101. */
  102. public function getExtensions() {
  103. jimport('joomla.filesystem.folder');
  104. jimport('joomla.filesystem.file');
  105. $extensions = array();
  106. // recursively get all extension files
  107. $files = JFolder::files(WF_EDITOR_EXTENSIONS, '\.xml$', true, true);
  108. foreach ($files as $file) {
  109. $object = new StdClass();
  110. $object->folder = basename(dirname($file));
  111. $object->manifest = $file;
  112. $object->plugins = array();
  113. $name = basename($file, '.xml');
  114. $object->name = $name;
  115. $object->description = '';
  116. $object->id = $object->folder . '.' . $object->name;
  117. // get core xml
  118. $xml = WFXMLElement::load($file);
  119. if ($xml) {
  120. if ((string) $xml->getName() != 'extension' && (string) $xml->getName() != 'install') {
  121. continue;
  122. }
  123. $plugins = (string) $xml->plugins;
  124. if ($plugins) {
  125. $object->plugins = explode(',', $plugins);
  126. }
  127. $object->name = (string) $xml->name;
  128. $object->title = (string) $xml->name;
  129. $object->description = (string) $xml->description;
  130. $object->creationdate = (string) $xml->creationDate;
  131. $object->author = (string) $xml->author;
  132. $object->version = (string) $xml->version;
  133. $object->type = (string) $xml->attributes()->folder;
  134. $object->authorUrl = (string) $xml->authorUrl;
  135. $object->folder = (string) $xml->attributes()->folder;
  136. $object->core = (int) $xml->attributes()->core ? 1 : 0;
  137. if ($object->core == 0) {
  138. // load language
  139. $language = JFactory::getLanguage();
  140. $language->load('com_jce_' . $object->folder . '_' . $name, JPATH_SITE);
  141. }
  142. $object->extension = $name;
  143. $extensions[] = $object;
  144. }
  145. }
  146. return $extensions;
  147. }
  148. /**
  149. * Process import data from XML file
  150. * @param object $file XML file
  151. * @param boolean $install Can be used by the package installer
  152. * @return
  153. */
  154. public function processImport($file, $install = false) {
  155. return true;
  156. }
  157. public static function addToProfile($id, $plugin) {
  158. JTable::addIncludePath(dirname(dirname(__FILE__)) . '/tables');
  159. // Add to Default Group
  160. $profile = JTable::getInstance('profiles', 'WFTable');
  161. if ($profile->load($id)) {
  162. // Add to plugins list
  163. $plugins = explode(',', $profile->plugins);
  164. if (!in_array($plugin->name, $plugins)) {
  165. $plugins[] = $plugin->name;
  166. }
  167. $profile->plugins = implode(',', $plugins);
  168. if ($plugin->icon) {
  169. if (in_array($plugin->name, preg_split('/[;,]+/', $profile->rows)) === false) {
  170. // get rows as array
  171. $rows = explode(';', $profile->rows);
  172. if (count($rows)) {
  173. // get key (row number)
  174. $key = count($rows) - 1;
  175. // get row contents as array
  176. $row = explode(',', $rows[$key]);
  177. // add plugin name to end of row
  178. $row[] = $plugin->name;
  179. // add row data back to rows array
  180. $rows[$key] = implode(',', $row);
  181. $profile->rows = implode(';', $rows);
  182. }
  183. }
  184. if (!$profile->store()) {
  185. JError::raiseWarning(100, 'WF_INSTALLER_PLUGIN_PROFILE_ERROR');
  186. }
  187. }
  188. }
  189. return true;
  190. }
  191. public static function removeFromProfile($id, $plugin) {
  192. JTable::addIncludePath(dirname(dirname(__FILE__)) . '/tables');
  193. // Add to Default Group
  194. $profile = JTable::getInstance('profiles', 'WFTable');
  195. if ($profile->load($id)) {
  196. // Add to plugins list
  197. $plugins = explode(',', $profile->plugins);
  198. if (!in_array($plugin->name, $plugins)) {
  199. $plugins[] = $plugin->name;
  200. }
  201. $profile->plugins = implode(',', $plugins);
  202. if ($plugin->icon) {
  203. // check if its in the profile
  204. if (in_array($plugin->name, preg_split('/[;,]+/', $profile->rows))) {
  205. $lists = array();
  206. foreach (explode(';', $profile->rows) as $list) {
  207. $icons = explode(',', $list);
  208. foreach ($icons as $k => $v) {
  209. if ($plugin->name == $v) {
  210. unset($icons[$k]);
  211. }
  212. }
  213. $lists[] = implode(',', $icons);
  214. }
  215. $profile->rows = implode(';', $lists);
  216. }
  217. if (!$profile->store()) {
  218. JError::raiseWarning(100, JText::sprintf('WF_INSTALLER_REMOVE_FROM_GROUP_ERROR', $plugin->name));
  219. }
  220. }
  221. }
  222. return true;
  223. }
  224. /**
  225. * Add index.html files to each folder
  226. * @access private
  227. */
  228. private static function addIndexfiles($path) {
  229. jimport('joomla.filesystem.folder');
  230. jimport('joomla.filesystem.file');
  231. // get the base file
  232. $file = dirname(dirname(__FILE__)) . '/index.html';
  233. if (is_file($file) && is_dir($path)) {
  234. JFile::copy($file, $path . '/' . basename($file));
  235. // admin component
  236. $folders = JFolder::folders($path, '.', true, true);
  237. foreach ($folders as $folder) {
  238. JFile::copy($file, $folder . '/' . basename($file));
  239. }
  240. }
  241. }
  242. public static function postInstall($route, $plugin, $installer) {
  243. $db = JFactory::getDBO();
  244. jimport('joomla.filesystem.folder');
  245. // load the plugin and enable
  246. if (isset($plugin->row) && $plugin->row > 0) {
  247. $query = $db->getQuery(true);
  248. if (is_object($query)) {
  249. $query->select('id')->from('#__wf_profiles')->where('name = ' . $db->Quote('Default') . ' OR id = 1');
  250. } else {
  251. $query = 'SELECT id'
  252. . ' FROM #__wf_profiles'
  253. . ' WHERE name = ' . $db->Quote('Default') . ' OR id = 1';
  254. }
  255. $db->setQuery($query);
  256. $id = $db->loadResult();
  257. if ($id) {
  258. if ($route == 'install') {
  259. // add to profile
  260. self::addToProfile($id, $plugin);
  261. } else {
  262. // remove from profile
  263. self::removeFromProfile($id, $plugin);
  264. }
  265. }
  266. }
  267. if ($route == 'install') {
  268. if ($plugin->type == 'extension') {
  269. $plugin->path = $plugin->path . '/' . $plugin->name;
  270. }
  271. // add index.html files
  272. self::addIndexfiles($plugin->path);
  273. }
  274. return true;
  275. }
  276. }