PageRenderTime 27ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

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