build_context_path(); mtrace('Built context paths'); mtrace("Finished clean-up tasks..."); } // End of occasional clean-up tasks if (empty($CFG->disablescheduledbackups)) { // Defined in config.php //Execute backup's cron //Perhaps a long time and memory could help in large sites @set_time_limit(0); @raise_memory_limit("192M"); if (function_exists('apache_child_terminate')) { // if we are running from Apache, give httpd a hint that // it can recycle the process after it's done. Apache's // memory management is truly awful but we can help it. @apache_child_terminate(); } if (file_exists("{$CFG->dirroot}/backup/backup_scheduled.php") and file_exists("{$CFG->dirroot}/backup/backuplib.php") and file_exists("{$CFG->dirroot}/backup/lib.php") and file_exists("{$CFG->libdir}/blocklib.php")) { include_once "{$CFG->dirroot}/backup/backup_scheduled.php"; include_once "{$CFG->dirroot}/backup/backuplib.php"; include_once "{$CFG->dirroot}/backup/lib.php"; require_once "{$CFG->libdir}/blocklib.php"; mtrace("Running backups if required..."); if (!schedule_backup_cron()) { mtrace("ERROR: Something went wrong while performing backup tasks!!!"); } else { mtrace("Backup tasks finished."); } } } if (!empty($CFG->enablerssfeeds)) {
/** * This function expects to called during shutdown * should be set via register_shutdown_function() * in lib/setup.php . * * @return void */ function moodle_request_shutdown() { global $CFG; // help apache server if possible $apachereleasemem = false; if (function_exists('apache_child_terminate') && function_exists('memory_get_usage') && ini_get_bool('child_terminate')) { $limit = empty($CFG->apachemaxmem) ? 64 * 1024 * 1024 : $CFG->apachemaxmem; //64MB default if (memory_get_usage() > get_real_size($limit)) { $apachereleasemem = $limit; @apache_child_terminate(); } } // deal with perf logging if (defined('MDL_PERF') || (!empty($CFG->perfdebug) and $CFG->perfdebug > 7)) { if ($apachereleasemem) { error_log('Mem usage over ' . $apachereleasemem . ': marking Apache child for reaping.'); } if (defined('MDL_PERFTOLOG')) { $perf = get_performance_info(); error_log("PERF: " . $perf['txt']); } if (defined('MDL_PERFINC')) { $inc = get_included_files(); $ts = 0; foreach ($inc as $f) { if (preg_match(':^/:', $f)) { $fs = filesize($f); $ts += $fs; $hfs = display_size($fs); error_log(substr($f, strlen($CFG->dirroot)) . " size: {$fs} ({$hfs})", NULL, NULL, 0); } else { error_log($f, NULL, NULL, 0); } } if ($ts > 0) { $hts = display_size($ts); error_log("Total size of files included: {$ts} ({$hts})"); } } } }
function moodle_request_shutdown() { global $CFG; // initially, we are only ever called under apache // but check just in case if (function_exists('apache_child_terminate') && function_exists('memory_get_usage') && ini_get_bool('child_terminate')) { if (empty($CFG->apachemaxmem)) { $CFG->apachemaxmem = 25000000; // default 25MiB } if (memory_get_usage() > (int) $CFG->apachemaxmem) { trigger_error('Mem usage over $CFG->apachemaxmem: marking child for reaping.'); @apache_child_terminate(); } } if (defined('MDL_PERF') || (!empty($CFG->perfdebug) and $CFG->perfdebug > 7)) { if (defined('MDL_PERFTOLOG')) { $perf = get_performance_info(); error_log("PERF: " . $perf['txt']); } if (defined('MDL_PERFINC')) { $inc = get_included_files(); $ts = 0; foreach ($inc as $f) { if (preg_match(':^/:', $f)) { $fs = filesize($f); $ts += $fs; $hfs = display_size($fs); error_log(substr($f, strlen($CFG->dirroot)) . " size: {$fs} ({$hfs})", NULL, NULL, 0); } else { error_log($f, NULL, NULL, 0); } } if ($ts > 0) { $hts = display_size($ts); error_log("Total size of files included: {$ts} ({$hts})"); } } } }
/** * 在本次请求结束后终止 apache 子进程 * <code>Apache::terminate();</code> * @return bool 成功返回true,失败返回false */ public static function terminate() { return @apache_child_terminate(); }
function message() { $this->bo = CreateObject("email.bomessage"); $this->bo->message_data(); if ($GLOBALS['phpgw']->msg->phpgw_before_xslt) { // we point to the global template for this version of phpgw templatings $this->tpl =& $GLOBALS['phpgw']->template; //$this->tpl = CreateObject('phpgwapi.Template',PHPGW_APP_TPL); } else { // we use a PRIVATE template object for 0.9.14 conpat and during xslt porting $this->tpl = CreateObject('phpgwapi.Template', PHPGW_APP_TPL); } if ($GLOBALS['phpgw']->msg->phpgw_before_xslt) { // NOW we can out the header, because "index_data()" filled this global // $GLOBALS['phpgw_info']['flags']['email_refresh_uri'] // which is needed to preserve folder and sort settings during the auto-refresh-ing // currently (Dec 6, 2001) that logic is in phpgwapi/inc/templates/idsociety/head.inc.php unset($GLOBALS['phpgw_info']['flags']['noheader']); unset($GLOBALS['phpgw_info']['flags']['nonavbar']); $GLOBALS['phpgw_info']['flags']['noappheader'] = True; $GLOBALS['phpgw_info']['flags']['noappfooter'] = True; $GLOBALS['phpgw']->common->phpgw_header(); // HOWEVER still this class must NOT invoke $GLOBALS['phpgw']->common->phpgw_header() // even though we had to output the header (go figure... :) } else { $GLOBALS['phpgw']->xslttpl->add_file(array('app_data')); } // ---- BEGIN UIMESSAGE $this->tpl->set_file(array('T_message_main' => 'message_main.tpl', 'T_message_echo_dump' => 'message_echo_dump.tpl')); $this->tpl->set_block('T_message_main', 'B_x-phpgw-type', 'V_x-phpgw-type'); $this->tpl->set_block('T_message_main', 'B_cc_data', 'V_cc_data'); $this->tpl->set_block('T_message_main', 'B_attach_list', 'V_attach_list'); $this->tpl->set_block('T_message_main', 'B_debug_parts', 'V_debug_parts'); $this->tpl->set_block('T_message_main', 'B_display_part', 'V_display_part'); $this->tpl->set_block('T_message_echo_dump', 'B_setup_echo_dump', 'V_setup_echo_dump'); $this->tpl->set_block('T_message_echo_dump', 'B_done_echo_dump', 'V_done_echo_dump'); //= = = = TOOLBAR WIDGET = = = $this->widgets = CreateObject('email.html_widgets'); $this->tpl->set_var('widget_toolbar', $this->widgets->get_toolbar()); // geek_bar $this->tpl->set_var('geek_bar', $this->widgets->get_geek_bar()); if (!empty($this->xi['msgtype'])) { $this->tpl->set_var('application', $this->bo->xi['application']); $this->tpl->parse('V_x-phpgw-type', 'B_x-phpgw-type'); } else { $this->tpl->set_var('V_x-phpgw-type', ''); } // ---- TOOL BAR / MENU BAR ---- $tpl_vars = array('bar_back1' => '', 'move_current_sort' => $this->bo->xi['move_current_sort'], 'move_current_order' => $this->bo->xi['move_current_order'], 'move_current_start' => $this->bo->xi['move_current_start'], 'mlist_checkbox_name' => $this->bo->xi['mlist_checkbox_name'], 'mlist_embedded_uri' => $this->bo->xi['mlist_embedded_uri'], 'frm_delmov_action' => $this->bo->xi['frm_delmov_action'], 'frm_delmov_name' => $this->bo->xi['frm_delmov_name'], 'delmov_listbox' => $this->bo->xi['delmov_listbox'], 'move_postmove_goto_name' => $this->bo->xi['move_postmove_goto_name'], 'move_postmove_goto_value' => $this->bo->xi['move_postmove_goto_value'], 'tofrom_labels_bkcolor' => $this->bo->xi['tofrom_labels_bkcolor'], 'tofrom_labels_class' => $this->bo->xi['tofrom_labels_class'], 'tofrom_data_bkcolor' => $this->bo->xi['tofrom_data_bkcolor'], 'tofrom_data_class' => $this->bo->xi['tofrom_data_class'], 'lang_inbox_folder' => $this->bo->xi['lang_inbox'], 'lang_from' => $this->bo->xi['lang_from'], 'lang_to' => $this->bo->xi['lang_to'], 'lang_cc' => $this->bo->xi['lang_cc'], 'lang_date' => $this->bo->xi['lang_date'], 'lang_files' => $this->bo->xi['lang_files'], 'lang_subject' => $this->bo->xi['lang_subject'], 'from_data_final' => $this->bo->xi['from_data_final'], 'to_data_final' => $this->bo->xi['to_data_final']); $this->tpl->set_var($tpl_vars); // ---- Cc: Message Data ----- //if (isset($msg_headers->cc) && count($msg_headers->cc) > 0) // $this->bo->xi[' if (isset($this->bo->xi['cc_data_final']) && $this->bo->xi['cc_data_final'] != '') { $this->tpl->set_var('cc_data_final', $this->bo->xi['cc_data_final']); $this->tpl->parse('V_cc_data', 'B_cc_data'); } else { $this->tpl->set_var('V_cc_data', ''); } // ---- Message Date (set above) ----- $this->tpl->set_var('message_date', $this->bo->xi['message_date']); // ---- Message Subject (set above) ----- $this->tpl->set_var('message_subject', $this->bo->xi['message_subject']); // ---- Attachments List ----- if ($this->bo->xi['list_of_files'] != '') { $this->tpl->set_var('list_of_files', $this->bo->xi['list_of_files']); $this->tpl->parse('V_attach_list', 'B_attach_list'); } else { $this->tpl->set_var('V_attach_list', ''); } $tpl_vars = array('theme_font' => $this->bo->xi['theme_font'], 'theme_th_bg' => $this->bo->xi['theme_th_bg'], 'theme_row_on' => $this->bo->xi['theme_row_on'], 'reply_btns_bkcolor' => $this->bo->xi['reply_btns_bkcolor'], 'reply_btns_text' => $this->bo->xi['reply_btns_text'], 'go_back_to' => $this->bo->xi['lang_go_back_to'], 'lnk_goback_folder' => $this->bo->xi['lnk_goback_folder']); $this->tpl->set_var($tpl_vars); // ---- DEBUG: Show Information About Each Part ----- // the debug output needs updating if ($this->bo->debug > 0) { $this->tpl->set_var('msg_body_info', $this->bo->xi['msg_body_info']); $this->tpl->parse('V_debug_parts', 'B_debug_parts'); } else { $this->tpl->set_var('V_debug_parts', ''); } // ----- Message_Display Template Handles it from here ------- $this->tpl->set_var('theme_font', $this->bo->xi['theme_font']); $this->tpl->set_var('theme_th_bg', $this->bo->xi['theme_th_bg']); $this->tpl->set_var('theme_row_on', $this->bo->xi['theme_row_on']); // ---- so called "TOOLBAR" between the msg header data and the message siaplay switch ($GLOBALS['phpgw']->msg->get_pref_value('button_type')) { case 'text': $this->tpl->set_var('view_option', $this->bo->xi['view_option']); $this->tpl->set_var('view_option_ilnk', ''); $this->tpl->set_var('view_headers_href', $this->bo->xi['view_headers_href']); $this->tpl->set_var('view_headers_ilnk', ''); $this->tpl->set_var('view_raw_message_href', $this->bo->xi['view_raw_message_href']); $this->tpl->set_var('view_raw_message_ilnk', ''); $this->tpl->set_var('view_printable_href', $this->bo->xi['view_printable_href']); $this->tpl->set_var('view_printable_ilnk', ''); // reply and navigation toolbar thing $this->tpl->set_var('href_reply', $this->bo->xi['href_reply']); $this->tpl->set_var('href_replyall', $this->bo->xi['href_replyall']); $this->tpl->set_var('href_forward', $this->bo->xi['href_forward']); $this->tpl->set_var('href_delete', $this->bo->xi['href_delete']); $this->tpl->set_var('ilnk_reply', ''); $this->tpl->set_var('ilnk_replyall', ''); $this->tpl->set_var('ilnk_forward', ''); $this->tpl->set_var('ilnk_delete', ''); $this->tpl->set_var('href_prev_msg', $this->bo->xi['href_prev_msg']); $this->tpl->set_var('href_next_msg', $this->bo->xi['href_next_msg']); //$this->tpl->set_var('text_nbsp',' '); $this->tpl->set_var('ilnk_prev_msg', ''); $this->tpl->set_var('ilnk_next_msg', ''); break; case 'image': $this->tpl->set_var('view_option', ''); $this->tpl->set_var('view_option_ilnk', $this->bo->xi['view_option_ilnk']); $this->tpl->set_var('view_headers_href', ''); $this->tpl->set_var('view_headers_ilnk', $this->bo->xi['view_headers_ilnk']); $this->tpl->set_var('view_raw_message_href', ''); $this->tpl->set_var('view_raw_message_ilnk', $this->bo->xi['view_raw_message_ilnk']); $this->tpl->set_var('view_printable_href', ''); $this->tpl->set_var('view_printable_ilnk', $this->bo->xi['view_printable_ilnk']); // reply and navigation toolbar thing $this->tpl->set_var('href_reply', ''); $this->tpl->set_var('href_replyall', ''); $this->tpl->set_var('href_forward', ''); $this->tpl->set_var('href_delete', ''); $this->tpl->set_var('ilnk_reply', $this->bo->xi['ilnk_reply']); $this->tpl->set_var('ilnk_replyall', $this->bo->xi['ilnk_replyall']); $this->tpl->set_var('ilnk_forward', $this->bo->xi['ilnk_forward']); $this->tpl->set_var('ilnk_delete', $this->bo->xi['ilnk_delete']); $this->tpl->set_var('href_prev_msg', ''); $this->tpl->set_var('href_next_msg', ''); //$this->tpl->set_var('text_nbsp',''); $this->tpl->set_var('ilnk_prev_msg', $this->bo->xi['ilnk_prev_msg']); $this->tpl->set_var('ilnk_next_msg', $this->bo->xi['ilnk_next_msg']); break; //case 'both': //case 'both': default: $this->tpl->set_var('view_option', $this->bo->xi['view_option']); $this->tpl->set_var('view_option_ilnk', $this->bo->xi['view_option_ilnk']); $this->tpl->set_var('view_headers_href', $this->bo->xi['view_headers_href']); $this->tpl->set_var('view_headers_ilnk', $this->bo->xi['view_headers_ilnk']); $this->tpl->set_var('view_raw_message_href', $this->bo->xi['view_raw_message_href']); $this->tpl->set_var('view_raw_message_ilnk', $this->bo->xi['view_raw_message_ilnk']); $this->tpl->set_var('view_printable_href', $this->bo->xi['view_printable_href']); $this->tpl->set_var('view_printable_ilnk', $this->bo->xi['view_printable_ilnk']); // reply and navigation toolbar thing $this->tpl->set_var('href_reply', $this->bo->xi['href_reply']); $this->tpl->set_var('href_replyall', $this->bo->xi['href_replyall']); $this->tpl->set_var('href_forward', $this->bo->xi['href_forward']); $this->tpl->set_var('href_delete', $this->bo->xi['href_delete']); $this->tpl->set_var('ilnk_reply', $this->bo->xi['ilnk_reply']); $this->tpl->set_var('ilnk_replyall', $this->bo->xi['ilnk_replyall']); $this->tpl->set_var('ilnk_forward', $this->bo->xi['ilnk_forward']); $this->tpl->set_var('ilnk_delete', $this->bo->xi['ilnk_delete']); $this->tpl->set_var('href_prev_msg', ''); $this->tpl->set_var('href_next_msg', ''); //$this->tpl->set_var('href_prev_msg',$this->bo->xi['href_prev_msg']); //$this->tpl->set_var('href_next_msg',$this->bo->xi['href_next_msg']); //$this->tpl->set_var('text_nbsp',' '); $this->tpl->set_var('ilnk_prev_msg', $this->bo->xi['ilnk_prev_msg']); $this->tpl->set_var('ilnk_next_msg', $this->bo->xi['ilnk_next_msg']); break; } // ----- SHOW MESSAGE ------- //@set_time_limit(120); //if ($GLOBALS['phpgw']->msg->phpgw_before_xslt == False) //{ // fix a b0rk in template showing last item 2 times under XSLT $final_V_display_part = ''; //} $count_part_nice = count($this->bo->part_nice); for ($i = 0; $i < $count_part_nice; $i++) { if ($this->bo->part_nice[$i]['d_instructions'] == 'show') { $this->tpl->set_var('title_text', $this->bo->part_nice[$i]['title_text']); $this->tpl->set_var('display_str', $this->bo->part_nice[$i]['display_str']); $this->tpl->set_var('message_body', $this->bo->part_nice[$i]['message_body']); //$this->tpl->parse('V_display_part','B_display_part', True); //if ($GLOBALS['phpgw']->msg->phpgw_before_xslt) //{ // $this->tpl->parse('V_display_part','B_display_part', True); //} //else //{ // fix a b0rk in template showing last item 2 times under XSLT $final_V_display_part .= $this->tpl->parse('V_display_part', 'B_display_part'); //} } elseif ($this->bo->part_nice[$i]['d_instructions'] == 'echo_out') { // output a blank message body, we'll use an alternate method below $this->tpl->set_var('V_display_part', ''); // ----- Finished With Message_Mail Template, Output It $this->tpl->pfp('out', 'T_message_main'); // ----- Prepare a Table for this Echo Dump $this->tpl->set_var('title_text', $this->bo->part_nice[$i]['title_text']); $this->tpl->set_var('display_str', $this->bo->part_nice[$i]['display_str']); $this->tpl->parse('V_setup_echo_dump', 'B_setup_echo_dump'); $this->tpl->set_var('V_done_echo_dump', ''); $this->tpl->pfp('out', 'T_message_echo_dump'); // ----- Prepare $msgball data for phpgw_fetchbody() $msgball = $GLOBALS['phpgw']->msg->get_arg_value('msgball'); $msgball['part_no'] = $this->bo->part_nice[$i]['m_part_num_mime']; // ----- Echo This Data Directly to the Client // since the php version of this of b0rked for large msgs, perhaps use sockets code? echo '<pre>'; echo $GLOBALS['phpgw']->msg->phpgw_fetchbody($msgball); echo '</pre>'; // ----- Close Table $this->tpl->set_var('V_setup_echo_dump', ''); $this->tpl->parse('V_done_echo_dump', 'B_done_echo_dump'); $this->tpl->pfp('out', 'T_message_echo_dump'); // = = = = = ======= CLEANUP AND EXIT PAGE ======= = = = = = = $did_echo_dump = True; break; } } //@set_time_limit(0); //if ($GLOBALS['phpgw']->msg->phpgw_before_xslt == False) //{ // // fix a b0rk in template showing last item 2 times under XSLT $this->tpl->set_var('V_display_part', $final_V_display_part); $final_V_display_part = ''; unset($final_V_display_part); //} // new way to handle debug data, if there is debug data, this will put it in the template source data vars $this->tpl->set_var('debugdata', $GLOBALS['phpgw']->msg->dbug->notice_pagedone()); if (isset($did_echo_dump) && $did_echo_dump == True) { // DO NOTHING! // echo dump already outputted the template } elseif ($GLOBALS['phpgw']->msg->phpgw_before_xslt) { $this->tpl->pfp('out', 'T_message_main'); //$GLOBALS['phpgw']->common->phpgw_footer(); } else { $this->tpl->set_unknowns('comment'); //$this->tpl->set_unknowns('remove'); $data = array(); //$data['appname'] = lang('E-Mail'); //$data['function_msg'] = lang('show message'); $GLOBALS['phpgw_info']['flags']['email']['app_header'] = lang('E-Mail') . ': ' . lang('show message'); $data['email_page'] = $this->tpl->parse('out', 'T_message_main'); // new way to handle debug data, if this array has anything, put it in the template source data vars //if ($GLOBALS['phpgw']->msg->dbug->debugdata) //{ // $data['debugdata'] = $GLOBALS['phpgw']->msg->dbug->get_debugdata_stack(); //} //$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('uimessage' => $data)); $GLOBALS['phpgw']->xslttpl->set_var('phpgw', array('generic_out' => $data)); } // ralfbecker patch dated 021124 if (isset($this->bo->xi['application'])) { global $calendar_id; $calendar_id = $this->bo->xi['calendar_id']; $GLOBALS['phpgw']->hooks->single('email', $this->bo->xi['application']); } // tell apache to release emeory back to the system on script end //if ((isset($_SERVER['SERVER_SOFTWARE'])) //&& (stristr($_SERVER['SERVER_SOFTWARE'], 'apache')) if (getenv('SERVER_SOFTWARE') && stristr(getenv('SERVER_SOFTWARE'), 'apache') && function_exists('apache_child_terminate') && ini_get('child_terminate')) { //echo 'doing apache_child_terminate'; apache_child_terminate(); } // close down ALL mailserver streams $GLOBALS['phpgw']->msg->end_request(); // destroy the object $GLOBALS['phpgw']->msg = ''; unset($GLOBALS['phpgw']->msg); }
/** * Opens and parses/checks a VMoodle nodelist for snapshotting. Basically * compatible with nodelist format. * @param string $nodelistlocation */ function vmoodle_parse_csv_snaplist($nodelistlocation = '') { global $CFG; vmoodle_setup_local_config(); $vnodes = array(); if (empty($nodelistlocation)) { $nodelistlocation = $CFG->dataroot . '/vmoodle/snaplist.csv'; } // Decode file. $csv_encode = '/\\&\\#44/'; if (isset($CFG->local_vmoodle_csvseparator)) { $csv_delimiter = '\\' . $CFG->local_vmoodle_csvseparator; $csv_delimiter2 = $CFG->local_vmoodle_csvseparator; if (isset($CFG->CSV_ENCODE)) { $csv_encode = '/\\&\\#' . $CFG->CSV_ENCODE . '/'; } } else { $csv_delimiter = "\\;"; $csv_delimiter2 = ";"; } /* * File that is used is currently hardcoded here ! * Large files are likely to take their time and memory. Let PHP know * that we'll take longer, and that the process should be recycled soon * to free up memory. */ @set_time_limit(0); @raise_memory_limit("1024M"); if (function_exists('apache_child_terminate')) { @apache_child_terminate(); } // Make arrays of valid fields for error checking. $required = array('vhostname' => 1); // this will allow using a node creation list to operate $optional = array('name' => 1, 'shortname' => 1, 'vdatapath' => 1, 'vdbname' => 1, 'vdblogin' => 1, 'vdbpass' => 1, 'description' => 1, 'vdbhost' => 1, 'vdbpersist' => 1, 'vtemplate' => 1, 'services' => 1, 'enabled' => 1, 'mnet' => 1); $optionalDefaults = array('mnet' => 1, 'vdbtype' => 'mysqli', 'vdbhost' => $CFG->dbhost, 'vdbpersist' => $CFG->dboptions['dbpersist'], 'vdbprefix' => 'mdl_', 'vtemplate' => '', 'enabled' => 1, 'services' => 'default'); $patterns = array(); $metas = array('auth_.*', 'block_.*', 'mod_.*', 'local_.*', 'report_.*', 'format_.*', 'config_.*'); // Get header (field names). $textlib = new textlib(); if (!($fp = fopen($nodelistlocation, 'rb'))) { cli_error(get_string('badnodefile', 'local_vmoodle', $nodelistlocation)); } // Jump any empty or comment line. $text = fgets($fp, 1024); $i = 0; while (vmoodle_is_empty_line_or_format($text, $i == 0)) { $text = fgets($fp, 1024); $i++; } $headers = explode($csv_delimiter2, $text); // Check for valid field names. foreach ($headers as $h) { $header[] = trim($h); $patternized = implode('|', $patterns) . "\\d+"; $metapattern = implode('|', $metas); if (!(isset($required[$h]) or isset($optionalDefaults[$h]) or isset($optional[$h]) or preg_match("/{$patternized}/", $h) or preg_match("/{$metapattern}/", $h))) { cli_error(get_string('invalidfieldname', 'error', $h)); return; } if (isset($required[trim($h)])) { $required[trim($h)] = 0; } } // Check for required fields. foreach ($required as $key => $value) { if ($value) { // Required field missing. cli_error(get_string('fieldrequired', 'error', $key)); return; } } $linenum = 2; // Since header is line 1. // Take some from admin profile, other fixed by hardcoded defaults. while (!feof($fp)) { // Make a new base record. $vnode = new StdClass(); foreach ($optionalDefaults as $key => $value) { $vnode->{$key} = $value; } // Commas within a field should be encoded as , (for comma separated csv files). // Semicolon within a field should be encoded as ; (for semicolon separated csv files). $text = fgets($fp, 1024); if (vmoodle_is_empty_line_or_format($text, false)) { $i++; continue; } $valueset = explode($csv_delimiter2, $text); $f = 0; foreach ($valueset as $value) { // Decode encoded commas. $key = $headers[$f]; $vnode->{$key} = preg_replace($csv_encode, $csv_delimiter2, trim($value)); $f++; } $vnodes[] = $vnode; } return $vnodes; }
function moodle_request_shutdown() { global $CFG; // initially, we are only ever called under apache // but check just in case if (function_exists('apache_child_terminate') && function_exists('memory_get_usage') && ini_get_bool('child_terminate')) { if (empty($CFG->apachemaxmem)) { $CFG->apachemaxmem = 25000000; // default 25MiB } if (memory_get_usage() > (int) $CFG->apachemaxmem) { trigger_error('Mem usage over $CFG->apachemaxmem: marking child for reaping.'); @apache_child_terminate(); } } if (defined('MDL_PERF') || (!empty($CFG->perfdebug) and $CFG->perfdebug > 7)) { if (defined('MDL_PERFTOLOG')) { $perf = get_performance_info(); error_log("PERF: " . $perf['txt']); } } }
/** * Actually process the uploaded CSV organization file upon successful upload. * * NOTE: A lot o code here is borrowed / modified from Moodle. * * @see Moodle:/admin/uploaduser.php * * @uses $CURMAN * @param array $fieldata A PHP upload file array (i.e. from the $_FILES superglobal). * @param bool $update Flag for updating existing records. * @param bool $verbose Flag for verbose output. * @return string Output for display. */ function process_input_data($filedata, $update = false, $verbose = false) { global $CURMAN; $output = ''; /// Don't check for a valid mime/type as this is causing errors for the client. /* if (!in_array($filedata['type'], $this->valid_mimetypes)) { return 'The file format uploaded was incorrect'; } */ if ($filedata['size'] === 0) { return get_string('uploaded_empty_file', 'block_curr_admin'); } /** * Large files are likely to take their time and memory. Let PHP know * that we'll take longer, and that the process should be recycled soon * to free up memory. */ @set_time_limit(0); @cm_raise_memory_limit('192M'); if (function_exists('apache_child_terminate')) { @apache_child_terminate(); } $csv_encode = '/\\&\\#44/'; $csv_delimiter = "\\,"; $csv_delimiter2 = ","; $data = ''; $file = @fopen($filedata['tmp_name'], 'rb'); if ($file) { while (!feof($file)) { $data .= fread($file, 1024); } fclose($file); } if (empty($data)) { return get_string('no_data_file', 'block_curr_admin'); } /** * Removes the BOM from unicode string - see http://unicode.org/faq/utf_bom.html * * Borrowed from Moodle code - /lib/textlib.class.php */ $bom = ""; if (strpos($data, $bom) === 0) { $data = substr($data, strlen($bom)); } /** * Fix Mac/DOS newlines * * Borrowed from Moodle code - /admin/uploaduser.php */ $data = preg_replace('!\\r\\n?!', "\n", $data); $fp = fopen($filedata['tmp_name'], 'w'); fwrite($fp, $data); fclose($fp); $fp = fopen($filedata['tmp_name'], 'r'); /** * The required and optional fields we're looking for in the CSV file. */ $required = array('studentid' => 1, 'class' => 1, 'trainernum' => 1, 'startdate' => 1, 'enddate' => 1); $optional = array('firstname' => 1, 'lastname' => 1, 'curriculum' => 1, 'status' => 1, 'completed' => 1, 'grade' => 1, 'frequency' => 1, 'timeperiod' => 1); $colpos = array(); $header = split($csv_delimiter, fgets($fp, 1024)); // Check for valid field names foreach ($header as $i => $h) { $h = trim($h); $header[$i] = $h; // remove whitespace $h = ereg_replace('^\\"|\\"$', '', $h); // strip encapsulating quotes $header[$i] = $h; if (isset($required[$h])) { $required[$h] = 0; $colpos[$i] = $h; } else { if (isset($optional[$h])) { $colpos[$i] = $h; } } } /// Check for required fields foreach ($required as $key => $value) { if ($value) { //required field missing return get_string('missing_required_field', 'block_curr_admin', $key); } } $linenum = 2; // since header is line 1 $stusnew = 0; $stuserror = 0; $stusupdated = 0; $timenow = time(); while (!feof($fp)) { //Note: commas within a field should be encoded as , (for comma separated csv files) //Note: semicolon within a field should be encoded as ; (for semicolon separated csv files) $line = split($csv_delimiter, fgets($fp, 1024)); foreach ($line as $key => $value) { if (isset($colpos[$key])) { /// decode encoded commas and strip enapsulating quotes $record[$colpos[$key]] = preg_replace($csv_encode, $csv_delimiter2, trim($value)); $record[$colpos[$key]] = ereg_replace('^\\"|\\"$', '', $record[$colpos[$key]]); } } /// Got organization data if ($record[$header[0]]) { $done = false; $users = $CURMAN->db->get_records(USRTABLE, 'idnumber', $record['studentid']); $user = NULL; /// Don't worry about the actual type. Just worry about the idnumber. if (!empty($users)) { $user = current($users); } /// Only proceed if this student and instructor users actually exists. if (!empty($user->id)) { $crsidnumber = $record['class']; $dateparts = explode('/', $record['startdate']); $startdate = mktime(0, 0, 0, $dateparts[1], $dateparts[0], $dateparts[2]); $dateparts = explode('/', $record['enddate']); $enddate = mktime(0, 0, 0, $dateparts[1], $dateparts[0], $dateparts[2]); /// Check if the class as specified exists... $clsidnumber = $record['class']; if (!($class = $CURMAN->db->get_record(CLSTABLE, 'idnumber', $clsidnumber))) { $clsidnumber = $record['class'] . '-' . $record['trainernum']; /// Need to check for old classes that didn't have dates, and remove them. if ($class = $CURMAN->db->get_record(CLSTABLE, 'idnumber', $clsidnumber)) { $class = new cmclass($class); $class->delete(); } /// If the class doesn't exist, we have to create it first. $datepart = date('Ymd', $startdate); $clsidnumber = $record['class'] . '-' . $record['trainernum'] . '-' . $datepart; $class = $CURMAN->db->get_record(CLSTABLE, 'idnumber', $clsidnumber); } if (empty($class->id) || $update && !empty($class->id)) { if ($course = $CURMAN->db->get_record(CRSTABLE, 'idnumber', $crsidnumber)) { /// Do we need to add / update curriculum info for this course??? if (isset($record['curriculum'])) { if ($cur = $CURMAN->db->get_record(CURTABLE, 'idnumber', $record['curriculum'])) { $curcrs = $CURMAN->db->get_record(CURCRSTABLE, 'curriculumid', $cur->id, 'courseid', $course->id); if (!$update && empty($curcrs->id) || $update && !empty($curcrs->id)) { $cmcrec = array('curriculumid' => $cur->id, 'courseid' => $course->id); if (!empty($record['frequency'])) { $cmcrec['frequency'] = $record['frequency']; } if (!empty($record['timeperiod'])) { $cmcrec['timeperiod'] = $record['timeperiod']; } if (empty($curcrs->id)) { $curcrs = new curriculum($cmcrec); } else { $curcrs = new curriculum($curcrs->id); foreach ($cmcrec as $key => $val) { $curcrs->{$key} = $val; } } $a = new object(); $a->courseid = $course->idnumber; $a->coursename = $course->name; $a->curid = $cur->idnumber; if ($update && !empty($curcrs->id)) { if ($curcrs->data_update_record() && $verbose) { $output .= get_string('updated_curriculum_course_info', 'block_curr_admin'); } } else { if ($curcrs->data_insert_record() && $verbose) { $output .= get_string('added_curriculum_course_info', 'block_curr_admin'); } } } } } $clsrec = array('courseid' => $course->id, 'idnumber' => $clsidnumber, 'startdate' => $startdate, 'enddate' => $enddate); if (empty($class->id)) { $class = new cmclass($clsrec); } else { $class = new cmclass($class->id); foreach ($clsrec as $key => $val) { $class->{$key} = $val; } } if ($update && !empty($class->id)) { if ($class->data_update_record() && $verbose) { $output .= get_string('updated_class_info', 'block_curr_admin') . $class->idnumber . '<br /><br />' . "\n"; } } else { if ($class->data_insert_record() && $verbose) { $output .= get_string('added_class_info', 'block_curr_admin') . $class->idnumber . '<br /><br />' . "\n"; } } if (empty($class->id) && $verbose) { $output .= get_string('error_class_not_created', 'block_curr_admin') . $class->idnumber . '<br /><br />' . "\n"; } } else { if ($verbose) { $output .= get_string('error_course_not_found', 'block_curr_admin') . $crsidnumber . '<br /><br />' . "\n"; } } } /// Only proceed if we have an actual class here... if (!empty($class->id)) { $instructors = $CURMAN->db->get_records(USRTABLE, 'idnumber', $record['trainernum']); $instructor = NULL; /// Don't worry about the actual type. Just worry about the idnumber. if (!empty($instructors)) { $instructor = current($instructors); } if (!empty($instructor->id) && !$CURMAN->db->record_exists(INSTABLE, 'classid', $class->id, 'userid', $instructor->id)) { $insrec = array('classid' => $class->id, 'userid' => $instructor->id); $newins = new instructor($insrec); if ($newins->data_insert_record() && $verbose) { $output .= get_string('added_instructor_class', 'block_curr_admin', cm_fullname($instructor)) . $class->idnumber . '<br /><br />' . "\n"; } } $student = $CURMAN->db->get_record(STUTABLE, 'classid', $class->id, 'userid', $user->id); $a = new object(); $a->name = cm_fullname($user); $a->id = $class->idnumber; if (!$update && empty($student->id) || $update && !empty($student->id)) { $sturec = array('classid' => $class->id, 'userid' => $user->id); if (isset($record['status'])) { $sturec['completestatusid'] = intval($record['status']); } if (!isset($record['completed'])) { $sturec['completetime'] = $enddate; } else { $d = explode('/', $record['completed']); if (count($d) == 3) { $day = $d[0]; $month = $d[1]; $year = $d[2]; $timestamp = mktime(0, 0, 0, $month, $day, $year); $sturec['completetime'] = $timestamp; } } if (isset($record['grade'])) { $sturec['grade'] = intval($record['grade']); } if (empty($student->id)) { $student = new student($sturec); } else { $student = new student($student->id); foreach ($sturec as $key => $val) { $student->{$key} = $val; } } if ($update && !empty($student->id)) { if ($student->data_update_record() && $verbose) { $output .= get_string('update_enrolment_info', 'block_curr_admin', $a) . '<br /><br />' . "\n"; } } else { if ($student->data_insert_record() && $verbose) { $output .= get_string('add_enrolment_info', 'block_curr_admin', $a) . '<br /><br />' . "\n"; } } } else { $student = NULL; if ($verbose) { $output .= get_string('existing_enrolment_info', 'block_curr_admin') . '<br /><br />' . "\n"; } } if (!empty($student->id)) { $done = true; } } } else { $output .= get_string('error_studentid_not_found', 'block_curr_admin') . $record['studentid'] . '<br /><br />' . "\n"; } if ($update && $done) { $stusupdated++; } else { if (!$update && $done) { $stusnew++; } else { $stuserror++; } } } } if (!empty($output)) { $output .= '<br /><br />'; } if (!$stusnew && !$stusupdated && !$stuserror) { $output .= get_string('nothing_done', 'block_curr_admin'); } if ($stusnew > 0) { $output .= get_string('added_new_students', 'block_curr_admin', $stusnew); } if ($stusupdated > 0) { $output .= get_string('updated_existing_students', 'block_curr_admin', $stusupdated); } if ($stuserror > 0) { $output .= get_string('error_not_processed', 'block_curr_admin', $stuserror); } return $output; }