public static function install_from_file($version, $revision, $ufile, $ulog) { global $DB; // remove old update files (will be there if a previous update fails) file_put_contents($ulog, "remove old update folder\n", FILE_APPEND); core_remove_folder(NAVIGATE_PATH . '/updates/update'); // decompress file_put_contents($ulog, "create new folder\n", FILE_APPEND); mkdir(NAVIGATE_PATH . '/updates/update'); $zip = new ZipArchive(); file_put_contents($ulog, "open zip file\n", FILE_APPEND); if ($zip->open($ufile) === TRUE) { file_put_contents($ulog, "extract zip file\n", FILE_APPEND); $zip->extractTo(NAVIGATE_PATH . '/updates/update'); $zip->close(); } else { file_put_contents($ulog, "zip extraction failed\n", FILE_APPEND); @unlink($ufile); core_remove_folder(NAVIGATE_PATH . '/updates/update'); return false; } // chmod files (may fail, but not fatal error) file_put_contents($ulog, "chmod update (may fail in Windows)... ", FILE_APPEND); $chmod_status = core_chmodr(NAVIGATE_PATH . '/updates/update', 0755); file_put_contents($ulog, $chmod_status . "\n", FILE_APPEND); // do file changes file_put_contents($ulog, "parse file changes\n", FILE_APPEND); $hgchanges = file_get_contents(NAVIGATE_PATH . '/updates/update/changes.txt'); $hgchanges = explode("\n", $hgchanges); foreach ($hgchanges as $change) { file_put_contents($ulog, $change . "\n", FILE_APPEND); $change = trim($change); if (empty($change)) { continue; } $change = explode(" ", $change, 2); // new, removed and modified files // M = modified // A = added // R = removed // C = clean // ! = missing (deleted by non-hg command, but still tracked) // ? = not tracked // I = ignored // = origin of the previous file listed as A (added) $file = str_replace('\\', '/', $change[1]); //if(substr($file, 0, strlen('plugins/'))=='plugins/') continue; if (substr($file, 0, strlen('setup/')) == 'setup/') { continue; } switch ($change[0]) { case 'A': // added a new file // added a new file case 'M': // modified file if (!file_exists(NAVIGATE_PATH . '/updates/update/' . $file)) { file_put_contents($ulog, "file doesn't exist!\n", FILE_APPEND); return false; } @mkdir(dirname(NAVIGATE_PATH . '/' . $file), 0777, true); if (!@copy(NAVIGATE_PATH . '/updates/update/' . $file, NAVIGATE_PATH . '/' . $file)) { file_put_contents($ulog, "cannot copy file!\n", FILE_APPEND); return false; } break; case 'R': // remove file @unlink(NAVIGATE_PATH . '/' . $file); break; default: // all other cases // IGNORE the change, as we are now only getting the modified files } } // process SQL updates file_put_contents($ulog, "process sql update\n", FILE_APPEND); if (file_exists(NAVIGATE_PATH . '/updates/update/update.sql')) { $sql = file_get_contents(NAVIGATE_PATH . '/updates/update/update.sql'); // execute SQL in a transaction // http://php.net/manual/en/pdo.transactions.php try { // can't do it in one step => SQLSTATE[HY000]: General error: 2014 $sql = explode("\n\n", $sql); //file_put_contents($ulog, "begin transaction\n", FILE_APPEND); //$DB->beginTransaction(); foreach ($sql as $sqlline) { $sqlline = trim($sqlline); if (empty($sqlline)) { continue; } file_put_contents($ulog, "execute sql:\n" . $sqlline . "\n", FILE_APPEND); if (!$DB->execute($sqlline)) { file_put_contents($ulog, "execute failed: " . $DB->get_last_error() . "\n", FILE_APPEND); //throw new Exception($DB->get_last_error()); } // force commit changes (slower but safer... no --> SQLSTATE[HY000]: General error: 2014) $DB->disconnect(); $DB->connect(); } //file_put_contents($ulog, "commit transaction\n", FILE_APPEND); //$DB->commit(); } catch (Exception $e) { file_put_contents($ulog, "transaction error: \n" . $e->getMessage() . "\n", FILE_APPEND); //$DB->rollBack(); return false; } } else { file_put_contents($ulog, "no SQL found\n", FILE_APPEND); } // add the update row to know which navigate revision is currently installed file_put_contents($ulog, "insert new version row on updates\n", FILE_APPEND); $urow = new update(); $urow->id = 0; $urow->version = $version; $urow->revision = $revision; $urow->date_updated = time(); $urow->status = 'ok'; $urow->changelog = ''; try { $ok = $urow->insert(); } catch (Exception $e) { $error = $e->getMessage(); } if ($error) { file_put_contents($ulog, "execute insert failed:\n" . $DB->get_last_error() . "\n", FILE_APPEND); } if (file_exists(NAVIGATE_PATH . '/updates/update/update-post.php')) { include_once NAVIGATE_PATH . '/updates/update/update-post.php'; } file_put_contents($ulog, "update finished!\n", FILE_APPEND); $urow->changelog = file_get_contents($ulog); $urow->save(); @unlink($ufile); update::cache_clean(); return true; }
/** * Changes the UNIX permissions of a file or folder and its contents * * @param string $path Absolute path to the file or folder to change * @param string $filemode UNIX permissions code, p.e. 0755 * @return boolean TRUE if no problem found, FALSE otherwise. Note: on Windows systems this function is always FALSE. */ function core_chmodr($path, $filemode) { if (!is_dir($path)) { return chmod($path, $filemode); } $dh = opendir($path); while (($file = readdir($dh)) !== false) { if ($file != '.' && $file != '..') { $fullpath = $path . '/' . $file; if (is_link($fullpath)) { return FALSE; } else { if (!is_dir($fullpath) && !chmod($fullpath, $filemode)) { return FALSE; } else { if (!core_chmodr($fullpath, $filemode)) { return FALSE; } } } } } closedir($dh); if (chmod($path, $filemode)) { return TRUE; } else { return FALSE; } }