/** * Deletes the web server writable directories. * * @param int $value * 1: clean templates_c, 2: clean upload, 3: clean both * @param bool $rmdir */ public function cleanup($value, $rmdir = TRUE) { $value = (int) $value; if ($value & 1) { // clean templates_c CRM_Utils_File::cleanDir($this->templateCompileDir, $rmdir); CRM_Utils_File::createDir($this->templateCompileDir); } if ($value & 2) { // clean upload dir CRM_Utils_File::cleanDir($this->uploadDir); CRM_Utils_File::createDir($this->uploadDir); } // Whether we delete/create or simply preserve directories, we should // certainly make sure the restrictions are enforced. foreach (array($this->templateCompileDir, $this->uploadDir, $this->configAndLogDir, $this->customFileUploadDir) as $dir) { if ($dir && is_dir($dir)) { CRM_Utils_File::restrictAccess($dir); } } }
/** * Install a module and then delete (leaving stale DB info); restore * the module by downloading new code. * * Note that some metadata changes between versions -- the original has * file="oddball", and the upgrade has file="newextension". */ public function testReplace_InstalledMissing() { $testingTypeManager = $this->getMock('CRM_Extension_Manager_Interface'); $manager = $this->_createManager(array(self::TESTING_TYPE => $testingTypeManager)); // initial installation $this->assertEquals('uninstalled', $manager->getStatus('test.whiz.bang')); $manager->install(array('test.whiz.bang')); $this->assertEquals('installed', $manager->getStatus('test.whiz.bang')); // dirty remove $this->assertTrue(file_exists("{$this->basedir}/weird/whizbang/info.xml")); CRM_Utils_File::cleanDir("{$this->basedir}/weird/whizbang", TRUE, FALSE); $this->assertFalse(file_exists("{$this->basedir}/weird/whizbang/info.xml")); $manager->refresh(); $this->assertEquals('installed-missing', $manager->getStatus('test.whiz.bang')); // download and reinstall $this->download = $this->_createDownload('test.whiz.bang', 'newextension'); $testingTypeManager->expects($this->once())->method('onPreReplace'); $testingTypeManager->expects($this->once())->method('onPostReplace'); $manager->replace($this->download); $this->assertEquals('installed', $manager->getStatus('test.whiz.bang')); $this->assertTrue(file_exists("{$this->basedir}/test.whiz.bang/info.xml")); $this->assertTrue(file_exists("{$this->basedir}/test.whiz.bang/newextension.php")); $this->assertEquals('newextension', $this->mapper->keyToInfo('test.whiz.bang')->file); $this->assertDBQuery('newextension', 'SELECT file FROM civicrm_extension WHERE full_name ="test.whiz.bang"'); }
/** * Install an extension then forcibly remove the code and cleanup DB afterwards. */ public function testInstall_DirtyRemove_Disable_Restore() { // create temporary extension (which can dirtily remove later) $this->_createExtension('test.extension.manager.module.auto2', 'module', 'test_extension_manager_module_auto2'); $mainfile = $this->basedir . '/test.extension.manager.module.auto2/test_extension_manager_module_auto2.php'; $this->assertTrue(file_exists($mainfile)); $manager = $this->system->getManager(); $this->assertModuleActiveByName(FALSE, 'test_extension_manager_module_auto2'); $this->assertModuleActiveByKey(FALSE, 'test.extension.manager.module.auto2'); // install it $manager->install(array('test.extension.manager.module.auto2')); $this->assertEquals('installed', $manager->getStatus('test.extension.manager.module.auto2')); $this->assertHookCounts('test_extension_manager_module_auto2', array('install' => 1, 'enable' => 1, 'disable' => 0, 'uninstall' => 0)); $this->assertModuleActiveByName(TRUE, 'test_extension_manager_module_auto2'); $this->assertModuleActiveByKey(TRUE, 'test.extension.manager.module.auto2'); // dirty removal CRM_Utils_File::cleanDir($this->basedir . '/test.extension.manager.module.auto2', TRUE, FALSE); $manager->refresh(); $this->assertEquals('installed-missing', $manager->getStatus('test.extension.manager.module.auto2')); // disable while missing $manager->disable(array('test.extension.manager.module.auto2')); $this->assertEquals('disabled-missing', $manager->getStatus('test.extension.manager.module.auto2')); $this->assertHookCounts('test_extension_manager_module_auto2', array('install' => 1, 'enable' => 1, 'disable' => 0, 'uninstall' => 0)); $this->assertModuleActiveByName(FALSE, 'test_extension_manager_module_auto2'); $this->assertModuleActiveByKey(FALSE, 'test.extension.manager.moduletest'); // restore the code $this->_createExtension('test.extension.manager.module.auto2', 'module', 'test_extension_manager_module_auto2'); $manager->refresh(); $this->assertHookCounts('test_extension_manager_module_auto2', array('install' => 1, 'enable' => 1, 'disable' => 0, 'uninstall' => 0)); $this->assertEquals('disabled', $manager->getStatus('test.extension.manager.module.auto2')); $this->assertModuleActiveByName(FALSE, 'test_extension_manager_module_auto2'); $this->assertModuleActiveByKey(FALSE, 'test.extension.manager.module.auto2'); }
/** * delete a directory given a path name, delete children directories * and files if needed * * @param string $path the path name * * @return void * @access public * @static */ public function cleanDir($target, $rmdir = true) { static $exceptions = array('.', '..'); if ($sourcedir = @opendir($target)) { while (false !== ($sibling = readdir($sourcedir))) { if (!in_array($sibling, $exceptions)) { $object = $target . DIRECTORY_SEPARATOR . $sibling; if (is_dir($object)) { CRM_Utils_File::cleanDir($object, $rmdir); } else { if (is_file($object)) { $result = @unlink($object); } } } } closedir($sourcedir); if ($rmdir) { $result = @rmdir($target); } } }
/** * delete the web server writable directories * * @param int $value 1 - clean templates_c, 2 - clean upload, 3 - clean both * * @access public * @return void */ public function cleanup($value) { $value = (int) $value; if ($value & 1) { // clean templates_c CRM_Utils_File::cleanDir($this->templateCompileDir); CRM_Utils_File::createDir($this->templateCompileDir); } if ($value & 2) { // clean upload dir CRM_Utils_File::cleanDir($this->uploadDir); CRM_Utils_File::createDir($this->uploadDir); } }
/** * Extract an extension from a zip file. * * @param string $key * The name of the extension being installed; this usually matches the basedir in the .zip. * @param string $zipFile * The local path to a .zip file. * @return string|FALSE * zip file path */ public function extractFiles($key, $zipFile) { $config = CRM_Core_Config::singleton(); $zip = new ZipArchive(); $res = $zip->open($zipFile); if ($res === TRUE) { $zipSubDir = CRM_Utils_Zip::guessBasedir($zip, $key); if ($zipSubDir === FALSE) { CRM_Core_Session::setStatus(ts('Unable to extract the extension: bad directory structure'), '', 'error'); return FALSE; } $extractedZipPath = $this->tmpDir . DIRECTORY_SEPARATOR . $zipSubDir; if (is_dir($extractedZipPath)) { if (!CRM_Utils_File::cleanDir($extractedZipPath, TRUE, FALSE)) { CRM_Core_Session::setStatus(ts('Unable to extract the extension: %1 cannot be cleared', array(1 => $extractedZipPath)), ts('Installation Error'), 'error'); return FALSE; } } if (!$zip->extractTo($this->tmpDir)) { CRM_Core_Session::setStatus(ts('Unable to extract the extension to %1.', array(1 => $this->tmpDir)), ts('Installation Error'), 'error'); return FALSE; } $zip->close(); } else { CRM_Core_Session::setStatus(ts('Unable to extract the extension.'), '', 'error'); return FALSE; } return $extractedZipPath; }
/** * Move $fromDir to $toDir, replacing/deleting any * pre-existing content. * * @param string $fromDir * The directory which should be moved. * @param string $toDir * The new location of the directory. * @param bool $verbose * * @return bool * TRUE on success */ public static function replaceDir($fromDir, $toDir, $verbose = FALSE) { if (is_dir($toDir)) { if (!self::cleanDir($toDir, TRUE, $verbose)) { return FALSE; } } // return rename($fromDir, $toDir); CRM-11987, https://bugs.php.net/bug.php?id=54097 CRM_Utils_File::copyDir($fromDir, $toDir); if (!CRM_Utils_File::cleanDir($fromDir, TRUE, FALSE)) { CRM_Core_Session::setStatus(ts('Failed to clean temp dir: %1', array(1 => $fromDir)), '', 'alert'); return FALSE; } return TRUE; }
public function cleanTempDirs() { if (!is_array($this->tempDirs)) { // fix test errors where this is not set return; } foreach ($this->tempDirs as $tempDir) { if (is_dir($tempDir)) { CRM_Utils_File::cleanDir($tempDir, TRUE, FALSE); } } }
public function deleteFiles($id, $key) { require_once 'CRM/Utils/File.php'; $e = self::$_extensions; CRM_Utils_File::cleanDir($e['per_id'][$id]['path']); }
/** * delete a directory given a path name, delete children directories * and files if needed * * @param string $path the path name * * @return void * @access public * @static */ public static function cleanDir($target, $rmdir = TRUE, $verbose = TRUE) { static $exceptions = array('.', '..'); if ($target == '' || $target == '/') { throw new Exception("Overly broad deletion"); } if ($sourcedir = @opendir($target)) { while (FALSE !== ($sibling = readdir($sourcedir))) { if (!in_array($sibling, $exceptions)) { $object = $target . DIRECTORY_SEPARATOR . $sibling; if (is_dir($object)) { CRM_Utils_File::cleanDir($object, $rmdir); } elseif (is_file($object)) { if (!unlink($object)) { CRM_Core_Session::setStatus(ts('Unable to remove file %1', array(1 => $object)) . '<br/>'); } } } } closedir($sourcedir); if ($rmdir) { if (rmdir($target)) { if ($verbose) { CRM_Core_Session::setStatus(ts('Removed directory %1', array(1 => $target)) . '<br/>'); } return TRUE; } else { CRM_Core_Session::setStatus(ts('Unable to remove directory %1', array(1 => $target)) . '<br/>'); } } } }
/** * delete a directory given a path name, delete children directories * and files if needed * * @param string $path the path name * * @return void * @access public * @static */ function cleanDir($target) { if ($sourcedir = @opendir($target)) { while (false !== ($sibling = readdir($sourcedir))) { if (!in_array($sibling, $GLOBALS['_CRM_UTILS_FILE']['exceptions'])) { $object = $target . DIRECTORY_SEPARATOR . $sibling; if (is_dir($object)) { CRM_Utils_File::cleanDir($object); } else { if (is_file($object)) { $result = @unlink($object); } } } } closedir($sourcedir); $result = @rmdir($target); } }
public function installFiles() { $config = CRM_Core_Config::singleton(); $zip = new ZipArchive(); $res = $zip->open($this->tmpFile); if ($res === TRUE) { $zipSubDir = CRM_Utils_Zip::guessBasedir($zip, $this->key); if ($zipSubDir === FALSE) { CRM_Core_Session::setStatus(ts('Unable to extract the extension: bad directory structure') . '<br/>'); return FALSE; } $path = $config->extensionsDir . DIRECTORY_SEPARATOR . 'tmp'; $extractedZipPath = $path . DIRECTORY_SEPARATOR . $zipSubDir; if (is_dir($extractedZipPath)) { if (!CRM_Utils_File::cleanDir($extractedZipPath, TRUE, FALSE)) { CRM_Core_Session::setStatus(ts('Unable to extract the extension: %1 cannot be cleared', array(1 => $extractedZipPath)) . '<br/>'); return FALSE; } } if (!$zip->extractTo($path)) { CRM_Core_Session::setStatus(ts('Unable to extract the extension to %1.', array(1 => $path)) . '<br/>'); return FALSE; } $zip->close(); } else { CRM_Core_Session::setStatus('Unable to extract the extension.'); return FALSE; } $filename = $extractedZipPath . DIRECTORY_SEPARATOR . 'info.xml'; if (!is_readable($filename)) { CRM_Core_Session::setStatus(ts('Failed reading data from %1 during installation', array(1 => $filename)) . '<br/>'); return FALSE; } $newxml = file_get_contents($filename); if (empty($newxml)) { CRM_Core_Session::setStatus(ts('Failed reading data from %1 during installation', array(1 => $filename)) . '<br/>'); return FALSE; } $check = new CRM_Core_Extensions_Extension($this->key . ".newversion"); $check->readXMLInfo($newxml); if ($check->version != $this->version) { CRM_Core_Error::fatal('Cannot install - there are differences between extdir XML file and archive XML file!'); } // Why is this a copy instead of a move? CRM_Utils_File::copyDir($extractedZipPath, $config->extensionsDir . DIRECTORY_SEPARATOR . $this->key); if (!CRM_Utils_File::cleanDir($extractedZipPath, TRUE, FALSE)) { CRM_Core_Session::setStatus(ts('Failed to clean temp dir: %1', array(1 => $extractedZipPath)) . '<br/>'); } return TRUE; }
/** * delete the web server writable directories * * @param int $value 1 - clean templates_c, 2 - clean upload, 3 - clean both * * @access public * * @return void */ public function cleanup($value, $rmdir = TRUE) { $value = (int) $value; if ($value & 1) { // clean templates_c CRM_Utils_File::cleanDir($this->templateCompileDir, $rmdir); CRM_Utils_File::createDir($this->templateCompileDir); } if ($value & 2) { // clean upload dir CRM_Utils_File::cleanDir($this->uploadDir); CRM_Utils_File::createDir($this->uploadDir); CRM_Utils_File::restrictAccess($this->uploadDir); } }
/** * Build The Bar Gharph image with given params * and store in upload/pChart directory. * * @param array $params an assoc array of name/value pairs * @return array $filesPath created image files Path. * * @static */ static function barGraph($params, $divisionWidth = 44) { if (empty($params)) { return; } //get the required directory path. $config =& CRM_Core_Config::singleton(); //get the default currency. $currency = $config->defaultCurrency; $pChartPath = str_replace('templates', 'packages', $config->templateDir); $pChartPath .= 'pChart/Fonts/'; $uploadDirURL = str_replace('persist/contribute/', 'upload/pChart/', $config->imageUploadURL); $uploadDirPath = $config->uploadDir . 'pChart/'; //create pchart directory, if exist clean and then create again. if (is_dir($uploadDirPath)) { CRM_Utils_File::cleanDir($uploadDirPath); CRM_Utils_File::createDir($uploadDirPath); } else { CRM_Utils_File::createDir($uploadDirPath); } require_once 'packages/pChart/pData.class.php'; require_once 'packages/pChart/pChart.class.php'; $chartCount = 0; $filesValues = array(); foreach ($params as $chartIndex => $chartValues) { $chartCount++; $shades = 0; $names = $values = array(); foreach ($chartValues['values'] as $indexName => $indexValue) { $names[] = $indexName; $values[] = $indexValue; $shades++; } $legend = CRM_Utils_Array::value('legend', $chartValues); $xname = CRM_Utils_Array::value('xname', $chartValues); $yname = CRM_Utils_Array::value('yname', $chartValues); //calculate max scale for graph. $maxScale = ceil(max($values) * 1.1); $fontSize = 8; $angleOfIncline = 45; $monetaryformatting = true; require_once 'CRM/Utils/Money.php'; $formatedMoney = CRM_Utils_Money::format(max($values)); $positions = imageftbbox($fontSize, 0, $pChartPath . "tahoma.ttf", $formatedMoney); $scaleTextWidth = $positions[2] - $positions[0]; //need to increase Ysize if we incline money value. $increaseYBy = 0; $inclinePositions = imageftbbox($fontSize, $angleOfIncline, $pChartPath . "tahoma.ttf", $formatedMoney); $inclineTextWidth = $inclinePositions[2] - $inclinePositions[0]; if ($inclineTextWidth > $divisionWidth) { $increaseYBy = $inclineTextWidth / 2; } //Initialise the co-ordinates. $xComponent = 20; $yComponent = 35; $ySize = 300; //calculate coords. $x1 = $xComponent + $scaleTextWidth; $y1 = $yComponent + $increaseYBy; $ySize += $increaseYBy; $y2 = $ySize - $yComponent; //calculate x axis size as per number of months. $x2 = $xComponent + $divisionWidth + $scaleTextWidth + (count($chartValues['values']) - 1) * $divisionWidth; $xSize = $x2 + $xComponent; $dataSet = new pData(); $dataSet->AddPoint($values, "Serie1"); $dataSet->AddPoint($names, "Serie2"); $dataSet->AddSerie("Serie1"); $dataSet->SetAbsciseLabelSerie("Serie2"); //Initialise the graph $chart = new pChart($xSize, $ySize); $chart->setFontProperties($pChartPath . "tahoma.ttf", $fontSize); $chart->setGraphArea($x1, $y1, $x2, $y2); //set the y axis scale. $chart->setFixedScale(0, $maxScale, 1); $chart->drawFilledRoundedRectangle(0, 0, $xSize, $ySize, 5, 240, 240, 240); $chart->drawRoundedRectangle(0, 0, $xSize, $ySize, 5, 230, 230, 230); $chart->drawGraphArea(255, 255, 255, TRUE); $chart->drawScale($dataSet->GetData(), $dataSet->GetDataDescription(), SCALE_NORMAL, 150, 150, 150, TRUE, 0, 2, TRUE, 1, FALSE, $divisionWidth, $monetaryformatting); $chart->drawGrid(4, TRUE, 230, 230, 230, 50); //set colors. $chart->setColorShades($shades, self::$_colors); //Draw the bar chart $chart->drawBarGraph($dataSet->GetData(), $dataSet->GetDataDescription(), TRUE, 80, true); //get the series values and write at top. $chart->setColorPalette(0, 0, 0, 255); $dataDesc = $dataSet->GetDataDescription(); $chart->writeValues($dataSet->GetData(), $dataSet->GetDataDescription(), $dataDesc['Values'], $monetaryformatting, $angleOfIncline); //Write the title if ($legend) { $chart->setFontProperties($pChartPath . "tahoma.ttf", 10); $chart->drawTitle(10, 20, $legend, 50, 50, 50); } if ($xname) { $chart->setFontProperties($pChartPath . "tahoma.ttf", 8); $chart->drawTitle(0, 90, $xname, 2, 0, 2); } if ($yname) { $chart->setFontProperties($pChartPath . "tahoma.ttf", 8); $chart->drawTitle(40, 290, $yname, 2, 0, 20); } $fileName = "pChartByMonth{$chartCount}" . time() . '.png'; $chart->Render($uploadDirPath . $fileName); //get the file path. $filesValues[$chartIndex]['file_name'] = $uploadDirURL . $fileName; //get the co-ordinates $coords = $chart->coordinates(); //format the coordinates to make graph clickable. $position = 0; $chartCoords = array(); foreach ($chartValues['values'] as $name => $value) { $chartCoords[$name] = implode(',', array($coords['xCoords'][$position], $coords['yCoords'][$position], $coords['xCoords'][$position] + $divisionWidth / 2, $y2)); $position++; } $filesValues[$chartIndex]['coords'] = $chartCoords; //free the chart and data objects. unset($chart); unset($dataSet); } return $filesValues; }