/** * Get form * * @return Form_Form */ public function getForm() { $form = new Form_Form("settings"); $field = new Form_Field_Hidden("save"); $field->setDefaultValue(1); $form->addField($field); $field = new Form_Field_Select("maxentrylifetime", t("admin.settings.2")); $field->attr->add("data-setting", 1); $field->addOption("", t("admin.settings.4")); $field->addOption("-1 month", "1 " . t("months")); $field->addOption("-2 month", "2 " . t("months")); $field->addOption("-3 month", "3 " . t("months")); $field->addOption("-6 month", "6 " . t("months")); $field->addOption("-1 year", "1 " . t("years")); $field->setDefaultValue(RDR_Setting::get($field->name)->value); $form->addField($field); $field = new Form_Field_Select("maxeventlifetime", t("admin.settings.3")); $field->attr->add("data-setting", 1); $field->addOption("", "1 " . t("days")); $field->addOption("-2 day", "2 " . t("days")); $field->addOption("-3 days", "3 " . t("days")); $field->addOption("-1 week", "1 " . t("weeks")); $field->setDefaultValue(RDR_Setting::get($field->name)->value); $form->addField($field); return $form; }
/** * Do the database update */ static function run() { $generator = CHOQ_DB_Generator::create(db()); $generator->addModule("RDR"); $generator->updateDatabase(); # setting current version $setting = RDR_Setting::get("dbversion"); $setting->value = RDR_VERSION; $setting->store(); }
/** * Old entries will be deleted automatically with this cleanup */ static function cleanupEvents() { $time = RDR_Setting::get("maxeventlifetime")->value; if (!$time) { $time = "- 1 day"; } while (true) { $tmp = RDR_Event::getByCondition("createTime < {0}", array(dt("now {$time}")), null, 1000); if (!$tmp) { break; } db()->deleteMultiple($tmp); } }
/** * Load the View */ public function onLoad() { if (req()->isAjax() && get("code") == self::getValidHash()) { $data = null; switch (get("action")) { case "update-check": # fetching branches from GIT $data = array("error" => t("update.4")); $branches = self::getGitJSON("https://api.github.com/repos/brainfoolong/nreeda/branches"); if ($branches) { $newest = null; foreach ($branches as $branch) { if ($branch["name"] == "master") { continue; } if (!$newest || version_compare($branch["name"], $newest, ">")) { $newest = $branch["name"]; } } $data = array("version" => $newest, "update" => version_compare($newest, RDR_VERSION, ">")); RDR_Setting::set("latestversion", $newest); } break; case "start": if (RDR_Cron::isRunning()) { $data = array("message" => t("update.1"), "event" => "error"); } else { RDR_Maintenance::enableMaintenanceMode(); $data = array("message" => sprintf(nl2br(t("update.2"), true), url()->getByAlias("root", l("RDR_Maintenance")) . '?disable-maintenance=' . RDR_Maintenance::getValidHash(), 'RDR::$maintenanceMode = true'), "event" => "success", "next" => "check"); } break; case "check": try { # checking all files and directories for write access $files = CHOQ_FileManager::getFiles(CHOQ_ROOT_DIRECTORY, true, true); $count = 0; $files[] = CHOQ_ROOT_DIRECTORY; foreach ($files as $file) { if (substr($file, 0, 1) == ".") { continue; } if (!is_writable($file)) { $count++; } } if ($count) { error(sprintf(t("update.3"), $count)); } $version = RDR_Setting::get("latestversion")->value; $data = array("message" => sprintf(t("update.5"), $version), "event" => "success", "next" => "prepare", "params" => array("version" => $version)); } catch (Exception $e) { $data = array("message" => sprintf(t("update.7"), $e->getMessage()), "event" => "error"); } break; case "prepare": try { # downloading zip file from GIT $url = "https://github.com/brainfoolong/nreeda/archive/" . get("version") . ".zip"; $data = RDR_FileContents::get($url); if ($data === false) { return; } $tmpZip = CHOQ_ACTIVE_MODULE_DIRECTORY . "/tmp/update.zip"; file_put_contents($tmpZip, $data); $updateDir = CHOQ_ACTIVE_MODULE_DIRECTORY . "/tmp/update"; if (!is_dir($updateDir)) { mkdir($updateDir); } # removing all old files $files = CHOQ_FileManager::getFiles($updateDir, true, true); foreach ($files as $file) { if (!is_dir($file)) { unlink($file); } } foreach ($files as $file) { if (is_dir($file)) { rmdir($file); } } # extract zip file to tmp folder $zip = new ZipArchive(); $zip->open($tmpZip); $zip->extractTo($updateDir); $zip->close(); $folder = $updateDir . "/nreeda-" . get("version"); $data = array("message" => t("update.9"), "event" => "success", "next" => "update", "params" => array("updatefolder" => $folder, "rootfolder" => CHOQ_ROOT_DIRECTORY, "updateurl" => url()->getByAlias("base", "modules/RDR/tmp/update/nreeda-" . get("version") . "/update.php"))); } catch (Exception $e) { $data = array("message" => sprintf(t("update.7"), $e->getMessage()), "event" => "error"); } break; case "db": try { RDR_DBUpdate::run(); $data = array("message" => t("update.10"), "event" => "success", "next" => "cleanup"); } catch (Exception $e) { $data = array("message" => sprintf(t("update.7"), $e->getMessage()), "event" => "error"); } break; case "cleanup": try { # deleting all update files $updateDir = CHOQ_ACTIVE_MODULE_DIRECTORY . "/tmp/update"; if (is_dir($updateDir)) { $files = CHOQ_FileManager::getFiles($updateDir, true, true); foreach ($files as $file) { if (!is_dir($file)) { unlink($file); } } foreach ($files as $file) { if (is_dir($file)) { rmdir($file); } } } if (is_dir($updateDir)) { rmdir($updateDir); } $updateFile = CHOQ_ROOT_DIRECTORY . "/update.php"; if (file_exists($updateFile)) { unlink($updateFile); } $data = array("message" => t("update.11"), "event" => "success"); } catch (Exception $e) { $data = array("message" => sprintf(t("update.7"), $e->getMessage()), "event" => "error"); } break; case "disable": RDR_Maintenance::disableMaintenanceMode(); break; } echo json_encode($data); return; } needRole(RDR_User::ROLE_ADMIN, true); view("RDR_BasicFrame", array("view" => $this)); }
/** * Create a entry for given item * * @param SimpleXMLElement $item * @param RDR_Feed $feed * @return RDR_Entry | null */ private static function createEntryForItem($item, RDR_Feed $feed) { $guid = self::xmlValue($item->guid); if (!$guid) { $guid = self::xmlValue($item->id); } if (!$guid) { $guid = self::xmlValue($item->link); } $entry = RDR_Entry::get($feed, $guid); $now = dt("now"); $entry->title = null; $entry->title = cut(self::xmlValue($item->title), 252, "..."); $entry->text = null; $entry->text = self::xmlValue($item->description); if (!$entry->text) { $entry->text = self::xmlValue($item->content); } if (!$entry->text) { $entry->text = self::xmlValue($item->summary); } $entry->datetime = null; $entry->datetime = dt(self::xmlValue($item->pubDate)); if (!$entry->datetime->valid) { $entry->datetime = dt(self::xmlValue($item->published)); } if (!$entry->datetime->valid) { $entry->datetime = dt(self::xmlValue($item->updated)); } if ($entry->datetime->valid && $entry->datetime->getUnixtime() > $now->getUnixtime()) { $entry->datetime = $now; } $count = count($item->link); foreach ($item->link as $link) { if ($count > 1) { if (self::xmlAttr($link, "type") == "text/html") { $entry->link = self::xmlAttr($link, "href"); } } else { $entry->link = self::xmlValue($link); } } if (!$entry->link) { $entry->link = self::xmlValue($item->link); } if (!$entry->link) { $entry->link = self::xmlAttr($item->link, "href"); } $entry->image = null; $enclosures = $item->xpath("enclosure"); if ($enclosures) { foreach ($enclosures as $encl) { if ($entry->image) { break; } if (self::xmlAttr($encl, "type")) { if (strpos(self::xmlAttr($encl, "type"), "image") !== false) { $entry->image = self::xmlAttr($encl, "url"); } } elseif (self::xmlAttr($encl, "url")) { if (preg_match("~jpg|jpeg|gif|png~i", self::xmlAttr($encl, "url"))) { $entry->image = self::xmlAttr($encl, "url"); } } } } $namespaces = $item->getNameSpaces(true); if ($namespaces) { foreach ($namespaces as $ns) { $tmp = $item->children($ns); foreach ($tmp as $key => $value) { if (!$entry->datetime->valid && strpos($key, "date") !== false) { $entry->datetime = dt(self::xmlValue($value)); } if (!$entry->title && strpos($key, "title") !== false) { $entry->title = cut(self::xmlValue($value), 252, "..."); } if (strpos($key, "description") !== false || strpos($key, "content") !== false || strpos($key, "encoded") !== false) { $text = self::xmlValue($value); if ($text && (!$entry->text || mb_strlen($text) > $entry->text)) { $entry->text = $text; } if (!$entry->image) { $attr = $value->attributes(); $url = isset($attr->url) ? self::xmlValue($attr->url) : null; $type = isset($attr->type) ? self::xmlValue($attr->type) : null; if ($url && (preg_match("~\\.(jpg|jpeg|png|gif)~i", $url) || preg_match("~image\\/~i", $type))) { $entry->image = $url; } } } } } } # if datetime is older than maxlifetime than don't store if (!$entry->datetime->valid) { $entry->datetime = $now; } $time = RDR_Setting::get("maxentrylifetime")->value; if ($time && $entry->datetime->getUnixtime() < dt("now {$time}")->getUnixtime()) { return; } $entry->store(); return $entry; }