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.

Installer.php 18KB

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