Exemple #1
0
 static function admin_manage_dropbox($admin)
 {
     if (!Visitor::current()->group->can("add_post", "add_draft")) {
         show_403(__("Access Denied"), __("You do not have sufficient privileges to create posts."));
     }
     if (empty($_POST)) {
         return $admin->display("manage_dropbox");
     }
     $config = Config::current();
     if (!isset($config->module_dropbox["oauth_token"])) {
         Flash::notice(__("You need to authorize Dropbox first.", "dropbox"), "/admin/?action=dropbox_settings");
     }
     $data = json_decode(file_get_contents("http://chyrp.net/api/1/dropboxsync.php?keys"), true);
     $app_key = $data["key"];
     $app_secret = $data["secret"];
     $storage = new \Dropbox\OAuth\Storage\Session();
     $OAuth = new \Dropbox\OAuth\Consumer\Curl($app_key, $app_secret, $storage);
     $dropbox = new \Dropbox\API($OAuth);
     $delta = $dropbox->delta();
     $delta = $delta["body"];
     if ($delta->cursor != $config->module_dropbox["cursor"]) {
         if (count($delta->entries) > 0) {
             foreach ($delta->entries as $entry) {
                 $tmpfname = tempnam("/tmp", "md");
                 $file = $dropbox->getFile(ltrim($entry[0], "/"), $tmpfname);
                 $post = new FrontMatter($file["name"]);
                 $date = explode(".", ltrim($entry[0], "/"));
                 $values = array("title" => $post->fetch("title"), "body" => $post->fetch("content"));
                 # Set defaults
                 fallback($clean, oneof($post->fetch("slug"), strtolower(str_replace(" ", "-", $post->fetch("title")))));
                 fallback($url, Post::check_url($clean));
                 fallback($pinned, oneof($post->fetch("pinned"), 0));
                 fallback($status, oneof($post->fetch("status"), "public"));
                 fallback($date, oneof(datetime($post->fetch("date")), datetime($date[0])));
                 $post = Post::add($values, $clean, $url, "text", 1, $pinned, $status, datetime($post->fetch("date")), datetime($post->fetch("date")), false);
             }
         }
         $set = array($config->set("module_dropbox", array("oauth_token_secret" => $config->module_dropbox['oauth_token_secret'], "oauth_token" => $config->module_dropbox['oauth_token'], "uid" => $config->module_dropbox['uid'], "cursor" => $delta->cursor)));
         if (!in_array(false, $set)) {
             Flash::notice(__("Post imported successfully.", "dropbox"), "/admin/?action=manage_posts");
         }
     }
 }