PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/api.php

http://github.com/papr/JavaScript-Comments
PHP | 173 lines | 136 code | 31 blank | 6 comment | 58 complexity | 3d4a2c57615ef835571499f3c9531ca3 MD5 | raw file
  1. <?php
  2. /* ---------------------------------------------------------------- [Settings] */
  3. $jsonDatabaseFile = '72F887F9-7D09-43CC-ADD4-576D34960C2F.database';
  4. /* Should be changed by you */
  5. $adminUserName = "demo";
  6. $adminUserPassword = "demo"; /* Comparing will be made with md5 */
  7. for($tIndex = 0; $tIndex < 5; ++$tIndex) {
  8. $adminUserName = md5($adminUserName);
  9. $adminUserPassword = md5($adminUserPassword);
  10. }
  11. $encryptedUsername = $adminUserName;
  12. $encryptedPassword = $adminUserPassword;
  13. /* ---------------------------------------------------------------- [Classes] */
  14. class Comments {
  15. var $items;
  16. function addComment($comment) {
  17. array_push($this->items, $comment);
  18. }
  19. function allComments($databaseFile){
  20. if (is_readable($databaseFile)) {
  21. if (!$handle = fopen($databaseFile, 'r')) {
  22. echo "Cannot open file ($jsonfile)";
  23. exit;
  24. }
  25. $this->items = json_decode(fread($handle, filesize($databaseFile)));
  26. if(count($this->items) < 1) {
  27. $this->items = array();
  28. }
  29. }
  30. }
  31. }
  32. class Comment {
  33. var $name;
  34. var $email;
  35. var $text;
  36. var $creationDate;
  37. var $url;
  38. var $id;
  39. var $IP;
  40. function createComment($cName, $cEmail, $cText, $cURL, $cIP) {
  41. $this->name = $cName;
  42. $this->email = $cEmail;
  43. $this->text = $cText;
  44. $this->creationDate = time() * 1000;
  45. $this->id = uniqid();
  46. $this->url = $cURL;
  47. $this->IP = $cIP;
  48. }
  49. }
  50. function cmp( $a, $b ) {
  51. if( $a->creationDate == $b->creationDate ){ return 0 ; }
  52. return ($a->creationDate < $b->creationDate) ? -1 : 1;
  53. }
  54. /* ---------------------------------------------------------------- [Functions] */
  55. if($_GET['action'] == "auth") {
  56. if( $_POST['adminUsername'] == $encryptedUsername &&
  57. $_POST['adminPassword'] == $encryptedPassword) {
  58. /* header("HTTP/1.0 200 All Right"); */
  59. echo("All Right");
  60. } else {
  61. /* header("HTTP/1.0 401 Unauthorized"); */ // If sent Javascript will do anything...
  62. echo("Error: Unauthorized.");
  63. }
  64. }
  65. if (is_writable($jsonDatabaseFile) && is_readable($jsonDatabaseFile)) {
  66. if (!$handle = fopen($jsonDatabaseFile, 'a+')) {
  67. echo "Cannot open file ($jsonDatabaseFile)";
  68. exit;
  69. }
  70. $comments = new Comments;
  71. $comments->allComments($jsonDatabaseFile);
  72. if($_GET['action'] == "save" && $_POST['id'] == "E73BF175-F920-447D-993D-CE4169F17BCD") {
  73. $postedComment = new Comment;
  74. $postedComment->createComment($_POST['commentatorName'], $_POST['commentatorEmail'], $_POST['commentatorText'], $_POST['commentatorWebsite'], $_SERVER['REMOTE_ADDR']);
  75. if(!is_null($postedComment)) {
  76. $comments->addComment($postedComment);
  77. echo(json_encode($postedComment));
  78. } else {
  79. echo("Error: Comment could not be saved.");
  80. }
  81. } else if ($_GET['action'] == "save" && $_POST['id'] != "E73BF175-F920-447D-993D-CE4169F17BCD"){
  82. echo("Error: Unauthorized.");
  83. }
  84. if($_GET['action'] == "remove") {
  85. if( $_POST['adminUsername'] == $encryptedUsername &&
  86. $_POST['adminPassword'] == $encryptedPassword) {
  87. $postid = $_GET['id'];
  88. if($postid == "") {
  89. echo("Error: No ID sent or it is empty.");
  90. } else {
  91. for($i = 0; $i < count($comments->items); ++$i) {
  92. if($comments->items[$i]->id == $postid) {
  93. unset($comments->items[$i]);
  94. echo($postid);
  95. }
  96. }
  97. }
  98. } else {
  99. header("HTTP/1.0 401 Unauthorized");
  100. echo("Error: Unauthorized.");
  101. }
  102. }
  103. usort($comments->items,'cmp');
  104. if (!$handle = fopen($jsonDatabaseFile, 'w+')) {
  105. echo "Cannot open file ($jsonDatabaseFile)";
  106. exit;
  107. }
  108. $json = json_encode($comments->items);
  109. if (fwrite($handle, $json) === FALSE) {
  110. echo "Cannot write to file ($jsonDatabaseFile)";
  111. exit;
  112. }
  113. fclose($handle);
  114. if($_GET['action'] == "getDB") {
  115. if($_GET['showMails'] == "TRUE") {
  116. if( $_POST['adminUsername'] == $encryptedUsername &&
  117. $_POST['adminPassword'] == $encryptedPassword) {
  118. echo (json_encode($comments->items));
  119. } else {
  120. header("HTTP/1.0 401 Unauthorized");
  121. echo("Error: Unauthorized.");
  122. }
  123. } else {
  124. $itemsWithoutMail = array();
  125. foreach($comments->items as &$item) {
  126. $item->email = md5($item->email);
  127. $item->IP = md5(md5($item->IP));
  128. array_push(&$itemsWithoutMail, &$item);
  129. }
  130. echo (json_encode($itemsWithoutMail));
  131. }
  132. }
  133. } else {
  134. echo "The file $jsonDatabaseFile is not writable/readable";
  135. }
  136. if( $_GET['action'] != "auth" &&
  137. $_GET['action'] != "remove" &&
  138. $_GET['action'] != "save" &&
  139. $_GET['action'] != "getDB") {
  140. header("Location:api-doc.html");
  141. }
  142. ?>