Zend - The PHP Company




Calendars

Add Code


cal  

Type: code fragment
Added by: h3
Entered: 07/03/2004
Last modified: 03/12/2003
Rating: - (fewer than 3 votes)
Views: 4701
this is a tiny calendar parser, it uses the linux cal command and parse it to html, added some security fixes and layout bugs got fixed too.


<?php
// +-----------------------------------------------------------------------+
// | Copyright �2003-2004  h3                    Http://h3.valleyfield.net |
// | All   rights   reserved.                    E  x  p a  n  d  the  web |
// |                                                                       |
// | o Name  : cal                                                         |
// | o Client  : open source release                                       |
// | o Version : 1.1                                                       |
// |                                                                       |
// |   Requires                                                            |
// | o Linux server with "cal" command available                           |
// | o Apache with backquotes enabled (safe mode off)                      |
// |                                                                       |
// |  revisions;                                                           |
// | o Fixed date("y",time) to date("Y",time) bug                          |
// | o Fixed arbitrary url command vulnerability                           |
// | o Fixed current day highlight bug                                     |
// | o Improved layout and readability                                     |
// | o Added languages support                                             |
// |                                                                       |
// | THIS SOFTWARE IS PROVIDED BY THE  COPYRIGHT HOLDERS  AND CONTRIBUTORS |
// | "AS IS" AND  ANY EXPRESS  OR IMPLIED  WARRANTIES, INCLUDING,  BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE  COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES (INCLUDING,  BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,  OR  TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF  SUCH DAMAGE. |
// |                                                                       |
// +-----------------------------------------------------------------------+
// | Author: h3 <h3@valleyfield.net , h3@mindkind.org>                     |
// +-----------------------------------------------------------------------+

# Language settings
# days shortnames: FR    ENG
$cal['DAYS'][0] = "di"# su
$cal['DAYS'][1] = "lu"# mo
$cal['DAYS'][2] = "ma"# tu
$cal['DAYS'][3] = "me"# we
$cal['DAYS'][4] = "je"# th
$cal['DAYS'][5] = "ve"# fr
$cal['DAYS'][6] = "sa"# sa
# months shortnames: FR      ENG
$cal['MONTHS'][0] = "jan";  # jan
$cal['MONTHS'][1] = "fev";  # feb
$cal['MONTHS'][2] = "mar";  # mar
$cal['MONTHS'][3] = "avr";  # apr
$cal['MONTHS'][4] = "mai";  # may
$cal['MONTHS'][5] = "jun";  # jun
$cal['MONTHS'][6] = "jui";  # jul
$cal['MONTHS'][7] = "aou";  # aug
$cal['MONTHS'][8] = "sep";  # sep
$cal['MONTHS'][9] = "oct";  # oct
$cal['MONTHS'][10] = "nov"# nov
$cal['MONTHS'][11] = "dec"# dec
?>
<STYLE>
#TB-CAL {border-left: 1px solid #666;border-right: 1px solid #666;border-bottom: 1px solid #666;font: normal 10px verdana, arial;background: #F1F1F1;}
#TD-CAL-DATE {border-left: 1px solid #666;border-right: 1px solid #666;border-top: 1px solid #666;background: #F1F1F1;font: bold 12px verdana, arial;-background: #3677A9;text-align: left;color:#FFF;padding: 2px;padding-left: 4px;}
#TD-CAL-DAY {background: #FFF;padding: 2px;}
#TD-CAL-CURRENTDAY {background: #C9DCF8;font-weight:bold;padding: 2px;}
#TD-CAL-BLANK {background: #E1E1E1;}
#TD-CAL-DAYS {font: bold 10px verdana, arial;color: #FFF;background:#3677A9;border-bottom:1px solid #1B4E75;text-align:center;}
#TD-CAL-MENUBAR {border-top: 1px solid #CCC;padding: 2px;text-align: right;}
input, select {height: 19px;font: normal 10px #666;border: 1px solid #666;}
</STYLE>
<?
function create_table($in,$days) {
    if(
is_array($in)) {
        
$out "<TABLE id="TB-CAL"><TR>";
        while(
current($days)) {
            
$out .= "<TD id="TD-CAL-DAYS">"current($days) ."</TD>";
            
next($days);
        }
        
$out .= "</TR>n";
        foreach(
$in as $k => $wk) {
            
$out .= "<TR>";
            if(
$k==&& count($wk)<7) {
                
$out .= str_repeat("<TD id="TD-CAL-BLANK"></TD>",(7-count($wk)));
            }
            foreach(
$wk as $d) {
                if(
$d==date("d",time()) && (!isset($_GET['month']) || $_GET['month']==date("m",time()))) {
                    
$css "TD-CAL-CURRENTDAY";
                } else {
                    
$css "TD-CAL-DAY";
                }
                
$out .= "<TD id="". $css ."" align="center" valign="middle">".$d."</TD>";
            }
            if(
$k>=&& count($wk)<7) {
                
$out .= str_repeat("<TD id="TD-CAL-BLANK"></TD>",(7-count($wk)));
            }
            
$out .= "</TR>n";
        }
        return 
$out;
    }
}
if(isset(
$_GET['year']) || isset($_GET['month'])) {
    if(!isset(
$_GET['year']) || !is_numeric($_GET['year']) || strlen($_GET['year'])>4) {
        
$year date("Y",time());
    } else {
        
$year $_GET['year'];
    }
    if(!isset(
$_GET['month']) || !is_numeric($_GET['month']) || strlen($_GET['month'])>2) {
        
$month date("m",time());
    } else {
        
$month $_GET['month'];
    }
    
$cmd "cal "$month ." "$year;
} else {
    
$cmd "cal";
}
$out = `$cmd`;
$rows explode("n",$out);
$week = array();
# splitting weeks appart
for($x=0;$x<count($rows);$x++) {
    if(
$x==0) {
        
$cal['DATE'] = trim($rows[$x]);
    }
    if(
$x!=&& $x!=1) {
        
$week[$x] = explode(" ",$rows[$x]);
    }
}
# get rid of empty values
for($x=0;$x<count($week);$x++) {
    for(
$y=0;$y<count($week[$x]);$y++) {
        
$tmp $week[$x][$y];
        if(!empty(
$tmp)) {
            
$weeks[$x][] = $tmp;
        }
    }
}
?>
<TABLE cellspacing="0" cellpadding="0"><TR><TD id="TD-CAL-DATE"><? echo $cal['DATE']; ?></TD></TR>
<TR><TD><? echo create_table($weeks,$cal['DAYS']); ?></TD></TR>
<TR><TD colspan="7" id="TD-CAL-MENUBAR"><P><FORM name="FRM-CAL-NAV" action="" method="get">
<SELECT name="month">
<?php
for($x=0;$x<count($cal['MONTHS']);$x++) {
    if((!isset(
$_GET['month']) && date("m",time())==($x+1)) || (isset($_GET['month']) && $_GET['month']==($x+1))) {
        echo 
"<OPTION value="". ($x+1) ."" SELECTED>"$cal['MONTHS'][$x] ."</OPTION>n";
    } else {
        echo 
"<OPTION value="". ($x+1) ."">"$cal['MONTHS'][$x] ."</OPTION>n";
    }
}
?>
</SELECT> <INPUT type="text" name="year" value="<? echo (isset($_GET['year'])) ? $_GET['year'] : date("Y",time()); ?>" style="width:30px;" maxlenght="4" />
<INPUT type="submit" value="go" /></FORM></P></TD></TR></TABLE>


Usage Example


runs stand alone, but it's easy to put it in a variable with output buffering.


Rate This Script





Search



This Category All Categories