<?php

function chmoddir($path) {
    if (file_exists($path) && (fileperms($path) & 0777) != 0777) {
        echo "chmod 777 $path\n";
        chmod($path, 0777);
    }
}

if (@$argv[2] == NULL) exit();
                        
for ($c = $argv[2]; $c >= $argv[1]; $c--) {
  $start = $c * 1000;
  $end = ($c+1) * 1000;
  echo date("H:i:s")." Change mod from $start to $end\n";
  $dbh = new PDO('mysql:host=115.84.178.137;dbname=tvtl', 'violet', 'bk,,123~~', array( PDO::ATTR_PERSISTENT => false));
  $stmt = $dbh->prepare(
    "select pr_id, pr_category from tblpresentation ".
    "where pr_id>=$start and pr_id<$end");
  $stmt->execute();

  while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
    list($pr_id, $pr_cat) = array_values($row);
    $path = "/data/violet/uploads/resources/$pr_cat/$pr_id";
    chmoddir($path);
    chmoddir($path."/images");
  }
}

echo "\nChange mod finish at ".date("H:i:s")."\n\n";
