Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
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.

611 lines
17KB

  1. <?php
  2. /**
  3. * @file src/Core/Install.php
  4. */
  5. namespace Friendica\Core;
  6. use DOMDocument;
  7. use Exception;
  8. use Friendica\Database\DBA;
  9. use Friendica\Database\DBStructure;
  10. use Friendica\Object\Image;
  11. use Friendica\Util\Network;
  12. use Friendica\Util\Strings;
  13. /**
  14. * Contains methods for installation purpose of Friendica
  15. */
  16. class Installer
  17. {
  18. // Default values for the install page
  19. const DEFAULT_LANG = 'en';
  20. const DEFAULT_TZ = 'America/Los_Angeles';
  21. const DEFAULT_HOST = 'localhost';
  22. /**
  23. * @var array the check outcomes
  24. */
  25. private $checks;
  26. /**
  27. * @var string The path to the PHP binary
  28. */
  29. private $phppath = null;
  30. /**
  31. * Returns all checks made
  32. *
  33. * @return array the checks
  34. */
  35. public function getChecks()
  36. {
  37. return $this->checks;
  38. }
  39. /**
  40. * Returns the PHP path
  41. *
  42. * @return string the PHP Path
  43. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  44. */
  45. public function getPHPPath()
  46. {
  47. // if not set, determine the PHP path
  48. if (!isset($this->phppath)) {
  49. $this->checkPHP();
  50. $this->resetChecks();
  51. }
  52. return $this->phppath;
  53. }
  54. /**
  55. * Resets all checks
  56. */
  57. public function resetChecks()
  58. {
  59. $this->checks = [];
  60. }
  61. /**
  62. * Install constructor.
  63. *
  64. */
  65. public function __construct()
  66. {
  67. $this->checks = [];
  68. }
  69. /**
  70. * Checks the current installation environment. There are optional and mandatory checks.
  71. *
  72. * @param string $baseurl The baseurl of Friendica
  73. * @param string $phpath Optional path to the PHP binary
  74. *
  75. * @return bool if the check succeed
  76. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  77. */
  78. public function checkEnvironment($baseurl, $phpath = null)
  79. {
  80. $returnVal = true;
  81. if (isset($phpath)) {
  82. if (!$this->checkPHP($phpath)) {
  83. $returnVal = false;
  84. }
  85. }
  86. if (!$this->checkFunctions()) {
  87. $returnVal = false;
  88. }
  89. if (!$this->checkImagick()) {
  90. $returnVal = false;
  91. }
  92. if (!$this->checkLocalIni()) {
  93. $returnVal = false;
  94. }
  95. if (!$this->checkSmarty3()) {
  96. $returnVal = false;
  97. }
  98. if (!$this->checkKeys()) {
  99. $returnVal = false;
  100. }
  101. if (!$this->checkHtAccess($baseurl)) {
  102. $returnVal = false;
  103. }
  104. return $returnVal;
  105. }
  106. /**
  107. * Executes the installation of Friendica in the given environment.
  108. * - Creates `config/local.config.php`
  109. * - Installs Database Structure
  110. *
  111. * @param string $phppath Path to the PHP-Binary (optional, if not set e.g. 'php' or '/usr/bin/php')
  112. * @param string $urlpath Path based on the URL of Friendica (e.g. '/friendica')
  113. * @param string $dbhost Hostname/IP of the Friendica Database
  114. * @param string $dbuser Username of the Database connection credentials
  115. * @param string $dbpass Password of the Database connection credentials
  116. * @param string $dbdata Name of the Database
  117. * @param string $timezone Timezone of the Friendica Installaton (e.g. 'Europe/Berlin')
  118. * @param string $language 2-letter ISO 639-1 code (eg. 'en')
  119. * @param string $adminmail Mail-Adress of the administrator
  120. * @param string $basepath The basepath of Friendica
  121. *
  122. * @return bool true if the config was created, otherwise false
  123. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  124. */
  125. public function createConfig($phppath, $urlpath, $dbhost, $dbuser, $dbpass, $dbdata, $timezone, $language, $adminmail, $basepath)
  126. {
  127. $tpl = Renderer::getMarkupTemplate('local.config.tpl');
  128. $txt = Renderer::replaceMacros($tpl, [
  129. '$phpath' => $phppath,
  130. '$dbhost' => $dbhost,
  131. '$dbuser' => $dbuser,
  132. '$dbpass' => $dbpass,
  133. '$dbdata' => $dbdata,
  134. '$timezone' => $timezone,
  135. '$language' => $language,
  136. '$urlpath' => $urlpath,
  137. '$adminmail' => $adminmail,
  138. ]);
  139. $result = file_put_contents($basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php', $txt);
  140. if (!$result) {
  141. $this->addCheck(L10n::t('The database configuration file "config/local.config.php" could not be written. Please use the enclosed text to create a configuration file in your web server root.'), false, false, htmlentities($txt, ENT_COMPAT, 'UTF-8'));
  142. }
  143. return $result;
  144. }
  145. /***
  146. * Installs the DB-Scheme for Friendica
  147. *
  148. * @param string $basePath The base path of this application
  149. *
  150. * @return bool true if the installation was successful, otherwise false
  151. * @throws Exception
  152. */
  153. public function installDatabase($basePath)
  154. {
  155. $result = DBStructure::update($basePath, false, true, true);
  156. if ($result) {
  157. $txt = L10n::t('You may need to import the file "database.sql" manually using phpmyadmin or mysql.') . EOL;
  158. $txt .= L10n::t('Please see the file "INSTALL.txt".');
  159. $this->addCheck($txt, false, true, htmlentities($result, ENT_COMPAT, 'UTF-8'));
  160. return false;
  161. }
  162. return true;
  163. }
  164. /**
  165. * Adds new checks to the array $checks
  166. *
  167. * @param string $title The title of the current check
  168. * @param bool $status 1 = check passed, 0 = check not passed
  169. * @param bool $required 1 = check is mandatory, 0 = check is optional
  170. * @param string $help A help-string for the current check
  171. * @param string $error_msg Optional. A error message, if the current check failed
  172. */
  173. private function addCheck($title, $status, $required, $help, $error_msg = "")
  174. {
  175. array_push($this->checks, [
  176. 'title' => $title,
  177. 'status' => $status,
  178. 'required' => $required,
  179. 'help' => $help,
  180. 'error_msg' => $error_msg,
  181. ]);
  182. }
  183. /**
  184. * PHP Check
  185. *
  186. * Checks the PHP environment.
  187. *
  188. * - Checks if a PHP binary is available
  189. * - Checks if it is the CLI version
  190. * - Checks if "register_argc_argv" is enabled
  191. *
  192. * @param string $phppath Optional. The Path to the PHP-Binary
  193. * @param bool $required Optional. If set to true, the PHP-Binary has to exist (Default false)
  194. *
  195. * @return bool false if something required failed
  196. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  197. */
  198. public function checkPHP($phppath = null, $required = false)
  199. {
  200. $passed3 = false;
  201. if (!isset($phppath)) {
  202. $phppath = 'php';
  203. }
  204. $passed = file_exists($phppath);
  205. if (!$passed) {
  206. $phppath = trim(shell_exec('which ' . $phppath));
  207. $passed = strlen($phppath);
  208. }
  209. $help = "";
  210. if (!$passed) {
  211. $help .= L10n::t('Could not find a command line version of PHP in the web server PATH.') . EOL;
  212. $help .= L10n::t("If you don't have a command line version of PHP installed on your server, you will not be able to run the background processing. See <a href='https://github.com/friendica/friendica/blob/master/doc/Install.md#set-up-the-worker'>'Setup the worker'</a>") . EOL;
  213. $help .= EOL . EOL;
  214. $tpl = Renderer::getMarkupTemplate('field_input.tpl');
  215. $help .= Renderer::replaceMacros($tpl, [
  216. '$field' => ['phpath', L10n::t('PHP executable path'), $phppath, L10n::t('Enter full path to php executable. You can leave this blank to continue the installation.')],
  217. ]);
  218. $phppath = "";
  219. }
  220. $this->addCheck(L10n::t('Command line PHP') . ($passed ? " (<tt>$phppath</tt>)" : ""), $passed, false, $help);
  221. if ($passed) {
  222. $cmd = "$phppath -v";
  223. $result = trim(shell_exec($cmd));
  224. $passed2 = (strpos($result, "(cli)") !== false);
  225. list($result) = explode("\n", $result);
  226. $help = "";
  227. if (!$passed2) {
  228. $help .= L10n::t("PHP executable is not the php cli binary \x28could be cgi-fgci version\x29") . EOL;
  229. $help .= L10n::t('Found PHP version: ') . "<tt>$result</tt>";
  230. }
  231. $this->addCheck(L10n::t('PHP cli binary'), $passed2, true, $help);
  232. } else {
  233. // return if it was required
  234. return !$required;
  235. }
  236. if ($passed2) {
  237. $str = Strings::getRandomName(8);
  238. $cmd = "$phppath bin/testargs.php $str";
  239. $result = trim(shell_exec($cmd));
  240. $passed3 = $result == $str;
  241. $help = "";
  242. if (!$passed3) {
  243. $help .= L10n::t('The command line version of PHP on your system does not have "register_argc_argv" enabled.') . EOL;
  244. $help .= L10n::t('This is required for message delivery to work.');
  245. } else {
  246. $this->phppath = $phppath;
  247. }
  248. $this->addCheck(L10n::t('PHP register_argc_argv'), $passed3, true, $help);
  249. }
  250. // passed2 & passed3 are required if first check passed
  251. return $passed2 && $passed3;
  252. }
  253. /**
  254. * OpenSSL Check
  255. *
  256. * Checks the OpenSSL Environment
  257. *
  258. * - Checks, if the command "openssl_pkey_new" is available
  259. *
  260. * @return bool false if something required failed
  261. */
  262. public function checkKeys()
  263. {
  264. $help = '';
  265. $res = false;
  266. $status = true;
  267. if (function_exists('openssl_pkey_new')) {
  268. $res = openssl_pkey_new([
  269. 'digest_alg' => 'sha1',
  270. 'private_key_bits' => 4096,
  271. 'encrypt_key' => false
  272. ]);
  273. }
  274. // Get private key
  275. if (!$res) {
  276. $help .= L10n::t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys') . EOL;
  277. $help .= L10n::t('If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".');
  278. $status = false;
  279. }
  280. $this->addCheck(L10n::t('Generate encryption keys'), $res, true, $help);
  281. return $status;
  282. }
  283. /**
  284. * PHP basic function check
  285. *
  286. * @param string $name The name of the function
  287. * @param string $title The (localized) title of the function
  288. * @param string $help The (localized) help of the function
  289. * @param boolean $required If true, this check is required
  290. *
  291. * @return bool false, if the check failed
  292. */
  293. private function checkFunction($name, $title, $help, $required)
  294. {
  295. $currHelp = '';
  296. $status = true;
  297. if (!function_exists($name)) {
  298. $currHelp = $help;
  299. $status = false;
  300. }
  301. $this->addCheck($title, $status, $required, $currHelp);
  302. return $status || (!$status && !$required);
  303. }
  304. /**
  305. * PHP functions Check
  306. *
  307. * Checks the following PHP functions
  308. * - libCurl
  309. * - GD Graphics
  310. * - OpenSSL
  311. * - PDO or MySQLi
  312. * - mb_string
  313. * - XML
  314. * - iconv
  315. * - POSIX
  316. *
  317. * @return bool false if something required failed
  318. */
  319. public function checkFunctions()
  320. {
  321. $returnVal = true;
  322. $help = '';
  323. $status = true;
  324. if (function_exists('apache_get_modules')) {
  325. if (!in_array('mod_rewrite', apache_get_modules())) {
  326. $help = L10n::t('Error: Apache webserver mod-rewrite module is required but not installed.');
  327. $status = false;
  328. $returnVal = false;
  329. }
  330. }
  331. $this->addCheck(L10n::t('Apache mod_rewrite module'), $status, true, $help);
  332. $help = '';
  333. $status = true;
  334. if (!function_exists('mysqli_connect') && !class_exists('pdo')) {
  335. $status = false;
  336. $help = L10n::t('Error: PDO or MySQLi PHP module required but not installed.');
  337. $returnVal = false;
  338. } else {
  339. if (!function_exists('mysqli_connect') && class_exists('pdo') && !in_array('mysql', \PDO::getAvailableDrivers())) {
  340. $status = false;
  341. $help = L10n::t('Error: The MySQL driver for PDO is not installed.');
  342. $returnVal = false;
  343. }
  344. }
  345. $this->addCheck(L10n::t('PDO or MySQLi PHP module'), $status, true, $help);
  346. // check for XML DOM Documents being able to be generated
  347. $help = '';
  348. $status = true;
  349. try {
  350. new DOMDocument();
  351. } catch (Exception $e) {
  352. $help = L10n::t('Error, XML PHP module required but not installed.');
  353. $status = false;
  354. $returnVal = false;
  355. }
  356. $this->addCheck(L10n::t('XML PHP module'), $status, true, $help);
  357. $status = $this->checkFunction('curl_init',
  358. L10n::t('libCurl PHP module'),
  359. L10n::t('Error: libCURL PHP module required but not installed.'),
  360. true
  361. );
  362. $returnVal = $returnVal ? $status : false;
  363. $status = $this->checkFunction('imagecreatefromjpeg',
  364. L10n::t('GD graphics PHP module'),
  365. L10n::t('Error: GD graphics PHP module with JPEG support required but not installed.'),
  366. true
  367. );
  368. $returnVal = $returnVal ? $status : false;
  369. $status = $this->checkFunction('openssl_public_encrypt',
  370. L10n::t('OpenSSL PHP module'),
  371. L10n::t('Error: openssl PHP module required but not installed.'),
  372. true
  373. );
  374. $returnVal = $returnVal ? $status : false;
  375. $status = $this->checkFunction('mb_strlen',
  376. L10n::t('mb_string PHP module'),
  377. L10n::t('Error: mb_string PHP module required but not installed.'),
  378. true
  379. );
  380. $returnVal = $returnVal ? $status : false;
  381. $status = $this->checkFunction('iconv_strlen',
  382. L10n::t('iconv PHP module'),
  383. L10n::t('Error: iconv PHP module required but not installed.'),
  384. true
  385. );
  386. $returnVal = $returnVal ? $status : false;
  387. $status = $this->checkFunction('posix_kill',
  388. L10n::t('POSIX PHP module'),
  389. L10n::t('Error: POSIX PHP module required but not installed.'),
  390. true
  391. );
  392. $returnVal = $returnVal ? $status : false;
  393. $status = $this->checkFunction('json_encode',
  394. L10n::t('JSON PHP module'),
  395. L10n::t('Error: JSON PHP module required but not installed.'),
  396. true
  397. );
  398. $returnVal = $returnVal ? $status : false;
  399. return $returnVal;
  400. }
  401. /**
  402. * "config/local.config.php" - Check
  403. *
  404. * Checks if it's possible to create the "config/local.config.php"
  405. *
  406. * @return bool false if something required failed
  407. */
  408. public function checkLocalIni()
  409. {
  410. $status = true;
  411. $help = "";
  412. if ((file_exists('config/local.config.php') && !is_writable('config/local.config.php')) ||
  413. (!file_exists('config/local.config.php') && !is_writable('.'))) {
  414. $status = false;
  415. $help = L10n::t('The web installer needs to be able to create a file called "local.config.php" in the "config" folder of your web server and it is unable to do so.') . EOL;
  416. $help .= L10n::t('This is most often a permission setting, as the web server may not be able to write files in your folder - even if you can.') . EOL;
  417. $help .= L10n::t('At the end of this procedure, we will give you a text to save in a file named local.config.php in your Friendica "config" folder.') . EOL;
  418. $help .= L10n::t('You can alternatively skip this procedure and perform a manual installation. Please see the file "INSTALL.txt" for instructions.') . EOL;
  419. }
  420. $this->addCheck(L10n::t('config/local.config.php is writable'), $status, false, $help);
  421. // Local INI File is not required
  422. return true;
  423. }
  424. /**
  425. * Smarty3 Template Check
  426. *
  427. * Checks, if the directory of Smarty3 is writable
  428. *
  429. * @return bool false if something required failed
  430. */
  431. public function checkSmarty3()
  432. {
  433. $status = true;
  434. $help = "";
  435. if (!is_writable('view/smarty3')) {
  436. $status = false;
  437. $help = L10n::t('Friendica uses the Smarty3 template engine to render its web views. Smarty3 compiles templates to PHP to speed up rendering.') . EOL;
  438. $help .= L10n::t('In order to store these compiled templates, the web server needs to have write access to the directory view/smarty3/ under the Friendica top level folder.') . EOL;
  439. $help .= L10n::t("Please ensure that the user that your web server runs as \x28e.g. www-data\x29 has write access to this folder.") . EOL;
  440. $help .= L10n::t("Note: as a security measure, you should give the web server write access to view/smarty3/ only--not the template files \x28.tpl\x29 that it contains.") . EOL;
  441. }
  442. $this->addCheck(L10n::t('view/smarty3 is writable'), $status, true, $help);
  443. return $status;
  444. }
  445. /**
  446. * ".htaccess" - Check
  447. *
  448. * Checks, if "url_rewrite" is enabled in the ".htaccess" file
  449. *
  450. * @param string $baseurl The baseurl of the app
  451. * @return bool false if something required failed
  452. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  453. */
  454. public function checkHtAccess($baseurl)
  455. {
  456. $status = true;
  457. $help = "";
  458. $error_msg = "";
  459. if (function_exists('curl_init')) {
  460. $fetchResult = Network::fetchUrlFull($baseurl . "/install/testrewrite");
  461. $url = Strings::normaliseLink($baseurl . "/install/testrewrite");
  462. if ($fetchResult->getReturnCode() != 204) {
  463. $fetchResult = Network::fetchUrlFull($url);
  464. }
  465. if ($fetchResult->getReturnCode() != 204) {
  466. $status = false;
  467. $help = L10n::t('Url rewrite in .htaccess is not working. Make sure you copied .htaccess-dist to .htaccess.');
  468. $error_msg = [];
  469. $error_msg['head'] = L10n::t('Error message from Curl when fetching');
  470. $error_msg['url'] = $fetchResult->getRedirectUrl();
  471. $error_msg['msg'] = $fetchResult->getError();
  472. }
  473. $this->addCheck(L10n::t('Url rewrite is working'), $status, true, $help, $error_msg);
  474. } else {
  475. // cannot check modrewrite if libcurl is not installed
  476. /// @TODO Maybe issue warning here?
  477. }
  478. return $status;
  479. }
  480. /**
  481. * Imagick Check
  482. *
  483. * Checks, if the imagick module is available
  484. *
  485. * @return bool false if something required failed
  486. */
  487. public function checkImagick()
  488. {
  489. $imagick = false;
  490. $gif = false;
  491. if (class_exists('Imagick')) {
  492. $imagick = true;
  493. $supported = Image::supportedTypes();
  494. if (array_key_exists('image/gif', $supported)) {
  495. $gif = true;
  496. }
  497. }
  498. if (!$imagick) {
  499. $this->addCheck(L10n::t('ImageMagick PHP extension is not installed'), $imagick, false, "");
  500. } else {
  501. $this->addCheck(L10n::t('ImageMagick PHP extension is installed'), $imagick, false, "");
  502. if ($imagick) {
  503. $this->addCheck(L10n::t('ImageMagick supports GIF'), $gif, false, "");
  504. }
  505. }
  506. // Imagick is not required
  507. return true;
  508. }
  509. /**
  510. * Checking the Database connection and if it is available for the current installation
  511. *
  512. * @param string $dbhost Hostname/IP of the Friendica Database
  513. * @param string $dbuser Username of the Database connection credentials
  514. * @param string $dbpass Password of the Database connection credentials
  515. * @param string $dbdata Name of the Database
  516. *
  517. * @return bool true if the check was successful, otherwise false
  518. * @throws Exception
  519. */
  520. public function checkDB($dbhost, $dbuser, $dbpass, $dbdata)
  521. {
  522. if (!DBA::connect($dbhost, $dbuser, $dbpass, $dbdata)) {
  523. $this->addCheck(L10n::t('Could not connect to database.'), false, true, '');
  524. return false;
  525. }
  526. if (DBA::connected()) {
  527. if (DBStructure::existsTable('user')) {
  528. $this->addCheck(L10n::t('Database already in use.'), false, true, '');
  529. return false;
  530. }
  531. }
  532. return true;
  533. }
  534. }