26
26
use Pydio \Core \Controller \ProgressBarCLI ;
27
27
use Pydio \Core \Services \RolesService ;
28
28
use Pydio \Core \Services \UsersService ;
29
+ use Pydio \Core \Utils \FileHelper ;
29
30
use Pydio \Core \Utils \Vars \InputFilter ;
30
31
use Pydio \Core \Utils \Vars \StringHelper ;
31
32
@@ -343,7 +344,7 @@ public function getUserEntries($login = null, $countOnly = false, $offset = -1,
343
344
//Update progress bar in CLI mode
344
345
$ isListAll = (($ offset == -1 ) && ($ limit == -1 ) && (is_null ($ login )) && $ regexpOnSearchAttr && (php_sapi_name () == "cli " ));
345
346
if ($ isListAll ) {
346
- $ total = $ this ->getCountFromCache ();
347
+ $ total = $ this ->getCountFromCache (" / " );
347
348
$ progressBar = new ProgressBarCLI ();
348
349
$ progressBar ->init ($ index , $ total ["count " ], "Get ldap users " );
349
350
}
@@ -460,10 +461,17 @@ public function listUsersPaginated($baseGroup, $regexp, $offset, $limit, $recurs
460
461
return $ persons ;
461
462
}
462
463
464
+ /**
465
+ * @param string $baseGroup
466
+ * @param string $regexp
467
+ * @param null $filterProperty
468
+ * @param null $filterValue
469
+ * @param bool $recursive
470
+ * @return mixed
471
+ */
463
472
public function getUsersCount ($ baseGroup = "/ " , $ regexp = "" , $ filterProperty = null , $ filterValue = null , $ recursive = true )
464
473
{
465
- $ check_cache = $ this ->getCountFromCache ();
466
-
474
+ $ check_cache = $ this ->getCountFromCache ($ baseGroup );
467
475
if ((is_array ($ check_cache ) && $ check_cache ["count " ] > 0 )) {
468
476
return $ check_cache ["count " ];
469
477
}
@@ -477,7 +485,7 @@ public function getUsersCount($baseGroup = "/", $regexp = "", $filterProperty =
477
485
}
478
486
479
487
$ res = $ this ->getUserEntries (StringHelper::regexpToLdap ($ regexp ), true , null );
480
- $ this ->saveCountToCache ($ res );
488
+ $ this ->saveCountToCache ($ res, $ baseGroup );
481
489
$ this ->dynamicFilter = null ;
482
490
return $ res ["count " ];
483
491
}
@@ -988,30 +996,50 @@ public function fakeMemberOf($conn, $groupDN, $filterString, $atts, &$entry)
988
996
}
989
997
}
990
998
991
- public function getCountFromCache ()
999
+ /**
1000
+ * @return string
1001
+ * @throws \Exception
1002
+ */
1003
+ private function getCacheCountFileName (){
1004
+ return $ this ->getPluginCacheDir () . DIRECTORY_SEPARATOR . "ldap.ser " ;
1005
+ }
1006
+
1007
+ /**
1008
+ * @param $baseGroup
1009
+ * @return int
1010
+ */
1011
+ public function getCountFromCache ($ baseGroup )
992
1012
{
993
1013
$ ttl = $ this ->getOption ("LDAP_COUNT_CACHE_TTL " );
994
1014
if (empty ($ ttl )) $ ttl = 1 ;
995
- $ fileName = "ldap.ser " ;
996
- if (file_exists ($ this ->getPluginCacheDir () . DIRECTORY_SEPARATOR . $ fileName )) {
997
- $ fileContent = unserialize (file_get_contents ($ this ->getPluginCacheDir () . DIRECTORY_SEPARATOR . $ fileName ));
998
- if (($ fileContent ) && ($ fileContent ["count " ]) && ($ fileContent ["timestamp " ]) && ((time () - $ fileContent ["timestamp " ]) < 60 * 60 * $ ttl )) {
999
- return $ fileContent ;
1000
- }
1015
+ $ fileContent = FileHelper::loadSerialFile ($ this ->getCacheCountFileName ());
1016
+ if (!empty ($ fileContent ) && $ fileContent [$ baseGroup ]["count " ] && $ fileContent [$ baseGroup ]["timestamp " ] && (time () - $ fileContent [$ baseGroup ]["timestamp " ]) < 60 * 60 * $ ttl ) {
1017
+ return $ fileContent [$ baseGroup ];
1001
1018
}
1002
1019
return 0 ;
1003
1020
}
1004
1021
1005
- public function saveCountToCache ($ fileContent )
1022
+ /**
1023
+ * @param $fileContent
1024
+ * @param $baseGroup
1025
+ * @throws \Exception
1026
+ */
1027
+ public function saveCountToCache ($ fileContent , $ baseGroup )
1006
1028
{
1007
- $ fileName = "ldap.ser " ;
1008
1029
if (!is_dir ($ this ->getPluginCacheDir (false , true ))) return ;
1009
- if (is_array ($ fileContent ) && ($ fileContent > 0 )) {
1030
+ $ fileName = $ this ->getCacheCountFileName ();
1031
+
1032
+ $ existing = FileHelper::loadSerialFile ($ fileName );
1033
+ if (!empty ($ existing ) && is_array ($ existing )) {
1034
+ $ data = $ existing ;
1035
+ }else {
1036
+ $ data = [];
1037
+ }
1038
+
1039
+ if (is_array ($ fileContent ) && count ($ fileContent ) > 0 ) {
1010
1040
$ fileContent ["timestamp " ] = time ();
1011
- if (file_exists ($ this ->getPluginCacheDir () . DIRECTORY_SEPARATOR . $ fileName )) {
1012
- unlink ($ this ->getPluginCacheDir () . "/ " . $ fileName );
1013
- }
1014
- file_put_contents ($ this ->getPluginCacheDir () . DIRECTORY_SEPARATOR . $ fileName , serialize ($ fileContent ));
1041
+ $ data [$ baseGroup ] = $ fileContent ;
1042
+ FileHelper::saveSerialFile ($ fileName , $ data , false );
1015
1043
}
1016
1044
}
1017
1045
0 commit comments