1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | namespace Aurora\Modules\OEmbedFiles; |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | class Module extends \Aurora\System\Module\AbstractModule |
23: | { |
24: | protected $aProviders = array(); |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | public function init() |
33: | { |
34: | $this->loadProviders(); |
35: | |
36: | $this->subscribeEvent('Files::GetLinkType', array($this, 'onGetLinkType')); |
37: | $this->subscribeEvent('Files::CheckUrl', array($this, 'onCheckUrl')); |
38: | $this->subscribeEvent('Files::PopulateFileItem::after', array($this, 'onAfterPopulateFileItem')); |
39: | } |
40: | |
41: | |
42: | |
43: | |
44: | public static function getInstance() |
45: | { |
46: | return parent::getInstance(); |
47: | } |
48: | |
49: | |
50: | |
51: | |
52: | public static function Decorator() |
53: | { |
54: | return parent::Decorator(); |
55: | } |
56: | |
57: | |
58: | |
59: | |
60: | public function getModuleSettings() |
61: | { |
62: | return $this->oModuleSettings; |
63: | } |
64: | |
65: | |
66: | |
67: | |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | public function onGetLinkType($Link, &$Result) |
74: | { |
75: | $Result = !!($this->getOembedFileInfo($Link)); |
76: | return $Result; |
77: | } |
78: | |
79: | |
80: | |
81: | |
82: | |
83: | |
84: | |
85: | |
86: | public function onCheckUrl($aArgs, &$mResult) |
87: | { |
88: | $iUserId = \Aurora\System\Api::getAuthenticatedUserId(); |
89: | |
90: | if ($iUserId) { |
91: | if (!empty($aArgs['Url'])) { |
92: | $oInfo = $this->getOembedFileInfo($aArgs['Url']); |
93: | if ($oInfo) { |
94: | $mResult['Size'] = isset($oInfo->fileSize) ? $oInfo->fileSize : ''; |
95: | $mResult['Name'] = isset($oInfo->title) ? $oInfo->title : ''; |
96: | $mResult['LinkType'] = 'oembeded'; |
97: | $mResult['Thumb'] = isset($oInfo->thumbnailUrl) ? $oInfo->thumbnailUrl : null; |
98: | } |
99: | } |
100: | } |
101: | } |
102: | |
103: | |
104: | |
105: | |
106: | |
107: | |
108: | |
109: | |
110: | public function onAfterPopulateFileItem($aArgs, &$oItem) |
111: | { |
112: | $bBreak = false; |
113: | if ($oItem->IsLink) { |
114: | $Result = $this->getOembedFileInfo($oItem->LinkUrl); |
115: | |
116: | if ($Result) { |
117: | $oItem->LinkType = 'oembeded'; |
118: | $oItem->Name = isset($Result->title) ? $Result->title : $oItem->Name; |
119: | $oItem->Size = isset($Result->fileSize) ? $Result->fileSize : $oItem->Size; |
120: | $oItem->OembedHtml = isset($Result->html) ? $Result->html : $oItem->OembedHtml; |
121: | $oItem->Thumb = true; |
122: | $oItem->ThumbnailUrl = $Result->thumbnailUrl; |
123: | $oItem->IsExternal = true; |
124: | } |
125: | $bBreak = !!$Result; |
126: | } |
127: | return $bBreak; |
128: | } |
129: | |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | protected function getOembedFileInfo($sUrl) |
137: | { |
138: | $mResult = false; |
139: | $sOembedUrl = ''; |
140: | |
141: | foreach ($this->aProviders as $aProvider) { |
142: | if (\preg_match("/" . $aProvider['patterns'] . "/", $sUrl)) { |
143: | $sOembedUrl = $aProvider['url'] . $sUrl; |
144: | break; |
145: | } |
146: | } |
147: | |
148: | if (false !== \strpos($sUrl, 'instagram.com')) { |
149: | $sUrl = \str_replace('instagram.com', 'instagr.am', $sUrl); |
150: | $sOembedUrl = 'https://api.instagram.com/oembed?url=' . $sUrl; |
151: | } |
152: | |
153: | if (\strlen($sOembedUrl) > 0) { |
154: | $oCurl = \curl_init(); |
155: | \curl_setopt_array($oCurl, array( |
156: | CURLOPT_URL => $sOembedUrl, |
157: | CURLOPT_HEADER => 0, |
158: | CURLOPT_RETURNTRANSFER => true, |
159: | CURLOPT_FOLLOWLOCATION => true, |
160: | CURLOPT_ENCODING => '', |
161: | CURLOPT_AUTOREFERER => true, |
162: | CURLOPT_SSL_VERIFYPEER => false, |
163: | CURLOPT_CONNECTTIMEOUT => 5, |
164: | CURLOPT_TIMEOUT => 5, |
165: | CURLOPT_MAXREDIRS => 5 |
166: | )); |
167: | $sResult = \curl_exec($oCurl); |
168: | \curl_close($oCurl); |
169: | $oResult = \json_decode($sResult); |
170: | |
171: | if ($oResult) { |
172: | $sSearch = $oResult->html; |
173: | $aPatterns = array('/ width="\d+."/', '/ height="\d+."/', '/(src="[^\"]+)/'); |
174: | $aResults = array(' width="896"', ' height="504"', '$1?&autoplay=1&auto_play=true'); |
175: | $oResult->html = \preg_replace($aPatterns, $aResults, $sSearch); |
176: | |
177: | $aRemoteFileInfo = \Aurora\System\Utils::GetRemoteFileInfo($sUrl); |
178: | $oResult->fileSize = $aRemoteFileInfo['size']; |
179: | |
180: | $oResult->thumbnailUrl = isset($oResult->thumbnail_url) ? $oResult->thumbnail_url : ''; |
181: | |
182: | $mResult = new \Aurora\Modules\OEmbedFiles\Classes\FileInfo(); |
183: | $mResult->html = $oResult->html; |
184: | $mResult->fileSize = $oResult->fileSize; |
185: | $mResult->thumbnailUrl = $oResult->thumbnailUrl; |
186: | } |
187: | } |
188: | |
189: | return $mResult; |
190: | } |
191: | |
192: | |
193: | |
194: | |
195: | protected function loadProviders() |
196: | { |
197: | $sFile = __DIR__ . DIRECTORY_SEPARATOR . 'providers.json'; |
198: | if (\file_exists($sFile)) { |
199: | $sJsonData = \file_get_contents($sFile); |
200: | $aJsonData = \json_decode($sJsonData, true); |
201: | foreach ($aJsonData as $aProvider) { |
202: | $this->aProviders[$aProvider['title']] = array( |
203: | 'patterns' => $aProvider['url_re'], |
204: | 'url' => $aProvider['endpoint_url'] |
205: | ); |
206: | } |
207: | } |
208: | } |
209: | |
210: | } |
211: | |