[vassalengine-svn-trunk] [vassalengine-svn] SF.net SVN: vassalengine:[7112] site-src/trunk/site
Brought to you by:
rodneykinney,
uckelman
From: <uck...@us...> - 2010-08-12 20:36:50
|
Revision: 7112 http://vassalengine.svn.sourceforge.net/vassalengine/?rev=7112&view=rev Author: uckelman Date: 2010-08-12 20:36:41 +0000 (Thu, 12 Aug 2010) Log Message: ----------- Connected news submission form to phpns. Modified Paths: -------------- site-src/trunk/site/news_submit.php Added Paths: ----------- site-src/trunk/site/util/newsbot-config.php.example Modified: site-src/trunk/site/news_submit.php =================================================================== --- site-src/trunk/site/news_submit.php 2010-08-12 14:02:26 UTC (rev 7111) +++ site-src/trunk/site/news_submit.php 2010-08-12 20:36:41 UTC (rev 7112) @@ -11,11 +11,10 @@ exit; } -$key = $_COOKIE['VASSAL_login']; +$key = isset($_COOKIE['VASSAL_login']) ? $_COOKIE['VASSAL_login'] : ''; -# sanitize the input -$headline = addslashes($_POST['headline']); -$text = addslashes($_POST['text']); +$headline = isset($_POST['headline']) ? $_POST['headline'] : ''; +$text = isset($_POST['text']) ? $_POST['text'] : ''; try { # check for blank headline @@ -38,16 +37,10 @@ $auth = new AuthDB(); $username = $auth->user_for_cookie($key); + submit_story($username, $headline, $text); - require_once('sso/NewsDB.php'); - - $news = new NewsDB(); - - $query = "INSERT INTO news (headline, text) VALUES ('$headline', '$text')"; - $news->write($query); - print_top($title); - echo '<p>Item submitted.</p>'; + echo '<p>Your news item has been submitted. It will appear in the news feed after approval by the news editor.</p>'; print_bottom(); exit; } @@ -83,5 +76,105 @@ END; } +# +# This is a shim for submitting items to phpns +# +function submit_story($username, $headline, $text) { + $ch = curl_init(); + $cfile = tempnam('/tmp', 'cookies'); + + # login + $url = 'http://localhost/news/login.php?do=p'; + + require_once('util/newsbot-config.php'); + + $data = array( + 'username' => NEWSBOT_USERNAME, + 'password' => NEWSBOT_PASSWORD, + 'remember' => 0 + ); + + curl_setopt_array($ch, array( + CURLOPT_URL => $url, + CURLOPT_HEADER => false, # don't need it + CURLOPT_RETURNTRANSFER => true, # prevent printing + CURLOPT_COOKIEJAR => $cfile, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $data, + CURLOPT_FOLLOWLOCATION => false # 302 is expected result + )); + + curl_exec($ch); + if (curl_errno($ch) != 0) { + throw new ErrorException('curl: ' . curl_error($ch)); + } + + # phpns redirects to index.php on success, so check for 302 + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + if ($code != 302) { + throw new ErrorException("login failed: $code"); + } + + # post an article + $url = 'http://localhost/news/article.php?do=p'; + + $data = array( + 'article_title' => $headline, +# 'article_subtitle' => '', + 'article_cat' => 'all', + 'article_text' => $text, +# 'article_exptext' => '', +# 'image' => '', +# 'start_date' => '', +# 'end_date' => '', +# 'acchecked' => '0', +# 'achecked' => '0', + ); + + curl_setopt_array($ch, array( + CURLOPT_URL => $url, + CURLOPT_HEADER => false, # don't need it + CURLOPT_RETURNTRANSFER => true, + CURLOPT_COOKIEFILE => $cfile, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $data, + CURLOPT_FOLLOWLOCATION => false + )); + + $result = curl_exec($ch); + if (curl_errno($ch) != 0) { + throw new ErrorException('curl: ' . curl_error($ch)); + } + + # check that posting succeeded + if (strpos($result, 'Article Success') === false) { + throw new ErrorException('posting failed'); + } + + # logout + $url = 'http://localhost/news/login.php?do=logout'; + + curl_setopt_array($ch, array( + CURLOPT_URL => $url, + CURLOPT_HEADER => false, # don't need it + CURLOPT_RETURNTRANSFER => true, + CURLOPT_COOKIEFILE => $cfile, + CURLOPT_HTTPGET => true, + CURLOPT_FOLLOWLOCATION => false # 302 is expected result + )); + + $result = curl_exec($ch); + if (curl_errno($ch) != 0) { + throw new ErrorException('curl: ' . curl_error($ch)); + } + + # cleanup + if (!unlink($cfile)) { + die("failed to delete $cfile"); + } + + curl_close($ch); +} + ?> Added: site-src/trunk/site/util/newsbot-config.php.example =================================================================== --- site-src/trunk/site/util/newsbot-config.php.example (rev 0) +++ site-src/trunk/site/util/newsbot-config.php.example 2010-08-12 20:36:41 UTC (rev 7112) @@ -0,0 +1,7 @@ +<?php + +# constants used by news bot +define('NEWSBOT_USERNAME', 'news_bot'); +define('NEWSBOT_PASSWORD', 'password'); + +?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ This SF.net email is sponsored by Make an app they can't live without Enter the BlackBerry Developer Challenge http://p.sf.net/sfu/RIM-dev2dev _______________________________________________ vassalengine-svn mailing list vas...@li... https://lists.sourceforge.net/lists/listinfo/vassalengine-svn |