checks if URL exists before trying to download attachment
authorPhilipp Schüle <p.schuele@metaways.de>
Fri, 22 Aug 2014 12:13:40 +0000 (14:13 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Thu, 4 Sep 2014 09:26:44 +0000 (11:26 +0200)
+ adds helper fn urlExists()

Change-Id: Ib5e3669e98e5cf6cd95170e70f801efc50b6789f
Reviewed-on: http://gerrit.tine20.com/customers/1037
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Calendar/Convert/Event/VCalendar/Abstract.php
tine20/Tinebase/Helper.php

index 76182c1..14b1199 100644 (file)
@@ -920,7 +920,7 @@ class Calendar_Convert_Event_VCalendar_Abstract extends Tinebase_Convert_VCalend
                         }
                     } elseif('URI' === $value) {
                         /*
                         }
                     } elseif('URI' === $value) {
                         /*
-                         * ATTACH;VALUE=URI:https://ical.familienservice.de/calendars/__uids__/0AA0
+                         * ATTACH;VALUE=URI:https://server.com/calendars/__uids__/0AA0
  3A3B-F7B6-459A-AB3E-4726E53637D0/dropbox/4971F93F-8657-412B-841A-A0FD913
  9CD61.dropbox/Canada.png
                          */
  3A3B-F7B6-459A-AB3E-4726E53637D0/dropbox/4971F93F-8657-412B-841A-A0FD913
  9CD61.dropbox/Canada.png
                          */
@@ -941,17 +941,29 @@ class Calendar_Convert_Event_VCalendar_Abstract extends Tinebase_Convert_VCalend
                             // we are client and found an external hosted attachment that we need to import
                             $user = Tinebase_Core::getUser();
                             $userCredentialCache = Tinebase_Core::get(Tinebase_Core::USERCREDENTIALCACHE);
                             // we are client and found an external hosted attachment that we need to import
                             $user = Tinebase_Core::getUser();
                             $userCredentialCache = Tinebase_Core::get(Tinebase_Core::USERCREDENTIALCACHE);
-                            $stream = fopen($matches[1] . $userCredentialCache->username . ':' . $userCredentialCache->password . '@' . $matches[2], 'r');
-                            $attachment = new Tinebase_Model_Tree_Node(array(
-                                'name'         => $name,
-                                'type'         => Tinebase_Model_Tree_Node::TYPE_FILE,
-                                'contenttype'  => (string) $property['FMTTYPE'],
-                                'tempFile'     => $stream,
-                                ), true);
-                            $attachments->addRecord($attachment);
+                            $url = $matches[1] . $userCredentialCache->username . ':' . $userCredentialCache->password . '@' . $matches[2];
+                            $attachmentInfo = $matches[1] . $matches[2]. ' ' . $name . ' ' . $managedId;
+                            if (urlExists($url)) {
+                                if (Tinebase_Core::isLogLevel(Zend_Log::INFO))
+                                    Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
+                                            . ' Downloading attachment: ' . $attachmentInfo);
+                                
+                                $stream = fopen($url, 'r');
+                                $attachment = new Tinebase_Model_Tree_Node(array(
+                                    'name'         => $name,
+                                    'type'         => Tinebase_Model_Tree_Node::TYPE_FILE,
+                                    'contenttype'  => (string) $property['FMTTYPE'],
+                                    'tempFile'     => $stream,
+                                    ), true);
+                                $attachments->addRecord($attachment);
+                            } else {
+                                if (Tinebase_Core::isLogLevel(Zend_Log::WARN)) Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ 
+                                    . ' Url not found (got 404): ' . $attachmentInfo . ' - Skipping attachment');
+                            }
                         } else {
                             if (Tinebase_Core::isLogLevel(Zend_Log::WARN))
                         } else {
                             if (Tinebase_Core::isLogLevel(Zend_Log::WARN))
-                                Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' attachment found with malformed url: '.$url. ' '.$name. ' '.$managedId);
+                                Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ 
+                                    . ' Attachment found with malformed url: ' . $attachmentInfo);
                         }
                     }
                     break;
                         }
                     }
                     break;
index 66215e5..c43d998 100644 (file)
@@ -286,3 +286,22 @@ function formatMicrotimeDiff($timediff)
         return $ms.'ms';
     }
 }
         return $ms.'ms';
     }
 }
+
+/**
+ * checks if an url exists by checking headers 
+ * -> inspired by http://php.net/manual/en/function.file-exists.php#75064
+ * 
+ * @param string $url
+ * @return boolean
+ */
+function urlExists($url)
+{
+    $file_headers = @get_headers($url);
+    if ($file_headers[0] == 'HTTP/1.1 404 Not Found') {
+        $exists = false;
+    } else {
+        $exists = true;
+    }
+    
+    return $exists;
+}