1 <?php
2
3 namespace Setup;
4
5 use Settings as SET;
6
7 8 9 10 11
12 class PHP extends AbstractBinSetup {
13
14 15 16 17 18
19 protected $binchecker;
20
21 22 23 24 25
26 function __construct() {
27 $this->dest = DIR_TMP . 'php.zip';
28 $this->dest_unzip = DIR_TMP . 'php' . DIRECTORY_SEPARATOR;
29
30 $this->binchecker = new \BinChecker\PHP();
31 $this->links = $this->binchecker->getLinks();
32
33 $this
34 ->promptDownload()
35 ->unzip()
36 ->copy()
37 ->renameIni()
38 ->editIni();
39 }
40
41 42 43 44 45 46
47 protected function editIni() {
48 $timezone = SET::$s->php_date_timezone;
49 $ini = $this->unzipped_destination . 'php.ini';
50
51 if(!$timezone) {
52 $timezone = 'Europe/London';
53 _echo('Your timezone setting wasn\'t found. Setting it to Europe/London.');
54 SET::$s->php_date_timezone = $timezone;
55 SET::$s->save();
56 }
57
58 $contents = file_get_contents($ini);
59
60 $err_log_dir = '"' . str_replace(DIRECTORY_SEPARATOR, '/', DIR_LOGS) . 'php/php_errors.log"';
61
62 if($contents) {
63 $contents = str_ireplace([
64 ';date.timezone =',
65 '; extension_dir = "ext"',
66 ';error_log = php_errors.log'
67 ],
68 [
69 'date.timezone = ' . $timezone,
70 'extension_dir = "' . $this->unzipped_destination . 'ext"',
71 'error_log = ' . $err_log_dir . ''
72 ],
73 $contents);
74
75 if(file_put_contents($ini, $contents) !== false) {
76 _echo('php.ini edited');
77 } else {
78 $msg = 'Failed to edit php.ini. You will have to set the following yourself:' . PHP_EOL
79 . "\t Find ';date.timezone =' and change it to 'date.timezone = ' . $timezone'" . PHP_EOL
80 . "\t Find '; extension_dir = \"ext\"' and change it to 'extension_dir = \"ext\"'" . PHP_EOL
81 . "\t Find ';error_log = php_errors.log' and change it to 'error_log = $err_log_dir'";
82 _echo($msg . PHP_EOL . 'Press ENTER to continue');
83 \IO::readline();
84 }
85 } else {
86 _echo('Failed to open php.ini for editing. You will have to set the timezone yourself.');
87 }
88
89 return $this;
90 }
91
92 93 94 95 96 97
98 protected function renameIni() {
99 if(!file_exists($this->unzipped_destination . 'php.ini')) {
100 $file = null;
101 if(file_exists($this->unzipped_destination . 'php.ini-development')) {
102 $file = $this->unzipped_destination . 'php.ini-development';
103 } elseif(file_exists($this->unzipped_destination . 'php.ini-production')) {
104 $file = $this->unzipped_destination . 'php.ini-production';
105 } else {
106 die('Could not find sample php.ini. Aborting.');
107 }
108
109 copy($file, $this->unzipped_destination . 'php.ini');
110
111 if(!file_exists($this->unzipped_destination . 'php.ini')) {
112 die('Failed to copy sample php.ini. Aborting.');
113 } else {
114 _echo('Sample php.ini copied');
115 }
116 } else {
117 _echo('php.ini found');
118 }
119
120 return $this;
121 }
122
123 124 125 126 127 128
129 protected function copy() {
130 _echo('Copying unzipped contents..');
131
132 $source = rtrim($this->dest_unzip, DIRECTORY_SEPARATOR);
133 $this->unzipped_destination = DIR_PHP . $this->version;
134
135 if(!file_exists($this->unzipped_destination)) {
136 mkdir($this->unzipped_destination . DIRECTORY_SEPARATOR, 777, true);
137 }
138
139 shell_exec('xcopy /s /e "' . $source . '" "' . $this->unzipped_destination . '"');
140
141 $this->unzipped_destination .= DIRECTORY_SEPARATOR;
142
143 if(file_exists($this->unzipped_destination . 'php.ini-development') ||
144 file_exists($this->unzipped_destination . 'php.ini')
145 ) {
146 _echo('Copy successful!');
147 $this->updateSettings();
148 $this->cleanup();
149 } else {
150 die('Failed to copy. Terminating setup.');
151 }
152
153 return $this;
154 }
155
156 157 158 159 160 161
162 protected function updateSettings() {
163 SET::$s->php_version = $this->version;
164 SET::$s->save();
165
166 return $this;
167 }
168 }