Browse Source

Add DB stuffs

master
Chimo 4 years ago
parent
commit
4eb5abb254
4 changed files with 73 additions and 5 deletions
  1. 3
    0
      .gitignore
  2. 13
    0
      config.dist.php
  3. 45
    5
      index.php
  4. 12
    0
      schema.sql

+ 3
- 0
.gitignore View File

@@ -0,0 +1,3 @@
1
+config.php
2
+vendor
3
+*.log

+ 13
- 0
config.dist.php View File

@@ -0,0 +1,13 @@
1
+<?php
2
+$config = array(
3
+    "site" => array(
4
+        "host" => "example.org"
5
+    ),
6
+
7
+    "db" => array(
8
+        "host" => "localhost",
9
+        "name" => "webmentions",
10
+        "username" => "webmentions",
11
+        "password" => ""
12
+    )
13
+);

+ 45
- 5
index.php View File

@@ -70,11 +70,51 @@ if (stristr($sourceHTML, $targetURL)) {
70 70
 
71 71
     $on = '<a href="' . $entry_url . '">' . $entry_name . '</a>' ;
72 72
 
73
-    error_log( 
74
-        sprintf('This page has been mentioned %s on %s',
75
-            $by,
76
-            $on
77
-        ),
73
+    /*************
74
+     * DB Stuffs *
75
+     *************/
76
+
77
+    $dsn = 'mysql:dbname=' . $config['db']['name'] . ';host=' . $config['db']['host'];
78
+    $user = $config['db']['username'];
79
+    $password = $config['db']['password'];
80
+
81
+    try {
82
+        $dbh = new PDO($dsn, $user, $password);
83
+    } catch (PDOException $e) {
84
+        error_log('Connection failed: ' . $e->getMessage(), 4);
85
+        exit("Err");
86
+    }
87
+
88
+    $rendered = 'This page has been mentioned ' . $by . ' on ' . $on;
89
+
90
+    $sth = $dbh->prepare(
91
+        'INSERT INTO webmentions (local_url, remote_url, remote_name, remote_author, remote_author_url, rendered) ' .
92
+        'VALUES(?, ?, ?, ?, ?, ?)'
93
+    );
94
+
95
+    if ($sth === false) {
96
+        error_log("Could not prepare SQL statement.");
97
+        error_log("Error code: " . $dbh->errorCode());
98
+        error_log("Error info: " . $dbh->errorInfo());
99
+    }
100
+
101
+    $res = $sth->execute(array(
102
+        $target_url,
103
+        $entry_url,
104
+        $entry_name,
105
+        $author_name,
106
+        $author_url,
107
+        $rendered
108
+    ));
109
+
110
+    if ($res === false) {
111
+        error_log("Could not execute SQL query.");
112
+        error_log("Error code: " . $dbh->errorCode());
113
+        error_log("Error info: " . $dbh->errorInfo());
114
+    }
115
+
116
+    error_log(
117
+        $rendered,
78 118
         4
79 119
     );
80 120
 

+ 12
- 0
schema.sql View File

@@ -0,0 +1,12 @@
1
+CREATE TABLE `webmentions` (
2
+    `id` int(11) NOT NULL AUTO_INCREMENT,
3
+    `local_url` text NOT NULL,
4
+    `remote_url` text NOT NULL,
5
+    `remote_name` varchar(255) NOT NULL,
6
+    `remote_author` varchar(255),
7
+    `remote_author_url` text,
8
+    `rendered` text NOT NULL,
9
+    `created` datetime NOT NULL DEFAULT NOW(),
10
+    `last_checked` datetime NOT NULL DEFAULT NOW(),
11
+    PRIMARY KEY (`id`)
12
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Loading…
Cancel
Save