read and save ini text files.
Config_Lite has the native PHP function
`parse_ini_file' under the hood.
The API is inspired by Python's ConfigParser.
A "Config_Lite" file consists of
"name = value" entries and sections,
"[section]"
followed by "name = value" entries
function parse_ini() { $config = new Config_Lite('config.ini.php'); $iconVal = array('fa fa-television', 'fa fa-download', 'fa fa-server', 'fa fa-play-circle', 'fa fa-tint', 'fa fa-globe', 'glyphicon glyphicon-calendar', 'glyphicon glyphicon-dashboard', 'glyphicon glyphicon-bullhorn', 'glyphicon glyphicon-search', 'glyphicon glyphicon-headphones'); $pageOutput = "<form>"; $pageOutput .= "<div class='applicationContainer'>General:<br>Title: <input type='text' class='general-value' name='general-title' value='" . $config->get('general', 'title') . "'>"; $pageOutput .= "<div>Enable Dropdown: <input class='general-value' name='general-enabledropdown' type='checkbox' "; if ($config->get('general', 'enabledropdown') == true) { $pageOutput .= "checked></div></div><br><br>"; } else { $pageOutput .= "></div></div><br>"; } $pageOutput .= "<input type='hidden' class='settings-value' name='settings-enabled' value='true'>" . "<input type='hidden' class='settings-value' name='settings-default' value='false'>" . "<input type='hidden' class='settings-value' name='settings-name' value='Settings'>" . "<input type='hidden' class='settings-value' name='settings-url' value='settings.php'>" . "<input type='hidden' class='settings-value' name='settings-landingpage' value='false'>" . "<input type='hidden' class='settings-value' name='settings-icon' value='fa fa-server'>" . "<input type='hidden' class='settings-value' name='settings-dd' value='true'>"; $pageOutput .= "<div id='sortable'>"; foreach ($config as $section => $name) { if (is_array($name) && $section != "settings" && $section != "general") { $pageOutput .= "<div class='applicationContainer' id='" . $section . "'>\n <span class='example_icon'></span>"; foreach ($name as $key => $val) { if ($key == "url") { $pageOutput .= "<div>{$key}:<input class='" . $section . "-value' name='" . $section . "-" . $key . "' type='text' value='" . $val . "'></div>"; } else { if ($key == "name") { $pageOutput .= "<div>{$key}:<input class='appName " . $section . "-value' was='" . $section . "' name='" . $section . "-" . $key . "' type='text' value='" . $val . "'></div>"; } else { if ($key == "icon") { $pageOutput .= "<div>{$key}:<select class='iconDD " . $section . "-value' name='" . $section . "-" . $key . "'>"; foreach ($iconVal as $icon) { $pageOutput .= "<option value='" . $icon . "'" . ($val == $icon ? " selected>" : ">") . explode(' ', $icon)[1] . "</option>"; } $pageOutput .= "</select></div>"; } elseif ($key == "default") { $pageOutput .= "<div>{$key}:<input type='radio' class='radio " . $section . "-value' name='" . $section . "-" . $key . "'"; if ($val == "true") { $pageOutput .= " checked></div>"; } else { $pageOutput .= "></div>"; } } else { $pageOutput .= "<div>{$key}:<input class='checkbox " . $section . "-value' name='" . $section . "-" . $key . "' type='checkbox' "; if ($val == "true") { $pageOutput .= " checked></div>"; } else { $pageOutput .= "></div>"; } } } } } $pageOutput .= "<input type='button' class='removeButton' value='Remove' id='remove-" . $section . "'></div>"; //Put this back to the left when ajax is ready -- <input type='button' class='saveButton' value='Save' id='save-" . $section . "'> } } $pageOutput .= "</div><div class='center' id='addApplicationButton'>\n <input type='button' id='addApplication' value='Add New Application'></div>\n <div id='saved'>Saved!</div><div id='removed' class='hidden'></div></form>"; return $pageOutput; }
function landingPage($keyname) { $config = new Config_Lite(CONFIG); $item = "\r\n <html lang=\"en\">\r\n <head>\r\n <title>" . $config->get($keyname, 'name') . "</title>\r\n <link rel=\"stylesheet\" href=\"css/landing.css\">\r\n </head>\r\n <body>\r\n <div class=\"login\">\r\n <div class=\"heading\">\r\n <h2><span class=\"fa " . $config->get($keyname, 'icon') . " fa-3x\"></span></h2>\r\n <section>\r\n <a href=\"" . $config->get($keyname, 'url') . "\" target=\"_self\" title=\"Launch " . $config->get($keyname, 'name') . "!\"><button class=\"float\">Launch " . $config->get($keyname, 'name') . "</button></a>\r\n </section>\r\n </div>\r\n </div>\r\n </body></html>"; if (empty($item)) { $item = ''; } return $item; }
</div> <div class="modal-footer"> <button type='button' class="btn btn-primary" data-dismiss="modal">Okay</button> </div> </div> </div> <div id="updateContainer"></div> <script src="js/jquery-2.2.0.min.js"></script> <script src="js/jquery-ui.min.js"></script> <script src="js/jquery.form.min.js"></script> <script src="js/bootstrap.min.js"></script> <script type="text/javascript" src="js/iconset-fontawesome-4.2.0.min.js"></script> <script type="text/javascript" src="js/bootstrap-iconpicker.min.js"></script> <script type="text/javascript" src="js/main.js"></script> <script type="text/javascript" src="js/functions.js"></script> <?php if ($upgrade) { echo "<script type=\"text/javascript\">\$('#upgradeModal').modal();</script>"; } $config = new Config_Lite(CONFIG); if ($config->get('general', 'updatepopup', 'false') == "true") { echo "<script type=\"text/javascript\">\$(document).ready(function (\$) { var updateCheck = setInterval(updateBox(),1000); })</script>"; } ?> <meta id='gitData'> <meta id='secret'> <meta id='branch'> </body> </html>
public function check() { $checkStart = (int) $this->_config->get("general", "check_start", 7); $checkEnd = (int) $this->_config->get("general", "check_end", 24); if ($checkStart > 23) { $checkStart = 0; } if ($checkEnd < 1) { $checkEnd = 24; } $hour = (int) date("G"); if ($hour < $checkStart || $hour >= $checkEnd) { $this->_logger->info("Hors de la plage horaire. Contrôle annulé."); return; } $this->_logger->info("Contrôle des alertes."); $this->_checkConnection(); $users = $this->_userStorage->fetchAll(); // génération d'URL court pour les SMS $curlTinyurl = curl_init(); curl_setopt($curlTinyurl, CURLOPT_RETURNTRANSFER, 1); $storageType = $this->_config->get("storage", "type", "files"); foreach ($users as $user) { if ($storageType == "db") { $storage = new \App\Storage\Db\Alert($this->_userStorage->getDbConnection(), $user); $this->_logger->info("User: "******"/var/configs/" . $user->getUsername() . ".csv"; if (!is_file($file)) { continue; } $storage = new \App\Storage\File\Alert($file); $this->_logger->debug("Fichier config: " . $file); } // configuration des notifications. $notifications = array(); $notifications_params = $user->getOption("notification"); if ($notifications_params && is_array($notifications_params)) { foreach ($notifications_params as $notification_name => $options) { if (!is_array($options)) { continue; } try { $notifications[$notification_name] = \Message\AdapterFactory::factory($notification_name, $options); $this->_logger->debug("notification " . get_class($notifications[$notification_name]) . " activée"); } catch (\Exception $e) { $this->_logger->warn("notification " . $notification_name . " invalide"); } } } $alerts = $storage->fetchAll(); $this->_logger->info(count($alerts) . " alerte" . (count($alerts) > 1 ? "s" : "") . " trouvée" . (count($alerts) > 1 ? "s" : "")); if (count($alerts) == 0) { continue; } $unique_ads = $user->getOption("unique_ads"); foreach ($alerts as $i => $alert) { $currentTime = time(); if (!isset($alert->time_updated)) { $alert->time_updated = 0; } if ((int) $alert->time_updated + (int) $alert->interval * 60 > $currentTime || $alert->suspend) { continue; } $this->_logger->info("Contrôle de l'alerte " . $alert->url); try { $parser = \AdService\ParserFactory::factory($alert->url); } catch (\AdService\Exception $e) { $this->_logger->info("\t" . $e->getMessage()); continue; } $this->_logger->debug("Dernière mise à jour : " . (!empty($alert->time_updated) ? date("d/m/Y H:i", (int) $alert->time_updated) : "inconnue")); $this->_logger->debug("Dernière annonce : " . (!empty($alert->time_last_ad) ? date("d/m/Y H:i", (int) $alert->time_last_ad) : "inconnue")); $alert->time_updated = $currentTime; if (!($content = $this->_httpClient->request($alert->url))) { $this->_logger->error("Curl Error : " . $this->_httpClient->getError()); continue; } $cities = array(); if ($alert->cities) { $cities = array_map("trim", explode("\n", mb_strtolower($alert->cities))); } $filter = new \AdService\Filter(array("price_min" => $alert->price_min, "price_max" => $alert->price_max, "cities" => $cities, "price_strict" => (bool) $alert->price_strict, "categories" => $alert->getCategories(), "min_id" => $unique_ads ? $alert->last_id : 0)); $ads = $parser->process($content, $filter, parse_url($alert->url, PHP_URL_SCHEME)); $countAds = count($ads); if ($countAds == 0) { $storage->save($alert); continue; } $siteConfig = \AdService\SiteConfigFactory::factory($alert->url); $newAds = array(); $time_last_ad = (int) $alert->time_last_ad; foreach ($ads as $ad) { if ($time_last_ad < $ad->getDate()) { $newAds[$ad->getId()] = (require DOCUMENT_ROOT . "/app/mail/views/mail-ad.phtml"); if ($alert->time_last_ad < $ad->getDate()) { $alert->time_last_ad = $ad->getDate(); } if ($unique_ads && $ad->getId() > $alert->last_id) { $alert->last_id = $ad->getId(); } } } if (!$newAds) { $storage->save($alert); continue; } $countAds = count($newAds); $this->_logger->info($countAds . " annonce" . ($countAds > 1 ? "s" : "") . " trouvée" . ($countAds > 1 ? "s" : "")); $this->_mailer->clearAddresses(); $error = false; if ($alert->send_mail) { try { $emails = explode(",", $alert->email); foreach ($emails as $email) { $this->_mailer->addAddress(trim($email)); } } catch (phpmailerException $e) { $this->_logger->warn($e->getMessage()); $error = true; } if (!$error) { if ($alert->group_ads) { $newAdsCount = count($newAds); $subject = "Alert " . $siteConfig->getOption("site_name") . " : " . $alert->title; $message = '<h2>' . $newAdsCount . ' nouvelle' . ($newAdsCount > 1 ? 's' : '') . ' annonce' . ($newAdsCount > 1 ? 's' : '') . ' - ' . date("d/m/Y H:i", $currentTime) . '</h2> <p>Lien de recherche: <a href="' . htmlspecialchars($alert->url, null, "UTF-8") . '">' . htmlspecialchars($alert->url, null, "UTF-8") . '</a></p> <hr /><br />' . implode("<br /><hr /><br />", $newAds) . '<hr /><br />'; $this->_mailer->Subject = $subject; $this->_mailer->Body = $message; try { $this->_mailer->send(); } catch (phpmailerException $e) { $this->_logger->warn($e->getMessage()); } } else { $newAds = array_reverse($newAds, true); foreach ($newAds as $id => $ad) { $subject = ($alert->title ? $alert->title . " : " : "") . $ads[$id]->getTitle(); $message = '<h2>Nouvelle annonce - ' . date("d/m/Y H:i", $currentTime) . '</h2> <p>Lien de recherche: <a href="' . htmlspecialchars($alert->url, null, "UTF-8") . '">' . htmlspecialchars($alert->url, null, "UTF-8") . '</a></p> <hr /><br />' . $ad . '<hr /><br />'; $this->_mailer->Subject = $subject; $this->_mailer->Body = $message; try { $this->_mailer->send(); } catch (phpmailerException $e) { $this->_logger->warn($e->getMessage()); } } } } } if ($notifications && ($alert->send_sms_free_mobile || $alert->send_sms_ovh || $alert->send_pushbullet || $alert->send_notifymyandroid || $alert->send_pushover)) { if ($countAds < 5) { // limite à 5 SMS foreach ($newAds as $id => $ad) { $ad = $ads[$id]; // récupère l'objet. $url = $ad->getLink(); if (false !== strpos($url, "leboncoin")) { $url = "http://mobile.leboncoin.fr/vi/" . $ad->getId() . ".htm"; } curl_setopt($curlTinyurl, CURLOPT_URL, "http://tinyurl.com/api-create.php?url=" . $url); if ($url = curl_exec($curlTinyurl)) { $msg = "Nouvelle annonce " . ($alert->title ? $alert->title . " : " : "") . $ad->getTitle(); $others = array(); if ($ad->getPrice()) { $others[] = number_format($ad->getPrice(), 0, ',', ' ') . $ad->getCurrency(); } if ($ad->getCity()) { $others[] = $ad->getCity(); } elseif ($ad->getCountry()) { $others[] = $ad->getCountry(); } if ($others) { $msg .= " (" . implode(", ", $others) . ")"; } $params = array("title" => "Alerte " . $siteConfig->getOption("site_name"), "description" => "Nouvelle annonce" . ($alert->title ? " pour : " . $alert->title : ""), "url" => $url); foreach ($notifications as $key => $notifier) { switch ($key) { case "freeMobile": $key_test = "send_sms_free_mobile"; break; case "ovh": $key_test = "send_sms_ovh"; break; default: $key_test = "send_" . $key; } if (isset($alert->{$key_test}) && $alert->{$key_test}) { try { $notifier->send($msg, $params); } catch (Exception $e) { $this->_logger->warn("Erreur sur envoi via " . get_class($notifier) . ": (" . $e->getCode() . ") " . $e->getMessage()); } } } } } } else { // envoi un msg global curl_setopt($curlTinyurl, CURLOPT_URL, "http://tinyurl.com/api-create.php?url=" . $alert->url); if ($url = curl_exec($curlTinyurl)) { $msg = "Il y a " . $countAds . " nouvelles annonces pour votre alerte '" . ($alert->title ? $alert->title : "sans nom") . "'"; $params = array("title" => "Alerte " . $siteConfig->getOption("site_name"), "description" => "Nouvelle" . ($countAds > 1 ? "s" : "") . " annonce" . ($countAds > 1 ? "s" : "") . ($alert->title ? " pour : " . $alert->title : ""), "url" => $url); foreach ($notifications as $key => $notifier) { switch ($key) { case "freeMobile": $key_test = "send_sms_free_mobile"; break; case "ovh": $key_test = "send_sms_ovh"; break; default: $key_test = "send_" . $key; } if (isset($alert->{$key_test}) && $alert->{$key_test}) { try { $notifier->send($msg, $params); } catch (Exception $e) { $this->_logger->warn("Erreur sur envoi via " . get_class($notifier) . ": (" . $e->getCode() . ") " . $e->getMessage()); } } } } } } $storage->save($alert); } } curl_close($curlTinyurl); $this->_mailer->smtpClose(); }
<?php require_once 'Config/Lite.php'; $config = new Config_Lite(); // read config file $config->read('phpmussel.ini'); // get value from config file var_dump($config->getBool('general', 'cleanup')); var_dump($config->getBool('general', 'disable_cli')); // set value in config file to false $config->set('general', 'cleanup', false); $config->set('general', 'disable_cli', true); // get current value (not yet saved to the config file) var_dump($config->getBool('general', 'cleanup')); var_dump($config->getBool('general', 'disable_cli')); // save new value to config file $config->save(); // read updated config file $config->read('phpmussel.ini'); // get saved value from config file var_dump($config->read('phpmussel.ini')->getBool('general', 'cleanup')); var_dump($config->read('phpmussel.ini')->getBool('general', 'disable_cli'));