PageRenderTime 87ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_kunena/liveupdate/classes/model.php

https://github.com/draganz/Kunena-2.0
PHP | 171 lines | 125 code | 26 blank | 20 comment | 18 complexity | 9261cbc7bd0f89924df393dd62dc57da MD5 | raw file
  1. <?php
  2. /**
  3. * @package LiveUpdate
  4. * @copyright Copyright (c)2010-2012 Nicholas K. Dionysopoulos / AkeebaBackup.com
  5. * @license GNU LGPLv3 or later <http://www.gnu.org/copyleft/lesser.html>
  6. */
  7. defined('_JEXEC') or die();
  8. jimport('joomla.application.component.model');
  9. /**
  10. * The Live Update MVC model
  11. */
  12. class LiveUpdateModel extends JModel
  13. {
  14. public function download()
  15. {
  16. // Get the path to Joomla!'s temporary directory
  17. $jreg = JFactory::getConfig();
  18. if(version_compare(JVERSION, '3.0.0', 'ge')) {
  19. $tmpdir = $jreg->get('tmp_path');
  20. } else {
  21. $tmpdir = $jreg->getValue('config.tmp_path');
  22. }
  23. jimport('joomla.filesystem.folder');
  24. // Make sure the user doesn't use the system-wide tmp directory. You know, the one that's
  25. // being erased periodically and will cause a real mess while installing extensions (Grrr!)
  26. if(realpath($tmpdir) == '/tmp') {
  27. // Someone inform the user that what he's doing is insecure and stupid, please. In the
  28. // meantime, I will fix what is broken.
  29. $tmpdir = JPATH_SITE.'/tmp';
  30. } // Make sure that folder exists (users do stupid things too often; you'd be surprised)
  31. elseif(!JFolder::exists($tmpdir)) {
  32. // Darn it, user! WTF where you thinking? OK, let's use a directory I know it's there...
  33. $tmpdir = JPATH_SITE.'/tmp';
  34. }
  35. // Oki. Let's get the URL of the package
  36. $updateInfo = LiveUpdate::getUpdateInformation();
  37. $config = LiveUpdateConfig::getInstance();
  38. $auth = $config->getAuthorization();
  39. $url = $updateInfo->downloadURL;
  40. // Sniff the package type. If sniffing is impossible, I'll assume a ZIP package
  41. $basename = basename($url);
  42. if(strstr($basename,'?')) {
  43. $basename = substr($basename, strstr($basename,'?')+1);
  44. }
  45. if(substr($basename,-4) == '.zip') {
  46. $type = 'zip';
  47. } elseif(substr($basename,-4) == '.tar') {
  48. $type = 'tar';
  49. } elseif(substr($basename,-4) == '.tgz') {
  50. $type = 'tar.gz';
  51. } elseif(substr($basename,-7) == '.tar.gz') {
  52. $type = 'tar.gz';
  53. } else {
  54. $type = 'zip';
  55. }
  56. // Cache the path to the package file and the temp installation directory in the session
  57. $target = $tmpdir.'/'.$updateInfo->extInfo->name.'.update.'.$type;
  58. $tempdir = $tmpdir.'/'.$updateInfo->extInfo->name.'_update';
  59. $session = JFactory::getSession();
  60. $session->set('target', $target, 'liveupdate');
  61. $session->set('tempdir', $tempdir, 'liveupdate');
  62. // Let's download!
  63. require_once dirname(__FILE__).'/download.php';
  64. return LiveUpdateDownloadHelper::download($url, $target);
  65. }
  66. public function extract()
  67. {
  68. $session = JFactory::getSession();
  69. $target = $session->get('target', '', 'liveupdate');
  70. $tempdir = $session->get('tempdir', '', 'liveupdate');
  71. jimport('joomla.filesystem.archive');
  72. return JArchive::extract( $target, $tempdir);
  73. }
  74. public function install()
  75. {
  76. $session = JFactory::getSession();
  77. $tempdir = $session->get('tempdir', '', 'liveupdate');
  78. jimport('joomla.installer.installer');
  79. jimport('joomla.installer.helper');
  80. $installer = JInstaller::getInstance();
  81. $packageType = JInstallerHelper::detectType($tempdir);
  82. if(!$packageType) {
  83. $msg = JText::_('LIVEUPDATE_INVALID_PACKAGE_TYPE');
  84. $result = false;
  85. } elseif (!$installer->install($tempdir)) {
  86. // There was an error installing the package
  87. $msg = JText::sprintf('LIVEUPDATE_INSTALLEXT', JText::_($packageType), JText::_('LIVEUPDATE_Error'));
  88. $result = false;
  89. } else {
  90. // Package installed sucessfully
  91. $msg = JText::sprintf('LIVEUPDATE_INSTALLEXT', JText::_($packageType), JText::_('LIVEUPDATE_Success'));
  92. $result = true;
  93. }
  94. $app = JFactory::getApplication();
  95. $app->enqueueMessage($msg);
  96. $this->setState('result', $result);
  97. $this->setState('packageType', $packageType);
  98. if($packageType) {
  99. $this->setState('name', $installer->get('name'));
  100. $this->setState('message', $installer->message);
  101. if(version_compare(JVERSION,'1.6.0','ge')) {
  102. $this->setState('extmessage', $installer->get('extension_message'));
  103. } else {
  104. $this->setState('extmessage', $installer->get('extension.message'));
  105. }
  106. }
  107. return $result;
  108. }
  109. public function cleanup()
  110. {
  111. $session = JFactory::getSession();
  112. $target = $session->get('target', '', 'liveupdate');
  113. $tempdir = $session->get('tempdir', '', 'liveupdate');
  114. jimport('joomla.installer.helper');
  115. JInstallerHelper::cleanupInstall($target, $tempdir);
  116. $session->clear('target','liveupdate');
  117. $session->clear('tempdir','liveupdate');
  118. }
  119. public function getSRPURL($return = '')
  120. {
  121. $session = JFactory::getSession();
  122. $tempdir = $session->get('tempdir', '', 'liveupdate');
  123. jimport('joomla.installer.installer');
  124. jimport('joomla.installer.helper');
  125. jimport('joomla.filesystem.file');
  126. $instModelFile = JPATH_ADMINISTRATOR.'/components/com_akeeba/models/installer.php';
  127. if(!JFile::exists($instModelFile)) return false;
  128. require_once JPATH_ADMINISTRATOR.'/components/com_akeeba/models/installer.php';
  129. $model = JModel::getInstance('Installer', 'AkeebaModel');
  130. $packageType = JInstallerHelper::detectType($tempdir);
  131. $name = $model->getExtensionName($tempdir);
  132. $url = 'index.php?option=com_akeeba&view=backup&tag=restorepoint&type='.$packageType.'&name='.urlencode($name['name']);
  133. switch($packageType) {
  134. case 'module':
  135. case 'template':
  136. $url .= '&group='.$name['client'];
  137. break;
  138. case 'plugin':
  139. $url .= '&group='.$name['group'];
  140. break;
  141. }
  142. if(!empty($return)) $url .= '&returnurl='.urlencode($return);
  143. return $url;
  144. }
  145. }