|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This software is governed by the CeCILL-B license. If a copy of this license |
| 4 | + * is not distributed with this file, you can obtain one at |
| 5 | + * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt |
| 6 | + * |
| 7 | + * Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ |
| 8 | + * Authors of Framadate/OpenSondate: Framasoft (https://github.com/framasoft) |
| 9 | + * |
| 10 | + * ============================= |
| 11 | + * |
| 12 | + * Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence |
| 13 | + * ne se trouve pas avec ce fichier vous pouvez l'obtenir sur |
| 14 | + * http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt |
| 15 | + * |
| 16 | + * Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ |
| 17 | + * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft) |
| 18 | + */ |
| 19 | +namespace Framadate\Services; |
| 20 | +use Framadate\Utils; |
| 21 | +use Smarty; |
| 22 | + |
| 23 | +/** |
| 24 | + * This class helps to clean all inputs from the users or external services. |
| 25 | + */ |
| 26 | +class InstallService { |
| 27 | + |
| 28 | + private $fields = array( |
| 29 | + 'General' => |
| 30 | + array( |
| 31 | + 'appName' => 'Framadate', |
| 32 | + 'appMail' => '', |
| 33 | + 'responseMail' => '', |
| 34 | + 'defaultLanguage' => 'fr', |
| 35 | + 'cleanUrl' => true |
| 36 | + ), |
| 37 | + 'Database configuration' => |
| 38 | + array( |
| 39 | + 'dbConnectionString' => 'mysql:host=HOST;dbname=SCHEMA;port=3306', |
| 40 | + 'dbUser' => 'root', |
| 41 | + 'dbPassword' => '', |
| 42 | + 'dbPrefix' => 'fd_', |
| 43 | + 'migrationTable' => 'framadate_migration' |
| 44 | + ) |
| 45 | + ); |
| 46 | + |
| 47 | + function __construct() {} |
| 48 | + |
| 49 | + public function install($data, Smarty &$smarty) { |
| 50 | + // Check values are present |
| 51 | + if (empty($data['appName']) || empty($data['appMail']) || empty($data['defaultLanguage']) || empty($data['dbConnectionString']) || empty($data['dbUser'])) { |
| 52 | + return $this->error('MISSING_VALUES'); |
| 53 | + } |
| 54 | + |
| 55 | + // Connect to database |
| 56 | + $connect = $this->connectTo($data['dbConnectionString'], $data['dbUser'], $data['dbPassword']); |
| 57 | + if (!$connect) { |
| 58 | + return $this->error('CANT_CONNECT_TO_DATABASE'); |
| 59 | + } |
| 60 | + |
| 61 | + // Create database schema |
| 62 | + $this->createDatabaseSchema($connect); |
| 63 | + |
| 64 | + // Write configuration to conf.php file |
| 65 | + $this->writeConfiguration($data, $smarty); |
| 66 | + |
| 67 | + return $this->ok(); |
| 68 | + } |
| 69 | + |
| 70 | + function connectTo($connectionString, $user, $password) { |
| 71 | + try { |
| 72 | + $pdo = @new \PDO($connectionString, $user, $password); |
| 73 | + $pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_OBJ); |
| 74 | + $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
| 75 | + return $pdo; |
| 76 | + } catch(\Exception $e) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + function writeConfiguration($data, Smarty &$smarty) { |
| 82 | + foreach($this->fields as $groupKey=>$group) { |
| 83 | + foreach ($group as $field=>$value) { |
| 84 | + $smarty->assign($field, $data[$field]); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + $content = $smarty->fetch('admin/config.tpl'); |
| 89 | + |
| 90 | + $this->writeToFile($content); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param $content |
| 95 | + */ |
| 96 | + function writeToFile($content) { |
| 97 | + file_put_contents(CONF_FILENAME, $content); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Execute SQL installation scripts. |
| 102 | + * |
| 103 | + * @param \PDO $connect |
| 104 | + */ |
| 105 | + function createDatabaseSchema($connect) { |
| 106 | + $dir = opendir(ROOT_DIR . '/install/'); |
| 107 | + while ($dir && ($file = readdir($dir)) !== false) { |
| 108 | + if ($file !== '.' && $file !== '..' && strpos($file, '.mysql.auto.sql')) { |
| 109 | + $statement = file_get_contents(ROOT_DIR . '/install/' . $file); |
| 110 | + $connect->exec($statement); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @return array |
| 117 | + */ |
| 118 | + function ok() { |
| 119 | + return array( |
| 120 | + 'status' => 'OK', |
| 121 | + 'msg' => __f('Installation', 'Ended', Utils::get_server_name()) |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @param $msg |
| 127 | + * @return array |
| 128 | + */ |
| 129 | + function error($msg) { |
| 130 | + return array( |
| 131 | + 'status' => 'ERROR', |
| 132 | + 'code' => $msg |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments