PageRenderTime 101ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/administrator/components/com_jcomments/install/helpers/installerhelper.php

https://github.com/Shigaru/shigaru
PHP | 307 lines | 227 code | 47 blank | 33 comment | 71 complexity | 4ea3c7a2414a451bf0f4aed86fdf5f9b MD5 | raw file
  1. <?php
  2. /**
  3. * JComments - Joomla Comment System
  4. *
  5. * Service functions for JComments Installer
  6. *
  7. * @static
  8. * @version 2.0
  9. * @package JComments
  10. * @subpackage Installer
  11. * @author Sergey M. Litvinov (smart@joomlatune.ru)
  12. * @copyright (C) 2008 by Sergey M. Litvinov (http://www.joomlatune.ru)
  13. * @license GNU/GPL: http://www.gnu.org/copyleft/gpl.html
  14. *
  15. * If you fork this to create your own project,
  16. * please make a reference to JComments someplace in your code
  17. * and provide a link to http://www.joomlatune.ru
  18. **/
  19. class JCommentsInstallerHelper
  20. {
  21. /**
  22. * Returns component's version info from .xml file
  23. *
  24. * @return object Object with two fields: releaseVersion and releaseDate
  25. */
  26. function getVersionInfo()
  27. {
  28. static $versionInfo;
  29. global $mainframe;
  30. if (!isset($versionInfo)) {
  31. $versionInfo = new StdClass();
  32. $versionInfo->releaseVersion = 'x.x.x.x';
  33. $versionInfo->releaseDate = date('Y');
  34. $file = JCOMMENTS_ADMIN . DS . 'jcomments.xml';
  35. if (!is_file($file)) {
  36. $file = JCOMMENTS_ADMIN . DS . 'jcomments10.xml';
  37. if (!is_file($file)) {
  38. $file = JCOMMENTS_ADMIN . DS . 'jcomments15.xml';
  39. if (!is_file($file)) {
  40. $file = JCOMMENTS_ADMIN . DS . 'manifest.xml';
  41. }
  42. }
  43. }
  44. if (JCOMMENTS_JVERSION == '1.0') {
  45. require_once ($mainframe->getCfg('absolute_path') . DS . 'includes' . DS . 'domit' . DS . 'xml_domit_lite_include.php');
  46. $xmlDoc = new DOMIT_Lite_Document();
  47. $xmlDoc->resolveErrors(false);
  48. if (is_file($file)) {
  49. if ($xmlDoc->loadXML($file, false, true)) {
  50. $root = &$xmlDoc->documentElement;
  51. if (($root->getTagName() == 'mosinstall' || $root->getTagName() == 'install') && ($root->getAttribute("type") == "component")) {
  52. $element = &$root->getElementsByPath('creationDate', 1);
  53. $versionInfo->releaseDate = $element ? $element->getText() : date('Y');
  54. $element = &$root->getElementsByPath('version', 1);
  55. $versionInfo->releaseVersion = $element ? $element->getText() : '';
  56. }
  57. }
  58. }
  59. } else if (JCOMMENTS_JVERSION == '1.5') {
  60. $data = JApplicationHelper::parseXMLInstallFile($file);
  61. $versionInfo->releaseDate = $data['creationdate'];
  62. $versionInfo->releaseVersion = $data['version'];
  63. }
  64. }
  65. return $versionInfo;
  66. }
  67. /**
  68. * Chmods files and directories recursivly to given permissions
  69. *
  70. * @param string $path Root path to begin changing mode [without trailing slash]
  71. * @param string $filemode Octal representation of the value to change file mode to [null = no change]
  72. * @param string $foldermode Octal representation of the value to change folder mode to [null = no change]
  73. * @return boolean True if successful [one fail means the whole operation failed]
  74. */
  75. function setPermissions( $path, $filemode = '0644', $foldermode = '0755' )
  76. {
  77. // Initialize return value
  78. $ret = true;
  79. if (is_dir($path)) {
  80. $dh = opendir($path);
  81. while (($file = readdir($dh)) !== false) {
  82. if ($file != '.' && $file != '..') {
  83. $fullpath = $path . '/' . $file;
  84. if (is_dir($fullpath)) {
  85. if (!JCommentsInstallerHelper::setPermissions($fullpath, $filemode, $foldermode)) {
  86. $ret = false;
  87. }
  88. } else {
  89. if (isset($filemode)) {
  90. if (!@ chmod($fullpath, octdec($filemode))) {
  91. $ret = false;
  92. }
  93. }
  94. }
  95. }
  96. }
  97. closedir($dh);
  98. if (isset($foldermode)) {
  99. if (!@ chmod($path, octdec($foldermode))) {
  100. $ret = false;
  101. }
  102. }
  103. } else {
  104. if (isset($filemode)) {
  105. $ret = @ chmod($path, octdec($filemode));
  106. }
  107. }
  108. return $ret;
  109. }
  110. function deleteFile( $file )
  111. {
  112. if (JCOMMENTS_JVERSION == '1.5') {
  113. jimport('joomla.filesystem.file');
  114. return JFile::delete($file);
  115. } else {
  116. @unlink($file);
  117. }
  118. return true;
  119. }
  120. function copyFile( $src, $dst )
  121. {
  122. if (JCOMMENTS_JVERSION == '1.5') {
  123. jimport('joomla.filesystem.file');
  124. return JFile::copy($src, $dst);
  125. } else {
  126. return @copy($src, $dst);
  127. }
  128. }
  129. function installPlugin($name, $element, $folder, $files, $message, $errorMessage, &$installer)
  130. {
  131. static $ordering, $plugins;
  132. $db =& JCommentsFactory::getDBO();
  133. if (JCOMMENTS_JVERSION == '1.0') {
  134. global $mainframe;
  135. $pluginsName = 'mambots';
  136. $pluginsTable = '#__mambots';
  137. $pluginsDstPath = $mainframe->getCfg('absolute_path') . DS . 'mambots' . DS . $folder;
  138. $pluginsQuery = "INSERT INTO `#__mambots` (`name`, `element`, `folder`, `access`, `ordering`, `published` ) VALUES ('%s', '%s', '%s', 0, %s, 1 );";
  139. $pluginsExt = 'x10';
  140. } else if (JCOMMENTS_JVERSION == '1.5') {
  141. $version = new JVersion();
  142. if (version_compare('1.6', $version->getShortVersion()) <= 0) {
  143. $pluginsName = 'plugins';
  144. $pluginsTable = '#__extensions';
  145. $pluginsDstPath = JPATH_ROOT . DS . 'plugins' . DS . $folder;
  146. $pluginsQuery = "INSERT INTO `#__extensions` (`type`, `name`, `element`, `folder`, `access`, `ordering`, `enabled`) VALUES ('plugin', '%s', '%s', '%s', 1, %s, 1 );";
  147. $pluginsExt = 'x15';
  148. } else {
  149. $pluginsName = 'plugins';
  150. $pluginsTable = '#__plugins';
  151. $pluginsDstPath = JPATH_ROOT . DS . 'plugins' . DS . $folder;
  152. $pluginsQuery = "INSERT INTO `#__plugins` (`name`, `element`, `folder`, `access`, `ordering`, `published` ) VALUES ('%s', '%s', '%s', 0, %s, 1 );";
  153. $pluginsExt = 'x15';
  154. }
  155. }
  156. if (empty($ordering)) {
  157. $db->setQuery("SELECT folder, MAX(ordering) as maxid FROM `" . $pluginsTable . "` GROUP BY `folder`;");
  158. $ordering = @$db->loadObjectList('folder');
  159. //echo $db->getErrorMsg();
  160. }
  161. if (empty($plugins)) {
  162. $db->setQuery("SELECT CONCAT(folder, '.', element) as plugin FROM `" . $pluginsTable . "` WHERE `folder` <> '' order by `folder`;");
  163. $plugins = $db->loadResultArray();
  164. //echo $db->getErrorMsg();
  165. }
  166. $pluginsSrcPath = JCOMMENTS_ADMIN . DS . 'install' . DS . 'plugins' . DS . $folder;
  167. if(!is_writable($pluginsDstPath . DS)) {
  168. @chmod($pluginsDstPath . DS, 0755);
  169. }
  170. $result = true;
  171. $files[] = $element . '.php';
  172. $files[] = $element . '.' . $pluginsExt;
  173. foreach($files as $file) {
  174. $dstFileName = $pluginsDstPath . DS . $file;
  175. $srcFileName = $pluginsSrcPath . DS . $file;
  176. if (strpos($dstFileName, $pluginsExt) !== false) {
  177. $dstFileName = str_replace($pluginsExt, 'xml', $dstFileName);
  178. }
  179. if (is_file($dstFileName)) {
  180. JCommentsInstallerHelper::deleteFile($dstFileName);
  181. }
  182. $result = $result && JCommentsInstallerHelper::copyFile($srcFileName, $dstFileName);
  183. }
  184. if (!in_array($folder . '.' . $element, $plugins)) {
  185. $maxId = isset($ordering[$folder]) ? intval($ordering[$folder]->maxid) + 1 : 0;
  186. $db->setQuery(sprintf($pluginsQuery, $name, $element, $folder, $maxId));
  187. $db->query();
  188. }
  189. if ($message != '') {
  190. $installer->addMessage(JText::_($message), $result);
  191. if (!$result && $errorMessage != '') {
  192. $installer->addWarning(JText::sprintf($errorMessage, $pluginsName));
  193. }
  194. }
  195. }
  196. function uninstallPlugin($element, $folder, $files = array())
  197. {
  198. if (JCOMMENTS_JVERSION == '1.0') {
  199. global $mainframe;
  200. $pluginsTable = '#__mambots';
  201. $pluginsDstPath = $mainframe->getCfg('absolute_path') . DS . 'mambots' . DS . $folder;
  202. } else if (JCOMMENTS_JVERSION == '1.5') {
  203. $version = new JVersion();
  204. if (version_compare('1.6.0.0', $version->getShortVersion()) <= 0) {
  205. $pluginsTable = '#__extensions';
  206. $pluginsDstPath = JPATH_ROOT . DS . 'plugins' . DS . $folder;
  207. } else {
  208. $pluginsTable = '#__plugins';
  209. $pluginsDstPath = JPATH_ROOT . DS . 'plugins' . DS . $folder;
  210. }
  211. }
  212. $files[] = $element . '.php';
  213. $files[] = $element . '.xml';
  214. foreach($files as $file) {
  215. $dstFileName = $pluginsDstPath . DS . $file;
  216. if (is_file($dstFileName)) {
  217. JCommentsInstallerHelper::deleteFile($dstFileName);
  218. }
  219. }
  220. $db =& JCommentsFactory::getDBO();
  221. $db->setQuery("DELETE FROM `" . $pluginsTable . "` WHERE `element` = '" . $element . "' and `folder` = '" . $folder . "';");
  222. $db->query();
  223. }
  224. function extractArchive( $source , $destination )
  225. {
  226. if (JCOMMENTS_JVERSION == '1.0') {
  227. global $mainframe;
  228. require_once( $mainframe->getCfg('absolute_path') . '/administrator/includes/pcl/pclzip.lib.php' );
  229. require_once( $mainframe->getCfg('absolute_path') . '/administrator/includes/pcl/pclerror.lib.php' );
  230. $zipfile = new PclZip( $source );
  231. if((substr(PHP_OS, 0, 3) == 'WIN')) {
  232. define('OS_WINDOWS',1);
  233. } else {
  234. define('OS_WINDOWS',0);
  235. }
  236. return $zipfile->extract(PCLZIP_OPT_PATH, $destination);
  237. } else {
  238. jimport('joomla.filesystem.file');
  239. jimport('joomla.filesystem.folder');
  240. jimport('joomla.filesystem.archive');
  241. jimport('joomla.filesystem.path');
  242. $destination = JPath::clean( $destination );
  243. $source = JPath::clean( $source );
  244. return JArchive::extract($source , $destination);
  245. }
  246. }
  247. function extractJCommentsPlugins()
  248. {
  249. $source = JCOMMENTS_BASE . DS . 'plugins' . DS . 'plugins.zip';
  250. $destination = JCOMMENTS_BASE . DS . 'plugins' . DS;
  251. return JCommentsInstallerHelper::extractArchive($source , $destination);
  252. }
  253. function removeJCommentsPlugins()
  254. {
  255. $source = JCOMMENTS_BASE . DS . 'plugins' . DS . 'plugins.zip';
  256. $destination = JCOMMENTS_BASE . DS . 'plugins' . DS;
  257. return JCommentsInstallerHelper::extractArchive($zip , $destination);
  258. }
  259. }
  260. ?>