Exemple #1
0
 /**
  * Destroy current session
  */
 public static function delCurrent()
 {
     // Delete the global object
     if ($session = LigminchaGlobalSession::getCurrent()) {
         lgDebug('Session destroyed', $session);
         LigminchaGlobalDistributed::del(array('id' => $session->id));
     }
     // Delete the SSO cookie
     LigminchaGlobalSSO::delCookie();
 }
Exemple #2
0
 /**
  * Process an incoming sync item
  * - this causes the crud updates that the object represents to be replicated locally
  * - more sync objects may be created for re-routing purposes
  */
 public static function process($crud, $fields, $origin)
 {
     if ($crud == 'U') {
         $method = 'update';
     } elseif ($crud == 'D') {
         $method = 'del';
     } else {
         die("Unknown CRUD method \"{$crud}\"");
     }
     if (!is_array($fields)) {
         $fields = json_decode($fields, true);
     }
     lgDebug("Calling \"{$method}\" CRUD method");
     call_user_func("LigminchaGlobalDistributed::{$method}", $fields, $origin);
 }
Exemple #3
0
 /**
  * Add the toolbar body content to the final page
  */
 public function onAfterFinalPageOutput($output)
 {
     // Get the page output buffer
     $buffer = ob_get_clean();
     ob_start();
     // Add the toolbar head code into the page head area
     $buffer = str_replace('</head>', "{$this->head}\n</head>", $buffer);
     // Add the toolbar body code into start of the page body
     $buffer = preg_replace('#<body.*?>#', "\$0\n{$this->body}", $buffer);
     // Remove the wiki login link
     $buffer = str_replace('id="p-personal"', 'style="display:none"', $buffer);
     // Output the modified buffer
     echo $buffer;
     lgDebug("Global toolbar added to MediaWiki page");
     return true;
 }
Exemple #4
0
 /**
  * Render an iFrame that requests the global toolbar
  */
 public static function toolbar()
 {
     // Get the url of the global app
     $config = JFactory::getConfig();
     $lgGlobalAppDomain = $config->get('lgGlobalApp', 'global.ligmincha.org');
     // Include the code to render the toolbar
     require __DIR__ . '/toolbar.php';
     // Add the toolbar to the body if we have a user
     $app = JFactory::getApplication('site');
     $page = $app->getBody();
     // Add the toolbar head code into the page head area
     $page = str_replace('</head>', "{$lgToolbarHead}\n</head>", $page);
     // Add the toolbar body code into start of the page body
     $page = preg_replace('#<body.*?>#', "\$0\n{$lgToolbarBody}", $page);
     // Set the image to the currently selected header (tempprary: fake template updating demo)
     $data = LigminchaGlobalServer::getCurrent()->data;
     $template = array_key_exists('template', $data) ? $data['template'] : 'maple';
     $page = preg_replace('#(?<=src="images/headers/)(.+?)(?=\\.jpg")#', $template, $page);
     lgDebug("Template set to \"{$template}\"");
     // Update the page content
     $app->setBody($page);
     lgDebug("Global toolbar added to Joomla page");
 }
Exemple #5
0
 /**
  * Decode incoming data (unless it's already an array which is the case when it comes from Ajax)
  */
 private static function decodeData($data)
 {
     lgDebug('Decoding data');
     $data = is_array($data) ? $data : json_decode($data, true);
     lgDebug('Data decoded (Objects: ' . (count($data) - 3) . ')');
     return $data;
 }
Exemple #6
0
 /**
  * Update or create an object in the database and queue the changes if necessary
  * - $origin is passed if this changed arrived from a remote queue
  * - $silent is used to stop any sync objects being generated by the change
  */
 public function update($origin = false, $silent = false)
 {
     $db = JFactory::getDbo();
     $table = LigminchaGlobalDistributed::sqlTable();
     // Bail if no type
     if ($this->type < 1) {
         die('Typeless distributed objects not allowed!');
     }
     // Update an existing object in the database
     if ($this->exists) {
         // TODO: Validate cond
         // Update automatic properties
         $this->flag(LG_NEW, false);
         $this->modified = self::timestamp();
         $sqlVals = $this->sqlValues(false);
         lgDebug('Updating database, object: ' . $this->id);
         $db->setQuery("UPDATE {$table} SET {$sqlVals} WHERE `id`=0x{$this->id}");
         $db->query();
     } else {
         // Only set the automatic properties for locally created non-existent objects
         if (!$origin) {
             $this->flag(LG_NEW, true);
             $this->modified = null;
             $this->creation = self::timestamp();
             // The entry is owned by the user unless it's a server/sync/user object
             if ($this->type == LG_SERVER || $this->type == LG_USER || $this->type == LG_SYNC) {
                 $this->owner = null;
             } else {
                 $this->owner = LigminchaGlobalUser::getCurrent() ? LigminchaGlobalUser::getCurrent()->id : null;
             }
         }
         $sqlVals = $this->sqlValues();
         $db->setQuery("REPLACE INTO {$table} SET {$sqlVals}");
         $db->query();
     }
     // Add outgoing sync objects depending on the context of this change
     // TODO: $private = $this->flag( LG_PRIVATE ) ? $this->owner->server : false
     if (!$silent && !$this->flag(LG_LOCAL)) {
         LigminchaGlobalSync::create('U', $this->fields(), $origin, $private = false);
     }
 }
Exemple #7
0
 /**
  * Get/create current object instance
  */
 public static function getCurrent()
 {
     if (is_null(self::$current)) {
         // Make a new uuid from the server's secret
         $config = JFactory::getConfig();
         $id = self::hash($config->get('secret'));
         self::$current = self::newFromId($id);
         // If the object was newly created, populate with default initial data and save
         if (!self::$current->tag) {
             lgDebug('Server object created', self::$current);
             // Make it easy to find this server by domain
             self::$current->tag = $_SERVER['HTTP_HOST'];
             // Server information
             self::$current->data = self::serverData();
             // Save our new instance to the DB (if we have a master yet)
             if (self::$master) {
                 self::$current->update();
                 lgDebug('Server object updated', self::$master);
             } else {
                 self::$deferred = true;
                 lgDebug('Server object update deferred, master unknown');
             }
         } else {
             lgDebug('Server object retrieved from database', self::$current);
         }
     }
     // If we have a master and we're not in standalone, ensure the server data is up to date
     if (self::$master && !LG_STANDALONE) {
         static $checked = false;
         if (!$checked) {
             $checked = true;
             if (json_encode(self::$current->data) !== json_encode(self::serverData(self::$current->data))) {
                 self::$current->data = self::serverData(self::$current->data);
                 self::$current->update();
             }
         }
     }
     return self::$current;
 }