Skip to content

Commit

Permalink
xerte/new_template.php: remove nasty loop_create_folder and replace i…
Browse files Browse the repository at this point in the history
…t with copy_r which seems vaguely readable; add debugging stuff; change to new db_query_one

git-svn-id: https://xerteonlinetoolkits.googlecode.com/svn/trunk@107 912cdd6b-5c7d-d5a7-a2ba-d0f0cdb91641
  • Loading branch information
Dave Goodwin committed Sep 28, 2011
1 parent b527ddf commit 43136cd
Showing 1 changed file with 86 additions and 156 deletions.
242 changes: 86 additions & 156 deletions modules/xerte/new_template.php
Expand Up @@ -12,163 +12,93 @@
$temp_dir_path="";
$temp_new_path="";

/**
*
* Function create folder loop
* This function creates folders needed when creating a template
* @param string $foldername - the path to this folder
* @param number $looplevel - a number to make sure that we enter and leave each folder correctly
* @version 1.0
* @author Patrick Lockley
*/


function create_folder_loop($folder_name,$loop_level){

global $dir_path, $new_path, $temp_dir_path, $temp_new_path;

while($f = readdir($folder_name)){

$full = $dir_path . "/" . $f;

if(is_dir($full)){

if(($f==".")||($f=="..")){

}else{

$new_folder = opendir($full);
$temp_dir_path = $dir_path;
$temp_new_path = $new_path;
$new_path = $new_path . "/" . $f;
$dir_path=$full;

if(@mkdir($new_path)){
if(@chmod($new_path, 0777)){

create_folder_loop($new_folder,++$loop_level);

}else{

receive_message($_SESSION['toolkits_logon_username'], "FILE_SYSTEM", "MAJOR", "Failed to set permissions on folder", "Failed to set correct rights on " . $new_path);

return false;
}
}else{

receive_message($_SESSION['toolkits_logon_username'], "FILE_SYSTEM", "CRITICIAL", "Failed to create folder", "Failed to create folder " . $new_path);

return false;

}


}

}else{

$file_dest_path = $new_path . "/" . $f;
if(@copy($full, $file_dest_path)){
if(@chmod($file_dest_path, 0777)){


}else{

receive_message($_SESSION['toolkits_logon_username'], "FILE_SYSTEM", "CRITICAL", "Failed to copy file", "Failed to copy file " . $full . " " . $file_dest_path);
return false;

}
}else{

receive_message($_SESSION['toolkits_logon_username'], "FILE_SYSTEM", "MAJOR", "Failed to set rights on file", "Failed to set rights on file " . $full . " " . $file_dest_path);

return false;

}

}
}

$dir_path = $temp_dir_path;
$new_path = $temp_new_path;

/**
* loop level is used to check for the recusion to make sure it has worked ok. A failure in this is not critical but is used in error reporting
*/

$loop_level--;

if($loop_level==-1){
return true;
}

// taken from php.net/manual/en/function.copy.php

define('DS', DIRECTORY_SEPARATOR); // I always use this short form in my code.
function copy_r( $path, $dest )
{
if(preg_match('/\.svn/', $path)) {
_Debug("Skipping .svn dir ($path)");
return true;
}
_debug("Copying $path to $dest, recursively... ");

if( is_dir($path) )
{
@mkdir( $dest );
$objects = scandir($path);
if( sizeof($objects) > 0 )
{
foreach( $objects as $file )
{
if( $file == "." || $file == ".." )
continue;
// go on
if( is_dir( $path.DS.$file ) )
{
copy_r( $path.DS.$file, $dest.DS.$file );
}
else
{
copy( $path.DS.$file, $dest.DS.$file );
}
}
}
return true;
}
elseif( is_file($path) )
{
return copy($path, $dest);
}
else
{
return false;
}
}

/**
*
* Function sort out paramaters
* This function creates folders needed when duplicating a template
* @param number $folder_name_id - the id of this template
* @param number $tutorial_id_from_post - the parent template name for the new tutorial
* @version 1.0
* @author Patrick Lockley
*/


function create_new_template($folder_name_id,$tutorial_id_from_post){

global $dir_path, $new_path, $temp_dir_path, $temp_new_path, $xerte_toolkits_site;

$database_id=database_connect("file_library database connect success","file_library database connect fail");

$query_for_framework = "select template_framework from " . $xerte_toolkits_site->database_table_prefix . "originaltemplatesdetails where template_name =\"" . $tutorial_id_from_post . "\"";

$query_for_framework_response = mysql_query($query_for_framework);

$row_framework = mysql_fetch_array($query_for_framework_response);

$dir_path = $xerte_toolkits_site->basic_template_path . $row_framework['template_framework'] . "/templates/" . $tutorial_id_from_post;

/**
* Get the id of the folder we are looking to copy into
*/

$new_path = $xerte_toolkits_site->users_file_area_full . $folder_name_id . "-" . $_SESSION['toolkits_logon_username'] . "-" . $tutorial_id_from_post;

$path = $xerte_toolkits_site->users_file_area_full . $folder_name_id . "-" . $_SESSION['toolkits_logon_username'] . "-" . $tutorial_id_from_post;

if(mkdir($path)){

if(@chmod($path,0777)){

$d = opendir($dir_path);

if(create_folder_loop($d,-1)){

return true;

}else{

return false;

}

}else{

receive_message($_SESSION['toolkits_logon_username'], "FILE_SYSTEM", "MAJOR", "Failed to set rights on parent folder for template", "Failed to set rights on parent folder " . $path);

return false;


}
}else{

receive_message($_SESSION['toolkits_logon_username'], "FILE_SYSTEM", "CRITICAL", "Failed to create parent folder for template", "Failed to create parent folder " . $path);

return false;

}

*
* Function sort out paramaters
* This function creates folders needed when duplicating a template
* @param number $folder_name_id - the id of this template
* @param number $tutorial_id_from_post - the parent template name for the new tutorial
* @version 1.0
* @author Patrick Lockley
*/


function create_new_template($folder_name_id,$parent_template_name){

global $dir_path, $new_path, $temp_dir_path, $temp_new_path, $xerte_toolkits_site;


$row_framework = db_query_one("SELECT template_framework from {$xerte_toolkits_site->database_table_prefix}originaltemplatesdetails WHERE template_name = ?", array($parent_template_name));


// I think this is wrong, currently looking like : /home/david/src/xerteonlinetoolkits/modules//templates/0 should presumably be home/david/src/xerteonlinetoolkits/modules/xerte/templates/Nottingham
$dir_path = $xerte_toolkits_site->basic_template_path . $row_framework['template_framework'] . "/templates/" . $parent_template_name;

/**
* Get the id of the folder we are looking to copy into
*/

_debug("Creating new template : $folder_name_id, $parent_template_name");
$new_path = $xerte_toolkits_site->users_file_area_full . $folder_name_id . "-" . $_SESSION['toolkits_logon_username'] . "-" . $parent_template_name;
$path = $xerte_toolkits_site->users_file_area_full . $folder_name_id . "-" . $_SESSION['toolkits_logon_username'] . "-" . $parent_template_name;

if(mkdir($path)){
_debug("Created $path ok");
if(@chmod($path,0777)){
$ok = copy_r($dir_path, $path);
_debug("Copy_r returned " . print_r($ok, true));
return $ok;
}else{
_debug("Failed to set rights ");
receive_message($_SESSION['toolkits_logon_username'], "FILE_SYSTEM", "MAJOR", "Failed to set rights on parent folder for template", "Failed to set rights on parent folder " . $path);
return false;
}
}else{
receive_message($_SESSION['toolkits_logon_username'], "FILE_SYSTEM", "CRITICAL", "Failed to create parent folder for template", "Failed to create parent folder " . $path);
return false;
}
}

?>

0 comments on commit 43136cd

Please sign in to comment.