function update($id, $fields, $values = false, $ignore_cache = false) { $id = sqlescape($id); // Turn a key => value array into pairs if ($values === false && is_array($fields)) { $values = $fields; $fields = array_keys($fields); } // Multiple columns to update if (is_array($fields)) { $query_parts = array(); foreach ($fields as $key) { $val = current($values); if (is_array($val)) { $val = BigTree::json(BigTree::translateArray($val)); } else { $val = BigTreeAdmin::autoIPL($val); } $query_parts[] = "`{$key}` = '" . sqlescape($val) . "'"; next($values); } sqlquery("UPDATE `" . $this->Table . "` SET " . implode(", ", $query_parts) . " WHERE id = '{$id}'"); // Single column to update } else { if (is_array($values)) { $val = json_encode(BigTree::translateArray($values)); } else { $val = BigTreeAdmin::autoIPL($values); } sqlquery("UPDATE `" . $this->Table . "` SET `{$fields}` = '" . sqlescape($val) . "' WHERE id = '{$id}'"); } if (!$ignore_cache) { BigTreeAutoModule::recacheItem($id, $this->Table); } }
static function cachePut($identifier, $key, $value, $replace = true) { $identifier = sqlescape($identifier); $key = sqlescape($key); $f = sqlfetch(sqlquery("SELECT `key` FROM bigtree_caches WHERE `identifier` = '{$identifier}' AND `key` = '{$key}'")); if ($f && !$replace) { return false; } $value = BigTree::json($value, true); if ($f) { sqlquery("UPDATE bigtree_caches SET `value` = '{$value}', `timestamp` = NOW() WHERE `identifier` = '{$identifier}' AND `key` = '{$key}'"); } else { sqlquery("INSERT INTO bigtree_caches (`identifier`,`key`,`value`) VALUES ('{$identifier}','{$key}','{$value}')"); } return true; }
function _local_bigtree_update_200() { global $cms, $admin; // Drop unused comments column sqlquery("ALTER TABLE bigtree_pending_changes DROP COLUMN `comments`"); // Add extension columns sqlquery("ALTER TABLE bigtree_callouts ADD COLUMN `extension` VARCHAR(255)"); sqlquery("ALTER TABLE bigtree_callouts ADD FOREIGN KEY (extension) REFERENCES `bigtree_extensions` (id) ON DELETE CASCADE"); sqlquery("ALTER TABLE bigtree_feeds ADD COLUMN `extension` VARCHAR(255)"); sqlquery("ALTER TABLE bigtree_feeds ADD FOREIGN KEY (extension) REFERENCES `bigtree_extensions` (id) ON DELETE CASCADE"); sqlquery("ALTER TABLE bigtree_field_types ADD COLUMN `extension` VARCHAR(255)"); sqlquery("ALTER TABLE bigtree_field_types ADD FOREIGN KEY (extension) REFERENCES `bigtree_extensions` (id) ON DELETE CASCADE"); sqlquery("ALTER TABLE bigtree_modules ADD COLUMN `extension` VARCHAR(255)"); sqlquery("ALTER TABLE bigtree_modules ADD FOREIGN KEY (extension) REFERENCES `bigtree_extensions` (id) ON DELETE CASCADE"); sqlquery("ALTER TABLE bigtree_module_groups ADD COLUMN `extension` VARCHAR(255)"); sqlquery("ALTER TABLE bigtree_module_groups ADD FOREIGN KEY (extension) REFERENCES `bigtree_extensions` (id) ON DELETE CASCADE"); sqlquery("ALTER TABLE bigtree_settings ADD COLUMN `extension` VARCHAR(255)"); sqlquery("ALTER TABLE bigtree_settings ADD FOREIGN KEY (extension) REFERENCES `bigtree_extensions` (id) ON DELETE CASCADE"); sqlquery("ALTER TABLE bigtree_templates ADD COLUMN `extension` VARCHAR(255)"); sqlquery("ALTER TABLE bigtree_templates ADD FOREIGN KEY (extension) REFERENCES `bigtree_extensions` (id) ON DELETE CASCADE"); // New publish_hook column, consolidate other hooks into one column sqlquery("ALTER TABLE bigtree_pending_changes ADD COLUMN `publish_hook` VARCHAR(255)"); sqlquery("ALTER TABLE bigtree_module_forms ADD COLUMN `hooks` TEXT"); sqlquery("ALTER TABLE bigtree_module_embeds ADD COLUMN `hooks` TEXT"); $q = sqlquery("SELECT * FROM bigtree_module_forms"); while ($f = sqlfetch($q)) { $hooks = array(); $hooks["pre"] = $f["preprocess"]; $hooks["post"] = $f["callback"]; $hooks["publish"] = ""; sqlquery("UPDATE bigtree_module_forms SET hooks = '" . BigTree::json($hooks, true) . "' WHERE id = '" . $f["id"] . "'"); } $q = sqlquery("SELECT * FROM bigtree_module_embeds"); while ($f = sqlfetch($q)) { $hooks = array(); $hooks["pre"] = $f["preprocess"]; $hooks["post"] = $f["callback"]; $hooks["publish"] = ""; sqlquery("UPDATE bigtree_module_embeds SET hooks = '" . BigTree::json($hooks, true) . "' WHERE id = '" . $f["id"] . "'"); } sqlquery("ALTER TABLE bigtree_module_forms DROP COLUMN `preprocess`"); sqlquery("ALTER TABLE bigtree_module_forms DROP COLUMN `callback`"); sqlquery("ALTER TABLE bigtree_module_embeds DROP COLUMN `preprocess`"); sqlquery("ALTER TABLE bigtree_module_embeds DROP COLUMN `callback`"); // Adjust groups/callouts for multi-support -- first we drop the foreign key $table_desc = BigTree::describeTable("bigtree_callouts"); foreach ($table_desc["foreign_keys"] as $name => $definition) { if ($definition["local_columns"][0] === "group") { sqlquery("ALTER TABLE bigtree_callouts DROP FOREIGN KEY `{$name}`"); } } // Add the field to the groups sqlquery("ALTER TABLE bigtree_callout_groups ADD COLUMN `callouts` TEXT AFTER `name`"); // Find all the callouts in each group $q = sqlquery("SELECT * FROM bigtree_callout_groups"); while ($f = sqlfetch($q)) { $callouts = array(); $qq = sqlquery("SELECT * FROM bigtree_callouts WHERE `group` = '" . $f["id"] . "' ORDER BY position DESC, id ASC"); while ($ff = sqlfetch($qq)) { $callouts[] = $ff["id"]; } sqlquery("UPDATE bigtree_callout_groups SET `callouts` = '" . BigTree::json($callouts, true) . "' WHERE id = '" . $f["id"] . "'"); } // Drop the group column sqlquery("ALTER TABLE bigtree_callouts DROP COLUMN `group`"); // Security policy setting sqlquery("INSERT INTO `bigtree_settings` (`id`,`value`,`system`) VALUES ('bigtree-internal-security-policy','{}','on')"); sqlquery("CREATE TABLE `bigtree_login_attempts` (`id` int(11) unsigned NOT NULL AUTO_INCREMENT, `ip` int(11) DEFAULT NULL, `user` int(11) DEFAULT NULL, `timestamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8"); sqlquery("CREATE TABLE `bigtree_login_bans` (`id` int(11) unsigned NOT NULL AUTO_INCREMENT, `ip` int(11) DEFAULT NULL, `user` int(11) DEFAULT NULL, `created` timestamp NULL DEFAULT CURRENT_TIMESTAMP, `expires` datetime DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8"); // Media settings sqlquery("INSERT INTO `bigtree_settings` (`id`,`value`,`system`) VALUES ('bigtree-internal-media-settings','{}','on')"); // New field types @unlink(SERVER_ROOT . "cache/bigtree-form-field-types.json"); // Setup an anonymous function for converting a resource set $resource_converter = function ($resources) { $new_resources = array(); foreach ($resources as $item) { // Array of Items no longer exists, switching to Matrix if ($item["type"] == "array") { $item["type"] = "matrix"; $item["columns"] = array(); $x = 0; foreach ($item["fields"] as $field) { $x++; $item["columns"][] = array("id" => $field["key"], "type" => $field["type"], "title" => $field["title"], "display_title" => $x == 1 ? "on" : ""); } unset($item["fields"]); } $r = array("id" => $item["id"], "type" => $item["type"], "title" => $item["title"], "subtitle" => $item["subtitle"], "options" => array()); foreach ($item as $key => $val) { if ($key != "id" && $key != "title" && $key != "subtitle" && $key != "type") { $r["options"][$key] = $val; } } $new_resources[] = $r; } return BigTree::json($new_resources, true); }; $field_converter = function ($fields) { $new_fields = array(); foreach ($fields as $id => $field) { // Array of Items no longer exists, switching to Matrix if ($field["type"] == "array") { $field["type"] = "matrix"; $field["columns"] = array(); $x = 0; foreach ($field["fields"] as $subfield) { $x++; $field["columns"][] = array("id" => $subfield["key"], "type" => $subfield["type"], "title" => $subfield["title"], "display_title" => $x == 1 ? "on" : ""); } unset($field["fields"]); } $r = array("column" => $id, "type" => $field["type"], "title" => $field["title"], "subtitle" => $field["subtitle"], "options" => array()); foreach ($field as $key => $val) { if ($key != "id" && $key != "title" && $key != "subtitle" && $key != "type") { $r["options"][$key] = $val; } } $new_fields[] = $r; } return $new_fields; }; // New resource format to be less restrictive on option names $q = sqlquery("SELECT * FROM bigtree_callouts"); while ($f = sqlfetch($q)) { $resources = $resource_converter(json_decode($f["resources"], true)); sqlquery("UPDATE bigtree_callouts SET resources = '{$resources}' WHERE id = '" . $f["id"] . "'"); } $q = sqlquery("SELECT * FROM bigtree_templates"); while ($f = sqlfetch($q)) { $resources = $resource_converter(json_decode($f["resources"], true)); sqlquery("UPDATE bigtree_templates SET resources = '{$resources}' WHERE id = '" . $f["id"] . "'"); } // Forms and Embedded Forms $q = sqlquery("SELECT * FROM bigtree_module_forms"); while ($f = sqlfetch($q)) { $fields = $field_converter(json_decode($f["fields"], true)); sqlquery("UPDATE bigtree_module_forms SET fields = '" . BigTree::json($fields, true) . "' WHERE id = '" . $f["id"] . "'"); } $q = sqlquery("SELECT * FROM bigtree_module_embeds"); while ($f = sqlfetch($q)) { $fields = $field_converter(json_decode($f["fields"], true)); sqlquery("UPDATE bigtree_module_embeds SET fields = '" . BigTree::json($fields, true) . "' WHERE id = '" . $f["id"] . "'"); } // Settings $q = sqlquery("SELECT * FROM bigtree_settings WHERE type = 'array'"); while ($f = sqlfetch($q)) { // Update settings options to turn array into matrix $options = json_decode($f["options"], true); $options["columns"] = array(); $x = 0; foreach ($options["fields"] as $field) { $x++; $options["columns"][] = array("id" => $field["key"], "type" => $field["type"], "title" => $field["title"], "display_title" => $x == 1 ? "on" : ""); if ($x == 1) { $display_key = $field["key"]; } } unset($options["fields"]); // Update the value to set an internal title key $value = BigTreeCMS::getSetting($f["id"]); foreach ($value as &$entry) { $entry["__internal-title"] = $entry[$display_key]; } unset($entry); // Update type/options sqlquery("UPDATE bigtree_settings SET type = 'matrix', options = '" . BigTree::json($options, true) . "' WHERE id = '" . $f["id"] . "'"); // Update value separately BigTreeAdmin::updateSettingValue($f["id"], $value); } }
function updateUser($id, $data) { global $bigtree; $id = sqlescape($id); // See if there's an email collission $r = sqlrows(sqlquery("SELECT * FROM bigtree_users WHERE email = '" . sqlescape($data["email"]) . "' AND id != '{$id}'")); if ($r) { return false; } // If this person has higher access levels than the person trying to update them, fail. $current = static::getUser($id); if ($current["level"] > $this->Level) { return false; } $level = intval($data["level"]); $email = sqlescape($data["email"]); $name = sqlescape(htmlspecialchars($data["name"])); $company = sqlescape(htmlspecialchars($data["company"])); $daily_digest = $data["daily_digest"] ? "on" : ""; $permissions = BigTree::json($data["permissions"], true); $alerts = BigTree::json($data["alerts"], true); // If the user is editing themselves, they can't change the level. if ($this->ID == $current["id"]) { $level = $current["level"]; } // Don't allow the level to be set higher than the logged in user's level if ($level > $this->Level) { $level = $this->Level; } if ($data["password"]) { $phpass = new PasswordHash($bigtree["config"]["password_depth"], TRUE); $password = sqlescape($phpass->HashPassword(trim($data["password"]))); sqlquery("UPDATE bigtree_users SET `email` = '{$email}', `password` = '{$password}', `name` = '{$name}', `company` = '{$company}', `level` = '{$level}', `permissions` = '{$permissions}', `alerts` = '{$alerts}', `daily_digest` = '{$daily_digest}' WHERE id = '{$id}'"); } else { sqlquery("UPDATE bigtree_users SET `email` = '{$email}', `name` = '{$name}', `company` = '{$company}', `level` = '{$level}', `permissions` = '{$permissions}', `alerts` = '{$alerts}', `daily_digest` = '{$daily_digest}' WHERE id = '{$id}'"); } $this->track("bigtree_users", $id, "updated"); return true; }
sqlquery("INSERT INTO bigtree_feeds (`route`,`name`,`description`,`type`,`table`,`fields`,`options`) VALUES ('" . sqlescape($feed["route"]) . "','" . sqlescape($feed["name"]) . "','" . sqlescape($feed["description"]) . "','" . sqlescape($feed["type"]) . "','" . sqlescape($feed["table"]) . "','{$fields}','{$options}')"); } } // Import Field Types foreach ($json["components"]["field_types"] as $type) { if ($type) { sqlquery("DELETE FROM bigtree_field_types WHERE id = '" . sqlescape($type["id"]) . "'"); // Backwards compatibility with field types packaged for 4.1 if (!isset($type["use_cases"])) { $type["use_cases"] = array("templates" => $type["pages"], "modules" => $type["modules"], "callouts" => $type["callouts"], "settings" => $type["settings"]); } $use_cases = is_array($type["use_cases"]) ? sqlescape(json_encode($type["use_cases"])) : sqlescape($type["use_cases"]); $self_draw = $type["self_draw"] ? "'on'" : "NULL"; sqlquery("INSERT INTO bigtree_field_types (`id`,`name`,`use_cases`,`self_draw`) VALUES ('" . sqlescape($type["id"]) . "','" . sqlescape($type["name"]) . "','{$use_cases}',{$self_draw})"); } } // Import files foreach ($json["files"] as $file) { BigTree::copyFile(SERVER_ROOT . "cache/package/{$file}", SERVER_ROOT . $file); } // Empty view cache sqlquery("DELETE FROM bigtree_module_view_cache"); // Remove the package directory BigTree::deleteDirectory(SERVER_ROOT . "cache/package/"); // Clear module class cache and field type cache. @unlink(SERVER_ROOT . "cache/bigtree-module-class-list.json"); @unlink(SERVER_ROOT . "cache/bigtree-form-field-types.json"); sqlquery("INSERT INTO bigtree_extensions (`id`,`type`,`name`,`version`,`last_updated`,`manifest`) VALUES ('" . sqlescape($json["id"]) . "','package','" . sqlescape($json["title"]) . "','" . sqlescape($json["version"]) . "',NOW(),'" . BigTree::json($json, true) . "')"); sqlquery("SET foreign_key_checks = 1"); $admin->growl("Developer", "Installed Package"); BigTree::redirect(DEVELOPER_ROOT . "packages/install/complete/");
// Table doesn't exist in the new manifest, so we're going to drop it } else { $package["sql_revisions"][$revision][] = "DROP TABLE IF EXISTS `{$table}`"; } } // Add new tables that don't exist in the old manifest foreach ($package["components"]["tables"] as $table => $create_statement) { if (!isset($existing_json["components"]["tables"][$table])) { $package["sql_revisions"][$revision][] = $create_statement; } } // Clean up the revisions (if we don't have any) $package["sql_revisions"] = array_filter($package["sql_revisions"]); } // Write the manifest file $json = BigTree::json($package); BigTree::putFile(SERVER_ROOT . "extensions/{$id}/manifest.json", $json); // Create the zip, clear caches since we may have moved the routes of field types and modules @unlink(SERVER_ROOT . "cache/package.zip"); @unlink(SERVER_ROOT . "cache/bigtree-form-field-types.json"); @unlink(SERVER_ROOT . "cache/bigtree-module-class-list.json"); include BigTree::path("inc/lib/pclzip.php"); $zip = new PclZip(SERVER_ROOT . "cache/package.zip"); $zip->create(BigTree::directoryContents(SERVER_ROOT . "extensions/{$id}/"), PCLZIP_OPT_REMOVE_PATH, SERVER_ROOT . "extensions/{$id}/"); // Store it in the database for future updates -- existing packages might be replaced if (sqlrows(sqlquery("SELECT id FROM bigtree_extensions WHERE id = '" . sqlescape($id) . "'"))) { sqlquery("UPDATE bigtree_extensions SET type = 'extension', name = '" . sqlescape($title) . "', version = '" . sqlescape($version) . "', last_updated = NOW(), manifest = '" . sqlescape($json) . "' WHERE id = '" . sqlescape($id) . "'"); } else { sqlquery("INSERT INTO bigtree_extensions (`id`,`type`,`name`,`version`,`last_updated`,`manifest`) VALUES ('" . sqlescape($id) . "','extension','" . sqlescape($title) . "','" . sqlescape($version) . "',NOW(),'" . sqlescape($json) . "')"); } // Turn foreign key checks back on
$thumbs[$key] = $file; } } // Upload the original to the proper place. if ($replacing) { $file = $storage->replace($first_copy, $file_name, "files/resources/"); } else { $file = $storage->store($first_copy, $file_name, "files/resources/"); } if (!$file) { $errors[] = "Uploading " . htmlspecialchars($file_name) . " failed (unknown error)."; } else { if (!$replacing) { $admin->createResource($folder, $file, $md5, $file_name, $extension, "on", $iheight, $iwidth, $thumbs); } else { $admin->updateResource($_POST["replace"], array("date" => date("Y-m-d H:i:s"), "md5" => $md5, "height" => $iheight, "width" => $iwidth, "thumbs" => BigTree::json($thumbs))); } } } } } } } } if (count($errors)) { $uploaded = count($_FILES["files"]["tmp_name"]) - count($errors); $success_message = "{$uploaded} file" . ($uploaded != 1 ? "s" : "") . " uploaded successfully."; echo 'parent.BigTreeFileManager.uploadError("' . implode("<br />", $errors) . '","' . $success_message . '");</script></body></html>'; } else { echo 'parent.BigTreeFileManager.finishedUpload(' . json_encode($errors) . ');</script></body></html>'; }
function cacheInformation() { $cache = array(); // First we're going to update the monthly view counts for all pages. $results = $this->getData($this->Settings["profile"], "1 month ago", "today", "pageviews", "pagePath"); $used_paths = array(); foreach ($results as $item) { $clean_path = sqlescape(trim($item->pagePath, "/")); $views = sqlescape($item->pageviews); // Sometimes Google has slightly different routes like "cheese" and "cheese/" so we need to add these page views together. if (in_array($clean_path, $used_paths)) { sqlquery("UPDATE bigtree_pages SET ga_page_views = (ga_page_views + {$views}) WHERE `path` = '{$clean_path}'"); } else { sqlquery("UPDATE bigtree_pages SET ga_page_views = {$views} WHERE `path` = '{$clean_path}'"); $used_paths[] = $clean_path; } } // Service Provider report $results = $this->getData($this->Settings["profile"], "1 month ago", "today", array("pageviews", "visits"), "networkLocation", "-ga:pageviews"); foreach ($results as $item) { $cache["service_providers"][] = array("name" => $item->networkLocation, "views" => $item->pageviews, "visits" => $item->visits); } // Referrer report $results = $this->getData($this->Settings["profile"], "1 month ago", "today", array("pageviews", "visits"), "source", "-ga:pageviews"); foreach ($results as $item) { $cache["referrers"][] = array("name" => $item->source, "views" => $item->pageviews, "visits" => $item->visits); } // Keyword report $results = $this->getData($this->Settings["profile"], "1 month ago", "today", array("pageviews", "visits"), "keyword", "-ga:pageviews"); foreach ($results as $item) { $cache["keywords"][] = array("name" => $item->keyword, "views" => $item->pageviews, "visits" => $item->visits); } // Yearly Report $this->getData($this->Settings["profile"], date("Y-01-01"), date("Y-m-d"), array("pageviews", "visits", "bounces", "timeOnSite"), "browser"); $cache["year"] = $this->cacheParseLastData(); $this->getData($this->Settings["profile"], date("Y-01-01", strtotime("-1 year")), date("Y-m-d", strtotime("-1 year")), array("pageviews", "visits", "bounces", "timeOnSite"), "browser"); $cache["year_ago_year"] = $this->cacheParseLastData(); // Quarterly Report $quarters = array(1, 3, 6, 9); $current_quarter_month = $quarters[floor((date("m") - 1) / 3)]; $this->getData($this->Settings["profile"], date("Y-" . str_pad($current_quarter_month, 2, "0", STR_PAD_LEFT) . "-01"), date("Y-m-d"), array("pageviews", "visits", "bounces", "timeOnSite"), "browser"); $cache["quarter"] = $this->cacheParseLastData(); $this->getData($this->Settings["profile"], date("Y-" . str_pad($current_quarter_month, 2, "0", STR_PAD_LEFT) . "-01", strtotime("-1 year")), date("Y-m-d", strtotime("-1 year")), array("pageviews", "visits", "bounces", "timeOnSite"), "browser"); $cache["year_ago_quarter"] = $this->cacheParseLastData(); // Monthly Report $this->getData($this->Settings["profile"], date("Y-m-01"), date("Y-m-d"), array("pageviews", "visits", "bounces", "timeOnSite"), "browser"); $cache["month"] = $this->cacheParseLastData(); $this->getData($this->Settings["profile"], date("Y-m-01", strtotime("-1 year")), date("Y-m-d", strtotime("-1 year")), array("pageviews", "visits", "bounces", "timeOnSite"), "browser"); $cache["year_ago_month"] = $this->cacheParseLastData(); // Two Week Heads Up $results = $this->getData($this->Settings["profile"], date("Y-m-d", strtotime("-2 weeks")), date("Y-m-d", strtotime("-1 day")), "visits", "date", "date"); foreach ($results as $item) { $cache["two_week"][$item->date] = $item->visits; } BigTree::putFile(SERVER_ROOT . "cache/analytics.json", BigTree::json($cache)); }