@@ -359,8 +359,12 @@ public static function insertFile($path, $name, $type, $accessGroupId) {
359359 // check if there is an old deletion request for the same filename
360360 $ qF = new QueryFilter (FileDelete::FILENAME , $ name , "= " );
361361 Factory::getFileDeleteFactory ()->massDeletion ([Factory::FILTER => $ qF ]);
362-
363- $ file = new File (null , $ name , Util::filesize ($ path ), 1 , $ fileType , $ accessGroupId );
362+ if ($ fileType == DFileType::RULE ) {
363+ $ file = new File (null , $ name , Util::filesize ($ path ), 1 , $ fileType , $ accessGroupId , Util::rulefileLineCount ($ path ));
364+ }
365+ else {
366+ $ file = new File (null , $ name , Util::filesize ($ path ), 1 , $ fileType , $ accessGroupId , Util::fileLineCount ($ path ));
367+ }
364368 $ file = Factory::getFileFactory ()->save ($ file );
365369 if ($ file == null ) {
366370 return false ;
@@ -647,7 +651,48 @@ public static function filesize($file) {
647651
648652 return $ pos ;
649653 }
654+
655+ /**
656+ * This counts the number of lines in a given file
657+ * @param $file string Filepath you want to get the size from
658+ * @return int -1 if the file doesn't exist, else filesize
659+ */
660+ public static function fileLineCount ($ file ) {
661+ if (!file_exists ($ file )) {
662+ return -1 ;
663+ }
664+ // find out what a prettier solution for this would be, as opposed to setting the max execution time to an arbitrary two hours
665+ ini_set ('max_execution_time ' , '7200 ' );
666+ $ file = new \SplFileObject ($ file , 'r ' );
667+ $ file ->seek (PHP_INT_MAX );
668+
669+ return $ file ->key ();
670+ }
650671
672+ /**
673+ * This counts the number of lines in a rule file, excluding lines starting with # and empty lines
674+ * @param $file string Filepath you want to get the size from
675+ * @return int -1 if the file doesn't exist, else filesize
676+ */
677+ public static function rulefileLineCount ($ file ) {
678+ if (!file_exists ($ file )) {
679+ return -1 ;
680+ }
681+ // find out what a prettier solution for this would be, as opposed to setting the max execution time to an arbitrary two hours
682+ ini_set ('max_execution_time ' , '7200 ' );
683+ $ lineCount = 0 ;
684+ $ handle = fopen ($ file , "r " );
685+ while (!feof ($ handle )){
686+ $ line = fgets ($ handle );
687+ if (!(Util::startsWith ($ line , '# ' ) or trim ($ line ) == "" )) {
688+ $ lineCount = $ lineCount + 1 ;
689+ }
690+ }
691+
692+ fclose ($ handle );
693+ return $ lineCount ;
694+ }
695+
651696 /**
652697 * Refreshes the page with the current url, also includes the query string.
653698 */
@@ -1348,7 +1393,9 @@ public static function arrayOfIds($array) {
13481393 }
13491394 return $ arr ;
13501395 }
1351-
1396+
1397+ // new function added: fileLineCount(). This function is independent of OS.
1398+ // check whether we can remove one of these functions
13521399 public static function countLines ($ tmpfile ) {
13531400 if (stripos (PHP_OS , "WIN " ) === 0 ) {
13541401 // windows line count
0 commit comments