strip.php
130 lines
| 4.2 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 | ||||
| class Strip { | ||||
| var $id, $old_id, $published, $media, $type, $title, $book, $page; | ||||
| } | ||||
| // 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) { | ||||
| global $mtdb; | ||||
| r4 | ||||
| r1 | $strip->book = ($strip->book == '') ? 'NULL' : (int)$strip->book; | |||
| r4 | $strip->page = ($strip->page == '') ? 'NULL' : (int)$strip->page; | |||
| r1 | $mtdb->query('START TRANSACTION'); | |||
| $newid = $mtdb->getOne('SELECT MAX(id) FROM strip') + 1; | ||||
| $sql = 'INSERT INTO strip ( id, published, media, type, title, book, page ) VALUES (' | ||||
| . $newid | ||||
| . ', FROM_UNIXTIME(' . (int)$strip->published | ||||
| . '), '. (int)$strip->media | ||||
| . ', ' . (int)$strip->type | ||||
| r4 | . ', "' . mysqli_real_escape_string( $mtdb->link, trim($strip->title) ) | |||
| r1 | . '", '. $strip->book | |||
| . ', ' . $strip->page | ||||
| . ')'; | ||||
| r4 | ||||
| r1 | $r = $mtdb->query( $sql ); | |||
| if( !$r ) { | ||||
| $mtdb->query('ROLLBACK'); | ||||
| return false; | ||||
| } | ||||
| $mtdb->query('COMMIT'); | ||||
| 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) { | ||||
| global $mtdb; | ||||
| r4 | ||||
| r1 | $strip->book = ($strip->book === '') ? 'NULL' : (int)$strip->book; | |||
| $strip->page = ($strip->page === '') ? 'NULL' : (int)$strip->page; | ||||
| r4 | ||||
| r1 | $mtdb->query('START TRANSACTION'); | |||
| $sql = 'UPDATE strip SET | ||||
| published = FROM_UNIXTIME(' . (int)$strip->published .') | ||||
| , media = '. (int)$strip->media .' | ||||
| , type = ' . (int)$strip->type .' | ||||
| r4 | , title = "' . mysqli_real_escape_string( $mtdb->link, trim($strip->title) ) .'" | |||
| r1 | , book = ' . (int)$strip->book .' | |||
| , page = ' . (int)$strip->page .' | ||||
| WHERE id = ' . (int)$strip->id; | ||||
| $mtdb->query( $sql ); | ||||
| $mtdb->query('COMMIT'); | ||||
| 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) | ||||
| { | ||||
| global $mtdb; | ||||
| $from_id = (int) $from_id; | ||||
| $to_id = (int) $to_id; | ||||
| r4 | ||||
| r1 | // Ensure our source exists | |||
| $num_strips = $mtdb->getOne( "SELECT COUNT(*) FROM strip WHERE id = $from_id" ); | ||||
| 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 | |||
| $mtdb->query( "UPDATE strip SET id = $to_id WHERE id = $from_id" ); | ||||
| $strip = $mtdb->getRow( "SELECT strip.id, extension FROM strip, media_t WHERE media_t.id = strip.media AND strip.id = $to_id" ); | ||||
| // 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 | ||||
| r1 | global $mtdb; | |||
| $r = $mtdb->query( 'DELETE FROM strip WHERE id=' . $id ); | ||||
| 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) { | ||||
| global $mtdb; | ||||
| return $mtdb->getRow( 'SELECT id, UNIX_TIMESTAMP(published) as published, type, media, title, book, page FROM strip WHERE id=' . (int)$id); | ||||
| } | ||||
| function get_stripimage_filename( $strip ) { | ||||
| global $mtdb; | ||||
| $ext = $mtdb->getOne( 'SELECT extension FROM media_t WHERE id=' . (int)$strip->media ); // filename extension | ||||
| return sprintf( '%s/%04d.%s', SITE_STRIP, $strip->id, $ext ); | ||||
| } | ||||
| function get_stripid_by_rantid($rantid) { | ||||
| global $mtdb; | ||||
| return $mtdb->getOne('SELECT MAX(strip.id) FROM strip,rant WHERE strip.published<=rant.published AND rant.id=' . (int)$rantid); | ||||
| } | ||||
| ?> | ||||
