Temos Archyvai: informacinės technologijos

Apie linux ir kitas operacines sistemas, techninę ir programinę įrangą

PHP Function to Validate Lithuanian Personal Identification Number

Below is a simple function for PHP to check if a number is a valid Lithuanian personal identification number. As I made it for Codeigniter 4, it is included in a class (that can be easily integrated into CI4 framework), but if you want, you can use it as a standalone function.

By default it does not consider valid the numbers with future dates (as people at the time of validation cannot have such numbers). But it might be modified easily to allow for such theoretical numbers.

To check an ID outside the Codeigniter 4 validation context, you would do something like this:

$check = new CustomRules();
$error = '';
 
$code = '36709010185';
echo $check->valid_lt_id($code, $error) ? 'ok' : $error;
// prints "Lithuanian Personal Identification Number is not valid: 
// wrong control number"
$error = '';
$code = '36709010186';
echo $check->valid_lt_id($code, $error) ? 'ok' : $error;
// prints "ok"

Toliau skaityti PHP Function to Validate Lithuanian Personal Identification Number

PHP function to check if a date is a holiday in Lithuania

Here is a simple function for PHP to check if a date, given in standard ISO format (Y-m-d) is a public holiday in Lithuania. I needed one for a project, so here it is for everyone:

 
/**
 * Checks if a passed date is a public holiday 
 * (or weekend) in Lithuania
 * 
 * @param bool $weekends - true if you want the function to include weekends
 *    default - false
 * @param string $date - 'Y-m-d' format date (or full ISO 8601 datetime string)
 *    defaults to current date
 * @return bool true (if holiday) or false (not a holiday)
*/
 
function is_lt_holiday(bool $weekends = false, string $date = null): int
{
    //date is today if it was not passed to the function
    if (!isset($date)) $date = date('Y-m-d');
 
    //check if the date is valid
    if (
         strlen($date) < 10 
      || !checkdate(substr($date,5,2), substr($date,8,2), substr($date,0,4))
    ) 
    {
        trigger_error('You should pass a valid date in \'Y-m-d\' format');
        return false;
    } 
 
    $year = substr($date,0,4);
    $mday = substr($date,5,5);
    $easter = date('Y-m-d',easter_date($year));
    $easter2 = date('Y-m-d', strtotime($easter . ' +1 day'));
    $public_holidays = [
        '01-01', //new year
        '02-16', //independence day
        '03-11', //restoration of independence day
        substr($easter,5,5), //easter day1
        substr($easter2,5,5), //easter day2
        '05-01', //int worker's day
        '06-24', //summer solstice
        '07-06', //state day
        '08-15', //mary's assumption
        '11-01', //all saints day
        '11-02', //day of the dead
        '12-24', //christmas eve
        '12-25', // christmas day1
        '12-26'  //christmas day2
       ];
 
    if (in_array($mday,$public_holidays)) {
        return true;
    }
    elseif (
       $weekends == true 
       && (date('D',strtotime($date)) == 'Sat' 
       || date('D',strtotime($date)) == 'Sun')
    ) 
    {
        return true;
    }
    return false;
}
 
 
// and now let's test it:
 
echo is_lt_holiday(true);
//today; it depends :)
echo is_lt_holiday(false,'2022-01-01');
//returns true
echo is_lt_holiday(false,'2022-01-02');
//returns false
echo is_lt_holiday(false,'2022-01-32');
//returns false and error notice - an impossible date

Downloading Manga Comics Using a Bash Script

My teenage daughter got immersed into manga, that is, Japanese comics books. That is not something that I would enjoy doing myself (never was much of a fan of comics books), but since she asked me to download the comics for offline reading, it became a father-and-daughter thing for us. So I decided to show her some Linux command line magic. After some tinkering I wrote a bash script that can download manga books from many popular manga sites to read offline, on an ebook reader. Toliau skaityti Downloading Manga Comics Using a Bash Script

Kaip nusipirkau suklastotą planšetę (ir kaip pavyko jos atsikratyti)

Prieš tris savaites pirkau dukrai planšetinį kompiuterį. Deja, pataikiau ant klastotės. Ypatingai buvo apmaudu ir nesmagu, nes kas jau kas, o aš turėjau suprasti iškart, kad su tuo kompiuteriu kažkas ne taip. Tai, kad to nesupratau laiku, kainavo man dvi savaites apmaudo.

Štai skelbimas „Išpardavimas Tik 149€ 64gb 4gb RAM 10 branduolių“, kuriame reklamuotą kompiuterį pirkau:

Skelbime nurodyti tokie pagrindiniai planšetės parametrai:

Linux audio: individualizuotas sprendimas

Su garso problemomis nuolat tenka susidurti savo prižiūrimuose Linux kompiuteriuose. Pirmą kartą čia apie tai rašiau dar 2010 metais Tai garso nėra, tai jis siunčiamas ne į tą įrenginį, į kurį norėčiau, tai nustatomas ne tas audio profilis… Šių problemų esmė – labai bendro pobūdžio, konkrečiai aparatinei įrangai nepritaikyti Linux garso posistemės Pulse Audio sprendimai. (Tiesa, ir prie Windows garso galima gerokai pavargti, tačiau šį kartą kalba ne apie Windows).

Linux, ir konkrečiau – mano naudojama darbastalio aplinka KDE, turi gerus audio tvarkymo įrankius, tačiau juose susigaudyti ne specialistui – ne taip paprasta. Štai, pvz., pasirinkite – kuris audio profilis jums reikalingas šiame paveikslėlyje?

Iš viso didžiulio sąrašo man tenka naudoti viso labo du profilius (dažniausiai – „Analoginė dvipusė stereo“, o prijungus išorinį televizorių – „Digital Stereo (HDMI) išvestis + Analoginė stereo įsvestis), tačiau tai teko išmokti ilgu bandymų ir klaidų keliu. Toliau skaityti Linux audio: individualizuotas sprendimas

Codeigniter form with integrated data and file validation

In one application built on Codeigniter framework I needed to create a special form. The form had to allow a user to both enter text data and upload an image. It was actually the first time I had any experience with programming forms that would include file upload. And I found out it is not a completely trivial task in Codeigniter. There are multiple reasons for this, but the main one is this: image validation and form data validation are done separately in Codeigniter. Not only the validation is done by two different classes, but the error messages are also independent.

So I went on to create a form that would integrate both types of validation (or rather, the error messages). On the way I had to decide on one major usability problem: what to do with image if the user submits the right image but the rest of the form does not validate (for example, forgets to enter name or email). I decided to go with the simplest option – the image is discarded, and the user is warned to not forget to attach the image again (since the file upload field cannot be repopulated, as other fields can be).

Admittedly, the image could be saved to save user the trouble of attaching it again, but in that case you have to solve another problem – what to do with the images that the user (a malicious user) submits and subsequently fails to submit the properly filled in form. I know, something could be done at the browser level using Javascript validation, but the server-side validation is still necessary… Toliau skaityti Codeigniter form with integrated data and file validation