Failed Conditions
Pull Request — master (#6)
by Sander
02:02
created

Evernote   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 17
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
D checkEvernote() 0 45 17
1
<?php
2
/**
3
 * Nextcloud - namespace OCA\Nextnote
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
6
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\OwnNote\Utility;
25
26
27
use DOMDocument;
28
use OC\Files\Filesystem;
29
30
class Evernote {
31
32
	/**
33
	 * @param $folder
34
	 * @param string $file
35
	 */
36
	public static function checkEvernote($folder, $file) {
37
		$utils = new Utils();
38
		if ($html = Filesystem::file_get_contents($folder . "/" . $file)) {
39
			$DOM = new DOMDocument;
40
			$DOM->loadHTML($html);
41
			$items = $DOM->getElementsByTagName('meta');
42
			$isEvernote = false;
43
			for ($i = 0; $i < $items->length; $i++) {
44
				$item = $items->item($i);
45
				if ($item->hasAttributes()) {
46
					$attrs = $item->attributes;
47
					foreach ($attrs as $a => $attr) {
48
						if ($attr->name == "name") {
49
							if ($attr->value == "exporter-version" || $attr->value == "Generator") {
50
								$isEvernote = true;
51
								continue;
52
							}
53
						}
54
					}
55
				}
56
			}
57
			if ($isEvernote) {
58
				$items = $DOM->getElementsByTagName('img');
59
				for ($i = 0; $i < $items->length; $i++) {
60
					$item = $items->item($i);
61
					if ($item->hasAttributes()) {
62
						$attrs = $item->attributes;
63
						foreach ($attrs as $a => $attr) {
64
							if ($attr->name == "src") {
65
								$url = $attr->value;
66
								if (!$utils->startsWith($url, "http") && !$utils->startsWith($url, "/") && !$utils->startsWith($url, "data")) {
67
									if ($data = Filesystem::file_get_contents($folder . "/" . $url)) {
68
										$type = pathinfo($url, PATHINFO_EXTENSION);
69
										$base64 = "data:image/" . $type . ";base64," . base64_encode($data);
70
										$html = str_replace($url, $base64, $html);
71
									}
72
								}
73
							}
74
						}
75
					}
76
				}
77
				Filesystem::file_put_contents($folder . "/" . $file, $html);
78
			}
79
		}
80
	}
81
}