Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NoteUtils.php « Misc « lib - github.com/CarnetApp/CarnetNextcloud.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9c3b2441c9caa848335efa2d70247a7e12244a75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace OCA\Carnet\Misc;
class NoteUtils{
    public static $defaultCarnetNotePath = "Documents/QuickNote";
    public static function getTextFromHTML($html){ //not optimized at alllll....
        return 
            //remove first and last br
            preg_replace('/^(<br\s*\/?>)*|(<br\s*\/?>)*$/i', '',
            //multiple to single br
            preg_replace('/(<br[^>]*>\s*){2,}/', '<br/>',
            //white spaces
            trim(
            //putting back [linebr] to br
            str_replace('[linebr]', '<br>',
            //removing html tags
            preg_replace('#<[^>]+>#', ' ', 
            //replacing all br by linebr
            preg_replace('/(<br\ ?\/?>)/', '[linebr]', 
            //all div to [linebr]
            preg_replace("/<div>(.*?)<\/div>/", "[linebr]$1",$html)
        ))))));
    }
    public static function getShortText($text){ //not optimized at alllll....
        return mb_substr($text, 0, 150);
    }

    public static function removeAccents($str) {
        $a = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ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', 'ǿ', 'Ά', 'ά', 'Έ', 'έ', 'Ό', 'ό', 'Ώ', 'ώ', 'Ί', 'ί', 'ϊ', 'ΐ', 'Ύ', 'ύ', 'ϋ', 'ΰ', 'Ή', 'ή');
        $b = array('A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o', 'Α', 'α', 'Ε', 'ε', 'Ο', 'ο', 'Ω', 'ω', 'Ι', 'ι', 'ι', 'ι', 'Υ', 'υ', 'υ', 'υ', 'Η', 'η');
        return str_replace($a, $b, $str);
    }

    public function getMetadata($carnetFolder, $path){
        $meta = array();
        $node = $carnetFolder->get($path);
        if($node->getType() === "dir"){
            $meta['lastmodfile'] = $node->getMTime();
            try{
                $meta['metadata'] = json_decode($node->get('metadata.json')->getContent());
            } catch(\OCP\Files\NotFoundException $e){
            
            }
            try{
                $text = self::getTextFromHTML($node->get('index.html')->getContent());

                $meta['shorttext'] = self::getShortText($text);
                $meta['text'] = $text;
                $meta['media'] = array();
                $meta['previews'] = array();

                try{
                    foreach($node->get('data')->getDirectoryListing() as $in){
                        if(substr($in->getName(), 0, strlen("preview")) === "preview"){
                            array_push($meta['previews'],"./note/getmedia?note=".$path."&media=data/".$in->getName());
                        } else  {
                            array_push($meta['media'], "./note/getmedia?note=".$path."&media=data/".$in->getName());
                        }
                        
                    }
                }
                catch(\OCP\Files\NotFoundException$e){
                    
                }
            }
            catch(\OCP\Files\NotFoundException $e){
                    
            }
        }
        else{
            $tmppath = tempnam(sys_get_temp_dir(), uniqid().".zip");
            
            file_put_contents($tmppath, $node->fopen("r"));
                $zipFile = new \PhpZip\ZipFile();
                $zipFile->openFromStream(fopen($tmppath, "r")); //issue with encryption when open directly + unexpectedly faster to copy before Oo'
                $meta['lastmodfile'] = $node->getMTime();
                try{
                    $meta['metadata'] = json_decode($zipFile->getEntryContents("metadata.json"));
                } catch(\PhpZip\Exception\ZipNotFoundEntry $e){
                
                }
                try{
                $text = self::getTextFromHTML($zipFile->getEntryContents("index.html"));
                $meta['shorttext'] = self::getShortText($text);
                $meta['lower_case_text'] = strtolower(self::removeAccents($text));
                $meta['media'] = array();
                $meta['previews'] = array();

                try{
                    foreach($zipFile->getListFiles() as $f){
                        if(substr($f, 0, strlen("data/preview")) === "data/preview"){
                            array_push($meta['previews'],"./note/getmedia?note=".$path."&media=".$f);
                        } else if(substr($f, 0, strlen("data/")) === "data/") {
                            array_push($meta['media'],"./note/getmedia?note=".$path."&media=".$f);
                        }
                        
                    }
                }
                catch(\PhpZip\Exception\ZipNotFoundEntry $e){
                    
                }
            } catch(\PhpZip\Exception\ZipNotFoundEntry $e){
                $meta['shorttext'] = "";
                
            }
            unlink($tmppath);
        }
        return $meta;
        
    }
}
?>