2012-09-07 13:22:37 +02:00
< ? php
2018-10-27 14:43:12 +02:00
/* Copyright ( C ) 2012 Regis Houssin < regis . houssin @ inodbox . com >
2012-09-07 13:22:37 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2012-09-07 13:22:37 +02:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2012-09-07 13:22:37 +02:00
*/
/**
2013-08-22 16:49:23 +02:00
* \file htdocs / ecm / ajax / ecmdatabase . php
2021-10-10 21:11:48 +02:00
* \brief File to build / refresh the ecm database for directories
2012-09-07 13:22:37 +02:00
*/
2021-02-25 22:26:26 +01:00
if ( ! defined ( 'NOTOKENRENEWAL' )) {
define ( 'NOTOKENRENEWAL' , '1' ); // Disables token renewal
}
if ( ! defined ( 'NOREQUIREMENU' )) {
define ( 'NOREQUIREMENU' , '1' );
}
if ( ! defined ( 'NOREQUIREAJAX' )) {
define ( 'NOREQUIREAJAX' , '1' );
}
if ( ! defined ( 'NOREQUIRESOC' )) {
define ( 'NOREQUIRESOC' , '1' );
}
2012-09-07 13:22:37 +02:00
require '../../main.inc.php' ;
2020-04-10 10:59:32 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2012-09-07 13:22:37 +02:00
2020-09-16 19:39:50 +02:00
$action = GETPOST ( 'action' , 'aZ09' );
2012-09-07 13:47:35 +02:00
$element = GETPOST ( 'element' , 'alpha' );
2012-09-07 13:22:37 +02:00
2021-10-10 21:11:48 +02:00
2012-09-07 13:22:37 +02:00
/*
* View
*/
top_httphead ();
2017-06-18 19:42:59 +02:00
//print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
2012-09-07 13:22:37 +02:00
// Load original field value
2021-02-25 22:26:26 +01:00
if ( isset ( $action ) && ! empty ( $action )) {
2020-04-10 10:59:32 +02:00
$error = 0 ;
2012-09-07 13:22:37 +02:00
2021-02-25 22:26:26 +01:00
if ( $action == 'build' && ! empty ( $element )) {
2020-04-10 10:59:32 +02:00
require_once DOL_DOCUMENT_ROOT . '/ecm/class/ecmdirectory.class.php' ;
2012-09-07 13:22:37 +02:00
$ecmdirstatic = new EcmDirectory ( $db );
2013-12-01 17:32:05 +01:00
$ecmdirtmp = new EcmDirectory ( $db );
2012-09-07 13:22:37 +02:00
2013-12-01 17:32:05 +01:00
// This part of code is same than into file index.php for action refreshmanual TODO Remove duplicate
2012-09-07 17:23:16 +02:00
clearstatcache ();
2020-04-10 10:59:32 +02:00
$diroutputslash = str_replace ( '\\' , '/' , $conf -> $element -> dir_output );
$diroutputslash .= '/' ;
2012-09-07 17:23:16 +02:00
// Scan directory tree on disk
2020-04-10 10:59:32 +02:00
$disktree = dol_dir_list ( $conf -> $element -> dir_output , 'directories' , 1 , '' , array ( '^temp$' ), '' , '' , 0 );
2012-09-07 17:23:16 +02:00
// Scan directory tree in database
2020-04-10 10:59:32 +02:00
$sqltree = $ecmdirstatic -> get_full_arbo ( 0 );
2012-09-07 17:23:16 +02:00
2020-04-10 10:59:32 +02:00
$adirwascreated = 0 ;
2012-09-07 17:23:16 +02:00
// Now we compare both trees to complete missing trees into database
//var_dump($disktree);
//var_dump($sqltree);
2021-02-25 22:26:26 +01:00
foreach ( $disktree as $dirdesc ) { // Loop on tree onto disk
2012-09-07 13:22:37 +02:00
set_time_limit ( 0 ); // To force restarts the timeout counter from zero
2012-09-07 17:23:16 +02:00
2020-04-10 10:59:32 +02:00
$dirisindatabase = 0 ;
2021-02-25 22:26:26 +01:00
foreach ( $sqltree as $dirsqldesc ) {
if ( $conf -> $element -> dir_output . '/' . $dirsqldesc [ 'fullrelativename' ] == $dirdesc [ 'fullname' ]) {
2020-04-10 10:59:32 +02:00
$dirisindatabase = 1 ;
2012-09-07 17:23:16 +02:00
break ;
}
}
2021-02-25 22:26:26 +01:00
if ( ! $dirisindatabase ) {
2021-10-10 21:11:48 +02:00
$txt = " Directory found on disk " . $dirdesc [ 'fullname' ] . " , not found into table ecm_directories, so we add it " ;
2012-09-07 17:23:16 +02:00
dol_syslog ( $txt );
// We must first find the fk_parent of directory to create $dirdesc['fullname']
2020-04-10 10:59:32 +02:00
$fk_parent = - 1 ;
$relativepathmissing = str_replace ( $diroutputslash , '' , $dirdesc [ 'fullname' ]);
$relativepathtosearchparent = $relativepathmissing ;
2012-09-07 17:23:16 +02:00
//dol_syslog("Try to find parent id for directory ".$relativepathtosearchparent);
2021-02-25 22:26:26 +01:00
if ( preg_match ( '/\//' , $relativepathtosearchparent )) {
2012-09-07 17:23:16 +02:00
//while (preg_match('/\//',$relativepathtosearchparent))
2020-04-10 10:59:32 +02:00
$relativepathtosearchparent = preg_replace ( '/\/[^\/]*$/' , '' , $relativepathtosearchparent );
$txt = " Is relative parent path " . $relativepathtosearchparent . " for " . $relativepathmissing . " found in sql tree ? " ;
2012-09-07 17:23:16 +02:00
dol_syslog ( $txt );
//print $txt." -> ";
2020-04-10 10:59:32 +02:00
$parentdirisindatabase = 0 ;
2021-02-25 22:26:26 +01:00
foreach ( $sqltree as $dirsqldesc ) {
if ( $dirsqldesc [ 'fullrelativename' ] == $relativepathtosearchparent ) {
2020-04-10 10:59:32 +02:00
$parentdirisindatabase = $dirsqldesc [ 'id' ];
2012-09-07 17:23:16 +02:00
break ;
}
}
2021-02-25 22:26:26 +01:00
if ( $parentdirisindatabase > 0 ) {
2012-09-07 17:23:16 +02:00
dol_syslog ( " Yes with id " . $parentdirisindatabase );
//print "Yes with id ".$parentdirisindatabase."<br>\n";
2020-04-10 10:59:32 +02:00
$fk_parent = $parentdirisindatabase ;
2012-09-07 17:23:16 +02:00
//break; // We found parent, we can stop the while loop
2020-05-21 15:05:19 +02:00
} else {
2012-09-07 17:23:16 +02:00
dol_syslog ( " No " );
//print "No<br>\n";
}
2020-05-21 15:05:19 +02:00
} else {
2012-09-07 17:23:16 +02:00
dol_syslog ( " Parent is root " );
2020-04-10 10:59:32 +02:00
$fk_parent = 0 ; // Parent is root
2012-09-07 17:23:16 +02:00
}
2021-02-25 22:26:26 +01:00
if ( $fk_parent >= 0 ) {
2012-09-07 17:23:16 +02:00
$ecmdirtmp -> ref = 'NOTUSEDYET' ;
$ecmdirtmp -> label = dol_basename ( $dirdesc [ 'fullname' ]);
$ecmdirtmp -> description = '' ;
$ecmdirtmp -> fk_parent = $fk_parent ;
2020-04-10 10:59:32 +02:00
$txt = " We create directory " . $ecmdirtmp -> label . " with parent " . $fk_parent ;
2012-09-07 17:23:16 +02:00
dol_syslog ( $txt );
//print $txt."<br>\n";
$id = $ecmdirtmp -> create ( $user );
2021-02-25 22:26:26 +01:00
if ( $id > 0 ) {
2020-04-10 10:59:32 +02:00
$newdirsql = array ( 'id' => $id ,
2012-09-07 17:23:16 +02:00
'id_mere' => $ecmdirtmp -> fk_parent ,
'label' => $ecmdirtmp -> label ,
'description' => $ecmdirtmp -> description ,
'fullrelativename' => $relativepathmissing );
2020-04-10 10:59:32 +02:00
$sqltree [] = $newdirsql ; // We complete fulltree for following loops
2012-09-07 17:23:16 +02:00
//var_dump($sqltree);
2020-04-10 10:59:32 +02:00
$adirwascreated = 1 ;
2020-05-21 15:05:19 +02:00
} else {
2012-09-07 17:23:16 +02:00
dol_syslog ( " Failed to create directory " . $ecmdirtmp -> label , LOG_ERR );
}
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$txt = " Parent of " . $dirdesc [ 'fullname' ] . " not found " ;
2012-09-07 17:23:16 +02:00
dol_syslog ( $txt );
//print $txt."<br>\n";
}
}
}
2020-03-23 15:54:02 +01:00
// Loop now on each sql tree to check if dir exists
2021-02-25 22:26:26 +01:00
foreach ( $sqltree as $dirdesc ) { // Loop on each sqltree to check dir is on disk
2020-04-10 10:59:32 +02:00
$dirtotest = $conf -> $element -> dir_output . '/' . $dirdesc [ 'fullrelativename' ];
2021-02-25 22:26:26 +01:00
if ( ! dol_is_dir ( $dirtotest )) {
2021-10-10 21:11:48 +02:00
dol_syslog ( $dirtotest . " not found onto disk. We delete from database dir with id= " . $dirdesc [ 'id' ]);
2020-04-10 10:59:32 +02:00
$ecmdirtmp -> id = $dirdesc [ 'id' ];
2019-01-27 11:55:16 +01:00
$ecmdirtmp -> delete ( $user , 'databaseonly' );
2013-12-01 17:32:05 +01:00
//exit;
}
2020-03-23 15:54:02 +01:00
}
2013-12-01 17:32:05 +01:00
2021-10-10 21:11:48 +02:00
dol_syslog ( " Nb of directories added into database = " . $adirwascreated );
$sql = " UPDATE " . MAIN_DB_PREFIX . " ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0 " ; // If pb into cache counting, we set to value -1 = "unknown"
2012-09-07 13:22:37 +02:00
$db -> query ( $sql );
}
}