module_handling.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2018 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. // Prerequisites checking
  52. if(isset($module_prereqs) && is_array($module_prereqs) && count($module_prereqs)){
  53. $prereqPass = true;
  54. $missingPrereqs = "";
  55. $i = 0;
  56. foreach($module_prereqs as $prereq){
  57. if(!preReqInstalled($prereq)){
  58. if($i == 0){
  59. $missingPrereqs .= $prereq["name"];
  60. }else{
  61. $missingPrereqs .= ", " . $prereq["name"];
  62. }
  63. $prereqPass = false;
  64. }
  65. $i++;
  66. }
  67. if(!$prereqPass){
  68. print_failure(get_lang_f("prereqs_missing", $missingPrereqs, $module_title));
  69. return -2;
  70. }
  71. }
  72. // Check if the module should be installed or not.
  73. if ( $install_if_optional == FALSE && $module_required == FALSE )
  74. return 2;
  75. echo "<p>".get_lang_f('adding_module',$module_title)."</p>";
  76. $module_id = $db->addModule($module_title, $module, $module_version, $db_version);
  77. if ( isset( $install_queries ) )
  78. {
  79. foreach ( $install_queries as $key_db_version => $querys )
  80. {
  81. foreach ( $querys as $query )
  82. {
  83. if ( $db->query($query) )
  84. continue;
  85. print_failure("".get_lang("query_failed")." (".$query.") ".get_lang("query_failed_2")." ERROR: ".$db->getError()."");
  86. return -2;
  87. }
  88. }
  89. }
  90. if ( isset($module_menus) && is_array($module_menus) )
  91. {
  92. foreach( $module_menus as $menu )
  93. {
  94. $db->addModuleMenu($module_id,$menu['subpage'],$menu['group'],$menu['name']);
  95. }
  96. }
  97. return 1;
  98. }
  99. function uninstall_module($db, $module_id, $module, $adminOverride = false)
  100. {
  101. // Don't allow users to uninstall core IMPORTANT MODULES
  102. if(!isCoreModule($module) || $adminOverride === true){
  103. if ( !is_file("modules/".$module."/module.php") )
  104. {
  105. print_failure("modules/".$module." ".get_lang("module_file_missing")."");
  106. return FALSE;
  107. }
  108. if ( $db->delModule($module_id) === FALSE )
  109. {
  110. print_failure(get_lang("failed_del_db"));
  111. return FALSE;
  112. }
  113. include("modules/".$module."/module.php");
  114. if ( isset( $uninstall_queries ) )
  115. {
  116. foreach ( $uninstall_queries as $query )
  117. {
  118. if ( !$db->query($query) )
  119. {
  120. print_failure("".get_lang("query_failed")." (".$query.") ".get_lang("query_failed_2")." ERROR: ".$db->getError()."");
  121. return FALSE;
  122. }
  123. }
  124. }
  125. return TRUE;
  126. }
  127. return false;
  128. }
  129. function update_module($db, $module_id, $module)
  130. {
  131. if ( !is_file("modules/".$module."/module.php") )
  132. {
  133. print_failure("modules/".$module." ".get_lang("module_file_missing")."");
  134. return FALSE;
  135. }
  136. include("modules/".$module."/module.php");
  137. $module_info = $db->getModule($module_id);
  138. if ( $module_info['version'] != $module_version)
  139. {
  140. if(method_exists($db, "getModuleMenu")){ // Added this check for successful updates
  141. // Get position of module so that users don't need to reorder them after updating
  142. $currentModuleMenuInfo = $db->getModuleMenu($module_id);
  143. if($currentModuleMenuInfo !== false && is_array($currentModuleMenuInfo)){
  144. $pos = $currentModuleMenuInfo["pos"];
  145. if(!isset($pos) || empty($pos)){
  146. $pos = 0;
  147. }
  148. }else{
  149. $pos = 0;
  150. }
  151. }else{
  152. $pos = 0;
  153. }
  154. // Debug
  155. // echo "<p>Module position is " . $pos . " for module " . $currentModuleMenuInfo["menu_name"] . " with ID of " . $module_id . "</p>";
  156. $db->delModuleMenu($module_id);
  157. if ( isset($module_menus) && is_array($module_menus) )
  158. {
  159. foreach( $module_menus as $menu )
  160. {
  161. $db->addModuleMenu($module_id,$menu['subpage'],$menu['group'],$menu['name'],$pos);
  162. }
  163. }
  164. }
  165. if ( $module_info['db_version'] < $db_version)
  166. {
  167. $i = $module_info['db_version'];
  168. while ($i < $db_version)
  169. {
  170. if(isset($install_queries))
  171. {
  172. foreach ( $install_queries[$i+1] as $query )
  173. {
  174. if ( $db->query($query) )
  175. continue;
  176. print_failure("".get_lang("query_failed")." (".$query.") ".get_lang("query_failed_2")." ERROR: ".$db->getError()."");
  177. return -2;
  178. }
  179. }
  180. ++$i;
  181. }
  182. }
  183. $db->updateModule($module_id, $module_version, $db_version);
  184. echo "<p>".get_lang_f('updated_module',$module_title)." - ".$module_info['db_version'] ." to ". $db_version."</p>";
  185. }
  186. ?>