module_handling.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2017 The OGP Development Team
  6. *
  7. * http://www.opengamepanel.org/
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. */
  24. function list_available_modules()
  25. {
  26. return makefilelist("modules/", ".|..|.svn", true, "folders");
  27. }
  28. /// \return -2 Error while handling db queries after installation.
  29. /// \return -1 In case of error.
  30. /// \return 0 If module already installed.
  31. /// \return 1 If module installation was successfull.
  32. /// \return 2 If module installation was optional and module was not installed.
  33. function install_module($db, $module, $install_if_optional = TRUE)
  34. {
  35. // each module must have module.php file which contains the required variables.
  36. /// \todo We might want to turn this to XML file.
  37. if ( !is_file("modules/".$module."/module.php") )
  38. {
  39. print_failure("modules/".$module." ".get_lang("module_file_missing")."");
  40. return -1;
  41. }
  42. include("modules/".$module."/module.php");
  43. // Check that required fields exist.
  44. if ( !isset($module_title,$module_version) )
  45. {
  46. print_failure("modules/".$module."/module.php ".get_lang("module_file_missing_info")."");
  47. return -1;
  48. }
  49. if ( $db->isModuleInstalled($module) )
  50. return 0;
  51. // Check if the module should be installed or not.
  52. if ( $install_if_optional == FALSE && $module_required == FALSE )
  53. return 2;
  54. echo "<p>".get_lang_f('adding_module',$module_title)."</p>";
  55. $module_id = $db->addModule($module_title, $module, $module_version, $db_version);
  56. if ( isset( $install_queries ) )
  57. {
  58. foreach ( $install_queries as $key_db_version => $querys )
  59. {
  60. foreach ( $querys as $query )
  61. {
  62. if ( $db->query($query) )
  63. continue;
  64. print_failure("".get_lang("query_failed")." (".$query.") ".get_lang("query_failed_2")." ERROR: ".$db->getError()."");
  65. return -2;
  66. }
  67. }
  68. }
  69. if ( isset($module_menus) && is_array($module_menus) )
  70. {
  71. foreach( $module_menus as $menu )
  72. {
  73. $db->addModuleMenu($module_id,$menu['subpage'],$menu['group'],$menu['name']);
  74. }
  75. }
  76. return 1;
  77. }
  78. function uninstall_module($db, $module_id, $module, $adminOverride = false)
  79. {
  80. // Don't allow users to uninstall core IMPORTANT MODULES
  81. if(!isCoreModule($module) || $adminOverride === true){
  82. if ( !is_file("modules/".$module."/module.php") )
  83. {
  84. print_failure("modules/".$module." ".get_lang("module_file_missing")."");
  85. return FALSE;
  86. }
  87. if ( $db->delModule($module_id) === FALSE )
  88. {
  89. print_failure(get_lang("failed_del_db"));
  90. return FALSE;
  91. }
  92. include("modules/".$module."/module.php");
  93. if ( isset( $uninstall_queries ) )
  94. {
  95. foreach ( $uninstall_queries as $query )
  96. {
  97. if ( !$db->query($query) )
  98. {
  99. print_failure("".get_lang("query_failed")." (".$query.") ".get_lang("query_failed_2")." ERROR: ".$db->getError()."");
  100. return FALSE;
  101. }
  102. }
  103. }
  104. return TRUE;
  105. }
  106. return false;
  107. }
  108. function update_module($db, $module_id, $module)
  109. {
  110. if ( !is_file("modules/".$module."/module.php") )
  111. {
  112. print_failure("modules/".$module." ".get_lang("module_file_missing")."");
  113. return FALSE;
  114. }
  115. include("modules/".$module."/module.php");
  116. $module_info = $db->getModule($module_id);
  117. if ( $module_info['version'] != $module_version)
  118. {
  119. if(method_exists($db, "getModuleMenu")){ // Added this check for successful updates
  120. // Get position of module so that users don't need to reorder them after updating
  121. $currentModuleMenuInfo = $db->getModuleMenu($module_id);
  122. if($currentModuleMenuInfo !== false && is_array($currentModuleMenuInfo)){
  123. $pos = $currentModuleMenuInfo["pos"];
  124. if(!isset($pos) || empty($pos)){
  125. $pos = 0;
  126. }
  127. }else{
  128. $pos = 0;
  129. }
  130. }else{
  131. $pos = 0;
  132. }
  133. // Debug
  134. // echo "<p>Module position is " . $pos . " for module " . $currentModuleMenuInfo["menu_name"] . " with ID of " . $module_id . "</p>";
  135. $db->delModuleMenu($module_id);
  136. if ( isset($module_menus) && is_array($module_menus) )
  137. {
  138. foreach( $module_menus as $menu )
  139. {
  140. $db->addModuleMenu($module_id,$menu['subpage'],$menu['group'],$menu['name'],$pos);
  141. }
  142. }
  143. }
  144. if ( $module_info['db_version'] < $db_version)
  145. {
  146. $i = $module_info['db_version'];
  147. while ($i < $db_version)
  148. {
  149. if(isset($install_queries))
  150. {
  151. foreach ( $install_queries[$i+1] as $query )
  152. {
  153. if ( $db->query($query) )
  154. continue;
  155. print_failure("".get_lang("query_failed")." (".$query.") ".get_lang("query_failed_2")." ERROR: ".$db->getError()."");
  156. return -2;
  157. }
  158. }
  159. ++$i;
  160. }
  161. }
  162. $db->updateModule($module_id, $module_version, $db_version);
  163. echo "<p>".get_lang_f('updated_module',$module_title)." - ".$module_info['db_version'] ." to ". $db_version."</p>";
  164. }
  165. ?>