You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1321 lines
40 KiB

  1. <?php
  2. /**
  3. * DokuWiki Plugin extension (Helper Component)
  4. *
  5. * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
  6. * @author Michael Hamann <michael@content-space.de>
  7. */
  8. use dokuwiki\Extension\Plugin;
  9. use dokuwiki\Extension\PluginInterface;
  10. use dokuwiki\Utf8\PhpString;
  11. use splitbrain\PHPArchive\Tar;
  12. use splitbrain\PHPArchive\ArchiveIOException;
  13. use splitbrain\PHPArchive\Zip;
  14. use dokuwiki\HTTP\DokuHTTPClient;
  15. use dokuwiki\Extension\PluginController;
  16. /**
  17. * Class helper_plugin_extension_extension represents a single extension (plugin or template)
  18. */
  19. class helper_plugin_extension_extension extends Plugin
  20. {
  21. private $id;
  22. private $base;
  23. private $is_template = false;
  24. private $localInfo;
  25. private $remoteInfo;
  26. private $managerData;
  27. /** @var helper_plugin_extension_repository $repository */
  28. private $repository;
  29. /** @var array list of temporary directories */
  30. private $temporary = [];
  31. /** @var string where templates are installed to */
  32. private $tpllib = '';
  33. /**
  34. * helper_plugin_extension_extension constructor.
  35. */
  36. public function __construct()
  37. {
  38. $this->tpllib = dirname(tpl_incdir()) . '/';
  39. }
  40. /**
  41. * Destructor
  42. *
  43. * deletes any dangling temporary directories
  44. */
  45. public function __destruct()
  46. {
  47. foreach ($this->temporary as $dir) {
  48. io_rmdir($dir, true);
  49. }
  50. }
  51. /**
  52. * @return bool false, this component is not a singleton
  53. */
  54. public function isSingleton()
  55. {
  56. return false;
  57. }
  58. /**
  59. * Set the name of the extension this instance shall represents, triggers loading the local and remote data
  60. *
  61. * @param string $id The id of the extension (prefixed with template: for templates)
  62. * @return bool If some (local or remote) data was found
  63. */
  64. public function setExtension($id)
  65. {
  66. $id = cleanID($id);
  67. $this->id = $id;
  68. $this->base = $id;
  69. if (str_starts_with($id, 'template:')) {
  70. $this->base = substr($id, 9);
  71. $this->is_template = true;
  72. } else {
  73. $this->is_template = false;
  74. }
  75. $this->localInfo = [];
  76. $this->managerData = [];
  77. $this->remoteInfo = [];
  78. if ($this->isInstalled()) {
  79. $this->readLocalData();
  80. $this->readManagerData();
  81. }
  82. if ($this->repository == null) {
  83. $this->repository = $this->loadHelper('extension_repository');
  84. }
  85. $this->remoteInfo = $this->repository->getData($this->getID());
  86. return ($this->localInfo || $this->remoteInfo);
  87. }
  88. /**
  89. * If the extension is installed locally
  90. *
  91. * @return bool If the extension is installed locally
  92. */
  93. public function isInstalled()
  94. {
  95. return is_dir($this->getInstallDir());
  96. }
  97. /**
  98. * If the extension is under git control
  99. *
  100. * @return bool
  101. */
  102. public function isGitControlled()
  103. {
  104. if (!$this->isInstalled()) return false;
  105. return file_exists($this->getInstallDir() . '/.git');
  106. }
  107. /**
  108. * If the extension is bundled
  109. *
  110. * @return bool If the extension is bundled
  111. */
  112. public function isBundled()
  113. {
  114. if (!empty($this->remoteInfo['bundled'])) return $this->remoteInfo['bundled'];
  115. return in_array(
  116. $this->id,
  117. [
  118. 'authad',
  119. 'authldap',
  120. 'authpdo',
  121. 'authplain',
  122. 'acl',
  123. 'config',
  124. 'extension',
  125. 'info',
  126. 'popularity',
  127. 'revert',
  128. 'safefnrecode',
  129. 'styling',
  130. 'testing',
  131. 'usermanager',
  132. 'logviewer',
  133. 'template:dokuwiki'
  134. ]
  135. );
  136. }
  137. /**
  138. * If the extension is protected against any modification (disable/uninstall)
  139. *
  140. * @return bool if the extension is protected
  141. */
  142. public function isProtected()
  143. {
  144. // never allow deinstalling the current auth plugin:
  145. global $conf;
  146. if ($this->id == $conf['authtype']) return true;
  147. /** @var PluginController $plugin_controller */
  148. global $plugin_controller;
  149. $cascade = $plugin_controller->getCascade();
  150. return (isset($cascade['protected'][$this->id]) && $cascade['protected'][$this->id]);
  151. }
  152. /**
  153. * If the extension is installed in the correct directory
  154. *
  155. * @return bool If the extension is installed in the correct directory
  156. */
  157. public function isInWrongFolder()
  158. {
  159. return $this->base != $this->getBase();
  160. }
  161. /**
  162. * If the extension is enabled
  163. *
  164. * @return bool If the extension is enabled
  165. */
  166. public function isEnabled()
  167. {
  168. global $conf;
  169. if ($this->isTemplate()) {
  170. return ($conf['template'] == $this->getBase());
  171. }
  172. /* @var PluginController $plugin_controller */
  173. global $plugin_controller;
  174. return $plugin_controller->isEnabled($this->base);
  175. }
  176. /**
  177. * If the extension should be updated, i.e. if an updated version is available
  178. *
  179. * @return bool If an update is available
  180. */
  181. public function updateAvailable()
  182. {
  183. if (!$this->isInstalled()) return false;
  184. if ($this->isBundled()) return false;
  185. $lastupdate = $this->getLastUpdate();
  186. if ($lastupdate === false) return false;
  187. $installed = $this->getInstalledVersion();
  188. if ($installed === false || $installed === $this->getLang('unknownversion')) return true;
  189. return $this->getInstalledVersion() < $this->getLastUpdate();
  190. }
  191. /**
  192. * If the extension is a template
  193. *
  194. * @return bool If this extension is a template
  195. */
  196. public function isTemplate()
  197. {
  198. return $this->is_template;
  199. }
  200. /**
  201. * Get the ID of the extension
  202. *
  203. * This is the same as getName() for plugins, for templates it's getName() prefixed with 'template:'
  204. *
  205. * @return string
  206. */
  207. public function getID()
  208. {
  209. return $this->id;
  210. }
  211. /**
  212. * Get the name of the installation directory
  213. *
  214. * @return string The name of the installation directory
  215. */
  216. public function getInstallName()
  217. {
  218. return $this->base;
  219. }
  220. // Data from plugin.info.txt/template.info.txt or the repo when not available locally
  221. /**
  222. * Get the basename of the extension
  223. *
  224. * @return string The basename
  225. */
  226. public function getBase()
  227. {
  228. if (!empty($this->localInfo['base'])) return $this->localInfo['base'];
  229. return $this->base;
  230. }
  231. /**
  232. * Get the display name of the extension
  233. *
  234. * @return string The display name
  235. */
  236. public function getDisplayName()
  237. {
  238. if (!empty($this->localInfo['name'])) return $this->localInfo['name'];
  239. if (!empty($this->remoteInfo['name'])) return $this->remoteInfo['name'];
  240. return $this->base;
  241. }
  242. /**
  243. * Get the author name of the extension
  244. *
  245. * @return string|bool The name of the author or false if there is none
  246. */
  247. public function getAuthor()
  248. {
  249. if (!empty($this->localInfo['author'])) return $this->localInfo['author'];
  250. if (!empty($this->remoteInfo['author'])) return $this->remoteInfo['author'];
  251. return false;
  252. }
  253. /**
  254. * Get the email of the author of the extension if there is any
  255. *
  256. * @return string|bool The email address or false if there is none
  257. */
  258. public function getEmail()
  259. {
  260. // email is only in the local data
  261. if (!empty($this->localInfo['email'])) return $this->localInfo['email'];
  262. return false;
  263. }
  264. /**
  265. * Get the email id, i.e. the md5sum of the email
  266. *
  267. * @return string|bool The md5sum of the email if there is any, false otherwise
  268. */
  269. public function getEmailID()
  270. {
  271. if (!empty($this->remoteInfo['emailid'])) return $this->remoteInfo['emailid'];
  272. if (!empty($this->localInfo['email'])) return md5($this->localInfo['email']);
  273. return false;
  274. }
  275. /**
  276. * Get the description of the extension
  277. *
  278. * @return string The description
  279. */
  280. public function getDescription()
  281. {
  282. if (!empty($this->localInfo['desc'])) return $this->localInfo['desc'];
  283. if (!empty($this->remoteInfo['description'])) return $this->remoteInfo['description'];
  284. return '';
  285. }
  286. /**
  287. * Get the URL of the extension, usually a page on dokuwiki.org
  288. *
  289. * @return string The URL
  290. */
  291. public function getURL()
  292. {
  293. if (!empty($this->localInfo['url'])) return $this->localInfo['url'];
  294. return 'https://www.dokuwiki.org/' .
  295. ($this->isTemplate() ? 'template' : 'plugin') . ':' . $this->getBase();
  296. }
  297. /**
  298. * Get the installed version of the extension
  299. *
  300. * @return string|bool The version, usually in the form yyyy-mm-dd if there is any
  301. */
  302. public function getInstalledVersion()
  303. {
  304. if (!empty($this->localInfo['date'])) return $this->localInfo['date'];
  305. if ($this->isInstalled()) return $this->getLang('unknownversion');
  306. return false;
  307. }
  308. /**
  309. * Get the install date of the current version
  310. *
  311. * @return string|bool The date of the last update or false if not available
  312. */
  313. public function getUpdateDate()
  314. {
  315. if (!empty($this->managerData['updated'])) return $this->managerData['updated'];
  316. return $this->getInstallDate();
  317. }
  318. /**
  319. * Get the date of the installation of the plugin
  320. *
  321. * @return string|bool The date of the installation or false if not available
  322. */
  323. public function getInstallDate()
  324. {
  325. if (!empty($this->managerData['installed'])) return $this->managerData['installed'];
  326. return false;
  327. }
  328. /**
  329. * Get the names of the dependencies of this extension
  330. *
  331. * @return array The base names of the dependencies
  332. */
  333. public function getDependencies()
  334. {
  335. if (!empty($this->remoteInfo['dependencies'])) return $this->remoteInfo['dependencies'];
  336. return [];
  337. }
  338. /**
  339. * Get the names of the missing dependencies
  340. *
  341. * @return array The base names of the missing dependencies
  342. */
  343. public function getMissingDependencies()
  344. {
  345. /* @var PluginController $plugin_controller */
  346. global $plugin_controller;
  347. $dependencies = $this->getDependencies();
  348. $missing_dependencies = [];
  349. foreach ($dependencies as $dependency) {
  350. if (!$plugin_controller->isEnabled($dependency)) {
  351. $missing_dependencies[] = $dependency;
  352. }
  353. }
  354. return $missing_dependencies;
  355. }
  356. /**
  357. * Get the names of all conflicting extensions
  358. *
  359. * @return array The names of the conflicting extensions
  360. */
  361. public function getConflicts()
  362. {
  363. if (!empty($this->remoteInfo['conflicts'])) return $this->remoteInfo['conflicts'];
  364. return [];
  365. }
  366. /**
  367. * Get the names of similar extensions
  368. *
  369. * @return array The names of similar extensions
  370. */
  371. public function getSimilarExtensions()
  372. {
  373. if (!empty($this->remoteInfo['similar'])) return $this->remoteInfo['similar'];
  374. return [];
  375. }
  376. /**
  377. * Get the names of the tags of the extension
  378. *
  379. * @return array The names of the tags of the extension
  380. */
  381. public function getTags()
  382. {
  383. if (!empty($this->remoteInfo['tags'])) return $this->remoteInfo['tags'];
  384. return [];
  385. }
  386. /**
  387. * Get the popularity information as floating point number [0,1]
  388. *
  389. * @return float|bool The popularity information or false if it isn't available
  390. */
  391. public function getPopularity()
  392. {
  393. if (!empty($this->remoteInfo['popularity'])) return $this->remoteInfo['popularity'];
  394. return false;
  395. }
  396. /**
  397. * Get the text of the update message if there is any
  398. *
  399. * @return string|bool The update message if there is any, false otherwise
  400. */
  401. public function getUpdateMessage()
  402. {
  403. if (!empty($this->remoteInfo['updatemessage'])) return $this->remoteInfo['updatemessage'];
  404. return false;
  405. }
  406. /**
  407. * Get the text of the security warning if there is any
  408. *
  409. * @return string|bool The security warning if there is any, false otherwise
  410. */
  411. public function getSecurityWarning()
  412. {
  413. if (!empty($this->remoteInfo['securitywarning'])) return $this->remoteInfo['securitywarning'];
  414. return false;
  415. }
  416. /**
  417. * Get the text of the security issue if there is any
  418. *
  419. * @return string|bool The security issue if there is any, false otherwise
  420. */
  421. public function getSecurityIssue()
  422. {
  423. if (!empty($this->remoteInfo['securityissue'])) return $this->remoteInfo['securityissue'];
  424. return false;
  425. }
  426. /**
  427. * Get the URL of the screenshot of the extension if there is any
  428. *
  429. * @return string|bool The screenshot URL if there is any, false otherwise
  430. */
  431. public function getScreenshotURL()
  432. {
  433. if (!empty($this->remoteInfo['screenshoturl'])) return $this->remoteInfo['screenshoturl'];
  434. return false;
  435. }
  436. /**
  437. * Get the URL of the thumbnail of the extension if there is any
  438. *
  439. * @return string|bool The thumbnail URL if there is any, false otherwise
  440. */
  441. public function getThumbnailURL()
  442. {
  443. if (!empty($this->remoteInfo['thumbnailurl'])) return $this->remoteInfo['thumbnailurl'];
  444. return false;
  445. }
  446. /**
  447. * Get the last used download URL of the extension if there is any
  448. *
  449. * @return string|bool The previously used download URL, false if the extension has been installed manually
  450. */
  451. public function getLastDownloadURL()
  452. {
  453. if (!empty($this->managerData['downloadurl'])) return $this->managerData['downloadurl'];
  454. return false;
  455. }
  456. /**
  457. * Get the download URL of the extension if there is any
  458. *
  459. * @return string|bool The download URL if there is any, false otherwise
  460. */
  461. public function getDownloadURL()
  462. {
  463. if (!empty($this->remoteInfo['downloadurl'])) return $this->remoteInfo['downloadurl'];
  464. return false;
  465. }
  466. /**
  467. * If the download URL has changed since the last download
  468. *
  469. * @return bool If the download URL has changed
  470. */
  471. public function hasDownloadURLChanged()
  472. {
  473. $lasturl = $this->getLastDownloadURL();
  474. $currenturl = $this->getDownloadURL();
  475. return ($lasturl && $currenturl && $lasturl != $currenturl);
  476. }
  477. /**
  478. * Get the bug tracker URL of the extension if there is any
  479. *
  480. * @return string|bool The bug tracker URL if there is any, false otherwise
  481. */
  482. public function getBugtrackerURL()
  483. {
  484. if (!empty($this->remoteInfo['bugtracker'])) return $this->remoteInfo['bugtracker'];
  485. return false;
  486. }
  487. /**
  488. * Get the URL of the source repository if there is any
  489. *
  490. * @return string|bool The URL of the source repository if there is any, false otherwise
  491. */
  492. public function getSourcerepoURL()
  493. {
  494. if (!empty($this->remoteInfo['sourcerepo'])) return $this->remoteInfo['sourcerepo'];
  495. return false;
  496. }
  497. /**
  498. * Get the donation URL of the extension if there is any
  499. *
  500. * @return string|bool The donation URL if there is any, false otherwise
  501. */
  502. public function getDonationURL()
  503. {
  504. if (!empty($this->remoteInfo['donationurl'])) return $this->remoteInfo['donationurl'];
  505. return false;
  506. }
  507. /**
  508. * Get the extension type(s)
  509. *
  510. * @return array The type(s) as array of strings
  511. */
  512. public function getTypes()
  513. {
  514. if (!empty($this->remoteInfo['types'])) return $this->remoteInfo['types'];
  515. if ($this->isTemplate()) return [32 => 'template'];
  516. return [];
  517. }
  518. /**
  519. * Get a list of all DokuWiki versions this extension is compatible with
  520. *
  521. * @return array The versions in the form yyyy-mm-dd => ('label' => label, 'implicit' => implicit)
  522. */
  523. public function getCompatibleVersions()
  524. {
  525. if (!empty($this->remoteInfo['compatible'])) return $this->remoteInfo['compatible'];
  526. return [];
  527. }
  528. /**
  529. * Get the date of the last available update
  530. *
  531. * @return string|bool The last available update in the form yyyy-mm-dd if there is any, false otherwise
  532. */
  533. public function getLastUpdate()
  534. {
  535. if (!empty($this->remoteInfo['lastupdate'])) return $this->remoteInfo['lastupdate'];
  536. return false;
  537. }
  538. /**
  539. * Get the base path of the extension
  540. *
  541. * @return string The base path of the extension
  542. */
  543. public function getInstallDir()
  544. {
  545. if ($this->isTemplate()) {
  546. return $this->tpllib . $this->base;
  547. } else {
  548. return DOKU_PLUGIN . $this->base;
  549. }
  550. }
  551. /**
  552. * The type of extension installation
  553. *
  554. * @return string One of "none", "manual", "git" or "automatic"
  555. */
  556. public function getInstallType()
  557. {
  558. if (!$this->isInstalled()) return 'none';
  559. if (!empty($this->managerData)) return 'automatic';
  560. if (is_dir($this->getInstallDir() . '/.git')) return 'git';
  561. return 'manual';
  562. }
  563. /**
  564. * If the extension can probably be installed/updated or uninstalled
  565. *
  566. * @return bool|string True or error string
  567. */
  568. public function canModify()
  569. {
  570. if ($this->isInstalled()) {
  571. if (!is_writable($this->getInstallDir())) {
  572. return 'noperms';
  573. }
  574. }
  575. if ($this->isTemplate() && !is_writable($this->tpllib)) {
  576. return 'notplperms';
  577. } elseif (!is_writable(DOKU_PLUGIN)) {
  578. return 'nopluginperms';
  579. }
  580. return true;
  581. }
  582. /**
  583. * Install an extension from a user upload
  584. *
  585. * @param string $field name of the upload file
  586. * @param boolean $overwrite overwrite folder if the extension name is the same
  587. * @throws Exception when something goes wrong
  588. * @return array The list of installed extensions
  589. */
  590. public function installFromUpload($field, $overwrite = true)
  591. {
  592. if ($_FILES[$field]['error']) {
  593. throw new Exception($this->getLang('msg_upload_failed') . ' (' . $_FILES[$field]['error'] . ')');
  594. }
  595. $tmp = $this->mkTmpDir();
  596. if (!$tmp) throw new Exception($this->getLang('error_dircreate'));
  597. // filename may contain the plugin name for old style plugins...
  598. $basename = basename($_FILES[$field]['name']);
  599. $basename = preg_replace('/\.(tar\.gz|tar\.bz|tar\.bz2|tar|tgz|tbz|zip)$/', '', $basename);
  600. $basename = preg_replace('/[\W]+/', '', $basename);
  601. if (!move_uploaded_file($_FILES[$field]['tmp_name'], "$tmp/upload.archive")) {
  602. throw new Exception($this->getLang('msg_upload_failed'));
  603. }
  604. $installed = $this->installArchive("$tmp/upload.archive", $overwrite, $basename);
  605. $this->updateManagerData('', $installed);
  606. $this->removeDeletedfiles($installed);
  607. $this->purgeCache();
  608. return $installed;
  609. }
  610. /**
  611. * Install an extension from a remote URL
  612. *
  613. * @param string $url
  614. * @param boolean $overwrite overwrite folder if the extension name is the same
  615. * @throws Exception when something goes wrong
  616. * @return array The list of installed extensions
  617. */
  618. public function installFromURL($url, $overwrite = true)
  619. {
  620. $path = $this->download($url);
  621. $installed = $this->installArchive($path, $overwrite);
  622. $this->updateManagerData($url, $installed);
  623. $this->removeDeletedfiles($installed);
  624. $this->purgeCache();
  625. return $installed;
  626. }
  627. /**
  628. * Install or update the extension
  629. *
  630. * @throws \Exception when something goes wrong
  631. * @return array The list of installed extensions
  632. */
  633. public function installOrUpdate()
  634. {
  635. $url = $this->getDownloadURL();
  636. $path = $this->download($url);
  637. $installed = $this->installArchive($path, $this->isInstalled(), $this->getBase());
  638. $this->updateManagerData($url, $installed);
  639. // refresh extension information
  640. if (!isset($installed[$this->getID()])) {
  641. throw new Exception('Error, the requested extension hasn\'t been installed or updated');
  642. }
  643. $this->removeDeletedfiles($installed);
  644. $this->setExtension($this->getID());
  645. $this->purgeCache();
  646. return $installed;
  647. }
  648. /**
  649. * Uninstall the extension
  650. *
  651. * @return bool If the plugin was sucessfully uninstalled
  652. */
  653. public function uninstall()
  654. {
  655. $this->purgeCache();
  656. return io_rmdir($this->getInstallDir(), true);
  657. }
  658. /**
  659. * Enable the extension
  660. *
  661. * @return bool|string True or an error message
  662. */
  663. public function enable()
  664. {
  665. if ($this->isTemplate()) return $this->getLang('notimplemented');
  666. if (!$this->isInstalled()) return $this->getLang('notinstalled');
  667. if ($this->isEnabled()) return $this->getLang('alreadyenabled');
  668. /* @var PluginController $plugin_controller */
  669. global $plugin_controller;
  670. if ($plugin_controller->enable($this->base)) {
  671. $this->purgeCache();
  672. return true;
  673. } else {
  674. return $this->getLang('pluginlistsaveerror');
  675. }
  676. }
  677. /**
  678. * Disable the extension
  679. *
  680. * @return bool|string True or an error message
  681. */
  682. public function disable()
  683. {
  684. if ($this->isTemplate()) return $this->getLang('notimplemented');
  685. /* @var PluginController $plugin_controller */
  686. global $plugin_controller;
  687. if (!$this->isInstalled()) return $this->getLang('notinstalled');
  688. if (!$this->isEnabled()) return $this->getLang('alreadydisabled');
  689. if ($plugin_controller->disable($this->base)) {
  690. $this->purgeCache();
  691. return true;
  692. } else {
  693. return $this->getLang('pluginlistsaveerror');
  694. }
  695. }
  696. /**
  697. * Purge the cache by touching the main configuration file
  698. */
  699. protected function purgeCache()
  700. {
  701. global $config_cascade;
  702. // expire dokuwiki caches
  703. // touching local.php expires wiki page, JS and CSS caches
  704. @touch(reset($config_cascade['main']['local']));
  705. }
  706. /**
  707. * Read local extension data either from info.txt or getInfo()
  708. */
  709. protected function readLocalData()
  710. {
  711. if ($this->isTemplate()) {
  712. $infopath = $this->getInstallDir() . '/template.info.txt';
  713. } else {
  714. $infopath = $this->getInstallDir() . '/plugin.info.txt';
  715. }
  716. if (is_readable($infopath)) {
  717. $this->localInfo = confToHash($infopath);
  718. } elseif (!$this->isTemplate() && $this->isEnabled()) {
  719. $path = $this->getInstallDir() . '/';
  720. $plugin = null;
  721. foreach (PluginController::PLUGIN_TYPES as $type) {
  722. if (file_exists($path . $type . '.php')) {
  723. $plugin = plugin_load($type, $this->base);
  724. if ($plugin instanceof PluginInterface) break;
  725. }
  726. if ($dh = @opendir($path . $type . '/')) {
  727. while (false !== ($cp = readdir($dh))) {
  728. if ($cp == '.' || $cp == '..' || !str_ends_with(strtolower($cp), '.php')) continue;
  729. $plugin = plugin_load($type, $this->base . '_' . substr($cp, 0, -4));
  730. if ($plugin instanceof PluginInterface) break;
  731. }
  732. if ($plugin instanceof PluginInterface) break;
  733. closedir($dh);
  734. }
  735. }
  736. if ($plugin instanceof PluginInterface) {
  737. $this->localInfo = $plugin->getInfo();
  738. }
  739. }
  740. }
  741. /**
  742. * Save the given URL and current datetime in the manager.dat file of all installed extensions
  743. *
  744. * @param string $url Where the extension was downloaded from. (empty for manual installs via upload)
  745. * @param array $installed Optional list of installed plugins
  746. */
  747. protected function updateManagerData($url = '', $installed = null)
  748. {
  749. $origID = $this->getID();
  750. if (is_null($installed)) {
  751. $installed = [$origID];
  752. }
  753. foreach (array_keys($installed) as $ext) {
  754. if ($this->getID() != $ext) $this->setExtension($ext);
  755. if ($url) {
  756. $this->managerData['downloadurl'] = $url;
  757. } elseif (isset($this->managerData['downloadurl'])) {
  758. unset($this->managerData['downloadurl']);
  759. }
  760. if (isset($this->managerData['installed'])) {
  761. $this->managerData['updated'] = date('r');
  762. } else {
  763. $this->managerData['installed'] = date('r');
  764. }
  765. $this->writeManagerData();
  766. }
  767. if ($this->getID() != $origID) $this->setExtension($origID);
  768. }
  769. /**
  770. * Read the manager.dat file
  771. */
  772. protected function readManagerData()
  773. {
  774. $managerpath = $this->getInstallDir() . '/manager.dat';
  775. if (is_readable($managerpath)) {
  776. $file = @file($managerpath);
  777. if (!empty($file)) {
  778. foreach ($file as $line) {
  779. [$key, $value] = sexplode('=', trim($line, DOKU_LF), 2, '');
  780. $key = trim($key);
  781. $value = trim($value);
  782. // backwards compatible with old plugin manager
  783. if ($key == 'url') $key = 'downloadurl';
  784. $this->managerData[$key] = $value;
  785. }
  786. }
  787. }
  788. }
  789. /**
  790. * Write the manager.data file
  791. */
  792. protected function writeManagerData()
  793. {
  794. $managerpath = $this->getInstallDir() . '/manager.dat';
  795. $data = '';
  796. foreach ($this->managerData as $k => $v) {
  797. $data .= $k . '=' . $v . DOKU_LF;
  798. }
  799. io_saveFile($managerpath, $data);
  800. }
  801. /**
  802. * Returns a temporary directory
  803. *
  804. * The directory is registered for cleanup when the class is destroyed
  805. *
  806. * @return false|string
  807. */
  808. protected function mkTmpDir()
  809. {
  810. $dir = io_mktmpdir();
  811. if (!$dir) return false;
  812. $this->temporary[] = $dir;
  813. return $dir;
  814. }
  815. /**
  816. * downloads a file from the net and saves it
  817. *
  818. * - $file is the directory where the file should be saved
  819. * - if successful will return the name used for the saved file, false otherwise
  820. *
  821. * @author Andreas Gohr <andi@splitbrain.org>
  822. * @author Chris Smith <chris@jalakai.co.uk>
  823. *
  824. * @param string $url url to download
  825. * @param string $file path to file or directory where to save
  826. * @param string $defaultName fallback for name of download
  827. * @return bool|string if failed false, otherwise true or the name of the file in the given dir
  828. */
  829. protected function downloadToFile($url, $file, $defaultName = '')
  830. {
  831. global $conf;
  832. $http = new DokuHTTPClient();
  833. $http->max_bodysize = 0;
  834. $http->timeout = 25; //max. 25 sec
  835. $http->keep_alive = false; // we do single ops here, no need for keep-alive
  836. $http->agent = 'DokuWiki HTTP Client (Extension Manager)';
  837. $data = $http->get($url);
  838. if ($data === false) return false;
  839. $name = '';
  840. if (isset($http->resp_headers['content-disposition'])) {
  841. $content_disposition = $http->resp_headers['content-disposition'];
  842. $match = [];
  843. if (
  844. is_string($content_disposition) &&
  845. preg_match('/attachment;\s*filename\s*=\s*"([^"]*)"/i', $content_disposition, $match)
  846. ) {
  847. $name = PhpString::basename($match[1]);
  848. }
  849. }
  850. if (!$name) {
  851. if (!$defaultName) return false;
  852. $name = $defaultName;
  853. }
  854. $file .= $name;
  855. $fileexists = file_exists($file);
  856. $fp = @fopen($file, "w");
  857. if (!$fp) return false;
  858. fwrite($fp, $data);
  859. fclose($fp);
  860. if (!$fileexists && $conf['fperm']) chmod($file, $conf['fperm']);
  861. return $name;
  862. }
  863. /**
  864. * Download an archive to a protected path
  865. *
  866. * @param string $url The url to get the archive from
  867. * @throws Exception when something goes wrong
  868. * @return string The path where the archive was saved
  869. */
  870. public function download($url)
  871. {
  872. // check the url
  873. if (!preg_match('/https?:\/\//i', $url)) {
  874. throw new Exception($this->getLang('error_badurl'));
  875. }
  876. // try to get the file from the path (used as plugin name fallback)
  877. $file = parse_url($url, PHP_URL_PATH);
  878. if (is_null($file)) {
  879. $file = md5($url);
  880. } else {
  881. $file = PhpString::basename($file);
  882. }
  883. // create tmp directory for download
  884. if (!($tmp = $this->mkTmpDir())) {
  885. throw new Exception($this->getLang('error_dircreate'));
  886. }
  887. // download
  888. if (!$file = $this->downloadToFile($url, $tmp . '/', $file)) {
  889. io_rmdir($tmp, true);
  890. throw new Exception(sprintf(
  891. $this->getLang('error_download'),
  892. '<bdi>' . hsc($url) . '</bdi>'
  893. ));
  894. }
  895. return $tmp . '/' . $file;
  896. }
  897. /**
  898. * @param string $file The path to the archive that shall be installed
  899. * @param bool $overwrite If an already installed plugin should be overwritten
  900. * @param string $base The basename of the plugin if it's known
  901. * @throws Exception when something went wrong
  902. * @return array list of installed extensions
  903. */
  904. public function installArchive($file, $overwrite = false, $base = '')
  905. {
  906. $installed_extensions = [];
  907. // create tmp directory for decompression
  908. if (!($tmp = $this->mkTmpDir())) {
  909. throw new Exception($this->getLang('error_dircreate'));
  910. }
  911. // add default base folder if specified to handle case where zip doesn't contain this
  912. if ($base && !@mkdir($tmp . '/' . $base)) {
  913. throw new Exception($this->getLang('error_dircreate'));
  914. }
  915. // decompress
  916. $this->decompress($file, "$tmp/" . $base);
  917. // search $tmp/$base for the folder(s) that has been created
  918. // move the folder(s) to lib/..
  919. $result = ['old' => [], 'new' => []];
  920. $default = ($this->isTemplate() ? 'template' : 'plugin');
  921. if (!$this->findFolders($result, $tmp . '/' . $base, $default)) {
  922. throw new Exception($this->getLang('error_findfolder'));
  923. }
  924. // choose correct result array
  925. if (count($result['new'])) {
  926. $install = $result['new'];
  927. } else {
  928. $install = $result['old'];
  929. }
  930. if (!count($install)) {
  931. throw new Exception($this->getLang('error_findfolder'));
  932. }
  933. // now install all found items
  934. foreach ($install as $item) {
  935. // where to install?
  936. if ($item['type'] == 'template') {
  937. $target_base_dir = $this->tpllib;
  938. } else {
  939. $target_base_dir = DOKU_PLUGIN;
  940. }
  941. if (!empty($item['base'])) {
  942. // use base set in info.txt
  943. } elseif ($base && count($install) == 1) {
  944. $item['base'] = $base;
  945. } else {
  946. // default - use directory as found in zip
  947. // plugins from github/master without *.info.txt will install in wrong folder
  948. // but using $info->id will make 'code3' fail (which should install in lib/code/..)
  949. $item['base'] = basename($item['tmp']);
  950. }
  951. // check to make sure we aren't overwriting anything
  952. $target = $target_base_dir . $item['base'];
  953. if (!$overwrite && file_exists($target)) {
  954. // this info message is not being exposed via exception,
  955. // so that it's not interrupting the installation
  956. msg(sprintf($this->getLang('msg_nooverwrite'), $item['base']));
  957. continue;
  958. }
  959. $action = file_exists($target) ? 'update' : 'install';
  960. // copy action
  961. if ($this->dircopy($item['tmp'], $target)) {
  962. // return info
  963. $id = $item['base'];
  964. if ($item['type'] == 'template') {
  965. $id = 'template:' . $id;
  966. }
  967. $installed_extensions[$id] = [
  968. 'base' => $item['base'],
  969. 'type' => $item['type'],
  970. 'action' => $action
  971. ];
  972. } else {
  973. throw new Exception(sprintf(
  974. $this->getLang('error_copy') . DOKU_LF,
  975. '<bdi>' . $item['base'] . '</bdi>'
  976. ));
  977. }
  978. }
  979. // cleanup
  980. if ($tmp) io_rmdir($tmp, true);
  981. if (function_exists('opcache_reset')) {
  982. opcache_reset();
  983. }
  984. return $installed_extensions;
  985. }
  986. /**
  987. * Find out what was in the extracted directory
  988. *
  989. * Correct folders are searched recursively using the "*.info.txt" configs
  990. * as indicator for a root folder. When such a file is found, it's base
  991. * setting is used (when set). All folders found by this method are stored
  992. * in the 'new' key of the $result array.
  993. *
  994. * For backwards compatibility all found top level folders are stored as
  995. * in the 'old' key of the $result array.
  996. *
  997. * When no items are found in 'new' the copy mechanism should fall back
  998. * the 'old' list.
  999. *
  1000. * @author Andreas Gohr <andi@splitbrain.org>
  1001. * @param array $result - results are stored here
  1002. * @param string $directory - the temp directory where the package was unpacked to
  1003. * @param string $default_type - type used if no info.txt available
  1004. * @param string $subdir - a subdirectory. do not set. used by recursion
  1005. * @return bool - false on error
  1006. */
  1007. protected function findFolders(&$result, $directory, $default_type = 'plugin', $subdir = '')
  1008. {
  1009. $this_dir = "$directory$subdir";
  1010. $dh = @opendir($this_dir);
  1011. if (!$dh) return false;
  1012. $found_dirs = [];
  1013. $found_files = 0;
  1014. $found_template_parts = 0;
  1015. while (false !== ($f = readdir($dh))) {
  1016. if ($f == '.' || $f == '..') continue;
  1017. if (is_dir("$this_dir/$f")) {
  1018. $found_dirs[] = "$subdir/$f";
  1019. } else {
  1020. // it's a file -> check for config
  1021. $found_files++;
  1022. switch ($f) {
  1023. case 'plugin.info.txt':
  1024. case 'template.info.txt':
  1025. // we have found a clear marker, save and return
  1026. $info = [];
  1027. $type = explode('.', $f, 2);
  1028. $info['type'] = $type[0];
  1029. $info['tmp'] = $this_dir;
  1030. $conf = confToHash("$this_dir/$f");
  1031. $info['base'] = basename($conf['base']);
  1032. $result['new'][] = $info;
  1033. return true;
  1034. case 'main.php':
  1035. case 'details.php':
  1036. case 'mediamanager.php':
  1037. case 'style.ini':
  1038. $found_template_parts++;
  1039. break;
  1040. }
  1041. }
  1042. }
  1043. closedir($dh);
  1044. // files where found but no info.txt - use old method
  1045. if ($found_files) {
  1046. $info = [];
  1047. $info['tmp'] = $this_dir;
  1048. // does this look like a template or should we use the default type?
  1049. if ($found_template_parts >= 2) {
  1050. $info['type'] = 'template';
  1051. } else {
  1052. $info['type'] = $default_type;
  1053. }
  1054. $result['old'][] = $info;
  1055. return true;
  1056. }
  1057. // we have no files yet -> recurse
  1058. foreach ($found_dirs as $found_dir) {
  1059. $this->findFolders($result, $directory, $default_type, "$found_dir");
  1060. }
  1061. return true;
  1062. }
  1063. /**
  1064. * Decompress a given file to the given target directory
  1065. *
  1066. * Determines the compression type from the file extension
  1067. *
  1068. * @param string $file archive to extract
  1069. * @param string $target directory to extract to
  1070. * @throws Exception
  1071. * @return bool
  1072. */
  1073. private function decompress($file, $target)
  1074. {
  1075. // decompression library doesn't like target folders ending in "/"
  1076. if (str_ends_with($target, '/')) $target = substr($target, 0, -1);
  1077. $ext = $this->guessArchiveType($file);
  1078. if (in_array($ext, ['tar', 'bz', 'gz'])) {
  1079. try {
  1080. $tar = new Tar();
  1081. $tar->open($file);
  1082. $tar->extract($target);
  1083. } catch (ArchiveIOException $e) {
  1084. throw new Exception($this->getLang('error_decompress') . ' ' . $e->getMessage(), $e->getCode(), $e);
  1085. }
  1086. return true;
  1087. } elseif ($ext == 'zip') {
  1088. try {
  1089. $zip = new Zip();
  1090. $zip->open($file);
  1091. $zip->extract($target);
  1092. } catch (ArchiveIOException $e) {
  1093. throw new Exception($this->getLang('error_decompress') . ' ' . $e->getMessage(), $e->getCode(), $e);
  1094. }
  1095. return true;
  1096. }
  1097. // the only case when we don't get one of the recognized archive types is
  1098. // when the archive file can't be read
  1099. throw new Exception($this->getLang('error_decompress') . ' Couldn\'t read archive file');
  1100. }
  1101. /**
  1102. * Determine the archive type of the given file
  1103. *
  1104. * Reads the first magic bytes of the given file for content type guessing,
  1105. * if neither bz, gz or zip are recognized, tar is assumed.
  1106. *
  1107. * @author Andreas Gohr <andi@splitbrain.org>
  1108. * @param string $file The file to analyze
  1109. * @return string|false false if the file can't be read, otherwise an "extension"
  1110. */
  1111. private function guessArchiveType($file)
  1112. {
  1113. $fh = fopen($file, 'rb');
  1114. if (!$fh) return false;
  1115. $magic = fread($fh, 5);
  1116. fclose($fh);
  1117. if (strpos($magic, "\x42\x5a") === 0) return 'bz';
  1118. if (strpos($magic, "\x1f\x8b") === 0) return 'gz';
  1119. if (strpos($magic, "\x50\x4b\x03\x04") === 0) return 'zip';
  1120. return 'tar';
  1121. }
  1122. /**
  1123. * Copy with recursive sub-directory support
  1124. *
  1125. * @param string $src filename path to file
  1126. * @param string $dst filename path to file
  1127. * @return bool|int|string
  1128. */
  1129. private function dircopy($src, $dst)
  1130. {
  1131. global $conf;
  1132. if (is_dir($src)) {
  1133. if (!$dh = @opendir($src)) return false;
  1134. if ($ok = io_mkdir_p($dst)) {
  1135. while ($ok && (false !== ($f = readdir($dh)))) {
  1136. if ($f == '..' || $f == '.') continue;
  1137. $ok = $this->dircopy("$src/$f", "$dst/$f");
  1138. }
  1139. }
  1140. closedir($dh);
  1141. return $ok;
  1142. } else {
  1143. $existed = file_exists($dst);
  1144. if (!@copy($src, $dst)) return false;
  1145. if (!$existed && $conf['fperm']) chmod($dst, $conf['fperm']);
  1146. @touch($dst, filemtime($src));
  1147. }
  1148. return true;
  1149. }
  1150. /**
  1151. * Delete outdated files from updated plugins
  1152. *
  1153. * @param array $installed
  1154. */
  1155. private function removeDeletedfiles($installed)
  1156. {
  1157. foreach ($installed as $extension) {
  1158. // only on update
  1159. if ($extension['action'] == 'install') continue;
  1160. // get definition file
  1161. if ($extension['type'] == 'template') {
  1162. $extensiondir = $this->tpllib;
  1163. } else {
  1164. $extensiondir = DOKU_PLUGIN;
  1165. }
  1166. $extensiondir = $extensiondir . $extension['base'] . '/';
  1167. $definitionfile = $extensiondir . 'deleted.files';
  1168. if (!file_exists($definitionfile)) continue;
  1169. // delete the old files
  1170. $list = file($definitionfile);
  1171. foreach ($list as $line) {
  1172. $line = trim(preg_replace('/#.*$/', '', $line));
  1173. if (!$line) continue;
  1174. $file = $extensiondir . $line;
  1175. if (!file_exists($file)) continue;
  1176. io_rmdir($file, true);
  1177. }
  1178. }
  1179. }
  1180. }
  1181. // vim:ts=4:sw=4:et: