public static function getDiskUsageStats($update = true) { $config = KTConfig::getSingleton(); $cmd = KTUtil::findCommand('externalBinary/df', 'df'); if ($cmd === false) { if ($update) { KTUtil::setSystemSetting('DiskUsage', 'n/a'); } return false; } $warningPercent = $config->get('DiskUsage/warningThreshold', 15); $urgentPercent = $config->get('DiskUsage/urgentThreshold', 5); if (OS_WINDOWS) { $cmd = str_replace('/', '\\', $cmd); $res = KTUtil::pexec("\"{$cmd}\" -B 1 2>&1"); $result = implode("\r\n", $res['out']); } else { if (strtolower(PHP_OS) == 'darwin') { $result = shell_exec($cmd . " -k 2>&1"); } else { $result = shell_exec($cmd . " -B 1 2>&1"); } } if (strpos($result, 'cannot read table of mounted file systems') !== false) { if ($update) { KTUtil::setSystemSetting('DiskUsage', 'n/a'); } return false; } $result = explode("\n", $result); unset($result[0]); // gets rid of headings $usage = array(); foreach ($result as $line) { if (empty($line)) { continue; } preg_match('/(.*)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\%\\s+(.*)/', $line, $matches); list($line, $filesystem, $size, $used, $avail, $usedp, $mount) = $matches; if ($size === 0 || empty($size)) { continue; } if (strtolower(PHP_OS) == 'darwin') { $size = $size * 1024; $used = $used * 1024; $avail = $avail * 1024; } $colour = ''; if ($usedp >= 100 - $urgentPercent) { $colour = 'red'; } elseif ($usedp >= 100 - $warningPercent) { $colour = 'orange'; } $usage[] = array('filesystem' => trim($filesystem), 'size' => KTUtil::filesizeToString($size), 'used' => KTUtil::filesizeToString($used), 'available' => KTUtil::filesizeToString($avail), 'usage' => $usedp . '%', 'mounted' => trim($mount), 'colour' => $colour); } if ($update) { KTUtil::setSystemSetting('DiskUsage', serialize($usage)); } return $usage; }
} else { // If the OS is Unix or Linux $sPath = realpath('../../dmsctl.sh'); if (file_exists($sPath)) { // If Open office needs to be restarted - stop it here if ($restartOO) { $sCmd = "\"{$sPath}\" restart soffice >/dev/null &"; $default->log->debug('Check Open Office Task: ' . get_current_user()); $default->log->debug('Check Open Office Task: ' . $sCmd); KTUtil::pexec($sCmd); $default->log->debug('Check Open Office Task: Attempted restart using dmsctl.sh.'); } else { $sCmd = "\"{$sPath}\" start soffice >/dev/null &"; $default->log->debug('Check Open Office Task: ' . get_current_user()); $default->log->debug('Check Open Office Task: ' . $sCmd); KTUtil::pexec($sCmd); $default->log->debug('Check Open Office Task: Attempted start using dmsctl.sh.'); } if ($sGiveOutput) { echo 2; } exit; } else { $default->log->debug('Check Open Office Task: Can\'t find dmsctl.sh, this may be a source install.'); if ($sGiveOutput) { echo 0; } exit; } } $default->log->debug('Check Open Office Task: Can\'t start Open office, this may be a source install.');
// Run the script $cmd = "\"{$file}\" {$sParameters}"; if ($ext == 'php') { $oKTConfig = KTConfig::getSingleton(); $phpPath = $oKTConfig->get('externalBinary/php', 'php'); //$phpPath = KTUtil::findCommand('externalBinary/php'); // being protective as some scripts work on relative paths $dirname = dirname($file); chdir($dirname); $cmd = "\"{$phpPath}\" {$cmd}"; } if (OS_WINDOWS) { $default->log->debug("Scheduler - dirname: {$dirname} cmd: {$cmd}"); //$WshShell = new COM("WScript.Shell"); //$res = $WshShell->Run($cmd, 0, true); KTUtil::pexec($cmd); } else { $cmd .= strtolower($sTask) == 'openoffice test' ? ' >/dev/null &' : ' 2>&1'; $default->log->debug("Scheduler cmd: {$cmd}"); $res = shell_exec($cmd); } // On completion - reset run time $iEnd = KTUtil::getBenchmarkTime(); $iDuration = number_format($iEnd - $iStart, 2); $ignore = array('openoffice test'); if (!empty($res)) { $func = in_array(strtolower($sTask), $ignore) ? 'debug' : 'info'; $default->log->{$func}("Scheduler - Task: {$sTask}"); $default->log->{$func}("Scheduler - Command: {$cmd}"); $default->log->{$func}("Scheduler - Output: {$res}"); $default->log->{$func}("Scheduler - Background tasks should not produce output. Please review why this is producing output.");
function init() { $oKTConfig =& KTConfig::getSingleton(); $sBasedir = $oKTConfig->get("urls/tmpDirectory"); $sTmpPath = tempnam($sBasedir, 'archiveimportstorage'); if ($sTmpPath === false) { return PEAR::raiseError(_kt("Could not create temporary directory for archive storage")); } if (!file_exists($this->sZipPath)) { return PEAR::raiseError(_kt("Archive file given does not exist")); } unlink($sTmpPath); mkdir($sTmpPath, 0700); $this->sBasePath = $sTmpPath; // Set environment language to output character encoding $sOutputEncoding = $oKTConfig->get('export/encoding', 'UTF-8'); $loc = $sOutputEncoding; putenv("LANG={$loc}"); putenv("LANGUAGE={$loc}"); $loc = setlocale(LC_ALL, $loc); // File Archive doesn't unzip properly so sticking to the original unzip functionality if ($this->sExtension == 'zip') { // ** Original zip functionality $sUnzipCommand = KTUtil::findCommand("import/unzip", "unzip"); if (empty($sUnzipCommand)) { return PEAR::raiseError(_kt("unzip command not found on system")); } $aArgs = array($sUnzipCommand, "-q", "-n", "-d", $sTmpPath, $this->sZipPath); $aRes = KTUtil::pexec($aArgs); if ($aRes['ret'] !== 0) { return PEAR::raiseError(_kt("Could not retrieve contents from zip storage")); } } else { File_Archive::extract(File_Archive::readArchive($this->sExtension, File_Archive::readUploadedFile('file')), $dst = $sTmpPath); } }
/** * Creates an archive file * * @return string */ public function archive() { $zip = KTUtil::findCommand('export/zip', 'zip'); chdir(dirname($this->path)); $subdir = basename($this->path); $archivename = $this->path . '.zip'; $cmd = "\"{$zip}\" -r \"{$archivename}\" \"{$subdir}\""; KTUtil::pexec($cmd); return $archivename; }
/** * Zip the temp folder */ function createZipFile($bEchoStatus = FALSE) { if (empty($this->aPaths)) { return PEAR::raiseError(_kt("No folders or documents found to compress")); } // Set environment language to output character encoding $loc = $this->sOutputEncoding; putenv("LANG={$loc}"); putenv("LANGUAGE={$loc}"); $loc = setlocale(LC_ALL, $loc); $sManifest = sprintf("%s/%s", $this->sTmpPath, "MANIFEST"); file_put_contents($sManifest, join("\n", $this->aPaths)); $sZipFile = sprintf("%s/%s.zip", $this->sTmpPath, $this->sZipFileName); $sZipFile = str_replace('<', '', str_replace('</', '', str_replace('>', '', $sZipFile))); $sZipCommand = KTUtil::findCommand("export/zip", "zip"); $aCmd = array($sZipCommand, "-r", $sZipFile, ".", "-i@MANIFEST"); $sOldPath = getcwd(); chdir($this->sTmpPath); // Note that the popen means that pexec will return a file descriptor $aOptions = array('popen' => 'r'); $fh = KTUtil::pexec($aCmd, $aOptions); if ($bEchoStatus) { $last_beat = time(); while (!feof($fh)) { if ($i % 1000 == 0) { $this_beat = time(); if ($last_beat + 1 < $this_beat) { $last_beat = $this_beat; print " "; } } $contents = fread($fh, 4096); if ($contents) { print nl2br($this->_convertEncoding($contents, false)); } $i++; } } pclose($fh); // Save the zip file and path into session $_SESSION['zipcompression'] = KTUtil::arrayGet($_SESSION, 'zipcompression', array()); $sExportCode = KTUtil::randomString(); $_SESSION['zipcompression'][$sExportCode] = array('file' => $sZipFile, 'dir' => $this->sTmpPath); $_SESSION['zipcompression']['exportcode'] = $sExportCode; $this->sZipFile = $sZipFile; return $sExportCode; }
function extract_contents($sFilename, $sTempFilename) { $sCommand = KTUtil::findCommand($this->commandconfig, $this->command); if (empty($sCommand)) { return false; } $cmdline = array($sCommand); $cmdline = kt_array_merge($cmdline, $this->args); $cmdline[] = $sFilename; $aOptions = array(); $aOptions['exec_wait'] = 'true'; if ($this->use_pipes) { $aOptions["append"] = $sTempFilename; } else { $cmdline[] = $sTempFilename; } $aRet = KTUtil::pexec($cmdline, $aOptions); $this->aCommandOutput = $aRet['out']; $contents = file_get_contents($sTempFilename); return $contents; }
function extract_contents($sFilename, $sTmpFilename) { $sUnzipCommand = KTUtil::findCommand("import/unzip", "unzip"); if (empty($sUnzipCommand)) { return; } $oKTConfig =& KTConfig::getSingleton(); $sBasedir = $oKTConfig->get("urls/tmpDirectory"); $this->sTmpPath = tempnam($sBasedir, 'opendocumentextract'); if ($this->sTmpPath === false) { return; } unlink($this->sTmpPath); mkdir($this->sTmpPath, 0700); $sCmd = array($sUnzipCommand, "-q", "-n", "-d", $this->sTmpPath, $sFilename); KTUtil::pexec($sCmd, array('exec_wait' => 'true')); $sManifest = sprintf("%s/%s", $this->sTmpPath, "META-INF/manifest.xml"); if (OS_WINDOWS) { $sManifest = str_replace('/', '\\', $sManifest); } if (!file_exists($sManifest)) { $this->cleanup(); return; } $sContentFile = sprintf("%s/%s", $this->sTmpPath, "content.xml"); if (OS_WINDOWS) { $sContentFile = str_replace('/', '\\', $sContentFile); } if (!file_exists($sContentFile)) { $this->cleanup(); return; } $sContent = file_get_contents($sContentFile); $sContent = preg_replace("@(</?[^>]*>)+@", " ", $sContent); $this->cleanup(); return $sContent; }
/** * Executes a command. Returns true if successful. * * @param string $cmd A command line instruction. * @return boolean */ protected function exec($cmd) { $config = KTConfig::getSingleton(); $temp_dir = $config->get('urls/tmpDirectory'); $res = 0; $docid = $this->document->getId(); $script_prefix = $temp_dir . '/' . time() . '-' . $docid; $script_out = $script_prefix . '.out'; // define the scripts that we want if (OS_WINDOWS) { $script_name = $script_prefix . '.bat'; $script = "rem This is an auto generated file. \n"; $script .= $cmd . ' 2>"' . $script_out . "\"\r\n"; $script .= "set er=%ERRORLEVEL%\r\n"; $script .= "exit /B %er%\r\n"; } else { $script_name = $script_prefix . '.sh'; $script = "#!/bin/sh\n"; $script .= "# This is an auto generated file. \n"; $script .= $cmd . ' 2>>"' . $script_out . "\""; if ($this->pipeStdoutToDevNull) { $script .= " >/dev/null"; } $script .= "\n"; $script .= "exit \$?\n"; } // write the script file if (file_put_contents($script_name, $script) === false) { $this->output = _kt('Could not create exec script: ') . $script_name; return false; } // execute the script file if (OS_WINDOWS) { $res = KTUtil::pexec("\"{$script_name}\""); $res = $res['ret']; } else { if (chmod($script_name, 0755) === false) { $this->output = _kt('Could change permission on exec script: ') . $script_name; return false; } system($script_name, $res); } // remote the script file and get the output if available @unlink($script_name); if (file_exists($script_out)) { $this->output = file_get_contents($script_out); @unlink($script_out); } return $res == 0 && (empty($this->output) || $this->allowOutput); }
/** * Generates the thumbnail from the pdf * * @return boolean */ private function generateThumbnail() { /* The thumbnail is displayed in the info panel and the document view The info panel is in the plugin ktcore/documentpreview/ - add a hook in there but build the functionality in this plugin ie keep the plugins separate and don't create dependencies - if the thumbnail plugin is disabled then maybe display a normal sized info panel The document view will display the thumbnail on the right in a document viewlet similar to the workflow viewlet - check out ktcore/KTDocumentViewlets.php - viewlet class is below */ global $default; $mimeTypeId = $this->document->getMimeTypeID(); $mimeType = KTMime::getMimeTypeName($mimeTypeId); // Get the pdf source file - if the document is a pdf then use the document as the source if ($mimeType == 'application/pdf') { $pdfDir = $default->documentRoot; $pdfFile = $pdfDir . DIRECTORY_SEPARATOR . $this->document->getStoragePath(); } else { $pdfDir = $default->pdfDirectory; $pdfFile = $pdfDir . DIRECTORY_SEPARATOR . $this->document->iId . '.pdf'; } $thumbnaildir = $default->varDirectory . DIRECTORY_SEPARATOR . 'thumbnails'; if (stristr(PHP_OS, 'WIN')) { $thumbnaildir = str_replace('/', '\\', $thumbnaildir); $pdfFile = str_replace('/', '\\', $pdfFile); } $thumbnailfile = $thumbnaildir . DIRECTORY_SEPARATOR . $this->document->iId . '.jpg'; //if thumbail dir does not exist, generate one and add an index file to block access if (!file_exists($thumbnaildir)) { mkdir($thumbnaildir, 0755); touch($thumbnaildir . DIRECTORY_SEPARATOR . 'index.html'); file_put_contents($thumbnaildir . DIRECTORY_SEPARATOR . 'index.html', 'You do not have permission to access this directory.'); } // if there is no pdf that exists - hop out if (!file_exists($pdfFile)) { $default->log->debug('Thumbnail Generator Plugin: PDF file does not exist, cannot generate a thumbnail'); return false; } // if a previous version of the thumbnail exists - delete it if (file_exists($thumbnailfile)) { @unlink($thumbnailfile); } // do generation $pathConvert = !empty($default->convertPath) ? $default->convertPath : 'convert'; // windows path may contain spaces if (stristr(PHP_OS, 'WIN')) { $cmd = "\"{$pathConvert}\" \"{$pdfFile}[0]\" -resize 200x200 \"{$thumbnailfile}\""; } else { $cmd = "{$pathConvert} {$pdfFile}[0] -resize 200x200 {$thumbnailfile}"; } $result = KTUtil::pexec($cmd); return true; }