Advertisement
Guest User

Mejf

a guest
Feb 25th, 2010
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.10 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3.     // Fill these in...
  4.     $user = "";
  5.     $pass = "";
  6.    
  7.     $apiKey = "fd1e8237b859ceead8b6bc0549913e6fe254c609";
  8.    
  9.     $userid = -1;
  10.     // The code below is for finding out your userid.
  11.     $rightTag = false;
  12.     function startTag($prs, $data) {
  13.         global $rightTag;
  14.        
  15.         if ($data == "USERID")
  16.             $rightTag = true;
  17.     }
  18.    
  19.     function contents($prs, $data) {
  20.         global $rightTag, $userid;
  21.        
  22.         if ($rightTag) {
  23.             $userid = (int) $data;
  24.             $rightTag = false;
  25.             echo "userid: $userid\n";
  26.         }
  27.     }
  28.    
  29.     $userinfoForm = "https://api.myphotodiary.com" . "/login_status.xml";
  30.    
  31.     $exec = "curl -H \"Expect:\" -u \"" . $user . ":" . $pass . "\" -F \"api_key=" . $apiKey . "\" " . $userinfoForm;
  32.     $xml = shell_exec($exec);
  33. //  echo "$xml\n";
  34.    
  35.     $prs = xml_parser_create();
  36.     xml_set_element_handler($prs, "startTag", null);
  37.     xml_set_character_data_handler($prs, "contents");
  38.     xml_parse($prs, $xml);
  39.    
  40.     function error($msg, $exit=true) {
  41.         shell_exec("zenity --error --text=".escapeshellarg($msg));
  42.        
  43.         if ($exit)
  44.             exit(-1);
  45.     }
  46.    
  47.     function confirm($msg) {
  48.         return 1 - (int) shell_exec("zenity --question --ok-label=Yes --cancel-label=No --text=".escapeshellarg($msg).">/dev/null; echo $?");
  49.     }
  50.    
  51.     function input($msg, $default="", $width=200) {
  52.         if (is_array($default)) {
  53.             $targ = "";
  54.             foreach ($default as $t) {
  55.                 $t = trim($t);
  56.                 if (!empty($t))
  57.                     $targ = escapeshellarg($t) . " " . $targ;
  58.             }
  59.             $default = $targ;
  60.         } else {
  61.         }
  62.        
  63.         return trim(shell_exec("zenity --entry --text=\"$msg\" --entry-text=$default --width=$width"));
  64.     }
  65.    
  66.     function exif2timestamp($exifString) {
  67.         $exifPieces = explode(":", $exifString);
  68.         return strtotime($exifPieces[0] . "-" . $exifPieces[1] . "-" . $exifPieces[2] . ":" . $exifPieces[3] . ":" . $exifPieces[4]);
  69.     }
  70.  
  71.    
  72.     if (count($argv) > 1) {
  73.         $picture = $argv[1];
  74.     } else {
  75.         $picture = trim(shell_exec("zenity --file-selection --file-filter=\"*.jpg\""));
  76.     }
  77.  
  78.     $picture = realpath($picture);
  79.    
  80.     if (!file_exists($picture)) {
  81.         if (!empty($picture))
  82.             error("File does not exist: $picture");
  83.         exit(-1);
  84.     }
  85.    
  86.     $exif = exif_read_data($picture);
  87.     //var_dump($exif);
  88.     if ($exif && isset($exif['Orientation'])) {
  89.         $or = $exif['Orientation'];
  90.         switch ($or) {
  91.         case 1: break;
  92.         case 3: $rot = 2; break;
  93.         case 6: $rot = 1; break;
  94.         case 8: $rot = 3; break;
  95.         default:
  96.             error("Invalid EXIF rotation: $or");
  97.         }
  98.     }
  99.     echo date("Y n j\n", exif2timestamp($exif['DateTime']));
  100.     $edate = array('y' => (int) date("Y", exif2timestamp($exif['DateTime'])), 'm' => (int) date("n", exif2timestamp($exif['DateTime'])), 'd' => (int) date("j", exif2timestamp($exif['DateTime'])));
  101.     var_dump($edate);
  102.    
  103.     $title = input("Enter title", file(getenv("HOME")."/.bdbuploadertitle"));
  104.     file_put_contents(getenv("HOME")."/.bdbuploadertitle", "$title\n", FILE_APPEND);
  105.    
  106.     $text = input("Enter description", file(getenv("HOME")."/.bdbuploadertext"), 600);
  107.  
  108.     if (empty($text)) {
  109.         error("Can not have empty description!");
  110.     }
  111.  
  112.     file_put_contents(getenv("HOME")."/.bdbuploadertext", "$text\n", FILE_APPEND);
  113.    
  114.     // Extract date from directory
  115.     $dir = basename(dirname(realpath($picture)));
  116.     echo "$dir\n";
  117.     if (!($ex = explode("-", $dir)))
  118.         $ex = explode("_", $dir);
  119.     $dateok = false;
  120.     if ($ex && count($ex) == 3) {
  121.         $ddate = array('y' => (int) $ex[0], 'm' => (int) $ex[1], 'd' => (int) $ex[2]);
  122.  
  123.         $dateok = confirm("Is the date {$ddate['y']}-{$ddate['m']}-{$ddate['d']} (found by directory) ok?");
  124.     }
  125.    
  126.     if (!$dateok) {
  127.         $year = (int) input("Enter year", date("Y"));
  128.         $month = (int) input("Enter month", date("n"));
  129.         $day = (int) input("Enter day", date("j"));
  130.         $date = array('y' => $year, 'm' => $month, 'd' => $day);
  131.     } else {
  132.         $date = $ddate;
  133.     }
  134.    
  135.     if (!checkdate($date['m'], $date['d'], $date['y'])) {
  136.         error("Invalid date: {$date['y']}-{$date['m']}-{$date['d']}");
  137.     }
  138.  
  139.     echo "Uploading file $picture, '$title', '$text', date: {$date['y']}-{$date['m']}-{$date['d']}\n";
  140.    
  141.     do {
  142.         $ch = curl_init();
  143.         curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:", "Api-Key: $apiKey"));
  144.         curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  145.         curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
  146.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  147.         curl_setopt($ch, CURLOPT_URL, "https://api.myphotodiary.com/users/$userid/images/dates/{$date['y']}/{$date['m']}/{$date['d']}.json");
  148.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  149.         curl_setopt($ch, CURLOPT_POST, true);
  150.         $parm = array("image" => "@$picture", "title" => "$title", "text" => "$text");
  151.         if (isset($rot))
  152.             $parm["rotation"] = $rot;
  153.         curl_setopt($ch, CURLOPT_POSTFIELDS, $parm);
  154.         $response2 = curl_exec($ch);
  155.         $response = json_decode($response2);
  156.         if ($response == NULL) {
  157.             var_dump($response2);
  158.             var_dump($response);
  159.             $tryagain = confirm("Failed (".curl_error($ch).") to upload file: $picture, Title: $title, Text: $text. Try again?");
  160.         } else {
  161.             $tryagain = false;
  162.             shell_exec("notify-send \"File ".basename($picture)." successfully uploaded to date {$date['y']}-{$date['m']}-{$date['d']}!\"");
  163.         }
  164.         curl_close($ch);
  165.     } while ($tryagain);
  166.    
  167.     var_dump($response);
  168. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement