PageRenderTime 59ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/upgrade.php

http://github.com/vito/chyrp
PHP | 1296 lines | 841 code | 211 blank | 244 comment | 108 complexity | 56a4000eed567640dd7353fc91765627 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * File: Upgrader
  4. * A task-based general-purpose Chyrp upgrader.
  5. *
  6. * Performs upgrade functions based on individual tasks, and checks whether or not they need to be done.
  7. *
  8. * Version-agnostic. Completely safe to be run at all times, by anyone.
  9. */
  10. header("Content-type: text/html; charset=UTF-8");
  11. define('DEBUG', true);
  12. define('CACHE_TWIG', false);
  13. define('JAVASCRIPT', false);
  14. define('ADMIN', false);
  15. define('AJAX', false);
  16. define('XML_RPC', false);
  17. define('TRACKBACK', false);
  18. define('UPGRADING', true);
  19. define('INSTALLING', false);
  20. define('TESTER', true);
  21. define('INDEX', false);
  22. define('MAIN_DIR', dirname(__FILE__));
  23. define('INCLUDES_DIR', dirname(__FILE__)."/includes");
  24. define('MODULES_DIR', MAIN_DIR."/modules");
  25. define('FEATHERS_DIR', MAIN_DIR."/feathers");
  26. define('THEMES_DIR', MAIN_DIR."/themes");
  27. if (!AJAX and
  28. extension_loaded("zlib") and
  29. !ini_get("zlib.output_compression") and
  30. isset($_SERVER['HTTP_ACCEPT_ENCODING']) and
  31. substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], "gzip") and
  32. USE_ZLIB) {
  33. ob_start("ob_gzhandler");
  34. header("Content-Encoding: gzip");
  35. } else
  36. ob_start();
  37. /**
  38. * Function: config_file
  39. * Returns what config file their install is set up for.
  40. */
  41. function config_file() {
  42. if (file_exists(INCLUDES_DIR."/config.yaml.php"))
  43. return INCLUDES_DIR."/config.yaml.php";
  44. if (file_exists(INCLUDES_DIR."/config.yml.php"))
  45. return INCLUDES_DIR."/config.yml.php";
  46. if (file_exists(INCLUDES_DIR."/config.php"))
  47. return INCLUDES_DIR."/config.php";
  48. exit("Config file not found.");
  49. }
  50. /**
  51. * Function: database_file
  52. * Returns what database config file their install is set up for.
  53. */
  54. function database_file() {
  55. if (file_exists(INCLUDES_DIR."/database.yaml.php"))
  56. return INCLUDES_DIR."/database.yaml.php";
  57. if (file_exists(INCLUDES_DIR."/database.yml.php"))
  58. return INCLUDES_DIR."/database.yml.php";
  59. if (file_exists(INCLUDES_DIR."/database.php"))
  60. return INCLUDES_DIR."/database.php";
  61. return false;
  62. }
  63. /**
  64. * Function: using_yaml
  65. * Are they using YAML config storage?
  66. */
  67. function using_yaml() {
  68. return (basename(config_file()) != "config.php" and basename(database_file()) != "database.php") or !database_file();
  69. }
  70. # Evaluate the code in their config files, but with the classes renamed, so we can safely retrieve the values.
  71. if (!using_yaml()) {
  72. eval(str_replace(array("<?php", "?>", "Config"),
  73. array("", "", "OldConfig"),
  74. file_get_contents(config_file())));
  75. if (database_file())
  76. eval(str_replace(array("<?php", "?>", "SQL"),
  77. array("", "", "OldSQL"),
  78. file_get_contents(database_file())));
  79. }
  80. # File: Helpers
  81. # Various functions used throughout Chyrp's code.
  82. require_once INCLUDES_DIR."/helpers.php";
  83. # File: Gettext
  84. # Gettext library.
  85. require_once INCLUDES_DIR."/lib/gettext/gettext.php";
  86. # File: Streams
  87. # Streams library.
  88. require_once INCLUDES_DIR."/lib/gettext/streams.php";
  89. # File: YAML
  90. # Horde YAML parsing library.
  91. require_once INCLUDES_DIR."/lib/YAML.php";
  92. # File: SQL
  93. # See Also:
  94. # <SQL>
  95. require INCLUDES_DIR."/class/SQL.php";
  96. /**
  97. * Class: Config
  98. * Handles writing to whichever config file they're using.
  99. */
  100. class Config {
  101. # Array: $yaml
  102. # Stores all of the YAML data.
  103. static $yaml = array("config" => array(),
  104. "database" => array());
  105. /**
  106. * Function: get
  107. * Returns a config setting.
  108. *
  109. * Parameters:
  110. * $setting - The setting to return.
  111. */
  112. static function get($setting) {
  113. return (isset(Config::$yaml["config"][$setting])) ? Config::$yaml["config"][$setting] : false ;
  114. }
  115. /**
  116. * Function: set
  117. * Sets a config setting.
  118. *
  119. * Parameters:
  120. * $setting - The config setting to set.
  121. * $value - The value for the setting.
  122. * $message - The message to display with test().
  123. */
  124. static function set($setting, $value, $message = null) {
  125. if (self::get($setting) == $value) return;
  126. if (!isset($message))
  127. $message = _f("Setting %s to %s...", array($setting, normalize(print_r($value, true))));
  128. Config::$yaml["config"][$setting] = $value;
  129. $protection = "<?php header(\"Status: 403\"); exit(\"Access denied.\"); ?>\n";
  130. $dump = $protection.YAML::dump(Config::$yaml["config"]);
  131. echo $message.test(@file_put_contents(INCLUDES_DIR."/config.yaml.php", $dump));
  132. }
  133. /**
  134. * Function: check
  135. * Goes a config exist?
  136. *
  137. * Parameters:
  138. * $setting - Name of the config to check.
  139. */
  140. static function check($setting) {
  141. return (isset(Config::$yaml["config"][$setting]));
  142. }
  143. /**
  144. * Function: fallback
  145. * Sets a config setting to $value if it does not exist.
  146. *
  147. * Parameters:
  148. * $setting - The config setting to set.
  149. * $value - The value for the setting.
  150. * $message - The message to display with test().
  151. */
  152. static function fallback($setting, $value, $message = null) {
  153. if (!isset($message))
  154. $message = _f("Adding %s setting...", array($setting));
  155. if (!self::check($setting))
  156. echo self::set($setting, $value, $message);
  157. }
  158. /**
  159. * Function: remove
  160. * Removes a setting if it exists.
  161. *
  162. * Parameters:
  163. * $setting - The setting to remove.
  164. */
  165. static function remove($setting) {
  166. if (!self::check($setting)) return;
  167. unset(Config::$yaml["config"][$setting]);
  168. $protection = "<?php header(\"Status: 403\"); exit(\"Access denied.\"); ?>\n";
  169. $dump = $protection.YAML::dump(Config::$yaml["config"]);
  170. echo _f("Removing %s setting...", array($setting)).
  171. test(@file_put_contents(INCLUDES_DIR."/config.yaml.php", $dump));
  172. }
  173. }
  174. if (using_yaml()) {
  175. Config::$yaml["config"] = YAML::load(preg_replace("/<\?php(.+)\?>\n?/s", "", file_get_contents(config_file())));
  176. if (database_file())
  177. Config::$yaml["database"] = YAML::load(preg_replace("/<\?php(.+)\?>\n?/s",
  178. "",
  179. file_get_contents(database_file())));
  180. else
  181. Config::$yaml["database"] = oneof(@Config::$yaml["config"]["sql"], array());
  182. } else {
  183. # $config and $sql here are loaded from the eval()'s above.
  184. foreach ($config as $name => $val)
  185. Config::$yaml["config"][$name] = $val;
  186. foreach ($sql as $name => $val)
  187. Config::$yaml["database"][$name] = $val;
  188. }
  189. load_translator("chyrp", INCLUDES_DIR."/locale/".Config::get("locale").".mo");
  190. /**
  191. * Function: test
  192. * Attempts to perform a task, and displays a "success" or "failed" message determined by the outcome.
  193. *
  194. * Parameters:
  195. * $try - The task to attempt. Should return something that evaluates to true or false.
  196. * $message - Message to display for the test.
  197. */
  198. function test($try, $message = "") {
  199. $sql = SQL::current();
  200. if (!empty($sql->error)) {
  201. $message.= "\n".$sql->error."\n\n";
  202. $sql->error = "";
  203. }
  204. $info = $message;
  205. if ($try)
  206. return " <span class=\"yay\">".__("success!")."</span>\n";
  207. else
  208. return " <span class=\"boo\">".__("failed!")."</span>\n".$info;
  209. }
  210. #---------------------------------------------
  211. # Upgrading Actions
  212. #---------------------------------------------
  213. /**
  214. * Function: fix_htaccess
  215. * Repairs their .htaccess file.
  216. */
  217. function fix_htaccess() {
  218. $url = "http://".$_SERVER['HTTP_HOST'].str_replace("/upgrade.php", "", $_SERVER['REQUEST_URI']);
  219. $index = (parse_url($url, PHP_URL_PATH)) ? "/".trim(parse_url($url, PHP_URL_PATH), "/")."/" : "/" ;
  220. $path = preg_quote($index, "/");
  221. $htaccess_has_chyrp = (file_exists(MAIN_DIR."/.htaccess") and preg_match("/<IfModule mod_rewrite\.c>\n([\s]*)RewriteEngine On\n([\s]*)RewriteBase {$path}\n([\s]*)RewriteCond %\{REQUEST_FILENAME\} !-f\n([\s]*)RewriteCond %\{REQUEST_FILENAME\} !-d\n([\s]*)RewriteRule (\^\.\+\\$|\!\\.\(gif\|jpg\|png\|css\)) index\.php \[L\]\n([\s]*)RewriteRule \^\.\+\\\.twig\\$ index\.php \[L\]\n([\s]*)<\/IfModule>/", file_get_contents(MAIN_DIR."/.htaccess")));
  222. if ($htaccess_has_chyrp)
  223. return;
  224. $htaccess = "<IfModule mod_rewrite.c>\nRewriteEngine On\nRewriteBase {$index}\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule ^.+\$ index.php [L]\nRewriteRule ^.+\\.twig\$ index.php [L]\n</IfModule>";
  225. if (!file_exists(MAIN_DIR."/.htaccess"))
  226. echo __("Generating .htaccess file...").
  227. test(@file_put_contents(MAIN_DIR."/.htaccess", $htaccess), __("Try creating the file and/or CHMODding it to 777 temporarily."));
  228. else
  229. echo __("Appending to .htaccess file...").
  230. test(@file_put_contents(MAIN_DIR."/.htaccess", "\n\n".$htaccess, FILE_APPEND), __("Try creating the file and/or CHMODding it to 777 temporarily."));
  231. }
  232. /**
  233. * Function: tweets_to_posts
  234. * Enacts the "tweet" to "post" rename.
  235. *
  236. * Versions: 1.0.2 => 1.0.3
  237. */
  238. function tweets_to_posts() {
  239. if (SQL::current()->query("SELECT * FROM __tweets"))
  240. echo __("Renaming tweets table to posts...").
  241. test(SQL::current()->query("RENAME TABLE __tweets TO __posts"));
  242. if (SQL::current()->query("SELECT add_tweet FROM __groups"))
  243. echo __("Renaming add_tweet permission to add_post...").
  244. test(SQL::current()->query("ALTER TABLE __groups CHANGE add_tweet add_post TINYINT(1) NOT NULL DEFAULT '0'"));
  245. if (SQL::current()->query("SELECT edit_tweet FROM __groups"))
  246. echo __("Renaming edit_tweet permission to edit_post...").
  247. test(SQL::current()->query("ALTER TABLE __groups CHANGE edit_tweet edit_post TINYINT(1) NOT NULL DEFAULT '0'"));
  248. if (SQL::current()->query("SELECT delete_tweet FROM __groups"))
  249. echo __("Renaming delete_tweet permission to delete_post...").
  250. test(SQL::current()->query("ALTER TABLE __groups CHANGE delete_tweet delete_post TINYINT(1) NOT NULL DEFAULT '0'"));
  251. if (Config::check("tweets_per_page")) {
  252. Config::fallback("posts_per_page", Config::get("tweets_per_page"));
  253. Config::remove("tweets_per_page");
  254. }
  255. if (Config::check("tweet_url")) {
  256. Config::fallback("post_url", Config::get("tweet_url"));
  257. Config::remove("tweet_url");
  258. }
  259. if (Config::check("rss_tweets")) {
  260. Config::fallback("rss_posts", Config::get("rss_posts"));
  261. Config::remove("rss_tweets");
  262. }
  263. }
  264. /**
  265. * Function: pages_parent_id_column
  266. * Adds the @parent_id@ column to the "pages" table.
  267. *
  268. * Versions: 1.0.3 => 1.0.4
  269. */
  270. function pages_parent_id_column() {
  271. if (SQL::current()->query("SELECT parent_id FROM __pages"))
  272. return;
  273. echo __("Adding parent_id column to pages table...").
  274. test(SQL::current()->query("ALTER TABLE __pages ADD parent_id INT(11) NOT NULL DEFAULT '0' AFTER user_id"));
  275. }
  276. /**
  277. * Function: pages_list_order_column
  278. * Adds the @list_order@ column to the "pages" table.
  279. *
  280. * Versions: 1.0.4 => 1.1.0
  281. */
  282. function pages_list_order_column() {
  283. if (SQL::current()->query("SELECT list_order FROM __pages"))
  284. return;
  285. echo __("Adding list_order column to pages table...").
  286. test(SQL::current()->query("ALTER TABLE __pages ADD list_order INT(11) NOT NULL DEFAULT '0' AFTER show_in_list"));
  287. }
  288. /**
  289. * Function: remove_beginning_slash_from_post_url
  290. * Removes the slash at the beginning of the post URL setting.
  291. */
  292. function remove_beginning_slash_from_post_url() {
  293. if (substr(Config::get("post_url"), 0, 1) == "/")
  294. Config::set("post_url", ltrim(Config::get("post_url"), "/"));
  295. }
  296. /**
  297. * Function: move_yml_yaml
  298. * Renames config.yml.php to config.yaml.php.
  299. *
  300. * Versions: 1.1.2 => 1.1.3
  301. */
  302. function move_yml_yaml() {
  303. if (file_exists(INCLUDES_DIR."/config.yml.php"))
  304. echo __("Moving /includes/config.yml.php to /includes/config.yaml.php...").
  305. test(@rename(INCLUDES_DIR."/config.yml.php", INCLUDES_DIR."/config.yaml.php"), __("Try CHMODding the file to 777."));
  306. }
  307. /**
  308. * Function: update_protection
  309. * Updates the PHP protection code in the config file.
  310. */
  311. function update_protection() {
  312. if (!file_exists(INCLUDES_DIR."/config.yaml.php") or
  313. substr_count(file_get_contents(INCLUDES_DIR."/config.yaml.php"),
  314. "<?php header(\"Status: 403\"); exit(\"Access denied.\"); ?>"))
  315. return;
  316. $contents = file_get_contents(INCLUDES_DIR."/config.yaml.php");
  317. $new_error = preg_replace("/<\?php (.+) \?>/",
  318. "<?php header(\"Status: 403\"); exit(\"Access denied.\"); ?>",
  319. $contents);
  320. echo __("Updating protection code in config.yaml.php...").
  321. test(@file_put_contents(INCLUDES_DIR."/config.yaml.php", $new_error), __("Try CHMODding the file to 777."));
  322. }
  323. /**
  324. * Function: theme_default_to_stardust
  325. * Changes their theme from "default" to "stardust", or leaves it alone if they're not using "default".
  326. *
  327. * Versions: 1.1.3.2 => 2.0
  328. */
  329. function theme_default_to_stardust() {
  330. if (Config::get("theme") != "default") return;
  331. Config::set("theme", "stardust");
  332. }
  333. /**
  334. * Function: default_db_adapter_to_mysql
  335. * Adds an "adapter" SQL setting if it doesn't exist, and sets it to "mysql".
  336. *
  337. * Versions: 1.1.3.2 => 2.0
  338. */
  339. function default_db_adapter_to_mysql() {
  340. $sql = SQL::current();
  341. if (isset($sql->adapter)) return;
  342. $sql->set("adapter", "mysql");
  343. }
  344. /**
  345. * Function: move_upload
  346. * Renames the "upload" directory to "uploads".
  347. */
  348. function move_upload() {
  349. if (file_exists(MAIN_DIR."/upload") and !file_exists(MAIN_DIR."/uploads"))
  350. echo __("Renaming /upload directory to /uploads...").test(@rename(MAIN_DIR."/upload", MAIN_DIR."/uploads"), __("Try CHMODding the directory to 777."));
  351. }
  352. /**
  353. * Function: make_posts_xml
  354. * Updates all of the post XML data to well-formed non-CDATAized XML.
  355. *
  356. * Versions: 1.1.3.2 => 2.0
  357. */
  358. function make_posts_safe() {
  359. if (!$posts = SQL::current()->query("SELECT * FROM __posts"))
  360. return;
  361. if (!SQL::current()->query("SELECT xml FROM __posts"))
  362. return;
  363. function clean_xml(&$input) {
  364. $input = trim($input);
  365. }
  366. while ($post = $posts->fetchObject()) {
  367. if (!substr_count($post->xml, "<![CDATA["))
  368. continue;
  369. $post->xml = str_replace("<![CDATA[]]>", "", $post->xml);
  370. $xml = simplexml_load_string($post->xml, "SimpleXMLElement", LIBXML_NOCDATA);
  371. $parse = xml2arr($xml);
  372. array_walk_recursive($parse, "clean_xml");
  373. $new_xml = new SimpleXMLElement("<post></post>");
  374. arr2xml($new_xml, $parse);
  375. echo _f("Sanitizing XML data of post #%d...", array($post->id)).
  376. test(SQL::current()->update("posts",
  377. array("id" => $post->id),
  378. array("xml" => $new_xml->asXML())));
  379. }
  380. }
  381. /**
  382. * Function: rss_posts_to_feed_items
  383. * Rename the feed items setting.
  384. *
  385. * Versions: 1.1.3.2 => 2.0
  386. */
  387. function rss_posts_to_feed_items() {
  388. if (!Config::check("rss_posts"))
  389. return;
  390. Config::fallback("feed_items", Config::get("rss_posts"));
  391. Config::remove("rss_posts");
  392. }
  393. /**
  394. * Function: update_groups_to_yaml
  395. * Updates the groups to use YAML-based permissions instead of table columns.
  396. *
  397. * Versions: 1.1.3.2 => 2.0
  398. */
  399. function update_groups_to_yaml() {
  400. if (!SQL::current()->query("SELECT view_site FROM __groups")) return;
  401. $get_groups = SQL::current()->query("SELECT * FROM __groups");
  402. echo __("Backing up current groups table...").test($get_groups);
  403. if (!$get_groups) return;
  404. $groups = array();
  405. # Generate an array of groups, name => permissions.
  406. while ($group = $get_groups->fetchObject()) {
  407. $groups[$group->name] = array("permissions" => array());
  408. foreach ($group as $key => $val)
  409. if ($key != "name" and $key != "id" and $val)
  410. $groups[$group->name]["permissions"][] = $key;
  411. elseif ($key == "id")
  412. $groups[$group->name]["id"] = $val;
  413. }
  414. # Convert permissions array to a YAML dump.
  415. foreach ($groups as $key => &$val)
  416. $val["permissions"] = YAML::dump($val["permissions"]);
  417. $drop_groups = SQL::current()->query("DROP TABLE __groups");
  418. echo __("Dropping old groups table...").test($drop_groups);
  419. if (!$drop_groups) return;
  420. $groups_table = SQL::current()->query("CREATE TABLE __groups (
  421. id INTEGER PRIMARY KEY AUTO_INCREMENT,
  422. name VARCHAR(100) DEFAULT '',
  423. permissions LONGTEXT,
  424. UNIQUE (name)
  425. ) DEFAULT CHARSET=utf8");
  426. echo __("Creating new groups table...").test($groups_table);
  427. if (!$groups_table) return;
  428. foreach($groups as $name => $values)
  429. echo _f("Restoring group \"%s\"...", array($name)).
  430. test(SQL::current()->insert("groups",
  431. array("id" => $values["id"],
  432. "name" => $name,
  433. "permissions" => $values["permissions"])));
  434. }
  435. /**
  436. * Function: add_permissions_table
  437. * Creates the "permissions" table and fills it in with the default set.
  438. *
  439. * Versions: 1.1.3.2 => 2.0
  440. */
  441. function add_permissions_table() {
  442. if (SQL::current()->query("SELECT * FROM __permissions")) return;
  443. $permissions_table = SQL::current()->query("CREATE TABLE __permissions (
  444. id VARCHAR(100) DEFAULT '' PRIMARY KEY,
  445. name VARCHAR(100) DEFAULT ''
  446. ) DEFAULT CHARSET=utf8");
  447. echo __("Creating new permissions table...").test($permissions_table);
  448. if (!$permissions_table) return;
  449. $permissions = array("change_settings" => "Change Settings",
  450. "toggle_extensions" => "Toggle Extensions",
  451. "view_site" => "View Site",
  452. "view_private" => "View Private Posts",
  453. "view_draft" => "View Drafts",
  454. "view_own_draft" => "View Own Drafts",
  455. "add_post" => "Add Posts",
  456. "add_draft" => "Add Drafts",
  457. "edit_post" => "Edit Posts",
  458. "edit_draft" => "Edit Drafts",
  459. "edit_own_post" => "Edit Own Posts",
  460. "edit_own_draft" => "Edit Own Drafts",
  461. "delete_post" => "Delete Posts",
  462. "delete_draft" => "Delete Drafts",
  463. "delete_own_post" => "Delete Own Posts",
  464. "delete_own_draft" => "Delete Own Drafts",
  465. "add_page" => "Add Pages",
  466. "edit_page" => "Edit Pages",
  467. "delete_page" => "Delete Pages",
  468. "add_user" => "Add Users",
  469. "edit_user" => "Edit Users",
  470. "delete_user" => "Delete Users",
  471. "add_group" => "Add Groups",
  472. "edit_group" => "Edit Groups",
  473. "delete_group" => "Delete Groups");
  474. foreach ($permissions as $id => $name)
  475. echo _f("Inserting permission \"%s\"...", array($name)).
  476. test(SQL::current()->insert("permissions",
  477. array("id" => $id,
  478. "name" => $name)));
  479. }
  480. /**
  481. * Function: add_sessions_table
  482. * Creates the "sessions" table.
  483. *
  484. * Versions: 1.1.3.2 => 2.0
  485. */
  486. function add_sessions_table() {
  487. if (SQL::current()->query("SELECT * FROM __sessions")) return;
  488. echo __("Creating `sessions` table...").
  489. test(SQL::current()->query("CREATE TABLE __sessions (
  490. id VARCHAR(40) DEFAULT '',
  491. data LONGTEXT,
  492. user_id INTEGER DEFAULT '0',
  493. created_at DATETIME DEFAULT NULL,
  494. updated_at DATETIME DEFAULT NULL,
  495. PRIMARY KEY (id)
  496. ) DEFAULT CHARSET=utf8") or die(mysql_error()));
  497. }
  498. /**
  499. * Function: update_permissions_table
  500. * Updates the "permissions" table from ## (id) => foo_bar (name) to foo_bar (id) => Foo Bar (name).
  501. *
  502. * Versions: 2.0b => 2.0
  503. */
  504. function update_permissions_table() {
  505. # If there are any non-numeric IDs in the permissions database, assume this is already done.
  506. $check = SQL::current()->query("SELECT * FROM __permissions");
  507. while ($row = $check->fetchObject())
  508. if (!is_numeric($row->id))
  509. return;
  510. $permissions_backup = array();
  511. $get_permissions = SQL::current()->query("SELECT * FROM __permissions");
  512. echo __("Backing up current permissions table...").test($get_permissions);
  513. if (!$get_permissions) return;
  514. while ($permission = $get_permissions->fetchObject())
  515. $permissions_backup[] = $permission->name;
  516. $drop_permissions = SQL::current()->query("DROP TABLE __permissions");
  517. echo __("Dropping old permissions table...").test($drop_permissions);
  518. if (!$drop_permissions) return;
  519. echo __("Creating new permissions table...").
  520. test(SQL::current()->query("CREATE TABLE IF NOT EXISTS __permissions (
  521. id VARCHAR(100) DEFAULT '' PRIMARY KEY,
  522. name VARCHAR(100) DEFAULT ''
  523. ) DEFAULT CHARSET=utf8"));
  524. $permissions = array("change_settings" => "Change Settings",
  525. "toggle_extensions" => "Toggle Extensions",
  526. "view_site" => "View Site",
  527. "view_private" => "View Private Posts",
  528. "view_draft" => "View Drafts",
  529. "view_own_draft" => "View Own Drafts",
  530. "add_post" => "Add Posts",
  531. "add_draft" => "Add Drafts",
  532. "edit_post" => "Edit Posts",
  533. "edit_draft" => "Edit Drafts",
  534. "edit_own_post" => "Edit Own Posts",
  535. "edit_own_draft" => "Edit Own Drafts",
  536. "delete_post" => "Delete Posts",
  537. "delete_draft" => "Delete Drafts",
  538. "delete_own_post" => "Delete Own Posts",
  539. "delete_own_draft" => "Delete Own Drafts",
  540. "add_page" => "Add Pages",
  541. "edit_page" => "Edit Pages",
  542. "delete_page" => "Delete Pages",
  543. "add_user" => "Add Users",
  544. "edit_user" => "Edit Users",
  545. "delete_user" => "Delete Users",
  546. "add_group" => "Add Groups",
  547. "edit_group" => "Edit Groups",
  548. "delete_group" => "Delete Groups");
  549. foreach ($permissions_backup as $id) {
  550. $name = isset($permissions[$id]) ? $permissions[$id] : camelize($id, true);
  551. echo _f("Restoring permission \"%s\"...", array($name)).
  552. test(SQL::current()->insert("permissions",
  553. array("id" => $id,
  554. "name" => $name)));
  555. }
  556. }
  557. /**
  558. * Function: update_custom_routes
  559. * Updates the custom routes to be path => action instead of # => path.
  560. *
  561. * Versions: 2.0rc1 => 2.0rc2
  562. */
  563. function update_custom_routes() {
  564. $custom_routes = Config::get("routes");
  565. if (empty($custom_routes)) return;
  566. $new_routes = array();
  567. foreach ($custom_routes as $key => $route) {
  568. if (!is_int($key))
  569. return;
  570. $split = array_filter(explode("/", $route));
  571. if (!isset($split[0]))
  572. return;
  573. echo _f("Updating custom route %s to new format...", array($route)).
  574. test(isset($split[0]) and $new_routes[$route] = $split[0]);
  575. }
  576. Config::set("routes", $new_routes, "Setting new custom routes configuration...");
  577. }
  578. /**
  579. * Function: remove_database_config_file
  580. * Removes the database.yaml.php file, which is merged into config.yaml.php.
  581. *
  582. * Versions: 2.0rc1 => 2.0rc2
  583. */
  584. function remove_database_config_file() {
  585. if (file_exists(INCLUDES_DIR."/database.yaml.php"))
  586. echo __("Removing database.yaml.php file...").
  587. test(@unlink(INCLUDES_DIR."/database.yaml.php"), __("Try deleting it manually."));
  588. }
  589. /**
  590. * Function: rename_database_setting_to_sql
  591. * Renames the "database" config setting to "sql".
  592. */
  593. function rename_database_setting_to_sql() {
  594. if (Config::check("sql")) return;
  595. Config::set("sql", Config::get("database"));
  596. Config::remove("database");
  597. }
  598. /**
  599. * Function: update_post_status_column
  600. * Updates the @status@ column on the "posts" table to be a generic varchar field instead of enum.
  601. *
  602. * Versions: 2.0rc1 => 2.0rc2
  603. */
  604. function update_post_status_column() {
  605. $sql = SQL::current();
  606. if (!$column = $sql->query("SHOW COLUMNS FROM __posts WHERE Field = 'status'"))
  607. return;
  608. if ($column->fetchObject()->Type == "varchar(32)")
  609. return;
  610. echo __("Updating `status` column on `posts` table...")."\n";
  611. echo " - ".__("Backing up `posts` table...").
  612. test($backup = $sql->select("posts"));
  613. if (!$backup)
  614. return;
  615. $backups = $backup->fetchAll();
  616. echo " - ".__("Dropping `posts` table...").
  617. test($drop = $sql->query("DROP TABLE __posts"));
  618. if (!$drop)
  619. return;
  620. echo " - ".__("Creating `posts` table...").
  621. test($create = $sql->query("CREATE TABLE IF NOT EXISTS __posts (
  622. id INTEGER PRIMARY KEY AUTO_INCREMENT,
  623. xml LONGTEXT,
  624. feather VARCHAR(32) DEFAULT '',
  625. clean VARCHAR(128) DEFAULT '',
  626. url VARCHAR(128) DEFAULT '',
  627. pinned TINYINT(1) DEFAULT 0,
  628. status VARCHAR(32) DEFAULT 'public',
  629. user_id INTEGER DEFAULT 0,
  630. created_at DATETIME DEFAULT NULL,
  631. updated_at DATETIME DEFAULT NULL
  632. ) DEFAULT CHARSET=utf8"));
  633. if (!$create) {
  634. echo " -".test(false, _f("Backup written to %s.", array("./_posts.bak.txt")));
  635. return file_put_contents("./_posts.bak.txt", var_export($backups, true));
  636. }
  637. foreach ($backups as $backup) {
  638. echo " - "._f("Restoring post #%d...", array($backup["id"])).
  639. test($insert = $sql->insert("posts", $backup), _f("Backup written to %s.", array("./_posts.bak.txt")));
  640. if (!$insert)
  641. return file_put_contents("./_posts.bak.txt", var_export($backups, true));
  642. }
  643. echo " -".test(true);
  644. }
  645. /**
  646. * Function: add_post_attributes_table
  647. * Adds the "post_attributes" table.
  648. *
  649. * Versions: 2.0rc1 => 2.0rc2
  650. */
  651. function add_post_attributes_table() {
  652. $sql = SQL::current();
  653. if ($sql->select("post_attributes"))
  654. return;
  655. echo __("Creating `post_attributes` table...").
  656. test($sql->query("CREATE TABLE __post_attributes (
  657. post_id INTEGER NOT NULL ,
  658. name VARCHAR(100) DEFAULT '',
  659. value LONGTEXT,
  660. PRIMARY KEY (post_id, name)
  661. ) DEFAULT CHARSET=utf8"));
  662. }
  663. /**
  664. * Function: post_xml_to_db
  665. * Migrates the XML post attributes to the "post_attributes" table.
  666. *
  667. * Versions: 2.0rc1 => 2.0rc2
  668. */
  669. function post_xml_to_db() {
  670. $sql = SQL::current();
  671. if (!$rows = $sql->query("SELECT id, xml FROM __posts"))
  672. return;
  673. function insert_attributes($sql, $row, $xml, &$inserts) {
  674. foreach ($xml as $name => $value) {
  675. if (is_array($value))
  676. $value = YAML::dump($value);
  677. if (!$sql->insert("post_attributes",
  678. array("post_id" => $row["id"],
  679. "name" => $name,
  680. "value" => $value))) {
  681. # Clear successful attribute insertions so the
  682. # user can try again without primary key conflicts.
  683. foreach ($inserts as $insertion)
  684. $sql->delete("post_attributes",
  685. array("post_id" => $insertion["id"],
  686. "name" => $insertion["name"]));
  687. return false;
  688. } else
  689. $inserts[] = array("id" => $row["id"],
  690. "name" => $name);
  691. }
  692. return true;
  693. }
  694. $results = array();
  695. foreach ($rows->fetchAll() as $row) {
  696. if (empty($row["xml"]))
  697. continue;
  698. $xml = xml2arr(new SimpleXMLElement($row["xml"]));
  699. $inserts = array();
  700. echo _f("Migrating attributes of post #%d...", array($row["id"])).
  701. test($results[] = insert_attributes($sql, $row, $xml, $inserts));
  702. }
  703. if (!in_array(false, $results)) {
  704. echo __("Removing `xml` column from `posts` table...")."\n";
  705. echo " - ".__("Backing up `posts` table...").
  706. test($backup = $sql->select("posts"));
  707. if (!$backup)
  708. return;
  709. $backups = $backup->fetchAll();
  710. echo " - ".__("Dropping `posts` table...").
  711. test($drop = $sql->query("DROP TABLE __posts"));
  712. if (!$drop)
  713. return;
  714. echo " - ".__("Creating `posts` table...").
  715. test($create = $sql->query("CREATE TABLE IF NOT EXISTS __posts (
  716. id INTEGER PRIMARY KEY AUTO_INCREMENT,
  717. feather VARCHAR(32) DEFAULT '',
  718. clean VARCHAR(128) DEFAULT '',
  719. url VARCHAR(128) DEFAULT '',
  720. pinned TINYINT(1) DEFAULT 0,
  721. status VARCHAR(32) DEFAULT 'public',
  722. user_id INTEGER DEFAULT 0,
  723. created_at DATETIME DEFAULT NULL,
  724. updated_at DATETIME DEFAULT NULL
  725. ) DEFAULT CHARSET=utf8"));
  726. if (!$create)
  727. return file_put_contents("./_posts.bak.txt", var_export($backups, true));
  728. foreach ($backups as $backup) {
  729. unset($backup["xml"]);
  730. echo " - "._f("Restoring post #%d...", array($backup["id"])).
  731. test($insert = $sql->insert("posts", $backup));
  732. if (!$insert)
  733. return file_put_contents("./_posts.bak.txt", var_export($backups, true));
  734. }
  735. echo " -".test(true);
  736. }
  737. }
  738. /**
  739. * Function: add_group_id_to_permissions
  740. * Adds the @group_id@ column to the "permissions" table.
  741. *
  742. * Versions: 2.0rc1 => 2.0rc2
  743. */
  744. function add_group_id_to_permissions() {
  745. $sql = SQL::current();
  746. if ($sql->select("permissions", "group_id"))
  747. return;
  748. echo __("Backing up permissions...").
  749. test($permissions = $sql->select("permissions"));
  750. if (!$permissions)
  751. return;
  752. $backup = $permissions->fetchAll();
  753. echo __("Dropping `permissions` table...").
  754. test($sql->query("DROP TABLE __permissions"));
  755. echo __("Creating `permissions` table...").
  756. test($sql->query("CREATE TABLE __permissions (
  757. id VARCHAR(100) DEFAULT '',
  758. name VARCHAR(100) DEFAULT '',
  759. group_id INTEGER DEFAULT 0,
  760. PRIMARY KEY (id, group_id)
  761. ) DEFAULT CHARSET=utf8"));
  762. foreach ($backup as $permission)
  763. echo _f("Restoring permission `%s`...", array($permission["name"])).
  764. test($sql->insert("permissions",
  765. array("id" => $permission["id"],
  766. "name" => $permission["name"],
  767. "group_id" => 0)));
  768. }
  769. /**
  770. * Function: group_permissions_to_db
  771. * Migrates the group permissions from a YAML column to the "permissions" table.
  772. *
  773. * Versions: 2.0rc1 => 2.0rc2
  774. */
  775. function group_permissions_to_db() {
  776. $sql = SQL::current();
  777. if (!$sql->select("groups", "permissions"))
  778. return;
  779. echo __("Backing up groups...").
  780. test($groups = $sql->select("groups"));
  781. if (!$groups)
  782. return;
  783. $backup = $groups->fetchAll();
  784. $names = array();
  785. foreach($backup as $group) {
  786. $names[$group["id"]] = $group["name"];
  787. $permissions[$group["id"]] = empty($group["permissions"]) ? array() : YAML::load($group["permissions"]) ;
  788. }
  789. echo __("Dropping `groups` table...").
  790. test($sql->query("DROP TABLE __groups"));
  791. echo __("Creating `groups` table...").
  792. test($sql->query("CREATE TABLE __groups (
  793. id INTEGER PRIMARY KEY AUTO_INCREMENT,
  794. name VARCHAR(100) DEFAULT '',
  795. UNIQUE (name)
  796. ) DEFAULT CHARSET=utf8"));
  797. foreach ($names as $id => $name)
  798. echo _f("Restoring group `%s`...", array($name)).
  799. test($sql->insert("groups",
  800. array("id" => $id,
  801. "name" => $name)));
  802. foreach ($permissions as $id => $permissions)
  803. foreach ($permissions as $permission)
  804. echo _f("Restoring permission `%s` on group `%s`...", array($permission, $names[$id])).
  805. test($sql->insert("permissions",
  806. array("id" => $permission,
  807. "name" => $sql->select("permissions", "name", array("id" => $permission))->fetchColumn(),
  808. "group_id" => $id)));
  809. }
  810. /**
  811. * Function: remove_old_files
  812. * Removes old/unused files from previous installs.
  813. */
  814. function remove_old_files() {
  815. if (file_exists(INCLUDES_DIR."/config.php"))
  816. echo __("Removing `includes/config.php` file...").
  817. test(@unlink(INCLUDES_DIR."/config.php"));
  818. if (file_exists(INCLUDES_DIR."/database.php"))
  819. echo __("Removing `includes/database.php` file...").
  820. test(@unlink(INCLUDES_DIR."/database.php"));
  821. if (file_exists(INCLUDES_DIR."/rss.php"))
  822. echo __("Removing `includes/rss.php` file...").
  823. test(@unlink(INCLUDES_DIR."/rss.php"));
  824. if (file_exists(INCLUDES_DIR."/bookmarklet.php"))
  825. echo __("Removing `includes/bookmarklet.php` file...").
  826. test(@unlink(INCLUDES_DIR."/bookmarklet.php"));
  827. }
  828. /**
  829. * Function: update_user_password_column
  830. * Updates the @password@ column on the "users" table to have a length of 60.
  831. *
  832. * Versions: 2.0rc3 => 2.0
  833. */
  834. function update_user_password_column() {
  835. $sql = SQL::current();
  836. if (!$column = $sql->query("SHOW COLUMNS FROM __users WHERE Field = 'password'"))
  837. return;
  838. if ($column->fetchObject()->Type == "varchar(60)")
  839. return;
  840. echo __("Updating `password` column on `users` table...")."\n";
  841. echo " - ".__("Backing up `users` table...").
  842. test($backup = $sql->select("users"));
  843. if (!$backup)
  844. return;
  845. $backups = $backup->fetchAll();
  846. echo " - ".__("Dropping `users` table...").
  847. test($drop = $sql->query("DROP TABLE __users"));
  848. if (!$drop)
  849. return;
  850. echo " - ".__("Creating `users` table...").
  851. test($create = $sql->query("CREATE TABLE IF NOT EXISTS `__users` (
  852. `id` int(11) NOT NULL AUTO_INCREMENT,
  853. `login` varchar(64) DEFAULT '',
  854. `password` varchar(60) DEFAULT NULL,
  855. `full_name` varchar(250) DEFAULT '',
  856. `email` varchar(128) DEFAULT '',
  857. `website` varchar(128) DEFAULT '',
  858. `group_id` int(11) DEFAULT '0',
  859. `joined_at` datetime DEFAULT NULL,
  860. PRIMARY KEY (`id`),
  861. UNIQUE KEY `login` (`login`)
  862. ) DEFAULT CHARSET=utf8"));
  863. if (!$create) {
  864. echo " -".test(false, _f("Backup written to %s.", array("./_users.bak.txt")));
  865. return file_put_contents("./_users.bak.txt", var_export($backups, true));
  866. }
  867. foreach ($backups as $backup) {
  868. echo " - "._f("Restoring user #%d...", array($backup["id"])).
  869. test($insert = $sql->insert("users", $backup), _f("Backup written to %s.", array("./_users.bak.txt")));
  870. if (!$insert)
  871. return file_put_contents("./_users.bak.txt", var_export($backups, true));
  872. }
  873. echo " -".test(true);
  874. }
  875. ?>
  876. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  877. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  878. <html xmlns="http://www.w3.org/1999/xhtml">
  879. <head>
  880. <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  881. <title><?php echo __("Chyrp Upgrader"); ?></title>
  882. <style type="text/css" media="screen">
  883. html, body, ul, ol, li,
  884. h1, h2, h3, h4, h5, h6,
  885. form, fieldset, a, p {
  886. margin: 0;
  887. padding: 0;
  888. border: 0;
  889. }
  890. html {
  891. font-size: 62.5%;
  892. }
  893. body {
  894. font: 1.25em/1.5em normal Verdana, Helvetica, Arial, sans-serif;
  895. color: #626262;
  896. background: #e8e8e8;
  897. padding: 0 0 5em;
  898. }
  899. .window {
  900. width: 30em;
  901. background: #fff;
  902. padding: 2em;
  903. margin: 5em auto 0;
  904. -webkit-border-radius: 2em;
  905. -moz-border-radius: 2em;
  906. }
  907. h1 {
  908. color: #ccc;
  909. font-size: 3em;
  910. margin: 1em 0 .5em;
  911. text-align: center;
  912. }
  913. h1.first {
  914. margin-top: .25em;
  915. }
  916. h1.what_now {
  917. margin-top: .5em;
  918. }
  919. code {
  920. color: #06B;
  921. font-family: Monaco, monospace;
  922. }
  923. a:link, a:visited {
  924. color: #6B0;
  925. }
  926. pre.pane {
  927. height: 15em;
  928. overflow-y: auto;
  929. margin: -2.68em -2.68em 4em;
  930. padding: 2.5em;
  931. background: #333;
  932. color: #fff;
  933. -webkit-border-top-left-radius: 2.5em;
  934. -webkit-border-top-right-radius: 2.5em;
  935. -moz-border-radius-topleft: 2.5em;
  936. -moz-border-radius-topright: 2.5em;
  937. }
  938. span.yay { color: #0f0; }
  939. span.boo { color: #f00; }
  940. a.big,
  941. button {
  942. background: #eee;
  943. display: block;
  944. text-align: center;
  945. margin-top: 2em;
  946. padding: .75em 1em;
  947. color: #777;
  948. text-shadow: #fff .1em .1em 0;
  949. font: 1em normal "Lucida Grande", Verdana, Helvetica, Arial, sans-serif;
  950. text-decoration: none;
  951. border: 0;
  952. cursor: pointer;
  953. -webkit-border-radius: .5em;
  954. -moz-border-radius: .5em;
  955. }
  956. button {
  957. width: 100%;
  958. }
  959. a.big:hover,
  960. button:hover {
  961. background: #f5f5f5;
  962. }
  963. a.big:active,
  964. button:active {
  965. background: #e0e0e0;
  966. }
  967. ul, ol {
  968. margin: 0 0 1em 2em;
  969. }
  970. li {
  971. margin-bottom: .5em;
  972. }
  973. ul {
  974. margin-bottom: 1.5em;
  975. }
  976. p {
  977. margin-bottom: 1em;
  978. }
  979. </style>
  980. </head>
  981. <body>
  982. <div class="window">
  983. <?php if (!empty($_POST) and $_POST['upgrade'] == "yes"): ?>
  984. <pre class="pane"><?php
  985. # Begin with file/config upgrade tasks.
  986. fix_htaccess();
  987. remove_beginning_slash_from_post_url();
  988. move_yml_yaml();
  989. update_protection();
  990. theme_default_to_stardust();
  991. Config::fallback("routes", array());
  992. Config::fallback("secure_hashkey", md5(random(32, true)));
  993. Config::fallback("enable_xmlrpc", true);
  994. Config::fallback("enable_ajax", true);
  995. Config::fallback("uploads_path", "/uploads/");
  996. Config::fallback("chyrp_url", Config::get("url"));
  997. Config::fallback("sql", Config::$yaml["database"]);
  998. Config::fallback("timezone", "America/New_York");
  999. Config::remove("rss_posts");
  1000. Config::remove("time_offset");
  1001. move_upload();
  1002. remove_database_config_file();
  1003. rename_database_setting_to_sql();
  1004. update_custom_routes();
  1005. default_db_adapter_to_mysql();
  1006. # Perform database upgrade tasks after all the files/config upgrade tasks are done.
  1007. # Prepare the SQL interface.
  1008. $sql = SQL::current();
  1009. # Set the SQL info.
  1010. foreach (Config::$yaml["config"]["sql"] as $name => $value)
  1011. $sql->$name = $value;
  1012. # Initialize connection to SQL server.
  1013. $sql->connect();
  1014. tweets_to_posts();
  1015. pages_parent_id_column();
  1016. pages_list_order_column();
  1017. make_posts_safe();
  1018. rss_posts_to_feed_items();
  1019. update_groups_to_yaml();
  1020. add_permissions_table();
  1021. add_sessions_table();
  1022. update_permissions_table();
  1023. update_post_status_column();
  1024. add_post_attributes_table();
  1025. post_xml_to_db();
  1026. add_group_id_to_permissions();
  1027. group_permissions_to_db();
  1028. remove_old_files();
  1029. update_user_password_column();
  1030. # Perform Module/Feather upgrades.
  1031. foreach ((array) Config::get("enabled_modules") as $module)
  1032. if (file_exists(MAIN_DIR."/modules/".$module."/upgrades.php")) {
  1033. ob_start();
  1034. echo $begin = _f("Calling <span class=\"yay\">%s</span> Module's upgrader...", array($module))."\n";
  1035. require MAIN_DIR."/modules/".$module."/upgrades.php";
  1036. $buf = ob_get_contents();
  1037. if (ob_get_contents() == $begin)
  1038. ob_end_clean();
  1039. else
  1040. ob_end_flush();
  1041. }
  1042. foreach ((array) Config::get("enabled_feathers") as $feather)
  1043. if (file_exists(MAIN_DIR."/feathers/".$feather."/upgrades.php")) {
  1044. ob_start();
  1045. echo $begin = _f("Calling <span class=\"yay\">%s</span> Feather's upgrader...", array($feather))."\n";
  1046. require MAIN_DIR."/feathers/".$feather."/upgrades.php";
  1047. $buf = ob_get_contents();
  1048. if (ob_get_contents() == $begin)
  1049. ob_end_clean();
  1050. else
  1051. ob_end_flush();
  1052. }
  1053. ?>
  1054. <?php echo __("Done!"); ?>
  1055. </pre>
  1056. <h1 class="what_now"><?php echo __("What now?"); ?></h1>
  1057. <ol>
  1058. <li><?php echo __("Look through the results up there for any failed tasks. If you see any and you can't figure out why, you can ask for help at the <a href=\"http://chyrp.net/community/\">Chyrp Community</a>."); ?></li>
  1059. <li><?php echo __("If any of your Modules or Feathers have new versions available for this release, check if an <code>upgrades.php</code> file exists in their main directory. If that file exists, run this upgrader again after enabling the Module or Feather and it will run the upgrade tasks."); ?></li>
  1060. <li><?php echo __("When you are done, you can delete this file. It doesn't pose any real threat on its own, but you should delete it anyway, just to be sure."); ?></li>
  1061. </ol>
  1062. <h1 class="tips"><?php echo __("Tips"); ?></h1>
  1063. <ul>
  1064. <li><?php echo __("If the admin area looks weird, try clearing your cache."); ?></li>
  1065. <li><?php echo __("As of v2.0, Chyrp uses time zones to determine timestamps. Please set your installation to the correct timezone at <a href=\"admin/index.php?action=general_settings\">General Settings</a>."); ?></li>
  1066. <li><?php echo __("Check the group permissions &ndash; they might have changed, and certain Admin functionality would be disabled until you enabled the permissions for the particular groups. <a href=\"admin/index.php?action=manage_groups\">Manage Groups &rarr;</a>"); ?></li>
  1067. </ul>
  1068. <a class="big" href="<?php echo (Config::check("url") ? Config::get("url") : Config::get("chyrp_url")); ?>"><?php echo __("All done!"); ?></a>
  1069. <?php else: ?>
  1070. <h1 class="first"><?php echo __("Halt!"); ?></h1>
  1071. <p><?php echo __("That button may look ready for a-clickin&rsquo;, but please take these preemptive measures before indulging:"); ?></p>
  1072. <ol>
  1073. <li><?php echo __("<strong>Make a backup of your installation.</strong> You never know."); ?></li>
  1074. <

Large files files are truncated, but you can click here to view the full file