]> gitweb.fluxo.info Git - semanticscuttle.git/commitdiff
Implement request #2833793: Export to avahi config files
authorcweiske <cweiske@b3834d28-1941-0410-a4f8-b48e95affb8f>
Tue, 19 Jan 2010 23:01:36 +0000 (23:01 +0000)
committercweiske <cweiske@b3834d28-1941-0410-a4f8-b48e95affb8f>
Tue, 19 Jan 2010 23:01:36 +0000 (23:01 +0000)
  - we support zeroconf networking!

git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@620 b3834d28-1941-0410-a4f8-b48e95affb8f

data/config.default.php
doc/ChangeLog
scripts/avahi-export.php [new file with mode: 0644]

index a0010e91244ea30d86d5c4a00ad4803340dcf41c..9febb79d1ceec3cbf040d7fc79f129cf24a2e438 100644 (file)
@@ -630,4 +630,35 @@ $descriptionAnchors = array(
  */
 $googleAnalyticsCode = null;
 
+
+
+
+/****************************
+ * avahi export script
+ */
+
+/**
+ * Location of avahi service files,
+ * often /etc/avahi/services/
+ *
+ * @var string
+ */
+$avahiServiceFilePath = '/etc/avahi/services/';
+
+/**
+ * File name prefix of SemanticScuttle-generated
+ * service files
+ *
+ * @var string
+ */
+$avahiServiceFilePrefix = 'semanticscuttle-';
+
+/**
+ * Name of tag that bookmarks need to have to
+ * get exported into avahi service files.
+ *
+ * @var string
+ */
+$avahiTagName = 'zeroconf';
+
 ?>
index 6e2676ea20a254a1cfdc2232c139b6bb0c40dfa8..3dbeaed528326966ceaa7987815b935c981fa40c 100644 (file)
@@ -19,6 +19,8 @@ ChangeLog for SemantiScuttle
   special characters did not get escaped.
 - Special header file for shell scripts (header-standalone.php)
 - Fix E_NOTICE when calling alltags.php without any parameter
+- Implement request #2833793: Export to avahi config files
+  - we support zeroconf networking!
 
 
 0.95.2 - 2010-01-16
diff --git a/scripts/avahi-export.php b/scripts/avahi-export.php
new file mode 100644 (file)
index 0000000..f609949
--- /dev/null
@@ -0,0 +1,107 @@
+<?php
+/**
+ * Exports bookmarks tagged with "zeroconf"
+ * as avahi service files.
+ *
+ * XML Documentation: "man 5 avahi.service"
+ *
+ *
+ * This file is part of
+ * SemanticScuttle - your social bookmark manager.
+ *
+ * PHP version 5.
+ *
+ * @category Bookmarking
+ * @package  SemanticScuttle
+ * @author   Christian Weiske <cweiske@cweiske.de>
+ * @license  GPL http://www.gnu.org/licenses/gpl.html
+ * @link     http://sourceforge.net/projects/semanticscuttle
+ */
+require_once dirname(__FILE__) . '/../src/SemanticScuttle/header-standalone.php';
+
+$fileprefix = $GLOBALS['avahiServiceFilePrefix'];
+$filepath   = $GLOBALS['avahiServiceFilePath'];
+
+$arSchemes = array(
+    'ftp'  => array(21, '_ftp._tcp'),
+    'ssh'  => array(22, '_ftp._tcp'),
+    'sftp' => array(22, '_sftp-ssh._tcp'),
+    'http' => array(80, '_http._tcp'),
+);
+
+if (!is_writable($filepath)) {
+    echo "avahi service directory is not writable:\n";
+    echo $filepath . "\n";
+    exit(1);
+}
+
+//clean out existing SemanticScuttle service files
+$existing = glob($filepath . '/' . $fileprefix . '*');
+if (count($existing) > 0) {
+    foreach ($existing as $file) {
+        unlink($file);
+    }
+}
+
+$bs = SemanticScuttle_Service_Factory::get('Bookmark');
+$bookmarks = $bs->getBookmarks(0, null, null, $GLOBALS['avahiTagName']);
+$bookmarks = $bookmarks['bookmarks'];
+
+if (count($bookmarks) == 0) {
+    echo 'No "' . $GLOBALS['avahiTagName'] . '"-tagged bookmarks available.' . "\n";
+    exit(0);
+}
+
+$written = 0;
+foreach ($bookmarks as $bm) {
+    $xTitle = htmlspecialchars($bm['bTitle']);
+    $parts  = parse_url($bm['bAddress']);
+
+    if (!isset($parts['host'])) {
+        echo 'No hostname in: ' . $bm['bAddress'] . "\n";
+        exit(2);
+    }
+
+    $xHostname = htmlspecialchars($parts['host']);
+    $xPath     = isset($parts['path']) ? $parts['path'] : '';
+    if (isset($parts['query'])) {
+        $xPath .= '?' . $parts['query'];
+    }
+    if (isset($parts['fragment'])) {
+        $xPath .= '#' . $parts['fragment'];
+    }
+
+    $scheme = isset($parts['scheme']) ? $parts['scheme'] : 'http';
+    if (!isset($arSchemes[$scheme])) {
+        //dying is hard, but at least the user knows
+        // that something is seriously wrong
+        echo "Unknown scheme: $scheme\n";
+        exit(3);
+    }
+    list($xPort, $xType) = $arSchemes[$scheme];
+
+    if (isset($parts['port'])) {
+        $xPort = (int)$parts['port'];
+    }
+
+    $xml = <<<XML
+<?xml version="1.0" standalone='no'?>
+<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
+<service-group>
+  <name>{$xTitle}</name>
+  <service>
+    <type>{$xType}</type>
+    <host-name>{$xHostname}</host-name>
+    <port>{$xPort}</port>
+    <txt-record>path={$xPath}</txt-record>
+  </service>
+</service-group>
+XML;
+
+    $file = $filepath . '/' . $fileprefix . $bm['bId'] . '.service';
+    file_put_contents($file, $xml);
+    ++$written;
+}
+
+echo $written . " service files created\n";
+?>
\ No newline at end of file