private function applet($flow_id, $inst_id, $type = 'voice') { $flow_id = $this->set_flow_id($flow_id); $flow = $this->get_flow(); $instance = null; $applet = null; try { switch ($type) { case 'sms': if (isset($_REQUEST['Body']) && $inst_id == 'start') { $_COOKIE['sms-body'] = $_REQUEST['Body']; $sms = $_REQUEST['Body']; // Expires after three hours set_cookie('sms-body', $sms, 60 * 60 * 3); } else { $sms = isset($_COOKIE['sms-body']) ? $_COOKIE['sms-body'] : null; set_cookie('sms-body', null, time() - 3600); } $sms_data = $flow->sms_data; if (!empty($sms_data)) { $flow_data = get_object_vars(json_decode($sms_data)); $instance = isset($flow_data[$inst_id]) ? $flow_data[$inst_id] : null; } if (!is_null($instance)) { $plugin_dir_name = ''; $applet_dir_name = ''; list($plugin_dir_name, $applet_dir_name) = explode('---', $instance->type); $applet = Applet::get($plugin_dir_name, $applet_dir_name, null, $instance); $applet->flow_type = $type; $applet->instance_id = $inst_id; $applet->sms = $sms; if ($sms) { $_POST['Body'] = $_GET['Body'] = $_REQUEST['Body'] = $sms; } $this->session->unset_userdata('sms-body'); $applet->currentURI = site_url("twiml/applet/sms/{$flow_id}/{$inst_id}"); $baseURI = site_url("twiml/applet/sms/{$flow_id}/"); $this->applet_headers($applet, $plugin_dir_name); echo $applet->twiml($flow, $baseURI, $instance); } break; case 'voice': $voice_data = $flow->data; if (!empty($voice_data)) { $flow_data = get_object_vars(json_decode($voice_data)); $instance = isset($flow_data[$inst_id]) ? $flow_data[$inst_id] : null; } if (!is_null($instance)) { $plugin_dir_name = ''; $applet_dir_name = ''; list($plugin_dir_name, $applet_dir_name) = explode('---', $instance->type); $applet = Applet::get($plugin_dir_name, $applet_dir_name, null, $instance); $applet->flow_type = $type; $applet->instance_id = $inst_id; $applet->currentURI = site_url("twiml/applet/voice/{$flow_id}/{$inst_id}"); $baseURI = site_url("twiml/applet/voice/{$flow_id}/"); $this->applet_headers($applet, $plugin_dir_name); echo $applet->twiml($flow, $baseURI, $instance); } break; } if (!is_object($applet)) { $this->response->say('Unknown applet instance in flow ' . $flow_id, $this->say_params); $this->response->respond(); } } catch (Exception $ex) { $this->response->say('Error: ' + $ex->getMessage(), $this->say_params); $this->response->respond(); } }
public static function get_applets($flow_type = 'voice') { $applets = array(); $applet_names_by_plugin = array(); $plugin_dir_names = scandir(PLUGIN_PATH); foreach ($plugin_dir_names as $plugin_dir_name) { // Ignore current pwd if ($plugin_dir_name[0] == '.') { continue; } // Add to applet collection if contains an applets dir $applets_path = PLUGIN_PATH . '/' . $plugin_dir_name . '/applets'; if (is_dir($applets_path)) { $applet_names_by_plugin[$plugin_dir_name] = scandir($applets_path); } } // Iterate each applet to check for valid structure and create an instance foreach ($applet_names_by_plugin as $plugin_dir_name => $applet_dir_names) { foreach ($applet_dir_names as $applet_dir_name) { // Ignore current pwd if ($applet_dir_name[0] == '.') { continue; } try { $applet = null; $applet_path = PLUGIN_PATH . '/' . $plugin_dir_name . '/applets/' . $applet_dir_name; // Sanity check and make sure this path is accessible if (!is_dir($applet_path)) { throw new AppletException("Applet path inaccessible {$applet_path}"); } // Check for required files $required_min = count(self::$requiredFiles); $files = scandir($applet_path); // If we have the minimum number of required files, lets move on. $required = array_intersect(self::$requiredFiles, $files); if (count($required) < $required_min) { throw new AppletException("Missing a required file, found: " . implode(', ', $files) . "\nRequired: " . implode(', ', self::$requiredFiles)); } /* Process configuration */ $applet_config = file_get_contents($applet_path . '/applet.json'); $applet_config = json_decode($applet_config); if (is_null($applet_config)) { throw new AppletException("Syntax error in applet.json"); } /* Build Applet Instance */ $applet = Applet::get($plugin_dir_name, $applet_dir_name, $applet_config); $applet->flow_type = $flow_type; if (is_null($applet)) { throw new AppletException("Failed to create instance of applet"); } $applets[$applet->id] = $applet; } catch (AppletException $e) { // Your applet developer has failed you at this point $message = "An error occurred loading applet: {$applet_dir_name} from {$plugin_dir_name}"; // Notify the user $ci =& get_instance(); $ci->session->set_userdata('error', $message); // Next applet in the list continue; } } } uasort($applets, array("Applet", "applet_sort")); return $applets; }