public function testAddDeleteShadowUser()
 {
     //    Dal::register_query_callback("explain_query");
     global $network_info;
     echo "getting a user\n";
     $user = Test::get_test_user();
     $testusername = $user->first_name . " " . $user->last_name;
     echo "test user = {$testusername}\n";
     $namespace = 'php_unit';
     // testuser data
     $testdata = array('user_id' => "pa_" . $user->user_id, 'login_name' => 'testuser', 'email' => $namespace . $user->email, 'first_name' => $user->first_name, 'last_name' => $user->last_name);
     echo "TEST DATA:\n";
     print_r($testdata);
     $shadow_user = new ShadowUser($namespace);
     echo "Test load this shadow user, this should fail\n";
     $sh = $shadow_user->load($testdata['user_id']);
     $this->assertNull($sh);
     echo "Create a shadow user\n";
     $shadow_user = ShadowUser::create($namespace, $testdata, $network_info);
     echo "SHADOW USER DATA:\n";
     print_r($shadow_user);
     $this->assertNotNull($shadow_user);
     echo "Test updating the data\n";
     $testdata2 = $testdata;
     $testdata2['email'] = $namespace . "add" . $user->email;
     $testdata2['login_name'] = "newlogin";
     $testdata2['first_name'] = "newName";
     print_r($testdata2);
     $su2 = new ShadowUser($namespace);
     // load this with new data
     $su2->load($testdata2);
     unset($su2);
     Cache::reset();
     // now load it only via the original remote uid
     $su3 = new ShadowUser($namespace);
     $su3->load($testdata['user_id']);
     echo "UPDATED SHADOW USER DATA:\n";
     print_r($su3);
     echo "Delete it\n";
     ShadowUser::delete($shadow_user->user_id);
     // there should not be a shadow user of this id anymore
     $this->assertNull($shadow_user->load($testdata['user_id']));
 }
 function handle_request()
 {
     $json = new Services_JSON();
     try {
         global $_PA, $HTTP_RAW_POST_DATA;
         if (!@$_PA->enable_widgetization_server) {
             $this->fail("Widget server is not enabled; you must set \$_PA->enable_widgetization_server = TRUE in local_config.php.");
         }
         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
             $this->fail("This URL handles POST requests only");
         }
         if ($_SERVER['CONTENT_TYPE'] != 'application/x-javascript') {
             $this->fail("Content-Type of application/x-javascript required");
         }
         // Parse input
         $request = $json->decode($HTTP_RAW_POST_DATA);
         if ($request == NULL) {
             $this->fail("Null request");
         }
         if (@$_PA->log_widget_requests) {
             Logger::log("WidgetServer::handle_request(): request={$HTTP_RAW_POST_DATA}", LOGGER_ACTION);
         }
         $this->global = $request->global;
         // This should probably be in config.inc.  For the moment
         // we figure out the network based on the URL, as with the
         // rest of the system.
         PA::$network_info = get_network_info();
         $lang = "english";
         if (!empty($this->global->language)) {
             switch ($this->global->language) {
                 case 'en':
                     break;
                 case 'fr':
                     $lang = "french";
                     break;
                 default:
                     $this->fail("Unknown language: {$this->global}->language");
             }
         }
         PA::load_language($lang);
         // Create items as required
         if (!empty($this->global->items)) {
             foreach ($this->global->items as $item) {
                 $item_params = array();
                 foreach ($item as $k => $v) {
                     $item_params[$k] = $v;
                 }
                 Item::sync($item_params);
                 // create or update row in 'items' database table
             }
         }
         // Set up globals - network, user etc
         if (!empty($this->global->user)) {
             $user_info = array("user_id" => $this->global->user->id, "login_name" => $this->global->user->login, "email" => $this->global->user->email, "first_name" => $this->global->user->first_name, "last_name" => $this->global->user->last_name, "url" => $this->global->user->url, "thumbnail_url" => $this->global->user->thumbnail_url);
             // load (and sync!) or create a shadow user for the current remote user
             PA::$login_user = new ShadowUser($this->global->user->namespace);
             if (!PA::$login_user->load($user_info)) {
                 // we haven't seen this remote user before - create account
                 PA::$login_user = ShadowUser::create($this->global->user->namespace, $user_info, PA::$network_info);
                 //FIXME: need to define what remote urls mean.  in this case "url" should be used instead of /users/$login_name when generating internal urls, so it should go in a global profile block rather than something specific to the remote site.
                 PA::$login_user->set_profile_field($this->global->user->namespace, "url", $this->global->user->url);
             }
             PA::$login_uid = PA::$login_user->user_id;
         }
         // Render modules
         $modules = array();
         foreach ($request->modules as $req_module) {
             $module = array();
             $module['id'] = $req_module->id;
             $module['name'] = $name = $req_module->name;
             $params = array();
             foreach ($req_module->params as $k => $v) {
                 $params[$k] = $v;
             }
             // clean up URLs that may have the port 80 specified
             // this would lead to cross server AJAX problems in safari etc
             // although we are actually on the same server
             // domain.tld:80/file/ and domain.tld/file/
             foreach (array('get_url', 'ajax_url', 'post_url') as $i => $url) {
                 $req_module->{$url} = preg_replace('|:80/*|', '/', $req_module->{$url});
             }
             // dispatch module
             ob_start();
             $module['html'] = $this->render_module($req_module->method, $req_module->name, $req_module->args, $params, $req_module->get_url, $req_module->ajax_url, $req_module->post_url, $req_module->param_prefix);
             // prefix for input parameters and textareas
             $errors = ob_get_contents();
             ob_end_clean();
             if (!empty($errors)) {
                 $module['errors'] = $errors;
             }
             $modules[] = $module;
         }
         $response = array('modules' => $modules);
         header("Content-Type: application/x-javascript");
         echo $json->encode($response);
     } catch (WidgetException $e) {
         echo $json->encode(array("error" => $e->getMessage()));
     }
 }
 function handle_request()
 {
     $json = new Services_JSON();
     try {
         global $HTTP_RAW_POST_DATA;
         if (!@PA::$config->enable_widgetization_server) {
             $this->fail("Widget server is not enabled; you must set \\PA::{$config->enable_widgetization_server} = TRUE in local_config.php.");
         }
         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
             $this->fail("This URL handles POST requests only");
         }
         if ($_SERVER['CONTENT_TYPE'] != 'application/x-javascript') {
             $this->fail("Content-Type of application/x-javascript required");
         }
         // Parse input
         $request = $json->decode($HTTP_RAW_POST_DATA);
         if ($request == NULL) {
             $this->fail("Null request");
         }
         $this->global = $request->global;
         // Set up globals - network, user etc
         if (!empty($this->global->user)) {
             PA::$login_user = new ShadowUser($this->global->namespace);
             // see if we can load it already
             if (!PA::$login_user->load($this->global->user->user_id)) {
                 // wasn't here before, so we create a shadow account
                 PA::$login_user = ShadowUser::create($this->global->namespace, $this->global->user, PA::$network_info);
             }
             PA::$login_uid = PA::$login_user->user_id;
         }
         // This should probably be in config.inc.  For the moment
         // we figure out the network based on the URL, as with the
         // rest of the system.
         PA::$network_info = get_network_info();
         // Render modules
         $modules = array();
         foreach ($request->modules as $req_module) {
             $module = array();
             $module['id'] = $req_module->id;
             $module['name'] = $name = $req_module->name;
             $params = array();
             foreach ($req_module->params as $k => $v) {
                 $params[$k] = $v;
             }
             // dispatch module
             ob_start();
             $module['html'] = $this->render_module($req_module->method, $req_module->name, $req_module->args, $params, $req_module->post_url, $req_module->param_prefix);
             // prefix for input parameters and textareas
             $errors = ob_get_contents();
             ob_end_clean();
             if (!empty($errors)) {
                 $module['errors'] = $errors;
             }
             $modules[] = $module;
         }
         $response = array('modules' => $modules);
         header("Content-Type: application/x-javascript");
         echo $json->encode($response);
     } catch (WidgetException $e) {
         echo $json->encode(array("error" => $e->getMessage()));
     }
 }