function rmdirRecursive($dir) { if (!file_exists($dir)) { // Do nothing return 0; } // Remove symlink, but not their content if (is_link($dir)) { unlink($dir); return 0; } if (empty($dir)) { return 0; } $total = 0; $files = array_diff(scandir($dir), array('.', '..')); foreach ($files as $file) { $path = $dir . '/' . $file; if (is_dir($path)) { $total += rmdirRecursive($path); } else { unlink($path); ++$total; } } rmdir($dir); ++$total; return $total; }
public function run() { if ($this->config->project === 'default') { throw new ProjectNeeded(); } $path = $this->config->projects_root . '/projects/' . $this->config->project; if (!file_exists($path)) { throw new NoSuchProject($this->config->project); } display("Cleaning project {$this->config->project}\n"); $dirsToErase = array('log', 'report', 'Premier-ace', 'faceted', 'faceted2', 'ambassador'); foreach ($dirsToErase as $dir) { $dirPath = $path . '/' . $dir; if (file_exists($dirPath)) { display('removing ' . $dir); rmdirRecursive($dirPath); } } // rebuild log mkdir($path . '/log', 0755); $filesToErase = array('Flat-html.html', 'Flat-markdown.md', 'Flat-sqlite.sqlite', 'Flat-text.txt', 'Premier-ace.zip', 'Premier-html.html', 'Premier-markdown.md', 'Premier-sqlite.sqlite', 'Premier-text.txt', 'datastore.sqlite', 'magicnumber.sqlite', 'report.html', 'report.md', 'report.odt', 'report.pdf', 'report.sqlite', 'report.txt', 'report.zip', 'EchoWithConcat.json', 'PhpFunctions.json', 'bigArrays.txt', 'counts.sqlite', 'stats.txt', 'dump.sqlite', 'faceted.zip', 'faceted2.zip'); $total = 0; foreach ($filesToErase as $file) { $filePath = $path . '/' . $file; if (file_exists($filePath)) { display('removing ' . $file); unlink($filePath); ++$total; } } display("Removed {$total} files\n"); $this->datastore = new Datastore($this->config, Datastore::CREATE); display("Recreating database\n"); }
public function run() { if ($this->config->project === 'default') { throw new ProjectNeeded(); } if (!file_exists($this->config->projects_root . '/projects/' . $this->config->project)) { throw new NoSuchProject($this->config->project); } rmdirRecursive($this->config->projects_root . '/projects/' . $this->config->project); display('Project ' . $this->config->project . ' removed.'); }
function rmdirRecursive($path) { $dir = opendir($path); while ($entry = readdir($dir)) { if (is_file("{$path}/{$entry}")) { unlink("{$path}/{$entry}"); } elseif (is_dir("{$path}/{$entry}") && $entry != '.' && $entry != '..') { rmdirRecursive("{$path}/{$entry}"); } } closedir($dir); return rmdir($path); }
function rmdirRecursive($path, $followLinks = false) { $files = scandir($path); foreach ($files as $entry) { if (is_file("{$path}/{$entry}") || !$followLinks && is_link("{$path}/{$entry}")) { @unlink("{$path}/{$entry}"); } elseif (is_dir("{$path}/{$entry}") && $entry != '.' && $entry != '..') { rmdirRecursive("{$path}/{$entry}"); // recursive } } return @rmdir($path); }
function rmdirRecursive($path, $followLinks = false) { $dir = opendir($path); while ($entry = readdir($dir)) { if (is_file("{$path}/{$entry}") || !$followLinks && is_link("{$path}/{$entry}")) { @unlink("{$path}/{$entry}"); } elseif (is_dir("{$path}/{$entry}") && $entry != '.' && $entry != '..') { rmdirRecursive("{$path}/{$entry}"); } } closedir($dir); return @rmdir($path); }
function rmdirRecursive($dir) { $files = scandir($dir); array_shift($files); array_shift($files); foreach ($files as $file) { $file = $dir . DIRECTORY_SEPARATOR . $file; if (is_dir($file)) { rmdirRecursive($file); } else { unlink($file); } } rmdir($dir); }
public function run() { $project = $this->config->project; if ($project == 'default') { throw new ProjectNeeded(); } $repositoryURL = $this->config->repository; if ($this->config->delete === true) { display("Deleting {$project}\n"); // final wait..., just in case sleep(2); rmdirRecursive($this->config->projects_root . '/projects/' . $project); } elseif ($this->config->update === true) { display("Updating {$project}\n"); shell_exec('cd ' . $this->config->projects_root . '/projects/' . $project . '/code/; git pull'); } else { display("Initializing {$project} with '{$repositoryURL}'\n"); $this->init_project($project, $repositoryURL); } display("Done\n"); }
public function run() { if ($this->config->project === 'default') { throw new ProjectNeeded(); } $path = $this->config->projects_root . '/projects/' . $this->config->project; if (!file_exists($path)) { throw new NoSuchProject($this->config->project); } if (!file_exists($path . '/code')) { throw new NoCodeInProject($this->config->project); } switch (true) { // symlink case case $this->config->project_vcs === 'symlink': // Nothing to do, the symlink is here for that break; // copy case // copy case case $this->config->project_vcs === 'copy': // Remove and copy again $total = rmdirRecursive($this->config->projects_root . '/projects/' . $this->config->project . '/code/'); display("{$total} files were removed"); $total = copyDir(realpath($this->config->project_url), $this->config->projects_root . '/projects/' . $this->config->project . '/code'); display("{$total} files were copied"); break; // Git case // Git case case file_exists($path . '/code/.git'): display('Git pull for ' . $this->config->project); $res = shell_exec('cd ' . $path . '/code/; git branch | grep \\*'); $branch = substr(trim($res), 2); $resInitial = shell_exec('cd ' . $path . '/code/; git show-ref --heads ' . $branch); $date = trim(shell_exec('cd ' . $path . '/code/; git pull --quiet; git log -1 --format=%cd ')); $resFinal = shell_exec('cd ' . $path . '/code/; git show-ref --heads ' . $branch); if ($resFinal != $resInitial) { display("Git updated to commit {$res} (Last commit : {$date})"); } else { display("No update available (Last commit : {$date})"); } break; // svn case // svn case case file_exists($path . '/code/.svn'): display('SVN update ' . $this->config->project); $res = shell_exec('cd ' . $path . '/code/; svn update'); preg_match('/At revision (\\d+)/', $res, $r); display("SVN updated to revision {$r['1']}"); break; // bazaar case // bazaar case case file_exists($path . '/code/.bzr'): display('Bazaar update ' . $this->config->project); $res = shell_exec('cd ' . $path . '/code/; bzr update 2>&1'); preg_match('/revision (\\d+)/', $res, $r); display("Bazaar updated to revision {$r['1']}"); break; // composer case // composer case case $this->config->project_vcs === 'composer': display('Composer update ' . $this->config->project); $res = shell_exec('cd ' . $path . '/code/; composer install '); $json = file_get_contents($path . '/code/composer.lock'); $json = json_decode($json); foreach ($json->packages as $package) { if ($package->name == $this->config->project_url) { display("Composer updated to revision " . $package->source->reference . ' ( version : ' . $package->version . ' )'); } } break; default: display('No VCS found to update (Only git, svn and bazaar are supported. Ask exakat to add more.'); } }
public function generate($dirName, $fileName = null) { if ($fileName === null) { return "Can't produce report to stdout\nAborting\n"; } // Clean temporary destination if (file_exists($dirName . '/' . $fileName)) { rmdirRecursive($dirName . '/' . $fileName); } $finalName = $fileName; $tmpFileName = '.' . $fileName; mkdir($dirName . '/' . $tmpFileName, self::FOLDER_PRIVILEGES); // Building index.html $html = file_get_contents($this->config->dir_root . '/media/faceted2/index.html'); $html = str_replace('PROJECT_NAME', $this->config->project_name, $html); file_put_contents($dirName . '/' . $tmpFileName . '/index.html', $html); // Building app.js $js = file_get_contents($this->config->dir_root . '/media/faceted2/app.js'); $json = parent::generate($dirName); $js = str_replace('DUMP_JSON', $json, $js); $docs = array(); $analyzes = json_decode($json); foreach ($analyzes as $analyze) { $ini = parse_ini_file($this->config->dir_root . '/human/en/' . $analyze->analyzer . '.ini'); $docs[$ini['name']] = $ini['description']; } $docs = json_encode($docs); $js = str_replace('__DOCS__', $docs, $js); $json = parent::generate($dirName); $js = str_replace('DUMP_JSON', $json, $js); print file_put_contents($dirName . '/' . $tmpFileName . '/app.js', $js) . ' octets écrits'; copyDir($this->config->dir_root . '/media/faceted2/bower_components', $dirName . '/' . $tmpFileName . '/bower_components'); copyDir($this->config->dir_root . '/media/faceted2/node_modules', $dirName . '/' . $tmpFileName . '/node_modules'); copy($this->config->dir_root . '/media/faceted2/exakat.css', $dirName . '/' . $tmpFileName . '/exakat.css'); $css = file_get_contents($this->config->dir_root . '/media/faceted/faceted.css'); file_put_contents($dirName . '/' . $tmpFileName . '/faceted.css', $css); $errors = json_decode($json); $docsList = array(); $filesList = array(); foreach ($errors as $error) { $docsList[$error->analyzer] = $error->error; $filesList[$error->file] = $error->line; } asort($docsList); $docsHtml = '<dl>'; foreach ($docsList as $id => $dl) { $ini = parse_ini_file($this->config->dir_root . '/human/en/' . $id . '.ini'); $description = htmlentities($ini['description'], ENT_COMPAT | ENT_HTML401, 'UTF-8'); $description = preg_replace_callback('/\\s*(<\\?php.*?\\?>)\\s*/si', function ($r) { return '<br />' . highlight_string(html_entity_decode($r[1]), true); }, $description); $description = nl2br($description); $docsHtml .= "<dt id=\"{$id}\">{$dl}</dt>\n <dd>{$description}</dd>\n"; } $docsHtml .= '</dl>'; $docs = file_get_contents($this->config->dir_root . '/media/faceted/docs.html'); $docs = str_replace('DOCS_LIST', $docsHtml, $docs); $docs = str_replace('PROJECT_NAME', $this->config->project_name, $docs); file_put_contents($dirName . '/' . $tmpFileName . '/docs.html', $docs); foreach ($filesList as $path => $line) { $dirs = explode('/', $path); array_pop($dirs); // remove file name array_shift($dirs); // remove root / $d = ''; foreach ($dirs as $dir) { $d .= '/' . $dir; if (!file_exists($dirName . '/' . $tmpFileName . $d)) { mkdir($dirName . '/' . $tmpFileName . $d, 0755); } } $php = file_get_contents($dirName . '/code' . $path); $html = highlight_string($php, true); $html = preg_replace_callback('$<br />$s', function ($r) { static $line; if (!isset($line)) { $line = 2; } else { ++$line; } return "<br id=\"{$line}\" />{$line}) "; }, $html); $html = '<code><a id="1" />1) ' . substr($html, 6); file_put_contents($dirName . '/' . $tmpFileName . $path . '.html', $html); } if (file_exists($dirName . '/' . $finalName)) { rename($dirName . '/' . $finalName, $dirName . '/.' . $tmpFileName); rename($dirName . '/' . $tmpFileName, $dirName . '/' . $finalName); // Clean previous folder if ($dirName . '/.' . $tmpFileName !== '/') { rmdirRecursive($dirName . '/.' . $fileName); } } else { // No previous art, so just move rename($dirName . '/' . $tmpFileName, $dirName . '/' . $finalName); } }
private function cleanFolder() { if (file_exists($this->tmpName . '/datas/base.html')) { unlink($this->tmpName . '/datas/base.html'); unlink($this->tmpName . '/datas/menu.html'); } // Clean final destination if ($this->finalName !== '/') { rmdirRecursive($this->finalName); } if (file_exists($this->finalName)) { display($this->finalName . " folder was not cleaned. Please, remove it before producing the report. Aborting report\n"); return; } rename($this->tmpName, $this->finalName); }
return false; } // Deleting empty folders if (rmdirRecursive($extractPath . DIRECTORY_SEPARATOR . 'administrator') !== true) { return false; } if (rmdirRecursive($extractPath . DIRECTORY_SEPARATOR . 'components') !== true) { return false; } if (rmdirRecursive($extractPath . DIRECTORY_SEPARATOR . 'plugins') !== true) { return false; } if (rmdirRecursive($extractPath . DIRECTORY_SEPARATOR . 'libraries') !== true) { return false; } if (rmdirRecursive($extractPath . DIRECTORY_SEPARATOR . 'layouts') !== true) { return false; } $files = files($extractPath); $rootFiles = array('pkg_neno.xml', 'script.php', 'codeception.yml', 'composer.json', 'RoboFile.php'); $noExtensionFolders = array('tests', 'media', 'layouts', 'cli', 'packages', 'vendor'); foreach ($files as $file) { if (!in_array($file, $rootFiles)) { unlink($extractPath . DIRECTORY_SEPARATOR . $file); } } $folders = folders($extractPath); foreach ($folders as $extensionFolder) { if (!in_array($extensionFolder, $noExtensionFolders)) { // Parse installation file. $installationFileContent = file_get_contents($extractPath . DIRECTORY_SEPARATOR . $extensionFolder . DIRECTORY_SEPARATOR . 'neno.xml');
runCommand("/bin/rm -rf " . $path); } } if (!$runningOnWindows) { $result = array(); echo "Running as:"; exec('/usr/bin/whoami 2>&1', $result); foreach ($result as $row) { echo $row, "\n"; } } //Create and flush assets directory rmdirRecursive(pth($root . "frontend/www/assets")); createDirIfNotExists(pth($root . "frontend/www/assets")); //Create and flush assets directory rmdirRecursive(pth($root . "backend/www/assets")); createDirIfNotExists(pth($root . "backend/www/assets")); // runtime createDirIfNotExists(pth($root . "frontend/runtime")); createDirIfNotExists(pth($root . "console/runtime")); createDirIfNotExists(pth($root . "backend/runtime")); //Setting permissions if (!$runningOnWindows) { chmod(pth($root . "frontend/runtime"), 02775); //permissions with setguid chmod(pth($root . "console/runtime"), 02775); chmod(pth($root . "backend/runtime"), 02775); chmod(pth($root . "frontend/www/assets"), 02775); chmod(pth($root . "backend/www/assets"), 02775); } //applying migrations
public function run() { if (($file = $this->config->file) !== 'stdout') { display("Anonymizing file {$file}\n"); if (!file_exists($file)) { die('Can\'t anonymize ' . $file . ' as it doesn\'t exist' . "\n"); } if (!$this->checkCompilation($file)) { die('Can\'t anonymize ' . $file . ' as it doesn\'t compile with PHP ' . PHP_VERSION . "\n"); } $this->processFile($file); } else { $dir = $this->config->dirname; if (!empty($dir)) { if (substr($dir, -1) === '/') { $dir = substr($dir, 0, -1); } if (!file_exists($dir)) { die('Can\'t anonymize ' . $dir . ' as it doesn\'t exist' . "\n"); } display("Anonymizing directory {$dir}\n"); $files = rglob($dir); $total = 0; if (file_exists($dir . '.anon')) { rmdirRecursive($dir . '.anon'); } mkdir($dir . '.anon', 0755); foreach ($files as $file) { if ($this->checkCompilation($file)) { ++$this->strings; $total += (int) $this->processFile($file, $dir . '.anon/' . $this->strings . '.php'); } } display("Anonymized {$total} files\n"); } elseif (($project = $this->config->project) !== 'default') { display("Anonymizing project {$project}\n"); $dir = $this->config->projects_root . '/projects/' . $project . '/' . $project; if (!file_exists($this->config->projects_root . '/projects/' . $project)) { die('Can\'t anonymize project ' . $project . ' as it doesn\'t exist' . "\n"); } if (!file_exists($this->config->projects_root . '/projects/' . $project . '/code')) { die('Can\'t anonymize project ' . $project . ' as it doesn\'t have code' . "\n"); } $files = $this->datastore->getCol('files', 'file'); $path = $this->config->projects_root . '/projects/' . $this->config->project . '/code'; $total = 0; if (file_exists($dir . '.anon')) { rmdirRecursive($dir . '.anon'); } mkdir($dir . '.anon', 0755); foreach ($files as $file) { if ($this->checkCompilation($path . $file)) { ++$this->strings; $total += (int) $this->processFile($path . $file, $dir . '.anon/' . $this->strings . '.php'); } } display("Anonymized {$total} files\n"); } else { die("Usage : php exakat anonymize -file <filename>\n -d <dirname>\n -p <project>\n"); } } display('Processing file ' . $file . ' into ' . $file . ".anon\n"); }
// Compress library $files = files($extractPath . DIRECTORY_SEPARATOR . 'lib_neno', true); $zipData = array(); foreach ($files as $file) { $zipData[] = array('name' => str_replace($extractPath . DIRECTORY_SEPARATOR . 'lib_neno' . DIRECTORY_SEPARATOR, '', $file), 'file' => $file); } createZip($packagePath . DIRECTORY_SEPARATOR . 'packages' . DIRECTORY_SEPARATOR . 'lib_neno.zip', $zipData); rmdirRecursive($extractPath . DIRECTORY_SEPARATOR . 'lib_neno'); // Compress library $files = files($extractPath, true); $zipData = array(); foreach ($files as $file) { $zipData[] = array('name' => str_replace($extractPath . DIRECTORY_SEPARATOR, '', $file), 'file' => $file); } createZip($packagePath . DIRECTORY_SEPARATOR . 'pkg_neno.zip', $zipData); rmdirRecursive($packagePath . DIRECTORY_SEPARATOR . 'packages'); unlink($packagePath . DIRECTORY_SEPARATOR . 'pkg_neno.xml'); unlink($packagePath . DIRECTORY_SEPARATOR . 'script.php'); } function folders($path) { $it = new DirectoryIterator($path); $folders = array(); while ($it->valid()) { if (is_dir($it->getPathname()) && !$it->isDot() && $it->getFilename() != '.git') { $folders[] = $it->getFilename(); } $it->next(); } return $folders; }
public function generate($folder, $name = 'report') { $finalName = $name; $name = '.' . $name; if ($name === null) { return "Can't produce Devoops format to stdout"; } // Clean final destination if ($folder . '/' . $finalName !== '/') { rmdirRecursive($folder . '/' . $finalName); } if (file_exists($folder . '/' . $finalName)) { display($folder . '/' . $finalName . " folder was not cleaned. Please, remove it before producing the report. Aborting report\n"); return; } // Clean temporary destination if (file_exists($folder . '/' . $name)) { rmdirRecursive($folder . '/' . $name); } mkdir($folder . '/' . $name, Devoops::FOLDER_PRIVILEGES); mkdir($folder . '/' . $name . '/ajax', Devoops::FOLDER_PRIVILEGES); copyDir($this->config->dir_root . '/media/devoops/css', $folder . '/' . $name . '/css'); copyDir($this->config->dir_root . '/media/devoops/img', $folder . '/' . $name . '/img'); copyDir($this->config->dir_root . '/media/devoops/js', $folder . '/' . $name . '/js'); copyDir($this->config->dir_root . '/media/devoops/plugins', $folder . '/' . $name . '/plugins'); display("Copied media files"); $this->dump = new \Sqlite3($folder . '/dump.sqlite', SQLITE3_OPEN_READONLY); $this->datastore = new \sqlite3($folder . '/datastore.sqlite', \SQLITE3_OPEN_READONLY); // Compatibility $compatibility = array('Compilation' => 'Compilation'); foreach ($this->config->other_php_versions as $code) { if ($code == 52) { continue; } $version = $code[0] . '.' . substr($code, 1); $compatibility['Compatibility ' . $version] = 'Compatibility'; } // Analyze $analyze = array(); //count > 0 AND print 'SELECT * FROM resultsCounts WHERE analyzer in ' . $this->themesList . ' ORDER BY id'; $res = $this->sqlite->query('SELECT * FROM resultsCounts WHERE analyzer in ' . $this->themesList); while ($row = $res->fetchArray()) { $analyzer = Analyzer::getInstance($row['analyzer']); $this->analyzers[$analyzer->getDescription()->getName()] = $analyzer; $analyze[$analyzer->getDescription()->getName()] = 'OneAnalyzer'; } uksort($analyze, function ($a, $b) { return -strnatcmp($a, $b); }); $analyze = array_merge(array('Results Counts' => 'AnalyzersResultsCounts'), $analyze); // Files $files = array(); $res = $this->sqlite->query('SELECT DISTINCT file FROM results ORDER BY file'); while ($row = $res->fetchArray()) { $files[$row['file']] = 'OneFile'; } $files = array_merge(array('Files Counts' => 'FilesResultsCounts'), $files); $summary = array('Report presentation' => array('Audit configuration' => 'AuditConfiguration', 'Processed Files' => 'ProcessedFiles', 'Non-processed Files' => 'NonProcessedFiles'), 'Zend Framework' => $analyze); $summaryHtml = $this->makeSummary($summary); $faviconHtml = ''; if (file_exists($this->config->dir_root . '/projects/' . $this->config->project . '/code/favicon.ico')) { // Should be checked and reported copy($this->config->dir_root . '/projects/' . $this->config->project . '/code/favicon.ico', $folder . '/' . $name . '/img/' . $this->config->project . '.ico'); $faviconHtml = <<<HTML <img src="img/{$this->config->project}.ico" class="img-circle" alt="{$this->config->project} logo" /> HTML; if (!empty($this->config->project_url)) { $faviconHtml = "<a href=\"{$this->config->project_url}\" class=\"avatar\">{$faviconHtml}</a>"; } $faviconHtml = <<<HTML \t\t\t\t<div class="avatar"> \t\t\t\t\t{$faviconHtml} \t\t\t\t</div> HTML; } $html = file_get_contents($this->config->dir_root . '/media/devoops/index.exakat.html'); $html = str_replace('<menu>', $summaryHtml, $html); $html = str_replace('EXAKAT_VERSION', Exakat::VERSION, $html); $html = str_replace('EXAKAT_BUILD', Exakat::BUILD, $html); $html = str_replace('PROJECT_NAME', $this->config->project_name, $html); $html = str_replace('PROJECT_FAVICON', $faviconHtml, $html); file_put_contents($folder . '/' . $name . '/index.html', $html); foreach ($summary as $titleUp => $section) { foreach ($section as $title => $method) { if (method_exists($this, $method)) { $html = $this->{$method}($title); } else { $html = 'Using default for ' . $title . "\n"; display($html); } $filename = $this->makeFileName($title); $html = <<<HTML <script language="javascript"> if (!document.getElementById("main")) { window.location.href = "../index.html#ajax/{$filename}"; } </script > <div class="row"> \t<div id="breadcrumb" class="col-xs-12"> \t\t<a href="#" class="show-sidebar"> \t\t\t<i class="fa fa-bars"></i> \t\t</a> \t\t<ol class="breadcrumb pull-left"> \t\t\t<li><a href="index.html">Dashboard</a></li> \t\t\t<li><a href="#ajax/About-This-Report.html">About This Report</a></li> \t\t</ol> \t</div> </div> <h4 class="page-header">{$title}</h4> <div class="row"> \t<div class="col-xs-12"> {$html} </div> </div> HTML; file_put_contents($folder . '/' . $name . '/ajax/' . $filename, $html); } } rename($folder . '/' . $name, $folder . '/' . $finalName); return ''; }
private function cleanTmpDir() { rmdirRecursive($this->config->projects_root . '/projects/.exakat/'); mkdir($this->config->projects_root . '/projects/.exakat/'); }