Index: common/cache.php
===================================================================
--- common/cache.php	(revision 1177)
+++ common/cache.php	(working copy)
@@ -43,7 +43,7 @@ function disk_cache_has($cache_id, $date = null) {
 
         // Delete old stuff.
         if ($mtime === false || $mtime < $date) {
-            @unlink($fname);
+            @unlink($file_name);
             return false;
         } else {
             return true;
@@ -126,7 +126,8 @@ function disk_cache_purge() {
 
 //
 // Memory caching implementation depends on IA_MEM_CACHE_METHOD.
-// There are only four functions, which should be enough for just about everything.
+// There are only four functions, which should be enough for just about
+// everything.
 //
 // You can store arrays, ints, string, null but not booleans because they're
 // used by the mem_cache_get function. You could store booleans as 0/1 however.
@@ -141,12 +142,11 @@ function disk_cache_purge() {
 //      mem_cache_delete removes something from the cache, use this when the
 //      object is no longer valid.
 //
-//      mem_cache_purge deletes everything from the cache. Try to avoid calling it.
+//      mem_cache_purge deletes everything from the cache.
+//      Try to avoid calling it.
 //
 if (IA_MEM_CACHE_METHOD == 'none') {
-
-    // Fake cache implementation/
-
+    // Fake cache implementation
     function mem_cache_get($cache_id) {
         return false;
     }
@@ -156,17 +156,16 @@ if (IA_MEM_CACHE_METHOD == 'none') {
     }
 
     function mem_cache_delete($cache_id) {
+        return false;
     }
 
     function mem_cache_purge() {
+        return false;
     }
-
 } else if (IA_MEM_CACHE_METHOD == 'eaccelerator') {
-
     // eAccelerator SHM memory cache
-
     function mem_cache_get($cache_id) {
-        $res = eaccelerator_get($cache_id);
+        $res = @eaccelerator_get($cache_id);
         if ($res === null) {
             if (IA_LOG_MEM_CACHE) {
                 log_print("MEM CACHE: miss on $cache_id");
@@ -181,42 +180,37 @@ if (IA_MEM_CACHE_METHOD == 'none') {
     }
 
     function mem_cache_set($cache_id, $object, $ttl = IA_MEM_CACHE_EXPIRATION) {
-        log_assert($object !== 'false', "Can't cache false values");
-        eaccelerator_put($cache_id, serialize($object), $ttl);
-
+        log_assert($object !== false, "Can't cache false values");
+        if (!@eaccelerator_put($cache_id, serialize($object), $ttl)) {
+            log_warn('Error occurred while storing ' . $cache_id . ' in cache');
+        }
         if (IA_LOG_MEM_CACHE) {
             log_print("MEM CACHE: store $cache_id");
         }
-
         return $object;
     }
 
     function mem_cache_delete($cache_id) {
-        eaccelerator_rm($cache_id);
         if (IA_LOG_MEM_CACHE) {
             log_print("MEM CACHE: delete $cache_id");
         }
+        return @eaccelerator_rm($cache_id);
     }
 
-    // Purge the entire SHM cache.
-    // FIXME: This does not work
     function mem_cache_purge() {
         if (IA_LOG_MEM_CACHE) {
             log_print("MEM CACHE: purge");
         }
-        eaccelerator_gc();
+        return @eaccelerator_gc();
     }
-
 } else if (IA_MEM_CACHE_METHOD == 'memcached') {
-
-    // memcached cache implementation.
-
-    $_memcache = memcache_pconnect('localhost');
+    // Memcached cache implementation.
+    $_memcache = memcache_pconnect(IA_MEMCACHED_HOST, IA_MEMCACHED_PORT);
 
     function mem_cache_get($cache_id) {
         global $_memcache;
-        $res = memcache_get($_memcache, $cache_id);
-        if ($res === false) {
+        $res = @memcache_get($_memcache, $cache_id);
+        if ($res === false || $res === null) {
             if (IA_LOG_MEM_CACHE) {
                 log_print("MEM CACHE: miss on $cache_id");
             }
@@ -225,15 +219,16 @@ if (IA_MEM_CACHE_METHOD == 'none') {
             if (IA_LOG_MEM_CACHE) {
                 log_print("MEM CACHE: hit on $cache_id");
             }
-            return unserialize($res);
+            return $res;
         }
     }
 
     function mem_cache_set($cache_id, $object, $ttl = IA_MEM_CACHE_EXPIRATION) {
         global $_memcache;
-        log_assert($object !== 'false', "Can't cache false values");
-        memcache_set($_memcache, $cache_id, serialize($object), 0, $ttl);
-
+        log_assert($object !== false, "Can't cache false values");
+        if (!@memcache_set($_memcache, $cache_id, $object, 0, $ttl)) {
+            log_warn('Error occurred while storing ' . $cache_id . ' in cache');
+        }
         if (IA_LOG_MEM_CACHE) {
             log_print("MEM CACHE: store $cache_id");
         }
@@ -242,26 +237,23 @@ if (IA_MEM_CACHE_METHOD == 'none') {
 
     function mem_cache_delete($cache_id) {
         global $_memcache;
-        memcache_delete($_memcache, $cache_id);
-
         if (IA_LOG_MEM_CACHE) {
             log_print("MEM CACHE: delete $cache_id");
         }
+        return @memcache_delete($_memcache, $cache_id);
     }
 
     function mem_cache_purge() {
         global $_memcache;
-        memcache_flush($_memcache);
-
         if (IA_LOG_MEM_CACHE) {
             log_print("MEM CACHE: purge");
         }
+        return @memcache_flush($_memcache);
     }
-
 } else if (IA_MEM_CACHE_METHOD == 'apc') {
     // APC cache implementation
     function mem_cache_get($cache_id) {
-        $res = apc_fetch($cache_id);
+        $res = @apc_fetch($cache_id);
         if ($res === false) {
             if (IA_LOG_MEM_CACHE) {
                 log_print("MEM CACHE: miss on $cache_id");
@@ -276,9 +268,10 @@ if (IA_MEM_CACHE_METHOD == 'none') {
     }
 
     function mem_cache_set($cache_id, $object, $ttl = IA_MEM_CACHE_EXPIRATION) {
-        log_assert($object !== 'false', "Can't cache false values");
-        apc_store($cache_id, serialize($object), $ttl);
-
+        log_assert($object !== false, "Can't cache false values");
+        if (!@apc_store($cache_id, serialize($object), $ttl)) {
+            log_warn('Error occurred while storing ' . $cache_id . ' in cache');
+        }
         if (IA_LOG_MEM_CACHE) {
             log_print("MEM CACHE: store $cache_id");
         }
@@ -286,18 +279,16 @@ if (IA_MEM_CACHE_METHOD == 'none') {
     }
 
     function mem_cache_delete($cache_id) {
-        apc_delete($cache_id);
         if (IA_LOG_MEM_CACHE) {
             log_print("MEM CACHE: delete $cache_id");
         }
+        return @apc_delete($cache_id);
     }
 
     function mem_cache_purge() {
         if (IA_LOG_MEM_CACHE) {
             log_print("MEM CACHE: purge");
         }
-        apc_clear_cache();
+        return @apc_clear_cache();
     }
 }
-
-?>
Index: common/common.php
===================================================================
--- common/common.php	(revision 1177)
+++ common/common.php	(working copy)
@@ -90,19 +90,19 @@ define("IA_USER_DEFINED_ROUND_TASK_LIMIT", '25');
 // FIXME: Not fully tested!
 if (!function_exists('checkdnsrr')) {
     log_warn("Function checkdnsrr does not exist. ".
-             "Presuming Windows NT enviroment and reimplementing it.");
+             "Presuming Windows NT environment and reimplementing it.");
 
-    function checkdnsrr($hostName, $recType = "MX")
-    {
-        if (empty($hostName)) {
+    function checkdnsrr($host_name, $rec_type = "MX") {
+        if (empty($host_name)) {
             return false;
         }
 
-        exec("nslookup -type=$recType $hostName", $result);
+        $result = array();
+        exec("nslookup -type=$rec_type $host_name", $result);
         // Check each line to find the one that starts with the host name.
         // If it exists then the function succeeded.
         foreach ($result as $line) {
-            if (eregi("^$hostName" ,$line)) {
+            if (eregi("^$host_name", $line)) {
                 return true;
             }
         }
@@ -162,6 +162,7 @@ function is_page_name($page_name) {
  * @return array                returns an array containing the matched user
  */
 function get_page_user_name($page_name) {
+    $matches = array();
     preg_match("/^ ".
                 preg_quote(IA_USER_TEXTBLOCK_PREFIX, '/').
                 '('.IA_RE_USER_NAME.") (\/?.*) $/xi",
@@ -272,7 +273,8 @@ function is_tag($tag) {
 
 // Taggable objects
 function is_taggable($obj) {
-    return $obj == 'user' || $obj == 'task' || $obj == 'round' || $obj == 'textblock';
+    return $obj == 'user' || $obj == 'task' || $obj == 'round' ||
+           $obj == 'textblock';
 }
 
 /**
@@ -295,7 +297,8 @@ function create_function_cached($args, $code) {
         return create_function($args, $code);
     }
     static $_cache = array();
-    $key = str_replace('|', '<|>', $args) . '|' . str_replace('|', '<|>', $code);
+    $key = str_replace('|', '<|>', $args) . '|' .
+           str_replace('|', '<|>', $code);
     if (!array_key_exists($key, $_cache)) {
         $_cache[$key] = create_function($args, $code);
     }
@@ -304,8 +307,7 @@ function create_function_cached($args, $code) {
 
 // Checks system requirements.
 // This will fail early if something is missing.
-function check_requirements()
-{
+function check_requirements() {
     $extensions = get_loaded_extensions();
 
     if (version_compare(phpversion(), '5.0', '<')) {
@@ -324,9 +326,10 @@ function check_requirements()
         log_warn("mbstring extension missing. inline diff will not be enabled");
     }
     if (!function_exists("finfo_open")) {
-        log_warn("finfo_open missing, falling back to mime_content_type.");
+        log_warn('finfo_open missing, falling back to mime_content_type.');
         if (!function_exists("mime_content_type")) {
-            log_warn("mime_content_type missing, mime-types will default to application/octet-stream.");
+            log_warn('mime_content_type missing, mime-types will default to ' .
+                     'application/octet-stream.');
         }
     }
 
@@ -378,14 +381,14 @@ function is_connection_secure() {
     $https = getattr($_SERVER, 'HTTPS', null);
 
     if ($https == 'on' || $https == '1' ||
-            getattr($_SERVER, 'SERVER_PORT', 80) == 443) {
+        getattr($_SERVER, 'HTTP_X_FORWARDED_PROTO') === 'https') {
         return true;
     }
     return false;
 }
 
 /*
- *Return information about the remote IP address. Useful for logging.
+ * Return information about the remote IP address. Useful for logging.
  * This isn't necessarily just an IP address. It might contain proxy
  * information when available.
  *
@@ -404,6 +407,3 @@ function remote_ip_info() {
         return getattr($_SERVER, 'REMOTE_ADDR');
     }
 }
-
-
-?>
Index: common/db/task.php
===================================================================
--- common/db/task.php	(revision 1177)
+++ common/db/task.php	(working copy)
@@ -9,7 +9,8 @@ require_once(IA_ROOT_DIR . "common/db/round_task.php");
 function _task_cache_add($task) {
     if (!is_null($task)) {
         log_assert_valid(task_validate($task));
-        mem_cache_set("task-by-id:{$task['id']}", $task, IA_MEM_CACHE_TASK_EXPIRATION);
+        mem_cache_set("task-by-id:{$task['id']}", $task,
+                      IA_MEM_CACHE_TASK_EXPIRATION);
     }
     return $task;
 }
@@ -31,7 +32,8 @@ function task_get($task_id) {
                      db_escape($task_id));
 
     // This way nulls (missing tasks) get cached too.
-    return mem_cache_set("task-by-id:$task_id", db_fetch($query), IA_MEM_CACHE_TASK_EXPIRATION);
+    return mem_cache_set("task-by-id:$task_id", db_fetch($query),
+                         IA_MEM_CACHE_TASK_EXPIRATION);
 }
 
 // Create new task
@@ -46,9 +48,11 @@ function task_create($task, $task_params, $remote_ip_info = null) {
 
         // Copy templates.
         require_once(IA_ROOT_DIR . "common/textblock.php");
-        $replace = array("task_id" => $task['id'], "task_title" => ucfirst($task['id']));
+        $replace = array("task_id" => $task['id'],
+                         "task_title" => ucfirst($task['id']));
         textblock_copy_replace("template/newtask", $task['page_name'],
-                $replace, "task: {$task['id']}", $task['user_id'], $remote_ip_info);
+                               $replace, "task: {$task['id']}",
+                               $task['user_id'], $remote_ip_info);
 
         _task_cache_add($task);
     }
@@ -118,7 +122,8 @@ function task_delete($task) {
     }
 
     // Delete task
-    db_query("DELETE FROM `ia_task` WHERE `id` = '" . db_escape($task["id"]) . "'");
+    db_query("DELETE FROM `ia_task` WHERE `id` = '" .
+             db_escape($task["id"]) . "'");
 
     // Delete all task parameters
     task_update_parameters($task["id"], array());
@@ -156,7 +161,7 @@ function task_get_all() {
 }
 
 // Returns list of round ids that include this task
-function task_get_parent_rounds($task_id, $force_no_cache=false) {
+function task_get_parent_rounds($task_id, $force_no_cache = false) {
     log_assert(is_task_id($task_id));
     if (!$force_no_cache) {
         $result = mem_cache_get("task-rounds-by-id:$task_id");
@@ -188,7 +193,7 @@ function task_get_parent_rounds($task_id, $force_no_cache=false) {
 // $user_id can submit
 function task_get_submit_rounds($task_id, $user_id) {
     $rounds = task_get_parent_rounds($task_id);
-    foreach($rounds as $id => $round) {
+    foreach ($rounds as $id => $round) {
         $round = round_get($rounds[$id]);
         if (!security_query($user_id, "round-submit", $round)) {
             unset($rounds[$id]);
@@ -361,8 +366,8 @@ function task_get_user_score($task_id, $user_id, $round_id = 'arhiva') {
     }
 
     // Check the cache
-    if (($res = mem_cache_get("user-task-round:".$user_id."-".$task_id."-".$round_id)) != false) {
-        log_var_dump($res);
+    $cache_key = 'user-task-round:'.$user_id.'-'.$task_id.'-'.$round_id;
+    if (($res = mem_cache_get($cache_key)) != false) {
         return $res;
     }
 
@@ -375,7 +380,7 @@ function task_get_user_score($task_id, $user_id, $round_id = 'arhiva') {
     $score = getattr($res, "score", null);
 
     // Keep in cache
-    mem_cache_set("user-task-round:".$user_id."-".$task_id."-".$round_id, (int)$score);
+    mem_cache_set($cache_key, (int)$score);
 
     return $score;
 }
@@ -427,4 +432,3 @@ function task_user_update_submit_count($user_id, $round_id, $task_id) {
            db_quote($user_id), db_quote($round_id), db_quote($task_id));
     db_query($query);
 }
-?>
Index: config.php.sample
===================================================================
--- config.php.sample	(revision 1177)
+++ config.php.sample	(working copy)
@@ -66,7 +66,7 @@ define("IA_COOKIE_DOMAIN", null);
 
 // infoarena session lifetime
 // defaults to 5days
-define("IA_SESSION_LIFETIME_SECONDS", 5*24*3600);
+define("IA_SESSION_LIFETIME_SECONDS", 5 * 24 * 3600);
 
 // Fatal error mask.
 // These are the errors the scripts halts on.
@@ -125,14 +125,28 @@ define("IA_MEM_CACHE_METHOD", "none");
 // Default TTL for cache items.
 define("IA_MEM_CACHE_EXPIRATION", 3600);
 
-// Round TTL. Smaller because of evaluator.
-define("IA_MEM_CACHE_ROUND_EXPIRATION", 10);
+// TTL for tag caches.
+define("IA_MEM_CACHE_TAGS_EXPIRATION", 3600);
 
-// Task TTL. Smaller because of evaluator.
-define("IA_MEM_CACHE_TASK_EXPIRATION", 10);
+if (IA_MEM_CACHE_METHOD === "memcached") {
+    define('IA_MEMCACHED_HOST', '127.0.0.1');
+    define('IA_MEMCACHED_PORT', 11211);
 
-// TTL for tag caches.
-define("IA_MEM_CACHE_TAGS_EXPIRATION", 600);
+    // Round TTL.
+    define("IA_MEM_CACHE_ROUND_EXPIRATION", 3600);
+
+    // Task TTL.
+    define("IA_MEM_CACHE_TASK_EXPIRATION", 3600);
+} else {
+    // Smaller TTLs because evaluator doesn't have access to cache to
+    // invalidate old data on updates.
+
+    // Round TTL.
+    define("IA_MEM_CACHE_ROUND_EXPIRATION", 10);
+
+    // Task TTL.
+    define("IA_MEM_CACHE_TASK_EXPIRATION", 10);
+}
 
 // Logging options. Anything else in normal operation is a bug.
 // Filling error_log on a production machine sucks.
@@ -162,5 +176,3 @@ define("IA_FACEBOOK_APP_ID", '159779000715246');
 
 // Twitter Account
 define("IA_TWITTER_ACCOUNT", 'infoarena.ro');
-
-?>
Index: www/controllers/textblock.php
===================================================================
--- www/controllers/textblock.php	(revision 1177)
+++ www/controllers/textblock.php	(working copy)
@@ -17,7 +17,6 @@ function controller_textblock_view($page_name, $rev_num = null,
 
     // If the page is missing jump to the edit/create controller.
     if ($crpage) {
-        // FIXME: hack to properly display latest revision.
         // Checks if $rev_num is the latest.
         $rev_count = textblock_get_revision_count($page_name);
         if ($rev_num && $rev_num != $rev_count) {
@@ -103,12 +102,15 @@ function controller_textblock_diff($page_name) {
     // Get diffs.
     $diff_title = diff_inline(array($revfrom['title'], $revto['title']));
     $diff_content = diff_inline(array($revfrom['text'], $revto['text']));
-    $diff_security = diff_inline(array($revfrom['security'], $revto['security']));
-    $diff_forum = diff_string(array($revfrom['forum_topic'], $revto['forum_topic']));
+    $diff_security = diff_inline(array($revfrom['security'],
+                                       $revto['security']));
+    $diff_forum = diff_string(array($revfrom['forum_topic'],
+                                    $revto['forum_topic']));
 
     $view = array();
     $view['page_name'] = $page['name'];
-    $view['title'] = "Diferente pentru $page_name intre reviziile $revfrom_id si $revto_id";
+    $view['title'] = 'Diferente pentru ' . $page_name . ' intre reviziile ' .
+                     $revfrom_id . ' si ' . $revto_id;
     $view['rev_count'] = $rev_count;
     $view['revfrom_id'] = $revfrom_id;
     $view['revto_id'] = $revto_id;
@@ -180,7 +182,6 @@ function controller_textblock_history($page_name) {
     } else {
         $count = $options['display_entries'];
     }
-    //log_print($options['first_entry']." -> ".$options['display_entries']. " $start -> $count, $total");
     $revs = textblock_get_revision_list(
             $page_name, false, true,
             $start, $count);
@@ -237,7 +238,8 @@ function controller_textblock_delete_many($textblocks, $redirect) {
     foreach ($textblocks as $name) {
         if (!is_page_name($name)) {
             ++$bad_page_names;
-        } else if (identity_can("textblock-delete", textblock_get_revision($name))) {
+        } else if (identity_can("textblock-delete",
+                                textblock_get_revision($name))) {
             $deleted += textblock_delete($name);
         } else {
             ++$not_deleted_because_of_permision;
@@ -277,7 +279,8 @@ function controller_textblock_delete_revision($page = null, $rev_num = null) {
         redirect(url_home());
     }
 
-    $revision = textblock_get_revision($page, $rev_num == $total_revs ? null : $rev_num);
+    $revision = textblock_get_revision(
+        $page, $rev_num == $total_revs ? null : $rev_num);
     if ($total_revs == 1) {
         identity_require('textblock-delete', $revision);
         textblock_delete($page);
@@ -295,5 +298,3 @@ function controller_textblock_delete_revision($page = null, $rev_num = null) {
     flash("Revizia a fost stearsa");
     redirect(url_textblock_history($page));
 }
-
-?>
