示例#1
0
 /**
  * Apply the update
  *  
  * @access public      
  * @return boolean
  */
 public static function applyPackage($pharWithPath = null, $pharType = 'addon')
 {
     $phar_can_open = true;
     $meta = array();
     $pro_hart = array();
     try {
         if ($pharWithPath == null) {
             $pharFile = 'update.phar';
             $phar = new Phar(DIR_FS_WORK . 'updates/' . $pharFile);
             $meta = $phar->getMetadata();
             self::$_to_version = $meta['version_to'];
             // reset the log
             if (file_exists(DIR_FS_WORK . 'logs/update-' . self::$_to_version . '.txt') && is_writable(DIR_FS_WORK . 'logs/update-' . self::$_to_version . '.txt')) {
                 unlink(DIR_FS_WORK . 'logs/update-' . self::$_to_version . '.txt');
             }
             $pharCode = 'update';
             if (defined('ADDONS_SYSTEM_LOADED_7_PRO_STATUS') && is_dir(DIR_FS_CATALOG . 'addons/Loaded_7_Pro')) {
                 self::rmdir_r(DIR_FS_CATALOG . 'addons/Loaded_7_Pro');
             }
             if (defined('ADDONS_SYSTEM_LOADED_7_B2B_STATUS') && is_dir(DIR_FS_CATALOG . 'addons/Loaded_7_B2B')) {
                 self::rmdir_r(DIR_FS_CATALOG . 'addons/Loaded_7_B2B');
             }
             self::log('##### UPDATE TO ' . self::$_to_version . ' STARTED', $pharCode);
         } else {
             $pharFile = end(explode('/', $pharWithPath));
             $phar = new Phar(DIR_FS_WORK . 'addons/' . $pharFile);
             $meta = $phar->getMetadata();
             $pharCode = str_replace('.phar', '', $pharFile);
             if ($meta['type'] != '') {
                 $pharType = $meta['type'];
             }
             // reset the log
             if (file_exists(DIR_FS_WORK . 'logs/addon-' . $pharCode . '.txt') && is_writable(DIR_FS_WORK . 'logs/addon-' . $pharCode . '.txt')) {
                 unlink(DIR_FS_WORK . 'logs/addon-' . $pharCode . '.txt');
             }
             self::log('##### ADDON INSTALL ' . $pharCode . ' STARTED', $pharCode);
         }
         // first delete files before extracting new files
         if (is_array($meta['delete']) && count($meta['delete']) > 0) {
             foreach ($meta['delete'] as $file) {
                 $directory = realpath(DIR_FS_CATALOG) . '/';
                 if (file_exists($directory . $file)) {
                     if (is_dir($directory . $file)) {
                         if (rename($directory . $file, $directory . dirname($file) . '/.CU_' . basename($file))) {
                             $pro_hart[] = array('type' => 'directory', 'where' => $directory, 'path' => dirname($file) . '/.CU_' . basename($file), 'log' => true);
                         }
                     } else {
                         if (rename($directory . $file, $directory . dirname($file) . '/.CU_' . basename($file))) {
                             $pro_hart[] = array('type' => 'file', 'where' => $directory, 'path' => dirname($file) . '/.CU_' . basename($file), 'log' => true);
                         }
                     }
                 }
             }
         }
         // loop through each file individually as extractTo() does not work with
         // directories (see http://bugs.php.net/bug.php?id=54289)
         foreach (new RecursiveIteratorIterator($phar) as $iteration) {
             if (($pos = strpos($iteration->getPathName(), '.phar')) !== false) {
                 $file = substr($iteration->getPathName(), $pos + 6);
                 if (version_compare($meta['compatibility'], '7.002.0.0') != -1 || $pharType == 'template' || $pharType == 'language' || $pharWithPath == null) {
                     $directory = realpath(DIR_FS_CATALOG) . '/';
                 } else {
                     $directory = realpath(DIR_FS_CATALOG) . '/addons/' . $pharCode . '/';
                 }
                 if (file_exists($directory . $file)) {
                     if (rename($directory . $file, $directory . dirname($file) . '/.CU_' . basename($file))) {
                         $pro_hart[] = array('type' => 'file', 'where' => $directory, 'path' => dirname($file) . '/.CU_' . basename($file), 'log' => false);
                     }
                 }
                 if ($phar->extractTo($directory, $file, true)) {
                     self::log('Extracted: ' . $file, $pharCode);
                 } else {
                     self::log('*** Could Not Extract: ' . $file, $pharCode);
                 }
             }
         }
         self::log('##### CLEANUP', $pharCode);
         foreach (array_reverse($pro_hart, true) as $mess) {
             if ($mess['type'] == 'directory') {
                 if (self::rmdir_r($mess['where'] . $mess['path'])) {
                     if ($mess['log'] === true) {
                         self::log('Deleted: ' . str_replace('/.CU_', '/', $mess['path']), $pharCode);
                     }
                 } else {
                     if ($mess['log'] === true) {
                         self::log('*** Could Not Delete: ' . str_replace('/.CU_', '/', $mess['path']), $pharCode);
                     }
                 }
             } else {
                 if (unlink($mess['where'] . $mess['path'])) {
                     if ($mess['log'] === true) {
                         self::log('Deleted: ' . str_replace('/.CU_', '/', $mess['path']), $pharCode);
                     }
                 } else {
                     if ($mess['log'] === true) {
                         self::log('*** Could Not Delete: ' . str_replace('/.CU_', '/', $mess['path']), $pharCode);
                     }
                 }
             }
         }
     } catch (Exception $e) {
         $phar_can_open = false;
         self::log('##### ERROR: ' . $e->getMessage(), $pharCode);
         self::log('##### REVERTING STARTED', $pharCode);
         foreach (array_reverse($pro_hart, true) as $mess) {
             if ($mess['type'] == 'directory') {
                 if (file_exists($mess['where'] . str_replace('/.CU_', '/', $mess['path']))) {
                     self::rmdir_r($mess['where'] . str_replace('/.CU_', '/', $mess['path']));
                 }
             } else {
                 if (file_exists($mess['where'] . str_replace('/.CU_', '/', $mess['path']))) {
                     unlink($mess['where'] . str_replace('/.CU_', '/', $mess['path']));
                 }
             }
             if (file_exists($mess['where'] . $mess['path'])) {
                 rename($mess['where'] . $mess['path'], $mess['where'] . str_replace('/.CU_', '/', $mess['path']));
             }
             self::log('Reverted: ' . str_replace('/.CU_', '/', $mess['path']), $pharCode);
         }
         self::log('##### REVERTING COMPLETE', $pharCode);
         self::log('##### UPDATE TO ' . self::$_to_version . ' FAILED', $pharCode);
         trigger_error($e->getMessage());
         trigger_error('Please review the update log at: ' . DIR_FS_WORK . 'logs/update-' . self::$_to_version . '.txt');
     }
     if ($pharWithPath == null) {
         // execute run after process
         self::doRunAfter();
         // verify 644 permissions on PHP files on Linux systems
         if (utility::execEnabled() === true && utility::isLinux() === true) {
             try {
                 exec('\\find ' . DIR_FS_CATALOG . ' \\( -type f -exec chmod 644 {} \\; \\);');
                 self::log('##### UPDATED Permissions on PHP files/directories', $pharCode);
             } catch (Exception $e) {
                 self::log('*** Could NOT Set Permissions on PHP files/directories', $pharCode);
             }
             self::log('##### UPDATE TO ' . self::$_to_version . ' COMPLETE', $pharCode);
         } else {
             try {
                 self::chmod_r(DIR_FS_CATALOG);
                 self::log('##### UPDATED Permissions on PHP files/directories', $pharCode);
             } catch (Exception $e) {
                 self::log('*** Could NOT Set Permissions on PHP files/directories', $pharCode);
             }
             // remove the update phar
             if (file_exists(DIR_FS_WORK . 'updates/update.phar')) {
                 unlink(DIR_FS_WORK . 'updates/update.phar');
             }
             self::log('##### UPDATE TO ' . self::$_to_version . ' COMPLETE', $pharCode);
         }
     } else {
         // remove the addon phar & pubkey
         if (file_exists(DIR_FS_WORK . 'addons/' . $pharCode . '.phar')) {
             unlink(DIR_FS_WORK . 'addons/' . $pharCode . '.phar');
         }
         if (file_exists(DIR_FS_WORK . 'addons/' . $pharCode . '.phar.pubkey')) {
             unlink(DIR_FS_WORK . 'addons/' . $pharCode . '.phar.pubkey');
         }
         self::log('##### ADDON INSTALL ' . $code . ' COMPLETE', $pharCode);
     }
     return $phar_can_open;
 }