コード例 #1
0
ファイル: DatabaseManager.php プロジェクト: unpush/p2-php
 /**
  * 画像を削除
  */
 public static function remove($target, $to_blacklist = false)
 {
     $removed_files = array();
     if (empty($target)) {
         return $removed_files;
     }
     if (!is_array($target)) {
         if (is_integer($target) || ctype_digit($target)) {
             $id = (int) $target;
             if ($id > 0) {
                 $target = array($id);
             } else {
                 return $removed_files;
             }
         } else {
             P2Util::pushInfoHtml('<p>WARNING! IC2_DatabaseManager::remove(): 不正な引数</p>');
             return $removed_files;
         }
     }
     // トランザクションの開始
     $ta = new IC2_DataObject_Images();
     $db = $ta->getDatabaseConnection();
     if ($db->phptype == 'pgsql') {
         $ta->query('BEGIN');
     } elseif ($db->phptype == 'sqlite') {
         $db->query('BEGIN;');
     }
     // 画像を削除
     foreach ($target as $id) {
         $icdb = new IC2_DataObject_Images();
         $icdb->whereAdd("id = {$id}");
         if ($icdb->find(true)) {
             // キャッシュしているファイルを削除
             $t1 = new IC2_Thumbnailer(IC2_Thumbnailer::SIZE_PC);
             $t2 = new IC2_Thumbnailer(IC2_Thumbnailer::SIZE_MOBILE);
             $t3 = new IC2_Thumbnailer(IC2_Thumbnailer::SIZE_INTERMD);
             $srcPath = $t1->srcPath($icdb->size, $icdb->md5, $icdb->mime);
             $t1Path = $t1->thumbPath($icdb->size, $icdb->md5, $icdb->mime);
             $t2Path = $t2->thumbPath($icdb->size, $icdb->md5, $icdb->mime);
             $t3Path = $t3->thumbPath($icdb->size, $icdb->md5, $icdb->mime);
             if (file_exists($srcPath)) {
                 unlink($srcPath);
                 $removed_files[] = $srcPath;
             }
             if (file_exists($t1Path)) {
                 unlink($t1Path);
                 $removed_files[] = $t1Path;
             }
             if (file_exists($t2Path)) {
                 unlink($t2Path);
                 $removed_files[] = $t2Path;
             }
             if (file_exists($t3Path)) {
                 unlink($t3Path);
                 $removed_files[] = $t3Path;
             }
             // ブラックリスト送りの準備
             if ($to_blacklist) {
                 $_blacklist = new IC2_DataObject_BlackList();
                 $_blacklist->size = $icdb->size;
                 $_blacklist->md5 = $icdb->md5;
                 if ($icdb->mime == 'clamscan/infected' || $icdb->rank == -4) {
                     $_blacklist->type = 2;
                 } elseif ($icdb->rank < 0) {
                     $_blacklist->type = 1;
                 } else {
                     $_blacklist->type = 0;
                 }
             }
             // 同一画像を検索
             $remover = new IC2_DataObject_Images();
             $remover->whereAddQuoted('size', '=', $icdb->size);
             $remover->whereAddQuoted('md5', '=', $icdb->md5);
             //$remover->whereAddQuoted('mime', '=', $icdb->mime); // SizeとMD5で十分
             $remover->find();
             while ($remover->fetch()) {
                 // ブラックリスト送りにする
                 if ($to_blacklist) {
                     $blacklist = clone $_blacklist;
                     $blacklist->uri = $remover->uri;
                     $blacklist->insert();
                 }
                 // テーブルから抹消
                 $remover->delete();
             }
         }
     }
     // トランザクションのコミット
     if ($db->phptype == 'pgsql') {
         $ta->query('COMMIT');
     } elseif ($db->phptype == 'sqlite') {
         $db->query('COMMIT;');
     }
     return $removed_files;
 }