<?php

function checkdir($dir) {
  if (!file_exists($dir)) {
    echo "create $dir\n";
    mkdir($dir, 0777);
  }
  elseif ((fileperms($dir) & 0777) != 0777) {
    echo "chmod 777 $dir\n";
    chmod($dir, 0777);
  }
}

echo "Reading from database at ".date("Y-m-d h:i:s")."\n";
$dbh = new PDO('mysql:host=115.84.178.137;dbname=tvtl', 'violet', 'bk,,123~~', array( PDO::ATTR_PERSISTENT => false));
$stmt = $dbh->prepare("select bl_id from tblblog");
$stmt->execute();

echo "Start check blog at ".date("Y-m-d h:i:s")."\n";
umask(0);
while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
  list($bl_id) = array_values($row);
  $path = "/srv/www/tvtl/web/uploads/blogs/$bl_id";
  checkdir($path);
  checkdir($path.'/entrythumb');
  checkdir($path.'/layout');
  checkdir($path.'/js');
}

echo "\nCheck blog finish at ".date("h:i:s")."\n\n";
