A tumblelog CMS built on AJAX, PHP and MySQL.

functions.php 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. if(!defined('entry') || !entry) die('Not a valid page');
  3. /* ===========================
  4. gelato CMS - A PHP based tumblelog CMS
  5. development version
  6. http://www.gelatocms.com/
  7. gelato CMS is a free software licensed under the GPL 2.0
  8. Copyright (C) 2007 by Pedro Santana <pecesama at gmail dot com>
  9. =========================== */
  10. ?>
  11. <?php
  12. function version() {
  13. return "0.90";
  14. }
  15. function beginsWith($str, $sub) {
  16. return (strpos($str, $sub) === 0);
  17. }
  18. function endsWith($str, $sub) {
  19. return (substr($str, strlen($str) - strlen($sub)) == $sub);
  20. }
  21. function getFileName($fileUrl) {
  22. $path = explode('/', $fileUrl);
  23. return $path[count($path)-1];
  24. }
  25. function isMP3($fileUrl) {
  26. if (endsWith($fileUrl, ".mp3")) {
  27. return true;
  28. } else {
  29. return false;
  30. }
  31. }
  32. function getMP3File($remoteFileName) {
  33. if (isMP3($remoteFileName)) {
  34. if (getFile($remoteFileName)) {
  35. return true;
  36. } else {
  37. return false;
  38. }
  39. } elseif (isGoEar($remoteFileName)) {
  40. return true;
  41. } else {
  42. return false;
  43. }
  44. }
  45. function getGoEarCode($songUrl) {
  46. $pos = strpos($songUrl, "?v=");
  47. $lon = strlen($songUrl);
  48. $str = substr($songUrl, $pos + 3, $lon);
  49. return $str;
  50. }
  51. function isGoEar($songUrl) {
  52. if (beginsWith($songUrl, "http://www.goear.com/listen.php?v="))
  53. return true;
  54. else
  55. return false;
  56. }
  57. function isImageFile($photoUrl) {
  58. if (endsWith($photoUrl, ".jpg")) { return true; }
  59. elseif (endsWith($photoUrl, ".gif")) { return true; }
  60. elseif (endsWith($photoUrl, ".png")) { return true; }
  61. else { return false; }
  62. }
  63. function getPhotoFile($remoteFileName) {
  64. if (isImageFile($remoteFileName)) {
  65. if (getFile($remoteFileName)) {
  66. return true;
  67. } else {
  68. return false;
  69. }
  70. } else {
  71. return false;
  72. }
  73. }
  74. function getFile($remoteFileName) {
  75. $fileName = "../uploads/".sanitizeName(getFileName($remoteFileName));
  76. $str = _file_get_contents($remoteFileName);
  77. if (!$handle = fopen($fileName, 'w')) {
  78. //die("no se abrio de escritura");
  79. return false;
  80. }
  81. if (fwrite($handle, $str) === FALSE) {
  82. //die("no se escribio");
  83. return false;
  84. }
  85. fclose($handle);
  86. return true;
  87. }
  88. function isVimeoVideo($videoUrl) {
  89. if (beginsWith($videoUrl, "http://vimeo.com/clip:") || beginsWith($videoUrl, "http://www.vimeo.com/clip:"))
  90. return true;
  91. else
  92. return false;
  93. }
  94. function getVimeoVideoUrl($videoUrl) {
  95. return array_pop(explode("clip:",$videoUrl));
  96. }
  97. function isYoutubeVideo($videoUrl) {
  98. if (beginsWith($videoUrl, "http://youtube.com/watch?v=") || beginsWith($videoUrl, "http://www.youtube.com/watch?v="))
  99. return true;
  100. else
  101. return false;
  102. }
  103. function getYoutubeVideoUrl($videoUrl) {
  104. $params = explode("?v=", $videoUrl);
  105. $params2 = explode("&",$params[1]);
  106. return $params2[0];
  107. }
  108. function isVideo($url) {
  109. if (isYoutubeVideo($url)) { return true; }
  110. elseif (isVimeoVideo($url)) { return true; }
  111. else { return false; }
  112. }
  113. function sendMail($to, $title, $body, $from) {
  114. $rp = trim($from);
  115. $org = "gelato CMS";
  116. $mailer = "gelato CMS Mailer";
  117. $head = '';
  118. $head .= "Content-Type: text/html \r\n";
  119. $head .= "Date: ". date('r'). " \r\n";
  120. $head .= "Return-Path: $rp \r\n";
  121. $head .= "From: $from \r\n";
  122. $head .= "Sender: $from \r\n";
  123. $head .= "Reply-To: $from \r\n";
  124. $head .= "Organization: $org \r\n";
  125. $head .= "X-Sender: $from \r\n";
  126. $head .= "X-Priority: 3 \r\n";
  127. $head .= "X-Mailer: $mailer \r\n";
  128. $body = str_replace("\r\n", "\n", $body);
  129. $body = str_replace("\n", "\r\n", $body);
  130. return @mail($to, $title, $body, $head);
  131. }
  132. function getThemes() {
  133. $themes_dir = "themes";
  134. $dirs = array();
  135. $path = getcwd();
  136. $dir = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$themes_dir : $path."/".$themes_dir;
  137. $dir = str_replace("admin\\", "", $dir);
  138. $dir = str_replace("admin/", "", $dir);
  139. $handle = opendir($dir);
  140. $i=0;
  141. while($filename = readdir($handle)) {
  142. if($filename != "." && $filename != "..") {
  143. $dirs[$i]=trim($filename);
  144. $i++;
  145. }
  146. }
  147. closedir($handle);
  148. return $dirs;
  149. }
  150. function sanitizeName($name) {
  151. $name = preg_replace('/[\'"]/', '', $name);
  152. $name = preg_replace('/[^a-zA-Z0-9]+/', '-', $name);
  153. $name = trim($name, '-');
  154. $name = strtolower($name);
  155. //HACK: We need to rework the regular expression to allow the dot
  156. $ext = substr($name, strlen($name)-3, strlen($name));
  157. $body = substr($name, 0, strlen($name)-4);
  158. $name = $body.".".$ext;
  159. return $name;
  160. }
  161. function _file_get_contents($path) {
  162. // Modified function from:
  163. // http://work.dokoku.net/Anieto2k/_file_get_contents.phps
  164. // http://www.anieto2k.com/2007/02/09/file_get_contents-y-dreamhost/
  165. if (!preg_match('/^http/i', $path)) {
  166. if ($fp = fopen($path, 'r')) {
  167. return fread($fp, 1024);
  168. } else {
  169. return false;
  170. }
  171. } else {
  172. if (extension_loaded('curl') && version_compare(get_curl_version(), '7.10.5', '>=')) {
  173. $ch = curl_init();
  174. $timeout = 5;
  175. curl_setopt ($ch, CURLOPT_URL, $path);
  176. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  177. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  178. $file_contents = curl_exec($ch);
  179. curl_close($ch);
  180. if (is_string($file_contents)) {
  181. return $file_contents;
  182. } else {
  183. return false;
  184. }
  185. } else {
  186. $data = parse_url($path);
  187. if (!$data['host'] || $data['scheme'] != "http") {
  188. return false;
  189. }
  190. $f = @fsockopen($data['host'], ($data['port']) ? $data['port'] : 80, $e1, $e2, 3);
  191. if (!$f) {
  192. return false;
  193. }
  194. $q = "GET " . $data['path'] . (isset($data['query'])?'?'.$data['query']:'') . " HTTP/1.1\r\n";
  195. $q .= "Host: " . $data['host'] . "\r\n";
  196. $q .= "Connection: close\r\n";
  197. $q .= "Referer: http://www.gelatocms.com/\r\n\r\n";
  198. $recv = "";
  199. fwrite($f, $q);
  200. while (!feof($f)) {
  201. $recv .= fread($f, 1024);
  202. }
  203. $request = $q;
  204. $response = substr($recv, 0, strpos($recv, "\r\n\r\n"));
  205. $body = substr($recv, strpos($recv, "\r\n\r\n") + 4);
  206. if (preg_match('/http\/1\\.[0|1] ([0-9]{3})/i', $response, $res)) {
  207. if ($res[1][0] != "2") {
  208. return false;
  209. }
  210. } else {
  211. return false;
  212. }
  213. if (preg_match('/transfer-encoding:\s*chunked/i', $response)) {
  214. $tmp_body = $body;
  215. $new = "";
  216. $exit = false;
  217. while (!$exit) {
  218. if (preg_match('/^([0-9a-f]+).*?\r\n/i', $tmp_body, $res)) {
  219. $len = hexdec($res[1]);
  220. if ($len == "0") {
  221. $exit = true;
  222. break;
  223. }
  224. $new .= substr($tmp_body, strlen($res[0]), $len);
  225. $tmp_body = substr($tmp_body, strlen($res[0]) + $len + strlen("\r\n"));
  226. } else {
  227. $exit = true;
  228. }
  229. }
  230. $body = $new;
  231. }
  232. return $body;
  233. }
  234. }
  235. }
  236. function get_curl_version() {
  237. $curl = 0;
  238. if (is_array(curl_version())) {
  239. $curl = curl_version();
  240. $curl = $curl['version'];
  241. } else {
  242. $curl = curl_version();
  243. $curl = explode(' ', $curl);
  244. $curl = explode('/', $curl[0]);
  245. $curl = $curl[1];
  246. }
  247. return $curl;
  248. }
  249. function transform_offset($offset){
  250. $sp = strpos($offset , ".")? explode("." , $offset) : false;
  251. if(is_array($sp)){
  252. $minutes = strval($sp[1]);
  253. $off_h = $sp[0]*3600;
  254. $off_m = (($minutes*60)/100)*60;
  255. $off = $off_h+$off_m;
  256. } else {
  257. $off = ($offset*3600);
  258. }
  259. return $off;
  260. }
  261. function getLangs() {
  262. $langs_dir = "languages";
  263. $dirs = array();
  264. $path = getcwd();
  265. $dir = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$langs_dir : $path."/".$langs_dir;
  266. $dir = str_replace("admin\\", "", $dir);
  267. $dir = str_replace("admin/", "", $dir);
  268. $i=0;
  269. $cls_lang_dir = @ dir($dir);
  270. while (($directory = $cls_lang_dir->read()) !== false) {
  271. if($directory != "." && $directory != "..") {
  272. $dir2 = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$langs_dir."\\".$directory : $path."/".$langs_dir."/".$directory;
  273. $dir2 = str_replace("admin\\", "", $dir2);
  274. $dir2 = str_replace("admin/", "", $dir2);
  275. if(is_dir($dir2)){
  276. $cls_lang_dir2 = @ dir($dir2);
  277. while (($directory2 = $cls_lang_dir2->read()) !== false) {
  278. if($directory2 != "." && $directory2 != "..") {
  279. if (preg_match('|^\.+$|', $directory2)){
  280. continue;
  281. }
  282. if (preg_match('|\.mo$|', $directory2)){
  283. if(!in_array($directory2,$dirs)){
  284. $dirs[$i]=trim($directory);
  285. $i++;
  286. }
  287. }
  288. }
  289. }
  290. }
  291. }
  292. }
  293. $dirs = array_unique($dirs);
  294. return $dirs;
  295. }
  296. function sql_escape($value) {
  297. if(get_magic_quotes_gpc()) {
  298. $value = stripslashes($value);
  299. }
  300. if( function_exists("mysql_real_escape_string")) {
  301. $value = mysql_real_escape_string($value);
  302. } else {
  303. $value = addslashes($value);
  304. }
  305. return $value;
  306. }
  307. ?>