PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/xnews/admin/cloner.php

https://github.com/severnaya99/Sg-2010
PHP | 165 lines | 111 code | 9 blank | 45 comment | 37 complexity | 179529bbf0505bec7bde2403abb356ae MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0
  1. <?php
  2. // $Id$
  3. // ------------------------------------------------------------------------ //
  4. // XOOPS - PHP Content Management System //
  5. // Copyright (c) 2000 XOOPS.org //
  6. // <http://www.xoops.org/> //
  7. // ------------------------------------------------------------------------ //
  8. // This program is free software; you can redistribute it and/or modify //
  9. // it under the terms of the GNU General Public License as published by //
  10. // the Free Software Foundation; either version 2 of the License, or //
  11. // (at your option) any later version. //
  12. // //
  13. // You may not change or alter any portion of this comment or credits //
  14. // of supporting developers from this source code or any supporting //
  15. // source code which is considered copyrighted (c) material of the //
  16. // original comment or credit authors. //
  17. // //
  18. // This program is distributed in the hope that it will be useful, //
  19. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  20. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  21. // GNU General Public License for more details. //
  22. // //
  23. // You should have received a copy of the GNU General Public License //
  24. // along with this program; if not, write to the Free Software //
  25. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
  26. // ------------------------------------------------------------------------ //
  27. /**
  28. * Module Cloner file
  29. *
  30. * Enable webmasters to clone the news module.
  31. *
  32. * NOTE : Please give credits if you copy this code !
  33. *
  34. * @package News
  35. * @author DNPROSSI
  36. * @copyright (c) DNPROSSI
  37. */
  38. function nw_cloneFileFolder($path, $patterns)
  39. {
  40. $patKeys = array_keys($patterns);
  41. $patValues = array_values($patterns);
  42. // work around for PHP < 5.0.x
  43. if(!function_exists('file_put_contents')) {
  44. function file_put_contents($filename, $data, $file_append = false) {
  45. $fp = fopen($filename, (!$file_append ? 'w+' : 'a+'));
  46. if ( !$fp ) {
  47. trigger_error('file_put_contents cannot write in file.', E_USER_ERROR);
  48. return;
  49. }
  50. fputs($fp, $data);
  51. fclose($fp);
  52. }
  53. }
  54. $newpath = str_replace($patKeys[0], $patValues[0], $path);
  55. if ( is_dir($path) )
  56. {
  57. // create new dir
  58. if ( !is_dir($newpath) ){ mkdir($newpath); };
  59. // check all files in dir, and process them
  60. if ( $handle = opendir($path) )
  61. {
  62. while ( $file = readdir($handle) )
  63. {
  64. if ( $file != '.' && $file != '..' )
  65. {
  66. nw_cloneFileFolder("$path/$file", $patterns);
  67. }
  68. }
  69. closedir($handle);
  70. }
  71. }
  72. else
  73. {
  74. //trigger_error('in else', E_USER_ERROR);
  75. if ( preg_match('/(.jpg|.gif|.png|.zip)$/i', $path) )
  76. {
  77. copy($path, $newpath);
  78. }
  79. else
  80. {
  81. $content = file_get_contents($path);
  82. for ( $i = 0; $i < sizeof($patterns); ++$i )
  83. {
  84. $content = str_replace($patKeys[$i], $patValues[$i], $content);
  85. }
  86. file_put_contents($newpath, $content);
  87. //trigger_error($path. ' ---- ' .$newpath , E_USER_WARNING);
  88. }
  89. }
  90. }
  91. //DNPROSSI
  92. function nw_clonefilename($path, $old_subprefix, $new_subprefix)
  93. {
  94. for ($i = 0; $i <= 1; $i++)
  95. {
  96. if ( $handle = opendir($path[$i]) )
  97. {
  98. while ( $file = readdir($handle) )
  99. {
  100. if ( $file != '.' && $file != '..' )
  101. {
  102. $newfilename = str_replace($old_subprefix, $new_subprefix, $file);
  103. @rename( $path[$i].$file, $path[$i].$newfilename );
  104. }
  105. }
  106. closedir($handle);
  107. }
  108. }
  109. }
  110. //DNPROSSI
  111. function nw_deleteclonefile($path, $new_subprefix)
  112. {
  113. for ($i = 0; $i <= 1; $i++)
  114. {
  115. if ( $handle = opendir($path[$i]) )
  116. {
  117. while ( $file = readdir($handle) )
  118. {
  119. if ( $file != '.' && $file != '..' )
  120. {
  121. $pos = strpos($file, $new_subprefix);
  122. if ( $pos !== false )
  123. {
  124. //trigger_error($file. ' ---- Deleted' , E_USER_WARNING);
  125. @unlink( $path[$i].$file );
  126. }
  127. }
  128. }
  129. closedir($handle);
  130. }
  131. }
  132. }
  133. //DNPROSSI
  134. function nw_clonecopyfile($srcpath, $destpath, $filename)
  135. {
  136. if ( $handle = opendir($srcpath) )
  137. {
  138. if ( $filename == '' )
  139. {
  140. while ( $file = readdir($handle) )
  141. {
  142. if ( $file != '.' && $file != '..' )
  143. {
  144. @copy($srcpath.$file, $destpath.$file );
  145. }
  146. }
  147. } else {
  148. if ( file_exists($srcpath.$filename) )
  149. {
  150. @copy($srcpath.$filename, $destpath.$filename);
  151. }
  152. }
  153. closedir($handle);
  154. }
  155. }
  156. ?>