rants.php
193 lines
| 4.9 KiB
| text/x-php
|
PhpLexer
/ include / rants.php
| r1 | <?php | |||
| r27 | class Rant | |||
| { | ||||
| public $id; | ||||
| public $published; | ||||
| public $status; | ||||
| public $side; | ||||
| public $author; | ||||
| public $title; | ||||
| public $body; | ||||
| public $link; | ||||
| public $imagetype; | ||||
| public $imagetext; | ||||
| r1 | } | |||
| r27 | function saverant($rant) | |||
| { | ||||
| if ($rant->id) | ||||
| r1 | return updaterant($rant); | |||
| else | ||||
| return insertrant($rant); | ||||
| } | ||||
| r27 | function insertrant($rant) | |||
| { | ||||
| global $dbConnection; | ||||
| $sql = 'INSERT INTO rant (published, status, side, author, title, body, link, imagetype, imagetext) VALUES ' . | ||||
| '(FROM_UNIXTIME(:published), :status, :side, :author, :title, :body, :link, :imagetype, :imagetext)'; | ||||
| $stmt = $dbConnection->prepare($sql); | ||||
| $stmt->bindValue('published', (int)$rant->published); | ||||
| $stmt->bindValue('status', $rant->status); | ||||
| $stmt->bindValue('side', $rant->side); | ||||
| $stmt->bindValue('author', (int)$rant->author); | ||||
| $stmt->bindValue('title', trim($rant->title)); | ||||
| $stmt->bindValue('body', trim($rant->body)); | ||||
| $stmt->bindValue('link', trim($rant->link)); | ||||
| $stmt->bindValue('imagetype', $rant->imagetype); | ||||
| $stmt->bindValue('imagetext', trim($rant->imagetext)); | ||||
| if ($stmt->execute()) | ||||
| { | ||||
| r1 | //logthis( 'Saved changes to rant ' . $rant->id ); | |||
| r27 | $rant->id = $dbConnection->lastInsertId(); | |||
| r4 | ||||
| r27 | adminlog("Rant " . $rant->id . " saved.", MTS_RANT, MTA_ADD); | |||
| r4 | ||||
| r27 | if ($rant->status == "published") | |||
| r1 | { | |||
| r27 | adminlog("Rant " . $rant->id . " published.", MTS_RANT, MTA_ADD); | |||
| /* | ||||
| r1 | $poster = get_userdatabyid($rant->author); | |||
| twitterpost("New rant posted by ".$poster->name.": ".SITE_HOST.SITE_PATH."/rant/".$rant->id); | ||||
| if($rant->author === 1) { | ||||
| tumblrpost($rant->title, $rant->body); | ||||
| } | ||||
| r27 | */ | |||
| r1 | } | |||
| r4 | ||||
| r1 | return $rant->id; | |||
| } | ||||
| r27 | ||||
| r1 | return false; | |||
| r4 | } | |||
| r1 | ||||
| r27 | function updaterant($rant) | |||
| { | ||||
| r1 | if ( !(int)$rant->id ) return false; | |||
| r27 | global $dbConnection; | |||
| # First, check if it's published already | ||||
| $sql = 'SELECT status FROM rant WHERE id = ?'; | ||||
| $stmt = $dbConnection->prepare($sql); | ||||
| $stmt->bindValue(1, $rant->id); | ||||
| $stmt->execute(); | ||||
| $status = $stmt->fetchColumn(); | ||||
| adminlog("Rant " . $rant->id . " updated.", MTS_RANT, MTA_UPDATE); | ||||
| $sql = 'UPDATE rant SET published = FROM_UNIXTIME(:published), status = :status, side = :side, author = :author, ' . | ||||
| 'title = :title, body = :body, link = :link, imagetype = :imagetype, imagetext = :imagetext WHERE id = :id'; | ||||
| $stmt = $dbConnection->prepare($sql); | ||||
| $stmt->bindValue('id', (int)$rant->id); | ||||
| $stmt->bindValue('published', (int)$rant->published); | ||||
| $stmt->bindValue('status', $rant->status); | ||||
| $stmt->bindValue('side', $rant->side); | ||||
| $stmt->bindValue('author', (int)$rant->author); | ||||
| $stmt->bindValue('title', trim($rant->title)); | ||||
| $stmt->bindValue('body', trim($rant->body)); | ||||
| $stmt->bindValue('link', trim($rant->link)); | ||||
| $stmt->bindValue('imagetype', (int)$rant->imagetype); | ||||
| $stmt->bindValue('imagetext', trim($rant->imagetext)); | ||||
| if ($status == "draft" && $rant->status == "published") | ||||
| r1 | { | |||
| r27 | adminlog("Rant " . $rant->id . " published.", MTS_RANT, MTA_UPDATE); | |||
| /* | ||||
| r1 | $poster = get_userdatabyid($rant->author); | |||
| twitterpost("New rant posted by ".$poster->name.": ".SITE_HOST.SITE_PATH."/rant/".$rant->id); | ||||
| if($rant->author === 1) { | ||||
| tumblrpost($rant->title, $rant->body); | ||||
| } | ||||
| r27 | */ | |||
| r1 | } | |||
| r4 | ||||
| r27 | return $stmt->execute(); | |||
| r1 | } | |||
| r27 | function deleterant($rantid) | |||
| { | ||||
| r1 | if ( !(int)$rantid ) return false; | |||
| r27 | global $dbConnection; | |||
| $sql = 'DELETE FROM rant WHERE id = ?'; | ||||
| $stmt = $dbConnection->prepare($sql); | ||||
| $stmt->bindValue(1, $rantid); | ||||
| adminlog("Rant " . $rantid . " deleted.", MTS_RANT, MTA_DELETE); | ||||
| return $stmt->execute(); | ||||
| r1 | } | |||
| function deleteattachment($id) | ||||
| { | ||||
| r27 | global $dbConnection; | |||
| // Remove attachment from filesystem | ||||
| $file = SITE_PATH_ABS . '/' . get_rantattachment_filename($id); | ||||
| unlink($file) or adminlog("Could not delete $file", MTS_RANT, MTA_DELETE, E_USER_WARNING); | ||||
| // Remove from database | ||||
| $sql = 'DELETE FROM rant_attachment WHERE id = ?'; | ||||
| $stmt = $dbConnection->prepare($sql); | ||||
| $stmt->bindValue(1, $id); | ||||
| $stmt->execute(); | ||||
| r1 | adminlog("Deleted attachment $id", MTS_RANT, MTA_DELETE); | |||
| } | ||||
| r27 | function getrant($id) | |||
| { | ||||
| global $dbConnection; | ||||
| $sql = 'SELECT id, UNIX_TIMESTAMP(published) as published, status, side, author, title, body, link, imagetype, imagetext FROM rant WHERE id = ?'; | ||||
| $stmt = $dbConnection->prepare($sql); | ||||
| $stmt->bindValue(1, (int)$id); | ||||
| $stmt->execute(); | ||||
| return $stmt->fetch(); | ||||
| r1 | } | |||
| r27 | function get_rantimage_filename($rant) | |||
| { | ||||
| global $dbConnection; | ||||
| $sql = 'SELECT extension FROM media_t WHERE id = ?'; | ||||
| $stmt = $dbConnection->prepare($sql); | ||||
| $stmt->bindValue(1, (int)$rant->imagetype); | ||||
| $stmt->execute(); | ||||
| $ext = $stmt->fetchColumn(); // filename extension | ||||
| return sprintf('%s/%04d.%s', SITE_RANT, (int)$rant->id, $ext); | ||||
| r1 | } | |||
| r27 | function get_rantattachment_filename($id) | |||
| { | ||||
| global $dbConnection; | ||||
| $sql = 'SELECT extension FROM media_t JOIN rant_attachment ra ON ra.media = media_t.id WHERE ra.id = ?'; | ||||
| $stmt = $dbConnection->prepare($sql); | ||||
| $stmt->bindValue(1, (int)$id); | ||||
| $stmt->execute(); | ||||
| $ext = $stmt->fetchColumn(); // filename extension | ||||
| return sprintf('%s/%d.%s', SITE_RANT_ATTACHMENT, (int)$id, $ext ); | ||||
| r1 | } | |||
| ?> | ||||
