install.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. <?php
  2. // Install extension, https://github.com/annaesvensson/yellow-install
  3. class YellowInstall {
  4. const VERSION = "0.9.3";
  5. const PRIORITY = "1";
  6. public $yellow; // access to API
  7. // Handle initialisation
  8. public function onLoad($yellow) {
  9. $this->yellow = $yellow;
  10. }
  11. // Handle request
  12. public function onRequest($scheme, $address, $base, $location, $fileName) {
  13. return $this->processRequestInstall($scheme, $address, $base, $location, $fileName);
  14. }
  15. // Handle command
  16. public function onCommand($command, $text) {
  17. return $this->processCommandInstall($command, $text);
  18. }
  19. // Process request to install website
  20. public function processRequestInstall($scheme, $address, $base, $location, $fileName) {
  21. $statusCode = 0;
  22. if ($this->yellow->lookup->isContentFile($fileName) || is_string_empty($fileName)) {
  23. if ($this->yellow->system->get("updateCurrentRelease")=="none") {
  24. $this->checkServerRequirements();
  25. $author = trim(preg_replace("/[^\pL\d\-\. ]/u", "-", $this->yellow->page->getRequest("author")));
  26. $email = trim($this->yellow->page->getRequest("email"));
  27. $password = trim($this->yellow->page->getRequest("password"));
  28. $language = trim($this->yellow->page->getRequest("language"));
  29. $extension = trim($this->yellow->page->getRequest("extension"));
  30. $status = trim($this->yellow->page->getRequest("status"));
  31. $statusCode = $this->updateLog();
  32. $statusCode = max($statusCode, $this->updateLanguages("small"));
  33. $errorMessage = $this->yellow->page->errorMessage;
  34. $this->yellow->content->pages["root/"] = array();
  35. $this->yellow->page = new YellowPage($this->yellow);
  36. $this->yellow->page->setRequestInformation($scheme, $address, $base, $location, $fileName, false);
  37. $this->yellow->page->parseMeta($this->getRawDataInstall(), $statusCode, $errorMessage);
  38. $this->yellow->page->parseContent();
  39. $this->yellow->page->parsePage();
  40. if ($status=="install") $status = $this->updateExtensions("small", $extension)==200 ? "ok" : "error";
  41. if ($status=="ok") $status = $this->updateUser($email, $password, $author, $language)==200 ? "ok" : "error";
  42. if ($status=="ok") $status = $this->updateAuthentication($scheme, $address, $base, $email)==200 ? "ok" : "error";
  43. if ($status=="ok") $status = $this->updateContent($language, "installHome", "/")==200 ? "ok" : "error";
  44. if ($status=="ok") $status = $this->updateContent($language, "installAbout", "/about/")==200 ? "ok" : "error";
  45. if ($status=="ok") $status = $this->updateContent($language, "installDefault", "/shared/page-new-default")==200 ? "ok" : "error";
  46. if ($status=="ok") $status = $this->updateContent($language, "installWiki", "/shared/page-new-wiki")==200 ? "ok" : "error";
  47. if ($status=="ok") $status = $this->updateContent($language, "installBlog", "/shared/page-new-blog")==200 ? "ok" : "error";
  48. if ($status=="ok") $status = $this->updateContent($language, "coreError404", "/shared/page-error-404")==200 ? "ok" : "error";
  49. if ($status=="ok") $status = $this->updateSettings()==200 ? "ok" : "error";
  50. if ($status=="ok") $status = $this->removeInstall()==200 ? "done" : "error";
  51. } else {
  52. $status = $this->removeInstall(true)==200 ? "done" : "error";
  53. }
  54. if ($status=="done") {
  55. $location = $this->yellow->lookup->normaliseUrl($scheme, $address, $base, "/");
  56. $statusCode = $this->yellow->sendStatus(303, $location);
  57. } else {
  58. $statusCode = $this->yellow->sendData($this->yellow->page->statusCode, $this->yellow->page->headerData, $this->yellow->page->outputData);
  59. }
  60. }
  61. return $statusCode;
  62. }
  63. // Process command to install website
  64. public function processCommandInstall($command, $text) {
  65. $statusCode = 0;
  66. if ($this->yellow->system->get("updateCurrentRelease")=="none") {
  67. $this->checkCommandRequirements();
  68. list($installation, $option) = $this->yellow->toolbox->getTextArguments($text);
  69. if (is_string_empty($command)) {
  70. $statusCode = 200;
  71. echo "Datenstrom Yellow is for people who make small websites. https://datenstrom.se/yellow/\n";
  72. echo "Syntax: php yellow.php\n";
  73. echo " php yellow.php about [extension]\n";
  74. echo " php yellow.php serve [url]\n";
  75. echo " php yellow.php skip installation [option]\n";
  76. } elseif ($command=="about" || $command=="serve") {
  77. $statusCode = 0;
  78. } elseif ($command=="skip" && $installation=="installation") {
  79. $statusCode = $this->updateLog();
  80. if ($statusCode==200) $statusCode = $this->updateLanguages($option);
  81. if ($statusCode==200) $statusCode = $this->updateExtensions($option, "");
  82. if ($statusCode==200) $statusCode = $this->updateSettings(true);
  83. if ($statusCode==200) $statusCode = $this->removeInstall();
  84. if ($statusCode>=400) {
  85. echo "ERROR installing files: ".$this->yellow->page->errorMessage."\n";
  86. echo "The installation has not been completed. Please run command again.\n";
  87. } else {
  88. $extensionsCount = $this->getExtensionsCount();
  89. echo "Yellow $command: $extensionsCount extension".($extensionsCount!=1 ? "s" : "").", 0 errors\n";
  90. }
  91. } else {
  92. $statusCode = 500;
  93. echo "The installation has not been completed. Please type 'php yellow.php serve' or 'php yellow.php skip installation`.\n";
  94. }
  95. } else {
  96. $statusCode = $this->removeInstall(true);
  97. if ($statusCode==200) $statusCode = 0;
  98. if ($statusCode>=400) {
  99. echo "ERROR installing files: ".$this->yellow->page->errorMessage."\n";
  100. echo "Detected ZIP files, 0 extensions installed. Please run command again.\n";
  101. }
  102. }
  103. return $statusCode;
  104. }
  105. // Update log file
  106. public function updateLog() {
  107. $statusCode = 200;
  108. $fileName = $this->yellow->system->get("coreExtensionDirectory").$this->yellow->system->get("coreWebsiteFile");
  109. if (!is_file($fileName)) {
  110. list($name, $version, $os) = $this->yellow->toolbox->detectServerInformation();
  111. $product = "Datenstrom Yellow ".YellowCore::RELEASE;
  112. $this->yellow->toolbox->log("info", "Install $product, PHP ".PHP_VERSION.", $name $version, $os");
  113. foreach ($this->yellow->extension->data as $key=>$value) {
  114. if ($key=="install") continue;
  115. $this->yellow->toolbox->log("info", "Install extension '".ucfirst($key)." $value[version]'");
  116. }
  117. if (!is_file($fileName)) {
  118. $statusCode = 500;
  119. $this->yellow->page->error($statusCode, "Can't write file '$fileName'!");
  120. }
  121. }
  122. return $statusCode;
  123. }
  124. // Update languages
  125. public function updateLanguages($option) {
  126. $statusCode = 200;
  127. $path = $this->yellow->system->get("coreWorkerDirectory")."install-language.bin";
  128. $zip = new ZipArchive();
  129. if ($zip->open($path)===true) {
  130. $pathBase = "";
  131. if (preg_match("#^(.*\/).*?$#", $zip->getNameIndex(0), $matches)) $pathBase = $matches[1];
  132. $fileData = $zip->getFromName($pathBase.$this->yellow->system->get("updateExtensionFile"));
  133. foreach ($this->getLanguageExtensionsRequired($fileData, $option) as $extension) {
  134. $fileDataPhp = $zip->getFromName($pathBase."translations/$extension/$extension.php");
  135. $fileDataIni = $zip->getFromName($pathBase."translations/$extension/extension.ini");
  136. $statusCode = max($statusCode, $this->updateLanguageArchive($fileDataPhp, $fileDataIni, $pathBase, "install"));
  137. }
  138. $this->yellow->extension->load($this->yellow->system->get("coreWorkerDirectory"));
  139. $this->yellow->language->load($this->yellow->system->get("coreExtensionDirectory").$this->yellow->system->get("coreLanguageFile"));
  140. $zip->close();
  141. } else {
  142. $statusCode = 500;
  143. $this->yellow->page->error($statusCode, "Can't open file '$path'!");
  144. }
  145. return $statusCode;
  146. }
  147. // Update language archive
  148. public function updateLanguageArchive($fileDataPhp, $fileDataIni, $pathBase, $action) {
  149. $statusCode = 200;
  150. if ($this->yellow->extension->isExisting("update")) {
  151. $settings = $this->yellow->toolbox->getTextSettings($fileDataIni, "");
  152. $extension = lcfirst($settings->get("extension"));
  153. $version = $settings->get("version");
  154. $modified = strtotime($settings->get("published"));
  155. $fileNamePhp = $this->yellow->system->get("coreWorkerDirectory").$extension.".php";
  156. if (!is_string_empty($extension) && !is_string_empty($version) && !is_file($fileNamePhp)) {
  157. $statusCode = max($statusCode, $this->yellow->extension->get("update")->updateExtensionSettings($extension, $action, $settings));
  158. $statusCode = max($statusCode, $this->yellow->extension->get("update")->updateExtensionFile(
  159. $fileNamePhp, $fileDataPhp, $modified, 0, 0, "create", $extension));
  160. $this->yellow->toolbox->log($statusCode==200 ? "info" : "error", ucfirst($action)." extension '".ucfirst($extension)." $version'");
  161. }
  162. }
  163. return $statusCode;
  164. }
  165. // Update extensions
  166. public function updateExtensions($option, $extension) {
  167. $statusCode = 200;
  168. if ($this->yellow->extension->isExisting("update")) {
  169. if (!is_string_empty($option)) {
  170. if ($option=="medium" || $option=="large") {
  171. $path = $this->yellow->system->get("coreExtensionDirectory");
  172. $fileData = $this->yellow->toolbox->readFile($path.$this->yellow->system->get("updateAvailableFile"));
  173. $settings = $this->yellow->toolbox->getTextSettings($fileData, "extension");
  174. $extensions = $this->getAvailableExtensionsRequired($settings, $option);
  175. $statusCode = $this->downloadExtensionsAvailable($settings, $extensions);
  176. $path = $this->yellow->system->get("coreWorkerDirectory");
  177. foreach ($this->yellow->toolbox->getDirectoryEntries($path, "/^install-.*\.bin$/", true, false) as $entry) {
  178. if (basename($entry)=="install-language.bin") continue;
  179. if (preg_match("/^install-(.*?)\.bin/", basename($entry), $matches) && !in_array($matches[1], $extensions)) continue;
  180. $statusCode = max($statusCode, $this->yellow->extension->get("update")->updateExtensionArchive($entry, "install"));
  181. }
  182. }
  183. if (!($option=="small" || $option=="medium" || $option=="large")) {
  184. $statusCode = 500;
  185. $this->yellow->page->error($statusCode, "Option '$option' not supported!");
  186. }
  187. }
  188. if (!is_string_empty($extension)) {
  189. $path = $this->yellow->system->get("coreWorkerDirectory")."install-".$extension.".bin";
  190. if (is_file($path)) {
  191. $statusCode = $this->yellow->extension->get("update")->updateExtensionArchive($path, "install");
  192. }
  193. }
  194. }
  195. return $statusCode;
  196. }
  197. // Update user
  198. public function updateUser($email, $password, $name, $language) {
  199. $statusCode = 200;
  200. if ($this->yellow->extension->isExisting("edit") && !is_string_empty($email) && !is_string_empty($password)) {
  201. if (is_string_empty($name)) $name = $this->yellow->system->get("sitename");
  202. $fileNameUser = $this->yellow->system->get("coreExtensionDirectory").$this->yellow->system->get("coreUserFile");
  203. $settings = array(
  204. "name" => $name,
  205. "description" => $this->yellow->language->getText("editUserDescription", $language),
  206. "language" => $language,
  207. "access" => "create, edit, delete, restore, upload, configure, update",
  208. "home" => "/",
  209. "hash" => $this->yellow->extension->get("edit")->response->createHash($password),
  210. "stamp" => $this->yellow->extension->get("edit")->response->createStamp(),
  211. "pending" => "none",
  212. "failed" => "0",
  213. "modified" => date("Y-m-d H:i:s", time()),
  214. "status" => "active");
  215. if (!$this->yellow->user->save($fileNameUser, $email, $settings)) {
  216. $statusCode = 500;
  217. $this->yellow->page->error($statusCode, "Can't write file '$fileNameUser'!");
  218. }
  219. $this->yellow->toolbox->log($statusCode==200 ? "info" : "error", "Add user '".strtok($name, " ")."'");
  220. }
  221. return $statusCode;
  222. }
  223. // Update authentication
  224. public function updateAuthentication($scheme, $address, $base, $email) {
  225. if ($this->yellow->extension->isExisting("edit") && $this->yellow->user->isExisting($email)) {
  226. $base = rtrim($base.$this->yellow->system->get("editLocation"), "/");
  227. $this->yellow->extension->get("edit")->response->createCookies($scheme, $address, $base, $email);
  228. }
  229. return 200;
  230. }
  231. // Update content
  232. public function updateContent($language, $name, $location) {
  233. $statusCode = 200;
  234. $fileName = $this->yellow->lookup->findFileFromContentLocation($location);
  235. $fileData = str_replace("\r\n", "\n", $this->yellow->toolbox->readFile($fileName));
  236. if (!is_string_empty($fileData) && $language!="en") {
  237. $titleOld = "Title: ".$this->yellow->language->getText("{$name}Title", "en")."\n";
  238. $titleNew = "Title: ".$this->yellow->language->getText("{$name}Title", $language)."\n";
  239. $fileData = str_replace($titleOld, $titleNew, $fileData);
  240. $textOld = str_replace("\\n", "\n", $this->yellow->language->getText("{$name}Text", "en"));
  241. $textNew = str_replace("\\n", "\n", $this->yellow->language->getText("{$name}Text", $language));
  242. $fileData = str_replace($textOld, $textNew, $fileData);
  243. if (!$this->yellow->toolbox->writeFile($fileName, $fileData)) {
  244. $statusCode = 500;
  245. $this->yellow->page->error($statusCode, "Can't write file '$fileName'!");
  246. }
  247. }
  248. return $statusCode;
  249. }
  250. // Update settings
  251. public function updateSettings($skipInstallation = false) {
  252. $statusCode = 200;
  253. $fileName = $this->yellow->system->get("coreExtensionDirectory").$this->yellow->system->get("coreSystemFile");
  254. if (!$this->yellow->system->save($fileName, $this->getSystemSettings($skipInstallation))) {
  255. $statusCode = 500;
  256. $this->yellow->page->error($statusCode, "Can't write file '$fileName'!");
  257. }
  258. $language = $this->yellow->system->get("language");
  259. $fileName = $this->yellow->system->get("coreExtensionDirectory").$this->yellow->system->get("coreLanguageFile");
  260. $fileData = $this->yellow->toolbox->readFile($fileName);
  261. if (strposu($fileData, "Language:")===false) {
  262. if (!is_string_empty($fileData)) $fileData .= "\n";
  263. $fileData .= "Language: $language\n";
  264. $fileData .= "media/images/photo.jpg: ".$this->yellow->language->getText("installExampleImage", $language)."\n";
  265. if (!$this->yellow->toolbox->writeFile($fileName, $fileData)) {
  266. $statusCode = 500;
  267. $this->yellow->page->error($statusCode, "Can't write file '$fileName'!");
  268. }
  269. }
  270. return $statusCode;
  271. }
  272. // Remove files used by installation
  273. public function removeInstall($log = false) {
  274. $statusCode = 200;
  275. if (function_exists("opcache_reset")) opcache_reset();
  276. $path = $this->yellow->system->get("coreWorkerDirectory");
  277. foreach ($this->yellow->toolbox->getDirectoryEntries($path, "/^install-.*\.bin$/", true, false) as $entry) {
  278. if (!$this->yellow->toolbox->deleteFile($entry)) {
  279. $statusCode = 500;
  280. $this->yellow->page->error($statusCode, "Can't delete file '$entry'!");
  281. }
  282. }
  283. $fileName = $this->yellow->system->get("coreWorkerDirectory")."install.php";
  284. if ($statusCode==200 && !$this->yellow->toolbox->deleteFile($fileName)) {
  285. $statusCode = 500;
  286. $this->yellow->page->error($statusCode, "Can't delete file '$fileName'!");
  287. }
  288. if ($statusCode==200) unset($this->yellow->extension->data["install"]);
  289. $fileName = $this->yellow->system->get("coreExtensionDirectory").$this->yellow->system->get("coreExtensionFile");
  290. $fileData = $this->yellow->toolbox->readFile($fileName);
  291. $fileDataNew = $this->yellow->toolbox->unsetTextSettings($fileData, "extension", "install");
  292. if ($statusCode==200 && !$this->yellow->toolbox->writeFile($fileName, $fileDataNew)) {
  293. $statusCode = 500;
  294. $this->yellow->page->error($statusCode, "Can't write file '$fileName'!");
  295. }
  296. if ($log) $this->yellow->toolbox->log($statusCode==200 ? "info" : "error", "Uninstall extension 'Install ".YellowInstall::VERSION."'");
  297. return $statusCode;
  298. }
  299. // Check web server requirements
  300. public function checkServerRequirements() {
  301. if ($this->yellow->system->get("coreDebugMode")>=1) {
  302. list($name, $version, $os) = $this->yellow->toolbox->detectServerInformation();
  303. echo "YellowInstall::checkServerRequirements for $name $version, $os<br/>\n";
  304. }
  305. if (!$this->checkServerComplete()) $this->yellow->exitFatalError("Datenstrom Yellow requires complete upload!");
  306. if (!$this->checkServerWrite()) $this->yellow->exitFatalError("Datenstrom Yellow requires write access!");
  307. if (!$this->checkServerConfiguration()) $this->yellow->exitFatalError("Datenstrom Yellow requires configuration file!");
  308. if (!$this->checkServerRewrite()) $this->yellow->exitFatalError("Datenstrom Yellow requires rewrite support!");
  309. }
  310. // Check command line requirements
  311. public function checkCommandRequirements() {
  312. if ($this->yellow->system->get("coreDebugMode")>=1) {
  313. list($name, $version, $os) = $this->yellow->toolbox->detectServerInformation();
  314. echo "YellowInstall::checkCommandRequirements for $name $version, $os<br/>\n";
  315. }
  316. if (!$this->checkServerComplete()) $this->yellow->exitFatalError("Datenstrom Yellow requires complete upload!");
  317. if (!$this->checkServerWrite()) $this->yellow->exitFatalError("Datenstrom Yellow requires write access!");
  318. }
  319. // Check web server complete upload
  320. public function checkServerComplete() {
  321. $complete = true;
  322. $fileName = $this->yellow->system->get("coreExtensionDirectory").$this->yellow->system->get("coreExtensionFile");
  323. $fileData = $this->yellow->toolbox->readFile($fileName);
  324. $settings = $this->yellow->toolbox->getTextSettings($fileData, "extension");
  325. $fileNames = array($fileName);
  326. foreach ($settings as $extension=>$block) {
  327. foreach ($block as $key=>$value) {
  328. if (strposu($key, "/")) {
  329. list($entry, $flags) = $this->yellow->toolbox->getTextList($value, ",", 2);
  330. if (!preg_match("/create/i", $flags)) continue;
  331. if (preg_match("/delete/i", $flags)) continue;
  332. if (preg_match("/additional/i", $flags)) continue;
  333. array_push($fileNames, $key);
  334. }
  335. }
  336. }
  337. foreach ($fileNames as $fileName) {
  338. if (!is_file($fileName) || filesize($fileName)==0) {
  339. $complete = false;
  340. if ($this->yellow->system->get("coreDebugMode")>=1) {
  341. echo "YellowInstall::checkServerComplete detected missing file:$fileName<br/>\n";
  342. }
  343. }
  344. }
  345. return $complete;
  346. }
  347. // Check web server write access
  348. public function checkServerWrite() {
  349. $fileName = $this->yellow->system->get("coreExtensionDirectory").$this->yellow->system->get("coreSystemFile");
  350. return $this->yellow->system->save($fileName, array());
  351. }
  352. // Check web server configuration file
  353. public function checkServerConfiguration() {
  354. list($name) = $this->yellow->toolbox->detectServerInformation();
  355. return strtoloweru($name)!="apache" || is_file(".htaccess");
  356. }
  357. // Check web server rewrite support
  358. public function checkServerRewrite() {
  359. $rewrite = true;
  360. if (!$this->isServerBuiltin()) {
  361. $curlHandle = curl_init();
  362. list($scheme, $address, $base) = $this->yellow->lookup->getRequestInformation();
  363. $location = $this->yellow->system->get("coreThemeLocation").$this->yellow->lookup->normaliseName($this->yellow->system->get("theme")).".css";
  364. $url = $this->yellow->lookup->normaliseUrl($scheme, $address, $base, $location);
  365. curl_setopt($curlHandle, CURLOPT_URL, $url);
  366. curl_setopt($curlHandle, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; YellowInstall/".YellowInstall::VERSION).")";
  367. curl_setopt($curlHandle, CURLOPT_NOBODY, 1);
  368. curl_setopt($curlHandle, CURLOPT_CONNECTTIMEOUT, 30);
  369. curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);
  370. curl_exec($curlHandle);
  371. $statusCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
  372. curl_close($curlHandle);
  373. if ($statusCode!=200) {
  374. $rewrite = false;
  375. if ($this->yellow->system->get("coreDebugMode")>=1 && !$rewrite) {
  376. echo "YellowInstall::checkServerRewrite detected failed url:$url<br/>\n";
  377. }
  378. }
  379. }
  380. return $rewrite;
  381. }
  382. // Download available extension files
  383. public function downloadExtensionsAvailable($settings, $extensions) {
  384. $statusCode = 200;
  385. if ($this->yellow->extension->isExisting("update")) {
  386. $path = $this->yellow->system->get("coreWorkerDirectory");
  387. $extensionsNow = 0;
  388. $extensionsTotal = count($extensions);
  389. $curlHandle = curl_init();
  390. foreach ($extensions as $extension) {
  391. echo "\rDownloading available extensions ".$this->getProgressPercent(++$extensionsNow, $extensionsTotal, 5, 95)."%... ";
  392. $fileName = $path."install-".$this->yellow->lookup->normaliseName($extension, true, false, true).".bin";
  393. if (is_file($fileName)) continue;
  394. $url = $settings[$extension]->get("downloadUrl");
  395. curl_setopt($curlHandle, CURLOPT_URL, $this->yellow->extension->get("update")->getExtensionDownloadUrl($url));
  396. curl_setopt($curlHandle, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; YellowInstall/".YellowInstall::VERSION).")";
  397. curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
  398. curl_setopt($curlHandle, CURLOPT_CONNECTTIMEOUT, 30);
  399. $fileData = curl_exec($curlHandle);
  400. $statusCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
  401. $redirectUrl = ($statusCode>=300 && $statusCode<=399) ? curl_getinfo($curlHandle, CURLINFO_REDIRECT_URL) : "";
  402. if ($statusCode==0) {
  403. $statusCode = 450;
  404. $this->yellow->page->error($statusCode, "Can't connect to the update server!");
  405. }
  406. if ($statusCode!=450 && $statusCode!=200) {
  407. $statusCode = 500;
  408. $this->yellow->page->error($statusCode, "Can't download file '$url'!");
  409. }
  410. if ($statusCode==200 && !$this->yellow->toolbox->writeFile($fileName, $fileData)) {
  411. $statusCode = 500;
  412. $this->yellow->page->error($statusCode, "Can't write file '$fileName'!");
  413. }
  414. if ($this->yellow->system->get("coreDebugMode")>=2 && !is_string_empty($redirectUrl)) {
  415. echo "YellowInstall::downloadExtensionsAvailable redirected to url:$redirectUrl<br/>\n";
  416. }
  417. if ($this->yellow->system->get("coreDebugMode")>=2) {
  418. echo "YellowInstall::downloadExtensionsAvailable status:$statusCode url:$url<br/>\n";
  419. }
  420. if ($statusCode!=200) break;
  421. }
  422. curl_close($curlHandle);
  423. echo "\rDownloading available extensions 100%... done\n";
  424. }
  425. return $statusCode;
  426. }
  427. // Return available extensions required
  428. public function getAvailableExtensionsRequired($settings, $option) {
  429. $extensions = array();
  430. if ($option=="medium") {
  431. $text = "help highlight search toc";
  432. $extensions = array_unique(array_filter($this->yellow->toolbox->getTextArguments($text), "strlen"));
  433. } elseif ($option=="large") {
  434. foreach ($settings as $key=>$value) {
  435. if (preg_match("/language/i", $value->get("tag"))) continue;
  436. array_push($extensions, strtoloweru($key));
  437. }
  438. }
  439. return $extensions;
  440. }
  441. // Return language extensions required
  442. public function getLanguageExtensionsRequired($fileData, $option) {
  443. $extensions = array();
  444. $languages = array();
  445. foreach ($this->yellow->toolbox->getTextLines($fileData) as $line) {
  446. if (preg_match("/^\s*(.*?)\s*:\s*(.*?)\s*$/", $line, $matches)) {
  447. if (!is_string_empty($matches[1]) && !is_string_empty($matches[2]) && strposu($matches[1], "/")) {
  448. $extension = basename($matches[1]);
  449. $extension = $this->yellow->lookup->normaliseName($extension, true, true);
  450. list($entry, $flags) = $this->yellow->toolbox->getTextList($matches[2], ",", 2);
  451. $arguments = preg_split("/\s*,\s*/", trim($flags));
  452. $language = array_pop($arguments);
  453. if (preg_match("/^(.*)\.php$/", basename($entry))) {
  454. $languages[$language] = $extension;
  455. }
  456. }
  457. }
  458. }
  459. if ($option=="large") {
  460. foreach ($languages as $language=>$extension) {
  461. array_push($extensions, $extension);
  462. }
  463. } else {
  464. foreach ($this->getSystemLanguages("en, de, sv") as $language) {
  465. if (isset($languages[$language])) array_push($extensions, $languages[$language]);
  466. }
  467. $extensions = array_slice($extensions, 0, 3);
  468. }
  469. return $extensions;
  470. }
  471. // Return extensions installed
  472. public function getExtensionsCount() {
  473. $fileNameCurrent = $this->yellow->system->get("coreExtensionDirectory").$this->yellow->system->get("coreExtensionFile");
  474. $fileData = $this->yellow->toolbox->readFile($fileNameCurrent);
  475. $settings = $this->yellow->toolbox->getTextSettings($fileData, "extension");
  476. return count($settings);
  477. }
  478. // Return system languages
  479. public function getSystemLanguages($languagesDefault) {
  480. $languages = array();
  481. foreach (preg_split("/\s*,\s*/", $this->yellow->toolbox->getServer("HTTP_ACCEPT_LANGUAGE")) as $string) {
  482. list($language, $dummy) = $this->yellow->toolbox->getTextList($string, ";", 2);
  483. if (!is_string_empty($language)) array_push($languages, $language);
  484. }
  485. foreach (preg_split("/\s*,\s*/", $languagesDefault) as $language) {
  486. if (!is_string_empty($language)) array_push($languages, $language);
  487. }
  488. return array_unique($languages);
  489. }
  490. // Return system settings
  491. public function getSystemSettings($skipInstallation) {
  492. $settings = array();
  493. foreach ($_REQUEST as $key=>$value) {
  494. if (!$this->yellow->system->isExisting($key)) continue;
  495. if ($key=="password" || $key=="status") continue;
  496. $settings[$key] = trim($value);
  497. }
  498. if ($this->yellow->system->get("sitename")=="Datenstrom Yellow") $settings["sitename"] = $this->yellow->toolbox->detectServerSitename();
  499. if ($this->yellow->system->get("generateStaticUrl")=="auto" && getenv("URL")!==false) $settings["generateStaticUrl"] = getenv("URL");
  500. if ($this->yellow->system->get("generateStaticUrl")=="auto" && $skipInstallation) $settings["generateStaticUrl"] = "http://localhost:8000/";
  501. if ($this->yellow->system->get("coreTimezone")=="UTC") $settings["coreTimezone"] = $this->yellow->toolbox->detectServerTimezone();
  502. if ($this->yellow->system->get("updateEventPending")=="none") {
  503. $settings["updateEventPending"] = "website/install";
  504. } else {
  505. $themeStandard = ",".$this->yellow->system->get("theme")."/install";
  506. $settings["updateEventPending"] = $this->yellow->system->get("updateEventPending").$themeStandard;
  507. }
  508. $settings["updateCurrentRelease"] = YellowCore::RELEASE;
  509. return $settings;
  510. }
  511. // Return raw data for install page
  512. public function getRawDataInstall() {
  513. $languages = $this->yellow->system->getAvailable("language");
  514. $language = $this->yellow->toolbox->detectBrowserLanguage($languages, $this->yellow->system->get("language"));
  515. $this->yellow->language->set($language);
  516. $rawData = "---\nTitle:".$this->yellow->language->getText("installTitle")."\nLanguage:$language\nNavigation:navigation\nHeader:none\nFooter:none\nSidebar:none\n---\n";
  517. $rawData .= "<form class=\"install-form\" action=\"".$this->yellow->page->getLocation(true)."\" method=\"post\">\n";
  518. $rawData .= "<p><label for=\"author\">".$this->yellow->language->getText("editSignupName")."</label><br /><input class=\"form-control\" type=\"text\" maxlength=\"64\" name=\"author\" id=\"author\" value=\"\"></p>\n";
  519. $rawData .= "<p><label for=\"email\">".$this->yellow->language->getText("editSignupEmail")."</label><br /><input class=\"form-control\" type=\"text\" maxlength=\"64\" name=\"email\" id=\"email\" value=\"\"></p>\n";
  520. $rawData .= "<p><label for=\"password\">".$this->yellow->language->getText("editSignupPassword")."</label><br /><input class=\"form-control\" type=\"password\" maxlength=\"64\" name=\"password\" id=\"password\" value=\"\"></p>\n";
  521. $rawData .= "<p>".$this->yellow->language->getText("installLanguage")."</p>\n<p>";
  522. foreach ($languages as $language) {
  523. $checked = $language==$this->yellow->language->language ? " checked=\"checked\"" : "";
  524. $rawData .= "<label for=\"{$language}-language\"><input type=\"radio\" name=\"language\" id=\"{$language}-language\" value=\"$language\"$checked> ".$this->yellow->language->getTextHtml("languageDescription", $language)."</label><br />";
  525. }
  526. $rawData .= "</p>\n";
  527. $rawData .= "<p>".$this->yellow->language->getText("installExtension")."</p>\n<p>";
  528. foreach (array("website", "wiki", "blog") as $extension) {
  529. $checked = $extension=="website" ? " checked=\"checked\"" : "";
  530. $rawData .= "<label for=\"{$extension}-extension\"><input type=\"radio\" name=\"extension\" id=\"{$extension}-extension\" value=\"$extension\"$checked> ".$this->yellow->language->getTextHtml("installExtension".ucfirst($extension))."</label><br />";
  531. }
  532. $rawData .= "</p>\n";
  533. $rawData .= "<input class=\"btn\" type=\"submit\" value=\"".$this->yellow->language->getText("installButton")."\" />\n";
  534. $rawData .= "<input type=\"hidden\" name=\"status\" value=\"install\" />\n";
  535. $rawData .= "</form>\n";
  536. return $rawData;
  537. }
  538. // Return progress in percent
  539. public function getProgressPercent($now, $total, $increments, $max) {
  540. $max = intval($max/$increments) * $increments;
  541. $percent = intval(($max/$total) * $now);
  542. if ($increments>1) $percent = intval($percent/$increments) * $increments;
  543. return min($max, $percent);
  544. }
  545. // Check if running built-in web server
  546. public function isServerBuiltin() {
  547. list($name) = $this->yellow->toolbox->detectServerInformation();
  548. return strtoloweru($name)=="built-in";
  549. }
  550. }