PageRenderTime 67ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/Util/postdata.php

https://github.com/camlegleiter/Project-CyCal
PHP | 304 lines | 242 code | 16 blank | 46 comment | 79 complexity | c395b3848158505d91c74269e0323393 MD5 | raw file
  1. <?php
  2. /*
  3. =====================================
  4. INCLUDES
  5. =====================================
  6. */
  7. if (!isset($TO_ROOT))
  8. $TO_ROOT = "../";
  9. require $TO_ROOT."includes/membersOnly.php";
  10. require $TO_ROOT."includes/simple_html_dom.php";
  11. /*
  12. =====================================
  13. SENDING FUNCTIONS
  14. =====================================
  15. */
  16. function errorMessage($error){
  17. //for errors, use 409 error
  18. header("HTTP/1.1 409 ".$error);
  19. echo $error;
  20. exit;
  21. }
  22. function successMessage($success){
  23. echo $success;
  24. exit;
  25. }
  26. /*
  27. =====================================
  28. Error Testing
  29. =====================================
  30. */
  31. if($_POST['error'] || isset($_GET['error'])){
  32. errorMessage('Error message flag set');
  33. }
  34. if($_POST['success']){
  35. successMessage('Success message flag set');
  36. }
  37. /*
  38. =====================================
  39. GRAB POST DATA
  40. =====================================
  41. */
  42. $action = strtolower($_POST['action']);
  43. $userid = mysql_real_escape_string($_SESSION['id']);
  44. $_POST['rss'] = json_decode(stripslashes($_POST['rss']),true);
  45. $rss = $_POST['rss'];
  46. $posx = mysql_real_escape_string($_POST['posx']);
  47. $posy = mysql_real_escape_string($_POST['posy']);
  48. $sizex = mysql_real_escape_string($_POST['sizex']);
  49. $sizey = mysql_real_escape_string($_POST['sizey']);
  50. $themeid = mysql_real_escape_string($_POST['themeid']);
  51. $minimized = mysql_real_escape_string($_POST['minimized']);
  52. /*
  53. =====================================
  54. Print all
  55. =====================================
  56. */
  57. if($_POST['print']){
  58. successMessage(print_r($_POST, true));
  59. }
  60. /*
  61. =====================================
  62. SET DEFAULTS
  63. =====================================
  64. */
  65. if(!isset($action) || empty($action)){
  66. //Needs to throw error after done testing!
  67. $action = "add";
  68. //errorMessage("action must be specified: ['add','delete','edit', 'get']");
  69. }
  70. if(!isset($posx) || empty($posx)){
  71. $posx = 0;
  72. }
  73. if(!isset($posy) || empty($posy)){
  74. $posy = 0;
  75. }
  76. if(!isset($sizex) || empty($sizex)){
  77. $sizex = 500;
  78. }
  79. if(!isset($sizey) || empty($sizey)){
  80. $sizey = 400;
  81. }
  82. if(!isset($themeid) || empty($themeid)){
  83. $themeid = -1;
  84. }
  85. if(!isset($minimized) || empty($minimized)){
  86. $minimized = 0;
  87. }
  88. /*
  89. =====================================
  90. ERROR CHECKING
  91. =====================================
  92. */
  93. if (!is_numeric($posx))
  94. {
  95. errorMessage("posx is not an int");
  96. }
  97. if (!is_numeric($posy))
  98. {
  99. errorMessage("posy is not an int");
  100. }
  101. if (!is_numeric($sizex))
  102. {
  103. errorMessage("sizex is not an int");
  104. }
  105. if (!is_numeric($sizey))
  106. {
  107. errorMessage("sizey is not an int");
  108. }
  109. if (!is_numeric($themeid))
  110. {
  111. errorMessage("themeid is not an int");
  112. }
  113. if (!is_numeric($minimized))
  114. {
  115. errorMessage("minimized is not an int");
  116. }
  117. /*
  118. =====================================
  119. DO WORK
  120. =====================================
  121. */
  122. if ($action == "add")
  123. {
  124. $count = 0;
  125. foreach ($rss as $value)
  126. {
  127. $html = @file_get_html($value);
  128. if (!$html)
  129. errorMessage('Invalid RSS Feed:'.$value);
  130. $good = null;
  131. $rss = $html->find('rss',0);
  132. if($rss != null)
  133. {
  134. $chan = $rss->find('channel',0);
  135. if($chan != null)
  136. {
  137. $good = true;
  138. }
  139. }
  140. if($good == null){
  141. errorMessage('Invalid RSS Feed:'.$value);
  142. }
  143. $errorvalue = urlencode($value);
  144. $value = mysql_real_escape_string($errorvalue);
  145. mysql_query("INSERT INTO panel(userid,rss,posx,posy,sizex,sizey,themeid,minimized) VALUES ('$userid','$value','$posx','$posy','$sizex','$sizey','$themeid','$minimized')");
  146. $rows = mysql_affected_rows();
  147. if($rows != -1){
  148. $count++;
  149. }
  150. }
  151. if($count == 0){
  152. errorMessage("Those feeds are already on your page or you have not selected a feed.");
  153. }
  154. else{
  155. successMessage('');
  156. }
  157. }
  158. else if ($action == "delete")
  159. {
  160. $count = 0;
  161. foreach ($rss as $feed)
  162. {
  163. $feed = mysql_real_escape_string(urlencode($feed));
  164. $rssCheck = mysql_query("DELETE FROM panel WHERE userid='$userid' AND rss='$feed'");
  165. $rssCheckTheme = mysql_query("DELETE FROM theme WHERE userid='$userid' AND rss='$feed'");
  166. $rows = mysql_affected_rows();
  167. if($rows != 0){
  168. $count++;
  169. }
  170. }
  171. if($count == 0){
  172. errorMessage("Those feeds are not in our database.");
  173. }
  174. else{
  175. successMessage('');
  176. }
  177. }
  178. else if ($action == "edit")
  179. {
  180. if(count($rss) != 1){
  181. errorMessage("1 RSS feed must be given.");
  182. }
  183. //Get it so it matches the database
  184. $feed = mysql_real_escape_string(urlencode($rss[0]));
  185. if(mysql_query("UPDATE panel SET posx='$posx',posy='$posy', sizex='$sizex' , sizey='$sizey' WHERE userid='$userid' AND rss='$feed'")){
  186. successMessage('');
  187. }
  188. else{
  189. errorMessage("Failed to Update Panel");
  190. }
  191. }
  192. else if ($action == "get")
  193. {
  194. $panelTheme = "";
  195. if (!empty($rss[0]))
  196. {
  197. $panelTheme = "AND rss='".urlencode($rss[0])."'";
  198. }
  199. $getRSS = mysql_query("SELECT * FROM panel WHERE userid='$userid' $panelTheme");
  200. $rssarr = array();
  201. while ($row = mysql_fetch_assoc($getRSS))
  202. {
  203. $rss = array();
  204. $rss['rss'] = urldecode($row['rss']);
  205. $rss['posx'] = $row['posx'];
  206. $rss['posy'] = $row['posy'];
  207. $rss['sizex'] = $row['sizex'];
  208. $rss['sizey'] = $row['sizey'];
  209. $rss['themeid'] = array();
  210. if ($row['themeid'] == -1)
  211. {
  212. $rss['themeid']['type'] = "System";
  213. }
  214. else
  215. {
  216. $rss['themeid']['type'] = "User";
  217. $getTheme = mysql_query("SELECT * FROM theme WHERE userid='$userid' AND rss='".$row['rss']."'");
  218. $themeRow = mysql_fetch_assoc($getTheme);
  219. mysql_free_result($getTheme);
  220. $rss['themeid']['fontname'] = $themeRow['fontname'];
  221. $rss['themeid']['fontsize'] = $themeRow['fontsize'];
  222. $rss['themeid']['fontcolor'] = $themeRow['fontcolor'];
  223. $rss['themeid']['backcolor'] = $themeRow['backcolor'];
  224. $rss['themeid']['name'] = $themeRow['name'];
  225. }
  226. array_push($rssarr, $rss);
  227. }
  228. mysql_free_result($getRSS);
  229. successMessage(json_encode($rssarr));
  230. $rows = mysql_fetch_assoc($getRSS);
  231. mysql_free_result($rssCheck);
  232. successMessage(print_r($rows,true));
  233. }
  234. else if ($action == "settheme")
  235. {
  236. if(count($rss) != 1){
  237. errorMessage("1 RSS feed must be given.");
  238. }
  239. $feed = mysql_real_escape_string(urlencode($rss[0]));
  240. if ($themeid == -1)
  241. {
  242. mysql_query("UPDATE panel SET themeid='-1' WHERE userid='$userid' AND rss='$feed'");
  243. @mysql_query("DELETE FROM theme WHERE userid='$userid' AND rss='$feed'");
  244. successMessage("reset theme");
  245. }
  246. else
  247. {
  248. $fontname = mysql_real_escape_string($_POST['fontname']);
  249. $fontsize = mysql_real_escape_string($_POST['fontsize']);
  250. $fontcolor = mysql_real_escape_string($_POST['fontcolor']);
  251. $backcolor = mysql_real_escape_string($_POST['backcolor']);
  252. $name = mysql_real_escape_string($_POST['name']);
  253. if(!isset($fontname) || empty($fontname)){
  254. $fontname = "Verdana";
  255. }
  256. if(!isset($fontsize) || empty($fontsize)){
  257. $fontsize = 12;
  258. }
  259. if(!isset($fontcolor) || empty($fontcolor)){
  260. $fontcolor = "#000000";
  261. }
  262. if(!isset($backcolor) || empty($backcolor)){
  263. $backcolor = "#CCCCCC";
  264. }
  265. if(!isset($name) || empty($name)){
  266. $name = "";
  267. }
  268. mysql_query("UPDATE panel SET themeid='1' WHERE userid='$userid' AND rss='$feed'");
  269. //if (mysql_affected_rows() == 0)
  270. // errorMessage("Failed to update theme for: ".$rss[0]." (Error: 0)");
  271. mysql_query("UPDATE theme SET fontname='$fontname',fontsize='$fontsize',fontcolor='$fontcolor',backcolor='$backcolor',name='$name' WHERE userid='$userid' AND rss='$feed'");
  272. if (mysql_affected_rows() == 0)
  273. {
  274. mysql_query("INSERT INTO theme (userid,rss,fontname,fontsize,fontcolor,backcolor,name) VALUES ('$userid','$feed','$fontname','$fontsize','$fontcolor','$backcolor','$name')");
  275. if (mysql_affected_rows() == 0)
  276. {
  277. errorMessage("Failed to update theme for: ".$rss[0]." (Error: 1)");
  278. }
  279. successMessage("inserted new theme");
  280. }
  281. successMessage("updated new theme");
  282. }
  283. }
  284. else
  285. {
  286. errorMessage("action '".urlencode($action)."' is not valid");
  287. }
  288. ?>