Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adding support for authentication

 - Adding ssl support and user/password to curl class
 - Fixed problem Updating resource
  • Loading branch information...
commit b50f9f086cf6ee6ce3b78312b5fec9f9c752f765 1 parent c511598
Taylor luk authored May 07, 2009
23  active_resource.php
@@ -5,16 +5,15 @@
5 5
 class ActiveResource {
6 6
   protected $attributes;
7 7
   public $site = 'http://localhost:3000';
  8
+  public $user, $password;
8 9
   public $timeout = 2000;
9 10
   public $connection;
10 11
   public $response;
11  
-  public $resources;
12 12
   public $extension = 'xml';
13 13
   
14 14
   function __construct($attributes = array()) {
15 15
     $this->attributes = $attributes;
16 16
     $this->connection = new Curl;
17  
-    $this->resources = pluralize(strtolower(get_class($this)));;
18 17
   }
19 18
   
20 19
   /* Class Methods */
@@ -101,7 +100,7 @@ function save() {
101 100
     
102 101
     if (isset($this->id)) {
103 102
       $this->connection->headers = array("Content-Type" => "application/xml");
104  
-      $this->connection->put(self::element_url($this->id, $class), $data);
  103
+      $resp = $this->connection->put(self::element_url($this->id, $class), $data);
105 104
       return true;
106 105
     }
107 106
     $this->connection->headers = array("Content-Type" => "application/xml");
@@ -142,8 +141,8 @@ static function find_single($scope, $class, $options = array()) {
142 141
         throw new Exception("Object not found");
143 142
       }
144 143
 
145  
-      $xml = simplexml_load_string($resp->body)->{strtolower($class)};
146  
-      return new $class((array) $xml);
  144
+      $xml = simplexml_load_string($response->body)->xpath("//". strtolower($class));
  145
+      return new $class((array) $xml[0]);
147 146
     }
148 147
 
149 148
     static function find_every($class, $options = array()) {
@@ -170,7 +169,14 @@ static function config($class) {
170 169
       extract(get_class_vars($class));
171 170
       if ($site[strlen($site) - 1] === '/')
172 171
         $site = substr($site, 0, -1);
173  
-      return compact('site', 'timeout', 'extension');
  172
+        
  173
+      $parts = parse_url($site);
  174
+      
  175
+      if (isset($parts['user']) && isset($parts['password'])) {
  176
+        $this->user = $parts['user'];
  177
+        $this->password = $parts['password'];
  178
+      }
  179
+      return compact('user', 'password', 'site', 'timeout', 'extension');
174 180
     }
175 181
 
176 182
     static function collection_name($class = null) {
@@ -201,8 +207,11 @@ function request_body($params) {
201 207
       $src = '';
202 208
       $element = strtolower(get_class($this));
203 209
 
204  
-      foreach ($params as $k => $v)
  210
+      foreach ($params as $k => $v) {
  211
+        if ($k === 'updated_at'|| $k === 'created_at') continue;
  212
+         
205 213
         $src .= "<{$k}>". utf8_encode($v) ."</{$k}>";
  214
+      }
206 215
       return "<{$element}>{$src}</{$element}>";
207 216
     }
208 217
 }
10  lib/curl.php
@@ -75,6 +75,16 @@ protected function request($method, $url, $vars = array())
75 75
         curl_setopt($this->handle, CURLOPT_URL, $url);
76 76
         curl_setopt($this->handle, CURLOPT_USERAGENT, $this->user_agent);
77 77
         
  78
+        $parts = parse_url($url);
  79
+        
  80
+        if(strpos($url, 'https') === 0)
  81
+            curl_setopt($this->handle, CURLOPT_SSL_VERIFYPEER, false);
  82
+
  83
+        if(!empty($parts['user']) && !empty($parts['pass'])) {
  84
+            curl_setopt($this->handle, CURLOPT_USERPWD, $parts['user'] . ":" . $parts['pass']);
  85
+            $url = str_replace($parts['user'] . ":" . $parts['pass'].'@', '', $url);
  86
+        }
  87
+
78 88
         # Format custom headers for this request and set CURL option
79 89
         $headers = array();
80 90
         foreach ($this->headers as $key => $value) {
55  test/active_resource_test.php
... ...
@@ -1,13 +1,11 @@
1 1
 <?php
2 2
 
3  
-require 'simpletest/runner.php';
4 3
 require '../active_resource.php';
5 4
 
6  
-define('RESOURCE_URI', 'http://creagency-insurance.local');
  5
+define('RESOURCE_URI', 'http://admin:CheeseBurger@localhost');
7 6
 
8 7
 class Article extends ActiveResource {
9 8
   var $site = RESOURCE_URI;
10  
-  
11 9
   function __toString() {
12 10
     return $this->title;
13 11
   }
@@ -15,41 +13,56 @@ function __toString() {
15 13
 
16 14
 class Question extends ActiveResource {
17 15
   var $site = RESOURCE_URI;
18  
-  
19 16
   function __toString() {
20 17
     return $this->text;
21 18
   }
22 19
 }
23 20
 
  21
+# edit form
  22
+if (!empty($_GET['action']) && $_GET['action'] == 'edit'):
  23
+  $article = Article::find($_GET['article']);
24 24
 
25  
-## Find all
26  
-
27  
-// 
28  
-// $p = Page::create(array(
29  
-//     'title' => 'Hello there elton an title',
30  
-//     'body' => 'article body'
31  
-// ));
32  
-// 
33  
-
  25
+  if (!empty($_POST['article'])) {
  26
+    $article->title = $_POST['article']['title'];
  27
+    $article->body = $_POST['article']['body'];
  28
+    $article->save();
  29
+  
  30
+    header("Location: {$_SERVER["PHP_SELF"]}");
  31
+  }
34 32
 ?>
  33
+<form method="post">
  34
+  <p>
  35
+  <label>Title</label><br/>
  36
+  <input name="article[title]" value="<?=$article->title?>">
  37
+  </p>
  38
+  
  39
+  <p>
  40
+  <label>Body</label><br/>
  41
+  <textarea name="article[body]" rows="20" cols="80"><?=$article->body?></textarea><br/>
  42
+  </p>
35 43
 
  44
+  <input type="submit" value="Update">
  45
+</form>
  46
+<? die;endif ?>
36 47
 
37  
-<h1> All articles </h1>
38 48
 
39  
-<? foreach( Article::find('all') as $article): ?>
  49
+<? if (isset($_GET['article'])): ?>
  50
+  <? $article = Article::find($_GET['article']) ?>
  51
+  <h1><?= $article->title ?></h1>
  52
+  <a href="<?=$_SERVER['REQUEST_URI']?>&action=edit">edit</a>
  53
+  <p><?= $article->body ?>
40 54
 
41  
- <li><a href=""><?= $article ?></a></li>
42  
-
43  
-<? endforeach ?>
  55
+<? endif ?>
44 56
 
45 57
 
  58
+<h1> All articles </h1>
  59
+<? foreach( Article::find('all') as $article): ?>
  60
+ <li><a href="<?= $_SERVER["PHP_SELF"]?>?article=<?=$article->id?>"><?= $article ?></a></li>
  61
+<? endforeach ?>
46 62
 <hr>
47 63
 
48 64
 
49 65
 <h1> All Question</h1>
50  
-
51 66
 <? foreach( Question::find('all') as $question): ?>
52  
-
53 67
  <li><a href=""><?= $question ?></a></li>
54  
-
55 68
 <? endforeach ?>

0 notes on commit b50f9f0

Please sign in to comment.
Something went wrong with that request. Please try again.