PageRenderTime 34ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/packages/actions/addpackage.act.php

http://awarenet.googlecode.com/
PHP | 198 lines | 105 code | 34 blank | 59 comment | 44 complexity | 31f7c9b5b2112a22a49c5ef2b32fc36f MD5 | raw file
Possible License(s): GPL-3.0
  1. <?
  2. require_once($kapenta->installPath . 'modules/packages/inc/kupdatemanager.class.php');
  3. require_once($kapenta->installPath . 'modules/packages/inc/kpackage.class.php');
  4. require_once($kapenta->installPath . 'modules/packages/inc/ksource.class.php');
  5. //--------------------------------------------------------------------------------------------------
  6. //* add a package / install on this instance
  7. //--------------------------------------------------------------------------------------------------
  8. //post: action - set to addPackage [string]
  9. //post: UID - UID of package on repository [string]
  10. //post: username - respository username [string]
  11. //post: password - repository password [string]
  12. //TODO: move most of this to KUpdatemanager object
  13. //----------------------------------------------------------------------------------------------
  14. // check post vars and user role
  15. //----------------------------------------------------------------------------------------------
  16. if ('admin' != $user->role) { $page->do403(); }
  17. $UID = ''; //% UID of package [string]
  18. $username = ''; //% username on repository [string]
  19. $password = ''; //% password on repository [string]
  20. if (false == array_key_exists('action', $_POST)) { $page->do404('Action not given.', true); }
  21. if ('addPackage' != $_POST['action']) { $page->do404('Action not recognized.'); }
  22. if (true == array_key_exists('source', $_POST)) { $source = $_POST['source']; }
  23. if (true == array_key_exists('UID', $_POST)) { $UID = $_POST['UID']; }
  24. if (true == array_key_exists('username', $_POST)) { $username = $_POST['username']; }
  25. if (true == array_key_exists('password', $_POST)) { $username = $_POST['password']; }
  26. if ('' == trim($UID)) { $page->do404('UID not given.'); }
  27. $um = new KUpdateManager();
  28. $package = new KPackage($UID);
  29. $package->username = $username;
  30. $package->password = $username;
  31. $source = $package->source;
  32. //----------------------------------------------------------------------------------------------
  33. // start HTML output
  34. //----------------------------------------------------------------------------------------------
  35. echo $theme->expandBlocks('[[:theme::ifscrollheader::title=Install Package:]]', ''); flush();
  36. echo "<h1>Installing package: " . $package->name . " ($UID)</h1>";
  37. //----------------------------------------------------------------------------------------------
  38. // add the source
  39. //----------------------------------------------------------------------------------------------
  40. if (false == $um->hasSource($source)) {
  41. $um->log('Adding source: ' . $source, 'green');
  42. $um->addSource($source);
  43. }
  44. //----------------------------------------------------------------------------------------------
  45. // initialize in registry
  46. //----------------------------------------------------------------------------------------------
  47. $um->setPackageField($UID, 'source', $source);
  48. $um->setPackageField($UID, 'name', 'unknown');
  49. $um->setPackageField($UID, 'status', 'manual');
  50. $um->setPackageField($UID, 'user', $username);
  51. $um->setPackageField($UID, 'user', $password);
  52. //----------------------------------------------------------------------------------------------
  53. // try to download the package manifest
  54. //----------------------------------------------------------------------------------------------
  55. $um->log("<b>Repository:</b> $source");
  56. $um->log("Downloading package manifest...");
  57. $check = $package->updateFromRepository();
  58. if (false == $check) {
  59. $msg ="<b>Could not download package manifest:</b><br/>" . $package->manifestUrl . '<br/>';
  60. $um->log($msg, 'red');
  61. if (false == $package->loaded) {
  62. $um->log("Module not installed, please check details and retry.<br/>", 'red');
  63. echo $theme->expandBlocks('[[:theme::ifscrollfooter:]]', '');
  64. die();
  65. }
  66. }
  67. //----------------------------------------------------------------------------------------------
  68. // manifest loaded and saved, display file list
  69. //----------------------------------------------------------------------------------------------
  70. foreach($package->files as $file) { echo $file['path'] . "<br/>\n"; }
  71. $um->log("Downloaded package manifest.<br/>" . count($package->files) . ' files.', 'green');
  72. //----------------------------------------------------------------------------------------------
  73. // try download all outstanding files
  74. //----------------------------------------------------------------------------------------------
  75. $allOk = true;
  76. foreach($package->files as $file) {
  77. $msg = ''
  78. . '<b>file:</b> ' . $file['path'] . '<br/>'
  79. . '<b>sha1:</b> ' . $file['hash'] . '<br/>'
  80. . '<b>type:</b> ' . $file['type'] . '<br/>'
  81. . '<b>size:</b> ' . $file['size'] . '<br/>';
  82. if (false == file_exists($kapenta->installPath . $file['path'])) {
  83. $check = $package->updateFile($file['uid']);
  84. if (true == $check) { $um->log("Downloaded: " . $file['path'], 'green'); }
  85. else { $um->log("Download failed: " . $file['path'], 'red'); }
  86. } else {
  87. $msg .= "file exists...<br/>";
  88. echo $msg;
  89. }
  90. }
  91. if (false == $allOk) {
  92. $um->log("Some files could not be downloaded, aborting installation...", 'red');
  93. echo "<h2>Done.</h2>";
  94. echo $theme->expandBlocks('[[:theme::ifscrollfooter:]]', '');
  95. die();
  96. }
  97. //----------------------------------------------------------------------------------------------
  98. // call install function
  99. //----------------------------------------------------------------------------------------------
  100. if ('' != $package->installFile) {
  101. if (true == $kapenta->fileExists($package->installFile)) {
  102. //--------------------------------------------------------------------------------------
  103. // install script present, include it
  104. //--------------------------------------------------------------------------------------
  105. require_once($kapenta->installPath . $package->installFile);
  106. if (true == function_exists($package->installFn)) {
  107. //----------------------------------------------------------------------------------
  108. // install function present, run it
  109. //----------------------------------------------------------------------------------
  110. $fnName = $package->installFn;
  111. $report = $fnName();
  112. if (false == strpos($report, '<!-- error -->')) {
  113. //------------------------------------------------------------------------------
  114. // install script reports no errors
  115. //------------------------------------------------------------------------------
  116. echo "<div class='chatmessagegreeen'>$report</div>";
  117. } else {
  118. //------------------------------------------------------------------------------
  119. // install errors, abort
  120. //------------------------------------------------------------------------------
  121. echo "<div class='chatmessagered'>$report</div>"
  122. . "<div class='chatmessagered'>Install script reportes errors,"
  123. . " aborting installation.</div>"
  124. . "<h2>Done.</h2>"
  125. . $theme->expandBlocks('[[:theme::ifscrollfooter:]]', '');
  126. die();
  127. }
  128. } else {
  129. //----------------------------------------------------------------------------------
  130. // install function missing
  131. //----------------------------------------------------------------------------------
  132. echo "<div class='chatmessagered'>Install function missing, "
  133. . "aborting installation.</div>"
  134. . "<h2>Done.</h2>"
  135. . $theme->expandBlocks('[[:theme::ifscrollfooter:]]', '');
  136. die();
  137. }
  138. } else {
  139. //--------------------------------------------------------------------------------------
  140. // install script missing
  141. //--------------------------------------------------------------------------------------
  142. echo "<div class='chatmessagered'>Install file missing, aborting installation.</div>"
  143. . "<h2>Done.</h2>"
  144. . $theme->expandBlocks('[[:theme::ifscrollfooter:]]', '');
  145. die();
  146. }
  147. } else {
  148. //------------------------------------------------------------------------------------------
  149. // no install script defined for this package
  150. //------------------------------------------------------------------------------------------
  151. echo "<div class='chatmessageblack'>This package does not have an install file.</div>";
  152. }
  153. //----------------------------------------------------------------------------------------------
  154. // set registry keys
  155. //----------------------------------------------------------------------------------------------
  156. if (true == $allOk) {
  157. $um->setPackageField($package->UID, 'status', 'installed');
  158. }
  159. //----------------------------------------------------------------------------------------------
  160. // done
  161. //----------------------------------------------------------------------------------------------
  162. echo "<h2>Package installed.</h2>";
  163. echo $theme->expandBlocks('[[:theme::ifscrollfooter:]]', '');
  164. ?>