<?php if (!defined('PmWiki')) exit();


/*

 ***** 
 *
 *  OnDownload - Activate recipes matching marks on file download
 *  OnSession - Activate recipes in just one line for pairs of session name=value
 *  OnAction - Activate recipes in just one line for one action or more
 *  OnCookie - Activate recipes in just one line for pairs of cookie name=value
 *  OnGroup - Activate recipes in just one line for one or more groups
 *  OnPage - Activate recipes in just one line for one or more page names
 *  OnMark - Activate recipes by matching mark(s) in a page 
 *  OnPost - Activate recipes in just one line for a _POST var=value pair(s)
 *  OnFile - Activate recipes in just one line for a _FILES var=value pair(s)
 *  OnGet - Activate recipes in just one line for a _GET var=value pair(s)
 *
 * Copyright 2018 Carlos A. Bonamigo <cabsec.pmwiki@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 ***** 

*/

$RecipeInfo['OnEvent']['Version'] = '20180221';

# OnGet('gato=botas')
# OnGet('weather=warm,sea=cold,sand=thin') ou
# if there is one match it returns true
function OnGet($g){

if($$GLOBALS['_GET'] == '') return false;
if(stripos($g,'=') === false) return false;

$dget = trim($g); # all get pairs
$aget = $GLOBALS['_GET']; # all get

  if(MultiPairEqual($dget,$aget)) return true;
  else return false;

}

# OnPost('gato=botas')
# OnPost('weather=warm,sea=cold,sand=thin') ou
# if there is one match it returns true
function OnPost($p){

if($GLOBALS['_POST'] == '') return false;
if(stripos($p,'=') === false) return false;

$dpst = trim($p); # all get pairs
$apst = $GLOBALS['_POST']; # all get

  if(MultiPairEqual($dpst,$apst)) return true;
  else return false;

}

# OnFile('')
# here you are limited to
# check itens inside userfile
# name, tmp_name, type, size, 
# error, 
function OnFile($f){
global $action;

if($action != 'upload') return false;
if(stripos($f,'=') === false) return false;
if($GLOBALS['_FILES']['uploadfile'] == '') return false;

$dfil = trim($f); # all get pairs
$afil = $GLOBALS['_FILES']['uploadfile']; # all get

var_export($afil);

  if(MultiPairEqual($dfil,$afil)) return true;
  else return false;

}

# OnDownload('.txt,.bin,rad.gif,virus.zip')
# checks for marks on $_REQUEST['upname']
# on download, more than one requires ','
# between elements
function OnDownload($f){
global $action;

if($action != 'download') return false;
if($f == '') return false;
if($GLOBALS['_GET']['uploadfile']) return false;

$dwnl = trim($f); # all get pairs
$awnl = $GLOBALS['_GET']['upname']; # all get

  if(MultiContained($dwnl,$awnl)) return true;
  else return false;

}

# You can include one or more actions
# if one action is found return true
function OnAction($a){
global $action;

if($a == '') return false;
if($action == '') return false;

$dac = trim($a); # defined action(s)
$act = trim($action); # called action

  if(MultiEqual($dac,$act)) return true;
  else return false;

}

# You can include one or more pages
# if one page is found return true
# use: if(OnPage('Home,Miin')) echo "found";
function OnPage($p){
global $pagename;

if($p == '' ) return false;

$pgnm = $pagename;
$dpag = trim($p);# defined group(s)
$cpag = PageVar($pgnm,'$Name');# called group

  if(MultiEqual($dgrp,$cgrp)) return true;
  else return false;

}

# You can include one or more groups
# if one group is found return true
# use: if(OnGroup('Site')) echo "Found!";
function OnGroup($g){
global $pagename;

if($g == '' ) return false;

$pgnm = $pagename;
$dgrp = $g; # defined group(s)
$cgrp = PageVar($pgnm,'$Group'); # called group

  if(MultiEqual($dgrp,$cgrp)) return true;
  else return false;

}

# You can include one or more marks 
# if one mark is found return true
# use: if(OnMark('loadFonts'))
#   include_once("cookbook/loadfonts.php");
function OnMark($m){
global $pagename;

if($m == '' ) return false;

$pgnm = $pagename;
$amrk = $OnMarkGetAuth;
$dmrk = trim($m); # defined mark(s)

# now we get page text
# respect new OnMarkGetAuth var
if (!PageExists($pgnm)) return false;
else $page = ReadPage($pgnm,READPAGE_CURRENT);

if($page['text'] == '') return false;
else{ PCache($pgnm,$page); $ptxt = $page['text']; }

if(MultiContained($dmrk,$ptxt)) return true;
else return false;

}

# OnCookie('gato=botas,miaowMix=PleaseDeliver')
# OnCookie('weather=warm,sea=cold,sand=thin') ou
# if there is one match it returns true
# if you need match specificity call it with
# just one option
function OnCookie($c){

if($GLOBALS['_COOKIE'] == '') return false;
if(stripos($c,'=') === false) return false;

$dcke = trim($c); # defined group(s)
$acke = $GLOBALS['_COOKIE']; # all cokkies

  if(MultiPairEqual($dcke,$acke)) return true;
  else return false;

}

# OnSession('gato=botas,miaowMix=PleaseDeliver')
# OnSession('weather=warm,sea=cold,sand=thin') ou
# if there is one match it returns true
function OnSession($s){

@session_start();
if($GLOBALS['_SESSION'] == '')
if(stripos($s,'=') === false) return false;


$dsss = trim($s); # defined group(s)
$asss = $GLOBALS['_SESSION']; # all session

  if(MultiPairEqual($dsss,$asss)) return true;
  else return false;

}

function MultiEqual($n,$c){

  if(stripos($n,',') === false){

    if($n == $c) return true;
    else return false;

  }elseif(stripos($n,',') !== false){

  $arr = explode(',',$n);

    if(is_array($arr)){

      foreach($arr as $v){
      # if one match happens
      # we will return true
        if($c == trim($v)) $found = 1;
      }

      if($found == 1) return true;
      else return false;

    }
  }
}

function MultiPairEqual($n,$h){

  if(stripos($n,',') === false){

    $arr = explode('=',$n);

    if(is_array($arr)){
      if($h[$arr[0]] == trim($arr[1])) $found = 1;
    }

  }elseif(stripos($n,',') !== false){

  $arr = explode(',',$n);

    if(is_array($arr)){
      foreach($arr as $e){
        $n = explode('=',$e);
        if($h[$n[0]] == $n[1]) $found = 1;
      }
    }
  }

  if($found == 1) return true;
  else return false;

}

function MultiContained($n,$h){

  if(strpos($n,',') === false){

    if(strpos($h,$n) !== false) return true;
    else return false;

  }elseif(stripos($n,',') !== false){

  $arr = explode(',',$n);

    if(is_array($arr)){

      foreach($arr as $v){ #echo $v."<br>";
        if(strpos($h,trim("$v")) !== false) $found = 1;
      }

      if($found == 1) return true;
      else return false;

    }
  }
}