home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / calendar-2010-08-23.tar.gz / calendar-2010-08-23.tar / calendar / functions / admin_functions.php next >
PHP Script  |  2003-10-12  |  8KB  |  306 lines

  1. <?php
  2. // Is the user logged in
  3. //
  4. // returns boolean is the user logged in
  5. function is_loggedin () {
  6.     global $HTTP_SESSION_VARS;
  7.     if (!isset($HTTP_SESSION_VARS['phpical_loggedin']) || $HTTP_SESSION_VARS['phpical_loggedin'] == FALSE) {
  8.         return FALSE;
  9.     }
  10.     else
  11.         return TRUE;
  12. }
  13.  
  14. // Attempt to login. If login is valid, set the session variable 'phpical_loggedin' to TRUE and store the username and password in the session
  15. //
  16. // arg0: string username
  17. // arg1: string password
  18. // returns boolean was the login successful
  19. function login ($username, $password) {
  20.     global $HTTP_SESSION_VARS;
  21.     global $auth_method;
  22.     
  23.     switch ($auth_method) {
  24.         case 'ftp':
  25.             $loggedin = login_ftp($username, $password);
  26.             break;
  27.         case 'internal':
  28.             $loggedin = login_internal($username, $password);
  29.             break;
  30.         default:
  31.             $loggedin = FALSE;
  32.     }
  33.     
  34.     $HTTP_SESSION_VARS['phpical_loggedin'] = $loggedin;
  35.     if ($loggedin) {
  36.         $HTTP_SESSION_VARS['phpical_username'] = $username;
  37.         $HTTP_SESSION_VARS['phpical_password'] = $password;
  38.     }
  39.     
  40.     return $loggedin;
  41. }
  42.  
  43.  
  44. // Attempt to login to the ftp server
  45. //
  46. // arg0: string username
  47. // arg1: string password
  48. // returns boolean was login successful
  49. function login_ftp ($username, $password) {
  50.     global $ftp_server;
  51.     
  52.     // set up basic connection
  53.     $conn_id = @ftp_connect($ftp_server); 
  54.     
  55.     // login with username and password
  56.     $login_result = @ftp_login($conn_id, $username, $password); 
  57.     
  58.     // check connection
  59.     if ((!$conn_id) || (!$login_result)) { 
  60.         return FALSE;
  61.     }
  62.     
  63.     // close the FTP stream 
  64.     @ftp_close($conn_id);
  65.     
  66.     return TRUE;
  67. }
  68.  
  69. // Attempt to login using username and password defined in config.inc.php
  70. //
  71. // arg0: string username
  72. // arg1: string password
  73. // returns boolean was login successful
  74. function login_internal ($username, $password) {
  75.     global $auth_internal_username;
  76.     global $auth_internal_password;
  77.     
  78.     if ($auth_internal_username == $username && $auth_internal_password == $password)
  79.         return TRUE;
  80.     else
  81.         return FALSE;
  82. }
  83.  
  84. // Delete a calendar. If using ftp for authentication, use ftp to delete. Otherwise, use file system functions.
  85. //
  86. // arg0: string calendar file - not the full path
  87. // returns boolean was delete successful
  88. function delete_cal ($filename) {
  89.     global $HTTP_SESSION_VARS;
  90.     global $auth_method;
  91.     global $ftp_server;
  92.     global $calendar_path;
  93.     global $ftp_calendar_path;
  94.     
  95.     if ($auth_method == 'ftp') {
  96.         $filename = get_ftp_calendar_path() . "/" . $filename;
  97.         
  98.         // set up basic connection
  99.         $conn_id = @ftp_connect($ftp_server); 
  100.         
  101.         // login with username and password
  102.         $login_result = @ftp_login($conn_id, $HTTP_SESSION_VARS['phpical_username'], $HTTP_SESSION_VARS['phpical_password']); 
  103.         
  104.         // check connection
  105.         if ((!$conn_id) || (!$login_result))
  106.             return FALSE;
  107.         
  108.         // delete the file
  109.         $delete = @ftp_delete($conn_id, $filename); 
  110.         
  111.         // check delete status
  112.         if (!$delete)
  113.             return FALSE;
  114.         
  115.         // close the FTP stream 
  116.         @ftp_close($conn_id);
  117.         
  118.         return TRUE;
  119.     }
  120.     else {
  121.         $filename = $calendar_path . "/" . $filename;
  122.     
  123.         $delete = @unlink($filename); 
  124.         clearstatcache();
  125.         if (@file_exists($filename)) { 
  126.             $filesys = eregi_replace("/","\\", $filename); 
  127.             $delete = @system("del $filesys");
  128.             clearstatcache();
  129.             if (@file_exists($filename)) { 
  130.                 $delete = @chmod ($filename, 0775); 
  131.                 $delete = @unlink($filename); 
  132.                 $delete = @system("del $filesys");
  133.             }
  134.         }
  135.         clearstatcache();
  136.         if (@file_exists($filename)) {
  137.             return FALSE;
  138.         }
  139.         else {
  140.             return TRUE;
  141.         }
  142.         
  143.         return TRUE;
  144.     }
  145. }
  146.  
  147. // Copy the uploaded calendar. If using ftp for authentication, use ftp to copy. Otherwise, use file system functions.
  148. //
  149. // arg0: string full path to calendar file
  150. // arg1: string destination filename
  151. // returns boolean was copy successful
  152. function copy_cal ($source, $destination) {
  153.     global $HTTP_SESSION_VARS;
  154.     global $auth_method;
  155.     global $ftp_server;
  156.     global $calendar_path;
  157.     
  158.     if ($auth_method == 'ftp') {
  159.         $destination = get_ftp_calendar_path() . "/" . basename($destination);
  160.         $destination = str_replace ("\\", "/", realpath($destination));
  161.         
  162.         // set up basic connection
  163.         $conn_id = ftp_connect($ftp_server); 
  164.         
  165.         // login with username and password
  166.         $login_result = ftp_login($conn_id, $HTTP_SESSION_VARS['phpical_username'], $HTTP_SESSION_VARS['phpical_password']); 
  167.         
  168.         // check connection
  169.         if ((!$conn_id) || (!$login_result))
  170.             return FALSE;
  171.         
  172.         // upload the file
  173.         $upload = ftp_put($conn_id, $destination, $source, FTP_ASCII); 
  174.         
  175.         // check upload status
  176.         if (!$upload)
  177.             return FALSE;
  178.         
  179.         // close the FTP stream 
  180.         ftp_close($conn_id);
  181.         
  182.         return TRUE;
  183.     }
  184.     else {
  185.         $destination = $calendar_path . "/" . basename($destination);
  186.         
  187.         if (check_php_version('4.0.3')) {
  188.             return move_uploaded_file($source, $destination);
  189.         }
  190.         else {
  191.             return copy($source, $destination);
  192.         }
  193.     }
  194. }
  195.  
  196. // Find the full path to the caledar directory for use with ftp
  197. //  if $ftp_calendar_path == '', sends back the full path to the $calendar_path - this may not work depending 
  198. //  on ftp server config, but would be a best guess
  199. //
  200. // return string path to calendar directory for ftp operations
  201. function get_ftp_calendar_path() {
  202.     global $ftp_calendar_path;
  203.     global $calendar_path;
  204.     
  205.     if ($ftp_calendar_path != '')
  206.         return $ftp_calendar_path;
  207.     else {
  208.         return str_replace ("\\", "/", realpath($calendar_path));
  209.     }
  210. }
  211.  
  212. // Check to see if the current version of php is >= to the arguement
  213. //
  214. // arg0: string version of php to check against
  215. // return boolean true if $version is >= current php version
  216. function check_php_version($version) {
  217.     // intval used for version like "4.0.4pl1"
  218.     $testVer=intval(str_replace(".", "",$version));
  219.     $curVer=intval(str_replace(".", "",phpversion()));
  220.     if( $curVer < $testVer )
  221.         return FALSE;
  222.     return TRUE;
  223. }
  224.  
  225. // Is the file uploaded truly a file via HTTP POST - used to thwart a user from trying to trick the script from working on other files
  226. //
  227. // arg0: string filename
  228. // returns boolean is the uploaded a file
  229. function is_uploaded_file_v4 ($filename) {
  230.     if (!$tmp_file = get_cfg_var('upload_tmp_dir')) {
  231.         $tmp_file = dirname(tempnam('', ''));
  232.     }
  233.     $tmp_file .= '/' . basename($filename);
  234.     // For Windows compat
  235.     $filename = str_replace ("\\", "/", $filename);
  236.     $tmp_file = str_replace ("\\", "/", $tmp_file);
  237.     // User might have trailing slash in php.ini... 
  238.     return (ereg_replace('/+', '/', $tmp_file) == $filename);
  239. }
  240.  
  241. // return the appropriate error message if the file upload had an error
  242. //
  243. // arg0: array error number from $HTTP_POST_FILES[file]['error']
  244. // returns string error message
  245. function get_upload_error ($upload_error) {
  246.     global $php_error_lang;
  247.     global $upload_error_lang;
  248.     global $upload_error_gen_lang;
  249.     
  250.     if (isset($upload_error)) {
  251.         // This is only available in PHP >= 4.2.0
  252.         $error = $php_error_lang . " ";
  253.         switch($upload_error) {
  254.             case 0: //no error; possible file attack!
  255.             case 1: //uploaded file exceeds the upload_max_filesize directive in php.ini
  256.             case 2: //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form
  257.             case 3: //uploaded file was only partially uploaded
  258.             case 4: //no file was uploaded
  259.                 $error = $error . $upload_error . ": " . $upload_error_lang[$upload_error];
  260.                 break;
  261.             default: //a default error, just in case!  :)
  262.                 $error = $error . $upload_error . ": " . $upload_error_gen_lang;
  263.                 break;
  264.         }
  265.     }
  266.     else {
  267.         $error = $upload_error_gen_lang;
  268.     }
  269.     
  270.     return $error;
  271. }
  272.  
  273. // Check to see that the file has an .ics extension
  274. //
  275. // arg0: string filename
  276. // returns booloean does the filename end in .ics
  277. function is_uploaded_ics ($filename) {
  278.     // Check the file extension for .ics. Can also check the the mime type, but it's not reliable so why bother...
  279.     if(preg_match("/.ics$/i", $filename)) {
  280.         return TRUE;
  281.     }
  282.     else {
  283.         return FALSE;
  284.     }
  285. }
  286.  
  287. // Get all calendar filenames (not including path)
  288. //
  289. // argo: string path to calendar files
  290. // returns array filenames (not including path)
  291. function get_calendar_files($calendar_path) {
  292.     global $error_path_lang;
  293.     
  294.     $dir_handle = @opendir($calendar_path) or die(error(sprintf($error_path_lang, $calendar_path)));
  295.     $filelist = array();
  296.     while ($file = readdir($dir_handle)) {
  297.         if (preg_match("/^[^.].+\.ics$/", $file)) {
  298.             array_push($filelist, $file);
  299.         }
  300.     }
  301.     closedir($dir_handle);
  302.     natcasesort($filelist);
  303.     return $filelist;
  304. }
  305.  
  306. ?>