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);
     }
 }
Exemple #2
0
 protected function write($_msg, $_class_info = false)
 {
     $nl = chr(10);
     $msg = '';
     $data = array();
     $data['msg'] = '[' . trim($_msg) . ']';
     $data['request_uri'] = $_SERVER['REQUEST_URI'];
     $data['ip'] = $_SERVER['REMOTE_ADDR'];
     if (!!$_class_info) {
         if (is_object($_class_info)) {
             $data['classname'] = get_class($_class_info);
         } else {
             $data['code'] = $_class_info;
         }
     }
     $msg = str_replace('\\n', ' ', App\Json::encode($data));
     //$msg = $nl.'INFO - '.date('Y-m-d h:i:s A').' --> '.str_replace('\\', '', $msg);
     $msg = $nl . 'INFO - ' . $this->pinoyTime(date('Y-m-d h:i:s A'), 'Y-m-d h:i:s A') . ' --> ' . str_replace('\\', '', $msg);
     App\Files::append($this->log_file, $msg, true);
 }
Exemple #3
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;
 }
Exemple #4
0
 protected function testPusher($_post)
 {
     $p = $_post;
     $this->req($p, ['data']);
     // See: http://www.smashingmagazine.com/2012/05/building-real-time-commenting-system/
     // See: https://github.com/pusher/pusher-http-php
     $app_id = '149666';
     $app_key = '768422d844cb5acf6d6e';
     $app_secret = '2f685782367009dec1bf';
     $pusher = new Pusher($app_key, $app_secret, $app_id);
     // See: https://github.com/pusher/pusher-http-php#debugging--logging
     $pusher->set_logger(new App\Cb\RealTime\Logger());
     $pusher->trigger('currentBID_channel', 'currentBID_event', App\Json::decode($p['data']));
     xplog('API PUSHER TRIGGER: ' . App\Json::encode($p));
     xplog('DATA: ' . $p['data']);
     xplog('API PUSHER CHANNELS: ' . App\Json::encode($pusher->get_channels()));
     return ['api_name' => $_post['api_name'], 'payload' => 1];
 }