function scanDirectory($dir, $classPath = array()) { global $classTree; $d = dir($dir); echo "Scanning directory: " . $d->path . "\n"; while (false !== ($entry = $d->read())) { if ($entry[0] == '.') { continue; } $entryPath = $d->path . DIRECTORY_SEPARATOR . $entry; $entryClassPath = $classPath; $entryClassPath[] = preg_replace('/.md$/', '', $entry); if (is_dir($entryPath)) { scanDirectory($entryPath, $entryClassPath); } else { $entryName = implode('', array_map('ucfirst', $entryClassPath)); $parentClassPath = '/' . implode('/', $classPath); if (!isset($classTree[$parentClassPath])) { $classTree[$parentClassPath] = array(); } $classTree[$parentClassPath][$entryPath] = $entryName; } } $d->close(); }
function scanDirectory($path) { if (strstr("{$path}", '/.svn')) { echo 'SKIPPING ' . $path . "\n"; } elseif (is_dir($path)) { echo 'SCANNING ' . $path . "\n"; $dir = dir($path); while (false !== ($file = $dir->read())) { if ($file != '.' && $file != '..') { if (!is_link("{$path}/{$file}") && is_dir("{$path}/{$file}")) { scanDirectory("{$path}/{$file}"); } else { scanFile("{$path}/{$file}"); } } } $dir->close(); } }
/** * Scan directory * @param string $dir * @param string $classmap */ function scanDirectory($dir, &$classmap, &$counter) { global $ROOT_DIR; $handle = opendir($dir); while ($file = readdir($handle)) { if ($file != '.' && $file != '..') { if (is_dir($dir . $file)) { scanDirectory($dir . $file . '/', $classmap, $counter); } else { if (substr($file, 0, 6) == 'class.' && substr($file, -4) == '.php') { $counter++; $classname = substr(substr($file, 6), 0, -4); $filename = substr($dir . $file, strlen($ROOT_DIR)); $classmap .= "'{$classname}' => '{$filename}',\n"; } } } } closedir($handle); }
function _processFile($dir, $f, &$xmlStructure) { $filepath = "{$dir}/{$f}"; // $fileURL = "http://".$_SERVER[SERVER_NAME].dirname($_SERVER[SCRIPT_NAME]).substr($dir, 1)."/$f"; // what level are we at? $depth = strlen(preg_replace("/[^\\/]/", "", $dir)); // for directories (including symbolic links) if (is_link($filepath) || is_dir($filepath)) { // we don't examine some certain folders if (preg_match(IGNORE_FOLDERS, $filepath)) { return; } $name = "subfolder"; switch ($depth) { case 1: $name = "group"; break; case 2: $name = "project"; break; } $subnode = $xmlStructure->addChild($name); $subnode->addAttribute("name", $f); $subnode->addAttribute("depth", $depth); $subnode->addAttribute("path", $filepath); // resolve the link if (is_link($filepath)) { $filepath = resolvePath("{$dir}/" . readlink($filepath)); } scanDirectory($filepath, $subnode); // for text contents, e.g. contact } else { if (preg_match("/\\.(txt|md)\$/", $f)) { if (is_readable($filepath)) { $text = trim(file_get_contents($filepath)); if (strlen($text)) { $subnode = $xmlStructure->addChild("text", htmlentities($text)); $subnode->addAttribute("type", preg_replace("/\\.(txt|md)/", "", $f)); } } // content files } else { $subnode = $xmlStructure->addChild("file"); $subnode->addAttribute("path", $filepath); $subnode->addAttribute("name", preg_replace("/\\.[^.]+\$/", "", $f)); } } }
function showOverview($blogpage, $entriesPerPage) { $numEntriesCounted = 0; $numEntriesPrinted = 0; /* YEARS */ $arrayYears = scanDirectory("entries/", TRUE); foreach ($arrayYears as $currentYear) { $yearPath = "entries/{$currentYear}/"; /* MONTHS */ $arrayMonths = scanDirectory($yearPath, TRUE); foreach ($arrayMonths as $currentMonth) { $monthPath = $yearPath . "{$currentMonth}/"; /* DAYS */ $arrayDays = scanDirectory($monthPath, TRUE); foreach ($arrayDays as $currentDay) { $dayPath = $monthPath . "{$currentDay}/"; /* ENTRIES */ $arrayEntries = scanDirectory($dayPath, FALSE); foreach ($arrayEntries as $currentEntry) { if (is_dir($dayPath . $currentEntry)) { continue; } preg_match("/\\.([^\\.]+)\$/", $currentEntry, $matches); if ($matches[1] != "txt") { continue; } if ($numEntriesCounted < ($blogpage - 1) * $entriesPerPage) { $numEntriesCounted += 1; continue; } $blogentry = array("year" => $currentYear, "month" => $currentMonth, "day" => $currentDay, "id" => substr($currentEntry, 0, -4)); showEntry($blogentry, TRUE); $numEntriesPrinted += 1; if ($numEntriesPrinted >= $entriesPerPage) { return $numEntriesPrinted; } } } } } if ($numEntriesPrinted == 0) { echo "[no content]"; } return $numEntriesPrinted; }
function exportDir($dirid, $dirpath, &$i) { global $_lang; global $base; global $modx; global $limit; global $dbase; global $table_prefix; global $sqlcond; $sql = "SELECT id, alias, pagetitle, isfolder, (content = '' AND template = 0) AS wasNull, editedon FROM {$dbase}.`" . $table_prefix . "site_content` WHERE {$dbname}.`" . $table_prefix . "site_content`.parent = " . $dirid . " AND " . $sqlcond; $rs = mysql_query($sql); $dircontent = array(); while ($row = mysql_fetch_assoc($rs)) { if (!$row['wasNull']) { // needs writing a document $docname = getPageName($row['id'], $row['alias'], $modx->config['friendly_url_prefix'], $suffix = $modx->config['friendly_url_suffix']); printf($_lang['export_site_exporting_document'], $i++, $limit, $row['pagetitle'], $row['id']); $filename = $dirpath . $docname; if (is_dir($filename)) { removeDirectoryAll($filename); } if (!file_exists($filename) || filemtime($filename) < $row['editedon']) { if (!writeAPage($base, $row['id'], $filename)) { exit; } } else { echo '<p><span class="success">' . $_lang['export_site_success'] . "</span> " . $_lang["export_site_success_skip_doc"] . '</p>'; } $dircontent[] = $docname; } if ($row['isfolder']) { // needs making a folder $dirname = $dirpath . $row['alias']; if (!is_dir($dirname)) { if (file_exists($dirname)) { @unlink($dirname); } mkdir($dirname); if ($row['wasNull']) { printf($_lang['export_site_exporting_document'], $i++, $limit, $row['pagetitle'], $row['id']); echo '<p class="success">' . $_lang['export_site_success'] . '</p>'; } } else { if ($row['wasNull']) { printf($_lang['export_site_exporting_document'], $i++, $limit, $row['pagetitle'], $row['id']); echo '<p><span class="success">' . $_lang['export_site_success'] . $_lang["export_site_success_skip_dir"] . '</p>'; } } exportDir($row['id'], $dirname . "/", $i); $dircontent[] = $row['alias']; } } // remove No-MODx files/dirs if (!scanDirectory($dirpath, $dircontent)) { exit; } // print_r ($dircontent); }
$result[$sections[$j]] = $values[$j]; } else { $result[] = $values[$j]; } } return $result + $globals; } function processFile($from, $to) { echo "Processing " . basename($from) . "...\n"; $raw_data = parse_ini_file_php($from, false, false); $out = ''; foreach ($raw_data as $key => $line) { $out .= $key . '="'; $out .= str_replace('"', '', $line); // I hate myself, I hate Joomla! for relying on the f*cking PHP INI parser :( $out .= "\"\n"; } echo ">>> {$to}\n"; file_put_contents($to, $out); } $base_path = dirname(__FILE__) . DS . '..' . DS . 'translations'; $target_base = dirname(__FILE__); $files = scanDirectory($base_path . DS . 'backend'); foreach ($files as $file) { processFile($file, $target_base . DS . 'backend' . DS . basename($file)); } $files = scanDirectory($base_path . DS . 'frontend'); foreach ($files as $file) { processFile($file, $target_base . DS . 'frontend' . DS . basename($file)); }
} echo "FILE: " . $file . "\n"; $csscontent = file_get_contents($file); if ($csscontent === FALSE) { echo " not found.\n"; return ''; } $comp = ''; for ($i = 0; $i < strlen($csscontent);) { $x = strpos($csscontent, '@import url(', $i); if ($x === FALSE) { $comp .= substr($csscontent, $i); break; } $comp .= substr($csscontent, $i, $x - $i); $x += 12; $y = strpos($csscontent, ')', $x); $import = substr($csscontent, $x, $y - $x); $comp .= makeCSS($import); $i = $y + 2; // skip ); } return $comp; } // ===== MAIN ===== $files = scanDirectory('.', 'css'); //var_dump($files); foreach ($files as $f) { $dir = substr($f, 0, -4); file_put_contents($dir . '/' . $dir . '.pre.css', makeCSS($f)); }
private function scanDirectory($directory) { $files = array(); $dirs = array(); $handle = opendir($directory); if ($handle === false) { return array(); } while ($aFile = readdir($handle)) { if ($aFile != '.' && $aFile != '..') { if (is_file($directory . '/' . $aFile)) { $files[] = $aFile; } else { if ($aFile != '.svn' && is_dir($directory . '/' . $aFile)) { $dirs[] = $aFile; } } } } closedir($handle); // Recurse into sub-directories if (count($dirs)) { foreach ($dirs as $dir) { $morefiles = scanDirectory($directory . '/' . $dir); if (count($morefiles) == 0) { // Empty directory $files[] = $dir; } else { foreach ($morefiles as $aFile) { $files[] = $dir . '/' . $aFile; } } } } return $files; }
$opt = getopt('hd:', ['help', 'mask', 'unmask']); if (isset($opt['h']) || isset($opt['help'])) { showHelp(); exit(0); } $dir = realpath(__DIR__ . DIRECTORY_SEPARATOR . '..'); if (isset($opt['d'])) { $dir = $opt['d']; } if (isset($opt['mask'])) { echo "Mask *.php files in {$dir}\n"; $re = scanDirectory($dir, function ($f) { mask($f); }); if ($re !== 0) { echo "Fail to mask!\n"; } } elseif (isset($opt['unmask'])) { echo "Unmask *.php files in {$dir}\n"; $re = scanDirectory($dir, function ($f) { unmask($f); }); if ($re !== 0) { echo "Fail to unmask!\n"; } } else { echo "Invalid options\n"; showHelp(); $re = 0; } exit($re);