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.

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