1 <?php
  2   3   4   5   6   7   8 
  9 
 10 defined('JPATH_PLATFORM') or die;
 11 
 12 jimport('joomla.filesystem.folder');
 13 
 14  15  16  17  18 
 19 class JInstallerAdapterLibrary extends JInstallerAdapter
 20 {
 21      22  23  24  25  26  27  28 
 29     protected function checkExtensionInFilesystem()
 30     {
 31         if ($this->currentExtensionId)
 32         {
 33             
 34             if ($this->parent->isOverwrite() || $this->parent->isUpgrade())
 35             {
 36                 
 37                 $installer = new JInstaller; 
 38                 $installer->setPackageUninstall(true);
 39                 $installer->uninstall('library', $this->currentExtensionId);
 40 
 41                 
 42                 $this->currentExtensionId = null;
 43                 $this->extension = JTable::getInstance('Extension', 'JTable', array('dbo' => $this->db));
 44 
 45                 
 46                 $this->setRoute('update');
 47             }
 48             else
 49             {
 50                 
 51                 throw new RuntimeException(JText::_('JLIB_INSTALLER_ABORT_LIB_INSTALL_ALREADY_INSTALLED'));
 52             }
 53         }
 54     }
 55 
 56      57  58  59  60  61  62  63 
 64     protected function copyBaseFiles()
 65     {
 66         if ($this->parent->parseFiles($this->getManifest()->files, -1) === false)
 67         {
 68             throw new RuntimeException(JText::_('JLIB_INSTALLER_ABORT_LIB_COPY_FILES'));
 69         }
 70     }
 71 
 72      73  74  75  76  77  78  79 
 80     protected function finaliseInstall()
 81     {
 82         
 83         
 84         $update = JTable::getInstance('update');
 85         $uid = $update->find(
 86             array(
 87                 'element' => $this->element,
 88                 'type' => $this->type,
 89             )
 90         );
 91 
 92         if ($uid)
 93         {
 94             $update->delete($uid);
 95         }
 96 
 97         
 98         if ($this->route !== 'discover_install')
 99         {
100             $manifest = array();
101             $manifest['src'] = $this->parent->getPath('manifest');
102             $manifest['dest'] = JPATH_MANIFESTS . '/libraries/' . basename($this->parent->getPath('manifest'));
103 
104             if (!$this->parent->copyFiles(array($manifest), true))
105             {
106                 
107                 throw new RuntimeException(JText::_('JLIB_INSTALLER_ABORT_LIB_INSTALL_COPY_SETUP'));
108             }
109 
110             
111             if ($this->manifest_script)
112             {
113                 $path['src'] = $this->parent->getPath('source') . '/' . $this->manifest_script;
114                 $path['dest'] = $this->parent->getPath('extension_root') . '/' . $this->manifest_script;
115 
116                 if ($this->parent->isOverwrite() || !file_exists($path['dest']))
117                 {
118                     if (!$this->parent->copyFiles(array($path)))
119                     {
120                         
121                         throw new RuntimeException(
122                             JText::sprintf(
123                                 'JLIB_INSTALLER_ABORT_MANIFEST',
124                                 JText::_('JLIB_INSTALLER_' . strtoupper($this->route))
125                             )
126                         );
127                     }
128                 }
129             }
130         }
131     }
132 
133     134 135 136 137 138 139 140 141 
142     public function getElement($element = null)
143     {
144         if (!$element)
145         {
146             $manifestPath = JPath::clean($this->parent->getPath('manifest'));
147             $element = preg_replace('/\.xml/', '', basename($manifestPath));
148         }
149 
150         return $element;
151     }
152 
153     154 155 156 157 158 159 160 161 
162     public function loadLanguage($path = null)
163     {
164         $source = $this->parent->getPath('source');
165 
166         if (!$source)
167         {
168             $this->parent->setPath('source', JPATH_PLATFORM . '/' . $this->getElement());
169         }
170 
171         $extension = 'lib_' . $this->getElement();
172         $librarypath = (string) $this->getManifest()->libraryname;
173         $source = $path ?: JPATH_PLATFORM . '/' . $librarypath;
174 
175         $this->doLoadLanguage($extension, $source, JPATH_SITE);
176     }
177 
178     179 180 181 182 183 184 
185     protected function parseOptionalTags()
186     {
187         $this->parent->parseLanguages($this->getManifest()->languages);
188         $this->parent->parseMedia($this->getManifest()->media);
189     }
190 
191     192 193 194 195 196 197 
198     public function prepareDiscoverInstall()
199     {
200         $manifestPath = JPATH_MANIFESTS . '/libraries/' . $this->extension->element . '.xml';
201         $this->parent->manifest = $this->parent->isManifest($manifestPath);
202         $this->parent->setPath('manifest', $manifestPath);
203         $this->setManifest($this->parent->getManifest());
204     }
205 
206     207 208 209 210 211 212 213 
214     protected function setupInstallPaths()
215     {
216         $group = (string) $this->getManifest()->libraryname;
217 
218         if (!$group)
219         {
220             throw new RuntimeException(JText::_('JLIB_INSTALLER_ABORT_LIB_INSTALL_NOFILE'));
221         }
222 
223         $this->parent->setPath('extension_root', JPATH_PLATFORM . '/' . implode(DIRECTORY_SEPARATOR, explode('/', $group)));
224     }
225 
226     227 228 229 230 231 232 233 
234     protected function storeExtension()
235     {
236         
237         if ($this->route === 'discover_install')
238         {
239             $manifest_details = JInstaller::parseXMLInstallFile($this->parent->getPath('manifest'));
240 
241             $this->extension->manifest_cache = json_encode($manifest_details);
242             $this->extension->state = 0;
243             $this->extension->name = $manifest_details['name'];
244             $this->extension->enabled = 1;
245             $this->extension->params = $this->parent->getParams();
246 
247             if (!$this->extension->store())
248             {
249                 
250                 throw new RuntimeException(JText::_('JLIB_INSTALLER_ERROR_LIB_DISCOVER_STORE_DETAILS'));
251             }
252 
253             return;
254         }
255 
256         $this->extension->name = $this->name;
257         $this->extension->type = 'library';
258         $this->extension->element = $this->element;
259 
260         
261         $this->extension->folder = '';
262         $this->extension->enabled = 1;
263         $this->extension->protected = 0;
264         $this->extension->access = 1;
265         $this->extension->client_id = 0;
266         $this->extension->params = $this->parent->getParams();
267 
268         
269         $this->extension->custom_data = '';
270         $this->extension->system_data = '';
271 
272         
273         $this->extension->manifest_cache = $this->parent->generateManifestCache();
274 
275         if (!$this->extension->store())
276         {
277             
278             throw new RuntimeException(
279                 JText::sprintf(
280                     'JLIB_INSTALLER_ABORT_LIB_INSTALL_ROLLBACK',
281                     $this->extension->getError()
282                 )
283             );
284         }
285 
286         
287         
288         $this->parent->pushStep(array('type' => 'extension', 'id' => $this->extension->extension_id));
289     }
290 
291     292 293 294 295 296 297 
298     public function update()
299     {
300         
301         
302         $this->setManifest($this->parent->getManifest());
303 
304         
305         $this->parent->setOverwrite(true);
306         $this->parent->setUpgrade(true);
307 
308         
309         $this->setRoute('update');
310 
311         312 313 314 315 
316 
317         
318         $name = (string) $this->getManifest()->name;
319         $name = JFilterInput::getInstance()->clean($name, 'string');
320         $element = str_replace('.xml', '', basename($this->parent->getPath('manifest')));
321         $this->set('name', $name);
322         $this->set('element', $element);
323 
324         
325         $installer = new JInstaller;
326         $db = $this->parent->getDbo();
327         $query = $db->getQuery(true)
328             ->select($db->quoteName('extension_id'))
329             ->from($db->quoteName('#__extensions'))
330             ->where($db->quoteName('type') . ' = ' . $db->quote('library'))
331             ->where($db->quoteName('element') . ' = ' . $db->quote($element));
332         $db->setQuery($query);
333         $result = $db->loadResult();
334 
335         if ($result)
336         {
337             
338             $installer->setPackageUninstall(true);
339             $installer->uninstall('library', $result);
340 
341             
342             $this->currentExtensionId = null;
343             $this->extension = JTable::getInstance('Extension', 'JTable', array('dbo' => $this->db));
344         }
345 
346         
347         return $this->install();
348     }
349 
350     351 352 353 354 355 356 357 358 
359     public function uninstall($id)
360     {
361         $retval = true;
362 
363         
364         
365         $row = JTable::getInstance('extension');
366 
367         if (!$row->load((int) $id) || $row->element === '')
368         {
369             JLog::add(JText::_('ERRORUNKOWNEXTENSION'), JLog::WARNING, 'jerror');
370 
371             return false;
372         }
373 
374         
375         
376         if ($row->protected)
377         {
378             JLog::add(JText::_('JLIB_INSTALLER_ERROR_LIB_UNINSTALL_WARNCORELIBRARY'), JLog::WARNING, 'jerror');
379 
380             return false;
381         }
382 
383         384 385 386 
387         if ($row->package_id && !$this->parent->isPackageUninstall() && !$this->canUninstallPackageChild($row->package_id))
388         {
389             JLog::add(JText::sprintf('JLIB_INSTALLER_ERROR_CANNOT_UNINSTALL_CHILD_OF_PACKAGE', $row->name), JLog::WARNING, 'jerror');
390 
391             return false;
392         }
393 
394         $manifestFile = JPATH_MANIFESTS . '/libraries/' . $row->element . '.xml';
395 
396         
397         if (file_exists($manifestFile))
398         {
399             $manifest = new JInstallerManifestLibrary($manifestFile);
400 
401             
402             $this->parent->setPath('extension_root', JPATH_PLATFORM . '/' . $manifest->libraryname);
403 
404             $xml = simplexml_load_file($manifestFile);
405 
406             
407             if (!$xml)
408             {
409                 JLog::add(JText::_('JLIB_INSTALLER_ERROR_LIB_UNINSTALL_LOAD_MANIFEST'), JLog::WARNING, 'jerror');
410 
411                 return false;
412             }
413 
414             
415             if ($xml->getName() !== 'extension')
416             {
417                 JLog::add(JText::_('JLIB_INSTALLER_ERROR_LIB_UNINSTALL_INVALID_MANIFEST'), JLog::WARNING, 'jerror');
418 
419                 return false;
420             }
421 
422             $this->parent->removeFiles($xml->files, -1);
423             JFile::delete($manifestFile);
424         }
425         else
426         {
427             
428             $row->delete($row->extension_id);
429             unset($row);
430             JLog::add(JText::_('JLIB_INSTALLER_ERROR_LIB_UNINSTALL_INVALID_NOTFOUND_MANIFEST'), JLog::WARNING, 'jerror');
431 
432             return false;
433         }
434 
435         
436         
437         if (JFolder::exists($this->parent->getPath('extension_root')))
438         {
439             if (is_dir($this->parent->getPath('extension_root')))
440             {
441                 $files = JFolder::files($this->parent->getPath('extension_root'));
442 
443                 if (!count($files))
444                 {
445                     JFolder::delete($this->parent->getPath('extension_root'));
446                 }
447             }
448         }
449 
450         $this->parent->removeFiles($xml->media);
451         $this->parent->removeFiles($xml->languages);
452 
453         $row->delete($row->extension_id);
454         unset($row);
455 
456         return $retval;
457     }
458 
459     460 461 462 463 464 465 
466     public function discover()
467     {
468         $results = array();
469         $file_list = JFolder::files(JPATH_MANIFESTS . '/libraries', '\.xml$');
470 
471         foreach ($file_list as $file)
472         {
473             $manifest_details = JInstaller::parseXMLInstallFile(JPATH_MANIFESTS . '/libraries/' . $file);
474             $file = JFile::stripExt($file);
475             $extension = JTable::getInstance('extension');
476             $extension->set('type', 'library');
477             $extension->set('client_id', 0);
478             $extension->set('element', $file);
479             $extension->set('folder', '');
480             $extension->set('name', $file);
481             $extension->set('state', -1);
482             $extension->set('manifest_cache', json_encode($manifest_details));
483             $extension->set('params', '{}');
484             $results[] = $extension;
485         }
486 
487         return $results;
488     }
489 
490     491 492 493 494 495 496 
497     public function refreshManifestCache()
498     {
499         
500         $manifestPath = JPATH_MANIFESTS . '/libraries/' . $this->parent->extension->element . '.xml';
501         $this->parent->manifest = $this->parent->isManifest($manifestPath);
502         $this->parent->setPath('manifest', $manifestPath);
503 
504         $manifest_details = JInstaller::parseXMLInstallFile($this->parent->getPath('manifest'));
505         $this->parent->extension->manifest_cache = json_encode($manifest_details);
506         $this->parent->extension->name = $manifest_details['name'];
507 
508         try
509         {
510             return $this->parent->extension->store();
511         }
512         catch (RuntimeException $e)
513         {
514             JLog::add(JText::_('JLIB_INSTALLER_ERROR_LIB_REFRESH_MANIFEST_CACHE'), JLog::WARNING, 'jerror');
515 
516             return false;
517         }
518     }
519 }
520 
521 522 523 524 525 526 527 
528 class JInstallerLibrary extends JInstallerAdapterLibrary
529 {
530 }
531