Exemple #1
0
 public function install($patch)
 {
     $tmpfiles = $this->bakFiles = array();
     WindFolder::mkRecur($this->tmpPath);
     if ($this->ftp && !is_object($this->ftp)) {
         try {
             $this->ftp = $this->ftp['sftp'] ? new PwSftpSave($this->ftp) : new PwFtpSave($this->ftp);
         } catch (WindFtpException $e) {
             return false;
         }
     }
     foreach ($patch['rule'] as $rule) {
         $rule['filename'] = $this->sortFile($rule['filename']);
         $filename = ROOT_PATH . $rule['filename'];
         $search = base64_decode($rule['search']);
         $replace = base64_decode($rule['replace']);
         $count = $rule['count'];
         $nums = $rule['nums'];
         $str = WindFile::read($filename);
         $realCount = substr_count($str, $search);
         if ($realCount != $count) {
             return new PwError('APPCENTER:upgrade.patch.update.fail', array($patch['id']));
         }
         $bakfile = basename($rule['filename']) . '.' . Pw::time2str(WEKIT_TIMESTAMP, 'Ymd') . '.bak';
         $bakfile = $this->ftp ? dirname($rule['filename']) . '/' . $bakfile : dirname($filename) . '/' . $bakfile;
         $tmpfile = tempnam($this->tmpPath, 'patch');
         $replacestr = PwSystemHelper::replaceStr($str, $search, $replace, $count, $nums);
         WindFile::write($tmpfile, $replacestr);
         if ($this->ftp) {
             try {
                 $this->ftp->upload($filename, $bakfile);
                 $this->ftp->upload($tmpfile, $rule['filename']);
             } catch (WindFtpException $e) {
                 return false;
             }
         } else {
             if (!@copy($filename, $bakfile)) {
                 return new PwError('APPCENTER:upgrade.copy.fail', array($rule['filename']));
             }
             if (!@copy($tmpfile, $filename)) {
                 return new PwError('APPCENTER:upgrade.copy.fail', array($rule['filename']));
             }
         }
         $tmpfiles[] = $tmpfile;
         $this->bakFiles[] = $bakfile;
     }
     $this->_ds()->addLog($patch['id'], $patch, 2);
     return true;
 }