00001 <?php
00002
00003
00004
00005
00006
00007
00008 class SystemUpdateInstaller {
00009 const COPY_NO_REPLACE = 'no_replace';
00010 const COPY_OVERWRITE = 'overwrite';
00011
00012 const HTACCESS_OPTIONS = 'OPTIONS';
00013 const HTACCESS_REWRITE = 'REWRITE';
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 public static function copy_to_webroot($dir, $files, $policy = self::COPY_OVERWRITE) {
00025 $ret = new Status();
00026 $webroot = Config::get_value(Config::URL_ABSPATH);
00027
00028 return self::copy_to_dir($webroot, $dir, $files, $policy);
00029 }
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 public static function copy_to_app($target_dir, $source_dir, $files, $policy = self::COPY_OVERWRITE) {
00042 $ret = new Status();
00043 $app = APP_INCLUDE_ABSPATH;
00044
00045 return self::copy_to_dir($app . $target_dir, $source_dir, $files, $policy);
00046 }
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 public static function copy_file_to_app($source_file, $target_file, $policy = self::COPY_OVERWRITE) {
00064 $ret = new Status();
00065 $target = APP_INCLUDE_ABSPATH . ltrim($target_file, '/');
00066 if ($policy == self::COPY_OVERWRITE || !file_exists($target)) {
00067 if (!copy($source_file, $target)) {
00068 $ret->merge(tr(
00069 'Could not create %target from %source. Please do it manually',
00070 'systemupdate',
00071 array('%target' => $target, '%source' => $source_file)
00072 ));
00073 }
00074 }
00075 return $ret;
00076 }
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 private static function copy_to_dir($target_dir, $source_dir, $files, $policy) {
00089 $ret = new Status();
00090
00091 foreach(Arr::force($files, false) as $file) {
00092 if ($file == '.' || $file == '..') {
00093 continue;
00094 }
00095
00096 $target = $target_dir . $file;
00097 $source = $source_dir . $file;
00098
00099 if (is_dir($source)) {
00100 $ret->merge(self::copy_to_dir($target_dir, $source_dir, self::read_dir($source_dir, $file), $policy));
00101 }
00102 else {
00103 if ($policy == self::COPY_NO_REPLACE) {
00104 if (file_exists($target)) {
00105 continue;
00106 }
00107 }
00108 $target_base_dir = dirname($target);
00109 if (!file_exists($target_base_dir)) {
00110 mkdir($target_base_dir, 0775, true);
00111 }
00112 if (!copy($source, $target)) {
00113 $ret->append(tr('Could not copy %s to %d', 'systemupdate', array('%s' => $source, '%d' => $target)));
00114 }
00115 }
00116 }
00117
00118 return $ret;
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130 public static function modify_htaccess($module, $section, $content) {
00131 $ret = new Status();
00132 if (is_array($content)) {
00133 $content = implode("\n", $content);
00134 }
00135
00136 $start = '## start ' . $module . ' ' . $section;
00137 $end = '## end ' . $module . ' ' . $section;
00138 $content = "$start\n$content\n$end";
00139
00140 $htaccess_path = Config::get_value(Config::URL_ABSPATH) . '.htaccess';
00141 $htaccess = @file_get_contents($htaccess_path);
00142
00143 $pattern = preg_quote($start) . '.*' . preg_quote($end);
00144 $htaccess = preg_replace('|' . $pattern . '|s', '', $htaccess);
00145
00146 $section_string = "### BEGIN $section ###";
00147 $section_start = strpos($htaccess, $section_string);
00148 if ($section_start !== false) {
00149 $htaccess = str_replace($section_string, $section_string . "\n\n" . $content, $htaccess);
00150 while(strpos($htaccess, "\n\n\n") !== false) {
00151 $htaccess = str_replace("\n\n\n", "\n\n", $htaccess);
00152 }
00153 if (file_put_contents($htaccess_path, $htaccess) === false) {
00154 $ret->append('Could not write .htaccess');
00155 }
00156 }
00157 else {
00158 $ret->append("Your .htaccess is not ready to be automatically modified - or it misses the section $section");
00159 }
00160 return $ret;
00161 }
00162
00163
00164
00165
00166 private static function read_dir($root, $dir) {
00167 if (substr($dir, -1) != '/') {
00168 $dir .= '/';
00169 }
00170
00171 $source = $root . $dir;
00172 $arr_tmp = scandir($source);
00173 $ret = array();
00174 foreach($arr_tmp as $file) {
00175 if (substr($file, 0, 1) != '.') {
00176 $ret[] = $dir . $file;
00177 }
00178 }
00179
00180 return $ret;
00181 }
00182 }