Advertisement
Guest User

Untitled

a guest
Aug 1st, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. <?php
  2.  
  3. class NewsScript
  4. {
  5. var $maxLength,
  6. $lim, /* Num items to show */
  7. $template, /* Template to use */
  8. $templateView,
  9. $db_name, /* DB Settings */
  10. $db_host,
  11. $db_user,
  12. $db_password,
  13. $connection;
  14.  
  15. /**
  16. * Constructor, executed when we make a new object.
  17. *
  18. * @return NewsScript Object
  19. */
  20. function NewsScript()
  21. {
  22. // Import Database settings etc.
  23. require('config.php');
  24.  
  25. $this->db_host = $db_host;
  26. $this->db_name = $db_name;
  27. $this->db_user = $db_user;
  28. $this->db_password = $db_password;
  29. $this->lim = $lim;
  30. $this->template = $template;
  31. $this->templateView = $templateView;
  32. }
  33.  
  34. /**
  35. * Display ALL news articles
  36. *
  37. */
  38.  
  39. function trimlink($text, $length)
  40. {
  41. $dec = array("&", "\"", "'", "\\", '\"', "\'", "<", ">");
  42. $enc = array("&;", "&quot;", "&#39;", "&#92;", "&quot;", "&#39;", "&lt;", "&gt;");
  43. $text = str_replace($enc, $dec, $text);
  44. if (strlen($text) > $length) $text = substr($text, 0, ($length-3))."...";
  45. $text = str_replace($dec, $enc, $text);
  46. return $text;
  47. }
  48.  
  49. function displayAll()
  50. {
  51. $this->connect();
  52.  
  53. // Use the limit in our settings to decide how many to show
  54. $result = mysql_query("SELECT * FROM newstbl ORDER BY date DESC LIMIT 0, $this->lim");
  55.  
  56. /* While we have more rows display them */
  57. while($row = mysql_fetch_assoc($result))
  58. {
  59. // Display the row
  60. $id = $row['id'];
  61. $author = $row['author'];
  62. $content = $row['content'];
  63. $date = $this->convertDate($row['date']);
  64. $name = $row['name'];
  65. $png = $row['png'];
  66.  
  67. /*
  68. Variables are used in the template file to display news info
  69. */
  70. require($this->template);
  71.  
  72. }
  73.  
  74. $this->close();
  75. }
  76.  
  77. /**
  78. * Display a single news item given it's ID.
  79. *
  80. * @param $id - ID of item to display
  81. * @return true/false
  82. */
  83. function displayItem($id)
  84. {
  85. if(is_numeric($id))
  86. {
  87. $this->connect();
  88.  
  89. $result = mysql_query("SELECT * FROM newstbl WHERE id = '$id'");
  90. $row = mysql_fetch_assoc($result);
  91.  
  92. $id = $row['id'];
  93. $author = $row['author'];
  94. $content = $row['content'];
  95. $date = $this->convertDate($row['date']);
  96. $name = $row['name'];
  97. $png = $row['png'];
  98.  
  99. require($this->templateView); // Show item with our template.
  100.  
  101. $this->close();
  102. return true;
  103. }
  104. else return false; // Not valid number
  105.  
  106. }
  107.  
  108. /**
  109. * Add a News Item to the DB
  110. *
  111. * @return true/false (true if we are successful)
  112. */
  113. function addNews($author, $content, $name, $png)
  114. {
  115. $this->connect();
  116.  
  117. // Use MySQL CURDATE function to get the current Date.
  118. $sql = sprintf("INSERT INTO newstbl (author, content ,date ,name, png) VALUES ('%s','%s',CURDATE(),'%s','%s')",
  119. $this->clean($author), $this->clean($content), $this->clean($name), $this->clean($png));
  120.  
  121.  
  122. $result = mysql_query($sql, $this->connection);
  123. $this->close();
  124.  
  125. // are we successful?
  126. if($result)
  127. {
  128. return true;
  129. }
  130. else return false;
  131. }
  132.  
  133. /**
  134. * Deletes the given News Item
  135. *
  136. * @return true or false (false if error)
  137. */
  138. function deleteNews($id)
  139. {
  140. // Only allow numbers for ID
  141. if(is_numeric($id))
  142. {
  143. $this->connect();
  144.  
  145. $result = mysql_query("DELETE FROM newstbl WHERE id='$id'");
  146.  
  147. $this->close();
  148.  
  149. // are we successful?
  150. if($result)
  151. {
  152. return true;//yes
  153. }
  154. else return false;//no
  155.  
  156. }
  157. else return false; // None numeric value
  158.  
  159. }
  160.  
  161. /**
  162. * update a news atricle
  163. *
  164. * @param int $id
  165. * @param str $author
  166. * @param str $content
  167. *
  168. * @return true/false (successful?)
  169. */
  170. function updateNews($id, $author, $content)
  171. {
  172. if(is_numeric($id))
  173. {
  174. $this->connect();
  175.  
  176. $sql = sprintf("UPDATE newstbl SET author='%s', content='%s' WHERE id='%d'",
  177. $this->clean($author), $this->clean($content), $id);
  178.  
  179. $result = mysql_query($sql);
  180.  
  181. $this->close();
  182.  
  183. // are we successful?
  184. if($result)
  185. {
  186. return true;//yes
  187. }
  188. else return false;//no
  189. }
  190. return false;
  191. }
  192.  
  193. /**
  194. * Connect to DB
  195. *
  196. * @return true/false
  197. */
  198. function connect()
  199. {
  200. $this->connection = mysql_connect($this->db_host, $this->db_user, $this->db_password)
  201. or die("Unable to connect to MySQL");
  202.  
  203. mysql_select_db($this->db_name, $this->connection) or die("Unable to select DB!");
  204.  
  205. // Valid connection object?
  206. if(!$this->connection)
  207. {
  208. return false;
  209. }
  210. else return true;
  211. }
  212.  
  213. /**
  214. * close DB Connection
  215. *
  216. */
  217. function close()
  218. {
  219. mysql_close($this->connection);
  220. }
  221.  
  222. /**
  223. * Convert from MySQL Date format: yyyy-mm-dd
  224. * into dd/mm/yyyy
  225. *
  226. * @param MySQL $date
  227. * @return string dd/mm/yyyy
  228. */
  229. function convertDate($date)
  230. {
  231. $date_array = explode("-",$date); // split the array
  232. $y = $date_array[0];
  233. $m = $date_array[1];
  234. $d = $date_array[2];
  235.  
  236. return $d . "/" . $m . "/" . $y;
  237. }
  238.  
  239. /**
  240. * Cleans a string for input into a MySQL Database.
  241. * Gets rid of unwanted characters/SQL injection etc.
  242. *
  243. * @return string
  244. */
  245. function clean($str)
  246. {
  247. // Only remove slashes if it's already been slashed by PHP
  248. if(get_magic_quotes_gpc())
  249. {
  250. $str = stripslashes($str);
  251. }
  252. // Let MySQL remove nasty characters.
  253. $str = mysql_real_escape_string($str);
  254.  
  255. return $str;
  256. }
  257.  
  258. /**
  259. * Replace smiley text in the given text with
  260. * link to the actual smiley image.
  261. */
  262. function addSmiley($msg)
  263. {
  264. // example replacing ':)' with the smiley image file.
  265. $new_msg = str_replace(":)",'<img src="smileys/smiley1.gif">', $msg);
  266. return $new_msg;
  267. }
  268.  
  269.  
  270. }
  271.  
  272. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement