示例#1
0
 /**
  * @param Sag $sag An instantiated copy of Sag that you want this class to
  * use. If you don't specify a database (empty($sag->currentDatabase())) then
  * it will be set to '_users'.
  *
  * @return SagUserUtils
  */
 public function __construct($sag)
 {
     if (!$sag instanceof Sag) {
         throw new SagException('Tried to call setSag() with a non-Sag implementation.');
     }
     //Use the database if they pre-selected it, else default to Couch's default.
     $db = $sag->currentDatabase();
     if (empty($db)) {
         $sag->setDatabase('_users');
     }
     $this->sag = $sag;
 }
//connection variables from the VCAPS_SERVICES environment variable. This environment variable
//will be put in your project by Bluemix once you add the Cloudant database to your Bluemix
//application.
// vcap_services Extraction
$services_json = json_decode(getenv('VCAP_SERVICES'), true);
$VcapSvs = $services_json["cloudantNoSQLDB"][0]["credentials"];
// Extract the VCAP_SERVICES variables for Cloudant connection.
$myUsername = $VcapSvs["username"];
$myPassword = $VcapSvs["password"];
try {
    // Let's login to the database.
    $sag = new Sag($myUsername . ".cloudant.com");
    $sag->login($myUsername, $myPassword);
    // Now that we are logged in, we can create a database to use
    //$sag->createDatabase("ttpfeedback");
    $sag->setDatabase("ttpfeedback");
    /****feedback document starts ****/
    $namee = $_POST["name"];
    $com = $_POST["com"];
    $tweet = $_POST["tweet"];
    $acc = $_POST["service"];
    $sw = $_POST["topic"];
    $text = $_POST["text"];
    $rating = $_POST["rating"];
    /**test starts**/
    $ch = curl_init();
    $customer = array('EmailID' => $tweet, 'Organization' => $com, 'Rating' => $rating, 'Service' => $acc, 'Topics' => $sw, 'comments' => $text);
    $sag->put('"' . $namee . '"', $customer);
    /** feedback document ends **/
} catch (Exception $e) {
    //We sent something to Sag that it didn't expect.
示例#3
0
<?php

/*
 * A real quick example to get and update a doc.
 */
require_once './src/Sag.php';
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$school = $_POST['school'];
$discipline = $_POST['discipline'];
$sag = new Sag('127.0.0.1', '5984');
// Select the database that holds our blog's data.
$sag->setDatabase('sakib');
try {
    //Get a blog post (a StdClass)...
    $post = $sag->get('postID')->body;
    //...update its info...
    $post->views++;
    //..and send it back to the couch.
    if (!$sag->put($post->_id, $post)->body->ok) {
        error_log('Unable to log a view to CouchDB.');
    }
} catch (SagCouchException $e) {
    //The requested post doesn't exist - oh no!
    if ($e->getCode() == "404") {
        $e = new Exception("That post doesn't exist.");
    }
    throw $e;
} catch (SagException $e) {
    //We sent something to Sag that it didn't expect.
    error_log('Programmer error: ' . $e->getMessage());
//application.
// vcap_services Extraction
$services_json = json_decode(getenv('VCAP_SERVICES'), true);
$VcapSvs = $services_json["cloudantNoSQLDB"][0]["credentials"];
//Debug: If you want to see all the variables returned you can use this line of code.
//var_dump($services_json);
// Extract the VCAP_SERVICES variables for Cloudant connection.
$myUsername = $VcapSvs["username"];
$myPassword = $VcapSvs["password"];
try {
    // Let's login to the database.
    $sag = new Sag($myUsername . ".cloudant.com");
    $sag->login($myUsername, $myPassword);
    // Now that we are logged in, we can create a database to use
    $sag->createDatabase("mydatabase");
    $sag->setDatabase("mydatabase");
    if (!$sag->put("myId", '{"myKey":"Hello World from Cloudant!"}')->body->ok) {
        error_log('Unable to post a document to Cloudant.');
    } else {
        // We are now going to read a document from our cloudant database. We are going
        // to retrieve the value associated with myKey from the body of the document.
        //The SAG PHP library takes care of all the gory details and only retrieves the value.
        $resp = $sag->get('myId')->body->myKey;
        echo $resp;
    }
    // Assuming everything above was executed without error, we now are connected to the
    // database and have retrieved the value.
    //NOTE: Since we have a connection to the database, we can query the database for other
    //      documents and retrieve other variables at a later time. We do not need to connect
    //      to the database again.
} catch (Exception $e) {
示例#5
0
 }
 if (!isset($auth) || empty($auth)) {
     $auth = new $authType($config);
 }
 if (!$auth->isLoggedIn()) {
     $auth->login();
 }
 if ($groupShare) {
     $groups = new $groupType($config, $auth);
 } else {
     $groups = NULL;
 }
 $action = getRequest('action', FALSE, 'showFiles');
 $useRest = getRequest('useRest', FALSE, FALSE);
 $storage = new Sag();
 $storage->setDatabase($dbName);
 if ($action === "logout") {
     $auth->logout($_SERVER["SCRIPT_NAME"]);
     exit(0);
 }
 if (!in_array($action, array('downloadFile', 'fileInfo', 'fileUpload', 'legacyFileUpload', 'getCacheObject', 'handleUpload', 'handleLegacyUpload', 'showFiles', 'rawFileInfo', 'updateFileInfo'), TRUE)) {
     throw new Exception("unregistered action called");
 }
 if ($useRest) {
     $f = new Files($config, $storage, $auth, $groups, NULL);
     $f->setRest(TRUE);
     $content = $f->{$action}();
     header("Content-Type: application/json;charset=UTF-8");
     header('Content-Disposition: attachment; filename="response.json"');
     die($content);
 } else {
示例#6
0
<?php

header('Content-Type:text/plain');
require_once 'check_and_start_session.php';
require_once 'Sag/Sag.php';
$s = new Sag();
$s->setDatabase('slouchin_test');
// requires login, but we've got a cookie!
$s->setAuthSession(session_id());
$doc = new stdClass();
$doc->test = 'doc';
$doc->used_session_id = $s->authSession;
try {
    $res = $s->put(uniqid(), $doc);
    print_r($res);
} catch (SagCouchException $e) {
    print_r($e->getCode());
    print_r($e->getMessage());
}
 public static function makeCouchDBLibrary()
 {
     $sag = new Sag('205.186.144.58');
     $sag->setDatabase('plugins');
     return $sag;
 }
示例#8
0
文件: SagTest.php 项目: rwaldron/sag
 public function test_connectionFailure()
 {
     $badCouch = new Sag('example.com');
     $badCouch->setOpenTimeout(1);
     try {
         $badCouch->setDatabase('bwah');
         $badCouch->get('/asdf');
         $this->assertTrue(false);
         //shouldn't reach this line
     } catch (SagException $e) {
         $this->assertTrue(true);
     }
 }
示例#9
0
<?php

/*
 * A sample script illustrating one way to insert a user into the 
 *  CloudKnock database
 */
require 'classes/user.php';
require 'path/to/Sag.php';
$user = new User(null, null, null, null, "5551112222", null, "15551112222", null);
$user->setName("Your Name");
$sag = new Sag('your-db.couchone.com', '5984');
$sag->setDatabase('cloudknock');
$sag->post(sprintf($user));
<?php

// Include required classes.
require 'classes/tropo.class.php';
require 'classes/sag/sag.php';
// Grab the raw JSON sent from Tropo.
$json = file_get_contents("php://input");
// Create a new Session object and obtain the session ID value.
$session = new Session($json);
$session_id = $session->getId();
// Insert the Session object into a CouchDB database called sessions.
try {
    $sag = new Sag();
    $sag->setDatabase("sessions");
    $sag->put($session_id, $json);
} catch (SagCouchException $ex) {
    die("*** " . $ex->getMessage() . " ***");
}
// Create a new Tropo object.
$tropo = new Tropo();
// Set options for an Ask.
$options = array("attempts" => 20, "bargein" => true, "choices" => "[5 DIGITS]", "name" => "zip", "timeout" => 5, "allowSignals" => array("tooLong", "farTooLong"));
$tropo->ask("Please enter your 5 digit zip code.", $options);
// Set event handlers
$tropo->on(array("event" => "continue", "next" => "get_zip_code.php?uri=end", "say" => "Please hold."));
$tropo->on(array("event" => "tooLong", "next" => "get_zip_code.php?uri=end&tooLong=true", "say" => "Please hold on."));
$tropo->on(array("event" => "farTooLong", "next" => "get_zip_code.php?uri=end&farTooLong=true", "say" => "Please hold on for dear life."));
// Render JSON for Tropo to consume.
$tropo->renderJSON();
$fileStorageDir = getConfig($config, 'file_storage_dir', TRUE);
$cachePath = getConfig($config, 'cache_dir', TRUE);
/* each user has their own data directory, easy to link files
   to users in case the index breaks, so at least the files 
   themselves are not lost, only the metadata added by the user and
   sharing groups.
 */
$dbName = getConfig($config, 'db_name', TRUE);
$s = new Sag();
$dbs = $s->getAllDatabases()->body;
/* if db already exists, delete it */
if (in_array($dbName, $dbs)) {
    $s->deleteDatabase($dbName);
}
$s->createDatabase($dbName);
$s->setDatabase($dbName);
/* load all the map/reduce js functions from mapReduce directory */
$views = array();
foreach (glob("docs/mapReduce/*") as $mrFiles) {
    list($name, $type) = explode(".", basename($mrFiles));
    $views[$name][$type] = file_get_contents($mrFiles);
}
$view = array("_id" => "_design/files", "type" => "view", "language" => "javascript", "views" => $views);
// Add the view
$s->post($view);
// Delete all cache entries as to not accumulate too many
foreach (glob($cachePath . "/*") as $cacheEntry) {
    unlink($cacheEntry);
}
// Import all new entries
foreach (glob($fileStorageDir . "/*") as $userDir) {
示例#12
0
文件: index.php 项目: xwiz/sag
<?php

require_once '../../src/Sag.php';
session_start();
try {
    // We are going to get our page's content from Sag.
    $sag = new Sag('sbisbee.com');
    $sag->setDatabase('outlook');
    if ($_POST['login']) {
        echo '<p>Using login()';
        $_SESSION['AuthSession'] = $sag->login($_POST['username'], $_POST['password'], $sag::$AUTH_COOKIE);
    } else {
        if ($_SESSION['AuthSession']) {
            echo '<p>Using setCookie()';
            $sag->setCookie('AuthSession', $_SESSION['AuthSession']);
        }
    }
    $result = $sag->get('/');
} catch (Exception $e) {
    $error = $e->getMessage();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <title>proxyCookie example</title>
  </head>
  <body>
    <?php