strip.php
144 lines
| 4.7 KiB
| text/x-php
|
PhpLexer
/ include / strip.php
| r1 | <?php | |||
| // Book: The offset from 0 at the beginning of time | ||||
| // Page: The offset from 0 at the beginning of the volume | ||||
| r34 | class Strip | |||
| { | ||||
| public $id; | ||||
| public $old_id; | ||||
| public $published; | ||||
| public $media; | ||||
| public $type; | ||||
| public $title; | ||||
| public $book; | ||||
| public $page; | ||||
| r1 | } | |||
| // old_id is used to detect alterations to the strip id in forms. Not saved in database. | ||||
| // media is imagetype | ||||
| // Strip id is automatically incremented | ||||
| function insertstrip(&$strip) { | ||||
| r34 | global $dbConnection; | |||
| r4 | ||||
| r46 | $strip->book = ($strip->book == '') ? NULL : (int)$strip->book; | |||
| $strip->page = ($strip->page == '') ? NULL : (int)$strip->page; | ||||
| r4 | ||||
| r34 | $dbConnection->beginTransaction(); | |||
| $newid = $dbConnection->fetchColumn('SELECT MAX(id) FROM strip') + 1; | ||||
| $sql = 'INSERT INTO strip (id, published, media, type, title, book, page) VALUES(?, FROM_UNIXTIME(?), ?, ?, ?, ?, ?)'; | ||||
| $stmt = $dbConnection->prepare($sql); | ||||
| $stmt->bindValue(1, $newid); | ||||
| $stmt->bindValue(2, $strip->published, PDO::PARAM_INT); | ||||
| $stmt->bindValue(3, $strip->media, PDO::PARAM_INT); | ||||
| $stmt->bindValue(4, $strip->type, PDO::PARAM_INT); | ||||
| $stmt->bindValue(5, trim($strip->title)); | ||||
| $stmt->bindValue(6, $strip->book); | ||||
| $stmt->bindValue(7, $strip->page); | ||||
| $r = $stmt->execute(); | ||||
| r1 | if( !$r ) { | |||
| r34 | $dbConnection->rollback(); | |||
| r1 | return false; | |||
| } | ||||
| r34 | $dbConnection->commit(); | |||
| r1 | adminlog("Comic ".$newid." posted.", MTS_STRIP, MTA_ADD); | |||
| r4 | ||||
| r1 | $strip->id = $newid; | |||
| if( $strip->id == 0 ) return false; | ||||
| return true; | ||||
| r4 | } | |||
| r1 | ||||
| function updatestrip(&$strip) { | ||||
| r34 | global $dbConnection; | |||
| r4 | ||||
| r49 | $strip->book = ($strip->book === '') ? NULL : (int)$strip->book; | |||
| $strip->page = ($strip->page === '') ? NULL : (int)$strip->page; | ||||
| r4 | ||||
| r34 | $dbConnection->beginTransaction(); | |||
| $sql = 'UPDATE strip SET published = FROM_UNIXTIME(?), media = ?, type = ?, title = ?, book = ?, page = ? WHERE id = ?'; | ||||
| $stmt = $dbConnection->prepare($sql); | ||||
| $stmt->bindValue(1, $strip->published, PDO::PARAM_INT); | ||||
| $stmt->bindValue(2, $strip->media, PDO::PARAM_INT); | ||||
| $stmt->bindValue(3, $strip->type, PDO::PARAM_INT); | ||||
| $stmt->bindValue(4, trim($strip->title)); | ||||
| $stmt->bindValue(5, $strip->book, PDO::PARAM_INT); | ||||
| $stmt->bindValue(6, $strip->page, PDO::PARAM_INT); | ||||
| $stmt->bindValue(7, $strip->id, PDO::PARAM_INT); | ||||
| $stmt->execute(); | ||||
| $dbConnection->commit(); | ||||
| r1 | adminlog("Comic ".$strip->id." modified.", MTS_STRIP, MTA_MODIFY); | |||
| return true; | ||||
| r4 | } | |||
| r1 | ||||
| // Delete destination strip from DB and FS, and Update/Rename the source strip into place. Destructive Move! | ||||
| function move_strip($from_id, $to_id) | ||||
| { | ||||
| r34 | global $dbConnection; | |||
| r1 | $from_id = (int) $from_id; | |||
| $to_id = (int) $to_id; | ||||
| r4 | ||||
| r1 | // Ensure our source exists | |||
| r34 | $num_strips = $dbConnection->fetchColumn('SELECT COUNT(*) FROM strip WHERE id = ?', array($from_id)); | |||
| r1 | if($num_strips < 1) | |||
| mtdie("Cannot move strip number $from_id, because it cannot be found in database."); | ||||
| // Ready the destination | ||||
| deletestrip( $to_id ); | ||||
| r4 | ||||
| r1 | // Update database | |||
| r34 | $dbConnection->executeUpdate('UPDATE strip SET id = ? WHERE id = ?', array($to_id, $from_id)); | |||
| $strip = $dbConnection->executeQuery('SELECT strip.id, extension FROM strip, media_t WHERE media_t.id = strip.media AND strip.id = ?', array($to_id))->fetch(); | ||||
| r1 | ||||
| // Update filesystem | ||||
| foreach(glob(sprintf(SITE_PATH_ABS.'/'.SITE_STRIP.'/%04d.*', $from_id)) as $item) { | ||||
| preg_match('/\.(\w{3})$/', $item, $match) or die("Invalid filename: $item"); | ||||
| rename($item, sprintf(SITE_PATH_ABS.'/'.SITE_STRIP.'/%04d.%s', $to_id, $match[1])); | ||||
| } | ||||
| } | ||||
| // Classic swap function, using strip 0 as temporary storage. Can cause concurrency issues. FLOCK! | ||||
| function swap_strips( $from_id, $to_id ) { | ||||
| move_strip($from_id, 0); | ||||
| move_strip($to_id, $from_id); | ||||
| move_strip(0, $from_id ); | ||||
| adminlog("Comics ".$from_id." and ".$to_id." swapped.", MTS_STRIP, MTA_MODIFY); | ||||
| } | ||||
| function deletestrip($id) { | ||||
| $id = (int)$id; | ||||
| if ( !$id ) return false; | ||||
| r4 | ||||
| r34 | global $dbConnection; | |||
| $r = $dbConnection->executeUpdate('DELETE FROM strip WHERE id = ?', array($id)); | ||||
| r1 | foreach(glob(sprintf(SITE_PATH_ABS.'/'.SITE_STRIP.'/%04d*.*', $id)) as $item) | |||
| unlink($item); | ||||
| foreach(glob(sprintf(SITE_PATH_ABS.'/'.SITE_STRIP.'/restricted/%04d*.*', $id)) as $item) | ||||
| unlink($item); | ||||
| adminlog("Comic ".$id." deleted.", MTS_STRIP, MTA_DELETE); | ||||
| return $r; | ||||
| } | ||||
| function getstrip($id) { | ||||
| r34 | global $dbConnection; | |||
| return $dbConnection->executeQuery('SELECT id, UNIX_TIMESTAMP(published) as published, type, media, title, book, page FROM strip WHERE id = ?', array($id))->fetch(); | ||||
| r1 | } | |||
| function get_stripimage_filename( $strip ) { | ||||
| r34 | global $dbConnection; | |||
| $ext = $dbConnection->fetchColumn('SELECT extension FROM media_t WHERE id = ?', array($strip->media)); // filename extension | ||||
| r1 | return sprintf( '%s/%04d.%s', SITE_STRIP, $strip->id, $ext ); | |||
| } | ||||
| function get_stripid_by_rantid($rantid) { | ||||
| r34 | global $dbConnection; | |||
| return $dbConnection->fetchColumn('SELECT MAX(strip.id) FROM strip, rant WHERE strip.published <= rant.published AND rant.id = ?', array($rantid)); | ||||
| r1 | } | |||
| ?> | ||||
