PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/b2b/core/model/system/mdl.upgrade.php

http://phpfor.googlecode.com/
PHP | 270 lines | 223 code | 41 blank | 6 comment | 31 complexity | b765a4b92a6717e11cf41628caab48b9 MD5 | raw file
  1. <?php
  2. class mdl_upgrade extends modelFactory{
  3. var $line=0;
  4. var $sql=null;
  5. var $succ=false;
  6. function exec($step){
  7. if(method_exists($this,$action = 'action_'.$step)){
  8. $this->$action();
  9. }else{
  10. $this->action_welcome();
  11. }
  12. }
  13. function action_welcome(){
  14. $versionTxt = $this->system->version();
  15. $dbver = $_GET['force_ver']?$_GET['force_ver']:$this->dbVersion();
  16. $this->system->cache->clear();
  17. $smarty = &$this->system->loadModel('system/frontend');
  18. $smarty->compile_dir = HOME_DIR.'/cache/admin_tmpl';
  19. $smarty->clear_compiled_tpl();
  20. $smarty->compile_dir = HOME_DIR.'/cache/front_tmpl';
  21. $smarty->clear_compiled_tpl();
  22. if($this->checkFileList(BASE_DIR.'/upgrade.php',$failedFiles,1) || $_GET['ignore_lost']){
  23. $scripts = $this->scripts($dbver,$versionTxt['rev']);
  24. $data['scripts'] = &$scripts;
  25. if($data['scripts_count'] = count($scripts)){
  26. $dbver['version']==$versionTxt['dbver'];
  27. $this->output('ready.html',$data);
  28. }else{
  29. $this->output('done.html',$data);
  30. }
  31. }else{
  32. $data['files'] = $failedFiles;
  33. $this->output('uploading.html',$data);
  34. }
  35. }
  36. function action_runscript(){
  37. header('Content-type: text/html;charset=utf-8');
  38. set_time_limit(0);
  39. if(!($file = $_POST['file']) || !file_exists(CORE_DIR.'/updatescripts/'.$file)){
  40. echo 'missing file'.$_POST['file'];
  41. return;
  42. }
  43. if(!file_exists(HOME_DIR.'/logs/upgrade_'.substr( $_POST['file'] ,0 ,-4 ).'_'.$_POST['timeline'].'.log.php')){
  44. error_log('<?php exit()?>'." \n \n",3,HOME_DIR.'/logs/upgrade_'.substr( $_POST['file'] ,0 ,-4 ).'_'.$_POST['timeline'].'.log.php');
  45. }
  46. switch(ext_name($file)){
  47. case '.php':
  48. include(CORE_DIR.'/updatescripts/'.$file);
  49. if(class_exists('UpgradeScript')){
  50. $oUpgrade = new UpgradeScript();
  51. $oUpgrade->step=$_POST['step']?$_POST['step']:'1';
  52. $oUpgrade->runFunc = $_POST['runFunc']?$_POST['runFunc']:'first';
  53. $oUpgrade->status = $_POST['runStatus']?$_POST['runStatus']:'all-finish';
  54. $oUpgrade->version = substr( $file ,0 ,-4 );
  55. // $oUpgrade->runFunc = $runFunc;
  56. $oUpgrade->__Upgrade();
  57. }
  58. break;
  59. case '.sql':
  60. $this->run_update_sql($file);
  61. break;
  62. }
  63. $this->setDbver(substr($file,0,-4));
  64. }
  65. function setDbver($ver){
  66. $this->db->exec("drop table if exists ".DB_PREFIX."dbver");
  67. $this->db->exec("create table ".DB_PREFIX."dbver(`".$ver."` varchar(255))");
  68. }
  69. function run_update_sql($file){
  70. foreach($this->db->splitSql(file_get_contents(CORE_DIR.'/updatescripts/'.$file)) as $runningSQL){
  71. if($this->db->exec($runningSQL)){
  72. $output.=update_message($runningSQL);
  73. }else{
  74. $errinfo = $this->db->errorInfo();
  75. $etype = E_ERROR;
  76. if(preg_match('/syntax to use near \'(.*?)\' at line/i',$errinfo,$match)){
  77. $runningSQL = str_replace($match[1],'<b>'.$match[1].'</b>',$runningSQL);
  78. }elseif(preg_match('/Duplicate [a-z]+ name/',$errinfo)){
  79. $etype = E_WARNING;
  80. }
  81. $output.=update_message($runningSQL,$etype);
  82. }
  83. }
  84. echo $output;
  85. }
  86. function output($output,$data){
  87. $data['page'] = $output;
  88. $smarty = &$this->system->loadModel('system/frontend');
  89. foreach($data as $k=>$v){
  90. $smarty->assign($k,$v);
  91. }
  92. $smarty->assign('version', $this->system->version());
  93. header('Content-Type: text/html;charset=utf-8');
  94. $smarty->display('admin:upgrade/page.html');
  95. }
  96. function checkFileList($file,&$list,$ignore_lines=0){
  97. $list = array();
  98. $handle = fopen($file,'r');
  99. while ($line = fgetcsv($handle, 1000, ',')) {
  100. if($ignore_lines > $i++){
  101. continue;
  102. }
  103. if(md5_file(BASE_DIR.'/'.$line[0]) != $line[1]){
  104. $list[] = $line[0];
  105. }
  106. }
  107. fclose($handle);
  108. return 0==count($list);
  109. }
  110. function scripts($from,$to){
  111. $dir = CORE_DIR.'/updatescripts';
  112. if ($handle = opendir($dir)) {
  113. while (false !== ($file = readdir($handle))){
  114. if (is_file($dir.'/'.$file) && $file{0}!='.'){
  115. if(preg_match('/^([0-9]+)\.(sql|php)$/i',$file,$match)){
  116. if($match[1]>$from && $match[1]<=$to){
  117. $step[]=$file;
  118. if($match[2]=='php'){
  119. $order[] = $match[1]*2+1;
  120. }else{
  121. $order[] = $match[1]*2;
  122. }
  123. }
  124. }
  125. }
  126. }
  127. closedir($handle);
  128. }
  129. array_multisort($order,$step);
  130. return $step;
  131. }
  132. function dbVersion(){
  133. $rs = $this->db->exec('SHOW TABLES like "'.DB_PREFIX.'dbver"');
  134. if($rs->getArray(1)){
  135. $rs = $this->db->exec('select * from '.DB_PREFIX.'dbver');
  136. $col = $rs->FetchField(0);
  137. $rows = $rs->GetArray(1);
  138. $rs = null;
  139. return $col->name;
  140. }else{
  141. return 0;
  142. }
  143. }
  144. }
  145. function update_message($msg,$etype=0,$errinfo=null){
  146. $upgradeVersion = substr( $_POST['file'] ,0 ,-4 );
  147. $logFileName = HOME_DIR.'/logs/upgrade_'.$upgradeVersion.'_'.$_POST['timeline'].'.log.php';
  148. switch($etype){
  149. case E_WARNING:
  150. case E_USER_WARNING:
  151. error_log('?? '.$msg.($errinfo?'->'.$errinfo:'')." \n \n",3,$logFileName);
  152. return '<div class="runsqldetail"><div class="sql-body">'.$msg.'</div><div class="error">[??]'.$errinfo.'</div></div>';
  153. case E_ERROR:
  154. case E_USER_ERROR:
  155. error_log('?? '.$msg.($errinfo?'->'.$errinfo:'')." \n \n",3,$logFileName);
  156. return '<div class="runsqldetail"><div class="sql-body">'.$msg.'</div><div class="warning">[??]'.$errinfo.'</div></div>';
  157. default:
  158. error_log('?? '.$msg.($errinfo?'->'.$errinfo:'')." \n \n",3,$logFileName);
  159. return '<div class="runsqldetail"><div class="succ">[??]'.$msg.'</div></div>';
  160. }
  161. }
  162. class Upgrade {
  163. var $step;
  164. var $msg;
  165. var $status;
  166. var $title = '';
  167. var $funcList = array();
  168. var $runFunc = 'first';
  169. var $version;
  170. var $updateMsg;
  171. function Upgrade(){
  172. $this->system = &$GLOBALS['system'];
  173. $this->db = &$this->system->database();
  174. set_time_limit(0);
  175. foreach( array_diff( get_class_methods($this) , get_class_methods('Upgrade') ) as $func ){
  176. if( substr($func,0,8) != 'upgrade_' )
  177. continue;
  178. $this->funcList[] = substr($func,8);
  179. }
  180. // echo '<input type="hidden" class="allFunc" value="'.implode(',',$this->funcList).'"/>';
  181. }
  182. function upgrade_first(){
  183. }
  184. function upgrade_last(){
  185. }
  186. function __Upgrade(){
  187. // $this->status = $this->upgrade_.$this->runFunc();
  188. eval('$this->status = $this->upgrade_'.$this->runFunc.'();');
  189. // error_log("upgrade_".$this->runFunc."?? ".( $this->status=='continue'? )." \n",3,.HOME_DIR.'/logs/upgrade_'.$this->version.'.log');
  190. echo $this->updateMsg;
  191. if( $this->runFunc == 'first' ){
  192. $this->title = $this->title?$this->title:'?????';
  193. $this->runFunc = array_key_exists( 0, $this->funcList )?$this->funcList[0]:'last';
  194. $this->status = $this->status?$this->status:'finish';
  195. }else if( $this->runFunc == 'last' ){
  196. $this->status = 'all-finish';
  197. echo '<input type="hidden" class="upgrade-notice" value=\''.json_encode($this->noticeMsg).'\'/>';
  198. }else{
  199. switch($this->status){
  200. case 'continue':
  201. $this->title = $this->title?'???? '.$this->title:'???';
  202. //to be continue...
  203. break;
  204. case 'finish':
  205. $this->title = $this->title?$this->title:'???';
  206. $funcKey = array_search($this->runFunc, $this->funcList);
  207. if(array_key_exists( $funcKey+1, $this->funcList )){
  208. $this->runFunc = $this->funcList[$funcKey+1];
  209. }else{
  210. $this->title = '????????';
  211. $this->runFunc = 'last';
  212. }
  213. break;
  214. case 'error':
  215. echo '<input type="hidden" class="upgrade-notice" value=\''.json_encode($this->noticeMsg).'\'/>';
  216. $this->title .= ' ????';
  217. break;
  218. default:
  219. $this->status = 'all-finish';
  220. break;
  221. }
  222. }
  223. echo '<input type="hidden" class="up-title" value="'.$this->title.'"/>';
  224. echo '<input type="hidden" class="runFunc" value="'.$this->runFunc.'" />';
  225. echo '<input type="hidden" class="run-status" value="'.$this->status.'"/>';
  226. // usleep(100000);
  227. }
  228. }
  229. ?>