$row) { $line = ''; foreach($row as $value) { $line .= '"'.str_replace('"','""',$value).'";'; } fwrite($handle, $line."\n"); } fclose($handle); DocumentManager :: file_send_for_download($file, true, $filename.'.csv'); exit; } /** * Export tabular data to XLS-file * @param array $data * @param string $filename */ function export_table_xls($data, $filename = 'export') { $file = api_get_path(SYS_ARCHIVE_PATH).'/'.uniqid('').'.xls'; $handle = fopen($file, 'a+'); foreach ($data as $index => $row) { fwrite($handle, implode("\t", $row)."\n"); } fclose($handle); DocumentManager :: file_send_for_download($file, true, $filename.'.xls'); exit; } /** * Export tabular data to XML-file * @param array $data * @param string $filename */ function export_table_xml($data, $filename = 'export', $item_tagname = 'item', $wrapper_tagname = null) { $file = api_get_path(SYS_ARCHIVE_PATH).'/'.uniqid('').'.xml'; $handle = fopen($file, 'a+'); fwrite($handle, ''."\n"); if (!is_null($wrapper_tagname)) { fwrite($handle, "\t".'<'.$wrapper_tagname.'>'."\n"); } foreach ($data as $index => $row) { fwrite($handle, '<'.$item_tagname.'>'."\n"); foreach ($row as $key => $value) { fwrite($handle, "\t\t".'<'.$key.'>'.$value.''."\n"); } fwrite($handle, "\t".''."\n"); } if (!is_null($wrapper_tagname)) { fwrite($handle, ''."\n"); } fclose($handle); DocumentManager :: file_send_for_download($file, true, $filename.'.xml'); exit; } } /* ============================================================================== FUNCTIONS ============================================================================== */ /** * Backup a db to a file * * @param ressource $link lien vers la base de donnees * @param string $db_name nom de la base de donnees * @param boolean $structure true => sauvegarde de la structure des tables * @param boolean $donnees true => sauvegarde des donnes des tables * @param boolean $format format des donnees 'INSERT' => des clauses SQL INSERT 'CSV' => donnees separees par des virgules * @param boolean $insertComplet true => clause INSERT avec nom des champs * @param boolean $verbose true => comment are printed * @deprecated Function only used in deprecated function makeTheBackup(...) */ function backupDatabase($link, $db_name, $structure, $donnees, $format = 'SQL', $whereSave = '.', $insertComplet = '', $verbose = false) { $errorCode = ""; if (!is_resource($link)) { GLOBAL $error_msg, $error_no; $error_msg["backup"][] = "[".basename(__FILE__)."][".__LINE__."] link is not a ressource"; $error_no["backup"][] = "1"; return false; } mysql_select_db($db_name); $format = strtolower($format); $filename = $whereSave."/courseDbContent.".$format; $format = strtoupper($format); $fp = fopen($filename, "w"); if (!is_resource($fp)) return false; // liste des tables $res = mysql_list_tables($db_name, $link); $num_rows = mysql_num_rows($res); $i = 0; while ($i < $num_rows) { $tablename = mysql_tablename($res, $i); if ($format == "PHP") fwrite($fp, "\nmysql_query(\""); if ($format == "HTML") fwrite($fp, "\n

$tablename

"); if ($verbose) echo "[".$tablename."] "; if ($structure === true) { if ($format == "PHP" || $format == "SQL") fwrite($fp, "DROP TABLE IF EXISTS `$tablename`;"); if ($format == "PHP") fwrite($fp, "\");\n"); if ($format == "PHP") fwrite($fp, "\nmysql_query(\""); // requete de creation de la table $query = "SHOW CREATE TABLE `".$tablename."`"; $resCreate = mysql_query($query); $row = mysql_fetch_array($resCreate); $schema = $row[1].";"; if ($format == "PHP" || $format == "SQL") fwrite($fp, "$schema"); if ($format == "PHP") fwrite($fp, "\");\n\n"); } if ($donnees === true) { // les données de la table $query = "SELECT * FROM $tablename"; $resData = mysql_query($query); if (mysql_num_rows($resData) > 0) { $sFieldnames = ""; if ($insertComplet === true) { $num_fields = mysql_num_fields($resData); for ($j = 0; $j < $num_fields; $j ++) { $sFieldnames .= "`".mysql_field_name($resData, $j)."`, "; } $sFieldnames = "(".substr($sFieldnames, 0, -2).")"; } $sInsert = "INSERT INTO `$tablename` $sFieldnames values "; while ($rowdata = mysql_fetch_assoc($resData)) { if ($format == "HTML") { $lesDonnees = "\n\t\n\t\t\n\t\t"; } if ($format == "SQL" || $format == "PHP") { $lesDonnees = "".implode(",", $rowdata).""; $lesDonnees = str_replace("", "'", addslashes($lesDonnees)); if ($format == "SQL") { $lesDonnees = $sInsert." ( ".$lesDonnees." );"; } if ($format == "PHP") fwrite($fp, "\nmysql_query(\""); } fwrite($fp, "$lesDonnees"); if ($format == "PHP") fwrite($fp, "\");\n"); } } } $i ++; if ($format == "HTML") fwrite($fp, "\n
".implode("\n\t\t", $rowdata)."\n\t\t
\n
\n"); } echo "fin du backup au format :".$format; fclose($fp); } /** * @deprecated use function copyDirTo($origDirPath, $destination) in * fileManagerLib.inc.php */ function copydir($origine, $destination, $verbose = false) { $dossier = @ opendir($origine) or die("
impossible d'ouvrir ".$origine." [".__LINE__."]"); if ($verbose) echo "
$origine -> $destination"; /* if (file_exists($destination)) { echo "la cible existe, ca ne va pas être possible"; return 0; } */ mkpath($destination, 0770); if ($verbose) echo " [".basename($destination)."]
    "; $total = 0; while ($fichier = readdir($dossier)) { $l = array ('.', '..'); if (!in_array($fichier, $l)) { if (is_dir($origine."/".$fichier)) { if ($verbose) echo "
  1. "; $total += copydir("$origine/$fichier", "$destination/$fichier", $verbose); } else { copy("$origine/$fichier", "$destination/$fichier"); if ($verbose) echo "
  2. $fichier"; $total ++; } if ($verbose) echo "
  3. "; } } if ($verbose) echo "
"; return $total; } /** * Export a course to a zip file * * @param integer $currentCourseID needed sysId Of course to be exported * @param boolean $verbose_backup def FALSE echo step of work * @param string $ignore def NONE // future param for selected bloc to export. * @param string $formats def ALL ALL,SQL,PHP,XML,CSV,XLS,HTML * * @deprecated Function not in use (old backup system) * * 1° Check if all data needed are aivailable * 2° Build the archive repository tree * 3° Build exported element and Fill the archive repository tree * 4° Compress the tree == tree structure == == here we can found == /archivePath/ temporary files of export for the current claroline /$exportedCourseId temporary files of export for the current course /$dateBackuping/ root of the future archive archive.ini course properties readme.txt /originalDocs /html /sql /csv /xml /php ; about "ignore" As we don't know what is add in course by the local admin of claroline, I prefer follow the logic : save all except ... */ function makeTheBackup($exportedCourseId, $verbose_backup = FALSE, $ignore = "", $formats = "ALL") { global $error_msg, $error_no, $db, $archiveRepositorySys, $archiveRepositoryWeb, $appendCourse, $appendMainDb, $archiveName, $_configuration, $_course, $TABLEUSER, $TABLECOURSUSER, $TABLECOURS, $TABLEANNOUNCEMENT; // ****** 1° 2. params. $errorCode = 0; $stop = FALSE; // ****** 1° 2. 1 params.needed if (!isset ($exportedCourseId)) { $error_msg["backup"][] = "[".basename(__FILE__)."][".__LINE__."] Course Id Missing"; $error_no["backup"][] = "1"; $stop = TRUE; } if (!isset ($_configuration['main_database'])) { $error_msg["backup"][] = "[".basename(__FILE__)."][".__LINE__."] Main Db name is Missing"; $error_no["backup"][] = "2"; $stop = TRUE; } if (!isset ($archiveRepositorySys)) { $error_msg["backup"][] = "[".basename(__FILE__)."][".__LINE__."] archive Path not found"; $error_no["backup"][] = "3"; $stop = TRUE; } if (!isset ($appendMainDb)) { $error_msg["backup"][] = "[".basename(__FILE__)."][".__LINE__."] where place course datas from main db in archive"; $error_no["backup"][] = "4"; $stop = TRUE; } if (!isset ($appendCourse)) { $error_msg["backup"][] = "[".basename(__FILE__)."][".__LINE__."] where place course datas in archive"; $error_no["backup"][] = "5"; $stop = TRUE; } if (!isset ($TABLECOURS)) { $error_msg["backup"][] = "[".basename(__FILE__)."][".__LINE__."] name of table of course not defined"; $error_no["backup"][] = "6"; $stop = TRUE; } if (!isset ($TABLEUSER)) { $error_msg["backup"][] = "[".basename(__FILE__)."][".__LINE__."] name of table of users not defined"; $error_no["backup"][] = "7"; $stop = TRUE; } if (!isset ($TABLECOURSUSER)) { $error_msg["backup"][] = "[".basename(__FILE__)."][".__LINE__."] name of table of subscription of users in courses not defined"; $error_no["backup"][] = "8"; $stop = TRUE; } if ($stop) { return false; } // ****** 1° 2. 2 params.optional if (!isset ($verbose_backup)) { $verbose_backup = false; } // ****** 1° 3. check if course exist // not done ////////////////////////////////////////////// // ****** 2° Build the archive repository tree // ****** 2° 1. fix names $shortDateBackuping = date("YzBs"); // YEAR - Day in Year - Swatch - second $archiveFileName = "archive.".$exportedCourseId.".".$shortDateBackuping.".zip"; $dateBackuping = $shortDateBackuping; $archiveDir .= $archiveRepositorySys.$exportedCourseId."/".$shortDateBackuping."/"; $archiveDirOriginalDocs = $archiveDir."originalDocs/"; $archiveDirHtml = $archiveDir."HTML/"; $archiveDirCsv = $archiveDir."CSV/"; $archiveDirXml = $archiveDir."XML/"; $archiveDirPhp = $archiveDir."PHP/"; $archiveDirLog = $archiveDir."LOG/"; $archiveDirSql = $archiveDir."SQL/"; $systemFileNameOfArchive = "claroBak-".$exportedCourseId."-".$dateBackuping.".txt"; $systemFileNameOfArchiveIni = "archive.ini"; $systemFileNameOfReadMe = "readme.txt"; $systemFileNameOfarchiveLog = "readme.txt"; ################### if ($verbose_backup) { echo "
", get_lang('ArchiveName'), " : ", "", basename($systemFileNameOfArchive), "
", get_lang('ArchiveLocation'), " : ", "", realpath($systemFileNameOfArchive), "
", get_lang('SizeOf'), " ", realpath("../../".$exportedCourseId."/"), " : ", "", DirSize("../../".$exportedCourseId."/"), " bytes
"; if (function_exists(diskfreespace)) echo "".get_lang('DiskFreeSpace')." : ".diskfreespace("/")." bytes"; echo "
"; } mkpath($archiveDirOriginalDocs.$appendMainDb, $verbose_backup); mkpath($archiveDirHtml.$appendMainDb, $verbose_backup); mkpath($archiveDirCsv.$appendMainDb, $verbose_backup); mkpath($archiveDirXml.$appendMainDb, $verbose_backup); mkpath($archiveDirPhp.$appendMainDb, $verbose_backup); mkpath($archiveDirLog.$appendMainDb, $verbose_backup); mkpath($archiveDirSql.$appendMainDb, $verbose_backup); mkpath($archiveDirOriginalDocs.$appendCourse, $verbose_backup); mkpath($archiveDirHtml.$appendCourse, $verbose_backup); mkpath($archiveDirCsv.$appendCourse, $verbose_backup); mkpath($archiveDirXml.$appendCourse, $verbose_backup); mkpath($archiveDirPhp.$appendCourse, $verbose_backup); mkpath($archiveDirLog.$appendCourse, $verbose_backup); mkpath($archiveDirSql.$appendCourse, $verbose_backup); $dirCourBase = $archiveDirSqlCourse; $dirMainBase = $archiveDirSqlMainDb; ///////////////////////////////////////////////////////////////////////// // ****** 3° Build exported element and Fill the archive repository tree if ($verbose_backup) echo " build config file
"; // ******************************************************************** // build config file // ******************************************************************** $stringConfig = " ".get_lang('BUCourseDataOfMainBase')." ".$exportedCourseId."
";
	$sqlInsertCourse = "
		INSERT INTO course SET ";
	$csvInsertCourse = "\n";
	$iniCourse = "[".$exportedCourseId."]\n";
	$sqlSelectInfoCourse = "Select * from `".$TABLECOURS."` `course` where code = '".$exportedCourseId."' ";
	$resInfoCourse = api_sql_query($sqlSelectInfoCourse, __FILE__, __LINE__);
	$infoCourse = mysql_fetch_array($resInfoCourse);
	for ($noField = 0; $noField < mysql_num_fields($resInfoCourse); $noField ++)
	{
		if ($noField > 0)
			$sqlInsertCourse .= ", ";
		$nameField = mysql_field_name($resInfoCourse, $noField);
		/*echo "
		
$nameField -> ".$infoCourse["$nameField"]." "; */ $sqlInsertCourse .= "$nameField = '".$infoCourse["$nameField"]."'"; $csvInsertCourse .= "'".addslashes($infoCourse["$nameField"])."';"; } // buildTheIniFile $iniCourse .= "name=".strtr($infoCourse['title'], "()", "[]")."\n"."official_code=".strtr($infoCourse['visual_code'], "()", "[]")."\n".// use in echo "adminCode=".strtr($infoCourse['code'], "()", "[]")."\n".// use as key in db "path=".strtr($infoCourse['code'], "()", "[]")."\n".// use as key in path "dbName=".strtr($infoCourse['code'], "()", "[]")."\n".// use as key in db list "titular=".strtr($infoCourse['titulaire'], "()", "[]")."\n"."language=".strtr($infoCourse['language'], "()", "[]")."\n"."extLinkUrl=".strtr($infoCourse['departementUrl'], "()", "[]")."\n"."extLinkName=".strtr($infoCourse['departementName'], "()", "[]")."\n"."categoryCode=".strtr($infoCourse['faCode'], "()", "[]")."\n"."categoryName=".strtr($infoCourse['faName'], "()", "[]")."\n"."visibility=". ($infoCourse['visibility'] == 2 || $infoCourse['visibility'] == 3)."registrationAllowed=". ($infoCourse['visibility'] == 1 || $infoCourse['visibility'] == 2); $sqlInsertCourse .= ";"; // echo $csvInsertCourse."
"; $stringConfig .= " # Insert Course #------------------------ # ".$sqlInsertCourse." #------------------------ "; if ($verbose_backup) { echo "
"; } $fcoursql = fopen($archiveDirSql.$appendMainDb."course.sql", "w"); fwrite($fcoursql, $sqlInsertCourse); fclose($fcoursql); $fcourcsv = fopen($archiveDirCsv.$appendMainDb."course.csv", "w"); fwrite($fcourcsv, $csvInsertCourse); fclose($fcourcsv); $fcourini = fopen($archiveDir.$systemFileNameOfArchiveIni, "w"); fwrite($fcourini, $iniCourse); fclose($fcourini); echo $iniCourse, " ini Course"; // ******************************************************************** // info about users // ******************************************************************** // if ($backupUser ) { if ($verbose_backup) echo "
  • ".get_lang('BUUsersInMainBase')." ".$exportedCourseId."
    ";
    		// recup users
    		$sqlUserOfTheCourse = "
    					SELECT
    						`user`.*
    						FROM `".$TABLEUSER."`, `".$TABLECOURSUSER."`
    						WHERE `user`.`user_id`=`".$TABLECOURSUSER."`.`user_id`
    							AND `".$TABLECOURSUSER."`.`course_code`='".$exportedCourseId."'";
    		$resUsers = api_sql_query($sqlUserOfTheCourse, __FILE__, __LINE__);
    		$nbUsers = mysql_num_rows($resUsers);
    		if ($nbUsers > 0)
    		{
    			$nbFields = mysql_num_fields($resUsers);
    			$sqlInsertUsers = "";
    			$csvInsertUsers = "";
    			$htmlInsertUsers = "\t\n";
    			//
    			// creation of headers
    			//
    			for ($noField = 0; $noField < $nbFields; $noField ++)
    			{
    				$nameField = mysql_field_name($resUsers, $noField);
    				$csvInsertUsers .= "'".addslashes($nameField)."';";
    				$htmlInsertUsers .= "\t\t\n";
    			}
    			$htmlInsertUsers .= "\t\n";
    			//
    			// creation of body
    			//
    			while ($users = mysql_fetch_array($resUsers))
    			{
    				$htmlInsertUsers .= "\t\n";
    				$sqlInsertUsers .= "
    										INSERT IGNORE INTO user SET ";
    				$csvInsertUsers .= "\n";
    				for ($noField = 0; $noField < $nbFields; $noField ++)
    				{
    					if ($noField > 0)
    						$sqlInsertUsers .= ", ";
    					$nameField = mysql_field_name($resUsers, $noField);
    					/*echo "
    						
    $nameField -> ".$users["$nameField"]." "; */ $sqlInsertUsers .= "$nameField = '".$users["$nameField"]."' "; $csvInsertUsers .= "'".addslashes($users["$nameField"])."';"; $htmlInsertUsers .= "\t\t\n"; } $sqlInsertUsers .= ";"; $htmlInsertUsers .= "\t\n"; } $htmlInsertUsers .= "
    ".$nameField."
    ".$users["$nameField"]."
    \n"; $stringConfig .= " # INSERT Users #------------------------------------------ # ".$sqlInsertUsers." #------------------------------------------ "; $fuserssql = fopen($archiveDirSql.$appendMainDb."users.sql", "w"); fwrite($fuserssql, $sqlInsertUsers); fclose($fuserssql); $fuserscsv = fopen($archiveDirCsv.$appendMainDb."users.csv", "w"); fwrite($fuserscsv, $csvInsertUsers); fclose($fuserscsv); $fusershtml = fopen($archiveDirHtml.$appendMainDb."users.html", "w"); fwrite($fusershtml, $htmlInsertUsers); fclose($fusershtml); } else { if ($verbose_backup) { echo "
    NO user in this course !!!!

    "; } } if ($verbose_backup) { echo "
    "; } } /* End of backup user */ if ($saveAnnouncement) { // ******************************************************************** // info about announcment // ******************************************************************** if ($verbose_backup) { echo "
  • ".get_lang('BUAnnounceInMainBase')." ".$exportedCourseId."
    ";
    		}
    		// recup annonce
    		$sqlAnnounceOfTheCourse = "
    				SELECT
    					*
    					FROM  `".$TABLEANNOUNCEMENT."`
    					WHERE course_code='".$exportedCourseId."'";
    		$resAnn = api_sql_query($sqlAnnounceOfTheCourse, __FILE__, __LINE__);
    		$nbFields = mysql_num_fields($resAnn);
    		$sqlInsertAnn = "";
    		$csvInsertAnn = "";
    		$htmlInsertAnn .= "\t\n";
    		//
    		// creation of headers
    		//
    		for ($noField = 0; $noField < $nbFields; $noField ++)
    		{
    			$nameField = mysql_field_name($resUsers, $noField);
    			$csvInsertAnn .= "'".addslashes($nameField)."';";
    			$htmlInsertAnn .= "\t\t\n";
    		}
    		$htmlInsertAnn .= "\t\n";
    		//
    		// creation of body
    		//
    		while ($announce = mysql_fetch_array($resAnn))
    		{
    			$htmlInsertAnn .= "\t\n";
    			$sqlInsertAnn .= "
    						INSERT INTO users SET ";
    			$csvInsertAnn .= "\n";
    			for ($noField = 0; $noField < $nbFields; $noField ++)
    			{
    				if ($noField > 0)
    					$sqlInsertAnn .= ", ";
    				$nameField = mysql_field_name($resAnn, $noField);
    				/*echo "
    				
    $nameField -> ".$users["$nameField"]." "; */ $sqlInsertAnn .= "$nameField = '".addslashes($announce["$nameField"])."' "; $csvInsertAnn .= "'".addslashes($announce["$nameField"])."';"; $htmlInsertAnn .= "\t\t\n"; } $sqlInsertAnn .= ";"; $htmlInsertAnn .= "\t\n"; } if ($verbose_backup) { echo ""; } $htmlInsertAnn .= "
    ".$nameField."
    ".$announce["$nameField"]."
    \n"; $stringConfig .= " #INSERT ANNOUNCE #------------------------------------------ # ".$sqlInsertAnn." #------------------------------------------ "; $fannsql = fopen($archiveDirSql.$appendMainDb."annonces.sql", "w"); fwrite($fannsql, $sqlInsertAnn); fclose($fannsql); $fanncsv = fopen($archiveDirCsv.$appendMainDb."annnonces.csv", "w"); fwrite($fanncsv, $csvInsertAnn); fclose($fanncsv); $fannhtml = fopen($archiveDirHtml.$appendMainDb."annonces.html", "w"); fwrite($fannhtml, $htmlInsertAnn); fclose($fannhtml); /* End of backup Annonces */ } // we can copy file of course if ($verbose_backup) { echo '
  • '.get_lang('CopyDirectoryCourse'); } $nbFiles = copydir(api_get_path(SYS_COURSE_PATH).$_course['path'], $archiveDirOriginalDocs.$appendCourse, $verbose_backup); if ($verbose_backup) echo " ".$nbFiles." ".get_lang('FileCopied')."
  • "; $stringConfig .= " // ".$nbFiles." was in ".realpath($archiveDirOriginalDocs); // ******************************************************************** // Copy of DB course // with mysqldump // ******************************************************************** if ($verbose_backup) echo "
  • ".get_lang('BackupOfDataBase')." ".$exportedCourseId." (SQL)
    "; backupDatabase($db, $exportedCourseId, true, true, 'SQL', $archiveDirSql.$appendCourse, true, $verbose_backup); if ($verbose_backup) echo "
  • ".get_lang('BackupOfDataBase')." ".$exportedCourseId." (PHP)
    "; backupDatabase($db, $exportedCourseId, true, true, 'PHP', $archiveDirPhp.$appendCourse, true, $verbose_backup); if ($verbose_backup) echo "
  • ".get_lang('BackupOfDataBase')." ".$exportedCourseId." (CSV)
    "; backupDatabase($db, $exportedCourseId, true, true, 'CSV', $archiveDirCsv.$appendCourse, true, $verbose_backup); if ($verbose_backup) echo "
  • ".get_lang('BackupOfDataBase')." ".$exportedCourseId." (HTML)
    "; backupDatabase($db, $exportedCourseId, true, true, 'HTML', $archiveDirHtml.$appendCourse, true, $verbose_backup); if ($verbose_backup) echo "
  • ".get_lang('BackupOfDataBase')." ".$exportedCourseId." (XML)
    "; backupDatabase($db, $exportedCourseId, true, true, 'XML', $archiveDirXml.$appendCourse, true, $verbose_backup); if ($verbose_backup) echo "
  • ".get_lang('BackupOfDataBase')." ".$exportedCourseId." (LOG)
    "; backupDatabase($db, $exportedCourseId, true, true, 'LOG', $archiveDirLog.$appendCourse, true, $verbose_backup); // ******************************************************************** // Copy of DB course // with mysqldump // ******************************************************************** $fdesc = fopen($archiveDir.$systemFileNameOfArchive, "w"); fwrite($fdesc, $stringConfig); fclose($fdesc); if ($verbose_backup) echo "

  • "; /////////////////////////////////// // ****** 4° Compress the tree if (extension_loaded("zlib")) { $whatZip[] = $archiveRepositorySys.$exportedCourseId."/".$shortDateBackuping."/HTML"; $forgetPath = $archiveRepositorySys.$exportedCourseId."/".$shortDateBackuping."/"; $prefixPath = $exportedCourseId; $zipCourse = new PclZip($archiveRepositorySys.$archiveFileName); $zipRes = $zipCourse->create($whatZip, PCLZIP_OPT_ADD_PATH, $prefixPath, PCLZIP_OPT_REMOVE_PATH, $forgetPath); if ($zipRes == 0) { echo "", $zipCourse->errorInfo(true), ""; } else for ($i = 0; $i < sizeof($zipRes); $i ++) { for (reset($zipRes[$i]); $key = key($zipRes[$i]); next($zipRes[$i])) { echo "File $i / [$key] = ".$list[$i][$key]."
    "; } echo "
    "; } $pathToArchive = $archiveRepositoryWeb.$archiveFileName; if ($verbose_backup) { echo '
    '.get_lang('BuildTheCompressedFile'); } // removeDir($archivePath); } return 1; } // function makeTheBackup() ?>