public function serve(Request $request)
 {
     $p = [];
     $response = [];
     try {
         // See: http://www.w3schools.com/php/php_exception.asp
         if ($request->isMethod('post')) {
             $p = $request->all();
             App\Cb\Api::setPost($p)->authenticate();
             // Make sure required data is passed to the api
             $api_map = ['check' => 'App\\Cb\\Api::check', 'user_authenticate' => 'App\\Cb\\Api::userAuthenticate', 'logout' => 'App\\Cb\\Api::logout', 'add_user' => 'App\\Cb\\Api\\Users::addUser', 'update_user' => 'App\\Cb\\Api\\Users::updateUser', 'test_pusher' => 'App\\Cb\\Api::testPusher'];
             if (!isset($api_map[$p['api_name']])) {
                 throw new App\Cb\Api\Exceptyon('API "' . $p['api_name'] . '" is unknown');
             }
             $response = App\Cb\Api::run($api_map[$p['api_name']]);
             return response()->json($response);
         } else {
             throw new App\Cb\Api\Exceptyon('Please use POST method in accessing the api');
         }
     } catch (App\Cb\Api\Exceptyon $err) {
         $response['error'] = $err->getMessage();
         $response['passed'] = App\Json::encode($p);
         $response['res'] = App\Json::encode($request->all());
         $response['post'] = App\Json::encode($_POST);
         $response['request'] = App\Json::encode($_REQUEST);
         //xplog('RAW: '.file_get_contents("php://input"));
         if (isset($p['api_name'])) {
             $response['api_name'] = $p['api_name'];
         }
         return response()->json($response);
     }
 }
Beispiel #2
0
 protected function updateUser($_post)
 {
     $p = $_post;
     // Fields that must be set //
     App\Cb\Api::mustSet($p, ['user_id', 'fname', 'lname', 'phone', 'cell', 'company_name', 'company_street', 'company_state', 'company_phone', 'company_abn', 'company_city', 'company_postcode', 'company_color']);
     $uid = intval($p['user_id']);
     if ($uid < 1) {
         App\Cb\Api::error('Invalid user id sent');
     }
     $user_details = App\Cb\Users::getDetailsById($uid);
     if (!$user_details) {
         App\Cb\Api::error('Unable to find user details.');
     }
     // See: https://github.com/Respect/Validation/blob/master/docs/VALIDATORS.md
     $checks = [];
     $checks['fname'] = Valid::string()->notEmpty()->validate($p['fname']);
     $checks['lname'] = Valid::string()->notEmpty()->validate($p['lname']);
     if (in_array(false, $checks)) {
         App\Cb\Api::error('Some required field(s) have invalid values.');
     }
     // Check the logo file passed //
     if (isset($p['company_logo'])) {
         if (App\Json::isValid($p['company_logo'])) {
             // See: http://www.opinionatedgeek.com/dotnet/tools/Base64Encode/
             $logo_details = (object) App\Json::decode($p['company_logo']);
             if (isset($logo_details->base64)) {
                 // Check if file is a valid image //
                 if (!in_array($logo_details->extension, config('cleverbons.files.allowed_images'))) {
                     App\Cb\Api::error('Please upload a valid logo');
                 }
                 $has_uploaded_a_logo = true;
             }
         } else {
             xplog('Invalid JSON string passed |' . $p['company_logo'] . '|', __METHOD__);
         }
     }
     // Update user details //
     $updated_user_details = App\Cb\Users::update($user_details->id, ['fname' => $p['fname'], 'lname' => $p['lname'], 'phone' => $p['phone'], 'cellphone' => $p['cell']]);
     if (!$updated_user_details) {
         App\Cb\Api::error('Unable to save user details');
     }
     // Update user company details //
     $updated_company_details = App\Cb\Users\Company::update($user_details->id, ['name' => $p['company_name'], 'abn' => $p['company_abn'], 'street' => $p['company_street'], 'city' => $p['company_city'], 'state' => $p['company_state'], 'postcode' => $p['company_postcode'], 'phone' => $p['company_phone'], 'primary_color' => $p['company_color']]);
     if (!$updated_company_details) {
         App\Cb\Api::error('Unable to save company details');
     }
     if (isset($has_uploaded_a_logo)) {
         // Save the uploaded logo for his/her company //
         // See: http://www.opinionatedgeek.com/dotnet/tools/Base64Encode/
         if (!App\Cb\Users\Company::saveLogo($user_details->id, $logo_details, true)) {
             xplog('Unable to save logo file for user "' . $uid . '"', __METHOD__);
         }
     }
     // Requery the newly updated user details //
     $user_details = App\Cb\Users::getDetailsById($user_details->id);
     $res = ['api_name' => $_post['api_name'], 'payload' => ['user_details' => $user_details]];
     $user_company_details = App\Cb\Users\Company::getDetailsByUserId($user_details->id);
     if (!!$user_company_details) {
         $res['payload']['company_details'] = $user_company_details;
     }
     return $res;
 }