OpenRat.class.php (9765B)
1 <?php 2 3 4 class OpenRat { 5 6 public $text = ''; 7 public $filenames = array(); 8 public $filename; 9 public $subject; 10 public $keywords = array(); 11 public $shortUrl; 12 public $path; 13 public $debug = true; 14 public $timestamp; 15 public $config; 16 17 private $client; 18 19 private function request( $method,$parameter ) 20 { 21 22 $this->client->parameter = $parameter; 23 $this->client->method = $method; 24 $this->client->request(); 25 26 if ( $this->client->status != '200' || $this->debug) 27 { 28 echo '<span style="background-color:'.($this->client->status=='200'?'green':'red').'">HTTP-Status '.$this->client->status.'</span>'; 29 echo "<h4>".$parameter['action'].'/'.$parameter['subaction'].'</h4>'; 30 ?><pre><?php print_r(""); ?></pre><pre><?php print_r($this->client->response); ?></pre><?php 31 } 32 33 $response = json_decode($this->client->response,true); 34 35 if ( $response == null ) 36 { 37 echo '<span style="background-color:red">Kein JSON: <pre>'.htmlentities($this->client->response).'</pre></span>'; 38 exit; 39 } 40 41 return $response; 42 } 43 44 45 public function push() 46 { 47 $filesToPublish = array(); 48 $objectsToPublish = explode(',',$this->config['publish']); 49 require_once('./cms/openrat/OpenratClient.php'); 50 $this->client = new OpenratClient(); 51 52 $this->client->host = $this->config['host']; 53 $this->client->port = $this->config['port']; 54 $this->client->path = $this->config['path']; 55 $this->client->type ="application/json"; 56 57 58 $response = $this->request( 'GET', 59 array('action' =>'login', 60 'subaction'=>'login') ); 61 62 $token = $response['session']['token']; 63 $this->client->cookie =$response['session']['name'].'='.$response['session']['id']; 64 65 66 $response = $this->request( 'POST', array( 67 'action' => 'login', 68 'subaction' => 'login', 69 'token' => $token, 70 'dbid' => $this->config['database'], 71 'login_name' => $this->config['user' ], 72 'login_password'=> $this->config['password'] ) ); 73 74 $this->client->cookie =$response['session']['name'].'='.$response['session']['id']; 75 $token = $response['session']['token']; 76 77 78 // Projekt auswählen 79 $response = $this->request( 'POST', array( 80 'action' => 'start', 81 'subaction' => 'projectmenu', 82 'token' => $token, 83 'id' => $this->config['projectid']) ); 84 85 86 // Ordner laden. 87 $rootfolderid = $this->config['rootfolderid']; 88 $folderid = $rootfolderid; 89 90 $depth = 0; 91 foreach( $this->path as $foldername ) 92 { 93 $depth++; 94 $response = $this->request( 'GET', array 95 ( 96 'action' => 'folder', 97 'subaction' => 'edit', 98 'id' => $folderid 99 ) ); 100 101 // Prüfen, ob der nächste Unterordner bereits existiert. 102 $nextfolderid = null; 103 foreach( $response['output']['object'] as $objectid=>$object ) 104 { 105 if ( $object['name'] == $foldername ) 106 { 107 $nextfolderid = $objectid; 108 break; 109 } 110 } 111 if ( empty($nextfolderid) ) 112 { 113 // Der nächste Unterordner existiert noch nicht, also müssen wir diesen anlegen. 114 $responseCreate = $this->request( 'POST', array 115 ( 116 'action' => 'folder', 117 'subaction' => 'createfolder', 118 'id' => $folderid, 119 'token' => $token, 120 'name' => $foldername 121 ) ); 122 $nextfolderid = $responseCreate['output']['objectid']; 123 124 // Seite anlegen. 125 if ( $depth < count($this->path) ) 126 { 127 $response = $this->request( 'POST', array 128 ( 129 'action' => 'folder', 130 'subaction' => 'createpage', 131 'id' => $nextfolderid, 132 'templateid' => $this->config['templateid'], 133 'token' => $token, 134 'name' => $foldername, 135 'filename' => 'index' 136 ) ); 137 $pageobjectid = $response['output']['objectid']; 138 139 $objectsToPublish[] = $pageobjectid; 140 } 141 } 142 $folderid = $nextfolderid; 143 } 144 145 // Ein Unterordner für die Anlagen 146 $responseCreate = $this->request( 'POST', array 147 ( 148 'action' => 'folder', 149 'subaction' => 'createfolder', 150 'id' => $folderid, 151 'token' => $token, 152 'name' => 'attachments-'.$this->filename 153 ) ); 154 $attachment_folderid = $responseCreate['output']['objectid']; 155 156 // Seite für den Blogeintrag anlegen. 157 $response = $this->request( 'POST', array 158 ( 159 'action' => 'folder', 160 'subaction' => 'createpage', 161 'id' => $folderid, 162 'templateid' => $this->config['templateid'], 163 'token' => $token, 164 'name' => $this->subject, 165 'filename' => $this->filename 166 ) ); 167 $pageobjectid = $response['output']['objectid']; 168 169 $objectsToPublish[] = $pageobjectid; 170 // Timestamp nicht setzen (fraglich, ob die Funktion in der API bleibt) 171 // $response = $this->request( 'POST', array 172 // ( 173 // 'action' => 'page', 174 // 'subaction' => 'prop', 175 // 'id' => $pageobjectid, 176 // 'name' => $this->subject, 177 // 'filename' => 'index', 178 // 'token' => $token, 179 // 'creationTimestamp' => $this->timestamp 180 // ) ); 181 182 183 /* 184 * 185 // Ggf. Datei anlegen. 186 $response = $this->request( 'POST', array 187 ( 188 'action' => 'folder', 189 'subaction' => 'createfile', 190 'token' => $token, 191 'name' => $title, 192 'filename' => $title 193 ) ); 194 $pageobjectid = $response['objectid']; 195 */ 196 197 // Text speichern anlegen. 198 $response = $this->request( 'POST', array 199 ( 200 'action' => 'pageelement', 201 'subaction' => 'edit', 202 'id' => $pageobjectid, 203 'elementid' => $this->config['elementid_text'], 204 'token' => $token, 205 'release' => '1', 206 'text' => $this->text 207 ) ); 208 209 // Ordner für die Bilder speichern 210 $response = $this->request( 'POST', array 211 ( 212 'action' => 'pageelement', 213 'subaction' => 'edit', 214 'id' => $pageobjectid, 215 'elementid' => $this->config['elementid_attachment_folder'], 216 'token' => $token, 217 'release' => '1', 218 'linkobjectid' => $attachment_folderid 219 ) ); 220 221 // Datum speichern. 222 $response = $this->request( 'POST', array 223 ( 224 'action' => 'pageelement', 225 'subaction' => 'edit', 226 'id' => $pageobjectid, 227 'elementid' => $this->config['elementid_date'], 228 'token' => $token, 229 'release' => '1', 230 'date' => $this->timestamp 231 ) ); 232 233 foreach( $this->filenames as $file ) 234 { 235 // Datei anlegen. 236 $response = $this->request( 'POST', array 237 ( 238 'action' => 'folder', 239 'subaction' => 'createfile', 240 'id' => $attachment_folderid, 241 'token' => $token, 242 'name' => $file['name'], 243 'filename' => basename($file['name']) 244 ) ); 245 $fileobjectid = $response['output']['objectid']; 246 247 $filesToPublish[] = $fileobjectid; 248 249 // Datei-Inhalt hochladen. 250 $response = $this->request( 'POST', array 251 ( 252 'action' => 'file', 253 'subaction' => 'value', 254 'id' => $fileobjectid, 255 'token' => $token, 256 'value' => file_get_contents($file['filename']) 257 ) ); 258 259 } 260 261 // Keywords 262 foreach( $this->keywords as $keyword ) 263 { 264 $response = $this->request( 'GET', array 265 ( 266 'action' => 'folder', 267 'subaction' => 'edit', 268 'id' => $this->config['keywords_folderid'] 269 ) ); 270 271 // Prüfen, ob das Keyword schon existiert 272 $keyword_folderid = null; 273 foreach( $response['output']['object'] as $objectid=>$object ) 274 { 275 if ( $object['name'] == $keyword ) 276 { 277 $keyword_folderid = $objectid; 278 break; 279 } 280 } 281 if ( empty($keyword_folderid) ) 282 { 283 // Der Keyword-Ordner existiert noch nicht, also müssen wir diesen anlegen. 284 $responseCreate = $this->request( 'POST', array 285 ( 286 'action' => 'folder', 287 'subaction' => 'createfolder', 288 'id' => $this->config['keywords_folderid'], 289 'token' => $token, 290 'name' => $keyword 291 ) ); 292 $keyword_folderid = $responseCreate['output']['objectid']; 293 294 // Seite im neuen Keyword-Ordner anlegen 295 $response = $this->request( 'POST', array 296 ( 297 'action' => 'folder', 298 'subaction' => 'createpage', 299 'id' => $folderid, 300 'templateid' => $this->config['templateid'], 301 'token' => $token, 302 'name' => $keyword, 303 'filename' => 'index' 304 ) ); 305 $pageobjectid = $response['output']['objectid']; 306 307 $objectsToPublish[] = $pageobjectid; 308 } 309 310 $responseCreate = $this->request( 'POST', array 311 ( 312 'action' => 'folder', 313 'subaction' => 'createlink', 314 'id' => $keyword_folderid, 315 'token' => $token, 316 'name' => $this->subject, 317 'filename' => $this->filename, 318 'targetobjectid'=> $pageobjectid 319 ) ); 320 321 } 322 323 // Veröffentlichen der neuen und geänderten Seiten 324 foreach( $objectsToPublish as $objectToPublish ) 325 { 326 $response = $this->request( 'POST', array 327 ( 328 'action' => 'page', 329 'subaction' => 'pub', 330 'id' => $objectToPublish, 331 'token' => $token 332 ) ); 333 } 334 // Veröffentlichen der neuen und geänderten Dateien 335 foreach( $filesToPublish as $fileToPublish ) 336 { 337 $response = $this->request( 'POST', array 338 ( 339 'action' => 'file', 340 'subaction' => 'pub', 341 'id' => $fileToPublish, 342 'token' => $token 343 ) ); 344 } 345 } 346 347 348 } 349 350 ?>