Example #1
0
 /**
  * Controls the site update
  * @param string $site_module_file
  * @param boolean $verbose_errors.  Defaults to true
  */
 public static function updateSite($site_module_file, $verbose_errors = true)
 {
     if (!self::isAuthorizedToUpdate()) {
         return false;
     }
     I2CE::longExecution();
     $site_module_file = I2CE_FileSearch::absolut($site_module_file, 1);
     I2CE::siteInitialized(false);
     if (self::_updateSite($site_module_file, $verbose_errors)) {
         $config = I2CE::getConfig();
         $config->__set("/config/site/installation", 'done');
         if (array_key_exists('HTTP_HOST', $_SERVER)) {
             if (array_key_exists('request', $_GET)) {
                 $url = $_GET['request'];
             } else {
                 $url = I2CE::getAccessedBaseURL();
             }
             echo "<br/><span style='color:#993300'>Site was succesully updated.  Continue on to your <a href='{$url}'>site</a>?</span>";
             $msg = "Site was succesully updated.  Continue on to your site?";
             echo "<script type='text/javascript'>if (confirm('{$msg}')) {setTimeout(function() {window.location= '{$url}';},500)}</script>";
             //reload the requested page after 5 seconds
             flush();
             exit;
         } else {
             echo "Site was succesully updated\n";
             $enable = preg_split('/,/', I2CE::getRuntimeVariable('enable', ''), -1, PREG_SPLIT_NO_EMPTY);
             $disable = preg_split('/,/', I2CE::getRuntimeVariable('disable', ''), -1, PREG_SPLIT_NO_EMPTY);
             if (count($enable) + count($disable) > 0) {
                 $succ = self::updateModules($enable, $disable);
                 I2CE::siteInitialized(false);
                 return $succ;
             } else {
                 return true;
             }
         }
     } else {
         return false;
     }
 }
 protected function export_results()
 {
     if (!($flow = $this->request('flow'))) {
         I2CE::raiseError("Bad flow");
         $this->error("Invalid Flow");
         return false;
     }
     $this->instantiateDisplay('Default', $this->view);
     if (!$this->display_obj instanceof I2CE_CustomReport_Display_mHero) {
         I2CE::raiseError("Bad display");
         $this->error("Invalid Display");
         return false;
     }
     I2CE::longExecution();
     $titles = $this->get_display_field_titles();
     I2CE::raiseError("TITLES=" . print_r($titles, true));
     $labels = $this->rapidpro->get_flow_field_labels($flow);
     $headers = array();
     if (array_key_exists('HTTP_USER_AGENT', $_SERVER) && preg_match('/\\s+MSIE\\s+\\d\\.\\d;/', $_SERVER['HTTP_USER_AGENT'])) {
         $headers[] = "application/vnd.ms-excel";
     } else {
         $headers[] = "text/csv; charset=UTF-8";
     }
     $filename = 'mHero-' . '-' . $this->slug . '-export-' . date('Y') . '-' . date('m') . '-' . date('d') . '.csv';
     $headers[] = "Content-disposition: attachment; filename=\"{$filename}\"";
     $out = fopen("php://output", 'w');
     $keys = array('entityID' => 'Health Worker Registry Entity ID', 'iHRIS_link' => 'iHRIS Source Record');
     $keys = array_merge($keys, $labels);
     $keys = array_merge($keys, $titles);
     I2CE::raiseError("KEYS=" . print_r($keys, true));
     $csd_uuids = $this->display_obj->get_csd_uuids();
     $person_ids = array_flip($csd_uuids);
     $assigning_authority = $this->host . '/' . $this->slug;
     $contacts = $this->rapidpro->getOtherIDs($this->csd_host, $csd_uuids, $assigning_authority);
     $contact_list = array();
     foreach ($contacts as $entityID => $contact) {
         if (!is_array($contact) || !array_key_exists('otherID', $contact) || !is_array($contact['otherID']) || count($contact['otherID']) != 1 || !reset($contact['otherID']) || !is_array($otherID = current($contact['otherID'])) || !array_key_exists('value', $otherID) || !($rapidpro_id = $otherID['value'])) {
             continue;
         }
         $contact_list[$rapidpro_id] = $entityID;
     }
     I2CE::raiseError(print_r($contacts, true));
     I2CE::raiseError(print_r($contact_list, true));
     $runs = $this->rapidpro->getFlowValues($flow);
     //would be better if we could put the streaming function as a callback after each hit on rapidpro
     $results = array();
     foreach ($runs as $run) {
         I2CE::raiseMessage($run['contact']);
         if (!is_array($run) || !array_key_exists('contact', $run) || !($rapidpro_id = $run['contact']) || !array_key_exists($rapidpro_id, $contact_list) || !($entityID = $contact_list[$rapidpro_id]) || !array_key_exists('values', $run) || !is_array($run['values'])) {
             continue;
         }
         $values = array();
         foreach ($run['values'] as $val_set) {
             if (!is_array($val_set) || !array_key_exists('label', $val_set) || !($label = $val_set['label']) || !array_key_exists('text', $val_set)) {
                 continue;
             }
             $values[$label] = $val_set['text'];
         }
         $results[$entityID] = $values;
     }
     I2CE::raiseError(print_r($results, true));
     if ($errors = I2CE_Dumper::cleanlyEndOutputBuffers()) {
         I2CE::raiseError("Got errors:\n{$errors}");
     }
     I2CE::longExecution();
     foreach ($headers as $header) {
         header($header);
     }
     fputcsv($out, $keys);
     foreach ($results as $entityID => $values) {
         if (!array_key_exists($entityID, $person_ids) || !($person_id = $person_ids[$entityID])) {
             continue;
         }
         $iHRIS_link = I2CE::getAccessedBaseURL() . 'view?id=' . $person_id;
         $fields = array('entityID' => $entityID, 'iHRIS_link' => $iHRIS_link);
         foreach ($labels as $label) {
             if (!array_key_exists($label, $values)) {
                 $fields[] = '';
             } else {
                 $fields[] = $values[$label];
             }
         }
         $fields = array_merge($fields, $this->get_display_fields($person_id));
         foreach ($titles as $title => $label) {
             if (!array_key_exists($title, $fields) || !is_scalar($val = $fields[$title])) {
                 $fields[$title] = '';
             } else {
                 $fields[$title] = $val;
             }
         }
         fputcsv($out, $fields);
         flush();
     }
     fclose($out);
     exit(0);
 }
Example #3
0
 /**
  *Returns the base url from which the site was accessed.  If no .htaccess is used, ths will include the index.php.
  *If rewrites are used (via .htacces) this will no include the index.php. Point is... this is the base url from which
  *the site was accessed, no questions asked.  This of course assumes that you are now accessing the site via the command line
  *@return string
  */
 public static function getAccessedBaseURL()
 {
     return I2CE::getAccessedBaseURL();
 }
 /**
  * Trigger an event and call any associated handlers for all users linked to the trigger.
  * @param string $trigger The trigger being called
  * @param string $msg_key A key to look up in /modules/UserTriggers/messages to get the message to display.
  * @param string $message The message to include with the $msg_key if it exists.
  * @param string $link The link to send to the handlers (or use the default if set to true)
  * @param string $link_add Any data to include to the end of the link
  * @param array $args Any custom args to override for each handler that may be used, otherwise the default from MD will
  *                    be used.
  * @return boolean
  */
 public function trigger($trigger, $msg_key = '', $message = '', $link = false, $link_add = '', $args = array())
 {
     if (!$this->config->__isset("triggers/{$trigger}")) {
         I2CE::raiseError("No trigger found for {$trigger}!");
         return false;
     }
     $trigger_opts = $this->config->triggers->{$trigger}->getAsArray();
     $handlers = $this->config->handlers->getAsArray();
     $call_handlers = array();
     if (array_key_exists('handler', $trigger_opts) && is_array($trigger_opts['handler'])) {
         foreach ($trigger_opts['handler'] as $handler => $bool) {
             if (array_key_exists($handler, $handlers) && $bool) {
                 $call_handlers[] = $handler;
                 if (!array_key_exists('args', $trigger_opts)) {
                     $trigger_opts['args'] = array();
                 }
                 if (!array_key_exists($handler, $trigger_opts['args'])) {
                     $trigger_opts['args'][$handler] = array();
                 }
                 if (array_key_exists($handler, $args)) {
                     foreach ($args[$handler] as $key => $val) {
                         // Override the default args with whatever is sent to the method call.
                         $trigger_opts['args'][$handler][$key] = $val;
                     }
                 }
             }
         }
     }
     if (count($call_handlers) == 0) {
         I2CE::raiseError("No handlers enabled for {$trigger} so nothing to do!");
         return false;
     }
     $msg_defaults = array('prefix' => '', 'suffix' => '', 'link_text' => '', 'link' => '');
     if (array_key_exists('message', $trigger_opts)) {
         foreach ($msg_defaults as $key => &$val) {
             if (array_key_exists($key, $trigger_opts['message'])) {
                 $val = $trigger_opts['message'][$key];
             }
         }
     }
     $msg_lookup = '';
     if ($msg_key) {
         $this->config->setIfIsSet($msg_lookup, "messages/{$msg_key}");
     }
     if ($link) {
         if (is_string($link) && strlen($link) > 2) {
             $msg_defaults['link'] = $link;
         }
     }
     if ($link_add) {
         $msg_defaults['link'] .= $link_add;
     }
     if (strlen($msg_defaults['link']) > 2 && strtolower(substr($msg_defaults['link'], 0, 4)) != 'http') {
         $site_url = I2CE::getAccessedBaseURL();
         if ($msg_defaults['link'][0] == '/') {
             $site_url = substr($site_url, 0, strpos($site_url, '/', 9));
         }
         $msg_defaults['link'] = $site_url . $msg_defaults['link'];
     }
     $full_message = $msg_defaults['prefix'] . " " . $msg_lookup . " " . $message . " " . $msg_defaults['suffix'];
     $trigger_where = array('operator' => 'FIELD_LIMIT', 'style' => 'equals', 'field' => 'trigger', 'data' => array('value' => $trigger));
     $triggers = I2CE_FormStorage::listFields('user_trigger', array('parent'), false, $trigger_where);
     $ret_val = true;
     foreach ($triggers as $data) {
         $user_form = I2CE_FormFactory::instance()->createContainer($data['parent']);
         foreach ($call_handlers as $handler) {
             foreach ($handlers[$handler] as $module => $method) {
                 $modObj = $this->module_factory->getClass($module);
                 if ($modObj instanceof I2CE_Module) {
                     if ($modObj->_hasMethod($method)) {
                         if (!$modObj->{$method}($user_form->username, $trigger, $full_message, $msg_defaults['link'], $msg_defaults['link_text'], $trigger_opts['args'][$handler])) {
                             I2CE::raiseError("Error trying to call {$method} on {$module}");
                             $ret_val &= false;
                         }
                     } else {
                         I2CE::raiseError("Can't find method {$method} on {$module}");
                         $ret_val &= false;
                     }
                 } else {
                     I2CE::raiseError("{$module} isn't an instance of I2CE_Module");
                     $ret_val &= false;
                 }
             }
         }
     }
     return $ret_val;
 }
Example #5
0
 public static function dumpContents($file_loc, $headers, $cacheTime)
 {
     //now do the caching header for a file... http://www.php.net/manual/en/function.header.php#61903
     if (!is_readable($file_loc)) {
         I2CE::raiseError("Unable to read file at {$file_loc}", E_USER_NOTICE);
         //do something else?
         return false;
     }
     self::prepForDump(filemtime($file_loc), filesize($file_loc), $headers, $cacheTime);
     //open the file
     if (is_array($_GET) && array_key_exists('newman', $_GET)) {
         $contents = file_get_contents($file_loc);
         $contents = str_replace("{I2CESITEROOT}", I2CE::getAccessedBaseURL(), $contents);
         echo $contents;
     } else {
         readfile($file_loc);
     }
     return true;
 }