PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/admin/includes/classes/module_installer.php

https://github.com/yellow1912/module-manager
PHP | 229 lines | 191 code | 20 blank | 18 comment | 37 complexity | 860fc4947b9ed47e98b4f34924d0ee4c MD5 | raw file
  1. <?php
  2. require_once('sqlpatch_class.php');
  3. class module_installer extends sqlPatch{
  4. var $module_installer_status = false;
  5. var $module_db_info = false;
  6. var $module_installer_path;
  7. var $module_path;
  8. var $module_db_path;
  9. var $module_php_path;
  10. var $module_version_file_path;
  11. var $module_version_name = '';
  12. var $module_patch_level = -1;
  13. var $module_db_patch_files = array();
  14. var $module_php_patch_files = array();
  15. // php4 constructor
  16. function module_installer(){
  17. $this->module_installer_path = DIR_FS_ADMIN.'includes/module_installation/';
  18. $module_installer_status = $this->check_version_tracker_table();
  19. }
  20. function set_module($module_code){
  21. $this->module_code = $module_code;
  22. // init paths
  23. $this->module_path = $this->module_installer_path.$this->module_code.'/';
  24. $this->module_db_path = $this->module_path.'db/';
  25. $this->module_php_path = $this->module_path.'php/';
  26. $this->module_version_file_path = $this->module_path.'current_version.txt';
  27. }
  28. function check_module(){
  29. // get current version
  30. $this->get_current_version();
  31. $this->get_module_db_info();
  32. return $this->check_for_upgrade();
  33. }
  34. function get_module_db_info(){
  35. global $db;
  36. $sql = 'SELECT * FROM '.TABLE_MODULE_VERSION_TRACKER.' WHERE module_code=\''.$this->module_code.'\' LIMIT 1';
  37. require_once(DIR_FS_CATALOG . DIR_WS_CLASSES . 'ri_utility.php');
  38. $this->module_db_info = RIUtility::dbResultToArray($db->Execute($sql), true);
  39. return $this->module_db_info;
  40. }
  41. function get_current_version(){
  42. global $messageStack;
  43. $file_content = @file_get_contents($this->module_version_file_path);
  44. if($file_content !== false){
  45. $temp = explode('|', $file_content);
  46. if(!is_int($temp[0]) && (int)$temp[0] < 0){
  47. // sound the alarm here. version has to be a positive number
  48. $messageStack->add("Version number found in current_version.txt is not a positive integer", 'error');
  49. }
  50. else{
  51. $this->module_patch_level = (int)$temp[0];
  52. if(isset($temp[1]))
  53. $this->module_version_name = $temp[1];
  54. }
  55. }
  56. else
  57. $messageStack->add(sprintf("%s is missing", $this->module_version_file_path), 'warning');
  58. }
  59. function check_for_upgrade(){
  60. global $messageStack;
  61. $result = false;
  62. if($this->module_patch_level == -1){
  63. if($this->module_db_info === false){
  64. // sound the alarm
  65. $messageStack->add("Module version could not be found in file and database", 'error');
  66. }
  67. else
  68. $this->create_version_file();
  69. }
  70. else{
  71. if($this->module_db_info === false){
  72. // not in database yet? = not installed
  73. $this->insert_module_to_db();
  74. $result = -1;
  75. }
  76. elseif($this->module_patch_level > $this->module_db_info['patch_level']){
  77. $messageStack->add("Your module needs to be upgraded. Database patches will be applied!", 'warning');
  78. $result = (int)$this->module_db_info['patch_level'];
  79. }
  80. elseif($this->module_version_name != $this->module_db_info['version_name']){
  81. $messageStack->add("Your module version has been updated! No database patches applied!", 'warning');
  82. $this->update_module_patch_level($this->module_patch_level);
  83. $result = false;
  84. }
  85. }
  86. return $result;
  87. }
  88. function upgrade_module(){
  89. global $messageStack;
  90. if(($current_patch_level = $this->check_module()) === false)
  91. return;
  92. // let upgrade
  93. $this->get_db_patch_files();
  94. $this->get_php_patch_files();
  95. for($patch_level = ($current_patch_level + 1); $patch_level <= $this->module_patch_level; $patch_level++){
  96. // Check if the php patch is available
  97. $update_obj = false;
  98. if(!empty($this->module_php_patch_files[$patch_level])){
  99. require_once($this->module_php_path.$this->module_php_patch_files[$patch_level]);
  100. $update_obj = $this->_get_object("update_$patch_level");
  101. }
  102. // try to run before db_update
  103. if($update_obj != false && method_exists($update_obj, 'before_db_update'))
  104. $update_obj->before_db_update();
  105. // try to run db update
  106. if(!empty($this->module_db_patch_files[$patch_level])){
  107. $file = file($this->module_db_path.$this->module_db_patch_files[$patch_level]);
  108. if($this->execute_sql_file($file)){
  109. if($this->update_module_patch_level($patch_level) > 0)
  110. $messageStack->add("Upgraded database to patch level $patch_level", 'success');
  111. else
  112. $messageStack->add("Database upgraded but failed to update version_tracker", 'error');
  113. }
  114. else
  115. $messageStack->add("Faield to upgrade database to patch level $patch_level", 'error');
  116. }
  117. // try to run after db update
  118. if($update_obj != false && method_exists($update_obj, 'after_db_update'))
  119. $update_obj->after_db_update();
  120. }
  121. }
  122. function update_module_patch_level($patch_level){
  123. global $db;
  124. $data = array('patch_level' => $patch_level, 'version_name' => $this->module_version_name);
  125. zen_db_perform(TABLE_MODULE_VERSION_TRACKER, $data, 'update', 'module_code = \''.$this->module_code.'\'');
  126. return mysql_affected_rows($db->link);
  127. }
  128. // TODO: add checking if insert success
  129. function insert_module_to_db(){
  130. $data = array('module_code' => $this->module_code,
  131. 'patch_level' => -1,
  132. 'version_name' => $this->module_version_name);
  133. zen_db_perform(TABLE_MODULE_VERSION_TRACKER, $data);
  134. }
  135. function create_version_file(){
  136. global $messageStack;
  137. if($handle = @fopen($this->module_version_file_path, "w")){
  138. $content = $this->module_db_info['patch_level'].'|'.$this->module_db_info['version_name'];
  139. if (fwrite($handle, $content) === FALSE) {
  140. $messageStack->add(sprintf("Could write to %s",$this->module_version_file_path), 'error');
  141. }
  142. fclose($handle);
  143. }
  144. else{
  145. $messageStack->add(sprintf("Could not open %s to write",$this->module_version_file_path), 'error');
  146. }
  147. }
  148. function get_db_patch_files(){
  149. $this->module_db_patch_files = $this->_get_patch_files($this->module_db_path, 'sql');
  150. }
  151. function get_php_patch_files(){
  152. $this->module_php_patch_files = $this->_get_patch_files($this->module_php_path, 'php');
  153. }
  154. function _get_patch_files($path, $extension){
  155. global $messageStack;
  156. // create an array to hold file list
  157. $results = array();
  158. // create a handler for the directory
  159. // keep going until all files in directory have been read
  160. if ($handler = @opendir($path)){
  161. while (($file = readdir($handler)) !== false) {
  162. // if $file isn't this directory or its parent,
  163. // add it to the results array
  164. if ($file != '.' && $file != '..'){
  165. $file_parts = explode('.', $file);
  166. if(count($file_parts) > 2)
  167. $messageStack->add(sprintf("Illegal file name found in %s: %s:",$path, $file), 'error');
  168. else{
  169. $file_name = (int)$file_parts[0];
  170. $file_extension = $file_parts[1];
  171. if($file_extension == $extension){
  172. for($i =0; $i < $file_name; $i++)
  173. $results[] = '';
  174. $results[$file_name] = $file;
  175. }
  176. }
  177. }
  178. }
  179. closedir($handler);
  180. }
  181. return $results;
  182. }
  183. function check_version_tracker_table(){
  184. global $db, $messageStack;
  185. // TODO: check if database exists
  186. if(!@file_exists($this->module_installer_path.'.keep')){
  187. if($this->execute_sql_file(file($this->module_installer_path.'install.sql'))){
  188. $messageStack->add("Set up version tracker table", 'success');
  189. if($handle = @fopen($this->module_installer_path.'.keep', "w"))
  190. fclose($handle);
  191. else
  192. $messageStack->add("Failed to create .keep file", 'error');
  193. }
  194. else{
  195. $messageStack->add("Failed to set up version tracker table", 'error');
  196. return false;
  197. }
  198. }
  199. return true;
  200. }
  201. function _get_object( $name ){
  202. $obj = false;
  203. if( class_exists( $name ) ){
  204. $obj = new $name();
  205. }
  206. return $obj;
  207. }
  208. }
  209. ?>