Guest User

Untitled

a guest
May 27th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.41 KB | None | 0 0
  1. <?php
  2. // Database Config and Methods
  3. class Database {
  4. var $config = array(
  5. 'old' => array(
  6. 'username' => '********',
  7. 'password' => '********',
  8. 'hostname' => '********',
  9. 'database' => '********'
  10. ),
  11. 'default' => array(
  12. 'username' => '********',
  13. 'password' => '********',
  14. 'hostname' => '********',
  15. 'database' => '********'
  16. ),
  17. 'dev' => array(
  18. 'username' => '********',
  19. 'password' => '********',
  20. 'hostname' => '********',
  21. 'database' => '********'
  22. )
  23. );
  24. var $conn = null; // Placeholder for the SQL Connection object
  25.  
  26. function __construct($config = 'dev') {
  27. $this->connect($config); // Connect to the default database
  28. }
  29.  
  30. /**
  31. * Connect to the specified database config, or default
  32. */
  33. function connect($config = 'dev') {
  34. if (is_null($this->conn)) {
  35. $db = $this->config[$config];
  36. if (empty($db)) {
  37. die('Invalid database configuration');
  38. }
  39. $this->conn = mysql_connect($db['hostname'], $db['username'], $db['password']);
  40. if(!$this->conn) {
  41. die("Cannot connect to database server");
  42. }
  43. if(!mysql_select_db($db['database'])) {
  44. die("Cannot select database");
  45. }
  46. }
  47. return $this->conn;
  48. }
  49. }
  50.  
  51. ?>
  52. <?php
  53. /**
  54. * Contains all of the Model Classes
  55. *
  56. * @author Trae Robrock
  57. * @version $Id$
  58. * @copyright Trae Robrock, 12 August, 2009
  59. * @package ocsa.libs.models
  60. **/
  61.  
  62. /**
  63. * Define DocBlock
  64. **/
  65.  
  66. require_once('database.php');
  67.  
  68. /**
  69. * The Model for handling any calls to or from the Meetings table
  70. */
  71. class Meetings {
  72. var $config = array(
  73. 'table' => 'meetings',
  74. 'default_time' => '6:00 PM',
  75. );
  76.  
  77. function __construct() {
  78. $db = new Database();
  79. $conn = $db->connect();
  80.  
  81. $table = $this->config['table'];
  82. $sql = "DESCRIBE `$table`";
  83. $query = mysql_query($sql);
  84. if (!$query) {
  85. die('Could not connect to the database table!');
  86. }
  87. }
  88.  
  89. /**
  90. *
  91. * Get all the meetings in the database
  92. * $sort either ASC or DESC
  93. * Returns an associative array
  94. *
  95. */
  96. function getAll($sort = 'ASC') {
  97. $db = new Database();
  98. $conn = $db->connect();
  99.  
  100. if ((!$sort == 'DESC') && (!$sort == 'ASC')) {
  101. $sort = 'ASC';
  102. // Might want to log a possible malicious user here
  103. }
  104. $table = $this->config['table'];
  105. $sql = "SELECT * FROM `$table` ORDER BY `date` $sort";
  106. $result = mysql_query($sql);
  107. if (!$result) {
  108. die('Error with your query. ' . mysql_error());
  109. }
  110.  
  111. $return = array();
  112. while ($row = mysql_fetch_assoc($result)) {
  113. $return['Meetings'][] = $row;
  114. }
  115.  
  116. return $return;
  117. }
  118.  
  119. /**
  120. *
  121. * Get all the meetings for a specific date
  122. * $month integer 1 through 12
  123. * $sort works like getAll()
  124. * Returns an associative array
  125. *
  126. */
  127. function getByMonth($month = '', $sort = 'ASC') {
  128. $db = new Database();
  129. $conn = $db->connect();
  130.  
  131. if (empty($month)) {
  132. return false;
  133. }
  134. $current_year = date('Y', time());
  135. $next_month = (($month + 1) > 12) ? 1 : ($month + 1);
  136. $start_time = mktime(0,0,0,$month,1,$current_year);
  137. if ($next_month == 1) { $current_year++; }
  138. $end_time = mktime(0,0,0,$next_month,1,$current_year);
  139.  
  140. $sql = "SELECT * FROM `{$this->config['table']}` WHERE `date` >= FROM_UNIXTIME($start_time) AND `date` < FROM_UNIXTIME($end_time)";
  141.  
  142. $result = mysql_query($sql);
  143. if (!$result) {
  144. die('Error with your query. ' . mysql_error());
  145. }
  146.  
  147. $return = array();
  148. while ($row = mysql_fetch_assoc($result)) {
  149. $return['Meetings'][] = $row;
  150. }
  151.  
  152. return $return;
  153. }
  154.  
  155. /**
  156. *
  157. * Saves a new meeting
  158. * $date String compatible with strtotime()
  159. * $location String
  160. *
  161. */
  162. function saveMeeting($date, $location) {
  163. $db = new Database();
  164. $conn = $db->connect();
  165.  
  166. $date = strtotime($date);
  167. if (!$date) {
  168. die('Invalid Date Format!');
  169. }
  170.  
  171. if (date('H:i', $date) == '00:00') {
  172. $defTime = strtotime($this->config['default_time']);
  173. $date = mktime(date('H', $defTime), date('i', $defTime), 0, date('n', $date), date('j', $date), date('Y', $date));
  174. }
  175.  
  176. $location = mysql_escape_string(trim($location));
  177.  
  178. $sql = "INSERT INTO `{$this->config['table']}` (`date`, `location`) VALUES (FROM_UNIXTIME($date), '$location')";
  179. $result = mysql_query($sql);
  180. if (!$result) {
  181. return false;
  182. }
  183. else {
  184. $sql = "SELECT * FROM `{$this->config['table']}` WHERE `id` = LAST_INSERT_ID()";
  185. $result = mysql_query($sql);
  186. $return = mysql_fetch_row($result);
  187. return $return;
  188. }
  189. }
  190.  
  191. /**
  192. *
  193. * Build the HTML for the Month list with links
  194. *
  195. */
  196. function buildMonthList() {
  197. echo "<div id='meetings-list-months'>";
  198. echo "<a href='index.php'>All</a>\n";
  199. for ($i = 1; $i <= 12; $i++) {
  200. $month = date('M', mktime(0,0,0,$i,1,date('Y', time())));
  201. $data = $this->getByMonth($i);
  202. if (empty($data)) {
  203. echo "$month\n";
  204. }
  205. else {
  206. echo "<a href='index.php?m=$i'>$month</a>\n";
  207. }
  208. }
  209. echo "</div>";
  210. }
  211.  
  212. /**
  213. *
  214. * Deletes a meeting by its ID
  215. *
  216. */
  217. function deleteById($id = null) {
  218. $db = new Database();
  219. $conn = $db->connect();
  220.  
  221. if (!empty($id) && is_numeric($id)) {
  222. $sql = "DELETE FROM `{$this->config['table']}` WHERE `id` = $id";
  223. if (!mysql_query($sql)) {
  224. die('Couldnt delete the meeting');
  225. }
  226. else {
  227. return true;
  228. }
  229. }
  230. }
  231.  
  232. /**
  233. *
  234. * Build HTML for Meeting list
  235. *
  236. */
  237. function buildMeetingList($month = null) {
  238. echo '<table id="meetings-list-table"><tr><th>Date</th><th>Location</th></tr>';
  239. if ((!empty($month)) && (is_numeric($month)) && ($month > 0) && ($month <= 12)) {
  240. $month = trim($_GET['m']);
  241. $data = $this->getByMonth($month);
  242. }
  243. else {
  244. $data = $this->getAll();
  245. }
  246.  
  247. foreach($data['Meetings'] as $meeting) {
  248. $timestamp = strtotime($meeting['date']);
  249. $meeting['date'] = date('M d, Y h:ia', $timestamp);
  250. echo '<tr class=\'meeting-entry\' id=\'' . $meeting['id'] . '\'>';
  251. echo "<td>{$meeting['date']}</td>";
  252. echo "<td>{$meeting['location']}</td>";
  253. echo '</tr>';
  254. }
  255. echo '</table>';
  256. }
  257. }
  258.  
  259. /**
  260. * The Model for handling any calls to or from the Officers table
  261. */
  262. class Officers {
  263. var $config = array(
  264. 'table' => 'officers'
  265. );
  266.  
  267. function __construct() {
  268. $db = new Database();
  269. $conn = $db->connect();
  270.  
  271. $sql = "DESCRIBE `{$this->config['table']}`";
  272. $query = mysql_query($sql);
  273. if (!$query) {
  274. die('Could not connect to the database table!');
  275. }
  276. }
  277.  
  278. function viewAll()
  279. {
  280. $db = new Database();
  281. $conn = $db->connect();
  282.  
  283. $sql = "SELECT * FROM `{$this->config['table']}`";
  284. $result = mysql_query($sql);
  285. if (!$result) {
  286. die('Error with your query. ' . mysql_error());
  287. }
  288.  
  289. $return = array();
  290. while ($row = mysql_fetch_assoc($result)) {
  291. $return['Officers'][] = $row;
  292. }
  293.  
  294. return $return;
  295. }
  296.  
  297. function eraseAll()
  298. {
  299. $db = new Database();
  300. $conn = $db->connect();
  301.  
  302. $sql = "DELETE FROM `{$this->config['table']}`";
  303. var_dump($sql);
  304. $result = mysql_query($sql);
  305. var_dump($result);
  306. if (!$result) {
  307. die('Error with your query. ' . mysql_error());
  308. }
  309. else {
  310. return true;
  311. }
  312. }
  313.  
  314. function saveOfficer($first_name = null, $last_name = null, $email = null, $position = null)
  315. {
  316. $db = new Database();
  317. $conn = $db->connect();
  318.  
  319. if (!empty($first_name) && !empty($last_name) && !empty($email) && !empty($position)) {
  320. $sql = "INSERT INTO {$this->config['table']} (`id`, `first_name`, `last_name`, `email`, `position`) VALUES (NULL, '$first_name', '$last_name', '$email', '$position')";
  321. $result = mysql_query($sql);
  322. if (!$result) {
  323. return false;
  324. }
  325. else {
  326. return true;
  327. }
  328. }
  329. else {
  330. return false;
  331. }
  332. }
  333. }
  334.  
  335. /**
  336. * The model for handling any calls for the News table
  337. *
  338. * @package ocsa.libs.models.News
  339. * @author Trae Robrock
  340. **/
  341. class News
  342. {
  343. /**
  344. * The configuration var, holds any config specific to this model
  345. *
  346. * @var array
  347. **/
  348. var $config = array(
  349. 'table' => 'news',
  350. 'legacy' => array(
  351. 'active' => true,
  352. 'table' => 'spotlight',
  353. 'fields' => array(
  354. 'content' => 'text',
  355. ),
  356. ),
  357. );
  358.  
  359. /**
  360. * Constructor
  361. *
  362. * Tries connecting to the database, dies on failure
  363. *
  364. * @return void
  365. * @author Trae Robrock
  366. **/
  367. function __construct()
  368. {
  369. /**
  370. * Adds a little functionality to support the legacy system
  371. * This can be switched off once the site is refactored to use
  372. * The new database
  373. */
  374. if ($this->config['legacy']['active'] === true) {
  375. return;
  376. }
  377.  
  378. $db = new Database();
  379. $conn = $db->connect();
  380.  
  381. $sql = "DESCRIBE `{$this->config['table']}`";
  382. $query = mysql_query($sql);
  383. if (!$query) {
  384. die('Could not connect to the database table!');
  385. }
  386. }
  387.  
  388. /**
  389. * Gets all entries from the table and returns them as an array
  390. *
  391. * @return array Contains all the table entries
  392. * @author Trae Robrock
  393. **/
  394. function viewAll()
  395. {
  396. /**
  397. * Adds a little functionality to support the legacy system
  398. * This can be switched off once the site is refactored to use
  399. * The new database
  400. */
  401. if ($this->config['legacy']['active'] === true) {
  402. return $this->legacyViewAll();
  403. }
  404.  
  405. $db = new Database();
  406. $conn = $db->connect();
  407.  
  408. $sql = "SELECT * FROM `{$this->config['table']}` ORDER BY `id` DESC";
  409. $result = mysql_query($sql);
  410. if (!$result) {
  411. die('Error with your query. ' . mysql_error());
  412. }
  413.  
  414. $return = array();
  415. while ($row = mysql_fetch_assoc($result)) {
  416. $return['News'][] = $row;
  417. }
  418.  
  419. return $return;
  420. }
  421.  
  422. /**
  423. * Saves a new entry to the table
  424. *
  425. * @return boolean True on success, false on failure
  426. * @param string $title the title of the new story to add
  427. * @param string $content any content for the new story to add
  428. * @author Trae Robrock
  429. **/
  430. function saveNew($title = null, $content = null)
  431. {
  432. /**
  433. * Adds a little functionality to support the legacy system
  434. * This can be switched off once the site is refactored to use
  435. * The new database
  436. */
  437. if ($this->config['legacy']['active'] === true) {
  438. return $this->legacySaveNew($title, $content);
  439. }
  440.  
  441. $db = new Database();
  442. $conn = $db->connect();
  443.  
  444. $time = time();
  445.  
  446. if (!empty($title) && !empty($content)) {
  447. $sql = "INSERT INTO {$this->config['table']} (`id`, `date`, `title`, `content`) VALUES (NULL, FROM_UNIXTIME($time), '$title', '$content')";
  448. $result = mysql_query($sql);
  449. if (!$result) {
  450. return false;
  451. }
  452. else {
  453. return true;
  454. }
  455. }
  456. else {
  457. return false;
  458. }
  459. }
  460.  
  461. /**
  462. * This does the same as viewAll, but supports the legacy system's format
  463. *
  464. * @return array Contains all the table entries
  465. * @author Trae Robrock
  466. **/
  467. function legacyViewAll()
  468. {
  469. $db = new Database('old');
  470. $conn = $db->connect('old');
  471.  
  472. $sql = "SELECT * FROM `{$this->config['legacy']['table']}` ORDER BY `id` DESC";
  473. $result = mysql_query($sql);
  474. if (!$result) {
  475. die('Error with your query. ' . mysql_error());
  476. }
  477.  
  478. $return = array();
  479. while ($row = mysql_fetch_assoc($result)) {
  480. $return['News'][] = $row;
  481. }
  482.  
  483. return $this->legacyConvertFields($return);
  484. }
  485.  
  486. /**
  487. * This does the same as saveNew, but supports the legacy system's format
  488. *
  489. * @return boolean True on success, False on failure
  490. * @param string $title the title of the new story to add
  491. * @param string $content any content for the new story to add
  492. * @author Trae Robrock
  493. **/
  494. function legacySaveNew($title = null, $content = null)
  495. {
  496. $db = new Database('old');
  497. $conn = $db->connect('old');
  498.  
  499. $time = time();
  500.  
  501. $sql = "UPDATE {$this->config['legacy']['table']} SET `active` = 0 WHERE 1=1";
  502. $result = mysql_query($sql);
  503. if (!$result) {
  504. return false;
  505. }
  506.  
  507. if (!empty($title) && !empty($content)) {
  508. $sql = "INSERT INTO {$this->config['legacy']['table']} (`id`, `date`, `title`, `text`, `active`) VALUES (NULL, '$time', '$title', '$content', '1')";
  509. $result = mysql_query($sql);
  510. if (!$result) {
  511. return false;
  512. }
  513. else {
  514. return true;
  515. }
  516. }
  517. else {
  518. return false;
  519. }
  520. }
  521.  
  522. /**
  523. * This will take in a standard $data container filled with rows of table data
  524. * and convert the field names to be readable by the newer system files
  525. *
  526. * @return array Containing the original $data array with usable field names
  527. * @param array $data Standard data array with table data
  528. * @author Trae Robrock
  529. **/
  530. function legacyConvertFields($data = array())
  531. {
  532. $keys = array_keys($data);
  533. foreach ($keys as $key) {
  534. foreach ($data[$key] as $rowNumber => $rowData) {
  535. foreach ($rowData as $fieldName => $fieldData) {
  536. if ($newFieldName = array_search($fieldName, $this->config['legacy']['fields'])) {
  537. unset($data[$key][$rowNumber][$fieldName]);
  538. $data[$key][$rowNumber][$newFieldName] = $fieldData;
  539. }
  540. if ($fieldName == 'date') {
  541. $data[$key][$rowNumber][$fieldName] = date('m.d.Y', $fieldData);
  542. }
  543. }
  544. }
  545. }
  546.  
  547. return $data;
  548. }
  549.  
  550. } // END class News
  551.  
  552. ?>
Add Comment
Please, Sign In to add comment