SHARE
TWEET

Mejf

a guest Feb 6th, 2010 380 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/php
  2. <?php
  3.         // Fill these in...
  4.         $user = "";
  5.         $pass = "";
  6.        
  7.         // You can get your own apikey here: http://bilddagboken.se/p/apiDoc.html
  8.         // 40 characters of hexadecimal fun
  9.         $apiKey = "";
  10.        
  11.         $userid = -1;
  12.         // The commented code below is for finding out your userid.
  13.         // Uncomment the code, read the userid from stdout and insert it into the script.
  14.         // Or, uncomment and run the code for every picture you upload. Whatever...
  15. /*      $rightTag = false;
  16.         function startTag($prs, $data) {
  17.                 global $rightTag;
  18.                
  19.                 if ($data == "USERID")
  20.                         $rightTag = true;
  21.         }
  22.        
  23.         function contents($prs, $data) {
  24.                 global $rightTag, $userid;
  25.                
  26.                 if ($rightTag) {
  27.                         $userid = (int) $data;
  28.                         $rightTag = false;
  29.                         echo "userid: $userid\n";
  30.                 }
  31.         }
  32.        
  33.         $userinfoForm = "https://api.myphotodiary.com" . "/login_status.xml";
  34.        
  35.         $exec = "curl -H \"Expect:\" -u \"" . $user . ":" . $pass . "\" -F \"api_key=" . $apiKey . "\" " . $userinfoForm;
  36.         $xml = shell_exec($exec);
  37. //      echo "$xml\n";
  38.        
  39.         $prs = xml_parser_create();
  40.         xml_set_element_handler($prs, "startTag", null);
  41.         xml_set_character_data_handler($prs, "contents");
  42.         xml_parse($prs, $xml);*/
  43.        
  44.         if (count($argv) > 1) {
  45.                 $picture = $argv[1];
  46.         } else {
  47.                 $picture = trim(shell_exec("zenity --file-selection --file-filter=\"*.jpg\""));
  48.         }
  49.  
  50.         $picture = realpath($picture);
  51.        
  52.         if (!file_exists($picture)) {
  53.                 if (!empty($picture))
  54.                         shell_exec("zenity --error --text=\"File does not exist: ".escapeshellarg($picture)."\"");
  55.                 exit(-1);
  56.         }
  57.        
  58.         $exif = exif_read_data($picture);
  59.         var_dump($exif);
  60.         if ($exif && isset($exif['Orientation'])) {
  61.                 $or = $exif['Orientation'];
  62.                 switch ($or) {
  63.                 case 1: break;
  64.                 case 3: $rot = 2; break;
  65.                 case 6: $rot = 1; break;
  66.                 case 8: $rot = 3; break;
  67.                 default:
  68.                         shell_exec("zenity --error --text=\"Invalid EXIF rotation: $or\"");
  69.                         exit(-1);
  70.                 }
  71.         }
  72.        
  73.         $title = file(getenv("HOME")."/.bdbuploadertitle");
  74.         $title = $title[0];
  75.         $title = trim(shell_exec("zenity --entry --text=\"Enter title\" --entry-text=\"".($title?$title:"")."\""));
  76.         file_put_contents(getenv("HOME")."/.bdbuploadertitle", $title);
  77.         $text = file(getenv("HOME")."/.bdbuploadertext");
  78.         $text = $text[0];
  79.         $text = trim(shell_exec("zenity --entry --text=\"Enter description\" --entry-text=\"".($text?$text:"")."\""));
  80.         file_put_contents(getenv("HOME")."/.bdbuploadertext", $text);
  81.        
  82.         // Extract date from directory
  83.         $dir = basename(dirname(realpath($picture)));
  84.         echo "$dir\n";
  85.         if (!($ex = explode("-", $dir)))
  86.                 $ex = explode("_", $dir);
  87.         if ($ex) {
  88.                 $year = (int) $ex[0];
  89.                 $month = (int) $ex[1];
  90.                 $day = (int) $ex[2];
  91.         }
  92.        
  93.         $datenotok = (int) shell_exec("zenity --question --ok-label=Yes --cancel-label=No --text=\"Is the date $year-$month-$day ok?\">/dev/null; echo $?");
  94.        
  95.         if ($datenotok) {
  96.                 $year = (int) trim(shell_exec("zenity --entry --text=\"Enter year\" --entry-text=\"`date +%Y`\""));
  97.                 $month = (int) trim(shell_exec("zenity --entry --text=\"Enter month\" --entry-text=\"`date +%m`\""));
  98.                 $day = (int) trim(shell_exec("zenity --entry --text=\"Enter day\" --entry-text=\"`date +%d`\""));
  99.         }
  100.        
  101.         if (!checkdate($month, $day, $year)) {
  102.                 shell_exec("zenity --error --text=\"Invalid date: ".escapeshellarg("$year-$month-$day")."\"");
  103.                 exit(-1);
  104.         }
  105.  
  106.         echo "Uploading file $picture, '$title', '$text', date: $year-$month-$day\n";
  107.        
  108.         do {
  109.                 $ch = curl_init();
  110.                 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:", "Api-Key: $apiKey"));
  111.                 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  112.                 curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
  113.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  114.                 curl_setopt($ch, CURLOPT_URL, "https://api.myphotodiary.com/users/$userid/images/dates/$year/$month/$day.json");
  115.                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  116.                 curl_setopt($ch, CURLOPT_POST, true);
  117.                 $parm = array("image" => "@$picture", "title" => "$title", "text" => "$text");
  118.                 if (isset($rot))
  119.                         $parm["rotation"] = $rot;
  120.                 curl_setopt($ch, CURLOPT_POSTFIELDS, $parm);
  121.                 $response = curl_exec($ch);
  122.                 $response = json_decode($response);
  123.                 if ($response == NULL) {
  124.                         $donttryagain = (int) shell_exec("zenity --question --ok-label=Yes --cancel-label=No --text=\"Failed to upload file: ".escapeshellarg($picture).", Title: ".escapeshellarg($title).", Text: ".escapeshellarg($text).". Try again?\">/dev/null; echo $?");
  125.                 } else {
  126.                         $donttryagain = true;
  127.                         shell_exec("notify-send \"File ".basename($picture)." successfully uploaded to date $year-$month-$day!\"");
  128.                 }
  129.         } while (!$donttryagain);
  130.        
  131.         var_dump($response);
  132. ?>
RAW Paste Data
Want to get better at PHP?
Learn to code PHP in 2017
Top