<?php

ini_set("display_errors", E_ALL);
error_reporting(E_ALL);
include_once "../lib/config.php";
include_once "../lib/FlickrHA.php";
include_once "../lib/FlickrPhotoHA.php";
include_once "../lib/FlickrDB.php";
$flickr = new FlickrHA($config->flickr->apiKey, $config->flickr->secret, $config->flickr->tokenKey, $config->flickr->tokenSecret);
$flickrDB = new FlickrDB($flickr, $config->db);
require_once '../lib/Mustache/Autoloader.php';
Mustache_Autoloader::register();
$engine = new Mustache_Engine();
function getImageRatio($width, $height)
{
    $width = intval($width);
    $height = intval($height);
    if (!$width || !$height) {
        return "3-2";
    }
    $ratio = $width / $height;
    if ($ratio === 1 || $ratio > 1 && $ratio < 1.1 || $ratio < 1 && $ratio > 0.9) {
        return "1-1";
    }
    if ($ratio > 1) {
        $diff_3_2 = abs($ratio - 3 / 2);
        $diff_4_3 = abs($ratio - 4 / 3);
        $diff_16_9 = abs($ratio - 16 / 9);
        if ($diff_4_3 < $diff_3_2 && $diff_4_3 < $diff_16_9) {
            $diff = $diff_4_3 / (4 / 3);
            $ret = "4-3";
 public function savePhoto($setId, $photo, $originalSource, $xlSource)
 {
     $this->_connectDB();
     //$this->deletePhoto($setId,$photo);
     $sql = "INSERT INTO images (set_id,id,secret,server,farm,original_width,original_height,size_ratio, sizes,comments,original_source,xl_source,added) VALUES (";
     $width = $photo->width;
     $height = $photo->height;
     $size_ratio = $height === 0 || $width === 0 ? 0 : $width / $height;
     $sql .= $this->db->quote($setId);
     $sql .= "," . $this->db->quote($photo->id);
     $sql .= "," . $this->db->quote($photo->secret);
     $sql .= "," . $this->db->quote($photo->server);
     $sql .= "," . $this->db->quote($photo->farm);
     $sql .= "," . $width;
     $sql .= "," . $height;
     $sql .= "," . $size_ratio;
     $sql .= "," . $photo->sizes;
     $sql .= "," . $this->db->quote($photo->comments);
     $sql .= "," . $this->db->quote($originalSource);
     $sql .= "," . $this->db->quote($xlSource);
     $sql .= ", NOW()";
     $sql .= ")";
     //echo "d:".$sql;
     $ret = $this->db->insert($sql);
     //var_dump($ret);
     if (!$ret) {
         return FlickrDB::createErrorObject("error:" . $this->db->error . ",sql:" . $sql);
     }
     return $ret;
 }
</head>
<body>
<?php 
ini_set("display_errors", E_ALL);
error_reporting(E_ALL);
$apiKey = "e9c2d11b4b0c9861766632f00320b128";
$secret = "25e5fcd971be9bb9";
$tokenKey = "72157631808364528-6b6d2e85e9679674";
$tokenSecret = "27c1b928d9306c59";
include_once "../lib/config.php";
include_once "../lib/FlickrHA.php";
include_once "../lib/FlickrPhotoHA.php";
include_once "../lib/FlickrDB.php";
$flickr = new FlickrHA($config->flickr->apiKey, $config->flickr->secret, $config->flickr->tokenKey, $config->flickr->tokenSecret);
$flickrDB = new FlickrDB($flickr, $config->db);
$setName = isset($_REQUEST["setName"]) ? $_REQUEST["setName"] : false;
if (!$setName || !$config->sets[$setName]) {
    die("no set");
}
echo "<p>Delete old...</p>";
$flickrDB->deleteSetPhotos($config->sets[$setName]->setId);
echo "<p>Getting images for set " . $setName . "...</p>";
#$setId = "72157663923690755";
$json = $flickr->getSetPhotos($config->sets[$setName]->setId);
$i = 0;
foreach ($json->photoset->photo as $photo) {
    set_time_limit(20);
    echo "<div>";
    echo "<img src=\"" . FlickrPhotoHA::getUrl($photo, FlickrPhotoHA::SIZE_SMALL_240_SHORT) . "\" />";
    $sizes = $flickr->getPhotoSize($photo);