Ejemplo n.º 1
0
 /**
  * Method launched for the HTTP POST requests
  * 
  * @param  string|null correspond to the URL after /Lan
  * 
  * $instance = new EtherpadLiteClient(Config::get('etherpad-apikey'), Config::get('etherpad-url'));
  * 
  * @throws RestException
  */
 public function post($param = null)
 {
     // Sensure user authentified
     if (!Auth::user()) {
         throw new AuthNotConnectedException();
     }
     $data = RestHandler::getData();
     $padName = "";
     if (isset($data->autogenerate)) {
         // Generate unique random pad name
         $padName = Utilities::generateRandomString(isset($param['padid_length']) ? $param['padid_length'] : 10);
         $custom = true;
     } else {
         if (!isset($data->pad) || !isset($data->pad->pad_name) || $data->pad->pad_name === "") {
             throw new RestException("Unknown call exception", 500);
         } else {
             $padName = $data->pad->pad_name;
         }
     }
     // Ensure pad not already exists
     if (PadComponent::padExists($padName)) {
         throw new PadAlreadyExistsException($padName);
     }
     $cdatas = array('padID' => $padName, 'apikey' => $this->etherpadApiKey, 'text' => Config::get('etherpad-default-text') ? Config::get('etherpad-default-text') : '');
     $result = RestUtilities::callAPI($this->etherpadURLApi . 'createPad', RestMethods::GET, $cdatas);
     //        $result = json_encode(array('code' => 0));
     if ($result) {
         if (isset($result->error)) {
             throw new RestException($result->error);
         } else {
             if ($result->code == 0) {
                 $padConfig = PadConfigComponent::getByUrl($this->etherpadURLPads);
                 $record = array("pad_name" => $padName, "pad_config_id" => $padConfig->id, "creation_date" => date('Y-m-d H:i:s'), "last_update" => date('Y-m-d H:i:s'), "mail_owner" => Auth::user()->email, "status" => 1);
                 if (isset($data->pad->guests)) {
                     $record['guests'] = $data->pad->guests;
                 }
                 $pad = NotesFactory::build(PadComponent::getClass(), $record);
                 $pad->save();
                 $record['url'] = $padName;
                 $userGuests = $this->manageUserGuests($data, $pad);
                 if (count($userGuests['active_emails']) > 0) {
                     // Setting informations
                     $infos = array(array('pad' => array('full_link' => $this->etherpadURLPads . $pad->pad_name, 'pad_name' => $pad->pad_name), 'user' => array('email' => Auth::user()->email)));
                     $this->sendPadInformationsByMail($pad, $userGuests['active_emails'], 'guest_pad', $infos);
                 }
                 if (isset($data->autogenerated)) {
                     // TODO:
                     // Send mail to pad owner
                     // (create new mail template)
                 }
                 return array('data' => array('status' => "success", 'content' => $pad));
             } else {
                 if ($result->code == 1) {
                     // TODO: still in use ?
                     if ($custom) {
                         if (!isset($param['tentative']) || $param['tentative'] < 3) {
                             $opt = array();
                             if (isset($param['tentative'])) {
                                 $opt['tentative'] = $param['tentative'] + 1;
                                 $opt['padid_length'] = isset($param['padid_length']) ? $param['padid_length'] + 1 : 11;
                             } else {
                                 $opt['tentative'] = 1;
                             }
                             $this->post($opt);
                         } else {
                             throw new RestException("Request Time-out", 408);
                         }
                     } else {
                         throw new PadAlreadyExistsException($padName, 409);
                     }
                 } else {
                     throw new PadAlreadyExistsException($padName, 409);
                 }
             }
         }
     } else {
         throw new EtherpadNotFoundException();
     }
 }