Your browser must have JavaScript enabled in order to view this page.
 >  >
 
Welcome Guest#216 Login/register    Go to Bottom
Go to Top

Source for file dpfunctions.php

Documentation is available at dpfunctions.php

  1. <?php
  2. /**
  3.  * Common functions available to all objects in the universe
  4.  *
  5.  * DutchPIPE version 0.4; PHP version 5
  6.  *
  7.  * LICENSE: This source file is subject to version 1.0 of the DutchPIPE license.
  8.  * If you did not receive a copy of the DutchPIPE license, you can obtain one at
  9.  * http://dutchpipe.org/license/1_0.txt or by sending a note to
  10.  * license@dutchpipe.org, in which case you will be mailed a copy immediately.
  11.  *
  12.  * @package    DutchPIPE
  13.  * @subpackage lib
  14.  * @author     Lennert Stock <ls@dutchpipe.org>
  15.  * @copyright  2006, 2007 Lennert Stock
  16.  * @license    http://dutchpipe.org/license/1_0.txt  DutchPIPE License
  17.  * @version    Subversion: $Id: dpfunctions.php 311 2007-09-03 12:48:09Z ls $
  18.  * @link       http://dutchpipe.org/manual/package/DutchPIPE
  19.  * @see        dpuniverse.php, dptemplates.php
  20.  */
  21.  
  22. /**
  23.  * Gets the current user connected to the server
  24.  *
  25.  * If a user page or AJAX request caused the current chain of execution that
  26.  * caused this function to be called, that user object is returned. Otherwise
  27.  * FALSE is returned. For example, if the chain of execution if caused by a
  28.  * setTimeout, this will return FALSE.
  29.  *
  30.  * @return     object    Reference to user object of FALSE for no current user
  31.  */
  32. function &get_current_dpuser()
  33. {
  34.     global $grCurrentDpUniverse;
  35.  
  36.     return $grCurrentDpUniverse->getCurrentDpUser();
  37. }
  38.  
  39. /**
  40.  * Gets the current object performing an action
  41.  *
  42.  * This can also be a computer controlled character. If the chain of execution
  43.  * that caused this function to be called isn't the result of a user or npc
  44.  * performing an action, FALSE is returned.
  45.  *
  46.  * If the current object is a user performing an action, get_current_dpobject
  47.  * doesn't necessarily equal get_current_dpuser. Someone or something might
  48.  * force the user the perform an action.
  49.  *
  50.  * @return     object    Reference to user object of FALSE for no current user
  51.  */
  52. {
  53.     global $grCurrentDpObject;
  54.  
  55.     return $grCurrentDpObject;
  56. }
  57.  
  58. /**
  59.  * Gets the current universe handling the current user
  60.  *
  61.  * The universe object keeps track of all objects and provides methods such
  62.  * as newDpObject. Usually and currently there's just the one universe object.
  63.  * In the future there might be more, for instance to allow users to switch
  64.  * between different worlds without having to log in again.
  65.  *
  66.  * @return     object    Reference to user object of FALSE for no current user
  67.  */
  68. {
  69.     global $grCurrentDpUniverse;
  70.  
  71.     return $grCurrentDpUniverse;
  72. }
  73.  
  74. /**
  75.  * Initializes a new DutchPIPE property in an object
  76.  *
  77.  * Initializes a property which can be accessed in various ways.
  78.  *
  79.  * @param      mixed     $value      the initial value of the property if any
  80.  * @param      string    $setter     optional method to call when setting value
  81.  * @param      string    $getter     optional method to clal for retreival
  82.  * @return     array     A custom data structure, don't tamper with it
  83. */
  84. function new_dp_property($value NULL$setter NULL$getter NULL)
  85. {
  86.     return array('new_dp_property'$value$setter$getter);
  87. }
  88.  
  89. /**
  90.  * Includes universe object
  91.  *
  92.  * This is a wrapper around require_once. You can only inherit in
  93.  * DPUNIVERSE_PREFIX_PATH (dpuniverse/ in the standard distribution), which is
  94.  * the top directory for this function. Use the constants from
  95.  * config/dpuniverse.ini to form paths.
  96.  *
  97.  * Examples (from dpuniverse/page/login.php):
  98.  * inherit(DPUNIVERSE_STD_PATH . 'DpPage.php');
  99.  * inherit(DPUNIVERSE_INCLUDE_PATH . 'events.php');
  100.  *
  101.  * @param      string    $path       path to class file within dpuniverse/
  102.  */
  103. function inherit($path)
  104. {
  105.     /* PHP 5.1.5 kludge symlink bug fix */
  106.     $files get_included_files();
  107.     $pos dp_strrpos($path'/');
  108.     if (FALSE !== $pos && $pos != dp_strlen($path-1{
  109.         $file dp_substr($path$pos 1);
  110.     }
  111.     foreach ($files as $f{
  112.         $pos dp_strrpos($f'/');
  113.         if (FALSE !== $pos && $pos != dp_strlen($f-1{
  114.             if ($file === dp_substr($f$pos 1)) {
  115.                 return;
  116.             }
  117.         }
  118.     }
  119.  
  120.     require_once(DPUNIVERSE_PREFIX_PATH $path);
  121. }
  122.  
  123. /**
  124.  * Verifies that the contents of a variable can be called as a method
  125.  *
  126.  * Identical to the PHP {@link http://www.php.net/is_callable is_callable}
  127.  * method, except that it can be not be the name of a function stored in a
  128.  * string variable, it must be an object and the name of a method within the
  129.  * object, like this:
  130.  *
  131.  *     array($SomeObject, 'MethodName')
  132.  *
  133.  * @param      mixed     &$var        variable to check
  134.  * @return     boolean   TRUE if var is callable, FALSE otherwise
  135.  * @see        parse_dp_callable
  136.  */
  137. function is_dp_callable(&$var)
  138. {
  139.     return is_array($var&& is_callable($var);
  140. }
  141.  
  142. /**
  143.  * Replaces the contents of a callable variable
  144.  *
  145.  * Checks if the given variable can be called as a method with is_dp_callable.
  146.  * If it is, calls the method and replaces the variable with the result.
  147.  *
  148.  * @param      mixed     &$var        variable to check
  149.  * @see        is_dp_callable
  150.  */
  151. function parse_dp_callable(&$var)
  152. {
  153.     if (!is_dp_callable($var)) {
  154.         return;
  155.     }
  156.  
  157.     $nr_of_args func_num_args(1;
  158.     if (=== $nr_of_args{
  159.         $var $var[0]->{$var[1]}();
  160.         return;
  161.     }
  162.  
  163.     $args array_slice(func_get_args()1);
  164.     $var call_user_func_array(array($var[0]$var[1])$args);
  165. }
  166.  
  167. /**
  168.  * Reads entire file within the universe directory into a string
  169.  *
  170.  * Reads and returns the given file. The file should be within the directory
  171.  * defined by DPUNIVERSE_PATH, for example /page/foo.txt. When translations
  172.  * are enabled an alternative file can be created and used instead. For this to
  173.  * work, the file must be placed in the directory defined by DPSERVER_LOCALE in
  174.  * the directory defined by DPSERVER_GETTEXT_LOCALE_PATH, for example
  175.  * /home/dutchpipe/locale/nl_NL/page/foo.txt.
  176.  *
  177.  * @param      string    $path        path within the universe directory
  178.  * @return     mixed     string with file contents or FALSE on failure
  179.  * @see        DPUNIVERSE_PATH, DPSERVER_GETTEXT_ENABLED, DPSERVER_LOCALE,
  180.  8             DPSERVER_GETTEXT_LOCALE_PATH, dptext
  181.  */
  182. function dp_file_get_contents($path)
  183. {
  184.     return file_get_contents(
  185.         (DPSERVER_GETTEXT_ENABLED && DPSERVER_LOCALE != '0'
  186.         && file_exists(DPSERVER_GETTEXT_LOCALE_PATH DPSERVER_LOCALE $path)
  187.         : DPUNIVERSE_PREFIX_PATH)
  188.         . $path);
  189. }
  190.  
  191. /**
  192.  * Gets a 32-character random string
  193.  *
  194.  * @param      string    $length     optional different length <= 32
  195.  * @return     string    random characters
  196.  */
  197. function make_random_id($base 16)
  198. {
  199.     $id md5(uniqid(rand()true));
  200.     return 16 === $base $id base_convert($id16$base);
  201. }
  202.  
  203. /**
  204.  * Is the given integer or string a whole positive number?
  205.  *
  206.  * @param      mixed     $var        the integer of string to check
  207.  * @return     boolean   TRUE for a whole positive number, FALSE otherwise
  208.  */
  209. function is_whole_number($var)
  210. {
  211.    $var = (string)$var;
  212.  
  213.    for ($i 0$len dp_strlen($var)$i $len$i++{
  214.        if (($ascii_code ord($var[$i])) 48 || $ascii_code 57{
  215.            return FALSE;
  216.        }
  217.    }
  218.    return TRUE;
  219. }
  220.  
  221. /**
  222.  * Gets a short, descriptive string for an age in seconds
  223.  *
  224.  * This method is used for example to show the age of users. A new user could
  225.  * just be "1 minute and 15 seconds" old, while addicts could be hanging around
  226.  * for "2 years, 74 days and 20 hours".
  227.  *
  228.  * @param      int       $age        number of seconds
  229.  * @return     string    Short, descriptive age string
  230.  */
  231. function get_age2string($age)
  232. {
  233.     $rest_age$age;
  234.  
  235.     $rval array();
  236.     if ($age >= 31536000{
  237.         $tmp floor($age 31536000);
  238.         $rval[=== $tmp dp_text('1 year'dp_text('%d years'$tmp);
  239.         $rest_age $age 31536000;
  240.     }
  241.     if ($rest_age >= 86400{
  242.         $tmp floor($rest_age 86400);
  243.         $rval[=== $tmp dp_text('1 day'dp_text('%d days'$tmp);
  244.         $rest_age $rest_age 86400;
  245.     }
  246.     if ($rest_age >= 3600{
  247.         $tmp floor($rest_age 3600);
  248.         $rval[=== $tmp dp_text('1 hour'dp_text('%d hours'$tmp);
  249.         $rest_age $rest_age 3600;
  250.     }
  251.  
  252.     if ($age 31536000{
  253.         if ($rest_age >= 60{
  254.             $tmp floor($rest_age 60);
  255.             $rval[=== $tmp dp_text('1 minute')
  256.                 : dp_text('%d minutes'$tmp);
  257.             $rest_age $rest_age 60;
  258.         }
  259.         if ($age 86400{
  260.             $rval[=== $rest_age dp_text('1 second')
  261.                 : dp_text('%d seconds'$rest_age);
  262.         }
  263.     }
  264.  
  265.     if (=== ($sz count($rval))) {
  266.         return $rval[0];
  267.     }
  268.  
  269.     $last $rval[$sz 1];
  270.     $rval array_slice($rval0-1);
  271.  
  272.     return implode(', '$rval' ' dp_text('and'' ' $last;
  273. }
  274.  
  275. /**
  276.  * Copies and processes an uploaded image for a given object
  277.  *
  278.  * Copies an image for a given user with the given file path to a given
  279.  * destination. Resizes the image if the width in pixels exceeds
  280.  * DPSERVER_OBJECT_IMAGE_MAX_WIDTH and/or the height exceeds
  281.  * DPSERVER_OBJECT_IMAGE_MAX_HEIGHT. The image type must defined in
  282.  * DPSERVER_OBJECT_IMAGE_VALID_TYPES ("gif", "jpg" or "png" by default).
  283.  *
  284.  * @param      string    &$object   user who will use yhis avatar
  285.  * @param      string    $from      path to uploaded avatar image
  286.  * @param      string    $to        path of copied/resulting image
  287.  * @return     boolean   TRUE for success, error string for failure
  288.  * @see        DpUser::actionAvatar, DPSERVER_OBJECT_IMAGE_MAX_WIDTH,
  289.  *              DPSERVER_OBJECT_IMAGE_MAX_HEIGHT,
  290.  *              DPSERVER_OBJECT_IMAGE_VALID_TYPES
  291.  * @since      DutchPIPE 0.4.1
  292.  */
  293. function dp_upload_image(&$object$from$to)
  294. {
  295.     if (!DPUNIVERSE_AVATAR_CUSTOM_ENABLED || !function_exists('gd_info')) {
  296.         return dp_text("Image upload is not enabled.");
  297.     }
  298.  
  299.     $valid_types explode(','DPSERVER_OBJECT_IMAGE_VALID_TYPES);
  300.     if (!is_array($image_info getimagesize($from))) {
  301.         $err TRUE;
  302.     else {
  303.         $type image_type_to_extension($image_info[2]FALSE);
  304.  
  305.         if (!in_array($type$valid_types)
  306.                 && !($type === 'jpg' && in_array('jpeg'$valid_types))
  307.                 && !($type === 'jpeg' && in_array('jpg'$valid_types))) {
  308.             $err TRUE;
  309.         }
  310.     }
  311.     if (isset($err)) {
  312.         return sprintf(dp_text('Invalid file type. The type of image must be one of the following: %s.'),
  313.             implode(dp_text(', ')$valid_types));
  314.     }
  315.  
  316.     $copy_result copy($from$to);
  317.     @unlink($from);
  318.     if (TRUE !== $copy_result{
  319.         return dp_text("Failed to upload image.");
  320.     }
  321.  
  322.     list($width$height$image_info;
  323.  
  324.     // No resizing needed?
  325.     if ($width <= DPSERVER_OBJECT_IMAGE_MAX_WIDTH
  326.             && $height <= DPSERVER_OBJECT_IMAGE_MAX_HEIGHT{
  327.         $object->titleImgWidth $width;
  328.         $object->titleImgHeight $height;
  329.  
  330.         return TRUE;
  331.     }
  332.  
  333.     // Resize image: determine new dimensions first
  334.     $new_width $width;
  335.     $new_height $height;
  336.     if ($new_width DPSERVER_OBJECT_IMAGE_MAX_WIDTH{
  337.         $new_height round($new_height DPSERVER_OBJECT_IMAGE_MAX_WIDTH
  338.             / $new_width);
  339.         $new_width DPSERVER_OBJECT_IMAGE_MAX_WIDTH;
  340.     }
  341.     if ($new_height DPSERVER_OBJECT_IMAGE_MAX_HEIGHT{
  342.         $new_width round($new_width DPSERVER_OBJECT_IMAGE_MAX_HEIGHT
  343.             / $new_height);
  344.         $new_height DPSERVER_OBJECT_IMAGE_MAX_HEIGHT;
  345.     }
  346.  
  347.     // Resize image: resample with new dimensions
  348.     $image_p imagecreatetruecolor($new_width$new_height);
  349.     switch ($type{
  350.         case 'gif':
  351.             $image imagecreatefromgif($to);
  352.             $transparentIndex imagecolortransparent($image);
  353.             $trans_colors imagecolorsforindex($image$transparentIndex);
  354.             $trans_color imagecolorallocate($image$trans_colors['red'],
  355.                 $trans_colors['green']$trans_colors['blue']);
  356.             imagecolortransparent($image$trans_color);
  357.             imagecopyresampled($image_p$image0000$new_width,
  358.                 $new_height$width$height);
  359.             $trans_color imagecolorallocate($image_p,
  360.                 $trans_colors['red']$trans_colors['green'],
  361.                 $trans_colors['blue']);
  362.             imagecolortransparent($image_p$trans_color);
  363.             imagegif($image_p$to);
  364.             break;
  365.         case 'jpg':
  366.         case 'jpeg':
  367.             $image imagecreatefromjpeg($to);
  368.             imagecopyresampled($image_p$image0000$new_width,
  369.             $new_height$width$height);
  370.             $sharpenMatrix array(array(-1-1-1)array(-116-1),
  371.                 array(-1-1-1));
  372.             $divisor 8;
  373.             $offset 0;
  374.             imageconvolution($image_p$sharpenMatrix$divisor$offset);
  375.             imagejpeg($image_p$to90);
  376.             break;
  377.         case 'png':
  378.             $image imagecreatefrompng($to);
  379.             imageantialias($image_pTRUE);
  380.             imagealphablending($image_pFALSE);
  381.             imagesavealpha($image_pTRUE);
  382.             $transparent imagecolorallocatealpha($image_p255255255,
  383.                 127);
  384.             imagefilledrectangle($image_p00$new_width$new_height,
  385.                 $transparent);
  386.             imagecopyresampled($image_p$image0000$new_width,
  387.                 $new_height$width$height);
  388.             imagepng($image_p$to0);
  389.             break;
  390.         default:
  391.             unlink($to);
  392.             return dp_text("Failed to upload image because of a configuration error.");
  393.     }
  394.  
  395.     $object->titleImgWidth $new_width;
  396.     $object->titleImgHeight $new_height;
  397.  
  398.     return TRUE;
  399. }
  400. ?>

Documentation generated on Mon, 03 Sep 2007 22:19:19 +0200 by phpDocumentor 1.3.0RC6

Click me!
Guest#216
 
 
 
  Go to Top
 
 
Input Field OptionsClose Input Field Go to Top
 
Legal Notices | Copyright © 2006, 2007 Lennert Stock. All rights reserved. Last update: Mon Sep 03 2007, 21:50 CET.