public function run() { $needs_alert = false; $data = get_from_cache('events', 'SessionStart'); /* if the session didn't start we store the information; admin will * receive the alert only once. */ if ($data == NULL) { $data = array(); } $user = $this->ev->user->getAttribute('login'); if (isset($data[$user]) && $this->ev->ok) { unset($data[$user]); } if (!isset($data[$user]) && !$this->ev->ok) { $needs_alert = true; $data[$user] = false; } set_cache($data, 'events', 'SessionStart'); if ($needs_alert) { Logger::debug('main', 'SessionStartMail: sending alert'); $subject = sprintf(_('OVD Session alert: %s couldn\'t log in'), $user); if (isset($this->ev->error)) { $message = _("The following error happened:\n") . $this->ev->error; } else { $message = _('No error given'); } send_alert_mail($subject, $message); } return true; }
public function run() { $needs_alert = false; $data = get_from_cache('events', 'SqlFailure'); if ($data == NULL) { $data[$this->ev->host] = $this->ev->status; if ($this->ev->status < 0) { $needs_alert = true; } } else { if (isset($data[$this->ev->host]) && $data[$this->ev->host] != $this->ev->status) { $data[$this->ev->host] = $this->ev->status; $needs_alert = true; } } if ($needs_alert) { set_cache($data, 'events', 'SqlFailure'); Logger::debug('main', 'SqlFailureMail: sending alert'); if ($this->ev->status < 0) { $subject = sprintf(_('OVD Alert: MySQL %s is offline'), $this->ev->host); } else { $subject = sprintf(_('OVD End Alert: MySQL %s is up again'), $this->ev->host); } send_alert_mail($subject, ''); } return true; }
public function run() { Logger::debug('main', 'ServerStatusChangedTask::run'); $needs_cleanup = false; $data = get_from_cache('events', 'ServerStatusChanged'); if ($data == NULL) { $data[$this->ev->fqdn] = $this->ev->status; if ($this->ev->status != ServerStatusChanged::$ONLINE) { $needs_cleanup = true; } } else { if ($this->ev->status != ServerStatusChanged::$ONLINE) { $needs_cleanup = true; } } if ($needs_cleanup) { Logger::debug('main', 'ServerStatusChangedTask::run cleanup task for ' . $this->ev->fqdn); set_cache($data, 'events', 'ServerStatusChanged'); $tm = new Tasks_Manager(); $tm->load_from_server($this->ev->fqdn); foreach ($tm->tasks as $a_task) { $tm->remove($a_task->id); } } return true; }
public function run() { $needs_alert = false; $data = get_from_cache('events', 'ServerStatusChanged'); if ($data == NULL) { $data[$this->ev->fqdn] = $this->ev->status; if ($this->ev->status == ServerStatusChanged::$OFFLINE) { $needs_alert = true; } } else { if (isset($data[$this->ev->fqdn]) && $data[$this->ev->fqdn] != $this->ev->status) { $data[$this->ev->fqdn] = $this->ev->status; $needs_alert = true; } } if ($needs_alert) { Logger::debug('main', 'ServerStatusChangedMail: sending alert'); set_cache($data, 'events', 'ServerStatusChanged'); if ($this->ev->status == ServerStatusChanged::$OFFLINE) { $subject = sprintf(_('OVD Alert: %s is offline'), $this->ev->fqdn); } else { $subject = sprintf(_('OVD End Alert: %s is up again'), $this->ev->fqdn); } send_alert_mail($subject, ''); } return true; }
<?php $service_doc['lugar|place'] = array('en' => array('patron' => '/ubigeo/place/DEPARTMENT[/PROVINCE[/DISTRICT]]', 'description' => 'Searches for the ubigeo codes (reniec, inei) for the place given as ' . '/DEPARTMENT[/PROVINCE[/DISTRICT]] (the last 2 parameters are optional)'), 'es' => array('patron' => '/ubigeo/lugar/DEPARTAMENTO[/PROVINCIA[/DISTRITO]]', 'descripción' => 'Busca los códigos de ubigeo (reniec, inei) para el lugar indicado por ' . 'el /DEPARTAMENTO/PROVINCIA/DISTRITO (los últimos dos parámetros son opcionales)')); $flugar = function ($dpt, $prov = '', $dist = '') use($app, $db) { $key = strtoupper("{$dpt}/{$prov}/{$dist}"); $res = get_from_cache($key); if ($res === false) { $stm = $db->prepare('select * from ubigeo where nombreCompleto = :lugar'); $stm->bindValue(':lugar', $key, PDO::PARAM_STR); $stm->execute(); $res = $stm->fetchAll(); save_to_cache($key, $res); } if (count($res) === 0) { $app->getLog()->error('3:badlocation:' . $dpt . '/' . $prov . '/' . $dist); $res = array('error' => 3, 'msg' => 'no existe el lugar que ha indicado'); } echo json_encode(array($app->request()->getResourceUri() => $res)); }; $app->get('/ubigeo/lugar/:dpt(/:prov(/:dist))', $flugar)->name('lugar'); $app->get('/ubigeo/place/:dpt(/:prov(/:dist))', $flugar)->name('place');
<?php $service_doc['codigo|code'] = array('en' => array('pattern' => '/ubigeo/code/NNNNNN[/source] (N=0..9; source=reniec|inei|any)', 'description' => 'Searches for the place with ubigeo code NNNNNN, from the source requested. ' . 'Valid sources: reniec (default), inei, any'), 'es' => array('patron' => '/ubigeo/codigo/NNNNNN[/fuente] (N=0..9; fuente=reniec|inei|cualquiera)', 'descripción' => 'Busca el lugar con código de ubigeo NNNNNN, en la fuente indicada. ' . 'Fuentes posibles: reniec (usada por defecto), inei, cualquiera')); $fcode = function ($ucode, $source = 'reniec') use($app, $db) { if (in_array($source, array('reniec', 'inei', 'cualquiera', 'any'))) { $res = get_from_cache($ucode . $source); if ($res === false) { $sql = 'select * from ubigeo where '; switch ($source) { case 'reniec': $sql .= ' reniec = :codigo'; break; case 'inei': $sql .= ' inei = :codigo'; break; case 'cualquiera': case 'any': $sql .= ' inei = :codigo or reniec = :codigo'; break; } $stm = $db->prepare($sql); $stm->bindValue(':codigo', $ucode, PDO::PARAM_STR); $stm->execute(); $res = $stm->fetchAll(); save_to_cache($ucode . $source, $res); } if (empty($res)) { $app->getLog()->error('1:badcode:' . $ucode . ':' . $source); $res = array('error' => 1, 'msg' => 'no existe el código de ubigeo que ha indicado'); } } else {
public function show() { $id = $this->params['id']; $cache = true; if (isset($this->params['invalidate_cache'])) { $cache = $this->params['invalidate_cache'] != 1; } if ($cache && get_cache($id) && !(php_sapi_name() === 'cli')) { echo get_from_cache($id); return; } /* First, fill out top-level metadata, including the * table of contents. */ $m = new Mustache_Engine(array('partials_loader' => new Mustache_Loader_FilesystemLoader(implode(DIRECTORY_SEPARATOR, array(APP, 'views', 'findingaid'))))); $model = new FindingaidModel($this->params['id']); $options = array('panels' => array(), 'title' => fa_brevity($model->title())); $toc_entries_unsorted = array(); $toc_subentries = array(); foreach ($this->config->get('panels') as $stub) { $panel = $stub; $panel['heading_id'] = "fa-heading-{$panel['id']}"; $panel['body_id'] = "fa-body-{$panel['id']}"; $skip = true; if (array_key_exists('field', $panel)) { $data = $model->xpath("//{$panel['field']}"); foreach ($data as $datum) { if (strlen(trim($datum)) > 0) { $panel['single-field'] = trim($datum); $skip = false; break; } } } elseif (array_key_exists('fields', $panel)) { $panel['multi-field'] = array(); foreach ($panel['fields'] as $entry) { $data = $model->xpath("//{$entry['field']}"); $metadata = array(); foreach ($data as $datum) { if (strlen(trim($datum)) > 0) { $metadata[] = array('content' => trim($datum)); $skip = false; } } if (count($metadata) > 0) { $panel['multi-field'][] = array('field_id' => "fa-fields-{$panel['body_id']}-{$entry['id']}", 'label' => fa_brevity($entry['label']), 'entries' => $metadata); $skip = false; } if (array_key_exists('in_toc', $entry)) { if ($entry['in_toc']) { $toc_entry = array('label' => fa_brevity($entry['label']), 'id' => "fa-fields-{$panel['body_id']}-{$entry['id']}"); $toc_entries_unsorted[$entry['id']] = $toc_entry; } } } } else { $component_count = count($model->xpath('contents/c')); if ($component_count > 0) { $skip = false; $templates = array('container_list', 'component'); foreach ($templates as $template) { $this->templates[$template] = load_template("findingaid/{$template}"); } foreach ($model->xpath('contents/c') as $c) { $details = $this->render_component($m, $c); $panel['components'][] = array('component' => $details[0]); if ($details[1]['level'] === 'series') { $attributes = $c->attributes(); $toc_subentries[] = $details[1]['metadata']; } } $panel['contents_entries'] = array(); if (count($toc_subentries) > 0) { $panel['subentries'] = true; $panel['contents_entries'] = $toc_subentries; $toc_subentries = array(); } } } if ($skip) { continue; } $in_toc = false; if (array_key_exists('in_toc', $panel)) { $in_toc = $panel['in_toc']; if ($in_toc) { $toc_entry = array('label' => fa_brevity($panel['label']), 'id' => $panel['heading_id']); if (isset($panel['subentries'])) { $toc_entry['subentries'] = true; $toc_entry['contents_entries'] = $panel['contents_entries']; } $toc_entries_unsorted[$panel['id']] = $toc_entry; } } $options['panels'][] = $panel; } $toc_config = $this->config->get('toc'); $toc_entries = array(); foreach ($toc_config['entries'] as $entry) { if (array_key_exists($entry, $toc_entries_unsorted)) { $toc_entry = $toc_entries_unsorted[$entry]; $toc_entries[] = $toc_entry; } } $links = array(); foreach ($toc_config['links'] as $link) { if (array_key_exists('skip', $link)) { if ($link['skip']) { continue; } } if (array_key_exists('field', $link)) { $data = $model->xpath("//{$link['field']}"); $url = false; foreach ($data as $datum) { if (strlen(trim($datum)) > 0) { $url = trim($datum); break; } } $links[] = array('label' => $link['label'], 'url' => $url); } } $repository = $model->repository(); $requestable = $repository === 'University of Kentucky'; $toc_component = false; if ($requestable and $component_count == 0) { $toc_component = array('summary' => '', 'id' => 'fa-no-components-request', 'container_list' => fa_brevity($model->title()), 'volume' => '', 'container' => ''); } $toc_options = array('id' => "fa-{$toc_config['id']}", 'label' => fa_brevity($toc_config['label']), 'entries' => $toc_entries, 'links' => $links, 'requestable' => $requestable, 'toc_component' => $toc_component); $toc = $m->render(load_template('findingaid/toc'), $toc_options); $content = $m->render(load_template('findingaid/show'), $options); if ($requestable) { $requests_config = $this->config->get('requests'); $requests = $m->render(load_template('findingaid/requests'), array('id' => $requests_config['summary']['id'], 'label' => fa_brevity($requests_config['summary']['label']), 'list_id' => $requests_config['summary']['list_id'], 'title' => $this->cleanup($model->unittitle()), 'collection_id' => $model->id(), 'call_number' => $model->unitid(), 'item_date' => $model->unitdate(), 'item_url' => 'http://exploreuk.uky.edu/catalog/' . $model->id() . '/')); } else { $requests = ''; } $css_hrefs = array("css/bootstrap.min.css", "css/jquery-ui.min.css", "css/jquery.tagit.css", "css/extra.css", "css/footer.css", "css/rangy.css", "css/lity.min.css", "css/mediaelementplayer.min.css"); $css = array(); foreach ($css_hrefs as $href) { $css[] = array('href' => $href); } $layout = new Mustache_Engine(array('partials_loader' => new Mustache_Loader_FilesystemLoader(implode(DIRECTORY_SEPARATOR, array(APP, 'views', 'layouts'))))); $page = $layout->render(load_template('layouts/application'), array('content' => $content, 'toc' => $toc, 'requests' => $requests, 'css' => $css, 'js' => array(array('href' => 'js/app.js')), 'title' => $model->title(), 'requestable' => $requestable, 'repository' => $this->config->get_repo($repository))); set_cache($id, $page); if (php_sapi_name() === 'cli') { exit; } echo $page; }
<?php $service_doc['distritos|districts'] = array('en' => array('pattern' => '/ubigeo/districts/DEPARTMENT/PROVINCE', 'description' => 'Lists the ubigeo codes for all districts for a PROVINCE in a DEPARTMENT'), 'es' => array('patron' => '/ubigeo/distritos/DEPARTAMENTO/PROVINCIA', 'descripción' => 'Lista los códigos de ubigeo de los distritos en la PROVINCIA del DEPARTAMENTO')); $fdist = function ($dpt, $prov) use($app, $db) { $dpto = strtoupper($dpt); $prov = strtoupper($prov); $res = get_from_cache("{$dpto}/{$prov}/distritos"); if ($res === false) { $rows = $db->query("select reniec from ubigeo where nombreCompleto = '{$dpto}/{$prov}/'"); $dpres = $rows->fetchAll(); if (count($dpres) > 0) { $dpcode = $dpres[0]['reniec']; preg_match('/(\\d{4})\\d\\d/', $dpcode, $reg); $prefix = $reg[1]; $stmnt = $db->query("select * from ubigeo where reniec like '{$prefix}%' and reniec <> '{$dpcode}'"); $res = $stmnt->fetchAll(); save_to_cache("{$dpto}/{$prov}/distritos", $res); } else { $app->getLog()->error('6:badprov:' . $dpto . ':' . $prov); $res = array('error' => 6, 'msg' => 'no existe una provincia de departamento con nombre ' . $dpto . '/' . $prov); } } echo json_encode(array($app->request()->getResourceUri() => $res)); }; $app->get('/ubigeo/distritos/:dpt/:prov', $fdist)->name('distritos'); $app->get('/ubigeo/districts/:dpt/:prov', $fdist)->name('districts');
/** * Replace a location point into a human readable form with most effort. * This function requires MySQL connection to be active. * For example: * <ul> * <li>'start' => 'your starting point'. * <li>'xxx.xxx,yyy.yyy' => check in cache, or reverse geocode from google if miss * </ul> * @param string $location the original location constant */ function humanize_point($location) { global $global_mysqli_link; global $protokd_point_start, $protokd_point_finish; global $message_start, $message_finish; global $gmaps_geocode_url, $gmaps_server_key, $maximum_http_response_size; global $cache_geocoding; //location = start, return message start if ($location == $protokd_point_start) { return $message_start; } else { if ($location == $protokd_point_finish) { //location = finish, return message start return $message_finish; } else { //selain start atau finish //escape string location untuk dimasukkan ke query sql $location = mysqli_escape_string($global_mysqli_link, $location); //cek cache ada dengan key cache_geocoding,value location $cached_geocode = get_from_cache($cache_geocoding, $location); //kalo tidak null, return langsung if (!is_null($cached_geocode)) { return $cached_geocode; } else { //jika null cachenya // query from google. $full_url = "{$gmaps_geocode_url}?key={$gmaps_server_key}&latlng={$location}&sensor=false"; $result = file_get_contents($full_url, NULL, NULL, 0, $maximum_http_response_size + 1); if ($result == FALSE) { die_nice("There's an error while reading the geocoding response from {$full_url}."); } // TODO this checking doesn't seem to work. if (sizeof($result) > $maximum_http_response_size) { die_nice("Data returned from {$full_url} is greater than the maximum."); } $json_response = json_decode($result, true); if ($json_response == NULL) { die_nice("Unable to retrieve JSON response from Google geocoding service."); } //add sugestion place if ($json_response['status'] == 'OK') { $bestguess = $location; for ($i = 0; $i < count($json_response['results']); $i++) { foreach ($json_response['results'][0]['address_components'] as $component) { if (in_array('transit_station', $component['types']) || in_array('route', $component['types'])) { put_to_cache($cache_geocoding, $location, $component['long_name']); return $component['long_name']; } $bestguess = $component['long_name']; } } log_error("Warning: can't find street name, use best guess {$bestguess} for {$location}."); put_to_cache($cache_geocoding, $location, $bestguess); return $bestguess; } else { if ($json_response['status'] == 'ZERO_RESULTS') { // If not found, return the coordinate. log_error("Warning: can't find coordinate for {$location}."); return $location; } else { die_nice("Problem while geocoding from Google reverse geocoding: " . $result); } } } } } }
<?php $service_doc['provincias|provinces'] = array('en' => array('pattern' => '/ubigeo/provinces/DEPARTMENT', 'description' => 'Lists the ubigeo codes for all provinces in DEPARTMENT'), 'es' => array('patron' => '/ubigeo/provincias/DEPARTAMENTO', 'descripción' => 'Lista los códigos de ubigeo de las provincias en DEPARTAMENTO')); $fprovs = function ($dpt) use($app, $db) { $dpto = strtoupper($dpt); $res = get_from_cache($dpto . '/provincias'); if ($res === false) { $rows = $db->query("select reniec from ubigeo where nombreCompleto = '{$dpto}//'"); $dptores = $rows->fetchAll(); if (count($dptores) > 0) { $dptocode = $dptores[0]['reniec']; preg_match('/(\\d\\d)\\d{4}/', $dptocode, $reg); $prefix = $reg[1]; $stmt = $db->query("select * from ubigeo where reniec like '{$prefix}%00' and reniec <> '{$dptocode}'"); $res = $stmt->fetchAll(); save_to_cache($dpto . '/provincias', $res); } else { $app->getLog()->error('5:baddpto:' . $dpto); $res = array('error' => 5, 'msg' => 'no existe un departamento con nombre ' . $dpto); } } echo json_encode(array($app->request()->getResourceUri() => $res)); }; $app->get('/ubigeo/provincias/:dpt', $fprovs)->name('provincias'); $app->get('/ubigeo/provinces/:dpt', $fprovs)->name('provinces');
<?php $service_doc['departamentos|departments'] = array('en' => array('pattern' => '/ubigeo/departments', 'description' => 'Lists the ubigeo codes for all departments'), 'es' => array('patron' => '/ubigeo/departamentos', 'descripción' => 'Lista los códigos de ubigeo de todos los departamentos')); $fdepas = function () use($app, $db) { $res = get_from_cache('departamentos'); if ($res === false) { $stmt = $db->query("select * from ubigeo where nombreCompleto like '%//'"); $res = $stmt->fetchAll(); save_to_cache('departamentos', $res); } echo json_encode(array($app->request()->getResourceUri() => $res)); }; $app->get('/ubigeo/departamentos', $fdepas)->name('departamentos'); $app->get('/ubigeo/departments', $fdepas)->name('departments');
$db_name = 'shizqdb'; $db = mongofactory::getInstance($url, $db_name); $request_type = $_GET['type']; $appid = 'wx40c3a549a1f9df06'; $secret = '2b844fcf66c0e6bdfd608381f8f4c779'; if ($request_type === 'token') { $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $secret; $req = 'token'; } else { $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $_GET['token'] . '&type=jsapi'; $req = 'ticket'; } if (check_timeout($req)) { echo get_from_server($url, $req); } else { echo get_from_cache($req); } function check_timeout($req) { global $db; $time = time(); $res = $db->wechat->find(array('tag' => $req)); if ($res->hasNext()) { $res_info = $res->next(); $spend = $time - $res_info['save_time']; if ($spend > 7000) { // 过期 return true; } else { return false; }