/upload/converter/model/upgrade/configuration.php

https://github.com/pekka2/OpenCart-Migration-Upgrade-Tool · PHP · 233 lines · 197 code · 29 blank · 7 comment · 44 complexity · d5bcdf42783aba8dc85b4acda77a456a MD5 · raw file

  1. <?php
  2. class ModelUpgradeConfiguration extends Model{
  3. private $lang;
  4. private $text;
  5. private $dirAdmin;
  6. private $sowOps;
  7. private $upgrade;
  8. public function editConfig( $data ){
  9. $this->lang = $this->lmodel->get('upgrade_configuration');
  10. $this->simulate = ( !empty( $data['simulate'] ) ? true:false );
  11. $this->dirAdmin = ( !empty( $data['dirAdmin'] ) ? 'admin':'admin' ) .'/';
  12. $this->showOps = ( !empty( $data['showOps'] ) ? true:false );
  13. $this->upgrade = $data['upgrade'];
  14. $this->text = '';
  15. $modification = 'define(\'DIR_MODIFICATION\', \'' . DIR_MODIFICATION . '/\'); // OC 2';
  16. $upload = 'define(\'DIR_UPLOAD\', \'' . DIR_UPLOAD . '/\'); // OC 2';
  17. $server = $_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['SCRIPT_NAME']));
  18. $server = explode('/',$server);
  19. array_pop($server);
  20. $server = implode('/',$server);
  21. $http_server = '//define(\'HTTP_SERVER\', \'http://' . $server . '/\'); // OC 1.4+';
  22. $https_server = '//define(\'HTTPS_SERVER\', \'https://' .$server . '/\'); // OC 1.4+';
  23. $https_catalog = 'define(\'HTTPS_CATALOG\', \'https://' .$server . '/\'); // OC 1.4+';
  24. $db_port = 'define(\'DB_PORT\', \'3306\'); // OC 2.0.3 +';
  25. // frontend
  26. $content = file_get_contents( DIR_DOCUMENT_ROOT . 'config.php' );
  27. $check = $content;
  28. if( !strpos( $content, 'DIR_UPLOAD' ) || $this->upgrade > 2020 && !strpos( $content, 'DB_PORT') ) {
  29. $this->text .= $this->msg('<p><hr/></p>');
  30. // FILE yourstore.com/config.php
  31. $fp = file( DIR_DOCUMENT_ROOT . 'config.php' );
  32. if( !strpos( $content, 'HTTP_SERVER' ) ) {
  33. array_splice( $fp, 1, 0, $http_server . "\r\n" );
  34. array_splice( $fp, 2, 0, $https_server . "\r\n" );
  35. }
  36. if( !strpos( $content, 'DIR_UPLOAD' ) ) {
  37. array_splice( $fp, 18, 0, $modification . "\r\n" );
  38. array_splice( $fp, 19, 0, $upload . "\r\n" );
  39. }
  40. if($this->upgrade > 2020 && !strpos( $content, 'DB_PORT' )) {
  41. array_splice( $fp, 20, 0, $db_port . "\r\n" );
  42. }
  43. $content = implode( '', $fp );
  44. if( is_writable( DIR_DOCUMENT_ROOT . 'config.php' ) ) {
  45. if( !$this->simulate ) {
  46. $fw = fopen( DIR_DOCUMENT_ROOT . 'config.php', 'wb' );
  47. fwrite( $fw, $content );
  48. fclose( $fw );
  49. }
  50. if( !strpos( $check, 'HTTP_SERVER' ) ) {
  51. if( $this->showOps ){
  52. $this->text .= '<div class="notice"><pre>'.$http_server.'</pre></div>';
  53. }
  54. $this->text .= $this->msg( sprintf( $this->lang['msg_config_constant'], 'HTTP_SERVER', 'config.php' ) );
  55. if( $this->showOps ){
  56. $this->text .= '<div class="notice"><pre>'.$https_server.'</pre></div>';
  57. }
  58. $this->text .= $this->msg( sprintf( $this->lang['msg_config_constant'], 'HTTPS_SERVER', 'config.php' ) );
  59. }
  60. if($this->upgrade > 2020 && !strpos( $check, 'DB_PORT' )) {
  61. if( $this->showOps ){
  62. $this->text .= '<p><pre>'.$db_port.'</pre></p>';
  63. }
  64. $this->text .= $this->msg( sprintf( $this->lang['msg_config_constant'], 'DB_PORT', 'config.php' ) );
  65. }
  66. if( $this->showOps ){
  67. $this->text .= '<p><pre>'.$upload.'</pre></p>';
  68. }
  69. $this->text .= $this->msg( sprintf( $this->lang['msg_config_constant'], 'DIR_UPLOAD', 'config.php' ) );
  70. if( $this->showOps ){
  71. $this->text .= '<p><pre>'.$modification.'</pre></p>';
  72. }
  73. $this->text .= $this->msg( sprintf( $this->lang['msg_config_constant'], 'DIR_MODIFICATION', 'config.php' ) );
  74. if($this->upgrade > 2020 && !strpos( $content, 'DB_PORT' )) {
  75. if( $this->showOps ){
  76. $this->text .= '<p><pre>'.$db_port.'</pre></p>';
  77. }
  78. $this->text .= $this->msg( sprintf( $this->lang['msg_config_constant'], 'DB_PORT', $file ) );
  79. }
  80. }else{
  81. // $this->text .= $this->msg( sprintf( $this->lang, 'msg_perm_file', 'config.php' ) );
  82. }
  83. } else {
  84. $this->text .= $this->msg( sprintf( $this->lang['msg_config_uptodate'], 'config.php', '' ) );
  85. }
  86. $this->text .= $this->msg('<p><hr/></p>');
  87. // FILE yourstore.com/admin/config.php
  88. $file = $this->dirAdmin . 'config.php';
  89. $content2 = file_get_contents( DIR_DOCUMENT_ROOT . $file );
  90. if( !strpos( $content2, 'DIR_UPLOAD' ) ) {
  91. $fp2 = file( DIR_DOCUMENT_ROOT . $file );
  92. if( !strpos( $content2, 'HTTPS_CATALOG' ) ) {
  93. array_splice( $fp2, 8, 0, $https_catalog . "\r\n" );
  94. }
  95. array_splice( $fp2, 21, 0, $modification . "\r\n" );
  96. array_splice( $fp2, 22, 0, $upload . "\r\n" );
  97. if($this->upgrade > 2020 && !strpos( $content2, 'DB_PORT' )) {
  98. array_splice( $fp2, 25, 0, $db_port . "\r\n" );
  99. }
  100. $string = implode( '', $fp2 );
  101. if( is_writable( DIR_DOCUMENT_ROOT. $file ) ) {
  102. if( !$this->simulate ) {
  103. $fw = fopen( DIR_DOCUMENT_ROOT . $file, 'wb' );
  104. fwrite( $fw, $string );
  105. fclose( $fw );
  106. if( !$this->structure->getUpgrade() ){
  107. // Upgrade Cache
  108. $memory = DIR_UPGRADE . 'upgrade_cache.log';
  109. if(file_exists($memory)){
  110. $cache = unserialize(file_get_contents($memory));
  111. $cache['db'] = true;
  112. $cache['files'] = false;
  113. } else {
  114. $cache = array('db' => true,'files' => false );
  115. }
  116. if($cache){
  117. $fw = fopen( $memory, 'wb' );
  118. fwrite( $fw, serialize($cache) );
  119. fclose( $fw );
  120. }
  121. }
  122. }
  123. if( !strpos( $content2, 'HTTPS_CATALOG' ) ) {
  124. if( $this->showOps ){
  125. $this->text .= '<p><pre>'.$https_catalog.'</pre></p>';
  126. }
  127. $this->text .= $this->msg( sprintf( $this->lang['msg_config_constant'], 'HTTPS_CATALOG', $file ) );
  128. }
  129. if( $this->showOps ){
  130. $this->text .= '<p><pre>'.$upload.'</pre></p>';
  131. }
  132. $this->text .= $this->msg( sprintf( $this->lang['msg_config_constant'], 'DIR_UPLOAD', $file ) );
  133. if( $this->showOps ){
  134. $this->text .= '<p><pre>'.$modification.'</pre></p>';
  135. }
  136. $this->text .= $this->msg( sprintf( $this->lang['msg_config_constant'], 'DIR_MODIFICATION', $file ) );
  137. if($this->upgrade > 2020 && !strpos( $content2, 'DB_PORT' )) {
  138. if( $this->showOps ){
  139. $this->text .= '<p><pre>'.$db_port.'</pre></p>';
  140. }
  141. $this->text .= $this->msg( sprintf( $this->lang['msg_config_constant'], 'DB_PORT', $file ) );
  142. }
  143. }else{
  144. $this->text .= $this->msg( sprintf( $this->lang['msg_perm_file'], 'config.php', $file ) );
  145. }
  146. }else{
  147. $this->text .= $this->msg( sprintf( $this->lang['msg_config_uptodate'], $file, '' ) );
  148. }
  149. return $this->text;
  150. }
  151. public function constantFinishing($data){
  152. $this->lang = $this->lmodel->get('upgrade_configuration');
  153. $this->simulate = ( !empty( $data['simulate'] ) ? true:false );
  154. $this->dirAdmin = ( !empty( $data['dirAdmin'] ) ? 'admin':'admin' ) .'/';
  155. $this->showOps = ( !empty( $data['showOps'] ) ? true:false );
  156. $this->text = '';
  157. $http_server = '//define(\'HTTP_SERVER\', \'http://';
  158. $http_server2 = 'define(\'HTTP_SERVER\', \'http://';
  159. $https_server = '//define(\'HTTPS_SERVER\', \'https://';
  160. $https_server2 = 'define(\'HTTPS_SERVER\', \'https://';
  161. // frontend
  162. $file = DIR_DOCUMENT_ROOT . 'config.php';
  163. $content = file_get_contents( $file );
  164. $content = str_replace($http_server,$http_server2,$content);
  165. $content = str_replace($https_server,$https_server2,$content);
  166. if( !$this->simulate ) {
  167. $fw = fopen( $file, 'wb' );
  168. fwrite( $fw, $content );
  169. fclose( $fw );
  170. // Upgrade Cache
  171. $memory = DIR_UPGRADE . 'upgrade_cache.log';
  172. if(file_exists($memory)){
  173. $cache = unserialize(file_get_contents($memory));
  174. $cache['db'] = true;
  175. $cache['files'] = true;
  176. } else {
  177. $cache = array('db' => true,'files' => true );
  178. }
  179. if($cache){
  180. $fw = fopen( $memory, 'wb' );
  181. fwrite( $fw, serialize($cache) );
  182. fclose( $fw );
  183. }
  184. }
  185. $server = $_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['SCRIPT_NAME']));
  186. $server = explode('/',$server);
  187. array_pop($server);
  188. $server = implode('/',$server);
  189. $http_server = 'define(\'HTTP_SERVER\', \'http://' . $server . '/\'); // OC 1.4+';
  190. $https_server = 'define(\'HTTPS_SERVER\', \'https://' .$server . '/\'); // OC 1.4+';
  191. if( $this->showOps ){
  192. $this->text .= '<p><pre>'.$http_server.'</pre></p>';
  193. }
  194. $this->text .= $this->msg( sprintf( $this->lang['msg_config_constant'], 'HTTP_SERVER', 'config.php' ) );
  195. if( $this->showOps ){
  196. $this->text .= '<p><pre>'.$https_server.'</pre></p>';
  197. }
  198. $this->text .= $this->msg( sprintf( $this->lang['msg_config_constant'], 'HTTPS_SERVER', 'config.php' ) );
  199. return $this->text;
  200. }
  201. private function msg( $data ){
  202. return str_replace( $data, '<div class="msg round">' . $data .'</div>'."\r\n", $data);
  203. }
  204. }